1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 func TestYCbCr(t
*testing
.T
) {
35 subsampleRatios
:= []YCbCrSubsampleRatio
{
36 YCbCrSubsampleRatio444
,
37 YCbCrSubsampleRatio422
,
38 YCbCrSubsampleRatio420
,
39 YCbCrSubsampleRatio440
,
40 YCbCrSubsampleRatio411
,
41 YCbCrSubsampleRatio410
,
49 for _
, r
:= range rects
{
50 for _
, subsampleRatio
:= range subsampleRatios
{
51 for _
, delta
:= range deltas
{
52 testYCbCr(t
, r
, subsampleRatio
, delta
)
61 func testYCbCr(t
*testing
.T
, r Rectangle
, subsampleRatio YCbCrSubsampleRatio
, delta Point
) {
62 // Create a YCbCr image m, whose bounds are r translated by (delta.X, delta.Y).
64 m
:= NewYCbCr(r1
, subsampleRatio
)
66 // Test that the image buffer is reasonably small even if (delta.X, delta.Y) is far from the origin.
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
)
73 // Initialize m's pixels. For 422 and 420 subsampling, some of the Cb and Cr elements
74 // will be set multiple times. That's OK. We just want to avoid a uniform image.
75 for y
:= r1
.Min
.Y
; y
< r1
.Max
.Y
; y
++ {
76 for x
:= r1
.Min
.X
; x
< r1
.Max
.X
; x
++ {
79 m
.Y
[yi
] = uint8(16*y
+ x
)
80 m
.Cb
[ci
] = uint8(y
+ 16*x
)
81 m
.Cr
[ci
] = uint8(y
+ 16*x
)
85 // Make various sub-images of m.
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
)
93 // For each point in the sub-image's bounds, check that m.At(x, y) equals sub.At(x, y).
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
)
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
)
111 func TestYCbCrSlicesDontOverlap(t
*testing
.T
) {
112 m
:= NewYCbCr(Rect(0, 0, 8, 8), YCbCrSubsampleRatio420
)
113 names
:= []string{"Y", "Cb", "Cr"}
119 for i
, slice
:= range slices
{
120 want
:= uint8(10 + i
)
121 for j
:= range slice
{
125 for i
, slice
:= range slices
{
126 want
:= uint8(10 + i
)
127 for j
, got
:= range slice
{
129 t
.Fatalf("m.%s[%d]: got %d, want %d", names
[i
], j
, got
, want
)