forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageop_math.c
More file actions
966 lines (852 loc) · 34.6 KB
/
imageop_math.c
File metadata and controls
966 lines (852 loc) · 34.6 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
/*
This file is part of darktable,
Copyright (C) 2016-2025 darktable developers.
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/>.
*/
#include "common/darktable.h" // for darktable, darktable_t, dt_code...
#include "common/interpolation.h" // for dt_interpolation_new, dt_interp...
#include "develop/imageop.h" // for dt_iop_roi_t
#include "develop/imageop_math.h"
#include "imageio/imageio_common.h" // for FILTERS_ARE_4BAYER
#include <assert.h> // for assert
#include <glib.h> // for MIN, MAX, CLAMP, inline
#include <math.h> // for round, floorf, fmaxf
void dt_iop_flip_and_zoom_8(const uint8_t *in,
const int32_t iw,
const int32_t ih,
uint8_t *out,
const int32_t ow,
const int32_t oh,
const dt_image_orientation_t orientation,
uint32_t *width,
uint32_t *height)
{
// init strides:
const uint32_t iwd = (orientation & ORIENTATION_SWAP_XY) ? ih : iw;
const uint32_t iht = (orientation & ORIENTATION_SWAP_XY) ? iw : ih;
// DO NOT UPSCALE !!!
const float scale = fmaxf(1.0, fmaxf(iwd / (float)ow, iht / (float)oh));
const uint32_t wd = *width = MIN(ow, iwd / scale);
const uint32_t ht = *height = MIN(oh, iht / scale);
const int bpp = 4; // bytes per pixel
int32_t ii = 0, jj = 0;
int32_t si = 1, sj = iw;
if(orientation & ORIENTATION_FLIP_Y)
{
jj = ih - jj - 1;
sj = -sj;
}
if(orientation & ORIENTATION_FLIP_X)
{
ii = iw - ii - 1;
si = -si;
}
if(orientation & ORIENTATION_SWAP_XY)
{
int t = sj;
sj = si;
si = t;
}
const int32_t half_pixel = .5f * scale;
const int32_t offm = half_pixel * bpp * MIN(MIN(0, si), MIN(sj, si + sj));
const int32_t offM = half_pixel * bpp * MAX(MAX(0, si), MAX(sj, si + sj));
DT_OMP_FOR()
for(uint32_t j = 0; j < ht; j++)
{
uint8_t *out2 = out + bpp * wd * j;
const uint8_t *in2 = in + bpp * (iw * jj + ii + sj * (int32_t)(scale * j));
float stepi = 0.0f;
for(uint32_t i = 0; i < wd; i++)
{
const uint8_t *in3 = in2 + ((int32_t)stepi) * si * bpp;
// this should always be within the bounds of in[], due to the way
// wd/ht are constructed by always just rounding down. half_pixel should never
// add up to one pixel difference.
// we have this check with the hope the branch predictor will get rid of it:
if(in3 + offm >= in && in3 + offM < in + bpp * iw * ih)
{
for(int k = 0; k < 3; k++)
out2[k] = // in3[k];
CLAMP(((int32_t)in3[bpp * half_pixel * sj + k]
+ (int32_t)in3[bpp * half_pixel * (si + sj) + k]
+ (int32_t)in3[bpp * half_pixel * si + k] + (int32_t)in3[k])
/ 4,
0, 255);
}
out2 += bpp;
stepi += scale;
}
}
}
void dt_iop_clip_and_zoom_8(const uint8_t *i,
const int32_t ix,
const int32_t iy,
const int32_t iw,
const int32_t ih,
const int32_t ibw,
const int32_t ibh,
uint8_t *o,
const int32_t ox,
const int32_t oy,
const int32_t ow,
const int32_t oh,
const int32_t obw,
const int32_t obh)
{
const float scalex = iw / (float)ow;
const float scaley = ih / (float)oh;
const int32_t ix2 = MAX(ix, 0);
const int32_t iy2 = MAX(iy, 0);
const int32_t ox2 = MAX(ox, 0);
const int32_t oy2 = MAX(oy, 0);
const int32_t oh2 = MIN(MIN(oh, (ibh - iy2) / scaley), obh - oy2);
const int32_t ow2 = MIN(MIN(ow, (ibw - ix2) / scalex), obw - ox2);
assert((int)(ix2 + ow2 * scalex) <= ibw);
assert((int)(iy2 + oh2 * scaley) <= ibh);
assert(ox2 + ow2 <= obw);
assert(oy2 + oh2 <= obh);
assert(ix2 >= 0 && iy2 >= 0 && ox2 >= 0 && oy2 >= 0);
float x = ix2, y = iy2;
for(int s = 0; s < oh2; s++)
{
int idx = ox2 + obw * (oy2 + s);
for(int t = 0; t < ow2; t++)
{
for(int k = 0; k < 3; k++)
o[4 * idx + k] = // i[3*(ibw* (int)y + (int)x ) + k)];
CLAMP(((int32_t)i[(4 * (ibw * (int32_t)y + (int32_t)(x + .5f * scalex)) + k)]
+ (int32_t)i[(4 * (ibw * (int32_t)(y + .5f * scaley) + (int32_t)(x + .5f * scalex)) + k)]
+ (int32_t)i[(4 * (ibw * (int32_t)(y + .5f * scaley) + (int32_t)(x)) + k)]
+ (int32_t)i[(4 * (ibw * (int32_t)y + (int32_t)(x)) + k)])
/ 4,
0, 255);
x += scalex;
idx++;
}
y += scaley;
x = ix2;
}
}
/* apply clip and zoom on parts of a supplied full image, roi_in and roi_out define which part to work on.
gamma correction around scaling supported.
We don't do full RGB->linear and transformation but only use a gamma as that is fully sufficient
for scaling.
*/
void dt_iop_clip_and_zoom(float *out,
const float *const in,
const dt_iop_roi_t *const roi_out,
const dt_iop_roi_t *const roi_in,
const gboolean gamma)
{
const dt_interpolation_t *itor = dt_interpolation_new(DT_INTERPOLATION_USERPREF);
float *linear = gamma ? dt_alloc_align_float((size_t)roi_in->width * roi_in->height * 4) : NULL;
if(!linear)
return dt_interpolation_resample(itor, out, roi_out, in, roi_in);
static const dt_aligned_pixel_t two_point_four = { 2.4f, 2.4f, 2.4f, 2.4f };
static const dt_aligned_pixel_t rev_two_point_four = { 1.0f / 2.4f, 1.0f / 2.4f, 1.0f / 2.4f, 1.0f / 2.4f };
DT_OMP_SIMD(aligned(in, linear : 16))
for(size_t k = 0; k < (size_t)roi_in->width * roi_in->height*4; k += 4)
dt_vector_powf(&in[k], two_point_four, &linear[k]);
dt_interpolation_resample(itor, out, roi_out, linear, roi_in);
dt_free_align(linear);
DT_OMP_SIMD(aligned(out : 16))
for(size_t k = 0; k < (size_t)roi_out->width * roi_out->height * 4; k += 4)
dt_vector_powf(&out[k], rev_two_point_four, &out[k]);
}
// apply clip and zoom on the image region supplied in the input buffer.
// roi_in and roi_out describe which part of the full image this relates to but shifts are ignored.
void dt_iop_clip_and_zoom_roi(float *out,
const float *const in,
const dt_iop_roi_t *const roi_out,
const dt_iop_roi_t *const roi_in)
{
const dt_interpolation_t *itor = dt_interpolation_new(DT_INTERPOLATION_USERPREF);
dt_interpolation_resample_roi(itor, out, roi_out, in, roi_in);
}
#ifdef HAVE_OPENCL
// apply clip and zoom on parts of a supplied full image.
// roi_in and roi_out define which part to work on.
int dt_iop_clip_and_zoom_cl(int devid,
cl_mem dev_out,
cl_mem dev_in,
const dt_iop_roi_t *const roi_out,
const dt_iop_roi_t *const roi_in)
{
const dt_interpolation_t *itor = dt_interpolation_new(DT_INTERPOLATION_USERPREF);
return dt_interpolation_resample_cl(itor, devid, dev_out, roi_out, dev_in, roi_in);
}
// apply clip and zoom on the image region supplied in the input buffer.
// roi_in and roi_out describe which part of the full image this relates to but shifts are ignored.
int dt_iop_clip_and_zoom_roi_cl(int devid,
cl_mem dev_out,
cl_mem dev_in,
const dt_iop_roi_t *const roi_out,
const dt_iop_roi_t *const roi_in)
{
const dt_interpolation_t *itor = dt_interpolation_new(DT_INTERPOLATION_USERPREF);
cl_int err = dt_interpolation_resample_roi_cl(itor, devid, dev_out,
roi_out, dev_in, roi_in);
if(err == CL_INVALID_WORK_GROUP_SIZE)
{
// We ran into a "vertical number of taps exceeds the vertical workgroupsize" problem
// Instead of redoing the whole thing later we do an internal fallback to cpu here
float *in = dt_alloc_align_float((size_t)roi_in->width * roi_in->height * 4);
float *out = dt_alloc_align_float((size_t)roi_out->width * roi_out->height * 4);
if(out && in)
{
err = dt_opencl_copy_device_to_host(devid, in, dev_in, roi_in->width, roi_in->height, 4 * sizeof(float));
if(err == CL_SUCCESS)
{
dt_iop_clip_and_zoom_roi(out, in, roi_out, roi_in);
err = dt_opencl_write_host_to_device
(devid, out, dev_out, roi_out->width, roi_out->height, 4 * sizeof(float));
}
}
if(err == CL_SUCCESS)
dt_print_pipe(DT_DEBUG_OPENCL, "clip and zoom roi", NULL, NULL, devid, roi_in, roi_out,
"did fast cpu fallback");
else
dt_print_pipe(DT_DEBUG_OPENCL, "clip and zoom roi", NULL, NULL, devid, roi_in, roi_out,
"fast cpu fallback failing: %s", cl_errstr(err));
dt_free_align(in);
dt_free_align(out);
}
return err;
}
#endif
void dt_iop_clip_and_zoom_mosaic_half_size(uint16_t *const out,
const uint16_t *const in,
const dt_iop_roi_t *const roi_out,
const dt_iop_roi_t *const roi_in,
const int32_t out_stride,
const int32_t in_stride,
const uint32_t filters)
{
// adjust to pixel region and don't sample more than scale/2 nbs!
// pixel footprint on input buffer, radius:
const float px_footprint = 1.f / roi_out->scale;
// move to origin point 01 of a 2x2 CFA block
// (RGGB=0112 or CYGM=0132)
int trggbx = 0, trggby = 0;
if(FC(trggby, trggbx + 1, filters) != 1) trggbx++;
if(FC(trggby, trggbx, filters) != 0)
{
trggbx = (trggbx + 1) & 1;
trggby++;
}
const int rggbx = trggbx, rggby = trggby;
// Create a reverse lookup of FC(): for each CFA color, a list of
// offsets from start of a 2x2 block at which to find that
// color. First index is color, second is to the list of offsets,
// preceded by the number of offsets.
int clut[4][3] = {{0}};
for(int y = 0; y < 2; ++y)
for(int x = 0; x < 2; ++x)
{
const int c = FC(y + rggby, x + rggbx, filters);
assert(clut[c][0] < 2);
clut[c][++clut[c][0]] = x + y * in_stride;
}
DT_OMP_FOR()
for(int y = 0; y < roi_out->height; y++)
{
uint16_t *outc = out + out_stride * y;
const float fy = y * px_footprint;
const int miny = (CLAMPS((int)floorf(fy - px_footprint),
0, roi_in->height-3) & ~1u) + rggby;
const int maxy = MIN(roi_in->height-1, (int)ceilf(fy + px_footprint));
float fx = 0.0f;
for(int x = 0; x < roi_out->width; x++, fx += px_footprint, outc++)
{
const int minx = (CLAMPS((int)floorf(fx - px_footprint),
0, roi_in->width-3) & ~1u) + rggbx;
const int maxx = MIN(roi_in->width-1, (int)ceilf(fx + px_footprint));
const int c = FC(y, x, filters);
int num = 0;
uint32_t col = 0;
for(int yy = miny; yy < maxy; yy += 2)
for(int xx = minx; xx < maxx; xx += 2)
{
col += in[clut[c][1] + xx + in_stride * yy];
num++;
if(clut[c][0] == 2)
{ // G in RGGB CFA
col += in[clut[c][2] + xx + in_stride * yy];
num++;
}
}
if(num) *outc = col / num;
}
}
}
void dt_iop_clip_and_zoom_mosaic_half_size_f(float *const out,
const float *const in,
const dt_iop_roi_t *const roi_out,
const dt_iop_roi_t *const roi_in,
const int32_t out_stride,
const int32_t in_stride,
const uint32_t filters)
{
// adjust to pixel region and don't sample more than scale/2 nbs!
// pixel footprint on input buffer, radius:
const float px_footprint = 1.f / roi_out->scale;
// how many 2x2 blocks can be sampled inside that area
const int samples = round(px_footprint / 2);
// move p to point to an rggb block:
int trggbx = 0, trggby = 0;
if(FC(trggby, trggbx + 1, filters) != 1) trggbx++;
if(FC(trggby, trggbx, filters) != 0)
{
trggbx = (trggbx + 1) & 1;
trggby++;
}
const int rggbx = trggbx, rggby = trggby;
DT_OMP_FOR()
for(int y = 0; y < roi_out->height; y++)
{
float *outc = out + out_stride * y;
const float fy = y * px_footprint;
int py = (int)fy & ~1;
const float dy = (fy - py) / 2;
py = MIN(((roi_in->height - 6) & ~1u), py) + rggby;
int maxj = MIN(((roi_in->height - 5) & ~1u) + rggby, py + 2 * samples);
for(int x = 0; x < roi_out->width; x++)
{
dt_aligned_pixel_t col = { 0, 0, 0, 0 };
const float fx = x * px_footprint;
int px = (int)fx & ~1;
const float dx = (fx - px) / 2;
px = MIN(((roi_in->width - 6) & ~1u), px) + rggbx;
const int maxi = MIN(((roi_in->width - 5) & ~1u) + rggbx, px + 2 * samples);
dt_aligned_pixel_t p;
float num = 0;
// upper left 2x2 block of sampling region
p[0] = in[px + in_stride * py];
p[1] = in[px + 1 + in_stride * py];
p[2] = in[px + in_stride * (py + 1)];
p[3] = in[px + 1 + in_stride * (py + 1)];
for(int c = 0; c < 4; c++) col[c] += ((1 - dx) * (1 - dy)) * p[c];
// left 2x2 block border of sampling region
for(int j = py + 2; j <= maxj; j += 2)
{
p[0] = in[px + in_stride * j];
p[1] = in[px + 1 + in_stride * j];
p[2] = in[px + in_stride * (j + 1)];
p[3] = in[px + 1 + in_stride * (j + 1)];
for(int c = 0; c < 4; c++) col[c] += (1 - dx) * p[c];
}
// upper 2x2 block border of sampling region
for(int i = px + 2; i <= maxi; i += 2)
{
p[0] = in[i + in_stride * py];
p[1] = in[i + 1 + in_stride * py];
p[2] = in[i + in_stride * (py + 1)];
p[3] = in[i + 1 + in_stride * (py + 1)];
for(int c = 0; c < 4; c++) col[c] += (1 - dy) * p[c];
}
// 2x2 blocks in the middle of sampling region
for(int j = py + 2; j <= maxj; j += 2)
for(int i = px + 2; i <= maxi; i += 2)
{
p[0] = in[i + in_stride * j];
p[1] = in[i + 1 + in_stride * j];
p[2] = in[i + in_stride * (j + 1)];
p[3] = in[i + 1 + in_stride * (j + 1)];
for(int c = 0; c < 4; c++) col[c] += p[c];
}
if(maxi == px + 2 * samples && maxj == py + 2 * samples)
{
// right border
for(int j = py + 2; j <= maxj; j += 2)
{
p[0] = in[maxi + 2 + in_stride * j];
p[1] = in[maxi + 3 + in_stride * j];
p[2] = in[maxi + 2 + in_stride * (j + 1)];
p[3] = in[maxi + 3 + in_stride * (j + 1)];
for(int c = 0; c < 4; c++) col[c] += dx * p[c];
}
// upper right
p[0] = in[maxi + 2 + in_stride * py];
p[1] = in[maxi + 3 + in_stride * py];
p[2] = in[maxi + 2 + in_stride * (py + 1)];
p[3] = in[maxi + 3 + in_stride * (py + 1)];
for(int c = 0; c < 4; c++) col[c] += (dx * (1 - dy)) * p[c];
// lower border
for(int i = px + 2; i <= maxi; i += 2)
{
p[0] = in[i + in_stride * (maxj + 2)];
p[1] = in[i + 1 + in_stride * (maxj + 2)];
p[2] = in[i + in_stride * (maxj + 3)];
p[3] = in[i + 1 + in_stride * (maxj + 3)];
for(int c = 0; c < 4; c++) col[c] += dy * p[c];
}
// lower left 2x2 block
p[0] = in[px + in_stride * (maxj + 2)];
p[1] = in[px + 1 + in_stride * (maxj + 2)];
p[2] = in[px + in_stride * (maxj + 3)];
p[3] = in[px + 1 + in_stride * (maxj + 3)];
for(int c = 0; c < 4; c++) col[c] += ((1 - dx) * dy) * p[c];
// lower right 2x2 block
p[0] = in[maxi + 2 + in_stride * (maxj + 2)];
p[1] = in[maxi + 3 + in_stride * (maxj + 2)];
p[2] = in[maxi + 2 + in_stride * (maxj + 3)];
p[3] = in[maxi + 3 + in_stride * (maxj + 3)];
for(int c = 0; c < 4; c++) col[c] += (dx * dy) * p[c];
num = (samples + 1) * (samples + 1);
}
else if(maxi == px + 2 * samples)
{
// right border
for(int j = py + 2; j <= maxj; j += 2)
{
p[0] = in[maxi + 2 + in_stride * j];
p[1] = in[maxi + 3 + in_stride * j];
p[2] = in[maxi + 2 + in_stride * (j + 1)];
p[3] = in[maxi + 3 + in_stride * (j + 1)];
for(int c = 0; c < 4; c++) col[c] += dx * p[c];
}
// upper right
p[0] = in[maxi + 2 + in_stride * py];
p[1] = in[maxi + 3 + in_stride * py];
p[2] = in[maxi + 2 + in_stride * (py + 1)];
p[3] = in[maxi + 3 + in_stride * (py + 1)];
for(int c = 0; c < 4; c++) col[c] += (dx * (1 - dy)) * p[c];
num = ((maxj - py) / 2 + 1 - dy) * (samples + 1);
}
else if(maxj == py + 2 * samples)
{
// lower border
for(int i = px + 2; i <= maxi; i += 2)
{
p[0] = in[i + in_stride * (maxj + 2)];
p[1] = in[i + 1 + in_stride * (maxj + 2)];
p[2] = in[i + in_stride * (maxj + 3)];
p[3] = in[i + 1 + in_stride * (maxj + 3)];
for(int c = 0; c < 4; c++) col[c] += dy * p[c];
}
// lower left 2x2 block
p[0] = in[px + in_stride * (maxj + 2)];
p[1] = in[px + 1 + in_stride * (maxj + 2)];
p[2] = in[px + in_stride * (maxj + 3)];
p[3] = in[px + 1 + in_stride * (maxj + 3)];
for(int c = 0; c < 4; c++) col[c] += ((1 - dx) * dy) * p[c];
num = ((maxi - px) / 2 + 1 - dx) * (samples + 1);
}
else
{
num = ((maxi - px) / 2 + 1 - dx) * ((maxj - py) / 2 + 1 - dy);
}
const int c = (2 * ((y + rggby) % 2) + ((x + rggbx) % 2));
if(num) *outc = col[c] / num;
outc++;
}
}
}
/**
* downscales and clips a Fujifilm X-Trans mosaiced buffer (in) to the given region of interest (r_*)
* and writes it to out.
*/
void dt_iop_clip_and_zoom_mosaic_third_size_xtrans(uint16_t *const out,
const uint16_t *const in,
const dt_iop_roi_t *const roi_out,
const dt_iop_roi_t *const roi_in,
const int32_t out_stride,
const int32_t in_stride,
const uint8_t (*const xtrans)[6])
{
const float px_footprint = 1.f / roi_out->scale;
// Use box filter of width px_footprint*2+1 centered on the current
// sample (rounded to nearest input pixel) to anti-alias. Higher MP
// images need larger filters to avoid artifacts.
DT_OMP_FOR()
for(int y = 0; y < roi_out->height; y++)
{
uint16_t *outc = out + out_stride * y;
const float fy = y * px_footprint;
const int miny = MAX(0, (int)roundf(fy - px_footprint));
const int maxy = MIN(roi_in->height-1, (int)roundf(fy + px_footprint));
float fx = 0.0f;
for(int x = 0; x < roi_out->width; x++, fx += px_footprint, outc++)
{
const int minx = MAX(0, (int)roundf(fx - px_footprint));
const int maxx = MIN(roi_in->width-1, (int)roundf(fx + px_footprint));
const int c = FCxtrans(y, x, roi_out, xtrans);
int num = 0;
uint32_t col = 0;
for(int yy = miny; yy <= maxy; ++yy)
for(int xx = minx; xx <= maxx; ++xx)
if(FCxtrans(yy, xx, roi_in, xtrans) == c)
{
col += in[xx + in_stride * yy];
num++;
}
*outc = col / num;
}
}
}
void dt_iop_clip_and_zoom_mosaic_third_size_xtrans_f(float *const out,
const float *const in,
const dt_iop_roi_t *const roi_out,
const dt_iop_roi_t *const roi_in,
const int32_t out_stride,
const int32_t in_stride,
const uint8_t (*const xtrans)[6])
{
const float px_footprint = 1.f / roi_out->scale;
DT_OMP_FOR()
for(int y = 0; y < roi_out->height; y++)
{
float *outc = out + out_stride * y;
const float fy = y * px_footprint;
const int miny = MAX(0, (int)roundf(fy - px_footprint));
const int maxy = MIN(roi_in->height-1, (int)roundf(fy + px_footprint));
float fx = 0.0f;
for(int x = 0; x < roi_out->width; x++, fx += px_footprint, outc++)
{
const int minx = MAX(0, (int)roundf(fx - px_footprint));
const int maxx = MIN(roi_in->width-1, (int)roundf(fx + px_footprint));
const int c = FCxtrans(y, x, roi_out, xtrans);
int num = 0;
float col = 0.f;
for(int yy = miny; yy <= maxy; ++yy)
for(int xx = minx; xx <= maxx; ++xx)
if(FCxtrans(yy, xx, roi_in, xtrans) == c)
{
col += in[xx + in_stride * yy];
num++;
}
*outc = col / (float)num;
}
}
}
void dt_iop_clip_and_zoom_demosaic_passthrough_monochrome_f(float *out,
const float *const in,
const dt_iop_roi_t *const roi_out,
const dt_iop_roi_t *const roi_in,
const int32_t out_stride,
const int32_t in_stride)
{
// adjust to pixel region and don't sample more than scale/2 nbs!
// pixel footprint on input buffer, radius:
const float px_footprint = 1.f / roi_out->scale;
// how many pixels can be sampled inside that area
const int samples = round(px_footprint);
DT_OMP_FOR()
for(int y = 0; y < roi_out->height; y++)
{
float *outc = out + 4 * (out_stride * y);
const float fy = y * px_footprint;
int py = (int)fy;
const float dy = fy - py;
py = MIN(((roi_in->height - 3)), py);
const int maxj = MIN(((roi_in->height - 2)), py + samples);
for(int x = 0; x < roi_out->width; x++)
{
float col = 0.0f;
const float fx = x * px_footprint;
int px = (int)fx;
const float dx = fx - px;
px = MIN(((roi_in->width - 3)), px);
const int maxi = MIN(((roi_in->width - 2)), px + samples);
float p;
float num = 0;
// upper left pixel of sampling region
p = in[px + in_stride * py];
col += ((1 - dx) * (1 - dy)) * p;
// left pixel border of sampling region
for(int j = py + 1; j <= maxj; j++)
{
p = in[px + in_stride * j];
col += (1 - dx) * p;
}
// upper pixel border of sampling region
for(int i = px + 1; i <= maxi; i++)
{
p = in[i + in_stride * py];
col += (1 - dy) * p;
}
// pixels in the middle of sampling region
for(int j = py + 1; j <= maxj; j++)
for(int i = px + 1; i <= maxi; i++)
{
p = in[i + in_stride * j];
col += p;
}
if(maxi == px + samples && maxj == py + samples)
{
// right border
for(int j = py + 1; j <= maxj; j++)
{
p = in[maxi + 1 + in_stride * j];
col += dx * p;
}
// upper right
p = in[maxi + 1 + in_stride * py];
col += (dx * (1 - dy)) * p;
// lower border
for(int i = px + 1; i <= maxi; i++)
{
p = in[i + in_stride * (maxj + 1)];
col += dy * p;
}
// lower left pixel
p = in[px + in_stride * (maxj + 1)];
col += ((1 - dx) * dy) * p;
// lower right pixel
p = in[maxi + 1 + in_stride * (maxj + 1)];
col += (dx * dy) * p;
num = (samples + 1) * (samples + 1);
}
else if(maxi == px + samples)
{
// right border
for(int j = py + 1; j <= maxj; j++)
{
p = in[maxi + 1 + in_stride * j];
col += dx * p;
}
// upper right
p = in[maxi + 1 + in_stride * py];
col += (dx * (1 - dy)) * p;
num = ((maxj - py) / 2 + 1 - dy) * (samples + 1);
}
else if(maxj == py + samples)
{
// lower border
for(int i = px + 1; i <= maxi; i++)
{
p = in[i + in_stride * (maxj + 1)];
col += dy * p;
}
// lower left pixel
p = in[px + in_stride * (maxj + 1)];
col += ((1 - dx) * dy) * p;
num = ((maxi - px) / 2 + 1 - dx) * (samples + 1);
}
else
{
num = ((maxi - px) / 2 + 1 - dx) * ((maxj - py) / 2 + 1 - dy);
}
const float pix = (num) ? fmaxf(0.0f, col) / num : 0.0f;
outc[0] = pix;
outc[1] = pix;
outc[2] = pix;
outc[3] = 0.0f;
outc += 4;
}
}
}
void dt_iop_clip_and_zoom_demosaic_half_size_f(float *out,
const float *const in,
const dt_iop_roi_t *const roi_out,
const dt_iop_roi_t *const roi_in,
const int32_t out_stride,
const int32_t in_stride,
const uint32_t filters)
{
// adjust to pixel region and don't sample more than scale/2 nbs!
// pixel footprint on input buffer, radius:
const float px_footprint = 1.f / roi_out->scale;
// how many 2x2 blocks can be sampled inside that area
const int samples = round(px_footprint / 2);
// move p to point to an rggb block:
int trggbx = 0, trggby = 0;
if(FC(trggby, trggbx + 1, filters) != 1) trggbx++;
if(FC(trggby, trggbx, filters) != 0)
{
trggbx = (trggbx + 1) & 1;
trggby++;
}
const int rggbx = trggbx, rggby = trggby;
DT_OMP_FOR()
for(int y = 0; y < roi_out->height; y++)
{
float *outc = out + 4 * (out_stride * y);
const float fy = y * px_footprint;
int py = (int)fy & ~1;
const float dy = (fy - py) / 2;
py = MIN(((roi_in->height - 6) & ~1u), py) + rggby;
const int maxj = MIN(((roi_in->height - 5) & ~1u) + rggby, py + 2 * samples);
for(int x = 0; x < roi_out->width; x++)
{
dt_aligned_pixel_t col = { 0, 0, 0, 0 };
const float fx = x * px_footprint;
int px = (int)fx & ~1;
const float dx = (fx - px) / 2;
px = MIN(((roi_in->width - 6) & ~1u), px) + rggbx;
const int maxi = MIN(((roi_in->width - 5) & ~1u) + rggbx, px + 2 * samples);
dt_aligned_pixel_t p;
float num = 0;
// upper left 2x2 block of sampling region
p[0] = in[px + in_stride * py];
p[1] = in[px + 1 + in_stride * py] + in[px + in_stride * (py + 1)];
p[2] = in[px + 1 + in_stride * (py + 1)];
for(int c = 0; c < 3; c++) col[c] += ((1 - dx) * (1 - dy)) * p[c];
// left 2x2 block border of sampling region
for(int j = py + 2; j <= maxj; j += 2)
{
p[0] = in[px + in_stride * j];
p[1] = in[px + 1 + in_stride * j] + in[px + in_stride * (j + 1)];
p[2] = in[px + 1 + in_stride * (j + 1)];
for(int c = 0; c < 3; c++) col[c] += (1 - dx) * p[c];
}
// upper 2x2 block border of sampling region
for(int i = px + 2; i <= maxi; i += 2)
{
p[0] = in[i + in_stride * py];
p[1] = in[i + 1 + in_stride * py] + in[i + in_stride * (py + 1)];
p[2] = in[i + 1 + in_stride * (py + 1)];
for(int c = 0; c < 3; c++) col[c] += (1 - dy) * p[c];
}
// 2x2 blocks in the middle of sampling region
for(int j = py + 2; j <= maxj; j += 2)
for(int i = px + 2; i <= maxi; i += 2)
{
p[0] = in[i + in_stride * j];
p[1] = in[i + 1 + in_stride * j] + in[i + in_stride * (j + 1)];
p[2] = in[i + 1 + in_stride * (j + 1)];
for(int c = 0; c < 3; c++) col[c] += p[c];
}
if(maxi == px + 2 * samples && maxj == py + 2 * samples)
{
// right border
for(int j = py + 2; j <= maxj; j += 2)
{
p[0] = in[maxi + 2 + in_stride * j];
p[1] = in[maxi + 3 + in_stride * j] + in[maxi + 2 + in_stride * (j + 1)];
p[2] = in[maxi + 3 + in_stride * (j + 1)];
for(int c = 0; c < 3; c++) col[c] += dx * p[c];
}
// upper right
p[0] = in[maxi + 2 + in_stride * py];
p[1] = in[maxi + 3 + in_stride * py] + in[maxi + 2 + in_stride * (py + 1)];
p[2] = in[maxi + 3 + in_stride * (py + 1)];
for(int c = 0; c < 3; c++) col[c] += (dx * (1 - dy)) * p[c];
// lower border
for(int i = px + 2; i <= maxi; i += 2)
{
p[0] = in[i + in_stride * (maxj + 2)];
p[1] = in[i + 1 + in_stride * (maxj + 2)] + in[i + in_stride * (maxj + 3)];
p[2] = in[i + 1 + in_stride * (maxj + 3)];
for(int c = 0; c < 3; c++) col[c] += dy * p[c];
}
// lower left 2x2 block
p[0] = in[px + in_stride * (maxj + 2)];
p[1] = in[px + 1 + in_stride * (maxj + 2)] + in[px + in_stride * (maxj + 3)];
p[2] = in[px + 1 + in_stride * (maxj + 3)];
for(int c = 0; c < 3; c++) col[c] += ((1 - dx) * dy) * p[c];
// lower right 2x2 block
p[0] = in[maxi + 2 + in_stride * (maxj + 2)];
p[1] = in[maxi + 3 + in_stride * (maxj + 2)]
+ in[maxi + 2 + in_stride * (maxj + 3)];
p[2] = in[maxi + 3 + in_stride * (maxj + 3)];
for(int c = 0; c < 3; c++) col[c] += (dx * dy) * p[c];
num = (samples + 1) * (samples + 1);
}
else if(maxi == px + 2 * samples)
{
// right border
for(int j = py + 2; j <= maxj; j += 2)
{
p[0] = in[maxi + 2 + in_stride * j];
p[1] = in[maxi + 3 + in_stride * j] + in[maxi + 2 + in_stride * (j + 1)];
p[2] = in[maxi + 3 + in_stride * (j + 1)];
for(int c = 0; c < 3; c++) col[c] += dx * p[c];
}
// upper right
p[0] = in[maxi + 2 + in_stride * py];
p[1] = in[maxi + 3 + in_stride * py] + in[maxi + 2 + in_stride * (py + 1)];
p[2] = in[maxi + 3 + in_stride * (py + 1)];
for(int c = 0; c < 3; c++) col[c] += (dx * (1 - dy)) * p[c];
num = ((maxj - py) / 2 + 1 - dy) * (samples + 1);
}
else if(maxj == py + 2 * samples)
{
// lower border
for(int i = px + 2; i <= maxi; i += 2)
{
p[0] = in[i + in_stride * (maxj + 2)];
p[1] = in[i + 1 + in_stride * (maxj + 2)] + in[i + in_stride * (maxj + 3)];
p[2] = in[i + 1 + in_stride * (maxj + 3)];
for(int c = 0; c < 3; c++) col[c] += dy * p[c];
}
// lower left 2x2 block
p[0] = in[px + in_stride * (maxj + 2)];
p[1] = in[px + 1 + in_stride * (maxj + 2)] + in[px + in_stride * (maxj + 3)];
p[2] = in[px + 1 + in_stride * (maxj + 3)];
for(int c = 0; c < 3; c++) col[c] += ((1 - dx) * dy) * p[c];
num = ((maxi - px) / 2 + 1 - dx) * (samples + 1);
}
else
{
num = ((maxi - px) / 2 + 1 - dx) * ((maxj - py) / 2 + 1 - dy);
}
outc[0] = fmaxf(0.0f, col[0]) / num;
outc[1] = fmaxf(0.0f, col[1]) / num / 2.0f;
outc[2] = fmaxf(0.0f, col[2]) / num;
outc[3] = 0.0f;
outc += 4;
}
}
}
void dt_iop_clip_and_zoom_demosaic_third_size_xtrans_f(float *out,
const float *const in,
const dt_iop_roi_t *const roi_out,
const dt_iop_roi_t *const roi_in,
const int32_t out_stride,
const int32_t in_stride,
const uint8_t (*const xtrans)[6])
{
const float px_footprint = 1.f / roi_out->scale;
const int samples = MAX(1, (int)floorf(px_footprint / 3));
// A slightly different algorithm than
// dt_iop_clip_and_zoom_demosaic_half_size_f() which aligns to 2x2
// Bayer grid and hence most pull additional data from all edges
// which don't align with CFA. Instead align to a 3x3 pattern (which
// is semi-regular in X-Trans CFA). This code doesn't worry about
// fractional pixel offset of top/left of pattern nor oversampling
// by non-integer number of samples.
DT_OMP_FOR()
for(int y = 0; y < roi_out->height; y++)
{
float *outc = out + 4 * (out_stride * y);
const int py = CLAMPS((int)round((y - 0.5f) * px_footprint),
0, roi_in->height - 3);
const int ymax = MIN(roi_in->height - 3, py + 3 * samples);
for(int x = 0; x < roi_out->width; x++, outc += 4)
{
dt_aligned_pixel_t col = { 0.0f };
int num = 0;
const int px = CLAMPS((int)round((x - 0.5f) * px_footprint),
0, roi_in->width - 3);
const int xmax = MIN(roi_in->width - 3, px + 3 * samples);
for(int yy = py; yy <= ymax; yy += 3)
for(int xx = px; xx <= xmax; xx += 3)
{
for(int j = 0; j < 3; ++j)
for(int i = 0; i < 3; ++i)
col[FCxtrans(yy + j, xx + i, NULL, xtrans)]
+= in[xx + i + in_stride * (yy + j)];
num++;
}
// X-Trans RGB weighting averages to 2:5:2 for each 3x3 cell
outc[0] = fmaxf(0.0f, col[0]) / (num * 2);
outc[1] = fmaxf(0.0f, col[1]) / (num * 5);
outc[2] = fmaxf(0.0f, col[2]) / (num * 2);
}
}
}
// clang-format off
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
// clang-format on