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 darktable developers.
4
5 darktable is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 darktable is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with darktable. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/*
20 The lmmse code base used for the darktable port has been taken from rawtherapee derived librtprocess.
21 Adapt for dt and tiling - hanno schwalm 06/2021
22
23 LSMME demosaicing algorithm
24 L. Zhang and X. Wu,
25 Color demozaicing via directional Linear Minimum Mean Square-error Estimation,
26 IEEE Trans. on Image Processing, vol. 14, pp. 2167-2178, Dec. 2005.
27
28 Adapted to RawTherapee by Jacques Desmis 3/2013
29 Improved speed and reduced memory consumption by Ingo Weyrich 2/2015
30*/
31
32/*
33 Refinement based on EECI demosaicing algorithm by L. Chang and Y.P. Tan
34 Paul Lee
35 Adapted for RawTherapee - Jacques Desmis 04/2013
36*/
37
38/* Why tiling?
39 The internal tiling vastly reduces memory footprint and allows data processing to be done mostly
40 with in-cache data thus increasing performance.
41
42 The performance has been tested on a E-2288G for 45mpix images, tiling improves performance > 2-fold.
43 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)
44 The default is now 2 times slower than RCD and 2 times faster than AMaZE
45*/
46
47#ifndef LMMSE_GRP
48 #define LMMSE_GRP 136
49#endif
50
51#ifdef __GNUC__
52 #pragma GCC push_options
53 #pragma GCC optimize ("fast-math", "fp-contract=fast", "finite-math-only", "no-math-errno")
54#endif
55
56#define LMMSE_OVERLAP 8
57#define BORDER_AROUND 4
58#define LMMSE_TILESIZE (LMMSE_GRP - 2 * BORDER_AROUND)
59#define LMMSE_TILEVALID (LMMSE_TILESIZE - 2 * LMMSE_OVERLAP)
60#define w1 (LMMSE_GRP)
61#define w2 (LMMSE_GRP * 2)
62#define w3 (LMMSE_GRP * 3)
63#define w4 (LMMSE_GRP * 4)
64
65static INLINE float limf(float x, float min, float max)
66{
67 return fmaxf(min, fminf(x, max));
68}
69
70static INLINE float median3f(float x0, float x1, float x2)
71{
72 return fmaxf(fminf(x0,x1), fminf(x2, fmaxf(x0,x1)));
73}
74
75static INLINE float median9f(float a0, float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
76{
77 float tmp;
78 tmp = fminf(a1, a2);
79 a2 = fmaxf(a1, a2);
80 a1 = tmp;
81 tmp = fminf(a4, a5);
82 a5 = fmaxf(a4, a5);
83 a4 = tmp;
84 tmp = fminf(a7, a8);
85 a8 = fmaxf(a7, a8);
86 a7 = tmp;
87 tmp = fminf(a0, a1);
88 a1 = fmaxf(a0, a1);
89 a0 = tmp;
90 tmp = fminf(a3, a4);
91 a4 = fmaxf(a3, a4);
92 a3 = tmp;
93 tmp = fminf(a6, a7);
94 a7 = fmaxf(a6, a7);
95 a6 = tmp;
96 tmp = fminf(a1, a2);
97 a2 = fmaxf(a1, a2);
98 a1 = tmp;
99 tmp = fminf(a4, a5);
100 a5 = fminf(a4, a5);
101 a4 = tmp;
102 tmp = fminf(a7, a8);
103 a8 = fmaxf(a7, a8);
104 a3 = fmaxf(a0, a3);
105 a5 = fminf(a5, a8);
106 a7 = fmaxf(a4, tmp);
107 tmp = fminf(a4, tmp);
108 a6 = fmaxf(a3, a6);
109 a4 = fmaxf(a1, tmp);
110 a2 = fminf(a2, a5);
111 a4 = fminf(a4, a7);
112 tmp = fminf(a4, a2);
113 a2 = fmaxf(a4, a2);
114 a4 = fmaxf(a6, tmp);
115 return fminf(a4, a2);
116}
117
118static INLINE float calc_gamma(float val, float *table)
119{
120 const float index = val * 65535.0f;
121 if(index < 0.0f) return 0.0f;
122 if(index > 65534.99f) return 1.0f;
123 const int idx = (int)index;
124
125 const float diff = index - (float)idx;
126 const float p1 = table[idx];
127 const float p2 = table[idx+1] - p1;
128 return (p1 + p2 * diff);
129}
130
131#ifdef _OPENMP
132 #pragma omp declare simd aligned(in, out, gamma_in, gamma_out)
133#endif
134static void lmmse_demosaic(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->pipe->dsc.processed_maximum[0], fmaxf(piece->pipe->dsc.processed_maximum[1], piece->pipe->dsc.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 dt_omp_firstprivate(width, height, out, in, scaler, revscaler, filters)
171#endif
172 {
173 float *qix[6];
174 float *buffer = dt_alloc_align_float(LMMSE_GRP * LMMSE_GRP * 6);
175
176 qix[0] = buffer;
177 for(int i = 1; i < 6; i++)
178 {
179 qix[i] = qix[i - 1] + LMMSE_GRP * LMMSE_GRP;
180 }
181 memset_zero(buffer, sizeof(float) * LMMSE_GRP * LMMSE_GRP * 6);
182
183#ifdef _OPENMP
184 #pragma omp for schedule(simd:dynamic, 6) collapse(2)
185#endif
186 for(int tile_vertical = 0; tile_vertical < num_vertical; tile_vertical++)
187 {
188 for(int tile_horizontal = 0; tile_horizontal < num_horizontal; tile_horizontal++)
189 {
190 const int rowStart = tile_vertical * LMMSE_TILEVALID;
191 const int rowEnd = MIN(rowStart + LMMSE_TILESIZE, height);
192
193 const int colStart = tile_horizontal * LMMSE_TILEVALID;
194 const int colEnd = MIN(colStart + LMMSE_TILESIZE, width);
195
196 const int tileRows = MIN(rowEnd - rowStart, LMMSE_TILESIZE);
197 const int tileCols = MIN(colEnd - colStart, LMMSE_TILESIZE);
198
199 // index limit; normally is LMMSE_GRP but maybe missing bottom lines or right columns for outermost tile
200 const int last_rr = tileRows + 2 * BORDER_AROUND;
201 const int last_cc = tileCols + 2 * BORDER_AROUND;
202
203 for(int rrr = BORDER_AROUND, row = rowStart; rrr < tileRows + BORDER_AROUND; rrr++, row++)
204 {
205 float *cfa = qix[5] + rrr * LMMSE_GRP + BORDER_AROUND;
206 int idx = row * width + colStart;
207 for(int ccc = BORDER_AROUND, col = colStart; ccc < tileCols + BORDER_AROUND; ccc++, col++, cfa++, idx++)
208 {
209 cfa[0] = calc_gamma(revscaler * in[idx], gamma_in);
210 }
211 }
212
213 // G-R(B)
214 for(int rr = 2; rr < last_rr - 2; rr++)
215 {
216 // G-R(B) at R(B) location
217 for(int cc = 2 + (FC(rr, 2, filters) & 1); cc < last_cc - 2; cc += 2)
218 {
219 float *cfa = qix[5] + rr * LMMSE_GRP + cc;
220 const float v0 = 0.0625f * (cfa[-w1 - 1] + cfa[-w1 + 1] + cfa[w1 - 1] + cfa[w1 + 1]) + 0.25f * cfa[0];
221 // horizontal
222 float *hdiff = qix[0] + rr * LMMSE_GRP + cc;
223 hdiff[0] = -0.25f * (cfa[ -2] + cfa[ 2]) + 0.5f * (cfa[ -1] + cfa[0] + cfa[ 1]);
224 const float Y0 = v0 + 0.5f * hdiff[0];
225 hdiff[0] = (cfa[0] > 1.75f * Y0) ? median3f(hdiff[0], cfa[ -1], cfa[ 1]) : limf(hdiff[0], 0.0f, 1.0f);
226 hdiff[0] -= cfa[0];
227
228 // vertical
229 float *vdiff = qix[1] + rr * LMMSE_GRP + cc;
230 vdiff[0] = -0.25f * (cfa[-w2] + cfa[w2]) + 0.5f * (cfa[-w1] + cfa[0] + cfa[w1]);
231 const float Y1 = v0 + 0.5f * vdiff[0];
232 vdiff[0] = (cfa[0] > 1.75f * Y1) ? median3f(vdiff[0], cfa[-w1], cfa[w1]) : limf(vdiff[0], 0.0f, 1.0f);
233 vdiff[0] -= cfa[0];
234 }
235
236 // G-R(B) at G location
237 for(int ccc = 2 + (FC(rr, 3, filters) & 1); ccc < last_cc - 2; ccc += 2)
238 {
239 float *cfa = qix[5] + rr * LMMSE_GRP + ccc;
240 float *hdiff = qix[0] + rr * LMMSE_GRP + ccc;
241 float *vdiff = qix[1] + rr * LMMSE_GRP + ccc;
242 hdiff[0] = 0.25f * (cfa[ -2] + cfa[ 2]) - 0.5f * (cfa[ -1] + cfa[0] + cfa[ 1]);
243 vdiff[0] = 0.25f * (cfa[-w2] + cfa[w2]) - 0.5f * (cfa[-w1] + cfa[0] + cfa[w1]);
244 hdiff[0] = limf(hdiff[0], -1.0f, 0.0f) + cfa[0];
245 vdiff[0] = limf(vdiff[0], -1.0f, 0.0f) + cfa[0];
246 }
247 }
248
249 // apply low pass filter on differential colors
250 for (int rr = 4; rr < last_rr - 4; rr++)
251 {
252 for(int cc = 4; cc < last_cc - 4; cc++)
253 {
254 float *hdiff = qix[0] + rr * LMMSE_GRP + cc;
255 float *vdiff = qix[1] + rr * LMMSE_GRP + cc;
256 float *hlp = qix[2] + rr * LMMSE_GRP + cc;
257 float *vlp = qix[3] + rr * LMMSE_GRP + cc;
258 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]);
259 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]);
260 }
261 }
262
263 for(int rr = 4; rr < last_rr - 4; rr++)
264 {
265 for(int cc = 4 + (FC(rr, 4, filters) & 1); cc < last_cc - 4; cc += 2)
266 {
267 float *hdiff = qix[0] + rr * LMMSE_GRP + cc;
268 float *vdiff = qix[1] + rr * LMMSE_GRP + cc;
269 float *hlp = qix[2] + rr * LMMSE_GRP + cc;
270 float *vlp = qix[3] + rr * LMMSE_GRP + cc;
271 float *interp = qix[4] + rr * LMMSE_GRP + cc;
272 // horizontal
273 float p1 = hlp[-4];
274 float p2 = hlp[-3];
275 float p3 = hlp[-2];
276 float p4 = hlp[-1];
277 float p5 = hlp[ 0];
278 float p6 = hlp[ 1];
279 float p7 = hlp[ 2];
280 float p8 = hlp[ 3];
281 float p9 = hlp[ 4];
282 float mu = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) / 9.0f;
283 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);
284 p1 -= hdiff[-4];
285 p2 -= hdiff[-3];
286 p3 -= hdiff[-2];
287 p4 -= hdiff[-1];
288 p5 -= hdiff[ 0];
289 p6 -= hdiff[ 1];
290 p7 -= hdiff[ 2];
291 p8 -= hdiff[ 3];
292 p9 -= hdiff[ 4];
293 float vn = 1e-7f + sqf(p1) + sqf(p2) + sqf(p3) + sqf(p4) + sqf(p5) + sqf(p6) + sqf(p7) + sqf(p8) + sqf(p9);
294 float xh = (hdiff[0] * vx + hlp[0] * vn) / (vx + vn);
295 float vh = vx * vn / (vx + vn);
296
297 // vertical
298 p1 = vlp[-w4];
299 p2 = vlp[-w3];
300 p3 = vlp[-w2];
301 p4 = vlp[-w1];
302 p5 = vlp[ 0];
303 p6 = vlp[ w1];
304 p7 = vlp[ w2];
305 p8 = vlp[ w3];
306 p9 = vlp[ w4];
307 mu = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) / 9.0f;
308 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);
309 p1 -= vdiff[-w4];
310 p2 -= vdiff[-w3];
311 p3 -= vdiff[-w2];
312 p4 -= vdiff[-w1];
313 p5 -= vdiff[ 0];
314 p6 -= vdiff[ w1];
315 p7 -= vdiff[ w2];
316 p8 -= vdiff[ w3];
317 p9 -= vdiff[ w4];
318 vn = 1e-7f + sqf(p1) + sqf(p2) + sqf(p3) + sqf(p4) + sqf(p5) + sqf(p6) + sqf(p7) + sqf(p8) + sqf(p9);
319 float xv = (vdiff[0] * vx + vlp[0] * vn) / (vx + vn);
320 float vv = vx * vn / (vx + vn);
321 // interpolated G-R(B)
322 interp[0] = (xh * vv + xv * vh) / (vh + vv);
323 }
324 }
325
326 // copy CFA values
327 for(int rr = 0, row_in = rowStart - BORDER_AROUND; rr < last_rr; rr++, row_in++)
328 {
329 for(int cc = 0, col_in = colStart - BORDER_AROUND; cc < last_cc; cc++, col_in++)
330 {
331 const int c = FC(rr, cc, filters);
332 const gboolean inside = ((row_in >= 0) && (row_in < height) && (col_in >= 0) && (col_in < width));
333 float *colc = qix[c] + rr * LMMSE_GRP + cc;
334 colc[0] = (inside) ? qix[5][rr * LMMSE_GRP + cc] : 0.0f;
335 if(c != 1)
336 {
337 float *col1 = qix[1] + rr * LMMSE_GRP + cc;
338 float *interp = qix[4] + rr * LMMSE_GRP + cc;
339 col1[0] = (inside) ? colc[0] + interp[0] : 0.0f;
340 }
341 }
342 }
343
344 // bilinear interpolation for R/B
345 // interpolate R/B at G location
346 for(int rr = 1; rr < last_rr - 1; rr++)
347 {
348 for(int cc = 1 + (FC(rr, 2, filters) & 1), c = FC(rr, cc + 1, filters); cc < last_cc - 1; cc += 2)
349 {
350 float *colc = qix[c] + rr * LMMSE_GRP + cc;
351 float *col1 = qix[1] + rr * LMMSE_GRP + cc;
352 colc[0] = col1[0] + 0.5f * (colc[ -1] - col1[ -1] + colc[ 1] - col1[ 1]);
353 c = 2 - c;
354 colc = qix[c] + rr * LMMSE_GRP + cc;
355 colc[0] = col1[0] + 0.5f * (colc[-w1] - col1[-w1] + colc[w1] - col1[w1]);
356 c = 2 - c;
357 }
358 }
359
360 // interpolate R/B at B/R location
361 for(int rr = 1; rr < last_rr - 1; rr++)
362 {
363 for(int cc = 1 + (FC(rr, 1, filters) & 1), c = 2 - FC(rr, cc, filters); cc < last_cc - 1; cc += 2)
364 {
365 float *colc = qix[c] + rr * LMMSE_GRP + cc;
366 float *col1 = qix[1] + rr * LMMSE_GRP + cc;
367 colc[0] = col1[0] + 0.25f * (colc[-w1] - col1[-w1] + colc[ -1] - col1[ -1] + colc[ 1] - col1[ 1] + colc[ w1] - col1[ w1]);
368 }
369 }
370
371 // for the median and refine corrections we need to specify other loop bounds
372 // for inner vs outer tiles
373 const int ccmin = (tile_horizontal == 0) ? 6 : 0 ;
374 const int ccmax = last_cc - ((tile_horizontal == num_horizontal - 1) ? 6 : 0);
375 const int rrmin = (tile_vertical == 0) ? 6 : 0 ;
376 const int rrmax = last_rr - ((tile_vertical == num_vertical - 1) ? 6 : 0);
377
378 // median filter/
379 for(int pass = 0; pass < medians; pass++)
380 {
381 // Apply 3x3 median filter
382 // Compute median(R-G) and median(B-G)
383 for(int rr = 1; rr < last_rr - 1; rr++)
384 {
385 for(int c = 0; c < 3; c += 2)
386 {
387 const int d = c + 3 - (c == 0 ? 0 : 1);
388 for(int cc = 1; cc < last_cc - 1; cc++)
389 {
390 float *corr = qix[d] + rr * LMMSE_GRP + cc;
391 float *colc = qix[c] + rr * LMMSE_GRP + cc;
392 float *col1 = qix[1] + rr * LMMSE_GRP + cc;
393 // Assign 3x3 differential color values
394 corr[0] = median9f(colc[-w1-1] - col1[-w1-1],
395 colc[-w1 ] - col1[-w1 ],
396 colc[-w1+1] - col1[-w1+1],
397 colc[ -1] - col1[ -1],
398 colc[ 0] - col1[ 0],
399 colc[ 1] - col1[ 1],
400 colc[ w1-1] - col1[ w1-1],
401 colc[ w1 ] - col1[ w1 ],
402 colc[ w1+1] - col1[ w1+1]);
403 }
404 }
405 }
406
407 // red/blue at GREEN pixel locations & red/blue and green at BLUE/RED pixel locations
408 for(int rr = rrmin; rr < rrmax - 1; rr++)
409 {
410 float *col0 = qix[0] + rr * LMMSE_GRP + ccmin;
411 float *col1 = qix[1] + rr * LMMSE_GRP + ccmin;
412 float *col2 = qix[2] + rr * LMMSE_GRP + ccmin;
413 float *corr3 = qix[3] + rr * LMMSE_GRP + ccmin;
414 float *corr4 = qix[4] + rr * LMMSE_GRP + ccmin;
415 int c0 = FC(rr, 0, filters);
416 int c1 = FC(rr, 1, filters);
417
418 if(c0 == 1)
419 {
420 c1 = 2 - c1;
421 const int d = c1 + 3 - (c1 == 0 ? 0 : 1);
422 int cc;
423 float *col_c1 = qix[c1] + rr * LMMSE_GRP + ccmin;
424 float *corr_d = qix[d] + rr * LMMSE_GRP + ccmin;
425 for(cc = ccmin; cc < ccmax - 1; cc += 2)
426 {
427 col0[0] = col1[0] + corr3[0];
428 col2[0] = col1[0] + corr4[0];
429 col0++;
430 col1++;
431 col2++;
432 corr3++;
433 corr4++;
434 col_c1++;
435 corr_d++;
436 col_c1[0] = col1[0] + corr_d[0];
437 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
438 col0++;
439 col1++;
440 col2++;
441 corr3++;
442 corr4++;
443 col_c1++;
444 corr_d++;
445 }
446
447 if(cc < ccmax)
448 { // remaining pixel, only if width is odd
449 col0[0] = col1[0] + corr3[0];
450 col2[0] = col1[0] + corr4[0];
451 }
452 }
453 else
454 {
455 c0 = 2 - c0;
456 const int d = c0 + 3 - (c0 == 0 ? 0 : 1);
457 float *col_c0 = qix[c0] + rr * LMMSE_GRP + ccmin;
458 float *corr_d = qix[d] + rr * LMMSE_GRP + ccmin;
459 int cc;
460 for(cc = ccmin; cc < ccmax - 1; cc += 2)
461 {
462 col_c0[0] = col1[0] + corr_d[0];
463 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
464 col0++;
465 col1++;
466 col2++;
467 corr3++;
468 corr4++;
469 col_c0++;
470 corr_d++;
471 col0[0] = col1[0] + corr3[0];
472 col2[0] = col1[0] + corr4[0];
473 col0++;
474 col1++;
475 col2++;
476 corr3++;
477 corr4++;
478 col_c0++;
479 corr_d++;
480 }
481
482 if(cc < ccmax)
483 { // remaining pixel, only if width is odd
484 col_c0[0] = col1[0] + corr_d[0];
485 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
486 }
487 }
488 }
489 }
490
491 // we fill the non-approximated color channels from gamma corrected cfa data
492 for(int rrr = 4; rrr < last_rr - 4; rrr++)
493 {
494 for(int ccc = 4; ccc < last_cc - 4; ccc++)
495 {
496 const int idx = rrr * LMMSE_GRP + ccc;
497 const int c = FC(rrr, ccc, filters);
498 qix[c][idx] = qix[5][idx];
499 }
500 }
501
502 // As we have the color channels fully available we can do the refinements here in tiled code
503 for(int step = 0; step < refine; step++)
504 {
505 // Reinforce interpolated green pixels on RED/BLUE pixel locations
506 for(int rr = rrmin + 2; rr < rrmax - 2; rr++)
507 {
508 for(int cc = ccmin + 2 + (FC(rr, 2, filters) & 1), c = FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
509 {
510 float *rgb1 = qix[1] + rr * LMMSE_GRP + cc;
511 float *rgbc = qix[c] + rr * LMMSE_GRP + cc;
512
513 const float dL = 1.0f / (1.0f + fabsf(rgbc[ -2] - rgbc[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
514 const float dR = 1.0f / (1.0f + fabsf(rgbc[ 2] - rgbc[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
515 const float dU = 1.0f / (1.0f + fabsf(rgbc[-w2] - rgbc[0]) + fabsf(rgb1[w1] - rgb1[-w1]));
516 const float dD = 1.0f / (1.0f + fabsf(rgbc[ w2] - rgbc[0]) + fabsf(rgb1[w1] - rgb1[-w1]));
517 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));
518 }
519 }
520 // Reinforce interpolated red/blue pixels on GREEN pixel locations
521 for(int rr = rrmin + 2; rr < rrmax - 2; rr++)
522 {
523 for(int cc = ccmin + 2 + (FC(rr, 3, filters) & 1), c = FC(rr, cc + 1, filters); cc < ccmax - 2; cc += 2)
524 {
525 for(int i = 0; i < 2; c = 2 - c, i++)
526 {
527 float *rgb1 = qix[1] + rr * LMMSE_GRP + cc;
528 float *rgbc = qix[c] + rr * LMMSE_GRP + cc;
529
530 const float dL = 1.0f / (1.0f + fabsf(rgb1[ -2] - rgb1[0]) + fabsf(rgbc[ 1] - rgbc[ -1]));
531 const float dR = 1.0f / (1.0f + fabsf(rgb1[ 2] - rgb1[0]) + fabsf(rgbc[ 1] - rgbc[ -1]));
532 const float dU = 1.0f / (1.0f + fabsf(rgb1[-w2] - rgb1[0]) + fabsf(rgbc[w1] - rgbc[-w1]));
533 const float dD = 1.0f / (1.0f + fabsf(rgb1[ w2] - rgb1[0]) + fabsf(rgbc[w1] - rgbc[-w1]));
534 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));
535 }
536 }
537 }
538 // Reinforce integrated red/blue pixels on BLUE/RED pixel locations
539 for(int rr = rrmin + 2; rr < rrmax - 2; rr++)
540 {
541 for(int cc = ccmin + 2 + (FC(rr, 2, filters) & 1), c = 2 - FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
542 {
543 const int d = 2 - c;
544 float *rgb1 = qix[1] + rr * LMMSE_GRP + cc;
545 float *rgbc = qix[c] + rr * LMMSE_GRP + cc;
546 float *rgbd = qix[d] + rr * LMMSE_GRP + cc;
547
548 const float dL = 1.0f / (1.0f + fabsf(rgbd[ -2] - rgbd[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
549 const float dR = 1.0f / (1.0f + fabsf(rgbd[ 2] - rgbd[0]) + fabsf(rgb1[ 1] - rgb1[ -1]));
550 const float dU = 1.0f / (1.0f + fabsf(rgbd[-w2] - rgbd[0]) + fabsf(rgb1[w1] - rgb1[-w1]));
551 const float dD = 1.0f / (1.0f + fabsf(rgbd[ w2] - rgbd[0]) + fabsf(rgb1[w1] - rgb1[-w1]));
552 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 }
554 }
555 }
556
557 // write result to out
558 // For the outermost tiles in all directions we also write the otherwise overlapped area
559 const int first_vertical = rowStart + ((tile_vertical == 0) ? 0 : LMMSE_OVERLAP);
560 const int last_vertical = rowEnd - ((tile_vertical == num_vertical - 1) ? 0 : LMMSE_OVERLAP);
561 const int first_horizontal = colStart + ((tile_horizontal == 0) ? 0 : LMMSE_OVERLAP);
562 const int last_horizontal = colEnd - ((tile_horizontal == num_horizontal - 1) ? 0 : LMMSE_OVERLAP);
563 for(int row = first_vertical, rr = row - rowStart + BORDER_AROUND; row < last_vertical; row++, rr++)
564 {
565 float *dest = out + 4 * (row * width + first_horizontal);
566 const int idx = rr * LMMSE_GRP + first_horizontal - colStart + BORDER_AROUND;
567 float *col0 = qix[0] + idx;
568 float *col1 = qix[1] + idx;
569 float *col2 = qix[2] + idx;
570 for(int col = first_horizontal; col < last_horizontal; col++, dest +=4, col0++, col1++, col2++)
571 {
572 dest[0] = scaler * calc_gamma(col0[0], gamma_out);
573 dest[1] = scaler * calc_gamma(col1[0], gamma_out);
574 dest[2] = scaler * calc_gamma(col2[0], gamma_out);
575 dest[3] = 0.0f;
576 }
577 }
578 }
579 }
580 dt_free_align(buffer);
581 }
582}
583
584// revert specific aggressive optimizing
585#ifdef __GNUC__
586 #pragma GCC pop_options
587#endif
588
589#undef LMMSE_TILESIZE
590#undef LMMSE_OVERLAP
591#undef BORDER_AROUND
592#undef LMMSE_TILEVALID
593#undef w1
594#undef w2
595#undef w3
596#undef w4
597
598// clang-format off
599// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
600// vim: shiftwidth=2 expandtab tabstop=2 cindent
601// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
602// 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:1996
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
#define INLINE
Definition cacorrect.c:128
void dt_control_log(const char *msg,...)
Definition control.c:424
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:653
static float * dt_alloc_align_float(size_t pixels)
Definition darktable.h:345
#define dt_free_align(A)
Definition darktable.h:334
static int FC(const int row, const int col, const unsigned int filters)
Definition data/kernels/common.h:43
static INLINE float calc_gamma(float val, float *table)
Definition lmmse.c:118
static void lmmse_demosaic(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
static INLINE float median3f(float x0, float x1, float x2)
Definition lmmse.c:70
#define w2
Definition lmmse.c:61
#define LMMSE_TILEVALID
Definition lmmse.c:59
#define BORDER_AROUND
Definition lmmse.c:57
#define LMMSE_OVERLAP
Definition lmmse.c:56
#define LMMSE_GRP
Definition lmmse.c:48
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:75
#define w1
Definition lmmse.c:60
#define w4
Definition lmmse.c:63
#define w3
Definition lmmse.c:62
static INLINE float limf(float x, float min, float max)
Definition lmmse.c:65
#define LMMSE_TILESIZE
Definition lmmse.c:58
static float sqf(const float x)
Definition math.h:215
Definition pixelpipe_hb.h:46
struct dt_iop_module_t *struct dt_dev_pixelpipe_t * pipe
Definition pixelpipe_hb.h:48
Definition imageop.h:32
int width
Definition imageop.h:33
int height
Definition imageop.h:33
#define c1
#define MIN(a, b)
Definition thinplate.c:23