Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
basic.c
Go to the documentation of this file.
1/*
2 This file is part of the Ansel project.
3 Copyright (C) 2023, 2026 Aurélien PIERRE.
4
5 Ansel is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 Ansel is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Ansel. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/* taken from dcraw and demosaic_ppg below */
20
21static void lin_interpolate(float *out, const float *const in, const dt_iop_roi_t *const roi_out,
22 const dt_iop_roi_t *const roi_in, const uint32_t filters,
23 const uint8_t (*const xtrans)[6])
24{
25 const int colors = (filters == 9) ? 3 : 4;
26
27// border interpolate
28#ifdef _OPENMP
29#pragma omp parallel for default(none) \
30 dt_omp_firstprivate(colors, filters, in, roi_in, roi_out, xtrans) \
31 shared(out) \
32 schedule(static)
33#endif
34 for(int row = 0; row < roi_out->height; row++)
35 for(int col = 0; col < roi_out->width; col++)
36 {
37 dt_aligned_pixel_t sum = { 0.0f };
38 uint8_t count[4] = { 0 };
39 if(col == 1 && row >= 1 && row < roi_out->height - 1) col = roi_out->width - 1;
40 // average all the adjoining pixels inside image by color
41 for(int y = row - 1; y != row + 2; y++)
42 for(int x = col - 1; x != col + 2; x++)
43 if(y >= 0 && x >= 0 && y < roi_in->height && x < roi_in->width)
44 {
45 const int f = fcol(y + roi_in->y, x + roi_in->x, filters, xtrans);
46 sum[f] += in[y * roi_in->width + x];
47 count[f]++;
48 }
49 const int f = fcol(row + roi_in->y, col + roi_in->x, filters, xtrans);
50 // for current cell, copy the current sensor's color data,
51 // interpolate the other two colors from surrounding pixels of
52 // their color
53 for(int c = 0; c < colors; c++)
54 {
55 if(c != f && count[c] != 0)
56 out[4 * (row * roi_out->width + col) + c] = sum[c] / count[c];
57 else
58 out[4 * (row * roi_out->width + col) + c] = in[row * roi_in->width + col];
59 }
60 }
61
62 // build interpolation lookup table which for a given offset in the sensor
63 // lists neighboring pixels from which to interpolate:
64 // NUM_PIXELS # of neighboring pixels to read
65 // for (1..NUM_PIXELS):
66 // OFFSET # in bytes from current pixel
67 // WEIGHT # how much weight to give this neighbor
68 // COLOR # sensor color
69 // # weights of adjoining pixels not of this pixel's color
70 // COLORA TOT_WEIGHT
71 // COLORB TOT_WEIGHT
72 // COLORPIX # color of center pixel
73
74 int(*const lookup)[16][32] = malloc(sizeof(int) * 16 * 16 * 32);
75
76 const int size = (filters == 9) ? 6 : 16;
77 for(int row = 0; row < size; row++)
78 for(int col = 0; col < size; col++)
79 {
80 int *ip = &(lookup[row][col][1]);
81 int sum[4] = { 0 };
82 const int f = fcol(row + roi_in->y, col + roi_in->x, filters, xtrans);
83 // make list of adjoining pixel offsets by weight & color
84 for(int y = -1; y <= 1; y++)
85 for(int x = -1; x <= 1; x++)
86 {
87 const int weight = 1 << ((y == 0) + (x == 0));
88 const int color = fcol(row + y + roi_in->y, col + x + roi_in->x, filters, xtrans);
89 if(color == f) continue;
90 *ip++ = (roi_in->width * y + x);
91 *ip++ = weight;
92 *ip++ = color;
93 sum[color] += weight;
94 }
95 lookup[row][col][0] = (ip - &(lookup[row][col][0])) / 3; /* # of neighboring pixels found */
96 for(int c = 0; c < colors; c++)
97 if(c != f)
98 {
99 *ip++ = c;
100 *ip++ = sum[c];
101 }
102 *ip = f;
103 }
104
105#ifdef _OPENMP
106#pragma omp parallel for default(none) \
107 dt_omp_firstprivate(colors, in, lookup, roi_in, roi_out, size) \
108 shared(out) \
109 schedule(static)
110#endif
111 for(int row = 1; row < roi_out->height - 1; row++)
112 {
113 float *buf = out + 4 * roi_out->width * row + 4;
114 const float *buf_in = in + roi_in->width * row + 1;
115 for(int col = 1; col < roi_out->width - 1; col++)
116 {
117 dt_aligned_pixel_t sum = { 0.0f };
118 int *ip = &(lookup[row % size][col % size][0]);
119 // for each adjoining pixel not of this pixel's color, sum up its weighted values
120 for(int i = *ip++; i--; ip += 3) sum[ip[2]] += buf_in[ip[0]] * ip[1];
121 // for each interpolated color, load it into the pixel
122 for(int i = colors; --i; ip += 2) buf[*ip] = sum[ip[0]] / ip[1];
123 buf[*ip] = *buf_in;
124 buf += 4;
125 buf_in++;
126 }
127 }
128
130}
131
132
133
134#define SWAP(a, b) \
135 { \
136 const float tmp = (b); \
137 (b) = (a); \
138 (a) = tmp; \
139 }
140
141#ifdef _OPENMP
142 #pragma omp declare simd aligned(in, out)
143#endif
144static void pre_median_b(float *out, const float *const in, const dt_iop_roi_t *const roi, const uint32_t filters,
145 const int num_passes, const float threshold)
146{
147 dt_iop_image_copy_by_size(out, in, roi->width, roi->height, 1);
148
149 // now green:
150 const int lim[5] = { 0, 1, 2, 1, 0 };
151 for(int pass = 0; pass < num_passes; pass++)
152 {
153#ifdef _OPENMP
154#pragma omp parallel for default(none) \
155 dt_omp_firstprivate(filters, in, lim, roi, threshold) \
156 shared(out) \
157 schedule(static)
158#endif
159 for(int row = 3; row < roi->height - 3; row++)
160 {
161 float med[9];
162 int col = 3;
163 if(FC(row, col, filters) != 1 && FC(row, col, filters) != 3) col++;
164 float *pixo = out + (size_t)roi->width * row + col;
165 const float *pixi = in + (size_t)roi->width * row + col;
166 for(; col < roi->width - 3; col += 2)
167 {
168 int cnt = 0;
169 for(int k = 0, i = 0; i < 5; i++)
170 {
171 for(int j = -lim[i]; j <= lim[i]; j += 2)
172 {
173 if(fabsf(pixi[roi->width * (i - 2) + j] - pixi[0]) < threshold)
174 {
175 med[k++] = pixi[roi->width * (i - 2) + j];
176 cnt++;
177 }
178 else
179 med[k++] = 64.0f + pixi[roi->width * (i - 2) + j];
180 }
181 }
182 for(int i = 0; i < 8; i++)
183 for(int ii = i + 1; ii < 9; ii++)
184 if(med[i] > med[ii]) SWAP(med[i], med[ii]);
185 pixo[0] = (cnt == 1 ? med[4] - 64.0f : med[(cnt - 1) / 2]);
186 // pixo[0] = med[(cnt-1)/2];
187 pixo += 2;
188 pixi += 2;
189 }
190 }
191 }
192}
193
194static void pre_median(float *out, const float *const in, const dt_iop_roi_t *const roi, const uint32_t filters,
195 const int num_passes, const float threshold)
196{
197 pre_median_b(out, in, roi, filters, num_passes, threshold);
198}
199
200#define SWAPmed(I, J) \
201 if(med[I] > med[J]) SWAP(med[I], med[J])
202
203static void color_smoothing(float *out, const dt_iop_roi_t *const roi_out, const int num_passes)
204{
205 const int width4 = 4 * roi_out->width;
206
207 for(int pass = 0; pass < num_passes; pass++)
208 {
209 for(int c = 0; c < 3; c += 2)
210 {
211 {
212 float *outp = out;
213 for(int j = 0; j < roi_out->height; j++)
214 for(int i = 0; i < roi_out->width; i++, outp += 4) outp[3] = outp[c];
215 }
216#ifdef _OPENMP
217#pragma omp parallel for default(none) \
218 dt_omp_firstprivate(roi_out, width4) \
219 shared(out, c) \
220 schedule(static)
221#endif
222 for(int j = 1; j < roi_out->height - 1; j++)
223 {
224 float *outp = out + (size_t)4 * j * roi_out->width + 4;
225 for(int i = 1; i < roi_out->width - 1; i++, outp += 4)
226 {
227 float med[9] = {
228 outp[-width4 - 4 + 3] - outp[-width4 - 4 + 1], outp[-width4 + 0 + 3] - outp[-width4 + 0 + 1],
229 outp[-width4 + 4 + 3] - outp[-width4 + 4 + 1], outp[-4 + 3] - outp[-4 + 1],
230 outp[+0 + 3] - outp[+0 + 1], outp[+4 + 3] - outp[+4 + 1],
231 outp[+width4 - 4 + 3] - outp[+width4 - 4 + 1], outp[+width4 + 0 + 3] - outp[+width4 + 0 + 1],
232 outp[+width4 + 4 + 3] - outp[+width4 + 4 + 1],
233 };
234 /* optimal 9-element median search */
235 SWAPmed(1, 2);
236 SWAPmed(4, 5);
237 SWAPmed(7, 8);
238 SWAPmed(0, 1);
239 SWAPmed(3, 4);
240 SWAPmed(6, 7);
241 SWAPmed(1, 2);
242 SWAPmed(4, 5);
243 SWAPmed(7, 8);
244 SWAPmed(0, 3);
245 SWAPmed(5, 8);
246 SWAPmed(4, 7);
247 SWAPmed(3, 6);
248 SWAPmed(1, 4);
249 SWAPmed(2, 5);
250 SWAPmed(4, 7);
251 SWAPmed(4, 2);
252 SWAPmed(6, 4);
253 SWAPmed(4, 2);
254 outp[c] = fmaxf(med[4] + outp[1], 0.0f);
255 }
256 }
257 }
258 }
259}
260#undef SWAP
261
262static void green_equilibration_lavg(float *out, const float *const in, const int width, const int height,
263 const uint32_t filters, const int x, const int y, const float thr)
264{
265 const float maximum = 1.0f;
266
267 int oj = 2, oi = 2;
268 if(FC(oj + y, oi + x, filters) != 1) oj++;
269 if(FC(oj + y, oi + x, filters) != 1) oi++;
270 if(FC(oj + y, oi + x, filters) != 1) oj--;
271
273
274#ifdef _OPENMP
275#pragma omp parallel for default(none) \
276 dt_omp_firstprivate(height, in, thr, width, maximum) \
277 shared(out, oi, oj) \
278 schedule(static) collapse(2)
279#endif
280 for(size_t j = oj; j < height - 2; j += 2)
281 {
282 for(size_t i = oi; i < width - 2; i += 2)
283 {
284 const float o1_1 = in[(j - 1) * width + i - 1];
285 const float o1_2 = in[(j - 1) * width + i + 1];
286 const float o1_3 = in[(j + 1) * width + i - 1];
287 const float o1_4 = in[(j + 1) * width + i + 1];
288 const float o2_1 = in[(j - 2) * width + i];
289 const float o2_2 = in[(j + 2) * width + i];
290 const float o2_3 = in[j * width + i - 2];
291 const float o2_4 = in[j * width + i + 2];
292
293 const float m1 = (o1_1 + o1_2 + o1_3 + o1_4) / 4.0f;
294 const float m2 = (o2_1 + o2_2 + o2_3 + o2_4) / 4.0f;
295
296 // prevent divide by zero and ...
297 // guard against m1/m2 becoming too large (due to m2 being too small) which results in hot pixels
298 // also m1 must be checked to be positive
299 if((m2 > 0.0f) && (m1 > 0.0f) && (m1 / m2 < maximum * 2.0f))
300 {
301 const float c1 = (fabsf(o1_1 - o1_2) + fabsf(o1_1 - o1_3) + fabsf(o1_1 - o1_4) + fabsf(o1_2 - o1_3)
302 + fabsf(o1_3 - o1_4) + fabsf(o1_2 - o1_4)) / 6.0f;
303 const float c2 = (fabsf(o2_1 - o2_2) + fabsf(o2_1 - o2_3) + fabsf(o2_1 - o2_4) + fabsf(o2_2 - o2_3)
304 + fabsf(o2_3 - o2_4) + fabsf(o2_2 - o2_4)) / 6.0f;
305 if((in[j * width + i] < maximum * 0.95f) && (c1 < maximum * thr) && (c2 < maximum * thr))
306 {
307 out[j * width + i] = in[j * width + i] * m1 / m2;
308 }
309 }
310 }
311 }
312}
313
314static void green_equilibration_favg(float *out, const float *const in, const int width, const int height,
315 const uint32_t filters, const int x, const int y)
316{
317 int oj = 0, oi = 0;
318 // const float ratio_max = 1.1f;
319 double sum1 = 0.0, sum2 = 0.0, gr_ratio;
320
321 if((FC(oj + y, oi + x, filters) & 1) != 1) oi++;
322 const int g2_offset = oi ? -1 : 1;
324#ifdef _OPENMP
325#pragma omp parallel for default(none) \
326 dt_omp_firstprivate(g2_offset, height, in, width) \
327 reduction(+ : sum1, sum2) \
328 shared(oi, oj) \
329 schedule(static) collapse(2)
330#endif
331 for(size_t j = oj; j < (height - 1); j += 2)
332 {
333 for(size_t i = oi; i < (width - 1 - g2_offset); i += 2)
334 {
335 sum1 += in[j * width + i];
336 sum2 += in[(j + 1) * width + i + g2_offset];
337 }
338 }
339
340 if(sum1 > 0.0 && sum2 > 0.0)
341 gr_ratio = sum2 / sum1;
342 else
343 return;
344
345#ifdef _OPENMP
346#pragma omp parallel for default(none) \
347 dt_omp_firstprivate(g2_offset, height, in, width) \
348 shared(out, oi, oj, gr_ratio) \
349 schedule(static) collapse(2)
350#endif
351 for(int j = oj; j < (height - 1); j += 2)
352 {
353 for(int i = oi; i < (width - 1 - g2_offset); i += 2)
354 {
355 out[(size_t)j * width + i] = in[(size_t)j * width + i] * gr_ratio;
356 }
357 }
358}
359
360#ifdef HAVE_OPENCL
361
362// color smoothing step by multiple passes of median filtering
363static int color_smoothing_cl(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe,
364 const dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out,
365 const dt_iop_roi_t *const roi_out, const int passes)
366{
368
369 const int devid = pipe->devid;
370 const int width = roi_out->width;
371 const int height = roi_out->height;
372
373 cl_int err = -999;
374
375 cl_mem dev_tmp = dt_opencl_alloc_device(devid, width, height, sizeof(float) * 4);
376 if(dev_tmp == NULL) goto error;
377
378 dt_opencl_local_buffer_t locopt
379 = (dt_opencl_local_buffer_t){ .xoffset = 2*1, .xfactor = 1, .yoffset = 2*1, .yfactor = 1,
380 .cellsize = 4 * sizeof(float), .overhead = 0,
381 .sizex = 1 << 8, .sizey = 1 << 8 };
382
383 if(!dt_opencl_local_buffer_opt(devid, gd->kernel_color_smoothing, &locopt))
384 goto error;
385
386 // two buffer references for our ping-pong
387 cl_mem dev_t1 = dev_out;
388 cl_mem dev_t2 = dev_tmp;
389
390 for(int pass = 0; pass < passes; pass++)
391 {
392 size_t sizes[] = { ROUNDUP(width, locopt.sizex), ROUNDUP(height, locopt.sizey), 1 };
393 size_t local[] = { locopt.sizex, locopt.sizey, 1 };
394 dt_opencl_set_kernel_arg(devid, gd->kernel_color_smoothing, 0, sizeof(cl_mem), &dev_t1);
395 dt_opencl_set_kernel_arg(devid, gd->kernel_color_smoothing, 1, sizeof(cl_mem), &dev_t2);
396 dt_opencl_set_kernel_arg(devid, gd->kernel_color_smoothing, 2, sizeof(int), &width);
397 dt_opencl_set_kernel_arg(devid, gd->kernel_color_smoothing, 3, sizeof(int), &height);
398 dt_opencl_set_kernel_arg(devid, gd->kernel_color_smoothing, 4,
399 sizeof(float) * 4 * (locopt.sizex + 2) * (locopt.sizey + 2), NULL);
400 err = dt_opencl_enqueue_kernel_2d_with_local(devid, gd->kernel_color_smoothing, sizes, local);
401 if(err != CL_SUCCESS) goto error;
402
403 // swap dev_t1 and dev_t2
404 cl_mem t = dev_t1;
405 dev_t1 = dev_t2;
406 dev_t2 = t;
407 }
408
409 // after last step we find final output in dev_t1.
410 // let's see if this is in dev_tmp1 and needs to be copied to dev_out
411 if(dev_t1 == dev_tmp)
412 {
413 // copy data from dev_tmp -> dev_out
414 size_t origin[] = { 0, 0, 0 };
415 size_t region[] = { width, height, 1 };
416 err = dt_opencl_enqueue_copy_image(devid, dev_tmp, dev_out, origin, origin, region);
417 if(err != CL_SUCCESS) goto error;
418 }
419
421 return TRUE;
422
423error:
425 dt_print(DT_DEBUG_OPENCL, "[opencl_demosaic_color_smoothing] couldn't enqueue kernel! %d\n", err);
426 return FALSE;
427}
428
429static int green_equilibration_cl(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe,
430 const dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out,
431 const dt_iop_roi_t *const roi_in)
432{
435
436 const int devid = pipe->devid;
437 const int width = roi_in->width;
438 const int height = roi_in->height;
439
440 cl_mem dev_tmp = NULL;
441 cl_mem dev_m = NULL;
442 cl_mem dev_r = NULL;
443 cl_mem dev_in1 = NULL;
444 cl_mem dev_out1 = NULL;
445 cl_mem dev_in2 = NULL;
446 cl_mem dev_out2 = NULL;
447 float *sumsum = NULL;
448
449 cl_int err = -999;
450
451 if(data->green_eq == DT_IOP_GREEN_EQ_BOTH)
452 {
453 dev_tmp = dt_opencl_alloc_device(devid, width, height, sizeof(float));
454 if(dev_tmp == NULL) goto error;
455 }
456
457 switch(data->green_eq)
458 {
460 dev_in1 = dev_in;
461 dev_out1 = dev_out;
462 break;
464 dev_in2 = dev_in;
465 dev_out2 = dev_out;
466 break;
468 dev_in1 = dev_in;
469 dev_out1 = dev_tmp;
470 dev_in2 = dev_tmp;
471 dev_out2 = dev_out;
472 break;
474 default:
475 goto error;
476 }
477
478 if(data->green_eq == DT_IOP_GREEN_EQ_FULL || data->green_eq == DT_IOP_GREEN_EQ_BOTH)
479 {
480 dt_opencl_local_buffer_t flocopt
481 = (dt_opencl_local_buffer_t){ .xoffset = 0, .xfactor = 1, .yoffset = 0, .yfactor = 1,
482 .cellsize = 2 * sizeof(float), .overhead = 0,
483 .sizex = 1 << 4, .sizey = 1 << 4 };
484
485 if(!dt_opencl_local_buffer_opt(devid, gd->kernel_green_eq_favg_reduce_first, &flocopt))
486 goto error;
487
488 const size_t bwidth = ROUNDUP(width, flocopt.sizex);
489 const size_t bheight = ROUNDUP(height, flocopt.sizey);
490
491 const int bufsize = (bwidth / flocopt.sizex) * (bheight / flocopt.sizey);
492
493 dev_m = dt_opencl_alloc_device_buffer(devid, sizeof(float) * 2 * bufsize);
494 if(dev_m == NULL) goto error;
495
496 size_t fsizes[3] = { bwidth, bheight, 1 };
497 size_t flocal[3] = { flocopt.sizex, flocopt.sizey, 1 };
498 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_first, 0, sizeof(cl_mem), &dev_in1);
499 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_first, 1, sizeof(int), &width);
500 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_first, 2, sizeof(int), &height);
501 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_first, 3, sizeof(cl_mem), &dev_m);
502 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_first, 4, sizeof(uint32_t), (void *)&piece->dsc_in.filters);
503 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_first, 5, sizeof(int), &roi_in->x);
504 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_first, 6, sizeof(int), &roi_in->y);
505 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_first, 7,
506 sizeof(float) * 2 * flocopt.sizex * flocopt.sizey, NULL);
507 err = dt_opencl_enqueue_kernel_2d_with_local(devid, gd->kernel_green_eq_favg_reduce_first, fsizes,
508 flocal);
509 if(err != CL_SUCCESS) goto error;
510
511 dt_opencl_local_buffer_t slocopt
512 = (dt_opencl_local_buffer_t){ .xoffset = 0, .xfactor = 1, .yoffset = 0, .yfactor = 1,
513 .cellsize = sizeof(float) * 2, .overhead = 0,
514 .sizex = 1 << 16, .sizey = 1 };
515
516 if(!dt_opencl_local_buffer_opt(devid, gd->kernel_green_eq_favg_reduce_second, &slocopt))
517 goto error;
518
519 const int reducesize = MIN(REDUCESIZE, ROUNDUP(bufsize, slocopt.sizex) / slocopt.sizex);
520
521 dev_r = dt_opencl_alloc_device_buffer(devid, sizeof(float) * 2 * reducesize);
522 if(dev_r == NULL) goto error;
523
524 size_t ssizes[3] = { (size_t)reducesize * slocopt.sizex, 1, 1 };
525 size_t slocal[3] = { slocopt.sizex, 1, 1 };
526 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_second, 0, sizeof(cl_mem), &dev_m);
527 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_second, 1, sizeof(cl_mem), &dev_r);
528 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_second, 2, sizeof(int), &bufsize);
529 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_second, 3, sizeof(float) * 2 * slocopt.sizex, NULL);
530 err = dt_opencl_enqueue_kernel_2d_with_local(devid, gd->kernel_green_eq_favg_reduce_second, ssizes,
531 slocal);
532 if(err != CL_SUCCESS) goto error;
533
534 sumsum = dt_pixelpipe_cache_alloc_align_float((size_t)2 * reducesize, pipe);
535 if(sumsum == NULL) goto error;
536 err = dt_opencl_read_buffer_from_device(devid, (void *)sumsum, dev_r, 0,
537 sizeof(float) * 2 * reducesize, CL_TRUE);
538 if(err != CL_SUCCESS) goto error;
539
540 float sum1 = 0.0f, sum2 = 0.0f;
541 for(int k = 0; k < reducesize; k++)
542 {
543 sum1 += sumsum[2 * k];
544 sum2 += sumsum[2 * k + 1];
545 }
546
547 const float gr_ratio = (sum1 > 0.0f && sum2 > 0.0f) ? sum2 / sum1 : 1.0f;
548
549 size_t asizes[3] = { ROUNDUPDWD(width, devid), ROUNDUPDHT(height, devid), 1 };
550 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_apply, 0, sizeof(cl_mem), &dev_in1);
551 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_apply, 1, sizeof(cl_mem), &dev_out1);
552 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_apply, 2, sizeof(int), &width);
553 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_apply, 3, sizeof(int), &height);
554 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_apply, 4, sizeof(uint32_t), (void *)&piece->dsc_in.filters);
555 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_apply, 5, sizeof(int), &roi_in->x);
556 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_apply, 6, sizeof(int), &roi_in->y);
557 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_apply, 7, sizeof(float), &gr_ratio);
558 err = dt_opencl_enqueue_kernel_2d(devid, gd->kernel_green_eq_favg_apply, asizes);
559 if(err != CL_SUCCESS) goto error;
560 }
561
562 if(data->green_eq == DT_IOP_GREEN_EQ_LOCAL || data->green_eq == DT_IOP_GREEN_EQ_BOTH)
563 {
564 const dt_image_t *img = &self->dev->image_storage;
565 const float threshold = 0.0001f * img->exif_iso;
566
567 dt_opencl_local_buffer_t locopt
568 = (dt_opencl_local_buffer_t){ .xoffset = 2*2, .xfactor = 1, .yoffset = 2*2, .yfactor = 1,
569 .cellsize = 1 * sizeof(float), .overhead = 0,
570 .sizex = 1 << 8, .sizey = 1 << 8 };
571
572 if(!dt_opencl_local_buffer_opt(devid, gd->kernel_green_eq_lavg, &locopt))
573 goto error;
574
575 size_t sizes[3] = { ROUNDUP(width, locopt.sizex), ROUNDUP(height, locopt.sizey), 1 };
576 size_t local[3] = { locopt.sizex, locopt.sizey, 1 };
577 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_lavg, 0, sizeof(cl_mem), &dev_in2);
578 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_lavg, 1, sizeof(cl_mem), &dev_out2);
579 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_lavg, 2, sizeof(int), &width);
580 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_lavg, 3, sizeof(int), &height);
581 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_lavg, 4, sizeof(uint32_t), (void *)&piece->dsc_in.filters);
582 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_lavg, 5, sizeof(int), &roi_in->x);
583 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_lavg, 6, sizeof(int), &roi_in->y);
584 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_lavg, 7, sizeof(float), (void *)&threshold);
585 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_lavg, 8,
586 sizeof(float) * (locopt.sizex + 4) * (locopt.sizey + 4), NULL);
587 err = dt_opencl_enqueue_kernel_2d_with_local(devid, gd->kernel_green_eq_lavg, sizes, local);
588 if(err != CL_SUCCESS) goto error;
589 }
590
595 return TRUE;
596
597error:
602 dt_print(DT_DEBUG_OPENCL, "[opencl_demosaic_green_equilibration] couldn't enqueue kernel! %d\n", err);
603 return FALSE;
604}
605
606#endif // HAVE_OPENCL
607
608// clang-format off
609// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
610// vim: shiftwidth=2 expandtab tabstop=2 cindent
611// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
612// clang-format on
static void error(char *msg)
Definition ashift_lsd.c:202
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
static void lin_interpolate(float *out, const float *const in, const dt_iop_roi_t *const roi_out, const dt_iop_roi_t *const roi_in, const uint32_t filters, const uint8_t(*const xtrans)[6])
Definition basic.c:21
static void green_equilibration_favg(float *out, const float *const in, const int width, const int height, const uint32_t filters, const int x, const int y)
Definition basic.c:314
static void pre_median_b(float *out, const float *const in, const dt_iop_roi_t *const roi, const uint32_t filters, const int num_passes, const float threshold)
Definition basic.c:144
static void green_equilibration_lavg(float *out, const float *const in, const int width, const int height, const uint32_t filters, const int x, const int y, const float thr)
Definition basic.c:262
#define SWAPmed(I, J)
Definition basic.c:200
static void pre_median(float *out, const float *const in, const dt_iop_roi_t *const roi, const uint32_t filters, const int num_passes, const float threshold)
Definition basic.c:194
#define SWAP(a, b)
Definition basic.c:134
static void color_smoothing(float *out, const dt_iop_roi_t *const roi_out, const int num_passes)
Definition basic.c:203
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
static float lookup(read_only image2d_t lut, const float x)
Definition color_conversion.h:84
const float i
Definition colorspaces_inline_conversions.h:669
const float c
Definition colorspaces_inline_conversions.h:1365
const dt_aligned_pixel_t f
Definition colorspaces_inline_conversions.h:256
const float threshold
Definition colorspaces_inline_conversions.h:340
static const dt_colormatrix_t dt_aligned_pixel_t out
Definition colorspaces_inline_conversions.h:184
static const int row
Definition colorspaces_inline_conversions.h:175
void dt_print(dt_debug_thread_t thread, const char *msg,...)
Definition darktable.c:1530
@ DT_DEBUG_OPENCL
Definition darktable.h:642
#define dt_free(ptr)
Definition darktable.h:380
#define dt_pixelpipe_cache_free_align(mem)
Definition darktable.h:377
#define dt_pixelpipe_cache_alloc_align_float(pixels, pipe)
Definition darktable.h:366
static int FC(const int row, const int col, const unsigned int filters)
Definition data/kernels/common.h:47
#define REDUCESIZE
Definition demosaic.c:101
@ DT_IOP_GREEN_EQ_LOCAL
Definition demosaic.c:133
@ DT_IOP_GREEN_EQ_FULL
Definition demosaic.c:134
@ DT_IOP_GREEN_EQ_BOTH
Definition demosaic.c:135
@ DT_IOP_GREEN_EQ_NO
Definition demosaic.c:132
static void weight(const float *c1, const float *c2, const float sharpen, dt_aligned_pixel_t weight)
Definition eaw.c:33
const dt_collection_filter_flag_t colors[6]
Definition filter.c:303
static void dt_iop_image_copy_by_size(float *const __restrict__ out, const float *const __restrict__ in, const size_t width, const size_t height, const size_t ch)
Definition imagebuf.h:92
static int fcol(const int row, const int col, const uint32_t filters, const uint8_t(*const xtrans)[6])
Definition imageop_math.h:233
static const float x
Definition iop_profile.h:239
const int t
Definition iop_profile.h:227
size_t size
Definition mipmap_cache.c:3
k
Definition derive_filmic_v6_gamut_mapping.py:67
static int dt_opencl_enqueue_kernel_2d(const int dev, const int kernel, const size_t *sizes)
Definition opencl.h:574
static int dt_opencl_set_kernel_arg(const int dev, const int kernel, const size_t size, const void *arg)
Definition opencl.h:570
static void dt_opencl_release_mem_object(void *mem)
Definition opencl.h:619
static int dt_opencl_enqueue_kernel_2d_with_local(const int dev, const int kernel, const size_t *sizes, const size_t *local)
Definition opencl.h:578
Definition pixelpipe_hb.h:95
struct dt_iop_module_t *void * data
Definition pixelpipe_hb.h:96
Definition pixelpipe_hb.h:216
Definition common/image.h:247
float exif_iso
Definition common/image.h:254
Definition demosaic.c:217
Definition demosaic.c:159
Definition imageop.h:216
dt_iop_global_data_t * global_data
Definition imageop.h:279
Definition imageop.h:67
int x
Definition imageop.h:68
int width
Definition imageop.h:68
int height
Definition imageop.h:68
int y
Definition imageop.h:68
#define c2
Definition colorspaces_inline_conversions.h:1055
#define c1
Definition colorspaces_inline_conversions.h:1054
#define MIN(a, b)
Definition thinplate.c:32