Debian Codegraph @

converter/other/cameratopam/foveon.c

raw symbols SCIP
1 /* This code is licensed to the public by its copyright owners under GPL. */
2
3 #define _XOPEN_SOURCE 500 /* get M_PI in math.h */
4
5 #include <assert.h>
6 #include <string.h>
7 #include <float.h>
8 #include <math.h>
9
10 #include "mallocvar.h"
11 #include "pm.h"
12 #include "global_variables.h"
13 #include "decode.h"
14 #include "foveon.h"
15 #include "stdio_nofail.h"
16
17 #pragma GCC diagnostic push
18 #pragma GCC diagnostic ignored "-Wunused-const-variable"
19 #if HAVE_INT64
20 typedef int64_t INT64;
21 static bool const have64BitArithmetic = true;
22 #else
23 /* We define INT64 to something that lets the code compile, but we
24 should not execute any INT64 code, because it will get the wrong
25 result.
26 */
27 typedef int INT64;
28 static bool const have64BitArithmetic = false;
29 #endif
30 #pragma GCC diagnostic pop
31
32
33 #define FORC3 for (c=0; c < 3; c++)
34 #define FORC4 for (c=0; c < colors; c++)
35
36
37
38 static char *
39 foveon_gets(int const offset,
40 char * const str,
41 int const len) {
42
43 unsigned int i;
44 fseek_nofail (ifp, offset, SEEK_SET);
45 for (i=0; i < len-1; ++i) {
46 /* It certains seems wrong that we're reading a 16 bit integer
47 and assigning it to char, but that's what Dcraw does.
48 */
49 unsigned short val;
50 pm_readlittleshortu(ifp, &val);
51 str[i] = val;
52 if (str[i] == 0)
53 break;
54 }
55 str[i] = 0;
56 return str;
57 }
58
59
60
61 void
62 parse_foveon(FILE * const ifp) {
63 long fliplong;
64 long pos;
65 long magic;
66 long junk;
67 long entries;
68
69 fseek_nofail (ifp, 36, SEEK_SET);
70 pm_readlittlelong(ifp, &fliplong);
71 flip = fliplong;
72 fseek_nofail (ifp, -4, SEEK_END);
73 pm_readlittlelong(ifp, &pos);
74 fseek_nofail (ifp, pos, SEEK_SET);
75 pm_readlittlelong(ifp, &magic);
76 if (magic != 0x64434553)
77 return; /* SECd */
78 pm_readlittlelong(ifp, &junk);
79 pm_readlittlelong(ifp, &entries);
80 while (entries--) {
81 long off, len, tag;
82 long save;
83 long pent;
84 int i, poff[256][2];
85 char name[64];
86 long sec_;
87
88 pm_readlittlelong(ifp, &off);
89 pm_readlittlelong(ifp, &len);
90 pm_readlittlelong(ifp, &tag);
91
92 save = ftell_nofail(ifp);
93 fseek_nofail (ifp, off, SEEK_SET);
94 pm_readlittlelong(ifp, &sec_);
95 if (sec_ != (0x20434553 | (tag << 24))) return;
96 switch (tag) {
97 case 0x47414d49: /* IMAG */
98 if (data_offset)
99 break;
100 data_offset = off + 28;
101 fseek_nofail (ifp, 12, SEEK_CUR);
102 {
103 long wlong, hlong;
104 pm_readlittlelong(ifp, &wlong);
105 pm_readlittlelong(ifp, &hlong);
106 raw_width = wlong;
107 raw_height = hlong;
108 }
109 break;
110 case 0x464d4143: /* CAMF */
111 meta_offset = off + 24;
112 meta_length = len - 28;
113 if (meta_length > 0x20000)
114 meta_length = 0x20000;
115 break;
116 case 0x504f5250: /* PROP */
117 pm_readlittlelong(ifp, &junk);
118 pm_readlittlelong(ifp, &pent);
119 fseek_nofail (ifp, 12, SEEK_CUR);
120 off += pent*8 + 24;
121 if (pent > 256) pent=256;
122 for (i=0; i < pent*2; i++) {
123 long x;
124 pm_readlittlelong(ifp, &x);
125 poff[0][i] = off + x*2;
126 }
127 for (i=0; i < pent; i++) {
128 foveon_gets (poff[i][0], name, 64);
129 if (!strcmp (name, "CAMMANUF"))
130 foveon_gets (poff[i][1], make, 64);
131 if (!strcmp (name, "CAMMODEL"))
132 foveon_gets (poff[i][1], model, 64);
133 if (!strcmp (name, "WB_DESC"))
134 foveon_gets (poff[i][1], model2, 64);
135 if (!strcmp (name, "TIME"))
136 timestamp = atoi (foveon_gets (poff[i][1], name, 64));
137 }
138 }
139 fseek_nofail (ifp, save, SEEK_SET);
140 }
141 is_foveon = 1;
142 }
143
144
145
146 void
147 foveon_coeff(int * const useCoeffP,
148 float coeff[3][4]) {
149
150 static const float foveon[3][3] =
151 { { 1.4032, -0.2231, -0.1016 },
152 { -0.5263, 1.4816, 0.0170 },
153 { -0.0112, 0.0183, 0.9113 } };
154
155 int i, j;
156
157 for (i=0; i < 3; ++i)
158 for (j=0; j < 3; ++j)
159 coeff[i][j] = foveon[i][j];
160 *useCoeffP = 1;
161 }
162
163
164
165 static void
166 foveon_decoder (unsigned int const huff[1024],
167 unsigned int const code) {
168
169 struct decode *cur;
170 int len;
171 unsigned int code2;
172
173 cur = free_decode++;
174 if (free_decode > first_decode+2048) {
175 pm_error ("decoder table overflow");
176 }
177 if (code) {
178 unsigned int i;
179 for (i=0; i < 1024; i++) {
180 if (huff[i] == code) {
181 cur->leaf = i;
182 return;
183 }
184 }
185 }
186 if ((len = code >> 27) > 26)
187 return;
188 code2 = (len+1) << 27 | (code & 0x3ffffff) << 1;
189
190 cur->branch[0] = free_decode;
191 foveon_decoder (huff, code2);
192 cur->branch[1] = free_decode;
193 foveon_decoder (huff, code2+1);
194 }
195
196
197
198 static void
199 foveon_load_camf() {
200 unsigned int i, val;
201 long key;
202
203 fseek_nofail (ifp, meta_offset, SEEK_SET);
204 pm_readlittlelong(ifp, &key);
205 fread_nofail (meta_data, 1, meta_length, ifp);
206 for (i=0; i < meta_length; i++) {
207 key = (key * 1597 + 51749) % 244944;
208 assert(have64BitArithmetic);
209 val = key * (INT64) 301593171 >> 24;
210 meta_data[i] ^= ((((key << 8) - val) >> 1) + val) >> 17;
211 }
212 }
213
214
215
216 void
217 foveon_load_raw(Image const image) {
218 /*----------------------------------------------------------------------------
219 Caller must ensure that the system has 64 bit arithmetic (inasmuch as the
220 compile environment has type 'int64_t').
221 -----------------------------------------------------------------------------*/
222 struct decode *dindex;
223 short diff[1024], pred[3];
224 unsigned int huff[1024], bitbuf=0;
225 int row, col, bit=-1, c, i;
226
227 assert(have64BitArithmetic);
228
229 for (i=0; i < 1024; ++i)
230 pm_readlittleshort(ifp, &diff[i]);
231
232 for (i=0; i < 1024; ++i) {
233 long x;
234 pm_readlittlelong(ifp, &x);
235 huff[i] = x;
236 }
237 init_decoder();
238 foveon_decoder (huff, 0);
239
240 for (row=0; row < height; row++) {
241 long junk;
242 memset (pred, 0, sizeof pred);
243 if (!bit)
244 pm_readlittlelong(ifp, &junk);
245 for (col=bit=0; col < width; col++) {
246 FORC3 {
247 for (dindex=first_decode; dindex->branch[0]; ) {
248 if ((bit = (bit-1) & 31) == 31)
249 for (i=0; i < 4; i++)
250 bitbuf = (bitbuf << 8) + fgetc_nofail(ifp);
251 dindex = dindex->branch[bitbuf >> bit & 1];
252 }
253 pred[c] += diff[dindex->leaf];
254 }
255 FORC3 image[row*width+col][c] = pred[c];
256 }
257 }
258 foveon_load_camf();
259 maximum = clip_max = 0xffff;
260 }
261
262
263
264 static int
265 sget4(char const s[]) {
266 return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
267 }
268
269
270
271 static char *
272 foveon_camf_param (const char * const block,
273 const char * const param) {
274 unsigned idx, num;
275 char *pos, *cp, *dp;
276
277 for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
278 pos = meta_data + idx;
279 if (strncmp (pos, "CMb", 3)) break;
280 if (pos[3] != 'P') continue;
281 if (strcmp (block, pos+sget4(pos+12))) continue;
282 cp = pos + sget4(pos+16);
283 num = sget4(cp);
284 dp = pos + sget4(cp+4);
285 while (num--) {
286 cp += 8;
287 if (!strcmp (param, dp+sget4(cp)))
288 return dp+sget4(cp+4);
289 }
290 }
291 return NULL;
292 }
293
294
295
296 static void *
297 foveon_camf_matrix (int dim[3],
298 const char * const name) {
299
300 unsigned i, idx, type, ndim, size, *mat;
301 char *pos, *cp, *dp;
302
303 for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
304 pos = meta_data + idx;
305 if (strncmp (pos, "CMb", 3)) break;
306 if (pos[3] != 'M') continue;
307 if (strcmp (name, pos+sget4(pos+12))) continue;
308 dim[0] = dim[1] = dim[2] = 1;
309 cp = pos + sget4(pos+16);
310 type = sget4(cp);
311 if ((ndim = sget4(cp+4)) > 3) break;
312 dp = pos + sget4(cp+8);
313 for (i=ndim; i--; ) {
314 cp += 12;
315 dim[i] = sget4(cp);
316 }
317 if ((size = dim[0]*dim[1]*dim[2]) > meta_length/4) break;
318 MALLOCARRAY(mat, size);
319 if (mat == NULL)
320 pm_error("Unable to allocate memory for size=%d", size);
321 for (i=0; i < size; i++)
322 if (type && type != 6)
323 mat[i] = sget4(dp + i*4);
324 else
325 mat[i] = sget4(dp + i*2) & 0xffff;
326 return mat;
327 }
328 pm_message ("'%s' matrix not found!", name);
329 return NULL;
330 }
331
332
333
334 static int
335 foveon_fixed (void * const ptr,
336 int const size,
337 const char * const name) {
338 void *dp;
339 int dim[3];
340
341 dp = foveon_camf_matrix (dim, name);
342 if (!dp) return 0;
343 memcpy (ptr, dp, size*4);
344 free (dp);
345 return 1;
346 }
347
348
349
350 static float foveon_avg (unsigned short *pix, int range[2], float cfilt)
351 {
352 int i;
353 float val, min=FLT_MAX, max=-FLT_MAX, sum=0;
354
355 for (i=range[0]; i <= range[1]; i++) {
356 sum += val =
357 (short)pix[i*4] + ((short)pix[i*4]-(short)pix[(i-1)*4]) * cfilt;
358 if (min > val) min = val;
359 if (max < val) max = val;
360 }
361 return (sum - min - max) / (range[1] - range[0] - 1);
362 }
363
364
365
366 static short *foveon_make_curve (double max, double mul, double filt)
367 {
368 short *curve;
369 int size;
370 unsigned int i;
371 double x;
372
373 size = 4*M_PI*max / filt;
374 MALLOCARRAY(curve, size+1);
375 if (curve == NULL)
376 pm_error("Out of memory for %d-element curve array", size);
377
378 curve[0] = size;
379 for (i=0; i < size; ++i) {
380 x = i*filt/max/4;
381 curve[i+1] = (cos(x)+1)/2 * tanh(i*filt/mul) * mul + 0.5;
382 }
383 return curve;
384 }
385
386
387
388 static void foveon_make_curves
389 (short **curvep, float dq[3], float div[3], float filt)
390 {
391 double mul[3], max=0;
392 int c;
393
394 FORC3 mul[c] = dq[c]/div[c];
395 FORC3 if (max < mul[c]) max = mul[c];
396 FORC3 curvep[c] = foveon_make_curve (max, mul[c], filt);
397 }
398
399
400
401 static int foveon_apply_curve (short *curve, int i)
402 {
403 if (abs(i) >= (unsigned short)curve[0]) return 0;
404 return i < 0 ? -(unsigned short)curve[1-i] : (unsigned short)curve[1+i];
405 }
406
407
408
409 void
410 foveon_interpolate(Image const image,
411 float coeff[3][4]) {
412
413 static const short hood[] = {
414 -1,-1, -1,0, -1,1, 0,-1, 0,1, 1,-1, 1,0, 1,1 };
415 short *pix, prev[3], *curve[8], (*shrink)[3];
416 float cfilt=0.8, ddft[3][3][2], ppm[3][3][3];
417 float cam_xyz[3][3], correct[3][3], last[3][3], trans[3][3];
418 float chroma_dq[3], color_dq[3], diag[3][3], div[3];
419 float (*black)[3], (*sgain)[3], (*sgrow)[3];
420 float fsum[3], val, frow, num;
421 int row, col, c, i, j, diff, sgx, irow, sum, min, max, limit;
422 int dim[3], dscr[2][2], (*smrow[7])[3], total[4], ipix[3];
423 int work[3][3], smlast, smred, smred_p=0, dev[3];
424 int satlev[3], keep[4], active[4];
425 unsigned *badpix;
426 double dsum=0, trsum[3];
427 char str[128], *cp;
428
429 foveon_fixed (dscr, 4, "DarkShieldColRange");
430 foveon_fixed (ppm[0][0], 27, "PostPolyMatrix");
431 foveon_fixed (ddft[1][0], 12, "DarkDrift");
432 foveon_fixed (&cfilt, 1, "ColumnFilter");
433 foveon_fixed (satlev, 3, "SaturationLevel");
434 foveon_fixed (keep, 4, "KeepImageArea");
435 foveon_fixed (active, 4, "ActiveImageArea");
436 foveon_fixed (chroma_dq, 3, "ChromaDQ");
437 foveon_fixed (color_dq, 3,
438 foveon_camf_param ("IncludeBlocks", "ColorDQ") ?
439 "ColorDQ" : "ColorDQCamRGB");
440
441 if (!(cp = foveon_camf_param ("WhiteBalanceIlluminants", model2)))
442 { pm_message ( "Invalid white balance \"%s\"", model2);
443 return; }
444 foveon_fixed (cam_xyz, 9, cp);
445 foveon_fixed (correct, 9,
446 foveon_camf_param ("WhiteBalanceCorrections", model2));
447 memset (last, 0, sizeof last);
448 for (i=0; i < 3; i++)
449 for (j=0; j < 3; j++)
450 FORC3 last[i][j] += correct[i][c] * cam_xyz[c][j];
451
452 sprintf (str, "%sRGBNeutral", model2);
453 if (foveon_camf_param ("IncludeBlocks", str))
454 foveon_fixed (div, 3, str);
455 else {
456 #define LAST(x,y) last[(i+x)%3][(c+y)%3]
457 for (i=0; i < 3; i++)
458 FORC3 diag[c][i] = LAST(1,1)*LAST(2,2) - LAST(1,2)*LAST(2,1);
459 #undef LAST
460 FORC3 div[c] =
461 diag[c][0]*0.3127 + diag[c][1]*0.329 + diag[c][2]*0.3583;
462 }
463 num = 0;
464 FORC3 if (num < div[c]) num = div[c];
465 FORC3 div[c] /= num;
466
467 memset (trans, 0, sizeof trans);
468 for (i=0; i < 3; i++)
469 for (j=0; j < 3; j++)
470 FORC3 trans[i][j] += coeff[i][c] * last[c][j] * div[j];
471 FORC3 trsum[c] = trans[c][0] + trans[c][1] + trans[c][2];
472 dsum = (6*trsum[0] + 11*trsum[1] + 3*trsum[2]) / 20;
473 for (i=0; i < 3; i++)
474 FORC3 last[i][c] = trans[i][c] * dsum / trsum[i];
475 memset (trans, 0, sizeof trans);
476 for (i=0; i < 3; i++)
477 for (j=0; j < 3; j++)
478 FORC3 trans[i][j] += (i==c ? 32 : -1) * last[c][j] / 30;
479
480 foveon_make_curves (curve, color_dq, div, cfilt);
481 FORC3 chroma_dq[c] /= 3;
482 foveon_make_curves (curve+3, chroma_dq, div, cfilt);
483 FORC3 dsum += chroma_dq[c] / div[c];
484 curve[6] = foveon_make_curve (dsum, dsum, cfilt);
485 curve[7] = foveon_make_curve (dsum*2, dsum*2, cfilt);
486
487 sgain = foveon_camf_matrix (dim, "SpatialGain");
488 if (!sgain) return;
489 sgrow = calloc (dim[1], sizeof *sgrow);
490 sgx = (width + dim[1]-2) / (dim[1]-1);
491
492 black = calloc (height, sizeof(black[0]));
493 for (row=0; row < height; ++row) {
494 unsigned int i;
495 for (i=0; i < 3; ++i) {
496 unsigned int j;
497 for (j = 0; j < 2; ++j)
498 ddft[0][i][j] = ddft[1][i][j] +
499 row / (height-1.0) * (ddft[2][i][j] - ddft[1][i][j]);
500 }
501 FORC3 black[row][c] =
502 ( foveon_avg (image[row*width]+c, dscr[0], cfilt) +
503 foveon_avg (image[row*width]+c, dscr[1], cfilt) * 3
504 - ddft[0][c][0] ) / 4 - ddft[0][c][1];
505 }
506 memcpy (black, black+8, 8 * sizeof(black[0]));
507 memcpy (black+height-11, black+height-22, 11*(sizeof black[0]));
508 memcpy (last, black, sizeof last);
509
510 for (row=1; row < height-1; row++) {
511 FORC3 if (last[1][c] > last[0][c]) {
512 if (last[1][c] > last[2][c])
513 black[row][c] =
514 (last[0][c] > last[2][c]) ? last[0][c]:last[2][c];
515 } else
516 if (last[1][c] < last[2][c])
517 black[row][c] =
518 (last[0][c] < last[2][c]) ? last[0][c]:last[2][c];
519 memmove (last, last+1, 2*sizeof last[0]);
520 memcpy (last[2], black[row+1], sizeof last[2]);
521 }
522 FORC3 black[row][c] = (last[0][c] + last[1][c])/2;
523 FORC3 black[0][c] = (black[1][c] + black[3][c])/2;
524
525 val = 1 - exp(-1/24.0);
526 memcpy (fsum, black, sizeof fsum);
527 for (row=1; row < height; row++)
528 FORC3 fsum[c] += black[row][c] =
529 (black[row][c] - black[row-1][c])*val + black[row-1][c];
530 memcpy (last[0], black[height-1], sizeof last[0]);
531 FORC3 fsum[c] /= height;
532 for (row = height; row--; )
533 FORC3 last[0][c] = black[row][c] =
534 (black[row][c] - fsum[c] - last[0][c])*val + last[0][c];
535
536 memset (total, 0, sizeof total);
537 for (row=2; row < height; row+=4)
538 for (col=2; col < width; col+=4) {
539 FORC3 total[c] += (short) image[row*width+col][c];
540 total[3]++;
541 }
542 for (row=0; row < height; row++)
543 FORC3 black[row][c] += fsum[c]/2 + total[c]/(total[3]*100.0);
544
545 for (row=0; row < height; row++) {
546 unsigned int i;
547 for (i = 0; i < 3; ++i) {
548 unsigned int j;
549 for (j = 0; j < 2; ++j)
550 ddft[0][i][j] = ddft[1][i][j] +
551 row / (height-1.0) * (ddft[2][i][j] - ddft[1][i][j]);
552 }
553 pix = (short*)image[row*width];
554 memcpy (prev, pix, sizeof prev);
555 frow = row / (height-1.0) * (dim[2]-1);
556 if ((irow = frow) == dim[2]-1) irow--;
557 frow -= irow;
558 for (i=0; i < dim[1]; i++)
559 FORC3 sgrow[i][c] = sgain[ irow *dim[1]+i][c] * (1-frow) +
560 sgain[(irow+1)*dim[1]+i][c] * frow;
561 for (col=0; col < width; col++) {
562 FORC3 {
563 diff = pix[c] - prev[c];
564 prev[c] = pix[c];
565 ipix[c] = pix[c] + floor ((diff + (diff*diff >> 14)) * cfilt
566 - ddft[0][c][1]
567 - ddft[0][c][0]
568 * ((float) col/width - 0.5)
569 - black[row][c] );
570 }
571 FORC3 {
572 work[0][c] = ipix[c] * ipix[c] >> 14;
573 work[2][c] = ipix[c] * work[0][c] >> 14;
574 work[1][2-c] = ipix[(c+1) % 3] * ipix[(c+2) % 3] >> 14;
575 }
576 FORC3 {
577 for (val=i=0; i < 3; i++)
578 for ( j=0; j < 3; j++)
579 val += ppm[c][i][j] * work[i][j];
580 ipix[c] = floor ((ipix[c] + floor(val)) *
581 ( sgrow[col/sgx ][c] * (sgx - col%sgx) +
582 sgrow[col/sgx+1][c] * (col%sgx) ) / sgx /
583 div[c]);
584 if (ipix[c] > 32000) ipix[c] = 32000;
585 pix[c] = ipix[c];
586 }
587 pix += 4;
588 }
589 }
590 free (black);
591 free (sgrow);
592 free (sgain);
593
594 if ((badpix = foveon_camf_matrix (dim, "BadPixels"))) {
595 for (i=0; i < dim[0]; i++) {
596 col = (badpix[i] >> 8 & 0xfff) - keep[0];
597 row = (badpix[i] >> 20 ) - keep[1];
598 if ((unsigned)(row-1) > height-3 || (unsigned)(col-1) > width-3)
599 continue;
600 memset (fsum, 0, sizeof fsum);
601 for (sum=j=0; j < 8; j++)
602 if (badpix[i] & (1 << j)) {
603 FORC3 fsum[c] +=
604 image[(row+hood[j*2])*width+col+hood[j*2+1]][c];
605 sum++;
606 }
607 if (sum) FORC3 image[row*width+col][c] = fsum[c]/sum;
608 }
609 free (badpix);
610 }
611
612 /* Array for 5x5 Gaussian averaging of red values */
613 smrow[6] = calloc (width*5, sizeof **smrow);
614 if (smrow[6] == NULL)
615 pm_error("Out of memory");
616 for (i=0; i < 5; i++)
617 smrow[i] = smrow[6] + i*width;
618
619 /* Sharpen the reds against these Gaussian averages */
620 for (smlast=-1, row=2; row < height-2; row++) {
621 while (smlast < row+2) {
622 for (i=0; i < 6; i++)
623 smrow[(i+5) % 6] = smrow[i];
624 pix = (short*)image[++smlast*width+2];
625 for (col=2; col < width-2; col++) {
626 smrow[4][col][0] =
627 (pix[0]*6 + (pix[-4]+pix[4])*4 + pix[-8]+pix[8] + 8) >> 4;
628 pix += 4;
629 }
630 }
631 pix = (short*)image[row*width+2];
632 for (col=2; col < width-2; col++) {
633 smred = ( 6 * smrow[2][col][0]
634 + 4 * (smrow[1][col][0] + smrow[3][col][0])
635 + smrow[0][col][0] + smrow[4][col][0] + 8 ) >> 4;
636 if (col == 2)
637 smred_p = smred;
638 i = pix[0] + ((pix[0] - ((smred*7 + smred_p) >> 3)) >> 3);
639 if (i > 32000) i = 32000;
640 pix[0] = i;
641 smred_p = smred;
642 pix += 4;
643 }
644 }
645
646 /* Adjust the brighter pixels for better linearity */
647 FORC3 {
648 i = satlev[c] / div[c];
649 if (maximum > i) maximum = i;
650 }
651 clip_max = maximum;
652 limit = maximum * 9 >> 4;
653 for (pix=(short*)image[0]; pix < (short *) image[height*width]; pix+=4) {
654 if (pix[0] <= limit || pix[1] <= limit || pix[2] <= limit)
655 continue;
656 min = max = pix[0];
657 for (c=1; c < 3; c++) {
658 if (min > pix[c]) min = pix[c];
659 if (max < pix[c]) max = pix[c];
660 }
661 i = 0x4000 - ((min - limit) << 14) / limit;
662 i = 0x4000 - (i*i >> 14);
663 i = i*i >> 14;
664 FORC3 pix[c] += (max - pix[c]) * i >> 14;
665 }
666 /*
667 Because photons that miss one detector often hit another,
668 the sum R+G+B is much less noisy than the individual colors.
669 So smooth the hues without smoothing the total.
670 */
671 for (smlast=-1, row=2; row < height-2; row++) {
672 while (smlast < row+2) {
673 for (i=0; i < 6; i++)
674 smrow[(i+5) % 6] = smrow[i];
675 pix = (short*)image[++smlast*width+2];
676 for (col=2; col < width-2; col++) {
677 FORC3 smrow[4][col][c] = (pix[c-4]+2*pix[c]+pix[c+4]+2) >> 2;
678 pix += 4;
679 }
680 }
681 pix = (short*)image[row*width+2];
682 for (col=2; col < width-2; col++) {
683 FORC3 dev[c] = -foveon_apply_curve (curve[7], pix[c] -
684 ((smrow[1][col][c] +
685 2*smrow[2][col][c] +
686 smrow[3][col][c]) >> 2));
687 sum = (dev[0] + dev[1] + dev[2]) >> 3;
688 FORC3 pix[c] += dev[c] - sum;
689 pix += 4;
690 }
691 }
692 for (smlast=-1, row=2; row < height-2; row++) {
693 while (smlast < row+2) {
694 for (i=0; i < 6; i++)
695 smrow[(i+5) % 6] = smrow[i];
696 pix = (short*)image[++smlast*width+2];
697 for (col=2; col < width-2; col++) {
698 FORC3 smrow[4][col][c] =
699 (pix[c-8]+pix[c-4]+pix[c]+pix[c+4]+pix[c+8]+2) >> 2;
700 pix += 4;
701 }
702 }
703 pix = (short*)image[row*width+2];
704 for (col=2; col < width-2; col++) {
705 for (total[3]=375, sum=60, c=0; c < 3; c++) {
706 for (total[c]=i=0; i < 5; i++)
707 total[c] += smrow[i][col][c];
708 total[3] += total[c];
709 sum += pix[c];
710 }
711 if (sum < 0) sum = 0;
712 j = total[3] > 375 ? (sum << 16) / total[3] : sum * 174;
713 FORC3 pix[c] += foveon_apply_curve (curve[6],
714 ((j*total[c] + 0x8000) >> 16)
715 - pix[c]);
716 pix += 4;
717 }
718 }
719
720 /* Transform the image to a different colorspace */
721 for (pix=(short*)image[0]; pix < (short *) image[height*width]; pix+=4) {
722 FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]);
723 sum = (pix[0]+pix[1]+pix[1]+pix[2]) >> 2;
724 FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]-sum);
725 FORC3 {
726 for (dsum=i=0; i < 3; i++)
727 dsum += trans[c][i] * pix[i];
728 if (dsum < 0) dsum = 0;
729 if (dsum > 24000) dsum = 24000;
730 ipix[c] = dsum + 0.5;
731 }
732 FORC3 pix[c] = ipix[c];
733 }
734
735 /* Smooth the image bottom-to-top and save at 1/4 scale */
736 MALLOCARRAY(shrink, (width/4) * (height/4));
737 if (shrink == NULL)
738 pm_error("Out of memory allocating 1/4 scale array");
739
740 for (row = height/4; row > 0; --row) {
741 for (col=0; col < width/4; ++col) {
742 ipix[0] = ipix[1] = ipix[2] = 0;
743 for (i=0; i < 4; i++)
744 for (j=0; j < 4; j++)
745 FORC3 ipix[c] += image[(row*4+i)*width+col*4+j][c];
746 FORC3
747 if (row+2 > height/4)
748 shrink[row*(width/4)+col][c] = ipix[c] >> 4;
749 else
750 shrink[row*(width/4)+col][c] =
751 (shrink[(row+1)*(width/4)+col][c]*1840 +
752 ipix[c]*141 + 2048) >> 12;
753 }
754 }
755 /* From the 1/4-scale image, smooth right-to-left */
756 for (row=0; row < (height & ~3); ++row) {
757 ipix[0] = ipix[1] = ipix[2] = 0;
758 if ((row & 3) == 0)
759 for (col = width & ~3 ; col--; )
760 FORC3 smrow[0][col][c] = ipix[c] =
761 (shrink[(row/4)*(width/4)+col/4][c]*1485 +
762 ipix[c]*6707 + 4096) >> 13;
763
764 /* Then smooth left-to-right */
765 ipix[0] = ipix[1] = ipix[2] = 0;
766 for (col=0; col < (width & ~3); col++)
767 FORC3 smrow[1][col][c] = ipix[c] =
768 (smrow[0][col][c]*1485 + ipix[c]*6707 + 4096) >> 13;
769
770 /* Smooth top-to-bottom */
771 if (row == 0)
772 memcpy (smrow[2], smrow[1], sizeof **smrow * width);
773 else
774 for (col=0; col < (width & ~3); col++)
775 FORC3 smrow[2][col][c] =
776 (smrow[2][col][c]*6707 + smrow[1][col][c]*1485 + 4096)
777 >> 13;
778
779 /* Adjust the chroma toward the smooth values */
780 for (col=0; col < (width & ~3); col++) {
781 for (i=j=30, c=0; c < 3; c++) {
782 i += smrow[2][col][c];
783 j += image[row*width+col][c];
784 }
785 j = (j << 16) / i;
786 for (sum=c=0; c < 3; c++) {
787 ipix[c] = foveon_apply_curve(
788 curve[c+3],
789 ((smrow[2][col][c] * j + 0x8000) >> 16) -
790 image[row*width+col][c]);
791 sum += ipix[c];
792 }
793 sum >>= 3;
794 FORC3 {
795 i = image[row*width+col][c] + ipix[c] - sum;
796 if (i < 0) i = 0;
797 image[row*width+col][c] = i;
798 }
799 }
800 }
801 free (shrink);
802 free (smrow[6]);
803 for (i=0; i < 8; i++)
804 free (curve[i]);
805
806 /* Trim off the black border */
807 active[1] -= keep[1];
808 active[3] -= 2;
809 i = active[2] - active[0];
810 for (row = 0; row < active[3]-active[1]; row++)
811 memcpy (image[row*i], image[(row+active[1])*width+active[0]],
812 i * sizeof *image);
813 width = i;
814 height = row;
815 }
816
817
818