36 const uint32_t filters,
const uint8_t (*
const xtrans)[6],
const int only_vng_linear)
38 static const signed char terms[]
39 = { -2, -2, +0, -1, 1, 0x01, -2, -2, +0, +0, 2, 0x01, -2, -1, -1, +0, 1, 0x01, -2, -1, +0, -1, 1, 0x02,
40 -2, -1, +0, +0, 1, 0x03, -2, -1, +0, +1, 2, 0x01, -2, +0, +0, -1, 1, 0x06, -2, +0, +0, +0, 2, 0x02,
41 -2, +0, +0, +1, 1, 0x03, -2, +1, -1, +0, 1, 0x04, -2, +1, +0, -1, 2, 0x04, -2, +1, +0, +0, 1, 0x06,
42 -2, +1, +0, +1, 1, 0x02, -2, +2, +0, +0, 2, 0x04, -2, +2, +0, +1, 1, 0x04, -1, -2, -1, +0, 1, 0x80,
43 -1, -2, +0, -1, 1, 0x01, -1, -2, +1, -1, 1, 0x01, -1, -2, +1, +0, 2, 0x01, -1, -1, -1, +1, 1, 0x88,
44 -1, -1, +1, -2, 1, 0x40, -1, -1, +1, -1, 1, 0x22, -1, -1, +1, +0, 1, 0x33, -1, -1, +1, +1, 2, 0x11,
45 -1, +0, -1, +2, 1, 0x08, -1, +0, +0, -1, 1, 0x44, -1, +0, +0, +1, 1, 0x11, -1, +0, +1, -2, 2, 0x40,
46 -1, +0, +1, -1, 1, 0x66, -1, +0, +1, +0, 2, 0x22, -1, +0, +1, +1, 1, 0x33, -1, +0, +1, +2, 2, 0x10,
47 -1, +1, +1, -1, 2, 0x44, -1, +1, +1, +0, 1, 0x66, -1, +1, +1, +1, 1, 0x22, -1, +1, +1, +2, 1, 0x10,
48 -1, +2, +0, +1, 1, 0x04, -1, +2, +1, +0, 2, 0x04, -1, +2, +1, +1, 1, 0x04, +0, -2, +0, +0, 2, 0x80,
49 +0, -1, +0, +1, 2, 0x88, +0, -1, +1, -2, 1, 0x40, +0, -1, +1, +0, 1, 0x11, +0, -1, +2, -2, 1, 0x40,
50 +0, -1, +2, -1, 1, 0x20, +0, -1, +2, +0, 1, 0x30, +0, -1, +2, +1, 2, 0x10, +0, +0, +0, +2, 2, 0x08,
51 +0, +0, +2, -2, 2, 0x40, +0, +0, +2, -1, 1, 0x60, +0, +0, +2, +0, 2, 0x20, +0, +0, +2, +1, 1, 0x30,
52 +0, +0, +2, +2, 2, 0x10, +0, +1, +1, +0, 1, 0x44, +0, +1, +1, +2, 1, 0x10, +0, +1, +2, -1, 2, 0x40,
53 +0, +1, +2, +0, 1, 0x60, +0, +1, +2, +1, 1, 0x20, +0, +1, +2, +2, 1, 0x10, +1, -2, +1, +0, 1, 0x80,
54 +1, -1, +1, +1, 1, 0x88, +1, +0, +1, +2, 1, 0x08, +1, +0, +2, -1, 1, 0x40, +1, +0, +2, +1, 1, 0x10 };
55 static const signed char chood[]
56 = { -1, -1, -1, 0, -1, +1, 0, +1, +1, +1, +1, 0, +1, -1, 0, -1 };
57 int *ip, *code[16][16];
62 const int prow = (filters == 9) ? 6 : 8;
63 const int pcol = (filters == 9) ? 6 : 2;
64 const int colors = (filters == 9) ? 3 : 4;
67 uint32_t filters4 = filters;
70 else if((filters & 3) == 1)
71 filters4 = filters | 0x03030303u;
73 filters4 = filters | 0x0c0c0c0cu;
78 if(only_vng_linear)
return 0;
81 sizeof(**brow) *
width * 3 +
sizeof(*ip) * prow * pcol * 320,
85 fprintf(stderr,
"[demosaic] not able to allocate VNG buffer\n");
89 ip = (
int *)(buffer +
sizeof(**brow) *
width * 3);
92 for(
int col = 0; col < pcol; col++)
95 const signed char *cp = terms;
96 for(
int t = 0;
t < 64;
t++)
98 const int y1 = *cp++, x1 = *cp++;
99 const int y2 = *cp++, x2 = *cp++;
101 const int grads = *cp++;
102 const int color =
fcol(
row + y1, col + x1, filters4, xtrans);
103 if(
fcol(
row + y2, col + x2, filters4, xtrans) != color)
continue;
105 = (
fcol(
row, col + 1, filters4, xtrans) == color &&
fcol(
row + 1, col, filters4, xtrans) == color)
108 if(abs(y1 - y2) == diag && abs(x1 - x2) == diag)
continue;
109 *ip++ = (y1 *
width + x1) * 4 + color;
110 *ip++ = (y2 *
width + x2) * 4 + color;
112 for(
int g = 0;
g < 8;
g++)
113 if(grads & 1 <<
g) *ip++ =
g;
118 for(
int g = 0;
g < 8;
g++)
120 const int y = *cp++,
x = *cp++;
121 *ip++ = (y *
width +
x) * 4;
122 const int color =
fcol(
row, col, filters4, xtrans);
123 if(
fcol(
row + y, col +
x, filters4, xtrans) != color
124 &&
fcol(
row + y * 2, col +
x * 2, filters4, xtrans) == color)
125 *ip++ = (y *
width +
x) * 8 + color;
134 for(
int col = 2; col <
width - 2; col++)
137 float gval[8] = { 0.0f };
139 ip = code[(
row + roi_in->
y) % prow][(col + roi_in->
x) % pcol];
140 while((
g = ip[0]) != INT_MAX)
142 float diff = fabsf(pix[
g] - pix[ip[1]]) * ip[2];
145 if((
g = ip[-1]) == -1)
continue;
147 while((
g = *ip++) != -1) gval[
g] += diff;
150 float gmin = gval[0], gmax = gval[0];
151 for(
g = 1;
g < 8;
g++)
153 if(gmin > gval[
g]) gmin = gval[
g];
154 if(gmax < gval[
g]) gmax = gval[
g];
158 memcpy(brow[2][col], pix,
sizeof(*
out) * 4);
161 const float thold = gmin + (gmax * 0.5f);
163 const int color =
fcol(
row + roi_in->
y, col + roi_in->
x, filters4, xtrans);
165 for(
g = 0;
g < 8;
g++, ip += 2)
169 for(
int c = 0; c <
colors; c++)
170 if(c == color && ip[1])
171 sum[c] += (pix[c] + pix[ip[1]]) * 0.5f;
173 sum[c] += pix[ip[0] + c];
177 for(
int c = 0; c <
colors; c++)
179 float tot = pix[color];
180 if(c != color) tot += (sum[c] - sum[color]) / num;
181 brow[2][col][c] = tot;
187 for(
int g = 0;
g < 4;
g++) brow[(
g - 1) & 3] = brow[
g];
198 out[
i * 4 + 1] = (
out[
i * 4 + 1] +
out[
i * 4 + 3]) / 2.0f;
209 const gboolean smooth,
const int only_vng_linear)
214 const uint8_t(*
const xtrans)[6] = (
const uint8_t(*
const)[6])piece->
dsc_in.
xtrans;
225 const int size = (filters4 == 9u) ? 6 : 16;
226 const int colors = (filters4 == 9u) ? 3 : 4;
227 const int prow = (filters4 == 9u) ? 6 : 8;
228 const int pcol = (filters4 == 9u) ? 6 : 2;
229 const int devid = pipe->
devid;
231 const float processed_maximum[4]
237 cl_mem dev_tmp = NULL;
238 cl_mem dev_aux = NULL;
239 cl_mem dev_xtrans = NULL;
240 cl_mem dev_lookup = NULL;
241 cl_mem dev_code = NULL;
242 cl_mem dev_ips = NULL;
243 cl_mem dev_green_eq = NULL;
246 int32_t(*
lookup)[16][32] = NULL;
265 const size_t lookup_size = (size_t)16 * 16 * 32 *
sizeof(int32_t);
266 lookup = malloc(lookup_size);
269 for(
int col = 0; col <
size; col++)
273 const int f =
fcol(
row + roi_in->
y, col + roi_in->
x, filters4, xtrans);
275 for(
int y = -1; y <= 1; y++)
276 for(
int x = -1;
x <= 1;
x++)
278 const int weight = 1 << ((y == 0) + (
x == 0));
279 const int color =
fcol(
row + y + roi_in->
y, col +
x + roi_in->
x, filters4, xtrans);
280 if(color ==
f)
continue;
281 *ip++ = (y << 16) | (
x & 0xffffu);
287 for(
int c = 0; c <
colors; c++)
297 static const signed char terms[]
298 = { -2, -2, +0, -1, 1, 0x01, -2, -2, +0, +0, 2, 0x01, -2, -1, -1, +0, 1, 0x01, -2, -1, +0, -1, 1, 0x02,
299 -2, -1, +0, +0, 1, 0x03, -2, -1, +0, +1, 2, 0x01, -2, +0, +0, -1, 1, 0x06, -2, +0, +0, +0, 2, 0x02,
300 -2, +0, +0, +1, 1, 0x03, -2, +1, -1, +0, 1, 0x04, -2, +1, +0, -1, 2, 0x04, -2, +1, +0, +0, 1, 0x06,
301 -2, +1, +0, +1, 1, 0x02, -2, +2, +0, +0, 2, 0x04, -2, +2, +0, +1, 1, 0x04, -1, -2, -1, +0, 1, 0x80,
302 -1, -2, +0, -1, 1, 0x01, -1, -2, +1, -1, 1, 0x01, -1, -2, +1, +0, 2, 0x01, -1, -1, -1, +1, 1, 0x88,
303 -1, -1, +1, -2, 1, 0x40, -1, -1, +1, -1, 1, 0x22, -1, -1, +1, +0, 1, 0x33, -1, -1, +1, +1, 2, 0x11,
304 -1, +0, -1, +2, 1, 0x08, -1, +0, +0, -1, 1, 0x44, -1, +0, +0, +1, 1, 0x11, -1, +0, +1, -2, 2, 0x40,
305 -1, +0, +1, -1, 1, 0x66, -1, +0, +1, +0, 2, 0x22, -1, +0, +1, +1, 1, 0x33, -1, +0, +1, +2, 2, 0x10,
306 -1, +1, +1, -1, 2, 0x44, -1, +1, +1, +0, 1, 0x66, -1, +1, +1, +1, 1, 0x22, -1, +1, +1, +2, 1, 0x10,
307 -1, +2, +0, +1, 1, 0x04, -1, +2, +1, +0, 2, 0x04, -1, +2, +1, +1, 1, 0x04, +0, -2, +0, +0, 2, 0x80,
308 +0, -1, +0, +1, 2, 0x88, +0, -1, +1, -2, 1, 0x40, +0, -1, +1, +0, 1, 0x11, +0, -1, +2, -2, 1, 0x40,
309 +0, -1, +2, -1, 1, 0x20, +0, -1, +2, +0, 1, 0x30, +0, -1, +2, +1, 2, 0x10, +0, +0, +0, +2, 2, 0x08,
310 +0, +0, +2, -2, 2, 0x40, +0, +0, +2, -1, 1, 0x60, +0, +0, +2, +0, 2, 0x20, +0, +0, +2, +1, 1, 0x30,
311 +0, +0, +2, +2, 2, 0x10, +0, +1, +1, +0, 1, 0x44, +0, +1, +1, +2, 1, 0x10, +0, +1, +2, -1, 2, 0x40,
312 +0, +1, +2, +0, 1, 0x60, +0, +1, +2, +1, 1, 0x20, +0, +1, +2, +2, 1, 0x10, +1, -2, +1, +0, 1, 0x80,
313 +1, -1, +1, +1, 1, 0x88, +1, +0, +1, +2, 1, 0x08, +1, +0, +2, -1, 1, 0x40, +1, +0, +2, +1, 1, 0x10 };
314 static const signed char chood[]
315 = { -1, -1, -1, 0, -1, +1, 0, +1, +1, +1, +1, 0, +1, -1, 0, -1 };
317 const size_t ips_size = (size_t)prow * pcol * 352 *
sizeof(
int);
318 ips = malloc(ips_size);
324 for(
int col = 0; col < pcol; col++)
326 code[
row][col] = ip - ips;
327 const signed char *cp = terms;
328 for(
int t = 0;
t < 64;
t++)
330 const int y1 = *cp++, x1 = *cp++;
331 const int y2 = *cp++, x2 = *cp++;
333 const int grads = *cp++;
334 const int color =
fcol(
row + y1, col + x1, filters4, xtrans);
335 if(
fcol(
row + y2, col + x2, filters4, xtrans) != color)
continue;
337 = (
fcol(
row, col + 1, filters4, xtrans) == color &&
fcol(
row + 1, col, filters4, xtrans) == color)
340 if(abs(y1 - y2) == diag && abs(x1 - x2) == diag)
continue;
341 *ip++ = (y1 << 16) | (x1 & 0xffffu);
342 *ip++ = (y2 << 16) | (x2 & 0xffffu);
343 *ip++ = (color << 16) | (
weight & 0xffffu);
344 for(
int g = 0;
g < 8;
g++)
345 if(grads & 1 <<
g) *ip++ =
g;
350 for(
int g = 0;
g < 8;
g++)
352 const int y = *cp++,
x = *cp++;
353 *ip++ = (y << 16) | (
x & 0xffffu);
354 const int color =
fcol(
row, col, filters4, xtrans);
355 if(
fcol(
row + y, col +
x, filters4, xtrans) != color
356 &&
fcol(
row + y * 2, col +
x * 2, filters4, xtrans) == color)
358 *ip++ = (2*y << 16) | (2*
x & 0xffffu);
388 dev_in = dev_green_eq;
401 const int border = 1;
414 if(err != CL_SUCCESS)
goto error;
421 .cellsize = 1 *
sizeof(float), .overhead = 0,
422 .sizex = 1 << 8, .sizey = 1 << 8 };
428 size_t local[3] = { locopt.
sizex, locopt.
sizey, 1 };
436 sizeof(
float) * (locopt.
sizex + 2) * (locopt.
sizey + 2), NULL);
438 if(err != CL_SUCCESS)
goto error;
445 .cellsize = 4 *
sizeof(float), .overhead = 0,
446 .sizex = 1 << 8, .sizey = 1 << 8 };
452 size_t local[3] = { locopt.
sizex, locopt.
sizey, 1 };
466 if(err != CL_SUCCESS)
goto error;
471 const int border = 2;
484 if(err != CL_SUCCESS)
goto error;
490 size_t origin[] = { 0, 0, 0 };
493 if(err != CL_SUCCESS)
goto error;
501 if(err != CL_SUCCESS)
goto error;