Skip to content

Commit

Permalink
Fix median extend edge instead of wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonynsimon committed Aug 25, 2016
1 parent 2fbe6dc commit 831bd69
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 14 additions & 2 deletions effects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions effects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
Expand All @@ -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)))
}
}
}
Expand Down

0 comments on commit 831bd69

Please sign in to comment.