Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Thibault Durnez
- */
- __private void rgb2hsv(const int src_r, const int src_g, const int src_b, float* dst_h, float* dst_s, float* dst_v);
- __private void hsv2rgb(const float src_h, const float src_s, const float src_v, int* dst_r, int* dst_g, int* dst_b);
- __kernel void hueshift(__global const int *inputImage, __global int *outputImage, int hueshift_param) {
- const size_t rowcol = (get_global_id(1) * get_global_size(0) + get_global_id(0));
- int color = inputImage[rowcol];
- int blue = (color & 0xFF0000) >> 16;
- int green = (color & 0x00FF00) >> 8;
- int red = (color & 0x0000FF);
- float hue = 0;
- float saturation = 0;
- float value = 0;
- rgb2hsv(blue, green, red, &hue, &saturation, &value);
- hsv2rgb(fmod((hue + hueshift_param),360), saturation, value, &blue, &green, &red);
- int _blue = (blue << 16) & 0xFF0000;
- int _green = (green << 8) & 0x00FF00;
- int _red = (red << 0) & 0x0000FF;
- int newColor = (color & 0xFF000000) | _blue | _green | _red;
- outputImage[rowcol] = newColor;
- }
- __private void rgb2hsv(const int src_r, const int src_g, const int src_b, float* dst_h, float* dst_s, float* dst_v) {
- float r = src_r / 255.0f;
- float g = src_g / 255.0f;
- float b = src_b / 255.0f;
- float h, s, v;
- float max = fmax(r, fmax(g, b));
- float min = fmin(r, fmin(g, b));
- v = max;
- if (max == 0.0f) {
- s = 0;
- h = 0;
- } else if (max - min == 0.0f) {
- s = 0;
- h = 0;
- } else {
- s = (max - min) / max;
- if (max == r) {
- h = 60 * ((g - b) / (max - min)) + 0;
- } else if (max == g) {
- h = 60 * ((b - r) / (max - min)) + 120;
- } else {
- h = 60 * ((r - g) / (max - min)) + 240;
- }
- }
- if (h < 0) h += 360.0f;
- *(dst_h) = (h / 2);
- *(dst_s) = (s * 255);
- *(dst_v) = (v * 255);
- }
- __private void hsv2rgb(const float src_h, const float src_s, const float src_v, int* dst_r, int* dst_g, int* dst_b) {
- // float h = (float) src_h;
- float h = src_h * 2.0f;
- float s = src_s / 255.0f;
- float v = src_v / 255.0f;
- float r, g, b;
- int hi = (int) (h / 60.0f) % 6;
- float f = (h / 60.0f) - hi;
- float p = v * (1.0f - s);
- float q = v * (1.0f - s * f);
- float t = v * (1.0f - s * (1.0f - f));
- switch (hi) {
- case 0:
- r = v, g = t, b = p;
- break;
- case 1:
- r = q, g = v, b = p;
- break;
- case 2:
- r = p, g = v, b = t;
- break;
- case 3:
- r = p, g = q, b = v;
- break;
- case 4:
- r = t, g = p, b = v;
- break;
- case 5:
- r = v, g = p, b = q;
- break;
- default:
- break;
- }
- *(dst_r) = (int)(r * 255);
- *(dst_g) = (int)(g * 255);
- *(dst_b) = (int)(b * 255);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement