-
Notifications
You must be signed in to change notification settings - Fork 0
/
picam_test.go
128 lines (112 loc) · 2.09 KB
/
picam_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
package picam
import (
"fmt"
"image"
"image/color"
"testing"
)
func TestNew(t *testing.T) {
cam, err := New(640, 480, YUV)
if err != nil {
t.Fatal(err)
}
defer cam.Close()
}
func TestRead(t *testing.T) {
c := color.RGBA{}
tests := []struct {
format Format
want color.Color
}{
{YUV, color.YCbCrModel.Convert(c)},
{RGB, color.NRGBAModel.Convert(c)},
{Gray, color.GrayModel.Convert(c)},
}
for _, ts := range tests {
t.Run(fmt.Sprintf("%s", ts.format), func(t *testing.T) {
cam, err := New(640, 480, ts.format)
if err != nil {
t.Fatal(err)
}
defer cam.Close()
img := cam.Read()
got := img.ColorModel().Convert(c)
if got != ts.want {
t.Errorf("got: %T, want: %T", got, ts.want)
}
})
}
}
func TestReadUint8(t *testing.T) {
w, h := 640, 480
tests := []struct {
format Format
want int // byte size
}{
{YUV, w*h + w*h/2},
{RGB, w * h * 3},
{Gray, w * h},
}
for _, ts := range tests {
t.Run(fmt.Sprintf("%s", ts.format), func(t *testing.T) {
cam, err := New(640, 480, ts.format)
if err != nil {
t.Fatal(err)
}
defer cam.Close()
img := cam.ReadUint8()
got := len(img)
if got != ts.want {
t.Errorf("got: %d, want: %d", got, ts.want)
}
})
}
}
func TestReadUint8_Sizes(t *testing.T) {
tests := []struct {
format Format
width, height int
want int // byte size
}{
{
YUV,
320, 240,
320*240 + 320*240/2,
},
{
YUV,
100, 100,
128*112 + 128*112/2,
},
{
RGB,
320, 240,
320 * 240 * 3,
},
{
Gray,
320, 240,
320 * 240,
},
}
for _, ts := range tests {
t.Run(fmt.Sprintf("%s (%d,%d)", ts.format, ts.width, ts.height), func(t *testing.T) {
cam, err := New(ts.width, ts.height, ts.format)
if err != nil {
t.Fatal(err)
}
defer cam.Close()
raw := cam.ReadUint8()
got := len(raw)
if got != ts.want {
t.Errorf("got: %d, want: %d", got, ts.want)
}
img := cam.Read()
gotP := img.Bounds().Size()
wantP := image.Point{ts.width, ts.height}
if gotP != wantP {
t.Errorf("got: %+v, want: %+v", gotP, wantP)
}
})
}
}