浏览代码

Tests added

nfnt 14 年之前
父节点
当前提交
fdc4a64918
共有 3 个文件被更改,包括 87 次插入10 次删除
  1. 43 4
      README.md
  2. 7 0
      resize.go
  3. 37 6
      resize_test.go

+ 43 - 4
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
 =======

+ 7 - 0
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

+ 37 - 6
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()
 	}
 }