Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. /**
  2.  * Thibault Durnez
  3.  */
  4.  
  5. __private void rgb2hsv(const int src_r, const int src_g, const int src_b, float* dst_h, float* dst_s, float* dst_v);
  6. __private void hsv2rgb(const float src_h, const float src_s, const float src_v, int* dst_r, int* dst_g, int* dst_b);
  7.  
  8. __kernel void hueshift(__global const int *inputImage, __global int *outputImage, int hueshift_param) {
  9.  
  10.     const size_t rowcol = (get_global_id(1) * get_global_size(0) + get_global_id(0));
  11.  
  12.     int color = inputImage[rowcol];
  13.     int blue = (color & 0xFF0000) >> 16;
  14.     int green = (color & 0x00FF00) >> 8;
  15.     int red  = (color & 0x0000FF);
  16.  
  17.     float hue = 0;
  18.     float saturation = 0;
  19.     float value = 0;
  20.  
  21.     rgb2hsv(blue, green, red, &hue, &saturation, &value);
  22.     hsv2rgb(fmod((hue + hueshift_param),360), saturation, value, &blue, &green, &red);
  23.  
  24.  
  25.     int _blue =  (blue << 16) & 0xFF0000;
  26.     int _green = (green << 8) & 0x00FF00;
  27.     int _red =   (red << 0)   & 0x0000FF;
  28.  
  29.     int newColor = (color & 0xFF000000) | _blue | _green | _red;
  30.     outputImage[rowcol] = newColor;
  31. }
  32.  
  33.  
  34. __private void rgb2hsv(const int src_r, const int src_g, const int src_b, float* dst_h, float* dst_s, float* dst_v) {
  35.     float r = src_r / 255.0f;
  36.     float g = src_g / 255.0f;
  37.     float b = src_b / 255.0f;
  38.  
  39.     float h, s, v;
  40.  
  41.     float max = fmax(r, fmax(g, b));
  42.     float min = fmin(r, fmin(g, b));
  43.  
  44.     v = max;
  45.  
  46.     if (max == 0.0f) {
  47.         s = 0;
  48.         h = 0;
  49.     } else if (max - min == 0.0f) {
  50.         s = 0;
  51.         h = 0;
  52.     } else {
  53.         s = (max - min) / max;
  54.  
  55.         if (max == r) {
  56.             h = 60 * ((g - b) / (max - min)) + 0;
  57.         } else if (max == g) {
  58.             h = 60 * ((b - r) / (max - min)) + 120;
  59.         } else {
  60.             h = 60 * ((r - g) / (max - min)) + 240;
  61.         }
  62.     }
  63.  
  64.     if (h < 0) h += 360.0f;
  65.  
  66.     *(dst_h) = (h / 2);
  67.     *(dst_s) = (s * 255);
  68.     *(dst_v) = (v * 255);
  69. }
  70.  
  71.  
  72. __private void hsv2rgb(const float src_h, const float src_s, const float src_v, int* dst_r, int* dst_g, int* dst_b) {
  73.  
  74. //    float h = (float) src_h;
  75.     float h = src_h * 2.0f;
  76.     float s = src_s / 255.0f;
  77.     float v = src_v / 255.0f;
  78.  
  79.     float r, g, b;
  80.  
  81.     int hi = (int) (h / 60.0f) % 6;
  82.     float f = (h / 60.0f) - hi;
  83.  
  84.     float p = v * (1.0f - s);
  85.     float q = v * (1.0f - s * f);
  86.     float t = v * (1.0f - s * (1.0f - f));
  87.  
  88.     switch (hi) {
  89.         case 0:
  90.             r = v, g = t, b = p;
  91.             break;
  92.         case 1:
  93.             r = q, g = v, b = p;
  94.             break;
  95.         case 2:
  96.             r = p, g = v, b = t;
  97.             break;
  98.         case 3:
  99.             r = p, g = q, b = v;
  100.             break;
  101.         case 4:
  102.             r = t, g = p, b = v;
  103.             break;
  104.         case 5:
  105.             r = v, g = p, b = q;
  106.             break;
  107.         default:
  108.             break;
  109.     }
  110.  
  111.     *(dst_r) = (int)(r * 255);
  112.     *(dst_g) = (int)(g * 255);
  113.     *(dst_b) = (int)(b * 255);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement