瀏覽代碼

Return input image if output dimensions equal input dimensions.

jst 11 年之前
父節點
當前提交
9485f5475a
共有 2 個文件被更改,包括 21 次插入0 次删除
  1. 6 0
      resize.go
  2. 15 0
      resize_test.go

+ 6 - 0
resize.go

@@ -86,6 +86,12 @@ func Resize(width, height uint, img image.Image, interp InterpolationFunction) i
 	if height == 0 {
 		height = uint(0.7 + float64(img.Bounds().Dy())/scaleY)
 	}
+
+	// Trivial case: return input image
+	if int(width) == img.Bounds().Dx() && int(height) == img.Bounds().Dy() {
+		return img
+	}
+
 	if interp == NearestNeighbor {
 		return resizeNearest(width, height, scaleX, scaleY, img, interp)
 	}

+ 15 - 0
resize_test.go

@@ -70,6 +70,21 @@ func Test_Bounds(t *testing.T) {
 	out.At(0, 0)
 }
 
+func Test_SameSizeReturnsOriginal(t *testing.T) {
+	img := image.NewRGBA(image.Rect(0, 0, 10, 10))
+	out := Resize(0, 0, img, Lanczos2)
+
+	if img != out {
+		t.Fail()
+	}
+
+	out = Resize(10, 10, img, Lanczos2)
+
+	if img != out {
+		t.Fail()
+	}
+}
+
 func Benchmark_BigResizeLanczos3(b *testing.B) {
 	var m image.Image
 	for i := 0; i < b.N; i++ {