github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/image/color/ycbcr.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 // RGBToYCbCr converts an RGB triple to a Y'CbCr triple. 8 func RGBToYCbCr(r, g, b uint8) (uint8, uint8, uint8) { 9 // The JFIF specification says: 10 // Y' = 0.2990*R + 0.5870*G + 0.1140*B 11 // Cb = -0.1687*R - 0.3313*G + 0.5000*B + 128 12 // Cr = 0.5000*R - 0.4187*G - 0.0813*B + 128 13 // http://www.w3.org/Graphics/JPEG/jfif3.pdf says Y but means Y'. 14 15 r1 := int32(r) 16 g1 := int32(g) 17 b1 := int32(b) 18 yy := (19595*r1 + 38470*g1 + 7471*b1 + 1<<15) >> 16 19 cb := (-11056*r1 - 21712*g1 + 32768*b1 + 257<<15) >> 16 20 cr := (32768*r1 - 27440*g1 - 5328*b1 + 257<<15) >> 16 21 if yy < 0 { 22 yy = 0 23 } else if yy > 0xff { 24 yy = 0xff 25 } 26 if cb < 0 { 27 cb = 0 28 } else if cb > 0xff { 29 cb = 0xff 30 } 31 if cr < 0 { 32 cr = 0 33 } else if cr > 0xff { 34 cr = 0xff 35 } 36 return uint8(yy), uint8(cb), uint8(cr) 37 } 38 39 // YCbCrToRGB converts a Y'CbCr triple to an RGB triple. 40 func YCbCrToRGB(y, cb, cr uint8) (uint8, uint8, uint8) { 41 // The JFIF specification says: 42 // R = Y' + 1.40200*(Cr-128) 43 // G = Y' - 0.34414*(Cb-128) - 0.71414*(Cr-128) 44 // B = Y' + 1.77200*(Cb-128) 45 // http://www.w3.org/Graphics/JPEG/jfif3.pdf says Y but means Y'. 46 47 yy1 := int32(y) * 0x10100 // Convert 0x12 to 0x121200. 48 cb1 := int32(cb) - 128 49 cr1 := int32(cr) - 128 50 r := (yy1 + 91881*cr1) >> 16 51 g := (yy1 - 22554*cb1 - 46802*cr1) >> 16 52 b := (yy1 + 116130*cb1) >> 16 53 if r < 0 { 54 r = 0 55 } else if r > 0xff { 56 r = 0xff 57 } 58 if g < 0 { 59 g = 0 60 } else if g > 0xff { 61 g = 0xff 62 } 63 if b < 0 { 64 b = 0 65 } else if b > 0xff { 66 b = 0xff 67 } 68 return uint8(r), uint8(g), uint8(b) 69 } 70 71 // YCbCr represents a fully opaque 24-bit Y'CbCr color, having 8 bits each for 72 // one luma and two chroma components. 73 // 74 // JPEG, VP8, the MPEG family and other codecs use this color model. Such 75 // codecs often use the terms YUV and Y'CbCr interchangeably, but strictly 76 // speaking, the term YUV applies only to analog video signals, and Y' (luma) 77 // is Y (luminance) after applying gamma correction. 78 // 79 // Conversion between RGB and Y'CbCr is lossy and there are multiple, slightly 80 // different formulae for converting between the two. This package follows 81 // the JFIF specification at http://www.w3.org/Graphics/JPEG/jfif3.pdf. 82 type YCbCr struct { 83 Y, Cb, Cr uint8 84 } 85 86 func (c YCbCr) RGBA() (uint32, uint32, uint32, uint32) { 87 // This code is a copy of the YCbCrToRGB function above, except that it 88 // returns values in the range [0, 0xffff] instead of [0, 0xff]. There is a 89 // subtle difference between doing this and having YCbCr satisfy the Color 90 // interface by first converting to an RGBA. The latter loses some 91 // information by going to and from 8 bits per channel. 92 // 93 // For example, this code: 94 // const y, cb, cr = 0x7f, 0x7f, 0x7f 95 // r, g, b := color.YCbCrToRGB(y, cb, cr) 96 // r0, g0, b0, _ := color.YCbCr{y, cb, cr}.RGBA() 97 // r1, g1, b1, _ := color.RGBA{r, g, b, 0xff}.RGBA() 98 // fmt.Printf("0x%04x 0x%04x 0x%04x\n", r0, g0, b0) 99 // fmt.Printf("0x%04x 0x%04x 0x%04x\n", r1, g1, b1) 100 // prints: 101 // 0x7e18 0x808e 0x7db9 102 // 0x7e7e 0x8080 0x7d7d 103 104 yy1 := int32(c.Y) * 0x10100 // Convert 0x12 to 0x121200. 105 cb1 := int32(c.Cb) - 128 106 cr1 := int32(c.Cr) - 128 107 r := (yy1 + 91881*cr1) >> 8 108 g := (yy1 - 22554*cb1 - 46802*cr1) >> 8 109 b := (yy1 + 116130*cb1) >> 8 110 if r < 0 { 111 r = 0 112 } else if r > 0xffff { 113 r = 0xffff 114 } 115 if g < 0 { 116 g = 0 117 } else if g > 0xffff { 118 g = 0xffff 119 } 120 if b < 0 { 121 b = 0 122 } else if b > 0xffff { 123 b = 0xffff 124 } 125 return uint32(r), uint32(g), uint32(b), 0xffff 126 } 127 128 // YCbCrModel is the Model for Y'CbCr colors. 129 var YCbCrModel Model = ModelFunc(yCbCrModel) 130 131 func yCbCrModel(c Color) Color { 132 if _, ok := c.(YCbCr); ok { 133 return c 134 } 135 r, g, b, _ := c.RGBA() 136 y, u, v := RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8)) 137 return YCbCr{y, u, v} 138 } 139 140 // RGBToCMYK converts an RGB triple to a CMYK quadruple. 141 func RGBToCMYK(r, g, b uint8) (uint8, uint8, uint8, uint8) { 142 rr := uint32(r) 143 gg := uint32(g) 144 bb := uint32(b) 145 w := rr 146 if w < gg { 147 w = gg 148 } 149 if w < bb { 150 w = bb 151 } 152 if w == 0 { 153 return 0, 0, 0, 0xff 154 } 155 c := (w - rr) * 0xff / w 156 m := (w - gg) * 0xff / w 157 y := (w - bb) * 0xff / w 158 return uint8(c), uint8(m), uint8(y), uint8(0xff - w) 159 } 160 161 // CMYKToRGB converts a CMYK quadruple to an RGB triple. 162 func CMYKToRGB(c, m, y, k uint8) (uint8, uint8, uint8) { 163 w := uint32(0xffff - uint32(k)*0x101) 164 r := uint32(0xffff-uint32(c)*0x101) * w / 0xffff 165 g := uint32(0xffff-uint32(m)*0x101) * w / 0xffff 166 b := uint32(0xffff-uint32(y)*0x101) * w / 0xffff 167 return uint8(r >> 8), uint8(g >> 8), uint8(b >> 8) 168 } 169 170 // CMYK represents a fully opaque CMYK color, having 8 bits for each of cyan, 171 // magenta, yellow and black. 172 // 173 // It is not associated with any particular color profile. 174 type CMYK struct { 175 C, M, Y, K uint8 176 } 177 178 func (c CMYK) RGBA() (uint32, uint32, uint32, uint32) { 179 // This code is a copy of the CMYKToRGB function above, except that it 180 // returns values in the range [0, 0xffff] instead of [0, 0xff]. 181 182 w := uint32(0xffff - uint32(c.K)*0x101) 183 r := uint32(0xffff-uint32(c.C)*0x101) * w / 0xffff 184 g := uint32(0xffff-uint32(c.M)*0x101) * w / 0xffff 185 b := uint32(0xffff-uint32(c.Y)*0x101) * w / 0xffff 186 return uint32(r), uint32(g), uint32(b), 0xffff 187 } 188 189 // CMYKModel is the Model for CMYK colors. 190 var CMYKModel Model = ModelFunc(cmykModel) 191 192 func cmykModel(c Color) Color { 193 if _, ok := c.(CMYK); ok { 194 return c 195 } 196 r, g, b, _ := c.RGBA() 197 cc, mm, yy, kk := RGBToCMYK(uint8(r>>8), uint8(g>>8), uint8(b>>8)) 198 return CMYK{cc, mm, yy, kk} 199 }