r870: Merge 2.1:
[cinelerra_cv.git] / cinelerra / dcraw.c
blob3b70e6b90f92313c7dacde56be9f1fcbfd4e6cb5
1 /*
2 dcraw.c -- Dave Coffin's raw photo decoder
3 Copyright 1997-2006 by Dave Coffin, dcoffin a cybercom o net
5 This is a command-line ANSI C program to convert raw photos from
6 any digital camera on any computer running any operating system.
8 Attention! Some parts of this program are restricted under the
9 terms of the GNU General Public License. Such code is enclosed
10 in "BEGIN GPL BLOCK" and "END GPL BLOCK" declarations.
11 Any code not declared GPL is free for all uses.
13 Starting in Revision 1.237, the code to support Foveon cameras
14 is under GPL.
16 To lawfully redistribute dcraw.c, you must either (a) include
17 full source code for all executable files containing restricted
18 functions, (b) remove these functions, re-implement them, or
19 copy them from an earlier, non-GPL Revision of dcraw.c, or (c)
20 purchase a license from the author.
22 $Revision: 1.321 $
23 $Date: 2006/03/31 21:54:29 $
26 #define _GNU_SOURCE
27 #define _USE_MATH_DEFINES
28 #include <ctype.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <float.h>
32 #include <limits.h>
33 #include <math.h>
34 #include <setjmp.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
40 NO_JPEG disables decoding of compressed Kodak DC120 files.
41 NO_LCMS disables the "-p" option.
44 // CINELERRA
45 #define NO_LCMS
47 #ifndef NO_JPEG
48 #include <jpeglib.h>
49 #endif
50 #ifndef NO_LCMS
51 #include <lcms.h>
52 #endif
54 #ifdef __CYGWIN__
55 #include <io.h>
56 #endif
57 #ifdef WIN32
58 #include <sys/utime.h>
59 #include <winsock2.h>
60 #pragma comment(lib, "ws2_32.lib")
61 #define strcasecmp stricmp
62 typedef __int64 INT64;
63 typedef unsigned __int64 UINT64;
64 #else
65 #include <unistd.h>
66 #include <utime.h>
67 #include <netinet/in.h>
68 typedef long long INT64;
69 typedef unsigned long long UINT64;
70 #endif
72 #ifdef LJPEG_DECODE
73 #error Please compile dcraw.c by itself.
74 #error Do not link it with ljpeg_decode.
75 #endif
77 #ifndef LONG_BIT
78 #define LONG_BIT (8 * sizeof (long))
79 #endif
81 #define ushort UshORt
82 typedef unsigned char uchar;
83 typedef unsigned short ushort;
86 All global variables are defined here, and all functions that
87 access them are prefixed with "CLASS". Note that a thread-safe
88 C++ class cannot have non-const static local variables.
93 // CINELERRA
94 char dcraw_info[1024];
95 float **dcraw_data;
96 int dcraw_alpha;
97 float dcraw_matrix[9];
100 FILE *ifp;
101 short order;
102 char *ifname, make[64], model[72], model2[64], *meta_data, cdesc[5];
103 float flash_used, canon_ev, iso_speed, shutter, aperture, focal_len;
104 time_t timestamp;
105 unsigned shot_order, kodak_cbpp, filters;
106 int profile_offset, profile_length;
107 int thumb_offset, thumb_length, thumb_width, thumb_height, thumb_misc;
108 int data_offset, strip_offset, curve_offset, meta_offset, meta_length;
109 int tiff_nifds, tiff_flip, tiff_bps, tiff_compress;
110 int raw_height, raw_width, top_margin, left_margin;
111 int height, width, fuji_width, colors, tiff_samples;
112 int black, maximum, clip_max, raw_color, use_gamma;
113 int iheight, iwidth, shrink, flip, xmag, ymag;
114 int zero_after_ff, is_raw, dng_version, is_foveon;
115 ushort (*image)[4], white[8][8], curve[0x1000], cr2_slice[3];
116 float bright=1, red_scale=1, blue_scale=1, sigma_d=0, sigma_r=0;
117 int four_color_rgb=0, document_mode=0, clip_color=1;
118 int verbose=0, use_auto_wb=0, use_camera_wb=0, output_color=1;
119 int fuji_layout, fuji_secondary, use_secondary=0;
120 float cam_mul[4], pre_mul[4], rgb_cam[3][4]; /* RGB from camera color */
121 const double xyz_rgb[3][3] = { /* XYZ from RGB */
122 { 0.412453, 0.357580, 0.180423 },
123 { 0.212671, 0.715160, 0.072169 },
124 { 0.019334, 0.119193, 0.950227 } };
125 const float d65_white[3] = { 0.950456, 1, 1.088754 };
126 #define camera_red cam_mul[0]
127 #define camera_blue cam_mul[2]
128 int histogram[4][0x2000];
129 void write_ppm(FILE *);
130 void (*write_thumb)(FILE *), (*write_fun)(FILE *);
131 void (*load_raw)(), (*thumb_load_raw)();
132 jmp_buf failure;
134 struct decode {
135 struct decode *branch[2];
136 int leaf;
137 } first_decode[2048], *second_decode, *free_decode;
139 struct {
140 int width, height, bps, comp, phint, offset, flip, samples, bytes;
141 } tiff_ifd[10];
143 #define CLASS
145 #define FORC3 for (c=0; c < 3; c++)
146 #define FORC4 for (c=0; c < 4; c++)
147 #define FORCC for (c=0; c < colors; c++)
149 #define SQR(x) ((x)*(x))
150 #define ABS(x) (((int)(x) ^ ((int)(x) >> 31)) - ((int)(x) >> 31))
151 #define MIN(a,b) ((a) < (b) ? (a) : (b))
152 #define MAX(a,b) ((a) > (b) ? (a) : (b))
153 #define LIM(x,min,max) MAX(min,MIN(x,max))
154 #define ULIM(x,y,z) ((y) < (z) ? LIM(x,y,z) : LIM(x,z,y))
155 #define CLIP(x) LIM(x,0,clip_max)
156 #define SWAP(a,b) { a ^= b; a ^= (b ^= a); }
159 In order to inline this calculation, I make the risky
160 assumption that all filter patterns can be described
161 by a repeating pattern of eight rows and two columns
163 Return values are either 0/1/2/3 = G/M/C/Y or 0/1/2/3 = R/G1/B/G2
165 #define FC(row,col) \
166 (filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
168 #define BAYER(row,col) \
169 image[((row) >> shrink)*iwidth + ((col) >> shrink)][FC(row,col)]
172 PowerShot 600 PowerShot A50 PowerShot Pro70 Pro90 & G1
173 0xe1e4e1e4: 0x1b4e4b1e: 0x1e4b4e1b: 0xb4b4b4b4:
175 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
176 0 G M G M G M 0 C Y C Y C Y 0 Y C Y C Y C 0 G M G M G M
177 1 C Y C Y C Y 1 M G M G M G 1 M G M G M G 1 Y C Y C Y C
178 2 M G M G M G 2 Y C Y C Y C 2 C Y C Y C Y
179 3 C Y C Y C Y 3 G M G M G M 3 G M G M G M
180 4 C Y C Y C Y 4 Y C Y C Y C
181 PowerShot A5 5 G M G M G M 5 G M G M G M
182 0x1e4e1e4e: 6 Y C Y C Y C 6 C Y C Y C Y
183 7 M G M G M G 7 M G M G M G
184 0 1 2 3 4 5
185 0 C Y C Y C Y
186 1 G M G M G M
187 2 C Y C Y C Y
188 3 M G M G M G
190 All RGB cameras use one of these Bayer grids:
192 0x16161616: 0x61616161: 0x49494949: 0x94949494:
194 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
195 0 B G B G B G 0 G R G R G R 0 G B G B G B 0 R G R G R G
196 1 G R G R G R 1 B G B G B G 1 R G R G R G 1 G B G B G B
197 2 B G B G B G 2 G R G R G R 2 G B G B G B 2 R G R G R G
198 3 G R G R G R 3 B G B G B G 3 R G R G R G 3 G B G B G B
201 #ifndef __GLIBC__
202 char *my_memmem (char *haystack, size_t haystacklen,
203 char *needle, size_t needlelen)
205 char *c;
206 for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
207 if (!memcmp (c, needle, needlelen))
208 return c;
209 return NULL;
211 #define memmem my_memmem
212 #endif
214 void CLASS merror (void *ptr, char *where)
216 if (ptr) return;
217 fprintf (stderr, "%s: Out of memory in %s\n", ifname, where);
218 longjmp (failure, 1);
221 ushort CLASS sget2 (uchar *s)
223 if (order == 0x4949) /* "II" means little-endian */
224 return s[0] | s[1] << 8;
225 else /* "MM" means big-endian */
226 return s[0] << 8 | s[1];
229 ushort CLASS get2()
231 uchar str[2] = { 0xff,0xff };
232 fread (str, 1, 2, ifp);
233 return sget2(str);
236 int CLASS sget4 (uchar *s)
238 if (order == 0x4949)
239 return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
240 else
241 return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
243 #define sget4(s) sget4((uchar *)s)
245 int CLASS get4()
247 uchar str[4] = { 0xff,0xff,0xff,0xff };
248 fread (str, 1, 4, ifp);
249 return sget4(str);
252 int CLASS getint (int type)
254 return type == 3 ? get2() : get4();
257 float CLASS int_to_float (int i)
259 union { int i; float f; } u;
260 u.i = i;
261 return u.f;
264 double CLASS getreal (int type)
266 double num;
267 switch (type) {
268 case 3: return (unsigned short) get2();
269 case 4: return (unsigned int) get4();
270 case 5: num = (unsigned int) get4();
271 return num / (unsigned int) get4();
272 case 8: return (signed short) get2();
273 case 9: return (signed int) get4();
274 case 10: num = (signed int) get4();
275 return num / (signed int) get4();
276 case 11: return int_to_float (get4());
277 case 12:
278 fprintf (stderr, "%s: TIFF doubles not supported!\n", ifname);
279 longjmp (failure, 4);
280 default: return fgetc(ifp);
283 #define getrat() getreal(10)
285 void CLASS read_shorts (ushort *pixel, int count)
287 fread (pixel, 2, count, ifp);
288 if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
289 swab (pixel, pixel, count*2);
292 void CLASS canon_600_fixed_wb (int temp)
294 static const short mul[4][5] = {
295 { 667, 358,397,565,452 },
296 { 731, 390,367,499,517 },
297 { 1119, 396,348,448,537 },
298 { 1399, 485,431,508,688 } };
299 int lo, hi, i;
300 float frac=0;
302 for (lo=4; --lo; )
303 if (*mul[lo] <= temp) break;
304 for (hi=0; hi < 3; hi++)
305 if (*mul[hi] >= temp) break;
306 if (lo != hi)
307 frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
308 for (i=1; i < 5; i++)
309 pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
312 /* Return values: 0 = white 1 = near white 2 = not white */
313 int CLASS canon_600_color (int ratio[2], int mar)
315 int clipped=0, target, miss;
317 if (flash_used) {
318 if (ratio[1] < -104)
319 { ratio[1] = -104; clipped = 1; }
320 if (ratio[1] > 12)
321 { ratio[1] = 12; clipped = 1; }
322 } else {
323 if (ratio[1] < -264 || ratio[1] > 461) return 2;
324 if (ratio[1] < -50)
325 { ratio[1] = -50; clipped = 1; }
326 if (ratio[1] > 307)
327 { ratio[1] = 307; clipped = 1; }
329 target = flash_used || ratio[1] < 197
330 ? -38 - (398 * ratio[1] >> 10)
331 : -123 + (48 * ratio[1] >> 10);
332 if (target - mar <= ratio[0] &&
333 target + 20 >= ratio[0] && !clipped) return 0;
334 miss = target - ratio[0];
335 if (abs(miss) >= mar*4) return 2;
336 if (miss < -20) miss = -20;
337 if (miss > mar) miss = mar;
338 ratio[0] = target - miss;
339 return 1;
342 void CLASS canon_600_auto_wb()
344 int mar, row, col, i, j, st, count[] = { 0,0 };
345 int test[8], total[2][8], ratio[2][2], stat[2];
347 memset (&total, 0, sizeof total);
348 i = canon_ev + 0.5;
349 if (i < 10) mar = 150;
350 else if (i > 12) mar = 20;
351 else mar = 280 - 20 * i;
352 if (flash_used) mar = 80;
353 for (row=14; row < height-14; row+=4)
354 for (col=10; col < width; col+=2) {
355 for (i=0; i < 8; i++)
356 test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
357 BAYER(row+(i >> 1),col+(i & 1));
358 for (i=0; i < 8; i++)
359 if (test[i] < 150 || test[i] > 1500) goto next;
360 for (i=0; i < 4; i++)
361 if (abs(test[i] - test[i+4]) > 50) goto next;
362 for (i=0; i < 2; i++) {
363 for (j=0; j < 4; j+=2)
364 ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
365 stat[i] = canon_600_color (ratio[i], mar);
367 if ((st = stat[0] | stat[1]) > 1) goto next;
368 for (i=0; i < 2; i++)
369 if (stat[i])
370 for (j=0; j < 2; j++)
371 test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
372 for (i=0; i < 8; i++)
373 total[st][i] += test[i];
374 count[st]++;
375 next: continue;
377 if (count[0] | count[1]) {
378 st = count[0]*200 < count[1];
379 for (i=0; i < 4; i++)
380 pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
384 void CLASS canon_600_coeff()
386 static const short table[6][12] = {
387 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
388 { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
389 { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
390 { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
391 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
392 { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
393 int t=0, i, c;
394 float mc, yc;
396 mc = pre_mul[1] / pre_mul[2];
397 yc = pre_mul[3] / pre_mul[2];
398 if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
399 if (mc > 1.28 && mc <= 2) {
400 if (yc < 0.8789) t=3;
401 else if (yc <= 2) t=4;
403 if (flash_used) t=5;
404 for (raw_color = i=0; i < 3; i++)
405 FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
408 void CLASS canon_600_load_raw()
410 uchar data[1120], *dp;
411 ushort pixel[896], *pix;
412 int irow, row, col, val;
413 static const short mul[4][2] =
414 { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
416 for (irow=row=0; irow < height; irow++)
418 fread (data, 1120, 1, ifp);
419 for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8)
421 pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
422 pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
423 pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
424 pix[3] = (dp[4] << 2) + (dp[1] & 3);
425 pix[4] = (dp[5] << 2) + (dp[9] & 3);
426 pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
427 pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
428 pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
430 for (col=0; col < width; col++)
431 BAYER(row,col) = pixel[col];
432 for (col=width; col < 896; col++)
433 black += pixel[col];
434 if ((row+=2) > height) row = 1;
436 black = black / ((896 - width) * height) - 4;
437 for (row=0; row < height; row++)
438 for (col=0; col < width; col++) {
439 val = (BAYER(row,col) - black) * mul[row & 3][col & 1] >> 9;
440 if (val < 0) val = 0;
441 BAYER(row,col) = val;
443 canon_600_fixed_wb(1311);
444 canon_600_auto_wb();
445 canon_600_coeff();
446 maximum = (0x3ff - black) * 1109 >> 9;
447 black = 0;
450 void CLASS canon_a5_load_raw()
452 uchar data[1940], *dp;
453 ushort pixel[1552], *pix;
454 int row, col;
456 for (row=0; row < height; row++) {
457 fread (data, raw_width * 10 / 8, 1, ifp);
458 for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=10, pix+=8)
460 pix[0] = (dp[1] << 2) + (dp[0] >> 6);
461 pix[1] = (dp[0] << 4) + (dp[3] >> 4);
462 pix[2] = (dp[3] << 6) + (dp[2] >> 2);
463 pix[3] = (dp[2] << 8) + (dp[5] );
464 pix[4] = (dp[4] << 2) + (dp[7] >> 6);
465 pix[5] = (dp[7] << 4) + (dp[6] >> 4);
466 pix[6] = (dp[6] << 6) + (dp[9] >> 2);
467 pix[7] = (dp[9] << 8) + (dp[8] );
469 for (col=0; col < width; col++)
470 BAYER(row,col) = (pixel[col] & 0x3ff);
471 for (col=width; col < raw_width; col++)
472 black += pixel[col] & 0x3ff;
474 if (raw_width > width)
475 black /= (raw_width - width) * height;
476 maximum = 0x3ff;
480 getbits(-1) initializes the buffer
481 getbits(n) where 0 <= n <= 25 returns an n-bit integer
483 unsigned CLASS getbits (int nbits)
485 static unsigned bitbuf=0;
486 static int vbits=0, reset=0;
487 unsigned c;
489 if (nbits == -1)
490 return bitbuf = vbits = reset = 0;
491 if (nbits == 0 || reset) return 0;
492 while (vbits < nbits) {
493 c = fgetc(ifp);
494 if ((reset = zero_after_ff && c == 0xff && fgetc(ifp))) return 0;
495 bitbuf = (bitbuf << 8) + c;
496 vbits += 8;
498 vbits -= nbits;
499 return bitbuf << (32-nbits-vbits) >> (32-nbits);
502 void CLASS init_decoder()
504 memset (first_decode, 0, sizeof first_decode);
505 free_decode = first_decode;
509 Construct a decode tree according the specification in *source.
510 The first 16 bytes specify how many codes should be 1-bit, 2-bit
511 3-bit, etc. Bytes after that are the leaf values.
513 For example, if the source is
515 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
516 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
518 then the code is
520 00 0x04
521 010 0x03
522 011 0x05
523 100 0x06
524 101 0x02
525 1100 0x07
526 1101 0x01
527 11100 0x08
528 11101 0x09
529 11110 0x00
530 111110 0x0a
531 1111110 0x0b
532 1111111 0xff
534 uchar * CLASS make_decoder (const uchar *source, int level)
536 struct decode *cur;
537 static int leaf;
538 int i, next;
540 if (level==0) leaf=0;
541 cur = free_decode++;
542 if (free_decode > first_decode+2048) {
543 fprintf (stderr, "%s: decoder table overflow\n", ifname);
544 longjmp (failure, 2);
546 for (i=next=0; i <= leaf && next < 16; )
547 i += source[next++];
548 if (i > leaf) {
549 if (level < next) {
550 cur->branch[0] = free_decode;
551 make_decoder (source, level+1);
552 cur->branch[1] = free_decode;
553 make_decoder (source, level+1);
554 } else
555 cur->leaf = source[16 + leaf++];
557 return (uchar *) source + 16 + leaf;
560 void CLASS crw_init_tables (unsigned table)
562 static const uchar first_tree[3][29] = {
563 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
564 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
565 { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
566 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
567 { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
568 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
570 static const uchar second_tree[3][180] = {
571 { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
572 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
573 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
574 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
575 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
576 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
577 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
578 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
579 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
580 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
581 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
582 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
583 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
584 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
585 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
586 { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
587 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
588 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
589 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
590 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
591 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
592 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
593 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
594 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
595 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
596 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
597 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
598 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
599 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
600 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
601 { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
602 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
603 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
604 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
605 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
606 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
607 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
608 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
609 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
610 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
611 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
612 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
613 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
614 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
615 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
617 if (table > 2) table = 2;
618 init_decoder();
619 make_decoder ( first_tree[table], 0);
620 second_decode = free_decode;
621 make_decoder (second_tree[table], 0);
625 Return 0 if the image starts with compressed data,
626 1 if it starts with uncompressed low-order bits.
628 In Canon compressed data, 0xff is always followed by 0x00.
630 int CLASS canon_has_lowbits()
632 uchar test[0x4000];
633 int ret=1, i;
635 fseek (ifp, 0, SEEK_SET);
636 fread (test, 1, sizeof test, ifp);
637 for (i=540; i < sizeof test - 1; i++)
638 if (test[i] == 0xff) {
639 if (test[i+1]) return 1;
640 ret=0;
642 return ret;
645 void CLASS canon_compressed_load_raw()
647 ushort *pixel, *prow;
648 int lowbits, i, row, r, col, save, val;
649 unsigned irow, icol;
650 struct decode *decode, *dindex;
651 int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
652 uchar c;
654 crw_init_tables (tiff_compress);
655 pixel = calloc (raw_width*8, sizeof *pixel);
656 merror (pixel, "canon_compressed_load_raw()");
657 lowbits = canon_has_lowbits();
658 if (!lowbits) maximum = 0x3ff;
659 fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
660 zero_after_ff = 1;
661 getbits(-1);
662 for (row = 0; row < raw_height; row += 8) {
663 for (block=0; block < raw_width >> 3; block++) {
664 memset (diffbuf, 0, sizeof diffbuf);
665 decode = first_decode;
666 for (i=0; i < 64; i++ ) {
667 for (dindex=decode; dindex->branch[0]; )
668 dindex = dindex->branch[getbits(1)];
669 leaf = dindex->leaf;
670 decode = second_decode;
671 if (leaf == 0 && i) break;
672 if (leaf == 0xff) continue;
673 i += leaf >> 4;
674 len = leaf & 15;
675 if (len == 0) continue;
676 diff = getbits(len);
677 if ((diff & (1 << (len-1))) == 0)
678 diff -= (1 << len) - 1;
679 if (i < 64) diffbuf[i] = diff;
681 diffbuf[0] += carry;
682 carry = diffbuf[0];
683 for (i=0; i < 64; i++ ) {
684 if (pnum++ % raw_width == 0)
685 base[0] = base[1] = 512;
686 pixel[(block << 6) + i] = ( base[i & 1] += diffbuf[i] );
689 if (lowbits) {
690 save = ftell(ifp);
691 fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
692 for (prow=pixel, i=0; i < raw_width*2; i++) {
693 c = fgetc(ifp);
694 for (r=0; r < 8; r+=2, prow++) {
695 val = (*prow << 2) + ((c >> r) & 3);
696 if (raw_width == 2672 && val < 512) val += 2;
697 *prow = val;
700 fseek (ifp, save, SEEK_SET);
702 for (r=0; r < 8; r++) {
703 irow = row - top_margin + r;
704 if (irow >= height) continue;
705 for (col = 0; col < raw_width; col++) {
706 icol = col - left_margin;
707 if (icol < width)
708 BAYER(irow,icol) = pixel[r*raw_width+col];
709 else
710 black += pixel[r*raw_width+col];
714 free (pixel);
715 if (raw_width > width)
716 black /= (raw_width - width) * height;
720 Not a full implementation of Lossless JPEG, just
721 enough to decode Canon, Kodak and Adobe DNG images.
723 struct jhead {
724 int bits, high, wide, clrs, restart, vpred[4];
725 struct decode *huff[4];
726 ushort *row;
729 int CLASS ljpeg_start (struct jhead *jh, int info_only)
731 int i, tag, len;
732 uchar data[0x10000], *dp;
734 init_decoder();
735 for (i=0; i < 4; i++)
736 jh->huff[i] = free_decode;
737 jh->restart = INT_MAX;
738 fread (data, 2, 1, ifp);
739 if (data[1] != 0xd8) return 0;
740 do {
741 fread (data, 2, 2, ifp);
742 tag = data[0] << 8 | data[1];
743 len = (data[2] << 8 | data[3]) - 2;
744 if (tag <= 0xff00) return 0;
745 fread (data, 1, len, ifp);
746 switch (tag) {
747 case 0xffc0:
748 case 0xffc3:
749 jh->bits = data[0];
750 jh->high = data[1] << 8 | data[2];
751 jh->wide = data[3] << 8 | data[4];
752 jh->clrs = data[5];
753 break;
754 case 0xffc4:
755 if (info_only) break;
756 for (dp = data; dp < data+len && *dp < 4; ) {
757 jh->huff[*dp] = free_decode;
758 dp = make_decoder (++dp, 0);
760 break;
761 case 0xffdd:
762 jh->restart = data[0] << 8 | data[1];
764 } while (tag != 0xffda);
765 if (info_only) return 1;
766 jh->row = calloc (jh->wide*jh->clrs, 2);
767 merror (jh->row, " jpeg_start()");
768 return zero_after_ff = 1;
771 int CLASS ljpeg_diff (struct decode *dindex)
773 int len, diff;
775 while (dindex->branch[0])
776 dindex = dindex->branch[getbits(1)];
777 len = dindex->leaf;
778 if (len == 16 && (!dng_version || dng_version >= 0x1010000))
779 return -32768;
780 diff = getbits(len);
781 if ((diff & (1 << (len-1))) == 0)
782 diff -= (1 << len) - 1;
783 return diff;
786 void CLASS ljpeg_row (int jrow, struct jhead *jh)
788 int col, c, diff;
789 ushort *outp=jh->row;
791 if (jrow * jh->wide % jh->restart == 0) {
792 FORC4 jh->vpred[c] = 1 << (jh->bits-1);
793 if (jrow) get2(); /* Eat the FF Dx marker */
794 getbits(-1);
796 for (col=0; col < jh->wide; col++)
797 for (c=0; c < jh->clrs; c++) {
798 diff = ljpeg_diff (jh->huff[c]);
799 *outp = col ? outp[-jh->clrs]+diff : (jh->vpred[c] += diff);
800 outp++;
804 void CLASS lossless_jpeg_load_raw()
806 int jwide, jrow, jcol, val, jidx, i, j, row=0, col=0;
807 struct jhead jh;
808 int min=INT_MAX;
810 if (!ljpeg_start (&jh, 0)) return;
811 jwide = jh.wide * jh.clrs;
813 for (jrow=0; jrow < jh.high; jrow++) {
814 ljpeg_row (jrow, &jh);
815 for (jcol=0; jcol < jwide; jcol++) {
816 val = jh.row[jcol];
817 if (jh.bits <= 12)
818 val = curve[val];
819 if (cr2_slice[0]) {
820 jidx = jrow*jwide + jcol;
821 i = jidx / (cr2_slice[1]*jh.high);
822 if ((j = i >= cr2_slice[0]))
823 i = cr2_slice[0];
824 jidx -= i * (cr2_slice[1]*jh.high);
825 row = jidx / cr2_slice[1+j];
826 col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
828 if ((unsigned) (row-top_margin) < height) {
829 if ((unsigned) (col-left_margin) < width) {
830 BAYER(row-top_margin,col-left_margin) = val;
831 if (min > val) min = val;
832 } else black += val;
834 if (++col >= raw_width)
835 col = (row++,0);
838 free (jh.row);
839 if (raw_width > width)
840 black /= (raw_width - width) * height;
841 if (!strcasecmp(make,"KODAK"))
842 black = min;
845 void CLASS adobe_copy_pixel (int row, int col, ushort **rp)
847 unsigned r, c;
849 r = row -= top_margin;
850 c = col -= left_margin;
851 if (fuji_secondary && use_secondary) (*rp)++;
852 if (filters) {
853 if (fuji_width) {
854 r = row + fuji_width - 1 - (col >> 1);
855 c = row + ((col+1) >> 1);
857 if (r < height && c < width)
858 BAYER(r,c) = **rp < 0x1000 ? curve[**rp] : **rp;
859 *rp += 1 + fuji_secondary;
860 } else {
861 if (r < height && c < width)
862 for (c=0; c < tiff_samples; c++)
863 image[row*width+col][c] = (*rp)[c] < 0x1000 ? curve[(*rp)[c]]:(*rp)[c];
864 *rp += tiff_samples;
866 if (fuji_secondary && use_secondary) (*rp)--;
869 void CLASS adobe_dng_load_raw_lj()
871 int save, twide, trow=0, tcol=0, jrow, jcol;
872 struct jhead jh;
873 ushort *rp;
875 while (1) {
876 save = ftell(ifp);
877 fseek (ifp, get4(), SEEK_SET);
878 if (!ljpeg_start (&jh, 0)) break;
879 if (trow >= raw_height) break;
880 if (jh.high > raw_height-trow)
881 jh.high = raw_height-trow;
882 twide = jh.wide;
883 if (filters) twide *= jh.clrs;
884 else colors = jh.clrs;
885 if (fuji_secondary) twide /= 2;
886 if (twide > raw_width-tcol)
887 twide = raw_width-tcol;
889 for (jrow=0; jrow < jh.high; jrow++) {
890 ljpeg_row (jrow, &jh);
891 for (rp=jh.row, jcol=0; jcol < twide; jcol++)
892 adobe_copy_pixel (trow+jrow, tcol+jcol, &rp);
894 fseek (ifp, save+4, SEEK_SET);
895 if ((tcol += twide) >= raw_width) {
896 tcol = 0;
897 trow += jh.high;
899 free (jh.row);
903 void CLASS adobe_dng_load_raw_nc()
905 ushort *pixel, *rp;
906 int row, col;
908 pixel = calloc (raw_width * tiff_samples, sizeof *pixel);
909 merror (pixel, "adobe_dng_load_raw_nc()");
910 for (row=0; row < raw_height; row++) {
911 if (tiff_bps == 16)
912 read_shorts (pixel, raw_width * tiff_samples);
913 else {
914 getbits(-1);
915 for (col=0; col < raw_width * tiff_samples; col++)
916 pixel[col] = getbits(tiff_bps);
918 for (rp=pixel, col=0; col < raw_width; col++)
919 adobe_copy_pixel (row, col, &rp);
921 free (pixel);
924 void CLASS nikon_compressed_load_raw()
926 static const uchar nikon_tree[] = {
927 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0,
928 5,4,3,6,2,7,1,0,8,9,11,10,12
930 int csize, row, col, i, diff;
931 ushort vpred[4], hpred[2], *curve;
933 init_decoder();
934 make_decoder (nikon_tree, 0);
936 fseek (ifp, curve_offset, SEEK_SET);
937 read_shorts (vpred, 4);
938 csize = get2();
939 curve = calloc (csize, sizeof *curve);
940 merror (curve, "nikon_compressed_load_raw()");
941 read_shorts (curve, csize);
943 fseek (ifp, data_offset, SEEK_SET);
944 getbits(-1);
946 for (row=0; row < height; row++)
947 for (col=0; col < raw_width; col++)
949 diff = ljpeg_diff (first_decode);
950 if (col < 2) {
951 i = 2*(row & 1) + (col & 1);
952 vpred[i] += diff;
953 hpred[col] = vpred[i];
954 } else
955 hpred[col & 1] += diff;
956 if ((unsigned) (col-left_margin) >= width) continue;
957 diff = hpred[col & 1];
958 if (diff >= csize) diff = csize-1;
959 BAYER(row,col-left_margin) = curve[diff];
961 free (curve);
964 void CLASS nikon_load_raw()
966 int irow, row, col, i;
968 getbits(-1);
969 for (irow=0; irow < height; irow++) {
970 row = irow;
971 if (make[0] == 'O' || model[0] == 'E') {
972 row = irow * 2 % height + irow / (height/2);
973 if (row == 1 && data_offset == 0) {
974 fseek (ifp, 0, SEEK_END);
975 fseek (ifp, ftell(ifp)/2, SEEK_SET);
976 getbits(-1);
979 for (col=0; col < raw_width; col++) {
980 i = getbits(12);
981 if ((unsigned) (col-left_margin) < width)
982 BAYER(row,col-left_margin) = i;
983 if (tiff_compress == 34713 && (col % 10) == 9)
984 getbits(8);
990 Figure out if a NEF file is compressed. These fancy heuristics
991 are only needed for the D100, thanks to a bug in some cameras
992 that tags all images as "compressed".
994 int CLASS nikon_is_compressed()
996 uchar test[256];
997 int i;
999 if (tiff_compress != 34713)
1000 return 0;
1001 if (strcmp(model,"D100"))
1002 return 1;
1003 fseek (ifp, data_offset, SEEK_SET);
1004 fread (test, 1, 256, ifp);
1005 for (i=15; i < 256; i+=16)
1006 if (test[i]) return 1;
1007 return 0;
1011 Returns 1 for a Coolpix 995, 0 for anything else.
1013 int CLASS nikon_e995()
1015 int i, histo[256];
1016 const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
1018 memset (histo, 0, sizeof histo);
1019 fseek (ifp, -2000, SEEK_END);
1020 for (i=0; i < 2000; i++)
1021 histo[fgetc(ifp)]++;
1022 for (i=0; i < 4; i++)
1023 if (histo[often[i]] < 200)
1024 return 0;
1025 return 1;
1029 Returns 1 for a Coolpix 2100, 0 for anything else.
1031 int CLASS nikon_e2100()
1033 uchar t[12];
1034 int i;
1036 fseek (ifp, 0, SEEK_SET);
1037 for (i=0; i < 1024; i++) {
1038 fread (t, 1, 12, ifp);
1039 if (((t[2] & t[4] & t[7] & t[9]) >> 4
1040 & t[1] & t[6] & t[8] & t[11] & 3) != 3)
1041 return 0;
1043 return 1;
1047 Returns 0 for a Pentax Optio 33WR,
1048 1 for a Nikon E3700,
1049 2 for an Olympus C740UZ.
1051 int CLASS nikon_3700()
1053 int i, sum[] = { 0, 0 };
1054 uchar tail[952];
1056 fseek (ifp, -sizeof tail, SEEK_END);
1057 fread (tail, 1, sizeof tail, ifp);
1058 for (i=0; i < sizeof tail; i++)
1059 sum[(i>>2) & 1] += tail[i];
1060 if (sum[1] > 4*sum[0]) return 2;
1061 return sum[0] > 4*sum[1];
1065 Separates a Minolta DiMAGE Z2 from a Nikon E4300.
1067 int CLASS minolta_z2()
1069 int i;
1070 char tail[424];
1072 fseek (ifp, -sizeof tail, SEEK_END);
1073 fread (tail, 1, sizeof tail, ifp);
1074 for (i=0; i < sizeof tail; i++)
1075 if (tail[i]) return 1;
1076 return 0;
1079 /* Here raw_width is in bytes, not pixels. */
1080 void CLASS nikon_e900_load_raw()
1082 int offset=0, irow, row, col;
1084 for (irow=0; irow < height; irow++) {
1085 row = irow * 2 % height;
1086 if (row == 1)
1087 offset = - (-offset & -4096);
1088 fseek (ifp, offset, SEEK_SET);
1089 offset += raw_width;
1090 getbits(-1);
1091 for (col=0; col < width; col++)
1092 BAYER(row,col) = getbits(10);
1096 void CLASS nikon_e2100_load_raw()
1098 uchar data[3456], *dp;
1099 ushort pixel[2304], *pix;
1100 int row, col;
1102 for (row=0; row <= height; row+=2) {
1103 if (row == height) {
1104 fseek (ifp, ((width==1616) << 13) - (-ftell(ifp) & -2048), SEEK_SET);
1105 row = 1;
1107 fread (data, 1, width*3/2, ifp);
1108 for (dp=data, pix=pixel; pix < pixel+width; dp+=12, pix+=8) {
1109 pix[0] = (dp[2] >> 4) + (dp[ 3] << 4);
1110 pix[1] = (dp[2] << 8) + dp[ 1];
1111 pix[2] = (dp[7] >> 4) + (dp[ 0] << 4);
1112 pix[3] = (dp[7] << 8) + dp[ 6];
1113 pix[4] = (dp[4] >> 4) + (dp[ 5] << 4);
1114 pix[5] = (dp[4] << 8) + dp[11];
1115 pix[6] = (dp[9] >> 4) + (dp[10] << 4);
1116 pix[7] = (dp[9] << 8) + dp[ 8];
1118 for (col=0; col < width; col++)
1119 BAYER(row,col) = (pixel[col] & 0xfff);
1124 The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
1126 void CLASS fuji_load_raw()
1128 ushort *pixel;
1129 int row, col, r, c;
1131 pixel = calloc (raw_width, sizeof *pixel);
1132 merror (pixel, "fuji_load_raw()");
1133 for (row=0; row < raw_height; row++) {
1134 read_shorts (pixel, raw_width);
1135 for (col=0; col < fuji_width << !fuji_layout; col++) {
1136 if (fuji_layout) {
1137 r = fuji_width - 1 - col + (row >> 1);
1138 c = col + ((row+1) >> 1);
1139 } else {
1140 r = fuji_width - 1 + row - (col >> 1);
1141 c = row + ((col+1) >> 1);
1143 BAYER(r,c) = pixel[col];
1146 free (pixel);
1149 void CLASS jpeg_thumb (FILE *tfp)
1151 char *thumb = malloc (thumb_length);
1152 merror (thumb, "jpeg_thumb()");
1153 fread (thumb, 1, thumb_length, ifp);
1154 thumb[0] = 0xff;
1155 fwrite (thumb, 1, thumb_length, tfp);
1156 free (thumb);
1159 void CLASS ppm_thumb (FILE *tfp)
1161 char *thumb = malloc (thumb_length);
1162 merror (thumb, "ppm_thumb()");
1163 fprintf (tfp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1164 fread (thumb, 1, thumb_length, ifp);
1165 fwrite (thumb, 1, thumb_length, tfp);
1166 free (thumb);
1169 void CLASS layer_thumb (FILE *tfp)
1171 int i, c;
1172 char *thumb;
1173 colors = thumb_misc >> 5;
1174 thumb = malloc (thumb_length*colors);
1175 merror (thumb, "layer_thumb()");
1176 fprintf (tfp, "P%d\n%d %d\n255\n",
1177 5 + (thumb_misc >> 6), thumb_width, thumb_height);
1178 fread (thumb, thumb_length, colors, ifp);
1179 for (i=0; i < thumb_length; i++)
1180 FORCC putc (thumb[i+thumb_length*c], tfp);
1181 free (thumb);
1184 void CLASS rollei_thumb (FILE *tfp)
1186 int i, size = thumb_width * thumb_height;
1187 ushort *thumb = calloc (size, 2);
1188 merror (thumb, "rollei_thumb()");
1189 fprintf (tfp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1190 read_shorts (thumb, size);
1191 for (i=0; i < size; i++) {
1192 putc (thumb[i] << 3, tfp);
1193 putc (thumb[i] >> 5 << 2, tfp);
1194 putc (thumb[i] >> 11 << 3, tfp);
1196 free (thumb);
1199 void CLASS rollei_load_raw()
1201 uchar pixel[10];
1202 unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
1204 isix = raw_width * raw_height * 5 / 8;
1205 while (fread (pixel, 1, 10, ifp) == 10) {
1206 for (i=0; i < 10; i+=2) {
1207 todo[i] = iten++;
1208 todo[i+1] = pixel[i] << 8 | pixel[i+1];
1209 buffer = pixel[i] >> 2 | buffer << 6;
1211 for ( ; i < 16; i+=2) {
1212 todo[i] = isix++;
1213 todo[i+1] = buffer >> (14-i)*5;
1215 for (i=0; i < 16; i+=2) {
1216 row = todo[i] / raw_width - top_margin;
1217 col = todo[i] % raw_width - left_margin;
1218 if (row < height && col < width)
1219 BAYER(row,col) = (todo[i+1] & 0x3ff);
1222 maximum = 0x3ff;
1225 int CLASS bayer (unsigned row, unsigned col)
1227 return (row < height && col < width) ? BAYER(row,col) : 0;
1230 void CLASS phase_one_correct()
1232 unsigned entries, tag, data, save, col, row, type;
1233 int len, i, j, val[4], dev[4], sum, max;
1234 static const signed char dir[12][2] =
1235 { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
1236 {-2,-2}, {-2,2}, {2,-2}, {2,2} };
1238 if (!meta_length) return;
1239 fseek (ifp, meta_offset, SEEK_SET);
1240 order = get2();
1241 fseek (ifp, 6, SEEK_CUR);
1242 fseek (ifp, meta_offset+get4(), SEEK_SET);
1243 entries = get4(); get4();
1244 while (entries--) {
1245 tag = get4();
1246 len = get4();
1247 data = get4();
1248 save = ftell(ifp);
1249 fseek (ifp, meta_offset+data, SEEK_SET);
1250 if (tag == 0x400) /* Sensor defects */
1251 while ((len -= 8) >= 0) {
1252 col = get2() - left_margin;
1253 row = get2() - top_margin;
1254 type = get2(); get2();
1255 if (col >= width) continue;
1256 if (type == 131) /* Bad column */
1257 for (row=0; row < height; row++)
1258 if (FC(row,col) == 1) {
1259 for (sum=i=0; i < 4; i++)
1260 sum += val[i] = bayer (row+dir[i][0], col+dir[i][1]);
1261 for (max=i=0; i < 4; i++) {
1262 dev[i] = abs((val[i] << 2) - sum);
1263 if (dev[max] < dev[i]) max = i;
1265 BAYER(row,col) = (sum - val[max])/3.0 + 0.5;
1266 } else {
1267 for (sum=0, i=8; i < 12; i++)
1268 sum += bayer (row+dir[i][0], col+dir[i][1]);
1269 BAYER(row,col) = 0.5 + sum * 0.0732233 +
1270 (bayer(row,col-2) + bayer(row,col+2)) * 0.3535534;
1272 else if (type == 129) { /* Bad pixel */
1273 if (row >= height) continue;
1274 j = (FC(row,col) != 1) * 4;
1275 for (sum=0, i=j; i < j+8; i++)
1276 sum += bayer (row+dir[i][0], col+dir[i][1]);
1277 BAYER(row,col) = (sum + 4) >> 3;
1280 fseek (ifp, save, SEEK_SET);
1284 void CLASS phase_one_load_raw()
1286 int row, col, a, b;
1287 ushort *pixel, akey, bkey, mask;
1289 fseek (ifp, curve_offset, SEEK_SET);
1290 akey = get2();
1291 bkey = get2();
1292 mask = tiff_compress == 1 ? 0x5555:0x1354;
1293 fseek (ifp, data_offset + top_margin*raw_width*2, SEEK_SET);
1294 pixel = calloc (raw_width, sizeof *pixel);
1295 merror (pixel, "phase_one_load_raw()");
1296 for (row=0; row < height; row++) {
1297 read_shorts (pixel, raw_width);
1298 for (col=0; col < raw_width; col+=2) {
1299 a = pixel[col+0] ^ akey;
1300 b = pixel[col+1] ^ bkey;
1301 pixel[col+0] = (a & mask) | (b & ~mask);
1302 pixel[col+1] = (b & mask) | (a & ~mask);
1304 for (col=0; col < width; col++)
1305 BAYER(row,col) = pixel[col+left_margin];
1307 free (pixel);
1308 maximum = 0xffff;
1309 phase_one_correct();
1312 unsigned CLASS ph1_bits (int nbits)
1314 static UINT64 bitbuf=0;
1315 static int vbits=0;
1317 if (nbits == 0)
1318 return bitbuf = vbits = 0;
1319 if (vbits < nbits) {
1320 bitbuf = bitbuf << 32 | (unsigned) get4();
1321 vbits += 32;
1323 vbits -= nbits;
1324 return bitbuf << (64 - nbits - vbits) >> (64 - nbits);
1327 void CLASS phase_one_load_raw_c()
1329 static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
1330 int *offset, len[2], pred[2], row, col, i, j;
1331 ushort *pixel;
1333 pixel = calloc (raw_width + raw_height*2, 2);
1334 merror (pixel, "phase_one_load_raw_c()");
1335 offset = (int *) (pixel + raw_width);
1336 fseek (ifp, strip_offset, SEEK_SET);
1337 for (row=0; row < raw_height; row++)
1338 offset[row] = get4();
1339 for (row=0; row < raw_height; row++) {
1340 fseek (ifp, data_offset + offset[row], SEEK_SET);
1341 ph1_bits(0);
1342 pred[0] = pred[1] = 0;
1343 for (col=0; col < raw_width; col++) {
1344 if (col >= (raw_width & -8))
1345 len[0] = len[1] = 14;
1346 else if ((col & 7) == 0)
1347 for (i=0; i < 2; i++) {
1348 for (j=0; j < 5 && !ph1_bits(1); j++);
1349 if (j--) len[i] = length[j*2 + ph1_bits(1)];
1351 if ((i = len[col & 1]) == 14)
1352 pixel[col] = pred[col & 1] = ph1_bits(16);
1353 else
1354 pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
1356 if ((unsigned) (row-top_margin) < height)
1357 for (col=0; col < width; col++)
1358 BAYER(row-top_margin,col) = pixel[col+left_margin];
1360 free (pixel);
1361 maximum = 0x3fff;
1362 phase_one_correct();
1365 void CLASS hdr_load_raw()
1367 ushort *pixel;
1368 int r, c, row, col;
1370 pixel = calloc (raw_width, sizeof *pixel);
1371 merror (pixel, "hdr_load_raw()");
1372 for (r=0; r < height-32; r+=32)
1373 FORC3 for (row=r; row < r+32; row++) {
1374 read_shorts (pixel, raw_width);
1375 for (col=0; col < width; col++)
1376 image[row*width+col][c] = pixel[col];
1378 free (pixel);
1381 /* Here raw_width is in bytes, not pixels. */
1382 void CLASS packed_12_load_raw()
1384 int row, col;
1386 getbits(-1);
1387 for (row=0; row < height; row++) {
1388 for (col=0; col < width; col++)
1389 BAYER(row,col) = getbits(12);
1390 for (col = width*3/2; col < raw_width; col++)
1391 getbits(8);
1395 void CLASS unpacked_load_raw()
1397 ushort *pixel;
1398 int row, col;
1400 pixel = calloc (raw_width, sizeof *pixel);
1401 merror (pixel, "unpacked_load_raw()");
1402 for (row=0; row < height; row++) {
1403 read_shorts (pixel, raw_width);
1404 for (col=0; col < width; col++)
1405 BAYER(row,col) = pixel[col];
1407 free (pixel);
1410 void CLASS olympus_e300_load_raw()
1412 uchar *data, *dp;
1413 ushort *pixel, *pix;
1414 int dwide, row, col, bls=width, ble=raw_width;
1416 if (raw_width == 3360) bls += 4;
1417 if (raw_width == 3280) ble = bls + 8;
1418 dwide = raw_width * 16 / 10;
1419 data = malloc (dwide + raw_width*2);
1420 merror (data, "olympus_e300_load_raw()");
1421 pixel = (ushort *) (data + dwide);
1422 for (row=0; row < height; row++) {
1423 fread (data, 1, dwide, ifp);
1424 for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=3, pix+=2) {
1425 if (((dp-data) & 15) == 15) dp++;
1426 pix[0] = dp[1] << 8 | dp[0];
1427 pix[1] = dp[2] << 4 | dp[1] >> 4;
1429 for (col=0; col < width; col++)
1430 BAYER(row,col) = (pixel[col] & 0xfff);
1431 for (col=bls; col < ble; col++)
1432 black += pixel[col] & 0xfff;
1434 if (ble > bls) black /= (ble - bls) * height;
1435 free (data);
1438 void CLASS olympus_cseries_load_raw()
1440 int irow, row, col;
1442 for (irow=0; irow < height; irow++) {
1443 row = irow * 2 % height + irow / (height/2);
1444 if (row < 2) {
1445 fseek (ifp, data_offset - row*(-width*height*3/4 & -2048), SEEK_SET);
1446 getbits(-1);
1448 for (col=0; col < width; col++)
1449 BAYER(row,col) = getbits(12);
1453 void CLASS minolta_rd175_load_raw()
1455 uchar pixel[768];
1456 unsigned irow, box, row, col;
1458 for (irow=0; irow < 1481; irow++) {
1459 fread (pixel, 1, 768, ifp);
1460 box = irow / 82;
1461 row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
1462 switch (irow) {
1463 case 1477: case 1479: continue;
1464 case 1476: row = 984; break;
1465 case 1480: row = 985; break;
1466 case 1478: row = 985; box = 1;
1468 if ((box < 12) && (box & 1)) {
1469 for (col=0; col < 1533; col++, row ^= 1)
1470 if (col != 1) BAYER(row,col) = (col+1) & 2 ?
1471 pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
1472 BAYER(row,1) = pixel[1] << 1;
1473 BAYER(row,1533) = pixel[765] << 1;
1474 } else
1475 for (col=row & 1; col < 1534; col+=2)
1476 BAYER(row,col) = pixel[col/2] << 1;
1478 maximum = 0xff << 1;
1481 void CLASS eight_bit_load_raw()
1483 uchar *pixel;
1484 int row, col;
1486 pixel = calloc (raw_width, sizeof *pixel);
1487 merror (pixel, "eight_bit_load_raw()");
1488 for (row=0; row < height; row++) {
1489 fread (pixel, 1, raw_width, ifp);
1490 for (col=0; col < width; col++)
1491 BAYER(row,col) = pixel[col];
1493 free (pixel);
1494 maximum = 0xff;
1497 void CLASS casio_qv5700_load_raw()
1499 uchar data[3232], *dp;
1500 ushort pixel[2576], *pix;
1501 int row, col;
1503 for (row=0; row < height; row++) {
1504 fread (data, 1, 3232, ifp);
1505 for (dp=data, pix=pixel; dp < data+3220; dp+=5, pix+=4) {
1506 pix[0] = (dp[0] << 2) + (dp[1] >> 6);
1507 pix[1] = (dp[1] << 4) + (dp[2] >> 4);
1508 pix[2] = (dp[2] << 6) + (dp[3] >> 2);
1509 pix[3] = (dp[3] << 8) + (dp[4] );
1511 for (col=0; col < width; col++)
1512 BAYER(row,col) = (pixel[col] & 0x3ff);
1514 maximum = 0x3fc;
1517 void CLASS nucore_load_raw()
1519 ushort *pixel;
1520 int irow, row, col;
1522 pixel = calloc (width, 2);
1523 merror (pixel, "nucore_load_raw()");
1524 for (irow=0; irow < height; irow++) {
1525 read_shorts (pixel, width);
1526 row = irow/2 + height/2 * (irow & 1);
1527 for (col=0; col < width; col++)
1528 BAYER(row,col) = pixel[col];
1530 free (pixel);
1533 const int * CLASS make_decoder_int (const int *source, int level)
1535 struct decode *cur;
1537 cur = free_decode++;
1538 if (level < source[0]) {
1539 cur->branch[0] = free_decode;
1540 source = make_decoder_int (source, level+1);
1541 cur->branch[1] = free_decode;
1542 source = make_decoder_int (source, level+1);
1543 } else {
1544 cur->leaf = source[1];
1545 source += 2;
1547 return source;
1550 int CLASS radc_token (int tree)
1552 int t;
1553 static struct decode *dstart[18], *dindex;
1554 static const int *s, source[] = {
1555 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
1556 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
1557 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
1558 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
1559 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
1560 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
1561 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
1562 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
1563 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
1564 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
1565 1,0, 2,2, 2,-2,
1566 1,-3, 1,3,
1567 2,-17, 2,-5, 2,5, 2,17,
1568 2,-7, 2,2, 2,9, 2,18,
1569 2,-18, 2,-9, 2,-2, 2,7,
1570 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
1571 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
1572 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
1575 if (free_decode == first_decode)
1576 for (s=source, t=0; t < 18; t++) {
1577 dstart[t] = free_decode;
1578 s = make_decoder_int (s, 0);
1580 if (tree == 18) {
1581 if (kodak_cbpp == 243)
1582 return (getbits(6) << 2) + 2; /* most DC50 photos */
1583 else
1584 return (getbits(5) << 3) + 4; /* DC40, Fotoman Pixtura */
1586 for (dindex = dstart[tree]; dindex->branch[0]; )
1587 dindex = dindex->branch[getbits(1)];
1588 return dindex->leaf;
1591 #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
1593 #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
1594 : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
1596 void CLASS kodak_radc_load_raw()
1598 int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
1599 short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
1601 init_decoder();
1602 getbits(-1);
1603 for (i=0; i < sizeof(buf)/sizeof(short); i++)
1604 buf[0][0][i] = 2048;
1605 for (row=0; row < height; row+=4) {
1606 FORC3 mul[c] = getbits(6);
1607 FORC3 {
1608 val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
1609 s = val > 65564 ? 10:12;
1610 x = ~(-1 << (s-1));
1611 val <<= 12-s;
1612 for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
1613 buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
1614 last[c] = mul[c];
1615 for (r=0; r <= !c; r++) {
1616 buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
1617 for (tree=1, col=width/2; col > 0; ) {
1618 if ((tree = radc_token(tree))) {
1619 col -= 2;
1620 if (tree == 8)
1621 FORYX buf[c][y][x] = radc_token(tree+10) * mul[c];
1622 else
1623 FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
1624 } else
1625 do {
1626 nreps = (col > 2) ? radc_token(9) + 1 : 1;
1627 for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
1628 col -= 2;
1629 FORYX buf[c][y][x] = PREDICTOR;
1630 if (rep & 1) {
1631 step = radc_token(10) << 4;
1632 FORYX buf[c][y][x] += step;
1635 } while (nreps == 9);
1637 for (y=0; y < 2; y++)
1638 for (x=0; x < width/2; x++) {
1639 val = (buf[c][y+1][x] << 4) / mul[c];
1640 if (val < 0) val = 0;
1641 if (c)
1642 BAYER(row+y*2+c-1,x*2+2-c) = val;
1643 else
1644 BAYER(row+r*2+y,x*2+y) = val;
1646 memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
1649 for (y=row; y < row+4; y++)
1650 for (x=0; x < width; x++)
1651 if ((x+y) & 1) {
1652 val = (BAYER(y,x)-2048)*2 + (BAYER(y,x-1)+BAYER(y,x+1))/2;
1653 if (val < 0) val = 0;
1654 BAYER(y,x) = val;
1657 maximum = 10000;
1660 #undef FORYX
1661 #undef PREDICTOR
1663 #ifdef NO_JPEG
1664 void CLASS kodak_jpeg_load_raw() {}
1665 #else
1667 METHODDEF(boolean)
1668 fill_input_buffer (j_decompress_ptr cinfo)
1670 static uchar jpeg_buffer[4096];
1671 size_t nbytes;
1673 nbytes = fread (jpeg_buffer, 1, 4096, ifp);
1674 swab (jpeg_buffer, jpeg_buffer, nbytes);
1675 cinfo->src->next_input_byte = jpeg_buffer;
1676 cinfo->src->bytes_in_buffer = nbytes;
1677 return TRUE;
1680 void CLASS kodak_jpeg_load_raw()
1682 struct jpeg_decompress_struct cinfo;
1683 struct jpeg_error_mgr jerr;
1684 JSAMPARRAY buf;
1685 JSAMPLE (*pixel)[3];
1686 int row, col;
1688 cinfo.err = jpeg_std_error (&jerr);
1689 jpeg_create_decompress (&cinfo);
1690 jpeg_stdio_src (&cinfo, ifp);
1691 cinfo.src->fill_input_buffer = fill_input_buffer;
1692 jpeg_read_header (&cinfo, TRUE);
1693 jpeg_start_decompress (&cinfo);
1694 if ((cinfo.output_width != width ) ||
1695 (cinfo.output_height*2 != height ) ||
1696 (cinfo.output_components != 3 )) {
1697 fprintf (stderr, "%s: incorrect JPEG dimensions\n", ifname);
1698 jpeg_destroy_decompress (&cinfo);
1699 longjmp (failure, 3);
1701 buf = (*cinfo.mem->alloc_sarray)
1702 ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
1704 while (cinfo.output_scanline < cinfo.output_height) {
1705 row = cinfo.output_scanline * 2;
1706 jpeg_read_scanlines (&cinfo, buf, 1);
1707 pixel = (void *) buf[0];
1708 for (col=0; col < width; col+=2) {
1709 BAYER(row+0,col+0) = pixel[col+0][1] << 1;
1710 BAYER(row+1,col+1) = pixel[col+1][1] << 1;
1711 BAYER(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
1712 BAYER(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
1715 jpeg_finish_decompress (&cinfo);
1716 jpeg_destroy_decompress (&cinfo);
1717 maximum = 0xff << 1;
1719 #endif
1721 void CLASS kodak_dc120_load_raw()
1723 static const int mul[4] = { 162, 192, 187, 92 };
1724 static const int add[4] = { 0, 636, 424, 212 };
1725 uchar pixel[848];
1726 int row, shift, col;
1728 for (row=0; row < height; row++) {
1729 fread (pixel, 848, 1, ifp);
1730 shift = row * mul[row & 3] + add[row & 3];
1731 for (col=0; col < width; col++)
1732 BAYER(row,col) = (ushort) pixel[(col + shift) % 848];
1734 maximum = 0xff;
1737 void CLASS kodak_easy_load_raw()
1739 uchar *pixel;
1740 unsigned row, col, icol;
1742 if (raw_width > width)
1743 black = 0;
1744 pixel = calloc (raw_width, sizeof *pixel);
1745 merror (pixel, "kodak_easy_load_raw()");
1746 for (row=0; row < height; row++) {
1747 fread (pixel, 1, raw_width, ifp);
1748 for (col=0; col < raw_width; col++) {
1749 icol = col - left_margin;
1750 if (icol < width)
1751 BAYER(row,icol) = (ushort) curve[pixel[col]];
1752 else
1753 black += curve[pixel[col]];
1756 free (pixel);
1757 if (raw_width > width)
1758 black /= (raw_width - width) * height;
1759 if (!strncmp(model,"DC2",3))
1760 black = 0;
1761 maximum = curve[0xff];
1764 int CLASS kodak_65000_decode (short *out, int bsize)
1766 uchar c, blen[768];
1767 ushort raw[6];
1768 INT64 bitbuf=0;
1769 int save, bits=0, i, j, len, diff;
1771 save = ftell(ifp);
1772 bsize = (bsize + 3) & -4;
1773 for (i=0; i < bsize; i+=2) {
1774 c = fgetc(ifp);
1775 if ((blen[i ] = c & 15) > 12 ||
1776 (blen[i+1] = c >> 4) > 12 ) {
1777 fseek (ifp, save, SEEK_SET);
1778 for (i=0; i < bsize; i+=8) {
1779 read_shorts (raw, 6);
1780 out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
1781 out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
1782 for (j=0; j < 6; j++)
1783 out[i+2+j] = raw[j] & 0xfff;
1785 return 1;
1788 if ((bsize & 7) == 4) {
1789 bitbuf = fgetc(ifp) << 8;
1790 bitbuf += fgetc(ifp);
1791 bits = 16;
1793 for (i=0; i < bsize; i++) {
1794 len = blen[i];
1795 if (bits < len) {
1796 for (j=0; j < 32; j+=8)
1797 bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
1798 bits += 32;
1800 diff = bitbuf & (0xffff >> (16-len));
1801 bitbuf >>= len;
1802 bits -= len;
1803 if ((diff & (1 << (len-1))) == 0)
1804 diff -= (1 << len) - 1;
1805 out[i] = diff;
1807 return 0;
1810 void CLASS kodak_65000_load_raw()
1812 short buf[256];
1813 int row, col, len, pred[2], ret, i;
1815 for (row=0; row < height; row++)
1816 for (col=0; col < width; col+=256) {
1817 pred[0] = pred[1] = 0;
1818 len = MIN (256, width-col);
1819 ret = kodak_65000_decode (buf, len);
1820 for (i=0; i < len; i++)
1821 BAYER(row,col+i) = curve[ret ? buf[i] : (pred[i & 1] += buf[i])];
1825 void CLASS kodak_ycbcr_load_raw()
1827 short buf[384], *bp;
1828 int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
1829 ushort *ip;
1831 for (row=0; row < height; row+=2)
1832 for (col=0; col < width; col+=128) {
1833 len = MIN (128, width-col);
1834 kodak_65000_decode (buf, len*3);
1835 y[0][1] = y[1][1] = cb = cr = 0;
1836 for (bp=buf, i=0; i < len; i+=2, bp+=2) {
1837 cb += bp[4];
1838 cr += bp[5];
1839 rgb[1] = -((cb + cr + 2) >> 2);
1840 rgb[2] = rgb[1] + cb;
1841 rgb[0] = rgb[1] + cr;
1842 for (j=0; j < 2; j++)
1843 for (k=0; k < 2; k++) {
1844 y[j][k] = y[j][k^1] + *bp++;
1845 ip = image[(row+j)*width + col+i+k];
1846 FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
1852 void CLASS kodak_rgb_load_raw()
1854 short buf[768], *bp;
1855 int row, col, len, c, i, rgb[3];
1856 ushort *ip=image[0];
1858 for (row=0; row < height; row++)
1859 for (col=0; col < width; col+=256) {
1860 len = MIN (256, width-col);
1861 kodak_65000_decode (buf, len*3);
1862 memset (rgb, 0, sizeof rgb);
1863 for (bp=buf, i=0; i < len; i++, ip+=4)
1864 FORC3 ip[c] = (rgb[c] += *bp++) & 0xfff;
1868 void CLASS kodak_thumb_load_raw()
1870 int row, col;
1871 colors = thumb_misc >> 5;
1872 for (row=0; row < height; row++)
1873 for (col=0; col < width; col++)
1874 read_shorts (image[row*width+col], colors);
1875 maximum = (1 << (thumb_misc & 31)) - 1;
1878 void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
1880 static unsigned pad[128], p;
1882 if (start) {
1883 for (p=0; p < 4; p++)
1884 pad[p] = key = key * 48828125 + 1;
1885 pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
1886 for (p=4; p < 127; p++)
1887 pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
1888 for (p=0; p < 127; p++)
1889 pad[p] = htonl(pad[p]);
1891 while (len--)
1892 *data++ ^= pad[p++ & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
1895 void CLASS sony_load_raw()
1897 uchar head[40];
1898 ushort *pixel;
1899 unsigned i, key, row, col;
1901 fseek (ifp, 200896, SEEK_SET);
1902 fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
1903 order = 0x4d4d;
1904 key = get4();
1905 fseek (ifp, 164600, SEEK_SET);
1906 fread (head, 1, 40, ifp);
1907 sony_decrypt ((void *) head, 10, 1, key);
1908 for (i=26; i-- > 22; )
1909 key = key << 8 | head[i];
1910 fseek (ifp, data_offset, SEEK_SET);
1911 pixel = calloc (raw_width, sizeof *pixel);
1912 merror (pixel, "sony_load_raw()");
1913 for (row=0; row < height; row++) {
1914 fread (pixel, 2, raw_width, ifp);
1915 sony_decrypt ((void *) pixel, raw_width/2, !row, key);
1916 for (col=9; col < left_margin; col++)
1917 black += ntohs(pixel[col]);
1918 for (col=0; col < width; col++)
1919 BAYER(row,col) = ntohs(pixel[col+left_margin]);
1921 free (pixel);
1922 if (left_margin > 9)
1923 black /= (left_margin-9) * height;
1924 maximum = 0x3ff0;
1927 #define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
1929 /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
1930 void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
1932 uchar hist[3][13] = {
1933 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
1934 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
1935 { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
1936 int low, high=0xff, carry=0, nbits=8;
1937 int s, count, bin, next, i, sym[3];
1938 uchar diff, pred[]={0,0};
1939 ushort data=0, range=0;
1940 unsigned pix, row, col;
1942 fseek (ifp, seg[0][1]+1, SEEK_SET);
1943 getbits(-1);
1944 for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
1945 for (s=0; s < 3; s++) {
1946 data = data << nbits | getbits(nbits);
1947 if (carry < 0)
1948 carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
1949 while (--nbits >= 0)
1950 if ((data >> nbits & 0xff) == 0xff) break;
1951 if (nbits > 0)
1952 data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
1953 ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
1954 if (nbits >= 0) {
1955 data += getbits(1);
1956 carry = nbits - 8;
1958 count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
1959 for (bin=0; hist[s][bin+5] > count; bin++);
1960 low = hist[s][bin+5] * (high >> 4) >> 2;
1961 if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
1962 high -= low;
1963 for (nbits=0; high << nbits < 128; nbits++);
1964 range = (range+low) << nbits;
1965 high <<= nbits;
1966 next = hist[s][1];
1967 if (++hist[s][2] > hist[s][3]) {
1968 next = (next+1) & hist[s][0];
1969 hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
1970 hist[s][2] = 1;
1972 if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
1973 if (bin < hist[s][1])
1974 for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
1975 else if (next <= bin)
1976 for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
1978 hist[s][1] = next;
1979 sym[s] = bin;
1981 diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
1982 if (sym[0] & 4)
1983 diff = diff ? -diff : 0x80;
1984 if (ftell(ifp) + 12 >= seg[1][1])
1985 diff = 0;
1986 pred[pix & 1] += diff;
1987 row = pix / raw_width - top_margin;
1988 col = pix % raw_width - left_margin;
1989 if (row < height && col < width)
1990 BAYER(row,col) = pred[pix & 1];
1991 if (!(pix & 1) && HOLE(row)) pix += 2;
1993 maximum = 0xff;
1996 void CLASS smal_v6_load_raw()
1998 unsigned seg[2][2];
2000 fseek (ifp, 16, SEEK_SET);
2001 seg[0][0] = 0;
2002 seg[0][1] = get2();
2003 seg[1][0] = raw_width * raw_height;
2004 seg[1][1] = INT_MAX;
2005 smal_decode_segment (seg, 0);
2006 use_gamma = 0;
2009 int CLASS median4 (int *p)
2011 int min, max, sum, i;
2013 min = max = sum = p[0];
2014 for (i=1; i < 4; i++) {
2015 sum += p[i];
2016 if (min > p[i]) min = p[i];
2017 if (max < p[i]) max = p[i];
2019 return (sum - min - max) >> 1;
2022 void CLASS fill_holes (int holes)
2024 int row, col, val[4];
2026 for (row=2; row < height-2; row++) {
2027 if (!HOLE(row)) continue;
2028 for (col=1; col < width-1; col+=4) {
2029 val[0] = BAYER(row-1,col-1);
2030 val[1] = BAYER(row-1,col+1);
2031 val[2] = BAYER(row+1,col-1);
2032 val[3] = BAYER(row+1,col+1);
2033 BAYER(row,col) = median4(val);
2035 for (col=2; col < width-2; col+=4)
2036 if (HOLE(row-2) || HOLE(row+2))
2037 BAYER(row,col) = (BAYER(row,col-2) + BAYER(row,col+2)) >> 1;
2038 else {
2039 val[0] = BAYER(row,col-2);
2040 val[1] = BAYER(row,col+2);
2041 val[2] = BAYER(row-2,col);
2042 val[3] = BAYER(row+2,col);
2043 BAYER(row,col) = median4(val);
2048 void CLASS smal_v9_load_raw()
2050 unsigned seg[256][2], offset, nseg, holes, i;
2052 fseek (ifp, 67, SEEK_SET);
2053 offset = get4();
2054 nseg = fgetc(ifp);
2055 fseek (ifp, offset, SEEK_SET);
2056 for (i=0; i < nseg*2; i++)
2057 seg[0][i] = get4() + data_offset*(i & 1);
2058 fseek (ifp, 78, SEEK_SET);
2059 holes = fgetc(ifp);
2060 fseek (ifp, 88, SEEK_SET);
2061 seg[nseg][0] = raw_height * raw_width;
2062 seg[nseg][1] = get4() + data_offset;
2063 for (i=0; i < nseg; i++)
2064 smal_decode_segment (seg+i, holes);
2065 if (holes) fill_holes (holes);
2068 /* BEGIN GPL BLOCK */
2070 void CLASS foveon_decoder (unsigned size, unsigned code)
2072 static unsigned huff[1024];
2073 struct decode *cur;
2074 int i, len;
2076 if (!code) {
2077 for (i=0; i < size; i++)
2078 huff[i] = get4();
2079 init_decoder();
2081 cur = free_decode++;
2082 if (free_decode > first_decode+2048) {
2083 fprintf (stderr, "%s: decoder table overflow\n", ifname);
2084 longjmp (failure, 2);
2086 if (code)
2087 for (i=0; i < size; i++)
2088 if (huff[i] == code) {
2089 cur->leaf = i;
2090 return;
2092 if ((len = code >> 27) > 26) return;
2093 code = (len+1) << 27 | (code & 0x3ffffff) << 1;
2095 cur->branch[0] = free_decode;
2096 foveon_decoder (size, code);
2097 cur->branch[1] = free_decode;
2098 foveon_decoder (size, code+1);
2101 void CLASS foveon_thumb (FILE *tfp)
2103 int bwide, row, col, bit=-1, c, i;
2104 char *buf;
2105 struct decode *dindex;
2106 short pred[3];
2107 unsigned bitbuf=0;
2109 bwide = get4();
2110 fprintf (tfp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
2111 if (bwide > 0) {
2112 buf = malloc (bwide);
2113 merror (buf, "foveon_thumb()");
2114 for (row=0; row < thumb_height; row++) {
2115 fread (buf, 1, bwide, ifp);
2116 fwrite (buf, 3, thumb_width, tfp);
2118 free (buf);
2119 return;
2121 foveon_decoder (256, 0);
2123 for (row=0; row < thumb_height; row++) {
2124 memset (pred, 0, sizeof pred);
2125 if (!bit) get4();
2126 for (col=bit=0; col < thumb_width; col++)
2127 FORC3 {
2128 for (dindex=first_decode; dindex->branch[0]; ) {
2129 if ((bit = (bit-1) & 31) == 31)
2130 for (i=0; i < 4; i++)
2131 bitbuf = (bitbuf << 8) + fgetc(ifp);
2132 dindex = dindex->branch[bitbuf >> bit & 1];
2134 pred[c] += dindex->leaf;
2135 fputc (pred[c], tfp);
2140 void CLASS foveon_load_camf()
2142 unsigned key, i, val;
2144 fseek (ifp, meta_offset, SEEK_SET);
2145 key = get4();
2146 fread (meta_data, 1, meta_length, ifp);
2147 for (i=0; i < meta_length; i++) {
2148 key = (key * 1597 + 51749) % 244944;
2149 val = key * (INT64) 301593171 >> 24;
2150 meta_data[i] ^= ((((key << 8) - val) >> 1) + val) >> 17;
2154 void CLASS foveon_load_raw()
2156 struct decode *dindex;
2157 short diff[1024], pred[3];
2158 unsigned bitbuf=0;
2159 int fixed, row, col, bit=-1, c, i;
2161 fixed = get4();
2162 read_shorts ((ushort *) diff, 1024);
2163 if (!fixed) foveon_decoder (1024, 0);
2165 for (row=0; row < height; row++) {
2166 memset (pred, 0, sizeof pred);
2167 if (!bit && !fixed) get4();
2168 for (col=bit=0; col < width; col++) {
2169 if (fixed) {
2170 bitbuf = get4();
2171 FORC3 pred[2-c] += diff[bitbuf >> c*10 & 0x3ff];
2173 else FORC3 {
2174 for (dindex=first_decode; dindex->branch[0]; ) {
2175 if ((bit = (bit-1) & 31) == 31)
2176 for (i=0; i < 4; i++)
2177 bitbuf = (bitbuf << 8) + fgetc(ifp);
2178 dindex = dindex->branch[bitbuf >> bit & 1];
2180 pred[c] += diff[dindex->leaf];
2182 FORC3 image[row*width+col][c] = pred[c];
2185 if (document_mode)
2186 for (i=0; i < height*width*4; i++)
2187 if ((short) image[0][i] < 0) image[0][i] = 0;
2188 foveon_load_camf();
2189 clip_max = 0xffff;
2192 char * CLASS foveon_camf_param (char *block, char *param)
2194 unsigned idx, num;
2195 char *pos, *cp, *dp;
2197 for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
2198 pos = meta_data + idx;
2199 if (strncmp (pos, "CMb", 3)) break;
2200 if (pos[3] != 'P') continue;
2201 if (strcmp (block, pos+sget4(pos+12))) continue;
2202 cp = pos + sget4(pos+16);
2203 num = sget4(cp);
2204 dp = pos + sget4(cp+4);
2205 while (num--) {
2206 cp += 8;
2207 if (!strcmp (param, dp+sget4(cp)))
2208 return dp+sget4(cp+4);
2211 return NULL;
2214 void * CLASS foveon_camf_matrix (int dim[3], char *name)
2216 unsigned i, idx, type, ndim, size, *mat;
2217 char *pos, *cp, *dp;
2219 for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
2220 pos = meta_data + idx;
2221 if (strncmp (pos, "CMb", 3)) break;
2222 if (pos[3] != 'M') continue;
2223 if (strcmp (name, pos+sget4(pos+12))) continue;
2224 dim[0] = dim[1] = dim[2] = 1;
2225 cp = pos + sget4(pos+16);
2226 type = sget4(cp);
2227 if ((ndim = sget4(cp+4)) > 3) break;
2228 dp = pos + sget4(cp+8);
2229 for (i=ndim; i--; ) {
2230 cp += 12;
2231 dim[i] = sget4(cp);
2233 if ((size = dim[0]*dim[1]*dim[2]) > meta_length/4) break;
2234 mat = malloc (size * 4);
2235 merror (mat, "foveon_camf_matrix()");
2236 for (i=0; i < size; i++)
2237 if (type && type != 6)
2238 mat[i] = sget4(dp + i*4);
2239 else
2240 mat[i] = sget4(dp + i*2) & 0xffff;
2241 return mat;
2243 fprintf (stderr, "%s: \"%s\" matrix not found!\n", ifname, name);
2244 return NULL;
2247 int CLASS foveon_fixed (void *ptr, int size, char *name)
2249 void *dp;
2250 int dim[3];
2252 dp = foveon_camf_matrix (dim, name);
2253 if (!dp) return 0;
2254 memcpy (ptr, dp, size*4);
2255 free (dp);
2256 return 1;
2259 float CLASS foveon_avg (short *pix, int range[2], float cfilt)
2261 int i;
2262 float val, min=FLT_MAX, max=-FLT_MAX, sum=0;
2264 for (i=range[0]; i <= range[1]; i++) {
2265 sum += val = pix[i*4] + (pix[i*4]-pix[(i-1)*4]) * cfilt;
2266 if (min > val) min = val;
2267 if (max < val) max = val;
2269 return (sum - min - max) / (range[1] - range[0] - 1);
2272 short * CLASS foveon_make_curve (double max, double mul, double filt)
2274 short *curve;
2275 int i, size;
2276 double x;
2278 if (!filt) filt = 0.8;
2279 size = 4*M_PI*max / filt;
2280 curve = calloc (size+1, sizeof *curve);
2281 merror (curve, "foveon_make_curve()");
2282 curve[0] = size;
2283 for (i=0; i < size; i++) {
2284 x = i*filt/max/4;
2285 curve[i+1] = (cos(x)+1)/2 * tanh(i*filt/mul) * mul + 0.5;
2287 return curve;
2290 void CLASS foveon_make_curves
2291 (short **curvep, float dq[3], float div[3], float filt)
2293 double mul[3], max=0;
2294 int c;
2296 FORC3 mul[c] = dq[c]/div[c];
2297 FORC3 if (max < mul[c]) max = mul[c];
2298 FORC3 curvep[c] = foveon_make_curve (max, mul[c], filt);
2301 int CLASS foveon_apply_curve (short *curve, int i)
2303 if (abs(i) >= curve[0]) return 0;
2304 return i < 0 ? -curve[1-i] : curve[1+i];
2307 #define image ((short (*)[4]) image)
2309 void CLASS foveon_interpolate()
2311 static const short hood[] = { -1,-1, -1,0, -1,1, 0,-1, 0,1, 1,-1, 1,0, 1,1 };
2312 short *pix, prev[3], *curve[8], (*shrink)[3];
2313 float cfilt=0, ddft[3][3][2], ppm[3][3][3];
2314 float cam_xyz[3][3], correct[3][3], last[3][3], trans[3][3];
2315 float chroma_dq[3], color_dq[3], diag[3][3], div[3];
2316 float (*black)[3], (*sgain)[3], (*sgrow)[3];
2317 float fsum[3], val, frow, num;
2318 int row, col, c, i, j, diff, sgx, irow, sum, min, max, limit;
2319 int dim[3], dscr[2][2], dstb[4], (*smrow[7])[3], total[4], ipix[3];
2320 int work[3][3], smlast, smred, smred_p=0, dev[3];
2321 int satlev[3], keep[4], active[4];
2322 unsigned *badpix;
2323 double dsum=0, trsum[3];
2324 char str[128], *cp;
2326 if (verbose)
2327 fprintf (stderr, "Foveon interpolation...\n");
2329 foveon_fixed (dscr, 4, "DarkShieldColRange");
2330 foveon_fixed (ppm[0][0], 27, "PostPolyMatrix");
2331 foveon_fixed (satlev, 3, "SaturationLevel");
2332 foveon_fixed (keep, 4, "KeepImageArea");
2333 foveon_fixed (active, 4, "ActiveImageArea");
2334 foveon_fixed (chroma_dq, 3, "ChromaDQ");
2335 foveon_fixed (color_dq, 3,
2336 foveon_camf_param ("IncludeBlocks", "ColorDQ") ?
2337 "ColorDQ" : "ColorDQCamRGB");
2338 if (foveon_camf_param ("IncludeBlocks", "ColumnFilter"))
2339 foveon_fixed (&cfilt, 1, "ColumnFilter");
2341 memset (ddft, 0, sizeof ddft);
2342 if (!foveon_camf_param ("IncludeBlocks", "DarkDrift")
2343 || !foveon_fixed (ddft[1][0], 12, "DarkDrift"))
2344 for (i=0; i < 2; i++) {
2345 foveon_fixed (dstb, 4, i ? "DarkShieldBottom":"DarkShieldTop");
2346 for (row = dstb[1]; row <= dstb[3]; row++)
2347 for (col = dstb[0]; col <= dstb[2]; col++)
2348 FORC3 ddft[i+1][c][1] += (short) image[row*width+col][c];
2349 FORC3 ddft[i+1][c][1] /= (dstb[3]-dstb[1]+1) * (dstb[2]-dstb[0]+1);
2352 if (!(cp = foveon_camf_param ("WhiteBalanceIlluminants", model2)))
2353 { fprintf (stderr, "%s: Invalid white balance \"%s\"\n", ifname, model2);
2354 return; }
2355 foveon_fixed (cam_xyz, 9, cp);
2356 foveon_fixed (correct, 9,
2357 foveon_camf_param ("WhiteBalanceCorrections", model2));
2358 memset (last, 0, sizeof last);
2359 for (i=0; i < 3; i++)
2360 for (j=0; j < 3; j++)
2361 FORC3 last[i][j] += correct[i][c] * cam_xyz[c][j];
2363 sprintf (str, "%sRGBNeutral", model2);
2364 if (foveon_camf_param ("IncludeBlocks", str))
2365 foveon_fixed (div, 3, str);
2366 else {
2367 #define LAST(x,y) last[(i+x)%3][(c+y)%3]
2368 for (i=0; i < 3; i++)
2369 FORC3 diag[c][i] = LAST(1,1)*LAST(2,2) - LAST(1,2)*LAST(2,1);
2370 #undef LAST
2371 FORC3 div[c] = diag[c][0]*0.3127 + diag[c][1]*0.329 + diag[c][2]*0.3583;
2373 num = 0;
2374 FORC3 if (num < div[c]) num = div[c];
2375 FORC3 div[c] /= num;
2377 memset (trans, 0, sizeof trans);
2378 for (i=0; i < 3; i++)
2379 for (j=0; j < 3; j++)
2380 FORC3 trans[i][j] += rgb_cam[i][c] * last[c][j] * div[j];
2381 FORC3 trsum[c] = trans[c][0] + trans[c][1] + trans[c][2];
2382 dsum = (6*trsum[0] + 11*trsum[1] + 3*trsum[2]) / 20;
2383 for (i=0; i < 3; i++)
2384 FORC3 last[i][c] = trans[i][c] * dsum / trsum[i];
2385 memset (trans, 0, sizeof trans);
2386 for (i=0; i < 3; i++)
2387 for (j=0; j < 3; j++)
2388 FORC3 trans[i][j] += (i==c ? 32 : -1) * last[c][j] / 30;
2390 foveon_make_curves (curve, color_dq, div, cfilt);
2391 FORC3 chroma_dq[c] /= 3;
2392 foveon_make_curves (curve+3, chroma_dq, div, cfilt);
2393 FORC3 dsum += chroma_dq[c] / div[c];
2394 curve[6] = foveon_make_curve (dsum, dsum, cfilt);
2395 curve[7] = foveon_make_curve (dsum*2, dsum*2, cfilt);
2397 sgain = foveon_camf_matrix (dim, "SpatialGain");
2398 if (!sgain) return;
2399 sgrow = calloc (dim[1], sizeof *sgrow);
2400 sgx = (width + dim[1]-2) / (dim[1]-1);
2402 black = calloc (height, sizeof *black);
2403 for (row=0; row < height; row++) {
2404 for (i=0; i < 6; i++)
2405 ddft[0][0][i] = ddft[1][0][i] +
2406 row / (height-1.0) * (ddft[2][0][i] - ddft[1][0][i]);
2407 FORC3 black[row][c] =
2408 ( foveon_avg (image[row*width]+c, dscr[0], cfilt) +
2409 foveon_avg (image[row*width]+c, dscr[1], cfilt) * 3
2410 - ddft[0][c][0] ) / 4 - ddft[0][c][1];
2412 memcpy (black, black+8, sizeof *black*8);
2413 memcpy (black+height-11, black+height-22, 11*sizeof *black);
2414 memcpy (last, black, sizeof last);
2416 for (row=1; row < height-1; row++) {
2417 FORC3 if (last[1][c] > last[0][c]) {
2418 if (last[1][c] > last[2][c])
2419 black[row][c] = (last[0][c] > last[2][c]) ? last[0][c]:last[2][c];
2420 } else
2421 if (last[1][c] < last[2][c])
2422 black[row][c] = (last[0][c] < last[2][c]) ? last[0][c]:last[2][c];
2423 memmove (last, last+1, 2*sizeof last[0]);
2424 memcpy (last[2], black[row+1], sizeof last[2]);
2426 FORC3 black[row][c] = (last[0][c] + last[1][c])/2;
2427 FORC3 black[0][c] = (black[1][c] + black[3][c])/2;
2429 val = 1 - exp(-1/24.0);
2430 memcpy (fsum, black, sizeof fsum);
2431 for (row=1; row < height; row++)
2432 FORC3 fsum[c] += black[row][c] =
2433 (black[row][c] - black[row-1][c])*val + black[row-1][c];
2434 memcpy (last[0], black[height-1], sizeof last[0]);
2435 FORC3 fsum[c] /= height;
2436 for (row = height; row--; )
2437 FORC3 last[0][c] = black[row][c] =
2438 (black[row][c] - fsum[c] - last[0][c])*val + last[0][c];
2440 memset (total, 0, sizeof total);
2441 for (row=2; row < height; row+=4)
2442 for (col=2; col < width; col+=4) {
2443 FORC3 total[c] += (short) image[row*width+col][c];
2444 total[3]++;
2446 for (row=0; row < height; row++)
2447 FORC3 black[row][c] += fsum[c]/2 + total[c]/(total[3]*100.0);
2449 for (row=0; row < height; row++) {
2450 for (i=0; i < 6; i++)
2451 ddft[0][0][i] = ddft[1][0][i] +
2452 row / (height-1.0) * (ddft[2][0][i] - ddft[1][0][i]);
2453 pix = image[row*width];
2454 memcpy (prev, pix, sizeof prev);
2455 frow = row / (height-1.0) * (dim[2]-1);
2456 if ((irow = frow) == dim[2]-1) irow--;
2457 frow -= irow;
2458 for (i=0; i < dim[1]; i++)
2459 FORC3 sgrow[i][c] = sgain[ irow *dim[1]+i][c] * (1-frow) +
2460 sgain[(irow+1)*dim[1]+i][c] * frow;
2461 for (col=0; col < width; col++) {
2462 FORC3 {
2463 diff = pix[c] - prev[c];
2464 prev[c] = pix[c];
2465 ipix[c] = pix[c] + floor ((diff + (diff*diff >> 14)) * cfilt
2466 - ddft[0][c][1] - ddft[0][c][0] * ((float) col/width - 0.5)
2467 - black[row][c] );
2469 FORC3 {
2470 work[0][c] = ipix[c] * ipix[c] >> 14;
2471 work[2][c] = ipix[c] * work[0][c] >> 14;
2472 work[1][2-c] = ipix[(c+1) % 3] * ipix[(c+2) % 3] >> 14;
2474 FORC3 {
2475 for (val=i=0; i < 3; i++)
2476 for ( j=0; j < 3; j++)
2477 val += ppm[c][i][j] * work[i][j];
2478 ipix[c] = floor ((ipix[c] + floor(val)) *
2479 ( sgrow[col/sgx ][c] * (sgx - col%sgx) +
2480 sgrow[col/sgx+1][c] * (col%sgx) ) / sgx / div[c]);
2481 if (ipix[c] > 32000) ipix[c] = 32000;
2482 pix[c] = ipix[c];
2484 pix += 4;
2487 free (black);
2488 free (sgrow);
2489 free (sgain);
2491 if ((badpix = foveon_camf_matrix (dim, "BadPixels"))) {
2492 for (i=0; i < dim[0]; i++) {
2493 col = (badpix[i] >> 8 & 0xfff) - keep[0];
2494 row = (badpix[i] >> 20 ) - keep[1];
2495 if ((unsigned)(row-1) > height-3 || (unsigned)(col-1) > width-3)
2496 continue;
2497 memset (fsum, 0, sizeof fsum);
2498 for (sum=j=0; j < 8; j++)
2499 if (badpix[i] & (1 << j)) {
2500 FORC3 fsum[c] += (short)
2501 image[(row+hood[j*2])*width+col+hood[j*2+1]][c];
2502 sum++;
2504 if (sum) FORC3 image[row*width+col][c] = fsum[c]/sum;
2506 free (badpix);
2509 /* Array for 5x5 Gaussian averaging of red values */
2510 smrow[6] = calloc (width*5, sizeof **smrow);
2511 merror (smrow[6], "foveon_interpolate()");
2512 for (i=0; i < 5; i++)
2513 smrow[i] = smrow[6] + i*width;
2515 /* Sharpen the reds against these Gaussian averages */
2516 for (smlast=-1, row=2; row < height-2; row++) {
2517 while (smlast < row+2) {
2518 for (i=0; i < 6; i++)
2519 smrow[(i+5) % 6] = smrow[i];
2520 pix = image[++smlast*width+2];
2521 for (col=2; col < width-2; col++) {
2522 smrow[4][col][0] =
2523 (pix[0]*6 + (pix[-4]+pix[4])*4 + pix[-8]+pix[8] + 8) >> 4;
2524 pix += 4;
2527 pix = image[row*width+2];
2528 for (col=2; col < width-2; col++) {
2529 smred = ( 6 * smrow[2][col][0]
2530 + 4 * (smrow[1][col][0] + smrow[3][col][0])
2531 + smrow[0][col][0] + smrow[4][col][0] + 8 ) >> 4;
2532 if (col == 2)
2533 smred_p = smred;
2534 i = pix[0] + ((pix[0] - ((smred*7 + smred_p) >> 3)) >> 3);
2535 if (i > 32000) i = 32000;
2536 pix[0] = i;
2537 smred_p = smred;
2538 pix += 4;
2542 /* Adjust the brighter pixels for better linearity */
2543 min = 0xffff;
2544 FORC3 {
2545 i = satlev[c] / div[c];
2546 if (min > i) min = i;
2548 limit = min * 9 >> 4;
2549 for (pix=image[0]; pix < image[height*width]; pix+=4) {
2550 if (pix[0] <= limit || pix[1] <= limit || pix[2] <= limit)
2551 continue;
2552 min = max = pix[0];
2553 for (c=1; c < 3; c++) {
2554 if (min > pix[c]) min = pix[c];
2555 if (max < pix[c]) max = pix[c];
2557 if (min >= limit*2) {
2558 pix[0] = pix[1] = pix[2] = max;
2559 } else {
2560 i = 0x4000 - ((min - limit) << 14) / limit;
2561 i = 0x4000 - (i*i >> 14);
2562 i = i*i >> 14;
2563 FORC3 pix[c] += (max - pix[c]) * i >> 14;
2567 Because photons that miss one detector often hit another,
2568 the sum R+G+B is much less noisy than the individual colors.
2569 So smooth the hues without smoothing the total.
2571 for (smlast=-1, row=2; row < height-2; row++) {
2572 while (smlast < row+2) {
2573 for (i=0; i < 6; i++)
2574 smrow[(i+5) % 6] = smrow[i];
2575 pix = image[++smlast*width+2];
2576 for (col=2; col < width-2; col++) {
2577 FORC3 smrow[4][col][c] = (pix[c-4]+2*pix[c]+pix[c+4]+2) >> 2;
2578 pix += 4;
2581 pix = image[row*width+2];
2582 for (col=2; col < width-2; col++) {
2583 FORC3 dev[c] = -foveon_apply_curve (curve[7], pix[c] -
2584 ((smrow[1][col][c] + 2*smrow[2][col][c] + smrow[3][col][c]) >> 2));
2585 sum = (dev[0] + dev[1] + dev[2]) >> 3;
2586 FORC3 pix[c] += dev[c] - sum;
2587 pix += 4;
2590 for (smlast=-1, row=2; row < height-2; row++) {
2591 while (smlast < row+2) {
2592 for (i=0; i < 6; i++)
2593 smrow[(i+5) % 6] = smrow[i];
2594 pix = image[++smlast*width+2];
2595 for (col=2; col < width-2; col++) {
2596 FORC3 smrow[4][col][c] =
2597 (pix[c-8]+pix[c-4]+pix[c]+pix[c+4]+pix[c+8]+2) >> 2;
2598 pix += 4;
2601 pix = image[row*width+2];
2602 for (col=2; col < width-2; col++) {
2603 for (total[3]=375, sum=60, c=0; c < 3; c++) {
2604 for (total[c]=i=0; i < 5; i++)
2605 total[c] += smrow[i][col][c];
2606 total[3] += total[c];
2607 sum += pix[c];
2609 if (sum < 0) sum = 0;
2610 j = total[3] > 375 ? (sum << 16) / total[3] : sum * 174;
2611 FORC3 pix[c] += foveon_apply_curve (curve[6],
2612 ((j*total[c] + 0x8000) >> 16) - pix[c]);
2613 pix += 4;
2617 /* Transform the image to a different colorspace */
2618 for (pix=image[0]; pix < image[height*width]; pix+=4) {
2619 FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]);
2620 sum = (pix[0]+pix[1]+pix[1]+pix[2]) >> 2;
2621 FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]-sum);
2622 FORC3 {
2623 for (dsum=i=0; i < 3; i++)
2624 dsum += trans[c][i] * pix[i];
2625 if (dsum < 0) dsum = 0;
2626 if (dsum > 24000) dsum = 24000;
2627 ipix[c] = dsum + 0.5;
2629 FORC3 pix[c] = ipix[c];
2632 /* Smooth the image bottom-to-top and save at 1/4 scale */
2633 shrink = calloc ((width/4) * (height/4), sizeof *shrink);
2634 merror (shrink, "foveon_interpolate()");
2635 for (row = height/4; row--; )
2636 for (col=0; col < width/4; col++) {
2637 ipix[0] = ipix[1] = ipix[2] = 0;
2638 for (i=0; i < 4; i++)
2639 for (j=0; j < 4; j++)
2640 FORC3 ipix[c] += image[(row*4+i)*width+col*4+j][c];
2641 FORC3
2642 if (row+2 > height/4)
2643 shrink[row*(width/4)+col][c] = ipix[c] >> 4;
2644 else
2645 shrink[row*(width/4)+col][c] =
2646 (shrink[(row+1)*(width/4)+col][c]*1840 + ipix[c]*141 + 2048) >> 12;
2648 /* From the 1/4-scale image, smooth right-to-left */
2649 for (row=0; row < (height & ~3); row++) {
2650 ipix[0] = ipix[1] = ipix[2] = 0;
2651 if ((row & 3) == 0)
2652 for (col = width & ~3 ; col--; )
2653 FORC3 smrow[0][col][c] = ipix[c] =
2654 (shrink[(row/4)*(width/4)+col/4][c]*1485 + ipix[c]*6707 + 4096) >> 13;
2656 /* Then smooth left-to-right */
2657 ipix[0] = ipix[1] = ipix[2] = 0;
2658 for (col=0; col < (width & ~3); col++)
2659 FORC3 smrow[1][col][c] = ipix[c] =
2660 (smrow[0][col][c]*1485 + ipix[c]*6707 + 4096) >> 13;
2662 /* Smooth top-to-bottom */
2663 if (row == 0)
2664 memcpy (smrow[2], smrow[1], sizeof **smrow * width);
2665 else
2666 for (col=0; col < (width & ~3); col++)
2667 FORC3 smrow[2][col][c] =
2668 (smrow[2][col][c]*6707 + smrow[1][col][c]*1485 + 4096) >> 13;
2670 /* Adjust the chroma toward the smooth values */
2671 for (col=0; col < (width & ~3); col++) {
2672 for (i=j=30, c=0; c < 3; c++) {
2673 i += smrow[2][col][c];
2674 j += image[row*width+col][c];
2676 j = (j << 16) / i;
2677 for (sum=c=0; c < 3; c++) {
2678 ipix[c] = foveon_apply_curve (curve[c+3],
2679 ((smrow[2][col][c] * j + 0x8000) >> 16) - image[row*width+col][c]);
2680 sum += ipix[c];
2682 sum >>= 3;
2683 FORC3 {
2684 i = image[row*width+col][c] + ipix[c] - sum;
2685 if (i < 0) i = 0;
2686 image[row*width+col][c] = i;
2690 free (shrink);
2691 free (smrow[6]);
2692 for (i=0; i < 8; i++)
2693 free (curve[i]);
2695 /* Trim off the black border */
2696 active[1] -= keep[1];
2697 active[3] -= 2;
2698 i = active[2] - active[0];
2699 for (row = 0; row < active[3]-active[1]; row++)
2700 memcpy (image[row*i], image[(row+active[1])*width+active[0]],
2701 i * sizeof *image);
2702 width = i;
2703 height = row;
2705 #undef image
2707 /* END GPL BLOCK */
2710 Seach from the current directory up to the root looking for
2711 a ".badpixels" file, and fix those pixels now.
2713 void CLASS bad_pixels()
2715 FILE *fp=NULL;
2716 char *fname, *cp, line[128];
2717 int len, time, row, col, r, c, rad, tot, n, fixed=0;
2719 if (!filters) return;
2720 for (len=32 ; ; len *= 2) {
2721 fname = malloc (len);
2722 if (!fname) return;
2723 if (getcwd (fname, len-16)) break;
2724 free (fname);
2725 if (errno != ERANGE) return;
2727 #if defined(WIN32) || defined(DJGPP)
2728 if (fname[1] == ':')
2729 memmove (fname, fname+2, len-2);
2730 for (cp=fname; *cp; cp++)
2731 if (*cp == '\\') *cp = '/';
2732 #endif
2733 cp = fname + strlen(fname);
2734 if (cp[-1] == '/') cp--;
2735 while (*fname == '/') {
2736 strcpy (cp, "/.badpixels");
2737 if ((fp = fopen (fname, "r"))) break;
2738 if (cp == fname) break;
2739 while (*--cp != '/');
2741 free (fname);
2742 if (!fp) return;
2743 while (fgets (line, 128, fp)) {
2744 cp = strchr (line, '#');
2745 if (cp) *cp = 0;
2746 if (sscanf (line, "%d %d %d", &col, &row, &time) != 3) continue;
2747 if ((unsigned) col >= width || (unsigned) row >= height) continue;
2748 if (time > timestamp) continue;
2749 for (tot=n=0, rad=1; rad < 3 && n==0; rad++)
2750 for (r = row-rad; r <= row+rad; r++)
2751 for (c = col-rad; c <= col+rad; c++)
2752 if ((unsigned) r < height && (unsigned) c < width &&
2753 (r != row || c != col) && FC(r,c) == FC(row,col)) {
2754 tot += BAYER(r,c);
2755 n++;
2757 BAYER(row,col) = tot/n;
2758 if (verbose) {
2759 if (!fixed++)
2760 fprintf (stderr, "Fixed bad pixels at:");
2761 fprintf (stderr, " %d,%d", col, row);
2764 if (fixed) fputc ('\n', stderr);
2765 fclose (fp);
2768 void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
2770 double work[3][6], num;
2771 int i, j, k;
2773 for (i=0; i < 3; i++) {
2774 for (j=0; j < 6; j++)
2775 work[i][j] = j == i+3;
2776 for (j=0; j < 3; j++)
2777 for (k=0; k < size; k++)
2778 work[i][j] += in[k][i] * in[k][j];
2780 for (i=0; i < 3; i++) {
2781 num = work[i][i];
2782 for (j=0; j < 6; j++)
2783 work[i][j] /= num;
2784 for (k=0; k < 3; k++) {
2785 if (k==i) continue;
2786 num = work[k][i];
2787 for (j=0; j < 6; j++)
2788 work[k][j] -= work[i][j] * num;
2791 for (i=0; i < size; i++)
2792 for (j=0; j < 3; j++)
2793 for (out[i][j]=k=0; k < 3; k++)
2794 out[i][j] += work[j][k+3] * in[i][k];
2797 void CLASS cam_xyz_coeff (double cam_xyz[4][3])
2799 double cam_rgb[4][3], inverse[4][3], num;
2800 int i, j, k;
2802 for (i=0; i < colors; i++) /* Multiply out XYZ colorspace */
2803 for (j=0; j < 3; j++)
2804 for (cam_rgb[i][j] = k=0; k < 3; k++)
2805 cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
2807 for (i=0; i < colors; i++) { /* Normalize cam_rgb so that */
2808 for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
2809 num += cam_rgb[i][j];
2810 for (j=0; j < 3; j++)
2811 cam_rgb[i][j] /= num;
2812 pre_mul[i] = 1 / num;
2814 pseudoinverse (cam_rgb, inverse, colors);
2815 for (raw_color = i=0; i < 3; i++)
2816 for (j=0; j < colors; j++)
2817 rgb_cam[i][j] = inverse[j][i];
2820 #ifdef COLORCHECK
2821 void CLASS colorcheck()
2823 #define NSQ 24
2824 // Coordinates of the GretagMacbeth ColorChecker squares
2825 // width, height, 1st_column, 1st_row
2826 static const int cut[NSQ][4] = {
2827 { 241, 231, 234, 274 },
2828 { 251, 235, 534, 274 },
2829 { 255, 239, 838, 272 },
2830 { 255, 240, 1146, 274 },
2831 { 251, 237, 1452, 278 },
2832 { 243, 238, 1758, 288 },
2833 { 253, 253, 218, 558 },
2834 { 255, 249, 524, 562 },
2835 { 261, 253, 830, 562 },
2836 { 260, 255, 1144, 564 },
2837 { 261, 255, 1450, 566 },
2838 { 247, 247, 1764, 576 },
2839 { 255, 251, 212, 862 },
2840 { 259, 259, 518, 862 },
2841 { 263, 261, 826, 864 },
2842 { 265, 263, 1138, 866 },
2843 { 265, 257, 1450, 872 },
2844 { 257, 255, 1762, 874 },
2845 { 257, 253, 212, 1164 },
2846 { 262, 251, 516, 1172 },
2847 { 263, 257, 826, 1172 },
2848 { 263, 255, 1136, 1176 },
2849 { 255, 252, 1452, 1182 },
2850 { 257, 253, 1760, 1180 } };
2851 // ColorChecker Chart under 6500-kelvin illumination
2852 static const double gmb_xyY[NSQ][3] = {
2853 { 0.400, 0.350, 10.1 }, // Dark Skin
2854 { 0.377, 0.345, 35.8 }, // Light Skin
2855 { 0.247, 0.251, 19.3 }, // Blue Sky
2856 { 0.337, 0.422, 13.3 }, // Foliage
2857 { 0.265, 0.240, 24.3 }, // Blue Flower
2858 { 0.261, 0.343, 43.1 }, // Bluish Green
2859 { 0.506, 0.407, 30.1 }, // Orange
2860 { 0.211, 0.175, 12.0 }, // Purplish Blue
2861 { 0.453, 0.306, 19.8 }, // Moderate Red
2862 { 0.285, 0.202, 6.6 }, // Purple
2863 { 0.380, 0.489, 44.3 }, // Yellow Green
2864 { 0.473, 0.438, 43.1 }, // Orange Yellow
2865 { 0.187, 0.129, 6.1 }, // Blue
2866 { 0.305, 0.478, 23.4 }, // Green
2867 { 0.539, 0.313, 12.0 }, // Red
2868 { 0.448, 0.470, 59.1 }, // Yellow
2869 { 0.364, 0.233, 19.8 }, // Magenta
2870 { 0.196, 0.252, 19.8 }, // Cyan
2871 { 0.310, 0.316, 90.0 }, // White
2872 { 0.310, 0.316, 59.1 }, // Neutral 8
2873 { 0.310, 0.316, 36.2 }, // Neutral 6.5
2874 { 0.310, 0.316, 19.8 }, // Neutral 5
2875 { 0.310, 0.316, 9.0 }, // Neutral 3.5
2876 { 0.310, 0.316, 3.1 } }; // Black
2877 double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
2878 double inverse[NSQ][3], cam_xyz[4][3], num;
2879 int c, i, j, k, sq, row, col, count[4];
2881 memset (gmb_cam, 0, sizeof gmb_cam);
2882 for (sq=0; sq < NSQ; sq++) {
2883 FORCC count[c] = 0;
2884 for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
2885 for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
2886 c = FC(row,col);
2887 if (c >= colors) c -= 2;
2888 gmb_cam[sq][c] += BAYER(row,col);
2889 count[c]++;
2891 FORCC gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - black;
2892 gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
2893 gmb_xyz[sq][1] = gmb_xyY[sq][2];
2894 gmb_xyz[sq][2] = gmb_xyY[sq][2] *
2895 (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
2897 pseudoinverse (gmb_xyz, inverse, NSQ);
2898 for (i=0; i < colors; i++)
2899 for (j=0; j < 3; j++)
2900 for (cam_xyz[i][j] = k=0; k < NSQ; k++)
2901 cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
2902 cam_xyz_coeff (cam_xyz);
2903 if (verbose) {
2904 printf (" { \"%s %s\", %d,\n\t{", make, model, black);
2905 num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
2906 FORCC for (j=0; j < 3; j++)
2907 printf ("%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
2908 puts (" } },");
2910 #undef NSQ
2912 #endif
2914 void CLASS scale_colors()
2916 int row, col, x, y, c, val, shift=0;
2917 int min[4], max[4], sum[8];
2918 double dsum[8], dmin;
2920 maximum -= black;
2921 if (use_auto_wb || (use_camera_wb && camera_red == -1)) {
2922 FORC4 min[c] = INT_MAX;
2923 FORC4 max[c] = 0;
2924 memset (dsum, 0, sizeof dsum);
2925 for (row=0; row < height-7; row++)
2926 for (col=0; col < width-7; col++) {
2927 memset (sum, 0, sizeof sum);
2928 for (y=row; y < row+7; y++)
2929 for (x=col; x < col+7; x++)
2930 FORC4 {
2931 val = image[y*width+x][c];
2932 if (!val) continue;
2933 if (min[c] > val) min[c] = val;
2934 if (max[c] < val) max[c] = val;
2935 val -= black;
2936 if (val > maximum-25) goto skip_block;
2937 if (val < 0) val = 0;
2938 sum[c] += val;
2939 sum[c+4]++;
2941 for (c=0; c < 8; c++) dsum[c] += sum[c];
2942 skip_block:
2943 continue;
2945 FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
2947 if (use_camera_wb && camera_red != -1) {
2948 memset (sum, 0, sizeof sum);
2949 for (row=0; row < 8; row++)
2950 for (col=0; col < 8; col++) {
2951 c = FC(row,col);
2952 if ((val = white[row][col] - black) > 0)
2953 sum[c] += val;
2954 sum[c+4]++;
2956 if (sum[0] && sum[1] && sum[2] && sum[3])
2957 FORC4 pre_mul[c] = (float) sum[c+4] / sum[c];
2958 else if (camera_red && camera_blue)
2959 memcpy (pre_mul, cam_mul, sizeof pre_mul);
2960 else
2961 fprintf (stderr, "%s: Cannot use camera white balance.\n", ifname);
2963 if (raw_color || !output_color) {
2964 pre_mul[0] *= red_scale;
2965 pre_mul[2] *= blue_scale;
2967 if (pre_mul[3] == 0) pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
2968 dmin = DBL_MAX;
2969 FORC4 if (dmin > pre_mul[c])
2970 dmin = pre_mul[c];
2971 FORC4 pre_mul[c] /= dmin;
2973 while (maximum << shift < 0x8000) shift++;
2974 FORC4 pre_mul[c] *= 1 << shift;
2975 maximum <<= shift;
2977 if (write_fun != write_ppm || bright < 1) {
2978 maximum *= bright;
2979 if (maximum > 0xffff)
2980 maximum = 0xffff;
2981 FORC4 pre_mul[c] *= bright;
2983 if (verbose) {
2984 fprintf (stderr, "Scaling with black=%d, pre_mul[] =", black);
2985 FORC4 fprintf (stderr, " %f", pre_mul[c]);
2986 fputc ('\n', stderr);
2988 clip_max = clip_color ? maximum : 0xffff;
2989 for (row=0; row < height; row++)
2990 for (col=0; col < width; col++)
2991 FORC4 {
2992 val = image[row*width+col][c];
2993 if (!val) continue;
2994 val -= black;
2995 val *= pre_mul[c];
2996 image[row*width+col][c] = CLIP(val);
2998 if (filters && colors == 3) {
2999 if (four_color_rgb) {
3000 colors++;
3001 FORC3 rgb_cam[c][3] = rgb_cam[c][1] /= 2;
3002 } else {
3003 for (row = FC(1,0) >> 1; row < height; row+=2)
3004 for (col = FC(row,1) & 1; col < width; col+=2)
3005 image[row*width+col][1] = image[row*width+col][3];
3006 filters &= ~((filters & 0x55555555) << 1);
3011 void CLASS border_interpolate (int border)
3013 unsigned row, col, y, x, c, sum[8];
3015 for (row=0; row < height; row++)
3016 for (col=0; col < width; col++) {
3017 if (col==border && row >= border && row < height-border)
3018 col = width-border;
3019 memset (sum, 0, sizeof sum);
3020 for (y=row-1; y != row+2; y++)
3021 for (x=col-1; x != col+2; x++)
3022 if (y < height && x < width) {
3023 sum[FC(y,x)] += BAYER(y,x);
3024 sum[FC(y,x)+4]++;
3026 FORCC if (c != FC(row,col))
3027 image[row*width+col][c] = sum[c] / sum[c+4];
3031 void CLASS lin_interpolate()
3033 int code[8][2][32], *ip, sum[4];
3034 int c, i, x, y, row, col, shift, color;
3035 ushort *pix;
3037 if (verbose) fprintf (stderr, "Bilinear interpolation...\n");
3039 border_interpolate(1);
3040 for (row=0; row < 8; row++)
3042 for (col=0; col < 2; col++)
3044 ip = code[row][col];
3045 memset (sum, 0, sizeof sum);
3046 for (y=-1; y <= 1; y++)
3048 for (x=-1; x <= 1; x++)
3050 shift = (y==0) + (x==0);
3051 if (shift == 2) continue;
3052 color = FC(row+y,col+x);
3053 *ip++ = (width*y + x)*4 + color;
3054 *ip++ = shift;
3055 *ip++ = color;
3056 sum[color] += 1 << shift;
3060 FORCC
3062 if (c != FC(row,col))
3064 *ip++ = c;
3065 *ip++ = sum[c];
3071 for (row=1; row < height-1; row++)
3073 for (col=1; col < width-1; col++)
3075 pix = image[row*width+col];
3076 ip = code[row & 7][col & 1];
3077 memset (sum, 0, sizeof sum);
3078 for (i=8; i--; ip+=3)
3079 sum[ip[2]] += pix[ip[0]] << ip[1];
3080 for (i=colors; --i; ip+=2)
3081 pix[ip[0]] = sum[ip[0]] / ip[1];
3087 This algorithm is officially called:
3089 "Interpolation using a Threshold-based variable number of gradients"
3091 described in http://www-ise.stanford.edu/~tingchen/algodep/vargra.html
3093 I've extended the basic idea to work with non-Bayer filter arrays.
3094 Gradients are numbered clockwise from NW=0 to W=7.
3096 void CLASS vng_interpolate()
3098 static const signed char *cp, terms[] = {
3099 -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
3100 -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
3101 -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
3102 -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
3103 -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
3104 -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
3105 -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
3106 -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
3107 -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
3108 -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
3109 -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
3110 -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
3111 -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
3112 +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
3113 +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
3114 +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
3115 +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
3116 +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
3117 +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
3118 +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
3119 +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
3120 +1,+0,+2,+1,0,0x10
3121 }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
3122 ushort (*brow[5])[4], *pix;
3123 int code[8][2][320], *ip, gval[8], gmin, gmax, sum[4];
3124 int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
3125 int g, diff, thold, num, c;
3127 lin_interpolate();
3128 if (verbose) fprintf (stderr, "VNG interpolation...\n");
3130 for (row=0; row < 8; row++) { /* Precalculate for VNG */
3131 for (col=0; col < 2; col++) {
3132 ip = code[row][col];
3133 for (cp=terms, t=0; t < 64; t++) {
3134 y1 = *cp++; x1 = *cp++;
3135 y2 = *cp++; x2 = *cp++;
3136 weight = *cp++;
3137 grads = *cp++;
3138 color = FC(row+y1,col+x1);
3139 if (FC(row+y2,col+x2) != color) continue;
3140 diag = (FC(row,col+1) == color && FC(row+1,col) == color) ? 2:1;
3141 if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
3142 *ip++ = (y1*width + x1)*4 + color;
3143 *ip++ = (y2*width + x2)*4 + color;
3144 *ip++ = weight;
3145 for (g=0; g < 8; g++)
3146 if (grads & 1<<g) *ip++ = g;
3147 *ip++ = -1;
3149 *ip++ = INT_MAX;
3150 for (cp=chood, g=0; g < 8; g++) {
3151 y = *cp++; x = *cp++;
3152 *ip++ = (y*width + x) * 4;
3153 color = FC(row,col);
3154 if (FC(row+y,col+x) != color && FC(row+y*2,col+x*2) == color)
3155 *ip++ = (y*width + x) * 8 + color;
3156 else
3157 *ip++ = 0;
3161 brow[4] = calloc (width*3, sizeof **brow);
3162 merror (brow[4], "vng_interpolate()");
3163 for (row=0; row < 3; row++)
3164 brow[row] = brow[4] + row*width;
3165 for (row=2; row < height-2; row++) { /* Do VNG interpolation */
3166 for (col=2; col < width-2; col++) {
3167 pix = image[row*width+col];
3168 ip = code[row & 7][col & 1];
3169 memset (gval, 0, sizeof gval);
3170 while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
3171 diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
3172 gval[ip[3]] += diff;
3173 ip += 5;
3174 if ((g = ip[-1]) == -1) continue;
3175 gval[g] += diff;
3176 while ((g = *ip++) != -1)
3177 gval[g] += diff;
3179 ip++;
3180 gmin = gmax = gval[0]; /* Choose a threshold */
3181 for (g=1; g < 8; g++) {
3182 if (gmin > gval[g]) gmin = gval[g];
3183 if (gmax < gval[g]) gmax = gval[g];
3185 if (gmax == 0) {
3186 memcpy (brow[2][col], pix, sizeof *image);
3187 continue;
3189 thold = gmin + (gmax >> 1);
3190 memset (sum, 0, sizeof sum);
3191 color = FC(row,col);
3192 for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
3193 if (gval[g] <= thold) {
3194 FORCC
3195 if (c == color && ip[1])
3196 sum[c] += (pix[c] + pix[ip[1]]) >> 1;
3197 else
3198 sum[c] += pix[ip[0] + c];
3199 num++;
3202 FORCC { /* Save to buffer */
3203 t = pix[color];
3204 if (c != color)
3205 t += (sum[c] - sum[color]) / num;
3206 brow[2][col][c] = CLIP(t);
3209 if (row > 3) /* Write buffer to image */
3210 memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
3211 for (g=0; g < 4; g++)
3212 brow[(g-1) & 3] = brow[g];
3214 memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
3215 memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image);
3216 free (brow[4]);
3219 void CLASS cam_to_cielab (ushort cam[4], float lab[3])
3221 int c, i, j, k;
3222 float r, xyz[3];
3223 static float cbrt[0x10000], xyz_cam[3][4];
3225 if (cam == NULL) {
3226 for (i=0; i < 0x10000; i++) {
3227 r = (float) i / maximum;
3228 cbrt[i] = r > 0.008856 ? pow(r,1/3.0) : 7.787*r + 16/116.0;
3230 for (i=0; i < 3; i++)
3231 for (j=0; j < colors; j++)
3232 for (xyz_cam[i][j] = k=0; k < 3; k++)
3233 xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
3234 } else {
3235 for (i=0; i < 3; i++) {
3236 for (xyz[i]=0.5, c=0; c < colors; c++)
3237 xyz[i] += xyz_cam[i][c] * cam[c];
3238 xyz[i] = cbrt[CLIP((int) xyz[i])];
3240 lab[0] = 116 * xyz[1] - 16;
3241 lab[1] = 500 * (xyz[0] - xyz[1]);
3242 lab[2] = 200 * (xyz[1] - xyz[2]);
3247 Adaptive Homogeneity-Directed interpolation is based on
3248 the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
3250 #define TS 256 /* Tile Size */
3252 void CLASS ahd_interpolate()
3254 int i, j, top, left, row, col, tr, tc, fc, c, d, val, hm[2];
3255 ushort (*pix)[4], (*rix)[3];
3256 static const int dir[4] = { -1, 1, -TS, TS };
3257 unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
3258 float flab[3];
3259 ushort (*rgb)[TS][TS][3];
3260 short (*lab)[TS][TS][3];
3261 char (*homo)[TS][TS], *buffer;
3263 if (verbose) fprintf (stderr, "AHD interpolation...\n");
3265 border_interpolate(3);
3266 buffer = malloc (26*TS*TS); /* 1664 kB */
3267 merror (buffer, "ahd_interpolate()");
3268 rgb = (void *) buffer;
3269 lab = (void *) (buffer + 12*TS*TS);
3270 homo = (void *) (buffer + 24*TS*TS);
3272 for (top=0; top < height; top += TS-6)
3273 for (left=0; left < width; left += TS-6) {
3274 memset (rgb, 0, 12*TS*TS);
3276 /* Interpolate green horizontally and vertically: */
3277 for (row = top < 2 ? 2:top; row < top+TS && row < height-2; row++) {
3278 col = left + (FC(row,left) == 1);
3279 if (col < 2) col += 2;
3280 for (fc = FC(row,col); col < left+TS && col < width-2; col+=2) {
3281 pix = image + row*width+col;
3282 val = ((pix[-1][1] + pix[0][fc] + pix[1][1]) * 2
3283 - pix[-2][fc] - pix[2][fc]) >> 2;
3284 rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
3285 val = ((pix[-width][1] + pix[0][fc] + pix[width][1]) * 2
3286 - pix[-2*width][fc] - pix[2*width][fc]) >> 2;
3287 rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
3290 /* Interpolate red and blue, and convert to CIELab: */
3291 for (d=0; d < 2; d++)
3292 for (row=top+1; row < top+TS-1 && row < height-1; row++)
3293 for (col=left+1; col < left+TS-1 && col < width-1; col++) {
3294 pix = image + row*width+col;
3295 rix = &rgb[d][row-top][col-left];
3296 if ((c = 2 - FC(row,col)) == 1) {
3297 c = FC(row+1,col);
3298 val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
3299 - rix[-1][1] - rix[1][1] ) >> 1);
3300 rix[0][2-c] = CLIP(val);
3301 val = pix[0][1] + (( pix[-width][c] + pix[width][c]
3302 - rix[-TS][1] - rix[TS][1] ) >> 1);
3303 } else
3304 val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
3305 + pix[+width-1][c] + pix[+width+1][c]
3306 - rix[-TS-1][1] - rix[-TS+1][1]
3307 - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
3308 rix[0][c] = CLIP(val);
3309 c = FC(row,col);
3310 rix[0][c] = pix[0][c];
3311 cam_to_cielab (rix[0], flab);
3312 FORC3 lab[d][row-top][col-left][c] = 64*flab[c];
3314 /* Build homogeneity maps from the CIELab images: */
3315 memset (homo, 0, 2*TS*TS);
3316 for (row=top+2; row < top+TS-2 && row < height; row++) {
3317 tr = row-top;
3318 for (col=left+2; col < left+TS-2 && col < width; col++) {
3319 tc = col-left;
3320 for (d=0; d < 2; d++)
3321 for (i=0; i < 4; i++)
3322 ldiff[d][i] = ABS(lab[d][tr][tc][0]-lab[d][tr][tc+dir[i]][0]);
3323 leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
3324 MAX(ldiff[1][2],ldiff[1][3]));
3325 for (d=0; d < 2; d++)
3326 for (i=0; i < 4; i++)
3327 if (i >> 1 == d || ldiff[d][i] <= leps)
3328 abdiff[d][i] = SQR(lab[d][tr][tc][1]-lab[d][tr][tc+dir[i]][1])
3329 + SQR(lab[d][tr][tc][2]-lab[d][tr][tc+dir[i]][2]);
3330 abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
3331 MAX(abdiff[1][2],abdiff[1][3]));
3332 for (d=0; d < 2; d++)
3333 for (i=0; i < 4; i++)
3334 if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
3335 homo[d][tr][tc]++;
3338 /* Combine the most homogenous pixels for the final result: */
3339 for (row=top+3; row < top+TS-3 && row < height-3; row++) {
3340 tr = row-top;
3341 for (col=left+3; col < left+TS-3 && col < width-3; col++) {
3342 tc = col-left;
3343 for (d=0; d < 2; d++)
3344 for (hm[d]=0, i=tr-1; i <= tr+1; i++)
3345 for (j=tc-1; j <= tc+1; j++)
3346 hm[d] += homo[d][i][j];
3347 if (hm[0] != hm[1])
3348 FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
3349 else
3350 FORC3 image[row*width+col][c] =
3351 (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
3355 free (buffer);
3357 #undef TS
3360 Bilateral Filtering was developed by C. Tomasi and R. Manduchi.
3362 void CLASS bilateral_filter()
3364 float (**window)[7], *kernel, scale_r, elut[1024], sum[5];
3365 int c, i, wr, ws, wlast, row, col, y, x;
3366 unsigned sep;
3368 if (verbose) fprintf (stderr, "Bilateral filtering...\n");
3370 wr = ceil(sigma_d*2); /* window radius */
3371 ws = 2*wr + 1; /* window size */
3372 window = calloc ((ws+1)*sizeof *window +
3373 ws*width*sizeof **window + ws*sizeof *kernel, 1);
3374 merror (window, "bilateral_filter()");
3375 for (i=0; i <= ws; i++)
3376 window[i] = (float(*)[7]) (window+ws+1) + i*width;
3377 kernel = (float *) window[ws] + wr;
3378 for (i=-wr; i <= wr; i++)
3379 kernel[i] = 256 / (2*SQR(sigma_d)) * i*i + 0.25;
3380 scale_r = 256 / (2*SQR(sigma_r));
3381 for (i=0; i < 1024; i++)
3382 elut[i] = exp (-i/256.0);
3384 for (wlast=-1, row=0; row < height; row++) {
3385 while (wlast < row+wr) {
3386 wlast++;
3387 for (i=0; i <= ws; i++) /* rotate window rows */
3388 window[(ws+i) % (ws+1)] = window[i];
3389 if (wlast < height)
3390 for (col=0; col < width; col++) {
3391 FORCC window[ws-1][col][c] = image[wlast*width+col][c];
3392 cam_to_cielab (image[wlast*width+col], window[ws-1][col]+4);
3395 for (col=0; col < width; col++) {
3396 memset (sum, 0, sizeof sum);
3397 for (y=-wr; y <= wr; y++)
3398 if ((unsigned)(row+y) < height)
3399 for (x=-wr; x <= wr; x++)
3400 if ((unsigned)(col+x) < width) {
3401 sep = ( SQR(window[wr+y][col+x][4] - window[wr][col][4])
3402 + SQR(window[wr+y][col+x][5] - window[wr][col][5])
3403 + SQR(window[wr+y][col+x][6] - window[wr][col][6]) )
3404 * scale_r + kernel[y] + kernel[x];
3405 if (sep < 1024) {
3406 FORCC sum[c] += elut[sep] * window[wr+y][col+x][c];
3407 sum[4] += elut[sep];
3410 FORCC image[row*width+col][c] = sum[c]/sum[4];
3413 free (window);
3416 void CLASS tiff_get (unsigned base,
3417 unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
3419 *tag = get2();
3420 *type = get2();
3421 *len = get4();
3422 *save = ftell(ifp) + 4;
3423 if (*len * ("1112481124848"[*type < 13 ? *type:0]-'0') > 4)
3424 fseek (ifp, get4()+base, SEEK_SET);
3427 void CLASS parse_olympus_note (int base)
3429 unsigned entries, tag, type, len, save;
3431 entries = get2();
3432 while (entries--) {
3433 tiff_get (base, &tag, &type, &len, &save);
3434 if (tag == 257) thumb_offset = get4();
3435 if (tag == 258) thumb_length = get4();
3436 fseek (ifp, save, SEEK_SET);
3440 void CLASS parse_makernote (int base)
3442 static const uchar xlat[2][256] = {
3443 { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
3444 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
3445 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
3446 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
3447 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
3448 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
3449 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
3450 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
3451 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
3452 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
3453 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
3454 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
3455 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
3456 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
3457 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
3458 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
3459 { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
3460 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
3461 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
3462 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
3463 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
3464 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
3465 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
3466 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
3467 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
3468 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
3469 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
3470 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
3471 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
3472 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
3473 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
3474 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
3475 unsigned offset=0, entries, tag, type, len, save, c;
3476 unsigned ver97=0, serial=0, i;
3477 uchar buf97[324], ci, cj, ck;
3478 short sorder;
3479 char buf[10];
3481 The MakerNote might have its own TIFF header (possibly with
3482 its own byte-order!), or it might just be a table.
3484 sorder = order;
3485 fread (buf, 1, 10, ifp);
3486 if (!strncmp (buf,"KC" ,2) || /* these aren't TIFF format */
3487 !strncmp (buf,"KDK" ,3) ||
3488 !strncmp (buf,"MLY" ,3) ||
3489 !strncmp (buf,"VER" ,3) ||
3490 !strncmp (buf,"IIII",4) ||
3491 !strncmp (buf,"MMMM",4)) return;
3492 if (!strcmp (buf,"Nikon")) {
3493 base = ftell(ifp);
3494 order = get2();
3495 if (get2() != 42) goto quit;
3496 offset = get4();
3497 fseek (ifp, offset-8, SEEK_CUR);
3498 } else if (!strncmp (buf,"FUJIFILM",8) ||
3499 !strncmp (buf,"SONY",4) ||
3500 !strcmp (buf,"Panasonic")) {
3501 order = 0x4949;
3502 fseek (ifp, 2, SEEK_CUR);
3503 } else if (!strcmp (buf,"OLYMP") ||
3504 !strcmp (buf,"LEICA") ||
3505 !strcmp (buf,"Ricoh") ||
3506 !strcmp (buf,"EPSON"))
3507 fseek (ifp, -2, SEEK_CUR);
3508 else if (!strcmp (buf,"AOC") ||
3509 !strcmp (buf,"QVC"))
3510 fseek (ifp, -4, SEEK_CUR);
3511 else fseek (ifp, -10, SEEK_CUR);
3513 entries = get2();
3514 if (entries > 1000) return;
3515 while (entries--) {
3516 tiff_get (base, &tag, &type, &len, &save);
3517 if (tag == 2 && strstr(make,"NIKON"))
3518 iso_speed = (get2(),get2());
3519 if (tag == 4 && len == 27) {
3520 iso_speed = 50 * pow (2, (get4(),get2())/32.0 - 4);
3521 aperture = (get2(), pow (2, get2()/64.0));
3522 shutter = pow (2, ((short) get2())/-32.0);
3524 if (tag == 8 && type == 4)
3525 shot_order = get4();
3526 if (tag == 0xc && len == 4) {
3527 camera_red = getrat();
3528 camera_blue = getrat();
3530 if (tag == 0x14 && len == 2560 && type == 7) {
3531 fseek (ifp, 1248, SEEK_CUR);
3532 goto get2_256;
3534 if (strstr(make,"PENTAX")) {
3535 if (tag == 0x1b) tag = 0x1018;
3536 if (tag == 0x1c) tag = 0x1017;
3538 if (tag == 0x1d)
3539 while ((c = fgetc(ifp)))
3540 serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
3541 if (tag == 0x81 && type == 4) {
3542 data_offset = get4();
3543 fseek (ifp, data_offset + 41, SEEK_SET);
3544 raw_height = get2() * 2;
3545 raw_width = get2();
3546 filters = 0x61616161;
3548 if ((tag == 0x81 && type == 7) ||
3549 (tag == 0x100 && type == 7) ||
3550 (tag == 0x280 && type == 1)) {
3551 thumb_offset = ftell(ifp);
3552 thumb_length = len;
3554 if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
3555 thumb_offset += base;
3556 if (tag == 0x89 && type == 4)
3557 thumb_length = get4();
3558 if (tag == 0x8c)
3559 curve_offset = ftell(ifp) + 2112;
3560 if (tag == 0x96)
3561 curve_offset = ftell(ifp) + 2;
3562 if (tag == 0x97) {
3563 for (i=0; i < 4; i++)
3564 ver97 = (ver97 << 4) + fgetc(ifp)-'0';
3565 switch (ver97) {
3566 case 0x100:
3567 fseek (ifp, 68, SEEK_CUR);
3568 FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
3569 break;
3570 case 0x102:
3571 fseek (ifp, 6, SEEK_CUR);
3572 goto get2_rggb;
3573 case 0x103:
3574 fseek (ifp, 16, SEEK_CUR);
3575 FORC4 cam_mul[c] = get2();
3577 if (ver97 >> 8 == 2) {
3578 if (ver97 != 0x205) fseek (ifp, 280, SEEK_CUR);
3579 fread (buf97, 324, 1, ifp);
3582 if (tag == 0xa7 && ver97 >> 8 == 2) {
3583 ci = xlat[0][serial & 0xff];
3584 cj = xlat[1][fgetc(ifp)^fgetc(ifp)^fgetc(ifp)^fgetc(ifp)];
3585 ck = 0x60;
3586 for (i=0; i < 324; i++)
3587 buf97[i] ^= (cj += ci * ck++);
3588 FORC4 cam_mul[c ^ (c >> 1)] =
3589 sget2 (buf97 + (ver97 == 0x205 ? 14:6) + c*2);
3591 if (tag == 0x200 && len == 4)
3592 black = (get2()+get2()+get2()+get2())/4;
3593 if (tag == 0x201 && len == 4)
3594 goto get2_rggb;
3595 if (tag == 0x401 && len == 4) {
3596 black = (get4()+get4()+get4()+get4())/4;
3598 if (tag == 0xe01) { /* Nikon Capture Note */
3599 type = order;
3600 order = 0x4949;
3601 fseek (ifp, 22, SEEK_CUR);
3602 for (offset=22; offset+22 < len; offset += 22+i) {
3603 tag = get4();
3604 fseek (ifp, 14, SEEK_CUR);
3605 i = get4()-4;
3606 if (tag == 0x76a43207) flip = get2();
3607 else fseek (ifp, i, SEEK_CUR);
3609 order = type;
3611 if (tag == 0xe80 && len == 256 && type == 7) {
3612 fseek (ifp, 48, SEEK_CUR);
3613 camera_red = get2() * 508 * 1.078 / 0x10000;
3614 camera_blue = get2() * 382 * 1.173 / 0x10000;
3616 if (tag == 0xf00 && len == 614 && type == 7) {
3617 fseek (ifp, 176, SEEK_CUR);
3618 goto get2_256;
3620 if (tag == 0x1011 && len == 9 && use_camera_wb) {
3621 for (i=0; i < 3; i++)
3622 FORC3 rgb_cam[i][c] = ((short) get2()) / 256.0;
3623 raw_color = rgb_cam[0][0] < 1;
3625 if (tag == 0x1017)
3626 camera_red = get2() / 256.0;
3627 if (tag == 0x1018)
3628 camera_blue = get2() / 256.0;
3629 if (tag == 0x2011 && len == 2) {
3630 get2_256:
3631 order = 0x4d4d;
3632 camera_red = get2() / 256.0;
3633 camera_blue = get2() / 256.0;
3635 if (tag == 0x2020)
3636 parse_olympus_note (base);
3637 if (tag == 0x4001) {
3638 i = len == 582 ? 50 : len == 653 ? 68 : len == 796 ? 126 : 0;
3639 fseek (ifp, i ,SEEK_CUR);
3640 get2_rggb:
3641 FORC4 cam_mul[c ^ (c >> 1)] = get2();
3643 fseek (ifp, save, SEEK_SET);
3645 quit:
3646 order = sorder;
3650 Since the TIFF DateTime string has no timezone information,
3651 assume that the camera's clock was set to Universal Time.
3653 void CLASS get_timestamp (int reversed)
3655 struct tm t;
3656 char str[20];
3657 int i;
3659 if (timestamp) return;
3660 str[19] = 0;
3661 if (reversed)
3662 for (i=19; i--; ) str[i] = fgetc(ifp);
3663 else
3664 fread (str, 19, 1, ifp);
3665 if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
3666 &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
3667 return;
3668 t.tm_year -= 1900;
3669 t.tm_mon -= 1;
3670 if (mktime(&t) > 0)
3671 timestamp = mktime(&t);
3674 void CLASS parse_exif (int base)
3676 unsigned kodak, entries, tag, type, len, save;
3677 double expo;
3679 kodak = !strncmp(make,"EASTMAN",7);
3680 entries = get2();
3681 while (entries--) {
3682 tiff_get (base, &tag, &type, &len, &save);
3683 switch (tag) {
3684 case 33434: shutter = getrat(); break;
3685 case 33437: aperture = getrat(); break;
3686 case 34855: iso_speed = get2(); break;
3687 case 36867:
3688 case 36868: get_timestamp(0); break;
3689 case 37377: if ((expo = -getrat()) < 128)
3690 shutter = pow (2, expo); break;
3691 case 37378: aperture = pow (2, getrat()/2); break;
3692 case 37386: focal_len = getrat(); break;
3693 case 37500: parse_makernote (base); break;
3694 case 40962: if (kodak) raw_width = get4(); break;
3695 case 40963: if (kodak) raw_height = get4(); break;
3697 fseek (ifp, save, SEEK_SET);
3701 void CLASS parse_mos (int offset)
3703 char data[40];
3704 int skip, from, i, c, neut[4], planes=0, frot=0;
3705 static const char *mod[] = { "Aptus","Valeo","Volare" };
3706 static const unsigned bayer[] =
3707 { 0x94949494, 0x61616161, 0x16161616, 0x49494949 };
3709 fseek (ifp, offset, SEEK_SET);
3710 while (1) {
3711 fread (data, 1, 8, ifp);
3712 if (strcmp(data,"PKTS")) break;
3713 if (!make[0]) strcpy(make,"Leaf");
3714 fread (data, 1, 40, ifp);
3715 skip = get4();
3716 from = ftell(ifp);
3717 if (!strcmp(data,"JPEG_preview_data")) {
3718 thumb_offset = from;
3719 thumb_length = skip;
3721 if (!strcmp(data,"icc_camera_profile")) {
3722 profile_offset = from;
3723 profile_length = skip;
3725 if (!strcmp(data,"CaptProf_serial_number")) {
3726 fread (data, 1, 40, ifp);
3727 for (i=0; i < sizeof mod / sizeof *mod; i++)
3728 if (data[0] == mod[i][0] && data[1] == toupper(mod[i][1]))
3729 sprintf (model, "%s %d", mod[i], atoi(data+2));
3731 if (!strcmp(data,"CaptProf_number_of_planes"))
3732 fscanf (ifp, "%d", &planes);
3733 if (!strcmp(data,"CaptProf_raw_data_rotation"))
3734 fscanf (ifp, "%d", &flip);
3735 if (!strcmp(data,"CaptProf_mosaic_pattern"))
3736 FORC4 {
3737 fscanf (ifp, "%d", &i);
3738 if (i == 1) frot = c ^ (c >> 1);
3740 if (!strcmp(data,"ImgProf_rotation_angle")) {
3741 fscanf (ifp, "%d", &i);
3742 flip = i - flip;
3744 if (!strcmp(data,"NeutObj_neutrals")) {
3745 FORC4 fscanf (ifp, "%d", neut+c);
3746 FORC3 cam_mul[c] = (float) neut[0] / neut[c+1];
3748 parse_mos (from);
3749 fseek (ifp, skip+from, SEEK_SET);
3751 if (planes)
3752 filters = (planes == 1) * bayer[(flip/90 + frot) & 3];
3755 int CLASS parse_tiff_ifd (int base, int level)
3757 unsigned entries, tag, type, len, plen=16, save;
3758 int ifd, use_cm=0, cfa, i, j, c;
3759 char software[64], *cbuf, *cp;
3760 uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
3761 double dblack, cc[4][4], cm[4][3], cam_xyz[4][3];
3762 double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
3763 unsigned *buf, sony_offset=0, sony_length=0, sony_key=0;
3764 struct jhead jh;
3765 FILE *sfp;
3767 if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
3768 return 1;
3769 ifd = tiff_nifds++;
3770 for (j=0; j < 4; j++)
3771 for (i=0; i < 4; i++)
3772 cc[j][i] = i == j;
3773 entries = get2();
3774 if (entries > 512) return 1;
3775 while (entries--) {
3776 tiff_get (base, &tag, &type, &len, &save);
3777 switch (tag) {
3778 case 17: case 18:
3779 if (type == 3 && len == 1)
3780 cam_mul[(tag-17)*2] = get2() / 256.0;
3781 break;
3782 case 36: case 37: case 38:
3783 cam_mul[tag-0x24] = get2();
3784 break;
3785 case 39:
3786 if (len < 50 || cam_mul[0]) break;
3787 fseek (ifp, 12, SEEK_CUR);
3788 FORC3 cam_mul[c] = get2();
3789 break;
3790 case 2: case 256: /* ImageWidth */
3791 tiff_ifd[ifd].width = getint(type);
3792 break;
3793 case 3: case 257: /* ImageHeight */
3794 tiff_ifd[ifd].height = getint(type);
3795 break;
3796 case 258: /* BitsPerSample */
3797 tiff_ifd[ifd].samples = len;
3798 tiff_ifd[ifd].bps = get2();
3799 break;
3800 case 259: /* Compression */
3801 tiff_ifd[ifd].comp = get2();
3802 break;
3803 case 262: /* PhotometricInterpretation */
3804 tiff_ifd[ifd].phint = get2();
3805 break;
3806 case 271: /* Make */
3807 fgets (make, 64, ifp);
3808 break;
3809 case 272: /* Model */
3810 fgets (model, 64, ifp);
3811 break;
3812 case 273: /* StripOffset */
3813 case 513:
3814 tiff_ifd[ifd].offset = get4()+base;
3815 if (!tiff_ifd[ifd].width) {
3816 fseek (ifp, tiff_ifd[ifd].offset, SEEK_SET);
3817 if (ljpeg_start (&jh, 1)) {
3818 tiff_ifd[ifd].comp = 6;
3819 tiff_ifd[ifd].width = jh.wide << (jh.clrs == 2);
3820 tiff_ifd[ifd].height = jh.high;
3821 tiff_ifd[ifd].bps = jh.bits;
3822 tiff_ifd[ifd].samples = jh.clrs;
3825 break;
3826 case 274: /* Orientation */
3827 tiff_ifd[ifd].flip = "50132467"[get2() & 7]-'0';
3828 break;
3829 case 277: /* SamplesPerPixel */
3830 tiff_ifd[ifd].samples = getint(type);
3831 break;
3832 case 279: /* StripByteCounts */
3833 case 514:
3834 tiff_ifd[ifd].bytes = get4();
3835 break;
3836 case 305: /* Software */
3837 fgets (software, 64, ifp);
3838 if (!strncmp(software,"Adobe",5) ||
3839 !strncmp(software,"Bibble",6) ||
3840 !strcmp (software,"Digital Photo Professional"))
3841 is_raw = 0;
3842 break;
3843 case 306: /* DateTime */
3844 get_timestamp(0);
3845 break;
3846 case 324: /* TileOffsets */
3847 tiff_ifd[ifd].offset = level ? ftell(ifp) : get4();
3848 break;
3849 case 330: /* SubIFDs */
3850 while (len--) {
3851 i = ftell(ifp);
3852 fseek (ifp, get4()+base, SEEK_SET);
3853 if (parse_tiff_ifd (base, level+1)) break;
3854 fseek (ifp, i+4, SEEK_SET);
3856 break;
3857 case 400:
3858 strcpy (make, "Sarnoff");
3859 maximum = 0xfff;
3860 break;
3861 case 29184: sony_offset = get4(); break;
3862 case 29185: sony_length = get4(); break;
3863 case 29217: sony_key = get4(); break;
3864 case 29443:
3865 FORC4 cam_mul[c ^ (c < 2)] = get2();
3866 break;
3867 case 33405: /* Model2 */
3868 fgets (model2, 64, ifp);
3869 break;
3870 case 33422: /* CFAPattern */
3871 case 64777: /* Kodak P-series */
3872 if ((plen=len) > 16) plen = 16;
3873 fread (cfa_pat, 1, plen, ifp);
3874 for (colors=cfa=i=0; i < plen; i++) {
3875 colors += !(cfa & (1 << cfa_pat[i]));
3876 cfa |= 1 << cfa_pat[i];
3878 if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
3879 if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
3880 goto guess_cfa_pc;
3881 case 33434: /* ExposureTime */
3882 shutter = getrat();
3883 break;
3884 case 33437: /* FNumber */
3885 aperture = getrat();
3886 break;
3887 case 34310: /* Leaf metadata */
3888 parse_mos (ftell(ifp));
3889 break;
3890 case 34665: /* EXIF tag */
3891 fseek (ifp, get4()+base, SEEK_SET);
3892 parse_exif (base);
3893 break;
3894 case 34675: /* InterColorProfile */
3895 case 50831: /* AsShotICCProfile */
3896 profile_offset = ftell(ifp);
3897 profile_length = len;
3898 break;
3899 case 37122: /* CompressedBitsPerPixel */
3900 kodak_cbpp = get4();
3901 break;
3902 case 37386: /* FocalLength */
3903 focal_len = getrat();
3904 break;
3905 case 37393: /* ImageNumber */
3906 shot_order = getint(type);
3907 break;
3908 case 37400: /* old Kodak KDC tag */
3909 for (raw_color = i=0; i < 3; i++) {
3910 getrat();
3911 FORC3 rgb_cam[i][c] = getrat();
3913 break;
3914 case 46275: /* Imacon tags */
3915 strcpy (make, "Imacon");
3916 data_offset = ftell(ifp);
3917 break;
3918 case 46279:
3919 fseek (ifp, 78, SEEK_CUR);
3920 raw_width = get4();
3921 raw_height = get4();
3922 break;
3923 case 50454: /* Sinar tag */
3924 case 50455:
3925 if (!(cbuf = malloc(len))) break;
3926 fread (cbuf, 1, len, ifp);
3927 for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
3928 if (!strncmp (++cp,"Neutral ",8))
3929 sscanf (cp+8, "%f %f %f", cam_mul, cam_mul+1, cam_mul+2);
3930 free (cbuf);
3931 break;
3932 case 50706: /* DNGVersion */
3933 FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
3934 break;
3935 case 50710: /* CFAPlaneColor */
3936 if (len > 4) len = 4;
3937 colors = len;
3938 fread (cfa_pc, 1, colors, ifp);
3939 guess_cfa_pc:
3940 FORCC tab[cfa_pc[c]] = c;
3941 cdesc[c] = 0;
3942 for (i=16; i--; )
3943 filters = filters << 2 | tab[cfa_pat[i % plen]];
3944 break;
3945 case 50711: /* CFALayout */
3946 if (get2() == 2) {
3947 fuji_width = 1;
3948 filters = 0x49494949;
3950 break;
3951 case 291:
3952 case 2317:
3953 case 50712: /* LinearizationTable */
3954 if (len > 0x1000)
3955 len = 0x1000;
3956 read_shorts (curve, len);
3957 for (i=len; i < 0x1000; i++)
3958 curve[i] = curve[i-1];
3959 maximum = curve[0xfff];
3960 break;
3961 case 50714: /* BlackLevel */
3962 case 50715: /* BlackLevelDeltaH */
3963 case 50716: /* BlackLevelDeltaV */
3964 for (dblack=i=0; i < len; i++)
3965 dblack += getreal(type);
3966 black += dblack/len + 0.5;
3967 break;
3968 case 50717: /* WhiteLevel */
3969 maximum = getint(type);
3970 break;
3971 case 50718: /* DefaultScale */
3972 i = get4();
3973 j = get4() * get4();
3974 i *= get4();
3975 if (i > j) xmag = i / j;
3976 else ymag = j / i;
3977 break;
3978 case 50721: /* ColorMatrix1 */
3979 case 50722: /* ColorMatrix2 */
3980 FORCC for (j=0; j < 3; j++)
3981 cm[c][j] = getrat();
3982 use_cm = 1;
3983 break;
3984 case 50723: /* CameraCalibration1 */
3985 case 50724: /* CameraCalibration2 */
3986 for (i=0; i < colors; i++)
3987 FORCC cc[i][c] = getrat();
3988 case 50727: /* AnalogBalance */
3989 FORCC ab[c] = getrat();
3990 break;
3991 case 50728: /* AsShotNeutral */
3992 FORCC asn[c] = getreal(type);
3993 break;
3994 case 50729: /* AsShotWhiteXY */
3995 xyz[0] = getrat();
3996 xyz[1] = getrat();
3997 xyz[2] = 1 - xyz[0] - xyz[1];
3998 FORC3 xyz[c] /= d65_white[c];
3999 break;
4000 case 50740: /* DNGPrivateData */
4001 if (dng_version) break;
4002 fseek (ifp, get4()+base, SEEK_SET);
4003 parse_tiff_ifd (base, level+1);
4004 break;
4005 case 50752:
4006 read_shorts (cr2_slice, 3);
4007 break;
4008 case 50829: /* ActiveArea */
4009 top_margin = getint(type);
4010 left_margin = getint(type);
4011 height = getint(type) - top_margin;
4012 width = getint(type) - left_margin;
4013 break;
4014 case 64772: /* Kodak P-series */
4015 fseek (ifp, 16, SEEK_CUR);
4016 data_offset = get4();
4017 fseek (ifp, 28, SEEK_CUR);
4018 data_offset += get4();
4019 load_raw = packed_12_load_raw;
4021 fseek (ifp, save, SEEK_SET);
4023 if (sony_length && (buf = malloc(sony_length))) {
4024 fseek (ifp, sony_offset, SEEK_SET);
4025 fread (buf, sony_length, 1, ifp);
4026 sony_decrypt (buf, sony_length/4, 1, sony_key);
4027 sfp = ifp;
4028 if ((ifp = tmpfile())) {
4029 fwrite (buf, sony_length, 1, ifp);
4030 fseek (ifp, 0, SEEK_SET);
4031 parse_tiff_ifd (-sony_offset, level);
4032 fclose (ifp);
4034 ifp = sfp;
4035 free (buf);
4037 for (i=0; i < colors; i++)
4038 FORCC cc[i][c] *= ab[i];
4039 if (use_cm) {
4040 FORCC for (i=0; i < 3; i++)
4041 for (cam_xyz[c][i]=j=0; j < colors; j++)
4042 cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
4043 cam_xyz_coeff (cam_xyz);
4045 if (asn[0])
4046 FORCC pre_mul[c] = 1 / asn[c];
4047 if (!use_cm)
4048 FORCC pre_mul[c] /= cc[c][c];
4049 return 0;
4052 void CLASS parse_tiff (int base)
4054 int doff, max_samp=0, raw=-1, thm=-1, i;
4055 struct jhead jh;
4057 fseek (ifp, base, SEEK_SET);
4058 order = get2();
4059 if (order != 0x4949 && order != 0x4d4d) return;
4060 get2();
4061 memset (tiff_ifd, 0, sizeof tiff_ifd);
4062 tiff_nifds = 0;
4063 while ((doff = get4())) {
4064 fseek (ifp, doff+base, SEEK_SET);
4065 if (parse_tiff_ifd (base, 0)) break;
4067 if (!dng_version && !strncmp(make,"Kodak",5)) {
4068 fseek (ifp, 12+base, SEEK_SET);
4069 parse_tiff_ifd (base, 2);
4071 thumb_misc = 16;
4072 if (thumb_offset) {
4073 fseek (ifp, thumb_offset, SEEK_SET);
4074 if (ljpeg_start (&jh, 1)) {
4075 thumb_misc = jh.bits;
4076 thumb_width = jh.wide;
4077 thumb_height = jh.high;
4080 for (i=0; i < tiff_nifds; i++) {
4081 if (max_samp < tiff_ifd[i].samples)
4082 max_samp = tiff_ifd[i].samples;
4083 if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
4084 tiff_ifd[i].width*tiff_ifd[i].height > raw_width*raw_height) {
4085 raw_width = tiff_ifd[i].width;
4086 raw_height = tiff_ifd[i].height;
4087 tiff_bps = tiff_ifd[i].bps;
4088 tiff_compress = tiff_ifd[i].comp;
4089 data_offset = tiff_ifd[i].offset;
4090 tiff_flip = tiff_ifd[i].flip;
4091 tiff_samples = tiff_ifd[i].samples;
4092 fuji_secondary = tiff_samples == 2;
4093 raw = i;
4096 fuji_width *= (raw_width+1)/2;
4097 if (tiff_ifd[0].flip) tiff_flip = tiff_ifd[0].flip;
4098 if (raw >= 0) {
4099 if (tiff_compress < 2)
4100 load_raw = tiff_bps > 8 ? unpacked_load_raw : eight_bit_load_raw;
4101 if (tiff_compress/2 == 3)
4102 load_raw = lossless_jpeg_load_raw;
4103 if (tiff_compress == 32773)
4104 load_raw = packed_12_load_raw;
4105 if (tiff_compress == 65000)
4106 switch (tiff_ifd[raw].phint) {
4107 case 2: load_raw = kodak_rgb_load_raw; filters = 0; break;
4108 case 6: load_raw = kodak_ycbcr_load_raw; filters = 0; break;
4109 case 32803: load_raw = kodak_65000_load_raw;
4112 if (tiff_samples == 3 && tiff_bps == 8)
4113 if (!dng_version) is_raw = 0;
4114 for (i=0; i < tiff_nifds; i++)
4115 if (i != raw && tiff_ifd[i].samples == max_samp &&
4116 tiff_ifd[i].width * tiff_ifd[i].height / SQR(tiff_ifd[i].bps+1) >
4117 thumb_width * thumb_height / SQR(thumb_misc+1)) {
4118 thumb_width = tiff_ifd[i].width;
4119 thumb_height = tiff_ifd[i].height;
4120 thumb_offset = tiff_ifd[i].offset;
4121 thumb_length = tiff_ifd[i].bytes;
4122 thumb_misc = tiff_ifd[i].bps;
4123 thm = i;
4125 if (thm >= 0) {
4126 thumb_misc |= tiff_ifd[thm].samples << 5;
4127 switch (tiff_ifd[thm].comp) {
4128 case 0:
4129 write_thumb = layer_thumb;
4130 break;
4131 case 1:
4132 if (tiff_ifd[thm].bps > 8)
4133 thumb_load_raw = kodak_thumb_load_raw;
4134 else
4135 write_thumb = ppm_thumb;
4136 break;
4137 case 65000:
4138 thumb_load_raw = tiff_ifd[thm].phint == 6 ?
4139 kodak_ycbcr_load_raw : kodak_rgb_load_raw;
4144 void CLASS parse_minolta()
4146 int save, tag, len, offset, high=0, wide=0, i, c;
4148 fseek (ifp, 4, SEEK_SET);
4149 offset = get4() + 8;
4150 while ((save=ftell(ifp)) < offset) {
4151 tag = get4();
4152 len = get4();
4153 switch (tag) {
4154 case 0x505244: /* PRD */
4155 fseek (ifp, 8, SEEK_CUR);
4156 high = get2();
4157 wide = get2();
4158 break;
4159 case 0x574247: /* WBG */
4160 get4();
4161 i = strstr(model,"A200") ? 3:0;
4162 FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
4163 break;
4164 case 0x545457: /* TTW */
4165 parse_tiff (ftell(ifp));
4167 fseek (ifp, save+len+8, SEEK_SET);
4169 raw_height = high;
4170 raw_width = wide;
4171 data_offset = offset;
4175 Many cameras have a "debug mode" that writes JPEG and raw
4176 at the same time. The raw file has no header, so try to
4177 to open the matching JPEG file and read its metadata.
4179 void CLASS parse_external_jpeg()
4181 char *file, *ext, *jname, *jfile, *jext;
4182 FILE *save=ifp;
4184 ext = strrchr (ifname, '.');
4185 file = strrchr (ifname, '/');
4186 if (!file) file = strrchr (ifname, '\\');
4187 if (!file) file = ifname-1;
4188 file++;
4189 if (!ext || strlen(ext) != 4 || ext-file != 8) return;
4190 jname = malloc (strlen(ifname) + 1);
4191 merror (jname, "parse_external()");
4192 strcpy (jname, ifname);
4193 jfile = file - ifname + jname;
4194 jext = ext - ifname + jname;
4195 if (strcasecmp (ext, ".jpg")) {
4196 strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
4197 memcpy (jfile, file+4, 4);
4198 memcpy (jfile+4, file, 4);
4199 } else
4200 while (isdigit(*--jext)) {
4201 if (*jext != '9') {
4202 (*jext)++;
4203 break;
4205 *jext = '0';
4207 if (strcmp (jname, ifname)) {
4208 if ((ifp = fopen (jname, "rb"))) {
4209 if (verbose)
4210 fprintf (stderr, "Reading metadata from %s...\n", jname);
4211 parse_tiff (12);
4212 thumb_offset = 0;
4213 is_raw = 1;
4214 fclose (ifp);
4217 if (!timestamp)
4218 fprintf (stderr, "Failed to read metadata from %s\n", jname);
4219 free (jname);
4220 ifp = save;
4224 CIFF block 0x1030 contains an 8x8 white sample.
4225 Load this into white[][] for use in scale_colors().
4227 void CLASS ciff_block_1030()
4229 static const ushort key[] = { 0x410, 0x45f3 };
4230 int i, bpp, row, col, vbits=0;
4231 unsigned long bitbuf=0;
4233 if ((get2(),get4()) != 0x80008 || !get4()) return;
4234 bpp = get2();
4235 if (bpp != 10 && bpp != 12) return;
4236 for (i=row=0; row < 8; row++)
4237 for (col=0; col < 8; col++) {
4238 if (vbits < bpp) {
4239 bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
4240 vbits += 16;
4242 white[row][col] =
4243 bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp);
4244 vbits -= bpp;
4249 Parse a CIFF file, better known as Canon CRW format.
4251 void CLASS parse_ciff (int offset, int length)
4253 int tboff, nrecs, i, c, type, len, roff, aoff, save, wbi=-1;
4254 ushort key[] = { 0x410, 0x45f3 };
4256 if (strcmp(model,"Canon PowerShot G6") &&
4257 strcmp(model,"Canon PowerShot S60") &&
4258 strcmp(model,"Canon PowerShot S70") &&
4259 strcmp(model,"Canon PowerShot Pro1"))
4260 key[0] = key[1] = 0;
4261 fseek (ifp, offset+length-4, SEEK_SET);
4262 tboff = get4() + offset;
4263 fseek (ifp, tboff, SEEK_SET);
4264 nrecs = get2();
4265 if (nrecs > 100) return;
4266 for (i = 0; i < nrecs; i++) {
4267 type = get2();
4268 len = get4();
4269 roff = get4();
4270 aoff = offset + roff;
4271 save = ftell(ifp);
4272 if (type == 0x080a) { /* Get the camera make and model */
4273 fseek (ifp, aoff, SEEK_SET);
4274 fread (make, 64, 1, ifp);
4275 fseek (ifp, aoff+strlen(make)+1, SEEK_SET);
4276 fread (model, 64, 1, ifp);
4278 if (type == 0x102a) { /* Exposure info */
4279 fseek (ifp, aoff+4, SEEK_SET);
4280 iso_speed = 50 * pow (2, get2()/32.0 - 4);
4281 aperture = (get2(), pow (2, get2()/64.0));
4282 shutter = pow (2, ((short) get2())/-32.0);
4283 wbi = (get2(),get2());
4284 if (wbi > 17) wbi = 0;
4285 if (((!strcmp(model,"Canon EOS DIGITAL REBEL") ||
4286 !strcmp(model,"Canon EOS 300D DIGITAL"))) && wbi == 6)
4287 wbi++;
4289 if (type == 0x102c) { /* Get white balance (G2) */
4290 if (!strcmp(model,"Canon PowerShot G1") ||
4291 !strcmp(model,"Canon PowerShot Pro90 IS")) {
4292 fseek (ifp, aoff+120, SEEK_SET);
4293 FORC4 cam_mul[c ^ 2] = get2();
4294 } else {
4295 fseek (ifp, aoff+100, SEEK_SET);
4296 goto common;
4299 if (type == 0x0032) { /* Get white balance (D30 & G3) */
4300 if (!strcmp(model,"Canon EOS D30")) {
4301 fseek (ifp, aoff+72, SEEK_SET);
4302 common:
4303 camera_red = get2() ^ key[0];
4304 camera_red =(get2() ^ key[1]) / camera_red;
4305 camera_blue = get2() ^ key[0];
4306 camera_blue /= get2() ^ key[1];
4307 if (!wbi) camera_red = -1; /* Use my auto WB for this photo */
4308 } else if (key[0]) {
4309 fseek (ifp, aoff+96 + 8*("01345:000000006008"[wbi]-'0'), SEEK_SET);
4310 goto common;
4311 } else {
4312 fseek (ifp, aoff+80 + (wbi < 6 ? 8*("123451"[wbi]-'0') : 0), SEEK_SET);
4313 if (!camera_red)
4314 goto common;
4317 if (type == 0x10a9) { /* Get white balance (D60) */
4318 if (!strcmp(model,"Canon EOS 10D"))
4319 wbi = "0134560028"[wbi]-'0';
4320 fseek (ifp, aoff+2 + wbi*8, SEEK_SET);
4321 camera_red = get2();
4322 camera_red /= get2();
4323 camera_blue = get2();
4324 camera_blue = get2() / camera_blue;
4326 if (type == 0x1030 && (wbi == 6 || wbi == 15)) {
4327 fseek (ifp, aoff, SEEK_SET); /* Get white sample */
4328 ciff_block_1030();
4330 if (type == 0x1031) { /* Get the raw width and height */
4331 fseek (ifp, aoff+2, SEEK_SET);
4332 raw_width = get2();
4333 raw_height = get2();
4335 if (type == 0x180e) { /* Get the timestamp */
4336 fseek (ifp, aoff, SEEK_SET);
4337 timestamp = get4();
4339 if (type == 0x580e)
4340 timestamp = len;
4341 #ifdef LOCALTIME
4342 if ((type | 0x4000) == 0x580e)
4343 timestamp = mktime (gmtime (&timestamp));
4344 #endif
4345 if (type == 0x5813)
4346 flash_used = int_to_float(len);
4347 if (type == 0x5814)
4348 canon_ev = int_to_float(len);
4349 if (type == 0x5817)
4350 shot_order = len;
4351 if (type == 0x1810) { /* Get the rotation */
4352 fseek (ifp, aoff+12, SEEK_SET);
4353 flip = get4();
4355 if (type == 0x1818) {
4356 fseek (ifp, aoff+4, SEEK_SET);
4357 shutter = pow (2, -int_to_float(get4()));
4358 aperture = pow (2, int_to_float(get4())/2);
4360 if (type == 0x1835) { /* Get the decoder table */
4361 fseek (ifp, aoff, SEEK_SET);
4362 tiff_compress = get4();
4364 if (type == 0x2007) { /* Found the JPEG thumbnail */
4365 thumb_offset = aoff;
4366 thumb_length = len;
4368 if (type >> 8 == 0x28 || type >> 8 == 0x30) /* Get sub-tables */
4369 parse_ciff(aoff, len);
4370 fseek (ifp, save, SEEK_SET);
4374 void CLASS parse_rollei()
4376 char line[128], *val;
4377 struct tm t;
4379 fseek (ifp, 0, SEEK_SET);
4380 do {
4381 fgets (line, 128, ifp);
4382 if ((val = strchr(line,'=')))
4383 *val++ = 0;
4384 else
4385 val = line + strlen(line);
4386 if (!strcmp(line,"DAT"))
4387 sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
4388 if (!strcmp(line,"TIM"))
4389 sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
4390 if (!strcmp(line,"HDR"))
4391 thumb_offset = atoi(val);
4392 if (!strcmp(line,"X "))
4393 raw_width = atoi(val);
4394 if (!strcmp(line,"Y "))
4395 raw_height = atoi(val);
4396 if (!strcmp(line,"TX "))
4397 thumb_width = atoi(val);
4398 if (!strcmp(line,"TY "))
4399 thumb_height = atoi(val);
4400 } while (strncmp(line,"EOHD",4));
4401 data_offset = thumb_offset + thumb_width * thumb_height * 2;
4402 t.tm_year -= 1900;
4403 t.tm_mon -= 1;
4404 if (mktime(&t) > 0)
4405 timestamp = mktime(&t);
4406 strcpy (make, "Rollei");
4407 strcpy (model,"d530flex");
4408 write_thumb = rollei_thumb;
4411 void CLASS parse_phase_one (int base)
4413 unsigned entries, tag, type, len, data, save, i, c;
4414 char *cp;
4416 fseek (ifp, base, SEEK_SET);
4417 order = get4() & 0xffff;
4418 if (get4() >> 8 != 0x526177) return; /* "Raw" */
4419 fseek (ifp, base+get4(), SEEK_SET);
4420 entries = get4();
4421 get4();
4422 while (entries--) {
4423 tag = get4();
4424 type = get4();
4425 len = get4();
4426 data = get4();
4427 save = ftell(ifp);
4428 fseek (ifp, base+data, SEEK_SET);
4429 switch (tag) {
4430 case 0x100: flip = "0653"[data & 3]-'0'; break;
4431 case 0x106:
4432 for (raw_color = i=0; i < 3; i++)
4433 FORC3 rgb_cam[i][c] = int_to_float(get4());
4434 break;
4435 case 0x107:
4436 FORC3 cam_mul[c] = pre_mul[c] = int_to_float(get4());
4437 break;
4438 case 0x108: raw_width = data; break;
4439 case 0x109: raw_height = data; break;
4440 case 0x10a: left_margin = data; break;
4441 case 0x10b: top_margin = data; break;
4442 case 0x10c: width = data; break;
4443 case 0x10d: height = data; break;
4444 case 0x10e: tiff_compress = data; break;
4445 case 0x10f: data_offset = data+base; break;
4446 case 0x110: meta_offset = data+base;
4447 meta_length = len; break;
4448 case 0x112: curve_offset = save - 4; break;
4449 case 0x21c: strip_offset = data+base; break;
4450 case 0x301:
4451 model[63] = 0;
4452 fread (model, 1, 63, ifp);
4453 if ((cp = strstr(model," camera"))) *cp = 0;
4455 fseek (ifp, save, SEEK_SET);
4457 load_raw = tiff_compress < 3 ?
4458 phase_one_load_raw : phase_one_load_raw_c;
4459 strcpy (make, "Phase One");
4460 if (model[0]) return;
4461 switch (raw_height) {
4462 case 2060: strcpy (model,"LightPhase"); break;
4463 case 2682: strcpy (model,"H 10"); break;
4464 case 4128: strcpy (model,"H 20"); break;
4465 case 5488: strcpy (model,"H 25"); break;
4469 void CLASS parse_fuji (int offset)
4471 unsigned entries, tag, len, save, c;
4473 fseek (ifp, offset, SEEK_SET);
4474 entries = get4();
4475 if (entries > 255) return;
4476 while (entries--) {
4477 tag = get2();
4478 len = get2();
4479 save = ftell(ifp);
4480 if (tag == 0x100) {
4481 raw_height = get2();
4482 raw_width = get2();
4483 } else if (tag == 0x121) {
4484 height = get2();
4485 if ((width = get2()) == 4284) width += 3;
4486 } else if (tag == 0x130)
4487 fuji_layout = fgetc(ifp) >> 7;
4488 if (tag == 0x2ff0)
4489 FORC4 cam_mul[c ^ 1] = get2();
4490 fseek (ifp, save+len, SEEK_SET);
4492 if (fuji_layout) {
4493 height *= 2;
4494 width /= 2;
4498 int CLASS parse_jpeg (int offset)
4500 int len, save, hlen, mark;
4502 fseek (ifp, offset, SEEK_SET);
4503 if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
4505 while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
4506 order = 0x4d4d;
4507 len = get2() - 2;
4508 save = ftell(ifp);
4509 if (mark == 0xc0 || mark == 0xc3) {
4510 fgetc(ifp);
4511 raw_height = get2();
4512 raw_width = get2();
4514 order = get2();
4515 hlen = get4();
4516 if (get4() == 0x48454150) /* "HEAP" */
4517 parse_ciff (save+hlen, len-hlen);
4518 parse_tiff (save+6);
4519 fseek (ifp, save+len, SEEK_SET);
4521 return 1;
4524 void CLASS parse_riff()
4526 unsigned i, size, end;
4527 char tag[4], date[64], month[64];
4528 static const char mon[12][4] =
4529 { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
4530 struct tm t;
4532 order = 0x4949;
4533 fread (tag, 4, 1, ifp);
4534 size = get4();
4535 if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
4536 end = ftell(ifp) + size;
4537 get4();
4538 while (ftell(ifp) < end)
4539 parse_riff();
4540 } else if (!memcmp(tag,"IDIT",4) && size < 64) {
4541 fread (date, 64, 1, ifp);
4542 date[size] = 0;
4543 if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
4544 &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
4545 for (i=0; i < 12 && strcmp(mon[i],month); i++);
4546 t.tm_mon = i;
4547 t.tm_year -= 1900;
4548 if (mktime(&t) > 0)
4549 timestamp = mktime(&t);
4551 } else
4552 fseek (ifp, size, SEEK_CUR);
4555 void CLASS parse_smal (int offset, int fsize)
4557 int ver;
4559 fseek (ifp, offset+2, SEEK_SET);
4560 order = 0x4949;
4561 ver = fgetc(ifp);
4562 if (ver == 6)
4563 fseek (ifp, 5, SEEK_CUR);
4564 if (get4() != fsize) return;
4565 if (ver > 6) data_offset = get4();
4566 raw_height = height = get2();
4567 raw_width = width = get2();
4568 strcpy (make, "SMaL");
4569 sprintf (model, "v%d %dx%d", ver, width, height);
4570 if (ver == 6) load_raw = smal_v6_load_raw;
4571 if (ver == 9) load_raw = smal_v9_load_raw;
4574 char * CLASS foveon_gets (int offset, char *str, int len)
4576 int i;
4577 fseek (ifp, offset, SEEK_SET);
4578 for (i=0; i < len-1; i++)
4579 if ((str[i] = get2()) == 0) break;
4580 str[i] = 0;
4581 return str;
4584 void CLASS parse_foveon()
4586 int entries, img=0, off, len, tag, save, i, wide, high, pent, poff[256][2];
4587 char name[64], value[64];
4589 order = 0x4949; /* Little-endian */
4590 fseek (ifp, 36, SEEK_SET);
4591 flip = get4();
4592 fseek (ifp, -4, SEEK_END);
4593 fseek (ifp, get4(), SEEK_SET);
4594 if (get4() != 0x64434553) return; /* SECd */
4595 entries = (get4(),get4());
4596 while (entries--) {
4597 off = get4();
4598 len = get4();
4599 tag = get4();
4600 save = ftell(ifp);
4601 fseek (ifp, off, SEEK_SET);
4602 if (get4() != (0x20434553 | (tag << 24))) return;
4603 switch (tag) {
4604 case 0x47414d49: /* IMAG */
4605 case 0x32414d49: /* IMA2 */
4606 fseek (ifp, 12, SEEK_CUR);
4607 wide = get4();
4608 high = get4();
4609 if (wide > raw_width && high > raw_height) {
4610 raw_width = wide;
4611 raw_height = high;
4612 data_offset = off+24;
4614 fseek (ifp, off+28, SEEK_SET);
4615 if (fgetc(ifp) == 0xff && fgetc(ifp) == 0xd8) {
4616 thumb_offset = off+28;
4617 thumb_length = len-28;
4619 if (++img == 2 && !thumb_length) {
4620 thumb_offset = off+24;
4621 thumb_width = wide;
4622 thumb_height = high;
4623 write_thumb = foveon_thumb;
4625 break;
4626 case 0x464d4143: /* CAMF */
4627 meta_offset = off+24;
4628 meta_length = len-28;
4629 if (meta_length > 0x20000)
4630 meta_length = 0x20000;
4631 break;
4632 case 0x504f5250: /* PROP */
4633 pent = (get4(),get4());
4634 fseek (ifp, 12, SEEK_CUR);
4635 off += pent*8 + 24;
4636 if (pent > 256) pent=256;
4637 for (i=0; i < pent*2; i++)
4638 poff[0][i] = off + get4()*2;
4639 for (i=0; i < pent; i++) {
4640 foveon_gets (poff[i][0], name, 64);
4641 foveon_gets (poff[i][1], value, 64);
4642 if (!strcmp (name, "ISO"))
4643 iso_speed = atoi(value);
4644 if (!strcmp (name, "CAMMANUF"))
4645 strcpy (make, value);
4646 if (!strcmp (name, "CAMMODEL"))
4647 strcpy (model, value);
4648 if (!strcmp (name, "WB_DESC"))
4649 strcpy (model2, value);
4650 if (!strcmp (name, "TIME"))
4651 timestamp = atoi(value);
4652 if (!strcmp (name, "EXPTIME"))
4653 shutter = atoi(value) / 1000000.0;
4654 if (!strcmp (name, "APERTURE"))
4655 aperture = atof(value);
4656 if (!strcmp (name, "FLENGTH"))
4657 focal_len = atof(value);
4659 #ifdef LOCALTIME
4660 timestamp = mktime (gmtime (&timestamp));
4661 #endif
4663 fseek (ifp, save, SEEK_SET);
4665 is_foveon = 1;
4669 Thanks to Adobe for providing these excellent CAM -> XYZ matrices!
4671 void CLASS adobe_coeff (char *make, char *model)
4673 static const struct {
4674 const char *prefix;
4675 short black, trans[12];
4676 } table[] = {
4677 { "Canon EOS D2000", 0,
4678 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
4679 { "Canon EOS D6000", 0,
4680 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
4681 { "Canon EOS D30", 0,
4682 { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
4683 { "Canon EOS D60", 0,
4684 { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
4685 { "Canon EOS 5D", 0,
4686 { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
4687 { "Canon EOS 20Da", 0,
4688 { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
4689 { "Canon EOS 20D", 0,
4690 { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
4691 { "Canon EOS 350D", 0,
4692 { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
4693 { "Canon EOS DIGITAL REBEL XT", 0,
4694 { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
4695 { "Canon EOS-1Ds Mark II", 0,
4696 { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
4697 { "Canon EOS-1D Mark II N", 0,
4698 { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
4699 { "Canon EOS-1D Mark II", 0,
4700 { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
4701 { "Canon EOS-1DS", 0,
4702 { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
4703 { "Canon EOS-1D", 0,
4704 { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
4705 { "Canon EOS", 0,
4706 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
4707 { "Canon PowerShot A50", 0,
4708 { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
4709 { "Canon PowerShot A5", 0,
4710 { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
4711 { "Canon PowerShot G1", 0,
4712 { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
4713 { "Canon PowerShot G2", 0,
4714 { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
4715 { "Canon PowerShot G3", 0,
4716 { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
4717 { "Canon PowerShot G5", 0,
4718 { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
4719 { "Canon PowerShot G6", 0,
4720 { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
4721 { "Canon PowerShot Pro1", 0,
4722 { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
4723 { "Canon PowerShot Pro70", 34,
4724 { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
4725 { "Canon PowerShot Pro90", 0,
4726 { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
4727 { "Canon PowerShot S30", 0,
4728 { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
4729 { "Canon PowerShot S40", 0,
4730 { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
4731 { "Canon PowerShot S45", 0,
4732 { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
4733 { "Canon PowerShot S50", 0,
4734 { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
4735 { "Canon PowerShot S60", 0,
4736 { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
4737 { "Canon PowerShot S70", 0,
4738 { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
4739 { "Contax N Digital", 0,
4740 { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
4741 { "EPSON R-D1", 0,
4742 { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
4743 { "FUJIFILM FinePix E550", 0,
4744 { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
4745 { "FUJIFILM FinePix E900", 0,
4746 { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
4747 { "FUJIFILM FinePix F8", 0,
4748 { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
4749 { "FUJIFILM FinePix F7", 0,
4750 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
4751 { "FUJIFILM FinePix S20Pro", 0,
4752 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
4753 { "FUJIFILM FinePix S2Pro", 128,
4754 { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
4755 { "FUJIFILM FinePix S3Pro", 0,
4756 { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
4757 { "FUJIFILM FinePix S5000", 0,
4758 { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
4759 { "FUJIFILM FinePix S5100", 0,
4760 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
4761 { "FUJIFILM FinePix S5500", 0,
4762 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
4763 { "FUJIFILM FinePix S5200", 0,
4764 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
4765 { "FUJIFILM FinePix S5600", 0,
4766 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
4767 { "FUJIFILM FinePix S7000", 0,
4768 { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
4769 { "FUJIFILM FinePix S9", 0,
4770 { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
4771 { "KODAK NC2000F", 0, /* DJC */
4772 { 16475,-6903,-1218,-851,10375,477,2505,-7,1020 } },
4773 { "Kodak DCS315C", 8,
4774 { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
4775 { "Kodak DCS330C", 8,
4776 { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
4777 { "KODAK DCS420", 0,
4778 { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
4779 { "KODAK DCS460", 0,
4780 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
4781 { "KODAK EOSDCS1", 0,
4782 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
4783 { "KODAK EOSDCS3B", 0,
4784 { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
4785 { "Kodak DCS520C", 180,
4786 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
4787 { "Kodak DCS560C", 188,
4788 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
4789 { "Kodak DCS620C", 180,
4790 { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
4791 { "Kodak DCS620X", 185,
4792 { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
4793 { "Kodak DCS660C", 214,
4794 { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
4795 { "Kodak DCS720X", 0,
4796 { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
4797 { "Kodak DCS760C", 0,
4798 { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
4799 { "Kodak DCS Pro SLR", 0,
4800 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
4801 { "Kodak DCS Pro 14nx", 0,
4802 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
4803 { "Kodak DCS Pro 14", 0,
4804 { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
4805 { "Kodak ProBack645", 0,
4806 { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
4807 { "Kodak ProBack", 0,
4808 { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
4809 { "KODAK P850", 0,
4810 { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
4811 { "KODAK P880", 0,
4812 { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
4813 { "LEICA DIGILUX 2", 0,
4814 { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
4815 { "Leaf", 0,
4816 { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
4817 { "Minolta DiMAGE 5", 0,
4818 { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
4819 { "Minolta DiMAGE 7Hi", 0,
4820 { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
4821 { "Minolta DiMAGE 7", 0,
4822 { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
4823 { "Minolta DiMAGE A1", 0,
4824 { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
4825 { "MINOLTA DiMAGE A200", 0,
4826 { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
4827 { "Minolta DiMAGE A2", 0,
4828 { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
4829 { "Minolta DiMAGE Z2", 0, /* DJC */
4830 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
4831 { "MINOLTA DYNAX 5", 0,
4832 { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
4833 { "MINOLTA DYNAX 7", 0,
4834 { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
4835 { "NIKON D100", 0,
4836 { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
4837 { "NIKON D1H", 0,
4838 { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
4839 { "NIKON D1X", 0,
4840 { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
4841 { "NIKON D1", 0, /* multiplied by 2.218750, 1.0, 1.148438 */
4842 { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
4843 { "NIKON D2H", 0,
4844 { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
4845 { "NIKON D2X", 0,
4846 { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
4847 { "NIKON D50", 0,
4848 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
4849 { "NIKON D70", 0,
4850 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
4851 { "NIKON D200", 0,
4852 { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
4853 { "NIKON E950", 0, /* DJC */
4854 { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
4855 { "NIKON E995", 0, /* copied from E5000 */
4856 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
4857 { "NIKON E2500", 0,
4858 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
4859 { "NIKON E4300", 0, /* copied from Minolta DiMAGE Z2 */
4860 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
4861 { "NIKON E4500", 0,
4862 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
4863 { "NIKON E5000", 0,
4864 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
4865 { "NIKON E5400", 0,
4866 { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
4867 { "NIKON E5700", 0,
4868 { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
4869 { "NIKON E8400", 0,
4870 { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
4871 { "NIKON E8700", 0,
4872 { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
4873 { "NIKON E8800", 0,
4874 { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
4875 { "OLYMPUS C5050", 0,
4876 { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
4877 { "OLYMPUS C5060", 0,
4878 { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
4879 { "OLYMPUS C7070", 0,
4880 { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
4881 { "OLYMPUS C70", 0,
4882 { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
4883 { "OLYMPUS C80", 0,
4884 { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
4885 { "OLYMPUS E-10", 0,
4886 { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
4887 { "OLYMPUS E-1", 0,
4888 { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
4889 { "OLYMPUS E-20", 0,
4890 { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
4891 { "OLYMPUS E-300", 0,
4892 { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
4893 { "OLYMPUS E-500", 0,
4894 { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
4895 { "OLYMPUS SP500UZ", 0,
4896 { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
4897 { "PENTAX *ist DL", 0,
4898 { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
4899 { "PENTAX *ist DS2", 0,
4900 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
4901 { "PENTAX *ist DS", 0,
4902 { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
4903 { "PENTAX *ist D", 0,
4904 { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
4905 { "Panasonic DMC-FZ30", 0,
4906 { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
4907 { "Panasonic DMC-LC1", 0,
4908 { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
4909 { "Panasonic DMC-LX1", 0,
4910 { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
4911 { "SONY DSC-F828", 491,
4912 { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
4913 { "SONY DSC-R1", 512,
4914 { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
4915 { "SONY DSC-V3", 0,
4916 { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } }
4918 double cam_xyz[4][3];
4919 char name[130];
4920 int i, j;
4922 sprintf (name, "%s %s", make, model);
4923 for (i=0; i < sizeof table / sizeof *table; i++)
4924 if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
4925 if (table[i].black)
4926 black = table[i].black;
4927 for (j=0; j < 12; j++)
4928 cam_xyz[0][j] = table[i].trans[j] / 10000.0;
4929 cam_xyz_coeff (cam_xyz);
4930 break;
4934 void CLASS simple_coeff (int index)
4936 static const float table[][12] = {
4937 /* index 0 -- all Foveon cameras */
4938 { 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
4939 /* index 1 -- Kodak DC20 and DC25 */
4940 { 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
4941 /* index 2 -- Logitech Fotoman Pixtura */
4942 { 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
4943 /* index 3 -- Nikon E880, E900, and E990 */
4944 { -1.936280, 1.800443, -1.448486, 2.584324,
4945 1.405365, -0.524955, -0.289090, 0.408680,
4946 -1.204965, 1.082304, 2.941367, -1.818705 }
4948 int i, c;
4950 for (raw_color = i=0; i < 3; i++)
4951 FORCC rgb_cam[i][c] = table[index][i*colors+c];
4954 short CLASS guess_byte_order (int words)
4956 uchar test[4][2];
4957 int t=2, msb;
4958 double diff, sum[2] = {0,0};
4960 fread (test[0], 2, 2, ifp);
4961 for (words-=2; words--; ) {
4962 fread (test[t], 2, 1, ifp);
4963 for (msb=0; msb < 2; msb++) {
4964 diff = (test[t^2][msb] << 8 | test[t^2][!msb])
4965 - (test[t ][msb] << 8 | test[t ][!msb]);
4966 sum[msb] += diff*diff;
4968 t = (t+1) & 3;
4970 return sum[0] < sum[1] ? 0x4d4d : 0x4949;
4974 Identify which camera created this file, and set global variables
4975 accordingly.
4977 void CLASS identify()
4979 char head[32], *cp;
4980 unsigned hlen, fsize, i, c, is_canon;
4981 struct jhead jh;
4982 static const struct {
4983 int fsize;
4984 char make[12], model[15], withjpeg;
4985 } table[] = {
4986 { 62464, "Kodak", "DC20" ,0 },
4987 { 124928, "Kodak", "DC20" ,0 },
4988 { 311696, "ST Micro", "STV680 VGA" ,0 }, /* SPYz */
4989 { 614400, "Kodak", "KAI-0340" ,0 },
4990 { 787456, "Creative", "PC-CAM 600" ,0 },
4991 { 1138688, "Minolta", "RD175" ,0 },
4992 { 3840000, "Foculus", "531C" ,0 },
4993 { 1447680, "AVT", "F-145C" ,0 },
4994 { 1920000, "AVT", "F-201C" ,0 },
4995 { 5067304, "AVT", "F-510C" ,0 },
4996 { 10134608, "AVT", "F-510C" ,0 },
4997 { 16157136, "AVT", "F-810C" ,0 },
4998 { 6624000, "Pixelink", "A782" ,0 },
4999 { 13248000, "Pixelink", "A782" ,0 },
5000 { 6291456, "RoverShot","3320AF" ,0 },
5001 { 5939200, "OLYMPUS", "C770UZ" ,0 },
5002 { 1581060, "NIKON", "E900" ,1 }, /* or E900s,E910 */
5003 { 2465792, "NIKON", "E950" ,1 }, /* or E800,E700 */
5004 { 2940928, "NIKON", "E2100" ,1 }, /* or E2500 */
5005 { 4771840, "NIKON", "E990" ,1 }, /* or E995, Oly C3030Z */
5006 { 4775936, "NIKON", "E3700" ,1 }, /* or Optio 33WR */
5007 { 5869568, "NIKON", "E4300" ,1 }, /* or DiMAGE Z2 */
5008 { 5865472, "NIKON", "E4500" ,1 },
5009 { 7438336, "NIKON", "E5000" ,1 }, /* or E5700 */
5010 { 1976352, "CASIO", "QV-2000UX" ,1 },
5011 { 3217760, "CASIO", "QV-3*00EX" ,1 },
5012 { 6218368, "CASIO", "QV-5700" ,1 },
5013 { 7530816, "CASIO", "QV-R51" ,1 },
5014 { 7684000, "CASIO", "QV-4000" ,1 },
5015 { 4948608, "CASIO", "EX-S100" ,1 },
5016 { 7542528, "CASIO", "EX-Z50" ,1 },
5017 { 7753344, "CASIO", "EX-Z55" ,1 },
5018 { 7426656, "CASIO", "EX-P505" ,1 },
5019 { 9313536, "CASIO", "EX-P600" ,1 },
5020 { 10979200, "CASIO", "EX-P700" ,1 },
5021 { 3178560, "PENTAX", "Optio S" ,1 }, /* 8-bit */
5022 { 4841984, "PENTAX", "Optio S" ,1 }, /* 12-bit */
5023 { 6114240, "PENTAX", "Optio S4" ,1 }, /* or S4i */
5024 { 12582980, "Sinar", "" ,0 } };
5025 static const char *corp[] =
5026 { "Canon", "NIKON", "EPSON", "KODAK", "Kodak", "OLYMPUS", "PENTAX",
5027 "MINOLTA", "Minolta", "Konica", "CASIO", "Sinar", "Phase One" };
5029 tiff_flip = flip = filters = -1; /* 0 is valid, so -1 is unknown */
5030 raw_height = raw_width = fuji_width = cr2_slice[0] = 0;
5031 maximum = height = width = top_margin = left_margin = 0;
5032 make[0] = model[0] = model2[0] = cdesc[0] = 0;
5033 iso_speed = shutter = aperture = focal_len = 0;
5034 memset (white, 0, sizeof white);
5035 thumb_offset = thumb_length = thumb_width = thumb_height = 0;
5036 load_raw = thumb_load_raw = NULL;
5037 write_thumb = jpeg_thumb;
5038 data_offset = meta_length = tiff_bps = tiff_compress = 0;
5039 kodak_cbpp = zero_after_ff = dng_version = fuji_secondary = 0;
5040 timestamp = shot_order = tiff_samples = black = is_foveon = 0;
5041 is_raw = raw_color = use_gamma = xmag = ymag = 1;
5042 for (i=0; i < 4; i++) {
5043 cam_mul[i] = i == 1;
5044 pre_mul[i] = i < 3;
5045 FORC3 rgb_cam[c][i] = c == i;
5047 colors = 3;
5048 tiff_bps = 12;
5049 for (i=0; i < 0x1000; i++) curve[i] = i;
5050 profile_length = 0;
5052 order = get2();
5053 hlen = get4();
5054 fseek (ifp, 0, SEEK_SET);
5055 fread (head, 1, 32, ifp);
5056 fseek (ifp, 0, SEEK_END);
5057 fsize = ftell(ifp);
5058 if ((cp = (char *) memmem (head, 32, "MMMM", 4)) ||
5059 (cp = (char *) memmem (head, 32, "IIII", 4))) {
5060 parse_phase_one (cp-head);
5061 if (cp-head) parse_tiff(0);
5062 } else if (order == 0x4949 || order == 0x4d4d) {
5063 if (!memcmp (head+6,"HEAPCCDR",8)) {
5064 data_offset = hlen;
5065 parse_ciff (hlen, fsize - hlen);
5066 } else {
5067 parse_tiff(0);
5069 } else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
5070 !memcmp (head+6,"Exif",4)) {
5071 fseek (ifp, 4, SEEK_SET);
5072 fseek (ifp, 4 + get2(), SEEK_SET);
5073 if (fgetc(ifp) != 0xff)
5074 parse_tiff(12);
5075 thumb_offset = 0;
5076 } else if (!memcmp (head,"BM",2) &&
5077 head[26] == 1 && head[28] == 16 && head[30] == 0) {
5078 data_offset = 0x1000;
5079 order = 0x4949;
5080 fseek (ifp, 38, SEEK_SET);
5081 if (get4() == 2834 && get4() == 2834 && get4() == 0 && get4() == 4096) {
5082 strcpy (model, "BMQ");
5083 flip = 3;
5084 goto nucore;
5086 } else if (!memcmp (head,"BR",2)) {
5087 strcpy (model, "RAW");
5088 nucore:
5089 strcpy (make, "Nucore");
5090 order = 0x4949;
5091 fseek (ifp, 10, SEEK_SET);
5092 data_offset += get4();
5093 raw_width = (get4(),get4());
5094 raw_height = get4();
5095 if (model[0] == 'B' && raw_width == 2597) {
5096 raw_width++;
5097 data_offset -= 0x1000;
5099 } else if (!memcmp (head+25,"ARECOYK",7)) {
5100 strcpy (make, "Contax");
5101 strcpy (model,"N Digital");
5102 fseek (ifp, 33, SEEK_SET);
5103 get_timestamp(1);
5104 fseek (ifp, 60, SEEK_SET);
5105 FORC4 cam_mul[c ^ (c >> 1)] = get4();
5106 } else if (!strcmp (head, "PXN")) {
5107 strcpy (make, "Logitech");
5108 strcpy (model,"Fotoman Pixtura");
5109 } else if (!memcmp (head,"FUJIFILM",8)) {
5110 fseek (ifp, 84, SEEK_SET);
5111 thumb_offset = get4();
5112 thumb_length = get4();
5113 fseek (ifp, 92, SEEK_SET);
5114 parse_fuji (get4());
5115 if (thumb_offset > 120) {
5116 fseek (ifp, 120, SEEK_SET);
5117 fuji_secondary = (i = get4()) && 1;
5118 if (fuji_secondary && use_secondary)
5119 parse_fuji (i);
5121 fseek (ifp, 100, SEEK_SET);
5122 data_offset = get4();
5123 parse_tiff (thumb_offset+12);
5124 } else if (!memcmp (head,"RIFF",4)) {
5125 fseek (ifp, 0, SEEK_SET);
5126 parse_riff();
5127 } else if (!memcmp (head,"DSC-Image",9))
5128 parse_rollei();
5129 else if (!memcmp (head,"\0MRM",4))
5130 parse_minolta();
5131 else if (!memcmp (head,"FOVb",4))
5132 parse_foveon();
5133 else
5134 for (i=0; i < sizeof table / sizeof *table; i++)
5135 if (fsize == table[i].fsize) {
5136 strcpy (make, table[i].make );
5137 strcpy (model, table[i].model);
5138 if (table[i].withjpeg)
5139 parse_external_jpeg();
5141 parse_mos(8);
5142 parse_mos(3472);
5143 if (make[0] == 0) parse_smal (0, fsize);
5144 if (make[0] == 0) parse_jpeg (is_raw = 0);
5146 for (i=0; i < sizeof corp / sizeof *corp; i++)
5147 if (strstr (make, corp[i])) /* Simplify company names */
5148 strcpy (make, corp[i]);
5149 if (!strncmp (make,"KODAK",5))
5150 make[16] = model[16] = 0;
5151 cp = make + strlen(make); /* Remove trailing spaces */
5152 while (*--cp == ' ') *cp = 0;
5153 cp = model + strlen(model);
5154 while (*--cp == ' ') *cp = 0;
5155 i = strlen(make); /* Remove make from model */
5156 if (!strncmp (model, make, i) && model[i++] == ' ')
5157 memmove (model, model+i, 64-i);
5158 make[63] = model[63] = model2[63] = 0;
5159 if (!is_raw) return;
5161 if ((raw_height | raw_width) < 0)
5162 raw_height = raw_width = 0;
5163 if (!maximum) maximum = (1 << tiff_bps) - 1;
5164 if (!height) height = raw_height;
5165 if (!width) width = raw_width;
5166 if (fuji_width) {
5167 width = height + fuji_width;
5168 height = width - 1;
5169 xmag = ymag = 1;
5171 if (dng_version) {
5172 strcat (model," DNG");
5173 if (filters == UINT_MAX) filters = 0;
5174 if (!filters)
5175 colors = tiff_samples;
5176 if (tiff_compress == 1)
5177 load_raw = adobe_dng_load_raw_nc;
5178 if (tiff_compress == 7)
5179 load_raw = adobe_dng_load_raw_lj;
5180 FORC4 cam_mul[c] = pre_mul[c];
5181 goto dng_skip;
5184 /* We'll try to decode anything from Canon or Nikon. */
5186 if ((is_canon = !strcmp(make,"Canon"))) {
5187 load_raw = memcmp (head+6,"HEAPCCDR",8) ?
5188 lossless_jpeg_load_raw : canon_compressed_load_raw;
5189 maximum = 0xfff;
5191 if (!strcmp(make,"NIKON"))
5192 load_raw = nikon_is_compressed() ?
5193 nikon_compressed_load_raw : nikon_load_raw;
5194 if (!strncmp (make,"OLYMPUS",7))
5195 height += height & 1;
5197 /* Set parameters based on camera name (for non-DNG files). */
5199 if (is_foveon) {
5200 if (height*2 < width) ymag = 2;
5201 if (height > width) xmag = 2;
5202 filters = 0;
5203 load_raw = foveon_load_raw;
5204 simple_coeff(0);
5205 } else if (!strcmp(model,"PowerShot 600")) {
5206 height = 613;
5207 width = 854;
5208 colors = 4;
5209 filters = 0xe1e4e1e4;
5210 load_raw = canon_600_load_raw;
5211 } else if (!strcmp(model,"PowerShot A5") ||
5212 !strcmp(model,"PowerShot A5 Zoom")) {
5213 height = 773;
5214 width = 960;
5215 raw_width = 992;
5216 colors = 4;
5217 filters = 0x1e4e1e4e;
5218 load_raw = canon_a5_load_raw;
5219 } else if (!strcmp(model,"PowerShot A50")) {
5220 height = 968;
5221 width = 1290;
5222 raw_width = 1320;
5223 colors = 4;
5224 filters = 0x1b4e4b1e;
5225 load_raw = canon_a5_load_raw;
5226 } else if (!strcmp(model,"PowerShot Pro70")) {
5227 height = 1024;
5228 width = 1552;
5229 colors = 4;
5230 filters = 0x1e4b4e1b;
5231 load_raw = canon_a5_load_raw;
5232 } else if (!strcmp(model,"PowerShot Pro90 IS")) {
5233 width = 1896;
5234 colors = 4;
5235 filters = 0xb4b4b4b4;
5236 } else if (is_canon && raw_width == 2144) {
5237 height = 1550;
5238 width = 2088;
5239 top_margin = 8;
5240 left_margin = 4;
5241 if (!strcmp(model,"PowerShot G1")) {
5242 colors = 4;
5243 filters = 0xb4b4b4b4;
5245 } else if (is_canon && raw_width == 2224) {
5246 height = 1448;
5247 width = 2176;
5248 top_margin = 6;
5249 left_margin = 48;
5250 } else if (is_canon && raw_width == 2376) {
5251 height = 1720;
5252 width = 2312;
5253 top_margin = 6;
5254 left_margin = 12;
5255 } else if (is_canon && raw_width == 2672) {
5256 height = 1960;
5257 width = 2616;
5258 top_margin = 6;
5259 left_margin = 12;
5260 } else if (is_canon && raw_width == 3152) {
5261 height = 2056;
5262 width = 3088;
5263 top_margin = 12;
5264 left_margin = 64;
5265 maximum = 0xfa0;
5266 } else if (is_canon && raw_width == 3160) {
5267 height = 2328;
5268 width = 3112;
5269 top_margin = 12;
5270 left_margin = 44;
5271 } else if (is_canon && raw_width == 3344) {
5272 height = 2472;
5273 width = 3288;
5274 top_margin = 6;
5275 left_margin = 4;
5276 } else if (!strcmp(model,"EOS D2000C")) {
5277 filters = 0x61616161;
5278 black = curve[200];
5279 } else if (is_canon && raw_width == 3516) {
5280 top_margin = 14;
5281 left_margin = 42;
5282 goto canon_cr2;
5283 } else if (is_canon && raw_width == 3596) {
5284 top_margin = 12;
5285 left_margin = 74;
5286 goto canon_cr2;
5287 } else if (is_canon && raw_width == 4476) {
5288 top_margin = 34;
5289 left_margin = 90;
5290 goto canon_cr2;
5291 } else if (is_canon && raw_width == 5108) {
5292 top_margin = 13;
5293 left_margin = 98;
5294 maximum = 0xe80;
5295 canon_cr2:
5296 height = raw_height - top_margin;
5297 width = raw_width - left_margin;
5298 } else if (!strcmp(model,"D1")) {
5299 camera_red *= 256/527.0;
5300 camera_blue *= 256/317.0;
5301 } else if (!strcmp(model,"D1X")) {
5302 width -= 4;
5303 ymag = 2;
5304 } else if (!strncmp(model,"D50",3) || !strncmp(model,"D70",3)) {
5305 width--;
5306 maximum = 0xf53;
5307 } else if (!strcmp(model,"D100")) {
5308 if (tiff_compress == 34713 && load_raw == nikon_load_raw)
5309 raw_width = (width += 3) + 3;
5310 maximum = 0xf44;
5311 } else if (!strncmp(model,"D2H",3)) {
5312 left_margin = 6;
5313 width -= 14;
5314 } else if (!strcmp(model,"D2X")) {
5315 width -= 8;
5316 } else if (fsize == 1581060) {
5317 height = 963;
5318 width = 1287;
5319 raw_width = 1632;
5320 load_raw = nikon_e900_load_raw;
5321 maximum = 0x3f4;
5322 colors = 4;
5323 filters = 0x1e1e1e1e;
5324 simple_coeff(3);
5325 pre_mul[0] = 1.2085;
5326 pre_mul[1] = 1.0943;
5327 pre_mul[3] = 1.1103;
5328 } else if (fsize == 2465792) {
5329 height = 1203;
5330 width = 1616;
5331 raw_width = 2048;
5332 load_raw = nikon_e900_load_raw;
5333 maximum = 0x3dd;
5334 colors = 4;
5335 filters = 0x4b4b4b4b;
5336 adobe_coeff ("NIKON","E950");
5337 } else if (fsize == 4771840) {
5338 height = 1540;
5339 width = 2064;
5340 colors = 4;
5341 filters = 0xe1e1e1e1;
5342 load_raw = nikon_load_raw;
5343 if (!timestamp && nikon_e995())
5344 strcpy (model, "E995");
5345 if (strcmp(model,"E995")) {
5346 filters = 0xb4b4b4b4;
5347 simple_coeff(3);
5348 pre_mul[0] = 1.196;
5349 pre_mul[1] = 1.246;
5350 pre_mul[2] = 1.018;
5352 } else if (!strcmp(model,"E2100")) {
5353 if (!timestamp && !nikon_e2100()) goto cp_e2500;
5354 height = 1206;
5355 width = 1616;
5356 load_raw = nikon_e2100_load_raw;
5357 pre_mul[0] = 1.945;
5358 pre_mul[2] = 1.040;
5359 } else if (!strcmp(model,"E2500")) {
5360 cp_e2500:
5361 strcpy (model, "E2500");
5362 height = 1204;
5363 width = 1616;
5364 colors = 4;
5365 filters = 0x4b4b4b4b;
5366 } else if (fsize == 4775936) {
5367 height = 1542;
5368 width = 2064;
5369 load_raw = nikon_e2100_load_raw;
5370 pre_mul[0] = 1.818;
5371 pre_mul[2] = 1.618;
5372 if ((i = nikon_3700()) == 2) {
5373 strcpy (make, "OLYMPUS");
5374 strcpy (model, "C740UZ");
5375 } else if (i == 0) {
5376 strcpy (make, "PENTAX");
5377 strcpy (model,"Optio 33WR");
5378 flip = 1;
5379 filters = 0x16161616;
5380 pre_mul[0] = 1.331;
5381 pre_mul[2] = 1.820;
5383 } else if (fsize == 5869568) {
5384 height = 1710;
5385 width = 2288;
5386 filters = 0x16161616;
5387 if (!timestamp && minolta_z2()) {
5388 strcpy (make, "Minolta");
5389 strcpy (model,"DiMAGE Z2");
5391 if (make[0] == 'M')
5392 load_raw = nikon_e2100_load_raw;
5393 } else if (!strcmp(model,"E4500")) {
5394 height = 1708;
5395 width = 2288;
5396 colors = 4;
5397 filters = 0xb4b4b4b4;
5398 } else if (fsize == 7438336) {
5399 height = 1924;
5400 width = 2576;
5401 colors = 4;
5402 filters = 0xb4b4b4b4;
5403 } else if (!strcmp(model,"R-D1")) {
5404 tiff_compress = 34713;
5405 load_raw = nikon_load_raw;
5406 } else if (!strcmp(model,"FinePix S5100") ||
5407 !strcmp(model,"FinePix S5500")) {
5408 load_raw = unpacked_load_raw;
5409 maximum = 0x3e00;
5410 } else if (!strncmp(model,"FinePix",7)) {
5411 if (!strcmp(model+7,"S2Pro")) {
5412 strcpy (model+7," S2Pro");
5413 height = 2144;
5414 width = 2880;
5415 flip = 6;
5416 } else
5417 maximum = 0x3e00;
5418 top_margin = (raw_height - height)/2;
5419 left_margin = (raw_width - width )/2;
5420 data_offset += (top_margin*raw_width + left_margin) * 2;
5421 if (fuji_secondary)
5422 data_offset += use_secondary * ( strcmp(model+7," S3Pro")
5423 ? (raw_width *= 2) : raw_height*raw_width*2 );
5424 fuji_width = width >> !fuji_layout;
5425 width = (height >> fuji_layout) + fuji_width;
5426 raw_height = height;
5427 height = width - 1;
5428 load_raw = fuji_load_raw;
5429 if (!(fuji_width & 1)) filters = 0x49494949;
5430 } else if (!strcmp(model,"RD175")) {
5431 height = 986;
5432 width = 1534;
5433 data_offset = 513;
5434 filters = 0x61616161;
5435 load_raw = minolta_rd175_load_raw;
5436 } else if (!strcmp(model,"Digital Camera KD-400Z")) {
5437 height = 1712;
5438 width = 2312;
5439 raw_width = 2336;
5440 data_offset = 4034;
5441 fseek (ifp, 2032, SEEK_SET);
5442 goto konica_400z;
5443 } else if (!strcmp(model,"Digital Camera KD-510Z")) {
5444 data_offset = 4032;
5445 pre_mul[0] = 1.297;
5446 pre_mul[2] = 1.438;
5447 fseek (ifp, 2032, SEEK_SET);
5448 goto konica_510z;
5449 } else if (!strcasecmp(make,"MINOLTA")) {
5450 load_raw = unpacked_load_raw;
5451 maximum = 0xf7d;
5452 if (!strncmp(model,"DiMAGE A",8)) {
5453 if (!strcmp(model,"DiMAGE A200"))
5454 filters = 0x49494949;
5455 load_raw = packed_12_load_raw;
5456 maximum = model[8] == '1' ? 0xf8b : 0xfff;
5457 } else if (!strncmp(model,"ALPHA",5) ||
5458 !strncmp(model,"DYNAX",5) ||
5459 !strncmp(model,"MAXXUM",6)) {
5460 sprintf (model, "DYNAX %s", model+6 + (model[0]=='M'));
5461 load_raw = packed_12_load_raw;
5462 maximum = 0xffb;
5463 } else if (!strncmp(model,"DiMAGE G",8)) {
5464 if (model[8] == '4') {
5465 data_offset = 5056;
5466 pre_mul[0] = 1.602;
5467 pre_mul[2] = 1.441;
5468 fseek (ifp, 2078, SEEK_SET);
5469 height = 1716;
5470 width = 2304;
5471 } else if (model[8] == '5') {
5472 data_offset = 4016;
5473 fseek (ifp, 1936, SEEK_SET);
5474 konica_510z:
5475 height = 1956;
5476 width = 2607;
5477 raw_width = 2624;
5478 } else if (model[8] == '6') {
5479 data_offset = 4032;
5480 fseek (ifp, 2030, SEEK_SET);
5481 height = 2136;
5482 width = 2848;
5484 filters = 0x61616161;
5485 konica_400z:
5486 load_raw = unpacked_load_raw;
5487 maximum = 0x3df;
5488 order = 0x4d4d;
5489 FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
5491 if (pre_mul[0] == 1 && pre_mul[2] == 1) {
5492 pre_mul[0] = 1.42;
5493 pre_mul[2] = 1.25;
5495 } else if (!strcmp(model,"*ist DS")) {
5496 height -= 2;
5497 } else if (!strcmp(model,"Optio S")) {
5498 if (fsize == 3178560) {
5499 height = 1540;
5500 width = 2064;
5501 load_raw = eight_bit_load_raw;
5502 camera_red *= 4;
5503 camera_blue *= 4;
5504 pre_mul[0] = 1.391;
5505 pre_mul[2] = 1.188;
5506 } else {
5507 height = 1544;
5508 width = 2068;
5509 raw_width = 3136;
5510 load_raw = packed_12_load_raw;
5511 maximum = 0xf7c;
5512 pre_mul[0] = 1.137;
5513 pre_mul[2] = 1.453;
5515 } else if (!strncmp(model,"Optio S4",8)) {
5516 height = 1737;
5517 width = 2324;
5518 raw_width = 3520;
5519 load_raw = packed_12_load_raw;
5520 maximum = 0xf7a;
5521 pre_mul[0] = 1.980;
5522 pre_mul[2] = 1.570;
5523 } else if (!strcmp(model,"STV680 VGA")) {
5524 height = 484;
5525 width = 644;
5526 load_raw = eight_bit_load_raw;
5527 flip = 2;
5528 filters = 0x16161616;
5529 black = 16;
5530 pre_mul[0] = 1.097;
5531 pre_mul[2] = 1.128;
5532 } else if (!strcmp(model,"KAI-0340")) {
5533 height = 477;
5534 width = 640;
5535 order = 0x4949;
5536 data_offset = 3840;
5537 load_raw = unpacked_load_raw;
5538 pre_mul[0] = 1.561;
5539 pre_mul[2] = 2.454;
5540 } else if (!strcmp(model,"531C")) {
5541 height = 1200;
5542 width = 1600;
5543 load_raw = unpacked_load_raw;
5544 filters = 0x49494949;
5545 pre_mul[1] = 1.218;
5546 } else if (!strcmp(model,"F-145C")) {
5547 height = 1040;
5548 width = 1392;
5549 load_raw = eight_bit_load_raw;
5550 } else if (!strcmp(model,"F-201C")) {
5551 height = 1200;
5552 width = 1600;
5553 load_raw = eight_bit_load_raw;
5554 } else if (!strcmp(model,"F-510C")) {
5555 height = 1958;
5556 width = 2588;
5557 load_raw = (fsize < 7500000) ? eight_bit_load_raw : unpacked_load_raw;
5558 maximum = 0xfff0;
5559 } else if (!strcmp(model,"F-810C")) {
5560 height = 2469;
5561 width = 3272;
5562 load_raw = unpacked_load_raw;
5563 maximum = 0xfff0;
5564 } else if (!strcmp(model,"A782")) {
5565 height = 3000;
5566 width = 2208;
5567 filters = 0x61616161;
5568 load_raw = (fsize < 10000000) ? eight_bit_load_raw : unpacked_load_raw;
5569 maximum = 0xffc0;
5570 } else if (!strcmp(model,"3320AF")) {
5571 height = 1536;
5572 raw_width = width = 2048;
5573 filters = 0x61616161;
5574 load_raw = unpacked_load_raw;
5575 maximum = 0x3ff;
5576 pre_mul[0] = 1.717;
5577 pre_mul[2] = 1.138;
5578 fseek (ifp, 0x300000, SEEK_SET);
5579 if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
5580 data_offset = (2048 * 16 + 28) * 2;
5581 height -= 16;
5582 width -= 28;
5583 maximum = 0xf5c0;
5584 strcpy (make, "ISG");
5585 model[0] = 0;
5587 } else if (!strcmp(make,"Imacon")) {
5588 sprintf (model, "Ixpress %d-Mp", height*width/1000000);
5589 if (raw_width < 4096) {
5590 data_offset += 6 + raw_width*12;
5591 height = raw_height - 6;
5592 width = raw_width - 10;
5593 filters = 0x61616161;
5594 flip = height > width+10 ? 5:3;
5596 load_raw = unpacked_load_raw;
5597 maximum = 0xffff;
5598 pre_mul[0] = 1.963;
5599 pre_mul[2] = 1.430;
5600 } else if (!strcmp(make,"Sinar")) {
5601 if (!memcmp(head,"8BPS",4)) {
5602 fseek (ifp, 14, SEEK_SET);
5603 height = get4();
5604 width = get4();
5605 filters = 0x61616161;
5606 data_offset = 68;
5608 load_raw = unpacked_load_raw;
5609 maximum = 0x3fff;
5610 } else if (!strcmp(make,"Leaf")) {
5611 if (tiff_compress == 99)
5612 load_raw = lossless_jpeg_load_raw;
5613 maximum = 0x3fff;
5614 if (filters == 0) {
5615 strcpy (model, "Volare");
5616 load_raw = hdr_load_raw;
5617 maximum = 0xffff;
5618 raw_color = 0;
5620 } else if (!strcmp(make,"LEICA") || !strcmp(make,"Panasonic")) {
5621 if (width == 3880) {
5622 data_offset += 12;
5623 maximum = 0xf7f0;
5624 width -= 22;
5625 } else if (width == 3304) {
5626 maximum = 0xf94c;
5627 width -= 16;
5628 } else maximum = 0xfff0;
5629 load_raw = unpacked_load_raw;
5630 } else if (!strcmp(model,"P 30") || !strcmp(model,"P 45")) {
5631 black = 256;
5632 } else if (!strcmp(model,"E-1")) {
5633 filters = 0x61616161;
5634 maximum = 0xfff0;
5635 black = 1024;
5636 } else if (!strcmp(model,"E-10")) {
5637 maximum = 0xfff0;
5638 black = 2048;
5639 } else if (!strncmp(model,"E-20",4)) {
5640 maximum = 0xffc0;
5641 black = 2560;
5642 } else if (!strcmp(model,"E-300") ||
5643 !strcmp(model,"E-500")) {
5644 width -= 20;
5645 maximum = 0xfc30;
5646 if (fsize <= 15728640) {
5647 maximum = 0xfff;
5648 load_raw = olympus_e300_load_raw;
5650 } else if (!strcmp(model,"E-330")) {
5651 width -= 30;
5652 load_raw = olympus_e300_load_raw;
5653 } else if (!strcmp(model,"C770UZ")) {
5654 height = 1718;
5655 width = 2304;
5656 filters = 0x16161616;
5657 load_raw = nikon_e2100_load_raw;
5658 } else if (!strcmp(make,"OLYMPUS")) {
5659 load_raw = olympus_cseries_load_raw;
5660 if (!strcmp(model,"C5050Z") ||
5661 !strcmp(model,"C8080WZ"))
5662 filters = 0x16161616;
5663 if (!strcmp(model,"SP500UZ"))
5664 filters = 0x49494949;
5665 } else if (!strcmp(model,"N Digital")) {
5666 height = 2047;
5667 width = 3072;
5668 filters = 0x61616161;
5669 data_offset = 0x1a00;
5670 load_raw = packed_12_load_raw;
5671 maximum = 0xf1e;
5672 } else if (!strcmp(model,"DSC-F828")) {
5673 width = 3288;
5674 left_margin = 5;
5675 data_offset = 862144;
5676 load_raw = sony_load_raw;
5677 filters = 0x9c9c9c9c;
5678 colors = 4;
5679 strcpy (cdesc, "RGBE");
5680 } else if (!strcmp(model,"DSC-V3")) {
5681 width = 3109;
5682 left_margin = 59;
5683 data_offset = 787392;
5684 load_raw = sony_load_raw;
5685 } else if (!strcmp(model,"DSC-R1")) {
5686 width = 3925;
5687 order = 0x4d4d;
5688 } else if (!strncmp(model,"P850",4)) {
5689 maximum = 0xf7c;
5690 } else if (!strcasecmp(make,"KODAK")) {
5691 if (filters == UINT_MAX) filters = 0x61616161;
5692 if (!strcmp(model,"NC2000F")) {
5693 width -= 4;
5694 left_margin = 2;
5695 } else if (!strcmp(model,"EOSDCS3B")) {
5696 width -= 4;
5697 left_margin = 2;
5698 } else if (!strcmp(model,"EOSDCS1")) {
5699 width -= 4;
5700 left_margin = 2;
5701 } else if (!strcmp(model,"DCS420")) {
5702 width -= 4;
5703 left_margin = 2;
5704 } else if (!strcmp(model,"DCS460")) {
5705 width -= 4;
5706 left_margin = 2;
5707 } else if (!strcmp(model,"DCS460A")) {
5708 width -= 4;
5709 left_margin = 2;
5710 colors = 1;
5711 filters = 0;
5712 } else if (!strcmp(model,"DCS660M")) {
5713 black = 214;
5714 colors = 1;
5715 filters = 0;
5716 } else if (!strcmp(model,"DCS760M")) {
5717 colors = 1;
5718 filters = 0;
5720 if (load_raw == eight_bit_load_raw)
5721 load_raw = kodak_easy_load_raw;
5722 if (strstr(model,"DC25")) {
5723 strcpy (model, "DC25");
5724 data_offset = 15424;
5726 if (!strncmp(model,"DC2",3)) {
5727 height = 242;
5728 if (fsize < 100000) {
5729 raw_width = 256; width = 249;
5730 } else {
5731 raw_width = 512; width = 501;
5733 data_offset += raw_width + 1;
5734 colors = 4;
5735 filters = 0x8d8d8d8d;
5736 simple_coeff(1);
5737 pre_mul[1] = 1.179;
5738 pre_mul[2] = 1.209;
5739 pre_mul[3] = 1.036;
5740 load_raw = kodak_easy_load_raw;
5741 } else if (!strcmp(model,"Digital Camera 40")) {
5742 strcpy (model, "DC40");
5743 height = 512;
5744 width = 768;
5745 data_offset = 1152;
5746 load_raw = kodak_radc_load_raw;
5747 } else if (strstr(model,"DC50")) {
5748 strcpy (model, "DC50");
5749 height = 512;
5750 width = 768;
5751 data_offset = 19712;
5752 load_raw = kodak_radc_load_raw;
5753 } else if (strstr(model,"DC120")) {
5754 strcpy (model, "DC120");
5755 height = 976;
5756 width = 848;
5757 load_raw = (tiff_compress == 7)
5758 ? kodak_jpeg_load_raw : kodak_dc120_load_raw;
5760 } else if (!strcmp(model,"Fotoman Pixtura")) {
5761 height = 512;
5762 width = 768;
5763 data_offset = 3632;
5764 load_raw = kodak_radc_load_raw;
5765 filters = 0x61616161;
5766 simple_coeff(2);
5767 } else if (!strcmp(make,"Rollei")) {
5768 switch (raw_width) {
5769 case 1316:
5770 height = 1030;
5771 width = 1300;
5772 top_margin = 1;
5773 left_margin = 6;
5774 break;
5775 case 2568:
5776 height = 1960;
5777 width = 2560;
5778 top_margin = 2;
5779 left_margin = 8;
5781 filters = 0x16161616;
5782 load_raw = rollei_load_raw;
5783 pre_mul[0] = 1.8;
5784 pre_mul[2] = 1.3;
5785 } else if (!strcmp(model,"PC-CAM 600")) {
5786 height = 768;
5787 data_offset = width = 1024;
5788 filters = 0x49494949;
5789 load_raw = eight_bit_load_raw;
5790 pre_mul[0] = 1.14;
5791 pre_mul[2] = 2.73;
5792 } else if (!strcmp(model,"QV-2000UX")) {
5793 height = 1208;
5794 width = 1632;
5795 data_offset = width * 2;
5796 load_raw = eight_bit_load_raw;
5797 } else if (fsize == 3217760) {
5798 height = 1546;
5799 width = 2070;
5800 raw_width = 2080;
5801 load_raw = eight_bit_load_raw;
5802 } else if (!strcmp(model,"QV-4000")) {
5803 height = 1700;
5804 width = 2260;
5805 load_raw = unpacked_load_raw;
5806 maximum = 0xffff;
5807 } else if (!strcmp(model,"QV-5700")) {
5808 height = 1924;
5809 width = 2576;
5810 load_raw = casio_qv5700_load_raw;
5811 } else if (!strcmp(model,"QV-R51")) {
5812 height = 1926;
5813 width = 2576;
5814 raw_width = 3904;
5815 load_raw = packed_12_load_raw;
5816 pre_mul[0] = 1.340;
5817 pre_mul[2] = 1.672;
5818 } else if (!strcmp(model,"EX-S100")) {
5819 height = 1544;
5820 width = 2058;
5821 raw_width = 3136;
5822 load_raw = packed_12_load_raw;
5823 pre_mul[0] = 1.631;
5824 pre_mul[2] = 1.106;
5825 } else if (!strcmp(model,"EX-Z50")) {
5826 height = 1931;
5827 width = 2570;
5828 raw_width = 3904;
5829 load_raw = packed_12_load_raw;
5830 pre_mul[0] = 2.529;
5831 pre_mul[2] = 1.185;
5832 } else if (!strcmp(model,"EX-Z55")) {
5833 height = 1960;
5834 width = 2570;
5835 raw_width = 3904;
5836 load_raw = packed_12_load_raw;
5837 pre_mul[0] = 1.520;
5838 pre_mul[2] = 1.316;
5839 } else if (!strcmp(model,"EX-P505")) {
5840 height = 1928;
5841 width = 2568;
5842 raw_width = 3852;
5843 load_raw = packed_12_load_raw;
5844 pre_mul[0] = 2.07;
5845 pre_mul[2] = 1.88;
5846 } else if (fsize == 9313536) { /* EX-P600 or QV-R61 */
5847 height = 2142;
5848 width = 2844;
5849 raw_width = 4288;
5850 load_raw = packed_12_load_raw;
5851 pre_mul[0] = 1.797;
5852 pre_mul[2] = 1.219;
5853 } else if (!strcmp(model,"EX-P700")) {
5854 height = 2318;
5855 width = 3082;
5856 raw_width = 4672;
5857 load_raw = packed_12_load_raw;
5858 pre_mul[0] = 1.758;
5859 pre_mul[2] = 1.504;
5860 } else if (!strcmp(make,"Nucore")) {
5861 filters = 0x61616161;
5862 load_raw = unpacked_load_raw;
5863 if (width == 2598) {
5864 filters = 0x16161616;
5865 load_raw = nucore_load_raw;
5866 flip = 2;
5869 if (!model[0])
5870 sprintf (model, "%dx%d", width, height);
5871 if (filters == UINT_MAX) filters = 0x94949494;
5872 if (raw_color) adobe_coeff (make, model);
5873 if (thumb_offset && !thumb_height) {
5874 fseek (ifp, thumb_offset, SEEK_SET);
5875 if (ljpeg_start (&jh, 1)) {
5876 thumb_width = jh.wide;
5877 thumb_height = jh.high;
5880 dng_skip:
5881 if (!load_raw || !height) is_raw = 0;
5882 #ifdef NO_JPEG
5883 if (load_raw == kodak_jpeg_load_raw) {
5884 fprintf (stderr, "%s: You must link dcraw.c with libjpeg!!\n", ifname);
5885 is_raw = 0;
5887 #endif
5888 if (flip == -1) flip = tiff_flip;
5889 if (flip == -1) flip = 0;
5890 if (!cdesc[0])
5891 strcpy (cdesc, colors == 3 ? "RGB":"GMCY");
5892 if (!raw_height) raw_height = height;
5893 if (!raw_width ) raw_width = width;
5894 if (filters && colors == 3)
5895 for (i=0; i < 32; i+=4) {
5896 if ((filters >> i & 15) == 9)
5897 filters |= 2 << i;
5898 if ((filters >> i & 15) == 6)
5899 filters |= 8 << i;
5903 // CINELERRA
5904 if (flip & 4)
5905 sprintf(dcraw_info, "%d %d", height, width);
5906 else
5907 sprintf(dcraw_info, "%d %d", width, height);
5912 #ifndef NO_LCMS
5913 void CLASS apply_profile (char *input, char *output)
5915 char *prof;
5916 cmsHPROFILE hInProfile=NULL, hOutProfile;
5917 cmsHTRANSFORM hTransform;
5919 cmsErrorAction (LCMS_ERROR_SHOW);
5920 if (strcmp (input, "embed"))
5921 hInProfile = cmsOpenProfileFromFile (input, "r");
5922 else if (profile_length) {
5923 prof = malloc (profile_length);
5924 merror (prof, "apply_profile()");
5925 fseek (ifp, profile_offset, SEEK_SET);
5926 fread (prof, 1, profile_length, ifp);
5927 hInProfile = cmsOpenProfileFromMem (prof, profile_length);
5928 free (prof);
5929 } else
5930 fprintf (stderr, "%s has no embedded profile.\n", ifname);
5931 if (!hInProfile) return;
5932 hOutProfile = output ? cmsOpenProfileFromFile (output, "r")
5933 : cmsCreate_sRGBProfile();
5934 if (!hOutProfile) goto quit;
5935 if (verbose)
5936 fprintf (stderr, "Applying color profile...\n");
5937 hTransform = cmsCreateTransform (hInProfile, TYPE_RGBA_16,
5938 hOutProfile, TYPE_RGBA_16, INTENT_PERCEPTUAL, 0);
5939 cmsDoTransform (hTransform, image, image, width*height);
5940 maximum = 0xffff;
5941 raw_color = 1; /* Don't use rgb_cam with a profile */
5942 cmsDeleteTransform (hTransform);
5943 cmsCloseProfile (hOutProfile);
5944 quit:
5945 cmsCloseProfile (hInProfile);
5947 #endif
5949 void CLASS convert_to_rgb()
5951 int mix_green, row, col, c, i, j, k;
5952 ushort *img;
5953 float out[3], out_cam[3][4];
5954 static const double adobe_rgb[3][3] =
5955 { { 0.715146, 0.284856, 0.000000 },
5956 { 0.000000, 1.000000, 0.000000 },
5957 { 0.000000, 0.041166, 0.958839 } };
5958 static const double wgd65_rgb[3][3] =
5959 { { 0.593087, 0.404710, 0.002206 },
5960 { 0.095413, 0.843149, 0.061439 },
5961 { 0.011621, 0.069091, 0.919288 } };
5962 static const double prophoto_rgb[3][3] =
5963 { { 0.529317, 0.330092, 0.140588 },
5964 { 0.098368, 0.873465, 0.028169 },
5965 { 0.016879, 0.117663, 0.865457 } };
5966 static const double (*out_rgb[])[3] =
5967 { adobe_rgb, wgd65_rgb, prophoto_rgb, xyz_rgb };
5968 static const char *name[] =
5969 { "sRGB", "Adobe 1998 RGB", "Wide Gamut D65", "ProPhoto D65", "XYZ" };
5971 memcpy (out_cam, rgb_cam, sizeof out_cam);
5972 raw_color |= colors == 1 || output_color < 1 || output_color > 5;
5973 if (!raw_color) {
5974 if (output_color > 1)
5975 for (i=0; i < 3; i++)
5976 for (j=0; j < colors; j++)
5977 for (out_cam[i][j] = k=0; k < 3; k++)
5978 out_cam[i][j] += out_rgb[output_color-2][i][k] * rgb_cam[k][j];
5979 FORCC {
5980 out_cam[0][c] *= red_scale;
5981 out_cam[2][c] *= blue_scale;
5984 if (verbose)
5985 fprintf (stderr, raw_color ? "Building histograms...\n" :
5986 "Converting to %s colorspace...\n", name[output_color-1]);
5989 // CINELERRA
5990 // Export color matrix to Cinelerra.
5991 // It can't be applied before interpolation.
5992 k = 0;
5993 for(i = 0; i < 3; i++)
5995 for(j = 0; j < 3; j++)
5996 dcraw_matrix[k++] = rgb_cam[i][j];
6000 mix_green = raw_color && rgb_cam[1][1] == rgb_cam[1][3];
6001 memset (histogram, 0, sizeof histogram);
6002 for (row = 0; row < height; row++)
6003 for (col = 0; col < width; col++) {
6004 img = image[row*width+col];
6005 if (document_mode && filters)
6006 img[0] = img[FC(row,col)];
6007 else if (mix_green)
6008 img[1] = (img[1] + img[3]) >> 1;
6009 else if (!raw_color) {
6010 FORC3 for (out[c]=i=0; i < colors; i++)
6011 out[c] += img[i] * out_cam[c][i];
6012 FORC3 img[c] = CLIP((int) out[c]);
6014 FORCC histogram[c][img[c] >> 3]++;
6016 if (colors == 4 && (output_color || mix_green)) colors = 3;
6017 if (document_mode && filters) colors = 1;
6020 void CLASS fuji_rotate()
6022 int i, wide, high, row, col;
6023 double step;
6024 float r, c, fr, fc;
6025 unsigned ur, uc;
6026 ushort (*img)[4], (*pix)[4];
6028 if (!fuji_width) return;
6029 if (verbose)
6030 fprintf (stderr, "Rotating image 45 degrees...\n");
6031 fuji_width = (fuji_width - 1 + shrink) >> shrink;
6032 step = sqrt(0.5);
6033 wide = fuji_width / step;
6034 high = (height - fuji_width) / step;
6035 img = calloc (wide*high, sizeof *img);
6036 merror (img, "fuji_rotate()");
6038 for (row=0; row < high; row++)
6039 for (col=0; col < wide; col++) {
6040 ur = r = fuji_width + (row-col)*step;
6041 uc = c = (row+col)*step;
6042 if (ur > height-2 || uc > width-2) continue;
6043 fr = r - ur;
6044 fc = c - uc;
6045 pix = image + ur*width + uc;
6046 for (i=0; i < colors; i++)
6047 img[row*wide+col][i] =
6048 (pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
6049 (pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr;
6051 free (image);
6052 width = wide;
6053 height = high;
6054 image = img;
6055 fuji_width = 0;
6058 int CLASS flip_index (int row, int col)
6060 if (flip & 1) col = width - 1 - col;
6061 if (flip & 2) row = height - 1 - row;
6062 return (flip & 4) ? col * height + row
6063 : row * width + col;
6066 void CLASS flip_image()
6068 int row, col, soff=0, doff, rstep, cstep;
6069 INT64 *src, *dest;
6071 if (verbose)
6072 fprintf (stderr, "Flipping image %c:%c:%c...\n",
6073 flip & 1 ? 'H':'0', flip & 2 ? 'V':'0', flip & 4 ? 'T':'0');
6074 src = (INT64 *) image;
6075 dest = calloc (height * width, sizeof *dest);
6076 merror (dest, "flip_image()");
6077 doff = flip_index (0, 0);
6078 cstep = flip_index (0, 1) - doff;
6079 rstep = flip_index (1, 0) - flip_index (0, width);
6080 for (row=0; row < height; row++, doff += rstep)
6081 for (col=0; col < width; col++, doff += cstep)
6082 dest[doff] = src[soff++];
6083 image = (ushort (*)[4]) dest;
6084 free (src);
6085 if (flip & 4) {
6086 SWAP (height, width);
6087 SWAP (ymag, xmag);
6091 void CLASS write_ppm (FILE *ofp)
6093 uchar *ppm, lut[0x10000];
6094 int perc, c, val, total, i, row, col;
6095 float white=0, r;
6097 ppm = calloc (width, colors*xmag);
6098 merror (ppm, "write_ppm()");
6100 if (colors > 3)
6101 fprintf (ofp,
6102 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL 255\nTUPLTYPE %s\nENDHDR\n",
6103 xmag*width, ymag*height, colors, cdesc);
6104 else
6105 fprintf (ofp, "P%d\n%d %d\n255\n", colors/2+5, xmag*width, ymag*height);
6107 perc = width * height * 0.01; /* 99th percentile white point */
6108 if (fuji_width) perc /= 2;
6109 FORCC {
6110 for (val=0x2000, total=0; --val > 32; )
6111 if ((total += histogram[c][val]) > perc) break;
6112 if (white < val) white = val;
6114 white *= 8 / bright;
6115 for (i=0; i < 0x10000; i++) {
6116 r = i / white;
6117 val = 256 * ( !use_gamma ? r :
6118 #ifdef SRGB_GAMMA
6119 r <= 0.00304 ? r*12.92 : pow(r,2.5/6)*1.055-0.055 );
6120 #else
6121 r <= 0.018 ? r*4.5 : pow(r,0.45)*1.099-0.099 );
6122 #endif
6123 if (val > 255) val = 255;
6124 lut[i] = val;
6126 for (row=0; row < height; row++) {
6127 for (col=0; col < width; col++)
6128 FORCC for (i=0; i < xmag; i++)
6129 ppm[(col*xmag+i)*colors+c] = lut[image[row*width+col][c]];
6130 for (i=0; i < ymag; i++)
6131 fwrite (ppm, colors*xmag, width, ofp);
6133 free (ppm);
6136 void CLASS write_ppm16 (FILE *ofp)
6138 int row, col, c;
6139 ushort *ppm;
6141 ppm = calloc (width, 2*colors);
6142 merror (ppm, "write_ppm16()");
6143 if (maximum < 256) maximum = 256;
6144 if (colors > 3)
6145 fprintf (ofp,
6146 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
6147 width, height, colors, maximum, cdesc);
6148 else
6149 fprintf (ofp, "P%d\n%d %d\n%d\n",
6150 colors/2+5, width, height, maximum);
6152 for (row = 0; row < height; row++) {
6153 for (col = 0; col < width; col++)
6154 FORCC ppm[col*colors+c] = htons(image[row*width+col][c]);
6155 fwrite (ppm, 2*colors, width, ofp);
6157 free (ppm);
6164 // CINELERRA
6165 void CLASS write_cinelerra (FILE *ofp)
6167 int row, col, c, val;
6168 float *output;
6170 for (row = 0; row < height; row++)
6172 output = dcraw_data[row];
6174 if(document_mode)
6176 for (col = 0; col < width; col++)
6178 ushort *pixel = image[row * width + col];
6180 *output++ = (float)pixel[0] * red_scale / 0xffff;
6181 *output++ = (float)pixel[1] / 0xffff;
6182 *output++ = (float)pixel[2] * blue_scale / 0xffff;
6184 if(dcraw_alpha) *output++ = 1.0;
6187 else
6189 for (col = 0; col < width; col++)
6191 ushort *pixel = image[row * width + col];
6193 *output++ = (float)pixel[0] / 0xffff;
6194 *output++ = (float)pixel[1] / 0xffff;
6195 *output++ = (float)pixel[2] / 0xffff;
6197 if(dcraw_alpha) *output++ = 1.0;
6207 void CLASS write_psd (FILE *ofp)
6209 char head[] = {
6210 '8','B','P','S', /* signature */
6211 0,1,0,0,0,0,0,0, /* version and reserved */
6212 0,3, /* number of channels */
6213 0,0,0,0, /* height, big-endian */
6214 0,0,0,0, /* width, big-endian */
6215 0,16, /* 16-bit color */
6216 0,3, /* mode (1=grey, 3=rgb) */
6217 0,0,0,0, /* color mode data */
6218 0,0,0,0, /* image resources */
6219 0,0,0,0, /* layer/mask info */
6220 0,0 /* no compression */
6222 int hw[2], psize, row, col, c;
6223 ushort *buffer, *pred;
6225 hw[0] = htonl(height); /* write the header */
6226 hw[1] = htonl(width);
6227 memcpy (head+14, hw, sizeof hw);
6228 head[13] = head[25] = colors;
6229 fwrite (head, 40, 1, ofp);
6231 psize = height*width;
6232 buffer = calloc (colors, psize*2);
6233 merror (buffer, "write_psd()");
6234 pred = buffer;
6236 for (row = 0; row < height; row++)
6237 for (col = 0; col < width; col++) {
6238 FORCC pred[c*psize] = htons(image[row*width+col][c]);
6239 pred++;
6241 fwrite(buffer, psize*2, colors, ofp);
6242 free (buffer);
6245 // CINELERRA
6246 int CLASS dcraw_main (int argc, char **argv)
6248 // CINELERRA
6249 // Globals must be reset
6250 document_mode = 0;
6251 use_camera_wb = 0;
6253 int arg, status=0, user_flip=-1, user_black=-1, user_qual=-1;
6254 int timestamp_only=0, thumbnail_only=0, identify_only=0, write_to_stdout=0;
6255 int half_size=0, use_fuji_rotate=1, quality, i, c;
6256 char opt, *write_ext, *ofname, *cp;
6257 struct utimbuf ut;
6258 FILE *ofp = stdout;
6259 void (*write_image)(FILE *) = write_ppm;
6260 #ifndef NO_LCMS
6261 char *cam_profile = NULL, *out_profile = NULL;
6262 #endif
6264 #ifndef LOCALTIME
6265 putenv ("TZ=UTC");
6266 #endif
6267 if (argc == 1)
6269 fprintf (stderr,
6270 "\nRaw Photo Decoder \"dcraw\" v8.11"
6271 "\nby Dave Coffin, dcoffin a cybercom o net"
6272 "\n\nUsage: %s [options] file1 file2 ...\n"
6273 "\nValid options:"
6274 "\n-v Print verbose messages"
6275 "\n-c Write image data to standard output"
6276 "\n-e Extract embedded thumbnail image"
6277 "\n-i Identify files without decoding them"
6278 "\n-z Change file dates to camera timestamp"
6279 "\n-a Use automatic white balance"
6280 "\n-w Use camera white balance, if possible"
6281 "\n-r <num> Set red multiplier (default = 1.0)"
6282 "\n-l <num> Set blue multiplier (default = 1.0)"
6283 "\n-b <num> Set brightness (default = 1.0)"
6284 "\n-k <num> Set black point"
6285 "\n-n Don't clip colors"
6286 "\n-o [0-5] Output colorspace (raw,sRGB,Adobe,Wide,ProPhoto,XYZ)"
6287 #ifndef NO_LCMS
6288 "\n-o <file> Apply output ICC profile from file"
6289 "\n-p <file> Apply camera ICC profile from file or \"embed\""
6290 #endif
6291 "\n-d Document Mode (no color, no interpolation)"
6292 "\n-D Document Mode without scaling (totally raw)"
6293 "\n-q [0-3] Set the interpolation quality"
6294 "\n-h Half-size color image (twice as fast as \"-q 0\")"
6295 "\n-f Interpolate RGGB as four colors"
6296 "\n-B <domain> <range> Apply bilateral filter to reduce noise"
6297 "\n-j Show Fuji Super CCD images tilted 45 degrees"
6298 "\n-s Use secondary pixels (Fuji Super CCD SR only)"
6299 "\n-t [0-7] Flip image (0 = none, 3 = 180, 5 = 90CCW, 6 = 90CW)"
6300 "\n-2 Write 8-bit non-linear PPM (default)"
6301 "\n-4 Write 16-bit linear PPM"
6302 "\n-3 Write 16-bit linear PSD (Adobe Photoshop)"
6303 "\n\n", argv[0]);
6304 return 1;
6307 argv[argc] = "";
6308 for (arg=1; argv[arg][0] == '-'; ) {
6309 opt = argv[arg++][1];
6310 if ((strchr("Bbrlktq", opt) && !isdigit(argv[arg][0])) ||
6311 (opt == 'B' && !isdigit(argv[arg+1][0]))) {
6312 fprintf (stderr, "Non-numeric argument to \"-%c\"\n", opt);
6313 return 1;
6315 switch (opt)
6317 case 'B': sigma_d = atof(argv[arg++]);
6318 sigma_r = atof(argv[arg++]); break;
6319 case 'b': bright = atof(argv[arg++]); break;
6320 case 'r': red_scale = atof(argv[arg++]); break;
6321 case 'l': blue_scale = atof(argv[arg++]); break;
6322 case 'k': user_black = atoi(argv[arg++]); break;
6323 case 't': user_flip = atoi(argv[arg++]); break;
6324 case 'q': user_qual = atoi(argv[arg++]); break;
6325 case 'o':
6326 if (isdigit(argv[arg][0]) && !argv[arg][1])
6327 output_color = atoi(argv[arg++]);
6328 #ifndef NO_LCMS
6329 else out_profile = argv[arg++];
6330 break;
6331 case 'p': cam_profile = argv[arg++];
6332 #endif
6333 break;
6334 case 'z': timestamp_only = 1; break;
6335 case 'e': thumbnail_only = 1; break;
6336 case 'i': identify_only = 1; break;
6337 case 'c': write_to_stdout = 1; break;
6338 case 'v': verbose = 1; break;
6339 case 'h': half_size = 1; /* "-h" implies "-f" */
6340 case 'f': four_color_rgb = 1; break;
6341 case 'd': document_mode = 1; break;
6342 case 'D': document_mode = 2; break;
6343 case 'a': use_auto_wb = 1; break;
6344 case 'w': use_camera_wb = 1; break;
6345 case 'j': use_fuji_rotate = 0; break;
6346 case 's': use_secondary = 1; break;
6347 case 'n': clip_color = 0; break;
6348 case 'm': output_color = 0; break;
6350 case '2': write_image = write_ppm; break;
6351 case '4': write_image = write_ppm16; break;
6352 case '3': write_image = write_psd; break;
6354 default:
6355 fprintf (stderr, "Unknown option \"-%c\".\n", opt);
6356 return 1;
6362 if (arg == argc) {
6363 fprintf (stderr, "No files to process.\n");
6364 return 1;
6368 if (write_to_stdout) {
6369 // CINELERRA
6370 if (0 /* isatty(1) */) {
6371 fprintf (stderr, "Will not write an image to the terminal!\n");
6372 return 1;
6374 #if defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__)
6375 if (setmode(1,O_BINARY) < 0) {
6376 perror("setmode()");
6377 return 1;
6379 #endif
6383 for ( ; arg < argc; arg++) {
6384 status = 1;
6385 image = NULL;
6386 if (setjmp (failure)) {
6387 if (fileno(ifp) > 2) fclose(ifp);
6388 if (fileno(ofp) > 2) fclose(ofp);
6389 if (image) free (image);
6390 status = 1;
6391 continue;
6393 ifname = argv[arg];
6394 if (!(ifp = fopen (ifname, "rb"))) {
6395 perror (ifname);
6396 continue;
6398 status = (identify(),!is_raw);
6399 if (timestamp_only) {
6400 if ((status = !timestamp))
6401 fprintf (stderr, "%s has no timestamp.\n", ifname);
6402 else if (identify_only)
6403 printf ("%10ld%10d %s\n", (long) timestamp, shot_order, ifname);
6404 else {
6405 if (verbose)
6406 fprintf (stderr, "%s time set to %d.\n", ifname, (int) timestamp);
6407 ut.actime = ut.modtime = timestamp;
6408 utime (ifname, &ut);
6410 goto next;
6413 write_fun = write_image;
6415 // CINELERRA
6416 write_fun = write_cinelerra;
6419 if (thumbnail_only) {
6420 if ((status = !thumb_offset)) {
6421 fprintf (stderr, "%s has no thumbnail.\n", ifname);
6422 goto next;
6423 } else if (thumb_load_raw) {
6424 load_raw = thumb_load_raw;
6425 data_offset = thumb_offset;
6426 height = thumb_height;
6427 width = thumb_width;
6428 filters = 0;
6429 } else {
6430 fseek (ifp, thumb_offset, SEEK_SET);
6431 write_fun = write_thumb;
6432 goto thumbnail;
6435 if (load_raw == kodak_ycbcr_load_raw) {
6436 height += height & 1;
6437 width += width & 1;
6439 if (identify_only && verbose && make[0]) {
6440 printf ("\nFilename: %s\n", ifname);
6441 printf ("Timestamp: %s", ctime(&timestamp));
6442 printf ("Camera: %s %s\n", make, model);
6443 printf ("ISO speed: %d\n", (int) iso_speed);
6444 printf ("Shutter: ");
6445 if (shutter > 0 && shutter < 1)
6446 shutter = (printf ("1/"), 1 / shutter);
6447 printf ("%0.1f sec\n", shutter);
6448 printf ("Aperture: f/%0.1f\n", aperture);
6449 printf ("Focal Length: %d mm\n", (int) focal_len);
6450 printf ("Embedded ICC profile: %s\n", profile_length ? "yes":"no");
6451 printf ("Decodable with dcraw: %s\n", is_raw ? "yes":"no");
6452 if (thumb_offset)
6453 printf ("Thumb size: %4d x %d\n", thumb_width, thumb_height);
6454 printf ("Full size: %4d x %d\n", raw_width, raw_height);
6456 // CINELERRA
6457 // else
6458 // if (!is_raw)
6459 // fprintf (stderr, "Cannot decode %s\n", ifname);
6460 if (!is_raw) goto next;
6461 if (user_flip >= 0)
6462 flip = user_flip;
6463 switch ((flip+3600) % 360) {
6464 case 270: flip = 5; break;
6465 case 180: flip = 3; break;
6466 case 90: flip = 6;
6468 shrink = half_size && filters;
6469 iheight = (height + shrink) >> shrink;
6470 iwidth = (width + shrink) >> shrink;
6471 if (identify_only) {
6472 if (verbose) {
6473 if (fuji_width && use_fuji_rotate) {
6474 fuji_width = (fuji_width - 1 + shrink) >> shrink;
6475 iwidth = fuji_width / sqrt(0.5);
6476 iheight = (iheight - fuji_width) / sqrt(0.5);
6478 if (write_fun == write_ppm) {
6479 iheight *= ymag;
6480 iwidth *= xmag;
6482 if (flip & 4)
6483 SWAP (iheight, iwidth);
6484 printf ("Image size: %4d x %d\n", width, height);
6485 printf ("Output size: %4d x %d\n", iwidth, iheight);
6486 printf ("Raw colors: %d", colors);
6487 if (filters) {
6488 printf ("\nFilter pattern: ");
6489 if (!cdesc[3]) cdesc[3] = 'G';
6490 for (i=0; i < 32; i+=2)
6491 putchar (cdesc[filters >> i & 3]);
6493 printf ("\nDaylight multipliers:");
6494 FORCC printf (" %f", pre_mul[c]);
6495 if (cam_mul[0] > 0) {
6496 printf ("\nCamera multipliers:");
6497 FORCC printf (" %f", cam_mul[c]);
6499 putchar ('\n');
6500 } else
6501 // CINELERRA
6502 // printf ("%s is a %s %s image.\n", ifname, make, model);
6503 next:
6504 fclose(ifp);
6505 continue;
6507 image = calloc (iheight*iwidth*sizeof *image + meta_length, 1);
6508 merror (image, "main()");
6509 meta_data = (char *) (image + iheight*iwidth);
6510 if (verbose)
6511 fprintf (stderr,
6512 "Loading %s %s image from %s...\n", make, model, ifname);
6513 fseek (ifp, data_offset, SEEK_SET);
6514 (*load_raw)();
6515 bad_pixels();
6516 height = iheight;
6517 width = iwidth;
6518 quality = 2 + !fuji_width;
6519 if (user_qual >= 0) quality = user_qual;
6520 if (user_black >= 0) black = user_black;
6521 #ifdef COLORCHECK
6522 colorcheck();
6523 #endif
6524 if (is_foveon && !document_mode) foveon_interpolate();
6525 if (!is_foveon && document_mode < 2) scale_colors();
6526 if (shrink) filters = 0;
6527 cam_to_cielab (NULL,NULL);
6531 * printf("dcraw filters=%08x document_mode=%d quality=%d colors=%d\n",
6532 * filters,
6533 * document_mode,
6534 * quality,
6535 * colors);
6540 if (filters && !document_mode)
6542 if (quality == 0)
6543 lin_interpolate();
6544 else
6545 if (quality < 3 || colors > 3)
6546 vng_interpolate();
6547 else
6548 ahd_interpolate();
6551 if (sigma_d > 0 && sigma_r > 0) bilateral_filter();
6552 if (use_fuji_rotate) fuji_rotate();
6553 #ifndef NO_LCMS
6554 if (cam_profile) apply_profile (cam_profile, out_profile);
6555 #endif
6556 convert_to_rgb();
6557 if (flip) flip_image();
6558 thumbnail:
6559 if (write_fun == jpeg_thumb)
6560 write_ext = ".jpg";
6561 else if (write_fun == write_psd)
6562 write_ext = ".psd";
6563 else
6564 write_ext = ".pgm\0.ppm\0.ppm\0.pam" + colors*5-5;
6565 ofname = malloc (strlen(ifname) + 16);
6566 merror (ofname, "main()");
6567 if (write_to_stdout)
6568 strcpy (ofname, "standard output");
6569 else {
6570 strcpy (ofname, ifname);
6571 if ((cp = strrchr (ofname, '.'))) *cp = 0;
6572 if (thumbnail_only)
6573 strcat (ofname, ".thumb");
6574 strcat (ofname, write_ext);
6575 ofp = fopen (ofname, "wb");
6576 if (!ofp) {
6577 status = 1;
6578 perror(ofname);
6579 goto cleanup;
6582 if (verbose)
6583 fprintf (stderr, "Writing data to %s ...\n", ofname);
6584 (*write_fun)(ofp);
6585 fclose(ifp);
6586 if (ofp != stdout) fclose(ofp);
6587 cleanup:
6588 free (ofname);
6589 free (image);
6591 return status;