github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/image/color/ycbcr_test.go (about) 1 // Copyright 2011 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 color 6 7 import ( 8 "testing" 9 ) 10 11 func delta(x, y uint8) uint8 { 12 if x >= y { 13 return x - y 14 } 15 return y - x 16 } 17 18 // TestYCbCrRoundtrip tests that a subset of RGB space can be converted to YCbCr 19 // and back to within 1/256 tolerance. 20 func TestYCbCrRoundtrip(t *testing.T) { 21 for r := 0; r < 256; r += 7 { 22 for g := 0; g < 256; g += 5 { 23 for b := 0; b < 256; b += 3 { 24 r0, g0, b0 := uint8(r), uint8(g), uint8(b) 25 y, cb, cr := RGBToYCbCr(r0, g0, b0) 26 r1, g1, b1 := YCbCrToRGB(y, cb, cr) 27 if delta(r0, r1) > 1 || delta(g0, g1) > 1 || delta(b0, b1) > 1 { 28 t.Fatalf("\nr0, g0, b0 = %d, %d, %d\nr1, g1, b1 = %d, %d, %d", r0, g0, b0, r1, g1, b1) 29 } 30 } 31 } 32 } 33 } 34 35 // TestYCbCrToRGBConsistency tests that calling the RGBA method (16 bit color) 36 // then truncating to 8 bits is equivalent to calling the YCbCrToRGB function (8 37 // bit color). 38 func TestYCbCrToRGBConsistency(t *testing.T) { 39 for y := 0; y < 256; y += 7 { 40 for cb := 0; cb < 256; cb += 5 { 41 for cr := 0; cr < 256; cr += 3 { 42 x := YCbCr{uint8(y), uint8(cb), uint8(cr)} 43 r0, g0, b0, _ := x.RGBA() 44 r1, g1, b1 := uint8(r0>>8), uint8(g0>>8), uint8(b0>>8) 45 r2, g2, b2 := YCbCrToRGB(x.Y, x.Cb, x.Cr) 46 if r1 != r2 || g1 != g2 || b1 != b2 { 47 t.Fatalf("y, cb, cr = %d, %d, %d\nr1, g1, b1 = %d, %d, %d\nr2, g2, b2 = %d, %d, %d", 48 y, cb, cr, r1, g1, b1, r2, g2, b2) 49 } 50 } 51 } 52 } 53 } 54 55 // TestCMYKRoundtrip tests that a subset of RGB space can be converted to CMYK 56 // and back to within 1/256 tolerance. 57 func TestCMYKRoundtrip(t *testing.T) { 58 for r := 0; r < 256; r += 7 { 59 for g := 0; g < 256; g += 5 { 60 for b := 0; b < 256; b += 3 { 61 r0, g0, b0 := uint8(r), uint8(g), uint8(b) 62 c, m, y, k := RGBToCMYK(r0, g0, b0) 63 r1, g1, b1 := CMYKToRGB(c, m, y, k) 64 if delta(r0, r1) > 1 || delta(g0, g1) > 1 || delta(b0, b1) > 1 { 65 t.Fatalf("\nr0, g0, b0 = %d, %d, %d\nr1, g1, b1 = %d, %d, %d", r0, g0, b0, r1, g1, b1) 66 } 67 } 68 } 69 } 70 } 71 72 // TestCMYKToRGBConsistency tests that calling the RGBA method (16 bit color) 73 // then truncating to 8 bits is equivalent to calling the CMYKToRGB function (8 74 // bit color). 75 func TestCMYKToRGBConsistency(t *testing.T) { 76 for c := 0; c < 256; c += 7 { 77 for m := 0; m < 256; m += 5 { 78 for y := 0; y < 256; y += 3 { 79 for k := 0; k < 256; k += 11 { 80 x := CMYK{uint8(c), uint8(m), uint8(y), uint8(k)} 81 r0, g0, b0, _ := x.RGBA() 82 r1, g1, b1 := uint8(r0>>8), uint8(g0>>8), uint8(b0>>8) 83 r2, g2, b2 := CMYKToRGB(x.C, x.M, x.Y, x.K) 84 if r1 != r2 || g1 != g2 || b1 != b2 { 85 t.Fatalf("c, m, y, k = %d, %d, %d, %d\nr1, g1, b1 = %d, %d, %d\nr2, g2, b2 = %d, %d, %d", 86 c, m, y, k, r1, g1, b1, r2, g2, b2) 87 } 88 } 89 } 90 } 91 } 92 }