130 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)
142 float h1 = expf( -1.0f / 8.0f);
143 float h2 = expf( -4.0f / 8.0f);
144 float h3 = expf( -9.0f / 8.0f);
145 float h4 = expf(-16.0f / 8.0f);
146 float hs = h0 + 2.0f * (h1 + h2 + h3 + h4);
154 const int medians = (mode < 2) ? mode : 3;
156 const int refine = (mode > 2) ? mode - 2 : 0;
159 const float revscaler = 1.0f / scaler;
171 for(
int i = 1;
i < 6;
i++)
178 #pragma omp for schedule(simd:dynamic, 6) collapse(2)
180 for(
int tile_vertical = 0; tile_vertical < num_vertical; tile_vertical++)
182 for(
int tile_horizontal = 0; tile_horizontal < num_horizontal; tile_horizontal++)
203 cfa[0] =
calc_gamma(revscaler * in[idx], gamma_in);
208 for(
int rr = 2; rr < last_rr - 2; rr++)
211 for(
int cc = 2 + (
FC(rr, 2, filters) & 1); cc < last_cc - 2; cc += 2)
213 float *cfa = qix[5] + rr *
LMMSE_GRP + cc;
214 const float v0 = 0.0625f * (cfa[-
w1 - 1] + cfa[-
w1 + 1] + cfa[
w1 - 1] + cfa[
w1 + 1]) + 0.25f * cfa[0];
216 float *hdiff = qix[0] + rr *
LMMSE_GRP + cc;
217 hdiff[0] = -0.25f * (cfa[ -2] + cfa[ 2]) + 0.5f * (cfa[ -1] + cfa[0] + cfa[ 1]);
218 const float Y0 = v0 + 0.5f * hdiff[0];
219 hdiff[0] = (cfa[0] > 1.75f * Y0) ?
median3f(hdiff[0], cfa[ -1], cfa[ 1]) :
limf(hdiff[0], 0.0f, 1.0f);
223 float *vdiff = qix[1] + rr *
LMMSE_GRP + cc;
224 vdiff[0] = -0.25f * (cfa[-
w2] + cfa[
w2]) + 0.5f * (cfa[-
w1] + cfa[0] + cfa[
w1]);
225 const float Y1 = v0 + 0.5f * vdiff[0];
226 vdiff[0] = (cfa[0] > 1.75f * Y1) ?
median3f(vdiff[0], cfa[-
w1], cfa[
w1]) :
limf(vdiff[0], 0.0f, 1.0f);
231 for(
int ccc = 2 + (
FC(rr, 3, filters) & 1); ccc < last_cc - 2; ccc += 2)
233 float *cfa = qix[5] + rr *
LMMSE_GRP + ccc;
234 float *hdiff = qix[0] + rr *
LMMSE_GRP + ccc;
235 float *vdiff = qix[1] + rr *
LMMSE_GRP + ccc;
236 hdiff[0] = 0.25f * (cfa[ -2] + cfa[ 2]) - 0.5f * (cfa[ -1] + cfa[0] + cfa[ 1]);
237 vdiff[0] = 0.25f * (cfa[-
w2] + cfa[
w2]) - 0.5f * (cfa[-
w1] + cfa[0] + cfa[
w1]);
238 hdiff[0] =
limf(hdiff[0], -1.0f, 0.0f) + cfa[0];
239 vdiff[0] =
limf(vdiff[0], -1.0f, 0.0f) + cfa[0];
244 for (
int rr = 4; rr < last_rr - 4; rr++)
246 for(
int cc = 4; cc < last_cc - 4; cc++)
248 float *hdiff = qix[0] + rr *
LMMSE_GRP + cc;
249 float *vdiff = qix[1] + rr *
LMMSE_GRP + cc;
250 float *hlp = qix[2] + rr *
LMMSE_GRP + cc;
251 float *vlp = qix[3] + rr *
LMMSE_GRP + cc;
252 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]);
253 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]);
257 for(
int rr = 4; rr < last_rr - 4; rr++)
259 for(
int cc = 4 + (
FC(rr, 4, filters) & 1); cc < last_cc - 4; cc += 2)
261 float *hdiff = qix[0] + rr *
LMMSE_GRP + cc;
262 float *vdiff = qix[1] + rr *
LMMSE_GRP + cc;
263 float *hlp = qix[2] + rr *
LMMSE_GRP + cc;
264 float *vlp = qix[3] + rr *
LMMSE_GRP + cc;
265 float *interp = qix[4] + rr *
LMMSE_GRP + cc;
276 float mu = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) / 9.0f;
277 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);
287 float vn = 1e-7f + sqf(p1) + sqf(p2) + sqf(p3) + sqf(p4) + sqf(p5) + sqf(p6) + sqf(p7) + sqf(p8) + sqf(p9);
288 float xh = (hdiff[0] * vx + hlp[0] * vn) / (vx + vn);
289 float vh = vx * vn / (vx + vn);
301 mu = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) / 9.0f;
302 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);
312 vn = 1e-7f + sqf(p1) + sqf(p2) + sqf(p3) + sqf(p4) + sqf(p5) + sqf(p6) + sqf(p7) + sqf(p8) + sqf(p9);
313 float xv = (vdiff[0] * vx + vlp[0] * vn) / (vx + vn);
314 float vv = vx * vn / (vx + vn);
316 interp[0] = (xh * vv + xv * vh) / (vh + vv);
321 for(
int rr = 0, row_in = rowStart -
BORDER_AROUND; rr < last_rr; rr++, row_in++)
323 for(
int cc = 0, col_in = colStart -
BORDER_AROUND; cc < last_cc; cc++, col_in++)
325 const int c =
FC(rr, cc, filters);
326 const gboolean inside = ((row_in >= 0) && (row_in <
height) && (col_in >= 0) && (col_in <
width));
327 float *colc = qix[c] + rr *
LMMSE_GRP + cc;
328 colc[0] = (inside) ? qix[5][rr *
LMMSE_GRP + cc] : 0.0f;
331 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
332 float *interp = qix[4] + rr *
LMMSE_GRP + cc;
333 col1[0] = (inside) ? colc[0] + interp[0] : 0.0f;
340 for(
int rr = 1; rr < last_rr - 1; rr++)
342 for(
int cc = 1 + (
FC(rr, 2, filters) & 1), c =
FC(rr, cc + 1, filters); cc < last_cc - 1; cc += 2)
344 float *colc = qix[c] + rr *
LMMSE_GRP + cc;
345 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
346 colc[0] = col1[0] + 0.5f * (colc[ -1] - col1[ -1] + colc[ 1] - col1[ 1]);
349 colc[0] = col1[0] + 0.5f * (colc[-
w1] - col1[-
w1] + colc[
w1] - col1[
w1]);
355 for(
int rr = 1; rr < last_rr - 1; rr++)
357 for(
int cc = 1 + (
FC(rr, 1, filters) & 1), c = 2 -
FC(rr, cc, filters); cc < last_cc - 1; cc += 2)
359 float *colc = qix[c] + rr *
LMMSE_GRP + cc;
360 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
361 colc[0] = col1[0] + 0.25f * (colc[-
w1] - col1[-
w1] + colc[ -1] - col1[ -1] + colc[ 1] - col1[ 1] + colc[
w1] - col1[
w1]);
367 const int ccmin = (tile_horizontal == 0) ? 6 : 0 ;
368 const int ccmax = last_cc - ((tile_horizontal == num_horizontal - 1) ? 6 : 0);
369 const int rrmin = (tile_vertical == 0) ? 6 : 0 ;
370 const int rrmax = last_rr - ((tile_vertical == num_vertical - 1) ? 6 : 0);
373 for(
int pass = 0; pass < medians; pass++)
377 for(
int rr = 1; rr < last_rr - 1; rr++)
379 for(
int c = 0; c < 3; c += 2)
381 const int d = c + 3 - (c == 0 ? 0 : 1);
382 for(
int cc = 1; cc < last_cc - 1; cc++)
385 float *colc = qix[c] + rr *
LMMSE_GRP + cc;
386 float *col1 = qix[1] + rr *
LMMSE_GRP + cc;
389 colc[-
w1 ] - col1[-
w1 ],
390 colc[-
w1+1] - col1[-
w1+1],
391 colc[ -1] - col1[ -1],
394 colc[
w1-1] - col1[
w1-1],
395 colc[
w1 ] - col1[
w1 ],
396 colc[
w1+1] - col1[
w1+1]);
402 for(
int rr = rrmin; rr < rrmax - 1; rr++)
404 float *col0 = qix[0] + rr *
LMMSE_GRP + ccmin;
405 float *col1 = qix[1] + rr *
LMMSE_GRP + ccmin;
406 float *col2 = qix[2] + rr *
LMMSE_GRP + ccmin;
407 float *corr3 = qix[3] + rr *
LMMSE_GRP + ccmin;
408 float *corr4 = qix[4] + rr *
LMMSE_GRP + ccmin;
409 int c0 =
FC(rr, 0, filters);
410 int c1 =
FC(rr, 1, filters);
415 const int d =
c1 + 3 - (
c1 == 0 ? 0 : 1);
418 float *corr_d = qix[
d] + rr *
LMMSE_GRP + ccmin;
419 for(cc = ccmin; cc < ccmax - 1; cc += 2)
421 col0[0] = col1[0] + corr3[0];
422 col2[0] = col1[0] + corr4[0];
430 col_c1[0] = col1[0] + corr_d[0];
431 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
443 col0[0] = col1[0] + corr3[0];
444 col2[0] = col1[0] + corr4[0];
450 const int d = c0 + 3 - (c0 == 0 ? 0 : 1);
451 float *col_c0 = qix[c0] + rr *
LMMSE_GRP + ccmin;
452 float *corr_d = qix[
d] + rr *
LMMSE_GRP + ccmin;
454 for(cc = ccmin; cc < ccmax - 1; cc += 2)
456 col_c0[0] = col1[0] + corr_d[0];
457 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
465 col0[0] = col1[0] + corr3[0];
466 col2[0] = col1[0] + corr4[0];
478 col_c0[0] = col1[0] + corr_d[0];
479 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
486 for(
int rrr = 4; rrr < last_rr - 4; rrr++)
488 for(
int ccc = 4; ccc < last_cc - 4; ccc++)
491 const int c =
FC(rrr, ccc, filters);
492 qix[c][idx] = qix[5][idx];
497 for(
int step = 0; step <
refine; step++)
500 for(
int rr = rrmin + 2; rr < rrmax - 2; rr++)
502 for(
int cc = ccmin + 2 + (
FC(rr, 2, filters) & 1), c =
FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
504 float *rgb1 = qix[1] + rr *
LMMSE_GRP + cc;
505 float *rgbc = qix[c] + rr *
LMMSE_GRP + cc;
507 const float dL = 1.0f / (1.0f + fabsf(rgbc[ -2] - rgbc[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
508 const float dR = 1.0f / (1.0f + fabsf(rgbc[ 2] - rgbc[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
509 const float dU = 1.0f / (1.0f + fabsf(rgbc[-
w2] - rgbc[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
510 const float dD = 1.0f / (1.0f + fabsf(rgbc[
w2] - rgbc[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
511 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));
515 for(
int rr = rrmin + 2; rr < rrmax - 2; rr++)
517 for(
int cc = ccmin + 2 + (
FC(rr, 3, filters) & 1), c =
FC(rr, cc + 1, filters); cc < ccmax - 2; cc += 2)
519 for(
int i = 0;
i < 2; c = 2 - c,
i++)
521 float *rgb1 = qix[1] + rr *
LMMSE_GRP + cc;
522 float *rgbc = qix[c] + rr *
LMMSE_GRP + cc;
524 const float dL = 1.0f / (1.0f + fabsf(rgb1[ -2] - rgb1[0]) + fabsf(rgbc[ 1] - rgbc[ -1]));
525 const float dR = 1.0f / (1.0f + fabsf(rgb1[ 2] - rgb1[0]) + fabsf(rgbc[ 1] - rgbc[ -1]));
526 const float dU = 1.0f / (1.0f + fabsf(rgb1[-
w2] - rgb1[0]) + fabsf(rgbc[
w1] - rgbc[-
w1]));
527 const float dD = 1.0f / (1.0f + fabsf(rgb1[
w2] - rgb1[0]) + fabsf(rgbc[
w1] - rgbc[-
w1]));
528 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));
533 for(
int rr = rrmin + 2; rr < rrmax - 2; rr++)
535 for(
int cc = ccmin + 2 + (
FC(rr, 2, filters) & 1), c = 2 -
FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
538 float *rgb1 = qix[1] + rr *
LMMSE_GRP + cc;
539 float *rgbc = qix[c] + rr *
LMMSE_GRP + cc;
542 const float dL = 1.0f / (1.0f + fabsf(rgbd[ -2] - rgbd[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
543 const float dR = 1.0f / (1.0f + fabsf(rgbd[ 2] - rgbd[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
544 const float dU = 1.0f / (1.0f + fabsf(rgbd[-
w2] - rgbd[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
545 const float dD = 1.0f / (1.0f + fabsf(rgbd[
w2] - rgbd[0]) + fabsf(rgb1[
w1] - rgb1[-
w1]));
546 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));
553 const int first_vertical = rowStart + ((tile_vertical == 0) ? 0 :
LMMSE_OVERLAP);
554 const int last_vertical = rowEnd - ((tile_vertical == num_vertical - 1) ? 0 :
LMMSE_OVERLAP);
555 const int first_horizontal = colStart + ((tile_horizontal == 0) ? 0 :
LMMSE_OVERLAP);
556 const int last_horizontal = colEnd - ((tile_horizontal == num_horizontal - 1) ? 0 :
LMMSE_OVERLAP);
559 float *dest =
out + 4 * (
row *
width + first_horizontal);
561 float *col0 = qix[0] + idx;
562 float *col1 = qix[1] + idx;
563 float *col2 = qix[2] + idx;
564 for(
int col = first_horizontal; col < last_horizontal; col++, dest +=4, col0++, col1++, col2++)
566 dest[0] = scaler *
calc_gamma(col0[0], gamma_out);
567 dest[1] = scaler *
calc_gamma(col1[0], gamma_out);
568 dest[2] = scaler *
calc_gamma(col2[0], gamma_out);