Debian Codegraph @

fmt/yuyv/yuyv_test.go

raw
1 // Package v4l, a facade to the Video4Linux video capture interface
2 // Copyright (C) 2016 Zoltán Korándi <[email protected]>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 package yuyv
18
19 import (
20 "image"
21 "image/color"
22 "image/draw"
23 "testing"
24 )
25
26 var src = &Image{
27 Pix: []byte{
28 // +---+---+---+---+---+---+---+---+
29 // Y: | x | y | 0 | 0 |
30 // +---+---+---+---+---+---+---+---+
31 //
32 // +---+---+---+---+---+---+---+---+
33 // Cb: | x/2 | y | 0 | 0 | 1 |
34 // +---+---+---+---+---+---+---+---+
35 //
36 // +---+---+---+---+---+---+---+---+
37 // Cr: | x/2 | y | 1 | 1 | 1 |
38 // +---+---+---+---+---+---+---+---+
39 0x00, 0x01, 0x20, 0x07, 0x40, 0x41, 0x60, 0x47, 0x80, 0x81, 0xa0, 0x87, 0xc0, 0xc1, 0xe0, 0xc7, 0xff, 0xff, 0xff,
40 0x04, 0x09, 0x24, 0x0f, 0x44, 0x49, 0x64, 0x4f, 0x84, 0x89, 0xa4, 0x8f, 0xc4, 0xc9, 0xe4, 0xcf, 0xff, 0xff, 0xff,
41 0x08, 0x11, 0x28, 0x17, 0x48, 0x51, 0x68, 0x57, 0x88, 0x91, 0xa8, 0x97, 0xc8, 0xd1, 0xe8, 0xd7, 0xff, 0xff, 0xff,
42 0x0c, 0x19, 0x2c, 0x1f, 0x4c, 0x59, 0x6c, 0x5f, 0x8c, 0x99, 0xac, 0x9f, 0xcc, 0xd9, 0xec, 0xdf, 0xff, 0xff, 0xff,
43 0x10, 0x21, 0x30, 0x27, 0x50, 0x61, 0x70, 0x67, 0x90, 0xa1, 0xb0, 0xa7, 0xd0, 0xe1, 0xf0, 0xe7, 0xff, 0xff, 0xff,
44 0x14, 0x29, 0x34, 0x2f, 0x54, 0x69, 0x74, 0x6f, 0x94, 0xa9, 0xb4, 0xaf, 0xd4, 0xe9, 0xf4, 0xef, 0xff, 0xff, 0xff,
45 0x18, 0x31, 0x38, 0x37, 0x58, 0x71, 0x78, 0x77, 0x98, 0xb1, 0xb8, 0xb7, 0xd8, 0xf1, 0xf8, 0xf7, 0xff, 0xff, 0xff,
46 0x1c, 0x39, 0x3c, 0x3f, 0x5c, 0x79, 0x7c, 0x7f, 0x9c, 0xb9, 0xbc, 0xbf, 0xdc, 0xf9, 0xfc, 0xff, 0xff, 0xff, 0xff,
47 },
48 Stride: 19,
49 Rect: image.Rectangle{
50 Min: image.Point{10, 20},
51 Max: image.Point{18, 28},
52 },
53 }
54
55 func TestNew(t *testing.T) {
56 // This test never fails explicitly. If it doesn't panic, it's okay.
57 for L := 0; L < 4; L++ {
58 for T := 0; T < 4; T++ {
59 for R := 8; R < 12; R++ {
60 for B := 8; B < 12; B++ {
61 img := New(image.Rect(L, T, R, B))
62 for y := T; y < B; y++ {
63 for x := L; x < R; x++ {
64 img.At(x, y)
65 }
66 }
67 }
68 }
69 }
70 }
71 }
72
73 func TestImage(t *testing.T) {
74 for L := src.Rect.Min.X - 4; L < src.Rect.Max.X+4; L++ {
75 for T := src.Rect.Min.Y - 4; T < src.Rect.Max.Y+4; T++ {
76 for R := src.Rect.Min.X - 4; R < src.Rect.Max.X+4; R++ {
77 for B := src.Rect.Min.Y - 4; B < src.Rect.Max.Y+4; B++ {
78 r := image.Rect(L, T, R, B)
79 s := src.SubImage(r)
80 for y := src.Rect.Min.Y - 4; y < src.Rect.Max.Y+4; y++ {
81 for x := src.Rect.Min.X - 4; x < src.Rect.Max.X+4; x++ {
82 p := image.Pt(x, y)
83 c0 := s.At(x, y)
84 c1 := color.YCbCr{}
85 if p.In(r) && p.In(src.Rect) {
86 c1 = expectedAt(x, y)
87 }
88 if c0 != c1 {
89 t.Errorf("got: %v, expected: %v (x=%d, y=%d, r=%v)\n",
90 c0, c1, x, y, r)
91 return
92 }
93 }
94 }
95 }
96 }
97 }
98 }
99 }
100
101 func TestToRGBA(t *testing.T) {
102 t.Parallel()
103 r0 := image.Rect(-2, -6, 14, 10)
104 for L := r0.Min.X; L < r0.Max.X; L++ {
105 for T := r0.Min.Y; T < r0.Max.Y; T++ {
106 for R := r0.Min.X; R < r0.Max.X; R++ {
107 for B := r0.Min.Y; B < r0.Max.Y; B++ {
108 for X := src.Rect.Min.X - 4; X < src.Rect.Max.X+4; X++ {
109 for Y := src.Rect.Min.Y - 4; Y < src.Rect.Max.Y+4; Y++ {
110 r := image.Rect(L, T, R, B)
111 p := image.Pt(X, Y)
112 dst0 := image.NewRGBA(r0)
113 dst1 := image.NewRGBA(r0)
114 ToRGBA(dst0, r, src, p)
115 draw.Draw(dst1, r, src, p, draw.Over)
116 for x := r0.Min.X; x < r0.Max.X; x++ {
117 for y := r0.Min.Y; y < r0.Max.Y; y++ {
118 c0 := dst0.At(x, y)
119 c1 := dst1.At(x, y)
120 if c0 != c1 {
121 t.Errorf("got: %v, expected: %v (x=%d, y=%d, r=%v, p=%v)\n",
122 c0, c1, x, y, r, p)
123 return
124 }
125 }
126 }
127 }
128 }
129 }
130 }
131 }
132 }
133 }
134
135 func TestToGray(t *testing.T) {
136 t.Parallel()
137 r0 := image.Rect(-2, -6, 14, 10)
138 for L := r0.Min.X; L < r0.Max.X; L++ {
139 for T := r0.Min.Y; T < r0.Max.Y; T++ {
140 for R := r0.Min.X; R < r0.Max.X; R++ {
141 for B := r0.Min.Y; B < r0.Max.Y; B++ {
142 for X := src.Rect.Min.X - 4; X < src.Rect.Max.X+4; X++ {
143 for Y := src.Rect.Min.Y - 4; Y < src.Rect.Max.Y+4; Y++ {
144 r := image.Rect(L, T, R, B)
145 p := image.Pt(X, Y)
146 dst0 := image.NewGray(r0)
147 dst1 := image.NewGray(r0)
148 ToGray(dst0, r, src, p)
149 draw.Draw(dst1, r, src, p, draw.Over)
150 for x := r0.Min.X; x < r0.Max.X; x++ {
151 for y := r0.Min.Y; y < r0.Max.Y; y++ {
152 c0 := dst0.At(x, y)
153 c1 := dst1.At(x, y)
154 if c0 != c1 {
155 t.Errorf("got: %v, expected: %v (x=%d, y=%d, r=%v, p=%v)\n",
156 c0, c1, x, y, r, p)
157 return
158 }
159 }
160 }
161 }
162 }
163 }
164 }
165 }
166 }
167 }
168
169 func TestToYCbCr(t *testing.T) {
170 t.Parallel()
171 r0 := image.Rect(-2, -6, 14, 10)
172 for L := r0.Min.X; L < r0.Max.X; L++ {
173 for T := r0.Min.Y; T < r0.Max.Y; T++ {
174 for R := r0.Min.X; R < r0.Max.X; R++ {
175 for B := r0.Min.Y; B < r0.Max.Y; B++ {
176 for X := src.Rect.Min.X - 4; X < src.Rect.Max.X+4; X++ {
177 for Y := src.Rect.Min.Y - 4; Y < src.Rect.Max.Y+4; Y++ {
178 r := image.Rect(L, T, R, B)
179 p := image.Pt(X, Y)
180 d := p.Sub(r.Min)
181 dst := image.NewYCbCr(r0, image.YCbCrSubsampleRatio444)
182 ToYCbCr(dst, r, src, p)
183 for x := r0.Min.X; x < r0.Max.X; x++ {
184 for y := r0.Min.Y; y < r0.Max.Y; y++ {
185 c0 := dst.At(x, y)
186 c1 := color.YCbCr{}
187 if image.Pt(x, y).In(r) && image.Pt(x, y).Add(d).In(src.Rect) {
188 c1 = expectedAt(x+d.X, y+d.Y)
189 }
190 if c0 != c1 {
191 t.Errorf("got: %v, expected: %v (x=%d, y=%d, r=%v, p=%v)\n",
192 c0, c1, x, y, r, p)
193 return
194 }
195 }
196 }
197 }
198 }
199 }
200 }
201 }
202 }
203 }
204
205 func expectedAt(x, y int) color.YCbCr {
206 x = (x - src.Rect.Min.X) & 7
207 y = (y - src.Rect.Min.Y) & 7
208 return color.YCbCr{
209 Y: byte(x<<5 + y<<2),
210 Cb: byte(x&^1<<5 + y<<3 + 1),
211 Cr: byte(x&^1<<5 + y<<3 + 7),
212 }
213 }
214
215 func TestRGBConversion(t *testing.T) {
216 var (
217 ycc = New(image.Rect(0, 0, 2, 1))
218 rgb1 = image.NewRGBA(ycc.Rect)
219 rgb2 = image.NewRGBA(ycc.Rect)
220 )
221 for y := 0; y < 256; y++ {
222 for cb := 0; cb < 256; cb++ {
223 for cr := 0; cr < 256; cr++ {
224 var (
225 y = uint8(y)
226 cb = uint8(cb)
227 cr = uint8(cr)
228 )
229 ycc.Pix[0] = y
230 ycc.Pix[1] = cb
231 ycc.Pix[2] = y
232 ycc.Pix[3] = cr
233 draw.Draw(rgb1, rgb1.Rect, ycc, ycc.Rect.Min, draw.Over)
234 ToRGBA(rgb2, rgb2.Rect, ycc, ycc.Rect.Min)
235 for i, v := range rgb2.Pix {
236 if v != rgb1.Pix[i] {
237 t.Errorf("got: %v, expected: %v (y=%d, cb=%d, cr=%d)\n",
238 rgb2.Pix, rgb1.Pix, y, cb, cr)
239 return
240 }
241 }
242 }
243 }
244 }
245 }
246
247 func TestGrayConversion(t *testing.T) {
248 var (
249 ycc = New(image.Rect(0, 0, 2, 1))
250 gray1 = image.NewGray(ycc.Rect)
251 gray2 = image.NewGray(ycc.Rect)
252 )
253 for y := 0; y < 256; y++ {
254 for cb := 0; cb < 256; cb++ {
255 for cr := 0; cr < 256; cr++ {
256 var (
257 y = uint8(y)
258 cb = uint8(cb)
259 cr = uint8(cr)
260 )
261 ycc.Pix[0] = y
262 ycc.Pix[1] = cb
263 ycc.Pix[2] = y
264 ycc.Pix[3] = cr
265 draw.Draw(gray1, gray1.Rect, ycc, ycc.Rect.Min, draw.Over)
266 ToGray(gray2, gray2.Rect, ycc, ycc.Rect.Min)
267 for i, v := range gray2.Pix {
268 if v != gray1.Pix[i] {
269 t.Errorf("got: %v, expected: %v (y=%d, cb=%d, cr=%d)\n",
270 gray2.Pix, gray1.Pix, y, cb, cr)
271 return
272 }
273 }
274 }
275 }
276 }
277 }