Don't resize inputs with zero width or height
Inputs having zero as both width and height were caught, but either one alone caused a panic. This adds a test showing the old failing behavior and adds a check for non-positive width/height. Since Resize doesn't have an error return value, the behavior in this case is to return the original image. Fixes https://github.com/nfnt/resize/issues/52
This commit is contained in:
parent
891127d8d1
commit
874f89dba4
|
@ -78,6 +78,7 @@ var blur = 1.0
|
||||||
// If one of the parameters width or height is set to 0, its size will be calculated so that
|
// If one of the parameters width or height is set to 0, its size will be calculated so that
|
||||||
// the aspect ratio is that of the originating image.
|
// the aspect ratio is that of the originating image.
|
||||||
// The resizing algorithm uses channels for parallel computation.
|
// The resizing algorithm uses channels for parallel computation.
|
||||||
|
// If the input image has width or height of 0, it is returned unchanged.
|
||||||
func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
|
func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
|
||||||
scaleX, scaleY := calcFactors(width, height, float64(img.Bounds().Dx()), float64(img.Bounds().Dy()))
|
scaleX, scaleY := calcFactors(width, height, float64(img.Bounds().Dx()), float64(img.Bounds().Dy()))
|
||||||
if width == 0 {
|
if width == 0 {
|
||||||
|
@ -92,6 +93,11 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Input image has no pixels
|
||||||
|
if img.Bounds().Dx() <= 0 || img.Bounds().Dy() <= 0 {
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
|
||||||
if interp == NearestNeighbor {
|
if interp == NearestNeighbor {
|
||||||
return resizeNearest(width, height, scaleX, scaleY, img, interp)
|
return resizeNearest(width, height, scaleX, scaleY, img, interp)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,20 @@ func Test_ZeroImg(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_HalfZeroImg(t *testing.T) {
|
||||||
|
zeroImg := image.NewGray16(image.Rect(0, 0, 0, 100))
|
||||||
|
|
||||||
|
m := Resize(0, 1, zeroImg, NearestNeighbor)
|
||||||
|
if m.Bounds() != zeroImg.Bounds() {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
m = Resize(1, 0, zeroImg, NearestNeighbor)
|
||||||
|
if m.Bounds() != zeroImg.Bounds() {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Test_CorrectResize(t *testing.T) {
|
func Test_CorrectResize(t *testing.T) {
|
||||||
zeroImg := image.NewGray16(image.Rect(0, 0, 256, 256))
|
zeroImg := image.NewGray16(image.Rect(0, 0, 256, 256))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user