diff --git a/converter.go b/converter.go index bab59ef..df35cea 100644 --- a/converter.go +++ b/converter.go @@ -41,7 +41,7 @@ func clampUint16(in int64) uint16 { } func resizeGeneric(in image.Image, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Bounds().Dx(), in.Bounds().Dy()) newBounds := out.Bounds() for x := newBounds.Min.X; x < newBounds.Max.X; x++ { @@ -89,7 +89,7 @@ func resizeGeneric(in image.Image, out *image.RGBA64, scale float64, coeffs []in } func resizeRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []int16, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Rect.Dx(), in.Rect.Dy()) newBounds := out.Bounds() minX := oldBounds.Min.X * 4 maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 4 @@ -131,7 +131,7 @@ func resizeRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []int16, } func resizeRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Rect.Dx(), in.Rect.Dy()) newBounds := out.Bounds() minX := oldBounds.Min.X * 8 maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 8 @@ -181,7 +181,7 @@ func resizeRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs []i } func resizeGray(in *image.Gray, out *image.Gray, scale float64, coeffs []int16, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Rect.Dx(), in.Rect.Dy()) newBounds := out.Bounds() minX := oldBounds.Min.X maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) @@ -217,7 +217,7 @@ func resizeGray(in *image.Gray, out *image.Gray, scale float64, coeffs []int16, } func resizeGray16(in *image.Gray16, out *image.Gray16, scale float64, coeffs []int32, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Rect.Dx(), in.Rect.Dy()) newBounds := out.Bounds() minX := oldBounds.Min.X * 2 maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 2 @@ -255,7 +255,7 @@ func resizeGray16(in *image.Gray16, out *image.Gray16, scale float64, coeffs []i } func resizeYCbCr(in *ycc, out *ycc, scale float64, coeffs []int16, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Rect.Dx(), in.Rect.Dy()) newBounds := out.Bounds() minX := oldBounds.Min.X * 3 maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 3 @@ -295,7 +295,7 @@ func resizeYCbCr(in *ycc, out *ycc, scale float64, coeffs []int16, offset []int, } func nearestYCbCr(in *ycc, out *ycc, scale float64, coeffs []bool, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Rect.Dx(), in.Rect.Dy()) newBounds := out.Bounds() minX := oldBounds.Min.X * 3 maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 3 diff --git a/nearest.go b/nearest.go index 6ff22f6..69421f1 100644 --- a/nearest.go +++ b/nearest.go @@ -35,7 +35,7 @@ func floatToUint16(x float32) uint16 { } func nearestGeneric(in image.Image, out *image.RGBA64, scale float64, coeffs []bool, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Bounds().Dx(), in.Bounds().Dy()) newBounds := out.Bounds() for x := newBounds.Min.X; x < newBounds.Max.X; x++ { @@ -82,7 +82,7 @@ func nearestGeneric(in image.Image, out *image.RGBA64, scale float64, coeffs []b } func nearestRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []bool, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Rect.Dx(), in.Rect.Dy()) newBounds := out.Bounds() minX := oldBounds.Min.X * 4 maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 4 @@ -123,7 +123,7 @@ func nearestRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []bool, } func nearestRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs []bool, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Rect.Dx(), in.Rect.Dy()) newBounds := out.Bounds() minX := oldBounds.Min.X * 8 maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 8 @@ -172,7 +172,7 @@ func nearestRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs [] } func nearestGray(in *image.Gray, out *image.Gray, scale float64, coeffs []bool, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Rect.Dx(), in.Rect.Dy()) newBounds := out.Bounds() minX := oldBounds.Min.X maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) @@ -207,7 +207,7 @@ func nearestGray(in *image.Gray, out *image.Gray, scale float64, coeffs []bool, } func nearestGray16(in *image.Gray16, out *image.Gray16, scale float64, coeffs []bool, offset []int, filterLength int) { - oldBounds := in.Bounds() + oldBounds := image.Rect(0, 0, in.Rect.Dx(), in.Rect.Dy()) newBounds := out.Bounds() minX := oldBounds.Min.X * 2 maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 2 diff --git a/resize_test.go b/resize_test.go index 283b89c..41f8f46 100644 --- a/resize_test.go +++ b/resize_test.go @@ -3,14 +3,16 @@ package resize import ( "image" "image/color" - "runtime" + "image/png" + "os" + //"runtime" "testing" ) var img = image.NewGray16(image.Rect(0, 0, 3, 3)) func init() { - runtime.GOMAXPROCS(runtime.NumCPU()) + //runtime.GOMAXPROCS(runtime.NumCPU()) img.Set(1, 1, color.White) } @@ -64,6 +66,12 @@ func Test_SameColor(t *testing.T) { } } +func Test_Bounds(t *testing.T) { + img := image.NewRGBA(image.Rect(20, 10, 200, 99)) + out := Resize(80, 80, img, Lanczos2) + out.At(0, 0) +} + func Benchmark_BigResizeLanczos3(b *testing.B) { var m image.Image for i := 0; i < b.N; i++ { @@ -117,3 +125,21 @@ func Benchmark_LargeJpegThumbLanczos2(b *testing.B) { func Benchmark_LargeJpegThumbLanczos3(b *testing.B) { jpegThumb(b, Lanczos3) } + +func Benchmark_LargeN(b *testing.B) { + file, _ := os.Open("M94A0467.png") + defer file.Close() + img, _ := png.Decode(file) + var output image.Image + + b.ResetTimer() + for i := 0; i < b.N; i++ { + output = Resize(900, 0, img, Bicubic) + } + b.StopTimer() + + output.At(0, 0) + outPng, _ := os.Create("out.png") + defer outPng.Close() + png.Encode(outPng, output) +} diff --git a/ycc.go b/ycc.go index db6ffe2..1041599 100644 --- a/ycc.go +++ b/ycc.go @@ -38,7 +38,7 @@ type ycc struct { // PixOffset returns the index of the first element of Pix that corresponds to // the pixel at (x, y). func (p *ycc) PixOffset(x, y int) int { - return (y * p.Stride) + (x * 3) + return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3 } func (p *ycc) Bounds() image.Rectangle { @@ -159,8 +159,9 @@ func (p *ycc) YCbCr() *image.YCbCr { // imageYCbCrToYCC converts a YCbCr image to a ycc image for resizing. func imageYCbCrToYCC(in *image.YCbCr) *ycc { w, h := in.Rect.Dx(), in.Rect.Dy() + r := image.Rect(0, 0, w, h) buf := make([]uint8, 3*w*h) - p := ycc{Pix: buf, Stride: 3 * w, Rect: in.Rect, SubsampleRatio: in.SubsampleRatio} + p := ycc{Pix: buf, Stride: 3 * w, Rect: r, SubsampleRatio: in.SubsampleRatio} var off int switch in.SubsampleRatio { diff --git a/ycc_test.go b/ycc_test.go index 5128173..54d53d1 100644 --- a/ycc_test.go +++ b/ycc_test.go @@ -18,6 +18,7 @@ package resize import ( "image" + "image/color" "testing" ) @@ -119,3 +120,95 @@ func TestConvertYCbCr(t *testing.T) { } } } + +func TestYCbCr(t *testing.T) { + rects := []image.Rectangle{ + image.Rect(0, 0, 16, 16), + image.Rect(1, 0, 16, 16), + image.Rect(0, 1, 16, 16), + image.Rect(1, 1, 16, 16), + image.Rect(1, 1, 15, 16), + image.Rect(1, 1, 16, 15), + image.Rect(1, 1, 15, 15), + image.Rect(2, 3, 14, 15), + image.Rect(7, 0, 7, 16), + image.Rect(0, 8, 16, 8), + image.Rect(0, 0, 10, 11), + image.Rect(5, 6, 16, 16), + image.Rect(7, 7, 8, 8), + image.Rect(7, 8, 8, 9), + image.Rect(8, 7, 9, 8), + image.Rect(8, 8, 9, 9), + image.Rect(7, 7, 17, 17), + image.Rect(8, 8, 17, 17), + image.Rect(9, 9, 17, 17), + image.Rect(10, 10, 17, 17), + } + subsampleRatios := []image.YCbCrSubsampleRatio{ + image.YCbCrSubsampleRatio444, + image.YCbCrSubsampleRatio422, + image.YCbCrSubsampleRatio420, + image.YCbCrSubsampleRatio440, + } + deltas := []image.Point{ + image.Pt(0, 0), + image.Pt(1000, 1001), + image.Pt(5001, -400), + image.Pt(-701, -801), + } + for _, r := range rects { + for _, subsampleRatio := range subsampleRatios { + for _, delta := range deltas { + testYCbCr(t, r, subsampleRatio, delta) + } + } + if testing.Short() { + break + } + } +} + +func testYCbCr(t *testing.T, r image.Rectangle, subsampleRatio image.YCbCrSubsampleRatio, delta image.Point) { + // Create a YCbCr image m, whose bounds are r translated by (delta.X, delta.Y). + r1 := r.Add(delta) + img := image.NewYCbCr(r1, subsampleRatio) + + // Initialize img's pixels. For 422 and 420 subsampling, some of the Cb and Cr elements + // will be set multiple times. That's OK. We just want to avoid a uniform image. + for y := r1.Min.Y; y < r1.Max.Y; y++ { + for x := r1.Min.X; x < r1.Max.X; x++ { + yi := img.YOffset(x, y) + ci := img.COffset(x, y) + img.Y[yi] = uint8(16*y + x) + img.Cb[ci] = uint8(y + 16*x) + img.Cr[ci] = uint8(y + 16*x) + } + } + + m := imageYCbCrToYCC(img) + + // Make various sub-images of m. + for y0 := delta.Y + 3; y0 < delta.Y+7; y0++ { + for y1 := delta.Y + 8; y1 < delta.Y+13; y1++ { + for x0 := delta.X + 3; x0 < delta.X+7; x0++ { + for x1 := delta.X + 8; x1 < delta.X+13; x1++ { + subRect := image.Rect(x0, y0, x1, y1) + sub := m.SubImage(subRect).(*ycc) + + // For each point in the sub-image's bounds, check that m.At(x, y) equals sub.At(x, y). + for y := sub.Rect.Min.Y; y < sub.Rect.Max.Y; y++ { + for x := sub.Rect.Min.X; x < sub.Rect.Max.X; x++ { + color0 := m.At(x, y).(color.YCbCr) + color1 := sub.At(x, y).(color.YCbCr) + if color0 != color1 { + t.Errorf("r=%v, subsampleRatio=%v, delta=%v, x=%d, y=%d, color0=%v, color1=%v", + r, subsampleRatio, delta, x, y, color0, color1) + return + } + } + } + } + } + } + } +}