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.
13 func TestYCbCr(t
*testing
.T
) {
36 subsampleRatios
:= []YCbCrSubsampleRatio
{
37 YCbCrSubsampleRatio444
,
38 YCbCrSubsampleRatio422
,
39 YCbCrSubsampleRatio420
,
47 for _
, r
:= range rects
{
48 for _
, subsampleRatio
:= range subsampleRatios
{
49 for _
, delta
:= range deltas
{
50 testYCbCr(t
, r
, subsampleRatio
, delta
)
59 func testYCbCr(t
*testing
.T
, r Rectangle
, subsampleRatio YCbCrSubsampleRatio
, delta Point
) {
60 // Create a YCbCr image m, whose bounds are r translated by (delta.X, delta.Y).
62 m
:= NewYCbCr(r1
, subsampleRatio
)
64 // Test that the image buffer is reasonably small even if (delta.X, delta.Y) is far from the origin.
65 if len(m
.Y
) > 100*100 {
66 t
.Errorf("r=%v, subsampleRatio=%v, delta=%v: image buffer is too large",
67 r
, subsampleRatio
, delta
)
71 // Initialize m's pixels. For 422 and 420 subsampling, some of the Cb and Cr elements
72 // will be set multiple times. That's OK. We just want to avoid a uniform image.
73 for y
:= r1
.Min
.Y
; y
< r1
.Max
.Y
; y
++ {
74 for x
:= r1
.Min
.X
; x
< r1
.Max
.X
; x
++ {
77 m
.Y
[yi
] = uint8(16*y
+ x
)
78 m
.Cb
[ci
] = uint8(y
+ 16*x
)
79 m
.Cr
[ci
] = uint8(y
+ 16*x
)
83 // Make various sub-images of m.
84 for y0
:= delta
.Y
+ 3; y0
< delta
.Y
+7; y0
++ {
85 for y1
:= delta
.Y
+ 8; y1
< delta
.Y
+13; y1
++ {
86 for x0
:= delta
.X
+ 3; x0
< delta
.X
+7; x0
++ {
87 for x1
:= delta
.X
+ 8; x1
< delta
.X
+13; x1
++ {
88 subRect
:= Rect(x0
, y0
, x1
, y1
)
89 sub
:= m
.SubImage(subRect
).(*YCbCr
)
91 // For each point in the sub-image's bounds, check that m.At(x, y) equals sub.At(x, y).
92 for y
:= sub
.Rect
.Min
.Y
; y
< sub
.Rect
.Max
.Y
; y
++ {
93 for x
:= sub
.Rect
.Min
.X
; x
< sub
.Rect
.Max
.X
; x
++ {
94 color0
:= m
.At(x
, y
).(color
.YCbCr
)
95 color1
:= sub
.At(x
, y
).(color
.YCbCr
)
97 t
.Errorf("r=%v, subsampleRatio=%v, delta=%v, x=%d, y=%d, color0=%v, color1=%v",
98 r
, subsampleRatio
, delta
, x
, y
, color0
, color1
)