22 const dt_iop_roi_t *
const roi_in,
const uint32_t filters,
23 const uint8_t (*
const xtrans)[6])
25 const int colors = (filters == 9) ? 3 : 4;
29#pragma omp parallel for default(none) \
30 dt_omp_firstprivate(colors, filters, in, roi_in, roi_out, xtrans) \
35 for(
int col = 0; col < roi_out->
width; col++)
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;
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)
45 const int f =
fcol(y + roi_in->
y,
x + roi_in->
x, filters, xtrans);
46 sum[
f] += in[y * roi_in->
width +
x];
49 const int f =
fcol(
row + roi_in->
y, col + roi_in->
x, filters, xtrans);
55 if(
c !=
f && count[
c] != 0)
74 int(*
const lookup)[16][32] = malloc(
sizeof(
int) * 16 * 16 * 32);
76 const int size = (filters == 9) ? 6 : 16;
78 for(
int col = 0; col <
size; col++)
82 const int f =
fcol(
row + roi_in->
y, col + roi_in->
x, filters, xtrans);
84 for(
int y = -1; y <= 1; y++)
85 for(
int x = -1;
x <= 1;
x++)
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);
106#pragma omp parallel for default(none) \
107 dt_omp_firstprivate(colors, in, lookup, roi_in, roi_out, size) \
114 const float *buf_in = in + roi_in->
width *
row + 1;
115 for(
int col = 1; col < roi_out->
width - 1; col++)
117 dt_aligned_pixel_t sum = { 0.0f };
120 for(
int i = *ip++;
i--; ip += 3) sum[ip[2]] += buf_in[ip[0]] * ip[1];
122 for(
int i =
colors; --
i; ip += 2) buf[*ip] = sum[ip[0]] / ip[1];
136 const float tmp = (b); \
142 #pragma omp declare simd aligned(in, out)
145 const int num_passes,
const float threshold)
150 const int lim[5] = { 0, 1, 2, 1, 0 };
151 for(
int pass = 0; pass < num_passes; pass++)
154#pragma omp parallel for default(none) \
155 dt_omp_firstprivate(filters, in, lim, roi, threshold) \
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)
169 for(
int k = 0,
i = 0;
i < 5;
i++)
171 for(
int j = -lim[
i]; j <= lim[
i]; j += 2)
175 med[k++] = pixi[roi->
width * (
i - 2) + j];
179 med[k++] = 64.0f + pixi[roi->
width * (
i - 2) + j];
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]);
195 const int num_passes,
const float threshold)
200#define SWAPmed(I, J) \
201 if(med[I] > med[J]) SWAP(med[I], med[J])
205 const int width4 = 4 * roi_out->
width;
207 for(
int pass = 0; pass < num_passes; pass++)
209 for(
int c = 0;
c < 3;
c += 2)
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];
217#pragma omp parallel for default(none) \
218 dt_omp_firstprivate(roi_out, width4) \
222 for(
int j = 1; j < roi_out->
height - 1; j++)
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)
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],
254 outp[
c] = fmaxf(med[4] + outp[1], 0.0f);
263 const uint32_t filters,
const int x,
const int y,
const float thr)
265 const float maximum = 1.0f;
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--;
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)
280 for(
size_t j = oj; j <
height - 2; j += 2)
282 for(
size_t i = oi;
i <
width - 2;
i += 2)
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];
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;
299 if((m2 > 0.0f) && (m1 > 0.0f) && (m1 / m2 < maximum * 2.0f))
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))
315 const uint32_t filters,
const int x,
const int y)
319 double sum1 = 0.0, sum2 = 0.0, gr_ratio;
321 if((
FC(oj + y, oi +
x, filters) & 1) != 1) oi++;
322 const int g2_offset = oi ? -1 : 1;
325#pragma omp parallel for default(none) \
326 dt_omp_firstprivate(g2_offset, height, in, width) \
327 reduction(+ : sum1, sum2) \
329 schedule(static) collapse(2)
331 for(
size_t j = oj; j < (
height - 1); j += 2)
333 for(
size_t i = oi;
i < (
width - 1 - g2_offset);
i += 2)
335 sum1 += in[j *
width +
i];
336 sum2 += in[(j + 1) *
width +
i + g2_offset];
340 if(sum1 > 0.0 && sum2 > 0.0)
341 gr_ratio = sum2 / sum1;
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)
351 for(
int j = oj; j < (
height - 1); j += 2)
353 for(
int i = oi;
i < (
width - 1 - g2_offset);
i += 2)
369 const int devid = pipe->devid;
375 cl_mem dev_tmp = dt_opencl_alloc_device(devid,
width,
height,
sizeof(
float) * 4);
376 if(dev_tmp == NULL)
goto error;
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 };
383 if(!dt_opencl_local_buffer_opt(devid, gd->kernel_color_smoothing, &locopt))
387 cl_mem dev_t1 = dev_out;
388 cl_mem dev_t2 = dev_tmp;
390 for(
int pass = 0; pass < passes; pass++)
392 size_t sizes[] = { ROUNDUP(
width, locopt.sizex), ROUNDUP(
height, locopt.sizey), 1 };
393 size_t local[] = { locopt.sizex, locopt.sizey, 1 };
399 sizeof(
float) * 4 * (locopt.sizex + 2) * (locopt.sizey + 2), NULL);
401 if(err != CL_SUCCESS)
goto error;
411 if(dev_t1 == dev_tmp)
414 size_t origin[] = { 0, 0, 0 };
416 err = dt_opencl_enqueue_copy_image(devid, dev_tmp, dev_out, origin, origin, region);
417 if(err != CL_SUCCESS)
goto error;
436 const int devid = pipe->devid;
440 cl_mem dev_tmp = 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;
453 dev_tmp = dt_opencl_alloc_device(devid,
width,
height,
sizeof(
float));
454 if(dev_tmp == NULL)
goto error;
457 switch(data->green_eq)
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 };
485 if(!dt_opencl_local_buffer_opt(devid, gd->kernel_green_eq_favg_reduce_first, &flocopt))
488 const size_t bwidth = ROUNDUP(
width, flocopt.sizex);
489 const size_t bheight = ROUNDUP(
height, flocopt.sizey);
491 const int bufsize = (bwidth / flocopt.sizex) * (bheight / flocopt.sizey);
493 dev_m = dt_opencl_alloc_device_buffer(devid,
sizeof(
float) * 2 * bufsize);
494 if(dev_m == NULL)
goto error;
496 size_t fsizes[3] = { bwidth, bheight, 1 };
497 size_t flocal[3] = { flocopt.sizex, flocopt.sizey, 1 };
502 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_first, 4,
sizeof(uint32_t), (
void *)&piece->dsc_in.filters);
506 sizeof(
float) * 2 * flocopt.sizex * flocopt.sizey, NULL);
509 if(err != CL_SUCCESS)
goto error;
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 };
516 if(!dt_opencl_local_buffer_opt(devid, gd->kernel_green_eq_favg_reduce_second, &slocopt))
519 const int reducesize =
MIN(
REDUCESIZE, ROUNDUP(bufsize, slocopt.sizex) / slocopt.sizex);
521 dev_r = dt_opencl_alloc_device_buffer(devid,
sizeof(
float) * 2 * reducesize);
522 if(dev_r == NULL)
goto error;
524 size_t ssizes[3] = { (size_t)reducesize * slocopt.sizex, 1, 1 };
525 size_t slocal[3] = { slocopt.sizex, 1, 1 };
529 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_reduce_second, 3,
sizeof(
float) * 2 * slocopt.sizex, NULL);
532 if(err != CL_SUCCESS)
goto error;
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;
540 float sum1 = 0.0f, sum2 = 0.0f;
541 for(
int k = 0;
k < reducesize;
k++)
543 sum1 += sumsum[2 *
k];
544 sum2 += sumsum[2 *
k + 1];
547 const float gr_ratio = (sum1 > 0.0f && sum2 > 0.0f) ? sum2 / sum1 : 1.0f;
549 size_t asizes[3] = { ROUNDUPDWD(
width, devid), ROUNDUPDHT(
height, devid), 1 };
554 dt_opencl_set_kernel_arg(devid, gd->kernel_green_eq_favg_apply, 4,
sizeof(uint32_t), (
void *)&piece->dsc_in.filters);
559 if(err != CL_SUCCESS)
goto error;
564 const dt_image_t *img = &self->dev->image_storage;
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 };
572 if(!dt_opencl_local_buffer_opt(devid, gd->kernel_green_eq_lavg, &locopt))
575 size_t sizes[3] = { ROUNDUP(
width, locopt.sizex), ROUNDUP(
height, locopt.sizey), 1 };
576 size_t local[3] = { locopt.sizex, locopt.sizey, 1 };
586 sizeof(
float) * (locopt.sizex + 4) * (locopt.sizey + 4), NULL);
588 if(err != CL_SUCCESS)
goto error;
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
dt_iop_global_data_t * global_data
Definition imageop.h:279
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