forked from makiuchi-d/gozxing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_image_luminance_source_test.go
145 lines (125 loc) · 3.5 KB
/
go_image_luminance_source_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
138
139
140
141
142
143
144
145
package gozxing
import (
"image"
"image/color"
"testing"
)
type testImage struct {
rect image.Rectangle
}
func (this *testImage) ColorModel() color.Model {
return color.RGBAModel
}
func (this *testImage) Bounds() image.Rectangle {
return this.rect
}
func (this *testImage) At(x, y int) color.Color {
width := this.rect.Max.X - this.rect.Min.X
height := this.rect.Max.Y - this.rect.Min.Y
rgb := xy2rgb(x, y, width, height)
r := byte((rgb >> 16) & 0xff)
g := byte((rgb >> 8) & 0xff)
b := byte(rgb & 0xff)
return color.RGBA{r, g, b, 0xff}
}
func newTestImage(w, h int) *testImage {
return &testImage{image.Rect(0, 0, w, h)}
}
func TestNewBinaryBitmapFromImage(t *testing.T) {
img := newTestImage(15, 10)
bmp, e := NewBinaryBitmapFromImage(img)
if e != nil {
t.Fatalf("NewBinaryBitmapFromImage returns error: %v", e)
}
if w, h := bmp.GetWidth(), bmp.GetHeight(); w != 15 || h != 10 {
t.Fatalf("NewBinaryBitmapFromImage = %vx%v, expect 15x10", w, h)
}
}
func TestNewLuminanceSourceFromImage(t *testing.T) {
img := newTestImage(10, 10)
src := NewLuminanceSourceFromImage(img)
if _, ok := src.(*GoImageLuminanceSource); !ok {
t.Fatalf("NewLuminanceSourceFromImage must return *RGBLuminanceSource, %T", src)
}
matrix := src.GetMatrix()
for y := 0; y < 10; y++ {
for x := 0; x < 10; x++ {
expect := rgb2lumina(xy2rgb(x, y, 10, 10))
lumina := matrix[y*10+x]
if lumina != expect {
t.Fatalf("matrix[%v,%v] = %v, expect %v", x, y, lumina, expect)
}
}
}
}
func TestGoImageLuminanceSource_Crop(t *testing.T) {
img := newTestImage(20, 20)
src := NewLuminanceSourceFromImage(img)
_, e := src.Crop(10, 10, 20, 20)
if e == nil {
t.Fatalf("Crop must be error")
}
cropped, e := src.Crop(5, 5, 10, 10)
if e != nil {
t.Fatalf("Crop returns error, %v", e)
}
matrix := cropped.GetMatrix()
for y := 0; y < 10; y++ {
for x := 0; x < 10; x++ {
expect := rgb2lumina(xy2rgb(x+5, y+5, 20, 20))
lumina := matrix[y*10+x]
if lumina != expect {
t.Fatalf("matrix[%v,%v] = %v, expect %v", x, y, lumina, expect)
}
}
}
}
func TestGoImageLuminanceSource_Invert(t *testing.T) {
img := newTestImage(10, 10)
src := NewLuminanceSourceFromImage(img)
inverted := src.Invert()
matrix := inverted.GetMatrix()
for y := 0; y < 10; y++ {
for x := 0; x < 10; x++ {
expect := 255 - rgb2lumina(xy2rgb(x, y, 10, 10))
lumina := matrix[y*10+x]
if lumina != expect {
t.Fatalf("matrix[%v,%v] = %v, expect %v", x, y, lumina, expect)
}
}
}
}
func TestGoImageLuminanceSource_RotateCounterClockwise(t *testing.T) {
img := newTestImage(20, 20)
src, _ := NewLuminanceSourceFromImage(img).Crop(5, 3, 10, 15)
if !src.IsRotateSupported() {
t.Fatalf("IsRotateSupported must be true")
}
rotated, e := src.RotateCounterClockwise()
if e != nil {
t.Fatalf("RotateCounterClockwise returns error, %v", e)
}
if w, h := rotated.GetWidth(), rotated.GetHeight(); w != 15 || h != 10 {
t.Fatalf("roated size = %v,%v, expect 15,10", w, h)
}
matrix := rotated.GetMatrix()
for y := 0; y < 10; y++ {
for x := 0; x < 15; x++ {
oldx := 5 + 10 - 1 - y
oldy := 3 + x
expect := rgb2lumina(xy2rgb(oldx, oldy, 20, 20))
lumina := matrix[y*15+x]
if lumina != expect {
t.Fatalf("matrix[%v,%v] = %v, expect %v", x, y, lumina, expect)
}
}
}
}
func TestGoImageLuminanceSource_RotateCounterClockwise45(t *testing.T) {
img := newTestImage(10, 10)
src := NewLuminanceSourceFromImage(img)
_, e := src.RotateCounterClockwise45()
if e == nil {
t.Fatalf("RotateCounterClockwise45 must be error")
}
}