RGB YUV HSV YCbCr integer conversions


SUBMITTED BY: Guest

DATE: Dec. 31, 2013, 9:14 a.m.

FORMAT: C++

SIZE: 3.6 kB

HITS: 1408

  1. typedef struct RgbColor
  2. {
  3. unsigned char r;
  4. unsigned char g;
  5. unsigned char b;
  6. } RgbColor;
  7. typedef struct HsvColor
  8. {
  9. unsigned char h;
  10. unsigned char s;
  11. unsigned char v;
  12. } HsvColor;
  13. RgbColor HsvToRgb(HsvColor hsv)
  14. {
  15. RgbColor rgb;
  16. unsigned char region, remainder, p, q, t;
  17. if (hsv.s == 0)
  18. {
  19. rgb.r = hsv.v;
  20. rgb.g = hsv.v;
  21. rgb.b = hsv.v;
  22. return rgb;
  23. }
  24. region = hsv.h / 43;
  25. remainder = (hsv.h - (region * 43)) * 6;
  26. p = (hsv.v * (255 - hsv.s)) >> 8;
  27. q = (hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8;
  28. t = (hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8;
  29. switch (region)
  30. {
  31. case 0:
  32. rgb.r = hsv.v; rgb.g = t; rgb.b = p;
  33. break;
  34. case 1:
  35. rgb.r = q; rgb.g = hsv.v; rgb.b = p;
  36. break;
  37. case 2:
  38. rgb.r = p; rgb.g = hsv.v; rgb.b = t;
  39. break;
  40. case 3:
  41. rgb.r = p; rgb.g = q; rgb.b = hsv.v;
  42. break;
  43. case 4:
  44. rgb.r = t; rgb.g = p; rgb.b = hsv.v;
  45. break;
  46. default:
  47. rgb.r = hsv.v; rgb.g = p; rgb.b = q;
  48. break;
  49. }
  50. return rgb;
  51. }
  52. HsvColor RgbToHsv(RgbColor rgb)
  53. {
  54. HsvColor hsv;
  55. unsigned char rgbMin, rgbMax;
  56. rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
  57. rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);
  58. hsv.v = rgbMax;
  59. if (hsv.v == 0)
  60. {
  61. hsv.h = 0;
  62. hsv.s = 0;
  63. return hsv;
  64. }
  65. hsv.s = 255 * long(rgbMax - rgbMin) / hsv.v;
  66. if (hsv.s == 0)
  67. {
  68. hsv.h = 0;
  69. return hsv;
  70. }
  71. if (rgbMax == rgb.r)
  72. hsv.h = 0 + 43 * (rgb.g - rgb.b) / (rgbMax - rgbMin);
  73. else if (rgbMax == rgb.g)
  74. hsv.h = 85 + 43 * (rgb.b - rgb.r) / (rgbMax - rgbMin);
  75. else
  76. hsv.h = 171 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin);
  77. return hsv;
  78. }
  79. #define CLIP(X) ( (X) > 255 ? 255 : (X) < 0 ? 0 : X)
  80. // RGB -> YUV
  81. #define RGB2Y(R, G, B) CLIP(( ( 66 * (R) + 129 * (G) + 25 * (B) + 128) >> 8) + 16)
  82. #define RGB2U(R, G, B) CLIP(( ( -38 * (R) - 74 * (G) + 112 * (B) + 128) >> 8) + 128)
  83. #define RGB2V(R, G, B) CLIP(( ( 112 * (R) - 94 * (G) - 18 * (B) + 128) >> 8) + 128)
  84. // YUV -> RGB
  85. #define C(Y) ( (Y) - 16 )
  86. #define D(U) ( (U) - 128 )
  87. #define E(V) ( (V) - 128 )
  88. #define YUV2R(Y, U, V) CLIP(( 298 * C(Y) + 409 * E(V) + 128) >> 8)
  89. #define YUV2G(Y, U, V) CLIP(( 298 * C(Y) - 100 * D(U) - 208 * E(V) + 128) >> 8)
  90. #define YUV2B(Y, U, V) CLIP(( 298 * C(Y) + 516 * D(U) + 128) >> 8)
  91. // RGB -> YCbCr
  92. #define CRGB2Y(R, G, B) CLIP((19595 * R + 38470 * G + 7471 * B ) >> 16)
  93. #define CRGB2Cb(R, G, B) CLIP((36962 * (B - CLIP((19595 * R + 38470 * G + 7471 * B ) >> 16) ) >> 16) + 128)
  94. #define CRGB2Cr(R, G, B) CLIP((46727 * (R - CLIP((19595 * R + 38470 * G + 7471 * B ) >> 16) ) >> 16) + 128)
  95. // YCbCr -> RGB
  96. #define CYCbCr2R(Y, Cb, Cr) CLIP( Y + ( 91881 * Cr >> 16 ) - 179 )
  97. #define CYCbCr2G(Y, Cb, Cr) CLIP( Y - (( 22544 * Cb + 46793 * Cr ) >> 16) + 135)
  98. #define CYCbCr2B(Y, Cb, Cr) CLIP( Y + (116129 * Cb >> 16 ) - 226 )

comments powered by Disqus