summaryrefslogtreecommitdiff
path: root/opencl/colors.cl
blob: 689a05302cafb36d19a50de87bdecf0f28843a87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/* This file is part of GEGL
 *
 * GEGL is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * GEGL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2012 Victor Oliveira (victormatheus@gmail.com)
 */

/* This is almost a copy-paste from babl/base color conversion functions

   XXX: This code is very hard to maintain and keep in sync with BABL, we should
        think in something better
*/

/* Alpha threshold used in the reference implementation for
 * un-pre-multiplication of color data:
 *
 * 0.01 / (2^16 - 1)
 */
#define BABL_ALPHA_THRESHOLD 0.0f

float linear_to_gamma_2_2 (float value);
float gamma_2_2_to_linear (float value);

/* babl reference file: babl/base/util.h */
float linear_to_gamma_2_2 (float value)
{
  if (value > 0.003130804954f)
    return 1.055f * native_powr (value, (1.0f/2.4f)) - 0.055f;
  return 12.92f * value;
}

float gamma_2_2_to_linear (float value)
{
  if (value > 0.04045f)
    return native_powr ((value + 0.055f) / 1.055f, 2.4f);
  return value / 12.92f;
}

/* -- RGBA float/u8 -- */

/* RGBA u8 -> RGBA float */
__kernel void rgbau8_to_rgbaf (__global const uchar4 * in,
                               __global       float4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = convert_float4(in[gid]) / 255.0f;
  float4 out_v = in_v;
  out[gid] = out_v;
}

/* RGBA float -> RGBA u8 */
__kernel void rgbaf_to_rgbau8 (__global const float4 * in,
                               __global       uchar4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 out_v = in_v;
  out[gid] = convert_uchar4_sat_rte(255.0f * out_v);
}


/* RGBA float -> RGB float */
__kernel void rgbaf_to_rgbf (__global const float4 * in,
                             __global       float  * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];

#if (__OPENCL_VERSION__ != CL_VERSION_1_0)
  vstore3 (in_v.xyz, gid, out);
#else
  out[3 * gid]     = in_v.x;
  out[3 * gid + 1] = in_v.y;
  out[3 * gid + 2] = in_v.z;
#endif
}

/* -- RaGaBaA float -- */

/* RGBA float -> RaGaBaA float */
__kernel void rgbaf_to_ragabaf (__global const float4 * in,
                                __global       float4 * out)
{
  int gid = get_global_id(0);
  float4 in_v = in[gid];
  float4 out_v;
  out_v   = in_v * in_v.w;
  out_v.w = in_v.w;
  out[gid] = out_v;
}


/* RaGaBaA float -> RGBA float */
__kernel void ragabaf_to_rgbaf (__global const float4 * in,
                                __global       float4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 out_v;
  out_v = (in_v.w > BABL_ALPHA_THRESHOLD)? in_v / in_v.w : (float4)(0.0f);
  out_v.w = in_v.w;
  out[gid] = out_v;
}

/* RGBA u8 -> RaGaBaA float */
__kernel void rgbau8_to_ragabaf (__global const uchar4 * in,
                                 __global       float4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = convert_float4(in[gid]) / 255.0f;
  float4 out_v;
  out_v   = in_v * in_v.w;
  out_v.w = in_v.w;
  out[gid] = out_v;
}


/* RaGaBaA float -> RGBA u8 */
__kernel void ragabaf_to_rgbau8 (__global const float4 * in,
                                 __global       uchar4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 out_v;
  out_v = (in_v.w > BABL_ALPHA_THRESHOLD)? in_v / in_v.w : (float4)(0.0f);
  out_v.w = in_v.w;
  out[gid] = convert_uchar4_sat_rte(255.0f * out_v);
}

/* RaGaBaA float -> RGBA_GAMMA_U8 */
__kernel void ragabaf_to_rgba_gamma_u8 (__global const float4 * in,
                                        __global       uchar4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 tmp_v;
  tmp_v = (in_v.w > BABL_ALPHA_THRESHOLD)? in_v / in_v.w : (float4)(0.0f);
  tmp_v.w = in_v.w;
  float4 out_v;
  out_v = (float4)(linear_to_gamma_2_2(tmp_v.x),
                   linear_to_gamma_2_2(tmp_v.y),
                   linear_to_gamma_2_2(tmp_v.z),
                   tmp_v.w);
  out[gid] = convert_uchar4_sat_rte(255.0f * out_v);
}

/* RaGaBaA float -> RGB_GAMMA_U8 */
__kernel void ragabaf_to_rgb_gamma_u8 (__global const float4 * in,
                                       __global       uchar  * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 tmp_v;
  tmp_v = (in_v.w > BABL_ALPHA_THRESHOLD)? in_v / in_v.w : (float4)(0.0f);
  tmp_v.w = in_v.w;
  float4 out_v;
  out_v = (float4)(linear_to_gamma_2_2(tmp_v.x),
                   linear_to_gamma_2_2(tmp_v.y),
                   linear_to_gamma_2_2(tmp_v.z),
                   tmp_v.w);
#if (__OPENCL_VERSION__ != CL_VERSION_1_0)
  vstore3 (convert_uchar3_sat_rte(255.0f * out_v.xyz), gid, out);
#else
  uchar4 sat = convert_uchar4_sat_rte (255.0f * out_v);
  out[3 * gid]     = sat.x;
  out[3 * gid + 1] = sat.y;
  out[3 * gid + 2] = sat.z;
#endif
}

/* -- R'G'B' float -- */

/* R'G'B' float -> RGBA float */
__kernel void rgb_gamma_f_to_rgbaf (__global const float * in,
                                    __global       float4 * out)
{
  int gid = get_global_id(0);
  float4 out_v;

#if (__OPENCL_VERSION__ != CL_VERSION_1_0)
  float3 in_v;
  in_v = vload3 (gid, in);
  out_v = (float4)(gamma_2_2_to_linear(in_v.x),
                   gamma_2_2_to_linear(in_v.y),
                   gamma_2_2_to_linear(in_v.z),
                   1.0f);
#else
  float r = in[3 * gid + 0];
  float g = in[3 * gid + 1];
  float b = in[3 * gid + 2];
  out_v = (float4)(gamma_2_2_to_linear(r),
                   gamma_2_2_to_linear(g),
                   gamma_2_2_to_linear(b),
                   1.0f);
#endif
  out[gid] = out_v;
}

/* -- R'G'B'A float -- */

/* rgba float -> r'g'b'a float */
__kernel void rgbaf_to_rgba_gamma_f (__global const float4 * in,
                                     __global       float4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 out_v;
  out_v = (float4)(linear_to_gamma_2_2(in_v.x),
                   linear_to_gamma_2_2(in_v.y),
                   linear_to_gamma_2_2(in_v.z),
                   in_v.w);
  out[gid] = out_v;
}

/* r'g'b'a float -> rgba float */
__kernel void rgba_gamma_f_to_rgbaf (__global const float4 * in,
                                     __global       float4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 out_v;
  out_v = (float4)(gamma_2_2_to_linear(in_v.x),
                   gamma_2_2_to_linear(in_v.y),
                   gamma_2_2_to_linear(in_v.z),
                   in_v.w);
  out[gid] = out_v;
}

/* rgba u8 -> r'g'b'a float */
__kernel void rgbau8_to_rgba_gamma_f (__global const uchar4 * in,
                                      __global       float4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = convert_float4(in[gid]) / 255.0f;
  float4 out_v;
  out_v = (float4)(linear_to_gamma_2_2(in_v.x),
                   linear_to_gamma_2_2(in_v.y),
                   linear_to_gamma_2_2(in_v.z),
                   in_v.w);
  out[gid] = out_v;
}

/* r'g'b'a float -> rgba u8 */
__kernel void rgba_gamma_f_to_rgbau8 (__global const float4 * in,
                                      __global       uchar4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 out_v;
  out_v = (float4)(gamma_2_2_to_linear(in_v.x),
                   gamma_2_2_to_linear(in_v.y),
                   gamma_2_2_to_linear(in_v.z),
                   in_v.w);
  out[gid] = convert_uchar4_sat_rte(255.0f * out_v);
}

/* -- Y'CbCrA float -- */

/* RGBA float -> Y'CbCrA float */
__kernel void rgbaf_to_ycbcraf (__global const float4 * in,
                                __global       float4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 out_v;

#if (__OPENCL_VERSION__ != CL_VERSION_1_0)
  float3 rgb = (float3)(linear_to_gamma_2_2(in_v.x),
                        linear_to_gamma_2_2(in_v.y),
                        linear_to_gamma_2_2(in_v.z));
#else
  float4 rgb = (float4)(linear_to_gamma_2_2(in_v.x),
                        linear_to_gamma_2_2(in_v.y),
                        linear_to_gamma_2_2(in_v.z), 1.0f);
#endif

  out_v = (float4)( 0.299f    * rgb.x + 0.587f    * rgb.y + 0.114f    * rgb.z,
                   -0.168736f * rgb.x - 0.331264f * rgb.y + 0.5f      * rgb.z,
                    0.5f      * rgb.x - 0.418688f * rgb.y - 0.081312f * rgb.z,
                   in_v.w);
  out[gid] = out_v;
}

/* Y'CbCrA float -> RGBA float */
__kernel void ycbcraf_to_rgbaf (__global const float4 * in,
                                __global       float4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 out_v;

  float4 rgb = (float4)(1.0f * in_v.x + 0.0f      * in_v.y + 1.40200f    * in_v.z,
                        1.0f * in_v.x - 0.344136f * in_v.y - 0.71414136f * in_v.z,
                        1.0f * in_v.x + 1.772f    * in_v.y + 0.0f        * in_v.z,
                        0.0f);

  out_v = (float4)(gamma_2_2_to_linear(rgb.x),
                   gamma_2_2_to_linear(rgb.y),
                   gamma_2_2_to_linear(rgb.z),
                   in_v.w);
  out[gid] = out_v;
}

/* RGBA u8 -> Y'CbCrA float */
__kernel void rgbau8_to_ycbcraf (__global const uchar4 * in,
                                 __global       float4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = convert_float4(in[gid]) / 255.0f;
  float4 out_v;

  float4 rgb = (float4)(linear_to_gamma_2_2(in_v.x),
                        linear_to_gamma_2_2(in_v.y),
                        linear_to_gamma_2_2(in_v.z),
                        0.0f);

  out_v = (float4)( 0.299f    * rgb.x + 0.587f    * rgb.y + 0.114f    * rgb.z,
                   -0.168736f * rgb.x - 0.331264f * rgb.y + 0.5f      * rgb.z,
                    0.5f      * rgb.x - 0.418688f * rgb.y - 0.081312f * rgb.z,
                   in_v.w);
  out[gid] = out_v;
}

/* Y'CbCrA float -> RGBA u8 */
__kernel void ycbcraf_to_rgbau8 (__global const float4 * in,
                                 __global       uchar4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 out_v;

  float4 rgb = (float4)(1.0f * in_v.x + 0.0f      * in_v.y + 1.40200f    * in_v.z,
                        1.0f * in_v.x - 0.344136f * in_v.y - 0.71414136f * in_v.z,
                        1.0f * in_v.x + 1.772f    * in_v.y + 0.0f        * in_v.z,
                        0.0f);

  out_v = (float4)(gamma_2_2_to_linear(rgb.x),
                   gamma_2_2_to_linear(rgb.y),
                   gamma_2_2_to_linear(rgb.z),
                   in_v.w);
  out[gid] = convert_uchar4_sat_rte(255.0f * out_v);
}

/* -- RGB u8 -- */

/* RGB u8 -> RGBA float */
__kernel void rgbu8_to_rgbaf (__global const uchar  * in,
                              __global       float4 * out)
{
  int gid = get_global_id(0);
#if (__OPENCL_VERSION__ != CL_VERSION_1_0)
  uchar3 in_v;
  float4 out_v;
  in_v = vload3 (gid, in);
  out_v.xyz = convert_float3(in_v) / 255.0f;
  out_v.w   = 1.0f;
#else
  float4 in_v = (float4) (in[3 * gid], in[3 * gid + 1], in[3 * gid + 2], 255.0f);
  float4 out_v = in_v / (float4) (255.0f);
#endif
  out[gid]  = out_v;
}

/* RGBA float -> RGB u8 */
__kernel void rgbaf_to_rgbu8 (__global const float4 * in,
                              __global       uchar  * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
#if (__OPENCL_VERSION__ != CL_VERSION_1_0)
  uchar3 out_v = convert_uchar3_sat_rte(255.0f * in_v.xyz);
  vstore3 (out_v, gid, out);
#else
  uchar4 out_v = convert_uchar4_sat_rte(255.0f * in_v);
  out[3 * gid]     = out_v.x;
  out[3 * gid + 1] = out_v.y;
  out[3 * gid + 2] = out_v.z;
#endif
}

/* -- Y u8 -- */

/* Y u8 -> Y float */
__kernel void yu8_to_yf (__global const uchar * in,
                         __global       float * out)
{
  int gid = get_global_id(0);
  float in_v  = convert_float (in[gid]) / 255.0f;
  float out_v;
  out_v = in_v;
  out[gid] = out_v;
}

/* -- Y float -- */

/* Y float -> RaGaBaA float */
__kernel void yf_to_ragabaf (__global const float * in,
                              __global       float4 * out)
{
  int gid = get_global_id(0);
  float  y  = in[gid];
  float4 out_v = (float4) (y, y, y, 1.0f);

  out[gid] = out_v;
}

/* -- YA float -- */

/* babl reference file: babl/base/rgb-constants.h */
#define RGB_LUMINANCE_RED    (0.222491f)
#define RGB_LUMINANCE_GREEN  (0.716888f)
#define RGB_LUMINANCE_BLUE   (0.060621f)

/* RGBA float -> YA float */
__kernel void rgbaf_to_yaf (__global const float4 * in,
                            __global       float2 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float2 out_v;

  float luminance = in_v.x * RGB_LUMINANCE_RED +
                    in_v.y * RGB_LUMINANCE_GREEN +
                    in_v.z * RGB_LUMINANCE_BLUE;

  out_v.x = luminance;
  out_v.y = in_v.w;

  out[gid] = out_v;
}

/* RaGaBaA float -> YA float */
__kernel void ragabaf_to_yaf (__global const float4 * in,
                              __global       float2 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 tmp_v = in_v / (float4) (in_v.w);

  float luminance = tmp_v.x * RGB_LUMINANCE_RED +
                    tmp_v.y * RGB_LUMINANCE_GREEN +
                    tmp_v.z * RGB_LUMINANCE_BLUE;

  out[gid] = (float2) (luminance, in_v.w);
}

/* YA float -> RGBA float */
__kernel void yaf_to_rgbaf (__global const float2 * in,
                            __global       float4 * out)
{
  int gid = get_global_id(0);
  float2 in_v  = in[gid];
  float4 out_v = (float4) (in_v.x, in_v.x, in_v.x, in_v.y);

  out[gid] = out_v;
}

/* YA float -> RaGaBaA float */
__kernel void yaf_to_ragabaf (__global const float2 * in,
                              __global       float4 * out)
{
  int gid = get_global_id(0);
  float2 in_v  = in[gid];
  float4 out_v = (float4) (in_v.x * in_v.y,
                           in_v.x * in_v.y,
                           in_v.x * in_v.y,
                           in_v.y);

  out[gid] = out_v;
}

/* RGBA u8 -> YA float */
__kernel void rgbau8_to_yaf (__global const uchar4 * in,
                             __global       float2 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = convert_float4(in[gid]) / 255.0f;
  float2 out_v;

  float luminance = in_v.x * RGB_LUMINANCE_RED +
                    in_v.y * RGB_LUMINANCE_GREEN +
                    in_v.z * RGB_LUMINANCE_BLUE;

  out_v.x = luminance;
  out_v.y = in_v.w;

  out[gid] = out_v;
}

/* YA float -> RGBA u8 */
__kernel void yaf_to_rgbau8 (__global const float2 * in,
                             __global       uchar4 * out)
{
  int gid = get_global_id(0);
  float2 in_v  = in[gid];
  float4 out_v = (float4) (in_v.x, in_v.x, in_v.x, in_v.y);

  out[gid] = convert_uchar4_sat_rte(255.0f * out_v);
}

/* YA float -> R'G'B'A u8 */
__kernel void yaf_to_rgba_gamma_u8 (__global const float2 * in,
                                    __global       uchar4 * out)
{
  int gid = get_global_id(0);
  float2 in_v  = in[gid];
  float4 tmp_v = (float4) (in_v.x, in_v.x, in_v.x, in_v.y);

  float4 out_v;
  out_v = (float4)(linear_to_gamma_2_2(tmp_v.x),
                   linear_to_gamma_2_2(tmp_v.y),
                   linear_to_gamma_2_2(tmp_v.z),
                   tmp_v.w);
  out[gid] = convert_uchar4_sat_rte(255.0f * out_v);
}

/* YA float -> R'G'B' u8 */
__kernel void yaf_to_rgb_gamma_u8 (__global const float2 * in,
                                   __global       uchar  * out)
{
  int gid = get_global_id(0);
  float2 in_v  = in[gid];
  uchar  tmp = convert_uchar_sat_rte (255.0f * linear_to_gamma_2_2 (in_v.x));

#if (__OPENCL_VERSION__ != CL_VERSION_1_0)
  vstore3 ((uchar3)tmp, gid, out);
#else
  out[3 * gid] = out[3 * gid + 1] = out[3 * gid + 2] = tmp;
#endif
}

/* R'G'B'A u8 */

/* rgba float -> r'g'b'a u8 */
__kernel void rgbaf_to_rgba_gamma_u8 (__global const float4 * in,
                                      __global       uchar4 * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 out_v;
  out_v = (float4)(linear_to_gamma_2_2(in_v.x),
                   linear_to_gamma_2_2(in_v.y),
                   linear_to_gamma_2_2(in_v.z),
                   in_v.w);
  out[gid] = convert_uchar4_sat_rte(255.0f * out_v);
}

/* R'G'B' u8 */

/* rgba float -> r'g'b' u8 */
__kernel void rgbaf_to_rgb_gamma_u8 (__global const float4 * in,
                                     __global       uchar  * out)
{
  int gid = get_global_id(0);
  float4 in_v  = in[gid];
  float4 tmp_v;
  tmp_v = (float4)(linear_to_gamma_2_2(in_v.x),
                   linear_to_gamma_2_2(in_v.y),
                   linear_to_gamma_2_2(in_v.z),
                   in_v.w);
#if (__OPENCL_VERSION__ != CL_VERSION_1_0)
  uchar3 out_v;
  out_v = convert_uchar3_sat_rte(255.0f * tmp_v.xyz);
  vstore3 (out_v, gid, out);
#else
  uchar4 out_v = convert_uchar4_sat_rte (255.0f * tmp_v);
  out[3 * gid]     = out_v.x;
  out[3 * gid + 1] = out_v.y;
  out[3 * gid + 2] = out_v.z;
#endif
}