Source file
src/image/ycbcr_test.go
Documentation: image
1
2
3
4
5 package image
6
7 import (
8 "image/color"
9 "testing"
10 )
11
12 func TestYCbCr(t *testing.T) {
13 rects := []Rectangle{
14 Rect(0, 0, 16, 16),
15 Rect(1, 0, 16, 16),
16 Rect(0, 1, 16, 16),
17 Rect(1, 1, 16, 16),
18 Rect(1, 1, 15, 16),
19 Rect(1, 1, 16, 15),
20 Rect(1, 1, 15, 15),
21 Rect(2, 3, 14, 15),
22 Rect(7, 0, 7, 16),
23 Rect(0, 8, 16, 8),
24 Rect(0, 0, 10, 11),
25 Rect(5, 6, 16, 16),
26 Rect(7, 7, 8, 8),
27 Rect(7, 8, 8, 9),
28 Rect(8, 7, 9, 8),
29 Rect(8, 8, 9, 9),
30 Rect(7, 7, 17, 17),
31 Rect(8, 8, 17, 17),
32 Rect(9, 9, 17, 17),
33 Rect(10, 10, 17, 17),
34 }
35 subsampleRatios := []YCbCrSubsampleRatio{
36 YCbCrSubsampleRatio444,
37 YCbCrSubsampleRatio422,
38 YCbCrSubsampleRatio420,
39 YCbCrSubsampleRatio440,
40 YCbCrSubsampleRatio411,
41 YCbCrSubsampleRatio410,
42 }
43 deltas := []Point{
44 Pt(0, 0),
45 Pt(1000, 1001),
46 Pt(5001, -400),
47 Pt(-701, -801),
48 }
49 for _, r := range rects {
50 for _, subsampleRatio := range subsampleRatios {
51 for _, delta := range deltas {
52 testYCbCr(t, r, subsampleRatio, delta)
53 }
54 }
55 if testing.Short() {
56 break
57 }
58 }
59 }
60
61 func testYCbCr(t *testing.T, r Rectangle, subsampleRatio YCbCrSubsampleRatio, delta Point) {
62
63 r1 := r.Add(delta)
64 m := NewYCbCr(r1, subsampleRatio)
65
66
67 if len(m.Y) > 100*100 {
68 t.Errorf("r=%v, subsampleRatio=%v, delta=%v: image buffer is too large",
69 r, subsampleRatio, delta)
70 return
71 }
72
73
74
75 for y := r1.Min.Y; y < r1.Max.Y; y++ {
76 for x := r1.Min.X; x < r1.Max.X; x++ {
77 yi := m.YOffset(x, y)
78 ci := m.COffset(x, y)
79 m.Y[yi] = uint8(16*y + x)
80 m.Cb[ci] = uint8(y + 16*x)
81 m.Cr[ci] = uint8(y + 16*x)
82 }
83 }
84
85
86 for y0 := delta.Y + 3; y0 < delta.Y+7; y0++ {
87 for y1 := delta.Y + 8; y1 < delta.Y+13; y1++ {
88 for x0 := delta.X + 3; x0 < delta.X+7; x0++ {
89 for x1 := delta.X + 8; x1 < delta.X+13; x1++ {
90 subRect := Rect(x0, y0, x1, y1)
91 sub := m.SubImage(subRect).(*YCbCr)
92
93
94 for y := sub.Rect.Min.Y; y < sub.Rect.Max.Y; y++ {
95 for x := sub.Rect.Min.X; x < sub.Rect.Max.X; x++ {
96 color0 := m.At(x, y).(color.YCbCr)
97 color1 := sub.At(x, y).(color.YCbCr)
98 if color0 != color1 {
99 t.Errorf("r=%v, subsampleRatio=%v, delta=%v, x=%d, y=%d, color0=%v, color1=%v",
100 r, subsampleRatio, delta, x, y, color0, color1)
101 return
102 }
103 }
104 }
105 }
106 }
107 }
108 }
109 }
110
111 func TestYCbCrSlicesDontOverlap(t *testing.T) {
112 m := NewYCbCr(Rect(0, 0, 8, 8), YCbCrSubsampleRatio420)
113 names := []string{"Y", "Cb", "Cr"}
114 slices := [][]byte{
115 m.Y[:cap(m.Y)],
116 m.Cb[:cap(m.Cb)],
117 m.Cr[:cap(m.Cr)],
118 }
119 for i, slice := range slices {
120 want := uint8(10 + i)
121 for j := range slice {
122 slice[j] = want
123 }
124 }
125 for i, slice := range slices {
126 want := uint8(10 + i)
127 for j, got := range slice {
128 if got != want {
129 t.Fatalf("m.%s[%d]: got %d, want %d", names[i], j, got, want)
130 }
131 }
132 }
133 }
134
View as plain text