Refactoring
This commit is contained in:
parent
494d8de4e5
commit
d1bef9bdb6
47
resize.go
47
resize.go
|
@ -30,16 +30,6 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Trans2 is a 2-dimensional linear transformation.
|
|
||||||
type Trans2 [6]float32
|
|
||||||
|
|
||||||
// Apply the transformation to a point (x,y).
|
|
||||||
func (t *Trans2) Eval(x, y float32) (u, v float32) {
|
|
||||||
u = t[0]*x + t[1]*y + t[2]
|
|
||||||
v = t[3]*x + t[4]*y + t[5]
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter can interpolate at points (x,y)
|
// Filter can interpolate at points (x,y)
|
||||||
type Filter interface {
|
type Filter interface {
|
||||||
Interpolate(u float32, y int) color.RGBA64
|
Interpolate(u float32, y int) color.RGBA64
|
||||||
|
@ -60,15 +50,11 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i
|
||||||
oldBounds := img.Bounds()
|
oldBounds := img.Bounds()
|
||||||
oldWidth := float32(oldBounds.Dx())
|
oldWidth := float32(oldBounds.Dx())
|
||||||
oldHeight := float32(oldBounds.Dy())
|
oldHeight := float32(oldBounds.Dy())
|
||||||
|
|
||||||
scaleX, scaleY := calcFactors(width, height, oldWidth, oldHeight)
|
scaleX, scaleY := calcFactors(width, height, oldWidth, oldHeight)
|
||||||
t := Trans2{scaleX, 0, float32(oldBounds.Min.X), 0, scaleY, float32(oldBounds.Min.Y)}
|
|
||||||
|
|
||||||
//resizedImg := image.NewRGBA64(image.Rect(0, 0, int(0.7+oldWidth/scaleX), int(0.7+oldHeight/scaleY)))
|
tempImg := image.NewRGBA64(image.Rect(0, 0, oldBounds.Dy(), int(0.7+oldWidth/scaleX)))
|
||||||
resizedImg := image.NewRGBA64(image.Rect(0, 0, oldBounds.Dy(), int(0.7+oldWidth/scaleX)))
|
b := tempImg.Bounds()
|
||||||
b := resizedImg.Bounds()
|
adjust := 0.5 * ((oldWidth-1.0)/scaleX - float32(b.Dy()-1))
|
||||||
adjustX := 0.5 * ((oldWidth-1.0)/scaleX - float32(b.Dy()-1))
|
|
||||||
//adjustY := 0.5 * ((oldHeight-1.0)/scaleY - float32(b.Dy()-1))
|
|
||||||
|
|
||||||
n := numJobs(b.Dy())
|
n := numJobs(b.Dy())
|
||||||
c := make(chan int, n)
|
c := make(chan int, n)
|
||||||
|
@ -79,19 +65,19 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i
|
||||||
var color color.RGBA64
|
var color color.RGBA64
|
||||||
for y := b.Min.X; y < b.Max.X; y++ {
|
for y := b.Min.X; y < b.Max.X; y++ {
|
||||||
for x := b.Min.Y; x < b.Max.Y; x++ {
|
for x := b.Min.Y; x < b.Max.Y; x++ {
|
||||||
u = t[0]*(float32(x)+adjustX) + t[2]
|
u = scaleX*(float32(x)+adjust) + float32(oldBounds.Min.X)
|
||||||
|
|
||||||
color = filter.Interpolate(u, y)
|
color = filter.Interpolate(u, y)
|
||||||
|
|
||||||
i := resizedImg.PixOffset(y, x)
|
i := tempImg.PixOffset(y, x)
|
||||||
resizedImg.Pix[i+0] = uint8(color.R >> 8)
|
tempImg.Pix[i+0] = uint8(color.R >> 8)
|
||||||
resizedImg.Pix[i+1] = uint8(color.R)
|
tempImg.Pix[i+1] = uint8(color.R)
|
||||||
resizedImg.Pix[i+2] = uint8(color.G >> 8)
|
tempImg.Pix[i+2] = uint8(color.G >> 8)
|
||||||
resizedImg.Pix[i+3] = uint8(color.G)
|
tempImg.Pix[i+3] = uint8(color.G)
|
||||||
resizedImg.Pix[i+4] = uint8(color.B >> 8)
|
tempImg.Pix[i+4] = uint8(color.B >> 8)
|
||||||
resizedImg.Pix[i+5] = uint8(color.B)
|
tempImg.Pix[i+5] = uint8(color.B)
|
||||||
resizedImg.Pix[i+6] = uint8(color.A >> 8)
|
tempImg.Pix[i+6] = uint8(color.A >> 8)
|
||||||
resizedImg.Pix[i+7] = uint8(color.A)
|
tempImg.Pix[i+7] = uint8(color.A)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c <- 1
|
c <- 1
|
||||||
|
@ -104,16 +90,16 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i
|
||||||
|
|
||||||
resultImg := image.NewRGBA64(image.Rect(0, 0, int(0.7+oldWidth/scaleX), int(0.7+oldHeight/scaleY)))
|
resultImg := image.NewRGBA64(image.Rect(0, 0, int(0.7+oldWidth/scaleX), int(0.7+oldHeight/scaleY)))
|
||||||
b = resultImg.Bounds()
|
b = resultImg.Bounds()
|
||||||
adjustX = 0.5 * ((oldWidth-1.0)/scaleX - float32(b.Dx()-1))
|
adjust = 0.5 * ((oldWidth-1.0)/scaleY - float32(b.Dx()-1))
|
||||||
|
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
go func(b image.Rectangle, c chan int) {
|
go func(b image.Rectangle, c chan int) {
|
||||||
filter := interp(resizedImg, float32(clampFactor(scaleY)))
|
filter := interp(tempImg, float32(clampFactor(scaleY)))
|
||||||
var u float32
|
var u float32
|
||||||
var color color.RGBA64
|
var color color.RGBA64
|
||||||
for y := b.Min.X; y < b.Max.X; y++ {
|
for y := b.Min.X; y < b.Max.X; y++ {
|
||||||
for x := b.Min.Y; x < b.Max.Y; x++ {
|
for x := b.Min.Y; x < b.Max.Y; x++ {
|
||||||
u = t[4]*(float32(x)+adjustX) + t[5]
|
u = scaleY*(float32(x)+adjust) + float32(oldBounds.Min.Y)
|
||||||
|
|
||||||
color = filter.Interpolate(u, y)
|
color = filter.Interpolate(u, y)
|
||||||
|
|
||||||
|
@ -130,7 +116,6 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i
|
||||||
}
|
}
|
||||||
c <- 1
|
c <- 1
|
||||||
}(image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n), c)
|
}(image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n), c)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user