1 /*
2 dcraw.c -- Dave Coffin's raw photo decoder
3 Copyright 1997-2015 by Dave Coffin, dcoffin a cybercom o net
4
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.
7
8 No license is required to download and use dcraw.c. However,
9 to lawfully redistribute dcraw, you must either (a) offer, at
10 no extra charge, full source code* for all executable files
11 containing RESTRICTED functions, (b) distribute this code under
12 the GPL Version 2 or later, (c) remove all RESTRICTED functions,
13 re-implement them, or copy them from an earlier, unrestricted
14 Revision of dcraw.c, or (d) purchase a license from the author.
15
16 The functions that process Foveon images have been RESTRICTED
17 since Revision 1.237. All other code remains free for all uses.
18
19 *If you have not modified dcraw.c in any way, a link to my
20 homepage qualifies as "full source code".
21
22 $Revision: 1.476 $
23 $Date: 2015/05/25 02:29:14 $
24 */
25
26 #define DCRAW_VERSION "9.26"
27
28 #ifndef _GNU_SOURCE
29 #define _GNU_SOURCE
30 #endif
31 #define _USE_MATH_DEFINES
32 #include <ctype.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <float.h>
36 #include <limits.h>
37 #include <math.h>
38 #include <setjmp.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 #include <sys/types.h>
44
45 #if defined(DJGPP) || defined(__MINGW32__)
46 #define fseeko fseek
47 #define ftello ftell
48 #else
49 #define fgetc getc_unlocked
50 #endif
51 #ifdef __CYGWIN__
52 #include <io.h>
53 #endif
54 #ifdef WIN32
55 #include <sys/utime.h>
56 #include <winsock2.h>
57 #pragma comment(lib, "ws2_32.lib")
58 #define snprintf _snprintf
59 #define strcasecmp stricmp
60 #define strncasecmp strnicmp
61 typedef __int64 INT64;
62 typedef unsigned __int64 UINT64;
63 #else
64 #include <unistd.h>
65 #include <utime.h>
66 #include <netinet/in.h>
67 typedef long long INT64;
68 typedef unsigned long long UINT64;
69 #endif
70
71 #define NODEPS
72 #ifdef NODEPS
73 #define NO_JASPER
74 #define NO_JPEG
75 #define NO_LCMS
76 #endif
77 #ifndef NO_JASPER
78 #include <jasper/jasper.h> /* Decode Red camera movies */
79 #endif
80 #ifndef NO_JPEG
81 #include <jpeglib.h> /* Decode compressed Kodak DC120 photos */
82 #endif /* and Adobe Lossy DNGs */
83 #ifndef NO_LCMS
84 #include <lcms2.h> /* Support color profiles */
85 #endif
86 #ifdef LOCALEDIR
87 #include <libintl.h>
88 #define _(String) gettext(String)
89 #else
90 #define _(String) (String)
91 #endif
92
93 #if !defined(uchar)
94 #define uchar unsigned char
95 #endif
96 #if !defined(ushort)
97 #define ushort unsigned short
98 #endif
99
100 /*
101 All global variables are defined here, and all functions that
102 access them are prefixed with "CLASS". Note that a thread-safe
103 C++ class cannot have non-const static local variables.
104 */
105 FILE *ifp, *ofp;
106 short order;
107 const char *ifname;
108 char *meta_data, xtrans[6][6], xtrans_abs[6][6];
109 char cdesc[5], desc[512], make[64], model[64], model2[64], artist[64];
110 float flash_used, canon_ev, iso_speed, shutter, aperture, focal_len;
111 time_t timestamp;
112 off_t strip_offset, data_offset;
113 off_t thumb_offset, meta_offset, profile_offset;
114 unsigned shot_order, kodak_cbpp, exif_cfa, unique_id;
115 unsigned thumb_length, meta_length, profile_length;
116 unsigned thumb_misc, *oprof, fuji_layout, shot_select=0, multi_out=0;
117 unsigned tiff_nifds, tiff_samples, tiff_bps, tiff_compress;
118 unsigned black, maximum, mix_green, raw_color, zero_is_bad;
119 unsigned zero_after_ff, is_raw, dng_version, is_foveon, data_error;
120 unsigned tile_width, tile_length, gpsdata[32], load_flags;
121 unsigned flip, tiff_flip, filters, colors;
122 ushort raw_height, raw_width, height, width, top_margin, left_margin;
123 ushort shrink, iheight, iwidth, fuji_width, thumb_width, thumb_height;
124 ushort *raw_image, (*image)[4], cblack[4102];
125 ushort white[8][8], curve[0x10000], cr2_slice[3], sraw_mul[4];
126 double pixel_aspect, aber[4]={1,1,1,1}, gamm[6]={ 0.45,4.5,0,0,0,0 };
127 float bright=1, user_mul[4]={0,0,0,0}, threshold=0;
128 int mask[8][4];
129 int half_size=0, four_color_rgb=0, document_mode=0, highlight=0;
130 int verbose=0, use_auto_wb=0, use_camera_wb=0, use_camera_matrix=1;
131 int output_color=1, output_bps=8, output_tiff=0, med_passes=0;
132 int no_auto_bright=0;
133 unsigned greybox[4] = { 0, 0, UINT_MAX, UINT_MAX };
134 float cam_mul[4], pre_mul[4], cmatrix[3][4], rgb_cam[3][4];
135 const double xyz_rgb[3][3] = { /* XYZ from RGB */
136 { 0.412453, 0.357580, 0.180423 },
137 { 0.212671, 0.715160, 0.072169 },
138 { 0.019334, 0.119193, 0.950227 } };
139 const float d65_white[3] = { 0.950456, 1, 1.088754 };
140 int histogram[4][0x2000];
141 void (*write_thumb)(), (*write_fun)();
142 void (*load_raw)(), (*thumb_load_raw)();
143 jmp_buf failure;
144
145 struct decode {
146 struct decode *branch[2];
147 int leaf;
148 } first_decode[2048], *second_decode, *free_decode;
149
150 struct tiff_ifd {
151 int width, height, bps, comp, phint, offset, flip, samples, bytes;
152 int tile_width, tile_length;
153 float shutter;
154 } tiff_ifd[10];
155
156 struct ph1 {
157 int format, key_off, tag_21a;
158 int black, split_col, black_col, split_row, black_row;
159 float tag_210;
160 } ph1;
161
162 #define CLASS
163
164 #define FORC(cnt) for (c=0; c < cnt; c++)
165 #define FORC3 FORC(3)
166 #define FORC4 FORC(4)
167 #define FORCC FORC(colors)
168
169 #define SQR(x) ((x)*(x))
170 #define ABS(x) (((int)(x) ^ ((int)(x) >> 31)) - ((int)(x) >> 31))
171 #define MIN(a,b) ((a) < (b) ? (a) : (b))
172 #define MAX(a,b) ((a) > (b) ? (a) : (b))
173 #define LIM(x,min,max) MAX(min,MIN(x,max))
174 #define ULIM(x,y,z) ((y) < (z) ? LIM(x,y,z) : LIM(x,z,y))
175 #define CLIP(x) LIM((int)(x),0,65535)
176 #define SWAP(a,b) { a=a+b; b=a-b; a=a-b; }
177
178 /*
179 In order to inline this calculation, I make the risky
180 assumption that all filter patterns can be described
181 by a repeating pattern of eight rows and two columns
182
183 Do not use the FC or BAYER macros with the Leaf CatchLight,
184 because its pattern is 16x16, not 2x8.
185
186 Return values are either 0/1/2/3 = G/M/C/Y or 0/1/2/3 = R/G1/B/G2
187
188 PowerShot 600 PowerShot A50 PowerShot Pro70 Pro90 & G1
189 0xe1e4e1e4: 0x1b4e4b1e: 0x1e4b4e1b: 0xb4b4b4b4:
190
191 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
192 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
193 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
194 2 M G M G M G 2 Y C Y C Y C 2 C Y C Y C Y
195 3 C Y C Y C Y 3 G M G M G M 3 G M G M G M
196 4 C Y C Y C Y 4 Y C Y C Y C
197 PowerShot A5 5 G M G M G M 5 G M G M G M
198 0x1e4e1e4e: 6 Y C Y C Y C 6 C Y C Y C Y
199 7 M G M G M G 7 M G M G M G
200 0 1 2 3 4 5
201 0 C Y C Y C Y
202 1 G M G M G M
203 2 C Y C Y C Y
204 3 M G M G M G
205
206 All RGB cameras use one of these Bayer grids:
207
208 0x16161616: 0x61616161: 0x49494949: 0x94949494:
209
210 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
211 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
212 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
213 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
214 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
215 */
216
217 #define RAW(row,col) \
218 raw_image[(row)*raw_width+(col)]
219
220 #define FC(row,col) \
221 (filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
222
223 #define BAYER(row,col) \
224 image[((row) >> shrink)*iwidth + ((col) >> shrink)][FC(row,col)]
225
226 #define BAYER2(row,col) \
227 image[((row) >> shrink)*iwidth + ((col) >> shrink)][fcol(row,col)]
228
fcol(int row,int col)229 int CLASS fcol (int row, int col)
230 {
231 static const char filter[16][16] =
232 { { 2,1,1,3,2,3,2,0,3,2,3,0,1,2,1,0 },
233 { 0,3,0,2,0,1,3,1,0,1,1,2,0,3,3,2 },
234 { 2,3,3,2,3,1,1,3,3,1,2,1,2,0,0,3 },
235 { 0,1,0,1,0,2,0,2,2,0,3,0,1,3,2,1 },
236 { 3,1,1,2,0,1,0,2,1,3,1,3,0,1,3,0 },
237 { 2,0,0,3,3,2,3,1,2,0,2,0,3,2,2,1 },
238 { 2,3,3,1,2,1,2,1,2,1,1,2,3,0,0,1 },
239 { 1,0,0,2,3,0,0,3,0,3,0,3,2,1,2,3 },
240 { 2,3,3,1,1,2,1,0,3,2,3,0,2,3,1,3 },
241 { 1,0,2,0,3,0,3,2,0,1,1,2,0,1,0,2 },
242 { 0,1,1,3,3,2,2,1,1,3,3,0,2,1,3,2 },
243 { 2,3,2,0,0,1,3,0,2,0,1,2,3,0,1,0 },
244 { 1,3,1,2,3,2,3,2,0,2,0,1,1,0,3,0 },
245 { 0,2,0,3,1,0,0,1,1,3,3,2,3,2,2,1 },
246 { 2,1,3,2,3,1,2,1,0,3,0,2,0,2,0,2 },
247 { 0,3,1,0,0,2,0,3,2,1,3,1,1,3,1,3 } };
248
249 if (filters == 1) return filter[(row+top_margin)&15][(col+left_margin)&15];
250 if (filters == 9) return xtrans[(row+6) % 6][(col+6) % 6];
251 return FC(row,col);
252 }
253
254 #ifndef __GLIBC__
my_memmem(char * haystack,size_t haystacklen,char * needle,size_t needlelen)255 char *my_memmem (char *haystack, size_t haystacklen,
256 char *needle, size_t needlelen)
257 {
258 char *c;
259 for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
260 if (!memcmp (c, needle, needlelen))
261 return c;
262 return 0;
263 }
264 #define memmem my_memmem
my_strcasestr(char * haystack,const char * needle)265 char *my_strcasestr (char *haystack, const char *needle)
266 {
267 char *c;
268 for (c = haystack; *c; c++)
269 if (!strncasecmp(c, needle, strlen(needle)))
270 return c;
271 return 0;
272 }
273 #define strcasestr my_strcasestr
274 #endif
275
merror(void * ptr,const char * where)276 void CLASS merror (void *ptr, const char *where)
277 {
278 if (ptr) return;
279 fprintf (stderr,_("%s: Out of memory in %s\n"), ifname, where);
280 longjmp (failure, 1);
281 }
282
derror()283 void CLASS derror()
284 {
285 if (!data_error) {
286 fprintf (stderr, "%s: ", ifname);
287 if (feof(ifp))
288 fprintf (stderr,_("Unexpected end of file\n"));
289 else
290 fprintf (stderr,_("Corrupt data near 0x%llx\n"), (INT64) ftello(ifp));
291 }
292 data_error++;
293 }
294
sget2(uchar * s)295 ushort CLASS sget2 (uchar *s)
296 {
297 if (order == 0x4949) /* "II" means little-endian */
298 return s[0] | s[1] << 8;
299 else /* "MM" means big-endian */
300 return s[0] << 8 | s[1];
301 }
302
get2()303 ushort CLASS get2()
304 {
305 uchar str[2] = { 0xff,0xff };
306 fread (str, 1, 2, ifp);
307 return sget2(str);
308 }
309
sget4(uchar * s)310 unsigned CLASS sget4 (uchar *s)
311 {
312 if (order == 0x4949)
313 return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
314 else
315 return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
316 }
317 #define sget4(s) sget4((uchar *)s)
318
get4()319 unsigned CLASS get4()
320 {
321 uchar str[4] = { 0xff,0xff,0xff,0xff };
322 fread (str, 1, 4, ifp);
323 return sget4(str);
324 }
325
getint(int type)326 unsigned CLASS getint (int type)
327 {
328 return type == 3 ? get2() : get4();
329 }
330
int_to_float(int i)331 float CLASS int_to_float (int i)
332 {
333 union { int i; float f; } u;
334 u.i = i;
335 return u.f;
336 }
337
getreal(int type)338 double CLASS getreal (int type)
339 {
340 union { char c[8]; double d; } u;
341 int i, rev;
342
343 switch (type) {
344 case 3: return (unsigned short) get2();
345 case 4: return (unsigned int) get4();
346 case 5: u.d = (unsigned int) get4();
347 return u.d / (unsigned int) get4();
348 case 8: return (signed short) get2();
349 case 9: return (signed int) get4();
350 case 10: u.d = (signed int) get4();
351 return u.d / (signed int) get4();
352 case 11: return int_to_float (get4());
353 case 12:
354 rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
355 for (i=0; i < 8; i++)
356 u.c[i ^ rev] = fgetc(ifp);
357 return u.d;
358 default: return fgetc(ifp);
359 }
360 }
361
read_shorts(ushort * pixel,int count)362 void CLASS read_shorts (ushort *pixel, int count)
363 {
364 if (fread (pixel, 2, count, ifp) < count) derror();
365 if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
366 swab (pixel, pixel, count*2);
367 }
368
cubic_spline(const int * x_,const int * y_,const int len)369 void CLASS cubic_spline (const int *x_, const int *y_, const int len)
370 {
371 float **A, *b, *c, *d, *x, *y;
372 int i, j;
373
374 A = (float **) calloc (((2*len + 4)*sizeof **A + sizeof *A), 2*len);
375 if (!A) return;
376 A[0] = (float *) (A + 2*len);
377 for (i = 1; i < 2*len; i++)
378 A[i] = A[0] + 2*len*i;
379 y = len + (x = i + (d = i + (c = i + (b = A[0] + i*i))));
380 for (i = 0; i < len; i++) {
381 x[i] = x_[i] / 65535.0;
382 y[i] = y_[i] / 65535.0;
383 }
384 for (i = len-1; i > 0; i--) {
385 b[i] = (y[i] - y[i-1]) / (x[i] - x[i-1]);
386 d[i-1] = x[i] - x[i-1];
387 }
388 for (i = 1; i < len-1; i++) {
389 A[i][i] = 2 * (d[i-1] + d[i]);
390 if (i > 1) {
391 A[i][i-1] = d[i-1];
392 A[i-1][i] = d[i-1];
393 }
394 A[i][len-1] = 6 * (b[i+1] - b[i]);
395 }
396 for(i = 1; i < len-2; i++) {
397 float v = A[i+1][i] / A[i][i];
398 for(j = 1; j <= len-1; j++)
399 A[i+1][j] -= v * A[i][j];
400 }
401 for(i = len-2; i > 0; i--) {
402 float acc = 0;
403 for(j = i; j <= len-2; j++)
404 acc += A[i][j]*c[j];
405 c[i] = (A[i][len-1] - acc) / A[i][i];
406 }
407 for (i = 0; i < 0x10000; i++) {
408 float x_out = (float)(i / 65535.0);
409 float y_out = 0;
410 for (j = 0; j < len-1; j++) {
411 if (x[j] <= x_out && x_out <= x[j+1]) {
412 float v = x_out - x[j];
413 y_out = y[j] +
414 ((y[j+1] - y[j]) / d[j] - (2 * d[j] * c[j] + c[j+1] * d[j])/6) * v
415 + (c[j] * 0.5) * v*v + ((c[j+1] - c[j]) / (6 * d[j])) * v*v*v;
416 }
417 }
418 curve[i] = y_out < 0.0 ? 0 : (y_out >= 1.0 ? 65535 :
419 (ushort)(y_out * 65535.0 + 0.5));
420 }
421 free (A);
422 }
423
canon_600_fixed_wb(int temp)424 void CLASS canon_600_fixed_wb (int temp)
425 {
426 static const short mul[4][5] = {
427 { 667, 358,397,565,452 },
428 { 731, 390,367,499,517 },
429 { 1119, 396,348,448,537 },
430 { 1399, 485,431,508,688 } };
431 int lo, hi, i;
432 float frac=0;
433
434 for (lo=4; --lo; )
435 if (*mul[lo] <= temp) break;
436 for (hi=0; hi < 3; hi++)
437 if (*mul[hi] >= temp) break;
438 if (lo != hi)
439 frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
440 for (i=1; i < 5; i++)
441 pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
442 }
443
444 /* Return values: 0 = white 1 = near white 2 = not white */
canon_600_color(int ratio[2],int mar)445 int CLASS canon_600_color (int ratio[2], int mar)
446 {
447 int clipped=0, target, miss;
448
449 if (flash_used) {
450 if (ratio[1] < -104)
451 { ratio[1] = -104; clipped = 1; }
452 if (ratio[1] > 12)
453 { ratio[1] = 12; clipped = 1; }
454 } else {
455 if (ratio[1] < -264 || ratio[1] > 461) return 2;
456 if (ratio[1] < -50)
457 { ratio[1] = -50; clipped = 1; }
458 if (ratio[1] > 307)
459 { ratio[1] = 307; clipped = 1; }
460 }
461 target = flash_used || ratio[1] < 197
462 ? -38 - (398 * ratio[1] >> 10)
463 : -123 + (48 * ratio[1] >> 10);
464 if (target - mar <= ratio[0] &&
465 target + 20 >= ratio[0] && !clipped) return 0;
466 miss = target - ratio[0];
467 if (abs(miss) >= mar*4) return 2;
468 if (miss < -20) miss = -20;
469 if (miss > mar) miss = mar;
470 ratio[0] = target - miss;
471 return 1;
472 }
473
canon_600_auto_wb()474 void CLASS canon_600_auto_wb()
475 {
476 int mar, row, col, i, j, st, count[] = { 0,0 };
477 int test[8], total[2][8], ratio[2][2], stat[2];
478
479 memset (&total, 0, sizeof total);
480 i = canon_ev + 0.5;
481 if (i < 10) mar = 150;
482 else if (i > 12) mar = 20;
483 else mar = 280 - 20 * i;
484 if (flash_used) mar = 80;
485 for (row=14; row < height-14; row+=4)
486 for (col=10; col < width; col+=2) {
487 for (i=0; i < 8; i++)
488 test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
489 BAYER(row+(i >> 1),col+(i & 1));
490 for (i=0; i < 8; i++)
491 if (test[i] < 150 || test[i] > 1500) goto next;
492 for (i=0; i < 4; i++)
493 if (abs(test[i] - test[i+4]) > 50) goto next;
494 for (i=0; i < 2; i++) {
495 for (j=0; j < 4; j+=2)
496 ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
497 stat[i] = canon_600_color (ratio[i], mar);
498 }
499 if ((st = stat[0] | stat[1]) > 1) goto next;
500 for (i=0; i < 2; i++)
501 if (stat[i])
502 for (j=0; j < 2; j++)
503 test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
504 for (i=0; i < 8; i++)
505 total[st][i] += test[i];
506 count[st]++;
507 next: ;
508 }
509 if (count[0] | count[1]) {
510 st = count[0]*200 < count[1];
511 for (i=0; i < 4; i++)
512 pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
513 }
514 }
515
canon_600_coeff()516 void CLASS canon_600_coeff()
517 {
518 static const short table[6][12] = {
519 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
520 { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
521 { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
522 { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
523 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
524 { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
525 int t=0, i, c;
526 float mc, yc;
527
528 mc = pre_mul[1] / pre_mul[2];
529 yc = pre_mul[3] / pre_mul[2];
530 if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
531 if (mc > 1.28 && mc <= 2) {
532 if (yc < 0.8789) t=3;
533 else if (yc <= 2) t=4;
534 }
535 if (flash_used) t=5;
536 for (raw_color = i=0; i < 3; i++)
537 FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
538 }
539
canon_600_load_raw()540 void CLASS canon_600_load_raw()
541 {
542 uchar data[1120], *dp;
543 ushort *pix;
544 int irow, row;
545
546 for (irow=row=0; irow < height; irow++) {
547 if (fread (data, 1, 1120, ifp) < 1120) derror();
548 pix = raw_image + row*raw_width;
549 for (dp=data; dp < data+1120; dp+=10, pix+=8) {
550 pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
551 pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
552 pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
553 pix[3] = (dp[4] << 2) + (dp[1] & 3);
554 pix[4] = (dp[5] << 2) + (dp[9] & 3);
555 pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
556 pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
557 pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
558 }
559 if ((row+=2) > height) row = 1;
560 }
561 }
562
canon_600_correct()563 void CLASS canon_600_correct()
564 {
565 int row, col, val;
566 static const short mul[4][2] =
567 { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
568
569 for (row=0; row < height; row++)
570 for (col=0; col < width; col++) {
571 if ((val = BAYER(row,col) - black) < 0) val = 0;
572 val = val * mul[row & 3][col & 1] >> 9;
573 BAYER(row,col) = val;
574 }
575 canon_600_fixed_wb(1311);
576 canon_600_auto_wb();
577 canon_600_coeff();
578 maximum = (0x3ff - black) * 1109 >> 9;
579 black = 0;
580 }
581
canon_s2is()582 int CLASS canon_s2is()
583 {
584 unsigned row;
585
586 for (row=0; row < 100; row++) {
587 fseek (ifp, row*3340 + 3284, SEEK_SET);
588 if (getc(ifp) > 15) return 1;
589 }
590 return 0;
591 }
592
getbithuff(int nbits,ushort * huff)593 unsigned CLASS getbithuff (int nbits, ushort *huff)
594 {
595 static unsigned bitbuf=0;
596 static int vbits=0, reset=0;
597 unsigned c;
598
599 if (nbits > 25) return 0;
600 if (nbits < 0)
601 return bitbuf = vbits = reset = 0;
602 if (nbits == 0 || vbits < 0) return 0;
603 while (!reset && vbits < nbits && (c = fgetc(ifp)) != EOF &&
604 !(reset = zero_after_ff && c == 0xff && fgetc(ifp))) {
605 bitbuf = (bitbuf << 8) + (uchar) c;
606 vbits += 8;
607 }
608 c = bitbuf << (32-vbits) >> (32-nbits);
609 if (huff) {
610 vbits -= huff[c] >> 8;
611 c = (uchar) huff[c];
612 } else
613 vbits -= nbits;
614 if (vbits < 0) derror();
615 return c;
616 }
617
618 #define getbits(n) getbithuff(n,0)
619 #define gethuff(h) getbithuff(*h,h+1)
620
621 /*
622 Construct a decode tree according the specification in *source.
623 The first 16 bytes specify how many codes should be 1-bit, 2-bit
624 3-bit, etc. Bytes after that are the leaf values.
625
626 For example, if the source is
627
628 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
629 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
630
631 then the code is
632
633 00 0x04
634 010 0x03
635 011 0x05
636 100 0x06
637 101 0x02
638 1100 0x07
639 1101 0x01
640 11100 0x08
641 11101 0x09
642 11110 0x00
643 111110 0x0a
644 1111110 0x0b
645 1111111 0xff
646 */
make_decoder_ref(const uchar ** source)647 ushort * CLASS make_decoder_ref (const uchar **source)
648 {
649 int max, len, h, i, j;
650 const uchar *count;
651 ushort *huff;
652
653 count = (*source += 16) - 17;
654 for (max=16; max && !count[max]; max--);
655 huff = (ushort *) calloc (1 + (1 << max), sizeof *huff);
656 merror (huff, "make_decoder()");
657 huff[0] = max;
658 for (h=len=1; len <= max; len++)
659 for (i=0; i < count[len]; i++, ++*source)
660 for (j=0; j < 1 << (max-len); j++)
661 if (h <= 1 << max)
662 huff[h++] = len << 8 | **source;
663 return huff;
664 }
665
make_decoder(const uchar * source)666 ushort * CLASS make_decoder (const uchar *source)
667 {
668 return make_decoder_ref (&source);
669 }
670
crw_init_tables(unsigned table,ushort * huff[2])671 void CLASS crw_init_tables (unsigned table, ushort *huff[2])
672 {
673 static const uchar first_tree[3][29] = {
674 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
675 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
676 { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
677 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
678 { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
679 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
680 };
681 static const uchar second_tree[3][180] = {
682 { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
683 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
684 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
685 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
686 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
687 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
688 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
689 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
690 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
691 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
692 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
693 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
694 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
695 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
696 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
697 { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
698 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
699 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
700 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
701 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
702 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
703 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
704 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
705 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
706 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
707 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
708 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
709 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
710 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
711 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
712 { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
713 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
714 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
715 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
716 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
717 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
718 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
719 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
720 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
721 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
722 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
723 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
724 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
725 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
726 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
727 };
728 if (table > 2) table = 2;
729 huff[0] = make_decoder ( first_tree[table]);
730 huff[1] = make_decoder (second_tree[table]);
731 }
732
733 /*
734 Return 0 if the image starts with compressed data,
735 1 if it starts with uncompressed low-order bits.
736
737 In Canon compressed data, 0xff is always followed by 0x00.
738 */
canon_has_lowbits()739 int CLASS canon_has_lowbits()
740 {
741 uchar test[0x4000];
742 int ret=1, i;
743
744 fseek (ifp, 0, SEEK_SET);
745 fread (test, 1, sizeof test, ifp);
746 for (i=540; i < sizeof test - 1; i++)
747 if (test[i] == 0xff) {
748 if (test[i+1]) return 1;
749 ret=0;
750 }
751 return ret;
752 }
753
canon_load_raw()754 void CLASS canon_load_raw()
755 {
756 ushort *pixel, *prow, *huff[2];
757 int nblocks, lowbits, i, c, row, r, save, val;
758 int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
759
760 crw_init_tables (tiff_compress, huff);
761 lowbits = canon_has_lowbits();
762 if (!lowbits) maximum = 0x3ff;
763 fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
764 zero_after_ff = 1;
765 getbits(-1);
766 for (row=0; row < raw_height; row+=8) {
767 pixel = raw_image + row*raw_width;
768 nblocks = MIN (8, raw_height-row) * raw_width >> 6;
769 for (block=0; block < nblocks; block++) {
770 memset (diffbuf, 0, sizeof diffbuf);
771 for (i=0; i < 64; i++ ) {
772 leaf = gethuff(huff[i > 0]);
773 if (leaf == 0 && i) break;
774 if (leaf == 0xff) continue;
775 i += leaf >> 4;
776 len = leaf & 15;
777 if (len == 0) continue;
778 diff = getbits(len);
779 if ((diff & (1 << (len-1))) == 0)
780 diff -= (1 << len) - 1;
781 if (i < 64) diffbuf[i] = diff;
782 }
783 diffbuf[0] += carry;
784 carry = diffbuf[0];
785 for (i=0; i < 64; i++ ) {
786 if (pnum++ % raw_width == 0)
787 base[0] = base[1] = 512;
788 if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
789 derror();
790 }
791 }
792 if (lowbits) {
793 save = ftell(ifp);
794 fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
795 for (prow=pixel, i=0; i < raw_width*2; i++) {
796 c = fgetc(ifp);
797 for (r=0; r < 8; r+=2, prow++) {
798 val = (*prow << 2) + ((c >> r) & 3);
799 if (raw_width == 2672 && val < 512) val += 2;
800 *prow = val;
801 }
802 }
803 fseek (ifp, save, SEEK_SET);
804 }
805 }
806 FORC(2) free (huff[c]);
807 }
808
809 struct jhead {
810 int algo, bits, high, wide, clrs, sraw, psv, restart, vpred[6];
811 ushort quant[64], idct[64], *huff[20], *free[20], *row;
812 };
813
ljpeg_start(struct jhead * jh,int info_only)814 int CLASS ljpeg_start (struct jhead *jh, int info_only)
815 {
816 ushort c, tag, len;
817 uchar data[0x10000];
818 const uchar *dp;
819
820 memset (jh, 0, sizeof *jh);
821 jh->restart = INT_MAX;
822 if ((fgetc(ifp),fgetc(ifp)) != 0xd8) return 0;
823 do {
824 if (!fread (data, 2, 2, ifp)) return 0;
825 tag = data[0] << 8 | data[1];
826 len = (data[2] << 8 | data[3]) - 2;
827 if (tag <= 0xff00) return 0;
828 fread (data, 1, len, ifp);
829 switch (tag) {
830 case 0xffc3:
831 jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
832 case 0xffc1:
833 case 0xffc0:
834 jh->algo = tag & 0xff;
835 jh->bits = data[0];
836 jh->high = data[1] << 8 | data[2];
837 jh->wide = data[3] << 8 | data[4];
838 jh->clrs = data[5] + jh->sraw;
839 if (len == 9 && !dng_version) getc(ifp);
840 break;
841 case 0xffc4:
842 if (info_only) break;
843 for (dp = data; dp < data+len && !((c = *dp++) & -20); )
844 jh->free[c] = jh->huff[c] = make_decoder_ref (&dp);
845 break;
846 case 0xffda:
847 jh->psv = data[1+data[0]*2];
848 jh->bits -= data[3+data[0]*2] & 15;
849 break;
850 case 0xffdb:
851 FORC(64) jh->quant[c] = data[c*2+1] << 8 | data[c*2+2];
852 break;
853 case 0xffdd:
854 jh->restart = data[0] << 8 | data[1];
855 }
856 } while (tag != 0xffda);
857 if (info_only) return 1;
858 if (jh->clrs > 6 || !jh->huff[0]) return 0;
859 FORC(19) if (!jh->huff[c+1]) jh->huff[c+1] = jh->huff[c];
860 if (jh->sraw) {
861 FORC(4) jh->huff[2+c] = jh->huff[1];
862 FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
863 }
864 jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
865 merror (jh->row, "ljpeg_start()");
866 return zero_after_ff = 1;
867 }
868
ljpeg_end(struct jhead * jh)869 void CLASS ljpeg_end (struct jhead *jh)
870 {
871 int c;
872 FORC4 if (jh->free[c]) free (jh->free[c]);
873 free (jh->row);
874 }
875
ljpeg_diff(ushort * huff)876 int CLASS ljpeg_diff (ushort *huff)
877 {
878 int len, diff;
879
880 len = gethuff(huff);
881 if (len == 16 && (!dng_version || dng_version >= 0x1010000))
882 return -32768;
883 diff = getbits(len);
884 if ((diff & (1 << (len-1))) == 0)
885 diff -= (1 << len) - 1;
886 return diff;
887 }
888
ljpeg_row(int jrow,struct jhead * jh)889 ushort * CLASS ljpeg_row (int jrow, struct jhead *jh)
890 {
891 int col, c, diff, pred, spred=0;
892 ushort mark=0, *row[3];
893
894 if (jrow * jh->wide % jh->restart == 0) {
895 FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
896 if (jrow) {
897 fseek (ifp, -2, SEEK_CUR);
898 do mark = (mark << 8) + (c = fgetc(ifp));
899 while (c != EOF && mark >> 4 != 0xffd);
900 }
901 getbits(-1);
902 }
903 FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
904 for (col=0; col < jh->wide; col++)
905 FORC(jh->clrs) {
906 diff = ljpeg_diff (jh->huff[c]);
907 if (jh->sraw && c <= jh->sraw && (col | c))
908 pred = spred;
909 else if (col) pred = row[0][-jh->clrs];
910 else pred = (jh->vpred[c] += diff) - diff;
911 if (jrow && col) switch (jh->psv) {
912 case 1: break;
913 case 2: pred = row[1][0]; break;
914 case 3: pred = row[1][-jh->clrs]; break;
915 case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
916 case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
917 case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
918 case 7: pred = (pred + row[1][0]) >> 1; break;
919 default: pred = 0;
920 }
921 if ((**row = pred + diff) >> jh->bits) derror();
922 if (c <= jh->sraw) spred = **row;
923 row[0]++; row[1]++;
924 }
925 return row[2];
926 }
927
lossless_jpeg_load_raw()928 void CLASS lossless_jpeg_load_raw()
929 {
930 int jwide, jrow, jcol, val, jidx, i, j, row=0, col=0;
931 struct jhead jh;
932 ushort *rp;
933
934 if (!ljpeg_start (&jh, 0)) return;
935 jwide = jh.wide * jh.clrs;
936
937 for (jrow=0; jrow < jh.high; jrow++) {
938 rp = ljpeg_row (jrow, &jh);
939 if (load_flags & 1)
940 row = jrow & 1 ? height-1-jrow/2 : jrow/2;
941 for (jcol=0; jcol < jwide; jcol++) {
942 val = curve[*rp++];
943 if (cr2_slice[0]) {
944 jidx = jrow*jwide + jcol;
945 i = jidx / (cr2_slice[1]*raw_height);
946 if ((j = i >= cr2_slice[0]))
947 i = cr2_slice[0];
948 jidx -= i * (cr2_slice[1]*raw_height);
949 row = jidx / cr2_slice[1+j];
950 col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
951 }
952 if (raw_width == 3984 && (col -= 2) < 0)
953 col += (row--,raw_width);
954 if ((unsigned) row < raw_height) RAW(row,col) = val;
955 if (++col >= raw_width)
956 col = (row++,0);
957 }
958 }
959 ljpeg_end (&jh);
960 }
961
canon_sraw_load_raw()962 void CLASS canon_sraw_load_raw()
963 {
964 struct jhead jh;
965 short *rp=0, (*ip)[4];
966 int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
967 int v[3]={0,0,0}, ver, hue;
968 char *cp;
969
970 if (!ljpeg_start (&jh, 0) || jh.clrs < 4) return;
971 jwide = (jh.wide >>= 1) * jh.clrs;
972
973 for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
974 scol = ecol;
975 ecol += cr2_slice[1] * 2 / jh.clrs;
976 if (!cr2_slice[0] || ecol > raw_width-1) ecol = raw_width & -2;
977 for (row=0; row < height; row += (jh.clrs >> 1) - 1) {
978 ip = (short (*)[4]) image + row*width;
979 for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
980 if ((jcol %= jwide) == 0)
981 rp = (short *) ljpeg_row (jrow++, &jh);
982 if (col >= width) continue;
983 FORC (jh.clrs-2)
984 ip[col + (c >> 1)*width + (c & 1)][0] = rp[jcol+c];
985 ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
986 ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
987 }
988 }
989 }
990 for (cp=model2; *cp && !isdigit(*cp); cp++);
991 sscanf (cp, "%d.%d.%d", v, v+1, v+2);
992 ver = (v[0]*1000 + v[1])*1000 + v[2];
993 hue = (jh.sraw+1) << 2;
994 if (unique_id >= 0x80000281 || (unique_id == 0x80000218 && ver > 1000006))
995 hue = jh.sraw << 1;
996 ip = (short (*)[4]) image;
997 rp = ip[0];
998 for (row=0; row < height; row++, ip+=width) {
999 if (row & (jh.sraw >> 1))
1000 for (col=0; col < width; col+=2)
1001 for (c=1; c < 3; c++)
1002 if (row == height-1)
1003 ip[col][c] = ip[col-width][c];
1004 else ip[col][c] = (ip[col-width][c] + ip[col+width][c] + 1) >> 1;
1005 for (col=1; col < width; col+=2)
1006 for (c=1; c < 3; c++)
1007 if (col == width-1)
1008 ip[col][c] = ip[col-1][c];
1009 else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
1010 }
1011 for ( ; rp < ip[0]; rp+=4) {
1012 if (unique_id == 0x80000218 ||
1013 unique_id == 0x80000250 ||
1014 unique_id == 0x80000261 ||
1015 unique_id == 0x80000281 ||
1016 unique_id == 0x80000287) {
1017 rp[1] = (rp[1] << 2) + hue;
1018 rp[2] = (rp[2] << 2) + hue;
1019 pix[0] = rp[0] + (( 50*rp[1] + 22929*rp[2]) >> 14);
1020 pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
1021 pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
1022 } else {
1023 if (unique_id < 0x80000218) rp[0] -= 512;
1024 pix[0] = rp[0] + rp[2];
1025 pix[2] = rp[0] + rp[1];
1026 pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12);
1027 }
1028 FORC3 rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
1029 }
1030 ljpeg_end (&jh);
1031 maximum = 0x3fff;
1032 }
1033
adobe_copy_pixel(unsigned row,unsigned col,ushort ** rp)1034 void CLASS adobe_copy_pixel (unsigned row, unsigned col, ushort **rp)
1035 {
1036 int c;
1037
1038 if (tiff_samples == 2 && shot_select) (*rp)++;
1039 if (raw_image) {
1040 if (row < raw_height && col < raw_width)
1041 RAW(row,col) = curve[**rp];
1042 *rp += tiff_samples;
1043 } else {
1044 if (row < height && col < width)
1045 FORC(tiff_samples)
1046 image[row*width+col][c] = curve[(*rp)[c]];
1047 *rp += tiff_samples;
1048 }
1049 if (tiff_samples == 2 && shot_select) (*rp)--;
1050 }
1051
ljpeg_idct(struct jhead * jh)1052 void CLASS ljpeg_idct (struct jhead *jh)
1053 {
1054 int c, i, j, len, skip, coef;
1055 float work[3][8][8];
1056 static float cs[106] = { 0 };
1057 static const uchar zigzag[80] =
1058 { 0, 1, 8,16, 9, 2, 3,10,17,24,32,25,18,11, 4, 5,12,19,26,33,
1059 40,48,41,34,27,20,13, 6, 7,14,21,28,35,42,49,56,57,50,43,36,
1060 29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,
1061 47,55,62,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63 };
1062
1063 if (!cs[0])
1064 FORC(106) cs[c] = cos((c & 31)*M_PI/16)/2;
1065 memset (work, 0, sizeof work);
1066 work[0][0][0] = jh->vpred[0] += ljpeg_diff (jh->huff[0]) * jh->quant[0];
1067 for (i=1; i < 64; i++ ) {
1068 len = gethuff (jh->huff[16]);
1069 i += skip = len >> 4;
1070 if (!(len &= 15) && skip < 15) break;
1071 coef = getbits(len);
1072 if ((coef & (1 << (len-1))) == 0)
1073 coef -= (1 << len) - 1;
1074 ((float *)work)[zigzag[i]] = coef * jh->quant[i];
1075 }
1076 FORC(8) work[0][0][c] *= M_SQRT1_2;
1077 FORC(8) work[0][c][0] *= M_SQRT1_2;
1078 for (i=0; i < 8; i++)
1079 for (j=0; j < 8; j++)
1080 FORC(8) work[1][i][j] += work[0][i][c] * cs[(j*2+1)*c];
1081 for (i=0; i < 8; i++)
1082 for (j=0; j < 8; j++)
1083 FORC(8) work[2][i][j] += work[1][c][j] * cs[(i*2+1)*c];
1084
1085 FORC(64) jh->idct[c] = CLIP(((float *)work[2])[c]+0.5);
1086 }
1087
lossless_dng_load_raw()1088 void CLASS lossless_dng_load_raw()
1089 {
1090 unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col, i, j;
1091 struct jhead jh;
1092 ushort *rp;
1093
1094 while (trow < raw_height) {
1095 save = ftell(ifp);
1096 if (tile_length < INT_MAX)
1097 fseek (ifp, get4(), SEEK_SET);
1098 if (!ljpeg_start (&jh, 0)) break;
1099 jwide = jh.wide;
1100 if (filters) jwide *= jh.clrs;
1101 jwide /= MIN (is_raw, tiff_samples);
1102 switch (jh.algo) {
1103 case 0xc1:
1104 jh.vpred[0] = 16384;
1105 getbits(-1);
1106 for (jrow=0; jrow+7 < jh.high; jrow += 8) {
1107 for (jcol=0; jcol+7 < jh.wide; jcol += 8) {
1108 ljpeg_idct (&jh);
1109 rp = jh.idct;
1110 row = trow + jcol/tile_width + jrow*2;
1111 col = tcol + jcol%tile_width;
1112 for (i=0; i < 16; i+=2)
1113 for (j=0; j < 8; j++)
1114 adobe_copy_pixel (row+i, col+j, &rp);
1115 }
1116 }
1117 break;
1118 case 0xc3:
1119 for (row=col=jrow=0; jrow < jh.high; jrow++) {
1120 rp = ljpeg_row (jrow, &jh);
1121 for (jcol=0; jcol < jwide; jcol++) {
1122 adobe_copy_pixel (trow+row, tcol+col, &rp);
1123 if (++col >= tile_width || col >= raw_width)
1124 row += 1 + (col = 0);
1125 }
1126 }
1127 }
1128 fseek (ifp, save+4, SEEK_SET);
1129 if ((tcol += tile_width) >= raw_width)
1130 trow += tile_length + (tcol = 0);
1131 ljpeg_end (&jh);
1132 }
1133 }
1134
packed_dng_load_raw()1135 void CLASS packed_dng_load_raw()
1136 {
1137 ushort *pixel, *rp;
1138 int row, col;
1139
1140 pixel = (ushort *) calloc (raw_width, tiff_samples*sizeof *pixel);
1141 merror (pixel, "packed_dng_load_raw()");
1142 for (row=0; row < raw_height; row++) {
1143 if (tiff_bps == 16)
1144 read_shorts (pixel, raw_width * tiff_samples);
1145 else {
1146 getbits(-1);
1147 for (col=0; col < raw_width * tiff_samples; col++)
1148 pixel[col] = getbits(tiff_bps);
1149 }
1150 for (rp=pixel, col=0; col < raw_width; col++)
1151 adobe_copy_pixel (row, col, &rp);
1152 }
1153 free (pixel);
1154 }
1155
pentax_load_raw()1156 void CLASS pentax_load_raw()
1157 {
1158 ushort bit[2][15], huff[4097];
1159 int dep, row, col, diff, c, i;
1160 ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
1161
1162 fseek (ifp, meta_offset, SEEK_SET);
1163 dep = (get2() + 12) & 15;
1164 fseek (ifp, 12, SEEK_CUR);
1165 FORC(dep) bit[0][c] = get2();
1166 FORC(dep) bit[1][c] = fgetc(ifp);
1167 FORC(dep)
1168 for (i=bit[0][c]; i <= ((bit[0][c]+(4096 >> bit[1][c])-1) & 4095); )
1169 huff[++i] = bit[1][c] << 8 | c;
1170 huff[0] = 12;
1171 fseek (ifp, data_offset, SEEK_SET);
1172 getbits(-1);
1173 for (row=0; row < raw_height; row++)
1174 for (col=0; col < raw_width; col++) {
1175 diff = ljpeg_diff (huff);
1176 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
1177 else hpred[col & 1] += diff;
1178 RAW(row,col) = hpred[col & 1];
1179 if (hpred[col & 1] >> tiff_bps) derror();
1180 }
1181 }
1182
nikon_load_raw()1183 void CLASS nikon_load_raw()
1184 {
1185 static const uchar nikon_tree[][32] = {
1186 { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
1187 5,4,3,6,2,7,1,0,8,9,11,10,12 },
1188 { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
1189 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
1190 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
1191 5,4,6,3,7,2,8,1,9,0,10,11,12 },
1192 { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
1193 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
1194 { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
1195 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
1196 { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
1197 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
1198 ushort *huff, ver0, ver1, vpred[2][2], hpred[2], csize;
1199 int i, min, max, step=0, tree=0, split=0, row, col, len, shl, diff;
1200
1201 fseek (ifp, meta_offset, SEEK_SET);
1202 ver0 = fgetc(ifp);
1203 ver1 = fgetc(ifp);
1204 if (ver0 == 0x49 || ver1 == 0x58)
1205 fseek (ifp, 2110, SEEK_CUR);
1206 if (ver0 == 0x46) tree = 2;
1207 if (tiff_bps == 14) tree += 3;
1208 read_shorts (vpred[0], 4);
1209 max = 1 << tiff_bps & 0x7fff;
1210 if ((csize = get2()) > 1)
1211 step = max / (csize-1);
1212 if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
1213 for (i=0; i < csize; i++)
1214 curve[i*step] = get2();
1215 for (i=0; i < max; i++)
1216 curve[i] = ( curve[i-i%step]*(step-i%step) +
1217 curve[i-i%step+step]*(i%step) ) / step;
1218 fseek (ifp, meta_offset+562, SEEK_SET);
1219 split = get2();
1220 } else if (ver0 != 0x46 && csize <= 0x4001)
1221 read_shorts (curve, max=csize);
1222 while (curve[max-2] == curve[max-1]) max--;
1223 huff = make_decoder (nikon_tree[tree]);
1224 fseek (ifp, data_offset, SEEK_SET);
1225 getbits(-1);
1226 for (min=row=0; row < height; row++) {
1227 if (split && row == split) {
1228 free (huff);
1229 huff = make_decoder (nikon_tree[tree+1]);
1230 max += (min = 16) << 1;
1231 }
1232 for (col=0; col < raw_width; col++) {
1233 i = gethuff(huff);
1234 len = i & 15;
1235 shl = i >> 4;
1236 diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
1237 if ((diff & (1 << (len-1))) == 0)
1238 diff -= (1 << len) - !shl;
1239 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
1240 else hpred[col & 1] += diff;
1241 if ((ushort)(hpred[col & 1] + min) >= max) derror();
1242 RAW(row,col) = curve[LIM((short)hpred[col & 1],0,0x3fff)];
1243 }
1244 }
1245 free (huff);
1246 }
1247
nikon_yuv_load_raw()1248 void CLASS nikon_yuv_load_raw()
1249 {
1250 int row, col, yuv[4], rgb[3], b, c;
1251 UINT64 bitbuf=0;
1252
1253 for (row=0; row < raw_height; row++)
1254 for (col=0; col < raw_width; col++) {
1255 if (!(b = col & 1)) {
1256 bitbuf = 0;
1257 FORC(6) bitbuf |= (UINT64) fgetc(ifp) << c*8;
1258 FORC(4) yuv[c] = (bitbuf >> c*12 & 0xfff) - (c >> 1 << 11);
1259 }
1260 rgb[0] = yuv[b] + 1.370705*yuv[3];
1261 rgb[1] = yuv[b] - 0.337633*yuv[2] - 0.698001*yuv[3];
1262 rgb[2] = yuv[b] + 1.732446*yuv[2];
1263 FORC3 image[row*width+col][c] = curve[LIM(rgb[c],0,0xfff)] / cam_mul[c];
1264 }
1265 }
1266
1267 /*
1268 Returns 1 for a Coolpix 995, 0 for anything else.
1269 */
nikon_e995()1270 int CLASS nikon_e995()
1271 {
1272 int i, histo[256];
1273 const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
1274
1275 memset (histo, 0, sizeof histo);
1276 fseek (ifp, -2000, SEEK_END);
1277 for (i=0; i < 2000; i++)
1278 histo[fgetc(ifp)]++;
1279 for (i=0; i < 4; i++)
1280 if (histo[often[i]] < 200)
1281 return 0;
1282 return 1;
1283 }
1284
1285 /*
1286 Returns 1 for a Coolpix 2100, 0 for anything else.
1287 */
nikon_e2100()1288 int CLASS nikon_e2100()
1289 {
1290 uchar t[12];
1291 int i;
1292
1293 fseek (ifp, 0, SEEK_SET);
1294 for (i=0; i < 1024; i++) {
1295 fread (t, 1, 12, ifp);
1296 if (((t[2] & t[4] & t[7] & t[9]) >> 4
1297 & t[1] & t[6] & t[8] & t[11] & 3) != 3)
1298 return 0;
1299 }
1300 return 1;
1301 }
1302
nikon_3700()1303 void CLASS nikon_3700()
1304 {
1305 int bits, i;
1306 uchar dp[24];
1307 static const struct {
1308 int bits;
1309 char make[12], model[15];
1310 } table[] = {
1311 { 0x00, "Pentax", "Optio 33WR" },
1312 { 0x03, "Nikon", "E3200" },
1313 { 0x32, "Nikon", "E3700" },
1314 { 0x33, "Olympus", "C740UZ" } };
1315
1316 fseek (ifp, 3072, SEEK_SET);
1317 fread (dp, 1, 24, ifp);
1318 bits = (dp[8] & 3) << 4 | (dp[20] & 3);
1319 for (i=0; i < sizeof table / sizeof *table; i++)
1320 if (bits == table[i].bits) {
1321 strcpy (make, table[i].make );
1322 strcpy (model, table[i].model);
1323 }
1324 }
1325
1326 /*
1327 Separates a Minolta DiMAGE Z2 from a Nikon E4300.
1328 */
minolta_z2()1329 int CLASS minolta_z2()
1330 {
1331 int i, nz;
1332 char tail[424];
1333
1334 fseek (ifp, -sizeof tail, SEEK_END);
1335 fread (tail, 1, sizeof tail, ifp);
1336 for (nz=i=0; i < sizeof tail; i++)
1337 if (tail[i]) nz++;
1338 return nz > 20;
1339 }
1340
1341 void CLASS jpeg_thumb();
1342
ppm_thumb()1343 void CLASS ppm_thumb()
1344 {
1345 char *thumb;
1346 thumb_length = thumb_width*thumb_height*3;
1347 thumb = (char *) malloc (thumb_length);
1348 merror (thumb, "ppm_thumb()");
1349 fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1350 fread (thumb, 1, thumb_length, ifp);
1351 fwrite (thumb, 1, thumb_length, ofp);
1352 free (thumb);
1353 }
1354
ppm16_thumb()1355 void CLASS ppm16_thumb()
1356 {
1357 int i;
1358 char *thumb;
1359 thumb_length = thumb_width*thumb_height*3;
1360 thumb = (char *) calloc (thumb_length, 2);
1361 merror (thumb, "ppm16_thumb()");
1362 read_shorts ((ushort *) thumb, thumb_length);
1363 for (i=0; i < thumb_length; i++)
1364 thumb[i] = ((ushort *) thumb)[i] >> 8;
1365 fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1366 fwrite (thumb, 1, thumb_length, ofp);
1367 free (thumb);
1368 }
1369
layer_thumb()1370 void CLASS layer_thumb()
1371 {
1372 int i, c;
1373 char *thumb, map[][4] = { "012","102" };
1374
1375 colors = thumb_misc >> 5 & 7;
1376 thumb_length = thumb_width*thumb_height;
1377 thumb = (char *) calloc (colors, thumb_length);
1378 merror (thumb, "layer_thumb()");
1379 fprintf (ofp, "P%d\n%d %d\n255\n",
1380 5 + (colors >> 1), thumb_width, thumb_height);
1381 fread (thumb, thumb_length, colors, ifp);
1382 for (i=0; i < thumb_length; i++)
1383 FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], ofp);
1384 free (thumb);
1385 }
1386
rollei_thumb()1387 void CLASS rollei_thumb()
1388 {
1389 unsigned i;
1390 ushort *thumb;
1391
1392 thumb_length = thumb_width * thumb_height;
1393 thumb = (ushort *) calloc (thumb_length, 2);
1394 merror (thumb, "rollei_thumb()");
1395 fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1396 read_shorts (thumb, thumb_length);
1397 for (i=0; i < thumb_length; i++) {
1398 putc (thumb[i] << 3, ofp);
1399 putc (thumb[i] >> 5 << 2, ofp);
1400 putc (thumb[i] >> 11 << 3, ofp);
1401 }
1402 free (thumb);
1403 }
1404
rollei_load_raw()1405 void CLASS rollei_load_raw()
1406 {
1407 uchar pixel[10];
1408 unsigned iten=0, isix, i, buffer=0, todo[16];
1409
1410 isix = raw_width * raw_height * 5 / 8;
1411 while (fread (pixel, 1, 10, ifp) == 10) {
1412 for (i=0; i < 10; i+=2) {
1413 todo[i] = iten++;
1414 todo[i+1] = pixel[i] << 8 | pixel[i+1];
1415 buffer = pixel[i] >> 2 | buffer << 6;
1416 }
1417 for ( ; i < 16; i+=2) {
1418 todo[i] = isix++;
1419 todo[i+1] = buffer >> (14-i)*5;
1420 }
1421 for (i=0; i < 16; i+=2)
1422 raw_image[todo[i]] = (todo[i+1] & 0x3ff);
1423 }
1424 maximum = 0x3ff;
1425 }
1426
raw(unsigned row,unsigned col)1427 int CLASS raw (unsigned row, unsigned col)
1428 {
1429 return (row < raw_height && col < raw_width) ? RAW(row,col) : 0;
1430 }
1431
phase_one_flat_field(int is_float,int nc)1432 void CLASS phase_one_flat_field (int is_float, int nc)
1433 {
1434 ushort head[8];
1435 unsigned wide, high, y, x, c, rend, cend, row, col;
1436 float *mrow, num, mult[4];
1437
1438 read_shorts (head, 8);
1439 if (head[2] * head[3] * head[4] * head[5] == 0) return;
1440 wide = head[2] / head[4] + (head[2] % head[4] != 0);
1441 high = head[3] / head[5] + (head[3] % head[5] != 0);
1442 mrow = (float *) calloc (nc*wide, sizeof *mrow);
1443 merror (mrow, "phase_one_flat_field()");
1444 for (y=0; y < high; y++) {
1445 for (x=0; x < wide; x++)
1446 for (c=0; c < nc; c+=2) {
1447 num = is_float ? getreal(11) : get2()/32768.0;
1448 if (y==0) mrow[c*wide+x] = num;
1449 else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
1450 }
1451 if (y==0) continue;
1452 rend = head[1] + y*head[5];
1453 for (row = rend-head[5];
1454 row < raw_height && row < rend &&
1455 row < head[1]+head[3]-head[5]; row++) {
1456 for (x=1; x < wide; x++) {
1457 for (c=0; c < nc; c+=2) {
1458 mult[c] = mrow[c*wide+x-1];
1459 mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
1460 }
1461 cend = head[0] + x*head[4];
1462 for (col = cend-head[4];
1463 col < raw_width &&
1464 col < cend && col < head[0]+head[2]-head[4]; col++) {
1465 c = nc > 2 ? FC(row-top_margin,col-left_margin) : 0;
1466 if (!(c & 1)) {
1467 c = RAW(row,col) * mult[c];
1468 RAW(row,col) = LIM(c,0,65535);
1469 }
1470 for (c=0; c < nc; c+=2)
1471 mult[c] += mult[c+1];
1472 }
1473 }
1474 for (x=0; x < wide; x++)
1475 for (c=0; c < nc; c+=2)
1476 mrow[c*wide+x] += mrow[(c+1)*wide+x];
1477 }
1478 }
1479 free (mrow);
1480 }
1481
phase_one_correct()1482 void CLASS phase_one_correct()
1483 {
1484 unsigned entries, tag, data, save, col, row, type;
1485 int len, i, j, k, cip, val[4], dev[4], sum, max;
1486 int head[9], diff, mindiff=INT_MAX, off_412=0;
1487 static const signed char dir[12][2] =
1488 { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
1489 {-2,-2}, {-2,2}, {2,-2}, {2,2} };
1490 float poly[8], num, cfrac, frac, mult[2], *yval[2];
1491 ushort *xval[2];
1492 int qmult_applied = 0, qlin_applied = 0;
1493
1494 if (half_size || !meta_length) return;
1495 if (verbose) fprintf (stderr,_("Phase One correction...\n"));
1496 fseek (ifp, meta_offset, SEEK_SET);
1497 order = get2();
1498 fseek (ifp, 6, SEEK_CUR);
1499 fseek (ifp, meta_offset+get4(), SEEK_SET);
1500 entries = get4(); get4();
1501 while (entries--) {
1502 tag = get4();
1503 len = get4();
1504 data = get4();
1505 save = ftell(ifp);
1506 fseek (ifp, meta_offset+data, SEEK_SET);
1507 if (tag == 0x419) { /* Polynomial curve */
1508 for (get4(), i=0; i < 8; i++)
1509 poly[i] = getreal(11);
1510 poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
1511 for (i=0; i < 0x10000; i++) {
1512 num = (poly[5]*i + poly[3])*i + poly[1];
1513 curve[i] = LIM(num,0,65535);
1514 } goto apply; /* apply to right half */
1515 } else if (tag == 0x41a) { /* Polynomial curve */
1516 for (i=0; i < 4; i++)
1517 poly[i] = getreal(11);
1518 for (i=0; i < 0x10000; i++) {
1519 for (num=0, j=4; j--; )
1520 num = num * i + poly[j];
1521 curve[i] = LIM(num+i,0,65535);
1522 } apply: /* apply to whole image */
1523 for (row=0; row < raw_height; row++)
1524 for (col = (tag & 1)*ph1.split_col; col < raw_width; col++)
1525 RAW(row,col) = curve[RAW(row,col)];
1526 } else if (tag == 0x400) { /* Sensor defects */
1527 while ((len -= 8) >= 0) {
1528 col = get2();
1529 row = get2();
1530 type = get2(); get2();
1531 if (col >= raw_width) continue;
1532 if (type == 131 || type == 137) /* Bad column */
1533 for (row=0; row < raw_height; row++)
1534 if (FC(row-top_margin,col-left_margin) == 1) {
1535 for (sum=i=0; i < 4; i++)
1536 sum += val[i] = raw (row+dir[i][0], col+dir[i][1]);
1537 for (max=i=0; i < 4; i++) {
1538 dev[i] = abs((val[i] << 2) - sum);
1539 if (dev[max] < dev[i]) max = i;
1540 }
1541 RAW(row,col) = (sum - val[max])/3.0 + 0.5;
1542 } else {
1543 for (sum=0, i=8; i < 12; i++)
1544 sum += raw (row+dir[i][0], col+dir[i][1]);
1545 RAW(row,col) = 0.5 + sum * 0.0732233 +
1546 (raw(row,col-2) + raw(row,col+2)) * 0.3535534;
1547 }
1548 else if (type == 129) { /* Bad pixel */
1549 if (row >= raw_height) continue;
1550 j = (FC(row-top_margin,col-left_margin) != 1) * 4;
1551 for (sum=0, i=j; i < j+8; i++)
1552 sum += raw (row+dir[i][0], col+dir[i][1]);
1553 RAW(row,col) = (sum + 4) >> 3;
1554 }
1555 }
1556 } else if (tag == 0x401) { /* All-color flat fields */
1557 phase_one_flat_field (1, 2);
1558 } else if (tag == 0x416 || tag == 0x410) {
1559 phase_one_flat_field (0, 2);
1560 } else if (tag == 0x40b) { /* Red+blue flat field */
1561 phase_one_flat_field (0, 4);
1562 } else if (tag == 0x412) {
1563 fseek (ifp, 36, SEEK_CUR);
1564 diff = abs (get2() - ph1.tag_21a);
1565 if (mindiff > diff) {
1566 mindiff = diff;
1567 off_412 = ftell(ifp) - 38;
1568 }
1569 } else if (tag == 0x41f && !qlin_applied) { /* Quadrant linearization */
1570 ushort lc[2][2][16], ref[16];
1571 int qr, qc;
1572 for (qr = 0; qr < 2; qr++)
1573 for (qc = 0; qc < 2; qc++)
1574 for (i = 0; i < 16; i++)
1575 lc[qr][qc][i] = get4();
1576 for (i = 0; i < 16; i++) {
1577 int v = 0;
1578 for (qr = 0; qr < 2; qr++)
1579 for (qc = 0; qc < 2; qc++)
1580 v += lc[qr][qc][i];
1581 ref[i] = (v + 2) >> 2;
1582 }
1583 for (qr = 0; qr < 2; qr++) {
1584 for (qc = 0; qc < 2; qc++) {
1585 int cx[19], cf[19];
1586 for (i = 0; i < 16; i++) {
1587 cx[1+i] = lc[qr][qc][i];
1588 cf[1+i] = ref[i];
1589 }
1590 cx[0] = cf[0] = 0;
1591 cx[17] = cf[17] = ((unsigned) ref[15] * 65535) / lc[qr][qc][15];
1592 cx[18] = cf[18] = 65535;
1593 cubic_spline(cx, cf, 19);
1594 for (row = (qr ? ph1.split_row : 0);
1595 row < (qr ? raw_height : ph1.split_row); row++)
1596 for (col = (qc ? ph1.split_col : 0);
1597 col < (qc ? raw_width : ph1.split_col); col++)
1598 RAW(row,col) = curve[RAW(row,col)];
1599 }
1600 }
1601 qlin_applied = 1;
1602 } else if (tag == 0x41e && !qmult_applied) { /* Quadrant multipliers */
1603 float qmult[2][2] = { { 1, 1 }, { 1, 1 } };
1604 get4(); get4(); get4(); get4();
1605 qmult[0][0] = 1.0 + getreal(11);
1606 get4(); get4(); get4(); get4(); get4();
1607 qmult[0][1] = 1.0 + getreal(11);
1608 get4(); get4(); get4();
1609 qmult[1][0] = 1.0 + getreal(11);
1610 get4(); get4(); get4();
1611 qmult[1][1] = 1.0 + getreal(11);
1612 for (row=0; row < raw_height; row++)
1613 for (col=0; col < raw_width; col++) {
1614 i = qmult[row >= ph1.split_row][col >= ph1.split_col] * RAW(row,col);
1615 RAW(row,col) = LIM(i,0,65535);
1616 }
1617 qmult_applied = 1;
1618 } else if (tag == 0x431 && !qmult_applied) { /* Quadrant combined */
1619 ushort lc[2][2][7], ref[7];
1620 int qr, qc;
1621 for (i = 0; i < 7; i++)
1622 ref[i] = get4();
1623 for (qr = 0; qr < 2; qr++)
1624 for (qc = 0; qc < 2; qc++)
1625 for (i = 0; i < 7; i++)
1626 lc[qr][qc][i] = get4();
1627 for (qr = 0; qr < 2; qr++) {
1628 for (qc = 0; qc < 2; qc++) {
1629 int cx[9], cf[9];
1630 for (i = 0; i < 7; i++) {
1631 cx[1+i] = ref[i];
1632 cf[1+i] = ((unsigned) ref[i] * lc[qr][qc][i]) / 10000;
1633 }
1634 cx[0] = cf[0] = 0;
1635 cx[8] = cf[8] = 65535;
1636 cubic_spline(cx, cf, 9);
1637 for (row = (qr ? ph1.split_row : 0);
1638 row < (qr ? raw_height : ph1.split_row); row++)
1639 for (col = (qc ? ph1.split_col : 0);
1640 col < (qc ? raw_width : ph1.split_col); col++)
1641 RAW(row,col) = curve[RAW(row,col)];
1642 }
1643 }
1644 qmult_applied = 1;
1645 qlin_applied = 1;
1646 }
1647 fseek (ifp, save, SEEK_SET);
1648 }
1649 if (off_412) {
1650 fseek (ifp, off_412, SEEK_SET);
1651 for (i=0; i < 9; i++) head[i] = get4() & 0x7fff;
1652 yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
1653 merror (yval[0], "phase_one_correct()");
1654 yval[1] = (float *) (yval[0] + head[1]*head[3]);
1655 xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
1656 xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
1657 get2();
1658 for (i=0; i < 2; i++)
1659 for (j=0; j < head[i+1]*head[i+3]; j++)
1660 yval[i][j] = getreal(11);
1661 for (i=0; i < 2; i++)
1662 for (j=0; j < head[i+1]*head[i+3]; j++)
1663 xval[i][j] = get2();
1664 for (row=0; row < raw_height; row++)
1665 for (col=0; col < raw_width; col++) {
1666 cfrac = (float) col * head[3] / raw_width;
1667 cfrac -= cip = cfrac;
1668 num = RAW(row,col) * 0.5;
1669 for (i=cip; i < cip+2; i++) {
1670 for (k=j=0; j < head[1]; j++)
1671 if (num < xval[0][k = head[1]*i+j]) break;
1672 frac = (j == 0 || j == head[1]) ? 0 :
1673 (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
1674 mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
1675 }
1676 i = ((mult[0] * (1-cfrac) + mult[1] * cfrac) * row + num) * 2;
1677 RAW(row,col) = LIM(i,0,65535);
1678 }
1679 free (yval[0]);
1680 }
1681 }
1682
phase_one_load_raw()1683 void CLASS phase_one_load_raw()
1684 {
1685 int a, b, i;
1686 ushort akey, bkey, mask;
1687
1688 fseek (ifp, ph1.key_off, SEEK_SET);
1689 akey = get2();
1690 bkey = get2();
1691 mask = ph1.format == 1 ? 0x5555:0x1354;
1692 fseek (ifp, data_offset, SEEK_SET);
1693 read_shorts (raw_image, raw_width*raw_height);
1694 if (ph1.format)
1695 for (i=0; i < raw_width*raw_height; i+=2) {
1696 a = raw_image[i+0] ^ akey;
1697 b = raw_image[i+1] ^ bkey;
1698 raw_image[i+0] = (a & mask) | (b & ~mask);
1699 raw_image[i+1] = (b & mask) | (a & ~mask);
1700 }
1701 }
1702
ph1_bithuff(int nbits,ushort * huff)1703 unsigned CLASS ph1_bithuff (int nbits, ushort *huff)
1704 {
1705 static UINT64 bitbuf=0;
1706 static int vbits=0;
1707 unsigned c;
1708
1709 if (nbits == -1)
1710 return bitbuf = vbits = 0;
1711 if (nbits == 0) return 0;
1712 if (vbits < nbits) {
1713 bitbuf = bitbuf << 32 | get4();
1714 vbits += 32;
1715 }
1716 c = bitbuf << (64-vbits) >> (64-nbits);
1717 if (huff) {
1718 vbits -= huff[c] >> 8;
1719 return (uchar) huff[c];
1720 }
1721 vbits -= nbits;
1722 return c;
1723 }
1724 #define ph1_bits(n) ph1_bithuff(n,0)
1725 #define ph1_huff(h) ph1_bithuff(*h,h+1)
1726
phase_one_load_raw_c()1727 void CLASS phase_one_load_raw_c()
1728 {
1729 static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
1730 int *offset, len[2], pred[2], row, col, i, j;
1731 ushort *pixel;
1732 short (*cblack)[2], (*rblack)[2];
1733
1734 pixel = (ushort *) calloc (raw_width*3 + raw_height*4, 2);
1735 merror (pixel, "phase_one_load_raw_c()");
1736 offset = (int *) (pixel + raw_width);
1737 fseek (ifp, strip_offset, SEEK_SET);
1738 for (row=0; row < raw_height; row++)
1739 offset[row] = get4();
1740 cblack = (short (*)[2]) (offset + raw_height);
1741 fseek (ifp, ph1.black_col, SEEK_SET);
1742 if (ph1.black_col)
1743 read_shorts ((ushort *) cblack[0], raw_height*2);
1744 rblack = cblack + raw_height;
1745 fseek (ifp, ph1.black_row, SEEK_SET);
1746 if (ph1.black_row)
1747 read_shorts ((ushort *) rblack[0], raw_width*2);
1748 for (i=0; i < 256; i++)
1749 curve[i] = i*i / 3.969 + 0.5;
1750 for (row=0; row < raw_height; row++) {
1751 fseek (ifp, data_offset + offset[row], SEEK_SET);
1752 ph1_bits(-1);
1753 pred[0] = pred[1] = 0;
1754 for (col=0; col < raw_width; col++) {
1755 if (col >= (raw_width & -8))
1756 len[0] = len[1] = 14;
1757 else if ((col & 7) == 0)
1758 for (i=0; i < 2; i++) {
1759 for (j=0; j < 5 && !ph1_bits(1); j++);
1760 if (j--) len[i] = length[j*2 + ph1_bits(1)];
1761 }
1762 if ((i = len[col & 1]) == 14)
1763 pixel[col] = pred[col & 1] = ph1_bits(16);
1764 else
1765 pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
1766 if (pred[col & 1] >> 16) derror();
1767 if (ph1.format == 5 && pixel[col] < 256)
1768 pixel[col] = curve[pixel[col]];
1769 }
1770 for (col=0; col < raw_width; col++) {
1771 i = (pixel[col] << 2) - ph1.black
1772 + cblack[row][col >= ph1.split_col]
1773 + rblack[col][row >= ph1.split_row];
1774 if (i > 0) RAW(row,col) = i;
1775 }
1776 }
1777 free (pixel);
1778 maximum = 0xfffc - ph1.black;
1779 }
1780
hasselblad_load_raw()1781 void CLASS hasselblad_load_raw()
1782 {
1783 struct jhead jh;
1784 int shot, row, col, *back[5], len[2], diff[12], pred, sh, f, s, c;
1785 unsigned upix, urow, ucol;
1786 ushort *ip;
1787
1788 if (!ljpeg_start (&jh, 0)) return;
1789 order = 0x4949;
1790 ph1_bits(-1);
1791 back[4] = (int *) calloc (raw_width, 3*sizeof **back);
1792 merror (back[4], "hasselblad_load_raw()");
1793 FORC3 back[c] = back[4] + c*raw_width;
1794 cblack[6] >>= sh = tiff_samples > 1;
1795 shot = LIM(shot_select, 1, tiff_samples) - 1;
1796 for (row=0; row < raw_height; row++) {
1797 FORC4 back[(c+3) & 3] = back[c];
1798 for (col=0; col < raw_width; col+=2) {
1799 for (s=0; s < tiff_samples*2; s+=2) {
1800 FORC(2) len[c] = ph1_huff(jh.huff[0]);
1801 FORC(2) {
1802 diff[s+c] = ph1_bits(len[c]);
1803 if ((diff[s+c] & (1 << (len[c]-1))) == 0)
1804 diff[s+c] -= (1 << len[c]) - 1;
1805 if (diff[s+c] == 65535) diff[s+c] = -32768;
1806 }
1807 }
1808 for (s=col; s < col+2; s++) {
1809 pred = 0x8000 + load_flags;
1810 if (col) pred = back[2][s-2];
1811 if (col && row > 1) switch (jh.psv) {
1812 case 11: pred += back[0][s]/2 - back[0][s-2]/2; break;
1813 }
1814 f = (row & 1)*3 ^ ((col+s) & 1);
1815 FORC (tiff_samples) {
1816 pred += diff[(s & 1)*tiff_samples+c];
1817 upix = pred >> sh & 0xffff;
1818 if (raw_image && c == shot)
1819 RAW(row,s) = upix;
1820 if (image) {
1821 urow = row-top_margin + (c & 1);
1822 ucol = col-left_margin - ((c >> 1) & 1);
1823 ip = &image[urow*width+ucol][f];
1824 if (urow < height && ucol < width)
1825 *ip = c < 4 ? upix : (*ip + upix) >> 1;
1826 }
1827 }
1828 back[2][s] = pred;
1829 }
1830 }
1831 }
1832 free (back[4]);
1833 ljpeg_end (&jh);
1834 if (image) mix_green = 1;
1835 }
1836
leaf_hdr_load_raw()1837 void CLASS leaf_hdr_load_raw()
1838 {
1839 ushort *pixel=0;
1840 unsigned tile=0, r, c, row, col;
1841
1842 if (!filters) {
1843 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1844 merror (pixel, "leaf_hdr_load_raw()");
1845 }
1846 FORC(tiff_samples)
1847 for (r=0; r < raw_height; r++) {
1848 if (r % tile_length == 0) {
1849 fseek (ifp, data_offset + 4*tile++, SEEK_SET);
1850 fseek (ifp, get4(), SEEK_SET);
1851 }
1852 if (filters && c != shot_select) continue;
1853 if (filters) pixel = raw_image + r*raw_width;
1854 read_shorts (pixel, raw_width);
1855 if (!filters && (row = r - top_margin) < height)
1856 for (col=0; col < width; col++)
1857 image[row*width+col][c] = pixel[col+left_margin];
1858 }
1859 if (!filters) {
1860 maximum = 0xffff;
1861 raw_color = 1;
1862 free (pixel);
1863 }
1864 }
1865
unpacked_load_raw()1866 void CLASS unpacked_load_raw()
1867 {
1868 int row, col, bits=0;
1869
1870 while (1 << ++bits < maximum);
1871 read_shorts (raw_image, raw_width*raw_height);
1872 for (row=0; row < raw_height; row++)
1873 for (col=0; col < raw_width; col++)
1874 if ((RAW(row,col) >>= load_flags) >> bits
1875 && (unsigned) (row-top_margin) < height
1876 && (unsigned) (col-left_margin) < width) derror();
1877 }
1878
sinar_4shot_load_raw()1879 void CLASS sinar_4shot_load_raw()
1880 {
1881 ushort *pixel;
1882 unsigned shot, row, col, r, c;
1883
1884 if (raw_image) {
1885 shot = LIM (shot_select, 1, 4) - 1;
1886 fseek (ifp, data_offset + shot*4, SEEK_SET);
1887 fseek (ifp, get4(), SEEK_SET);
1888 unpacked_load_raw();
1889 return;
1890 }
1891 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1892 merror (pixel, "sinar_4shot_load_raw()");
1893 for (shot=0; shot < 4; shot++) {
1894 fseek (ifp, data_offset + shot*4, SEEK_SET);
1895 fseek (ifp, get4(), SEEK_SET);
1896 for (row=0; row < raw_height; row++) {
1897 read_shorts (pixel, raw_width);
1898 if ((r = row-top_margin - (shot >> 1 & 1)) >= height) continue;
1899 for (col=0; col < raw_width; col++) {
1900 if ((c = col-left_margin - (shot & 1)) >= width) continue;
1901 image[r*width+c][(row & 1)*3 ^ (~col & 1)] = pixel[col];
1902 }
1903 }
1904 }
1905 free (pixel);
1906 mix_green = 1;
1907 }
1908
imacon_full_load_raw()1909 void CLASS imacon_full_load_raw()
1910 {
1911 int row, col;
1912
1913 if (!image) return;
1914 for (row=0; row < height; row++)
1915 for (col=0; col < width; col++)
1916 read_shorts (image[row*width+col], 3);
1917 }
1918
packed_load_raw()1919 void CLASS packed_load_raw()
1920 {
1921 int vbits=0, bwide, rbits, bite, half, irow, row, col, val, i;
1922 UINT64 bitbuf=0;
1923
1924 bwide = raw_width * tiff_bps / 8;
1925 bwide += bwide & load_flags >> 7;
1926 rbits = bwide * 8 - raw_width * tiff_bps;
1927 if (load_flags & 1) bwide = bwide * 16 / 15;
1928 bite = 8 + (load_flags & 24);
1929 half = (raw_height+1) >> 1;
1930 for (irow=0; irow < raw_height; irow++) {
1931 row = irow;
1932 if (load_flags & 2 &&
1933 (row = irow % half * 2 + irow / half) == 1 &&
1934 load_flags & 4) {
1935 if (vbits=0, tiff_compress)
1936 fseek (ifp, data_offset - (-half*bwide & -2048), SEEK_SET);
1937 else {
1938 fseek (ifp, 0, SEEK_END);
1939 fseek (ifp, ftell(ifp) >> 3 << 2, SEEK_SET);
1940 }
1941 }
1942 for (col=0; col < raw_width; col++) {
1943 for (vbits -= tiff_bps; vbits < 0; vbits += bite) {
1944 bitbuf <<= bite;
1945 for (i=0; i < bite; i+=8)
1946 bitbuf |= (unsigned) (fgetc(ifp) << i);
1947 }
1948 val = bitbuf << (64-tiff_bps-vbits) >> (64-tiff_bps);
1949 RAW(row,col ^ (load_flags >> 6 & 1)) = val;
1950 if (load_flags & 1 && (col % 10) == 9 && fgetc(ifp) &&
1951 row < height+top_margin && col < width+left_margin) derror();
1952 }
1953 vbits -= rbits;
1954 }
1955 }
1956
nokia_load_raw()1957 void CLASS nokia_load_raw()
1958 {
1959 uchar *data, *dp;
1960 int rev, dwide, row, col, c;
1961 double sum[]={0,0};
1962
1963 rev = 3 * (order == 0x4949);
1964 dwide = (raw_width * 5 + 1) / 4;
1965 data = (uchar *) malloc (dwide*2);
1966 merror (data, "nokia_load_raw()");
1967 for (row=0; row < raw_height; row++) {
1968 if (fread (data+dwide, 1, dwide, ifp) < dwide) derror();
1969 FORC(dwide) data[c] = data[dwide+(c ^ rev)];
1970 for (dp=data, col=0; col < raw_width; dp+=5, col+=4)
1971 FORC4 RAW(row,col+c) = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
1972 }
1973 free (data);
1974 maximum = 0x3ff;
1975 if (strcmp(make,"OmniVision")) return;
1976 row = raw_height/2;
1977 FORC(width-1) {
1978 sum[ c & 1] += SQR(RAW(row,c)-RAW(row+1,c+1));
1979 sum[~c & 1] += SQR(RAW(row+1,c)-RAW(row,c+1));
1980 }
1981 if (sum[1] > sum[0]) filters = 0x4b4b4b4b;
1982 }
1983
canon_rmf_load_raw()1984 void CLASS canon_rmf_load_raw()
1985 {
1986 int row, col, bits, orow, ocol, c;
1987
1988 for (row=0; row < raw_height; row++)
1989 for (col=0; col < raw_width-2; col+=3) {
1990 bits = get4();
1991 FORC3 {
1992 orow = row;
1993 if ((ocol = col+c-4) < 0) {
1994 ocol += raw_width;
1995 if ((orow -= 2) < 0)
1996 orow += raw_height;
1997 }
1998 RAW(orow,ocol) = curve[bits >> (10*c+2) & 0x3ff];
1999 }
2000 }
2001 maximum = curve[0x3ff];
2002 }
2003
pana_bits(int nbits)2004 unsigned CLASS pana_bits (int nbits)
2005 {
2006 static uchar buf[0x4000];
2007 static int vbits;
2008 int byte;
2009
2010 if (!nbits) return vbits=0;
2011 if (!vbits) {
2012 fread (buf+load_flags, 1, 0x4000-load_flags, ifp);
2013 fread (buf, 1, load_flags, ifp);
2014 }
2015 vbits = (vbits - nbits) & 0x1ffff;
2016 byte = vbits >> 3 ^ 0x3ff0;
2017 return (buf[byte] | buf[byte+1] << 8) >> (vbits & 7) & ~(-1 << nbits);
2018 }
2019
panasonic_load_raw()2020 void CLASS panasonic_load_raw()
2021 {
2022 int row, col, i, j, sh=0, pred[2], nonz[2];
2023
2024 pana_bits(0);
2025 for (row=0; row < height; row++)
2026 for (col=0; col < raw_width; col++) {
2027 if ((i = col % 14) == 0)
2028 pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
2029 if (i % 3 == 2) sh = 4 >> (3 - pana_bits(2));
2030 if (nonz[i & 1]) {
2031 if ((j = pana_bits(8))) {
2032 if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
2033 pred[i & 1] &= ~(-1 << sh);
2034 pred[i & 1] += j << sh;
2035 }
2036 } else if ((nonz[i & 1] = pana_bits(8)) || i > 11)
2037 pred[i & 1] = nonz[i & 1] << 4 | pana_bits(4);
2038 if ((RAW(row,col) = pred[col & 1]) > 4098 && col < width) derror();
2039 }
2040 }
2041
olympus_load_raw()2042 void CLASS olympus_load_raw()
2043 {
2044 ushort huff[4096];
2045 int row, col, nbits, sign, low, high, i, c, w, n, nw;
2046 int acarry[2][3], *carry, pred, diff;
2047
2048 huff[n=0] = 0xc0c;
2049 for (i=12; i--; )
2050 FORC(2048 >> i) huff[++n] = (i+1) << 8 | i;
2051 fseek (ifp, 7, SEEK_CUR);
2052 getbits(-1);
2053 for (row=0; row < height; row++) {
2054 memset (acarry, 0, sizeof acarry);
2055 for (col=0; col < raw_width; col++) {
2056 carry = acarry[col & 1];
2057 i = 2 * (carry[2] < 3);
2058 for (nbits=2+i; (ushort) carry[0] >> (nbits+i); nbits++);
2059 low = (sign = getbits(3)) & 3;
2060 sign = sign << 29 >> 31;
2061 if ((high = getbithuff(12,huff)) == 12)
2062 high = getbits(16-nbits) >> 1;
2063 carry[0] = (high << nbits) | getbits(nbits);
2064 diff = (carry[0] ^ sign) + carry[1];
2065 carry[1] = (diff*3 + carry[1]) >> 5;
2066 carry[2] = carry[0] > 16 ? 0 : carry[2]+1;
2067 if (col >= width) continue;
2068 if (row < 2 && col < 2) pred = 0;
2069 else if (row < 2) pred = RAW(row,col-2);
2070 else if (col < 2) pred = RAW(row-2,col);
2071 else {
2072 w = RAW(row,col-2);
2073 n = RAW(row-2,col);
2074 nw = RAW(row-2,col-2);
2075 if ((w < nw && nw < n) || (n < nw && nw < w)) {
2076 if (ABS(w-nw) > 32 || ABS(n-nw) > 32)
2077 pred = w + n - nw;
2078 else pred = (w + n) >> 1;
2079 } else pred = ABS(w-nw) > ABS(n-nw) ? w : n;
2080 }
2081 if ((RAW(row,col) = pred + ((diff << 2) | low)) >> 12) derror();
2082 }
2083 }
2084 }
2085
minolta_rd175_load_raw()2086 void CLASS minolta_rd175_load_raw()
2087 {
2088 uchar pixel[768];
2089 unsigned irow, box, row, col;
2090
2091 for (irow=0; irow < 1481; irow++) {
2092 if (fread (pixel, 1, 768, ifp) < 768) derror();
2093 box = irow / 82;
2094 row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
2095 switch (irow) {
2096 case 1477: case 1479: continue;
2097 case 1476: row = 984; break;
2098 case 1480: row = 985; break;
2099 case 1478: row = 985; box = 1;
2100 }
2101 if ((box < 12) && (box & 1)) {
2102 for (col=0; col < 1533; col++, row ^= 1)
2103 if (col != 1) RAW(row,col) = (col+1) & 2 ?
2104 pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
2105 RAW(row,1) = pixel[1] << 1;
2106 RAW(row,1533) = pixel[765] << 1;
2107 } else
2108 for (col=row & 1; col < 1534; col+=2)
2109 RAW(row,col) = pixel[col/2] << 1;
2110 }
2111 maximum = 0xff << 1;
2112 }
2113
quicktake_100_load_raw()2114 void CLASS quicktake_100_load_raw()
2115 {
2116 uchar pixel[484][644];
2117 static const short gstep[16] =
2118 { -89,-60,-44,-32,-22,-15,-8,-2,2,8,15,22,32,44,60,89 };
2119 static const short rstep[6][4] =
2120 { { -3,-1,1,3 }, { -5,-1,1,5 }, { -8,-2,2,8 },
2121 { -13,-3,3,13 }, { -19,-4,4,19 }, { -28,-6,6,28 } };
2122 static const short curve[256] =
2123 { 0,1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
2124 28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,53,
2125 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,
2126 79,80,81,82,83,84,86,88,90,92,94,97,99,101,103,105,107,110,112,114,116,
2127 118,120,123,125,127,129,131,134,136,138,140,142,144,147,149,151,153,155,
2128 158,160,162,164,166,168,171,173,175,177,179,181,184,186,188,190,192,195,
2129 197,199,201,203,205,208,210,212,214,216,218,221,223,226,230,235,239,244,
2130 248,252,257,261,265,270,274,278,283,287,291,296,300,305,309,313,318,322,
2131 326,331,335,339,344,348,352,357,361,365,370,374,379,383,387,392,396,400,
2132 405,409,413,418,422,426,431,435,440,444,448,453,457,461,466,470,474,479,
2133 483,487,492,496,500,508,519,531,542,553,564,575,587,598,609,620,631,643,
2134 654,665,676,687,698,710,721,732,743,754,766,777,788,799,810,822,833,844,
2135 855,866,878,889,900,911,922,933,945,956,967,978,989,1001,1012,1023 };
2136 int rb, row, col, sharp, val=0;
2137
2138 getbits(-1);
2139 memset (pixel, 0x80, sizeof pixel);
2140 for (row=2; row < height+2; row++) {
2141 for (col=2+(row & 1); col < width+2; col+=2) {
2142 val = ((pixel[row-1][col-1] + 2*pixel[row-1][col+1] +
2143 pixel[row][col-2]) >> 2) + gstep[getbits(4)];
2144 pixel[row][col] = val = LIM(val,0,255);
2145 if (col < 4)
2146 pixel[row][col-2] = pixel[row+1][~row & 1] = val;
2147 if (row == 2)
2148 pixel[row-1][col+1] = pixel[row-1][col+3] = val;
2149 }
2150 pixel[row][col] = val;
2151 }
2152 for (rb=0; rb < 2; rb++)
2153 for (row=2+rb; row < height+2; row+=2)
2154 for (col=3-(row & 1); col < width+2; col+=2) {
2155 if (row < 4 || col < 4) sharp = 2;
2156 else {
2157 val = ABS(pixel[row-2][col] - pixel[row][col-2])
2158 + ABS(pixel[row-2][col] - pixel[row-2][col-2])
2159 + ABS(pixel[row][col-2] - pixel[row-2][col-2]);
2160 sharp = val < 4 ? 0 : val < 8 ? 1 : val < 16 ? 2 :
2161 val < 32 ? 3 : val < 48 ? 4 : 5;
2162 }
2163 val = ((pixel[row-2][col] + pixel[row][col-2]) >> 1)
2164 + rstep[sharp][getbits(2)];
2165 pixel[row][col] = val = LIM(val,0,255);
2166 if (row < 4) pixel[row-2][col+2] = val;
2167 if (col < 4) pixel[row+2][col-2] = val;
2168 }
2169 for (row=2; row < height+2; row++)
2170 for (col=3-(row & 1); col < width+2; col+=2) {
2171 val = ((pixel[row][col-1] + (pixel[row][col] << 2) +
2172 pixel[row][col+1]) >> 1) - 0x100;
2173 pixel[row][col] = LIM(val,0,255);
2174 }
2175 for (row=0; row < height; row++)
2176 for (col=0; col < width; col++)
2177 RAW(row,col) = curve[pixel[row+2][col+2]];
2178 maximum = 0x3ff;
2179 }
2180
2181 #define radc_token(tree) ((signed char) getbithuff(8,huff[tree]))
2182
2183 #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
2184
2185 #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
2186 : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
2187
kodak_radc_load_raw()2188 void CLASS kodak_radc_load_raw()
2189 {
2190 static const char src[] = {
2191 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
2192 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
2193 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
2194 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
2195 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
2196 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
2197 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
2198 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
2199 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
2200 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
2201 1,0, 2,2, 2,-2,
2202 1,-3, 1,3,
2203 2,-17, 2,-5, 2,5, 2,17,
2204 2,-7, 2,2, 2,9, 2,18,
2205 2,-18, 2,-9, 2,-2, 2,7,
2206 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
2207 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
2208 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
2209 };
2210 ushort huff[19][256];
2211 int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
2212 short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
2213 static const ushort pt[] =
2214 { 0,0, 1280,1344, 2320,3616, 3328,8000, 4095,16383, 65535,16383 };
2215
2216 for (i=2; i < 12; i+=2)
2217 for (c=pt[i-2]; c <= pt[i]; c++)
2218 curve[c] = (float)
2219 (c-pt[i-2]) / (pt[i]-pt[i-2]) * (pt[i+1]-pt[i-1]) + pt[i-1] + 0.5;
2220 for (s=i=0; i < sizeof src; i+=2)
2221 FORC(256 >> src[i])
2222 ((ushort *)huff)[s++] = src[i] << 8 | (uchar) src[i+1];
2223 s = kodak_cbpp == 243 ? 2 : 3;
2224 FORC(256) huff[18][c] = (8-s) << 8 | c >> s << s | 1 << (s-1);
2225 getbits(-1);
2226 for (i=0; i < sizeof(buf)/sizeof(short); i++)
2227 ((short *)buf)[i] = 2048;
2228 for (row=0; row < height; row+=4) {
2229 FORC3 mul[c] = getbits(6);
2230 FORC3 {
2231 val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
2232 s = val > 65564 ? 10:12;
2233 x = ~(-1 << (s-1));
2234 val <<= 12-s;
2235 for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
2236 ((short *)buf[c])[i] = (((short *)buf[c])[i] * val + x) >> s;
2237 last[c] = mul[c];
2238 for (r=0; r <= !c; r++) {
2239 buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
2240 for (tree=1, col=width/2; col > 0; ) {
2241 if ((tree = radc_token(tree))) {
2242 col -= 2;
2243 if (tree == 8)
2244 FORYX buf[c][y][x] = (uchar) radc_token(18) * mul[c];
2245 else
2246 FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
2247 } else
2248 do {
2249 nreps = (col > 2) ? radc_token(9) + 1 : 1;
2250 for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
2251 col -= 2;
2252 FORYX buf[c][y][x] = PREDICTOR;
2253 if (rep & 1) {
2254 step = radc_token(10) << 4;
2255 FORYX buf[c][y][x] += step;
2256 }
2257 }
2258 } while (nreps == 9);
2259 }
2260 for (y=0; y < 2; y++)
2261 for (x=0; x < width/2; x++) {
2262 val = (buf[c][y+1][x] << 4) / mul[c];
2263 if (val < 0) val = 0;
2264 if (c) RAW(row+y*2+c-1,x*2+2-c) = val;
2265 else RAW(row+r*2+y,x*2+y) = val;
2266 }
2267 memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
2268 }
2269 }
2270 for (y=row; y < row+4; y++)
2271 for (x=0; x < width; x++)
2272 if ((x+y) & 1) {
2273 r = x ? x-1 : x+1;
2274 s = x+1 < width ? x+1 : x-1;
2275 val = (RAW(y,x)-2048)*2 + (RAW(y,r)+RAW(y,s))/2;
2276 if (val < 0) val = 0;
2277 RAW(y,x) = val;
2278 }
2279 }
2280 for (i=0; i < height*width; i++)
2281 raw_image[i] = curve[raw_image[i]];
2282 maximum = 0x3fff;
2283 }
2284
2285 #undef FORYX
2286 #undef PREDICTOR
2287
2288 #ifdef NO_JPEG
kodak_jpeg_load_raw()2289 void CLASS kodak_jpeg_load_raw() {}
lossy_dng_load_raw()2290 void CLASS lossy_dng_load_raw() {}
2291 #else
2292
2293 METHODDEF(boolean)
fill_input_buffer(j_decompress_ptr cinfo)2294 fill_input_buffer (j_decompress_ptr cinfo)
2295 {
2296 static uchar jpeg_buffer[4096];
2297 size_t nbytes;
2298
2299 nbytes = fread (jpeg_buffer, 1, 4096, ifp);
2300 swab (jpeg_buffer, jpeg_buffer, nbytes);
2301 cinfo->src->next_input_byte = jpeg_buffer;
2302 cinfo->src->bytes_in_buffer = nbytes;
2303 return TRUE;
2304 }
2305
kodak_jpeg_load_raw()2306 void CLASS kodak_jpeg_load_raw()
2307 {
2308 struct jpeg_decompress_struct cinfo;
2309 struct jpeg_error_mgr jerr;
2310 JSAMPARRAY buf;
2311 JSAMPLE (*pixel)[3];
2312 int row, col;
2313
2314 cinfo.err = jpeg_std_error (&jerr);
2315 jpeg_create_decompress (&cinfo);
2316 jpeg_stdio_src (&cinfo, ifp);
2317 cinfo.src->fill_input_buffer = fill_input_buffer;
2318 jpeg_read_header (&cinfo, TRUE);
2319 jpeg_start_decompress (&cinfo);
2320 if ((cinfo.output_width != width ) ||
2321 (cinfo.output_height*2 != height ) ||
2322 (cinfo.output_components != 3 )) {
2323 fprintf (stderr,_("%s: incorrect JPEG dimensions\n"), ifname);
2324 jpeg_destroy_decompress (&cinfo);
2325 longjmp (failure, 3);
2326 }
2327 buf = (*cinfo.mem->alloc_sarray)
2328 ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
2329
2330 while (cinfo.output_scanline < cinfo.output_height) {
2331 row = cinfo.output_scanline * 2;
2332 jpeg_read_scanlines (&cinfo, buf, 1);
2333 pixel = (JSAMPLE (*)[3]) buf[0];
2334 for (col=0; col < width; col+=2) {
2335 RAW(row+0,col+0) = pixel[col+0][1] << 1;
2336 RAW(row+1,col+1) = pixel[col+1][1] << 1;
2337 RAW(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
2338 RAW(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
2339 }
2340 }
2341 jpeg_finish_decompress (&cinfo);
2342 jpeg_destroy_decompress (&cinfo);
2343 maximum = 0xff << 1;
2344 }
2345
2346 void CLASS gamma_curve (double pwr, double ts, int mode, int imax);
2347
lossy_dng_load_raw()2348 void CLASS lossy_dng_load_raw()
2349 {
2350 struct jpeg_decompress_struct cinfo;
2351 struct jpeg_error_mgr jerr;
2352 JSAMPARRAY buf;
2353 JSAMPLE (*pixel)[3];
2354 unsigned sorder=order, ntags, opcode, deg, i, j, c;
2355 unsigned save=data_offset-4, trow=0, tcol=0, row, col;
2356 ushort cur[3][256];
2357 double coeff[9], tot;
2358
2359 if (meta_offset) {
2360 fseek (ifp, meta_offset, SEEK_SET);
2361 order = 0x4d4d;
2362 ntags = get4();
2363 while (ntags--) {
2364 opcode = get4(); get4(); get4();
2365 if (opcode != 8)
2366 { fseek (ifp, get4(), SEEK_CUR); continue; }
2367 fseek (ifp, 20, SEEK_CUR);
2368 if ((c = get4()) > 2) break;
2369 fseek (ifp, 12, SEEK_CUR);
2370 if ((deg = get4()) > 8) break;
2371 for (i=0; i <= deg && i < 9; i++)
2372 coeff[i] = getreal(12);
2373 for (i=0; i < 256; i++) {
2374 for (tot=j=0; j <= deg; j++)
2375 tot += coeff[j] * pow(i/255.0, j);
2376 cur[c][i] = tot*0xffff;
2377 }
2378 }
2379 order = sorder;
2380 } else {
2381 gamma_curve (1/2.4, 12.92, 1, 255);
2382 FORC3 memcpy (cur[c], curve, sizeof cur[0]);
2383 }
2384 cinfo.err = jpeg_std_error (&jerr);
2385 jpeg_create_decompress (&cinfo);
2386 while (trow < raw_height) {
2387 fseek (ifp, save+=4, SEEK_SET);
2388 if (tile_length < INT_MAX)
2389 fseek (ifp, get4(), SEEK_SET);
2390 jpeg_stdio_src (&cinfo, ifp);
2391 jpeg_read_header (&cinfo, TRUE);
2392 jpeg_start_decompress (&cinfo);
2393 buf = (*cinfo.mem->alloc_sarray)
2394 ((j_common_ptr) &cinfo, JPOOL_IMAGE, cinfo.output_width*3, 1);
2395 while (cinfo.output_scanline < cinfo.output_height &&
2396 (row = trow + cinfo.output_scanline) < height) {
2397 jpeg_read_scanlines (&cinfo, buf, 1);
2398 pixel = (JSAMPLE (*)[3]) buf[0];
2399 for (col=0; col < cinfo.output_width && tcol+col < width; col++) {
2400 FORC3 image[row*width+tcol+col][c] = cur[c][pixel[col][c]];
2401 }
2402 }
2403 jpeg_abort_decompress (&cinfo);
2404 if ((tcol += tile_width) >= raw_width)
2405 trow += tile_length + (tcol = 0);
2406 }
2407 jpeg_destroy_decompress (&cinfo);
2408 maximum = 0xffff;
2409 }
2410 #endif
2411
kodak_dc120_load_raw()2412 void CLASS kodak_dc120_load_raw()
2413 {
2414 static const int mul[4] = { 162, 192, 187, 92 };
2415 static const int add[4] = { 0, 636, 424, 212 };
2416 uchar pixel[848];
2417 int row, shift, col;
2418
2419 for (row=0; row < height; row++) {
2420 if (fread (pixel, 1, 848, ifp) < 848) derror();
2421 shift = row * mul[row & 3] + add[row & 3];
2422 for (col=0; col < width; col++)
2423 RAW(row,col) = (ushort) pixel[(col + shift) % 848];
2424 }
2425 maximum = 0xff;
2426 }
2427
eight_bit_load_raw()2428 void CLASS eight_bit_load_raw()
2429 {
2430 uchar *pixel;
2431 unsigned row, col;
2432
2433 pixel = (uchar *) calloc (raw_width, sizeof *pixel);
2434 merror (pixel, "eight_bit_load_raw()");
2435 for (row=0; row < raw_height; row++) {
2436 if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
2437 for (col=0; col < raw_width; col++)
2438 RAW(row,col) = curve[pixel[col]];
2439 }
2440 free (pixel);
2441 maximum = curve[0xff];
2442 }
2443
kodak_c330_load_raw()2444 void CLASS kodak_c330_load_raw()
2445 {
2446 uchar *pixel;
2447 int row, col, y, cb, cr, rgb[3], c;
2448
2449 pixel = (uchar *) calloc (raw_width, 2*sizeof *pixel);
2450 merror (pixel, "kodak_c330_load_raw()");
2451 for (row=0; row < height; row++) {
2452 if (fread (pixel, raw_width, 2, ifp) < 2) derror();
2453 if (load_flags && (row & 31) == 31)
2454 fseek (ifp, raw_width*32, SEEK_CUR);
2455 for (col=0; col < width; col++) {
2456 y = pixel[col*2];
2457 cb = pixel[(col*2 & -4) | 1] - 128;
2458 cr = pixel[(col*2 & -4) | 3] - 128;
2459 rgb[1] = y - ((cb + cr + 2) >> 2);
2460 rgb[2] = rgb[1] + cb;
2461 rgb[0] = rgb[1] + cr;
2462 FORC3 image[row*width+col][c] = curve[LIM(rgb[c],0,255)];
2463 }
2464 }
2465 free (pixel);
2466 maximum = curve[0xff];
2467 }
2468
kodak_c603_load_raw()2469 void CLASS kodak_c603_load_raw()
2470 {
2471 uchar *pixel;
2472 int row, col, y, cb, cr, rgb[3], c;
2473
2474 pixel = (uchar *) calloc (raw_width, 3*sizeof *pixel);
2475 merror (pixel, "kodak_c603_load_raw()");
2476 for (row=0; row < height; row++) {
2477 if (~row & 1)
2478 if (fread (pixel, raw_width, 3, ifp) < 3) derror();
2479 for (col=0; col < width; col++) {
2480 y = pixel[width*2*(row & 1) + col];
2481 cb = pixel[width + (col & -2)] - 128;
2482 cr = pixel[width + (col & -2)+1] - 128;
2483 rgb[1] = y - ((cb + cr + 2) >> 2);
2484 rgb[2] = rgb[1] + cb;
2485 rgb[0] = rgb[1] + cr;
2486 FORC3 image[row*width+col][c] = curve[LIM(rgb[c],0,255)];
2487 }
2488 }
2489 free (pixel);
2490 maximum = curve[0xff];
2491 }
2492
kodak_262_load_raw()2493 void CLASS kodak_262_load_raw()
2494 {
2495 static const uchar kodak_tree[2][26] =
2496 { { 0,1,5,1,1,2,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9 },
2497 { 0,3,1,1,1,1,1,2,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9 } };
2498 ushort *huff[2];
2499 uchar *pixel;
2500 int *strip, ns, c, row, col, chess, pi=0, pi1, pi2, pred, val;
2501
2502 FORC(2) huff[c] = make_decoder (kodak_tree[c]);
2503 ns = (raw_height+63) >> 5;
2504 pixel = (uchar *) malloc (raw_width*32 + ns*4);
2505 merror (pixel, "kodak_262_load_raw()");
2506 strip = (int *) (pixel + raw_width*32);
2507 order = 0x4d4d;
2508 FORC(ns) strip[c] = get4();
2509 for (row=0; row < raw_height; row++) {
2510 if ((row & 31) == 0) {
2511 fseek (ifp, strip[row >> 5], SEEK_SET);
2512 getbits(-1);
2513 pi = 0;
2514 }
2515 for (col=0; col < raw_width; col++) {
2516 chess = (row + col) & 1;
2517 pi1 = chess ? pi-2 : pi-raw_width-1;
2518 pi2 = chess ? pi-2*raw_width : pi-raw_width+1;
2519 if (col <= chess) pi1 = -1;
2520 if (pi1 < 0) pi1 = pi2;
2521 if (pi2 < 0) pi2 = pi1;
2522 if (pi1 < 0 && col > 1) pi1 = pi2 = pi-2;
2523 pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
2524 pixel[pi] = val = pred + ljpeg_diff (huff[chess]);
2525 if (val >> 8) derror();
2526 val = curve[pixel[pi++]];
2527 RAW(row,col) = val;
2528 }
2529 }
2530 free (pixel);
2531 FORC(2) free (huff[c]);
2532 }
2533
kodak_65000_decode(short * out,int bsize)2534 int CLASS kodak_65000_decode (short *out, int bsize)
2535 {
2536 uchar c, blen[768];
2537 ushort raw[6];
2538 INT64 bitbuf=0;
2539 int save, bits=0, i, j, len, diff;
2540
2541 save = ftell(ifp);
2542 bsize = (bsize + 3) & -4;
2543 for (i=0; i < bsize; i+=2) {
2544 c = fgetc(ifp);
2545 if ((blen[i ] = c & 15) > 12 ||
2546 (blen[i+1] = c >> 4) > 12 ) {
2547 fseek (ifp, save, SEEK_SET);
2548 for (i=0; i < bsize; i+=8) {
2549 read_shorts (raw, 6);
2550 out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
2551 out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
2552 for (j=0; j < 6; j++)
2553 out[i+2+j] = raw[j] & 0xfff;
2554 }
2555 return 1;
2556 }
2557 }
2558 if ((bsize & 7) == 4) {
2559 bitbuf = fgetc(ifp) << 8;
2560 bitbuf += fgetc(ifp);
2561 bits = 16;
2562 }
2563 for (i=0; i < bsize; i++) {
2564 len = blen[i];
2565 if (bits < len) {
2566 for (j=0; j < 32; j+=8)
2567 bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
2568 bits += 32;
2569 }
2570 diff = bitbuf & (0xffff >> (16-len));
2571 bitbuf >>= len;
2572 bits -= len;
2573 if ((diff & (1 << (len-1))) == 0)
2574 diff -= (1 << len) - 1;
2575 out[i] = diff;
2576 }
2577 return 0;
2578 }
2579
kodak_65000_load_raw()2580 void CLASS kodak_65000_load_raw()
2581 {
2582 short buf[256];
2583 int row, col, len, pred[2], ret, i;
2584
2585 for (row=0; row < height; row++)
2586 for (col=0; col < width; col+=256) {
2587 pred[0] = pred[1] = 0;
2588 len = MIN (256, width-col);
2589 ret = kodak_65000_decode (buf, len);
2590 for (i=0; i < len; i++)
2591 if ((RAW(row,col+i) = curve[ret ? buf[i] :
2592 (pred[i & 1] += buf[i])]) >> 12) derror();
2593 }
2594 }
2595
kodak_ycbcr_load_raw()2596 void CLASS kodak_ycbcr_load_raw()
2597 {
2598 short buf[384], *bp;
2599 int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
2600 ushort *ip;
2601
2602 if (!image) return;
2603 for (row=0; row < height; row+=2)
2604 for (col=0; col < width; col+=128) {
2605 len = MIN (128, width-col);
2606 kodak_65000_decode (buf, len*3);
2607 y[0][1] = y[1][1] = cb = cr = 0;
2608 for (bp=buf, i=0; i < len; i+=2, bp+=2) {
2609 cb += bp[4];
2610 cr += bp[5];
2611 rgb[1] = -((cb + cr + 2) >> 2);
2612 rgb[2] = rgb[1] + cb;
2613 rgb[0] = rgb[1] + cr;
2614 for (j=0; j < 2; j++)
2615 for (k=0; k < 2; k++) {
2616 if ((y[j][k] = y[j][k^1] + *bp++) >> 10) derror();
2617 ip = image[(row+j)*width + col+i+k];
2618 FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
2619 }
2620 }
2621 }
2622 }
2623
kodak_rgb_load_raw()2624 void CLASS kodak_rgb_load_raw()
2625 {
2626 short buf[768], *bp;
2627 int row, col, len, c, i, rgb[3];
2628 ushort *ip=image[0];
2629
2630 for (row=0; row < height; row++)
2631 for (col=0; col < width; col+=256) {
2632 len = MIN (256, width-col);
2633 kodak_65000_decode (buf, len*3);
2634 memset (rgb, 0, sizeof rgb);
2635 for (bp=buf, i=0; i < len; i++, ip+=4)
2636 FORC3 if ((ip[c] = rgb[c] += *bp++) >> 12) derror();
2637 }
2638 }
2639
kodak_thumb_load_raw()2640 void CLASS kodak_thumb_load_raw()
2641 {
2642 int row, col;
2643 colors = thumb_misc >> 5;
2644 for (row=0; row < height; row++)
2645 for (col=0; col < width; col++)
2646 read_shorts (image[row*width+col], colors);
2647 maximum = (1 << (thumb_misc & 31)) - 1;
2648 }
2649
sony_decrypt(unsigned * data,int len,int start,int key)2650 void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
2651 {
2652 static unsigned pad[128], p;
2653
2654 if (start) {
2655 for (p=0; p < 4; p++)
2656 pad[p] = key = key * 48828125 + 1;
2657 pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
2658 for (p=4; p < 127; p++)
2659 pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
2660 for (p=0; p < 127; p++)
2661 pad[p] = htonl(pad[p]);
2662 }
2663 while (len-- && p++)
2664 *data++ ^= pad[(p-1) & 127] = pad[p & 127] ^ pad[(p+64) & 127];
2665 }
2666
sony_load_raw()2667 void CLASS sony_load_raw()
2668 {
2669 uchar head[40];
2670 ushort *pixel;
2671 unsigned i, key, row, col;
2672
2673 fseek (ifp, 200896, SEEK_SET);
2674 fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
2675 order = 0x4d4d;
2676 key = get4();
2677 fseek (ifp, 164600, SEEK_SET);
2678 fread (head, 1, 40, ifp);
2679 sony_decrypt ((unsigned *) head, 10, 1, key);
2680 for (i=26; i-- > 22; )
2681 key = key << 8 | head[i];
2682 fseek (ifp, data_offset, SEEK_SET);
2683 for (row=0; row < raw_height; row++) {
2684 pixel = raw_image + row*raw_width;
2685 if (fread (pixel, 2, raw_width, ifp) < raw_width) derror();
2686 sony_decrypt ((unsigned *) pixel, raw_width/2, !row, key);
2687 for (col=0; col < raw_width; col++)
2688 if ((pixel[col] = ntohs(pixel[col])) >> 14) derror();
2689 }
2690 maximum = 0x3ff0;
2691 }
2692
sony_arw_load_raw()2693 void CLASS sony_arw_load_raw()
2694 {
2695 ushort huff[32770];
2696 static const ushort tab[18] =
2697 { 0xf11,0xf10,0xe0f,0xd0e,0xc0d,0xb0c,0xa0b,0x90a,0x809,
2698 0x708,0x607,0x506,0x405,0x304,0x303,0x300,0x202,0x201 };
2699 int i, c, n, col, row, sum=0;
2700
2701 huff[0] = 15;
2702 for (n=i=0; i < 18; i++)
2703 FORC(32768 >> (tab[i] >> 8)) huff[++n] = tab[i];
2704 getbits(-1);
2705 for (col = raw_width; col--; )
2706 for (row=0; row < raw_height+1; row+=2) {
2707 if (row == raw_height) row = 1;
2708 if ((sum += ljpeg_diff(huff)) >> 12) derror();
2709 if (row < height) RAW(row,col) = sum;
2710 }
2711 }
2712
gbl_dump_debayerbuf(ushort * buf)2713 void gbl_dump_debayerbuf(ushort *buf)
2714 {
2715 int i;
2716 for(i = 0; i < raw_width; i += 2) {
2717 fwrite(&(buf[i]), sizeof(ushort), 1, stdout); // R->R
2718 fwrite(&(buf[i+1]), sizeof(ushort), 1, stdout); // G1->G
2719 fwrite(&(buf[raw_width + i+1]), sizeof(ushort), 1, stdout); // B->B
2720 fwrite(&(buf[raw_width + i]), sizeof(ushort), 1, stdout); // G2->A
2721 }
2722 }
2723
sony_arw2_load_raw()2724 void CLASS sony_arw2_load_raw()
2725 {
2726 uchar *data, *dp;
2727 ushort pix[16];
2728 int row, col, val, max, min, imax, imin, sh, bit, i;
2729 long chunk_cnt=0;
2730 ushort outbuf[32];
2731 ushort *debayer_buf;
2732 int debayer_buf_cnt=0;
2733 int debayer_buf_size=raw_width * 2;
2734 int odd;
2735
2736 data = (uchar *) malloc (raw_width+1);
2737 merror (data, "sony_arw2_load_raw()");
2738 debayer_buf = (ushort *) malloc(sizeof(ushort) * debayer_buf_size);
2739 merror (debayer_buf, "sony_arw2_load_raw(debayer)");
2740 for (row=0; row < height; row++) {
2741 fread (data, 1, raw_width, ifp);
2742 for (dp=data, col=0; col < raw_width-30; dp+=16) {
2743 int maxes[16], mins[16], maxcnt=0, mincnt=0, alarm=0;
2744 max = 0x7ff & (val = sget4(dp));
2745 min = 0x7ff & val >> 11;
2746 imax = 0x0f & val >> 22;
2747 imin = 0x0f & val >> 26;
2748 for (sh=0; sh < 4 && 0x80 << sh <= max-min; sh++);
2749 for (bit=30, i=0; i < 16; i++) {
2750 if (i == imax)
2751 pix[i] = max;
2752 else if (i == imin)
2753 pix[i] = min;
2754 else {
2755 pix[i] = ((sget2(dp+(bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
2756 if (pix[i] > 0x7ff) pix[i] = 0x7ff;
2757 if(pix[i] > max || pix[i] < min) {
2758 alarm = 1;
2759 }
2760 bit += 7;
2761 }
2762 if(pix[i] == max)
2763 maxes[maxcnt++] = i;
2764 if(pix[i] == min)
2765 mins[mincnt++] = i;
2766 }
2767 odd = col % 2;
2768 for (i=0; i < 16; i++, col+=2) {
2769 outbuf[i * 2 + odd] = pix[i] << 5;
2770 // RAW(row,col) = curve[pix[i] << 1] >> 2;
2771 }
2772 if(mincnt > 1) {
2773 if(mins[0] != imin)
2774 alarm = 1;
2775 }
2776 if(maxcnt > 1) {
2777 if(maxes[maxcnt-1] != imax)
2778 alarm = 1;
2779 }
2780 if(alarm) {
2781 fprintf(stderr, "ALARM: %08x%02x\n", chunk_cnt, (imin << 4) | imax);
2782 }
2783 col -= odd ? 1:31;
2784 if(odd) {
2785 if(debayer_buf_cnt + 32 > debayer_buf_size) {
2786 fprintf(stderr, "assertion error, shouldn't happen\n");
2787 return;
2788 }
2789 memcpy(debayer_buf + debayer_buf_cnt, outbuf, sizeof(ushort)*32);
2790 debayer_buf_cnt += 32;
2791 if(debayer_buf_cnt >= debayer_buf_size) {
2792 gbl_dump_debayerbuf(debayer_buf);
2793 debayer_buf_cnt = 0;
2794 }
2795 }
2796 chunk_cnt++;
2797 }
2798 }
2799
2800 // 2 of tested cameras added zeroes for padding, whereas 1 added 0xFF
2801 // instead
2802 data[0] = 0;
2803 fread(data, 1, 1, ifp);
2804 if(data[0] != 0)
2805 fprintf(stderr, "PADDING: %hhx\n", data[0]);
2806
2807 free (data);
2808 free(debayer_buf);
2809 }
2810
samsung_load_raw()2811 void CLASS samsung_load_raw()
2812 {
2813 int row, col, c, i, dir, op[4], len[4];
2814
2815 order = 0x4949;
2816 for (row=0; row < raw_height; row++) {
2817 fseek (ifp, strip_offset+row*4, SEEK_SET);
2818 fseek (ifp, data_offset+get4(), SEEK_SET);
2819 ph1_bits(-1);
2820 FORC4 len[c] = row < 2 ? 7:4;
2821 for (col=0; col < raw_width; col+=16) {
2822 dir = ph1_bits(1);
2823 FORC4 op[c] = ph1_bits(2);
2824 FORC4 switch (op[c]) {
2825 case 3: len[c] = ph1_bits(4); break;
2826 case 2: len[c]--; break;
2827 case 1: len[c]++;
2828 }
2829 for (c=0; c < 16; c+=2) {
2830 i = len[((c & 1) << 1) | (c >> 3)];
2831 RAW(row,col+c) = ((signed) ph1_bits(i) << (32-i) >> (32-i)) +
2832 (dir ? RAW(row+(~c | -2),col+c) : col ? RAW(row,col+(c | -2)) : 128);
2833 if (c == 14) c = -1;
2834 }
2835 }
2836 }
2837 for (row=0; row < raw_height-1; row+=2)
2838 for (col=0; col < raw_width-1; col+=2)
2839 SWAP (RAW(row,col+1), RAW(row+1,col));
2840 }
2841
samsung2_load_raw()2842 void CLASS samsung2_load_raw()
2843 {
2844 static const ushort tab[14] =
2845 { 0x304,0x307,0x206,0x205,0x403,0x600,0x709,
2846 0x80a,0x90b,0xa0c,0xa0d,0x501,0x408,0x402 };
2847 ushort huff[1026], vpred[2][2] = {{0,0},{0,0}}, hpred[2];
2848 int i, c, n, row, col, diff;
2849
2850 huff[0] = 10;
2851 for (n=i=0; i < 14; i++)
2852 FORC(1024 >> (tab[i] >> 8)) huff[++n] = tab[i];
2853 getbits(-1);
2854 for (row=0; row < raw_height; row++)
2855 for (col=0; col < raw_width; col++) {
2856 diff = ljpeg_diff (huff);
2857 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
2858 else hpred[col & 1] += diff;
2859 RAW(row,col) = hpred[col & 1];
2860 if (hpred[col & 1] >> tiff_bps) derror();
2861 }
2862 }
2863
samsung3_load_raw()2864 void CLASS samsung3_load_raw()
2865 {
2866 int opt, init, mag, pmode, row, tab, col, pred, diff, i, c;
2867 ushort lent[3][2], len[4], *prow[2];
2868
2869 order = 0x4949;
2870 fseek (ifp, 9, SEEK_CUR);
2871 opt = fgetc(ifp);
2872 init = (get2(),get2());
2873 for (row=0; row < raw_height; row++) {
2874 fseek (ifp, (data_offset-ftell(ifp)) & 15, SEEK_CUR);
2875 ph1_bits(-1);
2876 mag = 0; pmode = 7;
2877 FORC(6) ((ushort *)lent)[c] = row < 2 ? 7:4;
2878 prow[ row & 1] = &RAW(row-1,1-((row & 1) << 1)); // green
2879 prow[~row & 1] = &RAW(row-2,0); // red and blue
2880 for (tab=0; tab+15 < raw_width; tab+=16) {
2881 if (~opt & 4 && !(tab & 63)) {
2882 i = ph1_bits(2);
2883 mag = i < 3 ? mag-'2'+"204"[i] : ph1_bits(12);
2884 }
2885 if (opt & 2)
2886 pmode = 7 - 4*ph1_bits(1);
2887 else if (!ph1_bits(1))
2888 pmode = ph1_bits(3);
2889 if (opt & 1 || !ph1_bits(1)) {
2890 FORC4 len[c] = ph1_bits(2);
2891 FORC4 {
2892 i = ((row & 1) << 1 | (c & 1)) % 3;
2893 len[c] = len[c] < 3 ? lent[i][0]-'1'+"120"[len[c]] : ph1_bits(4);
2894 lent[i][0] = lent[i][1];
2895 lent[i][1] = len[c];
2896 }
2897 }
2898 FORC(16) {
2899 col = tab + (((c & 7) << 1)^(c >> 3)^(row & 1));
2900 pred = (pmode == 7 || row < 2)
2901 ? (tab ? RAW(row,tab-2+(col & 1)) : init)
2902 : (prow[col & 1][col-'4'+"0224468"[pmode]] +
2903 prow[col & 1][col-'4'+"0244668"[pmode]] + 1) >> 1;
2904 diff = ph1_bits (i = len[c >> 2]);
2905 if (diff >> (i-1)) diff -= 1 << i;
2906 diff = diff * (mag*2+1) + mag;
2907 RAW(row,col) = pred + diff;
2908 }
2909 }
2910 }
2911 }
2912
2913 #define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
2914
2915 /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
smal_decode_segment(unsigned seg[2][2],int holes)2916 void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
2917 {
2918 uchar hist[3][13] = {
2919 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2920 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2921 { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
2922 int low, high=0xff, carry=0, nbits=8;
2923 int pix, s, count, bin, next, i, sym[3];
2924 uchar diff, pred[]={0,0};
2925 ushort data=0, range=0;
2926
2927 fseek (ifp, seg[0][1]+1, SEEK_SET);
2928 getbits(-1);
2929 for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
2930 for (s=0; s < 3; s++) {
2931 data = data << nbits | getbits(nbits);
2932 if (carry < 0)
2933 carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
2934 while (--nbits >= 0)
2935 if ((data >> nbits & 0xff) == 0xff) break;
2936 if (nbits > 0)
2937 data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
2938 ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
2939 if (nbits >= 0) {
2940 data += getbits(1);
2941 carry = nbits - 8;
2942 }
2943 count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
2944 for (bin=0; hist[s][bin+5] > count; bin++);
2945 low = hist[s][bin+5] * (high >> 4) >> 2;
2946 if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
2947 high -= low;
2948 for (nbits=0; high << nbits < 128; nbits++);
2949 range = (range+low) << nbits;
2950 high <<= nbits;
2951 next = hist[s][1];
2952 if (++hist[s][2] > hist[s][3]) {
2953 next = (next+1) & hist[s][0];
2954 hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
2955 hist[s][2] = 1;
2956 }
2957 if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
2958 if (bin < hist[s][1])
2959 for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
2960 else if (next <= bin)
2961 for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
2962 }
2963 hist[s][1] = next;
2964 sym[s] = bin;
2965 }
2966 diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
2967 if (sym[0] & 4)
2968 diff = diff ? -diff : 0x80;
2969 if (ftell(ifp) + 12 >= seg[1][1])
2970 diff = 0;
2971 raw_image[pix] = pred[pix & 1] += diff;
2972 if (!(pix & 1) && HOLE(pix / raw_width)) pix += 2;
2973 }
2974 maximum = 0xff;
2975 }
2976
smal_v6_load_raw()2977 void CLASS smal_v6_load_raw()
2978 {
2979 unsigned seg[2][2];
2980
2981 fseek (ifp, 16, SEEK_SET);
2982 seg[0][0] = 0;
2983 seg[0][1] = get2();
2984 seg[1][0] = raw_width * raw_height;
2985 seg[1][1] = INT_MAX;
2986 smal_decode_segment (seg, 0);
2987 }
2988
median4(int * p)2989 int CLASS median4 (int *p)
2990 {
2991 int min, max, sum, i;
2992
2993 min = max = sum = p[0];
2994 for (i=1; i < 4; i++) {
2995 sum += p[i];
2996 if (min > p[i]) min = p[i];
2997 if (max < p[i]) max = p[i];
2998 }
2999 return (sum - min - max) >> 1;
3000 }
3001
fill_holes(int holes)3002 void CLASS fill_holes (int holes)
3003 {
3004 int row, col, val[4];
3005
3006 for (row=2; row < height-2; row++) {
3007 if (!HOLE(row)) continue;
3008 for (col=1; col < width-1; col+=4) {
3009 val[0] = RAW(row-1,col-1);
3010 val[1] = RAW(row-1,col+1);
3011 val[2] = RAW(row+1,col-1);
3012 val[3] = RAW(row+1,col+1);
3013 RAW(row,col) = median4(val);
3014 }
3015 for (col=2; col < width-2; col+=4)
3016 if (HOLE(row-2) || HOLE(row+2))
3017 RAW(row,col) = (RAW(row,col-2) + RAW(row,col+2)) >> 1;
3018 else {
3019 val[0] = RAW(row,col-2);
3020 val[1] = RAW(row,col+2);
3021 val[2] = RAW(row-2,col);
3022 val[3] = RAW(row+2,col);
3023 RAW(row,col) = median4(val);
3024 }
3025 }
3026 }
3027
smal_v9_load_raw()3028 void CLASS smal_v9_load_raw()
3029 {
3030 unsigned seg[256][2], offset, nseg, holes, i;
3031
3032 fseek (ifp, 67, SEEK_SET);
3033 offset = get4();
3034 nseg = (uchar) fgetc(ifp);
3035 fseek (ifp, offset, SEEK_SET);
3036 for (i=0; i < nseg*2; i++)
3037 ((unsigned *)seg)[i] = get4() + data_offset*(i & 1);
3038 fseek (ifp, 78, SEEK_SET);
3039 holes = fgetc(ifp);
3040 fseek (ifp, 88, SEEK_SET);
3041 seg[nseg][0] = raw_height * raw_width;
3042 seg[nseg][1] = get4() + data_offset;
3043 for (i=0; i < nseg; i++)
3044 smal_decode_segment (seg+i, holes);
3045 if (holes) fill_holes (holes);
3046 }
3047
redcine_load_raw()3048 void CLASS redcine_load_raw()
3049 {
3050 #ifndef NO_JASPER
3051 int c, row, col;
3052 jas_stream_t *in;
3053 jas_image_t *jimg;
3054 jas_matrix_t *jmat;
3055 jas_seqent_t *data;
3056 ushort *img, *pix;
3057
3058 jas_init();
3059 in = jas_stream_fopen (ifname, "rb");
3060 jas_stream_seek (in, data_offset+20, SEEK_SET);
3061 jimg = jas_image_decode (in, -1, 0);
3062 if (!jimg) longjmp (failure, 3);
3063 jmat = jas_matrix_create (height/2, width/2);
3064 merror (jmat, "redcine_load_raw()");
3065 img = (ushort *) calloc ((height+2), (width+2)*2);
3066 merror (img, "redcine_load_raw()");
3067 FORC4 {
3068 jas_image_readcmpt (jimg, c, 0, 0, width/2, height/2, jmat);
3069 data = jas_matrix_getref (jmat, 0, 0);
3070 for (row = c >> 1; row < height; row+=2)
3071 for (col = c & 1; col < width; col+=2)
3072 img[(row+1)*(width+2)+col+1] = data[(row/2)*(width/2)+col/2];
3073 }
3074 for (col=1; col <= width; col++) {
3075 img[col] = img[2*(width+2)+col];
3076 img[(height+1)*(width+2)+col] = img[(height-1)*(width+2)+col];
3077 }
3078 for (row=0; row < height+2; row++) {
3079 img[row*(width+2)] = img[row*(width+2)+2];
3080 img[(row+1)*(width+2)-1] = img[(row+1)*(width+2)-3];
3081 }
3082 for (row=1; row <= height; row++) {
3083 pix = img + row*(width+2) + (col = 1 + (FC(row,1) & 1));
3084 for ( ; col <= width; col+=2, pix+=2) {
3085 c = (((pix[0] - 0x800) << 3) +
3086 pix[-(width+2)] + pix[width+2] + pix[-1] + pix[1]) >> 2;
3087 pix[0] = LIM(c,0,4095);
3088 }
3089 }
3090 for (row=0; row < height; row++)
3091 for (col=0; col < width; col++)
3092 RAW(row,col) = curve[img[(row+1)*(width+2)+col+1]];
3093 free (img);
3094 jas_matrix_destroy (jmat);
3095 jas_image_destroy (jimg);
3096 jas_stream_close (in);
3097 #endif
3098 }
3099
3100 /* RESTRICTED code starts here */
3101
foveon_decoder(unsigned size,unsigned code)3102 void CLASS foveon_decoder (unsigned size, unsigned code)
3103 {
3104 static unsigned huff[1024];
3105 struct decode *cur;
3106 int i, len;
3107
3108 if (!code) {
3109 for (i=0; i < size; i++)
3110 huff[i] = get4();
3111 memset (first_decode, 0, sizeof first_decode);
3112 free_decode = first_decode;
3113 }
3114 cur = free_decode++;
3115 if (free_decode > first_decode+2048) {
3116 fprintf (stderr,_("%s: decoder table overflow\n"), ifname);
3117 longjmp (failure, 2);
3118 }
3119 if (code)
3120 for (i=0; i < size; i++)
3121 if (huff[i] == code) {
3122 cur->leaf = i;
3123 return;
3124 }
3125 if ((len = code >> 27) > 26) return;
3126 code = (len+1) << 27 | (code & 0x3ffffff) << 1;
3127
3128 cur->branch[0] = free_decode;
3129 foveon_decoder (size, code);
3130 cur->branch[1] = free_decode;
3131 foveon_decoder (size, code+1);
3132 }
3133
foveon_thumb()3134 void CLASS foveon_thumb()
3135 {
3136 unsigned bwide, row, col, bitbuf=0, bit=1, c, i;
3137 char *buf;
3138 struct decode *dindex;
3139 short pred[3];
3140
3141 bwide = get4();
3142 fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
3143 if (bwide > 0) {
3144 if (bwide < thumb_width*3) return;
3145 buf = (char *) malloc (bwide);
3146 merror (buf, "foveon_thumb()");
3147 for (row=0; row < thumb_height; row++) {
3148 fread (buf, 1, bwide, ifp);
3149 fwrite (buf, 3, thumb_width, ofp);
3150 }
3151 free (buf);
3152 return;
3153 }
3154 foveon_decoder (256, 0);
3155
3156 for (row=0; row < thumb_height; row++) {
3157 memset (pred, 0, sizeof pred);
3158 if (!bit) get4();
3159 for (bit=col=0; col < thumb_width; col++)
3160 FORC3 {
3161 for (dindex=first_decode; dindex->branch[0]; ) {
3162 if ((bit = (bit-1) & 31) == 31)
3163 for (i=0; i < 4; i++)
3164 bitbuf = (bitbuf << 8) + fgetc(ifp);
3165 dindex = dindex->branch[bitbuf >> bit & 1];
3166 }
3167 pred[c] += dindex->leaf;
3168 fputc (pred[c], ofp);
3169 }
3170 }
3171 }
3172
foveon_sd_load_raw()3173 void CLASS foveon_sd_load_raw()
3174 {
3175 struct decode *dindex;
3176 short diff[1024];
3177 unsigned bitbuf=0;
3178 int pred[3], row, col, bit=-1, c, i;
3179
3180 read_shorts ((ushort *) diff, 1024);
3181 if (!load_flags) foveon_decoder (1024, 0);
3182
3183 for (row=0; row < height; row++) {
3184 memset (pred, 0, sizeof pred);
3185 if (!bit && !load_flags && atoi(model+2) < 14) get4();
3186 for (col=bit=0; col < width; col++) {
3187 if (load_flags) {
3188 bitbuf = get4();
3189 FORC3 pred[2-c] += diff[bitbuf >> c*10 & 0x3ff];
3190 }
3191 else FORC3 {
3192 for (dindex=first_decode; dindex->branch[0]; ) {
3193 if ((bit = (bit-1) & 31) == 31)
3194 for (i=0; i < 4; i++)
3195 bitbuf = (bitbuf << 8) + fgetc(ifp);
3196 dindex = dindex->branch[bitbuf >> bit & 1];
3197 }
3198 pred[c] += diff[dindex->leaf];
3199 if (pred[c] >> 16 && ~pred[c] >> 16) derror();
3200 }
3201 FORC3 image[row*width+col][c] = pred[c];
3202 }
3203 }
3204 }
3205
foveon_huff(ushort * huff)3206 void CLASS foveon_huff (ushort *huff)
3207 {
3208 int i, j, clen, code;
3209
3210 huff[0] = 8;
3211 for (i=0; i < 13; i++) {
3212 clen = getc(ifp);
3213 code = getc(ifp);
3214 for (j=0; j < 256 >> clen; )
3215 huff[code+ ++j] = clen << 8 | i;
3216 }
3217 get2();
3218 }
3219
foveon_dp_load_raw()3220 void CLASS foveon_dp_load_raw()
3221 {
3222 unsigned c, roff[4], row, col, diff;
3223 ushort huff[512], vpred[2][2], hpred[2];
3224
3225 fseek (ifp, 8, SEEK_CUR);
3226 foveon_huff (huff);
3227 roff[0] = 48;
3228 FORC3 roff[c+1] = -(-(roff[c] + get4()) & -16);
3229 FORC3 {
3230 fseek (ifp, data_offset+roff[c], SEEK_SET);
3231 getbits(-1);
3232 vpred[0][0] = vpred[0][1] = vpred[1][0] = vpred[1][1] = 512;
3233 for (row=0; row < height; row++) {
3234 for (col=0; col < width; col++) {
3235 diff = ljpeg_diff(huff);
3236 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
3237 else hpred[col & 1] += diff;
3238 image[row*width+col][c] = hpred[col & 1];
3239 }
3240 }
3241 }
3242 }
3243
foveon_load_camf()3244 void CLASS foveon_load_camf()
3245 {
3246 unsigned type, wide, high, i, j, row, col, diff;
3247 ushort huff[258], vpred[2][2] = {{512,512},{512,512}}, hpred[2];
3248
3249 fseek (ifp, meta_offset, SEEK_SET);
3250 type = get4(); get4(); get4();
3251 wide = get4();
3252 high = get4();
3253 if (type == 2) {
3254 fread (meta_data, 1, meta_length, ifp);
3255 for (i=0; i < meta_length; i++) {
3256 high = (high * 1597 + 51749) % 244944;
3257 wide = high * (INT64) 301593171 >> 24;
3258 meta_data[i] ^= ((((high << 8) - wide) >> 1) + wide) >> 17;
3259 }
3260 } else if (type == 4) {
3261 free (meta_data);
3262 meta_data = (char *) malloc (meta_length = wide*high*3/2);
3263 merror (meta_data, "foveon_load_camf()");
3264 foveon_huff (huff);
3265 get4();
3266 getbits(-1);
3267 for (j=row=0; row < high; row++) {
3268 for (col=0; col < wide; col++) {
3269 diff = ljpeg_diff(huff);
3270 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
3271 else hpred[col & 1] += diff;
3272 if (col & 1) {
3273 meta_data[j++] = hpred[0] >> 4;
3274 meta_data[j++] = hpred[0] << 4 | hpred[1] >> 8;
3275 meta_data[j++] = hpred[1];
3276 }
3277 }
3278 }
3279 } else
3280 fprintf (stderr,_("%s has unknown CAMF type %d.\n"), ifname, type);
3281 }
3282
foveon_camf_param(const char * block,const char * param)3283 const char * CLASS foveon_camf_param (const char *block, const char *param)
3284 {
3285 unsigned idx, num;
3286 char *pos, *cp, *dp;
3287
3288 for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
3289 pos = meta_data + idx;
3290 if (strncmp (pos, "CMb", 3)) break;
3291 if (pos[3] != 'P') continue;
3292 if (strcmp (block, pos+sget4(pos+12))) continue;
3293 cp = pos + sget4(pos+16);
3294 num = sget4(cp);
3295 dp = pos + sget4(cp+4);
3296 while (num--) {
3297 cp += 8;
3298 if (!strcmp (param, dp+sget4(cp)))
3299 return dp+sget4(cp+4);
3300 }
3301 }
3302 return 0;
3303 }
3304
foveon_camf_matrix(unsigned dim[3],const char * name)3305 void * CLASS foveon_camf_matrix (unsigned dim[3], const char *name)
3306 {
3307 unsigned i, idx, type, ndim, size, *mat;
3308 char *pos, *cp, *dp;
3309 double dsize;
3310
3311 for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
3312 pos = meta_data + idx;
3313 if (strncmp (pos, "CMb", 3)) break;
3314 if (pos[3] != 'M') continue;
3315 if (strcmp (name, pos+sget4(pos+12))) continue;
3316 dim[0] = dim[1] = dim[2] = 1;
3317 cp = pos + sget4(pos+16);
3318 type = sget4(cp);
3319 if ((ndim = sget4(cp+4)) > 3) break;
3320 dp = pos + sget4(cp+8);
3321 for (i=ndim; i--; ) {
3322 cp += 12;
3323 dim[i] = sget4(cp);
3324 }
3325 if ((dsize = (double) dim[0]*dim[1]*dim[2]) > meta_length/4) break;
3326 mat = (unsigned *) malloc ((size = dsize) * 4);
3327 merror (mat, "foveon_camf_matrix()");
3328 for (i=0; i < size; i++)
3329 if (type && type != 6)
3330 mat[i] = sget4(dp + i*4);
3331 else
3332 mat[i] = sget4(dp + i*2) & 0xffff;
3333 return mat;
3334 }
3335 fprintf (stderr,_("%s: \"%s\" matrix not found!\n"), ifname, name);
3336 return 0;
3337 }
3338
foveon_fixed(void * ptr,int size,const char * name)3339 int CLASS foveon_fixed (void *ptr, int size, const char *name)
3340 {
3341 void *dp;
3342 unsigned dim[3];
3343
3344 if (!name) return 0;
3345 dp = foveon_camf_matrix (dim, name);
3346 if (!dp) return 0;
3347 memcpy (ptr, dp, size*4);
3348 free (dp);
3349 return 1;
3350 }
3351
foveon_avg(short * pix,int range[2],float cfilt)3352 float CLASS foveon_avg (short *pix, int range[2], float cfilt)
3353 {
3354 int i;
3355 float val, min=FLT_MAX, max=-FLT_MAX, sum=0;
3356
3357 for (i=range[0]; i <= range[1]; i++) {
3358 sum += val = pix[i*4] + (pix[i*4]-pix[(i-1)*4]) * cfilt;
3359 if (min > val) min = val;
3360 if (max < val) max = val;
3361 }
3362 if (range[1] - range[0] == 1) return sum/2;
3363 return (sum - min - max) / (range[1] - range[0] - 1);
3364 }
3365
foveon_make_curve(double max,double mul,double filt)3366 short * CLASS foveon_make_curve (double max, double mul, double filt)
3367 {
3368 short *curve;
3369 unsigned i, size;
3370 double x;
3371
3372 if (!filt) filt = 0.8;
3373 size = 4*M_PI*max / filt;
3374 if (size == UINT_MAX) size--;
3375 curve = (short *) calloc (size+1, sizeof *curve);
3376 merror (curve, "foveon_make_curve()");
3377 curve[0] = size;
3378 for (i=0; i < size; i++) {
3379 x = i*filt/max/4;
3380 curve[i+1] = (cos(x)+1)/2 * tanh(i*filt/mul) * mul + 0.5;
3381 }
3382 return curve;
3383 }
3384
foveon_make_curves(short ** curvep,float dq[3],float div[3],float filt)3385 void CLASS foveon_make_curves
3386 (short **curvep, float dq[3], float div[3], float filt)
3387 {
3388 double mul[3], max=0;
3389 int c;
3390
3391 FORC3 mul[c] = dq[c]/div[c];
3392 FORC3 if (max < mul[c]) max = mul[c];
3393 FORC3 curvep[c] = foveon_make_curve (max, mul[c], filt);
3394 }
3395
foveon_apply_curve(short * curve,int i)3396 int CLASS foveon_apply_curve (short *curve, int i)
3397 {
3398 if (abs(i) >= curve[0]) return 0;
3399 return i < 0 ? -curve[1-i] : curve[1+i];
3400 }
3401
3402 #define image ((short (*)[4]) image)
3403
foveon_interpolate()3404 void CLASS foveon_interpolate()
3405 {
3406 static const short hood[] = { -1,-1, -1,0, -1,1, 0,-1, 0,1, 1,-1, 1,0, 1,1 };
3407 short *pix, prev[3], *curve[8], (*shrink)[3];
3408 float cfilt=0, ddft[3][3][2], ppm[3][3][3];
3409 float cam_xyz[3][3], correct[3][3], last[3][3], trans[3][3];
3410 float chroma_dq[3], color_dq[3], diag[3][3], div[3];
3411 float (*black)[3], (*sgain)[3], (*sgrow)[3];
3412 float fsum[3], val, frow, num;
3413 int row, col, c, i, j, diff, sgx, irow, sum, min, max, limit;
3414 int dscr[2][2], dstb[4], (*smrow[7])[3], total[4], ipix[3];
3415 int work[3][3], smlast, smred, smred_p=0, dev[3];
3416 int satlev[3], keep[4], active[4];
3417 unsigned dim[3], *badpix;
3418 double dsum=0, trsum[3];
3419 char str[128];
3420 const char* cp;
3421
3422 if (verbose)
3423 fprintf (stderr,_("Foveon interpolation...\n"));
3424
3425 foveon_load_camf();
3426 foveon_fixed (dscr, 4, "DarkShieldColRange");
3427 foveon_fixed (ppm[0][0], 27, "PostPolyMatrix");
3428 foveon_fixed (satlev, 3, "SaturationLevel");
3429 foveon_fixed (keep, 4, "KeepImageArea");
3430 foveon_fixed (active, 4, "ActiveImageArea");
3431 foveon_fixed (chroma_dq, 3, "ChromaDQ");
3432 foveon_fixed (color_dq, 3,
3433 foveon_camf_param ("IncludeBlocks", "ColorDQ") ?
3434 "ColorDQ" : "ColorDQCamRGB");
3435 if (foveon_camf_param ("IncludeBlocks", "ColumnFilter"))
3436 foveon_fixed (&cfilt, 1, "ColumnFilter");
3437
3438 memset (ddft, 0, sizeof ddft);
3439 if (!foveon_camf_param ("IncludeBlocks", "DarkDrift")
3440 || !foveon_fixed (ddft[1][0], 12, "DarkDrift"))
3441 for (i=0; i < 2; i++) {
3442 foveon_fixed (dstb, 4, i ? "DarkShieldBottom":"DarkShieldTop");
3443 for (row = dstb[1]; row <= dstb[3]; row++)
3444 for (col = dstb[0]; col <= dstb[2]; col++)
3445 FORC3 ddft[i+1][c][1] += (short) image[row*width+col][c];
3446 FORC3 ddft[i+1][c][1] /= (dstb[3]-dstb[1]+1) * (dstb[2]-dstb[0]+1);
3447 }
3448
3449 if (!(cp = foveon_camf_param ("WhiteBalanceIlluminants", model2)))
3450 { fprintf (stderr,_("%s: Invalid white balance \"%s\"\n"), ifname, model2);
3451 return; }
3452 foveon_fixed (cam_xyz, 9, cp);
3453 foveon_fixed (correct, 9,
3454 foveon_camf_param ("WhiteBalanceCorrections", model2));
3455 memset (last, 0, sizeof last);
3456 for (i=0; i < 3; i++)
3457 for (j=0; j < 3; j++)
3458 FORC3 last[i][j] += correct[i][c] * cam_xyz[c][j];
3459
3460 #define LAST(x,y) last[(i+x)%3][(c+y)%3]
3461 for (i=0; i < 3; i++)
3462 FORC3 diag[c][i] = LAST(1,1)*LAST(2,2) - LAST(1,2)*LAST(2,1);
3463 #undef LAST
3464 FORC3 div[c] = diag[c][0]*0.3127 + diag[c][1]*0.329 + diag[c][2]*0.3583;
3465 sprintf (str, "%sRGBNeutral", model2);
3466 if (foveon_camf_param ("IncludeBlocks", str))
3467 foveon_fixed (div, 3, str);
3468 num = 0;
3469 FORC3 if (num < div[c]) num = div[c];
3470 FORC3 div[c] /= num;
3471
3472 memset (trans, 0, sizeof trans);
3473 for (i=0; i < 3; i++)
3474 for (j=0; j < 3; j++)
3475 FORC3 trans[i][j] += rgb_cam[i][c] * last[c][j] * div[j];
3476 FORC3 trsum[c] = trans[c][0] + trans[c][1] + trans[c][2];
3477 dsum = (6*trsum[0] + 11*trsum[1] + 3*trsum[2]) / 20;
3478 for (i=0; i < 3; i++)
3479 FORC3 last[i][c] = trans[i][c] * dsum / trsum[i];
3480 memset (trans, 0, sizeof trans);
3481 for (i=0; i < 3; i++)
3482 for (j=0; j < 3; j++)
3483 FORC3 trans[i][j] += (i==c ? 32 : -1) * last[c][j] / 30;
3484
3485 foveon_make_curves (curve, color_dq, div, cfilt);
3486 FORC3 chroma_dq[c] /= 3;
3487 foveon_make_curves (curve+3, chroma_dq, div, cfilt);
3488 FORC3 dsum += chroma_dq[c] / div[c];
3489 curve[6] = foveon_make_curve (dsum, dsum, cfilt);
3490 curve[7] = foveon_make_curve (dsum*2, dsum*2, cfilt);
3491
3492 sgain = (float (*)[3]) foveon_camf_matrix (dim, "SpatialGain");
3493 if (!sgain) return;
3494 sgrow = (float (*)[3]) calloc (dim[1], sizeof *sgrow);
3495 sgx = (width + dim[1]-2) / (dim[1]-1);
3496
3497 black = (float (*)[3]) calloc (height, sizeof *black);
3498 for (row=0; row < height; row++) {
3499 for (i=0; i < 6; i++)
3500 ((float *)ddft[0])[i] = ((float *)ddft[1])[i] +
3501 row / (height-1.0) * (((float *)ddft[2])[i] - ((float *)ddft[1])[i]);
3502 FORC3 black[row][c] =
3503 ( foveon_avg (image[row*width]+c, dscr[0], cfilt) +
3504 foveon_avg (image[row*width]+c, dscr[1], cfilt) * 3
3505 - ddft[0][c][0] ) / 4 - ddft[0][c][1];
3506 }
3507 memcpy (black, black+8, sizeof *black*8);
3508 memcpy (black+height-11, black+height-22, 11*sizeof *black);
3509 memcpy (last, black, sizeof last);
3510
3511 for (row=1; row < height-1; row++) {
3512 FORC3 if (last[1][c] > last[0][c]) {
3513 if (last[1][c] > last[2][c])
3514 black[row][c] = (last[0][c] > last[2][c]) ? last[0][c]:last[2][c];
3515 } else
3516 if (last[1][c] < last[2][c])
3517 black[row][c] = (last[0][c] < last[2][c]) ? last[0][c]:last[2][c];
3518 memmove (last, last+1, 2*sizeof last[0]);
3519 memcpy (last[2], black[row+1], sizeof last[2]);
3520 }
3521 FORC3 black[row][c] = (last[0][c] + last[1][c])/2;
3522 FORC3 black[0][c] = (black[1][c] + black[3][c])/2;
3523
3524 val = 1 - exp(-1/24.0);
3525 memcpy (fsum, black, sizeof fsum);
3526 for (row=1; row < height; row++)
3527 FORC3 fsum[c] += black[row][c] =
3528 (black[row][c] - black[row-1][c])*val + black[row-1][c];
3529 memcpy (last[0], black[height-1], sizeof last[0]);
3530 FORC3 fsum[c] /= height;
3531 for (row = height; row--; )
3532 FORC3 last[0][c] = black[row][c] =
3533 (black[row][c] - fsum[c] - last[0][c])*val + last[0][c];
3534
3535 memset (total, 0, sizeof total);
3536 for (row=2; row < height; row+=4)
3537 for (col=2; col < width; col+=4) {
3538 FORC3 total[c] += (short) image[row*width+col][c];
3539 total[3]++;
3540 }
3541 for (row=0; row < height; row++)
3542 FORC3 black[row][c] += fsum[c]/2 + total[c]/(total[3]*100.0);
3543
3544 for (row=0; row < height; row++) {
3545 for (i=0; i < 6; i++)
3546 ((float *)ddft[0])[i] = ((float *)ddft[1])[i] +
3547 row / (height-1.0) * (((float *)ddft[2])[i] - ((float *)ddft[1])[i]);
3548 pix = image[row*width];
3549 memcpy (prev, pix, sizeof prev);
3550 frow = row / (height-1.0) * (dim[2]-1);
3551 if ((irow = frow) == dim[2]-1) irow--;
3552 frow -= irow;
3553 for (i=0; i < dim[1]; i++)
3554 FORC3 sgrow[i][c] = sgain[ irow *dim[1]+i][c] * (1-frow) +
3555 sgain[(irow+1)*dim[1]+i][c] * frow;
3556 for (col=0; col < width; col++) {
3557 FORC3 {
3558 diff = pix[c] - prev[c];
3559 prev[c] = pix[c];
3560 ipix[c] = pix[c] + floor ((diff + (diff*diff >> 14)) * cfilt
3561 - ddft[0][c][1] - ddft[0][c][0] * ((float) col/width - 0.5)
3562 - black[row][c] );
3563 }
3564 FORC3 {
3565 work[0][c] = ipix[c] * ipix[c] >> 14;
3566 work[2][c] = ipix[c] * work[0][c] >> 14;
3567 work[1][2-c] = ipix[(c+1) % 3] * ipix[(c+2) % 3] >> 14;
3568 }
3569 FORC3 {
3570 for (val=i=0; i < 3; i++)
3571 for ( j=0; j < 3; j++)
3572 val += ppm[c][i][j] * work[i][j];
3573 ipix[c] = floor ((ipix[c] + floor(val)) *
3574 ( sgrow[col/sgx ][c] * (sgx - col%sgx) +
3575 sgrow[col/sgx+1][c] * (col%sgx) ) / sgx / div[c]);
3576 if (ipix[c] > 32000) ipix[c] = 32000;
3577 pix[c] = ipix[c];
3578 }
3579 pix += 4;
3580 }
3581 }
3582 free (black);
3583 free (sgrow);
3584 free (sgain);
3585
3586 if ((badpix = (unsigned *) foveon_camf_matrix (dim, "BadPixels"))) {
3587 for (i=0; i < dim[0]; i++) {
3588 col = (badpix[i] >> 8 & 0xfff) - keep[0];
3589 row = (badpix[i] >> 20 ) - keep[1];
3590 if ((unsigned)(row-1) > height-3 || (unsigned)(col-1) > width-3)
3591 continue;
3592 memset (fsum, 0, sizeof fsum);
3593 for (sum=j=0; j < 8; j++)
3594 if (badpix[i] & (1 << j)) {
3595 FORC3 fsum[c] += (short)
3596 image[(row+hood[j*2])*width+col+hood[j*2+1]][c];
3597 sum++;
3598 }
3599 if (sum) FORC3 image[row*width+col][c] = fsum[c]/sum;
3600 }
3601 free (badpix);
3602 }
3603
3604 /* Array for 5x5 Gaussian averaging of red values */
3605 smrow[6] = (int (*)[3]) calloc (width*5, sizeof **smrow);
3606 merror (smrow[6], "foveon_interpolate()");
3607 for (i=0; i < 5; i++)
3608 smrow[i] = smrow[6] + i*width;
3609
3610 /* Sharpen the reds against these Gaussian averages */
3611 for (smlast=-1, row=2; row < height-2; row++) {
3612 while (smlast < row+2) {
3613 for (i=0; i < 6; i++)
3614 smrow[(i+5) % 6] = smrow[i];
3615 pix = image[++smlast*width+2];
3616 for (col=2; col < width-2; col++) {
3617 smrow[4][col][0] =
3618 (pix[0]*6 + (pix[-4]+pix[4])*4 + pix[-8]+pix[8] + 8) >> 4;
3619 pix += 4;
3620 }
3621 }
3622 pix = image[row*width+2];
3623 for (col=2; col < width-2; col++) {
3624 smred = ( 6 * smrow[2][col][0]
3625 + 4 * (smrow[1][col][0] + smrow[3][col][0])
3626 + smrow[0][col][0] + smrow[4][col][0] + 8 ) >> 4;
3627 if (col == 2)
3628 smred_p = smred;
3629 i = pix[0] + ((pix[0] - ((smred*7 + smred_p) >> 3)) >> 3);
3630 if (i > 32000) i = 32000;
3631 pix[0] = i;
3632 smred_p = smred;
3633 pix += 4;
3634 }
3635 }
3636
3637 /* Adjust the brighter pixels for better linearity */
3638 min = 0xffff;
3639 FORC3 {
3640 i = satlev[c] / div[c];
3641 if (min > i) min = i;
3642 }
3643 limit = min * 9 >> 4;
3644 for (pix=image[0]; pix < image[height*width]; pix+=4) {
3645 if (pix[0] <= limit || pix[1] <= limit || pix[2] <= limit)
3646 continue;
3647 min = max = pix[0];
3648 for (c=1; c < 3; c++) {
3649 if (min > pix[c]) min = pix[c];
3650 if (max < pix[c]) max = pix[c];
3651 }
3652 if (min >= limit*2) {
3653 pix[0] = pix[1] = pix[2] = max;
3654 } else {
3655 i = 0x4000 - ((min - limit) << 14) / limit;
3656 i = 0x4000 - (i*i >> 14);
3657 i = i*i >> 14;
3658 FORC3 pix[c] += (max - pix[c]) * i >> 14;
3659 }
3660 }
3661 /*
3662 Because photons that miss one detector often hit another,
3663 the sum R+G+B is much less noisy than the individual colors.
3664 So smooth the hues without smoothing the total.
3665 */
3666 for (smlast=-1, row=2; row < height-2; row++) {
3667 while (smlast < row+2) {
3668 for (i=0; i < 6; i++)
3669 smrow[(i+5) % 6] = smrow[i];
3670 pix = image[++smlast*width+2];
3671 for (col=2; col < width-2; col++) {
3672 FORC3 smrow[4][col][c] = (pix[c-4]+2*pix[c]+pix[c+4]+2) >> 2;
3673 pix += 4;
3674 }
3675 }
3676 pix = image[row*width+2];
3677 for (col=2; col < width-2; col++) {
3678 FORC3 dev[c] = -foveon_apply_curve (curve[7], pix[c] -
3679 ((smrow[1][col][c] + 2*smrow[2][col][c] + smrow[3][col][c]) >> 2));
3680 sum = (dev[0] + dev[1] + dev[2]) >> 3;
3681 FORC3 pix[c] += dev[c] - sum;
3682 pix += 4;
3683 }
3684 }
3685 for (smlast=-1, row=2; row < height-2; row++) {
3686 while (smlast < row+2) {
3687 for (i=0; i < 6; i++)
3688 smrow[(i+5) % 6] = smrow[i];
3689 pix = image[++smlast*width+2];
3690 for (col=2; col < width-2; col++) {
3691 FORC3 smrow[4][col][c] =
3692 (pix[c-8]+pix[c-4]+pix[c]+pix[c+4]+pix[c+8]+2) >> 2;
3693 pix += 4;
3694 }
3695 }
3696 pix = image[row*width+2];
3697 for (col=2; col < width-2; col++) {
3698 for (total[3]=375, sum=60, c=0; c < 3; c++) {
3699 for (total[c]=i=0; i < 5; i++)
3700 total[c] += smrow[i][col][c];
3701 total[3] += total[c];
3702 sum += pix[c];
3703 }
3704 if (sum < 0) sum = 0;
3705 j = total[3] > 375 ? (sum << 16) / total[3] : sum * 174;
3706 FORC3 pix[c] += foveon_apply_curve (curve[6],
3707 ((j*total[c] + 0x8000) >> 16) - pix[c]);
3708 pix += 4;
3709 }
3710 }
3711
3712 /* Transform the image to a different colorspace */
3713 for (pix=image[0]; pix < image[height*width]; pix+=4) {
3714 FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]);
3715 sum = (pix[0]+pix[1]+pix[1]+pix[2]) >> 2;
3716 FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]-sum);
3717 FORC3 {
3718 for (dsum=i=0; i < 3; i++)
3719 dsum += trans[c][i] * pix[i];
3720 if (dsum < 0) dsum = 0;
3721 if (dsum > 24000) dsum = 24000;
3722 ipix[c] = dsum + 0.5;
3723 }
3724 FORC3 pix[c] = ipix[c];
3725 }
3726
3727 /* Smooth the image bottom-to-top and save at 1/4 scale */
3728 shrink = (short (*)[3]) calloc ((height/4), (width/4)*sizeof *shrink);
3729 merror (shrink, "foveon_interpolate()");
3730 for (row = height/4; row--; )
3731 for (col=0; col < width/4; col++) {
3732 ipix[0] = ipix[1] = ipix[2] = 0;
3733 for (i=0; i < 4; i++)
3734 for (j=0; j < 4; j++)
3735 FORC3 ipix[c] += image[(row*4+i)*width+col*4+j][c];
3736 FORC3
3737 if (row+2 > height/4)
3738 shrink[row*(width/4)+col][c] = ipix[c] >> 4;
3739 else
3740 shrink[row*(width/4)+col][c] =
3741 (shrink[(row+1)*(width/4)+col][c]*1840 + ipix[c]*141 + 2048) >> 12;
3742 }
3743 /* From the 1/4-scale image, smooth right-to-left */
3744 for (row=0; row < (height & ~3); row++) {
3745 ipix[0] = ipix[1] = ipix[2] = 0;
3746 if ((row & 3) == 0)
3747 for (col = width & ~3 ; col--; )
3748 FORC3 smrow[0][col][c] = ipix[c] =
3749 (shrink[(row/4)*(width/4)+col/4][c]*1485 + ipix[c]*6707 + 4096) >> 13;
3750
3751 /* Then smooth left-to-right */
3752 ipix[0] = ipix[1] = ipix[2] = 0;
3753 for (col=0; col < (width & ~3); col++)
3754 FORC3 smrow[1][col][c] = ipix[c] =
3755 (smrow[0][col][c]*1485 + ipix[c]*6707 + 4096) >> 13;
3756
3757 /* Smooth top-to-bottom */
3758 if (row == 0)
3759 memcpy (smrow[2], smrow[1], sizeof **smrow * width);
3760 else
3761 for (col=0; col < (width & ~3); col++)
3762 FORC3 smrow[2][col][c] =
3763 (smrow[2][col][c]*6707 + smrow[1][col][c]*1485 + 4096) >> 13;
3764
3765 /* Adjust the chroma toward the smooth values */
3766 for (col=0; col < (width & ~3); col++) {
3767 for (i=j=30, c=0; c < 3; c++) {
3768 i += smrow[2][col][c];
3769 j += image[row*width+col][c];
3770 }
3771 j = (j << 16) / i;
3772 for (sum=c=0; c < 3; c++) {
3773 ipix[c] = foveon_apply_curve (curve[c+3],
3774 ((smrow[2][col][c] * j + 0x8000) >> 16) - image[row*width+col][c]);
3775 sum += ipix[c];
3776 }
3777 sum >>= 3;
3778 FORC3 {
3779 i = image[row*width+col][c] + ipix[c] - sum;
3780 if (i < 0) i = 0;
3781 image[row*width+col][c] = i;
3782 }
3783 }
3784 }
3785 free (shrink);
3786 free (smrow[6]);
3787 for (i=0; i < 8; i++)
3788 free (curve[i]);
3789
3790 /* Trim off the black border */
3791 active[1] -= keep[1];
3792 active[3] -= 2;
3793 i = active[2] - active[0];
3794 for (row=0; row < active[3]-active[1]; row++)
3795 memcpy (image[row*i], image[(row+active[1])*width+active[0]],
3796 i * sizeof *image);
3797 width = i;
3798 height = row;
3799 }
3800 #undef image
3801
3802 /* RESTRICTED code ends here */
3803
crop_masked_pixels()3804 void CLASS crop_masked_pixels()
3805 {
3806 int row, col;
3807 unsigned r, c, m, mblack[8], zero, val;
3808
3809 if (load_raw == &CLASS phase_one_load_raw ||
3810 load_raw == &CLASS phase_one_load_raw_c)
3811 phase_one_correct();
3812 if (fuji_width) {
3813 for (row=0; row < raw_height-top_margin*2; row++) {
3814 for (col=0; col < fuji_width << !fuji_layout; col++) {
3815 if (fuji_layout) {
3816 r = fuji_width - 1 - col + (row >> 1);
3817 c = col + ((row+1) >> 1);
3818 } else {
3819 r = fuji_width - 1 + row - (col >> 1);
3820 c = row + ((col+1) >> 1);
3821 }
3822 if (r < height && c < width)
3823 BAYER(r,c) = RAW(row+top_margin,col+left_margin);
3824 }
3825 }
3826 } else {
3827 for (row=0; row < height; row++)
3828 for (col=0; col < width; col++)
3829 BAYER2(row,col) = RAW(row+top_margin,col+left_margin);
3830 }
3831 if (mask[0][3] > 0) goto mask_set;
3832 if (load_raw == &CLASS canon_load_raw ||
3833 load_raw == &CLASS lossless_jpeg_load_raw) {
3834 mask[0][1] = mask[1][1] += 2;
3835 mask[0][3] -= 2;
3836 goto sides;
3837 }
3838 if (load_raw == &CLASS canon_600_load_raw ||
3839 load_raw == &CLASS sony_load_raw ||
3840 (load_raw == &CLASS eight_bit_load_raw && strncmp(model,"DC2",3)) ||
3841 load_raw == &CLASS kodak_262_load_raw ||
3842 (load_raw == &CLASS packed_load_raw && (load_flags & 32))) {
3843 sides:
3844 mask[0][0] = mask[1][0] = top_margin;
3845 mask[0][2] = mask[1][2] = top_margin+height;
3846 mask[0][3] += left_margin;
3847 mask[1][1] += left_margin+width;
3848 mask[1][3] += raw_width;
3849 }
3850 if (load_raw == &CLASS nokia_load_raw) {
3851 mask[0][2] = top_margin;
3852 mask[0][3] = width;
3853 }
3854 mask_set:
3855 memset (mblack, 0, sizeof mblack);
3856 for (zero=m=0; m < 8; m++)
3857 for (row=MAX(mask[m][0],0); row < MIN(mask[m][2],raw_height); row++)
3858 for (col=MAX(mask[m][1],0); col < MIN(mask[m][3],raw_width); col++) {
3859 c = FC(row-top_margin,col-left_margin);
3860 mblack[c] += val = RAW(row,col);
3861 mblack[4+c]++;
3862 zero += !val;
3863 }
3864 if (load_raw == &CLASS canon_600_load_raw && width < raw_width) {
3865 black = (mblack[0]+mblack[1]+mblack[2]+mblack[3]) /
3866 (mblack[4]+mblack[5]+mblack[6]+mblack[7]) - 4;
3867 canon_600_correct();
3868 } else if (zero < mblack[4] && mblack[5] && mblack[6] && mblack[7]) {
3869 FORC4 cblack[c] = mblack[c] / mblack[4+c];
3870 cblack[4] = cblack[5] = cblack[6] = 0;
3871 }
3872 }
3873
remove_zeroes()3874 void CLASS remove_zeroes()
3875 {
3876 unsigned row, col, tot, n, r, c;
3877
3878 for (row=0; row < height; row++)
3879 for (col=0; col < width; col++)
3880 if (BAYER(row,col) == 0) {
3881 tot = n = 0;
3882 for (r = row-2; r <= row+2; r++)
3883 for (c = col-2; c <= col+2; c++)
3884 if (r < height && c < width &&
3885 FC(r,c) == FC(row,col) && BAYER(r,c))
3886 tot += (n++,BAYER(r,c));
3887 if (n) BAYER(row,col) = tot/n;
3888 }
3889 }
3890
3891 /*
3892 Seach from the current directory up to the root looking for
3893 a ".badpixels" file, and fix those pixels now.
3894 */
bad_pixels(const char * cfname)3895 void CLASS bad_pixels (const char *cfname)
3896 {
3897 FILE *fp=0;
3898 char *fname, *cp, line[128];
3899 int len, time, row, col, r, c, rad, tot, n, fixed=0;
3900
3901 if (!filters) return;
3902 if (cfname)
3903 fp = fopen (cfname, "r");
3904 else {
3905 for (len=32 ; ; len *= 2) {
3906 fname = (char *) malloc (len);
3907 if (!fname) return;
3908 if (getcwd (fname, len-16)) break;
3909 free (fname);
3910 if (errno != ERANGE) return;
3911 }
3912 #if defined(WIN32) || defined(DJGPP)
3913 if (fname[1] == ':')
3914 memmove (fname, fname+2, len-2);
3915 for (cp=fname; *cp; cp++)
3916 if (*cp == '\\') *cp = '/';
3917 #endif
3918 cp = fname + strlen(fname);
3919 if (cp[-1] == '/') cp--;
3920 while (*fname == '/') {
3921 strcpy (cp, "/.badpixels");
3922 if ((fp = fopen (fname, "r"))) break;
3923 if (cp == fname) break;
3924 while (*--cp != '/');
3925 }
3926 free (fname);
3927 }
3928 if (!fp) return;
3929 while (fgets (line, 128, fp)) {
3930 cp = strchr (line, '#');
3931 if (cp) *cp = 0;
3932 if (sscanf (line, "%d %d %d", &col, &row, &time) != 3) continue;
3933 if ((unsigned) col >= width || (unsigned) row >= height) continue;
3934 if (time > timestamp) continue;
3935 for (tot=n=0, rad=1; rad < 3 && n==0; rad++)
3936 for (r = row-rad; r <= row+rad; r++)
3937 for (c = col-rad; c <= col+rad; c++)
3938 if ((unsigned) r < height && (unsigned) c < width &&
3939 (r != row || c != col) && fcol(r,c) == fcol(row,col)) {
3940 tot += BAYER2(r,c);
3941 n++;
3942 }
3943 BAYER2(row,col) = tot/n;
3944 if (verbose) {
3945 if (!fixed++)
3946 fprintf (stderr,_("Fixed dead pixels at:"));
3947 fprintf (stderr, " %d,%d", col, row);
3948 }
3949 }
3950 if (fixed) fputc ('\n', stderr);
3951 fclose (fp);
3952 }
3953
subtract(const char * fname)3954 void CLASS subtract (const char *fname)
3955 {
3956 FILE *fp;
3957 int dim[3]={0,0,0}, comment=0, number=0, error=0, nd=0, c, row, col;
3958 ushort *pixel;
3959
3960 if (!(fp = fopen (fname, "rb"))) {
3961 perror (fname); return;
3962 }
3963 if (fgetc(fp) != 'P' || fgetc(fp) != '5') error = 1;
3964 while (!error && nd < 3 && (c = fgetc(fp)) != EOF) {
3965 if (c == '#') comment = 1;
3966 if (c == '\n') comment = 0;
3967 if (comment) continue;
3968 if (isdigit(c)) number = 1;
3969 if (number) {
3970 if (isdigit(c)) dim[nd] = dim[nd]*10 + c -'0';
3971 else if (isspace(c)) {
3972 number = 0; nd++;
3973 } else error = 1;
3974 }
3975 }
3976 if (error || nd < 3) {
3977 fprintf (stderr,_("%s is not a valid PGM file!\n"), fname);
3978 fclose (fp); return;
3979 } else if (dim[0] != width || dim[1] != height || dim[2] != 65535) {
3980 fprintf (stderr,_("%s has the wrong dimensions!\n"), fname);
3981 fclose (fp); return;
3982 }
3983 pixel = (ushort *) calloc (width, sizeof *pixel);
3984 merror (pixel, "subtract()");
3985 for (row=0; row < height; row++) {
3986 fread (pixel, 2, width, fp);
3987 for (col=0; col < width; col++)
3988 BAYER(row,col) = MAX (BAYER(row,col) - ntohs(pixel[col]), 0);
3989 }
3990 free (pixel);
3991 fclose (fp);
3992 memset (cblack, 0, sizeof cblack);
3993 black = 0;
3994 }
3995
gamma_curve(double pwr,double ts,int mode,int imax)3996 void CLASS gamma_curve (double pwr, double ts, int mode, int imax)
3997 {
3998 int i;
3999 double g[6], bnd[2]={0,0}, r;
4000
4001 g[0] = pwr;
4002 g[1] = ts;
4003 g[2] = g[3] = g[4] = 0;
4004 bnd[g[1] >= 1] = 1;
4005 if (g[1] && (g[1]-1)*(g[0]-1) <= 0) {
4006 for (i=0; i < 48; i++) {
4007 g[2] = (bnd[0] + bnd[1])/2;
4008 if (g[0]) bnd[(pow(g[2]/g[1],-g[0]) - 1)/g[0] - 1/g[2] > -1] = g[2];
4009 else bnd[g[2]/exp(1-1/g[2]) < g[1]] = g[2];
4010 }
4011 g[3] = g[2] / g[1];
4012 if (g[0]) g[4] = g[2] * (1/g[0] - 1);
4013 }
4014 if (g[0]) g[5] = 1 / (g[1]*SQR(g[3])/2 - g[4]*(1 - g[3]) +
4015 (1 - pow(g[3],1+g[0]))*(1 + g[4])/(1 + g[0])) - 1;
4016 else g[5] = 1 / (g[1]*SQR(g[3])/2 + 1
4017 - g[2] - g[3] - g[2]*g[3]*(log(g[3]) - 1)) - 1;
4018 if (!mode--) {
4019 memcpy (gamm, g, sizeof gamm);
4020 return;
4021 }
4022 for (i=0; i < 0x10000; i++) {
4023 curve[i] = 0xffff;
4024 if ((r = (double) i / imax) < 1)
4025 curve[i] = 0x10000 * ( mode
4026 ? (r < g[3] ? r*g[1] : (g[0] ? pow( r,g[0])*(1+g[4])-g[4] : log(r)*g[2]+1))
4027 : (r < g[2] ? r/g[1] : (g[0] ? pow((r+g[4])/(1+g[4]),1/g[0]) : exp((r-1)/g[2]))));
4028 }
4029 }
4030
pseudoinverse(double (* in)[3],double (* out)[3],int size)4031 void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
4032 {
4033 double work[3][6], num;
4034 int i, j, k;
4035
4036 for (i=0; i < 3; i++) {
4037 for (j=0; j < 6; j++)
4038 work[i][j] = j == i+3;
4039 for (j=0; j < 3; j++)
4040 for (k=0; k < size; k++)
4041 work[i][j] += in[k][i] * in[k][j];
4042 }
4043 for (i=0; i < 3; i++) {
4044 num = work[i][i];
4045 for (j=0; j < 6; j++)
4046 work[i][j] /= num;
4047 for (k=0; k < 3; k++) {
4048 if (k==i) continue;
4049 num = work[k][i];
4050 for (j=0; j < 6; j++)
4051 work[k][j] -= work[i][j] * num;
4052 }
4053 }
4054 for (i=0; i < size; i++)
4055 for (j=0; j < 3; j++)
4056 for (out[i][j]=k=0; k < 3; k++)
4057 out[i][j] += work[j][k+3] * in[i][k];
4058 }
4059
cam_xyz_coeff(float rgb_cam[3][4],double cam_xyz[4][3])4060 void CLASS cam_xyz_coeff (float rgb_cam[3][4], double cam_xyz[4][3])
4061 {
4062 double cam_rgb[4][3], inverse[4][3], num;
4063 int i, j, k;
4064
4065 for (i=0; i < colors; i++) /* Multiply out XYZ colorspace */
4066 for (j=0; j < 3; j++)
4067 for (cam_rgb[i][j] = k=0; k < 3; k++)
4068 cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
4069
4070 for (i=0; i < colors; i++) { /* Normalize cam_rgb so that */
4071 for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
4072 num += cam_rgb[i][j];
4073 for (j=0; j < 3; j++)
4074 cam_rgb[i][j] /= num;
4075 pre_mul[i] = 1 / num;
4076 }
4077 pseudoinverse (cam_rgb, inverse, colors);
4078 for (i=0; i < 3; i++)
4079 for (j=0; j < colors; j++)
4080 rgb_cam[i][j] = inverse[j][i];
4081 }
4082
4083 #ifdef COLORCHECK
colorcheck()4084 void CLASS colorcheck()
4085 {
4086 #define NSQ 24
4087 // Coordinates of the GretagMacbeth ColorChecker squares
4088 // width, height, 1st_column, 1st_row
4089 int cut[NSQ][4]; // you must set these
4090 // ColorChecker Chart under 6500-kelvin illumination
4091 static const double gmb_xyY[NSQ][3] = {
4092 { 0.400, 0.350, 10.1 }, // Dark Skin
4093 { 0.377, 0.345, 35.8 }, // Light Skin
4094 { 0.247, 0.251, 19.3 }, // Blue Sky
4095 { 0.337, 0.422, 13.3 }, // Foliage
4096 { 0.265, 0.240, 24.3 }, // Blue Flower
4097 { 0.261, 0.343, 43.1 }, // Bluish Green
4098 { 0.506, 0.407, 30.1 }, // Orange
4099 { 0.211, 0.175, 12.0 }, // Purplish Blue
4100 { 0.453, 0.306, 19.8 }, // Moderate Red
4101 { 0.285, 0.202, 6.6 }, // Purple
4102 { 0.380, 0.489, 44.3 }, // Yellow Green
4103 { 0.473, 0.438, 43.1 }, // Orange Yellow
4104 { 0.187, 0.129, 6.1 }, // Blue
4105 { 0.305, 0.478, 23.4 }, // Green
4106 { 0.539, 0.313, 12.0 }, // Red
4107 { 0.448, 0.470, 59.1 }, // Yellow
4108 { 0.364, 0.233, 19.8 }, // Magenta
4109 { 0.196, 0.252, 19.8 }, // Cyan
4110 { 0.310, 0.316, 90.0 }, // White
4111 { 0.310, 0.316, 59.1 }, // Neutral 8
4112 { 0.310, 0.316, 36.2 }, // Neutral 6.5
4113 { 0.310, 0.316, 19.8 }, // Neutral 5
4114 { 0.310, 0.316, 9.0 }, // Neutral 3.5
4115 { 0.310, 0.316, 3.1 } }; // Black
4116 double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
4117 double inverse[NSQ][3], cam_xyz[4][3], balance[4], num;
4118 int c, i, j, k, sq, row, col, pass, count[4];
4119
4120 memset (gmb_cam, 0, sizeof gmb_cam);
4121 for (sq=0; sq < NSQ; sq++) {
4122 FORCC count[c] = 0;
4123 for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
4124 for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
4125 c = FC(row,col);
4126 if (c >= colors) c -= 2;
4127 gmb_cam[sq][c] += BAYER2(row,col);
4128 BAYER2(row,col) = black + (BAYER2(row,col)-black)/2;
4129 count[c]++;
4130 }
4131 FORCC gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - black;
4132 gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
4133 gmb_xyz[sq][1] = gmb_xyY[sq][2];
4134 gmb_xyz[sq][2] = gmb_xyY[sq][2] *
4135 (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
4136 }
4137 pseudoinverse (gmb_xyz, inverse, NSQ);
4138 for (pass=0; pass < 2; pass++) {
4139 for (raw_color = i=0; i < colors; i++)
4140 for (j=0; j < 3; j++)
4141 for (cam_xyz[i][j] = k=0; k < NSQ; k++)
4142 cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
4143 cam_xyz_coeff (rgb_cam, cam_xyz);
4144 FORCC balance[c] = pre_mul[c] * gmb_cam[20][c];
4145 for (sq=0; sq < NSQ; sq++)
4146 FORCC gmb_cam[sq][c] *= balance[c];
4147 }
4148 if (verbose) {
4149 printf (" { \"%s %s\", %d,\n\t{", make, model, black);
4150 num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
4151 FORCC for (j=0; j < 3; j++)
4152 printf ("%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
4153 puts (" } },");
4154 }
4155 #undef NSQ
4156 }
4157 #endif
4158
hat_transform(float * temp,float * base,int st,int size,int sc)4159 void CLASS hat_transform (float *temp, float *base, int st, int size, int sc)
4160 {
4161 int i;
4162 for (i=0; i < sc; i++)
4163 temp[i] = 2*base[st*i] + base[st*(sc-i)] + base[st*(i+sc)];
4164 for (; i+sc < size; i++)
4165 temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(i+sc)];
4166 for (; i < size; i++)
4167 temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(2*size-2-(i+sc))];
4168 }
4169
wavelet_denoise()4170 void CLASS wavelet_denoise()
4171 {
4172 float *fimg=0, *temp, thold, mul[2], avg, diff;
4173 int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
4174 ushort *window[4];
4175 static const float noise[] =
4176 { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
4177
4178 if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
4179
4180 while (maximum << scale < 0x10000) scale++;
4181 maximum <<= --scale;
4182 black <<= scale;
4183 FORC4 cblack[c] <<= scale;
4184 if ((size = iheight*iwidth) < 0x15550000)
4185 fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
4186 merror (fimg, "wavelet_denoise()");
4187 temp = fimg + size*3;
4188 if ((nc = colors) == 3 && filters) nc++;
4189 FORC(nc) { /* denoise R,G1,B,G3 individually */
4190 for (i=0; i < size; i++)
4191 fimg[i] = 256 * sqrt(image[i][c] << scale);
4192 for (hpass=lev=0; lev < 5; lev++) {
4193 lpass = size*((lev & 1)+1);
4194 for (row=0; row < iheight; row++) {
4195 hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
4196 for (col=0; col < iwidth; col++)
4197 fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
4198 }
4199 for (col=0; col < iwidth; col++) {
4200 hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
4201 for (row=0; row < iheight; row++)
4202 fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
4203 }
4204 thold = threshold * noise[lev];
4205 for (i=0; i < size; i++) {
4206 fimg[hpass+i] -= fimg[lpass+i];
4207 if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
4208 else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
4209 else fimg[hpass+i] = 0;
4210 if (hpass) fimg[i] += fimg[hpass+i];
4211 }
4212 hpass = lpass;
4213 }
4214 for (i=0; i < size; i++)
4215 image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
4216 }
4217 if (filters && colors == 3) { /* pull G1 and G3 closer together */
4218 for (row=0; row < 2; row++) {
4219 mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
4220 blk[row] = cblack[FC(row,0) | 1];
4221 }
4222 for (i=0; i < 4; i++)
4223 window[i] = (ushort *) fimg + width*i;
4224 for (wlast=-1, row=1; row < height-1; row++) {
4225 while (wlast < row+1) {
4226 for (wlast++, i=0; i < 4; i++)
4227 window[(i+3) & 3] = window[i];
4228 for (col = FC(wlast,1) & 1; col < width; col+=2)
4229 window[2][col] = BAYER(wlast,col);
4230 }
4231 thold = threshold/512;
4232 for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
4233 avg = ( window[0][col-1] + window[0][col+1] +
4234 window[2][col-1] + window[2][col+1] - blk[~row & 1]*4 )
4235 * mul[row & 1] + (window[1][col] + blk[row & 1]) * 0.5;
4236 avg = avg < 0 ? 0 : sqrt(avg);
4237 diff = sqrt(BAYER(row,col)) - avg;
4238 if (diff < -thold) diff += thold;
4239 else if (diff > thold) diff -= thold;
4240 else diff = 0;
4241 BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
4242 }
4243 }
4244 }
4245 free (fimg);
4246 }
4247
scale_colors()4248 void CLASS scale_colors()
4249 {
4250 unsigned bottom, right, size, row, col, ur, uc, i, x, y, c, sum[8];
4251 int val, dark, sat;
4252 double dsum[8], dmin, dmax;
4253 float scale_mul[4], fr, fc;
4254 ushort *img=0, *pix;
4255
4256 if (user_mul[0])
4257 memcpy (pre_mul, user_mul, sizeof pre_mul);
4258 if (use_auto_wb || (use_camera_wb && cam_mul[0] == -1)) {
4259 memset (dsum, 0, sizeof dsum);
4260 bottom = MIN (greybox[1]+greybox[3], height);
4261 right = MIN (greybox[0]+greybox[2], width);
4262 for (row=greybox[1]; row < bottom; row += 8)
4263 for (col=greybox[0]; col < right; col += 8) {
4264 memset (sum, 0, sizeof sum);
4265 for (y=row; y < row+8 && y < bottom; y++)
4266 for (x=col; x < col+8 && x < right; x++)
4267 FORC4 {
4268 if (filters) {
4269 c = fcol(y,x);
4270 val = BAYER2(y,x);
4271 } else
4272 val = image[y*width+x][c];
4273 if (val > maximum-25) goto skip_block;
4274 if ((val -= cblack[c]) < 0) val = 0;
4275 sum[c] += val;
4276 sum[c+4]++;
4277 if (filters) break;
4278 }
4279 FORC(8) dsum[c] += sum[c];
4280 skip_block: ;
4281 }
4282 FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
4283 }
4284 if (use_camera_wb && cam_mul[0] != -1) {
4285 memset (sum, 0, sizeof sum);
4286 for (row=0; row < 8; row++)
4287 for (col=0; col < 8; col++) {
4288 c = FC(row,col);
4289 if ((val = white[row][col] - cblack[c]) > 0)
4290 sum[c] += val;
4291 sum[c+4]++;
4292 }
4293 if (sum[0] && sum[1] && sum[2] && sum[3])
4294 FORC4 pre_mul[c] = (float) sum[c+4] / sum[c];
4295 else if (cam_mul[0] && cam_mul[2])
4296 memcpy (pre_mul, cam_mul, sizeof pre_mul);
4297 else
4298 fprintf (stderr,_("%s: Cannot use camera white balance.\n"), ifname);
4299 }
4300 if (pre_mul[1] == 0) pre_mul[1] = 1;
4301 if (pre_mul[3] == 0) pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
4302 dark = black;
4303 sat = maximum;
4304 if (threshold) wavelet_denoise();
4305 maximum -= black;
4306 for (dmin=DBL_MAX, dmax=c=0; c < 4; c++) {
4307 if (dmin > pre_mul[c])
4308 dmin = pre_mul[c];
4309 if (dmax < pre_mul[c])
4310 dmax = pre_mul[c];
4311 }
4312 if (!highlight) dmax = dmin;
4313 FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
4314 if (verbose) {
4315 fprintf (stderr,
4316 _("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark, sat);
4317 FORC4 fprintf (stderr, " %f", pre_mul[c]);
4318 fputc ('\n', stderr);
4319 }
4320 if (filters > 1000 && (cblack[4]+1)/2 == 1 && (cblack[5]+1)/2 == 1) {
4321 FORC4 cblack[FC(c/2,c%2)] +=
4322 cblack[6 + c/2 % cblack[4] * cblack[5] + c%2 % cblack[5]];
4323 cblack[4] = cblack[5] = 0;
4324 }
4325 size = iheight*iwidth;
4326 for (i=0; i < size*4; i++) {
4327 if (!(val = ((ushort *)image)[i])) continue;
4328 if (cblack[4] && cblack[5])
4329 val -= cblack[6 + i/4 / iwidth % cblack[4] * cblack[5] +
4330 i/4 % iwidth % cblack[5]];
4331 val -= cblack[i & 3];
4332 val *= scale_mul[i & 3];
4333 ((ushort *)image)[i] = CLIP(val);
4334 }
4335 if ((aber[0] != 1 || aber[2] != 1) && colors == 3) {
4336 if (verbose)
4337 fprintf (stderr,_("Correcting chromatic aberration...\n"));
4338 for (c=0; c < 4; c+=2) {
4339 if (aber[c] == 1) continue;
4340 img = (ushort *) malloc (size * sizeof *img);
4341 merror (img, "scale_colors()");
4342 for (i=0; i < size; i++)
4343 img[i] = image[i][c];
4344 for (row=0; row < iheight; row++) {
4345 ur = fr = (row - iheight*0.5) * aber[c] + iheight*0.5;
4346 if (ur > iheight-2) continue;
4347 fr -= ur;
4348 for (col=0; col < iwidth; col++) {
4349 uc = fc = (col - iwidth*0.5) * aber[c] + iwidth*0.5;
4350 if (uc > iwidth-2) continue;
4351 fc -= uc;
4352 pix = img + ur*iwidth + uc;
4353 image[row*iwidth+col][c] =
4354 (pix[ 0]*(1-fc) + pix[ 1]*fc) * (1-fr) +
4355 (pix[iwidth]*(1-fc) + pix[iwidth+1]*fc) * fr;
4356 }
4357 }
4358 free(img);
4359 }
4360 }
4361 }
4362
pre_interpolate()4363 void CLASS pre_interpolate()
4364 {
4365 ushort (*img)[4];
4366 int row, col, c;
4367
4368 if (shrink) {
4369 if (half_size) {
4370 height = iheight;
4371 width = iwidth;
4372 if (filters == 9) {
4373 for (row=0; row < 3; row++)
4374 for (col=1; col < 4; col++)
4375 if (!(image[row*width+col][0] | image[row*width+col][2]))
4376 goto break2; break2:
4377 for ( ; row < height; row+=3)
4378 for (col=(col-1)%3+1; col < width-1; col+=3) {
4379 img = image + row*width+col;
4380 for (c=0; c < 3; c+=2)
4381 img[0][c] = (img[-1][c] + img[1][c]) >> 1;
4382 }
4383 }
4384 } else {
4385 img = (ushort (*)[4]) calloc (height, width*sizeof *img);
4386 merror (img, "pre_interpolate()");
4387 for (row=0; row < height; row++)
4388 for (col=0; col < width; col++) {
4389 c = fcol(row,col);
4390 img[row*width+col][c] = image[(row >> 1)*iwidth+(col >> 1)][c];
4391 }
4392 free (image);
4393 image = img;
4394 shrink = 0;
4395 }
4396 }
4397 if (filters > 1000 && colors == 3) {
4398 mix_green = four_color_rgb ^ half_size;
4399 if (four_color_rgb | half_size) colors++;
4400 else {
4401 for (row = FC(1,0) >> 1; row < height; row+=2)
4402 for (col = FC(row,1) & 1; col < width; col+=2)
4403 image[row*width+col][1] = image[row*width+col][3];
4404 filters &= ~((filters & 0x55555555) << 1);
4405 }
4406 }
4407 if (half_size) filters = 0;
4408 }
4409
border_interpolate(int border)4410 void CLASS border_interpolate (int border)
4411 {
4412 unsigned row, col, y, x, f, c, sum[8];
4413
4414 for (row=0; row < height; row++)
4415 for (col=0; col < width; col++) {
4416 if (col==border && row >= border && row < height-border)
4417 col = width-border;
4418 memset (sum, 0, sizeof sum);
4419 for (y=row-1; y != row+2; y++)
4420 for (x=col-1; x != col+2; x++)
4421 if (y < height && x < width) {
4422 f = fcol(y,x);
4423 sum[f] += image[y*width+x][f];
4424 sum[f+4]++;
4425 }
4426 f = fcol(row,col);
4427 FORCC if (c != f && sum[c+4])
4428 image[row*width+col][c] = sum[c] / sum[c+4];
4429 }
4430 }
4431
lin_interpolate()4432 void CLASS lin_interpolate()
4433 {
4434 int code[16][16][32], size=16, *ip, sum[4];
4435 int f, c, i, x, y, row, col, shift, color;
4436 ushort *pix;
4437
4438 if (verbose) fprintf (stderr,_("Bilinear interpolation...\n"));
4439 if (filters == 9) size = 6;
4440 border_interpolate(1);
4441 for (row=0; row < size; row++)
4442 for (col=0; col < size; col++) {
4443 ip = code[row][col]+1;
4444 f = fcol(row,col);
4445 memset (sum, 0, sizeof sum);
4446 for (y=-1; y <= 1; y++)
4447 for (x=-1; x <= 1; x++) {
4448 shift = (y==0) + (x==0);
4449 color = fcol(row+y,col+x);
4450 if (color == f) continue;
4451 *ip++ = (width*y + x)*4 + color;
4452 *ip++ = shift;
4453 *ip++ = color;
4454 sum[color] += 1 << shift;
4455 }
4456 code[row][col][0] = (ip - code[row][col]) / 3;
4457 FORCC
4458 if (c != f) {
4459 *ip++ = c;
4460 *ip++ = 256 / sum[c];
4461 }
4462 }
4463 for (row=1; row < height-1; row++)
4464 for (col=1; col < width-1; col++) {
4465 pix = image[row*width+col];
4466 ip = code[row % size][col % size];
4467 memset (sum, 0, sizeof sum);
4468 for (i=*ip++; i--; ip+=3)
4469 sum[ip[2]] += pix[ip[0]] << ip[1];
4470 for (i=colors; --i; ip+=2)
4471 pix[ip[0]] = sum[ip[0]] * ip[1] >> 8;
4472 }
4473 }
4474
4475 /*
4476 This algorithm is officially called:
4477
4478 "Interpolation using a Threshold-based variable number of gradients"
4479
4480 described in http://scien.stanford.edu/pages/labsite/1999/psych221/projects/99/tingchen/algodep/vargra.html
4481
4482 I've extended the basic idea to work with non-Bayer filter arrays.
4483 Gradients are numbered clockwise from NW=0 to W=7.
4484 */
vng_interpolate()4485 void CLASS vng_interpolate()
4486 {
4487 static const signed char *cp, terms[] = {
4488 -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
4489 -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
4490 -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
4491 -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
4492 -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
4493 -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
4494 -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
4495 -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
4496 -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
4497 -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
4498 -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
4499 -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
4500 -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
4501 +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
4502 +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
4503 +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
4504 +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
4505 +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
4506 +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
4507 +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
4508 +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
4509 +1,+0,+2,+1,0,0x10
4510 }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
4511 ushort (*brow[5])[4], *pix;
4512 int prow=8, pcol=2, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
4513 int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
4514 int g, diff, thold, num, c;
4515
4516 lin_interpolate();
4517 if (verbose) fprintf (stderr,_("VNG interpolation...\n"));
4518
4519 if (filters == 1) prow = pcol = 16;
4520 if (filters == 9) prow = pcol = 6;
4521 ip = (int *) calloc (prow*pcol, 1280);
4522 merror (ip, "vng_interpolate()");
4523 for (row=0; row < prow; row++) /* Precalculate for VNG */
4524 for (col=0; col < pcol; col++) {
4525 code[row][col] = ip;
4526 for (cp=terms, t=0; t < 64; t++) {
4527 y1 = *cp++; x1 = *cp++;
4528 y2 = *cp++; x2 = *cp++;
4529 weight = *cp++;
4530 grads = *cp++;
4531 color = fcol(row+y1,col+x1);
4532 if (fcol(row+y2,col+x2) != color) continue;
4533 diag = (fcol(row,col+1) == color && fcol(row+1,col) == color) ? 2:1;
4534 if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
4535 *ip++ = (y1*width + x1)*4 + color;
4536 *ip++ = (y2*width + x2)*4 + color;
4537 *ip++ = weight;
4538 for (g=0; g < 8; g++)
4539 if (grads & 1<<g) *ip++ = g;
4540 *ip++ = -1;
4541 }
4542 *ip++ = INT_MAX;
4543 for (cp=chood, g=0; g < 8; g++) {
4544 y = *cp++; x = *cp++;
4545 *ip++ = (y*width + x) * 4;
4546 color = fcol(row,col);
4547 if (fcol(row+y,col+x) != color && fcol(row+y*2,col+x*2) == color)
4548 *ip++ = (y*width + x) * 8 + color;
4549 else
4550 *ip++ = 0;
4551 }
4552 }
4553 brow[4] = (ushort (*)[4]) calloc (width*3, sizeof **brow);
4554 merror (brow[4], "vng_interpolate()");
4555 for (row=0; row < 3; row++)
4556 brow[row] = brow[4] + row*width;
4557 for (row=2; row < height-2; row++) { /* Do VNG interpolation */
4558 for (col=2; col < width-2; col++) {
4559 pix = image[row*width+col];
4560 ip = code[row % prow][col % pcol];
4561 memset (gval, 0, sizeof gval);
4562 while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
4563 diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
4564 gval[ip[3]] += diff;
4565 ip += 5;
4566 if ((g = ip[-1]) == -1) continue;
4567 gval[g] += diff;
4568 while ((g = *ip++) != -1)
4569 gval[g] += diff;
4570 }
4571 ip++;
4572 gmin = gmax = gval[0]; /* Choose a threshold */
4573 for (g=1; g < 8; g++) {
4574 if (gmin > gval[g]) gmin = gval[g];
4575 if (gmax < gval[g]) gmax = gval[g];
4576 }
4577 if (gmax == 0) {
4578 memcpy (brow[2][col], pix, sizeof *image);
4579 continue;
4580 }
4581 thold = gmin + (gmax >> 1);
4582 memset (sum, 0, sizeof sum);
4583 color = fcol(row,col);
4584 for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
4585 if (gval[g] <= thold) {
4586 FORCC
4587 if (c == color && ip[1])
4588 sum[c] += (pix[c] + pix[ip[1]]) >> 1;
4589 else
4590 sum[c] += pix[ip[0] + c];
4591 num++;
4592 }
4593 }
4594 FORCC { /* Save to buffer */
4595 t = pix[color];
4596 if (c != color)
4597 t += (sum[c] - sum[color]) / num;
4598 brow[2][col][c] = CLIP(t);
4599 }
4600 }
4601 if (row > 3) /* Write buffer to image */
4602 memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
4603 for (g=0; g < 4; g++)
4604 brow[(g-1) & 3] = brow[g];
4605 }
4606 memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
4607 memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image);
4608 free (brow[4]);
4609 free (code[0][0]);
4610 }
4611
4612 /*
4613 Patterned Pixel Grouping Interpolation by Alain Desbiolles
4614 */
ppg_interpolate()4615 void CLASS ppg_interpolate()
4616 {
4617 int dir[5] = { 1, width, -1, -width, 1 };
4618 int row, col, diff[2], guess[2], c, d, i;
4619 ushort (*pix)[4];
4620
4621 border_interpolate(3);
4622 if (verbose) fprintf (stderr,_("PPG interpolation...\n"));
4623
4624 /* Fill in the green layer with gradients and pattern recognition: */
4625 for (row=3; row < height-3; row++)
4626 for (col=3+(FC(row,3) & 1), c=FC(row,col); col < width-3; col+=2) {
4627 pix = image + row*width+col;
4628 for (i=0; (d=dir[i]) > 0; i++) {
4629 guess[i] = (pix[-d][1] + pix[0][c] + pix[d][1]) * 2
4630 - pix[-2*d][c] - pix[2*d][c];
4631 diff[i] = ( ABS(pix[-2*d][c] - pix[ 0][c]) +
4632 ABS(pix[ 2*d][c] - pix[ 0][c]) +
4633 ABS(pix[ -d][1] - pix[ d][1]) ) * 3 +
4634 ( ABS(pix[ 3*d][1] - pix[ d][1]) +
4635 ABS(pix[-3*d][1] - pix[-d][1]) ) * 2;
4636 }
4637 d = dir[i = diff[0] > diff[1]];
4638 pix[0][1] = ULIM(guess[i] >> 2, pix[d][1], pix[-d][1]);
4639 }
4640 /* Calculate red and blue for each green pixel: */
4641 for (row=1; row < height-1; row++)
4642 for (col=1+(FC(row,2) & 1), c=FC(row,col+1); col < width-1; col+=2) {
4643 pix = image + row*width+col;
4644 for (i=0; (d=dir[i]) > 0; c=2-c, i++)
4645 pix[0][c] = CLIP((pix[-d][c] + pix[d][c] + 2*pix[0][1]
4646 - pix[-d][1] - pix[d][1]) >> 1);
4647 }
4648 /* Calculate blue for red pixels and vice versa: */
4649 for (row=1; row < height-1; row++)
4650 for (col=1+(FC(row,1) & 1), c=2-FC(row,col); col < width-1; col+=2) {
4651 pix = image + row*width+col;
4652 for (i=0; (d=dir[i]+dir[i+1]) > 0; i++) {
4653 diff[i] = ABS(pix[-d][c] - pix[d][c]) +
4654 ABS(pix[-d][1] - pix[0][1]) +
4655 ABS(pix[ d][1] - pix[0][1]);
4656 guess[i] = pix[-d][c] + pix[d][c] + 2*pix[0][1]
4657 - pix[-d][1] - pix[d][1];
4658 }
4659 if (diff[0] != diff[1])
4660 pix[0][c] = CLIP(guess[diff[0] > diff[1]] >> 1);
4661 else
4662 pix[0][c] = CLIP((guess[0]+guess[1]) >> 2);
4663 }
4664 }
4665
cielab(ushort rgb[3],short lab[3])4666 void CLASS cielab (ushort rgb[3], short lab[3])
4667 {
4668 int c, i, j, k;
4669 float r, xyz[3];
4670 static float cbrt[0x10000], xyz_cam[3][4];
4671
4672 if (!rgb) {
4673 for (i=0; i < 0x10000; i++) {
4674 r = i / 65535.0;
4675 cbrt[i] = r > 0.008856 ? pow(r,1/3.0) : 7.787*r + 16/116.0;
4676 }
4677 for (i=0; i < 3; i++)
4678 for (j=0; j < colors; j++)
4679 for (xyz_cam[i][j] = k=0; k < 3; k++)
4680 xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
4681 return;
4682 }
4683 xyz[0] = xyz[1] = xyz[2] = 0.5;
4684 FORCC {
4685 xyz[0] += xyz_cam[0][c] * rgb[c];
4686 xyz[1] += xyz_cam[1][c] * rgb[c];
4687 xyz[2] += xyz_cam[2][c] * rgb[c];
4688 }
4689 xyz[0] = cbrt[CLIP((int) xyz[0])];
4690 xyz[1] = cbrt[CLIP((int) xyz[1])];
4691 xyz[2] = cbrt[CLIP((int) xyz[2])];
4692 lab[0] = 64 * (116 * xyz[1] - 16);
4693 lab[1] = 64 * 500 * (xyz[0] - xyz[1]);
4694 lab[2] = 64 * 200 * (xyz[1] - xyz[2]);
4695 }
4696
4697 #define TS 512 /* Tile Size */
4698 #define fcol(row,col) xtrans[(row+6) % 6][(col+6) % 6]
4699
4700 /*
4701 Frank Markesteijn's algorithm for Fuji X-Trans sensors
4702 */
xtrans_interpolate(int passes)4703 void CLASS xtrans_interpolate (int passes)
4704 {
4705 int c, d, f, g, h, i, v, ng, row, col, top, left, mrow, mcol;
4706 int val, ndir, pass, hm[8], avg[4], color[3][8];
4707 static const short orth[12] = { 1,0,0,1,-1,0,0,-1,1,0,0,1 },
4708 patt[2][16] = { { 0,1,0,-1,2,0,-1,0,1,1,1,-1,0,0,0,0 },
4709 { 0,1,0,-2,1,0,-2,0,1,1,-2,-2,1,-1,-1,1 } },
4710 dir[4] = { 1,TS,TS+1,TS-1 };
4711 short allhex[3][3][2][8], *hex;
4712 ushort min, max, sgrow, sgcol;
4713 ushort (*rgb)[TS][TS][3], (*rix)[3], (*pix)[4];
4714 short (*lab) [TS][3], (*lix)[3];
4715 float (*drv)[TS][TS], diff[6], tr;
4716 char (*homo)[TS][TS], *buffer;
4717
4718 if (verbose)
4719 fprintf (stderr,_("%d-pass X-Trans interpolation...\n"), passes);
4720
4721 cielab (0,0);
4722 ndir = 4 << (passes > 1);
4723 buffer = (char *) malloc (TS*TS*(ndir*11+6));
4724 merror (buffer, "xtrans_interpolate()");
4725 rgb = (ushort(*)[TS][TS][3]) buffer;
4726 lab = (short (*) [TS][3])(buffer + TS*TS*(ndir*6));
4727 drv = (float (*)[TS][TS]) (buffer + TS*TS*(ndir*6+6));
4728 homo = (char (*)[TS][TS]) (buffer + TS*TS*(ndir*10+6));
4729
4730 /* Map a green hexagon around each non-green pixel and vice versa: */
4731 for (row=0; row < 3; row++)
4732 for (col=0; col < 3; col++)
4733 for (ng=d=0; d < 10; d+=2) {
4734 g = fcol(row,col) == 1;
4735 if (fcol(row+orth[d],col+orth[d+2]) == 1) ng=0; else ng++;
4736 if (ng == 4) { sgrow = row; sgcol = col; }
4737 if (ng == g+1) FORC(8) {
4738 v = orth[d ]*patt[g][c*2] + orth[d+1]*patt[g][c*2+1];
4739 h = orth[d+2]*patt[g][c*2] + orth[d+3]*patt[g][c*2+1];
4740 allhex[row][col][0][c^(g*2 & d)] = h + v*width;
4741 allhex[row][col][1][c^(g*2 & d)] = h + v*TS;
4742 }
4743 }
4744
4745 /* Set green1 and green3 to the minimum and maximum allowed values: */
4746 for (row=2; row < height-2; row++)
4747 for (min=~(max=0), col=2; col < width-2; col++) {
4748 if (fcol(row,col) == 1 && (min=~(max=0))) continue;
4749 pix = image + row*width + col;
4750 hex = allhex[row % 3][col % 3][0];
4751 if (!max) FORC(6) {
4752 val = pix[hex[c]][1];
4753 if (min > val) min = val;
4754 if (max < val) max = val;
4755 }
4756 pix[0][1] = min;
4757 pix[0][3] = max;
4758 switch ((row-sgrow) % 3) {
4759 case 1: if (row < height-3) { row++; col--; } break;
4760 case 2: if ((min=~(max=0)) && (col+=2) < width-3 && row > 2) row--;
4761 }
4762 }
4763
4764 for (top=3; top < height-19; top += TS-16)
4765 for (left=3; left < width-19; left += TS-16) {
4766 mrow = MIN (top+TS, height-3);
4767 mcol = MIN (left+TS, width-3);
4768 for (row=top; row < mrow; row++)
4769 for (col=left; col < mcol; col++)
4770 memcpy (rgb[0][row-top][col-left], image[row*width+col], 6);
4771 FORC3 memcpy (rgb[c+1], rgb[0], sizeof *rgb);
4772
4773 /* Interpolate green horizontally, vertically, and along both diagonals: */
4774 for (row=top; row < mrow; row++)
4775 for (col=left; col < mcol; col++) {
4776 if ((f = fcol(row,col)) == 1) continue;
4777 pix = image + row*width + col;
4778 hex = allhex[row % 3][col % 3][0];
4779 color[1][0] = 174 * (pix[ hex[1]][1] + pix[ hex[0]][1]) -
4780 46 * (pix[2*hex[1]][1] + pix[2*hex[0]][1]);
4781 color[1][1] = 223 * pix[ hex[3]][1] + pix[ hex[2]][1] * 33 +
4782 92 * (pix[ 0 ][f] - pix[ -hex[2]][f]);
4783 FORC(2) color[1][2+c] =
4784 164 * pix[hex[4+c]][1] + 92 * pix[-2*hex[4+c]][1] + 33 *
4785 (2*pix[0][f] - pix[3*hex[4+c]][f] - pix[-3*hex[4+c]][f]);
4786 FORC4 rgb[c^!((row-sgrow) % 3)][row-top][col-left][1] =
4787 LIM(color[1][c] >> 8,pix[0][1],pix[0][3]);
4788 }
4789
4790 for (pass=0; pass < passes; pass++) {
4791 if (pass == 1)
4792 memcpy (rgb+=4, buffer, 4*sizeof *rgb);
4793
4794 /* Recalculate green from interpolated values of closer pixels: */
4795 if (pass) {
4796 for (row=top+2; row < mrow-2; row++)
4797 for (col=left+2; col < mcol-2; col++) {
4798 if ((f = fcol(row,col)) == 1) continue;
4799 pix = image + row*width + col;
4800 hex = allhex[row % 3][col % 3][1];
4801 for (d=3; d < 6; d++) {
4802 rix = &rgb[(d-2)^!((row-sgrow) % 3)][row-top][col-left];
4803 val = rix[-2*hex[d]][1] + 2*rix[hex[d]][1]
4804 - rix[-2*hex[d]][f] - 2*rix[hex[d]][f] + 3*rix[0][f];
4805 rix[0][1] = LIM(val/3,pix[0][1],pix[0][3]);
4806 }
4807 }
4808 }
4809
4810 /* Interpolate red and blue values for solitary green pixels: */
4811 for (row=(top-sgrow+4)/3*3+sgrow; row < mrow-2; row+=3)
4812 for (col=(left-sgcol+4)/3*3+sgcol; col < mcol-2; col+=3) {
4813 rix = &rgb[0][row-top][col-left];
4814 h = fcol(row,col+1);
4815 memset (diff, 0, sizeof diff);
4816 for (i=1, d=0; d < 6; d++, i^=TS^1, h^=2) {
4817 for (c=0; c < 2; c++, h^=2) {
4818 g = 2*rix[0][1] - rix[i<<c][1] - rix[-i<<c][1];
4819 color[h][d] = g + rix[i<<c][h] + rix[-i<<c][h];
4820 if (d > 1)
4821 diff[d] += SQR (rix[i<<c][1] - rix[-i<<c][1]
4822 - rix[i<<c][h] + rix[-i<<c][h]) + SQR(g);
4823 }
4824 if (d > 1 && (d & 1))
4825 if (diff[d-1] < diff[d])
4826 FORC(2) color[c*2][d] = color[c*2][d-1];
4827 if (d < 2 || (d & 1)) {
4828 FORC(2) rix[0][c*2] = CLIP(color[c*2][d]/2);
4829 rix += TS*TS;
4830 }
4831 }
4832 }
4833
4834 /* Interpolate red for blue pixels and vice versa: */
4835 for (row=top+3; row < mrow-3; row++)
4836 for (col=left+3; col < mcol-3; col++) {
4837 if ((f = 2-fcol(row,col)) == 1) continue;
4838 rix = &rgb[0][row-top][col-left];
4839 c = (row-sgrow) % 3 ? TS:1;
4840 h = 3 * (c ^ TS ^ 1);
4841 for (d=0; d < 4; d++, rix += TS*TS) {
4842 i = d > 1 || ((d ^ c) & 1) ||
4843 ((ABS(rix[0][1]-rix[c][1])+ABS(rix[0][1]-rix[-c][1])) <
4844 2*(ABS(rix[0][1]-rix[h][1])+ABS(rix[0][1]-rix[-h][1]))) ? c:h;
4845 rix[0][f] = CLIP((rix[i][f] + rix[-i][f] +
4846 2*rix[0][1] - rix[i][1] - rix[-i][1])/2);
4847 }
4848 }
4849
4850 /* Fill in red and blue for 2x2 blocks of green: */
4851 for (row=top+2; row < mrow-2; row++) if ((row-sgrow) % 3)
4852 for (col=left+2; col < mcol-2; col++) if ((col-sgcol) % 3) {
4853 rix = &rgb[0][row-top][col-left];
4854 hex = allhex[row % 3][col % 3][1];
4855 for (d=0; d < ndir; d+=2, rix += TS*TS)
4856 if (hex[d] + hex[d+1]) {
4857 g = 3*rix[0][1] - 2*rix[hex[d]][1] - rix[hex[d+1]][1];
4858 for (c=0; c < 4; c+=2) rix[0][c] =
4859 CLIP((g + 2*rix[hex[d]][c] + rix[hex[d+1]][c])/3);
4860 } else {
4861 g = 2*rix[0][1] - rix[hex[d]][1] - rix[hex[d+1]][1];
4862 for (c=0; c < 4; c+=2) rix[0][c] =
4863 CLIP((g + rix[hex[d]][c] + rix[hex[d+1]][c])/2);
4864 }
4865 }
4866 }
4867 rgb = (ushort(*)[TS][TS][3]) buffer;
4868 mrow -= top;
4869 mcol -= left;
4870
4871 /* Convert to CIELab and differentiate in all directions: */
4872 for (d=0; d < ndir; d++) {
4873 for (row=2; row < mrow-2; row++)
4874 for (col=2; col < mcol-2; col++)
4875 cielab (rgb[d][row][col], lab[row][col]);
4876 for (f=dir[d & 3],row=3; row < mrow-3; row++)
4877 for (col=3; col < mcol-3; col++) {
4878 lix = &lab[row][col];
4879 g = 2*lix[0][0] - lix[f][0] - lix[-f][0];
4880 drv[d][row][col] = SQR(g)
4881 + SQR((2*lix[0][1] - lix[f][1] - lix[-f][1] + g*500/232))
4882 + SQR((2*lix[0][2] - lix[f][2] - lix[-f][2] - g*500/580));
4883 }
4884 }
4885
4886 /* Build homogeneity maps from the derivatives: */
4887 memset(homo, 0, ndir*TS*TS);
4888 for (row=4; row < mrow-4; row++)
4889 for (col=4; col < mcol-4; col++) {
4890 for (tr=FLT_MAX, d=0; d < ndir; d++)
4891 if (tr > drv[d][row][col])
4892 tr = drv[d][row][col];
4893 tr *= 8;
4894 for (d=0; d < ndir; d++)
4895 for (v=-1; v <= 1; v++)
4896 for (h=-1; h <= 1; h++)
4897 if (drv[d][row+v][col+h] <= tr)
4898 homo[d][row][col]++;
4899 }
4900
4901 /* Average the most homogenous pixels for the final result: */
4902 if (height-top < TS+4) mrow = height-top+2;
4903 if (width-left < TS+4) mcol = width-left+2;
4904 for (row = MIN(top,8); row < mrow-8; row++)
4905 for (col = MIN(left,8); col < mcol-8; col++) {
4906 for (d=0; d < ndir; d++)
4907 for (hm[d]=0, v=-2; v <= 2; v++)
4908 for (h=-2; h <= 2; h++)
4909 hm[d] += homo[d][row+v][col+h];
4910 for (d=0; d < ndir-4; d++)
4911 if (hm[d] < hm[d+4]) hm[d ] = 0; else
4912 if (hm[d] > hm[d+4]) hm[d+4] = 0;
4913 for (max=hm[0],d=1; d < ndir; d++)
4914 if (max < hm[d]) max = hm[d];
4915 max -= max >> 3;
4916 memset (avg, 0, sizeof avg);
4917 for (d=0; d < ndir; d++)
4918 if (hm[d] >= max) {
4919 FORC3 avg[c] += rgb[d][row][col][c];
4920 avg[3]++;
4921 }
4922 FORC3 image[(row+top)*width+col+left][c] = avg[c]/avg[3];
4923 }
4924 }
4925 free(buffer);
4926 border_interpolate(8);
4927 }
4928 #undef fcol
4929
4930 /*
4931 Adaptive Homogeneity-Directed interpolation is based on
4932 the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
4933 */
ahd_interpolate()4934 void CLASS ahd_interpolate()
4935 {
4936 int i, j, top, left, row, col, tr, tc, c, d, val, hm[2];
4937 static const int dir[4] = { -1, 1, -TS, TS };
4938 unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
4939 ushort (*rgb)[TS][TS][3], (*rix)[3], (*pix)[4];
4940 short (*lab)[TS][TS][3], (*lix)[3];
4941 char (*homo)[TS][TS], *buffer;
4942
4943 if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
4944
4945 cielab (0,0);
4946 border_interpolate(5);
4947 buffer = (char *) malloc (26*TS*TS);
4948 merror (buffer, "ahd_interpolate()");
4949 rgb = (ushort(*)[TS][TS][3]) buffer;
4950 lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
4951 homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
4952
4953 for (top=2; top < height-5; top += TS-6)
4954 for (left=2; left < width-5; left += TS-6) {
4955
4956 /* Interpolate green horizontally and vertically: */
4957 for (row=top; row < top+TS && row < height-2; row++) {
4958 col = left + (FC(row,left) & 1);
4959 for (c = FC(row,col); col < left+TS && col < width-2; col+=2) {
4960 pix = image + row*width+col;
4961 val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
4962 - pix[-2][c] - pix[2][c]) >> 2;
4963 rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
4964 val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
4965 - pix[-2*width][c] - pix[2*width][c]) >> 2;
4966 rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
4967 }
4968 }
4969 /* Interpolate red and blue, and convert to CIELab: */
4970 for (d=0; d < 2; d++)
4971 for (row=top+1; row < top+TS-1 && row < height-3; row++)
4972 for (col=left+1; col < left+TS-1 && col < width-3; col++) {
4973 pix = image + row*width+col;
4974 rix = &rgb[d][row-top][col-left];
4975 lix = &lab[d][row-top][col-left];
4976 if ((c = 2 - FC(row,col)) == 1) {
4977 c = FC(row+1,col);
4978 val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
4979 - rix[-1][1] - rix[1][1] ) >> 1);
4980 rix[0][2-c] = CLIP(val);
4981 val = pix[0][1] + (( pix[-width][c] + pix[width][c]
4982 - rix[-TS][1] - rix[TS][1] ) >> 1);
4983 } else
4984 val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
4985 + pix[+width-1][c] + pix[+width+1][c]
4986 - rix[-TS-1][1] - rix[-TS+1][1]
4987 - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
4988 rix[0][c] = CLIP(val);
4989 c = FC(row,col);
4990 rix[0][c] = pix[0][c];
4991 cielab (rix[0],lix[0]);
4992 }
4993 /* Build homogeneity maps from the CIELab images: */
4994 memset (homo, 0, 2*TS*TS);
4995 for (row=top+2; row < top+TS-2 && row < height-4; row++) {
4996 tr = row-top;
4997 for (col=left+2; col < left+TS-2 && col < width-4; col++) {
4998 tc = col-left;
4999 for (d=0; d < 2; d++) {
5000 lix = &lab[d][tr][tc];
5001 for (i=0; i < 4; i++) {
5002 ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
5003 abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
5004 + SQR(lix[0][2]-lix[dir[i]][2]);
5005 }
5006 }
5007 leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
5008 MAX(ldiff[1][2],ldiff[1][3]));
5009 abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
5010 MAX(abdiff[1][2],abdiff[1][3]));
5011 for (d=0; d < 2; d++)
5012 for (i=0; i < 4; i++)
5013 if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
5014 homo[d][tr][tc]++;
5015 }
5016 }
5017 /* Combine the most homogenous pixels for the final result: */
5018 for (row=top+3; row < top+TS-3 && row < height-5; row++) {
5019 tr = row-top;
5020 for (col=left+3; col < left+TS-3 && col < width-5; col++) {
5021 tc = col-left;
5022 for (d=0; d < 2; d++)
5023 for (hm[d]=0, i=tr-1; i <= tr+1; i++)
5024 for (j=tc-1; j <= tc+1; j++)
5025 hm[d] += homo[d][i][j];
5026 if (hm[0] != hm[1])
5027 FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
5028 else
5029 FORC3 image[row*width+col][c] =
5030 (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
5031 }
5032 }
5033 }
5034 free (buffer);
5035 }
5036 #undef TS
5037
median_filter()5038 void CLASS median_filter()
5039 {
5040 ushort (*pix)[4];
5041 int pass, c, i, j, k, med[9];
5042 static const uchar opt[] = /* Optimal 9-element median search */
5043 { 1,2, 4,5, 7,8, 0,1, 3,4, 6,7, 1,2, 4,5, 7,8,
5044 0,3, 5,8, 4,7, 3,6, 1,4, 2,5, 4,7, 4,2, 6,4, 4,2 };
5045
5046 for (pass=1; pass <= med_passes; pass++) {
5047 if (verbose)
5048 fprintf (stderr,_("Median filter pass %d...\n"), pass);
5049 for (c=0; c < 3; c+=2) {
5050 for (pix = image; pix < image+width*height; pix++)
5051 pix[0][3] = pix[0][c];
5052 for (pix = image+width; pix < image+width*(height-1); pix++) {
5053 if ((pix-image+1) % width < 2) continue;
5054 for (k=0, i = -width; i <= width; i += width)
5055 for (j = i-1; j <= i+1; j++)
5056 med[k++] = pix[j][3] - pix[j][1];
5057 for (i=0; i < sizeof opt; i+=2)
5058 if (med[opt[i]] > med[opt[i+1]])
5059 SWAP (med[opt[i]] , med[opt[i+1]]);
5060 pix[0][c] = CLIP(med[4] + pix[0][1]);
5061 }
5062 }
5063 }
5064 }
5065
blend_highlights()5066 void CLASS blend_highlights()
5067 {
5068 int clip=INT_MAX, row, col, c, i, j;
5069 static const float trans[2][4][4] =
5070 { { { 1,1,1 }, { 1.7320508,-1.7320508,0 }, { -1,-1,2 } },
5071 { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
5072 static const float itrans[2][4][4] =
5073 { { { 1,0.8660254,-0.5 }, { 1,-0.8660254,-0.5 }, { 1,0,1 } },
5074 { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
5075 float cam[2][4], lab[2][4], sum[2], chratio;
5076
5077 if ((unsigned) (colors-3) > 1) return;
5078 if (verbose) fprintf (stderr,_("Blending highlights...\n"));
5079 FORCC if (clip > (i = 65535*pre_mul[c])) clip = i;
5080 for (row=0; row < height; row++)
5081 for (col=0; col < width; col++) {
5082 FORCC if (image[row*width+col][c] > clip) break;
5083 if (c == colors) continue;
5084 FORCC {
5085 cam[0][c] = image[row*width+col][c];
5086 cam[1][c] = MIN(cam[0][c],clip);
5087 }
5088 for (i=0; i < 2; i++) {
5089 FORCC for (lab[i][c]=j=0; j < colors; j++)
5090 lab[i][c] += trans[colors-3][c][j] * cam[i][j];
5091 for (sum[i]=0,c=1; c < colors; c++)
5092 sum[i] += SQR(lab[i][c]);
5093 }
5094 chratio = sqrt(sum[1]/sum[0]);
5095 for (c=1; c < colors; c++)
5096 lab[0][c] *= chratio;
5097 FORCC for (cam[0][c]=j=0; j < colors; j++)
5098 cam[0][c] += itrans[colors-3][c][j] * lab[0][j];
5099 FORCC image[row*width+col][c] = cam[0][c] / colors;
5100 }
5101 }
5102
5103 #define SCALE (4 >> shrink)
recover_highlights()5104 void CLASS recover_highlights()
5105 {
5106 float *map, sum, wgt, grow;
5107 int hsat[4], count, spread, change, val, i;
5108 unsigned high, wide, mrow, mcol, row, col, kc, c, d, y, x;
5109 ushort *pixel;
5110 static const signed char dir[8][2] =
5111 { {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
5112
5113 if (verbose) fprintf (stderr,_("Rebuilding highlights...\n"));
5114
5115 grow = pow (2, 4-highlight);
5116 FORCC hsat[c] = 32000 * pre_mul[c];
5117 for (kc=0, c=1; c < colors; c++)
5118 if (pre_mul[kc] < pre_mul[c]) kc = c;
5119 high = height / SCALE;
5120 wide = width / SCALE;
5121 map = (float *) calloc (high, wide*sizeof *map);
5122 merror (map, "recover_highlights()");
5123 FORCC if (c != kc) {
5124 memset (map, 0, high*wide*sizeof *map);
5125 for (mrow=0; mrow < high; mrow++)
5126 for (mcol=0; mcol < wide; mcol++) {
5127 sum = wgt = count = 0;
5128 for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
5129 for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
5130 pixel = image[row*width+col];
5131 if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000) {
5132 sum += pixel[c];
5133 wgt += pixel[kc];
5134 count++;
5135 }
5136 }
5137 if (count == SCALE*SCALE)
5138 map[mrow*wide+mcol] = sum / wgt;
5139 }
5140 for (spread = 32/grow; spread--; ) {
5141 for (mrow=0; mrow < high; mrow++)
5142 for (mcol=0; mcol < wide; mcol++) {
5143 if (map[mrow*wide+mcol]) continue;
5144 sum = count = 0;
5145 for (d=0; d < 8; d++) {
5146 y = mrow + dir[d][0];
5147 x = mcol + dir[d][1];
5148 if (y < high && x < wide && map[y*wide+x] > 0) {
5149 sum += (1 + (d & 1)) * map[y*wide+x];
5150 count += 1 + (d & 1);
5151 }
5152 }
5153 if (count > 3)
5154 map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
5155 }
5156 for (change=i=0; i < high*wide; i++)
5157 if (map[i] < 0) {
5158 map[i] = -map[i];
5159 change = 1;
5160 }
5161 if (!change) break;
5162 }
5163 for (i=0; i < high*wide; i++)
5164 if (map[i] == 0) map[i] = 1;
5165 for (mrow=0; mrow < high; mrow++)
5166 for (mcol=0; mcol < wide; mcol++) {
5167 for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
5168 for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
5169 pixel = image[row*width+col];
5170 if (pixel[c] / hsat[c] > 1) {
5171 val = pixel[kc] * map[mrow*wide+mcol];
5172 if (pixel[c] < val) pixel[c] = CLIP(val);
5173 }
5174 }
5175 }
5176 }
5177 free (map);
5178 }
5179 #undef SCALE
5180
tiff_get(unsigned base,unsigned * tag,unsigned * type,unsigned * len,unsigned * save)5181 void CLASS tiff_get (unsigned base,
5182 unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
5183 {
5184 *tag = get2();
5185 *type = get2();
5186 *len = get4();
5187 *save = ftell(ifp) + 4;
5188 if (*len * ("11124811248484"[*type < 14 ? *type:0]-'0') > 4)
5189 fseek (ifp, get4()+base, SEEK_SET);
5190 }
5191
parse_thumb_note(int base,unsigned toff,unsigned tlen)5192 void CLASS parse_thumb_note (int base, unsigned toff, unsigned tlen)
5193 {
5194 unsigned entries, tag, type, len, save;
5195
5196 entries = get2();
5197 while (entries--) {
5198 tiff_get (base, &tag, &type, &len, &save);
5199 if (tag == toff) thumb_offset = get4()+base;
5200 if (tag == tlen) thumb_length = get4();
5201 fseek (ifp, save, SEEK_SET);
5202 }
5203 }
5204
5205 int CLASS parse_tiff_ifd (int base);
5206
parse_makernote(int base,int uptag)5207 void CLASS parse_makernote (int base, int uptag)
5208 {
5209 static const uchar xlat[2][256] = {
5210 { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
5211 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
5212 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
5213 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
5214 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
5215 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
5216 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
5217 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
5218 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
5219 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
5220 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
5221 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
5222 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
5223 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
5224 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
5225 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
5226 { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
5227 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
5228 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
5229 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
5230 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
5231 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
5232 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
5233 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
5234 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
5235 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
5236 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
5237 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
5238 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
5239 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
5240 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
5241 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
5242 unsigned offset=0, entries, tag, type, len, save, c;
5243 unsigned ver97=0, serial=0, i, wbi=0, wb[4]={0,0,0,0};
5244 uchar buf97[324], ci, cj, ck;
5245 short morder, sorder=order;
5246 char buf[10];
5247 /*
5248 The MakerNote might have its own TIFF header (possibly with
5249 its own byte-order!), or it might just be a table.
5250 */
5251 if (!strcmp(make,"Nokia")) return;
5252 fread (buf, 1, 10, ifp);
5253 if (!strncmp (buf,"KDK" ,3) || /* these aren't TIFF tables */
5254 !strncmp (buf,"VER" ,3) ||
5255 !strncmp (buf,"IIII",4) ||
5256 !strncmp (buf,"MMMM",4)) return;
5257 if (!strncmp (buf,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
5258 !strncmp (buf,"MLY" ,3)) { /* Minolta DiMAGE G series */
5259 order = 0x4d4d;
5260 while ((i=ftell(ifp)) < data_offset && i < 16384) {
5261 wb[0] = wb[2]; wb[2] = wb[1]; wb[1] = wb[3];
5262 wb[3] = get2();
5263 if (wb[1] == 256 && wb[3] == 256 &&
5264 wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
5265 FORC4 cam_mul[c] = wb[c];
5266 }
5267 goto quit;
5268 }
5269 if (!strcmp (buf,"Nikon")) {
5270 base = ftell(ifp);
5271 order = get2();
5272 if (get2() != 42) goto quit;
5273 offset = get4();
5274 fseek (ifp, offset-8, SEEK_CUR);
5275 } else if (!strcmp (buf,"OLYMPUS") ||
5276 !strcmp (buf,"PENTAX ")) {
5277 base = ftell(ifp)-10;
5278 fseek (ifp, -2, SEEK_CUR);
5279 order = get2();
5280 if (buf[0] == 'O') get2();
5281 } else if (!strncmp (buf,"SONY",4) ||
5282 !strcmp (buf,"Panasonic")) {
5283 goto nf;
5284 } else if (!strncmp (buf,"FUJIFILM",8)) {
5285 base = ftell(ifp)-10;
5286 nf: order = 0x4949;
5287 fseek (ifp, 2, SEEK_CUR);
5288 } else if (!strcmp (buf,"OLYMP") ||
5289 !strcmp (buf,"LEICA") ||
5290 !strcmp (buf,"Ricoh") ||
5291 !strcmp (buf,"EPSON"))
5292 fseek (ifp, -2, SEEK_CUR);
5293 else if (!strcmp (buf,"AOC") ||
5294 !strcmp (buf,"QVC"))
5295 fseek (ifp, -4, SEEK_CUR);
5296 else {
5297 fseek (ifp, -10, SEEK_CUR);
5298 if (!strncmp(make,"SAMSUNG",7))
5299 base = ftell(ifp);
5300 }
5301 entries = get2();
5302 if (entries > 1000) return;
5303 morder = order;
5304 while (entries--) {
5305 order = morder;
5306 tiff_get (base, &tag, &type, &len, &save);
5307 tag |= uptag << 16;
5308 if (tag == 2 && strstr(make,"NIKON") && !iso_speed)
5309 iso_speed = (get2(),get2());
5310 if (tag == 4 && len > 26 && len < 35) {
5311 if ((i=(get4(),get2())) != 0x7fff && !iso_speed)
5312 iso_speed = 50 * pow (2, i/32.0 - 4);
5313 if ((i=(get2(),get2())) != 0x7fff && !aperture)
5314 aperture = pow (2, i/64.0);
5315 if ((i=get2()) != 0xffff && !shutter)
5316 shutter = pow (2, (short) i/-32.0);
5317 wbi = (get2(),get2());
5318 shot_order = (get2(),get2());
5319 }
5320 if ((tag == 4 || tag == 0x114) && !strncmp(make,"KONICA",6)) {
5321 fseek (ifp, tag == 4 ? 140:160, SEEK_CUR);
5322 switch (get2()) {
5323 case 72: flip = 0; break;
5324 case 76: flip = 6; break;
5325 case 82: flip = 5; break;
5326 }
5327 }
5328 if (tag == 7 && type == 2 && len > 20)
5329 fgets (model2, 64, ifp);
5330 if (tag == 8 && type == 4)
5331 shot_order = get4();
5332 if (tag == 9 && !strcmp(make,"Canon"))
5333 fread (artist, 64, 1, ifp);
5334 if (tag == 0xc && len == 4)
5335 FORC3 cam_mul[(c << 1 | c >> 1) & 3] = getreal(type);
5336 if (tag == 0xd && type == 7 && get2() == 0xaaaa) {
5337 for (c=i=2; (ushort) c != 0xbbbb && i < len; i++)
5338 c = c << 8 | fgetc(ifp);
5339 while ((i+=4) < len-5)
5340 if (get4() == 257 && (i=len) && (c = (get4(),fgetc(ifp))) < 3)
5341 flip = "065"[c]-'0';
5342 }
5343 if (tag == 0x10 && type == 4)
5344 unique_id = get4();
5345 if (tag == 0x11 && is_raw && !strncmp(make,"NIKON",5)) {
5346 fseek (ifp, get4()+base, SEEK_SET);
5347 parse_tiff_ifd (base);
5348 }
5349 if (tag == 0x14 && type == 7) {
5350 if (len == 2560) {
5351 fseek (ifp, 1248, SEEK_CUR);
5352 goto get2_256;
5353 }
5354 fread (buf, 1, 10, ifp);
5355 if (!strncmp(buf,"NRW ",4)) {
5356 fseek (ifp, strcmp(buf+4,"0100") ? 46:1546, SEEK_CUR);
5357 cam_mul[0] = get4() << 2;
5358 cam_mul[1] = get4() + get4();
5359 cam_mul[2] = get4() << 2;
5360 }
5361 }
5362 if (tag == 0x15 && type == 2 && is_raw)
5363 fread (model, 64, 1, ifp);
5364 if (strstr(make,"PENTAX")) {
5365 if (tag == 0x1b) tag = 0x1018;
5366 if (tag == 0x1c) tag = 0x1017;
5367 }
5368 if (tag == 0x1d)
5369 while ((c = fgetc(ifp)) && c != EOF)
5370 serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
5371 if (tag == 0x29 && type == 1) {
5372 c = wbi < 18 ? "012347800000005896"[wbi]-'0' : 0;
5373 fseek (ifp, 8 + c*32, SEEK_CUR);
5374 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get4();
5375 }
5376 if (tag == 0x3d && type == 3 && len == 4)
5377 FORC4 cblack[c ^ c >> 1] = get2() >> (14-tiff_ifd[2].bps);
5378 if (tag == 0x81 && type == 4) {
5379 data_offset = get4();
5380 fseek (ifp, data_offset + 41, SEEK_SET);
5381 raw_height = get2() * 2;
5382 raw_width = get2();
5383 filters = 0x61616161;
5384 }
5385 if ((tag == 0x81 && type == 7) ||
5386 (tag == 0x100 && type == 7) ||
5387 (tag == 0x280 && type == 1)) {
5388 thumb_offset = ftell(ifp);
5389 thumb_length = len;
5390 }
5391 if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
5392 thumb_offset += base;
5393 if (tag == 0x89 && type == 4)
5394 thumb_length = get4();
5395 if (tag == 0x8c || tag == 0x96)
5396 meta_offset = ftell(ifp);
5397 if (tag == 0x97) {
5398 for (i=0; i < 4; i++)
5399 ver97 = ver97 * 10 + fgetc(ifp)-'0';
5400 switch (ver97) {
5401 case 100:
5402 fseek (ifp, 68, SEEK_CUR);
5403 FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
5404 break;
5405 case 102:
5406 fseek (ifp, 6, SEEK_CUR);
5407 FORC4 cam_mul[c ^ (c >> 1)] = get2();
5408 break;
5409 case 103:
5410 fseek (ifp, 16, SEEK_CUR);
5411 FORC4 cam_mul[c] = get2();
5412 }
5413 if (ver97 >= 200) {
5414 if (ver97 != 205) fseek (ifp, 280, SEEK_CUR);
5415 fread (buf97, 324, 1, ifp);
5416 }
5417 }
5418 if (tag == 0xa1 && type == 7) {
5419 order = 0x4949;
5420 fseek (ifp, 140, SEEK_CUR);
5421 FORC3 cam_mul[c] = get4();
5422 }
5423 if (tag == 0xa4 && type == 3) {
5424 fseek (ifp, wbi*48, SEEK_CUR);
5425 FORC3 cam_mul[c] = get2();
5426 }
5427 if (tag == 0xa7 && (unsigned) (ver97-200) < 17) {
5428 ci = xlat[0][serial & 0xff];
5429 cj = xlat[1][fgetc(ifp)^fgetc(ifp)^fgetc(ifp)^fgetc(ifp)];
5430 ck = 0x60;
5431 for (i=0; i < 324; i++)
5432 buf97[i] ^= (cj += ci * ck++);
5433 i = "66666>666;6A;:;55"[ver97-200] - '0';
5434 FORC4 cam_mul[c ^ (c >> 1) ^ (i & 1)] =
5435 sget2 (buf97 + (i & -2) + c*2);
5436 }
5437 if (tag == 0x200 && len == 3)
5438 shot_order = (get4(),get4());
5439 if (tag == 0x200 && len == 4)
5440 FORC4 cblack[c ^ c >> 1] = get2();
5441 if (tag == 0x201 && len == 4)
5442 FORC4 cam_mul[c ^ (c >> 1)] = get2();
5443 if (tag == 0x220 && type == 7)
5444 meta_offset = ftell(ifp);
5445 if (tag == 0x401 && type == 4 && len == 4)
5446 FORC4 cblack[c ^ c >> 1] = get4();
5447 if (tag == 0xe01) { /* Nikon Capture Note */
5448 order = 0x4949;
5449 fseek (ifp, 22, SEEK_CUR);
5450 for (offset=22; offset+22 < len; offset += 22+i) {
5451 tag = get4();
5452 fseek (ifp, 14, SEEK_CUR);
5453 i = get4()-4;
5454 if (tag == 0x76a43207) flip = get2();
5455 else fseek (ifp, i, SEEK_CUR);
5456 }
5457 }
5458 if (tag == 0xe80 && len == 256 && type == 7) {
5459 fseek (ifp, 48, SEEK_CUR);
5460 cam_mul[0] = get2() * 508 * 1.078 / 0x10000;
5461 cam_mul[2] = get2() * 382 * 1.173 / 0x10000;
5462 }
5463 if (tag == 0xf00 && type == 7) {
5464 if (len == 614)
5465 fseek (ifp, 176, SEEK_CUR);
5466 else if (len == 734 || len == 1502)
5467 fseek (ifp, 148, SEEK_CUR);
5468 else goto next;
5469 goto get2_256;
5470 }
5471 if ((tag == 0x1011 && len == 9) || tag == 0x20400200)
5472 for (i=0; i < 3; i++)
5473 FORC3 cmatrix[i][c] = ((short) get2()) / 256.0;
5474 if ((tag == 0x1012 || tag == 0x20400600) && len == 4)
5475 FORC4 cblack[c ^ c >> 1] = get2();
5476 if (tag == 0x1017 || tag == 0x20400100)
5477 cam_mul[0] = get2() / 256.0;
5478 if (tag == 0x1018 || tag == 0x20400100)
5479 cam_mul[2] = get2() / 256.0;
5480 if (tag == 0x2011 && len == 2) {
5481 get2_256:
5482 order = 0x4d4d;
5483 cam_mul[0] = get2() / 256.0;
5484 cam_mul[2] = get2() / 256.0;
5485 }
5486 if ((tag | 0x70) == 0x2070 && (type == 4 || type == 13))
5487 fseek (ifp, get4()+base, SEEK_SET);
5488 if (tag == 0x2020)
5489 parse_thumb_note (base, 257, 258);
5490 if (tag == 0x2040)
5491 parse_makernote (base, 0x2040);
5492 if (tag == 0xb028) {
5493 fseek (ifp, get4()+base, SEEK_SET);
5494 parse_thumb_note (base, 136, 137);
5495 }
5496 if (tag == 0x4001 && len > 500) {
5497 i = len == 582 ? 50 : len == 653 ? 68 : len == 5120 ? 142 : 126;
5498 fseek (ifp, i, SEEK_CUR);
5499 FORC4 cam_mul[c ^ (c >> 1)] = get2();
5500 for (i+=18; i <= len; i+=10) {
5501 get2();
5502 FORC4 sraw_mul[c ^ (c >> 1)] = get2();
5503 if (sraw_mul[1] == 1170) break;
5504 }
5505 }
5506 if (tag == 0x4021 && get4() && get4())
5507 FORC4 cam_mul[c] = 1024;
5508 if (tag == 0xa021)
5509 FORC4 cam_mul[c ^ (c >> 1)] = get4();
5510 if (tag == 0xa028)
5511 FORC4 cam_mul[c ^ (c >> 1)] -= get4();
5512 if (tag == 0xb001)
5513 unique_id = get2();
5514 next:
5515 fseek (ifp, save, SEEK_SET);
5516 }
5517 quit:
5518 order = sorder;
5519 }
5520
5521 /*
5522 Since the TIFF DateTime string has no timezone information,
5523 assume that the camera's clock was set to Universal Time.
5524 */
get_timestamp(int reversed)5525 void CLASS get_timestamp (int reversed)
5526 {
5527 struct tm t;
5528 char str[20];
5529 int i;
5530
5531 str[19] = 0;
5532 if (reversed)
5533 for (i=19; i--; ) str[i] = fgetc(ifp);
5534 else
5535 fread (str, 19, 1, ifp);
5536 memset (&t, 0, sizeof t);
5537 if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
5538 &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
5539 return;
5540 t.tm_year -= 1900;
5541 t.tm_mon -= 1;
5542 t.tm_isdst = -1;
5543 if (mktime(&t) > 0)
5544 timestamp = mktime(&t);
5545 }
5546
parse_exif(int base)5547 void CLASS parse_exif (int base)
5548 {
5549 unsigned kodak, entries, tag, type, len, save, c;
5550 double expo;
5551
5552 kodak = !strncmp(make,"EASTMAN",7) && tiff_nifds < 3;
5553 entries = get2();
5554 while (entries--) {
5555 tiff_get (base, &tag, &type, &len, &save);
5556 switch (tag) {
5557 case 33434: tiff_ifd[tiff_nifds-1].shutter =
5558 shutter = getreal(type); break;
5559 case 33437: aperture = getreal(type); break;
5560 case 34855: iso_speed = get2(); break;
5561 case 36867:
5562 case 36868: get_timestamp(0); break;
5563 case 37377: if ((expo = -getreal(type)) < 128)
5564 tiff_ifd[tiff_nifds-1].shutter =
5565 shutter = pow (2, expo); break;
5566 case 37378: aperture = pow (2, getreal(type)/2); break;
5567 case 37386: focal_len = getreal(type); break;
5568 case 37500: parse_makernote (base, 0); break;
5569 case 40962: if (kodak) raw_width = get4(); break;
5570 case 40963: if (kodak) raw_height = get4(); break;
5571 case 41730:
5572 if (get4() == 0x20002)
5573 for (exif_cfa=c=0; c < 8; c+=2)
5574 exif_cfa |= fgetc(ifp) * 0x01010101 << c;
5575 }
5576 fseek (ifp, save, SEEK_SET);
5577 }
5578 }
5579
parse_gps(int base)5580 void CLASS parse_gps (int base)
5581 {
5582 unsigned entries, tag, type, len, save, c;
5583
5584 entries = get2();
5585 while (entries--) {
5586 tiff_get (base, &tag, &type, &len, &save);
5587 switch (tag) {
5588 case 1: case 3: case 5:
5589 gpsdata[29+tag/2] = getc(ifp); break;
5590 case 2: case 4: case 7:
5591 FORC(6) gpsdata[tag/3*6+c] = get4(); break;
5592 case 6:
5593 FORC(2) gpsdata[18+c] = get4(); break;
5594 case 18: case 29:
5595 fgets ((char *) (gpsdata+14+tag/3), MIN(len,12), ifp);
5596 }
5597 fseek (ifp, save, SEEK_SET);
5598 }
5599 }
5600
romm_coeff(float romm_cam[3][3])5601 void CLASS romm_coeff (float romm_cam[3][3])
5602 {
5603 static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
5604 { { 2.034193, -0.727420, -0.306766 },
5605 { -0.228811, 1.231729, -0.002922 },
5606 { -0.008565, -0.153273, 1.161839 } };
5607 int i, j, k;
5608
5609 for (i=0; i < 3; i++)
5610 for (j=0; j < 3; j++)
5611 for (cmatrix[i][j] = k=0; k < 3; k++)
5612 cmatrix[i][j] += rgb_romm[i][k] * romm_cam[k][j];
5613 }
5614
parse_mos(int offset)5615 void CLASS parse_mos (int offset)
5616 {
5617 char data[40];
5618 int skip, from, i, c, neut[4], planes=0, frot=0;
5619 static const char *mod[] =
5620 { "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
5621 "Valeo 11p","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65",
5622 "Aptus 54S","Aptus 65S","Aptus 75S","AFi 5","AFi 6","AFi 7",
5623 "AFi-II 7","Aptus-II 7","","Aptus-II 6","","","Aptus-II 10","Aptus-II 5",
5624 "","","","","Aptus-II 10R","Aptus-II 8","","Aptus-II 12","","AFi-II 12" };
5625 float romm_cam[3][3];
5626
5627 fseek (ifp, offset, SEEK_SET);
5628 while (1) {
5629 if (get4() != 0x504b5453) break;
5630 get4();
5631 fread (data, 1, 40, ifp);
5632 skip = get4();
5633 from = ftell(ifp);
5634 if (!strcmp(data,"JPEG_preview_data")) {
5635 thumb_offset = from;
5636 thumb_length = skip;
5637 }
5638 if (!strcmp(data,"icc_camera_profile")) {
5639 profile_offset = from;
5640 profile_length = skip;
5641 }
5642 if (!strcmp(data,"ShootObj_back_type")) {
5643 fscanf (ifp, "%d", &i);
5644 if ((unsigned) i < sizeof mod / sizeof (*mod))
5645 strcpy (model, mod[i]);
5646 }
5647 if (!strcmp(data,"icc_camera_to_tone_matrix")) {
5648 for (i=0; i < 9; i++)
5649 ((float *)romm_cam)[i] = int_to_float(get4());
5650 romm_coeff (romm_cam);
5651 }
5652 if (!strcmp(data,"CaptProf_color_matrix")) {
5653 for (i=0; i < 9; i++)
5654 fscanf (ifp, "%f", (float *)romm_cam + i);
5655 romm_coeff (romm_cam);
5656 }
5657 if (!strcmp(data,"CaptProf_number_of_planes"))
5658 fscanf (ifp, "%d", &planes);
5659 if (!strcmp(data,"CaptProf_raw_data_rotation"))
5660 fscanf (ifp, "%d", &flip);
5661 if (!strcmp(data,"CaptProf_mosaic_pattern"))
5662 FORC4 {
5663 fscanf (ifp, "%d", &i);
5664 if (i == 1) frot = c ^ (c >> 1);
5665 }
5666 if (!strcmp(data,"ImgProf_rotation_angle")) {
5667 fscanf (ifp, "%d", &i);
5668 flip = i - flip;
5669 }
5670 if (!strcmp(data,"NeutObj_neutrals") && !cam_mul[0]) {
5671 FORC4 fscanf (ifp, "%d", neut+c);
5672 FORC3 cam_mul[c] = (float) neut[0] / neut[c+1];
5673 }
5674 if (!strcmp(data,"Rows_data"))
5675 load_flags = get4();
5676 parse_mos (from);
5677 fseek (ifp, skip+from, SEEK_SET);
5678 }
5679 if (planes)
5680 filters = (planes == 1) * 0x01010101 *
5681 (uchar) "\x94\x61\x16\x49"[(flip/90 + frot) & 3];
5682 }
5683
linear_table(unsigned len)5684 void CLASS linear_table (unsigned len)
5685 {
5686 int i;
5687 if (len > 0x1000) len = 0x1000;
5688 read_shorts (curve, len);
5689 for (i=len; i < 0x1000; i++)
5690 curve[i] = curve[i-1];
5691 maximum = curve[0xfff];
5692 }
5693
parse_kodak_ifd(int base)5694 void CLASS parse_kodak_ifd (int base)
5695 {
5696 unsigned entries, tag, type, len, save;
5697 int i, c, wbi=-2, wbtemp=6500;
5698 float mul[3]={1,1,1}, num;
5699 static const int wbtag[] = { 64037,64040,64039,64041,-1,-1,64042 };
5700
5701 entries = get2();
5702 if (entries > 1024) return;
5703 while (entries--) {
5704 tiff_get (base, &tag, &type, &len, &save);
5705 if (tag == 1020) wbi = getint(type);
5706 if (tag == 1021 && len == 72) { /* WB set in software */
5707 fseek (ifp, 40, SEEK_CUR);
5708 FORC3 cam_mul[c] = 2048.0 / get2();
5709 wbi = -2;
5710 }
5711 if (tag == 2118) wbtemp = getint(type);
5712 if (tag == 2120 + wbi && wbi >= 0)
5713 FORC3 cam_mul[c] = 2048.0 / getreal(type);
5714 if (tag == 2130 + wbi)
5715 FORC3 mul[c] = getreal(type);
5716 if (tag == 2140 + wbi && wbi >= 0)
5717 FORC3 {
5718 for (num=i=0; i < 4; i++)
5719 num += getreal(type) * pow (wbtemp/100.0, i);
5720 cam_mul[c] = 2048 / (num * mul[c]);
5721 }
5722 if (tag == 2317) linear_table (len);
5723 if (tag == 6020) iso_speed = getint(type);
5724 if (tag == 64013) wbi = fgetc(ifp);
5725 if ((unsigned) wbi < 7 && tag == wbtag[wbi])
5726 FORC3 cam_mul[c] = get4();
5727 if (tag == 64019) width = getint(type);
5728 if (tag == 64020) height = (getint(type)+1) & -2;
5729 fseek (ifp, save, SEEK_SET);
5730 }
5731 }
5732
5733 void CLASS parse_minolta (int base);
5734 int CLASS parse_tiff (int base);
5735
parse_tiff_ifd(int base)5736 int CLASS parse_tiff_ifd (int base)
5737 {
5738 unsigned entries, tag, type, len, plen=16, save;
5739 int ifd, use_cm=0, cfa, i, j, c, ima_len=0;
5740 char software[64], *cbuf, *cp;
5741 uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
5742 double cc[4][4], cm[4][3], cam_xyz[4][3], num;
5743 double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
5744 unsigned sony_curve[] = { 0,0,0,0,0,4095 };
5745 unsigned *buf, sony_offset=0, sony_length=0, sony_key=0;
5746 struct jhead jh;
5747 FILE *sfp;
5748
5749 if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
5750 return 1;
5751 ifd = tiff_nifds++;
5752 for (j=0; j < 4; j++)
5753 for (i=0; i < 4; i++)
5754 cc[j][i] = i == j;
5755 entries = get2();
5756 if (entries > 512) return 1;
5757 while (entries--) {
5758 tiff_get (base, &tag, &type, &len, &save);
5759 switch (tag) {
5760 case 5: width = get2(); break;
5761 case 6: height = get2(); break;
5762 case 7: width += get2(); break;
5763 case 9: if ((i = get2())) filters = i; break;
5764 case 17: case 18:
5765 if (type == 3 && len == 1)
5766 cam_mul[(tag-17)*2] = get2() / 256.0;
5767 break;
5768 case 23:
5769 if (type == 3) iso_speed = get2();
5770 break;
5771 case 28: case 29: case 30:
5772 cblack[tag-28] = get2();
5773 cblack[3] = cblack[1];
5774 break;
5775 case 36: case 37: case 38:
5776 cam_mul[tag-36] = get2();
5777 break;
5778 case 39:
5779 if (len < 50 || cam_mul[0]) break;
5780 fseek (ifp, 12, SEEK_CUR);
5781 FORC3 cam_mul[c] = get2();
5782 break;
5783 case 46:
5784 if (type != 7 || fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) break;
5785 thumb_offset = ftell(ifp) - 2;
5786 thumb_length = len;
5787 break;
5788 case 61440: /* Fuji HS10 table */
5789 fseek (ifp, get4()+base, SEEK_SET);
5790 parse_tiff_ifd (base);
5791 break;
5792 case 2: case 256: case 61441: /* ImageWidth */
5793 tiff_ifd[ifd].width = getint(type);
5794 break;
5795 case 3: case 257: case 61442: /* ImageHeight */
5796 tiff_ifd[ifd].height = getint(type);
5797 break;
5798 case 258: /* BitsPerSample */
5799 case 61443:
5800 tiff_ifd[ifd].samples = len & 7;
5801 tiff_ifd[ifd].bps = getint(type);
5802 break;
5803 case 61446:
5804 raw_height = 0;
5805 if (tiff_ifd[ifd].bps > 12) break;
5806 load_raw = &CLASS packed_load_raw;
5807 load_flags = get4() ? 24:80;
5808 break;
5809 case 259: /* Compression */
5810 tiff_ifd[ifd].comp = getint(type);
5811 break;
5812 case 262: /* PhotometricInterpretation */
5813 tiff_ifd[ifd].phint = get2();
5814 break;
5815 case 270: /* ImageDescription */
5816 fread (desc, 512, 1, ifp);
5817 break;
5818 case 271: /* Make */
5819 fgets (make, 64, ifp);
5820 break;
5821 case 272: /* Model */
5822 fgets (model, 64, ifp);
5823 break;
5824 case 280: /* Panasonic RW2 offset */
5825 if (type != 4) break;
5826 load_raw = &CLASS panasonic_load_raw;
5827 load_flags = 0x2008;
5828 case 273: /* StripOffset */
5829 case 513: /* JpegIFOffset */
5830 case 61447:
5831 tiff_ifd[ifd].offset = get4()+base;
5832 if (!tiff_ifd[ifd].bps && tiff_ifd[ifd].offset > 0) {
5833 fseek (ifp, tiff_ifd[ifd].offset, SEEK_SET);
5834 if (ljpeg_start (&jh, 1)) {
5835 tiff_ifd[ifd].comp = 6;
5836 tiff_ifd[ifd].width = jh.wide;
5837 tiff_ifd[ifd].height = jh.high;
5838 tiff_ifd[ifd].bps = jh.bits;
5839 tiff_ifd[ifd].samples = jh.clrs;
5840 if (!(jh.sraw || (jh.clrs & 1)))
5841 tiff_ifd[ifd].width *= jh.clrs;
5842 if ((tiff_ifd[ifd].width > 4*tiff_ifd[ifd].height) & ~jh.clrs) {
5843 tiff_ifd[ifd].width /= 2;
5844 tiff_ifd[ifd].height *= 2;
5845 }
5846 i = order;
5847 parse_tiff (tiff_ifd[ifd].offset + 12);
5848 order = i;
5849 }
5850 }
5851 break;
5852 case 274: /* Orientation */
5853 tiff_ifd[ifd].flip = "50132467"[get2() & 7]-'0';
5854 break;
5855 case 277: /* SamplesPerPixel */
5856 tiff_ifd[ifd].samples = getint(type) & 7;
5857 break;
5858 case 279: /* StripByteCounts */
5859 case 514:
5860 case 61448:
5861 tiff_ifd[ifd].bytes = get4();
5862 break;
5863 case 61454:
5864 FORC3 cam_mul[(4-c) % 3] = getint(type);
5865 break;
5866 case 305: case 11: /* Software */
5867 fgets (software, 64, ifp);
5868 if (!strncmp(software,"Adobe",5) ||
5869 !strncmp(software,"dcraw",5) ||
5870 !strncmp(software,"UFRaw",5) ||
5871 !strncmp(software,"Bibble",6) ||
5872 !strncmp(software,"Nikon Scan",10) ||
5873 !strcmp (software,"Digital Photo Professional"))
5874 is_raw = 0;
5875 break;
5876 case 306: /* DateTime */
5877 get_timestamp(0);
5878 break;
5879 case 315: /* Artist */
5880 fread (artist, 64, 1, ifp);
5881 break;
5882 case 322: /* TileWidth */
5883 tiff_ifd[ifd].tile_width = getint(type);
5884 break;
5885 case 323: /* TileLength */
5886 tiff_ifd[ifd].tile_length = getint(type);
5887 break;
5888 case 324: /* TileOffsets */
5889 tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
5890 if (len == 1)
5891 tiff_ifd[ifd].tile_width = tiff_ifd[ifd].tile_length = 0;
5892 if (len == 4) {
5893 load_raw = &CLASS sinar_4shot_load_raw;
5894 is_raw = 5;
5895 }
5896 break;
5897 case 330: /* SubIFDs */
5898 if (!strcmp(model,"DSLR-A100") && tiff_ifd[ifd].width == 3872) {
5899 load_raw = &CLASS sony_arw_load_raw;
5900 data_offset = get4()+base;
5901 ifd++; break;
5902 }
5903 while (len--) {
5904 i = ftell(ifp);
5905 fseek (ifp, get4()+base, SEEK_SET);
5906 if (parse_tiff_ifd (base)) break;
5907 fseek (ifp, i+4, SEEK_SET);
5908 }
5909 break;
5910 case 400:
5911 strcpy (make, "Sarnoff");
5912 maximum = 0xfff;
5913 break;
5914 case 28688:
5915 FORC4 sony_curve[c+1] = get2() >> 2 & 0xfff;
5916 for (i=0; i < 5; i++)
5917 for (j = sony_curve[i]+1; j <= sony_curve[i+1]; j++)
5918 curve[j] = curve[j-1] + (1 << i);
5919 break;
5920 case 29184: sony_offset = get4(); break;
5921 case 29185: sony_length = get4(); break;
5922 case 29217: sony_key = get4(); break;
5923 case 29264:
5924 parse_minolta (ftell(ifp));
5925 raw_width = 0;
5926 break;
5927 case 29443:
5928 FORC4 cam_mul[c ^ (c < 2)] = get2();
5929 break;
5930 case 29459:
5931 FORC4 cam_mul[c] = get2();
5932 i = (cam_mul[1] == 1024 && cam_mul[2] == 1024) << 1;
5933 SWAP (cam_mul[i],cam_mul[i+1])
5934 break;
5935 case 33405: /* Model2 */
5936 fgets (model2, 64, ifp);
5937 break;
5938 case 33421: /* CFARepeatPatternDim */
5939 if (get2() == 6 && get2() == 6)
5940 filters = 9;
5941 break;
5942 case 33422: /* CFAPattern */
5943 if (filters == 9) {
5944 FORC(36) ((char *)xtrans)[c] = fgetc(ifp) & 3;
5945 break;
5946 }
5947 case 64777: /* Kodak P-series */
5948 if ((plen=len) > 16) plen = 16;
5949 fread (cfa_pat, 1, plen, ifp);
5950 for (colors=cfa=i=0; i < plen && colors < 4; i++) {
5951 colors += !(cfa & (1 << cfa_pat[i]));
5952 cfa |= 1 << cfa_pat[i];
5953 }
5954 if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
5955 if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
5956 goto guess_cfa_pc;
5957 case 33424:
5958 case 65024:
5959 fseek (ifp, get4()+base, SEEK_SET);
5960 parse_kodak_ifd (base);
5961 break;
5962 case 33434: /* ExposureTime */
5963 tiff_ifd[ifd].shutter = shutter = getreal(type);
5964 break;
5965 case 33437: /* FNumber */
5966 aperture = getreal(type);
5967 break;
5968 case 34306: /* Leaf white balance */
5969 FORC4 cam_mul[c ^ 1] = 4096.0 / get2();
5970 break;
5971 case 34307: /* Leaf CatchLight color matrix */
5972 fread (software, 1, 7, ifp);
5973 if (strncmp(software,"MATRIX",6)) break;
5974 colors = 4;
5975 for (raw_color = i=0; i < 3; i++) {
5976 FORC4 fscanf (ifp, "%f", &rgb_cam[i][c^1]);
5977 if (!use_camera_wb) continue;
5978 num = 0;
5979 FORC4 num += rgb_cam[i][c];
5980 FORC4 rgb_cam[i][c] /= num;
5981 }
5982 break;
5983 case 34310: /* Leaf metadata */
5984 parse_mos (ftell(ifp));
5985 case 34303:
5986 strcpy (make, "Leaf");
5987 break;
5988 case 34665: /* EXIF tag */
5989 fseek (ifp, get4()+base, SEEK_SET);
5990 parse_exif (base);
5991 break;
5992 case 34853: /* GPSInfo tag */
5993 fseek (ifp, get4()+base, SEEK_SET);
5994 parse_gps (base);
5995 break;
5996 case 34675: /* InterColorProfile */
5997 case 50831: /* AsShotICCProfile */
5998 profile_offset = ftell(ifp);
5999 profile_length = len;
6000 break;
6001 case 37122: /* CompressedBitsPerPixel */
6002 kodak_cbpp = get4();
6003 break;
6004 case 37386: /* FocalLength */
6005 focal_len = getreal(type);
6006 break;
6007 case 37393: /* ImageNumber */
6008 shot_order = getint(type);
6009 break;
6010 case 37400: /* old Kodak KDC tag */
6011 for (raw_color = i=0; i < 3; i++) {
6012 getreal(type);
6013 FORC3 rgb_cam[i][c] = getreal(type);
6014 }
6015 break;
6016 case 40976:
6017 strip_offset = get4();
6018 switch (tiff_ifd[ifd].comp) {
6019 case 32770: load_raw = &CLASS samsung_load_raw; break;
6020 case 32772: load_raw = &CLASS samsung2_load_raw; break;
6021 case 32773: load_raw = &CLASS samsung3_load_raw; break;
6022 }
6023 break;
6024 case 46275: /* Imacon tags */
6025 strcpy (make, "Imacon");
6026 data_offset = ftell(ifp);
6027 ima_len = len;
6028 break;
6029 case 46279:
6030 if (!ima_len) break;
6031 fseek (ifp, 38, SEEK_CUR);
6032 case 46274:
6033 fseek (ifp, 40, SEEK_CUR);
6034 raw_width = get4();
6035 raw_height = get4();
6036 left_margin = get4() & 7;
6037 width = raw_width - left_margin - (get4() & 7);
6038 top_margin = get4() & 7;
6039 height = raw_height - top_margin - (get4() & 7);
6040 if (raw_width == 7262) {
6041 height = 5444;
6042 width = 7244;
6043 left_margin = 7;
6044 }
6045 fseek (ifp, 52, SEEK_CUR);
6046 FORC3 cam_mul[c] = getreal(11);
6047 fseek (ifp, 114, SEEK_CUR);
6048 flip = (get2() >> 7) * 90;
6049 if (width * height * 6 == ima_len) {
6050 if (flip % 180 == 90) SWAP(width,height);
6051 raw_width = width;
6052 raw_height = height;
6053 left_margin = top_margin = filters = flip = 0;
6054 }
6055 sprintf (model, "Ixpress %d-Mp", height*width/1000000);
6056 load_raw = &CLASS imacon_full_load_raw;
6057 if (filters) {
6058 if (left_margin & 1) filters = 0x61616161;
6059 load_raw = &CLASS unpacked_load_raw;
6060 }
6061 maximum = 0xffff;
6062 break;
6063 case 50454: /* Sinar tag */
6064 case 50455:
6065 if (!(cbuf = (char *) malloc(len))) break;
6066 fread (cbuf, 1, len, ifp);
6067 for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
6068 if (!strncmp (++cp,"Neutral ",8))
6069 sscanf (cp+8, "%f %f %f", cam_mul, cam_mul+1, cam_mul+2);
6070 free (cbuf);
6071 break;
6072 case 50458:
6073 if (!make[0]) strcpy (make, "Hasselblad");
6074 break;
6075 case 50459: /* Hasselblad tag */
6076 i = order;
6077 j = ftell(ifp);
6078 c = tiff_nifds;
6079 order = get2();
6080 fseek (ifp, j+(get2(),get4()), SEEK_SET);
6081 parse_tiff_ifd (j);
6082 maximum = 0xffff;
6083 tiff_nifds = c;
6084 order = i;
6085 break;
6086 case 50706: /* DNGVersion */
6087 FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
6088 if (!make[0]) strcpy (make, "DNG");
6089 is_raw = 1;
6090 break;
6091 case 50708: /* UniqueCameraModel */
6092 if (model[0]) break;
6093 fgets (make, 64, ifp);
6094 if ((cp = strchr(make,' '))) {
6095 strcpy(model,cp+1);
6096 *cp = 0;
6097 }
6098 break;
6099 case 50710: /* CFAPlaneColor */
6100 if (filters == 9) break;
6101 if (len > 4) len = 4;
6102 colors = len;
6103 fread (cfa_pc, 1, colors, ifp);
6104 guess_cfa_pc:
6105 FORCC tab[cfa_pc[c]] = c;
6106 cdesc[c] = 0;
6107 for (i=16; i--; )
6108 filters = filters << 2 | tab[cfa_pat[i % plen]];
6109 filters -= !filters;
6110 break;
6111 case 50711: /* CFALayout */
6112 if (get2() == 2) fuji_width = 1;
6113 break;
6114 case 291:
6115 case 50712: /* LinearizationTable */
6116 linear_table (len);
6117 break;
6118 case 50713: /* BlackLevelRepeatDim */
6119 cblack[4] = get2();
6120 cblack[5] = get2();
6121 if (cblack[4] * cblack[5] > sizeof cblack / sizeof *cblack - 6)
6122 cblack[4] = cblack[5] = 1;
6123 break;
6124 case 61450:
6125 cblack[4] = cblack[5] = MIN(sqrt(len),64);
6126 case 50714: /* BlackLevel */
6127 if (!(cblack[4] * cblack[5]))
6128 cblack[4] = cblack[5] = 1;
6129 FORC (cblack[4] * cblack[5])
6130 cblack[6+c] = getreal(type);
6131 black = 0;
6132 break;
6133 case 50715: /* BlackLevelDeltaH */
6134 case 50716: /* BlackLevelDeltaV */
6135 for (num=i=0; i < len; i++)
6136 num += getreal(type);
6137 black += num/len + 0.5;
6138 break;
6139 case 50717: /* WhiteLevel */
6140 maximum = getint(type);
6141 break;
6142 case 50718: /* DefaultScale */
6143 pixel_aspect = getreal(type);
6144 pixel_aspect /= getreal(type);
6145 break;
6146 case 50721: /* ColorMatrix1 */
6147 case 50722: /* ColorMatrix2 */
6148 FORCC for (j=0; j < 3; j++)
6149 cm[c][j] = getreal(type);
6150 use_cm = 1;
6151 break;
6152 case 50723: /* CameraCalibration1 */
6153 case 50724: /* CameraCalibration2 */
6154 for (i=0; i < colors; i++)
6155 FORCC cc[i][c] = getreal(type);
6156 break;
6157 case 50727: /* AnalogBalance */
6158 FORCC ab[c] = getreal(type);
6159 break;
6160 case 50728: /* AsShotNeutral */
6161 FORCC asn[c] = getreal(type);
6162 break;
6163 case 50729: /* AsShotWhiteXY */
6164 xyz[0] = getreal(type);
6165 xyz[1] = getreal(type);
6166 xyz[2] = 1 - xyz[0] - xyz[1];
6167 FORC3 xyz[c] /= d65_white[c];
6168 break;
6169 case 50740: /* DNGPrivateData */
6170 if (dng_version) break;
6171 parse_minolta (j = get4()+base);
6172 fseek (ifp, j, SEEK_SET);
6173 parse_tiff_ifd (base);
6174 break;
6175 case 50752:
6176 read_shorts (cr2_slice, 3);
6177 break;
6178 case 50829: /* ActiveArea */
6179 top_margin = getint(type);
6180 left_margin = getint(type);
6181 height = getint(type) - top_margin;
6182 width = getint(type) - left_margin;
6183 break;
6184 case 50830: /* MaskedAreas */
6185 for (i=0; i < len && i < 32; i++)
6186 ((int *)mask)[i] = getint(type);
6187 black = 0;
6188 break;
6189 case 51009: /* OpcodeList2 */
6190 meta_offset = ftell(ifp);
6191 break;
6192 case 64772: /* Kodak P-series */
6193 if (len < 13) break;
6194 fseek (ifp, 16, SEEK_CUR);
6195 data_offset = get4();
6196 fseek (ifp, 28, SEEK_CUR);
6197 data_offset += get4();
6198 load_raw = &CLASS packed_load_raw;
6199 break;
6200 case 65026:
6201 if (type == 2) fgets (model2, 64, ifp);
6202 }
6203 fseek (ifp, save, SEEK_SET);
6204 }
6205 if (sony_length && (buf = (unsigned *) malloc(sony_length))) {
6206 fseek (ifp, sony_offset, SEEK_SET);
6207 fread (buf, sony_length, 1, ifp);
6208 sony_decrypt (buf, sony_length/4, 1, sony_key);
6209 sfp = ifp;
6210 if ((ifp = tmpfile())) {
6211 fwrite (buf, sony_length, 1, ifp);
6212 fseek (ifp, 0, SEEK_SET);
6213 parse_tiff_ifd (-sony_offset);
6214 fclose (ifp);
6215 }
6216 ifp = sfp;
6217 free (buf);
6218 }
6219 for (i=0; i < colors; i++)
6220 FORCC cc[i][c] *= ab[i];
6221 if (use_cm) {
6222 FORCC for (i=0; i < 3; i++)
6223 for (cam_xyz[c][i]=j=0; j < colors; j++)
6224 cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
6225 cam_xyz_coeff (cmatrix, cam_xyz);
6226 }
6227 if (asn[0]) {
6228 cam_mul[3] = 0;
6229 FORCC cam_mul[c] = 1 / asn[c];
6230 }
6231 if (!use_cm)
6232 FORCC pre_mul[c] /= cc[c][c];
6233 return 0;
6234 }
6235
parse_tiff(int base)6236 int CLASS parse_tiff (int base)
6237 {
6238 int doff;
6239
6240 fseek (ifp, base, SEEK_SET);
6241 order = get2();
6242 if (order != 0x4949 && order != 0x4d4d) return 0;
6243 get2();
6244 while ((doff = get4())) {
6245 fseek (ifp, doff+base, SEEK_SET);
6246 if (parse_tiff_ifd (base)) break;
6247 }
6248 return 1;
6249 }
6250
apply_tiff()6251 void CLASS apply_tiff()
6252 {
6253 int max_samp=0, ties=0, os, ns, raw=-1, thm=-1, i;
6254 struct jhead jh;
6255
6256 thumb_misc = 16;
6257 if (thumb_offset) {
6258 fseek (ifp, thumb_offset, SEEK_SET);
6259 if (ljpeg_start (&jh, 1)) {
6260 thumb_misc = jh.bits;
6261 thumb_width = jh.wide;
6262 thumb_height = jh.high;
6263 }
6264 }
6265 for (i=tiff_nifds; i--; ) {
6266 if (tiff_ifd[i].shutter)
6267 shutter = tiff_ifd[i].shutter;
6268 tiff_ifd[i].shutter = shutter;
6269 }
6270 for (i=0; i < tiff_nifds; i++) {
6271 if (max_samp < tiff_ifd[i].samples)
6272 max_samp = tiff_ifd[i].samples;
6273 if (max_samp > 3) max_samp = 3;
6274 os = raw_width*raw_height;
6275 ns = tiff_ifd[i].width*tiff_ifd[i].height;
6276 if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
6277 (tiff_ifd[i].width | tiff_ifd[i].height) < 0x10000 &&
6278 ns && ((ns > os && (ties = 1)) ||
6279 (ns == os && shot_select == ties++))) {
6280 raw_width = tiff_ifd[i].width;
6281 raw_height = tiff_ifd[i].height;
6282 tiff_bps = tiff_ifd[i].bps;
6283 tiff_compress = tiff_ifd[i].comp;
6284 data_offset = tiff_ifd[i].offset;
6285 tiff_flip = tiff_ifd[i].flip;
6286 tiff_samples = tiff_ifd[i].samples;
6287 tile_width = tiff_ifd[i].tile_width;
6288 tile_length = tiff_ifd[i].tile_length;
6289 shutter = tiff_ifd[i].shutter;
6290 raw = i;
6291 }
6292 }
6293 if (is_raw == 1 && ties) is_raw = ties;
6294 if (!tile_width ) tile_width = INT_MAX;
6295 if (!tile_length) tile_length = INT_MAX;
6296 for (i=tiff_nifds; i--; )
6297 if (tiff_ifd[i].flip) tiff_flip = tiff_ifd[i].flip;
6298 if (raw >= 0 && !load_raw)
6299 switch (tiff_compress) {
6300 case 32767:
6301 if (tiff_ifd[raw].bytes == raw_width*raw_height) {
6302 tiff_bps = 12;
6303 load_raw = &CLASS sony_arw2_load_raw; break;
6304 }
6305 if (tiff_ifd[raw].bytes*8 != raw_width*raw_height*tiff_bps) {
6306 raw_height += 8;
6307 load_raw = &CLASS sony_arw_load_raw; break;
6308 }
6309 load_flags = 79;
6310 case 32769:
6311 load_flags++;
6312 case 32770:
6313 case 32773: goto slr;
6314 case 0: case 1:
6315 if (!strncmp(make,"OLYMPUS",7) &&
6316 tiff_ifd[raw].bytes*2 == raw_width*raw_height*3)
6317 load_flags = 24;
6318 if (tiff_ifd[raw].bytes*5 == raw_width*raw_height*8) {
6319 load_flags = 81;
6320 tiff_bps = 12;
6321 } slr:
6322 switch (tiff_bps) {
6323 case 8: load_raw = &CLASS eight_bit_load_raw; break;
6324 case 12: if (tiff_ifd[raw].phint == 2)
6325 load_flags = 6;
6326 load_raw = &CLASS packed_load_raw; break;
6327 case 14: load_flags = 0;
6328 case 16: load_raw = &CLASS unpacked_load_raw;
6329 if (!strncmp(make,"OLYMPUS",7) &&
6330 tiff_ifd[raw].bytes*7 > raw_width*raw_height)
6331 load_raw = &CLASS olympus_load_raw;
6332 }
6333 break;
6334 case 6: case 7: case 99:
6335 load_raw = &CLASS lossless_jpeg_load_raw; break;
6336 case 262:
6337 load_raw = &CLASS kodak_262_load_raw; break;
6338 case 34713:
6339 if ((raw_width+9)/10*16*raw_height == tiff_ifd[raw].bytes) {
6340 load_raw = &CLASS packed_load_raw;
6341 load_flags = 1;
6342 } else if (raw_width*raw_height*3 == tiff_ifd[raw].bytes*2) {
6343 load_raw = &CLASS packed_load_raw;
6344 if (model[0] == 'N') load_flags = 80;
6345 } else if (raw_width*raw_height*3 == tiff_ifd[raw].bytes) {
6346 load_raw = &CLASS nikon_yuv_load_raw;
6347 gamma_curve (1/2.4, 12.92, 1, 4095);
6348 memset (cblack, 0, sizeof cblack);
6349 filters = 0;
6350 } else if (raw_width*raw_height*2 == tiff_ifd[raw].bytes) {
6351 load_raw = &CLASS unpacked_load_raw;
6352 load_flags = 4;
6353 order = 0x4d4d;
6354 } else
6355 load_raw = &CLASS nikon_load_raw; break;
6356 case 65535:
6357 load_raw = &CLASS pentax_load_raw; break;
6358 case 65000:
6359 switch (tiff_ifd[raw].phint) {
6360 case 2: load_raw = &CLASS kodak_rgb_load_raw; filters = 0; break;
6361 case 6: load_raw = &CLASS kodak_ycbcr_load_raw; filters = 0; break;
6362 case 32803: load_raw = &CLASS kodak_65000_load_raw;
6363 }
6364 case 32867: case 34892: break;
6365 default: is_raw = 0;
6366 }
6367 if (!dng_version)
6368 if ( (tiff_samples == 3 && tiff_ifd[raw].bytes && tiff_bps != 14 &&
6369 (tiff_compress & -16) != 32768)
6370 || (tiff_bps == 8 && !strcasestr(make,"Kodak") &&
6371 !strstr(model2,"DEBUG RAW")))
6372 is_raw = 0;
6373 for (i=0; i < tiff_nifds; i++)
6374 if (i != raw && tiff_ifd[i].samples == max_samp &&
6375 tiff_ifd[i].width * tiff_ifd[i].height / (SQR(tiff_ifd[i].bps)+1) >
6376 thumb_width * thumb_height / (SQR(thumb_misc)+1)
6377 && tiff_ifd[i].comp != 34892) {
6378 thumb_width = tiff_ifd[i].width;
6379 thumb_height = tiff_ifd[i].height;
6380 thumb_offset = tiff_ifd[i].offset;
6381 thumb_length = tiff_ifd[i].bytes;
6382 thumb_misc = tiff_ifd[i].bps;
6383 thm = i;
6384 }
6385 if (thm >= 0) {
6386 thumb_misc |= tiff_ifd[thm].samples << 5;
6387 switch (tiff_ifd[thm].comp) {
6388 case 0:
6389 write_thumb = &CLASS layer_thumb;
6390 break;
6391 case 1:
6392 if (tiff_ifd[thm].bps <= 8)
6393 write_thumb = &CLASS ppm_thumb;
6394 else if (!strcmp(make,"Imacon"))
6395 write_thumb = &CLASS ppm16_thumb;
6396 else
6397 thumb_load_raw = &CLASS kodak_thumb_load_raw;
6398 break;
6399 case 65000:
6400 thumb_load_raw = tiff_ifd[thm].phint == 6 ?
6401 &CLASS kodak_ycbcr_load_raw : &CLASS kodak_rgb_load_raw;
6402 }
6403 }
6404 }
6405
parse_minolta(int base)6406 void CLASS parse_minolta (int base)
6407 {
6408 int save, tag, len, offset, high=0, wide=0, i, c;
6409 short sorder=order;
6410
6411 fseek (ifp, base, SEEK_SET);
6412 if (fgetc(ifp) || fgetc(ifp)-'M' || fgetc(ifp)-'R') return;
6413 order = fgetc(ifp) * 0x101;
6414 offset = base + get4() + 8;
6415 while ((save=ftell(ifp)) < offset) {
6416 for (tag=i=0; i < 4; i++)
6417 tag = tag << 8 | fgetc(ifp);
6418 len = get4();
6419 switch (tag) {
6420 case 0x505244: /* PRD */
6421 fseek (ifp, 8, SEEK_CUR);
6422 high = get2();
6423 wide = get2();
6424 break;
6425 case 0x574247: /* WBG */
6426 get4();
6427 i = strcmp(model,"DiMAGE A200") ? 0:3;
6428 FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
6429 break;
6430 case 0x545457: /* TTW */
6431 parse_tiff (ftell(ifp));
6432 data_offset = offset;
6433 }
6434 fseek (ifp, save+len+8, SEEK_SET);
6435 }
6436 raw_height = high;
6437 raw_width = wide;
6438 order = sorder;
6439 }
6440
6441 /*
6442 Many cameras have a "debug mode" that writes JPEG and raw
6443 at the same time. The raw file has no header, so try to
6444 to open the matching JPEG file and read its metadata.
6445 */
parse_external_jpeg()6446 void CLASS parse_external_jpeg()
6447 {
6448 const char *file, *ext;
6449 char *jname, *jfile, *jext;
6450 FILE *save=ifp;
6451
6452 ext = strrchr (ifname, '.');
6453 file = strrchr (ifname, '/');
6454 if (!file) file = strrchr (ifname, '\\');
6455 if (!file) file = ifname-1;
6456 file++;
6457 if (!ext || strlen(ext) != 4 || ext-file != 8) return;
6458 jname = (char *) malloc (strlen(ifname) + 1);
6459 merror (jname, "parse_external_jpeg()");
6460 strcpy (jname, ifname);
6461 jfile = file - ifname + jname;
6462 jext = ext - ifname + jname;
6463 if (strcasecmp (ext, ".jpg")) {
6464 strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
6465 if (isdigit(*file)) {
6466 memcpy (jfile, file+4, 4);
6467 memcpy (jfile+4, file, 4);
6468 }
6469 } else
6470 while (isdigit(*--jext)) {
6471 if (*jext != '9') {
6472 (*jext)++;
6473 break;
6474 }
6475 *jext = '0';
6476 }
6477 if (strcmp (jname, ifname)) {
6478 if ((ifp = fopen (jname, "rb"))) {
6479 if (verbose)
6480 fprintf (stderr,_("Reading metadata from %s ...\n"), jname);
6481 parse_tiff (12);
6482 thumb_offset = 0;
6483 is_raw = 1;
6484 fclose (ifp);
6485 }
6486 }
6487 if (!timestamp)
6488 fprintf (stderr,_("Failed to read metadata from %s\n"), jname);
6489 free (jname);
6490 ifp = save;
6491 }
6492
6493 /*
6494 CIFF block 0x1030 contains an 8x8 white sample.
6495 Load this into white[][] for use in scale_colors().
6496 */
ciff_block_1030()6497 void CLASS ciff_block_1030()
6498 {
6499 static const ushort key[] = { 0x410, 0x45f3 };
6500 int i, bpp, row, col, vbits=0;
6501 unsigned long bitbuf=0;
6502
6503 if ((get2(),get4()) != 0x80008 || !get4()) return;
6504 bpp = get2();
6505 if (bpp != 10 && bpp != 12) return;
6506 for (i=row=0; row < 8; row++)
6507 for (col=0; col < 8; col++) {
6508 if (vbits < bpp) {
6509 bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
6510 vbits += 16;
6511 }
6512 white[row][col] = bitbuf >> (vbits -= bpp) & ~(-1 << bpp);
6513 }
6514 }
6515
6516 /*
6517 Parse a CIFF file, better known as Canon CRW format.
6518 */
parse_ciff(int offset,int length,int depth)6519 void CLASS parse_ciff (int offset, int length, int depth)
6520 {
6521 int tboff, nrecs, c, type, len, save, wbi=-1;
6522 ushort key[] = { 0x410, 0x45f3 };
6523
6524 fseek (ifp, offset+length-4, SEEK_SET);
6525 tboff = get4() + offset;
6526 fseek (ifp, tboff, SEEK_SET);
6527 nrecs = get2();
6528 if ((nrecs | depth) > 127) return;
6529 while (nrecs--) {
6530 type = get2();
6531 len = get4();
6532 save = ftell(ifp) + 4;
6533 fseek (ifp, offset+get4(), SEEK_SET);
6534 if ((((type >> 8) + 8) | 8) == 0x38)
6535 parse_ciff (ftell(ifp), len, depth+1); /* Parse a sub-table */
6536 if (type == 0x0810)
6537 fread (artist, 64, 1, ifp);
6538 if (type == 0x080a) {
6539 fread (make, 64, 1, ifp);
6540 fseek (ifp, strlen(make) - 63, SEEK_CUR);
6541 fread (model, 64, 1, ifp);
6542 }
6543 if (type == 0x1810) {
6544 width = get4();
6545 height = get4();
6546 pixel_aspect = int_to_float(get4());
6547 flip = get4();
6548 }
6549 if (type == 0x1835) /* Get the decoder table */
6550 tiff_compress = get4();
6551 if (type == 0x2007) {
6552 thumb_offset = ftell(ifp);
6553 thumb_length = len;
6554 }
6555 if (type == 0x1818) {
6556 shutter = pow (2, -int_to_float((get4(),get4())));
6557 aperture = pow (2, int_to_float(get4())/2);
6558 }
6559 if (type == 0x102a) {
6560 iso_speed = pow (2, (get4(),get2())/32.0 - 4) * 50;
6561 aperture = pow (2, (get2(),(short)get2())/64.0);
6562 shutter = pow (2,-((short)get2())/32.0);
6563 wbi = (get2(),get2());
6564 if (wbi > 17) wbi = 0;
6565 fseek (ifp, 32, SEEK_CUR);
6566 if (shutter > 1e6) shutter = get2()/10.0;
6567 }
6568 if (type == 0x102c) {
6569 if (get2() > 512) { /* Pro90, G1 */
6570 fseek (ifp, 118, SEEK_CUR);
6571 FORC4 cam_mul[c ^ 2] = get2();
6572 } else { /* G2, S30, S40 */
6573 fseek (ifp, 98, SEEK_CUR);
6574 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2();
6575 }
6576 }
6577 if (type == 0x0032) {
6578 if (len == 768) { /* EOS D30 */
6579 fseek (ifp, 72, SEEK_CUR);
6580 FORC4 cam_mul[c ^ (c >> 1)] = 1024.0 / get2();
6581 if (!wbi) cam_mul[0] = -1; /* use my auto white balance */
6582 } else if (!cam_mul[0]) {
6583 if (get2() == key[0]) /* Pro1, G6, S60, S70 */
6584 c = (strstr(model,"Pro1") ?
6585 "012346000000000000":"01345:000000006008")[wbi]-'0'+ 2;
6586 else { /* G3, G5, S45, S50 */
6587 c = "023457000000006000"[wbi]-'0';
6588 key[0] = key[1] = 0;
6589 }
6590 fseek (ifp, 78 + c*8, SEEK_CUR);
6591 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2() ^ key[c & 1];
6592 if (!wbi) cam_mul[0] = -1;
6593 }
6594 }
6595 if (type == 0x10a9) { /* D60, 10D, 300D, and clones */
6596 if (len > 66) wbi = "0134567028"[wbi]-'0';
6597 fseek (ifp, 2 + wbi*8, SEEK_CUR);
6598 FORC4 cam_mul[c ^ (c >> 1)] = get2();
6599 }
6600 if (type == 0x1030 && (0x18040 >> wbi & 1))
6601 ciff_block_1030(); /* all that don't have 0x10a9 */
6602 if (type == 0x1031) {
6603 raw_width = (get2(),get2());
6604 raw_height = get2();
6605 }
6606 if (type == 0x5029) {
6607 focal_len = len >> 16;
6608 if ((len & 0xffff) == 2) focal_len /= 32;
6609 }
6610 if (type == 0x5813) flash_used = int_to_float(len);
6611 if (type == 0x5814) canon_ev = int_to_float(len);
6612 if (type == 0x5817) shot_order = len;
6613 if (type == 0x5834) unique_id = len;
6614 if (type == 0x580e) timestamp = len;
6615 if (type == 0x180e) timestamp = get4();
6616 #ifdef LOCALTIME
6617 if ((type | 0x4000) == 0x580e)
6618 timestamp = mktime (gmtime (×tamp));
6619 #endif
6620 fseek (ifp, save, SEEK_SET);
6621 }
6622 }
6623
parse_rollei()6624 void CLASS parse_rollei()
6625 {
6626 char line[128], *val;
6627 struct tm t;
6628
6629 fseek (ifp, 0, SEEK_SET);
6630 memset (&t, 0, sizeof t);
6631 do {
6632 fgets (line, 128, ifp);
6633 if ((val = strchr(line,'=')))
6634 *val++ = 0;
6635 else
6636 val = line + strlen(line);
6637 if (!strcmp(line,"DAT"))
6638 sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
6639 if (!strcmp(line,"TIM"))
6640 sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
6641 if (!strcmp(line,"HDR"))
6642 thumb_offset = atoi(val);
6643 if (!strcmp(line,"X "))
6644 raw_width = atoi(val);
6645 if (!strcmp(line,"Y "))
6646 raw_height = atoi(val);
6647 if (!strcmp(line,"TX "))
6648 thumb_width = atoi(val);
6649 if (!strcmp(line,"TY "))
6650 thumb_height = atoi(val);
6651 } while (strncmp(line,"EOHD",4));
6652 data_offset = thumb_offset + thumb_width * thumb_height * 2;
6653 t.tm_year -= 1900;
6654 t.tm_mon -= 1;
6655 if (mktime(&t) > 0)
6656 timestamp = mktime(&t);
6657 strcpy (make, "Rollei");
6658 strcpy (model,"d530flex");
6659 write_thumb = &CLASS rollei_thumb;
6660 }
6661
parse_sinar_ia()6662 void CLASS parse_sinar_ia()
6663 {
6664 int entries, off;
6665 char str[8], *cp;
6666
6667 order = 0x4949;
6668 fseek (ifp, 4, SEEK_SET);
6669 entries = get4();
6670 fseek (ifp, get4(), SEEK_SET);
6671 while (entries--) {
6672 off = get4(); get4();
6673 fread (str, 8, 1, ifp);
6674 if (!strcmp(str,"META")) meta_offset = off;
6675 if (!strcmp(str,"THUMB")) thumb_offset = off;
6676 if (!strcmp(str,"RAW0")) data_offset = off;
6677 }
6678 fseek (ifp, meta_offset+20, SEEK_SET);
6679 fread (make, 64, 1, ifp);
6680 make[63] = 0;
6681 if ((cp = strchr(make,' '))) {
6682 strcpy (model, cp+1);
6683 *cp = 0;
6684 }
6685 raw_width = get2();
6686 raw_height = get2();
6687 load_raw = &CLASS unpacked_load_raw;
6688 thumb_width = (get4(),get2());
6689 thumb_height = get2();
6690 write_thumb = &CLASS ppm_thumb;
6691 maximum = 0x3fff;
6692 }
6693
parse_phase_one(int base)6694 void CLASS parse_phase_one (int base)
6695 {
6696 unsigned entries, tag, type, len, data, save, i, c;
6697 float romm_cam[3][3];
6698 char *cp;
6699
6700 memset (&ph1, 0, sizeof ph1);
6701 fseek (ifp, base, SEEK_SET);
6702 order = get4() & 0xffff;
6703 if (get4() >> 8 != 0x526177) return; /* "Raw" */
6704 fseek (ifp, get4()+base, SEEK_SET);
6705 entries = get4();
6706 get4();
6707 while (entries--) {
6708 tag = get4();
6709 type = get4();
6710 len = get4();
6711 data = get4();
6712 save = ftell(ifp);
6713 fseek (ifp, base+data, SEEK_SET);
6714 switch (tag) {
6715 case 0x100: flip = "0653"[data & 3]-'0'; break;
6716 case 0x106:
6717 for (i=0; i < 9; i++)
6718 ((float *)romm_cam)[i] = getreal(11);
6719 romm_coeff (romm_cam);
6720 break;
6721 case 0x107:
6722 FORC3 cam_mul[c] = getreal(11);
6723 break;
6724 case 0x108: raw_width = data; break;
6725 case 0x109: raw_height = data; break;
6726 case 0x10a: left_margin = data; break;
6727 case 0x10b: top_margin = data; break;
6728 case 0x10c: width = data; break;
6729 case 0x10d: height = data; break;
6730 case 0x10e: ph1.format = data; break;
6731 case 0x10f: data_offset = data+base; break;
6732 case 0x110: meta_offset = data+base;
6733 meta_length = len; break;
6734 case 0x112: ph1.key_off = save - 4; break;
6735 case 0x210: ph1.tag_210 = int_to_float(data); break;
6736 case 0x21a: ph1.tag_21a = data; break;
6737 case 0x21c: strip_offset = data+base; break;
6738 case 0x21d: ph1.black = data; break;
6739 case 0x222: ph1.split_col = data; break;
6740 case 0x223: ph1.black_col = data+base; break;
6741 case 0x224: ph1.split_row = data; break;
6742 case 0x225: ph1.black_row = data+base; break;
6743 case 0x301:
6744 model[63] = 0;
6745 fread (model, 1, 63, ifp);
6746 if ((cp = strstr(model," camera"))) *cp = 0;
6747 }
6748 fseek (ifp, save, SEEK_SET);
6749 }
6750 load_raw = ph1.format < 3 ?
6751 &CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
6752 maximum = 0xffff;
6753 strcpy (make, "Phase One");
6754 if (model[0]) return;
6755 switch (raw_height) {
6756 case 2060: strcpy (model,"LightPhase"); break;
6757 case 2682: strcpy (model,"H 10"); break;
6758 case 4128: strcpy (model,"H 20"); break;
6759 case 5488: strcpy (model,"H 25"); break;
6760 }
6761 }
6762
parse_fuji(int offset)6763 void CLASS parse_fuji (int offset)
6764 {
6765 unsigned entries, tag, len, save, c;
6766
6767 fseek (ifp, offset, SEEK_SET);
6768 entries = get4();
6769 if (entries > 255) return;
6770 while (entries--) {
6771 tag = get2();
6772 len = get2();
6773 save = ftell(ifp);
6774 if (tag == 0x100) {
6775 raw_height = get2();
6776 raw_width = get2();
6777 } else if (tag == 0x121) {
6778 height = get2();
6779 if ((width = get2()) == 4284) width += 3;
6780 } else if (tag == 0x130) {
6781 fuji_layout = fgetc(ifp) >> 7;
6782 fuji_width = !(fgetc(ifp) & 8);
6783 } else if (tag == 0x131) {
6784 filters = 9;
6785 FORC(36) xtrans_abs[0][35-c] = fgetc(ifp) & 3;
6786 } else if (tag == 0x2ff0) {
6787 FORC4 cam_mul[c ^ 1] = get2();
6788 } else if (tag == 0xc000) {
6789 c = order;
6790 order = 0x4949;
6791 if ((tag = get4()) > 10000) tag = get4();
6792 width = tag;
6793 height = get4();
6794 order = c;
6795 }
6796 fseek (ifp, save+len, SEEK_SET);
6797 }
6798 height <<= fuji_layout;
6799 width >>= fuji_layout;
6800 }
6801
parse_jpeg(int offset)6802 int CLASS parse_jpeg (int offset)
6803 {
6804 int len, save, hlen, mark;
6805
6806 fseek (ifp, offset, SEEK_SET);
6807 if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
6808
6809 while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
6810 order = 0x4d4d;
6811 len = get2() - 2;
6812 save = ftell(ifp);
6813 if (mark == 0xc0 || mark == 0xc3) {
6814 fgetc(ifp);
6815 raw_height = get2();
6816 raw_width = get2();
6817 }
6818 order = get2();
6819 hlen = get4();
6820 if (get4() == 0x48454150) /* "HEAP" */
6821 parse_ciff (save+hlen, len-hlen, 0);
6822 if (parse_tiff (save+6)) apply_tiff();
6823 fseek (ifp, save+len, SEEK_SET);
6824 }
6825 return 1;
6826 }
6827
parse_riff()6828 void CLASS parse_riff()
6829 {
6830 unsigned i, size, end;
6831 char tag[4], date[64], month[64];
6832 static const char mon[12][4] =
6833 { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
6834 struct tm t;
6835
6836 order = 0x4949;
6837 fread (tag, 4, 1, ifp);
6838 size = get4();
6839 end = ftell(ifp) + size;
6840 if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
6841 get4();
6842 while (ftell(ifp)+7 < end && !feof(ifp))
6843 parse_riff();
6844 } else if (!memcmp(tag,"nctg",4)) {
6845 while (ftell(ifp)+7 < end) {
6846 i = get2();
6847 size = get2();
6848 if ((i+1) >> 1 == 10 && size == 20)
6849 get_timestamp(0);
6850 else fseek (ifp, size, SEEK_CUR);
6851 }
6852 } else if (!memcmp(tag,"IDIT",4) && size < 64) {
6853 fread (date, 64, 1, ifp);
6854 date[size] = 0;
6855 memset (&t, 0, sizeof t);
6856 if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
6857 &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
6858 for (i=0; i < 12 && strcasecmp(mon[i],month); i++);
6859 t.tm_mon = i;
6860 t.tm_year -= 1900;
6861 if (mktime(&t) > 0)
6862 timestamp = mktime(&t);
6863 }
6864 } else
6865 fseek (ifp, size, SEEK_CUR);
6866 }
6867
parse_qt(int end)6868 void CLASS parse_qt (int end)
6869 {
6870 unsigned save, size;
6871 char tag[4];
6872
6873 order = 0x4d4d;
6874 while (ftell(ifp)+7 < end) {
6875 save = ftell(ifp);
6876 if ((size = get4()) < 8) return;
6877 fread (tag, 4, 1, ifp);
6878 if (!memcmp(tag,"moov",4) ||
6879 !memcmp(tag,"udta",4) ||
6880 !memcmp(tag,"CNTH",4))
6881 parse_qt (save+size);
6882 if (!memcmp(tag,"CNDA",4))
6883 parse_jpeg (ftell(ifp));
6884 fseek (ifp, save+size, SEEK_SET);
6885 }
6886 }
6887
parse_smal(int offset,int fsize)6888 void CLASS parse_smal (int offset, int fsize)
6889 {
6890 int ver;
6891
6892 fseek (ifp, offset+2, SEEK_SET);
6893 order = 0x4949;
6894 ver = fgetc(ifp);
6895 if (ver == 6)
6896 fseek (ifp, 5, SEEK_CUR);
6897 if (get4() != fsize) return;
6898 if (ver > 6) data_offset = get4();
6899 raw_height = height = get2();
6900 raw_width = width = get2();
6901 strcpy (make, "SMaL");
6902 sprintf (model, "v%d %dx%d", ver, width, height);
6903 if (ver == 6) load_raw = &CLASS smal_v6_load_raw;
6904 if (ver == 9) load_raw = &CLASS smal_v9_load_raw;
6905 }
6906
parse_cine()6907 void CLASS parse_cine()
6908 {
6909 unsigned off_head, off_setup, off_image, i;
6910
6911 order = 0x4949;
6912 fseek (ifp, 4, SEEK_SET);
6913 is_raw = get2() == 2;
6914 fseek (ifp, 14, SEEK_CUR);
6915 is_raw *= get4();
6916 off_head = get4();
6917 off_setup = get4();
6918 off_image = get4();
6919 timestamp = get4();
6920 if ((i = get4())) timestamp = i;
6921 fseek (ifp, off_head+4, SEEK_SET);
6922 raw_width = get4();
6923 raw_height = get4();
6924 switch (get2(),get2()) {
6925 case 8: load_raw = &CLASS eight_bit_load_raw; break;
6926 case 16: load_raw = &CLASS unpacked_load_raw;
6927 }
6928 fseek (ifp, off_setup+792, SEEK_SET);
6929 strcpy (make, "CINE");
6930 sprintf (model, "%d", get4());
6931 fseek (ifp, 12, SEEK_CUR);
6932 switch ((i=get4()) & 0xffffff) {
6933 case 3: filters = 0x94949494; break;
6934 case 4: filters = 0x49494949; break;
6935 default: is_raw = 0;
6936 }
6937 fseek (ifp, 72, SEEK_CUR);
6938 switch ((get4()+3600) % 360) {
6939 case 270: flip = 4; break;
6940 case 180: flip = 1; break;
6941 case 90: flip = 7; break;
6942 case 0: flip = 2;
6943 }
6944 cam_mul[0] = getreal(11);
6945 cam_mul[2] = getreal(11);
6946 maximum = ~(-1 << get4());
6947 fseek (ifp, 668, SEEK_CUR);
6948 shutter = get4()/1000000000.0;
6949 fseek (ifp, off_image, SEEK_SET);
6950 if (shot_select < is_raw)
6951 fseek (ifp, shot_select*8, SEEK_CUR);
6952 data_offset = (INT64) get4() + 8;
6953 data_offset += (INT64) get4() << 32;
6954 }
6955
parse_redcine()6956 void CLASS parse_redcine()
6957 {
6958 unsigned i, len, rdvo;
6959
6960 order = 0x4d4d;
6961 is_raw = 0;
6962 fseek (ifp, 52, SEEK_SET);
6963 width = get4();
6964 height = get4();
6965 fseek (ifp, 0, SEEK_END);
6966 fseek (ifp, -(i = ftello(ifp) & 511), SEEK_CUR);
6967 if (get4() != i || get4() != 0x52454f42) {
6968 fprintf (stderr,_("%s: Tail is missing, parsing from head...\n"), ifname);
6969 fseek (ifp, 0, SEEK_SET);
6970 while ((len = get4()) != EOF) {
6971 if (get4() == 0x52454456)
6972 if (is_raw++ == shot_select)
6973 data_offset = ftello(ifp) - 8;
6974 fseek (ifp, len-8, SEEK_CUR);
6975 }
6976 } else {
6977 rdvo = get4();
6978 fseek (ifp, 12, SEEK_CUR);
6979 is_raw = get4();
6980 fseeko (ifp, rdvo+8 + shot_select*4, SEEK_SET);
6981 data_offset = get4();
6982 }
6983 }
6984
foveon_gets(int offset,char * str,int len)6985 char * CLASS foveon_gets (int offset, char *str, int len)
6986 {
6987 int i;
6988 fseek (ifp, offset, SEEK_SET);
6989 for (i=0; i < len-1; i++)
6990 if ((str[i] = get2()) == 0) break;
6991 str[i] = 0;
6992 return str;
6993 }
6994
parse_foveon()6995 void CLASS parse_foveon()
6996 {
6997 int entries, img=0, off, len, tag, save, i, wide, high, pent, poff[256][2];
6998 char name[64], value[64];
6999
7000 order = 0x4949; /* Little-endian */
7001 fseek (ifp, 36, SEEK_SET);
7002 flip = get4();
7003 fseek (ifp, -4, SEEK_END);
7004 fseek (ifp, get4(), SEEK_SET);
7005 if (get4() != 0x64434553) return; /* SECd */
7006 entries = (get4(),get4());
7007 while (entries--) {
7008 off = get4();
7009 len = get4();
7010 tag = get4();
7011 save = ftell(ifp);
7012 fseek (ifp, off, SEEK_SET);
7013 if (get4() != (0x20434553 | (tag << 24))) return;
7014 switch (tag) {
7015 case 0x47414d49: /* IMAG */
7016 case 0x32414d49: /* IMA2 */
7017 fseek (ifp, 8, SEEK_CUR);
7018 pent = get4();
7019 wide = get4();
7020 high = get4();
7021 if (wide > raw_width && high > raw_height) {
7022 switch (pent) {
7023 case 5: load_flags = 1;
7024 case 6: load_raw = &CLASS foveon_sd_load_raw; break;
7025 case 30: load_raw = &CLASS foveon_dp_load_raw; break;
7026 default: load_raw = 0;
7027 }
7028 raw_width = wide;
7029 raw_height = high;
7030 data_offset = off+28;
7031 is_foveon = 1;
7032 }
7033 fseek (ifp, off+28, SEEK_SET);
7034 if (fgetc(ifp) == 0xff && fgetc(ifp) == 0xd8
7035 && thumb_length < len-28) {
7036 thumb_offset = off+28;
7037 thumb_length = len-28;
7038 write_thumb = &CLASS jpeg_thumb;
7039 }
7040 if (++img == 2 && !thumb_length) {
7041 thumb_offset = off+24;
7042 thumb_width = wide;
7043 thumb_height = high;
7044 write_thumb = &CLASS foveon_thumb;
7045 }
7046 break;
7047 case 0x464d4143: /* CAMF */
7048 meta_offset = off+8;
7049 meta_length = len-28;
7050 break;
7051 case 0x504f5250: /* PROP */
7052 pent = (get4(),get4());
7053 fseek (ifp, 12, SEEK_CUR);
7054 off += pent*8 + 24;
7055 if ((unsigned) pent > 256) pent=256;
7056 for (i=0; i < pent*2; i++)
7057 ((int *)poff)[i] = off + get4()*2;
7058 for (i=0; i < pent; i++) {
7059 foveon_gets (poff[i][0], name, 64);
7060 foveon_gets (poff[i][1], value, 64);
7061 if (!strcmp (name, "ISO"))
7062 iso_speed = atoi(value);
7063 if (!strcmp (name, "CAMMANUF"))
7064 strcpy (make, value);
7065 if (!strcmp (name, "CAMMODEL"))
7066 strcpy (model, value);
7067 if (!strcmp (name, "WB_DESC"))
7068 strcpy (model2, value);
7069 if (!strcmp (name, "TIME"))
7070 timestamp = atoi(value);
7071 if (!strcmp (name, "EXPTIME"))
7072 shutter = atoi(value) / 1000000.0;
7073 if (!strcmp (name, "APERTURE"))
7074 aperture = atof(value);
7075 if (!strcmp (name, "FLENGTH"))
7076 focal_len = atof(value);
7077 }
7078 #ifdef LOCALTIME
7079 timestamp = mktime (gmtime (×tamp));
7080 #endif
7081 }
7082 fseek (ifp, save, SEEK_SET);
7083 }
7084 }
7085
7086 /*
7087 All matrices are from Adobe DNG Converter unless otherwise noted.
7088 */
adobe_coeff(const char * make,const char * model)7089 void CLASS adobe_coeff (const char *make, const char *model)
7090 {
7091 static const struct {
7092 const char *prefix;
7093 short black, maximum, trans[12];
7094 } table[] = {
7095 { "AgfaPhoto DC-833m", 0, 0, /* DJC */
7096 { 11438,-3762,-1115,-2409,9914,2497,-1227,2295,5300 } },
7097 { "Apple QuickTake", 0, 0, /* DJC */
7098 { 21392,-5653,-3353,2406,8010,-415,7166,1427,2078 } },
7099 { "Canon EOS D2000", 0, 0,
7100 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
7101 { "Canon EOS D6000", 0, 0,
7102 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
7103 { "Canon EOS D30", 0, 0,
7104 { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
7105 { "Canon EOS D60", 0, 0xfa0,
7106 { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
7107 { "Canon EOS 5DS", 0, 0x3c96,
7108 { 6250,-711,-808,-5153,12794,2636,-1249,2198,5610 } },
7109 { "Canon EOS 5D Mark III", 0, 0x3c80,
7110 { 6722,-635,-963,-4287,12460,2028,-908,2162,5668 } },
7111 { "Canon EOS 5D Mark II", 0, 0x3cf0,
7112 { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
7113 { "Canon EOS 5D", 0, 0xe6c,
7114 { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
7115 { "Canon EOS 6D", 0, 0x3c82,
7116 { 7034,-804,-1014,-4420,12564,2058,-851,1994,5758 } },
7117 { "Canon EOS 7D Mark II", 0, 0x3510,
7118 { 7268,-1082,-969,-4186,11839,2663,-825,2029,5839 } },
7119 { "Canon EOS 7D", 0, 0x3510,
7120 { 6844,-996,-856,-3876,11761,2396,-593,1772,6198 } },
7121 { "Canon EOS 10D", 0, 0xfa0,
7122 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
7123 { "Canon EOS 20Da", 0, 0,
7124 { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
7125 { "Canon EOS 20D", 0, 0xfff,
7126 { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
7127 { "Canon EOS 30D", 0, 0,
7128 { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
7129 { "Canon EOS 40D", 0, 0x3f60,
7130 { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
7131 { "Canon EOS 50D", 0, 0x3d93,
7132 { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
7133 { "Canon EOS 60D", 0, 0x2ff7,
7134 { 6719,-994,-925,-4408,12426,2211,-887,2129,6051 } },
7135 { "Canon EOS 70D", 0, 0x3bc7,
7136 { 7034,-804,-1014,-4420,12564,2058,-851,1994,5758 } },
7137 { "Canon EOS 100D", 0, 0x350f,
7138 { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
7139 { "Canon EOS 300D", 0, 0xfa0,
7140 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
7141 { "Canon EOS 350D", 0, 0xfff,
7142 { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
7143 { "Canon EOS 400D", 0, 0xe8e,
7144 { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
7145 { "Canon EOS 450D", 0, 0x390d,
7146 { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
7147 { "Canon EOS 500D", 0, 0x3479,
7148 { 4763,712,-646,-6821,14399,2640,-1921,3276,6561 } },
7149 { "Canon EOS 550D", 0, 0x3dd7,
7150 { 6941,-1164,-857,-3825,11597,2534,-416,1540,6039 } },
7151 { "Canon EOS 600D", 0, 0x3510,
7152 { 6461,-907,-882,-4300,12184,2378,-819,1944,5931 } },
7153 { "Canon EOS 650D", 0, 0x354d,
7154 { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
7155 { "Canon EOS 700D", 0, 0x3c00,
7156 { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
7157 { "Canon EOS 750D", 0, 0x368e,
7158 { 6362,-823,-847,-4426,12109,2616,-743,1857,5635 } },
7159 { "Canon EOS 760D", 0, 0x350f,
7160 { 6362,-823,-847,-4426,12109,2616,-743,1857,5635 } },
7161 { "Canon EOS 1000D", 0, 0xe43,
7162 { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
7163 { "Canon EOS 1100D", 0, 0x3510,
7164 { 6444,-904,-893,-4563,12308,2535,-903,2016,6728 } },
7165 { "Canon EOS 1200D", 0, 0x37c2,
7166 { 6461,-907,-882,-4300,12184,2378,-819,1944,5931 } },
7167 { "Canon EOS M3", 0, 0,
7168 { 6362,-823,-847,-4426,12109,2616,-743,1857,5635 } },
7169 { "Canon EOS M", 0, 0,
7170 { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
7171 { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
7172 { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
7173 { "Canon EOS-1Ds Mark II", 0, 0xe80,
7174 { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
7175 { "Canon EOS-1D Mark IV", 0, 0x3bb0,
7176 { 6014,-220,-795,-4109,12014,2361,-561,1824,5787 } },
7177 { "Canon EOS-1D Mark III", 0, 0x3bb0,
7178 { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
7179 { "Canon EOS-1D Mark II N", 0, 0xe80,
7180 { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
7181 { "Canon EOS-1D Mark II", 0, 0xe80,
7182 { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
7183 { "Canon EOS-1DS", 0, 0xe20,
7184 { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
7185 { "Canon EOS-1D C", 0, 0x3c4e,
7186 { 6847,-614,-1014,-4669,12737,2139,-1197,2488,6846 } },
7187 { "Canon EOS-1D X", 0, 0x3c4e,
7188 { 6847,-614,-1014,-4669,12737,2139,-1197,2488,6846 } },
7189 { "Canon EOS-1D", 0, 0xe20,
7190 { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
7191 { "Canon EOS C500", 853, 0, /* DJC */
7192 { 17851,-10604,922,-7425,16662,763,-3660,3636,22278 } },
7193 { "Canon PowerShot A530", 0, 0,
7194 { 0 } }, /* don't want the A5 matrix */
7195 { "Canon PowerShot A50", 0, 0,
7196 { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
7197 { "Canon PowerShot A5", 0, 0,
7198 { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
7199 { "Canon PowerShot G10", 0, 0,
7200 { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
7201 { "Canon PowerShot G11", 0, 0,
7202 { 12177,-4817,-1069,-1612,9864,2049,-98,850,4471 } },
7203 { "Canon PowerShot G12", 0, 0,
7204 { 13244,-5501,-1248,-1508,9858,1935,-270,1083,4366 } },
7205 { "Canon PowerShot G15", 0, 0,
7206 { 7474,-2301,-567,-4056,11456,2975,-222,716,4181 } },
7207 { "Canon PowerShot G16", 0, 0,
7208 { 8020,-2687,-682,-3704,11879,2052,-965,1921,5556 } },
7209 { "Canon PowerShot G1 X", 0, 0,
7210 { 7378,-1255,-1043,-4088,12251,2048,-876,1946,5805 } },
7211 { "Canon PowerShot G1", 0, 0,
7212 { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
7213 { "Canon PowerShot G2", 0, 0,
7214 { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
7215 { "Canon PowerShot G3", 0, 0,
7216 { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
7217 { "Canon PowerShot G5", 0, 0,
7218 { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
7219 { "Canon PowerShot G6", 0, 0,
7220 { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
7221 { "Canon PowerShot G7 X", 0, 0,
7222 { 9602,-3823,-937,-2984,11495,1675,-407,1415,5049 } },
7223 { "Canon PowerShot G9", 0, 0,
7224 { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
7225 { "Canon PowerShot Pro1", 0, 0,
7226 { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
7227 { "Canon PowerShot Pro70", 34, 0,
7228 { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
7229 { "Canon PowerShot Pro90", 0, 0,
7230 { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
7231 { "Canon PowerShot S30", 0, 0,
7232 { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
7233 { "Canon PowerShot S40", 0, 0,
7234 { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
7235 { "Canon PowerShot S45", 0, 0,
7236 { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
7237 { "Canon PowerShot S50", 0, 0,
7238 { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
7239 { "Canon PowerShot S60", 0, 0,
7240 { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
7241 { "Canon PowerShot S70", 0, 0,
7242 { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
7243 { "Canon PowerShot S90", 0, 0,
7244 { 12374,-5016,-1049,-1677,9902,2078,-83,852,4683 } },
7245 { "Canon PowerShot S95", 0, 0,
7246 { 13440,-5896,-1279,-1236,9598,1931,-180,1001,4651 } },
7247 { "Canon PowerShot S100", 0, 0,
7248 { 7968,-2565,-636,-2873,10697,2513,180,667,4211 } },
7249 { "Canon PowerShot S110", 0, 0,
7250 { 8039,-2643,-654,-3783,11230,2930,-206,690,4194 } },
7251 { "Canon PowerShot S120", 0, 0,
7252 { 6961,-1685,-695,-4625,12945,1836,-1114,2152,5518 } },
7253 { "Canon PowerShot SX1 IS", 0, 0,
7254 { 6578,-259,-502,-5974,13030,3309,-308,1058,4970 } },
7255 { "Canon PowerShot SX50 HS", 0, 0,
7256 { 12432,-4753,-1247,-2110,10691,1629,-412,1623,4926 } },
7257 { "Canon PowerShot SX60 HS", 0, 0,
7258 { 13161,-5451,-1344,-1989,10654,1531,-47,1271,4955 } },
7259 { "Canon PowerShot A3300", 0, 0, /* DJC */
7260 { 10826,-3654,-1023,-3215,11310,1906,0,999,4960 } },
7261 { "Canon PowerShot A470", 0, 0, /* DJC */
7262 { 12513,-4407,-1242,-2680,10276,2405,-878,2215,4734 } },
7263 { "Canon PowerShot A610", 0, 0, /* DJC */
7264 { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
7265 { "Canon PowerShot A620", 0, 0, /* DJC */
7266 { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
7267 { "Canon PowerShot A630", 0, 0, /* DJC */
7268 { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
7269 { "Canon PowerShot A640", 0, 0, /* DJC */
7270 { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
7271 { "Canon PowerShot A650", 0, 0, /* DJC */
7272 { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
7273 { "Canon PowerShot A720", 0, 0, /* DJC */
7274 { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
7275 { "Canon PowerShot S3 IS", 0, 0, /* DJC */
7276 { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
7277 { "Canon PowerShot SX110 IS", 0, 0, /* DJC */
7278 { 14134,-5576,-1527,-1991,10719,1273,-1158,1929,3581 } },
7279 { "Canon PowerShot SX220", 0, 0, /* DJC */
7280 { 13898,-5076,-1447,-1405,10109,1297,-244,1860,3687 } },
7281 { "Casio EX-S20", 0, 0, /* DJC */
7282 { 11634,-3924,-1128,-4968,12954,2015,-1588,2648,7206 } },
7283 { "Casio EX-Z750", 0, 0, /* DJC */
7284 { 10819,-3873,-1099,-4903,13730,1175,-1755,3751,4632 } },
7285 { "Casio EX-Z10", 128, 0xfff, /* DJC */
7286 { 9790,-3338,-603,-2321,10222,2099,-344,1273,4799 } },
7287 { "CINE 650", 0, 0,
7288 { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
7289 { "CINE 660", 0, 0,
7290 { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
7291 { "CINE", 0, 0,
7292 { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
7293 { "Contax N Digital", 0, 0xf1e,
7294 { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
7295 { "Epson R-D1", 0, 0,
7296 { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
7297 { "Fujifilm E550", 0, 0,
7298 { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
7299 { "Fujifilm E900", 0, 0,
7300 { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
7301 { "Fujifilm F5", 0, 0,
7302 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7303 { "Fujifilm F6", 0, 0,
7304 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7305 { "Fujifilm F77", 0, 0xfe9,
7306 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7307 { "Fujifilm F7", 0, 0,
7308 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
7309 { "Fujifilm F8", 0, 0,
7310 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7311 { "Fujifilm S100FS", 514, 0,
7312 { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
7313 { "Fujifilm S1", 0, 0,
7314 { 12297,-4882,-1202,-2106,10691,1623,-88,1312,4790 } },
7315 { "Fujifilm S20Pro", 0, 0,
7316 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
7317 { "Fujifilm S20", 512, 0x3fff,
7318 { 11401,-4498,-1312,-5088,12751,2613,-838,1568,5941 } },
7319 { "Fujifilm S2Pro", 128, 0,
7320 { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
7321 { "Fujifilm S3Pro", 0, 0,
7322 { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
7323 { "Fujifilm S5Pro", 0, 0,
7324 { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
7325 { "Fujifilm S5000", 0, 0,
7326 { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
7327 { "Fujifilm S5100", 0, 0,
7328 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
7329 { "Fujifilm S5500", 0, 0,
7330 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
7331 { "Fujifilm S5200", 0, 0,
7332 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
7333 { "Fujifilm S5600", 0, 0,
7334 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
7335 { "Fujifilm S6", 0, 0,
7336 { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
7337 { "Fujifilm S7000", 0, 0,
7338 { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
7339 { "Fujifilm S9000", 0, 0,
7340 { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
7341 { "Fujifilm S9500", 0, 0,
7342 { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
7343 { "Fujifilm S9100", 0, 0,
7344 { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
7345 { "Fujifilm S9600", 0, 0,
7346 { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
7347 { "Fujifilm SL1000", 0, 0,
7348 { 11705,-4262,-1107,-2282,10791,1709,-555,1713,4945 } },
7349 { "Fujifilm IS-1", 0, 0,
7350 { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
7351 { "Fujifilm IS Pro", 0, 0,
7352 { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
7353 { "Fujifilm HS10 HS11", 0, 0xf68,
7354 { 12440,-3954,-1183,-1123,9674,1708,-83,1614,4086 } },
7355 { "Fujifilm HS2", 0, 0,
7356 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7357 { "Fujifilm HS3", 0, 0,
7358 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7359 { "Fujifilm HS50EXR", 0, 0,
7360 { 12085,-4727,-953,-3257,11489,2002,-511,2046,4592 } },
7361 { "Fujifilm F900EXR", 0, 0,
7362 { 12085,-4727,-953,-3257,11489,2002,-511,2046,4592 } },
7363 { "Fujifilm X100S", 0, 0,
7364 { 10592,-4262,-1008,-3514,11355,2465,-870,2025,6386 } },
7365 { "Fujifilm X100T", 0, 0,
7366 { 10592,-4262,-1008,-3514,11355,2465,-870,2025,6386 } },
7367 { "Fujifilm X100", 0, 0,
7368 { 12161,-4457,-1069,-5034,12874,2400,-795,1724,6904 } },
7369 { "Fujifilm X10", 0, 0,
7370 { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
7371 { "Fujifilm X20", 0, 0,
7372 { 11768,-4971,-1133,-4904,12927,2183,-480,1723,4605 } },
7373 { "Fujifilm X30", 0, 0,
7374 { 12328,-5256,-1144,-4469,12927,1675,-87,1291,4351 } },
7375 { "Fujifilm X-Pro1", 0, 0,
7376 { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
7377 { "Fujifilm X-A1", 0, 0,
7378 { 11086,-4555,-839,-3512,11310,2517,-815,1341,5940 } },
7379 { "Fujifilm X-A2", 0, 0,
7380 { 10763,-4560,-917,-3346,11311,2322,-475,1135,5843 } },
7381 { "Fujifilm X-E1", 0, 0,
7382 { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
7383 { "Fujifilm X-E2", 0, 0,
7384 { 8458,-2451,-855,-4597,12447,2407,-1475,2482,6526 } },
7385 { "Fujifilm X-M1", 0, 0,
7386 { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
7387 { "Fujifilm X-S1", 0, 0,
7388 { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
7389 { "Fujifilm X-T1", 0, 0,
7390 { 8458,-2451,-855,-4597,12447,2407,-1475,2482,6526 } },
7391 { "Fujifilm XF1", 0, 0,
7392 { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
7393 { "Fujifilm XQ", 0, 0, // XQ1 and XQ2
7394 { 9252,-2704,-1064,-5893,14265,1717,-1101,2341,4349 } },
7395 { "Imacon Ixpress", 0, 0, /* DJC */
7396 { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
7397 { "Kodak NC2000", 0, 0,
7398 { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
7399 { "Kodak DCS315C", 8, 0,
7400 { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
7401 { "Kodak DCS330C", 8, 0,
7402 { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
7403 { "Kodak DCS420", 0, 0,
7404 { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
7405 { "Kodak DCS460", 0, 0,
7406 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
7407 { "Kodak EOSDCS1", 0, 0,
7408 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
7409 { "Kodak EOSDCS3B", 0, 0,
7410 { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
7411 { "Kodak DCS520C", 178, 0,
7412 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
7413 { "Kodak DCS560C", 177, 0,
7414 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
7415 { "Kodak DCS620C", 177, 0,
7416 { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
7417 { "Kodak DCS620X", 176, 0,
7418 { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
7419 { "Kodak DCS660C", 173, 0,
7420 { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
7421 { "Kodak DCS720X", 0, 0,
7422 { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
7423 { "Kodak DCS760C", 0, 0,
7424 { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
7425 { "Kodak DCS Pro SLR", 0, 0,
7426 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
7427 { "Kodak DCS Pro 14nx", 0, 0,
7428 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
7429 { "Kodak DCS Pro 14", 0, 0,
7430 { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
7431 { "Kodak ProBack645", 0, 0,
7432 { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
7433 { "Kodak ProBack", 0, 0,
7434 { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
7435 { "Kodak P712", 0, 0,
7436 { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
7437 { "Kodak P850", 0, 0xf7c,
7438 { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
7439 { "Kodak P880", 0, 0xfff,
7440 { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
7441 { "Kodak EasyShare Z980", 0, 0,
7442 { 11313,-3559,-1101,-3893,11891,2257,-1214,2398,4908 } },
7443 { "Kodak EasyShare Z981", 0, 0,
7444 { 12729,-4717,-1188,-1367,9187,2582,274,860,4411 } },
7445 { "Kodak EasyShare Z990", 0, 0xfed,
7446 { 11749,-4048,-1309,-1867,10572,1489,-138,1449,4522 } },
7447 { "Kodak EASYSHARE Z1015", 0, 0xef1,
7448 { 11265,-4286,-992,-4694,12343,2647,-1090,1523,5447 } },
7449 { "Leaf CMost", 0, 0,
7450 { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
7451 { "Leaf Valeo 6", 0, 0,
7452 { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
7453 { "Leaf Aptus 54S", 0, 0,
7454 { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
7455 { "Leaf Aptus 65", 0, 0,
7456 { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
7457 { "Leaf Aptus 75", 0, 0,
7458 { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
7459 { "Leaf", 0, 0,
7460 { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
7461 { "Mamiya ZD", 0, 0,
7462 { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
7463 { "Micron 2010", 110, 0, /* DJC */
7464 { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
7465 { "Minolta DiMAGE 5", 0, 0xf7d,
7466 { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
7467 { "Minolta DiMAGE 7Hi", 0, 0xf7d,
7468 { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
7469 { "Minolta DiMAGE 7", 0, 0xf7d,
7470 { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
7471 { "Minolta DiMAGE A1", 0, 0xf8b,
7472 { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
7473 { "Minolta DiMAGE A200", 0, 0,
7474 { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
7475 { "Minolta DiMAGE A2", 0, 0xf8f,
7476 { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
7477 { "Minolta DiMAGE Z2", 0, 0, /* DJC */
7478 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
7479 { "Minolta DYNAX 5", 0, 0xffb,
7480 { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
7481 { "Minolta DYNAX 7", 0, 0xffb,
7482 { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
7483 { "Motorola PIXL", 0, 0, /* DJC */
7484 { 8898,-989,-1033,-3292,11619,1674,-661,3178,5216 } },
7485 { "Nikon D100", 0, 0,
7486 { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
7487 { "Nikon D1H", 0, 0,
7488 { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
7489 { "Nikon D1X", 0, 0,
7490 { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
7491 { "Nikon D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
7492 { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
7493 { "Nikon D200", 0, 0xfbc,
7494 { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
7495 { "Nikon D2H", 0, 0,
7496 { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
7497 { "Nikon D2X", 0, 0,
7498 { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
7499 { "Nikon D3000", 0, 0,
7500 { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
7501 { "Nikon D3100", 0, 0,
7502 { 7911,-2167,-813,-5327,13150,2408,-1288,2483,7968 } },
7503 { "Nikon D3200", 0, 0xfb9,
7504 { 7013,-1408,-635,-5268,12902,2640,-1470,2801,7379 } },
7505 { "Nikon D3300", 0, 0,
7506 { 6988,-1384,-714,-5631,13410,2447,-1485,2204,7318 } },
7507 { "Nikon D300", 0, 0,
7508 { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
7509 { "Nikon D3X", 0, 0,
7510 { 7171,-1986,-648,-8085,15555,2718,-2170,2512,7457 } },
7511 { "Nikon D3S", 0, 0,
7512 { 8828,-2406,-694,-4874,12603,2541,-660,1509,7587 } },
7513 { "Nikon D3", 0, 0,
7514 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
7515 { "Nikon D40X", 0, 0,
7516 { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
7517 { "Nikon D40", 0, 0,
7518 { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
7519 { "Nikon D4S", 0, 0,
7520 { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
7521 { "Nikon D4", 0, 0,
7522 { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
7523 { "Nikon Df", 0, 0,
7524 { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
7525 { "Nikon D5000", 0, 0xf00,
7526 { 7309,-1403,-519,-8474,16008,2622,-2433,2826,8064 } },
7527 { "Nikon D5100", 0, 0x3de6,
7528 { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
7529 { "Nikon D5200", 0, 0,
7530 { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
7531 { "Nikon D5300", 0, 0,
7532 { 6988,-1384,-714,-5631,13410,2447,-1485,2204,7318 } },
7533 { "Nikon D5500", 0, 0,
7534 { 8821,-2938,-785,-4178,12142,2287,-824,1651,6860 } },
7535 { "Nikon D50", 0, 0,
7536 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
7537 { "Nikon D600", 0, 0x3e07,
7538 { 8178,-2245,-609,-4857,12394,2776,-1207,2086,7298 } },
7539 { "Nikon D610", 0, 0,
7540 { 8178,-2245,-609,-4857,12394,2776,-1207,2086,7298 } },
7541 { "Nikon D60", 0, 0,
7542 { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
7543 { "Nikon D7000", 0, 0,
7544 { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
7545 { "Nikon D7100", 0, 0,
7546 { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
7547 { "Nikon D7200", 0, 0,
7548 { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
7549 { "Nikon D750", 0, 0,
7550 { 9020,-2890,-715,-4535,12436,2348,-934,1919,7086 } },
7551 { "Nikon D700", 0, 0,
7552 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
7553 { "Nikon D70", 0, 0,
7554 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
7555 { "Nikon D810", 0, 0,
7556 { 9369,-3195,-791,-4488,12430,2301,-893,1796,6872 } },
7557 { "Nikon D800", 0, 0,
7558 { 7866,-2108,-555,-4869,12483,2681,-1176,2069,7501 } },
7559 { "Nikon D80", 0, 0,
7560 { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
7561 { "Nikon D90", 0, 0xf00,
7562 { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
7563 { "Nikon E700", 0, 0x3dd, /* DJC */
7564 { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
7565 { "Nikon E800", 0, 0x3dd, /* DJC */
7566 { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
7567 { "Nikon E950", 0, 0x3dd, /* DJC */
7568 { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
7569 { "Nikon E995", 0, 0, /* copied from E5000 */
7570 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
7571 { "Nikon E2100", 0, 0, /* copied from Z2, new white balance */
7572 { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711} },
7573 { "Nikon E2500", 0, 0,
7574 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
7575 { "Nikon E3200", 0, 0, /* DJC */
7576 { 9846,-2085,-1019,-3278,11109,2170,-774,2134,5745 } },
7577 { "Nikon E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
7578 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
7579 { "Nikon E4500", 0, 0,
7580 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
7581 { "Nikon E5000", 0, 0,
7582 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
7583 { "Nikon E5400", 0, 0,
7584 { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
7585 { "Nikon E5700", 0, 0,
7586 { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
7587 { "Nikon E8400", 0, 0,
7588 { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
7589 { "Nikon E8700", 0, 0,
7590 { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
7591 { "Nikon E8800", 0, 0,
7592 { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
7593 { "Nikon COOLPIX A", 0, 0,
7594 { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
7595 { "Nikon COOLPIX P330", 200, 0,
7596 { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
7597 { "Nikon COOLPIX P340", 200, 0,
7598 { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
7599 { "Nikon COOLPIX P6000", 0, 0,
7600 { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
7601 { "Nikon COOLPIX P7000", 0, 0,
7602 { 11432,-3679,-1111,-3169,11239,2202,-791,1380,4455 } },
7603 { "Nikon COOLPIX P7100", 0, 0,
7604 { 11053,-4269,-1024,-1976,10182,2088,-526,1263,4469 } },
7605 { "Nikon COOLPIX P7700", 200, 0,
7606 { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
7607 { "Nikon COOLPIX P7800", 200, 0,
7608 { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
7609 { "Nikon 1 V3", 0, 0,
7610 { 5958,-1559,-571,-4021,11453,2939,-634,1548,5087 } },
7611 { "Nikon 1 J4", 0, 0,
7612 { 5958,-1559,-571,-4021,11453,2939,-634,1548,5087 } },
7613 { "Nikon 1 J5", 0, 0, /* DJC */
7614 { 2621,-856,500,-4471,8761,5711,-1321,2644,11945 } },
7615 { "Nikon 1 S2", 200, 0,
7616 { 6612,-1342,-618,-3338,11055,2623,-174,1792,5075 } },
7617 { "Nikon 1 V2", 0, 0,
7618 { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
7619 { "Nikon 1 J3", 0, 0,
7620 { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
7621 { "Nikon 1 AW1", 0, 0,
7622 { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
7623 { "Nikon 1 ", 0, 0, /* J1, J2, S1, V1 */
7624 { 8994,-2667,-865,-4594,12324,2552,-699,1786,6260 } },
7625 { "Olympus C5050", 0, 0,
7626 { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
7627 { "Olympus C5060", 0, 0,
7628 { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
7629 { "Olympus C7070", 0, 0,
7630 { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
7631 { "Olympus C70", 0, 0,
7632 { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
7633 { "Olympus C80", 0, 0,
7634 { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
7635 { "Olympus E-10", 0, 0xffc,
7636 { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
7637 { "Olympus E-1", 0, 0,
7638 { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
7639 { "Olympus E-20", 0, 0xffc,
7640 { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
7641 { "Olympus E-300", 0, 0,
7642 { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
7643 { "Olympus E-330", 0, 0,
7644 { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
7645 { "Olympus E-30", 0, 0xfbc,
7646 { 8144,-1861,-1111,-7763,15894,1929,-1865,2542,7607 } },
7647 { "Olympus E-3", 0, 0xf99,
7648 { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
7649 { "Olympus E-400", 0, 0,
7650 { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
7651 { "Olympus E-410", 0, 0xf6a,
7652 { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
7653 { "Olympus E-420", 0, 0xfd7,
7654 { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
7655 { "Olympus E-450", 0, 0xfd2,
7656 { 8745,-2425,-1095,-7594,15613,2073,-1780,2309,7416 } },
7657 { "Olympus E-500", 0, 0,
7658 { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
7659 { "Olympus E-510", 0, 0xf6a,
7660 { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
7661 { "Olympus E-520", 0, 0xfd2,
7662 { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
7663 { "Olympus E-5", 0, 0xeec,
7664 { 11200,-3783,-1325,-4576,12593,2206,-695,1742,7504 } },
7665 { "Olympus E-600", 0, 0xfaf,
7666 { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
7667 { "Olympus E-620", 0, 0xfaf,
7668 { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
7669 { "Olympus E-P1", 0, 0xffd,
7670 { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
7671 { "Olympus E-P2", 0, 0xffd,
7672 { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
7673 { "Olympus E-P3", 0, 0,
7674 { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
7675 { "Olympus E-P5", 0, 0,
7676 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7677 { "Olympus E-PL1s", 0, 0,
7678 { 11409,-3872,-1393,-4572,12757,2003,-709,1810,7415 } },
7679 { "Olympus E-PL1", 0, 0,
7680 { 11408,-4289,-1215,-4286,12385,2118,-387,1467,7787 } },
7681 { "Olympus E-PL2", 0, 0xcf3,
7682 { 15030,-5552,-1806,-3987,12387,1767,-592,1670,7023 } },
7683 { "Olympus E-PL3", 0, 0,
7684 { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
7685 { "Olympus E-PL5", 0, 0xfcb,
7686 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7687 { "Olympus E-PL6", 0, 0,
7688 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7689 { "Olympus E-PL7", 0, 0,
7690 { 9197,-3190,-659,-2606,10830,2039,-458,1250,5458 } },
7691 { "Olympus E-PM1", 0, 0,
7692 { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
7693 { "Olympus E-PM2", 0, 0,
7694 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7695 { "Olympus E-M10", 0, 0,
7696 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7697 { "Olympus E-M1", 0, 0,
7698 { 7687,-1984,-606,-4327,11928,2721,-1381,2339,6452 } },
7699 { "Olympus E-M5MarkII", 0, 0,
7700 { 9422,-3258,-711,-2655,10898,2015,-512,1354,5512 } },
7701 { "Olympus E-M5", 0, 0xfe1,
7702 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7703 { "Olympus SH-2", 0, 0,
7704 { 10156,-3425,-1077,-2611,11177,1624,-385,1592,5080 } },
7705 { "Olympus SP350", 0, 0,
7706 { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
7707 { "Olympus SP3", 0, 0,
7708 { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
7709 { "Olympus SP500UZ", 0, 0xfff,
7710 { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
7711 { "Olympus SP510UZ", 0, 0xffe,
7712 { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
7713 { "Olympus SP550UZ", 0, 0xffe,
7714 { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
7715 { "Olympus SP560UZ", 0, 0xff9,
7716 { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
7717 { "Olympus SP570UZ", 0, 0,
7718 { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
7719 { "Olympus STYLUS1", 0, 0,
7720 { 8360,-2420,-880,-3928,12353,1739,-1381,2416,5173 } },
7721 { "Olympus TG-4", 0, 0,
7722 { 11426,-4159,-1126,-2066,10678,1593,-120,1327,4998 } },
7723 { "Olympus XZ-10", 0, 0,
7724 { 9777,-3483,-925,-2886,11297,1800,-602,1663,5134 } },
7725 { "Olympus XZ-1", 0, 0,
7726 { 10901,-4095,-1074,-1141,9208,2293,-62,1417,5158 } },
7727 { "Olympus XZ-2", 0, 0,
7728 { 9777,-3483,-925,-2886,11297,1800,-602,1663,5134 } },
7729 { "OmniVision", 0, 0, /* DJC */
7730 { 12782,-4059,-379,-478,9066,1413,1340,1513,5176 } },
7731 { "Pentax *ist DL2", 0, 0,
7732 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
7733 { "Pentax *ist DL", 0, 0,
7734 { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
7735 { "Pentax *ist DS2", 0, 0,
7736 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
7737 { "Pentax *ist DS", 0, 0,
7738 { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
7739 { "Pentax *ist D", 0, 0,
7740 { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
7741 { "Pentax K10D", 0, 0,
7742 { 9566,-2863,-803,-7170,15172,2112,-818,803,9705 } },
7743 { "Pentax K1", 0, 0,
7744 { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
7745 { "Pentax K20D", 0, 0,
7746 { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
7747 { "Pentax K200D", 0, 0,
7748 { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
7749 { "Pentax K2000", 0, 0,
7750 { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
7751 { "Pentax K-m", 0, 0,
7752 { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
7753 { "Pentax K-x", 0, 0,
7754 { 8843,-2837,-625,-5025,12644,2668,-411,1234,7410 } },
7755 { "Pentax K-r", 0, 0,
7756 { 9895,-3077,-850,-5304,13035,2521,-883,1768,6936 } },
7757 { "Pentax K-3", 0, 0,
7758 { 7415,-2052,-721,-5186,12788,2682,-1446,2157,6773 } },
7759 { "Pentax K-5 II", 0, 0,
7760 { 8170,-2725,-639,-4440,12017,2744,-771,1465,6599 } },
7761 { "Pentax K-5", 0, 0,
7762 { 8713,-2833,-743,-4342,11900,2772,-722,1543,6247 } },
7763 { "Pentax K-7", 0, 0,
7764 { 9142,-2947,-678,-8648,16967,1663,-2224,2898,8615 } },
7765 { "Pentax K-S1", 0, 0,
7766 { 8512,-3211,-787,-4167,11966,2487,-638,1288,6054 } },
7767 { "Pentax K-S2", 0, 0, /* DJC */
7768 { 5322,-2195,41,-3689,8902,4788,-858,1831,7969 } },
7769 { "Pentax Q-S1", 0, 0,
7770 { 12995,-5593,-1107,-1879,10139,2027,-64,1233,4919 } },
7771 { "Pentax 645D", 0, 0x3e00,
7772 { 10646,-3593,-1158,-3329,11699,1831,-667,2874,6287 } },
7773 { "Panasonic DMC-CM1", 15, 0,
7774 { 8770,-3194,-820,-2871,11281,1803,-513,1552,4434 } },
7775 { "Panasonic DMC-FZ8", 0, 0xf7f,
7776 { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
7777 { "Panasonic DMC-FZ18", 0, 0,
7778 { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
7779 { "Panasonic DMC-FZ28", 15, 0xf96,
7780 { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
7781 { "Panasonic DMC-FZ30", 0, 0xf94,
7782 { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
7783 { "Panasonic DMC-FZ3", 15, 0,
7784 { 9938,-2780,-890,-4604,12393,2480,-1117,2304,4620 } },
7785 { "Panasonic DMC-FZ4", 15, 0,
7786 { 13639,-5535,-1371,-1698,9633,2430,316,1152,4108 } },
7787 { "Panasonic DMC-FZ50", 0, 0,
7788 { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
7789 { "Panasonic DMC-FZ7", 15, 0,
7790 { 11532,-4324,-1066,-2375,10847,1749,-564,1699,4351 } },
7791 { "Leica V-LUX1", 0, 0,
7792 { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
7793 { "Panasonic DMC-L10", 15, 0xf96,
7794 { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
7795 { "Panasonic DMC-L1", 0, 0xf7f,
7796 { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
7797 { "Leica DIGILUX 3", 0, 0xf7f,
7798 { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
7799 { "Panasonic DMC-LC1", 0, 0,
7800 { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
7801 { "Leica DIGILUX 2", 0, 0,
7802 { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
7803 { "Panasonic DMC-LX100", 15, 0,
7804 { 8844,-3538,-768,-3709,11762,2200,-698,1792,5220 } },
7805 { "Leica D-LUX (Typ 109)", 15, 0,
7806 { 8844,-3538,-768,-3709,11762,2200,-698,1792,5220 } },
7807 { "Panasonic DMC-LF1", 15, 0,
7808 { 9379,-3267,-816,-3227,11560,1881,-926,1928,5340 } },
7809 { "Leica C (Typ 112)", 15, 0,
7810 { 9379,-3267,-816,-3227,11560,1881,-926,1928,5340 } },
7811 { "Panasonic DMC-LX1", 0, 0xf7f,
7812 { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
7813 { "Leica D-LUX2", 0, 0xf7f,
7814 { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
7815 { "Panasonic DMC-LX2", 0, 0,
7816 { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
7817 { "Leica D-LUX3", 0, 0,
7818 { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
7819 { "Panasonic DMC-LX3", 15, 0,
7820 { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
7821 { "Leica D-LUX 4", 15, 0,
7822 { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
7823 { "Panasonic DMC-LX5", 15, 0,
7824 { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
7825 { "Leica D-LUX 5", 15, 0,
7826 { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
7827 { "Panasonic DMC-LX7", 15, 0,
7828 { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
7829 { "Leica D-LUX 6", 15, 0,
7830 { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
7831 { "Panasonic DMC-FZ1000", 15, 0,
7832 { 7830,-2696,-763,-3325,11667,1866,-641,1712,4824 } },
7833 { "Leica V-LUX (Typ 114)", 15, 0,
7834 { 7830,-2696,-763,-3325,11667,1866,-641,1712,4824 } },
7835 { "Panasonic DMC-FZ100", 15, 0xfff,
7836 { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
7837 { "Leica V-LUX 2", 15, 0xfff,
7838 { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
7839 { "Panasonic DMC-FZ150", 15, 0xfff,
7840 { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
7841 { "Leica V-LUX 3", 15, 0xfff,
7842 { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
7843 { "Panasonic DMC-FZ200", 15, 0xfff,
7844 { 8112,-2563,-740,-3730,11784,2197,-941,2075,4933 } },
7845 { "Leica V-LUX 4", 15, 0xfff,
7846 { 8112,-2563,-740,-3730,11784,2197,-941,2075,4933 } },
7847 { "Panasonic DMC-FX150", 15, 0xfff,
7848 { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
7849 { "Panasonic DMC-G10", 0, 0,
7850 { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
7851 { "Panasonic DMC-G1", 15, 0xf94,
7852 { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
7853 { "Panasonic DMC-G2", 15, 0xf3c,
7854 { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
7855 { "Panasonic DMC-G3", 15, 0xfff,
7856 { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
7857 { "Panasonic DMC-G5", 15, 0xfff,
7858 { 7798,-2562,-740,-3879,11584,2613,-1055,2248,5434 } },
7859 { "Panasonic DMC-G6", 15, 0xfff,
7860 { 8294,-2891,-651,-3869,11590,2595,-1183,2267,5352 } },
7861 { "Panasonic DMC-GF1", 15, 0xf92,
7862 { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
7863 { "Panasonic DMC-GF2", 15, 0xfff,
7864 { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
7865 { "Panasonic DMC-GF3", 15, 0xfff,
7866 { 9051,-2468,-1204,-5212,13276,2121,-1197,2510,6890 } },
7867 { "Panasonic DMC-GF5", 15, 0xfff,
7868 { 8228,-2945,-660,-3938,11792,2430,-1094,2278,5793 } },
7869 { "Panasonic DMC-GF6", 15, 0,
7870 { 8130,-2801,-946,-3520,11289,2552,-1314,2511,5791 } },
7871 { "Panasonic DMC-GF7", 15, 0,
7872 { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
7873 { "Panasonic DMC-GH1", 15, 0xf92,
7874 { 6299,-1466,-532,-6535,13852,2969,-2331,3112,5984 } },
7875 { "Panasonic DMC-GH2", 15, 0xf95,
7876 { 7780,-2410,-806,-3913,11724,2484,-1018,2390,5298 } },
7877 { "Panasonic DMC-GH3", 15, 0,
7878 { 6559,-1752,-491,-3672,11407,2586,-962,1875,5130 } },
7879 { "Panasonic DMC-GH4", 15, 0,
7880 { 7122,-2108,-512,-3155,11201,2231,-541,1423,5045 } },
7881 { "Panasonic DMC-GM1", 15, 0,
7882 { 6770,-1895,-744,-5232,13145,2303,-1664,2691,5703 } },
7883 { "Panasonic DMC-GM5", 15, 0,
7884 { 8238,-3244,-679,-3921,11814,2384,-836,2022,5852 } },
7885 { "Panasonic DMC-GX1", 15, 0,
7886 { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
7887 { "Panasonic DMC-GX7", 15, 0,
7888 { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
7889 { "Panasonic DMC-TZ6", 15, 0,
7890 { 8607,-2822,-808,-3755,11930,2049,-820,2060,5224 } },
7891 { "Panasonic DMC-ZS4", 15, 0,
7892 { 8607,-2822,-808,-3755,11930,2049,-820,2060,5224 } },
7893 { "Panasonic DMC-TZ7", 15, 0,
7894 { 8802,-3135,-789,-3151,11468,1904,-550,1745,4810 } },
7895 { "Panasonic DMC-ZS5", 15, 0,
7896 { 8802,-3135,-789,-3151,11468,1904,-550,1745,4810 } },
7897 { "Phase One H 20", 0, 0, /* DJC */
7898 { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
7899 { "Phase One H 25", 0, 0,
7900 { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
7901 { "Phase One P 2", 0, 0,
7902 { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
7903 { "Phase One P 30", 0, 0,
7904 { 4516,-245,-37,-7020,14976,2173,-3206,4671,7087 } },
7905 { "Phase One P 45", 0, 0,
7906 { 5053,-24,-117,-5684,14076,1702,-2619,4492,5849 } },
7907 { "Phase One P40", 0, 0,
7908 { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
7909 { "Phase One P65", 0, 0,
7910 { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
7911 { "Photron BC2-HD", 0, 0, /* DJC */
7912 { 14603,-4122,-528,-1810,9794,2017,-297,2763,5936 } },
7913 { "Red One", 704, 0xffff, /* DJC */
7914 { 21014,-7891,-2613,-3056,12201,856,-2203,5125,8042 } },
7915 { "Samsung EX1", 0, 0x3e00,
7916 { 8898,-2498,-994,-3144,11328,2066,-760,1381,4576 } },
7917 { "Samsung EX2F", 0, 0x7ff,
7918 { 10648,-3897,-1055,-2022,10573,1668,-492,1611,4742 } },
7919 { "Samsung EK-GN120", 0, 0,
7920 { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
7921 { "Samsung NX mini", 0, 0,
7922 { 5222,-1196,-550,-6540,14649,2009,-1666,2819,5657 } },
7923 { "Samsung NX3000", 0, 0,
7924 { 8060,-2933,-761,-4504,12890,1762,-630,1489,5227 } },
7925 { "Samsung NX30", 0, 0, /* NX30, NX300, NX300M */
7926 { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
7927 { "Samsung NX2000", 0, 0,
7928 { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
7929 { "Samsung NX2", 0, 0xfff, /* NX20, NX200, NX210 */
7930 { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
7931 { "Samsung NX1000", 0, 0,
7932 { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
7933 { "Samsung NX1100", 0, 0,
7934 { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
7935 { "Samsung NX11", 0, 0,
7936 { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
7937 { "Samsung NX10", 0, 0, /* also NX100 */
7938 { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
7939 { "Samsung NX500", 0, 0,
7940 { 10686,-4042,-1052,-3595,13238,276,-464,1259,5931 } },
7941 { "Samsung NX5", 0, 0,
7942 { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
7943 { "Samsung NX1", 0, 0,
7944 { 10686,-4042,-1052,-3595,13238,276,-464,1259,5931 } },
7945 { "Samsung WB2000", 0, 0xfff,
7946 { 12093,-3557,-1155,-1000,9534,1733,-22,1787,4576 } },
7947 { "Samsung GX-1", 0, 0,
7948 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
7949 { "Samsung GX20", 0, 0, /* copied from Pentax K20D */
7950 { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
7951 { "Samsung S85", 0, 0, /* DJC */
7952 { 11885,-3968,-1473,-4214,12299,1916,-835,1655,5549 } },
7953 { "Sinar", 0, 0, /* DJC */
7954 { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
7955 { "Sony DSC-F828", 0, 0,
7956 { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
7957 { "Sony DSC-R1", 512, 0,
7958 { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
7959 { "Sony DSC-V3", 0, 0,
7960 { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
7961 { "Sony DSC-RX100M", 200, 0, /* M2 and M3 */
7962 { 6596,-2079,-562,-4782,13016,1933,-970,1581,5181 } },
7963 { "Sony DSC-RX100", 200, 0,
7964 { 8651,-2754,-1057,-3464,12207,1373,-568,1398,4434 } },
7965 { "Sony DSC-RX10", 200, 0,
7966 { 6679,-1825,-745,-5047,13256,1953,-1580,2422,5183 } },
7967 { "Sony DSC-RX1", 128, 0,
7968 { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
7969 { "Sony DSLR-A100", 0, 0xfeb,
7970 { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
7971 { "Sony DSLR-A290", 0, 0,
7972 { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
7973 { "Sony DSLR-A2", 0, 0,
7974 { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
7975 { "Sony DSLR-A300", 0, 0,
7976 { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
7977 { "Sony DSLR-A330", 0, 0,
7978 { 9847,-3091,-929,-8485,16346,2225,-714,595,7103 } },
7979 { "Sony DSLR-A350", 0, 0xffc,
7980 { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
7981 { "Sony DSLR-A380", 0, 0,
7982 { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
7983 { "Sony DSLR-A390", 0, 0,
7984 { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
7985 { "Sony DSLR-A450", 128, 0xfeb,
7986 { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
7987 { "Sony DSLR-A580", 128, 0xfeb,
7988 { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
7989 { "Sony DSLR-A500", 128, 0xfeb,
7990 { 6046,-1127,-278,-5574,13076,2786,-691,1419,7625 } },
7991 { "Sony DSLR-A5", 128, 0xfeb,
7992 { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
7993 { "Sony DSLR-A700", 128, 0,
7994 { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
7995 { "Sony DSLR-A850", 128, 0,
7996 { 5413,-1162,-365,-5665,13098,2866,-608,1179,8440 } },
7997 { "Sony DSLR-A900", 128, 0,
7998 { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } },
7999 { "Sony ILCA-77M2", 128, 0,
8000 { 5991,-1732,-443,-4100,11989,2381,-704,1467,5992 } },
8001 { "Sony ILCE-7M2", 128, 0,
8002 { 5271,-712,-347,-6153,13653,2763,-1601,2366,7242 } },
8003 { "Sony ILCE-7S", 128, 0,
8004 { 5838,-1430,-246,-3497,11477,2297,-748,1885,5778 } },
8005 { "Sony ILCE-7R", 128, 0,
8006 { 4913,-541,-202,-6130,13513,2906,-1564,2151,7183 } },
8007 { "Sony ILCE-7", 128, 0,
8008 { 5271,-712,-347,-6153,13653,2763,-1601,2366,7242 } },
8009 { "Sony ILCE", 128, 0, /* 3000, 5000, 5100, 6000, and QX1 */
8010 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8011 { "Sony NEX-5N", 128, 0,
8012 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8013 { "Sony NEX-5R", 128, 0,
8014 { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
8015 { "Sony NEX-5T", 128, 0,
8016 { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
8017 { "Sony NEX-3N", 128, 0,
8018 { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
8019 { "Sony NEX-3", 138, 0, /* DJC */
8020 { 6907,-1256,-645,-4940,12621,2320,-1710,2581,6230 } },
8021 { "Sony NEX-5", 116, 0, /* DJC */
8022 { 6807,-1350,-342,-4216,11649,2567,-1089,2001,6420 } },
8023 { "Sony NEX-3", 128, 0, /* Adobe */
8024 { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
8025 { "Sony NEX-5", 128, 0, /* Adobe */
8026 { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
8027 { "Sony NEX-6", 128, 0,
8028 { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
8029 { "Sony NEX-7", 128, 0,
8030 { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
8031 { "Sony NEX", 128, 0, /* NEX-C3, NEX-F3 */
8032 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8033 { "Sony SLT-A33", 128, 0,
8034 { 6069,-1221,-366,-5221,12779,2734,-1024,2066,6834 } },
8035 { "Sony SLT-A35", 128, 0,
8036 { 5986,-1618,-415,-4557,11820,3120,-681,1404,6971 } },
8037 { "Sony SLT-A37", 128, 0,
8038 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8039 { "Sony SLT-A55", 128, 0,
8040 { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
8041 { "Sony SLT-A57", 128, 0,
8042 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8043 { "Sony SLT-A58", 128, 0,
8044 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8045 { "Sony SLT-A65", 128, 0,
8046 { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
8047 { "Sony SLT-A77", 128, 0,
8048 { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
8049 { "Sony SLT-A99", 128, 0,
8050 { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
8051 };
8052 double cam_xyz[4][3];
8053 char name[130];
8054 int i, j;
8055
8056 sprintf (name, "%s %s", make, model);
8057 for (i=0; i < sizeof table / sizeof *table; i++)
8058 if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
8059 if (table[i].black) black = (ushort) table[i].black;
8060 if (table[i].maximum) maximum = (ushort) table[i].maximum;
8061 if (table[i].trans[0]) {
8062 for (raw_color = j=0; j < 12; j++)
8063 ((double *)cam_xyz)[j] = table[i].trans[j] / 10000.0;
8064 cam_xyz_coeff (rgb_cam, cam_xyz);
8065 }
8066 break;
8067 }
8068 }
8069
simple_coeff(int index)8070 void CLASS simple_coeff (int index)
8071 {
8072 static const float table[][12] = {
8073 /* index 0 -- all Foveon cameras */
8074 { 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
8075 /* index 1 -- Kodak DC20 and DC25 */
8076 { 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
8077 /* index 2 -- Logitech Fotoman Pixtura */
8078 { 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
8079 /* index 3 -- Nikon E880, E900, and E990 */
8080 { -1.936280, 1.800443, -1.448486, 2.584324,
8081 1.405365, -0.524955, -0.289090, 0.408680,
8082 -1.204965, 1.082304, 2.941367, -1.818705 }
8083 };
8084 int i, c;
8085
8086 for (raw_color = i=0; i < 3; i++)
8087 FORCC rgb_cam[i][c] = table[index][i*colors+c];
8088 }
8089
guess_byte_order(int words)8090 short CLASS guess_byte_order (int words)
8091 {
8092 uchar test[4][2];
8093 int t=2, msb;
8094 double diff, sum[2] = {0,0};
8095
8096 fread (test[0], 2, 2, ifp);
8097 for (words-=2; words--; ) {
8098 fread (test[t], 2, 1, ifp);
8099 for (msb=0; msb < 2; msb++) {
8100 diff = (test[t^2][msb] << 8 | test[t^2][!msb])
8101 - (test[t ][msb] << 8 | test[t ][!msb]);
8102 sum[msb] += diff*diff;
8103 }
8104 t = (t+1) & 3;
8105 }
8106 return sum[0] < sum[1] ? 0x4d4d : 0x4949;
8107 }
8108
find_green(int bps,int bite,int off0,int off1)8109 float CLASS find_green (int bps, int bite, int off0, int off1)
8110 {
8111 UINT64 bitbuf=0;
8112 int vbits, col, i, c;
8113 ushort img[2][2064];
8114 double sum[]={0,0};
8115
8116 FORC(2) {
8117 fseek (ifp, c ? off1:off0, SEEK_SET);
8118 for (vbits=col=0; col < width; col++) {
8119 for (vbits -= bps; vbits < 0; vbits += bite) {
8120 bitbuf <<= bite;
8121 for (i=0; i < bite; i+=8)
8122 bitbuf |= (unsigned) (fgetc(ifp) << i);
8123 }
8124 img[c][col] = bitbuf << (64-bps-vbits) >> (64-bps);
8125 }
8126 }
8127 FORC(width-1) {
8128 sum[ c & 1] += ABS(img[0][c]-img[1][c+1]);
8129 sum[~c & 1] += ABS(img[1][c]-img[0][c+1]);
8130 }
8131 return 100 * log(sum[0]/sum[1]);
8132 }
8133
8134 /*
8135 Identify which camera created this file, and set global variables
8136 accordingly.
8137 */
identify()8138 void CLASS identify()
8139 {
8140 static const short pana[][6] = {
8141 { 3130, 1743, 4, 0, -6, 0 },
8142 { 3130, 2055, 4, 0, -6, 0 },
8143 { 3130, 2319, 4, 0, -6, 0 },
8144 { 3170, 2103, 18, 0,-42, 20 },
8145 { 3170, 2367, 18, 13,-42,-21 },
8146 { 3177, 2367, 0, 0, -1, 0 },
8147 { 3304, 2458, 0, 0, -1, 0 },
8148 { 3330, 2463, 9, 0, -5, 0 },
8149 { 3330, 2479, 9, 0,-17, 4 },
8150 { 3370, 1899, 15, 0,-44, 20 },
8151 { 3370, 2235, 15, 0,-44, 20 },
8152 { 3370, 2511, 15, 10,-44,-21 },
8153 { 3690, 2751, 3, 0, -8, -3 },
8154 { 3710, 2751, 0, 0, -3, 0 },
8155 { 3724, 2450, 0, 0, 0, -2 },
8156 { 3770, 2487, 17, 0,-44, 19 },
8157 { 3770, 2799, 17, 15,-44,-19 },
8158 { 3880, 2170, 6, 0, -6, 0 },
8159 { 4060, 3018, 0, 0, 0, -2 },
8160 { 4290, 2391, 3, 0, -8, -1 },
8161 { 4330, 2439, 17, 15,-44,-19 },
8162 { 4508, 2962, 0, 0, -3, -4 },
8163 { 4508, 3330, 0, 0, -3, -6 },
8164 };
8165 static const ushort canon[][11] = {
8166 { 1944, 1416, 0, 0, 48, 0 },
8167 { 2144, 1560, 4, 8, 52, 2, 0, 0, 0, 25 },
8168 { 2224, 1456, 48, 6, 0, 2 },
8169 { 2376, 1728, 12, 6, 52, 2 },
8170 { 2672, 1968, 12, 6, 44, 2 },
8171 { 3152, 2068, 64, 12, 0, 0, 16 },
8172 { 3160, 2344, 44, 12, 4, 4 },
8173 { 3344, 2484, 4, 6, 52, 6 },
8174 { 3516, 2328, 42, 14, 0, 0 },
8175 { 3596, 2360, 74, 12, 0, 0 },
8176 { 3744, 2784, 52, 12, 8, 12 },
8177 { 3944, 2622, 30, 18, 6, 2 },
8178 { 3948, 2622, 42, 18, 0, 2 },
8179 { 3984, 2622, 76, 20, 0, 2, 14 },
8180 { 4104, 3048, 48, 12, 24, 12 },
8181 { 4116, 2178, 4, 2, 0, 0 },
8182 { 4152, 2772, 192, 12, 0, 0 },
8183 { 4160, 3124, 104, 11, 8, 65 },
8184 { 4176, 3062, 96, 17, 8, 0, 0, 16, 0, 7, 0x49 },
8185 { 4192, 3062, 96, 17, 24, 0, 0, 16, 0, 0, 0x49 },
8186 { 4312, 2876, 22, 18, 0, 2 },
8187 { 4352, 2874, 62, 18, 0, 0 },
8188 { 4476, 2954, 90, 34, 0, 0 },
8189 { 4480, 3348, 12, 10, 36, 12, 0, 0, 0, 18, 0x49 },
8190 { 4480, 3366, 80, 50, 0, 0 },
8191 { 4496, 3366, 80, 50, 12, 0 },
8192 { 4768, 3516, 96, 16, 0, 0, 0, 16 },
8193 { 4832, 3204, 62, 26, 0, 0 },
8194 { 4832, 3228, 62, 51, 0, 0 },
8195 { 5108, 3349, 98, 13, 0, 0 },
8196 { 5120, 3318, 142, 45, 62, 0 },
8197 { 5280, 3528, 72, 52, 0, 0 },
8198 { 5344, 3516, 142, 51, 0, 0 },
8199 { 5344, 3584, 126,100, 0, 2 },
8200 { 5360, 3516, 158, 51, 0, 0 },
8201 { 5568, 3708, 72, 38, 0, 0 },
8202 { 5632, 3710, 96, 17, 0, 0, 0, 16, 0, 0, 0x49 },
8203 { 5712, 3774, 62, 20, 10, 2 },
8204 { 5792, 3804, 158, 51, 0, 0 },
8205 { 5920, 3950, 122, 80, 2, 0 },
8206 { 6096, 4056, 72, 34, 0, 0 },
8207 { 8896, 5920, 160, 64, 0, 0 },
8208 };
8209 static const struct {
8210 ushort id;
8211 char model[20];
8212 } unique[] = {
8213 { 0x168, "EOS 10D" }, { 0x001, "EOS-1D" },
8214 { 0x175, "EOS 20D" }, { 0x174, "EOS-1D Mark II" },
8215 { 0x234, "EOS 30D" }, { 0x232, "EOS-1D Mark II N" },
8216 { 0x190, "EOS 40D" }, { 0x169, "EOS-1D Mark III" },
8217 { 0x261, "EOS 50D" }, { 0x281, "EOS-1D Mark IV" },
8218 { 0x287, "EOS 60D" }, { 0x167, "EOS-1DS" },
8219 { 0x325, "EOS 70D" },
8220 { 0x170, "EOS 300D" }, { 0x188, "EOS-1Ds Mark II" },
8221 { 0x176, "EOS 450D" }, { 0x215, "EOS-1Ds Mark III" },
8222 { 0x189, "EOS 350D" }, { 0x324, "EOS-1D C" },
8223 { 0x236, "EOS 400D" }, { 0x269, "EOS-1D X" },
8224 { 0x252, "EOS 500D" }, { 0x213, "EOS 5D" },
8225 { 0x270, "EOS 550D" }, { 0x218, "EOS 5D Mark II" },
8226 { 0x286, "EOS 600D" }, { 0x285, "EOS 5D Mark III" },
8227 { 0x301, "EOS 650D" }, { 0x302, "EOS 6D" },
8228 { 0x326, "EOS 700D" }, { 0x250, "EOS 7D" },
8229 { 0x393, "EOS 750D" }, { 0x289, "EOS 7D Mark II" },
8230 { 0x347, "EOS 760D" },
8231 { 0x254, "EOS 1000D" },
8232 { 0x288, "EOS 1100D" },
8233 { 0x327, "EOS 1200D" },
8234 { 0x346, "EOS 100D" },
8235 }, sonique[] = {
8236 { 0x002, "DSC-R1" }, { 0x100, "DSLR-A100" },
8237 { 0x101, "DSLR-A900" }, { 0x102, "DSLR-A700" },
8238 { 0x103, "DSLR-A200" }, { 0x104, "DSLR-A350" },
8239 { 0x105, "DSLR-A300" }, { 0x108, "DSLR-A330" },
8240 { 0x109, "DSLR-A230" }, { 0x10a, "DSLR-A290" },
8241 { 0x10d, "DSLR-A850" }, { 0x111, "DSLR-A550" },
8242 { 0x112, "DSLR-A500" }, { 0x113, "DSLR-A450" },
8243 { 0x116, "NEX-5" }, { 0x117, "NEX-3" },
8244 { 0x118, "SLT-A33" }, { 0x119, "SLT-A55V" },
8245 { 0x11a, "DSLR-A560" }, { 0x11b, "DSLR-A580" },
8246 { 0x11c, "NEX-C3" }, { 0x11d, "SLT-A35" },
8247 { 0x11e, "SLT-A65V" }, { 0x11f, "SLT-A77V" },
8248 { 0x120, "NEX-5N" }, { 0x121, "NEX-7" },
8249 { 0x123, "SLT-A37" }, { 0x124, "SLT-A57" },
8250 { 0x125, "NEX-F3" }, { 0x126, "SLT-A99V" },
8251 { 0x127, "NEX-6" }, { 0x128, "NEX-5R" },
8252 { 0x129, "DSC-RX100" }, { 0x12a, "DSC-RX1" },
8253 { 0x12e, "ILCE-3000" }, { 0x12f, "SLT-A58" },
8254 { 0x131, "NEX-3N" }, { 0x132, "ILCE-7" },
8255 { 0x133, "NEX-5T" }, { 0x134, "DSC-RX100M2" },
8256 { 0x135, "DSC-RX10" }, { 0x136, "DSC-RX1R" },
8257 { 0x137, "ILCE-7R" }, { 0x138, "ILCE-6000" },
8258 { 0x139, "ILCE-5000" }, { 0x13d, "DSC-RX100M3" },
8259 { 0x13e, "ILCE-7S" }, { 0x13f, "ILCA-77M2" },
8260 { 0x153, "ILCE-5100" }, { 0x154, "ILCE-7M2" },
8261 { 0x15a, "ILCE-QX1" },
8262 };
8263 static const struct {
8264 unsigned fsize;
8265 ushort rw, rh;
8266 uchar lm, tm, rm, bm, lf, cf, max, flags;
8267 char make[10], model[20];
8268 ushort offset;
8269 } table[] = {
8270 { 786432,1024, 768, 0, 0, 0, 0, 0,0x94,0,0,"AVT","F-080C" },
8271 { 1447680,1392,1040, 0, 0, 0, 0, 0,0x94,0,0,"AVT","F-145C" },
8272 { 1920000,1600,1200, 0, 0, 0, 0, 0,0x94,0,0,"AVT","F-201C" },
8273 { 5067304,2588,1958, 0, 0, 0, 0, 0,0x94,0,0,"AVT","F-510C" },
8274 { 5067316,2588,1958, 0, 0, 0, 0, 0,0x94,0,0,"AVT","F-510C",12 },
8275 { 10134608,2588,1958, 0, 0, 0, 0, 9,0x94,0,0,"AVT","F-510C" },
8276 { 10134620,2588,1958, 0, 0, 0, 0, 9,0x94,0,0,"AVT","F-510C",12 },
8277 { 16157136,3272,2469, 0, 0, 0, 0, 9,0x94,0,0,"AVT","F-810C" },
8278 { 15980544,3264,2448, 0, 0, 0, 0, 8,0x61,0,1,"AgfaPhoto","DC-833m" },
8279 { 9631728,2532,1902, 0, 0, 0, 0,96,0x61,0,0,"Alcatel","5035D" },
8280 { 2868726,1384,1036, 0, 0, 0, 0,64,0x49,0,8,"Baumer","TXG14",1078 },
8281 { 5298000,2400,1766,12,12,44, 2,40,0x94,0,2,"Canon","PowerShot SD300" },
8282 { 6553440,2664,1968, 4, 4,44, 4,40,0x94,0,2,"Canon","PowerShot A460" },
8283 { 6573120,2672,1968,12, 8,44, 0,40,0x94,0,2,"Canon","PowerShot A610" },
8284 { 6653280,2672,1992,10, 6,42, 2,40,0x94,0,2,"Canon","PowerShot A530" },
8285 { 7710960,2888,2136,44, 8, 4, 0,40,0x94,0,2,"Canon","PowerShot S3 IS" },
8286 { 9219600,3152,2340,36,12, 4, 0,40,0x94,0,2,"Canon","PowerShot A620" },
8287 { 9243240,3152,2346,12, 7,44,13,40,0x49,0,2,"Canon","PowerShot A470" },
8288 { 10341600,3336,2480, 6, 5,32, 3,40,0x94,0,2,"Canon","PowerShot A720 IS" },
8289 { 10383120,3344,2484,12, 6,44, 6,40,0x94,0,2,"Canon","PowerShot A630" },
8290 { 12945240,3736,2772,12, 6,52, 6,40,0x94,0,2,"Canon","PowerShot A640" },
8291 { 15636240,4104,3048,48,12,24,12,40,0x94,0,2,"Canon","PowerShot A650" },
8292 { 15467760,3720,2772, 6,12,30, 0,40,0x94,0,2,"Canon","PowerShot SX110 IS" },
8293 { 15534576,3728,2778,12, 9,44, 9,40,0x94,0,2,"Canon","PowerShot SX120 IS" },
8294 { 18653760,4080,3048,24,12,24,12,40,0x94,0,2,"Canon","PowerShot SX20 IS" },
8295 { 19131120,4168,3060,92,16, 4, 1,40,0x94,0,2,"Canon","PowerShot SX220 HS" },
8296 { 21936096,4464,3276,25,10,73,12,40,0x16,0,2,"Canon","PowerShot SX30 IS" },
8297 { 24724224,4704,3504, 8,16,56, 8,40,0x94,0,2,"Canon","PowerShot A3300 IS" },
8298 { 1976352,1632,1211, 0, 2, 0, 1, 0,0x94,0,1,"Casio","QV-2000UX" },
8299 { 3217760,2080,1547, 0, 0,10, 1, 0,0x94,0,1,"Casio","QV-3*00EX" },
8300 { 6218368,2585,1924, 0, 0, 9, 0, 0,0x94,0,1,"Casio","QV-5700" },
8301 { 7816704,2867,2181, 0, 0,34,36, 0,0x16,0,1,"Casio","EX-Z60" },
8302 { 2937856,1621,1208, 0, 0, 1, 0, 0,0x94,7,13,"Casio","EX-S20" },
8303 { 4948608,2090,1578, 0, 0,32,34, 0,0x94,7,1,"Casio","EX-S100" },
8304 { 6054400,2346,1720, 2, 0,32, 0, 0,0x94,7,1,"Casio","QV-R41" },
8305 { 7426656,2568,1928, 0, 0, 0, 0, 0,0x94,0,1,"Casio","EX-P505" },
8306 { 7530816,2602,1929, 0, 0,22, 0, 0,0x94,7,1,"Casio","QV-R51" },
8307 { 7542528,2602,1932, 0, 0,32, 0, 0,0x94,7,1,"Casio","EX-Z50" },
8308 { 7562048,2602,1937, 0, 0,25, 0, 0,0x16,7,1,"Casio","EX-Z500" },
8309 { 7753344,2602,1986, 0, 0,32,26, 0,0x94,7,1,"Casio","EX-Z55" },
8310 { 9313536,2858,2172, 0, 0,14,30, 0,0x94,7,1,"Casio","EX-P600" },
8311 { 10834368,3114,2319, 0, 0,27, 0, 0,0x94,0,1,"Casio","EX-Z750" },
8312 { 10843712,3114,2321, 0, 0,25, 0, 0,0x94,0,1,"Casio","EX-Z75" },
8313 { 10979200,3114,2350, 0, 0,32,32, 0,0x94,7,1,"Casio","EX-P700" },
8314 { 12310144,3285,2498, 0, 0, 6,30, 0,0x94,0,1,"Casio","EX-Z850" },
8315 { 12489984,3328,2502, 0, 0,47,35, 0,0x94,0,1,"Casio","EX-Z8" },
8316 { 15499264,3754,2752, 0, 0,82, 0, 0,0x94,0,1,"Casio","EX-Z1050" },
8317 { 18702336,4096,3044, 0, 0,24, 0,80,0x94,7,1,"Casio","EX-ZR100" },
8318 { 7684000,2260,1700, 0, 0, 0, 0,13,0x94,0,1,"Casio","QV-4000" },
8319 { 787456,1024, 769, 0, 1, 0, 0, 0,0x49,0,0,"Creative","PC-CAM 600" },
8320 { 28829184,4384,3288, 0, 0, 0, 0,36,0x61,0,0,"DJI" },
8321 { 15151104,4608,3288, 0, 0, 0, 0, 0,0x94,0,0,"Matrix" },
8322 { 3840000,1600,1200, 0, 0, 0, 0,65,0x49,0,0,"Foculus","531C" },
8323 { 307200, 640, 480, 0, 0, 0, 0, 0,0x94,0,0,"Generic" },
8324 { 62464, 256, 244, 1, 1, 6, 1, 0,0x8d,0,0,"Kodak","DC20" },
8325 { 124928, 512, 244, 1, 1,10, 1, 0,0x8d,0,0,"Kodak","DC20" },
8326 { 1652736,1536,1076, 0,52, 0, 0, 0,0x61,0,0,"Kodak","DCS200" },
8327 { 4159302,2338,1779, 1,33, 1, 2, 0,0x94,0,0,"Kodak","C330" },
8328 { 4162462,2338,1779, 1,33, 1, 2, 0,0x94,0,0,"Kodak","C330",3160 },
8329 { 2247168,1232, 912, 0, 0,16, 0, 0,0x00,0,0,"Kodak","C330" },
8330 { 3370752,1232, 912, 0, 0,16, 0, 0,0x00,0,0,"Kodak","C330" },
8331 { 6163328,2864,2152, 0, 0, 0, 0, 0,0x94,0,0,"Kodak","C603" },
8332 { 6166488,2864,2152, 0, 0, 0, 0, 0,0x94,0,0,"Kodak","C603",3160 },
8333 { 460800, 640, 480, 0, 0, 0, 0, 0,0x00,0,0,"Kodak","C603" },
8334 { 9116448,2848,2134, 0, 0, 0, 0, 0,0x00,0,0,"Kodak","C603" },
8335 { 12241200,4040,3030, 2, 0, 0,13, 0,0x49,0,0,"Kodak","12MP" },
8336 { 12272756,4040,3030, 2, 0, 0,13, 0,0x49,0,0,"Kodak","12MP",31556 },
8337 { 18000000,4000,3000, 0, 0, 0, 0, 0,0x00,0,0,"Kodak","12MP" },
8338 { 614400, 640, 480, 0, 3, 0, 0,64,0x94,0,0,"Kodak","KAI-0340" },
8339 { 15360000,3200,2400, 0, 0, 0, 0,96,0x16,0,0,"Lenovo","A820" },
8340 { 3884928,1608,1207, 0, 0, 0, 0,96,0x16,0,0,"Micron","2010",3212 },
8341 { 1138688,1534, 986, 0, 0, 0, 0, 0,0x61,0,0,"Minolta","RD175",513 },
8342 { 1581060,1305, 969, 0, 0,18, 6, 6,0x1e,4,1,"Nikon","E900" },
8343 { 2465792,1638,1204, 0, 0,22, 1, 6,0x4b,5,1,"Nikon","E950" },
8344 { 2940928,1616,1213, 0, 0, 0, 7,30,0x94,0,1,"Nikon","E2100" },
8345 { 4771840,2064,1541, 0, 0, 0, 1, 6,0xe1,0,1,"Nikon","E990" },
8346 { 4775936,2064,1542, 0, 0, 0, 0,30,0x94,0,1,"Nikon","E3700" },
8347 { 5865472,2288,1709, 0, 0, 0, 1, 6,0xb4,0,1,"Nikon","E4500" },
8348 { 5869568,2288,1710, 0, 0, 0, 0, 6,0x16,0,1,"Nikon","E4300" },
8349 { 7438336,2576,1925, 0, 0, 0, 1, 6,0xb4,0,1,"Nikon","E5000" },
8350 { 8998912,2832,2118, 0, 0, 0, 0,30,0x94,7,1,"Nikon","COOLPIX S6" },
8351 { 5939200,2304,1718, 0, 0, 0, 0,30,0x16,0,0,"Olympus","C770UZ" },
8352 { 3178560,2064,1540, 0, 0, 0, 0, 0,0x94,0,1,"Pentax","Optio S" },
8353 { 4841984,2090,1544, 0, 0,22, 0, 0,0x94,7,1,"Pentax","Optio S" },
8354 { 6114240,2346,1737, 0, 0,22, 0, 0,0x94,7,1,"Pentax","Optio S4" },
8355 { 10702848,3072,2322, 0, 0, 0,21,30,0x94,0,1,"Pentax","Optio 750Z" },
8356 { 4147200,1920,1080, 0, 0, 0, 0, 0,0x49,0,0,"Photron","BC2-HD" },
8357 { 4151666,1920,1080, 0, 0, 0, 0, 0,0x49,0,0,"Photron","BC2-HD",8 },
8358 { 13248000,2208,3000, 0, 0, 0, 0,13,0x61,0,0,"Pixelink","A782" },
8359 { 6291456,2048,1536, 0, 0, 0, 0,96,0x61,0,0,"RoverShot","3320AF" },
8360 { 311696, 644, 484, 0, 0, 0, 0, 0,0x16,0,8,"ST Micro","STV680 VGA" },
8361 { 16098048,3288,2448, 0, 0,24, 0, 9,0x94,0,1,"Samsung","S85" },
8362 { 16215552,3312,2448, 0, 0,48, 0, 9,0x94,0,1,"Samsung","S85" },
8363 { 20487168,3648,2808, 0, 0, 0, 0,13,0x94,5,1,"Samsung","WB550" },
8364 { 24000000,4000,3000, 0, 0, 0, 0,13,0x94,5,1,"Samsung","WB550" },
8365 { 12582980,3072,2048, 0, 0, 0, 0,33,0x61,0,0,"Sinar","",68 },
8366 { 33292868,4080,4080, 0, 0, 0, 0,33,0x61,0,0,"Sinar","",68 },
8367 { 44390468,4080,5440, 0, 0, 0, 0,33,0x61,0,0,"Sinar","",68 },
8368 { 1409024,1376,1024, 0, 0, 1, 0, 0,0x49,0,0,"Sony","XCD-SX910CR" },
8369 { 2818048,1376,1024, 0, 0, 1, 0,97,0x49,0,0,"Sony","XCD-SX910CR" },
8370 };
8371 static const char *corp[] =
8372 { "AgfaPhoto", "Canon", "Casio", "Epson", "Fujifilm",
8373 "Mamiya", "Minolta", "Motorola", "Kodak", "Konica", "Leica",
8374 "Nikon", "Nokia", "Olympus", "Pentax", "Phase One", "Ricoh",
8375 "Samsung", "Sigma", "Sinar", "Sony" };
8376 char head[32], *cp;
8377 int hlen, flen, fsize, zero_fsize=1, i, c;
8378 struct jhead jh;
8379
8380 tiff_flip = flip = filters = UINT_MAX; /* unknown */
8381 raw_height = raw_width = fuji_width = fuji_layout = cr2_slice[0] = 0;
8382 maximum = height = width = top_margin = left_margin = 0;
8383 cdesc[0] = desc[0] = artist[0] = make[0] = model[0] = model2[0] = 0;
8384 iso_speed = shutter = aperture = focal_len = unique_id = 0;
8385 tiff_nifds = 0;
8386 memset (tiff_ifd, 0, sizeof tiff_ifd);
8387 memset (gpsdata, 0, sizeof gpsdata);
8388 memset (cblack, 0, sizeof cblack);
8389 memset (white, 0, sizeof white);
8390 memset (mask, 0, sizeof mask);
8391 thumb_offset = thumb_length = thumb_width = thumb_height = 0;
8392 load_raw = thumb_load_raw = 0;
8393 write_thumb = &CLASS jpeg_thumb;
8394 data_offset = meta_offset = meta_length = tiff_bps = tiff_compress = 0;
8395 kodak_cbpp = zero_after_ff = dng_version = load_flags = 0;
8396 timestamp = shot_order = tiff_samples = black = is_foveon = 0;
8397 mix_green = profile_length = data_error = zero_is_bad = 0;
8398 pixel_aspect = is_raw = raw_color = 1;
8399 tile_width = tile_length = 0;
8400 for (i=0; i < 4; i++) {
8401 cam_mul[i] = i == 1;
8402 pre_mul[i] = i < 3;
8403 FORC3 cmatrix[c][i] = 0;
8404 FORC3 rgb_cam[c][i] = c == i;
8405 }
8406 colors = 3;
8407 for (i=0; i < 0x10000; i++) curve[i] = i;
8408
8409 order = get2();
8410 hlen = get4();
8411 fseek (ifp, 0, SEEK_SET);
8412 fread (head, 1, 32, ifp);
8413 fseek (ifp, 0, SEEK_END);
8414 flen = fsize = ftell(ifp);
8415 if ((cp = (char *) memmem (head, 32, "MMMM", 4)) ||
8416 (cp = (char *) memmem (head, 32, "IIII", 4))) {
8417 parse_phase_one (cp-head);
8418 if (cp-head && parse_tiff(0)) apply_tiff();
8419 } else if (order == 0x4949 || order == 0x4d4d) {
8420 if (!memcmp (head+6,"HEAPCCDR",8)) {
8421 data_offset = hlen;
8422 parse_ciff (hlen, flen-hlen, 0);
8423 load_raw = &CLASS canon_load_raw;
8424 } else if (parse_tiff(0)) apply_tiff();
8425 } else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
8426 !memcmp (head+6,"Exif",4)) {
8427 fseek (ifp, 4, SEEK_SET);
8428 data_offset = 4 + get2();
8429 fseek (ifp, data_offset, SEEK_SET);
8430 if (fgetc(ifp) != 0xff)
8431 parse_tiff(12);
8432 thumb_offset = 0;
8433 } else if (!memcmp (head+25,"ARECOYK",7)) {
8434 strcpy (make, "Contax");
8435 strcpy (model,"N Digital");
8436 fseek (ifp, 33, SEEK_SET);
8437 get_timestamp(1);
8438 fseek (ifp, 60, SEEK_SET);
8439 FORC4 cam_mul[c ^ (c >> 1)] = get4();
8440 } else if (!strcmp (head, "PXN")) {
8441 strcpy (make, "Logitech");
8442 strcpy (model,"Fotoman Pixtura");
8443 } else if (!strcmp (head, "qktk")) {
8444 strcpy (make, "Apple");
8445 strcpy (model,"QuickTake 100");
8446 load_raw = &CLASS quicktake_100_load_raw;
8447 } else if (!strcmp (head, "qktn")) {
8448 strcpy (make, "Apple");
8449 strcpy (model,"QuickTake 150");
8450 load_raw = &CLASS kodak_radc_load_raw;
8451 } else if (!memcmp (head,"FUJIFILM",8)) {
8452 fseek (ifp, 84, SEEK_SET);
8453 thumb_offset = get4();
8454 thumb_length = get4();
8455 fseek (ifp, 92, SEEK_SET);
8456 parse_fuji (get4());
8457 if (thumb_offset > 120) {
8458 fseek (ifp, 120, SEEK_SET);
8459 is_raw += (i = get4()) && 1;
8460 if (is_raw == 2 && shot_select)
8461 parse_fuji (i);
8462 }
8463 load_raw = &CLASS unpacked_load_raw;
8464 fseek (ifp, 100+28*(shot_select > 0), SEEK_SET);
8465 parse_tiff (data_offset = get4());
8466 parse_tiff (thumb_offset+12);
8467 apply_tiff();
8468 } else if (!memcmp (head,"RIFF",4)) {
8469 fseek (ifp, 0, SEEK_SET);
8470 parse_riff();
8471 } else if (!memcmp (head+4,"ftypqt ",9)) {
8472 fseek (ifp, 0, SEEK_SET);
8473 parse_qt (fsize);
8474 is_raw = 0;
8475 } else if (!memcmp (head,"\0\001\0\001\0@",6)) {
8476 fseek (ifp, 6, SEEK_SET);
8477 fread (make, 1, 8, ifp);
8478 fread (model, 1, 8, ifp);
8479 fread (model2, 1, 16, ifp);
8480 data_offset = get2();
8481 get2();
8482 raw_width = get2();
8483 raw_height = get2();
8484 load_raw = &CLASS nokia_load_raw;
8485 filters = 0x61616161;
8486 } else if (!memcmp (head,"NOKIARAW",8)) {
8487 strcpy (make, "NOKIA");
8488 order = 0x4949;
8489 fseek (ifp, 300, SEEK_SET);
8490 data_offset = get4();
8491 i = get4();
8492 width = get2();
8493 height = get2();
8494 switch (tiff_bps = i*8 / (width * height)) {
8495 case 8: load_raw = &CLASS eight_bit_load_raw; break;
8496 case 10: load_raw = &CLASS nokia_load_raw;
8497 }
8498 raw_height = height + (top_margin = i / (width * tiff_bps/8) - height);
8499 mask[0][3] = 1;
8500 filters = 0x61616161;
8501 } else if (!memcmp (head,"ARRI",4)) {
8502 order = 0x4949;
8503 fseek (ifp, 20, SEEK_SET);
8504 width = get4();
8505 height = get4();
8506 strcpy (make, "ARRI");
8507 fseek (ifp, 668, SEEK_SET);
8508 fread (model, 1, 64, ifp);
8509 data_offset = 4096;
8510 load_raw = &CLASS packed_load_raw;
8511 load_flags = 88;
8512 filters = 0x61616161;
8513 } else if (!memcmp (head,"XPDS",4)) {
8514 order = 0x4949;
8515 fseek (ifp, 0x800, SEEK_SET);
8516 fread (make, 1, 41, ifp);
8517 raw_height = get2();
8518 raw_width = get2();
8519 fseek (ifp, 56, SEEK_CUR);
8520 fread (model, 1, 30, ifp);
8521 data_offset = 0x10000;
8522 load_raw = &CLASS canon_rmf_load_raw;
8523 gamma_curve (0, 12.25, 1, 1023);
8524 } else if (!memcmp (head+4,"RED1",4)) {
8525 strcpy (make, "Red");
8526 strcpy (model,"One");
8527 parse_redcine();
8528 load_raw = &CLASS redcine_load_raw;
8529 gamma_curve (1/2.4, 12.92, 1, 4095);
8530 filters = 0x49494949;
8531 } else if (!memcmp (head,"DSC-Image",9))
8532 parse_rollei();
8533 else if (!memcmp (head,"PWAD",4))
8534 parse_sinar_ia();
8535 else if (!memcmp (head,"\0MRM",4))
8536 parse_minolta(0);
8537 else if (!memcmp (head,"FOVb",4))
8538 parse_foveon();
8539 else if (!memcmp (head,"CI",2))
8540 parse_cine();
8541 if (make[0] == 0)
8542 for (zero_fsize=i=0; i < sizeof table / sizeof *table; i++)
8543 if (fsize == table[i].fsize) {
8544 strcpy (make, table[i].make );
8545 strcpy (model, table[i].model);
8546 flip = table[i].flags >> 2;
8547 zero_is_bad = table[i].flags & 2;
8548 if (table[i].flags & 1)
8549 parse_external_jpeg();
8550 data_offset = table[i].offset;
8551 raw_width = table[i].rw;
8552 raw_height = table[i].rh;
8553 left_margin = table[i].lm;
8554 top_margin = table[i].tm;
8555 width = raw_width - left_margin - table[i].rm;
8556 height = raw_height - top_margin - table[i].bm;
8557 filters = 0x1010101 * table[i].cf;
8558 colors = 4 - !((filters & filters >> 1) & 0x5555);
8559 load_flags = table[i].lf;
8560 switch (tiff_bps = (fsize-data_offset)*8 / (raw_width*raw_height)) {
8561 case 6:
8562 load_raw = &CLASS minolta_rd175_load_raw; break;
8563 case 8:
8564 load_raw = &CLASS eight_bit_load_raw; break;
8565 case 10: case 12:
8566 load_flags |= 128;
8567 load_raw = &CLASS packed_load_raw; break;
8568 case 16:
8569 order = 0x4949 | 0x404 * (load_flags & 1);
8570 tiff_bps -= load_flags >> 4;
8571 tiff_bps -= load_flags = load_flags >> 1 & 7;
8572 load_raw = &CLASS unpacked_load_raw;
8573 }
8574 maximum = (1 << tiff_bps) - (1 << table[i].max);
8575 }
8576 if (zero_fsize) fsize = 0;
8577 if (make[0] == 0) parse_smal (0, flen);
8578 if (make[0] == 0) {
8579 parse_jpeg(0);
8580 if (!(strncmp(model,"ov",2) && strncmp(model,"RP_OV",5)) &&
8581 !fseek (ifp, -6404096, SEEK_END) &&
8582 fread (head, 1, 32, ifp) && !strcmp(head,"BRCMn")) {
8583 strcpy (make, "OmniVision");
8584 data_offset = ftell(ifp) + 0x8000-32;
8585 width = raw_width;
8586 raw_width = 2611;
8587 load_raw = &CLASS nokia_load_raw;
8588 filters = 0x16161616;
8589 } else is_raw = 0;
8590 }
8591
8592 for (i=0; i < sizeof corp / sizeof *corp; i++)
8593 if (strcasestr (make, corp[i])) /* Simplify company names */
8594 strcpy (make, corp[i]);
8595 if ((!strcmp(make,"Kodak") || !strcmp(make,"Leica")) &&
8596 ((cp = strcasestr(model," DIGITAL CAMERA")) ||
8597 (cp = strstr(model,"FILE VERSION"))))
8598 *cp = 0;
8599 if (!strncasecmp(model,"PENTAX",6))
8600 strcpy (make, "Pentax");
8601 cp = make + strlen(make); /* Remove trailing spaces */
8602 while (*--cp == ' ') *cp = 0;
8603 cp = model + strlen(model);
8604 while (*--cp == ' ') *cp = 0;
8605 i = strlen(make); /* Remove make from model */
8606 if (!strncasecmp (model, make, i) && model[i++] == ' ')
8607 memmove (model, model+i, 64-i);
8608 if (!strncmp (model,"FinePix ",8))
8609 strcpy (model, model+8);
8610 if (!strncmp (model,"Digital Camera ",15))
8611 strcpy (model, model+15);
8612 desc[511] = artist[63] = make[63] = model[63] = model2[63] = 0;
8613 if (!is_raw) goto notraw;
8614
8615 if (!height) height = raw_height;
8616 if (!width) width = raw_width;
8617 if (height == 2624 && width == 3936) /* Pentax K10D and Samsung GX10 */
8618 { height = 2616; width = 3896; }
8619 if (height == 3136 && width == 4864) /* Pentax K20D and Samsung GX20 */
8620 { height = 3124; width = 4688; filters = 0x16161616; }
8621 if (width == 4352 && (!strcmp(model,"K-r") || !strcmp(model,"K-x")))
8622 { width = 4309; filters = 0x16161616; }
8623 if (width >= 4960 && !strncmp(model,"K-5",3))
8624 { left_margin = 10; width = 4950; filters = 0x16161616; }
8625 if (width == 4736 && !strcmp(model,"K-7"))
8626 { height = 3122; width = 4684; filters = 0x16161616; top_margin = 2; }
8627 if (width == 6080 && !strcmp(model,"K-3"))
8628 { left_margin = 4; width = 6040; }
8629 if (width == 7424 && !strcmp(model,"645D"))
8630 { height = 5502; width = 7328; filters = 0x61616161; top_margin = 29;
8631 left_margin = 48; }
8632 if (height == 3014 && width == 4096) /* Ricoh GX200 */
8633 width = 4014;
8634 if (dng_version) {
8635 if (filters == UINT_MAX) filters = 0;
8636 if (filters) is_raw *= tiff_samples;
8637 else colors = tiff_samples;
8638 switch (tiff_compress) {
8639 case 0:
8640 case 1: load_raw = &CLASS packed_dng_load_raw; break;
8641 case 7: load_raw = &CLASS lossless_dng_load_raw; break;
8642 case 34892: load_raw = &CLASS lossy_dng_load_raw; break;
8643 default: load_raw = 0;
8644 }
8645 goto dng_skip;
8646 }
8647 if (!strcmp(make,"Canon") && !fsize && tiff_bps != 15) {
8648 if (!load_raw)
8649 load_raw = &CLASS lossless_jpeg_load_raw;
8650 for (i=0; i < sizeof canon / sizeof *canon; i++)
8651 if (raw_width == canon[i][0] && raw_height == canon[i][1]) {
8652 width = raw_width - (left_margin = canon[i][2]);
8653 height = raw_height - (top_margin = canon[i][3]);
8654 width -= canon[i][4];
8655 height -= canon[i][5];
8656 mask[0][1] = canon[i][6];
8657 mask[0][3] = -canon[i][7];
8658 mask[1][1] = canon[i][8];
8659 mask[1][3] = -canon[i][9];
8660 if (canon[i][10]) filters = canon[i][10] * 0x01010101;
8661 }
8662 if ((unique_id | 0x20000) == 0x2720000) {
8663 left_margin = 8;
8664 top_margin = 16;
8665 }
8666 }
8667 for (i=0; i < sizeof unique / sizeof *unique; i++)
8668 if (unique_id == 0x80000000 + unique[i].id) {
8669 adobe_coeff ("Canon", unique[i].model);
8670 if (model[4] == 'K' && strlen(model) == 8)
8671 strcpy (model, unique[i].model);
8672 }
8673 for (i=0; i < sizeof sonique / sizeof *sonique; i++)
8674 if (unique_id == sonique[i].id)
8675 strcpy (model, sonique[i].model);
8676 if (!strcmp(make,"Nikon")) {
8677 if (!load_raw)
8678 load_raw = &CLASS packed_load_raw;
8679 if (model[0] == 'E')
8680 load_flags |= !data_offset << 2 | 2;
8681 }
8682
8683 /* Set parameters based on camera name (for non-DNG files). */
8684
8685 if (!strcmp(model,"KAI-0340")
8686 && find_green (16, 16, 3840, 5120) < 25) {
8687 height = 480;
8688 top_margin = filters = 0;
8689 strcpy (model,"C603");
8690 }
8691 if (is_foveon) {
8692 if (height*2 < width) pixel_aspect = 0.5;
8693 if (height > width) pixel_aspect = 2;
8694 filters = 0;
8695 simple_coeff(0);
8696 } else if (!strcmp(make,"Canon") && tiff_bps == 15) {
8697 switch (width) {
8698 case 3344: width -= 66;
8699 case 3872: width -= 6;
8700 }
8701 if (height > width) {
8702 SWAP(height,width);
8703 SWAP(raw_height,raw_width);
8704 }
8705 if (width == 7200 && height == 3888) {
8706 raw_width = width = 6480;
8707 raw_height = height = 4320;
8708 }
8709 filters = 0;
8710 tiff_samples = colors = 3;
8711 load_raw = &CLASS canon_sraw_load_raw;
8712 } else if (!strcmp(model,"PowerShot 600")) {
8713 height = 613;
8714 width = 854;
8715 raw_width = 896;
8716 colors = 4;
8717 filters = 0xe1e4e1e4;
8718 load_raw = &CLASS canon_600_load_raw;
8719 } else if (!strcmp(model,"PowerShot A5") ||
8720 !strcmp(model,"PowerShot A5 Zoom")) {
8721 height = 773;
8722 width = 960;
8723 raw_width = 992;
8724 pixel_aspect = 256/235.0;
8725 filters = 0x1e4e1e4e;
8726 goto canon_a5;
8727 } else if (!strcmp(model,"PowerShot A50")) {
8728 height = 968;
8729 width = 1290;
8730 raw_width = 1320;
8731 filters = 0x1b4e4b1e;
8732 goto canon_a5;
8733 } else if (!strcmp(model,"PowerShot Pro70")) {
8734 height = 1024;
8735 width = 1552;
8736 filters = 0x1e4b4e1b;
8737 canon_a5:
8738 colors = 4;
8739 tiff_bps = 10;
8740 load_raw = &CLASS packed_load_raw;
8741 load_flags = 40;
8742 } else if (!strcmp(model,"PowerShot Pro90 IS") ||
8743 !strcmp(model,"PowerShot G1")) {
8744 colors = 4;
8745 filters = 0xb4b4b4b4;
8746 } else if (!strcmp(model,"PowerShot A610")) {
8747 if (canon_s2is()) strcpy (model+10, "S2 IS");
8748 } else if (!strcmp(model,"PowerShot SX220 HS")) {
8749 mask[1][3] = -4;
8750 } else if (!strcmp(model,"EOS D2000C")) {
8751 filters = 0x61616161;
8752 black = curve[200];
8753 } else if (!strcmp(model,"D1")) {
8754 cam_mul[0] *= 256/527.0;
8755 cam_mul[2] *= 256/317.0;
8756 } else if (!strcmp(model,"D1X")) {
8757 width -= 4;
8758 pixel_aspect = 0.5;
8759 } else if (!strcmp(model,"D40X") ||
8760 !strcmp(model,"D60") ||
8761 !strcmp(model,"D80") ||
8762 !strcmp(model,"D3000")) {
8763 height -= 3;
8764 width -= 4;
8765 } else if (!strcmp(model,"D3") ||
8766 !strcmp(model,"D3S") ||
8767 !strcmp(model,"D700")) {
8768 width -= 4;
8769 left_margin = 2;
8770 } else if (!strcmp(model,"D3100")) {
8771 width -= 28;
8772 left_margin = 6;
8773 } else if (!strcmp(model,"D5000") ||
8774 !strcmp(model,"D90")) {
8775 width -= 42;
8776 } else if (!strcmp(model,"D5100") ||
8777 !strcmp(model,"D7000") ||
8778 !strcmp(model,"COOLPIX A")) {
8779 width -= 44;
8780 } else if (!strcmp(model,"D3200") ||
8781 !strncmp(model,"D6",2) ||
8782 !strncmp(model,"D800",4)) {
8783 width -= 46;
8784 } else if (!strcmp(model,"D4") ||
8785 !strcmp(model,"Df")) {
8786 width -= 52;
8787 left_margin = 2;
8788 } else if (!strncmp(model,"D40",3) ||
8789 !strncmp(model,"D50",3) ||
8790 !strncmp(model,"D70",3)) {
8791 width--;
8792 } else if (!strcmp(model,"D100")) {
8793 if (load_flags)
8794 raw_width = (width += 3) + 3;
8795 } else if (!strcmp(model,"D200")) {
8796 left_margin = 1;
8797 width -= 4;
8798 filters = 0x94949494;
8799 } else if (!strncmp(model,"D2H",3)) {
8800 left_margin = 6;
8801 width -= 14;
8802 } else if (!strncmp(model,"D2X",3)) {
8803 if (width == 3264) width -= 32;
8804 else width -= 8;
8805 } else if (!strncmp(model,"D300",4)) {
8806 width -= 32;
8807 } else if (!strncmp(model,"COOLPIX P",9) && raw_width != 4032) {
8808 load_flags = 24;
8809 filters = 0x94949494;
8810 if (model[9] == '7' && iso_speed >= 400)
8811 black = 255;
8812 } else if (!strncmp(model,"1 ",2)) {
8813 height -= 2;
8814 } else if (fsize == 1581060) {
8815 simple_coeff(3);
8816 pre_mul[0] = 1.2085;
8817 pre_mul[1] = 1.0943;
8818 pre_mul[3] = 1.1103;
8819 } else if (fsize == 3178560) {
8820 cam_mul[0] *= 4;
8821 cam_mul[2] *= 4;
8822 } else if (fsize == 4771840) {
8823 if (!timestamp && nikon_e995())
8824 strcpy (model, "E995");
8825 if (strcmp(model,"E995")) {
8826 filters = 0xb4b4b4b4;
8827 simple_coeff(3);
8828 pre_mul[0] = 1.196;
8829 pre_mul[1] = 1.246;
8830 pre_mul[2] = 1.018;
8831 }
8832 } else if (fsize == 2940928) {
8833 if (!timestamp && !nikon_e2100())
8834 strcpy (model,"E2500");
8835 if (!strcmp(model,"E2500")) {
8836 height -= 2;
8837 load_flags = 6;
8838 colors = 4;
8839 filters = 0x4b4b4b4b;
8840 }
8841 } else if (fsize == 4775936) {
8842 if (!timestamp) nikon_3700();
8843 if (model[0] == 'E' && atoi(model+1) < 3700)
8844 filters = 0x49494949;
8845 if (!strcmp(model,"Optio 33WR")) {
8846 flip = 1;
8847 filters = 0x16161616;
8848 }
8849 if (make[0] == 'O') {
8850 i = find_green (12, 32, 1188864, 3576832);
8851 c = find_green (12, 32, 2383920, 2387016);
8852 if (abs(i) < abs(c)) {
8853 SWAP(i,c);
8854 load_flags = 24;
8855 }
8856 if (i < 0) filters = 0x61616161;
8857 }
8858 } else if (fsize == 5869568) {
8859 if (!timestamp && minolta_z2()) {
8860 strcpy (make, "Minolta");
8861 strcpy (model,"DiMAGE Z2");
8862 }
8863 load_flags = 6 + 24*(make[0] == 'M');
8864 } else if (fsize == 6291456) {
8865 fseek (ifp, 0x300000, SEEK_SET);
8866 if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
8867 height -= (top_margin = 16);
8868 width -= (left_margin = 28);
8869 maximum = 0xf5c0;
8870 strcpy (make, "ISG");
8871 model[0] = 0;
8872 }
8873 } else if (!strcmp(make,"Fujifilm")) {
8874 if (!strcmp(model+7,"S2Pro")) {
8875 strcpy (model,"S2Pro");
8876 height = 2144;
8877 width = 2880;
8878 flip = 6;
8879 } else if (load_raw != &CLASS packed_load_raw)
8880 maximum = (is_raw == 2 && shot_select) ? 0x2f00 : 0x3e00;
8881 top_margin = (raw_height - height) >> 2 << 1;
8882 left_margin = (raw_width - width ) >> 2 << 1;
8883 if (width == 2848 || width == 3664) filters = 0x16161616;
8884 if (width == 4032 || width == 4952) left_margin = 0;
8885 if (width == 3328 && (width -= 66)) left_margin = 34;
8886 if (width == 4936) left_margin = 4;
8887 if (!strcmp(model,"HS50EXR") ||
8888 !strcmp(model,"F900EXR")) {
8889 width += 2;
8890 left_margin = 0;
8891 filters = 0x16161616;
8892 }
8893 if (fuji_layout) raw_width *= is_raw;
8894 if (filters == 9)
8895 FORC(36) ((char *)xtrans)[c] =
8896 xtrans_abs[(c/6+top_margin) % 6][(c+left_margin) % 6];
8897 } else if (!strcmp(model,"KD-400Z")) {
8898 height = 1712;
8899 width = 2312;
8900 raw_width = 2336;
8901 goto konica_400z;
8902 } else if (!strcmp(model,"KD-510Z")) {
8903 goto konica_510z;
8904 } else if (!strcasecmp(make,"Minolta")) {
8905 if (!load_raw && (maximum = 0xfff))
8906 load_raw = &CLASS unpacked_load_raw;
8907 if (!strncmp(model,"DiMAGE A",8)) {
8908 if (!strcmp(model,"DiMAGE A200"))
8909 filters = 0x49494949;
8910 tiff_bps = 12;
8911 load_raw = &CLASS packed_load_raw;
8912 } else if (!strncmp(model,"ALPHA",5) ||
8913 !strncmp(model,"DYNAX",5) ||
8914 !strncmp(model,"MAXXUM",6)) {
8915 sprintf (model+20, "DYNAX %-10s", model+6+(model[0]=='M'));
8916 adobe_coeff (make, model+20);
8917 load_raw = &CLASS packed_load_raw;
8918 } else if (!strncmp(model,"DiMAGE G",8)) {
8919 if (model[8] == '4') {
8920 height = 1716;
8921 width = 2304;
8922 } else if (model[8] == '5') {
8923 konica_510z:
8924 height = 1956;
8925 width = 2607;
8926 raw_width = 2624;
8927 } else if (model[8] == '6') {
8928 height = 2136;
8929 width = 2848;
8930 }
8931 data_offset += 14;
8932 filters = 0x61616161;
8933 konica_400z:
8934 load_raw = &CLASS unpacked_load_raw;
8935 maximum = 0x3df;
8936 order = 0x4d4d;
8937 }
8938 } else if (!strcmp(model,"*ist D")) {
8939 load_raw = &CLASS unpacked_load_raw;
8940 data_error = -1;
8941 } else if (!strcmp(model,"*ist DS")) {
8942 height -= 2;
8943 } else if (!strcmp(make,"Samsung") && raw_width == 4704) {
8944 height -= top_margin = 8;
8945 width -= 2 * (left_margin = 8);
8946 load_flags = 32;
8947 } else if (!strcmp(make,"Samsung") && raw_height == 3714) {
8948 height -= top_margin = 18;
8949 left_margin = raw_width - (width = 5536);
8950 if (raw_width != 5600)
8951 left_margin = top_margin = 0;
8952 filters = 0x61616161;
8953 colors = 3;
8954 } else if (!strcmp(make,"Samsung") && raw_width == 5632) {
8955 order = 0x4949;
8956 height = 3694;
8957 top_margin = 2;
8958 width = 5574 - (left_margin = 32 + tiff_bps);
8959 if (tiff_bps == 12) load_flags = 80;
8960 } else if (!strcmp(make,"Samsung") && raw_width == 5664) {
8961 height -= top_margin = 17;
8962 left_margin = 96;
8963 width = 5544;
8964 filters = 0x49494949;
8965 } else if (!strcmp(make,"Samsung") && raw_width == 6496) {
8966 filters = 0x61616161;
8967 black = 1 << (tiff_bps - 7);
8968 } else if (!strcmp(model,"EX1")) {
8969 order = 0x4949;
8970 height -= 20;
8971 top_margin = 2;
8972 if ((width -= 6) > 3682) {
8973 height -= 10;
8974 width -= 46;
8975 top_margin = 8;
8976 }
8977 } else if (!strcmp(model,"WB2000")) {
8978 order = 0x4949;
8979 height -= 3;
8980 top_margin = 2;
8981 if ((width -= 10) > 3718) {
8982 height -= 28;
8983 width -= 56;
8984 top_margin = 8;
8985 }
8986 } else if (strstr(model,"WB550")) {
8987 strcpy (model, "WB550");
8988 } else if (!strcmp(model,"EX2F")) {
8989 height = 3045;
8990 width = 4070;
8991 top_margin = 3;
8992 order = 0x4949;
8993 filters = 0x49494949;
8994 load_raw = &CLASS unpacked_load_raw;
8995 } else if (!strcmp(model,"STV680 VGA")) {
8996 black = 16;
8997 } else if (!strcmp(model,"N95")) {
8998 height = raw_height - (top_margin = 2);
8999 } else if (!strcmp(model,"640x480")) {
9000 gamma_curve (0.45, 4.5, 1, 255);
9001 } else if (!strcmp(make,"Hasselblad")) {
9002 if (load_raw == &CLASS lossless_jpeg_load_raw)
9003 load_raw = &CLASS hasselblad_load_raw;
9004 if (raw_width == 7262) {
9005 height = 5444;
9006 width = 7248;
9007 top_margin = 4;
9008 left_margin = 7;
9009 filters = 0x61616161;
9010 } else if (raw_width == 7410 || raw_width == 8282) {
9011 height -= 84;
9012 width -= 82;
9013 top_margin = 4;
9014 left_margin = 41;
9015 filters = 0x61616161;
9016 } else if (raw_width == 9044) {
9017 height = 6716;
9018 width = 8964;
9019 top_margin = 8;
9020 left_margin = 40;
9021 black += load_flags = 256;
9022 maximum = 0x8101;
9023 } else if (raw_width == 4090) {
9024 strcpy (model, "V96C");
9025 height -= (top_margin = 6);
9026 width -= (left_margin = 3) + 7;
9027 filters = 0x61616161;
9028 }
9029 if (tiff_samples > 1) {
9030 is_raw = tiff_samples+1;
9031 if (!shot_select && !half_size) filters = 0;
9032 }
9033 } else if (!strcmp(make,"Sinar")) {
9034 if (!load_raw) load_raw = &CLASS unpacked_load_raw;
9035 if (is_raw > 1 && !shot_select && !half_size) filters = 0;
9036 maximum = 0x3fff;
9037 } else if (!strcmp(make,"Leaf")) {
9038 maximum = 0x3fff;
9039 fseek (ifp, data_offset, SEEK_SET);
9040 if (ljpeg_start (&jh, 1) && jh.bits == 15)
9041 maximum = 0x1fff;
9042 if (tiff_samples > 1) filters = 0;
9043 if (tiff_samples > 1 || tile_length < raw_height) {
9044 load_raw = &CLASS leaf_hdr_load_raw;
9045 raw_width = tile_width;
9046 }
9047 if ((width | height) == 2048) {
9048 if (tiff_samples == 1) {
9049 filters = 1;
9050 strcpy (cdesc, "RBTG");
9051 strcpy (model, "CatchLight");
9052 top_margin = 8; left_margin = 18; height = 2032; width = 2016;
9053 } else {
9054 strcpy (model, "DCB2");
9055 top_margin = 10; left_margin = 16; height = 2028; width = 2022;
9056 }
9057 } else if (width+height == 3144+2060) {
9058 if (!model[0]) strcpy (model, "Cantare");
9059 if (width > height) {
9060 top_margin = 6; left_margin = 32; height = 2048; width = 3072;
9061 filters = 0x61616161;
9062 } else {
9063 left_margin = 6; top_margin = 32; width = 2048; height = 3072;
9064 filters = 0x16161616;
9065 }
9066 if (!cam_mul[0] || model[0] == 'V') filters = 0;
9067 else is_raw = tiff_samples;
9068 } else if (width == 2116) {
9069 strcpy (model, "Valeo 6");
9070 height -= 2 * (top_margin = 30);
9071 width -= 2 * (left_margin = 55);
9072 filters = 0x49494949;
9073 } else if (width == 3171) {
9074 strcpy (model, "Valeo 6");
9075 height -= 2 * (top_margin = 24);
9076 width -= 2 * (left_margin = 24);
9077 filters = 0x16161616;
9078 }
9079 } else if (!strcmp(make,"Leica") || !strcmp(make,"Panasonic")) {
9080 if ((flen - data_offset) / (raw_width*8/7) == raw_height)
9081 load_raw = &CLASS panasonic_load_raw;
9082 if (!load_raw) {
9083 load_raw = &CLASS unpacked_load_raw;
9084 load_flags = 4;
9085 }
9086 zero_is_bad = 1;
9087 if ((height += 12) > raw_height) height = raw_height;
9088 for (i=0; i < sizeof pana / sizeof *pana; i++)
9089 if (raw_width == pana[i][0] && raw_height == pana[i][1]) {
9090 left_margin = pana[i][2];
9091 top_margin = pana[i][3];
9092 width += pana[i][4];
9093 height += pana[i][5];
9094 }
9095 filters = 0x01010101 * (uchar) "\x94\x61\x49\x16"
9096 [((filters-1) ^ (left_margin & 1) ^ (top_margin << 1)) & 3];
9097 } else if (!strcmp(model,"C770UZ")) {
9098 height = 1718;
9099 width = 2304;
9100 filters = 0x16161616;
9101 load_raw = &CLASS packed_load_raw;
9102 load_flags = 30;
9103 } else if (!strcmp(make,"Olympus")) {
9104 height += height & 1;
9105 if (exif_cfa) filters = exif_cfa;
9106 if (width == 4100) width -= 4;
9107 if (width == 4080) width -= 24;
9108 if (width == 9280) { width -= 6; height -= 6; }
9109 if (load_raw == &CLASS unpacked_load_raw)
9110 load_flags = 4;
9111 tiff_bps = 12;
9112 if (!strcmp(model,"E-300") ||
9113 !strcmp(model,"E-500")) {
9114 width -= 20;
9115 if (load_raw == &CLASS unpacked_load_raw) {
9116 maximum = 0xfc3;
9117 memset (cblack, 0, sizeof cblack);
9118 }
9119 } else if (!strcmp(model,"E-330")) {
9120 width -= 30;
9121 if (load_raw == &CLASS unpacked_load_raw)
9122 maximum = 0xf79;
9123 } else if (!strcmp(model,"SP550UZ")) {
9124 thumb_length = flen - (thumb_offset = 0xa39800);
9125 thumb_height = 480;
9126 thumb_width = 640;
9127 }
9128 } else if (!strcmp(model,"N Digital")) {
9129 height = 2047;
9130 width = 3072;
9131 filters = 0x61616161;
9132 data_offset = 0x1a00;
9133 load_raw = &CLASS packed_load_raw;
9134 } else if (!strcmp(model,"DSC-F828")) {
9135 width = 3288;
9136 left_margin = 5;
9137 mask[1][3] = -17;
9138 data_offset = 862144;
9139 load_raw = &CLASS sony_load_raw;
9140 filters = 0x9c9c9c9c;
9141 colors = 4;
9142 strcpy (cdesc, "RGBE");
9143 } else if (!strcmp(model,"DSC-V3")) {
9144 width = 3109;
9145 left_margin = 59;
9146 mask[0][1] = 9;
9147 data_offset = 787392;
9148 load_raw = &CLASS sony_load_raw;
9149 } else if (!strcmp(make,"Sony") && raw_width == 3984) {
9150 width = 3925;
9151 order = 0x4d4d;
9152 } else if (!strcmp(make,"Sony") && raw_width == 4288) {
9153 width -= 32;
9154 } else if (!strcmp(make,"Sony") && raw_width == 4928) {
9155 if (height < 3280) width -= 8;
9156 } else if (!strcmp(make,"Sony") && raw_width == 5504) {
9157 width -= height > 3664 ? 8 : 32;
9158 } else if (!strcmp(make,"Sony") && raw_width == 6048) {
9159 width -= 24;
9160 if (strstr(model,"RX1") || strstr(model,"A99"))
9161 width -= 6;
9162 } else if (!strcmp(make,"Sony") && raw_width == 7392) {
9163 width -= 30;
9164 } else if (!strcmp(model,"DSLR-A100")) {
9165 if (width == 3880) {
9166 height--;
9167 width = ++raw_width;
9168 } else {
9169 height -= 4;
9170 width -= 4;
9171 order = 0x4d4d;
9172 load_flags = 2;
9173 }
9174 filters = 0x61616161;
9175 } else if (!strcmp(model,"DSLR-A350")) {
9176 height -= 4;
9177 } else if (!strcmp(model,"PIXL")) {
9178 height -= top_margin = 4;
9179 width -= left_margin = 32;
9180 gamma_curve (0, 7, 1, 255);
9181 } else if (!strcmp(model,"C603") || !strcmp(model,"C330")
9182 || !strcmp(model,"12MP")) {
9183 order = 0x4949;
9184 if (filters && data_offset) {
9185 fseek (ifp, data_offset < 4096 ? 168 : 5252, SEEK_SET);
9186 read_shorts (curve, 256);
9187 } else gamma_curve (0, 3.875, 1, 255);
9188 load_raw = filters ? &CLASS eight_bit_load_raw :
9189 strcmp(model,"C330") ? &CLASS kodak_c603_load_raw :
9190 &CLASS kodak_c330_load_raw;
9191 load_flags = tiff_bps > 16;
9192 tiff_bps = 8;
9193 } else if (!strncasecmp(model,"EasyShare",9)) {
9194 data_offset = data_offset < 0x15000 ? 0x15000 : 0x17000;
9195 load_raw = &CLASS packed_load_raw;
9196 } else if (!strcasecmp(make,"Kodak")) {
9197 if (filters == UINT_MAX) filters = 0x61616161;
9198 if (!strncmp(model,"NC2000",6) ||
9199 !strncmp(model,"EOSDCS",6) ||
9200 !strncmp(model,"DCS4",4)) {
9201 width -= 4;
9202 left_margin = 2;
9203 if (model[6] == ' ') model[6] = 0;
9204 if (!strcmp(model,"DCS460A")) goto bw;
9205 } else if (!strcmp(model,"DCS660M")) {
9206 black = 214;
9207 goto bw;
9208 } else if (!strcmp(model,"DCS760M")) {
9209 bw: colors = 1;
9210 filters = 0;
9211 }
9212 if (!strcmp(model+4,"20X"))
9213 strcpy (cdesc, "MYCY");
9214 if (strstr(model,"DC25")) {
9215 strcpy (model, "DC25");
9216 data_offset = 15424;
9217 }
9218 if (!strncmp(model,"DC2",3)) {
9219 raw_height = 2 + (height = 242);
9220 if (flen < 100000) {
9221 raw_width = 256; width = 249;
9222 pixel_aspect = (4.0*height) / (3.0*width);
9223 } else {
9224 raw_width = 512; width = 501;
9225 pixel_aspect = (493.0*height) / (373.0*width);
9226 }
9227 top_margin = left_margin = 1;
9228 colors = 4;
9229 filters = 0x8d8d8d8d;
9230 simple_coeff(1);
9231 pre_mul[1] = 1.179;
9232 pre_mul[2] = 1.209;
9233 pre_mul[3] = 1.036;
9234 load_raw = &CLASS eight_bit_load_raw;
9235 } else if (!strcmp(model,"40")) {
9236 strcpy (model, "DC40");
9237 height = 512;
9238 width = 768;
9239 data_offset = 1152;
9240 load_raw = &CLASS kodak_radc_load_raw;
9241 } else if (strstr(model,"DC50")) {
9242 strcpy (model, "DC50");
9243 height = 512;
9244 width = 768;
9245 data_offset = 19712;
9246 load_raw = &CLASS kodak_radc_load_raw;
9247 } else if (strstr(model,"DC120")) {
9248 strcpy (model, "DC120");
9249 height = 976;
9250 width = 848;
9251 pixel_aspect = height/0.75/width;
9252 load_raw = tiff_compress == 7 ?
9253 &CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
9254 } else if (!strcmp(model,"DCS200")) {
9255 thumb_height = 128;
9256 thumb_width = 192;
9257 thumb_offset = 6144;
9258 thumb_misc = 360;
9259 write_thumb = &CLASS layer_thumb;
9260 black = 17;
9261 }
9262 } else if (!strcmp(model,"Fotoman Pixtura")) {
9263 height = 512;
9264 width = 768;
9265 data_offset = 3632;
9266 load_raw = &CLASS kodak_radc_load_raw;
9267 filters = 0x61616161;
9268 simple_coeff(2);
9269 } else if (!strncmp(model,"QuickTake",9)) {
9270 if (head[5]) strcpy (model+10, "200");
9271 fseek (ifp, 544, SEEK_SET);
9272 height = get2();
9273 width = get2();
9274 data_offset = (get4(),get2()) == 30 ? 738:736;
9275 if (height > width) {
9276 SWAP(height,width);
9277 fseek (ifp, data_offset-6, SEEK_SET);
9278 flip = ~get2() & 3 ? 5:6;
9279 }
9280 filters = 0x61616161;
9281 } else if (!strcmp(make,"Rollei") && !load_raw) {
9282 switch (raw_width) {
9283 case 1316:
9284 height = 1030;
9285 width = 1300;
9286 top_margin = 1;
9287 left_margin = 6;
9288 break;
9289 case 2568:
9290 height = 1960;
9291 width = 2560;
9292 top_margin = 2;
9293 left_margin = 8;
9294 }
9295 filters = 0x16161616;
9296 load_raw = &CLASS rollei_load_raw;
9297 }
9298 if (!model[0])
9299 sprintf (model, "%dx%d", width, height);
9300 if (filters == UINT_MAX) filters = 0x94949494;
9301 if (thumb_offset && !thumb_height) {
9302 fseek (ifp, thumb_offset, SEEK_SET);
9303 if (ljpeg_start (&jh, 1)) {
9304 thumb_width = jh.wide;
9305 thumb_height = jh.high;
9306 }
9307 }
9308 dng_skip:
9309 if ((use_camera_matrix & (use_camera_wb || dng_version))
9310 && cmatrix[0][0] > 0.125) {
9311 memcpy (rgb_cam, cmatrix, sizeof cmatrix);
9312 raw_color = 0;
9313 }
9314 if (raw_color) adobe_coeff (make, model);
9315 if (load_raw == &CLASS kodak_radc_load_raw)
9316 if (raw_color) adobe_coeff ("Apple","Quicktake");
9317 if (fuji_width) {
9318 fuji_width = width >> !fuji_layout;
9319 filters = fuji_width & 1 ? 0x94949494 : 0x49494949;
9320 width = (height >> fuji_layout) + fuji_width;
9321 height = width - 1;
9322 pixel_aspect = 1;
9323 } else {
9324 if (raw_height < height) raw_height = height;
9325 if (raw_width < width ) raw_width = width;
9326 }
9327 if (!tiff_bps) tiff_bps = 12;
9328 if (!maximum) maximum = (1 << tiff_bps) - 1;
9329 if (!load_raw || height < 22 || width < 22 ||
9330 tiff_bps > 16 || tiff_samples > 6 || colors > 4)
9331 is_raw = 0;
9332 #ifdef NO_JASPER
9333 if (load_raw == &CLASS redcine_load_raw) {
9334 fprintf (stderr,_("%s: You must link dcraw with %s!!\n"),
9335 ifname, "libjasper");
9336 is_raw = 0;
9337 }
9338 #endif
9339 #ifdef NO_JPEG
9340 if (load_raw == &CLASS kodak_jpeg_load_raw ||
9341 load_raw == &CLASS lossy_dng_load_raw) {
9342 fprintf (stderr,_("%s: You must link dcraw with %s!!\n"),
9343 ifname, "libjpeg");
9344 is_raw = 0;
9345 }
9346 #endif
9347 if (!cdesc[0])
9348 strcpy (cdesc, colors == 3 ? "RGBG":"GMCY");
9349 if (!raw_height) raw_height = height;
9350 if (!raw_width ) raw_width = width;
9351 if (filters > 999 && colors == 3)
9352 filters |= ((filters >> 2 & 0x22222222) |
9353 (filters << 2 & 0x88888888)) & filters << 1;
9354 notraw:
9355 if (flip == UINT_MAX) flip = tiff_flip;
9356 if (flip == UINT_MAX) flip = 0;
9357 }
9358
9359 #ifndef NO_LCMS
apply_profile(const char * input,const char * output)9360 void CLASS apply_profile (const char *input, const char *output)
9361 {
9362 char *prof;
9363 cmsHPROFILE hInProfile=0, hOutProfile=0;
9364 cmsHTRANSFORM hTransform;
9365 FILE *fp;
9366 unsigned size;
9367
9368 if (strcmp (input, "embed"))
9369 hInProfile = cmsOpenProfileFromFile (input, "r");
9370 else if (profile_length) {
9371 prof = (char *) malloc (profile_length);
9372 merror (prof, "apply_profile()");
9373 fseek (ifp, profile_offset, SEEK_SET);
9374 fread (prof, 1, profile_length, ifp);
9375 hInProfile = cmsOpenProfileFromMem (prof, profile_length);
9376 free (prof);
9377 } else
9378 fprintf (stderr,_("%s has no embedded profile.\n"), ifname);
9379 if (!hInProfile) return;
9380 if (!output)
9381 hOutProfile = cmsCreate_sRGBProfile();
9382 else if ((fp = fopen (output, "rb"))) {
9383 fread (&size, 4, 1, fp);
9384 fseek (fp, 0, SEEK_SET);
9385 oprof = (unsigned *) malloc (size = ntohl(size));
9386 merror (oprof, "apply_profile()");
9387 fread (oprof, 1, size, fp);
9388 fclose (fp);
9389 if (!(hOutProfile = cmsOpenProfileFromMem (oprof, size))) {
9390 free (oprof);
9391 oprof = 0;
9392 }
9393 } else
9394 fprintf (stderr,_("Cannot open file %s!\n"), output);
9395 if (!hOutProfile) goto quit;
9396 if (verbose)
9397 fprintf (stderr,_("Applying color profile...\n"));
9398 hTransform = cmsCreateTransform (hInProfile, TYPE_RGBA_16,
9399 hOutProfile, TYPE_RGBA_16, INTENT_PERCEPTUAL, 0);
9400 cmsDoTransform (hTransform, image, image, width*height);
9401 raw_color = 1; /* Don't use rgb_cam with a profile */
9402 cmsDeleteTransform (hTransform);
9403 cmsCloseProfile (hOutProfile);
9404 quit:
9405 cmsCloseProfile (hInProfile);
9406 }
9407 #endif
9408
convert_to_rgb()9409 void CLASS convert_to_rgb()
9410 {
9411 int row, col, c, i, j, k;
9412 ushort *img;
9413 float out[3], out_cam[3][4];
9414 double num, inverse[3][3];
9415 static const double xyzd50_srgb[3][3] =
9416 { { 0.436083, 0.385083, 0.143055 },
9417 { 0.222507, 0.716888, 0.060608 },
9418 { 0.013930, 0.097097, 0.714022 } };
9419 static const double rgb_rgb[3][3] =
9420 { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
9421 static const double adobe_rgb[3][3] =
9422 { { 0.715146, 0.284856, 0.000000 },
9423 { 0.000000, 1.000000, 0.000000 },
9424 { 0.000000, 0.041166, 0.958839 } };
9425 static const double wide_rgb[3][3] =
9426 { { 0.593087, 0.404710, 0.002206 },
9427 { 0.095413, 0.843149, 0.061439 },
9428 { 0.011621, 0.069091, 0.919288 } };
9429 static const double prophoto_rgb[3][3] =
9430 { { 0.529317, 0.330092, 0.140588 },
9431 { 0.098368, 0.873465, 0.028169 },
9432 { 0.016879, 0.117663, 0.865457 } };
9433 static const double (*out_rgb[])[3] =
9434 { rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb };
9435 static const char *name[] =
9436 { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" };
9437 static const unsigned phead[] =
9438 { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
9439 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
9440 unsigned pbody[] =
9441 { 10, 0x63707274, 0, 36, /* cprt */
9442 0x64657363, 0, 40, /* desc */
9443 0x77747074, 0, 20, /* wtpt */
9444 0x626b7074, 0, 20, /* bkpt */
9445 0x72545243, 0, 14, /* rTRC */
9446 0x67545243, 0, 14, /* gTRC */
9447 0x62545243, 0, 14, /* bTRC */
9448 0x7258595a, 0, 20, /* rXYZ */
9449 0x6758595a, 0, 20, /* gXYZ */
9450 0x6258595a, 0, 20 }; /* bXYZ */
9451 static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc };
9452 unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 };
9453
9454 gamma_curve (gamm[0], gamm[1], 0, 0);
9455 memcpy (out_cam, rgb_cam, sizeof out_cam);
9456 raw_color |= colors == 1 || document_mode ||
9457 output_color < 1 || output_color > 5;
9458 if (!raw_color) {
9459 oprof = (unsigned *) calloc (phead[0], 1);
9460 merror (oprof, "convert_to_rgb()");
9461 memcpy (oprof, phead, sizeof phead);
9462 if (output_color == 5) oprof[4] = oprof[5];
9463 oprof[0] = 132 + 12*pbody[0];
9464 for (i=0; i < pbody[0]; i++) {
9465 oprof[oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
9466 pbody[i*3+2] = oprof[0];
9467 oprof[0] += (pbody[i*3+3] + 3) & -4;
9468 }
9469 memcpy (oprof+32, pbody, sizeof pbody);
9470 oprof[pbody[5]/4+2] = strlen(name[output_color-1]) + 1;
9471 memcpy ((char *)oprof+pbody[8]+8, pwhite, sizeof pwhite);
9472 pcurve[3] = (short)(256/gamm[5]+0.5) << 16;
9473 for (i=4; i < 7; i++)
9474 memcpy ((char *)oprof+pbody[i*3+2], pcurve, sizeof pcurve);
9475 pseudoinverse ((double (*)[3]) out_rgb[output_color-1], inverse, 3);
9476 for (i=0; i < 3; i++)
9477 for (j=0; j < 3; j++) {
9478 for (num = k=0; k < 3; k++)
9479 num += xyzd50_srgb[i][k] * inverse[j][k];
9480 oprof[pbody[j*3+23]/4+i+2] = num * 0x10000 + 0.5;
9481 }
9482 for (i=0; i < phead[0]/4; i++)
9483 oprof[i] = htonl(oprof[i]);
9484 strcpy ((char *)oprof+pbody[2]+8, "auto-generated by dcraw");
9485 strcpy ((char *)oprof+pbody[5]+12, name[output_color-1]);
9486 for (i=0; i < 3; i++)
9487 for (j=0; j < colors; j++)
9488 for (out_cam[i][j] = k=0; k < 3; k++)
9489 out_cam[i][j] += out_rgb[output_color-1][i][k] * rgb_cam[k][j];
9490 }
9491 if (verbose)
9492 fprintf (stderr, raw_color ? _("Building histograms...\n") :
9493 _("Converting to %s colorspace...\n"), name[output_color-1]);
9494
9495 memset (histogram, 0, sizeof histogram);
9496 for (img=image[0], row=0; row < height; row++)
9497 for (col=0; col < width; col++, img+=4) {
9498 if (!raw_color) {
9499 out[0] = out[1] = out[2] = 0;
9500 FORCC {
9501 out[0] += out_cam[0][c] * img[c];
9502 out[1] += out_cam[1][c] * img[c];
9503 out[2] += out_cam[2][c] * img[c];
9504 }
9505 FORC3 img[c] = CLIP((int) out[c]);
9506 }
9507 else if (document_mode)
9508 img[0] = img[fcol(row,col)];
9509 FORCC histogram[c][img[c] >> 3]++;
9510 }
9511 if (colors == 4 && output_color) colors = 3;
9512 if (document_mode && filters) colors = 1;
9513 }
9514
fuji_rotate()9515 void CLASS fuji_rotate()
9516 {
9517 int i, row, col;
9518 double step;
9519 float r, c, fr, fc;
9520 unsigned ur, uc;
9521 ushort wide, high, (*img)[4], (*pix)[4];
9522
9523 if (!fuji_width) return;
9524 if (verbose)
9525 fprintf (stderr,_("Rotating image 45 degrees...\n"));
9526 fuji_width = (fuji_width - 1 + shrink) >> shrink;
9527 step = sqrt(0.5);
9528 wide = fuji_width / step;
9529 high = (height - fuji_width) / step;
9530 img = (ushort (*)[4]) calloc (high, wide*sizeof *img);
9531 merror (img, "fuji_rotate()");
9532
9533 for (row=0; row < high; row++)
9534 for (col=0; col < wide; col++) {
9535 ur = r = fuji_width + (row-col)*step;
9536 uc = c = (row+col)*step;
9537 if (ur > height-2 || uc > width-2) continue;
9538 fr = r - ur;
9539 fc = c - uc;
9540 pix = image + ur*width + uc;
9541 for (i=0; i < colors; i++)
9542 img[row*wide+col][i] =
9543 (pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
9544 (pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr;
9545 }
9546 free (image);
9547 width = wide;
9548 height = high;
9549 image = img;
9550 fuji_width = 0;
9551 }
9552
stretch()9553 void CLASS stretch()
9554 {
9555 ushort newdim, (*img)[4], *pix0, *pix1;
9556 int row, col, c;
9557 double rc, frac;
9558
9559 if (pixel_aspect == 1) return;
9560 if (verbose) fprintf (stderr,_("Stretching the image...\n"));
9561 if (pixel_aspect < 1) {
9562 newdim = height / pixel_aspect + 0.5;
9563 img = (ushort (*)[4]) calloc (width, newdim*sizeof *img);
9564 merror (img, "stretch()");
9565 for (rc=row=0; row < newdim; row++, rc+=pixel_aspect) {
9566 frac = rc - (c = rc);
9567 pix0 = pix1 = image[c*width];
9568 if (c+1 < height) pix1 += width*4;
9569 for (col=0; col < width; col++, pix0+=4, pix1+=4)
9570 FORCC img[row*width+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
9571 }
9572 height = newdim;
9573 } else {
9574 newdim = width * pixel_aspect + 0.5;
9575 img = (ushort (*)[4]) calloc (height, newdim*sizeof *img);
9576 merror (img, "stretch()");
9577 for (rc=col=0; col < newdim; col++, rc+=1/pixel_aspect) {
9578 frac = rc - (c = rc);
9579 pix0 = pix1 = image[c];
9580 if (c+1 < width) pix1 += 4;
9581 for (row=0; row < height; row++, pix0+=width*4, pix1+=width*4)
9582 FORCC img[row*newdim+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
9583 }
9584 width = newdim;
9585 }
9586 free (image);
9587 image = img;
9588 }
9589
flip_index(int row,int col)9590 int CLASS flip_index (int row, int col)
9591 {
9592 if (flip & 4) SWAP(row,col);
9593 if (flip & 2) row = iheight - 1 - row;
9594 if (flip & 1) col = iwidth - 1 - col;
9595 return row * iwidth + col;
9596 }
9597
9598 struct tiff_tag {
9599 ushort tag, type;
9600 int count;
9601 union { char c[4]; short s[2]; int i; } val;
9602 };
9603
9604 struct tiff_hdr {
9605 ushort order, magic;
9606 int ifd;
9607 ushort pad, ntag;
9608 struct tiff_tag tag[23];
9609 int nextifd;
9610 ushort pad2, nexif;
9611 struct tiff_tag exif[4];
9612 ushort pad3, ngps;
9613 struct tiff_tag gpst[10];
9614 short bps[4];
9615 int rat[10];
9616 unsigned gps[26];
9617 char desc[512], make[64], model[64], soft[32], date[20], artist[64];
9618 };
9619
tiff_set(struct tiff_hdr * th,ushort * ntag,ushort tag,ushort type,int count,int val)9620 void CLASS tiff_set (struct tiff_hdr *th, ushort *ntag,
9621 ushort tag, ushort type, int count, int val)
9622 {
9623 struct tiff_tag *tt;
9624 int c;
9625
9626 tt = (struct tiff_tag *)(ntag+1) + (*ntag)++;
9627 tt->val.i = val;
9628 if (type == 1 && count <= 4)
9629 FORC(4) tt->val.c[c] = val >> (c << 3);
9630 else if (type == 2) {
9631 count = strnlen((char *)th + val, count-1) + 1;
9632 if (count <= 4)
9633 FORC(4) tt->val.c[c] = ((char *)th)[val+c];
9634 } else if (type == 3 && count <= 2)
9635 FORC(2) tt->val.s[c] = val >> (c << 4);
9636 tt->count = count;
9637 tt->type = type;
9638 tt->tag = tag;
9639 }
9640
9641 #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
9642
tiff_head(struct tiff_hdr * th,int full)9643 void CLASS tiff_head (struct tiff_hdr *th, int full)
9644 {
9645 int c, psize=0;
9646 struct tm *t;
9647
9648 memset (th, 0, sizeof *th);
9649 th->order = htonl(0x4d4d4949) >> 16;
9650 th->magic = 42;
9651 th->ifd = 10;
9652 th->rat[0] = th->rat[2] = 300;
9653 th->rat[1] = th->rat[3] = 1;
9654 FORC(6) th->rat[4+c] = 1000000;
9655 th->rat[4] *= shutter;
9656 th->rat[6] *= aperture;
9657 th->rat[8] *= focal_len;
9658 strncpy (th->desc, desc, 512);
9659 strncpy (th->make, make, 64);
9660 strncpy (th->model, model, 64);
9661 strcpy (th->soft, "dcraw v"DCRAW_VERSION);
9662 t = localtime (×tamp);
9663 sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
9664 t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
9665 strncpy (th->artist, artist, 64);
9666 if (full) {
9667 tiff_set (th, &th->ntag, 254, 4, 1, 0);
9668 tiff_set (th, &th->ntag, 256, 4, 1, width);
9669 tiff_set (th, &th->ntag, 257, 4, 1, height);
9670 tiff_set (th, &th->ntag, 258, 3, colors, output_bps);
9671 if (colors > 2)
9672 th->tag[th->ntag-1].val.i = TOFF(th->bps);
9673 FORC4 th->bps[c] = output_bps;
9674 tiff_set (th, &th->ntag, 259, 3, 1, 1);
9675 tiff_set (th, &th->ntag, 262, 3, 1, 1 + (colors > 1));
9676 }
9677 tiff_set (th, &th->ntag, 270, 2, 512, TOFF(th->desc));
9678 tiff_set (th, &th->ntag, 271, 2, 64, TOFF(th->make));
9679 tiff_set (th, &th->ntag, 272, 2, 64, TOFF(th->model));
9680 if (full) {
9681 if (oprof) psize = ntohl(oprof[0]);
9682 tiff_set (th, &th->ntag, 273, 4, 1, sizeof *th + psize);
9683 tiff_set (th, &th->ntag, 277, 3, 1, colors);
9684 tiff_set (th, &th->ntag, 278, 4, 1, height);
9685 tiff_set (th, &th->ntag, 279, 4, 1, height*width*colors*output_bps/8);
9686 } else
9687 tiff_set (th, &th->ntag, 274, 3, 1, "12435867"[flip]-'0');
9688 tiff_set (th, &th->ntag, 282, 5, 1, TOFF(th->rat[0]));
9689 tiff_set (th, &th->ntag, 283, 5, 1, TOFF(th->rat[2]));
9690 tiff_set (th, &th->ntag, 284, 3, 1, 1);
9691 tiff_set (th, &th->ntag, 296, 3, 1, 2);
9692 tiff_set (th, &th->ntag, 305, 2, 32, TOFF(th->soft));
9693 tiff_set (th, &th->ntag, 306, 2, 20, TOFF(th->date));
9694 tiff_set (th, &th->ntag, 315, 2, 64, TOFF(th->artist));
9695 tiff_set (th, &th->ntag, 34665, 4, 1, TOFF(th->nexif));
9696 if (psize) tiff_set (th, &th->ntag, 34675, 7, psize, sizeof *th);
9697 tiff_set (th, &th->nexif, 33434, 5, 1, TOFF(th->rat[4]));
9698 tiff_set (th, &th->nexif, 33437, 5, 1, TOFF(th->rat[6]));
9699 tiff_set (th, &th->nexif, 34855, 3, 1, iso_speed);
9700 tiff_set (th, &th->nexif, 37386, 5, 1, TOFF(th->rat[8]));
9701 if (gpsdata[1]) {
9702 tiff_set (th, &th->ntag, 34853, 4, 1, TOFF(th->ngps));
9703 tiff_set (th, &th->ngps, 0, 1, 4, 0x202);
9704 tiff_set (th, &th->ngps, 1, 2, 2, gpsdata[29]);
9705 tiff_set (th, &th->ngps, 2, 5, 3, TOFF(th->gps[0]));
9706 tiff_set (th, &th->ngps, 3, 2, 2, gpsdata[30]);
9707 tiff_set (th, &th->ngps, 4, 5, 3, TOFF(th->gps[6]));
9708 tiff_set (th, &th->ngps, 5, 1, 1, gpsdata[31]);
9709 tiff_set (th, &th->ngps, 6, 5, 1, TOFF(th->gps[18]));
9710 tiff_set (th, &th->ngps, 7, 5, 3, TOFF(th->gps[12]));
9711 tiff_set (th, &th->ngps, 18, 2, 12, TOFF(th->gps[20]));
9712 tiff_set (th, &th->ngps, 29, 2, 12, TOFF(th->gps[23]));
9713 memcpy (th->gps, gpsdata, sizeof th->gps);
9714 }
9715 }
9716
jpeg_thumb()9717 void CLASS jpeg_thumb()
9718 {
9719 char *thumb;
9720 ushort exif[5];
9721 struct tiff_hdr th;
9722
9723 thumb = (char *) malloc (thumb_length);
9724 merror (thumb, "jpeg_thumb()");
9725 fread (thumb, 1, thumb_length, ifp);
9726 fputc (0xff, ofp);
9727 fputc (0xd8, ofp);
9728 if (strcmp (thumb+6, "Exif")) {
9729 memcpy (exif, "\xff\xe1 Exif\0\0", 10);
9730 exif[1] = htons (8 + sizeof th);
9731 fwrite (exif, 1, sizeof exif, ofp);
9732 tiff_head (&th, 0);
9733 fwrite (&th, 1, sizeof th, ofp);
9734 }
9735 fwrite (thumb+2, 1, thumb_length-2, ofp);
9736 free (thumb);
9737 }
9738
write_ppm_tiff()9739 void CLASS write_ppm_tiff()
9740 {
9741 struct tiff_hdr th;
9742 uchar *ppm;
9743 ushort *ppm2;
9744 int c, row, col, soff, rstep, cstep;
9745 int perc, val, total, white=0x2000;
9746
9747 perc = width * height * 0.01; /* 99th percentile white level */
9748 if (fuji_width) perc /= 2;
9749 if (!((highlight & ~2) || no_auto_bright))
9750 for (white=c=0; c < colors; c++) {
9751 for (val=0x2000, total=0; --val > 32; )
9752 if ((total += histogram[c][val]) > perc) break;
9753 if (white < val) white = val;
9754 }
9755 gamma_curve (gamm[0], gamm[1], 2, (white << 3)/bright);
9756 iheight = height;
9757 iwidth = width;
9758 if (flip & 4) SWAP(height,width);
9759 ppm = (uchar *) calloc (width, colors*output_bps/8);
9760 ppm2 = (ushort *) ppm;
9761 merror (ppm, "write_ppm_tiff()");
9762 if (output_tiff) {
9763 tiff_head (&th, 1);
9764 fwrite (&th, sizeof th, 1, ofp);
9765 if (oprof)
9766 fwrite (oprof, ntohl(oprof[0]), 1, ofp);
9767 } else if (colors > 3)
9768 fprintf (ofp,
9769 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
9770 width, height, colors, (1 << output_bps)-1, cdesc);
9771 else
9772 fprintf (ofp, "P%d\n%d %d\n%d\n",
9773 colors/2+5, width, height, (1 << output_bps)-1);
9774 soff = flip_index (0, 0);
9775 cstep = flip_index (0, 1) - soff;
9776 rstep = flip_index (1, 0) - flip_index (0, width);
9777 for (row=0; row < height; row++, soff += rstep) {
9778 for (col=0; col < width; col++, soff += cstep)
9779 if (output_bps == 8)
9780 FORCC ppm [col*colors+c] = curve[image[soff][c]] >> 8;
9781 else FORCC ppm2[col*colors+c] = curve[image[soff][c]];
9782 if (output_bps == 16 && !output_tiff && htons(0x55aa) != 0x55aa)
9783 swab (ppm2, ppm2, width*colors*2);
9784 fwrite (ppm, colors*output_bps/8, width, ofp);
9785 }
9786 free (ppm);
9787 }
9788
dcraw_main(char * raw_file)9789 int CLASS dcraw_main(char *raw_file)
9790 {
9791 int arg, status=0, quality, i, c;
9792 int timestamp_only=0, thumbnail_only=0, identify_only=0;
9793 int user_qual=-1, user_black=-1, user_sat=-1, user_flip=-1;
9794 int use_fuji_rotate=1, write_to_stdout=0, read_from_stdin=0;
9795 const char *sp, *bpfile=0, *dark_frame=0, *write_ext;
9796 char opm, opt, *ofname, *cp;
9797 struct utimbuf ut;
9798 #ifndef NO_LCMS
9799 const char *cam_profile=0, *out_profile=0;
9800 #endif
9801
9802 #ifndef LOCALTIME
9803 putenv ((char *) "TZ=UTC");
9804 #endif
9805 #ifdef LOCALEDIR
9806 setlocale (LC_CTYPE, "");
9807 setlocale (LC_MESSAGES, "");
9808 bindtextdomain ("dcraw", LOCALEDIR);
9809 textdomain ("dcraw");
9810 #endif
9811
9812 document_mode++;
9813 document_mode++;
9814 use_fuji_rotate = 0;
9815 status = 1;
9816 raw_image = 0;
9817 image = 0;
9818 oprof = 0;
9819 meta_data = ofname = 0;
9820 ofp = stdout;
9821 if (setjmp (failure)) {
9822 if (fileno(ifp) > 2) fclose(ifp);
9823 if (fileno(ofp) > 2) fclose(ofp);
9824 status = 1;
9825 goto cleanup;
9826 }
9827 ifname = raw_file;
9828 if (!(ifp = fopen (ifname, "rb"))) {
9829 perror (ifname);
9830 return 1;
9831 }
9832 status = (identify(),!is_raw);
9833 if (user_flip >= 0)
9834 flip = user_flip;
9835 switch ((flip+3600) % 360) {
9836 case 270: flip = 5; break;
9837 case 180: flip = 3; break;
9838 case 90: flip = 6;
9839 }
9840 write_fun = &CLASS write_ppm_tiff;
9841 if (load_raw == &CLASS kodak_ycbcr_load_raw) {
9842 height += height & 1;
9843 width += width & 1;
9844 }
9845 if (!is_raw)
9846 fprintf (stderr,_("Cannot decode file %s\n"), ifname);
9847 if (!is_raw) goto cleanup;
9848 shrink = filters && (half_size || (!identify_only &&
9849 (threshold || aber[0] != 1 || aber[2] != 1)));
9850 iheight = (height + shrink) >> shrink;
9851 iwidth = (width + shrink) >> shrink;
9852 if (meta_length) {
9853 meta_data = (char *) malloc (meta_length);
9854 merror (meta_data, "main()");
9855 }
9856 if (filters || colors == 1) {
9857 raw_image = (ushort *) calloc ((raw_height+7), raw_width*2);
9858 merror (raw_image, "main()");
9859 } else {
9860 image = (ushort (*)[4]) calloc (iheight, iwidth*sizeof *image);
9861 merror (image, "main()");
9862 }
9863 if (verbose)
9864 fprintf (stderr,_("Loading %s %s image from %s ...\n"),
9865 make, model, ifname);
9866 if (shot_select >= is_raw)
9867 fprintf (stderr,_("%s: \"-s %d\" requests a nonexistent image!\n"),
9868 ifname, shot_select);
9869 fseeko (ifp, data_offset, SEEK_SET);
9870 if(load_raw != &sony_arw2_load_raw) {
9871 fprintf(stderr, "not a sony ARW\n");
9872 goto cleanup;
9873 }
9874 fprintf(stderr, "IDENT: data_at=%u, size=%ux%u, half=%ux%u\n", data_offset, raw_width, raw_height, raw_width >> 1, raw_height >> 1);
9875 (*load_raw)();
9876 cleanup:
9877 if (meta_data) free (meta_data);
9878 if (ofname) free (ofname);
9879 if (oprof) free (oprof);
9880 if (image) free (image);
9881 if (multi_out) {
9882 if (++shot_select < is_raw) arg--;
9883 else shot_select = 0;
9884 }
9885
9886 return status;
9887 }
9888
main(int argc,char ** argv)9889 int main(int argc, char **argv)
9890 {
9891 if(argc < 2) {
9892 fprintf(stderr, "need a Sony ARW file as argument\n");
9893 return 1;
9894 }
9895 return dcraw_main(argv[1]);
9896 }
9897