-
Notifications
You must be signed in to change notification settings - Fork 4
/
image_test.go
126 lines (104 loc) · 3.45 KB
/
image_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2017 Whit Marbut. All rights reserved.
// License information may be found in the LICENSE file.
package epdfuse
import (
"bytes"
"image"
"image/png"
"testing"
)
func TestDetectScalePlacePlan(t *testing.T) {
fuse1 := NewEpdFuse()
fuse2 := NewCustomEpdFuse("./", 300, 100)
fuse3 := NewCustomEpdFuse("./", 300, 96)
fuse4 := NewCustomEpdFuse("./", 100, 96)
img := getTestImage()
var plan ScalePlan
plan = fuse1.detectScalePlacePlan(img)
if plan != SCALE_NO {
t.Errorf("Error detecting scale plan, expected %d, but got %d", SCALE_NO, plan)
}
plan = fuse2.detectScalePlacePlan(img)
if plan != SCALE_UP {
t.Errorf("Error detecting scale plan, expected %d, but got %d", SCALE_UP, plan)
}
plan = fuse3.detectScalePlacePlan(img)
if plan != SCALE_PLACE_ONLY {
t.Errorf("Error detecting scale plan, expected %d, but got %d", SCALE_PLACE_ONLY, plan)
}
plan = fuse4.detectScalePlacePlan(img)
if plan != SCALE_DOWN {
t.Errorf("Error detecting scale plan, expected %d, but got %d", SCALE_DOWN, plan)
}
}
func TestContrainingAxis(t *testing.T) {
fuse1 := NewEpdFuse() //2.083 r
fuse2 := NewCustomEpdFuse("./", 100, 96) //1.041 r
fuse3 := NewCustomEpdFuse("./", 300, 96) //3.125 r
img := getTestImage() //2.083 r
var axis Axis
axis = fuse1.constrainingAxis(img)
if axis != AXIS_Y {
t.Errorf("Error detecting constraining axis. Expected %d, but got %d", AXIS_Y, axis)
}
axis = fuse2.constrainingAxis(img)
if axis != AXIS_X {
t.Errorf("Error detecting constraining axis. Expected %d, but got %d", AXIS_X, axis)
}
axis = fuse3.constrainingAxis(img)
if axis != AXIS_Y {
t.Errorf("Error detecting constraining axis. Expected %d, but got %d", AXIS_Y, axis)
}
}
func TestScaleFactor(t *testing.T) {
fuse1 := NewEpdFuse() //2.083 r 200x96
fuse2 := NewCustomEpdFuse("./", 100, 48)
img := getTestImage() //200 x 96
var factor float64
factor = fuse1.scaleFactor(img, AXIS_X)
if factor != 1.0 {
t.Errorf("Error calculating scale factor. Expected %f, but got %f", float64(1), factor)
}
factor = fuse1.scaleFactor(img, AXIS_Y)
if factor != 1.0 {
t.Errorf("Error calculating scale factor. Expected %f, but got %f", float64(1), factor)
}
factor = fuse2.scaleFactor(img, AXIS_X)
if factor != 0.5 {
t.Errorf("Error calculating scale factor. Expected %f, but got %f", float64(0.5), factor)
}
factor = fuse2.scaleFactor(img, AXIS_Y)
if factor != 0.5 {
t.Errorf("Error calculating scale factor. Expected %f, but got %f", float64(0.5), factor)
}
}
func TestScaleDown(t *testing.T) {
//fuse1 := NewEpdFuse() //2.083 r 200x96
fuse2 := NewCustomEpdFuse("./", 100, 48)
img := getTestImage() //200 x 96
var testImg image.Image
testImg = fuse2.scale(img)
if testImg == nil || testImg.Bounds().Dx() != 100 || testImg.Bounds().Dy() != 48 {
t.Errorf("Error scaling image down. Expected 100x48, but got %dx%d", testImg.Bounds().Dx(), testImg.Bounds().Dy())
}
}
func TestScaleUp(t *testing.T) {
fuse2 := NewCustomEpdFuse("./", 400, 192)
img := getTestImage() //200 x 96
var testImg image.Image
testImg = fuse2.scale(img)
if testImg == nil || testImg.Bounds().Dx() != 400 || testImg.Bounds().Dy() != 192 {
t.Errorf("Error scaling image down. Expected 100x48, but got %dx%d", testImg.Bounds().Dx(), testImg.Bounds().Dy())
}
}
func getTestImage() image.Image {
imgByt, err := Asset("test.png")
if err != nil {
panic(err)
}
img, err := png.Decode(bytes.NewReader(imgByt))
if err != nil {
panic(err)
}
return img
}