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. 4 5package image 6 7import ( 8 "image/color" 9 "testing" 10) 11 12func 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 61func 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). 63 r1 := r.Add(delta) 64 m := NewYCbCr(r1, subsampleRatio) 65 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) 70 return 71 } 72 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++ { 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 // 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) 92 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) 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 111func 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