From fdc4a64918c1c514b6a5e08efd10a9f37b2e614d Mon Sep 17 00:00:00 2001 From: nfnt Date: Fri, 3 Aug 2012 18:12:26 +0200 Subject: [PATCH] Tests added --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++---- resize.go | 7 +++++++ resize_test.go | 43 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0981320..694bd60 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,49 @@ resize.Resize(w int, h int, img image.Image, interp InterpolationFunction) image The provided interpolation functions are -- NearestNeighbor: Nearest-neighbor interpolation -- Bilinear: Bilinear interpolation -- Bicubic: Bicubic interpolation -- Lanczos3: Convolution with windowed Sinc function, a=3 +- NearestNeighbor: [Nearest-neighbor interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation) +- Bilinear: [Bilinear interpolation](http://en.wikipedia.org/wiki/Bilinear_interpolation) +- Bicubic: [Bicubic interpolation](http://en.wikipedia.org/wiki/Bicubic_interpolation) +- Lanczos3: [Lanczos resampling](http://en.wikipedia.org/wiki/Lanczos_resampling) with a=3 + +Sample usage: + +```go +package main + +import ( + "github.com/nfnt/resize" + "image/jpeg" + "os" +) + +func main() { + // open "test.jpg" + file, err := os.Open("test.jpg") + if err != nil { + return + } + + // decode jpeg into image.Image + img, err := jpeg.Decode(file) + if err != nil { + return + } + file.Close() + + // resize to width 1000 using Lanczos resampling + m := resize.Resize(1000, -1, img, resize.Lanczos3) + + out, err := os.Create("test_resized.jpg") + if err != nil { + return + } + defer out.Close() + + // write new image to file + jpeg.Encode(out, m, nil) +} +``` License ======= diff --git a/resize.go b/resize.go index 36fd2f6..99aa21b 100644 --- a/resize.go +++ b/resize.go @@ -47,6 +47,13 @@ func (t *Trans2) Eval(x, y float32) (u, v float32) { // Calculate scaling factors using old and new image dimensions. func calcFactors(w, h int, wo, ho float32) (sx, sy float32) { + if w <= 0 { + w = -1 + } + if h <= 0 { + h = -1 + } + if w == -1 { if h == -1 { sx = 1.0 diff --git a/resize_test.go b/resize_test.go index c40aed2..f270bce 100644 --- a/resize_test.go +++ b/resize_test.go @@ -6,13 +6,44 @@ import ( "testing" ) +var img = image.NewGray16(image.Rect(0, 0, 3, 3)) + func Test_Nearest(t *testing.T) { - img := image.NewGray16(image.Rect(0,0, 3,3)) - img.Set(1,1, color.White) - - m := Resize(6,-1, img, NearestNeighbor) - - if m.At(2,2) != m.At(3,3) { + img.Set(1, 1, color.White) + + m := Resize(6, -1, img, NearestNeighbor) + + if m.At(2, 2) != m.At(3, 3) { + t.Fail() + } +} + +func Test_Param1(t *testing.T) { + m := Resize(-1, -1, img, NearestNeighbor) + if m.Bounds() != img.Bounds() { + t.Fail() + } +} + +func Test_Param2(t *testing.T) { + m := Resize(-100, -1, img, NearestNeighbor) + if m.Bounds() != img.Bounds() { + t.Fail() + } +} + +func Test_Param3(t *testing.T) { + m := Resize(0, -1, img, NearestNeighbor) + if m.Bounds() != img.Bounds() { + t.Fail() + } +} + +func Test_ZeroImg(t *testing.T) { + zeroImg := image.NewGray16(image.Rect(0, 0, 0, 0)) + + m := Resize(-1, -1, zeroImg, NearestNeighbor) + if m.Bounds() != zeroImg.Bounds() { t.Fail() } }