Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
lmmse.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2021 Hanno Schwalm.
4 Copyright (C) 2021 luzpaz.
5 Copyright (C) 2021 Pascal Obry.
6 Copyright (C) 2022 Martin Bařinka.
7 Copyright (C) 2023, 2026 Aurélien PIERRE.
8
9 darktable is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 darktable is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with darktable. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23/*
24 The lmmse code base used for the darktable port has been taken from rawtherapee derived librtprocess.
25 Adapt for dt and tiling - hanno schwalm 06/2021
26
27 LSMME demosaicing algorithm
28 L. Zhang and X. Wu,
29 Color demozaicing via directional Linear Minimum Mean Square-error Estimation,
30 IEEE Trans. on Image Processing, vol. 14, pp. 2167-2178, Dec. 2005.
31
32 Adapted to RawTherapee by Jacques Desmis 3/2013
33 Improved speed and reduced memory consumption by Ingo Weyrich 2/2015
34*/
35
36/*
37 Refinement based on EECI demosaicing algorithm by L. Chang and Y.P. Tan
38 Paul Lee
39 Adapted for RawTherapee - Jacques Desmis 04/2013
40*/
41
42/* Why tiling?
43 The internal tiling vastly reduces memory footprint and allows data processing to be done mostly
44 with in-cache data thus increasing performance.
45
46 The performance has been tested on a E-2288G for 45mpix images, tiling improves performance > 2-fold.
47 times in sec: basic (0.5->0.15), median (0.6->0.18), 3xmedian (0.8->0.22), 3xmedian + 2x refine (1.2->0.30)
48 The default is now 2 times slower than RCD and 2 times faster than AMaZE
49*/
50
51#ifndef LMMSE_GRP
52 #define LMMSE_GRP 136
53#endif
54
55#ifdef __GNUC__
56 #pragma GCC push_options
57 #pragma GCC optimize ("fast-math", "fp-contract=fast", "finite-math-only", "no-math-errno")
58#endif
59
60#define LMMSE_OVERLAP 8
61#define BORDER_AROUND 4
62#define LMMSE_TILESIZE (LMMSE_GRP - 2 * BORDER_AROUND)
63#define LMMSE_TILEVALID (LMMSE_TILESIZE - 2 * LMMSE_OVERLAP)
64#define w1 (LMMSE_GRP)
65#define w2 (LMMSE_GRP * 2)
66#define w3 (LMMSE_GRP * 3)
67#define w4 (LMMSE_GRP * 4)
68
69static INLINE float limf(float x, float min, float max)
70{
71 return fmaxf(min, fminf(x, max));
72}
73
74static INLINE float median3f(float x0, float x1, float x2)
75{
76 return fmaxf(fminf(x0,x1), fminf(x2, fmaxf(x0,x1)));
77}
78
79static INLINE float median9f(float a0, float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
80{
81 float tmp;
82 tmp = fminf(a1, a2);
83 a2 = fmaxf(a1, a2);
84 a1 = tmp;
85 tmp = fminf(a4, a5);
86 a5 = fmaxf(a4, a5);
87 a4 = tmp;
88 tmp = fminf(a7, a8);
89 a8 = fmaxf(a7, a8);
90 a7 = tmp;
91 tmp = fminf(a0, a1);
92 a1 = fmaxf(a0, a1);
93 a0 = tmp;
94 tmp = fminf(a3, a4);
95 a4 = fmaxf(a3, a4);
96 a3 = tmp;
97 tmp = fminf(a6, a7);
98 a7 = fmaxf(a6, a7);
99 a6 = tmp;
100 tmp = fminf(a1, a2);
101 a2 = fmaxf(a1, a2);
102 a1 = tmp;
103 tmp = fminf(a4, a5);
104 a5 = fminf(a4, a5);
105 a4 = tmp;
106 tmp = fminf(a7, a8);
107 a8 = fmaxf(a7, a8);
108 a3 = fmaxf(a0, a3);
109 a5 = fminf(a5, a8);
110 a7 = fmaxf(a4, tmp);
111 tmp = fminf(a4, tmp);
112 a6 = fmaxf(a3, a6);
113 a4 = fmaxf(a1, tmp);
114 a2 = fminf(a2, a5);
115 a4 = fminf(a4, a7);
116 tmp = fminf(a4, a2);
117 a2 = fmaxf(a4, a2);
118 a4 = fmaxf(a6, tmp);
119 return fminf(a4, a2);
120}
121
122static INLINE float calc_gamma(float val, float *table)
123{
124 const float index = val * 65535.0f;
125 if(index < 0.0f) return 0.0f;
126 if(index > 65534.99f) return 1.0f;
127 const int idx = (int)index;
128
129 const float diff = index - (float)idx;
130 const float p1 = table[idx];
131 const float p2 = table[idx+1] - p1;
132 return (p1 + p2 * diff);
133}
134static void lmmse_demosaic(const dt_dev_pixelpipe_iop_t *piece, float *const restrict out, const float *const restrict in, dt_iop_roi_t *const roi_out,
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)
136{
137 const int width = roi_in->width;
138 const int height = roi_in->height;
139
140 if((width < 16) || (height < 16))
141 {
142 dt_control_log(_("[lmmse_demosaic] too small area"));
143 return;
144 }
145
146 float h0 = 1.0f;
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);
152 h0 /= hs;
153 h1 /= hs;
154 h2 /= hs;
155 h3 /= hs;
156 h4 /= hs;
157
158 // median filter iterations
159 const int medians = (mode < 2) ? mode : 3;
160 // refinement steps
161 const int refine = (mode > 2) ? mode - 2 : 0;
162
163 const float scaler = fmaxf(piece->dsc_in.processed_maximum[0], fmaxf(piece->dsc_in.processed_maximum[1], piece->dsc_in.processed_maximum[2]));
164 const float revscaler = 1.0f / scaler;
165
166 const int num_vertical = 1 + (height - 2 * LMMSE_OVERLAP -1) / LMMSE_TILEVALID;
167 const int num_horizontal = 1 + (width - 2 * LMMSE_OVERLAP -1) / LMMSE_TILEVALID;
168#ifdef _OPENMP
169 #pragma omp parallel
170#endif
171 {
172 float *qix[6];
174
175 qix[0] = buffer;
176 for(int i = 1; i < 6; i++)
177 {
178 qix[i] = qix[i - 1] + LMMSE_GRP * LMMSE_GRP;
179 }
180 memset_zero(buffer, sizeof(float) * LMMSE_GRP * LMMSE_GRP * 6);
181
182#ifdef _OPENMP
183 #pragma omp for schedule(simd:dynamic, 6) collapse(2)
184#endif
185 for(int tile_vertical = 0; tile_vertical < num_vertical; tile_vertical++)
186 {
187 for(int tile_horizontal = 0; tile_horizontal < num_horizontal; tile_horizontal++)
188 {
189 const int rowStart = tile_vertical * LMMSE_TILEVALID;
190 const int rowEnd = MIN(rowStart + LMMSE_TILESIZE, height);
191
192 const int colStart = tile_horizontal * LMMSE_TILEVALID;
193 const int colEnd = MIN(colStart + LMMSE_TILESIZE, width);
194
195 const int tileRows = MIN(rowEnd - rowStart, LMMSE_TILESIZE);
196 const int tileCols = MIN(colEnd - colStart, LMMSE_TILESIZE);
197
198 // index limit; normally is LMMSE_GRP but maybe missing bottom lines or right columns for outermost tile
199 const int last_rr = tileRows + 2 * BORDER_AROUND;
200 const int last_cc = tileCols + 2 * BORDER_AROUND;
201
202 for(int rrr = BORDER_AROUND, row = rowStart; rrr < tileRows + BORDER_AROUND; rrr++, row++)
203 {
204 float *cfa = qix[5] + rrr * LMMSE_GRP + BORDER_AROUND;
205 int idx = row * width + colStart;
206 for(int ccc = BORDER_AROUND, col = colStart; ccc < tileCols + BORDER_AROUND; ccc++, col++, cfa++, idx++)
207 {
208 cfa[0] = calc_gamma(revscaler * in[idx], gamma_in);
209 }
210 }
211
212 // G-R(B)
213 for(int rr = 2; rr < last_rr - 2; rr++)
214 {
215 // G-R(B) at R(B) location
216 for(int cc = 2 + (FC(rr, 2, filters) & 1); cc < last_cc - 2; cc += 2)
217 {
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];
220 // horizontal
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);
225 hdiff[0] -= cfa[0];
226
227 // vertical
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);
232 vdiff[0] -= cfa[0];
233 }
234
235 // G-R(B) at G location
236 for(int ccc = 2 + (FC(rr, 3, filters) & 1); ccc < last_cc - 2; ccc += 2)
237 {
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];
245 }
246 }
247
248 // apply low pass filter on differential colors
249 for (int rr = 4; rr < last_rr - 4; rr++)
250 {
251 for(int cc = 4; cc < last_cc - 4; cc++)
252 {
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]);
259 }
260 }
261
262 for(int rr = 4; rr < last_rr - 4; rr++)
263 {
264 for(int cc = 4 + (FC(rr, 4, filters) & 1); cc < last_cc - 4; cc += 2)
265 {
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;
271 // horizontal
272 float p1 = hlp[-4];
273 float p2 = hlp[-3];
274 float p3 = hlp[-2];
275 float p4 = hlp[-1];
276 float p5 = hlp[ 0];
277 float p6 = hlp[ 1];
278 float p7 = hlp[ 2];
279 float p8 = hlp[ 3];
280 float p9 = hlp[ 4];
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);
283 p1 -= hdiff[-4];
284 p2 -= hdiff[-3];
285 p3 -= hdiff[-2];
286 p4 -= hdiff[-1];
287 p5 -= hdiff[ 0];
288 p6 -= hdiff[ 1];
289 p7 -= hdiff[ 2];
290 p8 -= hdiff[ 3];
291 p9 -= hdiff[ 4];
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);
295
296 // vertical
297 p1 = vlp[-w4];
298 p2 = vlp[-w3];
299 p3 = vlp[-w2];
300 p4 = vlp[-w1];
301 p5 = vlp[ 0];
302 p6 = vlp[ w1];
303 p7 = vlp[ w2];
304 p8 = vlp[ w3];
305 p9 = vlp[ w4];
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);
308 p1 -= vdiff[-w4];
309 p2 -= vdiff[-w3];
310 p3 -= vdiff[-w2];
311 p4 -= vdiff[-w1];
312 p5 -= vdiff[ 0];
313 p6 -= vdiff[ w1];
314 p7 -= vdiff[ w2];
315 p8 -= vdiff[ w3];
316 p9 -= vdiff[ w4];
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);
320 // interpolated G-R(B)
321 interp[0] = (xh * vv + xv * vh) / (vh + vv);
322 }
323 }
324
325 // copy CFA values
326 for(int rr = 0, row_in = rowStart - BORDER_AROUND; rr < last_rr; rr++, row_in++)
327 {
328 for(int cc = 0, col_in = colStart - BORDER_AROUND; cc < last_cc; cc++, col_in++)
329 {
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;
334 if(c != 1)
335 {
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;
339 }
340 }
341 }
342
343 // bilinear interpolation for R/B
344 // interpolate R/B at G location
345 for(int rr = 1; rr < last_rr - 1; rr++)
346 {
347 for(int cc = 1 + (FC(rr, 2, filters) & 1), c = FC(rr, cc + 1, filters); cc < last_cc - 1; cc += 2)
348 {
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]);
352 c = 2 - c;
353 colc = qix[c] + rr * LMMSE_GRP + cc;
354 colc[0] = col1[0] + 0.5f * (colc[-w1] - col1[-w1] + colc[w1] - col1[w1]);
355 c = 2 - c;
356 }
357 }
358
359 // interpolate R/B at B/R location
360 for(int rr = 1; rr < last_rr - 1; rr++)
361 {
362 for(int cc = 1 + (FC(rr, 1, filters) & 1), c = 2 - FC(rr, cc, filters); cc < last_cc - 1; cc += 2)
363 {
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]);
367 }
368 }
369
370 // for the median and refine corrections we need to specify other loop bounds
371 // for inner vs outer tiles
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);
376
377 // median filter/
378 for(int pass = 0; pass < medians; pass++)
379 {
380 // Apply 3x3 median filter
381 // Compute median(R-G) and median(B-G)
382 for(int rr = 1; rr < last_rr - 1; rr++)
383 {
384 for(int c = 0; c < 3; c += 2)
385 {
386 const int d = c + 3 - (c == 0 ? 0 : 1);
387 for(int cc = 1; cc < last_cc - 1; cc++)
388 {
389 float *corr = qix[d] + rr * LMMSE_GRP + cc;
390 float *colc = qix[c] + rr * LMMSE_GRP + cc;
391 float *col1 = qix[1] + rr * LMMSE_GRP + cc;
392 // Assign 3x3 differential color values
393 corr[0] = median9f(colc[-w1-1] - col1[-w1-1],
394 colc[-w1 ] - col1[-w1 ],
395 colc[-w1+1] - col1[-w1+1],
396 colc[ -1] - col1[ -1],
397 colc[ 0] - col1[ 0],
398 colc[ 1] - col1[ 1],
399 colc[ w1-1] - col1[ w1-1],
400 colc[ w1 ] - col1[ w1 ],
401 colc[ w1+1] - col1[ w1+1]);
402 }
403 }
404 }
405
406 // red/blue at GREEN pixel locations & red/blue and green at BLUE/RED pixel locations
407 for(int rr = rrmin; rr < rrmax - 1; rr++)
408 {
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);
416
417 if(c0 == 1)
418 {
419 c1 = 2 - c1;
420 const int d = c1 + 3 - (c1 == 0 ? 0 : 1);
421 int cc;
422 float *col_c1 = qix[c1] + rr * LMMSE_GRP + ccmin;
423 float *corr_d = qix[d] + rr * LMMSE_GRP + ccmin;
424 for(cc = ccmin; cc < ccmax - 1; cc += 2)
425 {
426 col0[0] = col1[0] + corr3[0];
427 col2[0] = col1[0] + corr4[0];
428 col0++;
429 col1++;
430 col2++;
431 corr3++;
432 corr4++;
433 col_c1++;
434 corr_d++;
435 col_c1[0] = col1[0] + corr_d[0];
436 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
437 col0++;
438 col1++;
439 col2++;
440 corr3++;
441 corr4++;
442 col_c1++;
443 corr_d++;
444 }
445
446 if(cc < ccmax)
447 { // remaining pixel, only if width is odd
448 col0[0] = col1[0] + corr3[0];
449 col2[0] = col1[0] + corr4[0];
450 }
451 }
452 else
453 {
454 c0 = 2 - c0;
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;
458 int cc;
459 for(cc = ccmin; cc < ccmax - 1; cc += 2)
460 {
461 col_c0[0] = col1[0] + corr_d[0];
462 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
463 col0++;
464 col1++;
465 col2++;
466 corr3++;
467 corr4++;
468 col_c0++;
469 corr_d++;
470 col0[0] = col1[0] + corr3[0];
471 col2[0] = col1[0] + corr4[0];
472 col0++;
473 col1++;
474 col2++;
475 corr3++;
476 corr4++;
477 col_c0++;
478 corr_d++;
479 }
480
481 if(cc < ccmax)
482 { // remaining pixel, only if width is odd
483 col_c0[0] = col1[0] + corr_d[0];
484 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
485 }
486 }
487 }
488 }
489
490 // we fill the non-approximated color channels from gamma corrected cfa data
491 for(int rrr = 4; rrr < last_rr - 4; rrr++)
492 {
493 for(int ccc = 4; ccc < last_cc - 4; ccc++)
494 {
495 const int idx = rrr * LMMSE_GRP + ccc;
496 const int c = FC(rrr, ccc, filters);
497 qix[c][idx] = qix[5][idx];
498 }
499 }
500
501 // As we have the color channels fully available we can do the refinements here in tiled code
502 for(int step = 0; step < refine; step++)
503 {
504 // Reinforce interpolated green pixels on RED/BLUE pixel locations
505 for(int rr = rrmin + 2; rr < rrmax - 2; rr++)
506 {
507 for(int cc = ccmin + 2 + (FC(rr, 2, filters) & 1), c = FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
508 {
509 float *rgb1 = qix[1] + rr * LMMSE_GRP + cc;
510 float *rgbc = qix[c] + rr * LMMSE_GRP + cc;
511
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));
517 }
518 }
519 // Reinforce interpolated red/blue pixels on GREEN pixel locations
520 for(int rr = rrmin + 2; rr < rrmax - 2; rr++)
521 {
522 for(int cc = ccmin + 2 + (FC(rr, 3, filters) & 1), c = FC(rr, cc + 1, filters); cc < ccmax - 2; cc += 2)
523 {
524 for(int i = 0; i < 2; c = 2 - c, i++)
525 {
526 float *rgb1 = qix[1] + rr * LMMSE_GRP + cc;
527 float *rgbc = qix[c] + rr * LMMSE_GRP + cc;
528
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));
534 }
535 }
536 }
537 // Reinforce integrated red/blue pixels on BLUE/RED pixel locations
538 for(int rr = rrmin + 2; rr < rrmax - 2; rr++)
539 {
540 for(int cc = ccmin + 2 + (FC(rr, 2, filters) & 1), c = 2 - FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
541 {
542 const int d = 2 - c;
543 float *rgb1 = qix[1] + rr * LMMSE_GRP + cc;
544 float *rgbc = qix[c] + rr * LMMSE_GRP + cc;
545 float *rgbd = qix[d] + rr * LMMSE_GRP + cc;
546
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));
552 }
553 }
554 }
555
556 // write result to out
557 // For the outermost tiles in all directions we also write the otherwise overlapped area
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);
562 for(int row = first_vertical, rr = row - rowStart + BORDER_AROUND; row < last_vertical; row++, rr++)
563 {
564 float *dest = out + 4 * (row * width + first_horizontal);
565 const int idx = rr * LMMSE_GRP + first_horizontal - colStart + BORDER_AROUND;
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++)
570 {
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);
574 dest[3] = 0.0f;
575 }
576 }
577 }
578 }
580 }
581}
582
583// revert specific aggressive optimizing
584#ifdef __GNUC__
585 #pragma GCC pop_options
586#endif
587
588#undef LMMSE_TILESIZE
589#undef LMMSE_OVERLAP
590#undef BORDER_AROUND
591#undef LMMSE_TILEVALID
592#undef w1
593#undef w2
594#undef w3
595#undef w4
596
597// clang-format off
598// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
599// vim: shiftwidth=2 expandtab tabstop=2 cindent
600// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
601// clang-format on
static int refine(struct point *reg, int *reg_size, image_double modgrad, double reg_angle, double prec, double p, struct rect *rec, image_char used, image_double angles, double density_th)
Definition ashift_lsd.c:2006
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
#define INLINE
Definition cacorrect.c:167
const float i
Definition colorspaces_inline_conversions.h:440
const float d
Definition colorspaces_inline_conversions.h:680
static const float const float const float min
Definition colorspaces_inline_conversions.h:438
const float max
Definition colorspaces_inline_conversions.h:490
const dt_colormatrix_t dt_aligned_pixel_t out
Definition colorspaces_inline_conversions.h:42
static const int row
Definition colorspaces_inline_conversions.h:35
void dt_control_log(const char *msg,...)
Definition control.c:530
static void memset_zero(void *const buffer, size_t size)
Set the memory buffer to zero as a pack of unsigned char.
Definition darktable.h:881
#define dt_pixelpipe_cache_alloc_align_float_cache(pixels, id)
Definition darktable.h:446
#define dt_pixelpipe_cache_free_align(mem)
Definition darktable.h:452
static int FC(const int row, const int col, const unsigned int filters)
Definition data/kernels/common.h:47
static const float x
Definition iop_profile.h:235
static INLINE float calc_gamma(float val, float *table)
Definition lmmse.c:122
static INLINE float median3f(float x0, float x1, float x2)
Definition lmmse.c:74
#define w2
Definition lmmse.c:65
#define LMMSE_TILEVALID
Definition lmmse.c:63
#define BORDER_AROUND
Definition lmmse.c:61
#define LMMSE_OVERLAP
Definition lmmse.c:60
#define LMMSE_GRP
Definition lmmse.c:52
static INLINE float median9f(float a0, float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
Definition lmmse.c:79
#define w1
Definition lmmse.c:64
#define w4
Definition lmmse.c:67
static void lmmse_demosaic(const dt_dev_pixelpipe_iop_t *piece, float *const restrict out, const float *const restrict in, dt_iop_roi_t *const roi_out, 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)
Definition lmmse.c:134
#define w3
Definition lmmse.c:66
static INLINE float limf(float x, float min, float max)
Definition lmmse.c:69
#define LMMSE_TILESIZE
Definition lmmse.c:62
return noise *sigma mu
Definition src/develop/noise_generator.h:89
Definition pixelpipe_hb.h:95
dt_iop_buffer_dsc_t dsc_in
Definition pixelpipe_hb.h:141
dt_aligned_pixel_t processed_maximum
Definition develop/format.h:77
Region of interest passed through the pixelpipe.
Definition imageop.h:72
int width
Definition imageop.h:73
int height
Definition imageop.h:73
#define c1
Definition colorspaces_inline_conversions.h:795
#define MIN(a, b)
Definition thinplate.c:32