|
@@ -0,0 +1,47 @@
|
|
1
|
+package resize
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "image"
|
|
5
|
+ "runtime"
|
|
6
|
+ "testing"
|
|
7
|
+)
|
|
8
|
+
|
|
9
|
+func init() {
|
|
10
|
+ runtime.GOMAXPROCS(runtime.NumCPU())
|
|
11
|
+}
|
|
12
|
+
|
|
13
|
+var thumbnailTests = []struct {
|
|
14
|
+ origWidth int
|
|
15
|
+ origHeight int
|
|
16
|
+ maxWidth uint
|
|
17
|
+ maxHeight uint
|
|
18
|
+ expectedWidth uint
|
|
19
|
+ expectedHeight uint
|
|
20
|
+}{
|
|
21
|
+ {5, 5, 10, 10, 5, 5},
|
|
22
|
+ {10, 10, 5, 5, 5, 5},
|
|
23
|
+ {10, 50, 10, 10, 2, 10},
|
|
24
|
+ {50, 10, 10, 10, 10, 2},
|
|
25
|
+ {50, 100, 60, 90, 45, 90},
|
|
26
|
+ {120, 100, 60, 90, 60, 50},
|
|
27
|
+ {200, 250, 200, 150, 120, 150},
|
|
28
|
+}
|
|
29
|
+
|
|
30
|
+func TestThumbnail(t *testing.T) {
|
|
31
|
+ for i, tt := range thumbnailTests {
|
|
32
|
+ img := image.NewGray16(image.Rect(0, 0, tt.origWidth, tt.origHeight))
|
|
33
|
+
|
|
34
|
+ outImg := Thumbnail(tt.maxWidth, tt.maxHeight, img, NearestNeighbor)
|
|
35
|
+
|
|
36
|
+ newWidth := uint(outImg.Bounds().Dx())
|
|
37
|
+ newHeight := uint(outImg.Bounds().Dy())
|
|
38
|
+ if newWidth != tt.expectedWidth ||
|
|
39
|
+ newHeight != tt.expectedHeight {
|
|
40
|
+ t.Errorf("%d. Thumbnail(%v, %v, img, NearestNeighbor) => "+
|
|
41
|
+ "width: %v, height: %v, want width: %v, height: %v",
|
|
42
|
+ i, tt.maxWidth, tt.maxHeight,
|
|
43
|
+ newWidth, newHeight, tt.expectedWidth, tt.expectedHeight,
|
|
44
|
+ )
|
|
45
|
+ }
|
|
46
|
+ }
|
|
47
|
+}
|