Refactoring

This commit is contained in:
jst 2013-11-18 20:19:56 +01:00
parent 494d8de4e5
commit d1bef9bdb6
1 changed files with 16 additions and 31 deletions

View File

@ -30,16 +30,6 @@ import (
"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)
type Filter interface {
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()
oldWidth := float32(oldBounds.Dx())
oldHeight := float32(oldBounds.Dy())
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)))
resizedImg := image.NewRGBA64(image.Rect(0, 0, oldBounds.Dy(), int(0.7+oldWidth/scaleX)))
b := resizedImg.Bounds()
adjustX := 0.5 * ((oldWidth-1.0)/scaleX - float32(b.Dy()-1))
//adjustY := 0.5 * ((oldHeight-1.0)/scaleY - float32(b.Dy()-1))
tempImg := image.NewRGBA64(image.Rect(0, 0, oldBounds.Dy(), int(0.7+oldWidth/scaleX)))
b := tempImg.Bounds()
adjust := 0.5 * ((oldWidth-1.0)/scaleX - float32(b.Dy()-1))
n := numJobs(b.Dy())
c := make(chan int, n)
@ -79,19 +65,19 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i
var color color.RGBA64
for y := b.Min.X; y < b.Max.X; y++ {
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)
i := resizedImg.PixOffset(y, x)
resizedImg.Pix[i+0] = uint8(color.R >> 8)
resizedImg.Pix[i+1] = uint8(color.R)
resizedImg.Pix[i+2] = uint8(color.G >> 8)
resizedImg.Pix[i+3] = uint8(color.G)
resizedImg.Pix[i+4] = uint8(color.B >> 8)
resizedImg.Pix[i+5] = uint8(color.B)
resizedImg.Pix[i+6] = uint8(color.A >> 8)
resizedImg.Pix[i+7] = uint8(color.A)
i := tempImg.PixOffset(y, x)
tempImg.Pix[i+0] = uint8(color.R >> 8)
tempImg.Pix[i+1] = uint8(color.R)
tempImg.Pix[i+2] = uint8(color.G >> 8)
tempImg.Pix[i+3] = uint8(color.G)
tempImg.Pix[i+4] = uint8(color.B >> 8)
tempImg.Pix[i+5] = uint8(color.B)
tempImg.Pix[i+6] = uint8(color.A >> 8)
tempImg.Pix[i+7] = uint8(color.A)
}
}
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)))
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++ {
go func(b image.Rectangle, c chan int) {
filter := interp(resizedImg, float32(clampFactor(scaleY)))
filter := interp(tempImg, float32(clampFactor(scaleY)))
var u float32
var color color.RGBA64
for y := b.Min.X; y < b.Max.X; y++ {
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)
@ -130,7 +116,6 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i
}
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)
}
for i := 0; i < n; i++ {