135 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)
147 float h1 = expf( -1.0f / 8.0f);
148 float h2 = expf( -4.0f / 8.0f);
149 float h3 = expf( -9.0f / 8.0f);
150 float h4 = expf(-16.0f / 8.0f);
151 float hs = h0 + 2.0f * (h1 + h2 + h3 + h4);
159 const int medians = (mode < 2) ? mode : 3;
161 const int refine = (mode > 2) ? mode - 2 : 0;
164 const float revscaler = 1.0f / scaler;
176 for(
int i = 1;
i < 6;
i++)
183 #pragma omp for schedule(simd:dynamic, 6) collapse(2)
185 for(
int tile_vertical = 0; tile_vertical < num_vertical; tile_vertical++)
187 for(
int tile_horizontal = 0; tile_horizontal < num_horizontal; tile_horizontal++)
208 cfa[0] =
calc_gamma(revscaler * in[idx], gamma_in);
213 for(
int rr = 2; rr < last_rr - 2; rr++)
216 for(
int cc = 2 + (
FC(rr, 2, filters) & 1); cc < last_cc - 2; cc += 2)
218 float *cfa = qix[5] + rr *
LMMSE_GRP + cc;
219 const float v0 = 0.0625f * (cfa[-
w1 - 1] + cfa[-
w1 + 1] + cfa[
w1 - 1] + cfa[
w1 + 1]) + 0.25f * cfa[0];
221 float *hdiff = qix[0] + rr *
LMMSE_GRP + cc;
222 hdiff[0] = -0.25f * (cfa[ -2] + cfa[ 2]) + 0.5f * (cfa[ -1] + cfa[0] + cfa[ 1]);
223 const float Y0 = v0 + 0.5f * hdiff[0];
224 hdiff[0] = (cfa[0] > 1.75f * Y0) ?
median3f(hdiff[0], cfa[ -1], cfa[ 1]) :
limf(hdiff[0], 0.0f, 1.0f);
228 float *vdiff = qix[1] + rr *
LMMSE_GRP + cc;
229 vdiff[0] = -0.25f * (cfa[-
w2] + cfa[
w2]) + 0.5f * (cfa[-
w1] + cfa[0] + cfa[
w1]);
230 const float Y1 = v0 + 0.5f * vdiff[0];
231 vdiff[0] = (cfa[0] > 1.75f * Y1) ?
median3f(vdiff[0], cfa[-
w1], cfa[
w1]) :
limf(vdiff[0], 0.0f, 1.0f);
236 for(
int ccc = 2 + (
FC(rr, 3, filters) & 1); ccc < last_cc - 2; ccc += 2)
238 float *cfa = qix[5] + rr *
LMMSE_GRP + ccc;
239 float *hdiff = qix[0] + rr *
LMMSE_GRP + ccc;
240 float *vdiff = qix[1] + rr *
LMMSE_GRP + ccc;
241 hdiff[0] = 0.25f * (cfa[ -2] + cfa[ 2]) - 0.5f * (cfa[ -1] + cfa[0] + cfa[ 1]);
242 vdiff[0] = 0.25f * (cfa[-
w2] + cfa[
w2]) - 0.5f * (cfa[-
w1] + cfa[0] + cfa[
w1]);
243 hdiff[0] =
limf(hdiff[0], -1.0f, 0.0f) + cfa[0];
244 vdiff[0] =
limf(vdiff[0], -1.0f, 0.0f) + cfa[0];
249 for (
int rr = 4; rr < last_rr - 4; rr++)
251 for(
int cc = 4; cc < last_cc - 4; cc++)
253 float *hdiff = qix[0] + rr *
LMMSE_GRP + cc;
254 float *vdiff = qix[1] + rr *
LMMSE_GRP + cc;
255 float *hlp = qix[2] + rr *
LMMSE_GRP + cc;
256 float *vlp = qix[3] + rr *
LMMSE_GRP + cc;
257 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]);
258 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]);
262 for(
int rr = 4; rr < last_rr - 4; rr++)
264 for(
int cc = 4 + (
FC(rr, 4, filters) & 1); cc < last_cc - 4; cc += 2)
266 float *hdiff = qix[0] + rr *
LMMSE_GRP + cc;
267 float *vdiff = qix[1] + rr *
LMMSE_GRP + cc;
268 float *hlp = qix[2] + rr *
LMMSE_GRP + cc;
269 float *vlp = qix[3] + rr *
LMMSE_GRP + cc;
270 float *interp = qix[4] + rr *
LMMSE_GRP + cc;
281 float mu = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) / 9.0f;
282 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);
292 float vn = 1e-7f + sqf(p1) + sqf(p2) + sqf(p3) + sqf(p4) + sqf(p5) + sqf(p6) + sqf(p7) + sqf(p8) + sqf(p9);
293 float xh = (hdiff[0] * vx + hlp[0] * vn) / (vx + vn);
294 float vh = vx * vn / (vx + vn);
306 mu = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) / 9.0f;
307 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);
317 vn = 1e-7f + sqf(p1) + sqf(p2) + sqf(p3) + sqf(p4) + sqf(p5) + sqf(p6) + sqf(p7) + sqf(p8) + sqf(p9);
318 float xv = (vdiff[0] * vx + vlp[0] * vn) / (vx + vn);
319 float vv = vx * vn / (vx + vn);
321 interp[0] = (xh * vv + xv * vh) / (vh + vv);
326 for(
int rr = 0, row_in = rowStart -
BORDER_AROUND; rr < last_rr; rr++, row_in++)
328 for(
int cc = 0, col_in = colStart -
BORDER_AROUND; cc < last_cc; cc++, col_in++)
330 const int c =
FC(rr, cc, filters);
331 const gboolean inside = ((row_in >= 0) && (row_in <
height) && (col_in >= 0) && (col_in <
width));
332 float *colc = qix[c] + rr *
LMMSE_GRP + cc;
333 colc[0] = (inside) ? qix[5][rr *
LMMSE_GRP + cc] : 0.0f;
336 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
337 float *interp = qix[4] + rr *
LMMSE_GRP + cc;
338 col1[0] = (inside) ? colc[0] + interp[0] : 0.0f;
345 for(
int rr = 1; rr < last_rr - 1; rr++)
347 for(
int cc = 1 + (
FC(rr, 2, filters) & 1), c =
FC(rr, cc + 1, filters); cc < last_cc - 1; cc += 2)
349 float *colc = qix[c] + rr *
LMMSE_GRP + cc;
350 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
351 colc[0] = col1[0] + 0.5f * (colc[ -1] - col1[ -1] + colc[ 1] - col1[ 1]);
354 colc[0] = col1[0] + 0.5f * (colc[-
w1] - col1[-
w1] + colc[
w1] - col1[
w1]);
360 for(
int rr = 1; rr < last_rr - 1; rr++)
362 for(
int cc = 1 + (
FC(rr, 1, filters) & 1), c = 2 -
FC(rr, cc, filters); cc < last_cc - 1; cc += 2)
364 float *colc = qix[c] + rr *
LMMSE_GRP + cc;
365 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
366 colc[0] = col1[0] + 0.25f * (colc[-
w1] - col1[-
w1] + colc[ -1] - col1[ -1] + colc[ 1] - col1[ 1] + colc[
w1] - col1[
w1]);
372 const int ccmin = (tile_horizontal == 0) ? 6 : 0 ;
373 const int ccmax = last_cc - ((tile_horizontal == num_horizontal - 1) ? 6 : 0);
374 const int rrmin = (tile_vertical == 0) ? 6 : 0 ;
375 const int rrmax = last_rr - ((tile_vertical == num_vertical - 1) ? 6 : 0);
378 for(
int pass = 0; pass < medians; pass++)
382 for(
int rr = 1; rr < last_rr - 1; rr++)
384 for(
int c = 0; c < 3; c += 2)
386 const int d = c + 3 - (c == 0 ? 0 : 1);
387 for(
int cc = 1; cc < last_cc - 1; cc++)
390 float *colc = qix[c] + rr *
LMMSE_GRP + cc;
391 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
394 colc[-
w1 ] - col1[-
w1 ],
395 colc[-
w1+1] - col1[-
w1+1],
396 colc[ -1] - col1[ -1],
399 colc[
w1-1] - col1[
w1-1],
400 colc[
w1 ] - col1[
w1 ],
401 colc[
w1+1] - col1[
w1+1]);
407 for(
int rr = rrmin; rr < rrmax - 1; rr++)
409 float *col0 = qix[0] + rr *
LMMSE_GRP + ccmin;
410 float *col1 = qix[1] + rr *
LMMSE_GRP + ccmin;
411 float *col2 = qix[2] + rr *
LMMSE_GRP + ccmin;
412 float *corr3 = qix[3] + rr *
LMMSE_GRP + ccmin;
413 float *corr4 = qix[4] + rr *
LMMSE_GRP + ccmin;
414 int c0 =
FC(rr, 0, filters);
415 int c1 =
FC(rr, 1, filters);
420 const int d =
c1 + 3 - (
c1 == 0 ? 0 : 1);
423 float *corr_d = qix[
d] + rr *
LMMSE_GRP + ccmin;
424 for(cc = ccmin; cc < ccmax - 1; cc += 2)
426 col0[0] = col1[0] + corr3[0];
427 col2[0] = col1[0] + corr4[0];
435 col_c1[0] = col1[0] + corr_d[0];
436 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
448 col0[0] = col1[0] + corr3[0];
449 col2[0] = col1[0] + corr4[0];
455 const int d = c0 + 3 - (c0 == 0 ? 0 : 1);
456 float *col_c0 = qix[c0] + rr *
LMMSE_GRP + ccmin;
457 float *corr_d = qix[
d] + rr *
LMMSE_GRP + ccmin;
459 for(cc = ccmin; cc < ccmax - 1; cc += 2)
461 col_c0[0] = col1[0] + corr_d[0];
462 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
470 col0[0] = col1[0] + corr3[0];
471 col2[0] = col1[0] + corr4[0];
483 col_c0[0] = col1[0] + corr_d[0];
484 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
491 for(
int rrr = 4; rrr < last_rr - 4; rrr++)
493 for(
int ccc = 4; ccc < last_cc - 4; ccc++)
496 const int c =
FC(rrr, ccc, filters);
497 qix[c][idx] = qix[5][idx];
502 for(
int step = 0; step <
refine; step++)
505 for(
int rr = rrmin + 2; rr < rrmax - 2; rr++)
507 for(
int cc = ccmin + 2 + (
FC(rr, 2, filters) & 1), c =
FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
509 float *rgb1 = qix[1] + rr *
LMMSE_GRP + cc;
510 float *rgbc = qix[c] + rr *
LMMSE_GRP + cc;
512 const float dL = 1.0f / (1.0f + fabsf(rgbc[ -2] - rgbc[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
513 const float dR = 1.0f / (1.0f + fabsf(rgbc[ 2] - rgbc[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
514 const float dU = 1.0f / (1.0f + fabsf(rgbc[-
w2] - rgbc[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
515 const float dD = 1.0f / (1.0f + fabsf(rgbc[
w2] - rgbc[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
516 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));
520 for(
int rr = rrmin + 2; rr < rrmax - 2; rr++)
522 for(
int cc = ccmin + 2 + (
FC(rr, 3, filters) & 1), c =
FC(rr, cc + 1, filters); cc < ccmax - 2; cc += 2)
524 for(
int i = 0;
i < 2; c = 2 - c,
i++)
526 float *rgb1 = qix[1] + rr *
LMMSE_GRP + cc;
527 float *rgbc = qix[c] + rr *
LMMSE_GRP + cc;
529 const float dL = 1.0f / (1.0f + fabsf(rgb1[ -2] - rgb1[0]) + fabsf(rgbc[ 1] - rgbc[ -1]));
530 const float dR = 1.0f / (1.0f + fabsf(rgb1[ 2] - rgb1[0]) + fabsf(rgbc[ 1] - rgbc[ -1]));
531 const float dU = 1.0f / (1.0f + fabsf(rgb1[-
w2] - rgb1[0]) + fabsf(rgbc[
w1] - rgbc[-
w1]));
532 const float dD = 1.0f / (1.0f + fabsf(rgb1[
w2] - rgb1[0]) + fabsf(rgbc[
w1] - rgbc[-
w1]));
533 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));
538 for(
int rr = rrmin + 2; rr < rrmax - 2; rr++)
540 for(
int cc = ccmin + 2 + (
FC(rr, 2, filters) & 1), c = 2 -
FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
543 float *rgb1 = qix[1] + rr *
LMMSE_GRP + cc;
544 float *rgbc = qix[c] + rr *
LMMSE_GRP + cc;
547 const float dL = 1.0f / (1.0f + fabsf(rgbd[ -2] - rgbd[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
548 const float dR = 1.0f / (1.0f + fabsf(rgbd[ 2] - rgbd[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
549 const float dU = 1.0f / (1.0f + fabsf(rgbd[-
w2] - rgbd[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
550 const float dD = 1.0f / (1.0f + fabsf(rgbd[
w2] - rgbd[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
551 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));
558 const int first_vertical = rowStart + ((tile_vertical == 0) ? 0 :
LMMSE_OVERLAP);
559 const int last_vertical = rowEnd - ((tile_vertical == num_vertical - 1) ? 0 :
LMMSE_OVERLAP);
560 const int first_horizontal = colStart + ((tile_horizontal == 0) ? 0 :
LMMSE_OVERLAP);
561 const int last_horizontal = colEnd - ((tile_horizontal == num_horizontal - 1) ? 0 :
LMMSE_OVERLAP);
564 float *dest =
out + 4 * (
row *
width + first_horizontal);
566 float *col0 = qix[0] + idx;
567 float *col1 = qix[1] + idx;
568 float *col2 = qix[2] + idx;
569 for(
int col = first_horizontal; col < last_horizontal; col++, dest +=4, col0++, col1++, col2++)
571 dest[0] = scaler *
calc_gamma(col0[0], gamma_out);
572 dest[1] = scaler *
calc_gamma(col1[0], gamma_out);
573 dest[2] = scaler *
calc_gamma(col2[0], gamma_out);