From 9485f5475a1a8a534404021371805a5fa35be57c Mon Sep 17 00:00:00 2001 From: jst Date: Wed, 17 Dec 2014 11:10:53 +0100 Subject: [PATCH] Return input image if output dimensions equal input dimensions. --- resize.go | 6 ++++++ resize_test.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/resize.go b/resize.go index 286802a..f6d24bb 100644 --- a/resize.go +++ b/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) } diff --git a/resize_test.go b/resize_test.go index ef14c66..f49bad6 100644 --- a/resize_test.go +++ b/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++ {