github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/image/ycbcr_test.go (about)

     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  
     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  	}
    41  	deltas := []Point{
    42  		Pt(0, 0),
    43  		Pt(1000, 1001),
    44  		Pt(5001, -400),
    45  		Pt(-701, -801),
    46  	}
    47  	for _, r := range rects {
    48  		for _, subsampleRatio := range subsampleRatios {
    49  			for _, delta := range deltas {
    50  				testYCbCr(t, r, subsampleRatio, delta)
    51  			}
    52  		}
    53  		if testing.Short() {
    54  			break
    55  		}
    56  	}
    57  }
    58  
    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).
    61  	r1 := r.Add(delta)
    62  	m := NewYCbCr(r1, subsampleRatio)
    63  
    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)
    68  		return
    69  	}
    70  
    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++ {
    75  			yi := m.YOffset(x, y)
    76  			ci := m.COffset(x, y)
    77  			m.Y[yi] = uint8(16*y + x)
    78  			m.Cb[ci] = uint8(y + 16*x)
    79  			m.Cr[ci] = uint8(y + 16*x)
    80  		}
    81  	}
    82  
    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)
    90  
    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)
    96  							if color0 != color1 {
    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)
    99  								return
   100  							}
   101  						}
   102  					}
   103  				}
   104  			}
   105  		}
   106  	}
   107  }