139 const dt_iop_roi_t *
const roi_in,
const uint32_t filters,
const uint32_t mode,
float *
const restrict gamma_in,
float *
const restrict gamma_out)
151 float h1 = expf( -1.0f / 8.0f);
152 float h2 = expf( -4.0f / 8.0f);
153 float h3 = expf( -9.0f / 8.0f);
154 float h4 = expf(-16.0f / 8.0f);
155 float hs = h0 + 2.0f * (h1 + h2 + h3 + h4);
163 const int medians = (mode < 2) ? mode : 3;
165 const int refine = (mode > 2) ? mode - 2 : 0;
168 const float revscaler = 1.0f / scaler;
173 #pragma omp parallel \
174 dt_omp_firstprivate(width, height, out, in, scaler, revscaler, filters)
181 for(
int i = 1;
i < 6;
i++)
188 #pragma omp for schedule(simd:dynamic, 6) collapse(2)
190 for(
int tile_vertical = 0; tile_vertical < num_vertical; tile_vertical++)
192 for(
int tile_horizontal = 0; tile_horizontal < num_horizontal; tile_horizontal++)
213 cfa[0] =
calc_gamma(revscaler * in[idx], gamma_in);
218 for(
int rr = 2; rr < last_rr - 2; rr++)
221 for(
int cc = 2 + (
FC(rr, 2, filters) & 1); cc < last_cc - 2; cc += 2)
223 float *cfa = qix[5] + rr *
LMMSE_GRP + cc;
224 const float v0 = 0.0625f * (cfa[-
w1 - 1] + cfa[-
w1 + 1] + cfa[
w1 - 1] + cfa[
w1 + 1]) + 0.25f * cfa[0];
226 float *hdiff = qix[0] + rr *
LMMSE_GRP + cc;
227 hdiff[0] = -0.25f * (cfa[ -2] + cfa[ 2]) + 0.5f * (cfa[ -1] + cfa[0] + cfa[ 1]);
228 const float Y0 = v0 + 0.5f * hdiff[0];
229 hdiff[0] = (cfa[0] > 1.75f * Y0) ?
median3f(hdiff[0], cfa[ -1], cfa[ 1]) :
limf(hdiff[0], 0.0f, 1.0f);
233 float *vdiff = qix[1] + rr *
LMMSE_GRP + cc;
234 vdiff[0] = -0.25f * (cfa[-
w2] + cfa[
w2]) + 0.5f * (cfa[-
w1] + cfa[0] + cfa[
w1]);
235 const float Y1 = v0 + 0.5f * vdiff[0];
236 vdiff[0] = (cfa[0] > 1.75f * Y1) ?
median3f(vdiff[0], cfa[-
w1], cfa[
w1]) :
limf(vdiff[0], 0.0f, 1.0f);
241 for(
int ccc = 2 + (
FC(rr, 3, filters) & 1); ccc < last_cc - 2; ccc += 2)
243 float *cfa = qix[5] + rr *
LMMSE_GRP + ccc;
244 float *hdiff = qix[0] + rr *
LMMSE_GRP + ccc;
245 float *vdiff = qix[1] + rr *
LMMSE_GRP + ccc;
246 hdiff[0] = 0.25f * (cfa[ -2] + cfa[ 2]) - 0.5f * (cfa[ -1] + cfa[0] + cfa[ 1]);
247 vdiff[0] = 0.25f * (cfa[-
w2] + cfa[
w2]) - 0.5f * (cfa[-
w1] + cfa[0] + cfa[
w1]);
248 hdiff[0] =
limf(hdiff[0], -1.0f, 0.0f) + cfa[0];
249 vdiff[0] =
limf(vdiff[0], -1.0f, 0.0f) + cfa[0];
254 for (
int rr = 4; rr < last_rr - 4; rr++)
256 for(
int cc = 4; cc < last_cc - 4; cc++)
258 float *hdiff = qix[0] + rr *
LMMSE_GRP + cc;
259 float *vdiff = qix[1] + rr *
LMMSE_GRP + cc;
260 float *hlp = qix[2] + rr *
LMMSE_GRP + cc;
261 float *vlp = qix[3] + rr *
LMMSE_GRP + cc;
262 hlp[0] = h0 * hdiff[0] + h1 * (hdiff[ -1] + hdiff[ 1]) + h2 * (hdiff[ -2] + hdiff[ 2]) + h3 * (hdiff[ -3] + hdiff[ 3]) + h4 * (hdiff[ -4] + hdiff[ 4]);
263 vlp[0] = h0 * vdiff[0] + h1 * (vdiff[-
w1] + vdiff[
w1]) + h2 * (vdiff[-
w2] + vdiff[
w2]) + h3 * (vdiff[-
w3] + vdiff[
w3]) + h4 * (vdiff[-
w4] + vdiff[
w4]);
267 for(
int rr = 4; rr < last_rr - 4; rr++)
269 for(
int cc = 4 + (
FC(rr, 4, filters) & 1); cc < last_cc - 4; cc += 2)
271 float *hdiff = qix[0] + rr *
LMMSE_GRP + cc;
272 float *vdiff = qix[1] + rr *
LMMSE_GRP + cc;
273 float *hlp = qix[2] + rr *
LMMSE_GRP + cc;
274 float *vlp = qix[3] + rr *
LMMSE_GRP + cc;
275 float *interp = qix[4] + rr *
LMMSE_GRP + cc;
286 float mu = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) / 9.0f;
287 float vx = 1e-7f +
sqf(p1 - mu) +
sqf(p2 - mu) +
sqf(p3 - mu) +
sqf(p4 - mu) +
sqf(p5 - mu) +
sqf(p6 - mu) +
sqf(p7 - mu) +
sqf(p8 - mu) +
sqf(p9 - mu);
298 float xh = (hdiff[0] * vx + hlp[0] * vn) / (vx + vn);
299 float vh = vx * vn / (vx + vn);
311 mu = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) / 9.0f;
312 vx = 1e-7f +
sqf(p1 - mu) +
sqf(p2 - mu) +
sqf(p3 - mu) +
sqf(p4 - mu) +
sqf(p5 - mu) +
sqf(p6 - mu) +
sqf(p7 - mu) +
sqf(p8 - mu) +
sqf(p9 - mu);
323 float xv = (vdiff[0] * vx + vlp[0] * vn) / (vx + vn);
324 float vv = vx * vn / (vx + vn);
326 interp[0] = (xh * vv + xv * vh) / (vh + vv);
331 for(
int rr = 0, row_in = rowStart -
BORDER_AROUND; rr < last_rr; rr++, row_in++)
333 for(
int cc = 0, col_in = colStart -
BORDER_AROUND; cc < last_cc; cc++, col_in++)
335 const int c =
FC(rr, cc, filters);
336 const gboolean inside = ((row_in >= 0) && (row_in <
height) && (col_in >= 0) && (col_in <
width));
338 colc[0] = (inside) ? qix[5][rr *
LMMSE_GRP + cc] : 0.0f;
341 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
342 float *interp = qix[4] + rr *
LMMSE_GRP + cc;
343 col1[0] = (inside) ? colc[0] + interp[0] : 0.0f;
350 for(
int rr = 1; rr < last_rr - 1; rr++)
352 for(
int cc = 1 + (
FC(rr, 2, filters) & 1),
c =
FC(rr, cc + 1, filters); cc < last_cc - 1; cc += 2)
355 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
356 colc[0] = col1[0] + 0.5f * (colc[ -1] - col1[ -1] + colc[ 1] - col1[ 1]);
359 colc[0] = col1[0] + 0.5f * (colc[-
w1] - col1[-
w1] + colc[
w1] - col1[
w1]);
365 for(
int rr = 1; rr < last_rr - 1; rr++)
367 for(
int cc = 1 + (
FC(rr, 1, filters) & 1),
c = 2 -
FC(rr, cc, filters); cc < last_cc - 1; cc += 2)
370 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
371 colc[0] = col1[0] + 0.25f * (colc[-
w1] - col1[-
w1] + colc[ -1] - col1[ -1] + colc[ 1] - col1[ 1] + colc[
w1] - col1[
w1]);
377 const int ccmin = (tile_horizontal == 0) ? 6 : 0 ;
378 const int ccmax = last_cc - ((tile_horizontal == num_horizontal - 1) ? 6 : 0);
379 const int rrmin = (tile_vertical == 0) ? 6 : 0 ;
380 const int rrmax = last_rr - ((tile_vertical == num_vertical - 1) ? 6 : 0);
383 for(
int pass = 0; pass < medians; pass++)
387 for(
int rr = 1; rr < last_rr - 1; rr++)
389 for(
int c = 0;
c < 3;
c += 2)
391 const int d =
c + 3 - (
c == 0 ? 0 : 1);
392 for(
int cc = 1; cc < last_cc - 1; cc++)
396 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
399 colc[-
w1 ] - col1[-
w1 ],
400 colc[-
w1+1] - col1[-
w1+1],
401 colc[ -1] - col1[ -1],
404 colc[
w1-1] - col1[
w1-1],
405 colc[
w1 ] - col1[
w1 ],
406 colc[
w1+1] - col1[
w1+1]);
412 for(
int rr = rrmin; rr < rrmax - 1; rr++)
414 float *col0 = qix[0] + rr *
LMMSE_GRP + ccmin;
415 float *col1 = qix[1] + rr *
LMMSE_GRP + ccmin;
416 float *col2 = qix[2] + rr *
LMMSE_GRP + ccmin;
417 float *corr3 = qix[3] + rr *
LMMSE_GRP + ccmin;
418 float *corr4 = qix[4] + rr *
LMMSE_GRP + ccmin;
419 int c0 =
FC(rr, 0, filters);
420 int c1 =
FC(rr, 1, filters);
425 const int d =
c1 + 3 - (
c1 == 0 ? 0 : 1);
428 float *corr_d = qix[
d] + rr *
LMMSE_GRP + ccmin;
429 for(cc = ccmin; cc < ccmax - 1; cc += 2)
431 col0[0] = col1[0] + corr3[0];
432 col2[0] = col1[0] + corr4[0];
440 col_c1[0] = col1[0] + corr_d[0];
441 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
453 col0[0] = col1[0] + corr3[0];
454 col2[0] = col1[0] + corr4[0];
460 const int d = c0 + 3 - (c0 == 0 ? 0 : 1);
461 float *col_c0 = qix[c0] + rr *
LMMSE_GRP + ccmin;
462 float *corr_d = qix[
d] + rr *
LMMSE_GRP + ccmin;
464 for(cc = ccmin; cc < ccmax - 1; cc += 2)
466 col_c0[0] = col1[0] + corr_d[0];
467 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
475 col0[0] = col1[0] + corr3[0];
476 col2[0] = col1[0] + corr4[0];
488 col_c0[0] = col1[0] + corr_d[0];
489 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
496 for(
int rrr = 4; rrr < last_rr - 4; rrr++)
498 for(
int ccc = 4; ccc < last_cc - 4; ccc++)
501 const int c =
FC(rrr, ccc, filters);
502 qix[
c][idx] = qix[5][idx];
507 for(
int step = 0; step <
refine; step++)
510 for(
int rr = rrmin + 2; rr < rrmax - 2; rr++)
512 for(
int cc = ccmin + 2 + (
FC(rr, 2, filters) & 1),
c =
FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
514 float *rgb1 = qix[1] + rr *
LMMSE_GRP + cc;
517 const float dL = 1.0f / (1.0f + fabsf(rgbc[ -2] - rgbc[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
518 const float dR = 1.0f / (1.0f + fabsf(rgbc[ 2] - rgbc[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
519 const float dU = 1.0f / (1.0f + fabsf(rgbc[-
w2] - rgbc[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
520 const float dD = 1.0f / (1.0f + fabsf(rgbc[
w2] - rgbc[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
521 rgb1[0] = (rgbc[0] + ((rgb1[-1] - rgbc[-1]) * dL + (rgb1[1] - rgbc[1]) * dR + (rgb1[-
w1] - rgbc[-
w1]) * dU + (rgb1[
w1] - rgbc[
w1]) * dD ) / (dL + dR + dU + dD));
525 for(
int rr = rrmin + 2; rr < rrmax - 2; rr++)
527 for(
int cc = ccmin + 2 + (
FC(rr, 3, filters) & 1),
c =
FC(rr, cc + 1, filters); cc < ccmax - 2; cc += 2)
529 for(
int i = 0;
i < 2;
c = 2 -
c,
i++)
531 float *rgb1 = qix[1] + rr *
LMMSE_GRP + cc;
534 const float dL = 1.0f / (1.0f + fabsf(rgb1[ -2] - rgb1[0]) + fabsf(rgbc[ 1] - rgbc[ -1]));
535 const float dR = 1.0f / (1.0f + fabsf(rgb1[ 2] - rgb1[0]) + fabsf(rgbc[ 1] - rgbc[ -1]));
536 const float dU = 1.0f / (1.0f + fabsf(rgb1[-
w2] - rgb1[0]) + fabsf(rgbc[
w1] - rgbc[-
w1]));
537 const float dD = 1.0f / (1.0f + fabsf(rgb1[
w2] - rgb1[0]) + fabsf(rgbc[
w1] - rgbc[-
w1]));
538 rgbc[0] = (rgb1[0] - ((rgb1[-1] - rgbc[-1]) * dL + (rgb1[1] - rgbc[1]) * dR + (rgb1[-
w1] - rgbc[-
w1]) * dU + (rgb1[
w1] - rgbc[
w1]) * dD ) / (dL + dR + dU + dD));
543 for(
int rr = rrmin + 2; rr < rrmax - 2; rr++)
545 for(
int cc = ccmin + 2 + (
FC(rr, 2, filters) & 1),
c = 2 -
FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
548 float *rgb1 = qix[1] + rr *
LMMSE_GRP + cc;
552 const float dL = 1.0f / (1.0f + fabsf(rgbd[ -2] - rgbd[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
553 const float dR = 1.0f / (1.0f + fabsf(rgbd[ 2] - rgbd[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
554 const float dU = 1.0f / (1.0f + fabsf(rgbd[-
w2] - rgbd[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
555 const float dD = 1.0f / (1.0f + fabsf(rgbd[
w2] - rgbd[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
556 rgbc[0] = (rgb1[0] - ((rgb1[-1] - rgbc[-1]) * dL + (rgb1[1] - rgbc[1]) * dR + (rgb1[-
w1] - rgbc[-
w1]) * dU + (rgb1[
w1] - rgbc[
w1]) * dD ) / (dL + dR + dU + dD));
563 const int first_vertical = rowStart + ((tile_vertical == 0) ? 0 :
LMMSE_OVERLAP);
564 const int last_vertical = rowEnd - ((tile_vertical == num_vertical - 1) ? 0 :
LMMSE_OVERLAP);
565 const int first_horizontal = colStart + ((tile_horizontal == 0) ? 0 :
LMMSE_OVERLAP);
566 const int last_horizontal = colEnd - ((tile_horizontal == num_horizontal - 1) ? 0 :
LMMSE_OVERLAP);
569 float *dest =
out + 4 * (
row *
width + first_horizontal);
571 float *col0 = qix[0] + idx;
572 float *col1 = qix[1] + idx;
573 float *col2 = qix[2] + idx;
574 for(
int col = first_horizontal; col < last_horizontal; col++, dest +=4, col0++, col1++, col2++)
576 dest[0] = scaler *
calc_gamma(col0[0], gamma_out);
577 dest[1] = scaler *
calc_gamma(col1[0], gamma_out);
578 dest[2] = scaler *
calc_gamma(col2[0], gamma_out);