forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.cl
More file actions
1269 lines (998 loc) · 35 KB
/
Copy pathbasic.cl
File metadata and controls
1269 lines (998 loc) · 35 KB
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of darktable,
copyright (c) 2009--2012 johannes hanika.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
const sampler_t sampleri = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
const sampler_t samplerf = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_LINEAR;
#ifndef M_PI
#define M_PI 3.14159265358979323846 // should be defined by the OpenCL compiler acc. to standard
#endif
int
FC(const int row, const int col, const unsigned int filters)
{
return filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3;
}
kernel void
whitebalance_1ui(read_only image2d_t in, write_only image2d_t out, const int width, const int height, global float *coeffs,
const unsigned int filters, const int rx, const int ry)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
const uint4 pixel = read_imageui(in, sampleri, (int2)(x, y));
write_imagef (out, (int2)(x, y), (float4)(pixel.x * coeffs[FC(ry+y, rx+x, filters)], 0.0f, 0.0f, 0.0f));
}
kernel void
whitebalance_1f(read_only image2d_t in, write_only image2d_t out, const int width, const int height, global float *coeffs,
const unsigned int filters, const int rx, const int ry)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
const float pixel = read_imagef(in, sampleri, (int2)(x, y)).x;
write_imagef (out, (int2)(x, y), (float4)(pixel * coeffs[FC(rx+y, ry+x, filters)], 0.0f, 0.0f, 0.0f));
}
kernel void
whitebalance_4f(read_only image2d_t in, write_only image2d_t out, const int width, const int height, global float *coeffs,
const unsigned int filters, const int rx, const int ry)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
const float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
write_imagef (out, (int2)(x, y), (float4)(pixel.x * coeffs[0], pixel.y * coeffs[1], pixel.z * coeffs[2], pixel.w));
}
/* kernel for the exposure plugin. should work transparently with float4 and float image2d. */
kernel void
exposure (read_only image2d_t in, write_only image2d_t out, const int width, const int height, const float black, const float scale)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
pixel.xyz = (pixel.xyz - black)*scale;
write_imagef (out, (int2)(x, y), pixel);
}
/* kernel for the highlights plugin. */
kernel void
highlights_4f (read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const int mode, const float clip)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
switch(mode)
{
case 1: // DT_IOP_HIGHLIGHTS_LCH
if(pixel.x > clip || pixel.y > clip || pixel.z > clip)
pixel.x = pixel.y = pixel.z = 0.299f * pixel.x + 0.587f * pixel.y + 0.144f*pixel.z;
break;
default: // 0, DT_IOP_HIGHLIGHTS_CLIP
pixel.x = fmin(clip, pixel.x);
pixel.y = fmin(clip, pixel.y);
pixel.z = fmin(clip, pixel.z);
break;
}
write_imagef (out, (int2)(x, y), pixel);
}
kernel void
highlights_1f (read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const int mode, const float clip, const int rx, const int ry, const int filters)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
// just carry over other 3 channels:
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
float lum[3] = {0.299f, 0.587f, 0.144f};
float accum[3] = {0.0f, 0.0f, 0.0f};
int cnt[3] = {0, 0, 0};
switch(mode)
{
case 1: // DT_IOP_HIGHLIGHTS_LCH
if(pixel.x > clip)
{
// go through all 9 neighbours
for(int jj=-1;jj<=1;jj++)
{
for(int ii=-1;ii<=1;ii++)
{
float px = read_imagef(in, sampleri, (int2)(x+ii, y+jj)).x;
if(px > clip)
{
int c = FC(ry+y+jj, rx+x+ii, filters);
accum[c] += lum[c]*px;
cnt[c] ++;
}
}
}
if(cnt[0] && cnt[1] && cnt[2])
{
pixel.x = 0.0f;
for(int c=0;c<3;c++)
pixel.x += accum[c]/cnt[c];
}
else pixel.x = clip;
}
break;
default: // 0, DT_IOP_HIGHLIGHTS_CLIP
pixel.x = fmin(clip, pixel.x);
break;
}
write_imagef (out, (int2)(x, y), pixel);
}
float
lookup_unbounded(read_only image2d_t lut, const float x, global float *a)
{
// in case the tone curve is marked as linear, return the fast
// path to linear unbounded (does not clip x at 1)
if(a[0] >= 0.0f)
{
if(x < 1.0f/a[0])
{
const int xi = clamp(x*65535.0f, 0.0f, 65535.0f);
const int2 p = (int2)((xi & 0xff), (xi >> 8));
return read_imagef(lut, sampleri, p).x;
}
else return a[1] * native_powr(x*a[0], a[2]);
}
else return x;
}
float
lookup(read_only image2d_t lut, const float x)
{
int xi = clamp(x*65535.0f, 0.0f, 65535.0f);
int2 p = (int2)((xi & 0xff), (xi >> 8));
return read_imagef(lut, sampleri, p).x;
}
/* kernel for the basecurve plugin. */
kernel void
basecurve (read_only image2d_t in, write_only image2d_t out, const int width, const int height,
read_only image2d_t table, global float *a)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
// use lut or extrapolation:
pixel.x = lookup_unbounded(table, pixel.x, a);
pixel.y = lookup_unbounded(table, pixel.y, a);
pixel.z = lookup_unbounded(table, pixel.z, a);
write_imagef (out, (int2)(x, y), pixel);
}
void
XYZ_to_Lab(float *xyz, float *lab)
{
xyz[0] *= (1.0f/0.9642f);
xyz[2] *= (1.0f/0.8242f);
for (int c=0; c<3; c++)
xyz[c] = xyz[c] > 0.008856f ? native_powr(xyz[c], 1.0f/3.0f) : 7.787f*xyz[c] + 16.0f/116.0f;
lab[0] = 116.0f * xyz[1] - 16.0f;
lab[1] = 500.0f * (xyz[0] - xyz[1]);
lab[2] = 200.0f * (xyz[1] - xyz[2]);
}
/* kernel for the plugin colorin */
kernel void
colorin (read_only image2d_t in, write_only image2d_t out, const int width, const int height,
global float *mat, read_only image2d_t lutr, read_only image2d_t lutg, read_only image2d_t lutb,
const int map_blues,
global float *a)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
float cam[3], XYZ[3], Lab[3];
cam[0] = lookup_unbounded(lutr, pixel.x, a);
cam[1] = lookup_unbounded(lutg, pixel.y, a+3);
cam[2] = lookup_unbounded(lutb, pixel.z, a+6);
const float YY = cam[0]+cam[1]+cam[2];
if(map_blues && YY > 0.0f)
{
// manual gamut mapping. these values cause trouble when converting back from Lab to sRGB:
const float zz = cam[2]/YY;
// lower amount and higher bound_z make the effect smaller.
// the effect is weakened the darker input values are, saturating at bound_Y
const float bound_z = 0.5f, bound_Y = 0.8f;
const float amount = 0.11f;
if (zz > bound_z)
{
const float t = (zz - bound_z)/(1.0f-bound_z) * fmin(1.0f, YY/bound_Y);
cam[1] += t*amount;
cam[2] -= t*amount;
}
}
// now convert camera to XYZ using the color matrix
for(int j=0;j<3;j++)
{
XYZ[j] = 0.0f;
for(int i=0;i<3;i++) XYZ[j] += mat[3*j+i] * cam[i];
}
XYZ_to_Lab(XYZ, (float *)&pixel);
write_imagef (out, (int2)(x, y), pixel);
}
/* kernel for the tonecurve plugin version 2 */
kernel void
tonecurve (read_only image2d_t in, write_only image2d_t out, const int width, const int height,
read_only image2d_t table_L, read_only image2d_t table_a, read_only image2d_t table_b,
const int autoscale_ab, global float *a)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
const float L_in = pixel.x/100.0f;
// use lut or extrapolation:
const float L = lookup_unbounded(table_L, L_in, a);
if (autoscale_ab == 0)
{
const float a_in = (pixel.y + 128.0f) / 256.0f;
const float b_in = (pixel.z + 128.0f) / 256.0f;
pixel.y = lookup(table_a, a_in);
pixel.z = lookup(table_b, b_in);
}
else if(pixel.x > 0.01f)
{
pixel.y *= L/pixel.x;
pixel.z *= L/pixel.x;
}
else
{
pixel.y *= L/0.01f;
pixel.z *= L/0.01f;
}
pixel.x = L;
write_imagef (out, (int2)(x, y), pixel);
}
/* kernel for the colorcorrection plugin. */
__kernel void
colorcorrection (read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const float saturation, const float a_scale, const float a_base,
const float b_scale, const float b_base)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
pixel.y = saturation*(pixel.y + pixel.x * a_scale + a_base);
pixel.z = saturation*(pixel.z + pixel.x * b_scale + b_base);
write_imagef (out, (int2)(x, y), pixel);
}
void
mul_mat_vec_2(const float4 m, const float2 *p, float2 *o)
{
(*o).x = (*p).x*m.x + (*p).y*m.y;
(*o).y = (*p).x*m.z + (*p).y*m.w;
}
void
backtransform(float2 *p, float2 *o, const float4 m, const float2 t)
{
(*p).y /= (1.0f + (*p).x*t.x);
(*p).x /= (1.0f + (*p).y*t.y);
mul_mat_vec_2(m, p, o);
}
float
interpolation_func_bicubic(float t)
{
float r;
t = fabs(t);
r = (t >= 2.0f) ? 0.0f : ((t > 1.0f) ? (0.5f*(t*(-t*t + 5.0f*t - 8.0f) + 4.0f)) : (0.5f*(t*(3.0f*t*t - 5.0f*t) + 2.0f)));
return r;
}
#define DT_LANCZOS_EPSILON (1e-9f)
#if 0
float
interpolation_func_lanczos(float width, float t)
{
float ta = fabs(t);
float r = (ta > width) ? 0.0f : ((ta < DT_LANCZOS_EPSILON) ? 1.0f : width*native_sin(M_PI*t)*native_sin(M_PI*t/width)/(M_PI*M_PI*t*t));
return r;
}
#else
float
sinf_fast(float t)
{
const float a = 4/(M_PI*M_PI);
const float p = 0.225f;
t = a*t*(M_PI - fabs(t));
return p*(t*fabs(t) - t) + t;
}
float
interpolation_func_lanczos(float width, float t)
{
/* Compute a value for sinf(pi.t) in [-pi pi] for which the value will be
* correct */
int a = (int)t;
float r = t - (float)a;
// Compute the correct sign for sinf(pi.r)
union { float f; unsigned int i; } sign;
sign.i = ((a&1)<<31) | 0x3f800000;
return (DT_LANCZOS_EPSILON + width*sign.f*sinf_fast(M_PI*r)*sinf_fast(M_PI*t/width))/(DT_LANCZOS_EPSILON + M_PI*M_PI*t*t);
}
#endif
/* kernel for clip&rotate: bilinear interpolation */
__kernel void
clip_rotate_bilinear(read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const int in_width, const int in_height,
const int2 roi_in, const int2 roi_out, const float scale_in, const float scale_out,
const int flip, const float2 ci, const float2 t, const float2 k, const float4 mat)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float2 pi, po;
pi.x = roi_out.x + scale_out * ci.x + x + 0.5f;
pi.y = roi_out.y + scale_out * ci.y + y + 0.5f;
pi.x -= flip ? t.y * scale_out : t.x * scale_out;
pi.y -= flip ? t.x * scale_out : t.y * scale_out;
pi /= scale_out;
backtransform(&pi, &po, mat, k);
po *= scale_in;
po.x += t.x * scale_in;
po.y += t.y * scale_in;
po.x -= roi_in.x;
po.y -= roi_in.y;
const int ii = (int)po.x;
const int jj = (int)po.y;
float4 o;
if (ii >=0 && jj >= 0 && ii <= in_width-2 && jj <= in_height-2)
o = read_imagef(in, samplerf, po);
else
o = (float4)0.0f;
write_imagef (out, (int2)(x, y), o);
}
/* kernel for clip&rotate: bicubic interpolation */
__kernel void
clip_rotate_bicubic(read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const int in_width, const int in_height,
const int2 roi_in, const int2 roi_out, const float scale_in, const float scale_out,
const int flip, const float2 ci, const float2 t, const float2 k, const float4 mat)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
const int kwidth = 2;
if(x >= width || y >= height) return;
float2 pi, po;
pi.x = roi_out.x + scale_out * ci.x + x + 0.5f;
pi.y = roi_out.y + scale_out * ci.y + y + 0.5f;
pi.x -= flip ? t.y * scale_out : t.x * scale_out;
pi.y -= flip ? t.x * scale_out : t.y * scale_out;
pi /= scale_out;
backtransform(&pi, &po, mat, k);
po *= scale_in;
po.x += t.x * scale_in;
po.y += t.y * scale_in;
po.x -= roi_in.x;
po.y -= roi_in.y;
int tx = po.x;
int ty = po.y;
float4 pixel = (float4)0.0f;
float weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_bicubic((float)(tx + ii) - po.x);
float wy = interpolation_func_bicubic((float)(ty + jj) - po.y);
float w = wx * wy;
pixel += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)) * w;
weight += w;
}
write_imagef (out, (int2)(x, y), pixel / weight);
}
/* kernel for clip&rotate: lanczos2 interpolation */
__kernel void
clip_rotate_lanczos2(read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const int in_width, const int in_height,
const int2 roi_in, const int2 roi_out, const float scale_in, const float scale_out,
const int flip, const float2 ci, const float2 t, const float2 k, const float4 mat)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
const int kwidth = 2;
if(x >= width || y >= height) return;
float2 pi, po;
pi.x = roi_out.x + scale_out * ci.x + x + 0.5f;
pi.y = roi_out.y + scale_out * ci.y + y + 0.5f;
pi.x -= flip ? t.y * scale_out : t.x * scale_out;
pi.y -= flip ? t.x * scale_out : t.y * scale_out;
pi /= scale_out;
backtransform(&pi, &po, mat, k);
po *= scale_in;
po.x += t.x * scale_in;
po.y += t.y * scale_in;
po.x -= roi_in.x;
po.y -= roi_in.y;
int tx = po.x;
int ty = po.y;
float4 pixel = (float4)0.0f;
float weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_lanczos(2, (float)(tx + ii) - po.x);
float wy = interpolation_func_lanczos(2, (float)(ty + jj) - po.y);
float w = wx * wy;
pixel += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)) * w;
weight += w;
}
write_imagef (out, (int2)(x, y), pixel / weight);
}
/* kernel for clip&rotate: lanczos3 interpolation */
__kernel void
clip_rotate_lanczos3(read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const int in_width, const int in_height,
const int2 roi_in, const int2 roi_out, const float scale_in, const float scale_out,
const int flip, const float2 ci, const float2 t, const float2 k, const float4 mat)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
const int kwidth = 3;
if(x >= width || y >= height) return;
float2 pi, po;
pi.x = roi_out.x + scale_out * ci.x + x + 0.5f;
pi.y = roi_out.y + scale_out * ci.y + y + 0.5f;
pi.x -= flip ? t.y * scale_out : t.x * scale_out;
pi.y -= flip ? t.x * scale_out : t.y * scale_out;
pi /= scale_out;
backtransform(&pi, &po, mat, k);
po *= scale_in;
po.x += t.x * scale_in;
po.y += t.y * scale_in;
po.x -= roi_in.x;
po.y -= roi_in.y;
int tx = po.x;
int ty = po.y;
float4 pixel = (float4)0.0f;
float weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_lanczos(3, (float)(tx + ii) - po.x);
float wy = interpolation_func_lanczos(3, (float)(ty + jj) - po.y);
float w = wx * wy;
pixel += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)) * w;
weight += w;
}
write_imagef (out, (int2)(x, y), pixel / weight);
}
/* kernels for the lens plugin: bilinear interpolation */
kernel void
lens_distort_bilinear (read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const int iwidth, const int iheight, const int roi_in_x, const int roi_in_y, global float *pi)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel;
float rx, ry;
const int piwidth = 2*3*width;
global float *ppi = pi + mad24(y, piwidth, 2*3*x);
rx = ppi[0] - roi_in_x;
ry = ppi[1] - roi_in_y;
pixel.x = (rx >= 0 && ry >= 0 && rx <= iwidth - 1 && ry <= iheight - 1) ? read_imagef(in, samplerf, (float2)(rx, ry)).x : 0.0f;
rx = ppi[2] - roi_in_x;
ry = ppi[3] - roi_in_y;
pixel.y = (rx >= 0 && ry >= 0 && rx <= iwidth - 1 && ry <= iheight - 1) ? read_imagef(in, samplerf, (float2)(rx, ry)).y : 0.0f;
pixel.w = (rx >= 0 && ry >= 0 && rx <= iwidth - 1 && ry <= iheight - 1) ? read_imagef(in, samplerf, (float2)(rx, ry)).w : 0.0f;
rx = ppi[4] - roi_in_x;
ry = ppi[5] - roi_in_y;
pixel.z = (rx >= 0 && ry >= 0 && rx <= iwidth - 1 && ry <= iheight - 1) ? read_imagef(in, samplerf, (float2)(rx, ry)).z : 0.0f;
write_imagef (out, (int2)(x, y), pixel);
}
/* kernels for the lens plugin: bicubic interpolation */
kernel void
lens_distort_bicubic (read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const int iwidth, const int iheight, const int roi_in_x, const int roi_in_y, global float *pi)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
const int kwidth = 2;
if(x >= width || y >= height) return;
float4 pixel = (float4)0.0f;
float rx, ry;
int tx, ty;
float sum, weight;
float2 sum2;
const int piwidth = 2*3*width;
global float *ppi = pi + mad24(y, piwidth, 2*3*x);
rx = ppi[0] - (float)roi_in_x;
ry = ppi[1] - (float)roi_in_y;
tx = rx;
ty = ry;
sum = 0.0f;
weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_bicubic((float)(tx + ii) - rx);
float wy = interpolation_func_bicubic((float)(ty + jj) - ry);
float w = wx * wy;
sum += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)).x * w;
weight += w;
}
pixel.x = (tx >= 0 && ty >= 0 && tx <= iwidth - 1 && ty <= iheight - 1) ? sum/weight : 0.0f;
rx = ppi[2] - (float)roi_in_x;
ry = ppi[3] - (float)roi_in_y;
tx = rx;
ty = ry;
sum2 = (float2)0.0f;
weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_bicubic((float)(tx + ii) - rx);
float wy = interpolation_func_bicubic((float)(ty + jj) - ry);
float w = wx * wy;
sum2 += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)).yw * w;
weight += w;
}
pixel.yw = (tx >= 0 && ty >= 0 && tx <= iwidth - 1 && ty <= iheight - 1) ? sum2/weight : (float2)0.0f;
rx = ppi[4] - (float)roi_in_x;
ry = ppi[5] - (float)roi_in_y;
tx = rx;
ty = ry;
sum = 0.0f;
weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_bicubic((float)(tx + ii) - rx);
float wy = interpolation_func_bicubic((float)(ty + jj) - ry);
float w = wx * wy;
sum += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)).z * w;
weight += w;
}
pixel.z = (tx >= 0 && ty >= 0 && tx <= iwidth - 1 && ty <= iheight - 1) ? sum/weight : 0.0f;
write_imagef (out, (int2)(x, y), pixel);
}
/* kernels for the lens plugin: lanczos2 interpolation */
kernel void
lens_distort_lanczos2 (read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const int iwidth, const int iheight, const int roi_in_x, const int roi_in_y, global float *pi)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
const int kwidth = 2;
if(x >= width || y >= height) return;
float4 pixel = (float4)0.0f;
float rx, ry;
int tx, ty;
float sum, weight;
float2 sum2;
const int piwidth = 2*3*width;
global float *ppi = pi + mad24(y, piwidth, 2*3*x);
rx = ppi[0] - (float)roi_in_x;
ry = ppi[1] - (float)roi_in_y;
tx = rx;
ty = ry;
sum = 0.0f;
weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_lanczos(2, (float)(tx + ii) - rx);
float wy = interpolation_func_lanczos(2, (float)(ty + jj) - ry);
float w = wx * wy;
sum += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)).x * w;
weight += w;
}
pixel.x = (tx >= 0 && ty >= 0 && tx <= iwidth - 1 && ty <= iheight - 1) ? sum/weight : 0.0f;
rx = ppi[2] - (float)roi_in_x;
ry = ppi[3] - (float)roi_in_y;
tx = rx;
ty = ry;
sum2 = (float2)0.0f;
weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_lanczos(2, (float)(tx + ii) - rx);
float wy = interpolation_func_lanczos(2, (float)(ty + jj) - ry);
float w = wx * wy;
sum2 += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)).yw * w;
weight += w;
}
pixel.yw = (tx >= 0 && ty >= 0 && tx <= iwidth - 1 && ty <= iheight - 1) ? sum2/weight : (float2)0.0f;
rx = ppi[4] - (float)roi_in_x;
ry = ppi[5] - (float)roi_in_y;
tx = rx;
ty = ry;
sum = 0.0f;
weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_lanczos(2, (float)(tx + ii) - rx);
float wy = interpolation_func_lanczos(2, (float)(ty + jj) - ry);
float w = wx * wy;
sum += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)).z * w;
weight += w;
}
pixel.z = (tx >= 0 && ty >= 0 && tx <= iwidth - 1 && ty <= iheight - 1) ? sum/weight : 0.0f;
write_imagef (out, (int2)(x, y), pixel);
}
/* kernels for the lens plugin: lanczos3 interpolation */
kernel void
lens_distort_lanczos3 (read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const int iwidth, const int iheight, const int roi_in_x, const int roi_in_y, global float *pi)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
const int kwidth = 3;
if(x >= width || y >= height) return;
float4 pixel = (float4)0.0f;
float rx, ry;
int tx, ty;
float sum, weight;
float2 sum2;
const int piwidth = 2*3*width;
global float *ppi = pi + mad24(y, piwidth, 2*3*x);
rx = ppi[0] - (float)roi_in_x;
ry = ppi[1] - (float)roi_in_y;
tx = rx;
ty = ry;
sum = 0.0f;
weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_lanczos(3, (float)(tx + ii) - rx);
float wy = interpolation_func_lanczos(3, (float)(ty + jj) - ry);
float w = wx * wy;
sum += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)).x * w;
weight += w;
}
pixel.x = (tx >= 0 && ty >= 0 && tx <= iwidth - 1 && ty <= iheight - 1) ? sum/weight : 0.0f;
rx = ppi[2] - (float)roi_in_x;
ry = ppi[3] - (float)roi_in_y;
tx = rx;
ty = ry;
sum2 = (float2)0.0f;
weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_lanczos(3, (float)(tx + ii) - rx);
float wy = interpolation_func_lanczos(3, (float)(ty + jj) - ry);
float w = wx * wy;
sum2 += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)).yw * w;
weight += w;
}
pixel.yw = (tx >= 0 && ty >= 0 && tx <= iwidth - 1 && ty <= iheight - 1) ? sum2/weight : (float2)0.0f;
rx = ppi[4] - (float)roi_in_x;
ry = ppi[5] - (float)roi_in_y;
tx = rx;
ty = ry;
sum = 0.0f;
weight = 0.0f;
for(int jj = 1 - kwidth; jj <= kwidth; jj++)
for(int ii= 1 - kwidth; ii <= kwidth; ii++)
{
float wx = interpolation_func_lanczos(3, (float)(tx + ii) - rx);
float wy = interpolation_func_lanczos(3, (float)(ty + jj) - ry);
float w = wx * wy;
sum += read_imagef(in, sampleri, (int2)(tx + ii, ty + jj)).z * w;
weight += w;
}
pixel.z = (tx >= 0 && ty >= 0 && tx <= iwidth - 1 && ty <= iheight - 1) ? sum/weight : 0.0f;
write_imagef (out, (int2)(x, y), pixel);
}
kernel void
lens_vignette (read_only image2d_t in, write_only image2d_t out, const int width, const int height, global float4 *pi)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
float4 scale = pi[mad24(y, width, x)]/(float4)0.5f;
pixel.xyz *= scale.xyz;
write_imagef (out, (int2)(x, y), pixel);
}
/* kernel for flip */
__kernel void
flip(read_only image2d_t in, write_only image2d_t out, const int width, const int height, const int orientation)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
int nx = (orientation & 4) ? y : x;
int ny = (orientation & 4) ? x : y;
int wd = (orientation & 4) ? height : width;
int ht = (orientation & 4) ? width : height;
nx = (orientation & 2) ? wd - nx - 1 : nx;
ny = (orientation & 1) ? ht - ny - 1 : ny;
write_imagef (out, (int2)(nx, ny), pixel);
}
/* we use this exp approximation to maintain full identity with cpu path */
float
fast_expf(const float x)
{
// meant for the range [-100.0f, 0.0f]. largest error ~ -0.06 at 0.0f.
// will get _a_lot_ worse for x > 0.0f (9000 at 10.0f)..
const int i1 = 0x3f800000u;
// e^x, the comment would be 2^x
const int i2 = 0x402DF854u;//0x40000000u;
// const int k = CLAMPS(i1 + x * (i2 - i1), 0x0u, 0x7fffffffu);
// without max clamping (doesn't work for large x, but is faster):
const int k0 = i1 + x * (i2 - i1);
const int k = k0 > 0 ? k0 : 0;
const float f = *(const float *)&k;
return f;
}
float
envelope(const float L)
{
const float x = clamp(L/100.0f, 0.0f, 1.0f);
// const float alpha = 2.0f;
const float beta = 0.6f;
if(x < beta)
{
// return 1.0f-fabsf(x/beta-1.0f)^2
const float tmp = fabs(x/beta-1.0f);
return 1.0f-tmp*tmp;
}
else
{
const float tmp1 = (1.0f-x)/(1.0f-beta);
const float tmp2 = tmp1*tmp1;
const float tmp3 = tmp2*tmp1;
return 3.0f*tmp2 - 2.0f*tmp3;
}
}
/* kernel for monochrome */
kernel void
monochrome_filter(
read_only image2d_t in,
write_only image2d_t out,
const int width,
const int height,
const float a,
const float b,
const float size)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel = read_imagef (in, sampleri, (int2)(x, y));
// TODO: this could be a native_expf, or exp2f, need to evaluate comparisons with cpu though:
pixel.x = 100.0f*fast_expf(-clamp(((pixel.y - a)*(pixel.y - a) + (pixel.z - b)*(pixel.z - b))/(2.0f * size), 0.0f, 1.0f));
write_imagef (out, (int2)(x, y), pixel);
}
kernel void
monochrome(
read_only image2d_t in,
read_only image2d_t base,
write_only image2d_t out,
const int width,
const int height,
const float a,
const float b,
const float size,
float highlights)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel = read_imagef (in, sampleri, (int2)(x, y));