diff --git a/converter.go b/converter.go index f9c520d..6c2c48f 100644 --- a/converter.go +++ b/converter.go @@ -134,7 +134,7 @@ func resizeRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []int16, } } -func resizeNRGBA(in *image.NRGBA, out *image.RGBA, scale float64, coeffs []int16, offset []int, filterLength int) { +func resizeNRGBA(in *image.NRGBA, out *image.NRGBA, scale float64, coeffs []int16, offset []int, filterLength int) { newBounds := out.Bounds() maxX := in.Bounds().Dx() - 1 @@ -235,7 +235,7 @@ func resizeRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs []i } } -func resizeNRGBA64(in *image.NRGBA64, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) { +func resizeNRGBA64(in *image.NRGBA64, out *image.NRGBA64, scale float64, coeffs []int32, offset []int, filterLength int) { newBounds := out.Bounds() maxX := in.Bounds().Dx() - 1 diff --git a/resize.go b/resize.go index 0d7fbf6..fdee5e8 100644 --- a/resize.go +++ b/resize.go @@ -140,14 +140,14 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i return result case *image.NRGBA: // 8-bit precision - temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width))) - result := image.NewRGBA(image.Rect(0, 0, int(width), int(height))) + temp := image.NewNRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width))) + result := image.NewNRGBA(image.Rect(0, 0, int(width), int(height))) // horizontal filter, results in transposed temporary image coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), taps, blur, scaleX, kernel) wg.Add(cpus) for i := 0; i < cpus; i++ { - slice := makeSlice(temp, i, cpus).(*image.RGBA) + slice := makeSlice(temp, i, cpus).(*image.NRGBA) go func() { defer wg.Done() resizeNRGBA(input, slice, scaleX, coeffs, offset, filterLength) @@ -159,10 +159,10 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), taps, blur, scaleY, kernel) wg.Add(cpus) for i := 0; i < cpus; i++ { - slice := makeSlice(result, i, cpus).(*image.RGBA) + slice := makeSlice(result, i, cpus).(*image.NRGBA) go func() { defer wg.Done() - resizeRGBA(temp, slice, scaleY, coeffs, offset, filterLength) + resizeNRGBA(temp, slice, scaleY, coeffs, offset, filterLength) }() } wg.Wait() @@ -229,14 +229,14 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i return result case *image.NRGBA64: // 16-bit precision - temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width))) - result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height))) + temp := image.NewNRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width))) + result := image.NewNRGBA64(image.Rect(0, 0, int(width), int(height))) // horizontal filter, results in transposed temporary image coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), taps, blur, scaleX, kernel) wg.Add(cpus) for i := 0; i < cpus; i++ { - slice := makeSlice(temp, i, cpus).(*image.RGBA64) + slice := makeSlice(temp, i, cpus).(*image.NRGBA64) go func() { defer wg.Done() resizeNRGBA64(input, slice, scaleX, coeffs, offset, filterLength) @@ -248,10 +248,10 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), taps, blur, scaleY, kernel) wg.Add(cpus) for i := 0; i < cpus; i++ { - slice := makeSlice(result, i, cpus).(*image.RGBA64) + slice := makeSlice(result, i, cpus).(*image.NRGBA64) go func() { defer wg.Done() - resizeRGBA64(temp, slice, scaleY, coeffs, offset, filterLength) + resizeNRGBA64(temp, slice, scaleY, coeffs, offset, filterLength) }() } wg.Wait() diff --git a/resize_test.go b/resize_test.go index 178740e..aa5f33b 100644 --- a/resize_test.go +++ b/resize_test.go @@ -3,6 +3,7 @@ package resize import ( "image" "image/color" + "reflect" "runtime" "testing" ) @@ -88,7 +89,7 @@ func Test_SameColorWithNRGBA(t *testing.T) { out := Resize(10, 10, img, Lanczos3) for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ { for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ { - color := out.At(x, y).(color.RGBA) + color := out.At(x, y).(color.NRGBA) if color.R != 0x80 || color.G != 0x80 || color.B != 0x80 || color.A != 0xFF { t.Errorf("%+v", color) } @@ -124,7 +125,7 @@ func Test_SameColorWithNRGBA64(t *testing.T) { out := Resize(10, 10, img, Lanczos3) for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ { for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ { - color := out.At(x, y).(color.RGBA64) + color := out.At(x, y).(color.NRGBA64) if color.R != 0x8000 || color.G != 0x8000 || color.B != 0x8000 || color.A != 0xFFFF { t.Errorf("%+v", color) } @@ -189,6 +190,25 @@ func Test_SameSizeReturnsOriginal(t *testing.T) { } } +func Test_ResizesToSameType(t *testing.T) { + images := []image.Image{ + image.NewRGBA(image.Rect(0, 0, 10, 10)), + image.NewRGBA64(image.Rect(0, 0, 10, 10)), + image.NewNRGBA(image.Rect(0, 0, 10, 10)), + image.NewNRGBA64(image.Rect(0, 0, 10, 10)), + image.NewGray(image.Rect(0, 0, 10, 10)), + image.NewGray16(image.Rect(0, 0, 10, 10)), + image.NewYCbCr(image.Rect(0, 0, 10, 10), image.YCbCrSubsampleRatio422), + } + + for _, image := range images { + resized := Resize(20, 0, image, Lanczos2) + if !(reflect.TypeOf(resized) == reflect.TypeOf(image)) { + t.Fail() + } + } +} + func Test_PixelCoordinates(t *testing.T) { checkers := image.NewGray(image.Rect(0, 0, 4, 4)) checkers.Pix = []uint8{