Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
ppg.c
Go to the documentation of this file.
1/*
2 This file is part of the Ansel project.
3 Copyright (C) 2023, 2025-2026 Aurélien PIERRE.
4
5 Ansel 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 Ansel 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 Ansel. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19
22static int demosaic_ppg(float *const out, const float *const in, const dt_iop_roi_t *const roi_out,
23 const dt_iop_roi_t *const roi_in, const uint32_t filters, const float thrs)
24{
25 // these may differ a little, if you're unlucky enough to split a bayer block with cropping or similar.
26 // we never want to access the input out of bounds though:
27 assert(roi_in->width >= roi_out->width);
28 assert(roi_in->height >= roi_out->height);
29 // border interpolate
30 float sum[8];
31 for(int j = 0; j < roi_out->height; j++)
32 for(int i = 0; i < roi_out->width; i++)
33 {
34 if(i == 3 && j >= 3 && j < roi_out->height - 3) i = roi_out->width - 3;
35 if(i == roi_out->width) break;
36 memset(sum, 0, sizeof(float) * 8);
37 for(int y = j - 1; y != j + 2; y++)
38 for(int x = i - 1; x != i + 2; x++)
39 {
40 const int yy = y + roi_out->y, xx = x + roi_out->x;
41 if((yy >= 0) && (xx >= 0) && (yy < roi_in->height) && (xx < roi_in->width))
42 {
43 const int f = FC(y, x, filters);
44 sum[f] += in[(size_t)yy * roi_in->width + xx];
45 sum[f + 4]++;
46 }
47 }
48 const int f = FC(j, i, filters);
49 for(int c = 0; c < 3; c++)
50 {
51 if(c != f && sum[c + 4] > 0.0f)
52 out[4 * ((size_t)j * roi_out->width + i) + c] = sum[c] / sum[c + 4];
53 else
54 out[4 * ((size_t)j * roi_out->width + i) + c]
55 = in[((size_t)j + roi_out->y) * roi_in->width + i + roi_out->x];
56 }
57 }
58 const int median = thrs > 0.0f;
59 // if(median) fbdd_green(out, in, roi_out, roi_in, filters);
60 const float *input = in;
61 if(median)
62 {
63 float *med_in = (float *)dt_pixelpipe_cache_alloc_align_float_cache((size_t)roi_in->height * roi_in->width, 0);
64 if(IS_NULL_PTR(med_in)) return 1;
65 pre_median(med_in, in, roi_in, filters, 1, thrs);
66 input = med_in;
67 }
68// for all pixels except those in the 3 pixel border:
69// interpolate green from input into out float array, or copy color.
71 for(int j = 3; j < roi_out->height - 3; j++)
72 {
73 float *buf = out + (size_t)4 * roi_out->width * j + 4 * 3;
74 const float *buf_in = input + (size_t)roi_in->width * (j + roi_out->y) + 3 + roi_out->x;
75 for(int i = 3; i < roi_out->width - 3; i++)
76 {
77 const int c = FC(j, i, filters);
79 const float pc = buf_in[0];
80 // if(__builtin_expect(c == 0 || c == 2, 1))
81 if(c == 0 || c == 2)
82 {
83 color[c] = pc;
84 // get stuff (hopefully from cache)
85 const float pym = buf_in[-roi_in->width * 1];
86 const float pym2 = buf_in[-roi_in->width * 2];
87 const float pym3 = buf_in[-roi_in->width * 3];
88 const float pyM = buf_in[+roi_in->width * 1];
89 const float pyM2 = buf_in[+roi_in->width * 2];
90 const float pyM3 = buf_in[+roi_in->width * 3];
91 const float pxm = buf_in[-1];
92 const float pxm2 = buf_in[-2];
93 const float pxm3 = buf_in[-3];
94 const float pxM = buf_in[+1];
95 const float pxM2 = buf_in[+2];
96 const float pxM3 = buf_in[+3];
97
98 const float guessx = (pxm + pc + pxM) * 2.0f - pxM2 - pxm2;
99 const float diffx = (fabsf(pxm2 - pc) + fabsf(pxM2 - pc) + fabsf(pxm - pxM)) * 3.0f
100 + (fabsf(pxM3 - pxM) + fabsf(pxm3 - pxm)) * 2.0f;
101 const float guessy = (pym + pc + pyM) * 2.0f - pyM2 - pym2;
102 const float diffy = (fabsf(pym2 - pc) + fabsf(pyM2 - pc) + fabsf(pym - pyM)) * 3.0f
103 + (fabsf(pyM3 - pyM) + fabsf(pym3 - pym)) * 2.0f;
104 if(diffx > diffy)
105 {
106 // use guessy
107 const float m = fminf(pym, pyM);
108 const float M = fmaxf(pym, pyM);
109 color[1] = fmaxf(fminf(guessy * .25f, M), m);
110 }
111 else
112 {
113 const float m = fminf(pxm, pxM);
114 const float M = fmaxf(pxm, pxM);
115 color[1] = fmaxf(fminf(guessx * .25f, M), m);
116 }
117 }
118 else
119 color[1] = pc;
120
121 color[3] = 0.0f;
122 // write using MOVNTPS (write combine omitting caches)
123 // _mm_stream_ps(buf, col);
124 memcpy(buf, color, sizeof(float) * 4);
125 buf += 4;
126 buf_in++;
127 }
128 }
129
130
131// for all pixels except the outermost row/column:
132// interpolate colors using out as input into float out array
134 for(int j = 1; j < roi_out->height - 1; j++)
135 {
136 float *buf = out + (size_t)4 * roi_out->width * j + 4;
137 for(int i = 1; i < roi_out->width - 1; i++)
138 {
139 // also prefetch direct nbs top/bottom
140 const int c = FC(j, i, filters);
141 dt_aligned_pixel_t color = { buf[0], buf[1], buf[2], buf[3] };
142
143 // fill all four pixels with correctly interpolated stuff: r/b for green1/2
144 // b for r and r for b
145 if(__builtin_expect(c & 1, 1)) // c == 1 || c == 3)
146 {
147 // calculate red and blue for green pixels:
148 // need 4-nbhood:
149 const float *nt = buf - 4 * roi_out->width;
150 const float *nb = buf + 4 * roi_out->width;
151 const float *nl = buf - 4;
152 const float *nr = buf + 4;
153 if(FC(j, i + 1, filters) == 0) // red nb in same row
154 {
155 color[2] = (nt[2] + nb[2] + 2.0f * color[1] - nt[1] - nb[1]) * .5f;
156 color[0] = (nl[0] + nr[0] + 2.0f * color[1] - nl[1] - nr[1]) * .5f;
157 }
158 else
159 {
160 // blue nb
161 color[0] = (nt[0] + nb[0] + 2.0f * color[1] - nt[1] - nb[1]) * .5f;
162 color[2] = (nl[2] + nr[2] + 2.0f * color[1] - nl[1] - nr[1]) * .5f;
163 }
164 }
165 else
166 {
167 // get 4-star-nbhood:
168 const float *ntl = buf - 4 - 4 * roi_out->width;
169 const float *ntr = buf + 4 - 4 * roi_out->width;
170 const float *nbl = buf - 4 + 4 * roi_out->width;
171 const float *nbr = buf + 4 + 4 * roi_out->width;
172
173 if(c == 0)
174 {
175 // red pixel, fill blue:
176 const float diff1 = fabsf(ntl[2] - nbr[2]) + fabsf(ntl[1] - color[1]) + fabsf(nbr[1] - color[1]);
177 const float guess1 = ntl[2] + nbr[2] + 2.0f * color[1] - ntl[1] - nbr[1];
178 const float diff2 = fabsf(ntr[2] - nbl[2]) + fabsf(ntr[1] - color[1]) + fabsf(nbl[1] - color[1]);
179 const float guess2 = ntr[2] + nbl[2] + 2.0f * color[1] - ntr[1] - nbl[1];
180 if(diff1 > diff2)
181 color[2] = guess2 * .5f;
182 else if(diff1 < diff2)
183 color[2] = guess1 * .5f;
184 else
185 color[2] = (guess1 + guess2) * .25f;
186 }
187 else // c == 2, blue pixel, fill red:
188 {
189 const float diff1 = fabsf(ntl[0] - nbr[0]) + fabsf(ntl[1] - color[1]) + fabsf(nbr[1] - color[1]);
190 const float guess1 = ntl[0] + nbr[0] + 2.0f * color[1] - ntl[1] - nbr[1];
191 const float diff2 = fabsf(ntr[0] - nbl[0]) + fabsf(ntr[1] - color[1]) + fabsf(nbl[1] - color[1]);
192 const float guess2 = ntr[0] + nbl[0] + 2.0f * color[1] - ntr[1] - nbl[1];
193 if(diff1 > diff2)
194 color[0] = guess2 * .5f;
195 else if(diff1 < diff2)
196 color[0] = guess1 * .5f;
197 else
198 color[0] = (guess1 + guess2) * .25f;
199 }
200 }
201 // _mm_stream_ps(buf, col);
202 memcpy(buf, color, sizeof(float) * 4);
203 buf += 4;
204 }
205 }
206
207
208 // _mm_sfence();
210 return 0;
211}
212
213// clang-format off
214// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
215// vim: shiftwidth=2 expandtab tabstop=2 cindent
216// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
217// clang-format on
#define m
Definition basecurve.c:277
static void pre_median(float *out, const float *const in, const dt_iop_roi_t *const roi, const uint32_t filters, const int num_passes, const float threshold)
Definition basic.c:182
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
const double thrs
Definition chart/main.c:54
const float i
Definition colorspaces_inline_conversions.h:440
const dt_aligned_pixel_t f
Definition colorspaces_inline_conversions.h:102
const dt_colormatrix_t dt_aligned_pixel_t out
Definition colorspaces_inline_conversions.h:42
static const dt_colormatrix_t M
Definition colorspaces_inline_conversions.h:682
#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
#define __DT_CLONE_TARGETS__
Definition darktable.h:367
#define __OMP_PARALLEL_FOR__(...)
Definition darktable.h:258
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition darktable.h:281
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
float dt_aligned_pixel_t[4]
Definition noiseprofile.c:28
#define median(a, n)
Definition noiseprofile.c:75
static __DT_CLONE_TARGETS__ int demosaic_ppg(float *const out, const float *const in, const dt_iop_roi_t *const roi_out, const dt_iop_roi_t *const roi_in, const uint32_t filters, const float thrs)
Definition ppg.c:22
Region of interest passed through the pixelpipe.
Definition imageop.h:72
int x
Definition imageop.h:73
int width
Definition imageop.h:73
int height
Definition imageop.h:73
int y
Definition imageop.h:73