diff --git a/transform/filters.go b/transform/filters.go index 85b512d..1cb8aa9 100644 --- a/transform/filters.go +++ b/transform/filters.go @@ -19,7 +19,7 @@ type ResampleFilter struct { // NearestNeighbor resampling filter assigns to each point the sample point nearest to it. var NearestNeighbor ResampleFilter -// Box resampling filter, only let pass values in the x < 0.5 range from sample. +// Box resampling filter, only let pass values in the x <= 0.5 range from sample. // It produces similar results to the Nearest Neighbor method. var Box ResampleFilter @@ -47,7 +47,7 @@ func init() { Box = ResampleFilter{ Support: 0.5, Fn: func(x float64) float64 { - if math.Abs(x) < 0.5 { + if math.Abs(x) <= 0.5 { return 1 } return 0 diff --git a/transform/resize_test.go b/transform/resize_test.go index 710de99..d881d1b 100644 --- a/transform/resize_test.go +++ b/transform/resize_test.go @@ -318,6 +318,39 @@ func TestResizeBox(t *testing.T) { } } +func TestResizeBoxHalfwaySample(t *testing.T) { + cases := []struct { + name string + width int + height int + }{ + {name: "x1.5", width: 3, height: 3}, + {name: "x2.5", width: 5, height: 5}, + {name: "x3.5", width: 7, height: 7}, + } + + img := &image.RGBA{ + Stride: 2 * 4, + Rect: image.Rect(0, 0, 2, 2), + Pix: []uint8{ + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + }, + } + + for _, c := range cases { + expected := image.NewRGBA(image.Rect(0, 0, c.width, c.height)) + for i := range expected.Pix { + expected.Pix[i] = 0xFF + } + + actual := Resize(img, c.width, c.height, Box) + if !util.RGBAImageEqual(actual, expected) { + t.Errorf("%s: expected: %#v, actual: %#v", "ResizeBox "+c.name, util.RGBAToString(expected), util.RGBAToString(actual)) + } + } +} + func TestResizeLinear(t *testing.T) { cases := []struct { name string