ycbcr.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. package color
  5. // RGBToYCbCr converts an RGB triple to a Y'CbCr triple.
  6. func RGBToYCbCr(r, g, b uint8) (uint8, uint8, uint8) {
  7. // The JFIF specification says:
  8. // Y' = 0.2990*R + 0.5870*G + 0.1140*B
  9. // Cb = -0.1687*R - 0.3313*G + 0.5000*B + 128
  10. // Cr = 0.5000*R - 0.4187*G - 0.0813*B + 128
  11. // http://www.w3.org/Graphics/JPEG/jfif3.pdf says Y but means Y'.
  12. r1 := int(r)
  13. g1 := int(g)
  14. b1 := int(b)
  15. yy := (19595*r1 + 38470*g1 + 7471*b1 + 1<<15) >> 16
  16. cb := (-11056*r1 - 21712*g1 + 32768*b1 + 257<<15) >> 16
  17. cr := (32768*r1 - 27440*g1 - 5328*b1 + 257<<15) >> 16
  18. if yy < 0 {
  19. yy = 0
  20. } else if yy > 255 {
  21. yy = 255
  22. }
  23. if cb < 0 {
  24. cb = 0
  25. } else if cb > 255 {
  26. cb = 255
  27. }
  28. if cr < 0 {
  29. cr = 0
  30. } else if cr > 255 {
  31. cr = 255
  32. }
  33. return uint8(yy), uint8(cb), uint8(cr)
  34. }
  35. // YCbCrToRGB converts a Y'CbCr triple to an RGB triple.
  36. func YCbCrToRGB(y, cb, cr uint8) (uint8, uint8, uint8) {
  37. // The JFIF specification says:
  38. // R = Y' + 1.40200*(Cr-128)
  39. // G = Y' - 0.34414*(Cb-128) - 0.71414*(Cr-128)
  40. // B = Y' + 1.77200*(Cb-128)
  41. // http://www.w3.org/Graphics/JPEG/jfif3.pdf says Y but means Y'.
  42. yy1 := int(y)<<16 + 1<<15
  43. cb1 := int(cb) - 128
  44. cr1 := int(cr) - 128
  45. r := (yy1 + 91881*cr1) >> 16
  46. g := (yy1 - 22554*cb1 - 46802*cr1) >> 16
  47. b := (yy1 + 116130*cb1) >> 16
  48. if r < 0 {
  49. r = 0
  50. } else if r > 255 {
  51. r = 255
  52. }
  53. if g < 0 {
  54. g = 0
  55. } else if g > 255 {
  56. g = 255
  57. }
  58. if b < 0 {
  59. b = 0
  60. } else if b > 255 {
  61. b = 255
  62. }
  63. return uint8(r), uint8(g), uint8(b)
  64. }
  65. // YCbCr represents a fully opaque 24-bit Y'CbCr color, having 8 bits each for
  66. // one luma and two chroma components.
  67. //
  68. // JPEG, VP8, the MPEG family and other codecs use this color model. Such
  69. // codecs often use the terms YUV and Y'CbCr interchangeably, but strictly
  70. // speaking, the term YUV applies only to analog video signals, and Y' (luma)
  71. // is Y (luminance) after applying gamma correction.
  72. //
  73. // Conversion between RGB and Y'CbCr is lossy and there are multiple, slightly
  74. // different formulae for converting between the two. This package follows
  75. // the JFIF specification at http://www.w3.org/Graphics/JPEG/jfif3.pdf.
  76. type YCbCr struct {
  77. Y, Cb, Cr uint8
  78. }
  79. func (c YCbCr) RGBA() (uint32, uint32, uint32, uint32) {
  80. r, g, b := YCbCrToRGB(c.Y, c.Cb, c.Cr)
  81. return uint32(r) * 0x101, uint32(g) * 0x101, uint32(b) * 0x101, 0xffff
  82. }
  83. // YCbCrModel is the Model for Y'CbCr colors.
  84. var YCbCrModel Model = ModelFunc(yCbCrModel)
  85. func yCbCrModel(c Color) Color {
  86. if _, ok := c.(YCbCr); ok {
  87. return c
  88. }
  89. r, g, b, _ := c.RGBA()
  90. y, u, v := RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8))
  91. return YCbCr{y, u, v}
  92. }