test: extend carver test

This commit is contained in:
esimov 2021-11-09 19:09:32 +02:00
parent 1627bd9ab9
commit 8c49996fb2
2 changed files with 38 additions and 0 deletions

View File

@ -202,3 +202,40 @@ func TestCarver_AddSeam(t *testing.T) {
t.Errorf("Seam should have been added")
}
}
func Test_ComputeSeams(t *testing.T) {
img := image.NewNRGBA(image.Rect(0, 0, ImgWidth, ImgHeight))
bounds := img.Bounds()
// We choose to fill up the background with an uniform white color
// Afterwards we'll replace the colors in a single row with lower intensity ones.
draw.Draw(img, bounds, &image.Uniform{image.White}, image.ZP, draw.Src)
dx, dy := img.Bounds().Dx(), img.Bounds().Dy()
// Replace the pixels in row 5 with lower intensity colors.
for x := 0; x < dx; x++ {
img.Pix[(5*dx+x)*4+0] = 0xdd
img.Pix[(5*dx+x)*4+1] = 0xdd
img.Pix[(5*dx+x)*4+2] = 0xdd
img.Pix[(5*dx+x)*4+3] = 0xdd
}
c := NewCarver(dx, dy)
c.ComputeSeams(img, p)
otherThenZero := findNonZeroValue(c.Points)
if !otherThenZero {
t.Errorf("The seams computation should have been returned a slice of points with values other then zeros")
}
}
// findNonZeroValue utility function to check if the slice contains values other then zeros.
func findNonZeroValue(points []float64) bool {
var found = false
for i := 0; i < len(points); i++ {
if points[i] != 0 {
found = true
}
}
return found
}

View File

@ -64,6 +64,7 @@ func (c *Carver) StackBlur(img *image.NRGBA, radius uint32) *image.NRGBA {
rInSum, gInSum, bInSum, aInSum,
pr, pg, pb, pa uint32
)
// Limit the maximum blur radius to 255, otherwise it overflows the multable length
// and will panic with and index out of range error.
if int(radius) >= len(mulTable) {