Browse Source

Dst and Src Image size checks added to Resize func

disintegration 13 years ago
parent
commit
2e77600be5
1 changed files with 21 additions and 1 deletions
  1. 21 1
      imaging.go

+ 21 - 1
imaging.go

@@ -289,6 +289,10 @@ func FlipV(img image.Image) draw.Image {
 
 // Simple image resize function. Will be removed later if not needed.
 func resizeNearest(img image.Image, dstW, dstH int) draw.Image {
+	if dstW <= 0 || dstH <= 0 {
+		return &image.RGBA{}
+	}
+
 	src := convertToRGBA(img)
 	dst := image.NewRGBA(image.Rect(0, 0, dstW, dstH))
 
@@ -298,6 +302,10 @@ func resizeNearest(img image.Image, dstW, dstH int) draw.Image {
 	srcMinX := srcBounds.Min.X
 	srcMinY := srcBounds.Min.Y
 
+	if srcW <= 0 || srcH <= 0 {
+		return &image.RGBA{}
+	}
+
 	dy := float64(srcH) / float64(dstH)
 	dx := float64(srcW) / float64(dstW)
 
@@ -318,7 +326,12 @@ func resizeNearest(img image.Image, dstW, dstH int) draw.Image {
 // Resizes image img to width=dstW and height=dstH.
 // Bilinear interpolation algorithm used at the moment. This may change later.
 func Resize(img image.Image, dstW, dstH int) draw.Image {
-	// TODO: check src and dst image bounds, fix small images resizing (w or h <= 2px)
+	if dstW <= 0 || dstH <= 0 {
+		return &image.RGBA{}
+	}
+	if dstW == 1 || dstH == 1 {
+		return resizeNearest(img, dstW, dstH)
+	}
 
 	src := convertToRGBA(img)
 	dst := image.NewRGBA(image.Rect(0, 0, dstW, dstH))
@@ -331,6 +344,13 @@ func Resize(img image.Image, dstW, dstH int) draw.Image {
 	srcMaxX := srcBounds.Max.X
 	srcMaxY := srcBounds.Max.Y
 
+	if srcW <= 0 || srcH <= 0 {
+		return &image.RGBA{}
+	}
+	if srcW == 1 || srcH == 1 {
+		return resizeNearest(img, dstW, dstH)
+	}
+
 	dy := float64(srcH-1) / float64(dstH-1)
 	dx := float64(srcW-1) / float64(dstW-1)