forked from makiuchi-d/gozxing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgb_luminance_source_test.go
156 lines (138 loc) · 3.8 KB
/
rgb_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
146
147
148
149
150
151
152
153
154
155
156
package gozxing
import (
"reflect"
"testing"
)
func makeRGBLSource(width, height int) *RGBLuminanceSource {
pixels := make([]int, width*height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
pixels[y*width+x] = xy2rgb(x, y, width, height)
}
}
return NewRGBLuminanceSource(width, height, pixels).(*RGBLuminanceSource)
}
func xy2rgb(x, y, width, height int) int {
r := (255 * x * 2 / (width - 1)) & 0xff
g := (255 * (x + y) / (width + height - 2)) & 0xff
b := (255 * y * 2 / (height - 1)) & 0xff
return (r << 16) + (g << 8) + b
}
func rgb2lumina(rgb int) byte {
r := (rgb >> 16) & 0xff
g := (rgb >> 8) & 0xff
b := rgb & 0xff
return byte((r + 2*g + b) / 4)
}
func TestRGBLuminanceSource_GetRow(t *testing.T) {
src := makeRGBLSource(10, 10)
if _, e := src.GetRow(-1, nil); e == nil {
t.Fatalf("GetRow(-1) must be error")
}
if _, e := src.GetRow(10, nil); e == nil {
t.Fatalf("GetRow(-1) must be error")
}
row, e := src.GetRow(0, nil)
if e != nil {
t.Fatalf("GetRow(0) returns error, %v", e)
}
// r = (255 * w * 2 / 9) & 0xff => 0, 56,113,170,226, 27, 84,140,197,254
// g = (255 * w / 18) & 0xff => 0, 14, 28, 42, 56, 70, 85, 99,113,127
// b = 0
// (r + 2*g + b) 4
expect := []byte{0, 21, 42, 63, 84, 41, 63, 84, 105, 127}
if !reflect.DeepEqual(row, expect) {
t.Fatalf("GetRow(0) = %v, expect %v", row, expect)
}
row, e = src.GetRow(9, row)
if e != nil {
t.Fatalf("GetRow(0) returns error, %v", e)
}
// r = (255 * w * 2 / 9) & 0xff => 0, 56,113,170,226, 27, 84,140,197,254
// g = (255 * (w+9) / 18) & 0xff => 127,141,155,170,184,198,212,226,240,255
// b = 254
expect = []byte{127, 148, 169, 191, 212, 169, 190, 211, 232, 254}
if !reflect.DeepEqual(row, expect) {
t.Fatalf("GetRow(0) = %v, expect %v", row, expect)
}
}
func TestRGBLuminanceSource_GetMatrix(t *testing.T) {
src := makeRGBLSource(10, 10)
matrix := src.GetMatrix()
if len(matrix) != len(src.luminances) {
t.Fatalf("GetMatrix len=%d, expect %d", len(matrix), len(src.luminances))
}
for i := range matrix {
x := i % src.dataWidth
y := i / src.dataWidth
if exp := rgb2lumina(xy2rgb(x, y, 10, 10)); matrix[i] != exp {
t.Fatalf("GetMatrix matrix[%v] = %v, expect %v", i, matrix[i], exp)
}
}
if !src.IsCropSupported() {
t.Fatalf("IsCropSupported must be true")
}
if _, e := src.Crop(1, 0, 10, 10); e == nil {
t.Fatalf("Crop must be error")
}
if _, e := src.Crop(0, 0, 10, 11); e == nil {
t.Fatalf("Crop must be error")
}
crop, e := src.Crop(0, 2, 10, 6)
if e != nil {
t.Fatalf("Crop returns error, %v", e)
}
cmatrix := crop.GetMatrix()
if !reflect.DeepEqual(cmatrix, matrix[20:len(matrix)-20]) {
t.Fatalf("%v", crop)
}
crop, e = crop.Crop(2, 0, 6, 6)
if e != nil {
t.Fatalf("Crop returns error, %v", e)
}
cmatrix = crop.GetMatrix()
expect := []byte{
84, 105, 127, 84, 105, 126,
105, 127, 148, 105, 127, 148,
127, 148, 169, 126, 148, 169,
84, 105, 126, 84, 105, 126,
105, 127, 148, 105, 127, 148,
126, 148, 169, 126, 148, 169,
}
if !reflect.DeepEqual(cmatrix, expect) {
t.Fatalf("Cropped matrix = %v, expect %v", cmatrix, expect)
}
}
func TestRGBLuminanceSource_Invert(t *testing.T) {
src := makeRGBLSource(10, 10)
inv := src.Invert()
srcm := src.GetMatrix()
invm := inv.GetMatrix()
for y := 0; y < 10; y++ {
for x := 0; x < 10; x++ {
i := y*10 + x
exp := byte(255 - int(srcm[i]))
if invm[i] != exp {
t.Fatalf("Inverted matrix[%v] = %v, expect %v", i, invm[i], exp)
}
}
}
}
func TestRGBLuminanceSource_String(t *testing.T) {
src := makeRGBLSource(10, 10)
str := src.String()
expect := "" +
"####+##+++\n" +
"###++#+++.\n" +
"##++++++..\n" +
"#+++.++...\n" +
"+++..+... \n" +
"##++++++..\n" +
"#+++.++...\n" +
"+++..+... \n" +
"++...... \n" +
"+... .. \n"
if str != expect {
t.Fatalf("String:\n%vexpect:\n%v", str, expect)
}
}