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#define LMMSE_OVERLAP 8
56#define BORDER_AROUND 4
57#define LMMSE_TILESIZE (LMMSE_GRP - 2 * BORDER_AROUND)
58#define LMMSE_TILEVALID (LMMSE_TILESIZE - 2 * LMMSE_OVERLAP)
59#define w1 (LMMSE_GRP)
60#define w2 (LMMSE_GRP * 2)
61#define w3 (LMMSE_GRP * 3)
62#define w4 (LMMSE_GRP * 4)
63
64static INLINE float limf(float x, float min, float max)
65{
66 return fmaxf(min, fminf(x, max));
67}
68
69static INLINE float median3f(float x0, float x1, float x2)
70{
71 return fmaxf(fminf(x0,x1), fminf(x2, fmaxf(x0,x1)));
72}
73
74static INLINE float median9f(float a0, float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
75{
76 float tmp;
77 tmp = fminf(a1, a2);
78 a2 = fmaxf(a1, a2);
79 a1 = tmp;
80 tmp = fminf(a4, a5);
81 a5 = fmaxf(a4, a5);
82 a4 = tmp;
83 tmp = fminf(a7, a8);
84 a8 = fmaxf(a7, a8);
85 a7 = tmp;
86 tmp = fminf(a0, a1);
87 a1 = fmaxf(a0, a1);
88 a0 = tmp;
89 tmp = fminf(a3, a4);
90 a4 = fmaxf(a3, a4);
91 a3 = tmp;
92 tmp = fminf(a6, a7);
93 a7 = fmaxf(a6, a7);
94 a6 = tmp;
95 tmp = fminf(a1, a2);
96 a2 = fmaxf(a1, a2);
97 a1 = tmp;
98 tmp = fminf(a4, a5);
99 a5 = fminf(a4, a5);
100 a4 = tmp;
101 tmp = fminf(a7, a8);
102 a8 = fmaxf(a7, a8);
103 a3 = fmaxf(a0, a3);
104 a5 = fminf(a5, a8);
105 a7 = fmaxf(a4, tmp);
106 tmp = fminf(a4, tmp);
107 a6 = fmaxf(a3, a6);
108 a4 = fmaxf(a1, tmp);
109 a2 = fminf(a2, a5);
110 a4 = fminf(a4, a7);
111 tmp = fminf(a4, a2);
112 a2 = fmaxf(a4, a2);
113 a4 = fmaxf(a6, tmp);
114 return fminf(a4, a2);
115}
116
117static INLINE float calc_gamma(float val, float *table)
118{
119 const float index = val * 65535.0f;
120 if(index < 0.0f) return 0.0f;
121 if(index > 65534.99f) return 1.0f;
122 const int idx = (int)index;
123
124 const float diff = index - (float)idx;
125 const float p1 = table[idx];
126 const float p2 = table[idx+1] - p1;
127 return (p1 + p2 * diff);
128}
129static 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,
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)
131{
132 const int width = roi_in->width;
133 const int height = roi_in->height;
134
135 if((width < 16) || (height < 16))
136 {
137 dt_control_log(_("[lmmse_demosaic] too small area"));
138 return;
139 }
140
141 float h0 = 1.0f;
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);
147 h0 /= hs;
148 h1 /= hs;
149 h2 /= hs;
150 h3 /= hs;
151 h4 /= hs;
152
153 // median filter iterations
154 const int medians = (mode < 2) ? mode : 3;
155 // refinement steps
156 const int refine = (mode > 2) ? mode - 2 : 0;
157
158 const float scaler = fmaxf(piece->dsc_in.processed_maximum[0], fmaxf(piece->dsc_in.processed_maximum[1], piece->dsc_in.processed_maximum[2]));
159 const float revscaler = 1.0f / scaler;
160
161 const int num_vertical = 1 + (height - 2 * LMMSE_OVERLAP -1) / LMMSE_TILEVALID;
162 const int num_horizontal = 1 + (width - 2 * LMMSE_OVERLAP -1) / LMMSE_TILEVALID;
163#ifdef _OPENMP
164 #pragma omp parallel
165#endif
166 {
167 float *qix[6];
169
170 qix[0] = buffer;
171 for(int i = 1; i < 6; i++)
172 {
173 qix[i] = qix[i - 1] + LMMSE_GRP * LMMSE_GRP;
174 }
175 memset_zero(buffer, sizeof(float) * LMMSE_GRP * LMMSE_GRP * 6);
176
177#ifdef _OPENMP
178 #pragma omp for schedule(simd:dynamic, 6) collapse(2)
179#endif
180 for(int tile_vertical = 0; tile_vertical < num_vertical; tile_vertical++)
181 {
182 for(int tile_horizontal = 0; tile_horizontal < num_horizontal; tile_horizontal++)
183 {
184 const int rowStart = tile_vertical * LMMSE_TILEVALID;
185 const int rowEnd = MIN(rowStart + LMMSE_TILESIZE, height);
186
187 const int colStart = tile_horizontal * LMMSE_TILEVALID;
188 const int colEnd = MIN(colStart + LMMSE_TILESIZE, width);
189
190 const int tileRows = MIN(rowEnd - rowStart, LMMSE_TILESIZE);
191 const int tileCols = MIN(colEnd - colStart, LMMSE_TILESIZE);
192
193 // index limit; normally is LMMSE_GRP but maybe missing bottom lines or right columns for outermost tile
194 const int last_rr = tileRows + 2 * BORDER_AROUND;
195 const int last_cc = tileCols + 2 * BORDER_AROUND;
196
197 for(int rrr = BORDER_AROUND, row = rowStart; rrr < tileRows + BORDER_AROUND; rrr++, row++)
198 {
199 float *cfa = qix[5] + rrr * LMMSE_GRP + BORDER_AROUND;
200 int idx = row * width + colStart;
201 for(int ccc = BORDER_AROUND; ccc < tileCols + BORDER_AROUND; ccc++, cfa++, idx++)
202 {
203 cfa[0] = calc_gamma(revscaler * in[idx], gamma_in);
204 }
205 }
206
207 // G-R(B)
208 for(int rr = 2; rr < last_rr - 2; rr++)
209 {
210 // G-R(B) at R(B) location
211 for(int cc = 2 + (FC(rr, 2, filters) & 1); cc < last_cc - 2; cc += 2)
212 {
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];
215 // horizontal
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);
220 hdiff[0] -= cfa[0];
221
222 // vertical
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);
227 vdiff[0] -= cfa[0];
228 }
229
230 // G-R(B) at G location
231 for(int ccc = 2 + (FC(rr, 3, filters) & 1); ccc < last_cc - 2; ccc += 2)
232 {
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];
240 }
241 }
242
243 // apply low pass filter on differential colors
244 for (int rr = 4; rr < last_rr - 4; rr++)
245 {
246 for(int cc = 4; cc < last_cc - 4; cc++)
247 {
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]);
254 }
255 }
256
257 for(int rr = 4; rr < last_rr - 4; rr++)
258 {
259 for(int cc = 4 + (FC(rr, 4, filters) & 1); cc < last_cc - 4; cc += 2)
260 {
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;
266 // horizontal
267 float p1 = hlp[-4];
268 float p2 = hlp[-3];
269 float p3 = hlp[-2];
270 float p4 = hlp[-1];
271 float p5 = hlp[ 0];
272 float p6 = hlp[ 1];
273 float p7 = hlp[ 2];
274 float p8 = hlp[ 3];
275 float p9 = hlp[ 4];
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);
278 p1 -= hdiff[-4];
279 p2 -= hdiff[-3];
280 p3 -= hdiff[-2];
281 p4 -= hdiff[-1];
282 p5 -= hdiff[ 0];
283 p6 -= hdiff[ 1];
284 p7 -= hdiff[ 2];
285 p8 -= hdiff[ 3];
286 p9 -= hdiff[ 4];
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);
290
291 // vertical
292 p1 = vlp[-w4];
293 p2 = vlp[-w3];
294 p3 = vlp[-w2];
295 p4 = vlp[-w1];
296 p5 = vlp[ 0];
297 p6 = vlp[ w1];
298 p7 = vlp[ w2];
299 p8 = vlp[ w3];
300 p9 = vlp[ w4];
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);
303 p1 -= vdiff[-w4];
304 p2 -= vdiff[-w3];
305 p3 -= vdiff[-w2];
306 p4 -= vdiff[-w1];
307 p5 -= vdiff[ 0];
308 p6 -= vdiff[ w1];
309 p7 -= vdiff[ w2];
310 p8 -= vdiff[ w3];
311 p9 -= vdiff[ w4];
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);
315 // interpolated G-R(B)
316 interp[0] = (xh * vv + xv * vh) / (vh + vv);
317 }
318 }
319
320 // copy CFA values
321 for(int rr = 0, row_in = rowStart - BORDER_AROUND; rr < last_rr; rr++, row_in++)
322 {
323 for(int cc = 0, col_in = colStart - BORDER_AROUND; cc < last_cc; cc++, col_in++)
324 {
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;
329 if(c != 1)
330 {
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;
334 }
335 }
336 }
337
338 // bilinear interpolation for R/B
339 // interpolate R/B at G location
340 for(int rr = 1; rr < last_rr - 1; rr++)
341 {
342 for(int cc = 1 + (FC(rr, 2, filters) & 1), c = FC(rr, cc + 1, filters); cc < last_cc - 1; cc += 2)
343 {
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]);
347 c = 2 - c;
348 colc = qix[c] + rr * LMMSE_GRP + cc;
349 colc[0] = col1[0] + 0.5f * (colc[-w1] - col1[-w1] + colc[w1] - col1[w1]);
350 c = 2 - c;
351 }
352 }
353
354 // interpolate R/B at B/R location
355 for(int rr = 1; rr < last_rr - 1; rr++)
356 {
357 for(int cc = 1 + (FC(rr, 1, filters) & 1), c = 2 - FC(rr, cc, filters); cc < last_cc - 1; cc += 2)
358 {
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]);
362 }
363 }
364
365 // for the median and refine corrections we need to specify other loop bounds
366 // for inner vs outer tiles
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);
371
372 // median filter/
373 for(int pass = 0; pass < medians; pass++)
374 {
375 // Apply 3x3 median filter
376 // Compute median(R-G) and median(B-G)
377 for(int rr = 1; rr < last_rr - 1; rr++)
378 {
379 for(int c = 0; c < 3; c += 2)
380 {
381 const int d = c + 3 - (c == 0 ? 0 : 1);
382 for(int cc = 1; cc < last_cc - 1; cc++)
383 {
384 float *corr = qix[d] + rr * LMMSE_GRP + cc;
385 float *colc = qix[c] + rr * LMMSE_GRP + cc;
386 float *col1 = qix[1] + rr * LMMSE_GRP + cc;
387 // Assign 3x3 differential color values
388 corr[0] = median9f(colc[-w1-1] - col1[-w1-1],
389 colc[-w1 ] - col1[-w1 ],
390 colc[-w1+1] - col1[-w1+1],
391 colc[ -1] - col1[ -1],
392 colc[ 0] - col1[ 0],
393 colc[ 1] - col1[ 1],
394 colc[ w1-1] - col1[ w1-1],
395 colc[ w1 ] - col1[ w1 ],
396 colc[ w1+1] - col1[ w1+1]);
397 }
398 }
399 }
400
401 // red/blue at GREEN pixel locations & red/blue and green at BLUE/RED pixel locations
402 for(int rr = rrmin; rr < rrmax - 1; rr++)
403 {
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);
411
412 if(c0 == 1)
413 {
414 c1 = 2 - c1;
415 const int d = c1 + 3 - (c1 == 0 ? 0 : 1);
416 int cc;
417 float *col_c1 = qix[c1] + rr * LMMSE_GRP + ccmin;
418 float *corr_d = qix[d] + rr * LMMSE_GRP + ccmin;
419 for(cc = ccmin; cc < ccmax - 1; cc += 2)
420 {
421 col0[0] = col1[0] + corr3[0];
422 col2[0] = col1[0] + corr4[0];
423 col0++;
424 col1++;
425 col2++;
426 corr3++;
427 corr4++;
428 col_c1++;
429 corr_d++;
430 col_c1[0] = col1[0] + corr_d[0];
431 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
432 col0++;
433 col1++;
434 col2++;
435 corr3++;
436 corr4++;
437 col_c1++;
438 corr_d++;
439 }
440
441 if(cc < ccmax)
442 { // remaining pixel, only if width is odd
443 col0[0] = col1[0] + corr3[0];
444 col2[0] = col1[0] + corr4[0];
445 }
446 }
447 else
448 {
449 c0 = 2 - c0;
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;
453 int cc;
454 for(cc = ccmin; cc < ccmax - 1; cc += 2)
455 {
456 col_c0[0] = col1[0] + corr_d[0];
457 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
458 col0++;
459 col1++;
460 col2++;
461 corr3++;
462 corr4++;
463 col_c0++;
464 corr_d++;
465 col0[0] = col1[0] + corr3[0];
466 col2[0] = col1[0] + corr4[0];
467 col0++;
468 col1++;
469 col2++;
470 corr3++;
471 corr4++;
472 col_c0++;
473 corr_d++;
474 }
475
476 if(cc < ccmax)
477 { // remaining pixel, only if width is odd
478 col_c0[0] = col1[0] + corr_d[0];
479 col1[0] = 0.5f * (col0[0] - corr3[0] + col2[0] - corr4[0]);
480 }
481 }
482 }
483 }
484
485 // we fill the non-approximated color channels from gamma corrected cfa data
486 for(int rrr = 4; rrr < last_rr - 4; rrr++)
487 {
488 for(int ccc = 4; ccc < last_cc - 4; ccc++)
489 {
490 const int idx = rrr * LMMSE_GRP + ccc;
491 const int c = FC(rrr, ccc, filters);
492 qix[c][idx] = qix[5][idx];
493 }
494 }
495
496 // As we have the color channels fully available we can do the refinements here in tiled code
497 for(int step = 0; step < refine; step++)
498 {
499 // Reinforce interpolated green pixels on RED/BLUE pixel locations
500 for(int rr = rrmin + 2; rr < rrmax - 2; rr++)
501 {
502 for(int cc = ccmin + 2 + (FC(rr, 2, filters) & 1), c = FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
503 {
504 float *rgb1 = qix[1] + rr * LMMSE_GRP + cc;
505 float *rgbc = qix[c] + rr * LMMSE_GRP + cc;
506
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));
512 }
513 }
514 // Reinforce interpolated red/blue pixels on GREEN pixel locations
515 for(int rr = rrmin + 2; rr < rrmax - 2; rr++)
516 {
517 for(int cc = ccmin + 2 + (FC(rr, 3, filters) & 1), c = FC(rr, cc + 1, filters); cc < ccmax - 2; cc += 2)
518 {
519 for(int i = 0; i < 2; c = 2 - c, i++)
520 {
521 float *rgb1 = qix[1] + rr * LMMSE_GRP + cc;
522 float *rgbc = qix[c] + rr * LMMSE_GRP + cc;
523
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));
529 }
530 }
531 }
532 // Reinforce integrated red/blue pixels on BLUE/RED pixel locations
533 for(int rr = rrmin + 2; rr < rrmax - 2; rr++)
534 {
535 for(int cc = ccmin + 2 + (FC(rr, 2, filters) & 1), c = 2 - FC(rr, cc, filters); cc < ccmax - 2; cc += 2)
536 {
537 const int d = 2 - c;
538 float *rgb1 = qix[1] + rr * LMMSE_GRP + cc;
539 float *rgbc = qix[c] + rr * LMMSE_GRP + cc;
540 float *rgbd = qix[d] + rr * LMMSE_GRP + cc;
541
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));
547 }
548 }
549 }
550
551 // write result to out
552 // For the outermost tiles in all directions we also write the otherwise overlapped area
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);
557 for(int row = first_vertical, rr = row - rowStart + BORDER_AROUND; row < last_vertical; row++, rr++)
558 {
559 float *dest = out + 4 * (row * width + first_horizontal);
560 const int idx = rr * LMMSE_GRP + first_horizontal - colStart + BORDER_AROUND;
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++)
565 {
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);
569 dest[3] = 0.0f;
570 }
571 }
572 }
573 }
575 }
576}
577
578#undef LMMSE_TILESIZE
579#undef LMMSE_OVERLAP
580#undef BORDER_AROUND
581#undef LMMSE_TILEVALID
582#undef w1
583#undef w2
584#undef w3
585#undef w4
586
587// clang-format off
588// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
589// vim: shiftwidth=2 expandtab tabstop=2 cindent
590// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
591// 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:880
#define dt_pixelpipe_cache_alloc_align_float_cache(pixels, id)
Definition darktable.h:447
#define dt_pixelpipe_cache_free_align(mem)
Definition darktable.h:453
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:117
static INLINE float median3f(float x0, float x1, float x2)
Definition lmmse.c:69
#define w2
Definition lmmse.c:60
#define LMMSE_TILEVALID
Definition lmmse.c:58
#define BORDER_AROUND
Definition lmmse.c:56
#define LMMSE_OVERLAP
Definition lmmse.c:55
#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:74
#define w1
Definition lmmse.c:59
#define w4
Definition lmmse.c:62
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:129
#define w3
Definition lmmse.c:61
static INLINE float limf(float x, float min, float max)
Definition lmmse.c:64
#define LMMSE_TILESIZE
Definition lmmse.c:57
return noise *sigma mu
Definition src/develop/noise_generator.h:89
Definition pixelpipe_hb.h:96
dt_iop_buffer_dsc_t dsc_in
Definition pixelpipe_hb.h:142
dt_aligned_pixel_t processed_maximum
Definition format.h:85
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