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