diff --git a/effects.go b/effects.go index 17089e1..19dbc31 100644 --- a/effects.go +++ b/effects.go @@ -138,8 +138,20 @@ func Median(img image.Image, size int) *image.RGBA { i := 0 for ky := 0; ky < size; ky++ { for kx := 0; kx < size; kx++ { - ix := (x - size/2 + kx + w) % (w) - iy := (y - size/2 + ky + h) % h + ix := x - size/2 + kx + iy := y - size/2 + ky + + if ix < 0 { + ix = 0 + } else if ix >= w { + ix = w - 1 + } + + if iy < 0 { + iy = 0 + } else if iy >= h { + iy = h - 1 + } ipos := iy*dst.Stride + ix*4 neighbors[i] = color.RGBA{ diff --git a/effects_test.go b/effects_test.go index 84ff84e..e9073d0 100644 --- a/effects_test.go +++ b/effects_test.go @@ -341,9 +341,9 @@ func TestMedian(t *testing.T) { Rect: image.Rect(0, 0, 3, 3), Stride: 3 * 4, Pix: []uint8{ - 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, - 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, - 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, + 0xFF, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0x0, 0xFF, + 0xFF, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0x0, 0xFF, + 0x0, 0xFF, 0x0, 0xFF, 0xFF, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0x0, 0xFF, }, }, }, @@ -352,7 +352,7 @@ func TestMedian(t *testing.T) { for _, c := range cases { actual := Median(c.value, c.size) if !rgbaImageEqual(actual, c.expected) { - t.Error(testFailMessage("Sobel", c.expected, actual)) + t.Error(testFailMessage("Sobel", formatImageString(c.expected), formatImageString(actual))) } } }