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