github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/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 // NYCbCrA represents a non-alpha-premultiplied Y'CbCr-with-alpha color, having 141 // 8 bits each for one luma, two chroma and one alpha component. 142 type NYCbCrA struct { 143 YCbCr 144 A uint8 145 } 146 147 func (c NYCbCrA) RGBA() (r, g, b, a uint32) { 148 r8, g8, b8 := YCbCrToRGB(c.Y, c.Cb, c.Cr) 149 a = uint32(c.A) * 0x101 150 r = uint32(r8) * 0x101 * a / 0xffff 151 g = uint32(g8) * 0x101 * a / 0xffff 152 b = uint32(b8) * 0x101 * a / 0xffff 153 return 154 } 155 156 // NYCbCrAModel is the Model for non-alpha-premultiplied Y'CbCr-with-alpha 157 // colors. 158 var NYCbCrAModel Model = ModelFunc(nYCbCrAModel) 159 160 func nYCbCrAModel(c Color) Color { 161 switch c := c.(type) { 162 case NYCbCrA: 163 return c 164 case YCbCr: 165 return NYCbCrA{c, 0xff} 166 } 167 r, g, b, a := c.RGBA() 168 169 // Convert from alpha-premultiplied to non-alpha-premultiplied. 170 if a != 0 { 171 r = (r * 0xffff) / a 172 g = (g * 0xffff) / a 173 b = (b * 0xffff) / a 174 } 175 176 y, u, v := RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8)) 177 return NYCbCrA{YCbCr{Y: y, Cb: u, Cr: v}, uint8(a >> 8)} 178 } 179 180 // RGBToCMYK converts an RGB triple to a CMYK quadruple. 181 func RGBToCMYK(r, g, b uint8) (uint8, uint8, uint8, uint8) { 182 rr := uint32(r) 183 gg := uint32(g) 184 bb := uint32(b) 185 w := rr 186 if w < gg { 187 w = gg 188 } 189 if w < bb { 190 w = bb 191 } 192 if w == 0 { 193 return 0, 0, 0, 0xff 194 } 195 c := (w - rr) * 0xff / w 196 m := (w - gg) * 0xff / w 197 y := (w - bb) * 0xff / w 198 return uint8(c), uint8(m), uint8(y), uint8(0xff - w) 199 } 200 201 // CMYKToRGB converts a CMYK quadruple to an RGB triple. 202 func CMYKToRGB(c, m, y, k uint8) (uint8, uint8, uint8) { 203 w := uint32(0xffff - uint32(k)*0x101) 204 r := uint32(0xffff-uint32(c)*0x101) * w / 0xffff 205 g := uint32(0xffff-uint32(m)*0x101) * w / 0xffff 206 b := uint32(0xffff-uint32(y)*0x101) * w / 0xffff 207 return uint8(r >> 8), uint8(g >> 8), uint8(b >> 8) 208 } 209 210 // CMYK represents a fully opaque CMYK color, having 8 bits for each of cyan, 211 // magenta, yellow and black. 212 // 213 // It is not associated with any particular color profile. 214 type CMYK struct { 215 C, M, Y, K uint8 216 } 217 218 func (c CMYK) RGBA() (uint32, uint32, uint32, uint32) { 219 // This code is a copy of the CMYKToRGB function above, except that it 220 // returns values in the range [0, 0xffff] instead of [0, 0xff]. 221 222 w := uint32(0xffff - uint32(c.K)*0x101) 223 r := uint32(0xffff-uint32(c.C)*0x101) * w / 0xffff 224 g := uint32(0xffff-uint32(c.M)*0x101) * w / 0xffff 225 b := uint32(0xffff-uint32(c.Y)*0x101) * w / 0xffff 226 return uint32(r), uint32(g), uint32(b), 0xffff 227 } 228 229 // CMYKModel is the Model for CMYK colors. 230 var CMYKModel Model = ModelFunc(cmykModel) 231 232 func cmykModel(c Color) Color { 233 if _, ok := c.(CMYK); ok { 234 return c 235 } 236 r, g, b, _ := c.RGBA() 237 cc, mm, yy, kk := RGBToCMYK(uint8(r>>8), uint8(g>>8), uint8(b>>8)) 238 return CMYK{cc, mm, yy, kk} 239 }