-
Notifications
You must be signed in to change notification settings - Fork 11
/
imageorient_test.go
137 lines (117 loc) · 3.19 KB
/
imageorient_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
127
128
129
130
131
132
133
134
135
136
137
package imageorient
import (
"fmt"
"image"
"image/color"
_ "image/jpeg"
"os"
"strings"
"testing"
)
var testFiles = []struct {
path string
orientation int
}{
{"testdata/orientation_0.jpg", 0},
{"testdata/orientation_1.jpg", 1},
{"testdata/orientation_2.jpg", 2},
{"testdata/orientation_3.jpg", 3},
{"testdata/orientation_4.jpg", 4},
{"testdata/orientation_5.jpg", 5},
{"testdata/orientation_6.jpg", 6},
{"testdata/orientation_7.jpg", 7},
{"testdata/orientation_8.jpg", 8},
}
const (
testWidth = 50
testHeight = 70
)
func TestReadOrientation(t *testing.T) {
for _, tf := range testFiles {
f, err := os.Open(tf.path)
if err != nil {
t.Fatalf("os.Open(%q): %v", tf.path, err)
}
o := readOrientation(f)
if o != tf.orientation {
t.Fatalf("expected orientation=%d but got %d (%s)", tf.orientation, o, tf.path)
}
}
}
func TestDecodeConfig(t *testing.T) {
for _, tf := range testFiles {
f, err := os.Open(tf.path)
if err != nil {
t.Fatalf("Open(%q): %v", tf.path, err)
}
cfg, format, err := DecodeConfig(f)
if err != nil {
t.Fatalf("Decode(%q): %v", tf.path, err)
}
if cfg.Width != testWidth {
t.Fatalf("bad decoded width (%q): want %d got %d", tf.path, testWidth, cfg.Width)
}
if cfg.Height != testHeight {
t.Fatalf("bad decoded width (%q): want %d got %d", tf.path, testHeight, cfg.Height)
}
if format != "jpeg" {
t.Fatalf("bad decoded format (%q): %s", tf.path, format)
}
}
_, _, err := DecodeConfig(strings.NewReader("bad data"))
if err == nil {
t.Fatalf("expected error on bad data")
}
}
func TestDecode(t *testing.T) {
f, err := os.Open("testdata/orientation_0.jpg")
if err != nil {
t.Fatalf("os.Open(%q): %v", "testdata/orientation_0.jpg", err)
}
orig, _, err := image.Decode(f)
if err != nil {
t.Fatalf("image.Decode(%q): %v", "testdata/orientation_0.jpg", err)
}
for _, tf := range testFiles {
f, err := os.Open(tf.path)
if err != nil {
t.Fatalf("Open(%q): %v", tf.path, err)
}
img, format, err := Decode(f)
if err != nil {
t.Fatalf("Decode(%q): %v", tf.path, err)
}
if err := compareImages(img, orig); err != nil {
t.Fatalf("decoded image differs from orig (%q): %v", tf.path, err)
}
if img.Bounds().Dx() != testWidth {
t.Fatalf("bad decoded width (%q): want %d got %d", tf.path, testWidth, img.Bounds().Dx())
}
if img.Bounds().Dy() != testHeight {
t.Fatalf("bad decoded width (%q): want %d got %d", tf.path, testHeight, img.Bounds().Dy())
}
if format != "jpeg" {
t.Fatalf("bad decoded format (%q): %s", tf.path, format)
}
}
_, _, err = Decode(strings.NewReader("bad data"))
if err == nil {
t.Fatalf("expected error on bad data")
}
}
func compareImages(m1, m2 image.Image) error {
if m1.Bounds() != m2.Bounds() {
return fmt.Errorf("bounds not equal: %v vs %v", m1.Bounds(), m2.Bounds())
}
for x := m1.Bounds().Min.X; x < m1.Bounds().Max.X; x++ {
for y := m1.Bounds().Min.Y; y < m1.Bounds().Max.Y; y++ {
c1 := color.GrayModel.Convert(m1.At(x, y)).(color.Gray)
c2 := color.GrayModel.Convert(m2.At(x, y)).(color.Gray)
d := int(c1.Y) - int(c2.Y)
if d < -5 || d > 5 {
return fmt.Errorf("pixels at (%d, %d) not equal: %v vs %v", x, y, c1, c2)
}
}
}
return nil
}