1 /*
2 * OpenMP multithread version of dcraw
3 * based on UFRaw dcraw_indi.c by Udi Fuchs
4 */
5 /*
6 dcraw.c -- Dave Coffin's raw photo decoder
7 Copyright 1997-2018 by Dave Coffin, dcoffin a cybercom o net
8
9 This is a command-line ANSI C program to convert raw photos from
10 any digital camera on any computer running any operating system.
11
12 No license is required to download and use dcraw.c. However,
13 to lawfully redistribute dcraw, you must either (a) offer, at
14 no extra charge, full source code* for all executable files
15 containing RESTRICTED functions, (b) distribute this code under
16 the GPL Version 2 or later, (c) remove all RESTRICTED functions,
17 re-implement them, or copy them from an earlier, unrestricted
18 Revision of dcraw.c, or (d) purchase a license from the author.
19
20 The functions that process Foveon images have been RESTRICTED
21 since Revision 1.237. All other code remains free for all uses.
22
23 *If you have not modified dcraw.c in any way, a link to my
24 homepage qualifies as "full source code".
25
26 $Revision: 1.478 $
27 $Date: 2018/06/01 20:36:25 $
28 */
29
30 #define DCRAW_VERSION "9.28"
31
32 #ifndef _GNU_SOURCE
33 #define _GNU_SOURCE
34 #endif
35 #define _USE_MATH_DEFINES
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <float.h>
40 #include <limits.h>
41 #include <math.h>
42 #include <setjmp.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <time.h>
47 #include <sys/types.h>
48
49 #if defined(DJGPP) || defined(__MINGW32__) || _MSC_VER <= 1900
50 #define fseeko fseek
51 #define ftello ftell
52 #else
53 #define fgetc getc_unlocked
54 #endif
55 #ifdef __CYGWIN__
56 #include <io.h>
57 #endif
58 #ifdef WIN32
59 #include <sys/utime.h>
60 #include <winsock2.h>
61 #pragma comment(lib, "ws2_32.lib")
62 #define snprintf _snprintf
63 #define strcasecmp stricmp
64 #define strncasecmp strnicmp
65 typedef __int64 INT64;
66 typedef unsigned __int64 UINT64;
67 #else
68 #include <unistd.h>
69 #include <utime.h>
70 #include <netinet/in.h>
71 typedef long long INT64;
72 typedef unsigned long long UINT64;
73 #endif
74
75 #ifdef NODEPS
76 #define NO_JASPER
77 #define NO_JPEG
78 #define NO_LCMS
79 #endif
80 #ifndef NO_JASPER
81 #include <jasper/jasper.h> /* Decode Red camera movies */
82 #endif
83 #ifndef NO_JPEG
84 #include <jpeglib.h> /* Decode compressed Kodak DC120 photos */
85 #endif /* and Adobe Lossy DNGs */
86 #ifndef NO_LCMS
87 #include <lcms2.h> /* Support color profiles */
88 #endif
89 #ifdef LOCALEDIR
90 #include <libintl.h>
91 #define _(String) gettext(String)
92 #else
93 #define _(String) (String)
94 #endif
95
96 #if !defined(uchar)
97 #define uchar unsigned char
98 #endif
99 #if !defined(ushort)
100 #define ushort unsigned short
101 #endif
102
103 #ifdef _OPENMP
104 #include <omp.h>
105 #define uf_omp_get_thread_num() omp_get_thread_num()
106 #define uf_omp_get_num_threads() omp_get_num_threads()
107 #else
108 #define uf_omp_get_thread_num() 0
109 #define uf_omp_get_num_threads() 1
110 #endif
111
112 /*
113 All global variables are defined here, and all functions that
114 access them are prefixed with "CLASS". For thread-safety, all
115 non-const static local variables except cbrt[] must be declared
116 "thread_local".
117 */
118 FILE *ifp, *ofp;
119 short order;
120 const char *ifname;
121 char *meta_data, xtrans[6][6], xtrans_abs[6][6];
122 char cdesc[5], desc[512], make[64], model[64], model2[64], artist[64];
123 float flash_used, canon_ev, iso_speed, shutter, aperture, focal_len;
124 time_t timestamp;
125 off_t strip_offset, data_offset;
126 off_t thumb_offset, meta_offset, profile_offset;
127 unsigned shot_order, kodak_cbpp, exif_cfa, unique_id;
128 unsigned thumb_length, meta_length, profile_length;
129 unsigned thumb_misc, *oprof, fuji_layout, shot_select=0, multi_out=0;
130 unsigned tiff_nifds, tiff_samples, tiff_bps, tiff_compress;
131 unsigned black, maximum, mix_green, raw_color, zero_is_bad;
132 unsigned zero_after_ff, is_raw, dng_version, is_foveon, data_error;
133 unsigned tile_width, tile_length, gpsdata[32], load_flags;
134 unsigned flip, tiff_flip, filters, colors;
135 ushort raw_height, raw_width, height, width, top_margin, left_margin;
136 ushort shrink, iheight, iwidth, fuji_width, thumb_width, thumb_height;
137 ushort *raw_image, (*image)[4], cblack[4102];
138 ushort white[8][8], curve[0x10000], cr2_slice[3], sraw_mul[4];
139 double pixel_aspect, aber[4]={1,1,1,1}, gamm[6]={ 0.45,4.5,0,0,0,0 };
140 float bright=1, user_mul[4]={0,0,0,0}, threshold=0;
141 int mask[8][4];
142 int half_size=0, four_color_rgb=0, document_mode=0, highlight=0;
143 int verbose=0, use_auto_wb=0, use_camera_wb=0, use_camera_matrix=1;
144 int output_color=1, output_bps=8, output_tiff=0, med_passes=0;
145 int no_auto_bright=0;
146 unsigned greybox[4] = { 0, 0, UINT_MAX, UINT_MAX };
147 float cam_mul[4], pre_mul[4], cmatrix[3][4], rgb_cam[3][4];
148 #ifndef LIGHTZONE
149 const
150 #endif
151 double xyz_rgb[3][3] = { /* XYZ from RGB */
152 { 0.412453, 0.357580, 0.180423 },
153 { 0.212671, 0.715160, 0.072169 },
154 { 0.019334, 0.119193, 0.950227 } };
155 const float d65_white[3] = { 0.950456, 1, 1.088754 };
156 int histogram[4][0x2000];
157 void (*write_thumb)(), (*write_fun)();
158 void (*load_raw)(), (*thumb_load_raw)();
159 jmp_buf failure;
160
161 struct decode {
162 struct decode *branch[2];
163 int leaf;
164 } first_decode[2048], *second_decode, *free_decode;
165
166 struct tiff_ifd {
167 int width, height, bps, comp, phint, offset, flip, samples, bytes;
168 int tile_width, tile_length;
169 float shutter;
170 } tiff_ifd[10];
171
172 struct ph1 {
173 int format, key_off, tag_21a;
174 int black, split_col, black_col, split_row, black_row;
175 float tag_210;
176 } ph1;
177
178 #define CLASS
179
180 #define FORC(cnt) for (c=0; c < cnt; c++)
181 #define FORC3 FORC(3)
182 #define FORC4 FORC(4)
183 #define FORCC FORC(colors)
184
185 #define SQR(x) ((x)*(x))
186 #define ABS(x) (((int)(x) ^ ((int)(x) >> 31)) - ((int)(x) >> 31))
187 #define MIN(a,b) ((a) < (b) ? (a) : (b))
188 #define MAX(a,b) ((a) > (b) ? (a) : (b))
189 #define LIM(x,min,max) MAX(min,MIN(x,max))
190 #define ULIM(x,y,z) ((y) < (z) ? LIM(x,y,z) : LIM(x,z,y))
191 #define CLIP(x) LIM((int)(x),0,65535)
192 #define SWAP(a,b) { a=a+b; b=a-b; a=a-b; }
193
194 /*
195 In order to inline this calculation, I make the risky
196 assumption that all filter patterns can be described
197 by a repeating pattern of eight rows and two columns
198
199 Do not use the FC or BAYER macros with the Leaf CatchLight,
200 because its pattern is 16x16, not 2x8.
201
202 Return values are either 0/1/2/3 = G/M/C/Y or 0/1/2/3 = R/G1/B/G2
203
204 PowerShot 600 PowerShot A50 PowerShot Pro70 Pro90 & G1
205 0xe1e4e1e4: 0x1b4e4b1e: 0x1e4b4e1b: 0xb4b4b4b4:
206
207 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
208 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
209 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
210 2 M G M G M G 2 Y C Y C Y C 2 C Y C Y C Y
211 3 C Y C Y C Y 3 G M G M G M 3 G M G M G M
212 4 C Y C Y C Y 4 Y C Y C Y C
213 PowerShot A5 5 G M G M G M 5 G M G M G M
214 0x1e4e1e4e: 6 Y C Y C Y C 6 C Y C Y C Y
215 7 M G M G M G 7 M G M G M G
216 0 1 2 3 4 5
217 0 C Y C Y C Y
218 1 G M G M G M
219 2 C Y C Y C Y
220 3 M G M G M G
221
222 All RGB cameras use one of these Bayer grids:
223
224 0x16161616: 0x61616161: 0x49494949: 0x94949494:
225
226 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
227 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
228 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
229 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
230 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
231 */
232
233 #define RAW(row,col) \
234 raw_image[(row)*raw_width+(col)]
235
236 #define FC(row,col) \
237 (filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
238
239 #define BAYER(row,col) \
240 image[((row) >> shrink)*iwidth + ((col) >> shrink)][FC(row,col)]
241
242 #define BAYER2(row,col) \
243 image[((row) >> shrink)*iwidth + ((col) >> shrink)][fcol(row,col)]
244
fcol(int row,int col)245 int CLASS fcol (int row, int col)
246 {
247 static const char filter[16][16] =
248 { { 2,1,1,3,2,3,2,0,3,2,3,0,1,2,1,0 },
249 { 0,3,0,2,0,1,3,1,0,1,1,2,0,3,3,2 },
250 { 2,3,3,2,3,1,1,3,3,1,2,1,2,0,0,3 },
251 { 0,1,0,1,0,2,0,2,2,0,3,0,1,3,2,1 },
252 { 3,1,1,2,0,1,0,2,1,3,1,3,0,1,3,0 },
253 { 2,0,0,3,3,2,3,1,2,0,2,0,3,2,2,1 },
254 { 2,3,3,1,2,1,2,1,2,1,1,2,3,0,0,1 },
255 { 1,0,0,2,3,0,0,3,0,3,0,3,2,1,2,3 },
256 { 2,3,3,1,1,2,1,0,3,2,3,0,2,3,1,3 },
257 { 1,0,2,0,3,0,3,2,0,1,1,2,0,1,0,2 },
258 { 0,1,1,3,3,2,2,1,1,3,3,0,2,1,3,2 },
259 { 2,3,2,0,0,1,3,0,2,0,1,2,3,0,1,0 },
260 { 1,3,1,2,3,2,3,2,0,2,0,1,1,0,3,0 },
261 { 0,2,0,3,1,0,0,1,1,3,3,2,3,2,2,1 },
262 { 2,1,3,2,3,1,2,1,0,3,0,2,0,2,0,2 },
263 { 0,3,1,0,0,2,0,3,2,1,3,1,1,3,1,3 } };
264
265 if (filters == 1) return filter[(row+top_margin)&15][(col+left_margin)&15];
266 if (filters == 9) return xtrans[(row+6) % 6][(col+6) % 6];
267 return FC(row,col);
268 }
269
270 #ifndef __GLIBC__
my_memmem(char * haystack,size_t haystacklen,char * needle,size_t needlelen)271 char *my_memmem (char *haystack, size_t haystacklen,
272 char *needle, size_t needlelen)
273 {
274 char *c;
275 for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
276 if (!memcmp (c, needle, needlelen))
277 return c;
278 return 0;
279 }
280 #define memmem my_memmem
my_strcasestr(char * haystack,const char * needle)281 char *my_strcasestr (char *haystack, const char *needle)
282 {
283 char *c;
284 for (c = haystack; *c; c++)
285 if (!strncasecmp(c, needle, strlen(needle)))
286 return c;
287 return 0;
288 }
289 #define strcasestr my_strcasestr
290 #endif
291
merror(void * ptr,const char * where)292 void CLASS merror (void *ptr, const char *where)
293 {
294 if (ptr) return;
295 fprintf (stderr,_("%s: Out of memory in %s\n"), ifname, where);
296 longjmp (failure, 1);
297 }
298
derror()299 void CLASS derror()
300 {
301 if (!data_error) {
302 fprintf (stderr, "%s: ", ifname);
303 if (feof(ifp))
304 fprintf (stderr,_("Unexpected end of file\n"));
305 else
306 fprintf (stderr,_("Corrupt data near 0x%llx\n"), (INT64) ftello(ifp));
307 }
308 data_error++;
309 }
310
sget2(uchar * s)311 ushort CLASS sget2 (uchar *s)
312 {
313 if (order == 0x4949) /* "II" means little-endian */
314 return s[0] | s[1] << 8;
315 else /* "MM" means big-endian */
316 return s[0] << 8 | s[1];
317 }
318
get2()319 ushort CLASS get2()
320 {
321 uchar str[2] = { 0xff,0xff };
322 fread (str, 1, 2, ifp);
323 return sget2(str);
324 }
325
sget4(uchar * s)326 unsigned CLASS sget4 (uchar *s)
327 {
328 if (order == 0x4949)
329 return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
330 else
331 return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
332 }
333 #define sget4(s) sget4((uchar *)s)
334
get4()335 unsigned CLASS get4()
336 {
337 uchar str[4] = { 0xff,0xff,0xff,0xff };
338 fread (str, 1, 4, ifp);
339 return sget4(str);
340 }
341
getint(int type)342 unsigned CLASS getint (int type)
343 {
344 return type == 3 ? get2() : get4();
345 }
346
int_to_float(int i)347 float CLASS int_to_float (int i)
348 {
349 union { int i; float f; } u;
350 u.i = i;
351 return u.f;
352 }
353
getreal(int type)354 double CLASS getreal (int type)
355 {
356 union { char c[8]; double d; } u;
357 int i, rev;
358
359 switch (type) {
360 case 3: return (unsigned short) get2();
361 case 4: return (unsigned int) get4();
362 case 5: u.d = (unsigned int) get4();
363 return u.d / (unsigned int) get4();
364 case 8: return (signed short) get2();
365 case 9: return (signed int) get4();
366 case 10: u.d = (signed int) get4();
367 return u.d / (signed int) get4();
368 case 11: return int_to_float (get4());
369 case 12:
370 rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
371 for (i=0; i < 8; i++)
372 u.c[i ^ rev] = fgetc(ifp);
373 return u.d;
374 default: return fgetc(ifp);
375 }
376 }
377
read_shorts(ushort * pixel,int count)378 void CLASS read_shorts (ushort *pixel, int count)
379 {
380 if (fread (pixel, 2, count, ifp) < count) derror();
381 if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
382 swab (pixel, pixel, count*2);
383 }
384
cubic_spline(const int * x_,const int * y_,const int len)385 void CLASS cubic_spline (const int *x_, const int *y_, const int len)
386 {
387 float **A, *b, *c, *d, *x, *y;
388 int i, j;
389
390 A = (float **) calloc (((2*len + 4)*sizeof **A + sizeof *A), 2*len);
391 if (!A) return;
392 A[0] = (float *) (A + 2*len);
393 for (i = 1; i < 2*len; i++)
394 A[i] = A[0] + 2*len*i;
395 y = len + (x = i + (d = i + (c = i + (b = A[0] + i*i))));
396 for (i = 0; i < len; i++) {
397 x[i] = x_[i] / 65535.0;
398 y[i] = y_[i] / 65535.0;
399 }
400 for (i = len-1; i > 0; i--) {
401 b[i] = (y[i] - y[i-1]) / (x[i] - x[i-1]);
402 d[i-1] = x[i] - x[i-1];
403 }
404 for (i = 1; i < len-1; i++) {
405 A[i][i] = 2 * (d[i-1] + d[i]);
406 if (i > 1) {
407 A[i][i-1] = d[i-1];
408 A[i-1][i] = d[i-1];
409 }
410 A[i][len-1] = 6 * (b[i+1] - b[i]);
411 }
412 for(i = 1; i < len-2; i++) {
413 float v = A[i+1][i] / A[i][i];
414 for(j = 1; j <= len-1; j++)
415 A[i+1][j] -= v * A[i][j];
416 }
417 for(i = len-2; i > 0; i--) {
418 float acc = 0;
419 for(j = i; j <= len-2; j++)
420 acc += A[i][j]*c[j];
421 c[i] = (A[i][len-1] - acc) / A[i][i];
422 }
423 for (i = 0; i < 0x10000; i++) {
424 float x_out = (float)(i / 65535.0);
425 float y_out = 0;
426 for (j = 0; j < len-1; j++) {
427 if (x[j] <= x_out && x_out <= x[j+1]) {
428 float v = x_out - x[j];
429 y_out = y[j] +
430 ((y[j+1] - y[j]) / d[j] - (2 * d[j] * c[j] + c[j+1] * d[j])/6) * v
431 + (c[j] * 0.5) * v*v + ((c[j+1] - c[j]) / (6 * d[j])) * v*v*v;
432 }
433 }
434 curve[i] = y_out < 0.0 ? 0 : (y_out >= 1.0 ? 65535 :
435 (ushort)(y_out * 65535.0 + 0.5));
436 }
437 free (A);
438 }
439
canon_600_fixed_wb(int temp)440 void CLASS canon_600_fixed_wb (int temp)
441 {
442 static const short mul[4][5] = {
443 { 667, 358,397,565,452 },
444 { 731, 390,367,499,517 },
445 { 1119, 396,348,448,537 },
446 { 1399, 485,431,508,688 } };
447 int lo, hi, i;
448 float frac=0;
449
450 for (lo=4; --lo; )
451 if (*mul[lo] <= temp) break;
452 for (hi=0; hi < 3; hi++)
453 if (*mul[hi] >= temp) break;
454 if (lo != hi)
455 frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
456 for (i=1; i < 5; i++)
457 pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
458 }
459
460 /* Return values: 0 = white 1 = near white 2 = not white */
canon_600_color(int ratio[2],int mar)461 int CLASS canon_600_color (int ratio[2], int mar)
462 {
463 int clipped=0, target, miss;
464
465 if (flash_used) {
466 if (ratio[1] < -104)
467 { ratio[1] = -104; clipped = 1; }
468 if (ratio[1] > 12)
469 { ratio[1] = 12; clipped = 1; }
470 } else {
471 if (ratio[1] < -264 || ratio[1] > 461) return 2;
472 if (ratio[1] < -50)
473 { ratio[1] = -50; clipped = 1; }
474 if (ratio[1] > 307)
475 { ratio[1] = 307; clipped = 1; }
476 }
477 target = flash_used || ratio[1] < 197
478 ? -38 - (398 * ratio[1] >> 10)
479 : -123 + (48 * ratio[1] >> 10);
480 if (target - mar <= ratio[0] &&
481 target + 20 >= ratio[0] && !clipped) return 0;
482 miss = target - ratio[0];
483 if (abs(miss) >= mar*4) return 2;
484 if (miss < -20) miss = -20;
485 if (miss > mar) miss = mar;
486 ratio[0] = target - miss;
487 return 1;
488 }
489
canon_600_auto_wb()490 void CLASS canon_600_auto_wb()
491 {
492 int mar, row, col, i, j, st, count[] = { 0,0 };
493 int test[8], total[2][8], ratio[2][2], stat[2];
494
495 memset (&total, 0, sizeof total);
496 i = canon_ev + 0.5;
497 if (i < 10) mar = 150;
498 else if (i > 12) mar = 20;
499 else mar = 280 - 20 * i;
500 if (flash_used) mar = 80;
501 for (row=14; row < height-14; row+=4)
502 for (col=10; col < width; col+=2) {
503 for (i=0; i < 8; i++)
504 test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
505 BAYER(row+(i >> 1),col+(i & 1));
506 for (i=0; i < 8; i++)
507 if (test[i] < 150 || test[i] > 1500) goto next;
508 for (i=0; i < 4; i++)
509 if (abs(test[i] - test[i+4]) > 50) goto next;
510 for (i=0; i < 2; i++) {
511 for (j=0; j < 4; j+=2)
512 ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
513 stat[i] = canon_600_color (ratio[i], mar);
514 }
515 if ((st = stat[0] | stat[1]) > 1) goto next;
516 for (i=0; i < 2; i++)
517 if (stat[i])
518 for (j=0; j < 2; j++)
519 test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
520 for (i=0; i < 8; i++)
521 total[st][i] += test[i];
522 count[st]++;
523 next: ;
524 }
525 if (count[0] | count[1]) {
526 st = count[0]*200 < count[1];
527 for (i=0; i < 4; i++)
528 pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
529 }
530 }
531
canon_600_coeff()532 void CLASS canon_600_coeff()
533 {
534 static const short table[6][12] = {
535 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
536 { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
537 { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
538 { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
539 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
540 { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
541 int t=0, i, c;
542 float mc, yc;
543
544 mc = pre_mul[1] / pre_mul[2];
545 yc = pre_mul[3] / pre_mul[2];
546 if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
547 if (mc > 1.28 && mc <= 2) {
548 if (yc < 0.8789) t=3;
549 else if (yc <= 2) t=4;
550 }
551 if (flash_used) t=5;
552 for (raw_color = i=0; i < 3; i++)
553 FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
554 }
555
canon_600_load_raw()556 void CLASS canon_600_load_raw()
557 {
558 uchar data[1120], *dp;
559 ushort *pix;
560 int irow, row;
561
562 for (irow=row=0; irow < height; irow++) {
563 if (fread (data, 1, 1120, ifp) < 1120) derror();
564 pix = raw_image + row*raw_width;
565 for (dp=data; dp < data+1120; dp+=10, pix+=8) {
566 pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
567 pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
568 pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
569 pix[3] = (dp[4] << 2) + (dp[1] & 3);
570 pix[4] = (dp[5] << 2) + (dp[9] & 3);
571 pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
572 pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
573 pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
574 }
575 if ((row+=2) > height) row = 1;
576 }
577 }
578
canon_600_correct()579 void CLASS canon_600_correct()
580 {
581 int row, col, val;
582 static const short mul[4][2] =
583 { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
584
585 for (row=0; row < height; row++)
586 for (col=0; col < width; col++) {
587 if ((val = BAYER(row,col) - black) < 0) val = 0;
588 val = val * mul[row & 3][col & 1] >> 9;
589 BAYER(row,col) = val;
590 }
591 canon_600_fixed_wb(1311);
592 canon_600_auto_wb();
593 canon_600_coeff();
594 maximum = (0x3ff - black) * 1109 >> 9;
595 black = 0;
596 }
597
canon_s2is()598 int CLASS canon_s2is()
599 {
600 unsigned row;
601
602 for (row=0; row < 100; row++) {
603 fseek (ifp, row*3340 + 3284, SEEK_SET);
604 if (getc(ifp) > 15) return 1;
605 }
606 return 0;
607 }
608
getbithuff(int nbits,ushort * huff)609 unsigned CLASS getbithuff (int nbits, ushort *huff)
610 {
611 static unsigned bitbuf=0;
612 static int vbits=0, reset=0;
613 unsigned c;
614
615 if (nbits > 25) return 0;
616 if (nbits < 0)
617 return bitbuf = vbits = reset = 0;
618 if (nbits == 0 || vbits < 0) return 0;
619 while (!reset && vbits < nbits && (c = fgetc(ifp)) != EOF &&
620 !(reset = zero_after_ff && c == 0xff && fgetc(ifp))) {
621 bitbuf = (bitbuf << 8) + (uchar) c;
622 vbits += 8;
623 }
624 c = bitbuf << (32-vbits) >> (32-nbits);
625 if (huff) {
626 vbits -= huff[c] >> 8;
627 c = (uchar) huff[c];
628 } else
629 vbits -= nbits;
630 if (vbits < 0) derror();
631 return c;
632 }
633
634 #define getbits(n) getbithuff(n,0)
635 #define gethuff(h) getbithuff(*h,h+1)
636
637 /*
638 Construct a decode tree according the specification in *source.
639 The first 16 bytes specify how many codes should be 1-bit, 2-bit
640 3-bit, etc. Bytes after that are the leaf values.
641
642 For example, if the source is
643
644 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
645 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
646
647 then the code is
648
649 00 0x04
650 010 0x03
651 011 0x05
652 100 0x06
653 101 0x02
654 1100 0x07
655 1101 0x01
656 11100 0x08
657 11101 0x09
658 11110 0x00
659 111110 0x0a
660 1111110 0x0b
661 1111111 0xff
662 */
make_decoder_ref(const uchar ** source)663 ushort * CLASS make_decoder_ref (const uchar **source)
664 {
665 int max, len, h, i, j;
666 const uchar *count;
667 ushort *huff;
668
669 count = (*source += 16) - 17;
670 for (max=16; max && !count[max]; max--);
671 huff = (ushort *) calloc (1 + (1 << max), sizeof *huff);
672 merror (huff, "make_decoder()");
673 huff[0] = max;
674 for (h=len=1; len <= max; len++)
675 for (i=0; i < count[len]; i++, ++*source)
676 for (j=0; j < 1 << (max-len); j++)
677 if (h <= 1 << max)
678 huff[h++] = len << 8 | **source;
679 return huff;
680 }
681
make_decoder(const uchar * source)682 ushort * CLASS make_decoder (const uchar *source)
683 {
684 return make_decoder_ref (&source);
685 }
686
crw_init_tables(unsigned table,ushort * huff[2])687 void CLASS crw_init_tables (unsigned table, ushort *huff[2])
688 {
689 static const uchar first_tree[3][29] = {
690 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
691 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
692 { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
693 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
694 { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
695 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
696 };
697 static const uchar second_tree[3][180] = {
698 { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
699 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
700 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
701 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
702 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
703 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
704 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
705 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
706 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
707 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
708 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
709 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
710 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
711 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
712 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
713 { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
714 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
715 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
716 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
717 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
718 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
719 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
720 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
721 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
722 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
723 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
724 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
725 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
726 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
727 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
728 { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
729 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
730 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
731 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
732 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
733 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
734 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
735 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
736 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
737 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
738 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
739 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
740 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
741 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
742 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
743 };
744 if (table > 2) table = 2;
745 huff[0] = make_decoder ( first_tree[table]);
746 huff[1] = make_decoder (second_tree[table]);
747 }
748
749 /*
750 Return 0 if the image starts with compressed data,
751 1 if it starts with uncompressed low-order bits.
752
753 In Canon compressed data, 0xff is always followed by 0x00.
754 */
canon_has_lowbits()755 int CLASS canon_has_lowbits()
756 {
757 uchar test[0x4000];
758 int ret=1, i;
759
760 fseek (ifp, 0, SEEK_SET);
761 fread (test, 1, sizeof test, ifp);
762 for (i=540; i < sizeof test - 1; i++)
763 if (test[i] == 0xff) {
764 if (test[i+1]) return 1;
765 ret=0;
766 }
767 return ret;
768 }
769
canon_load_raw()770 void CLASS canon_load_raw()
771 {
772 ushort *pixel, *prow, *huff[2];
773 int nblocks, lowbits, i, c, row, r, save, val;
774 int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
775
776 crw_init_tables (tiff_compress, huff);
777 lowbits = canon_has_lowbits();
778 if (!lowbits) maximum = 0x3ff;
779 fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
780 zero_after_ff = 1;
781 getbits(-1);
782 for (row=0; row < raw_height; row+=8) {
783 pixel = raw_image + row*raw_width;
784 nblocks = MIN (8, raw_height-row) * raw_width >> 6;
785 for (block=0; block < nblocks; block++) {
786 memset (diffbuf, 0, sizeof diffbuf);
787 for (i=0; i < 64; i++ ) {
788 leaf = gethuff(huff[i > 0]);
789 if (leaf == 0 && i) break;
790 if (leaf == 0xff) continue;
791 i += leaf >> 4;
792 len = leaf & 15;
793 if (len == 0) continue;
794 diff = getbits(len);
795 if ((diff & (1 << (len-1))) == 0)
796 diff -= (1 << len) - 1;
797 if (i < 64) diffbuf[i] = diff;
798 }
799 diffbuf[0] += carry;
800 carry = diffbuf[0];
801 for (i=0; i < 64; i++ ) {
802 if (pnum++ % raw_width == 0)
803 base[0] = base[1] = 512;
804 if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
805 derror();
806 }
807 }
808 if (lowbits) {
809 save = ftell(ifp);
810 fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
811 for (prow=pixel, i=0; i < raw_width*2; i++) {
812 c = fgetc(ifp);
813 for (r=0; r < 8; r+=2, prow++) {
814 val = (*prow << 2) + ((c >> r) & 3);
815 if (raw_width == 2672 && val < 512) val += 2;
816 *prow = val;
817 }
818 }
819 fseek (ifp, save, SEEK_SET);
820 }
821 }
822 FORC(2) free (huff[c]);
823 }
824
825 struct jhead {
826 int algo, bits, high, wide, clrs, sraw, psv, restart, vpred[6];
827 ushort quant[64], idct[64], *huff[20], *free[20], *row;
828 };
829
ljpeg_start(struct jhead * jh,int info_only)830 int CLASS ljpeg_start (struct jhead *jh, int info_only)
831 {
832 ushort c, tag, len;
833 uchar data[0x10000];
834 const uchar *dp;
835
836 memset (jh, 0, sizeof *jh);
837 jh->restart = INT_MAX;
838 if ((fgetc(ifp),fgetc(ifp)) != 0xd8) return 0;
839 do {
840 if (!fread (data, 2, 2, ifp)) return 0;
841 tag = data[0] << 8 | data[1];
842 len = (data[2] << 8 | data[3]) - 2;
843 if (tag <= 0xff00) return 0;
844 fread (data, 1, len, ifp);
845 switch (tag) {
846 case 0xffc3:
847 jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
848 case 0xffc1:
849 case 0xffc0:
850 jh->algo = tag & 0xff;
851 jh->bits = data[0];
852 jh->high = data[1] << 8 | data[2];
853 jh->wide = data[3] << 8 | data[4];
854 jh->clrs = data[5] + jh->sraw;
855 if (len == 9 && !dng_version) getc(ifp);
856 break;
857 case 0xffc4:
858 if (info_only) break;
859 for (dp = data; dp < data+len && !((c = *dp++) & -20); )
860 jh->free[c] = jh->huff[c] = make_decoder_ref (&dp);
861 break;
862 case 0xffda:
863 jh->psv = data[1+data[0]*2];
864 jh->bits -= data[3+data[0]*2] & 15;
865 break;
866 case 0xffdb:
867 FORC(64) jh->quant[c] = data[c*2+1] << 8 | data[c*2+2];
868 break;
869 case 0xffdd:
870 jh->restart = data[0] << 8 | data[1];
871 }
872 } while (tag != 0xffda);
873 if (jh->bits > 16 || jh->clrs > 6 ||
874 !jh->bits || !jh->high || !jh->wide || !jh->clrs) return 0;
875 if (info_only) return 1;
876 if (!jh->huff[0]) return 0;
877 FORC(19) if (!jh->huff[c+1]) jh->huff[c+1] = jh->huff[c];
878 if (jh->sraw) {
879 FORC(4) jh->huff[2+c] = jh->huff[1];
880 FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
881 }
882 jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
883 merror (jh->row, "ljpeg_start()");
884 return zero_after_ff = 1;
885 }
886
ljpeg_end(struct jhead * jh)887 void CLASS ljpeg_end (struct jhead *jh)
888 {
889 int c;
890 FORC4 if (jh->free[c]) free (jh->free[c]);
891 free (jh->row);
892 }
893
ljpeg_diff(ushort * huff)894 int CLASS ljpeg_diff (ushort *huff)
895 {
896 int len, diff;
897
898 len = gethuff(huff);
899 if (len == 16 && (!dng_version || dng_version >= 0x1010000))
900 return -32768;
901 diff = getbits(len);
902 if ((diff & (1 << (len-1))) == 0)
903 diff -= (1 << len) - 1;
904 return diff;
905 }
906
ljpeg_row(int jrow,struct jhead * jh)907 ushort * CLASS ljpeg_row (int jrow, struct jhead *jh)
908 {
909 int col, c, diff, pred, spred=0;
910 ushort mark=0, *row[3];
911
912 if (jrow * jh->wide % jh->restart == 0) {
913 FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
914 if (jrow) {
915 fseek (ifp, -2, SEEK_CUR);
916 do mark = (mark << 8) + (c = fgetc(ifp));
917 while (c != EOF && mark >> 4 != 0xffd);
918 }
919 getbits(-1);
920 }
921 FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
922 for (col=0; col < jh->wide; col++)
923 FORC(jh->clrs) {
924 diff = ljpeg_diff (jh->huff[c]);
925 if (jh->sraw && c <= jh->sraw && (col | c))
926 pred = spred;
927 else if (col) pred = row[0][-jh->clrs];
928 else pred = (jh->vpred[c] += diff) - diff;
929 if (jrow && col) switch (jh->psv) {
930 case 1: break;
931 case 2: pred = row[1][0]; break;
932 case 3: pred = row[1][-jh->clrs]; break;
933 case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
934 case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
935 case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
936 case 7: pred = (pred + row[1][0]) >> 1; break;
937 default: pred = 0;
938 }
939 if ((**row = pred + diff) >> jh->bits) derror();
940 if (c <= jh->sraw) spred = **row;
941 row[0]++; row[1]++;
942 }
943 return row[2];
944 }
945
lossless_jpeg_load_raw()946 void CLASS lossless_jpeg_load_raw()
947 {
948 int jwide, jrow, jcol, val, jidx, i, j, row=0, col=0;
949 struct jhead jh;
950 ushort *rp;
951
952 if (!ljpeg_start (&jh, 0)) return;
953 jwide = jh.wide * jh.clrs;
954
955 for (jrow=0; jrow < jh.high; jrow++) {
956 rp = ljpeg_row (jrow, &jh);
957 if (load_flags & 1)
958 row = jrow & 1 ? height-1-jrow/2 : jrow/2;
959 for (jcol=0; jcol < jwide; jcol++) {
960 val = curve[*rp++];
961 if (cr2_slice[0]) {
962 jidx = jrow*jwide + jcol;
963 i = jidx / (cr2_slice[1]*raw_height);
964 if ((j = i >= cr2_slice[0]))
965 i = cr2_slice[0];
966 jidx -= i * (cr2_slice[1]*raw_height);
967 row = jidx / cr2_slice[1+j];
968 col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
969 }
970 if (raw_width == 3984 && (col -= 2) < 0)
971 col += (row--,raw_width);
972 if ((unsigned) row < raw_height) RAW(row,col) = val;
973 if (++col >= raw_width)
974 col = (row++,0);
975 }
976 }
977 ljpeg_end (&jh);
978 }
979
canon_sraw_load_raw()980 void CLASS canon_sraw_load_raw()
981 {
982 struct jhead jh;
983 short *rp=0, (*ip)[4];
984 int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
985 int v[3]={0,0,0}, ver, hue;
986 char *cp;
987
988 if (!ljpeg_start (&jh, 0) || jh.clrs < 4) return;
989 jwide = (jh.wide >>= 1) * jh.clrs;
990
991 for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
992 scol = ecol;
993 ecol += cr2_slice[1] * 2 / jh.clrs;
994 if (!cr2_slice[0] || ecol > raw_width-1) ecol = raw_width & -2;
995 for (row=0; row < height; row += (jh.clrs >> 1) - 1) {
996 ip = (short (*)[4]) image + row*width;
997 for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
998 if ((jcol %= jwide) == 0)
999 rp = (short *) ljpeg_row (jrow++, &jh);
1000 if (col >= width) continue;
1001 FORC (jh.clrs-2)
1002 ip[col + (c >> 1)*width + (c & 1)][0] = rp[jcol+c];
1003 ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
1004 ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
1005 }
1006 }
1007 }
1008 for (cp=model2; *cp && !isdigit(*cp); cp++);
1009 sscanf (cp, "%d.%d.%d", v, v+1, v+2);
1010 ver = (v[0]*1000 + v[1])*1000 + v[2];
1011 hue = (jh.sraw+1) << 2;
1012 if (unique_id >= 0x80000281 || (unique_id == 0x80000218 && ver > 1000006))
1013 hue = jh.sraw << 1;
1014 ip = (short (*)[4]) image;
1015 rp = ip[0];
1016 for (row=0; row < height; row++, ip+=width) {
1017 if (row & (jh.sraw >> 1))
1018 for (col=0; col < width; col+=2)
1019 for (c=1; c < 3; c++)
1020 if (row == height-1)
1021 ip[col][c] = ip[col-width][c];
1022 else ip[col][c] = (ip[col-width][c] + ip[col+width][c] + 1) >> 1;
1023 for (col=1; col < width; col+=2)
1024 for (c=1; c < 3; c++)
1025 if (col == width-1)
1026 ip[col][c] = ip[col-1][c];
1027 else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
1028 }
1029 for ( ; rp < ip[0]; rp+=4) {
1030 if (unique_id == 0x80000218 ||
1031 unique_id == 0x80000250 ||
1032 unique_id == 0x80000261 ||
1033 unique_id == 0x80000281 ||
1034 unique_id == 0x80000287) {
1035 rp[1] = (rp[1] << 2) + hue;
1036 rp[2] = (rp[2] << 2) + hue;
1037 pix[0] = rp[0] + (( 50*rp[1] + 22929*rp[2]) >> 14);
1038 pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
1039 pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
1040 } else {
1041 if (unique_id < 0x80000218) rp[0] -= 512;
1042 pix[0] = rp[0] + rp[2];
1043 pix[2] = rp[0] + rp[1];
1044 pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12);
1045 }
1046 FORC3 rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
1047 }
1048 ljpeg_end (&jh);
1049 maximum = 0x3fff;
1050 }
1051
adobe_copy_pixel(unsigned row,unsigned col,ushort ** rp)1052 void CLASS adobe_copy_pixel (unsigned row, unsigned col, ushort **rp)
1053 {
1054 int c;
1055
1056 if (tiff_samples == 2 && shot_select) (*rp)++;
1057 if (raw_image) {
1058 if (row < raw_height && col < raw_width)
1059 RAW(row,col) = curve[**rp];
1060 *rp += tiff_samples;
1061 } else {
1062 if (row < height && col < width)
1063 FORC(tiff_samples)
1064 image[row*width+col][c] = curve[(*rp)[c]];
1065 *rp += tiff_samples;
1066 }
1067 if (tiff_samples == 2 && shot_select) (*rp)--;
1068 }
1069
ljpeg_idct(struct jhead * jh)1070 void CLASS ljpeg_idct (struct jhead *jh)
1071 {
1072 int c, i, j, len, skip, coef;
1073 float work[3][8][8];
1074 static float cs[106] = { 0 };
1075 static const uchar zigzag[80] =
1076 { 0, 1, 8,16, 9, 2, 3,10,17,24,32,25,18,11, 4, 5,12,19,26,33,
1077 40,48,41,34,27,20,13, 6, 7,14,21,28,35,42,49,56,57,50,43,36,
1078 29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,
1079 47,55,62,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63 };
1080
1081 if (!cs[0])
1082 FORC(106) cs[c] = cos((c & 31)*M_PI/16)/2;
1083 memset (work, 0, sizeof work);
1084 work[0][0][0] = jh->vpred[0] += ljpeg_diff (jh->huff[0]) * jh->quant[0];
1085 for (i=1; i < 64; i++ ) {
1086 len = gethuff (jh->huff[16]);
1087 i += skip = len >> 4;
1088 if (!(len &= 15) && skip < 15) break;
1089 coef = getbits(len);
1090 if ((coef & (1 << (len-1))) == 0)
1091 coef -= (1 << len) - 1;
1092 ((float *)work)[zigzag[i]] = coef * jh->quant[i];
1093 }
1094 FORC(8) work[0][0][c] *= M_SQRT1_2;
1095 FORC(8) work[0][c][0] *= M_SQRT1_2;
1096 for (i=0; i < 8; i++)
1097 for (j=0; j < 8; j++)
1098 FORC(8) work[1][i][j] += work[0][i][c] * cs[(j*2+1)*c];
1099 for (i=0; i < 8; i++)
1100 for (j=0; j < 8; j++)
1101 FORC(8) work[2][i][j] += work[1][c][j] * cs[(i*2+1)*c];
1102
1103 FORC(64) jh->idct[c] = CLIP(((float *)work[2])[c]+0.5);
1104 }
1105
lossless_dng_load_raw()1106 void CLASS lossless_dng_load_raw()
1107 {
1108 unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col, i, j;
1109 struct jhead jh;
1110 ushort *rp;
1111
1112 while (trow < raw_height) {
1113 save = ftell(ifp);
1114 if (tile_length < INT_MAX)
1115 fseek (ifp, get4(), SEEK_SET);
1116 if (!ljpeg_start (&jh, 0)) break;
1117 jwide = jh.wide;
1118 if (filters) jwide *= jh.clrs;
1119 jwide /= MIN (is_raw, tiff_samples);
1120 switch (jh.algo) {
1121 case 0xc1:
1122 jh.vpred[0] = 16384;
1123 getbits(-1);
1124 for (jrow=0; jrow+7 < jh.high; jrow += 8) {
1125 for (jcol=0; jcol+7 < jh.wide; jcol += 8) {
1126 ljpeg_idct (&jh);
1127 rp = jh.idct;
1128 row = trow + jcol/tile_width + jrow*2;
1129 col = tcol + jcol%tile_width;
1130 for (i=0; i < 16; i+=2)
1131 for (j=0; j < 8; j++)
1132 adobe_copy_pixel (row+i, col+j, &rp);
1133 }
1134 }
1135 break;
1136 case 0xc3:
1137 for (row=col=jrow=0; jrow < jh.high; jrow++) {
1138 rp = ljpeg_row (jrow, &jh);
1139 for (jcol=0; jcol < jwide; jcol++) {
1140 adobe_copy_pixel (trow+row, tcol+col, &rp);
1141 if (++col >= tile_width || col >= raw_width)
1142 row += 1 + (col = 0);
1143 }
1144 }
1145 }
1146 fseek (ifp, save+4, SEEK_SET);
1147 if ((tcol += tile_width) >= raw_width)
1148 trow += tile_length + (tcol = 0);
1149 ljpeg_end (&jh);
1150 }
1151 }
1152
packed_dng_load_raw()1153 void CLASS packed_dng_load_raw()
1154 {
1155 ushort *pixel, *rp;
1156 int row, col;
1157
1158 pixel = (ushort *) calloc (raw_width, tiff_samples*sizeof *pixel);
1159 merror (pixel, "packed_dng_load_raw()");
1160 for (row=0; row < raw_height; row++) {
1161 if (tiff_bps == 16)
1162 read_shorts (pixel, raw_width * tiff_samples);
1163 else {
1164 getbits(-1);
1165 for (col=0; col < raw_width * tiff_samples; col++)
1166 pixel[col] = getbits(tiff_bps);
1167 }
1168 for (rp=pixel, col=0; col < raw_width; col++)
1169 adobe_copy_pixel (row, col, &rp);
1170 }
1171 free (pixel);
1172 }
1173
pentax_load_raw()1174 void CLASS pentax_load_raw()
1175 {
1176 ushort bit[2][15], huff[4097];
1177 int dep, row, col, diff, c, i;
1178 ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
1179
1180 fseek (ifp, meta_offset, SEEK_SET);
1181 dep = (get2() + 12) & 15;
1182 fseek (ifp, 12, SEEK_CUR);
1183 FORC(dep) bit[0][c] = get2();
1184 FORC(dep) bit[1][c] = fgetc(ifp);
1185 FORC(dep)
1186 for (i=bit[0][c]; i <= ((bit[0][c]+(4096 >> bit[1][c])-1) & 4095); )
1187 huff[++i] = bit[1][c] << 8 | c;
1188 huff[0] = 12;
1189 fseek (ifp, data_offset, SEEK_SET);
1190 getbits(-1);
1191 for (row=0; row < raw_height; row++)
1192 for (col=0; col < raw_width; col++) {
1193 diff = ljpeg_diff (huff);
1194 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
1195 else hpred[col & 1] += diff;
1196 RAW(row,col) = hpred[col & 1];
1197 if (hpred[col & 1] >> tiff_bps) derror();
1198 }
1199 }
1200
nikon_load_raw()1201 void CLASS nikon_load_raw()
1202 {
1203 static const uchar nikon_tree[][32] = {
1204 { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
1205 5,4,3,6,2,7,1,0,8,9,11,10,12 },
1206 { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
1207 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
1208 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
1209 5,4,6,3,7,2,8,1,9,0,10,11,12 },
1210 { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
1211 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
1212 { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
1213 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
1214 { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
1215 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
1216 ushort *huff, ver0, ver1, vpred[2][2], hpred[2], csize;
1217 int i, min, max, step=0, tree=0, split=0, row, col, len, shl, diff;
1218
1219 fseek (ifp, meta_offset, SEEK_SET);
1220 ver0 = fgetc(ifp);
1221 ver1 = fgetc(ifp);
1222 if (ver0 == 0x49 || ver1 == 0x58)
1223 fseek (ifp, 2110, SEEK_CUR);
1224 if (ver0 == 0x46) tree = 2;
1225 if (tiff_bps == 14) tree += 3;
1226 read_shorts (vpred[0], 4);
1227 max = 1 << tiff_bps & 0x7fff;
1228 if ((csize = get2()) > 1)
1229 step = max / (csize-1);
1230 if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
1231 for (i=0; i < csize; i++)
1232 curve[i*step] = get2();
1233 for (i=0; i < max; i++)
1234 curve[i] = ( curve[i-i%step]*(step-i%step) +
1235 curve[i-i%step+step]*(i%step) ) / step;
1236 fseek (ifp, meta_offset+562, SEEK_SET);
1237 split = get2();
1238 } else if (ver0 != 0x46 && csize <= 0x4001)
1239 read_shorts (curve, max=csize);
1240 while (curve[max-2] == curve[max-1]) max--;
1241 huff = make_decoder (nikon_tree[tree]);
1242 fseek (ifp, data_offset, SEEK_SET);
1243 getbits(-1);
1244 for (min=row=0; row < height; row++) {
1245 if (split && row == split) {
1246 free (huff);
1247 huff = make_decoder (nikon_tree[tree+1]);
1248 max += (min = 16) << 1;
1249 }
1250 for (col=0; col < raw_width; col++) {
1251 i = gethuff(huff);
1252 len = i & 15;
1253 shl = i >> 4;
1254 diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
1255 if ((diff & (1 << (len-1))) == 0)
1256 diff -= (1 << len) - !shl;
1257 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
1258 else hpred[col & 1] += diff;
1259 if ((ushort)(hpred[col & 1] + min) >= max) derror();
1260 RAW(row,col) = curve[LIM((short)hpred[col & 1],0,0x3fff)];
1261 }
1262 }
1263 free (huff);
1264 }
1265
nikon_yuv_load_raw()1266 void CLASS nikon_yuv_load_raw()
1267 {
1268 int row, col, yuv[4], rgb[3], b, c;
1269 UINT64 bitbuf=0;
1270
1271 for (row=0; row < raw_height; row++)
1272 for (col=0; col < raw_width; col++) {
1273 if (!(b = col & 1)) {
1274 bitbuf = 0;
1275 FORC(6) bitbuf |= (UINT64) fgetc(ifp) << c*8;
1276 FORC(4) yuv[c] = (bitbuf >> c*12 & 0xfff) - (c >> 1 << 11);
1277 }
1278 rgb[0] = yuv[b] + 1.370705*yuv[3];
1279 rgb[1] = yuv[b] - 0.337633*yuv[2] - 0.698001*yuv[3];
1280 rgb[2] = yuv[b] + 1.732446*yuv[2];
1281 FORC3 image[row*width+col][c] = curve[LIM(rgb[c],0,0xfff)] / cam_mul[c];
1282 }
1283 }
1284
1285 /*
1286 Returns 1 for a Coolpix 995, 0 for anything else.
1287 */
nikon_e995()1288 int CLASS nikon_e995()
1289 {
1290 int i, histo[256];
1291 const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
1292
1293 memset (histo, 0, sizeof histo);
1294 fseek (ifp, -2000, SEEK_END);
1295 for (i=0; i < 2000; i++)
1296 histo[fgetc(ifp)]++;
1297 for (i=0; i < 4; i++)
1298 if (histo[often[i]] < 200)
1299 return 0;
1300 return 1;
1301 }
1302
1303 /*
1304 Returns 1 for a Coolpix 2100, 0 for anything else.
1305 */
nikon_e2100()1306 int CLASS nikon_e2100()
1307 {
1308 uchar t[12];
1309 int i;
1310
1311 fseek (ifp, 0, SEEK_SET);
1312 for (i=0; i < 1024; i++) {
1313 fread (t, 1, 12, ifp);
1314 if (((t[2] & t[4] & t[7] & t[9]) >> 4
1315 & t[1] & t[6] & t[8] & t[11] & 3) != 3)
1316 return 0;
1317 }
1318 return 1;
1319 }
1320
nikon_3700()1321 void CLASS nikon_3700()
1322 {
1323 int bits, i;
1324 uchar dp[24];
1325 static const struct {
1326 int bits;
1327 char make[12], model[15];
1328 } table[] = {
1329 { 0x00, "Pentax", "Optio 33WR" },
1330 { 0x03, "Nikon", "E3200" },
1331 { 0x32, "Nikon", "E3700" },
1332 { 0x33, "Olympus", "C740UZ" } };
1333
1334 fseek (ifp, 3072, SEEK_SET);
1335 fread (dp, 1, 24, ifp);
1336 bits = (dp[8] & 3) << 4 | (dp[20] & 3);
1337 for (i=0; i < sizeof table / sizeof *table; i++)
1338 if (bits == table[i].bits) {
1339 strcpy (make, table[i].make );
1340 strcpy (model, table[i].model);
1341 }
1342 }
1343
1344 /*
1345 Separates a Minolta DiMAGE Z2 from a Nikon E4300.
1346 */
minolta_z2()1347 int CLASS minolta_z2()
1348 {
1349 int i, nz;
1350 char tail[424];
1351
1352 fseek (ifp, -sizeof tail, SEEK_END);
1353 fread (tail, 1, sizeof tail, ifp);
1354 for (nz=i=0; i < sizeof tail; i++)
1355 if (tail[i]) nz++;
1356 return nz > 20;
1357 }
1358
1359 void CLASS jpeg_thumb();
1360
ppm_thumb()1361 void CLASS ppm_thumb()
1362 {
1363 char *thumb;
1364 thumb_length = thumb_width*thumb_height*3;
1365 thumb = (char *) malloc (thumb_length);
1366 merror (thumb, "ppm_thumb()");
1367 fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1368 fread (thumb, 1, thumb_length, ifp);
1369 fwrite (thumb, 1, thumb_length, ofp);
1370 free (thumb);
1371 }
1372
ppm16_thumb()1373 void CLASS ppm16_thumb()
1374 {
1375 int i;
1376 char *thumb;
1377 thumb_length = thumb_width*thumb_height*3;
1378 thumb = (char *) calloc (thumb_length, 2);
1379 merror (thumb, "ppm16_thumb()");
1380 read_shorts ((ushort *) thumb, thumb_length);
1381 for (i=0; i < thumb_length; i++)
1382 thumb[i] = ((ushort *) thumb)[i] >> 8;
1383 fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1384 fwrite (thumb, 1, thumb_length, ofp);
1385 free (thumb);
1386 }
1387
layer_thumb()1388 void CLASS layer_thumb()
1389 {
1390 int i, c;
1391 char *thumb, map[][4] = { "012","102" };
1392
1393 colors = thumb_misc >> 5 & 7;
1394 thumb_length = thumb_width*thumb_height;
1395 thumb = (char *) calloc (colors, thumb_length);
1396 merror (thumb, "layer_thumb()");
1397 fprintf (ofp, "P%d\n%d %d\n255\n",
1398 5 + (colors >> 1), thumb_width, thumb_height);
1399 fread (thumb, thumb_length, colors, ifp);
1400 for (i=0; i < thumb_length; i++)
1401 FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], ofp);
1402 free (thumb);
1403 }
1404
rollei_thumb()1405 void CLASS rollei_thumb()
1406 {
1407 unsigned i;
1408 ushort *thumb;
1409
1410 thumb_length = thumb_width * thumb_height;
1411 thumb = (ushort *) calloc (thumb_length, 2);
1412 merror (thumb, "rollei_thumb()");
1413 fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1414 read_shorts (thumb, thumb_length);
1415 for (i=0; i < thumb_length; i++) {
1416 putc (thumb[i] << 3, ofp);
1417 putc (thumb[i] >> 5 << 2, ofp);
1418 putc (thumb[i] >> 11 << 3, ofp);
1419 }
1420 free (thumb);
1421 }
1422
rollei_load_raw()1423 void CLASS rollei_load_raw()
1424 {
1425 uchar pixel[10];
1426 unsigned iten=0, isix, i, buffer=0, todo[16];
1427
1428 isix = raw_width * raw_height * 5 / 8;
1429 while (fread (pixel, 1, 10, ifp) == 10) {
1430 for (i=0; i < 10; i+=2) {
1431 todo[i] = iten++;
1432 todo[i+1] = pixel[i] << 8 | pixel[i+1];
1433 buffer = pixel[i] >> 2 | buffer << 6;
1434 }
1435 for ( ; i < 16; i+=2) {
1436 todo[i] = isix++;
1437 todo[i+1] = buffer >> (14-i)*5;
1438 }
1439 for (i=0; i < 16; i+=2)
1440 raw_image[todo[i]] = (todo[i+1] & 0x3ff);
1441 }
1442 maximum = 0x3ff;
1443 }
1444
raw(unsigned row,unsigned col)1445 int CLASS raw (unsigned row, unsigned col)
1446 {
1447 return (row < raw_height && col < raw_width) ? RAW(row,col) : 0;
1448 }
1449
phase_one_flat_field(int is_float,int nc)1450 void CLASS phase_one_flat_field (int is_float, int nc)
1451 {
1452 ushort head[8];
1453 unsigned wide, high, y, x, c, rend, cend, row, col;
1454 float *mrow, num, mult[4];
1455
1456 read_shorts (head, 8);
1457 if (head[2] * head[3] * head[4] * head[5] == 0) return;
1458 wide = head[2] / head[4] + (head[2] % head[4] != 0);
1459 high = head[3] / head[5] + (head[3] % head[5] != 0);
1460 mrow = (float *) calloc (nc*wide, sizeof *mrow);
1461 merror (mrow, "phase_one_flat_field()");
1462 for (y=0; y < high; y++) {
1463 for (x=0; x < wide; x++)
1464 for (c=0; c < nc; c+=2) {
1465 num = is_float ? getreal(11) : get2()/32768.0;
1466 if (y==0) mrow[c*wide+x] = num;
1467 else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
1468 }
1469 if (y==0) continue;
1470 rend = head[1] + y*head[5];
1471 for (row = rend-head[5];
1472 row < raw_height && row < rend &&
1473 row < head[1]+head[3]-head[5]; row++) {
1474 for (x=1; x < wide; x++) {
1475 for (c=0; c < nc; c+=2) {
1476 mult[c] = mrow[c*wide+x-1];
1477 mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
1478 }
1479 cend = head[0] + x*head[4];
1480 for (col = cend-head[4];
1481 col < raw_width &&
1482 col < cend && col < head[0]+head[2]-head[4]; col++) {
1483 c = nc > 2 ? FC(row-top_margin,col-left_margin) : 0;
1484 if (!(c & 1)) {
1485 c = RAW(row,col) * mult[c];
1486 RAW(row,col) = LIM(c,0,65535);
1487 }
1488 for (c=0; c < nc; c+=2)
1489 mult[c] += mult[c+1];
1490 }
1491 }
1492 for (x=0; x < wide; x++)
1493 for (c=0; c < nc; c+=2)
1494 mrow[c*wide+x] += mrow[(c+1)*wide+x];
1495 }
1496 }
1497 free (mrow);
1498 }
1499
phase_one_correct()1500 void CLASS phase_one_correct()
1501 {
1502 unsigned entries, tag, data, save, col, row, type;
1503 int len, i, j, k, cip, val[4], dev[4], sum, max;
1504 int head[9], diff, mindiff=INT_MAX, off_412=0;
1505 static const signed char dir[12][2] =
1506 { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
1507 {-2,-2}, {-2,2}, {2,-2}, {2,2} };
1508 float poly[8], num, cfrac, frac, mult[2], *yval[2];
1509 ushort *xval[2];
1510 int qmult_applied = 0, qlin_applied = 0;
1511
1512 if (half_size || !meta_length) return;
1513 if (verbose) fprintf (stderr,_("Phase One correction...\n"));
1514 fseek (ifp, meta_offset, SEEK_SET);
1515 order = get2();
1516 fseek (ifp, 6, SEEK_CUR);
1517 fseek (ifp, meta_offset+get4(), SEEK_SET);
1518 entries = get4(); get4();
1519 while (entries--) {
1520 tag = get4();
1521 len = get4();
1522 data = get4();
1523 save = ftell(ifp);
1524 fseek (ifp, meta_offset+data, SEEK_SET);
1525 if (tag == 0x419) { /* Polynomial curve */
1526 for (get4(), i=0; i < 8; i++)
1527 poly[i] = getreal(11);
1528 poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
1529 for (i=0; i < 0x10000; i++) {
1530 num = (poly[5]*i + poly[3])*i + poly[1];
1531 curve[i] = LIM(num,0,65535);
1532 } goto apply; /* apply to right half */
1533 } else if (tag == 0x41a) { /* Polynomial curve */
1534 for (i=0; i < 4; i++)
1535 poly[i] = getreal(11);
1536 for (i=0; i < 0x10000; i++) {
1537 for (num=0, j=4; j--; )
1538 num = num * i + poly[j];
1539 curve[i] = LIM(num+i,0,65535);
1540 } apply: /* apply to whole image */
1541 for (row=0; row < raw_height; row++)
1542 for (col = (tag & 1)*ph1.split_col; col < raw_width; col++)
1543 RAW(row,col) = curve[RAW(row,col)];
1544 } else if (tag == 0x400) { /* Sensor defects */
1545 while ((len -= 8) >= 0) {
1546 col = get2();
1547 row = get2();
1548 type = get2(); get2();
1549 if (col >= raw_width) continue;
1550 if (type == 131 || type == 137) /* Bad column */
1551 for (row=0; row < raw_height; row++)
1552 if (FC(row-top_margin,col-left_margin) == 1) {
1553 for (sum=i=0; i < 4; i++)
1554 sum += val[i] = raw (row+dir[i][0], col+dir[i][1]);
1555 for (max=i=0; i < 4; i++) {
1556 dev[i] = abs((val[i] << 2) - sum);
1557 if (dev[max] < dev[i]) max = i;
1558 }
1559 RAW(row,col) = (sum - val[max])/3.0 + 0.5;
1560 } else {
1561 for (sum=0, i=8; i < 12; i++)
1562 sum += raw (row+dir[i][0], col+dir[i][1]);
1563 RAW(row,col) = 0.5 + sum * 0.0732233 +
1564 (raw(row,col-2) + raw(row,col+2)) * 0.3535534;
1565 }
1566 else if (type == 129) { /* Bad pixel */
1567 if (row >= raw_height) continue;
1568 j = (FC(row-top_margin,col-left_margin) != 1) * 4;
1569 for (sum=0, i=j; i < j+8; i++)
1570 sum += raw (row+dir[i][0], col+dir[i][1]);
1571 RAW(row,col) = (sum + 4) >> 3;
1572 }
1573 }
1574 } else if (tag == 0x401) { /* All-color flat fields */
1575 phase_one_flat_field (1, 2);
1576 } else if (tag == 0x416 || tag == 0x410) {
1577 phase_one_flat_field (0, 2);
1578 } else if (tag == 0x40b) { /* Red+blue flat field */
1579 phase_one_flat_field (0, 4);
1580 } else if (tag == 0x412) {
1581 fseek (ifp, 36, SEEK_CUR);
1582 diff = abs (get2() - ph1.tag_21a);
1583 if (mindiff > diff) {
1584 mindiff = diff;
1585 off_412 = ftell(ifp) - 38;
1586 }
1587 } else if (tag == 0x41f && !qlin_applied) { /* Quadrant linearization */
1588 ushort lc[2][2][16], ref[16];
1589 int qr, qc;
1590 for (qr = 0; qr < 2; qr++)
1591 for (qc = 0; qc < 2; qc++)
1592 for (i = 0; i < 16; i++)
1593 lc[qr][qc][i] = get4();
1594 for (i = 0; i < 16; i++) {
1595 int v = 0;
1596 for (qr = 0; qr < 2; qr++)
1597 for (qc = 0; qc < 2; qc++)
1598 v += lc[qr][qc][i];
1599 ref[i] = (v + 2) >> 2;
1600 }
1601 for (qr = 0; qr < 2; qr++) {
1602 for (qc = 0; qc < 2; qc++) {
1603 int cx[19], cf[19];
1604 for (i = 0; i < 16; i++) {
1605 cx[1+i] = lc[qr][qc][i];
1606 cf[1+i] = ref[i];
1607 }
1608 cx[0] = cf[0] = 0;
1609 cx[17] = cf[17] = ((unsigned) ref[15] * 65535) / lc[qr][qc][15];
1610 cx[18] = cf[18] = 65535;
1611 cubic_spline(cx, cf, 19);
1612 for (row = (qr ? ph1.split_row : 0);
1613 row < (qr ? raw_height : ph1.split_row); row++)
1614 for (col = (qc ? ph1.split_col : 0);
1615 col < (qc ? raw_width : ph1.split_col); col++)
1616 RAW(row,col) = curve[RAW(row,col)];
1617 }
1618 }
1619 qlin_applied = 1;
1620 } else if (tag == 0x41e && !qmult_applied) { /* Quadrant multipliers */
1621 float qmult[2][2] = { { 1, 1 }, { 1, 1 } };
1622 get4(); get4(); get4(); get4();
1623 qmult[0][0] = 1.0 + getreal(11);
1624 get4(); get4(); get4(); get4(); get4();
1625 qmult[0][1] = 1.0 + getreal(11);
1626 get4(); get4(); get4();
1627 qmult[1][0] = 1.0 + getreal(11);
1628 get4(); get4(); get4();
1629 qmult[1][1] = 1.0 + getreal(11);
1630 for (row=0; row < raw_height; row++)
1631 for (col=0; col < raw_width; col++) {
1632 i = qmult[row >= ph1.split_row][col >= ph1.split_col] * RAW(row,col);
1633 RAW(row,col) = LIM(i,0,65535);
1634 }
1635 qmult_applied = 1;
1636 } else if (tag == 0x431 && !qmult_applied) { /* Quadrant combined */
1637 ushort lc[2][2][7], ref[7];
1638 int qr, qc;
1639 for (i = 0; i < 7; i++)
1640 ref[i] = get4();
1641 for (qr = 0; qr < 2; qr++)
1642 for (qc = 0; qc < 2; qc++)
1643 for (i = 0; i < 7; i++)
1644 lc[qr][qc][i] = get4();
1645 for (qr = 0; qr < 2; qr++) {
1646 for (qc = 0; qc < 2; qc++) {
1647 int cx[9], cf[9];
1648 for (i = 0; i < 7; i++) {
1649 cx[1+i] = ref[i];
1650 cf[1+i] = ((unsigned) ref[i] * lc[qr][qc][i]) / 10000;
1651 }
1652 cx[0] = cf[0] = 0;
1653 cx[8] = cf[8] = 65535;
1654 cubic_spline(cx, cf, 9);
1655 for (row = (qr ? ph1.split_row : 0);
1656 row < (qr ? raw_height : ph1.split_row); row++)
1657 for (col = (qc ? ph1.split_col : 0);
1658 col < (qc ? raw_width : ph1.split_col); col++)
1659 RAW(row,col) = curve[RAW(row,col)];
1660 }
1661 }
1662 qmult_applied = 1;
1663 qlin_applied = 1;
1664 }
1665 fseek (ifp, save, SEEK_SET);
1666 }
1667 if (off_412) {
1668 fseek (ifp, off_412, SEEK_SET);
1669 for (i=0; i < 9; i++) head[i] = get4() & 0x7fff;
1670 yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
1671 merror (yval[0], "phase_one_correct()");
1672 yval[1] = (float *) (yval[0] + head[1]*head[3]);
1673 xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
1674 xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
1675 get2();
1676 for (i=0; i < 2; i++)
1677 for (j=0; j < head[i+1]*head[i+3]; j++)
1678 yval[i][j] = getreal(11);
1679 for (i=0; i < 2; i++)
1680 for (j=0; j < head[i+1]*head[i+3]; j++)
1681 xval[i][j] = get2();
1682 for (row=0; row < raw_height; row++)
1683 for (col=0; col < raw_width; col++) {
1684 cfrac = (float) col * head[3] / raw_width;
1685 cfrac -= cip = cfrac;
1686 num = RAW(row,col) * 0.5;
1687 for (i=cip; i < cip+2; i++) {
1688 for (k=j=0; j < head[1]; j++)
1689 if (num < xval[0][k = head[1]*i+j]) break;
1690 frac = (j == 0 || j == head[1]) ? 0 :
1691 (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
1692 mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
1693 }
1694 i = ((mult[0] * (1-cfrac) + mult[1] * cfrac) * row + num) * 2;
1695 RAW(row,col) = LIM(i,0,65535);
1696 }
1697 free (yval[0]);
1698 }
1699 }
1700
phase_one_load_raw()1701 void CLASS phase_one_load_raw()
1702 {
1703 int a, b, i;
1704 ushort akey, bkey, mask;
1705
1706 fseek (ifp, ph1.key_off, SEEK_SET);
1707 akey = get2();
1708 bkey = get2();
1709 mask = ph1.format == 1 ? 0x5555:0x1354;
1710 fseek (ifp, data_offset, SEEK_SET);
1711 read_shorts (raw_image, raw_width*raw_height);
1712 if (ph1.format)
1713 for (i=0; i < raw_width*raw_height; i+=2) {
1714 a = raw_image[i+0] ^ akey;
1715 b = raw_image[i+1] ^ bkey;
1716 raw_image[i+0] = (a & mask) | (b & ~mask);
1717 raw_image[i+1] = (b & mask) | (a & ~mask);
1718 }
1719 }
1720
ph1_bithuff(int nbits,ushort * huff)1721 unsigned CLASS ph1_bithuff (int nbits, ushort *huff)
1722 {
1723 static UINT64 bitbuf=0;
1724 static int vbits=0;
1725 unsigned c;
1726
1727 if (nbits == -1)
1728 return bitbuf = vbits = 0;
1729 if (nbits == 0) return 0;
1730 if (vbits < nbits) {
1731 bitbuf = bitbuf << 32 | get4();
1732 vbits += 32;
1733 }
1734 c = bitbuf << (64-vbits) >> (64-nbits);
1735 if (huff) {
1736 vbits -= huff[c] >> 8;
1737 return (uchar) huff[c];
1738 }
1739 vbits -= nbits;
1740 return c;
1741 }
1742 #define ph1_bits(n) ph1_bithuff(n,0)
1743 #define ph1_huff(h) ph1_bithuff(*h,h+1)
1744
phase_one_load_raw_c()1745 void CLASS phase_one_load_raw_c()
1746 {
1747 static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
1748 int *offset, len[2], pred[2], row, col, i, j;
1749 ushort *pixel;
1750 short (*cblack)[2], (*rblack)[2];
1751
1752 pixel = (ushort *) calloc (raw_width*3 + raw_height*4, 2);
1753 merror (pixel, "phase_one_load_raw_c()");
1754 offset = (int *) (pixel + raw_width);
1755 fseek (ifp, strip_offset, SEEK_SET);
1756 for (row=0; row < raw_height; row++)
1757 offset[row] = get4();
1758 cblack = (short (*)[2]) (offset + raw_height);
1759 fseek (ifp, ph1.black_col, SEEK_SET);
1760 if (ph1.black_col)
1761 read_shorts ((ushort *) cblack[0], raw_height*2);
1762 rblack = cblack + raw_height;
1763 fseek (ifp, ph1.black_row, SEEK_SET);
1764 if (ph1.black_row)
1765 read_shorts ((ushort *) rblack[0], raw_width*2);
1766 for (i=0; i < 256; i++)
1767 curve[i] = i*i / 3.969 + 0.5;
1768 for (row=0; row < raw_height; row++) {
1769 fseek (ifp, data_offset + offset[row], SEEK_SET);
1770 ph1_bits(-1);
1771 pred[0] = pred[1] = 0;
1772 for (col=0; col < raw_width; col++) {
1773 if (col >= (raw_width & -8))
1774 len[0] = len[1] = 14;
1775 else if ((col & 7) == 0)
1776 for (i=0; i < 2; i++) {
1777 for (j=0; j < 5 && !ph1_bits(1); j++);
1778 if (j--) len[i] = length[j*2 + ph1_bits(1)];
1779 }
1780 if ((i = len[col & 1]) == 14)
1781 pixel[col] = pred[col & 1] = ph1_bits(16);
1782 else
1783 pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
1784 if (pred[col & 1] >> 16) derror();
1785 if (ph1.format == 5 && pixel[col] < 256)
1786 pixel[col] = curve[pixel[col]];
1787 }
1788 for (col=0; col < raw_width; col++) {
1789 i = (pixel[col] << 2*(ph1.format != 8)) - ph1.black
1790 + cblack[row][col >= ph1.split_col]
1791 + rblack[col][row >= ph1.split_row];
1792 if (i > 0) RAW(row,col) = i;
1793 }
1794 }
1795 free (pixel);
1796 maximum = 0xfffc - ph1.black;
1797 }
1798
hasselblad_load_raw()1799 void CLASS hasselblad_load_raw()
1800 {
1801 struct jhead jh;
1802 int shot, row, col, *back[5], len[2], diff[12], pred, sh, f, s, c;
1803 unsigned upix, urow, ucol;
1804 ushort *ip;
1805
1806 if (!ljpeg_start (&jh, 0)) return;
1807 order = 0x4949;
1808 ph1_bits(-1);
1809 back[4] = (int *) calloc (raw_width, 3*sizeof **back);
1810 merror (back[4], "hasselblad_load_raw()");
1811 FORC3 back[c] = back[4] + c*raw_width;
1812 cblack[6] >>= sh = tiff_samples > 1;
1813 shot = LIM(shot_select, 1, tiff_samples) - 1;
1814 for (row=0; row < raw_height; row++) {
1815 FORC4 back[(c+3) & 3] = back[c];
1816 for (col=0; col < raw_width; col+=2) {
1817 for (s=0; s < tiff_samples*2; s+=2) {
1818 FORC(2) len[c] = ph1_huff(jh.huff[0]);
1819 FORC(2) {
1820 diff[s+c] = ph1_bits(len[c]);
1821 if ((diff[s+c] & (1 << (len[c]-1))) == 0)
1822 diff[s+c] -= (1 << len[c]) - 1;
1823 if (diff[s+c] == 65535) diff[s+c] = -32768;
1824 }
1825 }
1826 for (s=col; s < col+2; s++) {
1827 pred = 0x8000 + load_flags;
1828 if (col) pred = back[2][s-2];
1829 if (col && row > 1) switch (jh.psv) {
1830 case 11: pred += back[0][s]/2 - back[0][s-2]/2; break;
1831 }
1832 f = (row & 1)*3 ^ ((col+s) & 1);
1833 FORC (tiff_samples) {
1834 pred += diff[(s & 1)*tiff_samples+c];
1835 upix = pred >> sh & 0xffff;
1836 if (raw_image && c == shot)
1837 RAW(row,s) = upix;
1838 if (image) {
1839 urow = row-top_margin + (c & 1);
1840 ucol = col-left_margin - ((c >> 1) & 1);
1841 ip = &image[urow*width+ucol][f];
1842 if (urow < height && ucol < width)
1843 *ip = c < 4 ? upix : (*ip + upix) >> 1;
1844 }
1845 }
1846 back[2][s] = pred;
1847 }
1848 }
1849 }
1850 free (back[4]);
1851 ljpeg_end (&jh);
1852 if (image) mix_green = 1;
1853 }
1854
leaf_hdr_load_raw()1855 void CLASS leaf_hdr_load_raw()
1856 {
1857 ushort *pixel=0;
1858 unsigned tile=0, r, c, row, col;
1859
1860 if (!filters) {
1861 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1862 merror (pixel, "leaf_hdr_load_raw()");
1863 }
1864 FORC(tiff_samples)
1865 for (r=0; r < raw_height; r++) {
1866 if (r % tile_length == 0) {
1867 fseek (ifp, data_offset + 4*tile++, SEEK_SET);
1868 fseek (ifp, get4(), SEEK_SET);
1869 }
1870 if (filters && c != shot_select) continue;
1871 if (filters) pixel = raw_image + r*raw_width;
1872 read_shorts (pixel, raw_width);
1873 if (!filters && (row = r - top_margin) < height)
1874 for (col=0; col < width; col++)
1875 image[row*width+col][c] = pixel[col+left_margin];
1876 }
1877 if (!filters) {
1878 maximum = 0xffff;
1879 raw_color = 1;
1880 free (pixel);
1881 }
1882 }
1883
unpacked_load_raw()1884 void CLASS unpacked_load_raw()
1885 {
1886 int row, col, bits=0;
1887
1888 while (1 << ++bits < maximum);
1889 read_shorts (raw_image, raw_width*raw_height);
1890 for (row=0; row < raw_height; row++)
1891 for (col=0; col < raw_width; col++)
1892 if ((RAW(row,col) >>= load_flags) >> bits
1893 && (unsigned) (row-top_margin) < height
1894 && (unsigned) (col-left_margin) < width) derror();
1895 }
1896
sinar_4shot_load_raw()1897 void CLASS sinar_4shot_load_raw()
1898 {
1899 ushort *pixel;
1900 unsigned shot, row, col, r, c;
1901
1902 if (raw_image) {
1903 shot = LIM (shot_select, 1, 4) - 1;
1904 fseek (ifp, data_offset + shot*4, SEEK_SET);
1905 fseek (ifp, get4(), SEEK_SET);
1906 unpacked_load_raw();
1907 return;
1908 }
1909 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1910 merror (pixel, "sinar_4shot_load_raw()");
1911 for (shot=0; shot < 4; shot++) {
1912 fseek (ifp, data_offset + shot*4, SEEK_SET);
1913 fseek (ifp, get4(), SEEK_SET);
1914 for (row=0; row < raw_height; row++) {
1915 read_shorts (pixel, raw_width);
1916 if ((r = row-top_margin - (shot >> 1 & 1)) >= height) continue;
1917 for (col=0; col < raw_width; col++) {
1918 if ((c = col-left_margin - (shot & 1)) >= width) continue;
1919 image[r*width+c][(row & 1)*3 ^ (~col & 1)] = pixel[col];
1920 }
1921 }
1922 }
1923 free (pixel);
1924 mix_green = 1;
1925 }
1926
imacon_full_load_raw()1927 void CLASS imacon_full_load_raw()
1928 {
1929 int row, col;
1930
1931 if (!image) return;
1932 for (row=0; row < height; row++)
1933 for (col=0; col < width; col++)
1934 read_shorts (image[row*width+col], 3);
1935 }
1936
packed_load_raw()1937 void CLASS packed_load_raw()
1938 {
1939 int vbits=0, bwide, rbits, bite, half, irow, row, col, val, i;
1940 UINT64 bitbuf=0;
1941
1942 bwide = raw_width * tiff_bps / 8;
1943 bwide += bwide & load_flags >> 9;
1944 rbits = bwide * 8 - raw_width * tiff_bps;
1945 if (load_flags & 1) bwide = bwide * 16 / 15;
1946 bite = 8 + (load_flags & 56);
1947 half = (raw_height+1) >> 1;
1948 for (irow=0; irow < raw_height; irow++) {
1949 row = irow;
1950 if (load_flags & 2 &&
1951 (row = irow % half * 2 + irow / half) == 1 &&
1952 load_flags & 4) {
1953 if (vbits=0, tiff_compress)
1954 fseek (ifp, data_offset - (-half*bwide & -2048), SEEK_SET);
1955 else {
1956 fseek (ifp, 0, SEEK_END);
1957 fseek (ifp, ftell(ifp) >> 3 << 2, SEEK_SET);
1958 }
1959 }
1960 for (col=0; col < raw_width; col++) {
1961 for (vbits -= tiff_bps; vbits < 0; vbits += bite) {
1962 bitbuf <<= bite;
1963 for (i=0; i < bite; i+=8)
1964 bitbuf |= ((UINT64) fgetc(ifp) << i);
1965 }
1966 val = bitbuf << (64-tiff_bps-vbits) >> (64-tiff_bps);
1967 RAW(row,col ^ (load_flags >> 6 & 3)) = val;
1968 if (load_flags & 1 && (col % 10) == 9 && fgetc(ifp) &&
1969 row < height+top_margin && col < width+left_margin) derror();
1970 }
1971 vbits -= rbits;
1972 }
1973 }
1974
nokia_load_raw()1975 void CLASS nokia_load_raw()
1976 {
1977 uchar *data, *dp;
1978 int rev, dwide, row, col, c;
1979 double sum[]={0,0};
1980
1981 rev = 3 * (order == 0x4949);
1982 dwide = (raw_width * 5 + 1) / 4;
1983 data = (uchar *) malloc (dwide*2);
1984 merror (data, "nokia_load_raw()");
1985 for (row=0; row < raw_height; row++) {
1986 if (fread (data+dwide, 1, dwide, ifp) < dwide) derror();
1987 FORC(dwide) data[c] = data[dwide+(c ^ rev)];
1988 for (dp=data, col=0; col < raw_width; dp+=5, col+=4)
1989 FORC4 RAW(row,col+c) = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
1990 }
1991 free (data);
1992 maximum = 0x3ff;
1993 if (strcmp(make,"OmniVision")) return;
1994 row = raw_height/2;
1995 FORC(width-1) {
1996 sum[ c & 1] += SQR(RAW(row,c)-RAW(row+1,c+1));
1997 sum[~c & 1] += SQR(RAW(row+1,c)-RAW(row,c+1));
1998 }
1999 if (sum[1] > sum[0]) filters = 0x4b4b4b4b;
2000 }
2001
canon_rmf_load_raw()2002 void CLASS canon_rmf_load_raw()
2003 {
2004 int row, col, bits, orow, ocol, c;
2005
2006 for (row=0; row < raw_height; row++)
2007 for (col=0; col < raw_width-2; col+=3) {
2008 bits = get4();
2009 FORC3 {
2010 orow = row;
2011 if ((ocol = col+c-4) < 0) {
2012 ocol += raw_width;
2013 if ((orow -= 2) < 0)
2014 orow += raw_height;
2015 }
2016 RAW(orow,ocol) = curve[bits >> (10*c+2) & 0x3ff];
2017 }
2018 }
2019 maximum = curve[0x3ff];
2020 }
2021
pana_bits(int nbits)2022 unsigned CLASS pana_bits (int nbits)
2023 {
2024 static uchar buf[0x4000];
2025 static int vbits;
2026 int byte;
2027
2028 if (!nbits) return vbits=0;
2029 if (!vbits) {
2030 fread (buf+load_flags, 1, 0x4000-load_flags, ifp);
2031 fread (buf, 1, load_flags, ifp);
2032 }
2033 vbits = (vbits - nbits) & 0x1ffff;
2034 byte = vbits >> 3 ^ 0x3ff0;
2035 return (buf[byte] | buf[byte+1] << 8) >> (vbits & 7) & ~(-1 << nbits);
2036 }
2037
panasonic_load_raw()2038 void CLASS panasonic_load_raw()
2039 {
2040 int row, col, i, j, sh=0, pred[2], nonz[2];
2041
2042 pana_bits(0);
2043 for (row=0; row < height; row++)
2044 for (col=0; col < raw_width; col++) {
2045 if ((i = col % 14) == 0)
2046 pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
2047 if (i % 3 == 2) sh = 4 >> (3 - pana_bits(2));
2048 if (nonz[i & 1]) {
2049 if ((j = pana_bits(8))) {
2050 if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
2051 pred[i & 1] &= ~(-1 << sh);
2052 pred[i & 1] += j << sh;
2053 }
2054 } else if ((nonz[i & 1] = pana_bits(8)) || i > 11)
2055 pred[i & 1] = nonz[i & 1] << 4 | pana_bits(4);
2056 if ((RAW(row,col) = pred[col & 1]) > 4098 && col < width) derror();
2057 }
2058 }
2059
olympus_load_raw()2060 void CLASS olympus_load_raw()
2061 {
2062 ushort huff[4096];
2063 int row, col, nbits, sign, low, high, i, c, w, n, nw;
2064 int acarry[2][3], *carry, pred, diff;
2065
2066 huff[n=0] = 0xc0c;
2067 for (i=12; i--; )
2068 FORC(2048 >> i) huff[++n] = (i+1) << 8 | i;
2069 fseek (ifp, 7, SEEK_CUR);
2070 getbits(-1);
2071 for (row=0; row < height; row++) {
2072 memset (acarry, 0, sizeof acarry);
2073 for (col=0; col < raw_width; col++) {
2074 carry = acarry[col & 1];
2075 i = 2 * (carry[2] < 3);
2076 for (nbits=2+i; (ushort) carry[0] >> (nbits+i); nbits++);
2077 low = (sign = getbits(3)) & 3;
2078 sign = sign << 29 >> 31;
2079 if ((high = getbithuff(12,huff)) == 12)
2080 high = getbits(16-nbits) >> 1;
2081 carry[0] = (high << nbits) | getbits(nbits);
2082 diff = (carry[0] ^ sign) + carry[1];
2083 carry[1] = (diff*3 + carry[1]) >> 5;
2084 carry[2] = carry[0] > 16 ? 0 : carry[2]+1;
2085 if (col >= width) continue;
2086 if (row < 2 && col < 2) pred = 0;
2087 else if (row < 2) pred = RAW(row,col-2);
2088 else if (col < 2) pred = RAW(row-2,col);
2089 else {
2090 w = RAW(row,col-2);
2091 n = RAW(row-2,col);
2092 nw = RAW(row-2,col-2);
2093 if ((w < nw && nw < n) || (n < nw && nw < w)) {
2094 if (ABS(w-nw) > 32 || ABS(n-nw) > 32)
2095 pred = w + n - nw;
2096 else pred = (w + n) >> 1;
2097 } else pred = ABS(w-nw) > ABS(n-nw) ? w : n;
2098 }
2099 if ((RAW(row,col) = pred + ((diff << 2) | low)) >> 12) derror();
2100 }
2101 }
2102 }
2103
canon_crx_load_raw()2104 void CLASS canon_crx_load_raw()
2105 {
2106 }
2107
fuji_xtrans_load_raw()2108 void CLASS fuji_xtrans_load_raw()
2109 {
2110 }
2111
minolta_rd175_load_raw()2112 void CLASS minolta_rd175_load_raw()
2113 {
2114 uchar pixel[768];
2115 unsigned irow, box, row, col;
2116
2117 for (irow=0; irow < 1481; irow++) {
2118 if (fread (pixel, 1, 768, ifp) < 768) derror();
2119 box = irow / 82;
2120 row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
2121 switch (irow) {
2122 case 1477: case 1479: continue;
2123 case 1476: row = 984; break;
2124 case 1480: row = 985; break;
2125 case 1478: row = 985; box = 1;
2126 }
2127 if ((box < 12) && (box & 1)) {
2128 for (col=0; col < 1533; col++, row ^= 1)
2129 if (col != 1) RAW(row,col) = (col+1) & 2 ?
2130 pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
2131 RAW(row,1) = pixel[1] << 1;
2132 RAW(row,1533) = pixel[765] << 1;
2133 } else
2134 for (col=row & 1; col < 1534; col+=2)
2135 RAW(row,col) = pixel[col/2] << 1;
2136 }
2137 maximum = 0xff << 1;
2138 }
2139
quicktake_100_load_raw()2140 void CLASS quicktake_100_load_raw()
2141 {
2142 uchar pixel[484][644];
2143 static const short gstep[16] =
2144 { -89,-60,-44,-32,-22,-15,-8,-2,2,8,15,22,32,44,60,89 };
2145 static const short rstep[6][4] =
2146 { { -3,-1,1,3 }, { -5,-1,1,5 }, { -8,-2,2,8 },
2147 { -13,-3,3,13 }, { -19,-4,4,19 }, { -28,-6,6,28 } };
2148 static const short curve[256] =
2149 { 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,
2150 28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,53,
2151 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,
2152 79,80,81,82,83,84,86,88,90,92,94,97,99,101,103,105,107,110,112,114,116,
2153 118,120,123,125,127,129,131,134,136,138,140,142,144,147,149,151,153,155,
2154 158,160,162,164,166,168,171,173,175,177,179,181,184,186,188,190,192,195,
2155 197,199,201,203,205,208,210,212,214,216,218,221,223,226,230,235,239,244,
2156 248,252,257,261,265,270,274,278,283,287,291,296,300,305,309,313,318,322,
2157 326,331,335,339,344,348,352,357,361,365,370,374,379,383,387,392,396,400,
2158 405,409,413,418,422,426,431,435,440,444,448,453,457,461,466,470,474,479,
2159 483,487,492,496,500,508,519,531,542,553,564,575,587,598,609,620,631,643,
2160 654,665,676,687,698,710,721,732,743,754,766,777,788,799,810,822,833,844,
2161 855,866,878,889,900,911,922,933,945,956,967,978,989,1001,1012,1023 };
2162 int rb, row, col, sharp, val=0;
2163
2164 getbits(-1);
2165 memset (pixel, 0x80, sizeof pixel);
2166 for (row=2; row < height+2; row++) {
2167 for (col=2+(row & 1); col < width+2; col+=2) {
2168 val = ((pixel[row-1][col-1] + 2*pixel[row-1][col+1] +
2169 pixel[row][col-2]) >> 2) + gstep[getbits(4)];
2170 pixel[row][col] = val = LIM(val,0,255);
2171 if (col < 4)
2172 pixel[row][col-2] = pixel[row+1][~row & 1] = val;
2173 if (row == 2)
2174 pixel[row-1][col+1] = pixel[row-1][col+3] = val;
2175 }
2176 pixel[row][col] = val;
2177 }
2178 for (rb=0; rb < 2; rb++)
2179 for (row=2+rb; row < height+2; row+=2)
2180 for (col=3-(row & 1); col < width+2; col+=2) {
2181 if (row < 4 || col < 4) sharp = 2;
2182 else {
2183 val = ABS(pixel[row-2][col] - pixel[row][col-2])
2184 + ABS(pixel[row-2][col] - pixel[row-2][col-2])
2185 + ABS(pixel[row][col-2] - pixel[row-2][col-2]);
2186 sharp = val < 4 ? 0 : val < 8 ? 1 : val < 16 ? 2 :
2187 val < 32 ? 3 : val < 48 ? 4 : 5;
2188 }
2189 val = ((pixel[row-2][col] + pixel[row][col-2]) >> 1)
2190 + rstep[sharp][getbits(2)];
2191 pixel[row][col] = val = LIM(val,0,255);
2192 if (row < 4) pixel[row-2][col+2] = val;
2193 if (col < 4) pixel[row+2][col-2] = val;
2194 }
2195 for (row=2; row < height+2; row++)
2196 for (col=3-(row & 1); col < width+2; col+=2) {
2197 val = ((pixel[row][col-1] + (pixel[row][col] << 2) +
2198 pixel[row][col+1]) >> 1) - 0x100;
2199 pixel[row][col] = LIM(val,0,255);
2200 }
2201 for (row=0; row < height; row++)
2202 for (col=0; col < width; col++)
2203 RAW(row,col) = curve[pixel[row+2][col+2]];
2204 maximum = 0x3ff;
2205 }
2206
2207 #define radc_token(tree) ((signed char) getbithuff(8,huff[tree]))
2208
2209 #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
2210
2211 #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
2212 : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
2213
kodak_radc_load_raw()2214 void CLASS kodak_radc_load_raw()
2215 {
2216 static const char src[] = {
2217 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
2218 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
2219 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
2220 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
2221 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
2222 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
2223 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
2224 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
2225 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
2226 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
2227 1,0, 2,2, 2,-2,
2228 1,-3, 1,3,
2229 2,-17, 2,-5, 2,5, 2,17,
2230 2,-7, 2,2, 2,9, 2,18,
2231 2,-18, 2,-9, 2,-2, 2,7,
2232 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
2233 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
2234 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
2235 };
2236 ushort huff[19][256];
2237 int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
2238 short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
2239 static const ushort pt[] =
2240 { 0,0, 1280,1344, 2320,3616, 3328,8000, 4095,16383, 65535,16383 };
2241
2242 for (i=2; i < 12; i+=2)
2243 for (c=pt[i-2]; c <= pt[i]; c++)
2244 curve[c] = (float)
2245 (c-pt[i-2]) / (pt[i]-pt[i-2]) * (pt[i+1]-pt[i-1]) + pt[i-1] + 0.5;
2246 for (s=i=0; i < sizeof src; i+=2)
2247 FORC(256 >> src[i])
2248 ((ushort *)huff)[s++] = src[i] << 8 | (uchar) src[i+1];
2249 s = kodak_cbpp == 243 ? 2 : 3;
2250 FORC(256) huff[18][c] = (8-s) << 8 | c >> s << s | 1 << (s-1);
2251 getbits(-1);
2252 for (i=0; i < sizeof(buf)/sizeof(short); i++)
2253 ((short *)buf)[i] = 2048;
2254 for (row=0; row < height; row+=4) {
2255 FORC3 mul[c] = getbits(6);
2256 FORC3 {
2257 val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
2258 s = val > 65564 ? 10:12;
2259 x = ~(-1 << (s-1));
2260 val <<= 12-s;
2261 for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
2262 ((short *)buf[c])[i] = (((short *)buf[c])[i] * val + x) >> s;
2263 last[c] = mul[c];
2264 for (r=0; r <= !c; r++) {
2265 buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
2266 for (tree=1, col=width/2; col > 0; ) {
2267 if ((tree = radc_token(tree))) {
2268 col -= 2;
2269 if (tree == 8)
2270 FORYX buf[c][y][x] = (uchar) radc_token(18) * mul[c];
2271 else
2272 FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
2273 } else
2274 do {
2275 nreps = (col > 2) ? radc_token(9) + 1 : 1;
2276 for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
2277 col -= 2;
2278 FORYX buf[c][y][x] = PREDICTOR;
2279 if (rep & 1) {
2280 step = radc_token(10) << 4;
2281 FORYX buf[c][y][x] += step;
2282 }
2283 }
2284 } while (nreps == 9);
2285 }
2286 for (y=0; y < 2; y++)
2287 for (x=0; x < width/2; x++) {
2288 val = (buf[c][y+1][x] << 4) / mul[c];
2289 if (val < 0) val = 0;
2290 if (c) RAW(row+y*2+c-1,x*2+2-c) = val;
2291 else RAW(row+r*2+y,x*2+y) = val;
2292 }
2293 memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
2294 }
2295 }
2296 for (y=row; y < row+4; y++)
2297 for (x=0; x < width; x++)
2298 if ((x+y) & 1) {
2299 r = x ? x-1 : x+1;
2300 s = x+1 < width ? x+1 : x-1;
2301 val = (RAW(y,x)-2048)*2 + (RAW(y,r)+RAW(y,s))/2;
2302 if (val < 0) val = 0;
2303 RAW(y,x) = val;
2304 }
2305 }
2306 for (i=0; i < height*width; i++)
2307 raw_image[i] = curve[raw_image[i]];
2308 maximum = 0x3fff;
2309 }
2310
2311 #undef FORYX
2312 #undef PREDICTOR
2313
2314 #ifdef NO_JPEG
kodak_jpeg_load_raw()2315 void CLASS kodak_jpeg_load_raw() {}
lossy_dng_load_raw()2316 void CLASS lossy_dng_load_raw() {}
2317 #else
2318
2319 METHODDEF(boolean)
fill_input_buffer(j_decompress_ptr cinfo)2320 fill_input_buffer (j_decompress_ptr cinfo)
2321 {
2322 static uchar jpeg_buffer[4096];
2323 size_t nbytes;
2324
2325 nbytes = fread (jpeg_buffer, 1, 4096, ifp);
2326 swab (jpeg_buffer, jpeg_buffer, nbytes);
2327 cinfo->src->next_input_byte = jpeg_buffer;
2328 cinfo->src->bytes_in_buffer = nbytes;
2329 return TRUE;
2330 }
2331
kodak_jpeg_load_raw()2332 void CLASS kodak_jpeg_load_raw()
2333 {
2334 struct jpeg_decompress_struct cinfo;
2335 struct jpeg_error_mgr jerr;
2336 JSAMPARRAY buf;
2337 JSAMPLE (*pixel)[3];
2338 int row, col;
2339
2340 cinfo.err = jpeg_std_error (&jerr);
2341 jpeg_create_decompress (&cinfo);
2342 jpeg_stdio_src (&cinfo, ifp);
2343 cinfo.src->fill_input_buffer = fill_input_buffer;
2344 jpeg_read_header (&cinfo, TRUE);
2345 jpeg_start_decompress (&cinfo);
2346 if ((cinfo.output_width != width ) ||
2347 (cinfo.output_height*2 != height ) ||
2348 (cinfo.output_components != 3 )) {
2349 fprintf (stderr,_("%s: incorrect JPEG dimensions\n"), ifname);
2350 jpeg_destroy_decompress (&cinfo);
2351 longjmp (failure, 3);
2352 }
2353 buf = (*cinfo.mem->alloc_sarray)
2354 ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
2355
2356 while (cinfo.output_scanline < cinfo.output_height) {
2357 row = cinfo.output_scanline * 2;
2358 jpeg_read_scanlines (&cinfo, buf, 1);
2359 pixel = (JSAMPLE (*)[3]) buf[0];
2360 for (col=0; col < width; col+=2) {
2361 RAW(row+0,col+0) = pixel[col+0][1] << 1;
2362 RAW(row+1,col+1) = pixel[col+1][1] << 1;
2363 RAW(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
2364 RAW(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
2365 }
2366 }
2367 jpeg_finish_decompress (&cinfo);
2368 jpeg_destroy_decompress (&cinfo);
2369 maximum = 0xff << 1;
2370 }
2371
2372 void CLASS gamma_curve (double pwr, double ts, int mode, int imax);
2373
lossy_dng_load_raw()2374 void CLASS lossy_dng_load_raw()
2375 {
2376 struct jpeg_decompress_struct cinfo;
2377 struct jpeg_error_mgr jerr;
2378 JSAMPARRAY buf;
2379 JSAMPLE (*pixel)[3];
2380 unsigned sorder=order, ntags, opcode, deg, i, j, c;
2381 unsigned save=data_offset-4, trow=0, tcol=0, row, col;
2382 ushort cur[3][256];
2383 double coeff[9], tot;
2384
2385 if (meta_offset) {
2386 fseek (ifp, meta_offset, SEEK_SET);
2387 order = 0x4d4d;
2388 ntags = get4();
2389 while (ntags--) {
2390 opcode = get4(); get4(); get4();
2391 if (opcode != 8)
2392 { fseek (ifp, get4(), SEEK_CUR); continue; }
2393 fseek (ifp, 20, SEEK_CUR);
2394 if ((c = get4()) > 2) break;
2395 fseek (ifp, 12, SEEK_CUR);
2396 if ((deg = get4()) > 8) break;
2397 for (i=0; i <= deg && i < 9; i++)
2398 coeff[i] = getreal(12);
2399 for (i=0; i < 256; i++) {
2400 for (tot=j=0; j <= deg; j++)
2401 tot += coeff[j] * pow(i/255.0, j);
2402 cur[c][i] = tot*0xffff;
2403 }
2404 }
2405 order = sorder;
2406 } else {
2407 gamma_curve (1/2.4, 12.92, 1, 255);
2408 FORC3 memcpy (cur[c], curve, sizeof cur[0]);
2409 }
2410 cinfo.err = jpeg_std_error (&jerr);
2411 jpeg_create_decompress (&cinfo);
2412 while (trow < raw_height) {
2413 fseek (ifp, save+=4, SEEK_SET);
2414 if (tile_length < INT_MAX)
2415 fseek (ifp, get4(), SEEK_SET);
2416 jpeg_stdio_src (&cinfo, ifp);
2417 jpeg_read_header (&cinfo, TRUE);
2418 jpeg_start_decompress (&cinfo);
2419 buf = (*cinfo.mem->alloc_sarray)
2420 ((j_common_ptr) &cinfo, JPOOL_IMAGE, cinfo.output_width*3, 1);
2421 while (cinfo.output_scanline < cinfo.output_height &&
2422 (row = trow + cinfo.output_scanline) < height) {
2423 jpeg_read_scanlines (&cinfo, buf, 1);
2424 pixel = (JSAMPLE (*)[3]) buf[0];
2425 for (col=0; col < cinfo.output_width && tcol+col < width; col++) {
2426 FORC3 image[row*width+tcol+col][c] = cur[c][pixel[col][c]];
2427 }
2428 }
2429 jpeg_abort_decompress (&cinfo);
2430 if ((tcol += tile_width) >= raw_width)
2431 trow += tile_length + (tcol = 0);
2432 }
2433 jpeg_destroy_decompress (&cinfo);
2434 maximum = 0xffff;
2435 }
2436 #endif
2437
kodak_dc120_load_raw()2438 void CLASS kodak_dc120_load_raw()
2439 {
2440 static const int mul[4] = { 162, 192, 187, 92 };
2441 static const int add[4] = { 0, 636, 424, 212 };
2442 uchar pixel[848];
2443 int row, shift, col;
2444
2445 for (row=0; row < height; row++) {
2446 if (fread (pixel, 1, 848, ifp) < 848) derror();
2447 shift = row * mul[row & 3] + add[row & 3];
2448 for (col=0; col < width; col++)
2449 RAW(row,col) = (ushort) pixel[(col + shift) % 848];
2450 }
2451 maximum = 0xff;
2452 }
2453
eight_bit_load_raw()2454 void CLASS eight_bit_load_raw()
2455 {
2456 uchar *pixel;
2457 unsigned row, col;
2458
2459 pixel = (uchar *) calloc (raw_width, sizeof *pixel);
2460 merror (pixel, "eight_bit_load_raw()");
2461 for (row=0; row < raw_height; row++) {
2462 if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
2463 for (col=0; col < raw_width; col++)
2464 RAW(row,col) = curve[pixel[col]];
2465 }
2466 free (pixel);
2467 maximum = curve[0xff];
2468 }
2469
kodak_c330_load_raw()2470 void CLASS kodak_c330_load_raw()
2471 {
2472 uchar *pixel;
2473 int row, col, y, cb, cr, rgb[3], c;
2474
2475 pixel = (uchar *) calloc (raw_width, 2*sizeof *pixel);
2476 merror (pixel, "kodak_c330_load_raw()");
2477 for (row=0; row < height; row++) {
2478 if (fread (pixel, raw_width, 2, ifp) < 2) derror();
2479 if (load_flags && (row & 31) == 31)
2480 fseek (ifp, raw_width*32, SEEK_CUR);
2481 for (col=0; col < width; col++) {
2482 y = pixel[col*2];
2483 cb = pixel[(col*2 & -4) | 1] - 128;
2484 cr = pixel[(col*2 & -4) | 3] - 128;
2485 rgb[1] = y - ((cb + cr + 2) >> 2);
2486 rgb[2] = rgb[1] + cb;
2487 rgb[0] = rgb[1] + cr;
2488 FORC3 image[row*width+col][c] = curve[LIM(rgb[c],0,255)];
2489 }
2490 }
2491 free (pixel);
2492 maximum = curve[0xff];
2493 }
2494
kodak_c603_load_raw()2495 void CLASS kodak_c603_load_raw()
2496 {
2497 uchar *pixel;
2498 int row, col, y, cb, cr, rgb[3], c;
2499
2500 pixel = (uchar *) calloc (raw_width, 3*sizeof *pixel);
2501 merror (pixel, "kodak_c603_load_raw()");
2502 for (row=0; row < height; row++) {
2503 if (~row & 1)
2504 if (fread (pixel, raw_width, 3, ifp) < 3) derror();
2505 for (col=0; col < width; col++) {
2506 y = pixel[width*2*(row & 1) + col];
2507 cb = pixel[width + (col & -2)] - 128;
2508 cr = pixel[width + (col & -2)+1] - 128;
2509 rgb[1] = y - ((cb + cr + 2) >> 2);
2510 rgb[2] = rgb[1] + cb;
2511 rgb[0] = rgb[1] + cr;
2512 FORC3 image[row*width+col][c] = curve[LIM(rgb[c],0,255)];
2513 }
2514 }
2515 free (pixel);
2516 maximum = curve[0xff];
2517 }
2518
kodak_262_load_raw()2519 void CLASS kodak_262_load_raw()
2520 {
2521 static const uchar kodak_tree[2][26] =
2522 { { 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 },
2523 { 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 } };
2524 ushort *huff[2];
2525 uchar *pixel;
2526 int *strip, ns, c, row, col, chess, pi=0, pi1, pi2, pred, val;
2527
2528 FORC(2) huff[c] = make_decoder (kodak_tree[c]);
2529 ns = (raw_height+63) >> 5;
2530 pixel = (uchar *) malloc (raw_width*32 + ns*4);
2531 merror (pixel, "kodak_262_load_raw()");
2532 strip = (int *) (pixel + raw_width*32);
2533 order = 0x4d4d;
2534 FORC(ns) strip[c] = get4();
2535 for (row=0; row < raw_height; row++) {
2536 if ((row & 31) == 0) {
2537 fseek (ifp, strip[row >> 5], SEEK_SET);
2538 getbits(-1);
2539 pi = 0;
2540 }
2541 for (col=0; col < raw_width; col++) {
2542 chess = (row + col) & 1;
2543 pi1 = chess ? pi-2 : pi-raw_width-1;
2544 pi2 = chess ? pi-2*raw_width : pi-raw_width+1;
2545 if (col <= chess) pi1 = -1;
2546 if (pi1 < 0) pi1 = pi2;
2547 if (pi2 < 0) pi2 = pi1;
2548 if (pi1 < 0 && col > 1) pi1 = pi2 = pi-2;
2549 pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
2550 pixel[pi] = val = pred + ljpeg_diff (huff[chess]);
2551 if (val >> 8) derror();
2552 val = curve[pixel[pi++]];
2553 RAW(row,col) = val;
2554 }
2555 }
2556 free (pixel);
2557 FORC(2) free (huff[c]);
2558 }
2559
kodak_65000_decode(short * out,int bsize)2560 int CLASS kodak_65000_decode (short *out, int bsize)
2561 {
2562 uchar c, blen[768];
2563 ushort raw[6];
2564 INT64 bitbuf=0;
2565 int save, bits=0, i, j, len, diff;
2566
2567 save = ftell(ifp);
2568 bsize = (bsize + 3) & -4;
2569 for (i=0; i < bsize; i+=2) {
2570 c = fgetc(ifp);
2571 if ((blen[i ] = c & 15) > 12 ||
2572 (blen[i+1] = c >> 4) > 12 ) {
2573 fseek (ifp, save, SEEK_SET);
2574 for (i=0; i < bsize; i+=8) {
2575 read_shorts (raw, 6);
2576 out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
2577 out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
2578 for (j=0; j < 6; j++)
2579 out[i+2+j] = raw[j] & 0xfff;
2580 }
2581 return 1;
2582 }
2583 }
2584 if ((bsize & 7) == 4) {
2585 bitbuf = fgetc(ifp) << 8;
2586 bitbuf += fgetc(ifp);
2587 bits = 16;
2588 }
2589 for (i=0; i < bsize; i++) {
2590 len = blen[i];
2591 if (bits < len) {
2592 for (j=0; j < 32; j+=8)
2593 bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
2594 bits += 32;
2595 }
2596 diff = bitbuf & (0xffff >> (16-len));
2597 bitbuf >>= len;
2598 bits -= len;
2599 if ((diff & (1 << (len-1))) == 0)
2600 diff -= (1 << len) - 1;
2601 out[i] = diff;
2602 }
2603 return 0;
2604 }
2605
kodak_65000_load_raw()2606 void CLASS kodak_65000_load_raw()
2607 {
2608 short buf[256];
2609 int row, col, len, pred[2], ret, i;
2610
2611 for (row=0; row < height; row++)
2612 for (col=0; col < width; col+=256) {
2613 pred[0] = pred[1] = 0;
2614 len = MIN (256, width-col);
2615 ret = kodak_65000_decode (buf, len);
2616 for (i=0; i < len; i++)
2617 if ((RAW(row,col+i) = curve[ret ? buf[i] :
2618 (pred[i & 1] += buf[i])]) >> 12) derror();
2619 }
2620 }
2621
kodak_ycbcr_load_raw()2622 void CLASS kodak_ycbcr_load_raw()
2623 {
2624 short buf[384], *bp;
2625 int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
2626 ushort *ip;
2627
2628 if (!image) return;
2629 for (row=0; row < height; row+=2)
2630 for (col=0; col < width; col+=128) {
2631 len = MIN (128, width-col);
2632 kodak_65000_decode (buf, len*3);
2633 y[0][1] = y[1][1] = cb = cr = 0;
2634 for (bp=buf, i=0; i < len; i+=2, bp+=2) {
2635 cb += bp[4];
2636 cr += bp[5];
2637 rgb[1] = -((cb + cr + 2) >> 2);
2638 rgb[2] = rgb[1] + cb;
2639 rgb[0] = rgb[1] + cr;
2640 for (j=0; j < 2; j++)
2641 for (k=0; k < 2; k++) {
2642 if ((y[j][k] = y[j][k^1] + *bp++) >> 10) derror();
2643 ip = image[(row+j)*width + col+i+k];
2644 FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
2645 }
2646 }
2647 }
2648 }
2649
kodak_rgb_load_raw()2650 void CLASS kodak_rgb_load_raw()
2651 {
2652 short buf[768], *bp;
2653 int row, col, len, c, i, rgb[3];
2654 ushort *ip=image[0];
2655
2656 for (row=0; row < height; row++)
2657 for (col=0; col < width; col+=256) {
2658 len = MIN (256, width-col);
2659 kodak_65000_decode (buf, len*3);
2660 memset (rgb, 0, sizeof rgb);
2661 for (bp=buf, i=0; i < len; i++, ip+=4)
2662 FORC3 if ((ip[c] = rgb[c] += *bp++) >> 12) derror();
2663 }
2664 }
2665
kodak_thumb_load_raw()2666 void CLASS kodak_thumb_load_raw()
2667 {
2668 int row, col;
2669 colors = thumb_misc >> 5;
2670 for (row=0; row < height; row++)
2671 for (col=0; col < width; col++)
2672 read_shorts (image[row*width+col], colors);
2673 maximum = (1 << (thumb_misc & 31)) - 1;
2674 }
2675
sony_decrypt(unsigned * data,int len,int start,int key)2676 void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
2677 {
2678 static unsigned pad[128], p;
2679
2680 if (start) {
2681 for (p=0; p < 4; p++)
2682 pad[p] = key = key * 48828125 + 1;
2683 pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
2684 for (p=4; p < 127; p++)
2685 pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
2686 for (p=0; p < 127; p++)
2687 pad[p] = htonl(pad[p]);
2688 }
2689 while (len-- && p++)
2690 *data++ ^= pad[(p-1) & 127] = pad[p & 127] ^ pad[(p+64) & 127];
2691 }
2692
sony_load_raw()2693 void CLASS sony_load_raw()
2694 {
2695 uchar head[40];
2696 ushort *pixel;
2697 unsigned i, key, row, col;
2698
2699 fseek (ifp, 200896, SEEK_SET);
2700 fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
2701 order = 0x4d4d;
2702 key = get4();
2703 fseek (ifp, 164600, SEEK_SET);
2704 fread (head, 1, 40, ifp);
2705 sony_decrypt ((unsigned *) head, 10, 1, key);
2706 for (i=26; i-- > 22; )
2707 key = key << 8 | head[i];
2708 fseek (ifp, data_offset, SEEK_SET);
2709 for (row=0; row < raw_height; row++) {
2710 pixel = raw_image + row*raw_width;
2711 if (fread (pixel, 2, raw_width, ifp) < raw_width) derror();
2712 sony_decrypt ((unsigned *) pixel, raw_width/2, !row, key);
2713 for (col=0; col < raw_width; col++)
2714 if ((pixel[col] = ntohs(pixel[col])) >> 14) derror();
2715 }
2716 maximum = 0x3ff0;
2717 }
2718
sony_arw_load_raw()2719 void CLASS sony_arw_load_raw()
2720 {
2721 ushort huff[32770];
2722 static const ushort tab[18] =
2723 { 0xf11,0xf10,0xe0f,0xd0e,0xc0d,0xb0c,0xa0b,0x90a,0x809,
2724 0x708,0x607,0x506,0x405,0x304,0x303,0x300,0x202,0x201 };
2725 int i, c, n, col, row, sum=0;
2726
2727 huff[0] = 15;
2728 for (n=i=0; i < 18; i++)
2729 FORC(32768 >> (tab[i] >> 8)) huff[++n] = tab[i];
2730 getbits(-1);
2731 for (col = raw_width; col--; )
2732 for (row=0; row < raw_height+1; row+=2) {
2733 if (row == raw_height) row = 1;
2734 if ((sum += ljpeg_diff(huff)) >> 12) derror();
2735 if (row < height) RAW(row,col) = sum;
2736 }
2737 }
2738
sony_arw2_load_raw()2739 void CLASS sony_arw2_load_raw()
2740 {
2741 uchar *data, *dp;
2742 ushort pix[16];
2743 int row, col, val, max, min, imax, imin, sh, bit, i;
2744
2745 data = (uchar *) malloc (raw_width+1);
2746 merror (data, "sony_arw2_load_raw()");
2747 for (row=0; row < height; row++) {
2748 fread (data, 1, raw_width, ifp);
2749 for (dp=data, col=0; col < raw_width-30; dp+=16) {
2750 max = 0x7ff & (val = sget4(dp));
2751 min = 0x7ff & val >> 11;
2752 imax = 0x0f & val >> 22;
2753 imin = 0x0f & val >> 26;
2754 for (sh=0; sh < 4 && 0x80 << sh <= max-min; sh++);
2755 for (bit=30, i=0; i < 16; i++)
2756 if (i == imax) pix[i] = max;
2757 else if (i == imin) pix[i] = min;
2758 else {
2759 pix[i] = ((sget2(dp+(bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
2760 if (pix[i] > 0x7ff) pix[i] = 0x7ff;
2761 bit += 7;
2762 }
2763 for (i=0; i < 16; i++, col+=2)
2764 RAW(row,col) = curve[pix[i] << 1] >> 2;
2765 col -= col & 1 ? 1:31;
2766 }
2767 }
2768 free (data);
2769 }
2770
samsung_load_raw()2771 void CLASS samsung_load_raw()
2772 {
2773 int row, col, c, i, dir, op[4], len[4];
2774
2775 order = 0x4949;
2776 for (row=0; row < raw_height; row++) {
2777 fseek (ifp, strip_offset+row*4, SEEK_SET);
2778 fseek (ifp, data_offset+get4(), SEEK_SET);
2779 ph1_bits(-1);
2780 FORC4 len[c] = row < 2 ? 7:4;
2781 for (col=0; col < raw_width; col+=16) {
2782 dir = ph1_bits(1);
2783 FORC4 op[c] = ph1_bits(2);
2784 FORC4 switch (op[c]) {
2785 case 3: len[c] = ph1_bits(4); break;
2786 case 2: len[c]--; break;
2787 case 1: len[c]++;
2788 }
2789 for (c=0; c < 16; c+=2) {
2790 i = len[((c & 1) << 1) | (c >> 3)];
2791 RAW(row,col+c) = ((signed) ph1_bits(i) << (32-i) >> (32-i)) +
2792 (dir ? RAW(row+(~c | -2),col+c) : col ? RAW(row,col+(c | -2)) : 128);
2793 if (c == 14) c = -1;
2794 }
2795 }
2796 }
2797 for (row=0; row < raw_height-1; row+=2)
2798 for (col=0; col < raw_width-1; col+=2)
2799 SWAP (RAW(row,col+1), RAW(row+1,col));
2800 }
2801
samsung2_load_raw()2802 void CLASS samsung2_load_raw()
2803 {
2804 static const ushort tab[14] =
2805 { 0x304,0x307,0x206,0x205,0x403,0x600,0x709,
2806 0x80a,0x90b,0xa0c,0xa0d,0x501,0x408,0x402 };
2807 ushort huff[1026], vpred[2][2] = {{0,0},{0,0}}, hpred[2];
2808 int i, c, n, row, col, diff;
2809
2810 huff[0] = 10;
2811 for (n=i=0; i < 14; i++)
2812 FORC(1024 >> (tab[i] >> 8)) huff[++n] = tab[i];
2813 getbits(-1);
2814 for (row=0; row < raw_height; row++)
2815 for (col=0; col < raw_width; col++) {
2816 diff = ljpeg_diff (huff);
2817 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
2818 else hpred[col & 1] += diff;
2819 RAW(row,col) = hpred[col & 1];
2820 if (hpred[col & 1] >> tiff_bps) derror();
2821 }
2822 }
2823
samsung3_load_raw()2824 void CLASS samsung3_load_raw()
2825 {
2826 int opt, init, mag, pmode, row, tab, col, pred, diff, i, c;
2827 ushort lent[3][2], len[4], *prow[2];
2828
2829 order = 0x4949;
2830 fseek (ifp, 9, SEEK_CUR);
2831 opt = fgetc(ifp);
2832 init = (get2(),get2());
2833 for (row=0; row < raw_height; row++) {
2834 fseek (ifp, (data_offset-ftell(ifp)) & 15, SEEK_CUR);
2835 ph1_bits(-1);
2836 mag = 0; pmode = 7;
2837 FORC(6) ((ushort *)lent)[c] = row < 2 ? 7:4;
2838 prow[ row & 1] = &RAW(row-1,1-((row & 1) << 1)); // green
2839 prow[~row & 1] = &RAW(row-2,0); // red and blue
2840 for (tab=0; tab+15 < raw_width; tab+=16) {
2841 if (~opt & 4 && !(tab & 63)) {
2842 i = ph1_bits(2);
2843 mag = i < 3 ? mag-'2'+"204"[i] : ph1_bits(12);
2844 }
2845 if (opt & 2)
2846 pmode = 7 - 4*ph1_bits(1);
2847 else if (!ph1_bits(1))
2848 pmode = ph1_bits(3);
2849 if (opt & 1 || !ph1_bits(1)) {
2850 FORC4 len[c] = ph1_bits(2);
2851 FORC4 {
2852 i = ((row & 1) << 1 | (c & 1)) % 3;
2853 len[c] = len[c] < 3 ? lent[i][0]-'1'+"120"[len[c]] : ph1_bits(4);
2854 lent[i][0] = lent[i][1];
2855 lent[i][1] = len[c];
2856 }
2857 }
2858 FORC(16) {
2859 col = tab + (((c & 7) << 1)^(c >> 3)^(row & 1));
2860 pred = (pmode == 7 || row < 2)
2861 ? (tab ? RAW(row,tab-2+(col & 1)) : init)
2862 : (prow[col & 1][col-'4'+"0224468"[pmode]] +
2863 prow[col & 1][col-'4'+"0244668"[pmode]] + 1) >> 1;
2864 diff = ph1_bits (i = len[c >> 2]);
2865 if (diff >> (i-1)) diff -= 1 << i;
2866 diff = diff * (mag*2+1) + mag;
2867 RAW(row,col) = pred + diff;
2868 }
2869 }
2870 }
2871 }
2872
2873 #define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
2874
2875 /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
smal_decode_segment(unsigned seg[2][2],int holes)2876 void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
2877 {
2878 uchar hist[3][13] = {
2879 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2880 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2881 { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
2882 int low, high=0xff, carry=0, nbits=8;
2883 int pix, s, count, bin, next, i, sym[3];
2884 uchar diff, pred[]={0,0};
2885 ushort data=0, range=0;
2886
2887 fseek (ifp, seg[0][1]+1, SEEK_SET);
2888 getbits(-1);
2889 if (seg[1][0] > raw_width*raw_height)
2890 seg[1][0] = raw_width*raw_height;
2891 for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
2892 for (s=0; s < 3; s++) {
2893 data = data << nbits | getbits(nbits);
2894 if (carry < 0)
2895 carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
2896 while (--nbits >= 0)
2897 if ((data >> nbits & 0xff) == 0xff) break;
2898 if (nbits > 0)
2899 data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
2900 ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
2901 if (nbits >= 0) {
2902 data += getbits(1);
2903 carry = nbits - 8;
2904 }
2905 count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
2906 for (bin=0; hist[s][bin+5] > count; bin++);
2907 low = hist[s][bin+5] * (high >> 4) >> 2;
2908 if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
2909 high -= low;
2910 for (nbits=0; high << nbits < 128; nbits++);
2911 range = (range+low) << nbits;
2912 high <<= nbits;
2913 next = hist[s][1];
2914 if (++hist[s][2] > hist[s][3]) {
2915 next = (next+1) & hist[s][0];
2916 hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
2917 hist[s][2] = 1;
2918 }
2919 if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
2920 if (bin < hist[s][1])
2921 for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
2922 else if (next <= bin)
2923 for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
2924 }
2925 hist[s][1] = next;
2926 sym[s] = bin;
2927 }
2928 diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
2929 if (sym[0] & 4)
2930 diff = diff ? -diff : 0x80;
2931 if (ftell(ifp) + 12 >= seg[1][1])
2932 diff = 0;
2933 raw_image[pix] = pred[pix & 1] += diff;
2934 if (!(pix & 1) && HOLE(pix / raw_width)) pix += 2;
2935 }
2936 maximum = 0xff;
2937 }
2938
smal_v6_load_raw()2939 void CLASS smal_v6_load_raw()
2940 {
2941 unsigned seg[2][2];
2942
2943 fseek (ifp, 16, SEEK_SET);
2944 seg[0][0] = 0;
2945 seg[0][1] = get2();
2946 seg[1][0] = raw_width * raw_height;
2947 seg[1][1] = INT_MAX;
2948 smal_decode_segment (seg, 0);
2949 }
2950
median4(int * p)2951 int CLASS median4 (int *p)
2952 {
2953 int min, max, sum, i;
2954
2955 min = max = sum = p[0];
2956 for (i=1; i < 4; i++) {
2957 sum += p[i];
2958 if (min > p[i]) min = p[i];
2959 if (max < p[i]) max = p[i];
2960 }
2961 return (sum - min - max) >> 1;
2962 }
2963
fill_holes(int holes)2964 void CLASS fill_holes (int holes)
2965 {
2966 int row, col, val[4];
2967
2968 for (row=2; row < height-2; row++) {
2969 if (!HOLE(row)) continue;
2970 for (col=1; col < width-1; col+=4) {
2971 val[0] = RAW(row-1,col-1);
2972 val[1] = RAW(row-1,col+1);
2973 val[2] = RAW(row+1,col-1);
2974 val[3] = RAW(row+1,col+1);
2975 RAW(row,col) = median4(val);
2976 }
2977 for (col=2; col < width-2; col+=4)
2978 if (HOLE(row-2) || HOLE(row+2))
2979 RAW(row,col) = (RAW(row,col-2) + RAW(row,col+2)) >> 1;
2980 else {
2981 val[0] = RAW(row,col-2);
2982 val[1] = RAW(row,col+2);
2983 val[2] = RAW(row-2,col);
2984 val[3] = RAW(row+2,col);
2985 RAW(row,col) = median4(val);
2986 }
2987 }
2988 }
2989
smal_v9_load_raw()2990 void CLASS smal_v9_load_raw()
2991 {
2992 unsigned seg[256][2], offset, nseg, holes, i;
2993
2994 fseek (ifp, 67, SEEK_SET);
2995 offset = get4();
2996 nseg = (uchar) fgetc(ifp);
2997 fseek (ifp, offset, SEEK_SET);
2998 for (i=0; i < nseg*2; i++)
2999 ((unsigned *)seg)[i] = get4() + data_offset*(i & 1);
3000 fseek (ifp, 78, SEEK_SET);
3001 holes = fgetc(ifp);
3002 fseek (ifp, 88, SEEK_SET);
3003 seg[nseg][0] = raw_height * raw_width;
3004 seg[nseg][1] = get4() + data_offset;
3005 for (i=0; i < nseg; i++)
3006 smal_decode_segment (seg+i, holes);
3007 if (holes) fill_holes (holes);
3008 }
3009
redcine_load_raw()3010 void CLASS redcine_load_raw()
3011 {
3012 #ifndef NO_JASPER
3013 int c, row, col;
3014 jas_stream_t *in;
3015 jas_image_t *jimg;
3016 jas_matrix_t *jmat;
3017 jas_seqent_t *data;
3018 ushort *img, *pix;
3019
3020 jas_init();
3021 in = jas_stream_fopen (ifname, "rb");
3022 jas_stream_seek (in, data_offset+20, SEEK_SET);
3023 jimg = jas_image_decode (in, -1, 0);
3024 if (!jimg) longjmp (failure, 3);
3025 jmat = jas_matrix_create (height/2, width/2);
3026 merror (jmat, "redcine_load_raw()");
3027 img = (ushort *) calloc ((height+2), (width+2)*2);
3028 merror (img, "redcine_load_raw()");
3029 FORC4 {
3030 jas_image_readcmpt (jimg, c, 0, 0, width/2, height/2, jmat);
3031 data = jas_matrix_getref (jmat, 0, 0);
3032 for (row = c >> 1; row < height; row+=2)
3033 for (col = c & 1; col < width; col+=2)
3034 img[(row+1)*(width+2)+col+1] = data[(row/2)*(width/2)+col/2];
3035 }
3036 for (col=1; col <= width; col++) {
3037 img[col] = img[2*(width+2)+col];
3038 img[(height+1)*(width+2)+col] = img[(height-1)*(width+2)+col];
3039 }
3040 for (row=0; row < height+2; row++) {
3041 img[row*(width+2)] = img[row*(width+2)+2];
3042 img[(row+1)*(width+2)-1] = img[(row+1)*(width+2)-3];
3043 }
3044 for (row=1; row <= height; row++) {
3045 pix = img + row*(width+2) + (col = 1 + (FC(row,1) & 1));
3046 for ( ; col <= width; col+=2, pix+=2) {
3047 c = (((pix[0] - 0x800) << 3) +
3048 pix[-(width+2)] + pix[width+2] + pix[-1] + pix[1]) >> 2;
3049 pix[0] = LIM(c,0,4095);
3050 }
3051 }
3052 for (row=0; row < height; row++)
3053 for (col=0; col < width; col++)
3054 RAW(row,col) = curve[img[(row+1)*(width+2)+col+1]];
3055 free (img);
3056 jas_matrix_destroy (jmat);
3057 jas_image_destroy (jimg);
3058 jas_stream_close (in);
3059 #endif
3060 }
3061
3062 /* RESTRICTED code starts here */
3063
foveon_decoder(unsigned size,unsigned code)3064 void CLASS foveon_decoder (unsigned size, unsigned code)
3065 {
3066 static unsigned huff[1024];
3067 struct decode *cur;
3068 int i, len;
3069
3070 if (!code) {
3071 for (i=0; i < size; i++)
3072 huff[i] = get4();
3073 memset (first_decode, 0, sizeof first_decode);
3074 free_decode = first_decode;
3075 }
3076 cur = free_decode++;
3077 if (free_decode > first_decode+2048) {
3078 fprintf (stderr,_("%s: decoder table overflow\n"), ifname);
3079 longjmp (failure, 2);
3080 }
3081 if (code)
3082 for (i=0; i < size; i++)
3083 if (huff[i] == code) {
3084 cur->leaf = i;
3085 return;
3086 }
3087 if ((len = code >> 27) > 26) return;
3088 code = (len+1) << 27 | (code & 0x3ffffff) << 1;
3089
3090 cur->branch[0] = free_decode;
3091 foveon_decoder (size, code);
3092 cur->branch[1] = free_decode;
3093 foveon_decoder (size, code+1);
3094 }
3095
foveon_thumb()3096 void CLASS foveon_thumb()
3097 {
3098 unsigned bwide, row, col, bitbuf=0, bit=1, c, i;
3099 char *buf;
3100 struct decode *dindex;
3101 short pred[3];
3102
3103 bwide = get4();
3104 fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
3105 if (bwide > 0) {
3106 if (bwide < thumb_width*3) return;
3107 buf = (char *) malloc (bwide);
3108 merror (buf, "foveon_thumb()");
3109 for (row=0; row < thumb_height; row++) {
3110 fread (buf, 1, bwide, ifp);
3111 fwrite (buf, 3, thumb_width, ofp);
3112 }
3113 free (buf);
3114 return;
3115 }
3116 foveon_decoder (256, 0);
3117
3118 for (row=0; row < thumb_height; row++) {
3119 memset (pred, 0, sizeof pred);
3120 if (!bit) get4();
3121 for (bit=col=0; col < thumb_width; col++)
3122 FORC3 {
3123 for (dindex=first_decode; dindex->branch[0]; ) {
3124 if ((bit = (bit-1) & 31) == 31)
3125 for (i=0; i < 4; i++)
3126 bitbuf = (bitbuf << 8) + fgetc(ifp);
3127 dindex = dindex->branch[bitbuf >> bit & 1];
3128 }
3129 pred[c] += dindex->leaf;
3130 fputc (pred[c], ofp);
3131 }
3132 }
3133 }
3134
foveon_sd_load_raw()3135 void CLASS foveon_sd_load_raw()
3136 {
3137 struct decode *dindex;
3138 short diff[1024];
3139 unsigned bitbuf=0;
3140 int pred[3], row, col, bit=-1, c, i;
3141
3142 read_shorts ((ushort *) diff, 1024);
3143 if (!load_flags) foveon_decoder (1024, 0);
3144
3145 for (row=0; row < height; row++) {
3146 memset (pred, 0, sizeof pred);
3147 if (!bit && !load_flags && atoi(model+2) < 14) get4();
3148 for (col=bit=0; col < width; col++) {
3149 if (load_flags) {
3150 bitbuf = get4();
3151 FORC3 pred[2-c] += diff[bitbuf >> c*10 & 0x3ff];
3152 }
3153 else FORC3 {
3154 for (dindex=first_decode; dindex->branch[0]; ) {
3155 if ((bit = (bit-1) & 31) == 31)
3156 for (i=0; i < 4; i++)
3157 bitbuf = (bitbuf << 8) + fgetc(ifp);
3158 dindex = dindex->branch[bitbuf >> bit & 1];
3159 }
3160 pred[c] += diff[dindex->leaf];
3161 if (pred[c] >> 16 && ~pred[c] >> 16) derror();
3162 }
3163 FORC3 image[row*width+col][c] = pred[c];
3164 }
3165 }
3166 }
3167
foveon_huff(ushort * huff)3168 void CLASS foveon_huff (ushort *huff)
3169 {
3170 int i, j, clen, code;
3171
3172 huff[0] = 8;
3173 for (i=0; i < 13; i++) {
3174 clen = getc(ifp);
3175 code = getc(ifp);
3176 for (j=0; j < 256 >> clen; )
3177 huff[code+ ++j] = clen << 8 | i;
3178 }
3179 get2();
3180 }
3181
foveon_dp_load_raw()3182 void CLASS foveon_dp_load_raw()
3183 {
3184 unsigned c, roff[4], row, col, diff;
3185 ushort huff[512], vpred[2][2], hpred[2];
3186
3187 fseek (ifp, 8, SEEK_CUR);
3188 foveon_huff (huff);
3189 roff[0] = 48;
3190 FORC3 roff[c+1] = -(-(roff[c] + get4()) & -16);
3191 FORC3 {
3192 fseek (ifp, data_offset+roff[c], SEEK_SET);
3193 getbits(-1);
3194 vpred[0][0] = vpred[0][1] = vpred[1][0] = vpred[1][1] = 512;
3195 for (row=0; row < height; row++) {
3196 for (col=0; col < width; col++) {
3197 diff = ljpeg_diff(huff);
3198 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
3199 else hpred[col & 1] += diff;
3200 image[row*width+col][c] = hpred[col & 1];
3201 }
3202 }
3203 }
3204 }
3205
foveon_load_camf()3206 void CLASS foveon_load_camf()
3207 {
3208 unsigned type, wide, high, i, j, row, col, diff;
3209 ushort huff[258], vpred[2][2] = {{512,512},{512,512}}, hpred[2];
3210
3211 fseek (ifp, meta_offset, SEEK_SET);
3212 type = get4(); get4(); get4();
3213 wide = get4();
3214 high = get4();
3215 if (type == 2) {
3216 fread (meta_data, 1, meta_length, ifp);
3217 for (i=0; i < meta_length; i++) {
3218 high = (high * 1597 + 51749) % 244944;
3219 wide = high * (INT64) 301593171 >> 24;
3220 meta_data[i] ^= ((((high << 8) - wide) >> 1) + wide) >> 17;
3221 }
3222 } else if (type == 4) {
3223 free (meta_data);
3224 meta_data = (char *) malloc (meta_length = wide*high*3/2);
3225 merror (meta_data, "foveon_load_camf()");
3226 foveon_huff (huff);
3227 get4();
3228 getbits(-1);
3229 for (j=row=0; row < high; row++) {
3230 for (col=0; col < wide; col++) {
3231 diff = ljpeg_diff(huff);
3232 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
3233 else hpred[col & 1] += diff;
3234 if (col & 1) {
3235 meta_data[j++] = hpred[0] >> 4;
3236 meta_data[j++] = hpred[0] << 4 | hpred[1] >> 8;
3237 meta_data[j++] = hpred[1];
3238 }
3239 }
3240 }
3241 } else
3242 fprintf (stderr,_("%s has unknown CAMF type %d.\n"), ifname, type);
3243 }
3244
foveon_camf_param(const char * block,const char * param)3245 const char * CLASS foveon_camf_param (const char *block, const char *param)
3246 {
3247 unsigned idx, num;
3248 char *pos, *cp, *dp;
3249
3250 for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
3251 pos = meta_data + idx;
3252 if (strncmp (pos, "CMb", 3)) break;
3253 if (pos[3] != 'P') continue;
3254 if (strcmp (block, pos+sget4(pos+12))) continue;
3255 cp = pos + sget4(pos+16);
3256 num = sget4(cp);
3257 dp = pos + sget4(cp+4);
3258 while (num--) {
3259 cp += 8;
3260 if (!strcmp (param, dp+sget4(cp)))
3261 return dp+sget4(cp+4);
3262 }
3263 }
3264 return 0;
3265 }
3266
foveon_camf_matrix(unsigned dim[3],const char * name)3267 void * CLASS foveon_camf_matrix (unsigned dim[3], const char *name)
3268 {
3269 unsigned i, idx, type, ndim, size, *mat;
3270 char *pos, *cp, *dp;
3271 double dsize;
3272
3273 for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
3274 pos = meta_data + idx;
3275 if (strncmp (pos, "CMb", 3)) break;
3276 if (pos[3] != 'M') continue;
3277 if (strcmp (name, pos+sget4(pos+12))) continue;
3278 dim[0] = dim[1] = dim[2] = 1;
3279 cp = pos + sget4(pos+16);
3280 type = sget4(cp);
3281 if ((ndim = sget4(cp+4)) > 3) break;
3282 dp = pos + sget4(cp+8);
3283 for (i=ndim; i--; ) {
3284 cp += 12;
3285 dim[i] = sget4(cp);
3286 }
3287 if ((dsize = (double) dim[0]*dim[1]*dim[2]) > meta_length/4) break;
3288 mat = (unsigned *) malloc ((size = dsize) * 4);
3289 merror (mat, "foveon_camf_matrix()");
3290 for (i=0; i < size; i++)
3291 if (type && type != 6)
3292 mat[i] = sget4(dp + i*4);
3293 else
3294 mat[i] = sget4(dp + i*2) & 0xffff;
3295 return mat;
3296 }
3297 fprintf (stderr,_("%s: \"%s\" matrix not found!\n"), ifname, name);
3298 return 0;
3299 }
3300
foveon_fixed(void * ptr,int size,const char * name)3301 int CLASS foveon_fixed (void *ptr, int size, const char *name)
3302 {
3303 void *dp;
3304 unsigned dim[3];
3305
3306 if (!name) return 0;
3307 dp = foveon_camf_matrix (dim, name);
3308 if (!dp) return 0;
3309 memcpy (ptr, dp, size*4);
3310 free (dp);
3311 return 1;
3312 }
3313
foveon_avg(short * pix,int range[2],float cfilt)3314 float CLASS foveon_avg (short *pix, int range[2], float cfilt)
3315 {
3316 int i;
3317 float val, min=FLT_MAX, max=-FLT_MAX, sum=0;
3318
3319 for (i=range[0]; i <= range[1]; i++) {
3320 sum += val = pix[i*4] + (pix[i*4]-pix[(i-1)*4]) * cfilt;
3321 if (min > val) min = val;
3322 if (max < val) max = val;
3323 }
3324 if (range[1] - range[0] == 1) return sum/2;
3325 return (sum - min - max) / (range[1] - range[0] - 1);
3326 }
3327
foveon_make_curve(double max,double mul,double filt)3328 short * CLASS foveon_make_curve (double max, double mul, double filt)
3329 {
3330 short *curve;
3331 unsigned i, size;
3332 double x;
3333
3334 if (!filt) filt = 0.8;
3335 size = 4*M_PI*max / filt;
3336 if (size == UINT_MAX) size--;
3337 curve = (short *) calloc (size+1, sizeof *curve);
3338 merror (curve, "foveon_make_curve()");
3339 curve[0] = size;
3340 for (i=0; i < size; i++) {
3341 x = i*filt/max/4;
3342 curve[i+1] = (cos(x)+1)/2 * tanh(i*filt/mul) * mul + 0.5;
3343 }
3344 return curve;
3345 }
3346
foveon_make_curves(short ** curvep,float dq[3],float div[3],float filt)3347 void CLASS foveon_make_curves
3348 (short **curvep, float dq[3], float div[3], float filt)
3349 {
3350 double mul[3], max=0;
3351 int c;
3352
3353 FORC3 mul[c] = dq[c]/div[c];
3354 FORC3 if (max < mul[c]) max = mul[c];
3355 FORC3 curvep[c] = foveon_make_curve (max, mul[c], filt);
3356 }
3357
foveon_apply_curve(short * curve,int i)3358 int CLASS foveon_apply_curve (short *curve, int i)
3359 {
3360 if (abs(i) >= curve[0]) return 0;
3361 return i < 0 ? -curve[1-i] : curve[1+i];
3362 }
3363
3364 #define image ((short (*)[4]) image)
3365
foveon_interpolate()3366 void CLASS foveon_interpolate()
3367 {
3368 static const short hood[] = { -1,-1, -1,0, -1,1, 0,-1, 0,1, 1,-1, 1,0, 1,1 };
3369 short *pix, prev[3], *curve[8], (*shrink)[3];
3370 float cfilt=0, ddft[3][3][2], ppm[3][3][3];
3371 float cam_xyz[3][3], correct[3][3], last[3][3], trans[3][3];
3372 float chroma_dq[3], color_dq[3], diag[3][3], div[3];
3373 float (*black)[3], (*sgain)[3], (*sgrow)[3];
3374 float fsum[3], val, frow, num;
3375 int row, col, c, i, j, diff, sgx, irow, sum, min, max, limit;
3376 int dscr[2][2], dstb[4], (*smrow[7])[3], total[4], ipix[3];
3377 int work[3][3], smlast, smred, smred_p=0, dev[3];
3378 int satlev[3], keep[4], active[4];
3379 unsigned dim[3], *badpix;
3380 double dsum=0, trsum[3];
3381 char str[128];
3382 const char* cp;
3383
3384 if (verbose)
3385 fprintf (stderr,_("Foveon interpolation...\n"));
3386
3387 foveon_load_camf();
3388 foveon_fixed (dscr, 4, "DarkShieldColRange");
3389 foveon_fixed (ppm[0][0], 27, "PostPolyMatrix");
3390 foveon_fixed (satlev, 3, "SaturationLevel");
3391 foveon_fixed (keep, 4, "KeepImageArea");
3392 foveon_fixed (active, 4, "ActiveImageArea");
3393 foveon_fixed (chroma_dq, 3, "ChromaDQ");
3394 foveon_fixed (color_dq, 3,
3395 foveon_camf_param ("IncludeBlocks", "ColorDQ") ?
3396 "ColorDQ" : "ColorDQCamRGB");
3397 if (foveon_camf_param ("IncludeBlocks", "ColumnFilter"))
3398 foveon_fixed (&cfilt, 1, "ColumnFilter");
3399
3400 memset (ddft, 0, sizeof ddft);
3401 if (!foveon_camf_param ("IncludeBlocks", "DarkDrift")
3402 || !foveon_fixed (ddft[1][0], 12, "DarkDrift"))
3403 for (i=0; i < 2; i++) {
3404 foveon_fixed (dstb, 4, i ? "DarkShieldBottom":"DarkShieldTop");
3405 for (row = dstb[1]; row <= dstb[3]; row++)
3406 for (col = dstb[0]; col <= dstb[2]; col++)
3407 FORC3 ddft[i+1][c][1] += (short) image[row*width+col][c];
3408 FORC3 ddft[i+1][c][1] /= (dstb[3]-dstb[1]+1) * (dstb[2]-dstb[0]+1);
3409 }
3410
3411 if (!(cp = foveon_camf_param ("WhiteBalanceIlluminants", model2)))
3412 { fprintf (stderr,_("%s: Invalid white balance \"%s\"\n"), ifname, model2);
3413 return; }
3414 foveon_fixed (cam_xyz, 9, cp);
3415 foveon_fixed (correct, 9,
3416 foveon_camf_param ("WhiteBalanceCorrections", model2));
3417 memset (last, 0, sizeof last);
3418 for (i=0; i < 3; i++)
3419 for (j=0; j < 3; j++)
3420 FORC3 last[i][j] += correct[i][c] * cam_xyz[c][j];
3421
3422 #define LAST(x,y) last[(i+x)%3][(c+y)%3]
3423 for (i=0; i < 3; i++)
3424 FORC3 diag[c][i] = LAST(1,1)*LAST(2,2) - LAST(1,2)*LAST(2,1);
3425 #undef LAST
3426 FORC3 div[c] = diag[c][0]*0.3127 + diag[c][1]*0.329 + diag[c][2]*0.3583;
3427 sprintf (str, "%sRGBNeutral", model2);
3428 if (foveon_camf_param ("IncludeBlocks", str))
3429 foveon_fixed (div, 3, str);
3430 num = 0;
3431 FORC3 if (num < div[c]) num = div[c];
3432 FORC3 div[c] /= num;
3433
3434 memset (trans, 0, sizeof trans);
3435 for (i=0; i < 3; i++)
3436 for (j=0; j < 3; j++)
3437 FORC3 trans[i][j] += rgb_cam[i][c] * last[c][j] * div[j];
3438 FORC3 trsum[c] = trans[c][0] + trans[c][1] + trans[c][2];
3439 dsum = (6*trsum[0] + 11*trsum[1] + 3*trsum[2]) / 20;
3440 for (i=0; i < 3; i++)
3441 FORC3 last[i][c] = trans[i][c] * dsum / trsum[i];
3442 memset (trans, 0, sizeof trans);
3443 for (i=0; i < 3; i++)
3444 for (j=0; j < 3; j++)
3445 FORC3 trans[i][j] += (i==c ? 32 : -1) * last[c][j] / 30;
3446
3447 foveon_make_curves (curve, color_dq, div, cfilt);
3448 FORC3 chroma_dq[c] /= 3;
3449 foveon_make_curves (curve+3, chroma_dq, div, cfilt);
3450 FORC3 dsum += chroma_dq[c] / div[c];
3451 curve[6] = foveon_make_curve (dsum, dsum, cfilt);
3452 curve[7] = foveon_make_curve (dsum*2, dsum*2, cfilt);
3453
3454 sgain = (float (*)[3]) foveon_camf_matrix (dim, "SpatialGain");
3455 if (!sgain) return;
3456 sgrow = (float (*)[3]) calloc (dim[1], sizeof *sgrow);
3457 sgx = (width + dim[1]-2) / (dim[1]-1);
3458
3459 black = (float (*)[3]) calloc (height, sizeof *black);
3460 for (row=0; row < height; row++) {
3461 for (i=0; i < 6; i++)
3462 ((float *)ddft[0])[i] = ((float *)ddft[1])[i] +
3463 row / (height-1.0) * (((float *)ddft[2])[i] - ((float *)ddft[1])[i]);
3464 FORC3 black[row][c] =
3465 ( foveon_avg (image[row*width]+c, dscr[0], cfilt) +
3466 foveon_avg (image[row*width]+c, dscr[1], cfilt) * 3
3467 - ddft[0][c][0] ) / 4 - ddft[0][c][1];
3468 }
3469 memcpy (black, black+8, sizeof *black*8);
3470 memcpy (black+height-11, black+height-22, 11*sizeof *black);
3471 memcpy (last, black, sizeof last);
3472
3473 for (row=1; row < height-1; row++) {
3474 FORC3 if (last[1][c] > last[0][c]) {
3475 if (last[1][c] > last[2][c])
3476 black[row][c] = (last[0][c] > last[2][c]) ? last[0][c]:last[2][c];
3477 } else
3478 if (last[1][c] < last[2][c])
3479 black[row][c] = (last[0][c] < last[2][c]) ? last[0][c]:last[2][c];
3480 memmove (last, last+1, 2*sizeof last[0]);
3481 memcpy (last[2], black[row+1], sizeof last[2]);
3482 }
3483 FORC3 black[row][c] = (last[0][c] + last[1][c])/2;
3484 FORC3 black[0][c] = (black[1][c] + black[3][c])/2;
3485
3486 val = 1 - exp(-1/24.0);
3487 memcpy (fsum, black, sizeof fsum);
3488 for (row=1; row < height; row++)
3489 FORC3 fsum[c] += black[row][c] =
3490 (black[row][c] - black[row-1][c])*val + black[row-1][c];
3491 memcpy (last[0], black[height-1], sizeof last[0]);
3492 FORC3 fsum[c] /= height;
3493 for (row = height; row--; )
3494 FORC3 last[0][c] = black[row][c] =
3495 (black[row][c] - fsum[c] - last[0][c])*val + last[0][c];
3496
3497 memset (total, 0, sizeof total);
3498 for (row=2; row < height; row+=4)
3499 for (col=2; col < width; col+=4) {
3500 FORC3 total[c] += (short) image[row*width+col][c];
3501 total[3]++;
3502 }
3503 for (row=0; row < height; row++)
3504 FORC3 black[row][c] += fsum[c]/2 + total[c]/(total[3]*100.0);
3505
3506 for (row=0; row < height; row++) {
3507 for (i=0; i < 6; i++)
3508 ((float *)ddft[0])[i] = ((float *)ddft[1])[i] +
3509 row / (height-1.0) * (((float *)ddft[2])[i] - ((float *)ddft[1])[i]);
3510 pix = image[row*width];
3511 memcpy (prev, pix, sizeof prev);
3512 frow = row / (height-1.0) * (dim[2]-1);
3513 if ((irow = frow) == dim[2]-1) irow--;
3514 frow -= irow;
3515 for (i=0; i < dim[1]; i++)
3516 FORC3 sgrow[i][c] = sgain[ irow *dim[1]+i][c] * (1-frow) +
3517 sgain[(irow+1)*dim[1]+i][c] * frow;
3518 for (col=0; col < width; col++) {
3519 FORC3 {
3520 diff = pix[c] - prev[c];
3521 prev[c] = pix[c];
3522 ipix[c] = pix[c] + floor ((diff + (diff*diff >> 14)) * cfilt
3523 - ddft[0][c][1] - ddft[0][c][0] * ((float) col/width - 0.5)
3524 - black[row][c] );
3525 }
3526 FORC3 {
3527 work[0][c] = ipix[c] * ipix[c] >> 14;
3528 work[2][c] = ipix[c] * work[0][c] >> 14;
3529 work[1][2-c] = ipix[(c+1) % 3] * ipix[(c+2) % 3] >> 14;
3530 }
3531 FORC3 {
3532 for (val=i=0; i < 3; i++)
3533 for ( j=0; j < 3; j++)
3534 val += ppm[c][i][j] * work[i][j];
3535 ipix[c] = floor ((ipix[c] + floor(val)) *
3536 ( sgrow[col/sgx ][c] * (sgx - col%sgx) +
3537 sgrow[col/sgx+1][c] * (col%sgx) ) / sgx / div[c]);
3538 if (ipix[c] > 32000) ipix[c] = 32000;
3539 pix[c] = ipix[c];
3540 }
3541 pix += 4;
3542 }
3543 }
3544 free (black);
3545 free (sgrow);
3546 free (sgain);
3547
3548 if ((badpix = (unsigned *) foveon_camf_matrix (dim, "BadPixels"))) {
3549 for (i=0; i < dim[0]; i++) {
3550 col = (badpix[i] >> 8 & 0xfff) - keep[0];
3551 row = (badpix[i] >> 20 ) - keep[1];
3552 if ((unsigned)(row-1) > height-3 || (unsigned)(col-1) > width-3)
3553 continue;
3554 memset (fsum, 0, sizeof fsum);
3555 for (sum=j=0; j < 8; j++)
3556 if (badpix[i] & (1 << j)) {
3557 FORC3 fsum[c] += (short)
3558 image[(row+hood[j*2])*width+col+hood[j*2+1]][c];
3559 sum++;
3560 }
3561 if (sum) FORC3 image[row*width+col][c] = fsum[c]/sum;
3562 }
3563 free (badpix);
3564 }
3565
3566 /* Array for 5x5 Gaussian averaging of red values */
3567 smrow[6] = (int (*)[3]) calloc (width*5, sizeof **smrow);
3568 merror (smrow[6], "foveon_interpolate()");
3569 for (i=0; i < 5; i++)
3570 smrow[i] = smrow[6] + i*width;
3571
3572 /* Sharpen the reds against these Gaussian averages */
3573 for (smlast=-1, row=2; row < height-2; row++) {
3574 while (smlast < row+2) {
3575 for (i=0; i < 6; i++)
3576 smrow[(i+5) % 6] = smrow[i];
3577 pix = image[++smlast*width+2];
3578 for (col=2; col < width-2; col++) {
3579 smrow[4][col][0] =
3580 (pix[0]*6 + (pix[-4]+pix[4])*4 + pix[-8]+pix[8] + 8) >> 4;
3581 pix += 4;
3582 }
3583 }
3584 pix = image[row*width+2];
3585 for (col=2; col < width-2; col++) {
3586 smred = ( 6 * smrow[2][col][0]
3587 + 4 * (smrow[1][col][0] + smrow[3][col][0])
3588 + smrow[0][col][0] + smrow[4][col][0] + 8 ) >> 4;
3589 if (col == 2)
3590 smred_p = smred;
3591 i = pix[0] + ((pix[0] - ((smred*7 + smred_p) >> 3)) >> 3);
3592 if (i > 32000) i = 32000;
3593 pix[0] = i;
3594 smred_p = smred;
3595 pix += 4;
3596 }
3597 }
3598
3599 /* Adjust the brighter pixels for better linearity */
3600 min = 0xffff;
3601 FORC3 {
3602 i = satlev[c] / div[c];
3603 if (min > i) min = i;
3604 }
3605 limit = min * 9 >> 4;
3606 for (pix=image[0]; pix < image[height*width]; pix+=4) {
3607 if (pix[0] <= limit || pix[1] <= limit || pix[2] <= limit)
3608 continue;
3609 min = max = pix[0];
3610 for (c=1; c < 3; c++) {
3611 if (min > pix[c]) min = pix[c];
3612 if (max < pix[c]) max = pix[c];
3613 }
3614 if (min >= limit*2) {
3615 pix[0] = pix[1] = pix[2] = max;
3616 } else {
3617 i = 0x4000 - ((min - limit) << 14) / limit;
3618 i = 0x4000 - (i*i >> 14);
3619 i = i*i >> 14;
3620 FORC3 pix[c] += (max - pix[c]) * i >> 14;
3621 }
3622 }
3623 /*
3624 Because photons that miss one detector often hit another,
3625 the sum R+G+B is much less noisy than the individual colors.
3626 So smooth the hues without smoothing the total.
3627 */
3628 for (smlast=-1, row=2; row < height-2; row++) {
3629 while (smlast < row+2) {
3630 for (i=0; i < 6; i++)
3631 smrow[(i+5) % 6] = smrow[i];
3632 pix = image[++smlast*width+2];
3633 for (col=2; col < width-2; col++) {
3634 FORC3 smrow[4][col][c] = (pix[c-4]+2*pix[c]+pix[c+4]+2) >> 2;
3635 pix += 4;
3636 }
3637 }
3638 pix = image[row*width+2];
3639 for (col=2; col < width-2; col++) {
3640 FORC3 dev[c] = -foveon_apply_curve (curve[7], pix[c] -
3641 ((smrow[1][col][c] + 2*smrow[2][col][c] + smrow[3][col][c]) >> 2));
3642 sum = (dev[0] + dev[1] + dev[2]) >> 3;
3643 FORC3 pix[c] += dev[c] - sum;
3644 pix += 4;
3645 }
3646 }
3647 for (smlast=-1, row=2; row < height-2; row++) {
3648 while (smlast < row+2) {
3649 for (i=0; i < 6; i++)
3650 smrow[(i+5) % 6] = smrow[i];
3651 pix = image[++smlast*width+2];
3652 for (col=2; col < width-2; col++) {
3653 FORC3 smrow[4][col][c] =
3654 (pix[c-8]+pix[c-4]+pix[c]+pix[c+4]+pix[c+8]+2) >> 2;
3655 pix += 4;
3656 }
3657 }
3658 pix = image[row*width+2];
3659 for (col=2; col < width-2; col++) {
3660 for (total[3]=375, sum=60, c=0; c < 3; c++) {
3661 for (total[c]=i=0; i < 5; i++)
3662 total[c] += smrow[i][col][c];
3663 total[3] += total[c];
3664 sum += pix[c];
3665 }
3666 if (sum < 0) sum = 0;
3667 j = total[3] > 375 ? (sum << 16) / total[3] : sum * 174;
3668 FORC3 pix[c] += foveon_apply_curve (curve[6],
3669 ((j*total[c] + 0x8000) >> 16) - pix[c]);
3670 pix += 4;
3671 }
3672 }
3673
3674 /* Transform the image to a different colorspace */
3675 for (pix=image[0]; pix < image[height*width]; pix+=4) {
3676 FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]);
3677 sum = (pix[0]+pix[1]+pix[1]+pix[2]) >> 2;
3678 FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]-sum);
3679 FORC3 {
3680 for (dsum=i=0; i < 3; i++)
3681 dsum += trans[c][i] * pix[i];
3682 if (dsum < 0) dsum = 0;
3683 if (dsum > 24000) dsum = 24000;
3684 ipix[c] = dsum + 0.5;
3685 }
3686 FORC3 pix[c] = ipix[c];
3687 }
3688
3689 /* Smooth the image bottom-to-top and save at 1/4 scale */
3690 shrink = (short (*)[3]) calloc ((height/4), (width/4)*sizeof *shrink);
3691 merror (shrink, "foveon_interpolate()");
3692 for (row = height/4; row--; )
3693 for (col=0; col < width/4; col++) {
3694 ipix[0] = ipix[1] = ipix[2] = 0;
3695 for (i=0; i < 4; i++)
3696 for (j=0; j < 4; j++)
3697 FORC3 ipix[c] += image[(row*4+i)*width+col*4+j][c];
3698 FORC3
3699 if (row+2 > height/4)
3700 shrink[row*(width/4)+col][c] = ipix[c] >> 4;
3701 else
3702 shrink[row*(width/4)+col][c] =
3703 (shrink[(row+1)*(width/4)+col][c]*1840 + ipix[c]*141 + 2048) >> 12;
3704 }
3705 /* From the 1/4-scale image, smooth right-to-left */
3706 for (row=0; row < (height & ~3); row++) {
3707 ipix[0] = ipix[1] = ipix[2] = 0;
3708 if ((row & 3) == 0)
3709 for (col = width & ~3 ; col--; )
3710 FORC3 smrow[0][col][c] = ipix[c] =
3711 (shrink[(row/4)*(width/4)+col/4][c]*1485 + ipix[c]*6707 + 4096) >> 13;
3712
3713 /* Then smooth left-to-right */
3714 ipix[0] = ipix[1] = ipix[2] = 0;
3715 for (col=0; col < (width & ~3); col++)
3716 FORC3 smrow[1][col][c] = ipix[c] =
3717 (smrow[0][col][c]*1485 + ipix[c]*6707 + 4096) >> 13;
3718
3719 /* Smooth top-to-bottom */
3720 if (row == 0)
3721 memcpy (smrow[2], smrow[1], sizeof **smrow * width);
3722 else
3723 for (col=0; col < (width & ~3); col++)
3724 FORC3 smrow[2][col][c] =
3725 (smrow[2][col][c]*6707 + smrow[1][col][c]*1485 + 4096) >> 13;
3726
3727 /* Adjust the chroma toward the smooth values */
3728 for (col=0; col < (width & ~3); col++) {
3729 for (i=j=30, c=0; c < 3; c++) {
3730 i += smrow[2][col][c];
3731 j += image[row*width+col][c];
3732 }
3733 j = (j << 16) / i;
3734 for (sum=c=0; c < 3; c++) {
3735 ipix[c] = foveon_apply_curve (curve[c+3],
3736 ((smrow[2][col][c] * j + 0x8000) >> 16) - image[row*width+col][c]);
3737 sum += ipix[c];
3738 }
3739 sum >>= 3;
3740 FORC3 {
3741 i = image[row*width+col][c] + ipix[c] - sum;
3742 if (i < 0) i = 0;
3743 image[row*width+col][c] = i;
3744 }
3745 }
3746 }
3747 free (shrink);
3748 free (smrow[6]);
3749 for (i=0; i < 8; i++)
3750 free (curve[i]);
3751
3752 /* Trim off the black border */
3753 active[1] -= keep[1];
3754 active[3] -= 2;
3755 i = active[2] - active[0];
3756 for (row=0; row < active[3]-active[1]; row++)
3757 memcpy (image[row*i], image[(row+active[1])*width+active[0]],
3758 i * sizeof *image);
3759 width = i;
3760 height = row;
3761 }
3762 #undef image
3763
3764 /* RESTRICTED code ends here */
3765
crop_masked_pixels()3766 void CLASS crop_masked_pixels()
3767 {
3768 int row, col;
3769 unsigned r, c, m, mblack[8], zero, val;
3770
3771 if (load_raw == &CLASS phase_one_load_raw ||
3772 load_raw == &CLASS phase_one_load_raw_c)
3773 phase_one_correct();
3774 if (fuji_width) {
3775 for (row=0; row < raw_height-top_margin*2; row++) {
3776 for (col=0; col < fuji_width << !fuji_layout; col++) {
3777 if (fuji_layout) {
3778 r = fuji_width - 1 - col + (row >> 1);
3779 c = col + ((row+1) >> 1);
3780 } else {
3781 r = fuji_width - 1 + row - (col >> 1);
3782 c = row + ((col+1) >> 1);
3783 }
3784 if (r < height && c < width)
3785 BAYER(r,c) = RAW(row+top_margin,col+left_margin);
3786 }
3787 }
3788 } else {
3789 for (row=0; row < height; row++)
3790 for (col=0; col < width; col++)
3791 BAYER2(row,col) = RAW(row+top_margin,col+left_margin);
3792 }
3793 if (mask[0][3] > 0) goto mask_set;
3794 if (load_raw == &CLASS canon_load_raw ||
3795 load_raw == &CLASS lossless_jpeg_load_raw) {
3796 mask[0][1] = mask[1][1] += 2;
3797 mask[0][3] -= 2;
3798 goto sides;
3799 }
3800 if (load_raw == &CLASS canon_600_load_raw ||
3801 load_raw == &CLASS sony_load_raw ||
3802 (load_raw == &CLASS eight_bit_load_raw && strncmp(model,"DC2",3)) ||
3803 load_raw == &CLASS kodak_262_load_raw ||
3804 (load_raw == &CLASS packed_load_raw && (load_flags & 256))) {
3805 sides:
3806 mask[0][0] = mask[1][0] = top_margin;
3807 mask[0][2] = mask[1][2] = top_margin+height;
3808 mask[0][3] += left_margin;
3809 mask[1][1] += left_margin+width;
3810 mask[1][3] += raw_width;
3811 }
3812 if (load_raw == &CLASS nokia_load_raw) {
3813 mask[0][2] = top_margin;
3814 mask[0][3] = width;
3815 }
3816 mask_set:
3817 memset (mblack, 0, sizeof mblack);
3818 for (zero=m=0; m < 8; m++)
3819 for (row=MAX(mask[m][0],0); row < MIN(mask[m][2],raw_height); row++)
3820 for (col=MAX(mask[m][1],0); col < MIN(mask[m][3],raw_width); col++) {
3821 c = FC(row-top_margin,col-left_margin);
3822 mblack[c] += val = RAW(row,col);
3823 mblack[4+c]++;
3824 zero += !val;
3825 }
3826 if (load_raw == &CLASS canon_600_load_raw && width < raw_width) {
3827 black = (mblack[0]+mblack[1]+mblack[2]+mblack[3]) /
3828 (mblack[4]+mblack[5]+mblack[6]+mblack[7]) - 4;
3829 canon_600_correct();
3830 } else if (zero < mblack[4] && mblack[5] && mblack[6] && mblack[7]) {
3831 FORC4 cblack[c] = mblack[c] / mblack[4+c];
3832 cblack[4] = cblack[5] = cblack[6] = 0;
3833 }
3834 }
3835
remove_zeroes()3836 void CLASS remove_zeroes()
3837 {
3838 unsigned row, col, tot, n, r, c;
3839
3840 for (row=0; row < height; row++)
3841 for (col=0; col < width; col++)
3842 if (BAYER(row,col) == 0) {
3843 tot = n = 0;
3844 for (r = row-2; r <= row+2; r++)
3845 for (c = col-2; c <= col+2; c++)
3846 if (r < height && c < width &&
3847 FC(r,c) == FC(row,col) && BAYER(r,c))
3848 tot += (n++,BAYER(r,c));
3849 if (n) BAYER(row,col) = tot/n;
3850 }
3851 }
3852
3853 /*
3854 Seach from the current directory up to the root looking for
3855 a ".badpixels" file, and fix those pixels now.
3856 */
bad_pixels(const char * cfname)3857 void CLASS bad_pixels (const char *cfname)
3858 {
3859 FILE *fp=0;
3860 char *fname, *cp, line[128];
3861 int len, time, row, col, r, c, rad, tot, n, fixed=0;
3862
3863 if (!filters) return;
3864 if (cfname)
3865 fp = fopen (cfname, "r");
3866 else {
3867 for (len=32 ; ; len *= 2) {
3868 fname = (char *) malloc (len);
3869 if (!fname) return;
3870 if (getcwd (fname, len-16)) break;
3871 free (fname);
3872 if (errno != ERANGE) return;
3873 }
3874 #if defined(WIN32) || defined(DJGPP)
3875 if (fname[1] == ':')
3876 memmove (fname, fname+2, len-2);
3877 for (cp=fname; *cp; cp++)
3878 if (*cp == '\\') *cp = '/';
3879 #endif
3880 cp = fname + strlen(fname);
3881 if (cp[-1] == '/') cp--;
3882 while (*fname == '/') {
3883 strcpy (cp, "/.badpixels");
3884 if ((fp = fopen (fname, "r"))) break;
3885 if (cp == fname) break;
3886 while (*--cp != '/');
3887 }
3888 free (fname);
3889 }
3890 if (!fp) return;
3891 while (fgets (line, 128, fp)) {
3892 cp = strchr (line, '#');
3893 if (cp) *cp = 0;
3894 if (sscanf (line, "%d %d %d", &col, &row, &time) != 3) continue;
3895 if ((unsigned) col >= width || (unsigned) row >= height) continue;
3896 if (time > timestamp) continue;
3897 for (tot=n=0, rad=1; rad < 3 && n==0; rad++)
3898 for (r = row-rad; r <= row+rad; r++)
3899 for (c = col-rad; c <= col+rad; c++)
3900 if ((unsigned) r < height && (unsigned) c < width &&
3901 (r != row || c != col) && fcol(r,c) == fcol(row,col)) {
3902 tot += BAYER2(r,c);
3903 n++;
3904 }
3905 BAYER2(row,col) = tot/n;
3906 if (verbose) {
3907 if (!fixed++)
3908 fprintf (stderr,_("Fixed dead pixels at:"));
3909 fprintf (stderr, " %d,%d", col, row);
3910 }
3911 }
3912 if (fixed) fputc ('\n', stderr);
3913 fclose (fp);
3914 }
3915
subtract(const char * fname)3916 void CLASS subtract (const char *fname)
3917 {
3918 FILE *fp;
3919 int dim[3]={0,0,0}, comment=0, number=0, error=0, nd=0, c, row, col;
3920 ushort *pixel;
3921
3922 if (!(fp = fopen (fname, "rb"))) {
3923 perror (fname); return;
3924 }
3925 if (fgetc(fp) != 'P' || fgetc(fp) != '5') error = 1;
3926 while (!error && nd < 3 && (c = fgetc(fp)) != EOF) {
3927 if (c == '#') comment = 1;
3928 if (c == '\n') comment = 0;
3929 if (comment) continue;
3930 if (isdigit(c)) number = 1;
3931 if (number) {
3932 if (isdigit(c)) dim[nd] = dim[nd]*10 + c -'0';
3933 else if (isspace(c)) {
3934 number = 0; nd++;
3935 } else error = 1;
3936 }
3937 }
3938 if (error || nd < 3) {
3939 fprintf (stderr,_("%s is not a valid PGM file!\n"), fname);
3940 fclose (fp); return;
3941 } else if (dim[0] != width || dim[1] != height || dim[2] != 65535) {
3942 fprintf (stderr,_("%s has the wrong dimensions!\n"), fname);
3943 fclose (fp); return;
3944 }
3945 pixel = (ushort *) calloc (width, sizeof *pixel);
3946 merror (pixel, "subtract()");
3947 for (row=0; row < height; row++) {
3948 fread (pixel, 2, width, fp);
3949 for (col=0; col < width; col++)
3950 BAYER(row,col) = MAX (BAYER(row,col) - ntohs(pixel[col]), 0);
3951 }
3952 free (pixel);
3953 fclose (fp);
3954 memset (cblack, 0, sizeof cblack);
3955 black = 0;
3956 }
3957
gamma_curve(double pwr,double ts,int mode,int imax)3958 void CLASS gamma_curve (double pwr, double ts, int mode, int imax)
3959 {
3960 int i;
3961 double g[6], bnd[2]={0,0}, r;
3962
3963 g[0] = pwr;
3964 g[1] = ts;
3965 g[2] = g[3] = g[4] = 0;
3966 bnd[g[1] >= 1] = 1;
3967 if (g[1] && (g[1]-1)*(g[0]-1) <= 0) {
3968 for (i=0; i < 48; i++) {
3969 g[2] = (bnd[0] + bnd[1])/2;
3970 if (g[0]) bnd[(pow(g[2]/g[1],-g[0]) - 1)/g[0] - 1/g[2] > -1] = g[2];
3971 else bnd[g[2]/exp(1-1/g[2]) < g[1]] = g[2];
3972 }
3973 g[3] = g[2] / g[1];
3974 if (g[0]) g[4] = g[2] * (1/g[0] - 1);
3975 }
3976 if (g[0]) g[5] = 1 / (g[1]*SQR(g[3])/2 - g[4]*(1 - g[3]) +
3977 (1 - pow(g[3],1+g[0]))*(1 + g[4])/(1 + g[0])) - 1;
3978 else g[5] = 1 / (g[1]*SQR(g[3])/2 + 1
3979 - g[2] - g[3] - g[2]*g[3]*(log(g[3]) - 1)) - 1;
3980 if (!mode--) {
3981 memcpy (gamm, g, sizeof gamm);
3982 return;
3983 }
3984 for (i=0; i < 0x10000; i++) {
3985 curve[i] = 0xffff;
3986 if ((r = (double) i / imax) < 1)
3987 curve[i] = 0x10000 * ( mode
3988 ? (r < g[3] ? r*g[1] : (g[0] ? pow( r,g[0])*(1+g[4])-g[4] : log(r)*g[2]+1))
3989 : (r < g[2] ? r/g[1] : (g[0] ? pow((r+g[4])/(1+g[4]),1/g[0]) : exp((r-1)/g[2]))));
3990 }
3991 }
3992
pseudoinverse(double (* in)[3],double (* out)[3],int size)3993 void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
3994 {
3995 double work[3][6], num;
3996 int i, j, k;
3997
3998 for (i=0; i < 3; i++) {
3999 for (j=0; j < 6; j++)
4000 work[i][j] = j == i+3;
4001 for (j=0; j < 3; j++)
4002 for (k=0; k < size; k++)
4003 work[i][j] += in[k][i] * in[k][j];
4004 }
4005 for (i=0; i < 3; i++) {
4006 num = work[i][i];
4007 for (j=0; j < 6; j++)
4008 work[i][j] /= num;
4009 for (k=0; k < 3; k++) {
4010 if (k==i) continue;
4011 num = work[k][i];
4012 for (j=0; j < 6; j++)
4013 work[k][j] -= work[i][j] * num;
4014 }
4015 }
4016 for (i=0; i < size; i++)
4017 for (j=0; j < 3; j++)
4018 for (out[i][j]=k=0; k < 3; k++)
4019 out[i][j] += work[j][k+3] * in[i][k];
4020 }
4021
cam_xyz_coeff(float rgb_cam[3][4],double cam_xyz[4][3])4022 void CLASS cam_xyz_coeff (float rgb_cam[3][4], double cam_xyz[4][3])
4023 {
4024 double cam_rgb[4][3], inverse[4][3], num;
4025 int i, j, k;
4026
4027 for (i=0; i < colors; i++) /* Multiply out XYZ colorspace */
4028 for (j=0; j < 3; j++)
4029 for (cam_rgb[i][j] = k=0; k < 3; k++)
4030 cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
4031
4032 for (i=0; i < colors; i++) { /* Normalize cam_rgb so that */
4033 for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
4034 num += cam_rgb[i][j];
4035 for (j=0; j < 3; j++)
4036 cam_rgb[i][j] /= num;
4037 pre_mul[i] = 1 / num;
4038 }
4039 pseudoinverse (cam_rgb, inverse, colors);
4040 for (i=0; i < 3; i++)
4041 for (j=0; j < colors; j++)
4042 rgb_cam[i][j] = inverse[j][i];
4043 }
4044
4045 #ifdef COLORCHECK
colorcheck()4046 void CLASS colorcheck()
4047 {
4048 #define NSQ 24
4049 // Coordinates of the GretagMacbeth ColorChecker squares
4050 // width, height, 1st_column, 1st_row
4051 int cut[NSQ][4]; // you must set these
4052 // ColorChecker Chart under 6500-kelvin illumination
4053 static const double gmb_xyY[NSQ][3] = {
4054 { 0.400, 0.350, 10.1 }, // Dark Skin
4055 { 0.377, 0.345, 35.8 }, // Light Skin
4056 { 0.247, 0.251, 19.3 }, // Blue Sky
4057 { 0.337, 0.422, 13.3 }, // Foliage
4058 { 0.265, 0.240, 24.3 }, // Blue Flower
4059 { 0.261, 0.343, 43.1 }, // Bluish Green
4060 { 0.506, 0.407, 30.1 }, // Orange
4061 { 0.211, 0.175, 12.0 }, // Purplish Blue
4062 { 0.453, 0.306, 19.8 }, // Moderate Red
4063 { 0.285, 0.202, 6.6 }, // Purple
4064 { 0.380, 0.489, 44.3 }, // Yellow Green
4065 { 0.473, 0.438, 43.1 }, // Orange Yellow
4066 { 0.187, 0.129, 6.1 }, // Blue
4067 { 0.305, 0.478, 23.4 }, // Green
4068 { 0.539, 0.313, 12.0 }, // Red
4069 { 0.448, 0.470, 59.1 }, // Yellow
4070 { 0.364, 0.233, 19.8 }, // Magenta
4071 { 0.196, 0.252, 19.8 }, // Cyan
4072 { 0.310, 0.316, 90.0 }, // White
4073 { 0.310, 0.316, 59.1 }, // Neutral 8
4074 { 0.310, 0.316, 36.2 }, // Neutral 6.5
4075 { 0.310, 0.316, 19.8 }, // Neutral 5
4076 { 0.310, 0.316, 9.0 }, // Neutral 3.5
4077 { 0.310, 0.316, 3.1 } }; // Black
4078 double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
4079 double inverse[NSQ][3], cam_xyz[4][3], balance[4], num;
4080 int c, i, j, k, sq, row, col, pass, count[4];
4081
4082 memset (gmb_cam, 0, sizeof gmb_cam);
4083 for (sq=0; sq < NSQ; sq++) {
4084 FORCC count[c] = 0;
4085 for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
4086 for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
4087 c = FC(row,col);
4088 if (c >= colors) c -= 2;
4089 gmb_cam[sq][c] += BAYER2(row,col);
4090 BAYER2(row,col) = black + (BAYER2(row,col)-black)/2;
4091 count[c]++;
4092 }
4093 FORCC gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - black;
4094 gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
4095 gmb_xyz[sq][1] = gmb_xyY[sq][2];
4096 gmb_xyz[sq][2] = gmb_xyY[sq][2] *
4097 (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
4098 }
4099 pseudoinverse (gmb_xyz, inverse, NSQ);
4100 for (pass=0; pass < 2; pass++) {
4101 for (raw_color = i=0; i < colors; i++)
4102 for (j=0; j < 3; j++)
4103 for (cam_xyz[i][j] = k=0; k < NSQ; k++)
4104 cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
4105 cam_xyz_coeff (rgb_cam, cam_xyz);
4106 FORCC balance[c] = pre_mul[c] * gmb_cam[20][c];
4107 for (sq=0; sq < NSQ; sq++)
4108 FORCC gmb_cam[sq][c] *= balance[c];
4109 }
4110 if (verbose) {
4111 printf (" { \"%s %s\", %d,\n\t{", make, model, black);
4112 num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
4113 FORCC for (j=0; j < 3; j++)
4114 printf ("%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
4115 puts (" } },");
4116 }
4117 #undef NSQ
4118 }
4119 #endif
4120
hat_transform(float * temp,float * base,int st,int size,int sc)4121 void CLASS hat_transform (float *temp, float *base, int st, int size, int sc)
4122 {
4123 int i;
4124 for (i=0; i < sc; i++)
4125 temp[i] = 2*base[st*i] + base[st*(sc-i)] + base[st*(i+sc)];
4126 for (; i+sc < size; i++)
4127 temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(i+sc)];
4128 for (; i < size; i++)
4129 temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(2*size-2-(i+sc))];
4130 }
4131
wavelet_denoise()4132 void CLASS wavelet_denoise()
4133 {
4134 float *fimg=0, *temp, thold, mul[2], avg, diff;
4135 int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
4136 ushort *window[4];
4137 static const float noise[] =
4138 { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
4139
4140 if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
4141
4142 while (maximum << scale < 0x10000) scale++;
4143 maximum <<= --scale;
4144 black <<= scale;
4145 FORC4 cblack[c] <<= scale;
4146 if ((size = iheight*iwidth) < 0x15550000)
4147 fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
4148 merror (fimg, "wavelet_denoise()");
4149 temp = fimg + size*3;
4150 if ((nc = colors) == 3 && filters) nc++;
4151 #if defined(__sun) && !defined(__GNUC__) /* Fix UFRaw bug #3205673 - NKBJ */
4152 #pragma omp parallel for default(shared) \
4153 private(c,i,hpass,lev,lpass,row,col,thold,fimg,temp)
4154 #else
4155 #pragma omp parallel for default(shared) \
4156 private(c,i,hpass,lev,lpass,row,col,thold,fimg,temp)
4157 #endif
4158 FORC(nc) { /* denoise R,G1,B,G3 individually */
4159 for (i=0; i < size; i++)
4160 fimg[i] = 256 * sqrt(image[i][c] << scale);
4161 for (hpass=lev=0; lev < 5; lev++) {
4162 lpass = size*((lev & 1)+1);
4163 for (row=0; row < iheight; row++) {
4164 hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
4165 for (col=0; col < iwidth; col++)
4166 fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
4167 }
4168 for (col=0; col < iwidth; col++) {
4169 hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
4170 for (row=0; row < iheight; row++)
4171 fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
4172 }
4173 thold = threshold * noise[lev];
4174 for (i=0; i < size; i++) {
4175 fimg[hpass+i] -= fimg[lpass+i];
4176 if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
4177 else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
4178 else fimg[hpass+i] = 0;
4179 if (hpass) fimg[i] += fimg[hpass+i];
4180 }
4181 hpass = lpass;
4182 }
4183 for (i=0; i < size; i++)
4184 image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
4185 }
4186 if (filters && colors == 3) { /* pull G1 and G3 closer together */
4187 for (row=0; row < 2; row++) {
4188 mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
4189 blk[row] = cblack[FC(row,0) | 1];
4190 }
4191 for (i=0; i < 4; i++)
4192 window[i] = (ushort *) fimg + width*i;
4193 for (wlast=-1, row=1; row < height-1; row++) {
4194 while (wlast < row+1) {
4195 for (wlast++, i=0; i < 4; i++)
4196 window[(i+3) & 3] = window[i];
4197 for (col = FC(wlast,1) & 1; col < width; col+=2)
4198 window[2][col] = BAYER(wlast,col);
4199 }
4200 thold = threshold/512;
4201 for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
4202 avg = ( window[0][col-1] + window[0][col+1] +
4203 window[2][col-1] + window[2][col+1] - blk[~row & 1]*4 )
4204 * mul[row & 1] + (window[1][col] + blk[row & 1]) * 0.5;
4205 avg = avg < 0 ? 0 : sqrt(avg);
4206 diff = sqrt(BAYER(row,col)) - avg;
4207 if (diff < -thold) diff += thold;
4208 else if (diff > thold) diff -= thold;
4209 else diff = 0;
4210 BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
4211 }
4212 }
4213 }
4214 free (fimg);
4215 }
4216
scale_colors()4217 void CLASS scale_colors()
4218 {
4219 unsigned bottom, right, size, row, col, ur, uc, i, x, y, c, sum[8];
4220 int val, dark, sat;
4221 double dsum[8], dmin, dmax;
4222 float scale_mul[4], fr, fc;
4223 ushort *img=0, *pix;
4224
4225 if (user_mul[0])
4226 memcpy (pre_mul, user_mul, sizeof pre_mul);
4227 if (use_auto_wb || (use_camera_wb && cam_mul[0] == -1)) {
4228 memset (dsum, 0, sizeof dsum);
4229 bottom = MIN (greybox[1]+greybox[3], height);
4230 right = MIN (greybox[0]+greybox[2], width);
4231 for (row=greybox[1]; row < bottom; row += 8)
4232 for (col=greybox[0]; col < right; col += 8) {
4233 memset (sum, 0, sizeof sum);
4234 for (y=row; y < row+8 && y < bottom; y++)
4235 for (x=col; x < col+8 && x < right; x++)
4236 FORC4 {
4237 if (filters) {
4238 c = fcol(y,x);
4239 val = BAYER2(y,x);
4240 } else
4241 val = image[y*width+x][c];
4242 if (val > maximum-25) goto skip_block;
4243 if ((val -= cblack[c]) < 0) val = 0;
4244 sum[c] += val;
4245 sum[c+4]++;
4246 if (filters) break;
4247 }
4248 FORC(8) dsum[c] += sum[c];
4249 skip_block: ;
4250 }
4251 FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
4252 }
4253 if (use_camera_wb && cam_mul[0] != -1) {
4254 memset (sum, 0, sizeof sum);
4255 for (row=0; row < 8; row++)
4256 for (col=0; col < 8; col++) {
4257 c = FC(row,col);
4258 if ((val = white[row][col] - cblack[c]) > 0)
4259 sum[c] += val;
4260 sum[c+4]++;
4261 }
4262 if (sum[0] && sum[1] && sum[2] && sum[3])
4263 FORC4 pre_mul[c] = (float) sum[c+4] / sum[c];
4264 else if (cam_mul[0] && cam_mul[2])
4265 memcpy (pre_mul, cam_mul, sizeof pre_mul);
4266 else
4267 fprintf (stderr,_("%s: Cannot use camera white balance.\n"), ifname);
4268 }
4269 if (pre_mul[1] == 0) pre_mul[1] = 1;
4270 if (pre_mul[3] == 0) pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
4271 dark = black;
4272 sat = maximum;
4273 if (threshold) wavelet_denoise();
4274 maximum -= black;
4275 for (dmin=DBL_MAX, dmax=c=0; c < 4; c++) {
4276 if (dmin > pre_mul[c])
4277 dmin = pre_mul[c];
4278 if (dmax < pre_mul[c])
4279 dmax = pre_mul[c];
4280 }
4281 if (!highlight) dmax = dmin;
4282 FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
4283 if (verbose) {
4284 fprintf (stderr,
4285 _("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark, sat);
4286 FORC4 fprintf (stderr, " %f", pre_mul[c]);
4287 fputc ('\n', stderr);
4288 }
4289 if (filters > 1000 && (cblack[4]+1)/2 == 1 && (cblack[5]+1)/2 == 1) {
4290 FORC4 cblack[FC(c/2,c%2)] +=
4291 cblack[6 + c/2 % cblack[4] * cblack[5] + c%2 % cblack[5]];
4292 cblack[4] = cblack[5] = 0;
4293 }
4294 size = iheight*iwidth;
4295 for (i=0; i < size*4; i++) {
4296 if (!(val = ((ushort *)image)[i])) continue;
4297 if (cblack[4] && cblack[5])
4298 val -= cblack[6 + i/4 / iwidth % cblack[4] * cblack[5] +
4299 i/4 % iwidth % cblack[5]];
4300 val -= cblack[i & 3];
4301 val *= scale_mul[i & 3];
4302 ((ushort *)image)[i] = CLIP(val);
4303 }
4304 if ((aber[0] != 1 || aber[2] != 1) && colors == 3) {
4305 if (verbose)
4306 fprintf (stderr,_("Correcting chromatic aberration...\n"));
4307 for (c=0; c < 4; c+=2) {
4308 if (aber[c] == 1) continue;
4309 img = (ushort *) malloc (size * sizeof *img);
4310 merror (img, "scale_colors()");
4311 for (i=0; i < size; i++)
4312 img[i] = image[i][c];
4313 for (row=0; row < iheight; row++) {
4314 ur = fr = (row - iheight*0.5) * aber[c] + iheight*0.5;
4315 if (ur > iheight-2) continue;
4316 fr -= ur;
4317 for (col=0; col < iwidth; col++) {
4318 uc = fc = (col - iwidth*0.5) * aber[c] + iwidth*0.5;
4319 if (uc > iwidth-2) continue;
4320 fc -= uc;
4321 pix = img + ur*iwidth + uc;
4322 image[row*iwidth+col][c] =
4323 (pix[ 0]*(1-fc) + pix[ 1]*fc) * (1-fr) +
4324 (pix[iwidth]*(1-fc) + pix[iwidth+1]*fc) * fr;
4325 }
4326 }
4327 free(img);
4328 }
4329 }
4330 }
4331
pre_interpolate()4332 void CLASS pre_interpolate()
4333 {
4334 ushort (*img)[4];
4335 int row, col, c;
4336
4337 if (shrink) {
4338 if (half_size) {
4339 height = iheight;
4340 width = iwidth;
4341 if (filters == 9) {
4342 for (row=0; row < 3; row++)
4343 for (col=1; col < 4; col++)
4344 if (!(image[row*width+col][0] | image[row*width+col][2]))
4345 goto break2; break2:
4346 for ( ; row < height; row+=3)
4347 for (col=(col-1)%3+1; col < width-1; col+=3) {
4348 img = image + row*width+col;
4349 for (c=0; c < 3; c+=2)
4350 img[0][c] = (img[-1][c] + img[1][c]) >> 1;
4351 }
4352 }
4353 } else {
4354 img = (ushort (*)[4]) calloc (height, width*sizeof *img);
4355 merror (img, "pre_interpolate()");
4356 for (row=0; row < height; row++)
4357 for (col=0; col < width; col++) {
4358 c = fcol(row,col);
4359 img[row*width+col][c] = image[(row >> 1)*iwidth+(col >> 1)][c];
4360 }
4361 free (image);
4362 image = img;
4363 shrink = 0;
4364 }
4365 }
4366 if (filters > 1000 && colors == 3) {
4367 mix_green = four_color_rgb ^ half_size;
4368 if (four_color_rgb | half_size) colors++;
4369 else {
4370 for (row = FC(1,0) >> 1; row < height; row+=2)
4371 for (col = FC(row,1) & 1; col < width; col+=2)
4372 image[row*width+col][1] = image[row*width+col][3];
4373 filters &= ~((filters & 0x55555555) << 1);
4374 }
4375 }
4376 if (half_size) filters = 0;
4377 }
4378
border_interpolate(int border)4379 void CLASS border_interpolate (int border)
4380 {
4381 int row;
4382 unsigned col, y, x, f, c, sum[8];
4383
4384 #pragma omp for
4385 for (row=0; row < height; row++)
4386 for (col=0; col < width; col++) {
4387 if (col==border && row >= border && row < height-border)
4388 col = width-border;
4389 memset (sum, 0, sizeof sum);
4390 for (y=row-1; y != row+2; y++)
4391 for (x=col-1; x != col+2; x++)
4392 if (y < height && x < width) {
4393 f = fcol(y,x);
4394 sum[f] += image[y*width+x][f];
4395 sum[f+4]++;
4396 }
4397 f = fcol(row,col);
4398 FORCC if (c != f && sum[c+4])
4399 image[row*width+col][c] = sum[c] / sum[c+4];
4400 }
4401 }
4402
lin_interpolate()4403 void CLASS lin_interpolate()
4404 {
4405 int code[16][16][32], size=16, *ip, sum[4];
4406 int f, c, i, x, y, row, col, shift, color;
4407 ushort *pix;
4408
4409 if (verbose) fprintf (stderr,_("Bilinear interpolation...\n"));
4410 if (filters == 9) size = 6;
4411 #pragma omp parallel default(shared) \
4412 private(ip, sum, f, c, i, x, y, row, col, shift, color, pix)
4413 {
4414 border_interpolate(1);
4415 #pragma omp for
4416 for (row=0; row < size; row++)
4417 for (col=0; col < size; col++) {
4418 ip = code[row][col]+1;
4419 f = fcol(row,col);
4420 memset (sum, 0, sizeof sum);
4421 for (y=-1; y <= 1; y++)
4422 for (x=-1; x <= 1; x++) {
4423 shift = (y==0) + (x==0);
4424 color = fcol(row+y,col+x);
4425 if (color == f) continue;
4426 *ip++ = (width*y + x)*4 + color;
4427 *ip++ = shift;
4428 *ip++ = color;
4429 sum[color] += 1 << shift;
4430 }
4431 code[row][col][0] = (ip - code[row][col]) / 3;
4432 FORCC
4433 if (c != f) {
4434 *ip++ = c;
4435 *ip++ = 256 / sum[c];
4436 }
4437 }
4438 #pragma omp for
4439 for (row=1; row < height-1; row++)
4440 for (col=1; col < width-1; col++) {
4441 pix = image[row*width+col];
4442 ip = code[row % size][col % size];
4443 memset (sum, 0, sizeof sum);
4444 for (i=*ip++; i--; ip+=3)
4445 sum[ip[2]] += pix[ip[0]] << ip[1];
4446 for (i=colors; --i; ip+=2)
4447 pix[ip[0]] = sum[ip[0]] * ip[1] >> 8;
4448 }
4449 } /* pragma omp parallel */
4450 }
4451
4452 /*
4453 This algorithm is officially called:
4454
4455 "Interpolation using a Threshold-based variable number of gradients"
4456
4457 described in http://scien.stanford.edu/pages/labsite/1999/psych221/projects/99/tingchen/algodep/vargra.html
4458
4459 I've extended the basic idea to work with non-Bayer filter arrays.
4460 Gradients are numbered clockwise from NW=0 to W=7.
4461 */
vng_interpolate()4462 void CLASS vng_interpolate()
4463 {
4464 static const signed char *cp, terms[] = {
4465 -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
4466 -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
4467 -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
4468 -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
4469 -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
4470 -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
4471 -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
4472 -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
4473 -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
4474 -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
4475 -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
4476 -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
4477 -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
4478 +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
4479 +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
4480 +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
4481 +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
4482 +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
4483 +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
4484 +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
4485 +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
4486 +1,+0,+2,+1,0,0x10
4487 }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
4488 ushort (*brow[4])[4], *pix;
4489 int prow=8, pcol=2, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
4490 int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
4491 int g, diff, thold, num, c;
4492
4493 lin_interpolate();
4494 if (verbose) fprintf (stderr,_("VNG interpolation...\n"));
4495
4496 if (filters == 1) prow = pcol = 16;
4497 if (filters == 9) prow = pcol = 6;
4498 int *ipalloc = ip = (int *) calloc (prow*pcol, 1280);
4499 merror (ip, "vng_interpolate()");
4500 for (row=0; row < prow; row++) /* Precalculate for VNG */
4501 for (col=0; col < pcol; col++) {
4502 code[row][col] = ip;
4503 for (cp=terms, t=0; t < 64; t++) {
4504 y1 = *cp++; x1 = *cp++;
4505 y2 = *cp++; x2 = *cp++;
4506 weight = *cp++;
4507 grads = *cp++;
4508 color = fcol(row+y1,col+x1);
4509 if (fcol(row+y2,col+x2) != color) continue;
4510 diag = (fcol(row,col+1) == color && fcol(row+1,col) == color) ? 2:1;
4511 if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
4512 *ip++ = (y1*width + x1)*4 + color;
4513 *ip++ = (y2*width + x2)*4 + color;
4514 *ip++ = weight;
4515 for (g=0; g < 8; g++)
4516 if (grads & 1<<g) *ip++ = g;
4517 *ip++ = -1;
4518 }
4519 *ip++ = INT_MAX;
4520 for (cp=chood, g=0; g < 8; g++) {
4521 y = *cp++; x = *cp++;
4522 *ip++ = (y*width + x) * 4;
4523 color = fcol(row,col);
4524 if (fcol(row+y,col+x) != color && fcol(row+y*2,col+x*2) == color)
4525 *ip++ = (y*width + x) * 8 + color;
4526 else
4527 *ip++ = 0;
4528 }
4529 }
4530 #pragma omp parallel default(shared) \
4531 private(row,col,g,brow,pix,ip,gval,diff,gmin,gmax,thold,sum,color,num,c,t)
4532 {
4533 ushort (*rowtmp)[4];
4534 rowtmp = (ushort(*)[4])malloc(4 * width * 4 * sizeof(ushort));
4535 int slice = (height - 4) / uf_omp_get_num_threads();
4536 int start_row = 2 + slice * uf_omp_get_thread_num();
4537 int end_row = MIN(start_row + slice, height - 2);
4538 for (row = start_row; row < end_row; row++) { /* Do VNG interpolation */
4539
4540 for (g = 0; g < 4; g++)
4541 brow[g] = &rowtmp[(row + g - 2) % 4];
4542 for (col=2; col < width-2; col++) {
4543 pix = image[row*width+col];
4544 ip = code[row % prow][col % pcol];
4545 memset (gval, 0, sizeof gval);
4546 while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
4547 diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
4548 gval[ip[3]] += diff;
4549 ip += 5;
4550 if ((g = ip[-1]) == -1) continue;
4551 gval[g] += diff;
4552 while ((g = *ip++) != -1)
4553 gval[g] += diff;
4554 }
4555 ip++;
4556 gmin = gmax = gval[0]; /* Choose a threshold */
4557 for (g=1; g < 8; g++) {
4558 if (gmin > gval[g]) gmin = gval[g];
4559 if (gmax < gval[g]) gmax = gval[g];
4560 }
4561 if (gmax == 0) {
4562 memcpy (brow[2][col], pix, sizeof *image);
4563 continue;
4564 }
4565 thold = gmin + (gmax >> 1);
4566 memset (sum, 0, sizeof sum);
4567 color = fcol(row,col);
4568 for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
4569 if (gval[g] <= thold) {
4570 FORCC
4571 if (c == color && ip[1])
4572 sum[c] += (pix[c] + pix[ip[1]]) >> 1;
4573 else
4574 sum[c] += pix[ip[0] + c];
4575 num++;
4576 }
4577 }
4578 FORCC { /* Save to buffer */
4579 t = pix[color];
4580 if (c != color)
4581 t += (sum[c] - sum[color]) / num;
4582 brow[2][col][c] = CLIP(t);
4583 }
4584 }
4585 if (row > start_row + 1) /* Write buffer to image */
4586 memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
4587 }
4588 if (row == height - 2) {
4589 memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
4590 memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image);
4591 }
4592 } /* pragma omp parallel */
4593 free(ipalloc);
4594 }
4595
4596 /*
4597 Patterned Pixel Grouping Interpolation by Alain Desbiolles
4598 */
ppg_interpolate()4599 void CLASS ppg_interpolate()
4600 {
4601 int dir[5] = { 1, width, -1, -width, 1 };
4602 int row, col, diff[2], guess[2], c, d, i;
4603 ushort (*pix)[4];
4604
4605 if (verbose) fprintf (stderr,_("PPG interpolation...\n"));
4606
4607 #pragma omp parallel default(shared) private(row,col,i,d,c,pix,guess)
4608 {
4609 border_interpolate(3);
4610 /* Fill in the green layer with gradients and pattern recognition: */
4611 #pragma omp for
4612 for (row=3; row < height-3; row++)
4613 for (col=3+(FC(row,3) & 1), c=FC(row,col); col < width-3; col+=2) {
4614 pix = image + row*width+col;
4615 for (i=0; (d=dir[i]) > 0; i++) {
4616 guess[i] = (pix[-d][1] + pix[0][c] + pix[d][1]) * 2
4617 - pix[-2*d][c] - pix[2*d][c];
4618 diff[i] = ( ABS(pix[-2*d][c] - pix[ 0][c]) +
4619 ABS(pix[ 2*d][c] - pix[ 0][c]) +
4620 ABS(pix[ -d][1] - pix[ d][1]) ) * 3 +
4621 ( ABS(pix[ 3*d][1] - pix[ d][1]) +
4622 ABS(pix[-3*d][1] - pix[-d][1]) ) * 2;
4623 }
4624 d = dir[i = diff[0] > diff[1]];
4625 pix[0][1] = ULIM(guess[i] >> 2, pix[d][1], pix[-d][1]);
4626 }
4627 /* Calculate red and blue for each green pixel: */
4628 #pragma omp for
4629 for (row=1; row < height-1; row++)
4630 for (col=1+(FC(row,2) & 1), c=FC(row,col+1); col < width-1; col+=2) {
4631 pix = image + row*width+col;
4632 for (i=0; (d=dir[i]) > 0; c=2-c, i++)
4633 pix[0][c] = CLIP((pix[-d][c] + pix[d][c] + 2*pix[0][1]
4634 - pix[-d][1] - pix[d][1]) >> 1);
4635 }
4636 /* Calculate blue for red pixels and vice versa: */
4637 #pragma omp for
4638 for (row=1; row < height-1; row++)
4639 for (col=1+(FC(row,1) & 1), c=2-FC(row,col); col < width-1; col+=2) {
4640 pix = image + row*width+col;
4641 for (i=0; (d=dir[i]+dir[i+1]) > 0; i++) {
4642 diff[i] = ABS(pix[-d][c] - pix[d][c]) +
4643 ABS(pix[-d][1] - pix[0][1]) +
4644 ABS(pix[ d][1] - pix[0][1]);
4645 guess[i] = pix[-d][c] + pix[d][c] + 2*pix[0][1]
4646 - pix[-d][1] - pix[d][1];
4647 }
4648 if (diff[0] != diff[1])
4649 pix[0][c] = CLIP(guess[diff[0] > diff[1]] >> 1);
4650 else
4651 pix[0][c] = CLIP((guess[0]+guess[1]) >> 2);
4652 }
4653 } /* pragma omp parallel */
4654 }
4655
cielab(ushort rgb[3],short lab[3])4656 void CLASS cielab (ushort rgb[3], short lab[3])
4657 {
4658 int c, i, j, k;
4659 float r, xyz[3];
4660 static float cbrt[0x10000], xyz_cam[3][4];
4661
4662 if (!rgb) {
4663 #pragma omp for
4664 for (i=0; i < 0x10000; i++) {
4665 r = i / 65535.0;
4666 cbrt[i] = r > 0.008856 ? pow(r,1/3.0) : 7.787*r + 16/116.0;
4667 }
4668 #pragma omp for
4669 for (i=0; i < 3; i++)
4670 for (j=0; j < colors; j++)
4671 for (xyz_cam[i][j] = k=0; k < 3; k++)
4672 #pragma omp atomic
4673 xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
4674 return;
4675 }
4676 xyz[0] = xyz[1] = xyz[2] = 0.5;
4677 FORCC {
4678 xyz[0] += xyz_cam[0][c] * rgb[c];
4679 xyz[1] += xyz_cam[1][c] * rgb[c];
4680 xyz[2] += xyz_cam[2][c] * rgb[c];
4681 }
4682 xyz[0] = cbrt[CLIP((int) xyz[0])];
4683 xyz[1] = cbrt[CLIP((int) xyz[1])];
4684 xyz[2] = cbrt[CLIP((int) xyz[2])];
4685 lab[0] = 64 * (116 * xyz[1] - 16);
4686 lab[1] = 64 * 500 * (xyz[0] - xyz[1]);
4687 lab[2] = 64 * 200 * (xyz[1] - xyz[2]);
4688 }
4689
4690 #define TS 512 /* Tile Size */
4691 #define fcol(row,col) xtrans[(row+6) % 6][(col+6) % 6]
4692
4693 #ifndef _OPENMP
4694 // Makes a 10% difference in performances in sequential version, but none at all
4695 // with OPENMP...
4696 // And STATIC_BUFFER is delicate to manage
4697 #define STATIC_BUFFER
4698 #define FIXED_NDIR
4699 #endif
4700
4701 /* Allow each tile to be calculate independently from the others.
4702 Explanation: the border pixels from each tiles start from the border pixel of
4703 other tiles.
4704 This goes from top to bottom:
4705 - the first pixels of each tile depend on the last ones of the previous tile
4706 - the last ones depend on the first ones of the next tile
4707 In sequential mode, the first pixels use the calculated values from the
4708 previous tiles and fresh values from the next tile.
4709 In OpenMP / parallel mode, the first pixels of the next files might have been
4710 already calculated.
4711 The STRICT_IMAGE makes it possible to use indeed fresh values, always.
4712 */
4713 #define STRICT_IMAGE
4714
4715 /*
4716 Frank Markesteijn's algorithm for Fuji X-Trans sensors
4717 */
xtrans_interpolate(int passes)4718 void CLASS xtrans_interpolate (int passes)
4719 {
4720 int c, d, f, g, h, i, v, ng, row, col, top, left, mrow, mcol;
4721 int val, pass, hm[8], avg[4], color[3][8];
4722 #ifdef FIXED_NDIR
4723 const int ndir = 4 << 1;
4724 #else
4725 int ndir = 4 << (passes > 1);
4726 #endif
4727
4728 static const short orth[12] = { 1,0,0,1,-1,0,0,-1,1,0,0,1 },
4729 patt[2][16] = { { 0,1,0,-1,2,0,-1,0,1,1,1,-1,0,0,0,0 },
4730 { 0,1,0,-2,1,0,-2,0,1,1,-2,-2,1,-1,-1,1 } },
4731 dir[4] = { 1,TS,TS+1,TS-1 };
4732 short allhex[3][3][2][8], *hex;
4733 ushort min, max, sgrow, sgcol;
4734 ushort (*rgb)[TS][TS][3], (*rix)[3], (*pix)[4];
4735 short (*lab) [TS][3], (*lix)[3];
4736 float (*drv)[TS][TS], diff[6], tr;
4737 char (*homo)[TS][TS];
4738 #ifdef STATIC_BUFFER
4739 static char buffer [TS*TS*(8*11+6)] __attribute__((aligned(64)));
4740 #else
4741 char *buffer;
4742 #endif
4743
4744 if (verbose)
4745 fprintf (stderr,_("%d-pass X-Trans interpolation...\n"), passes);
4746
4747 if ((4 << (passes > 1)) != ndir)
4748 fprintf (stderr,_("4 << (passes > 1) = %d != ndir = %d\n"),
4749 (4 << (passes > 1)), ndir);
4750
4751 #ifdef FIXED_NDIR
4752 #pragma omp parallel default (shared) \
4753 private (top, left, mrow, mcol, row, col, color, pix, hex, c, pass, rix, \
4754 val, h, f, d, g, tr, v, hm, max, avg, lix, diff, i, \
4755 buffer, rgb, lab, drv, homo, ng, min)
4756 #else
4757 #pragma omp parallel default (shared) \
4758 private (top, left, mrow, mcol, row, col, color, pix, hex, c, pass, rix, \
4759 val, h, f, d, g, tr, v, hm, max, avg, lix, diff, i, \
4760 buffer, rgb, lab, drv, homo, ng, min)
4761 #endif
4762 {
4763 cielab(0,0);
4764 border_interpolate(6);
4765
4766 /* Map a green hexagon around each non-green pixel and vice versa: */
4767 #pragma omp for
4768 for (row=0; row < 3; row++)
4769 for (col=0; col < 3; col++)
4770 for (ng=d=0; d < 10; d+=2) {
4771 g = fcol(row,col) == 1;
4772 if (fcol(row+orth[d],col+orth[d+2]) == 1) ng=0; else ng++;
4773 if (ng == 4) { sgrow = row; sgcol = col; }
4774 if (ng == g+1) FORC(8) {
4775 v = orth[d ]*patt[g][c*2] + orth[d+1]*patt[g][c*2+1];
4776 h = orth[d+2]*patt[g][c*2] + orth[d+3]*patt[g][c*2+1];
4777 allhex[row][col][0][c^(g*2 & d)] = h + v*width;
4778 allhex[row][col][1][c^(g*2 & d)] = h + v*TS;
4779 }
4780 }
4781
4782
4783 /* Set green1 and green3 to the minimum and maximum allowed values: */
4784 #pragma omp single
4785 for (row=2; row < height-2; row++)
4786 for (min=~(max=0), col=2; col < width-2; col++) {
4787 if (fcol(row,col) == 1 && (min=~(max=0))) continue;
4788 pix = image + row*width + col;
4789 hex = allhex[row % 3][col % 3][0];
4790 if (!max) FORC(6) {
4791 val = pix[hex[c]][1];
4792 if (min > val) min = val;
4793 if (max < val) max = val;
4794 }
4795 pix[0][1] = min;
4796 pix[0][3] = max;
4797 switch ((row-sgrow) % 3) {
4798 case 1: if (row < height-3) { row++; col--; } break;
4799 case 2: if ((min=~(max=0)) && (col+=2) < width-3 && row > 2) row--;
4800 }
4801 }
4802
4803 #ifdef STRICT_IMAGE
4804 ushort (*working_image)[4];
4805 working_image = (ushort (*)[4]) calloc (iheight, iwidth*sizeof *image);
4806 memcpy (working_image, image, iheight * iwidth * sizeof *image);
4807 merror (working_image, "xtrans_interpolate working_image");
4808 #endif
4809
4810 #ifndef STATIC_BUFFER
4811 buffer = (char *) malloc (TS*TS*(ndir*11+6));
4812 merror (buffer, "xtrans_interpolate()");
4813 #endif
4814
4815 rgb = (ushort(*)[TS][TS][3]) buffer;
4816 lab = (short (*) [TS][3])(buffer + TS*TS*(ndir*6));
4817 drv = (float (*)[TS][TS]) (buffer + TS*TS*(ndir*6+6));
4818 homo = (char (*)[TS][TS]) (buffer + TS*TS*(ndir*10+6));
4819
4820 #pragma omp for
4821 for (top=3; top < height-19; top += TS-16)
4822 for (left=3; left < width-19; left += TS-16) {
4823 mrow = MIN (top+TS, height-3);
4824 mcol = MIN (left+TS, width-3);
4825 for (row=top; row < mrow; row++)
4826 for (col=left; col < mcol; col++)
4827 #ifdef STRICT_IMAGE
4828 memcpy (rgb[0][row-top][col-left], working_image[row*width+col], 6);
4829 #else
4830 memcpy (rgb[0][row-top][col-left], image[row*width+col], 6);
4831 #endif
4832 FORC3 memcpy (rgb[c+1], rgb[0], sizeof *rgb);
4833
4834 /* Interpolate green horizontally, vertically, and along both diagonals: */
4835 for (row=top; row < mrow; row++)
4836 for (col=left; col < mcol; col++) {
4837 if ((f = fcol(row,col)) == 1) continue;
4838 #ifdef STRICT_IMAGE
4839 pix = working_image + row*width + col;
4840 #else
4841 pix = image + row*width + col;
4842 #endif
4843 hex = allhex[row % 3][col % 3][0];
4844 color[1][0] = 174 * (pix[ hex[1]][1] + pix[ hex[0]][1]) -
4845 46 * (pix[2*hex[1]][1] + pix[2*hex[0]][1]);
4846 color[1][1] = 223 * pix[ hex[3]][1] + pix[ hex[2]][1] * 33 +
4847 92 * (pix[ 0 ][f] - pix[ -hex[2]][f]);
4848 FORC(2) color[1][2+c] =
4849 164 * pix[hex[4+c]][1] + 92 * pix[-2*hex[4+c]][1] + 33 *
4850 (2*pix[0][f] - pix[3*hex[4+c]][f] - pix[-3*hex[4+c]][f]);
4851 FORC4 rgb[c^!((row-sgrow) % 3)][row-top][col-left][1] =
4852 LIM(color[1][c] >> 8,pix[0][1],pix[0][3]);
4853 }
4854
4855 for (pass=0; pass < passes; pass++) {
4856 if (pass == 1)
4857 memcpy (rgb+=4, buffer, 4*sizeof *rgb);
4858
4859 /* Recalculate green from interpolated values of closer pixels: */
4860 if (pass) {
4861 for (row=top+2; row < mrow-2; row++)
4862 for (col=left+2; col < mcol-2; col++) {
4863 if ((f = fcol(row,col)) == 1) continue;
4864 #ifdef STRICT_IMAGE
4865 pix = working_image + row*width + col;
4866 #else
4867 pix = image + row*width + col;
4868 #endif
4869 hex = allhex[row % 3][col % 3][1];
4870 for (d=3; d < 6; d++) {
4871 rix = &rgb[(d-2)^!((row-sgrow) % 3)][row-top][col-left];
4872 val = rix[-2*hex[d]][1] + 2*rix[hex[d]][1]
4873 - rix[-2*hex[d]][f] - 2*rix[hex[d]][f] + 3*rix[0][f];
4874 rix[0][1] = LIM(val/3,pix[0][1],pix[0][3]);
4875 }
4876 }
4877 }
4878
4879 /* Interpolate red and blue values for solitary green pixels: */
4880 for (row=(top-sgrow+4)/3*3+sgrow; row < mrow-2; row+=3)
4881 for (col=(left-sgcol+4)/3*3+sgcol; col < mcol-2; col+=3) {
4882 rix = &rgb[0][row-top][col-left];
4883 h = fcol(row,col+1);
4884 memset (diff, 0, sizeof diff);
4885 for (i=1, d=0; d < 6; d++, i^=TS^1, h^=2) {
4886 for (c=0; c < 2; c++, h^=2) {
4887 g = 2*rix[0][1] - rix[i<<c][1] - rix[-i<<c][1];
4888 color[h][d] = g + rix[i<<c][h] + rix[-i<<c][h];
4889 if (d > 1)
4890 diff[d] += SQR (rix[i<<c][1] - rix[-i<<c][1]
4891 - rix[i<<c][h] + rix[-i<<c][h]) + SQR(g);
4892 }
4893 if (d > 1 && (d & 1))
4894 if (diff[d-1] < diff[d])
4895 FORC(2) color[c*2][d] = color[c*2][d-1];
4896 if (d < 2 || (d & 1)) {
4897 FORC(2) rix[0][c*2] = CLIP(color[c*2][d]/2);
4898 rix += TS*TS;
4899 }
4900 }
4901 }
4902
4903 /* Interpolate red for blue pixels and vice versa: */
4904 for (row=top+3; row < mrow-3; row++)
4905 for (col=left+3; col < mcol-3; col++) {
4906 if ((f = 2-fcol(row,col)) == 1) continue;
4907 rix = &rgb[0][row-top][col-left];
4908 c = (row-sgrow) % 3 ? TS:1;
4909 h = 3 * (c ^ TS ^ 1);
4910 for (d=0; d < 4; d++, rix += TS*TS) {
4911 i = d > 1 || ((d ^ c) & 1) ||
4912 ((ABS(rix[0][1]-rix[c][1])+ABS(rix[0][1]-rix[-c][1])) <
4913 2*(ABS(rix[0][1]-rix[h][1])+ABS(rix[0][1]-rix[-h][1]))) ? c:h;
4914 rix[0][f] = CLIP((rix[i][f] + rix[-i][f] +
4915 2*rix[0][1] - rix[i][1] - rix[-i][1])/2);
4916 }
4917 }
4918
4919 /* Fill in red and blue for 2x2 blocks of green: */
4920 for (row=top+2; row < mrow-2; row++) if ((row-sgrow) % 3)
4921 for (col=left+2; col < mcol-2; col++) if ((col-sgcol) % 3) {
4922 rix = &rgb[0][row-top][col-left];
4923 hex = allhex[row % 3][col % 3][1];
4924 for (d=0; d < ndir; d+=2, rix += TS*TS)
4925 if (hex[d] + hex[d+1]) {
4926 g = 3*rix[0][1] - 2*rix[hex[d]][1] - rix[hex[d+1]][1];
4927 for (c=0; c < 4; c+=2) rix[0][c] =
4928 CLIP((g + 2*rix[hex[d]][c] + rix[hex[d+1]][c])/3);
4929 } else {
4930 g = 2*rix[0][1] - rix[hex[d]][1] - rix[hex[d+1]][1];
4931 for (c=0; c < 4; c+=2) rix[0][c] =
4932 CLIP((g + rix[hex[d]][c] + rix[hex[d+1]][c])/2);
4933 }
4934 }
4935 }
4936 rgb = (ushort(*)[TS][TS][3]) buffer;
4937 mrow -= top;
4938 mcol -= left;
4939
4940 /* Convert to CIELab and differentiate in all directions: */
4941 for (d=0; d < ndir; d++) {
4942 for (row=2; row < mrow-2; row++)
4943 for (col=2; col < mcol-2; col++)
4944 cielab (rgb[d][row][col], lab[row][col]);
4945 for (f=dir[d & 3],row=3; row < mrow-3; row++)
4946 for (col=3; col < mcol-3; col++) {
4947 lix = &lab[row][col];
4948 g = 2*lix[0][0] - lix[f][0] - lix[-f][0];
4949 drv[d][row][col] = SQR(g)
4950 + SQR((2*lix[0][1] - lix[f][1] - lix[-f][1] + g*500/232))
4951 + SQR((2*lix[0][2] - lix[f][2] - lix[-f][2] - g*500/580));
4952 }
4953 }
4954
4955 /* Build homogeneity maps from the derivatives: */
4956 memset(homo, 0, ndir*TS*TS);
4957 for (row=4; row < mrow-4; row++)
4958 for (col=4; col < mcol-4; col++) {
4959 for (tr=FLT_MAX, d=0; d < ndir; d++)
4960 tr = MIN(tr, (drv[d][row][col]));
4961 tr *= 8;
4962 for (d=0; d < ndir; d++)
4963 for (v=-1; v <= 1; v++)
4964 for (h=-1; h <= 1; h++)
4965 homo[d][row][col] += (drv[d][row+v][col+h] <= tr);
4966 }
4967
4968 /* Average the most homogenous pixels for the final result: */
4969 if (height-top < TS+4) mrow = height-top+2;
4970 if (width-left < TS+4) mcol = width-left+2;
4971 for (row = MIN(top,8); row < mrow-8; row++)
4972 for (col = MIN(left,8); col < mcol-8; col++) {
4973 for (d=0; d < ndir; d++)
4974 for (hm[d]=0, v=-2; v <= 2; v++)
4975 hm[d] += homo[d][row+v][col-2] +
4976 homo[d][row+v][col-1] +
4977 homo[d][row+v][col+0] +
4978 homo[d][row+v][col+1] +
4979 homo[d][row+v][col+2];
4980 for (d=0; d < ndir-4; d++)
4981 if (hm[d] < hm[d+4]) hm[d ] = 0; else
4982 if (hm[d] > hm[d+4]) hm[d+4] = 0;
4983 for (max=hm[0],d=1; d < ndir; d++)
4984 if (max < hm[d]) max = hm[d];
4985 max -= max >> 3;
4986 memset (avg, 0, sizeof avg);
4987 for (d=0; d < ndir; d++)
4988 if (hm[d] >= max) {
4989 FORC3 {
4990 #pragma omp atomic
4991 avg[c] += rgb[d][row][col][c];
4992 }
4993 avg[3]++;
4994 }
4995 FORC3 image[(row+top)*width+col+left][c] = avg[c]/avg[3];
4996 }
4997 }
4998 #ifndef STATIC_BUFFER
4999 free(buffer);
5000 #endif
5001 border_interpolate(8);
5002 } /* pragma omp parallel */
5003 }
5004 #undef fcol
5005
5006 /*
5007 Adaptive Homogeneity-Directed interpolation is based on
5008 the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
5009 */
ahd_interpolate()5010 void CLASS ahd_interpolate()
5011 {
5012 int i, j, top, left, row, col, tr, tc, c, d, val, hm[2];
5013 static const int dir[4] = { -1, 1, -TS, TS };
5014 unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
5015 ushort (*rgb)[TS][TS][3], (*rix)[3], (*pix)[4];
5016 short (*lab)[TS][TS][3], (*lix)[3];
5017 char (*homo)[TS][TS], *buffer;
5018
5019 if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
5020
5021 #pragma omp parallel default(shared) \
5022 private(top,left,row,col,pix,rix,lix,c,val,d,tc,tr,i,j,ldiff,abdiff,leps, \
5023 abeps,hm,buffer,rgb,lab,homo)
5024 {
5025 cielab (0,0);
5026 border_interpolate(5);
5027 buffer = (char *) malloc (26*TS*TS);
5028 merror (buffer, "ahd_interpolate()");
5029 rgb = (ushort(*)[TS][TS][3]) buffer;
5030 lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
5031 homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
5032
5033 #pragma omp for
5034 for (top=2; top < height-5; top += TS-6)
5035 for (left=2; left < width-5; left += TS-6) {
5036
5037 /* Interpolate green horizontally and vertically: */
5038 for (row=top; row < top+TS && row < height-2; row++) {
5039 col = left + (FC(row,left) & 1);
5040 for (c = FC(row,col); col < left+TS && col < width-2; col+=2) {
5041 pix = image + row*width+col;
5042 val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
5043 - pix[-2][c] - pix[2][c]) >> 2;
5044 rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
5045 val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
5046 - pix[-2*width][c] - pix[2*width][c]) >> 2;
5047 rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
5048 }
5049 }
5050 /* Interpolate red and blue, and convert to CIELab: */
5051 for (d=0; d < 2; d++)
5052 for (row=top+1; row < top+TS-1 && row < height-3; row++)
5053 for (col=left+1; col < left+TS-1 && col < width-3; col++) {
5054 pix = image + row*width+col;
5055 rix = &rgb[d][row-top][col-left];
5056 lix = &lab[d][row-top][col-left];
5057 if ((c = 2 - FC(row,col)) == 1) {
5058 c = FC(row+1,col);
5059 val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
5060 - rix[-1][1] - rix[1][1] ) >> 1);
5061 rix[0][2-c] = CLIP(val);
5062 val = pix[0][1] + (( pix[-width][c] + pix[width][c]
5063 - rix[-TS][1] - rix[TS][1] ) >> 1);
5064 } else
5065 val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
5066 + pix[+width-1][c] + pix[+width+1][c]
5067 - rix[-TS-1][1] - rix[-TS+1][1]
5068 - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
5069 rix[0][c] = CLIP(val);
5070 c = FC(row,col);
5071 rix[0][c] = pix[0][c];
5072 cielab (rix[0],lix[0]);
5073 }
5074 /* Build homogeneity maps from the CIELab images: */
5075 memset (homo, 0, 2*TS*TS);
5076 for (row=top+2; row < top+TS-2 && row < height-4; row++) {
5077 tr = row-top;
5078 for (col=left+2; col < left+TS-2 && col < width-4; col++) {
5079 tc = col-left;
5080 for (d=0; d < 2; d++) {
5081 lix = &lab[d][tr][tc];
5082 for (i=0; i < 4; i++) {
5083 ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
5084 abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
5085 + SQR(lix[0][2]-lix[dir[i]][2]);
5086 }
5087 }
5088 leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
5089 MAX(ldiff[1][2],ldiff[1][3]));
5090 abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
5091 MAX(abdiff[1][2],abdiff[1][3]));
5092 for (d=0; d < 2; d++)
5093 for (i=0; i < 4; i++)
5094 if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
5095 homo[d][tr][tc]++;
5096 }
5097 }
5098 /* Combine the most homogenous pixels for the final result: */
5099 for (row=top+3; row < top+TS-3 && row < height-5; row++) {
5100 tr = row-top;
5101 for (col=left+3; col < left+TS-3 && col < width-5; col++) {
5102 tc = col-left;
5103 for (d=0; d < 2; d++)
5104 for (hm[d]=0, i=tr-1; i <= tr+1; i++)
5105 for (j=tc-1; j <= tc+1; j++)
5106 hm[d] += homo[d][i][j];
5107 if (hm[0] != hm[1])
5108 FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
5109 else
5110 FORC3 image[row*width+col][c] =
5111 (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
5112 }
5113 }
5114 }
5115 free (buffer);
5116 } /* pragma omp parallel */
5117 }
5118 #undef TS
5119
median_filter()5120 void CLASS median_filter()
5121 {
5122 ushort (*pix)[4];
5123 int pass, c, i, j, k, med[9];
5124 static const uchar opt[] = /* Optimal 9-element median search */
5125 { 1,2, 4,5, 7,8, 0,1, 3,4, 6,7, 1,2, 4,5, 7,8,
5126 0,3, 5,8, 4,7, 3,6, 1,4, 2,5, 4,7, 4,2, 6,4, 4,2 };
5127
5128 for (pass=1; pass <= med_passes; pass++) {
5129 if (verbose)
5130 fprintf (stderr,_("Median filter pass %d...\n"), pass);
5131 for (c=0; c < 3; c+=2) {
5132 for (pix = image; pix < image+width*height; pix++)
5133 pix[0][3] = pix[0][c];
5134 for (pix = image+width; pix < image+width*(height-1); pix++) {
5135 if ((pix-image+1) % width < 2) continue;
5136 for (k=0, i = -width; i <= width; i += width)
5137 for (j = i-1; j <= i+1; j++)
5138 med[k++] = pix[j][3] - pix[j][1];
5139 for (i=0; i < sizeof opt; i+=2)
5140 if (med[opt[i]] > med[opt[i+1]])
5141 SWAP (med[opt[i]] , med[opt[i+1]]);
5142 pix[0][c] = CLIP(med[4] + pix[0][1]);
5143 }
5144 }
5145 }
5146 }
5147
blend_highlights()5148 void CLASS blend_highlights()
5149 {
5150 int clip=INT_MAX, row, col, c, i, j;
5151 static const float trans[2][4][4] =
5152 { { { 1,1,1 }, { 1.7320508,-1.7320508,0 }, { -1,-1,2 } },
5153 { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
5154 static const float itrans[2][4][4] =
5155 { { { 1,0.8660254,-0.5 }, { 1,-0.8660254,-0.5 }, { 1,0,1 } },
5156 { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
5157 float cam[2][4], lab[2][4], sum[2], chratio;
5158
5159 if ((unsigned) (colors-3) > 1) return;
5160 if (verbose) fprintf (stderr,_("Blending highlights...\n"));
5161 FORCC if (clip > (i = 65535*pre_mul[c])) clip = i;
5162 for (row=0; row < height; row++)
5163 for (col=0; col < width; col++) {
5164 FORCC if (image[row*width+col][c] > clip) break;
5165 if (c == colors) continue;
5166 FORCC {
5167 cam[0][c] = image[row*width+col][c];
5168 cam[1][c] = MIN(cam[0][c],clip);
5169 }
5170 for (i=0; i < 2; i++) {
5171 FORCC for (lab[i][c]=j=0; j < colors; j++)
5172 lab[i][c] += trans[colors-3][c][j] * cam[i][j];
5173 for (sum[i]=0,c=1; c < colors; c++)
5174 sum[i] += SQR(lab[i][c]);
5175 }
5176 chratio = sqrt(sum[1]/sum[0]);
5177 for (c=1; c < colors; c++)
5178 lab[0][c] *= chratio;
5179 FORCC for (cam[0][c]=j=0; j < colors; j++)
5180 cam[0][c] += itrans[colors-3][c][j] * lab[0][j];
5181 FORCC image[row*width+col][c] = cam[0][c] / colors;
5182 }
5183 }
5184
5185 #define SCALE (4 >> shrink)
recover_highlights()5186 void CLASS recover_highlights()
5187 {
5188 float *map, sum, wgt, grow;
5189 int hsat[4], count, spread, change, val, i;
5190 unsigned high, wide, mrow, mcol, row, col, kc, c, d, y, x;
5191 ushort *pixel;
5192 static const signed char dir[8][2] =
5193 { {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
5194
5195 if (verbose) fprintf (stderr,_("Rebuilding highlights...\n"));
5196
5197 grow = pow (2, 4-highlight);
5198 FORCC hsat[c] = 32000 * pre_mul[c];
5199 for (kc=0, c=1; c < colors; c++)
5200 if (pre_mul[kc] < pre_mul[c]) kc = c;
5201 high = height / SCALE;
5202 wide = width / SCALE;
5203 map = (float *) calloc (high, wide*sizeof *map);
5204 merror (map, "recover_highlights()");
5205 FORCC if (c != kc) {
5206 memset (map, 0, high*wide*sizeof *map);
5207 for (mrow=0; mrow < high; mrow++)
5208 for (mcol=0; mcol < wide; mcol++) {
5209 sum = wgt = count = 0;
5210 for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
5211 for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
5212 pixel = image[row*width+col];
5213 if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000) {
5214 sum += pixel[c];
5215 wgt += pixel[kc];
5216 count++;
5217 }
5218 }
5219 if (count == SCALE*SCALE)
5220 map[mrow*wide+mcol] = sum / wgt;
5221 }
5222 for (spread = 32/grow; spread--; ) {
5223 for (mrow=0; mrow < high; mrow++)
5224 for (mcol=0; mcol < wide; mcol++) {
5225 if (map[mrow*wide+mcol]) continue;
5226 sum = count = 0;
5227 for (d=0; d < 8; d++) {
5228 y = mrow + dir[d][0];
5229 x = mcol + dir[d][1];
5230 if (y < high && x < wide && map[y*wide+x] > 0) {
5231 sum += (1 + (d & 1)) * map[y*wide+x];
5232 count += 1 + (d & 1);
5233 }
5234 }
5235 if (count > 3)
5236 map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
5237 }
5238 for (change=i=0; i < high*wide; i++)
5239 if (map[i] < 0) {
5240 map[i] = -map[i];
5241 change = 1;
5242 }
5243 if (!change) break;
5244 }
5245 for (i=0; i < high*wide; i++)
5246 if (map[i] == 0) map[i] = 1;
5247 for (mrow=0; mrow < high; mrow++)
5248 for (mcol=0; mcol < wide; mcol++) {
5249 for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
5250 for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
5251 pixel = image[row*width+col];
5252 if (pixel[c] / hsat[c] > 1) {
5253 val = pixel[kc] * map[mrow*wide+mcol];
5254 if (pixel[c] < val) pixel[c] = CLIP(val);
5255 }
5256 }
5257 }
5258 }
5259 free (map);
5260 }
5261 #undef SCALE
5262
tiff_get(unsigned base,unsigned * tag,unsigned * type,unsigned * len,unsigned * save)5263 void CLASS tiff_get (unsigned base,
5264 unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
5265 {
5266 *tag = get2();
5267 *type = get2();
5268 *len = get4();
5269 *save = ftell(ifp) + 4;
5270 if (*len * ("11124811248484"[*type < 14 ? *type:0]-'0') > 4)
5271 fseek (ifp, get4()+base, SEEK_SET);
5272 }
5273
parse_thumb_note(int base,unsigned toff,unsigned tlen)5274 void CLASS parse_thumb_note (int base, unsigned toff, unsigned tlen)
5275 {
5276 unsigned entries, tag, type, len, save;
5277
5278 entries = get2();
5279 while (entries--) {
5280 tiff_get (base, &tag, &type, &len, &save);
5281 if (tag == toff) thumb_offset = get4()+base;
5282 if (tag == tlen) thumb_length = get4();
5283 fseek (ifp, save, SEEK_SET);
5284 }
5285 }
5286
5287 int CLASS parse_tiff_ifd (int base);
5288
parse_makernote(int base,int uptag)5289 void CLASS parse_makernote (int base, int uptag)
5290 {
5291 static const uchar xlat[2][256] = {
5292 { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
5293 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
5294 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
5295 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
5296 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
5297 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
5298 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
5299 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
5300 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
5301 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
5302 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
5303 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
5304 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
5305 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
5306 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
5307 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
5308 { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
5309 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
5310 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
5311 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
5312 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
5313 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
5314 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
5315 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
5316 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
5317 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
5318 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
5319 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
5320 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
5321 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
5322 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
5323 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
5324 unsigned offset=0, entries, tag, type, len, save, c;
5325 unsigned ver97=0, serial=0, i, wbi=0, wb[4]={0,0,0,0};
5326 uchar buf97[324], ci, cj, ck;
5327 short morder, sorder=order;
5328 char buf[10];
5329 /*
5330 The MakerNote might have its own TIFF header (possibly with
5331 its own byte-order!), or it might just be a table.
5332 */
5333 if (!strcmp(make,"Nokia")) return;
5334 fread (buf, 1, 10, ifp);
5335 if (!strncmp (buf,"KDK" ,3) || /* these aren't TIFF tables */
5336 !strncmp (buf,"VER" ,3) ||
5337 !strncmp (buf,"IIII",4) ||
5338 !strncmp (buf,"MMMM",4)) return;
5339 if (!strncmp (buf,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
5340 !strncmp (buf,"MLY" ,3)) { /* Minolta DiMAGE G series */
5341 order = 0x4d4d;
5342 while ((i=ftell(ifp)) < data_offset && i < 16384) {
5343 wb[0] = wb[2]; wb[2] = wb[1]; wb[1] = wb[3];
5344 wb[3] = get2();
5345 if (wb[1] == 256 && wb[3] == 256 &&
5346 wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
5347 FORC4 cam_mul[c] = wb[c];
5348 }
5349 goto quit;
5350 }
5351 if (!strcmp (buf,"Nikon")) {
5352 base = ftell(ifp);
5353 order = get2();
5354 if (get2() != 42) goto quit;
5355 offset = get4();
5356 fseek (ifp, offset-8, SEEK_CUR);
5357 } else if (!strcmp (buf,"OLYMPUS") ||
5358 !strcmp (buf,"PENTAX ")) {
5359 base = ftell(ifp)-10;
5360 fseek (ifp, -2, SEEK_CUR);
5361 order = get2();
5362 if (buf[0] == 'O') get2();
5363 } else if (!strncmp (buf,"SONY",4) ||
5364 !strcmp (buf,"Panasonic")) {
5365 goto nf;
5366 } else if (!strncmp (buf,"FUJIFILM",8)) {
5367 base = ftell(ifp)-10;
5368 nf: order = 0x4949;
5369 fseek (ifp, 2, SEEK_CUR);
5370 } else if (!strcmp (buf,"OLYMP") ||
5371 !strcmp (buf,"LEICA") ||
5372 !strcmp (buf,"Ricoh") ||
5373 !strcmp (buf,"EPSON"))
5374 fseek (ifp, -2, SEEK_CUR);
5375 else if (!strcmp (buf,"AOC") ||
5376 !strcmp (buf,"QVC"))
5377 fseek (ifp, -4, SEEK_CUR);
5378 else {
5379 fseek (ifp, -10, SEEK_CUR);
5380 if (!strncmp(make,"SAMSUNG",7))
5381 base = ftell(ifp);
5382 }
5383 entries = get2();
5384 if (entries > 1000) return;
5385 morder = order;
5386 while (entries--) {
5387 order = morder;
5388 tiff_get (base, &tag, &type, &len, &save);
5389 tag |= uptag << 16;
5390 if (tag == 2 && strstr(make,"NIKON") && !iso_speed)
5391 iso_speed = (get2(),get2());
5392 if (tag == 4 && len > 26 && len < 35) {
5393 if ((i=(get4(),get2())) != 0x7fff && !iso_speed)
5394 iso_speed = 50 * pow (2, i/32.0 - 4);
5395 if ((i=(get2(),get2())) != 0x7fff && !aperture)
5396 aperture = pow (2, i/64.0);
5397 if ((i=get2()) != 0xffff && !shutter)
5398 shutter = pow (2, (short) i/-32.0);
5399 wbi = (get2(),get2());
5400 shot_order = (get2(),get2());
5401 }
5402 if ((tag == 4 || tag == 0x114) && !strncmp(make,"KONICA",6)) {
5403 fseek (ifp, tag == 4 ? 140:160, SEEK_CUR);
5404 switch (get2()) {
5405 case 72: flip = 0; break;
5406 case 76: flip = 6; break;
5407 case 82: flip = 5; break;
5408 }
5409 }
5410 if (tag == 7 && type == 2 && len > 20)
5411 fgets (model2, 64, ifp);
5412 if (tag == 8 && type == 4)
5413 shot_order = get4();
5414 if (tag == 9 && !strcmp(make,"Canon"))
5415 fread (artist, 64, 1, ifp);
5416 if (tag == 0xc && len == 4)
5417 FORC3 cam_mul[(c << 1 | c >> 1) & 3] = getreal(type);
5418 if (tag == 0xd && type == 7 && get2() == 0xaaaa) {
5419 for (c=i=2; (ushort) c != 0xbbbb && i < len; i++)
5420 c = c << 8 | fgetc(ifp);
5421 while ((i+=4) < len-5)
5422 if (get4() == 257 && (i=len) && (c = (get4(),fgetc(ifp))) < 3)
5423 flip = "065"[c]-'0';
5424 }
5425 if (tag == 0x10 && type == 4)
5426 unique_id = get4();
5427 if (tag == 0x11 && is_raw && !strncmp(make,"NIKON",5)) {
5428 fseek (ifp, get4()+base, SEEK_SET);
5429 parse_tiff_ifd (base);
5430 }
5431 if (tag == 0x14 && type == 7) {
5432 if (len == 2560) {
5433 fseek (ifp, 1248, SEEK_CUR);
5434 goto get2_256;
5435 }
5436 fread (buf, 1, 10, ifp);
5437 if (!strncmp(buf,"NRW ",4)) {
5438 fseek (ifp, strcmp(buf+4,"0100") ? 46:1546, SEEK_CUR);
5439 cam_mul[0] = get4() << 2;
5440 cam_mul[1] = get4() + get4();
5441 cam_mul[2] = get4() << 2;
5442 }
5443 }
5444 if (tag == 0x15 && type == 2 && is_raw)
5445 fread (model, 64, 1, ifp);
5446 if (strstr(make,"PENTAX")) {
5447 if (tag == 0x1b) tag = 0x1018;
5448 if (tag == 0x1c) tag = 0x1017;
5449 }
5450 if (tag == 0x1d)
5451 while ((c = fgetc(ifp)) && c != EOF)
5452 serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
5453 if (tag == 0x29 && type == 1) {
5454 c = wbi < 18 ? "012347800000005896"[wbi]-'0' : 0;
5455 fseek (ifp, 8 + c*32, SEEK_CUR);
5456 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get4();
5457 }
5458 if (tag == 0x3d && type == 3 && len == 4)
5459 FORC4 cblack[c ^ c >> 1] = get2() >> (14-tiff_bps);
5460 if (tag == 0x81 && type == 4) {
5461 data_offset = get4();
5462 fseek (ifp, data_offset + 41, SEEK_SET);
5463 raw_height = get2() * 2;
5464 raw_width = get2();
5465 filters = 0x61616161;
5466 }
5467 if ((tag == 0x81 && type == 7) ||
5468 (tag == 0x100 && type == 7) ||
5469 (tag == 0x280 && type == 1)) {
5470 thumb_offset = ftell(ifp);
5471 thumb_length = len;
5472 }
5473 if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
5474 thumb_offset += base;
5475 if (tag == 0x89 && type == 4)
5476 thumb_length = get4();
5477 if (tag == 0x8c || tag == 0x96)
5478 meta_offset = ftell(ifp);
5479 if (tag == 0x97) {
5480 for (i=0; i < 4; i++)
5481 ver97 = ver97 * 10 + fgetc(ifp)-'0';
5482 switch (ver97) {
5483 case 100:
5484 fseek (ifp, 68, SEEK_CUR);
5485 FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
5486 break;
5487 case 102:
5488 fseek (ifp, 6, SEEK_CUR);
5489 FORC4 cam_mul[c ^ (c >> 1)] = get2();
5490 break;
5491 case 103:
5492 fseek (ifp, 16, SEEK_CUR);
5493 FORC4 cam_mul[c] = get2();
5494 }
5495 if (ver97 >= 200) {
5496 if (ver97 != 205) fseek (ifp, 280, SEEK_CUR);
5497 fread (buf97, 324, 1, ifp);
5498 }
5499 }
5500 if (tag == 0xa1 && type == 7) {
5501 order = 0x4949;
5502 fseek (ifp, 140, SEEK_CUR);
5503 FORC3 cam_mul[c] = get4();
5504 }
5505 if (tag == 0xa4 && type == 3) {
5506 fseek (ifp, wbi*48, SEEK_CUR);
5507 FORC3 cam_mul[c] = get2();
5508 }
5509 if (tag == 0xa7 && (unsigned) (ver97-200) < 17) {
5510 ci = xlat[0][serial & 0xff];
5511 cj = xlat[1][fgetc(ifp)^fgetc(ifp)^fgetc(ifp)^fgetc(ifp)];
5512 ck = 0x60;
5513 for (i=0; i < 324; i++)
5514 buf97[i] ^= (cj += ci * ck++);
5515 i = "66666>666;6A;:;55"[ver97-200] - '0';
5516 FORC4 cam_mul[c ^ (c >> 1) ^ (i & 1)] =
5517 sget2 (buf97 + (i & -2) + c*2);
5518 }
5519 if (tag == 0x200 && len == 3)
5520 shot_order = (get4(),get4());
5521 if (tag == 0x200 && len == 4)
5522 FORC4 cblack[c ^ c >> 1] = get2();
5523 if (tag == 0x201 && len == 4)
5524 FORC4 cam_mul[c ^ (c >> 1)] = get2();
5525 if (tag == 0x220 && type == 7)
5526 meta_offset = ftell(ifp);
5527 if (tag == 0x401 && type == 4 && len == 4)
5528 FORC4 cblack[c ^ c >> 1] = get4();
5529 if (tag == 0xe01) { /* Nikon Capture Note */
5530 order = 0x4949;
5531 fseek (ifp, 22, SEEK_CUR);
5532 for (offset=22; offset+22 < len; offset += 22+i) {
5533 tag = get4();
5534 fseek (ifp, 14, SEEK_CUR);
5535 i = get4()-4;
5536 if (tag == 0x76a43207) flip = get2();
5537 else fseek (ifp, i, SEEK_CUR);
5538 }
5539 }
5540 if (tag == 0xe80 && len == 256 && type == 7) {
5541 fseek (ifp, 48, SEEK_CUR);
5542 cam_mul[0] = get2() * 508 * 1.078 / 0x10000;
5543 cam_mul[2] = get2() * 382 * 1.173 / 0x10000;
5544 }
5545 if (tag == 0xf00 && type == 7) {
5546 if (len == 614)
5547 fseek (ifp, 176, SEEK_CUR);
5548 else if (len == 734 || len == 1502)
5549 fseek (ifp, 148, SEEK_CUR);
5550 else goto next;
5551 goto get2_256;
5552 }
5553 if ((tag == 0x1011 && len == 9) || tag == 0x20400200)
5554 for (i=0; i < 3; i++)
5555 FORC3 cmatrix[i][c] = ((short) get2()) / 256.0;
5556 if ((tag == 0x1012 || tag == 0x20400600) && len == 4)
5557 FORC4 cblack[c ^ c >> 1] = get2();
5558 if (tag == 0x1017 || tag == 0x20400100)
5559 cam_mul[0] = get2() / 256.0;
5560 if (tag == 0x1018 || tag == 0x20400100)
5561 cam_mul[2] = get2() / 256.0;
5562 if (tag == 0x2011 && len == 2) {
5563 get2_256:
5564 order = 0x4d4d;
5565 cam_mul[0] = get2() / 256.0;
5566 cam_mul[2] = get2() / 256.0;
5567 }
5568 if ((tag | 0x70) == 0x2070 && (type == 4 || type == 13))
5569 fseek (ifp, get4()+base, SEEK_SET);
5570 if (tag == 0x2020 && !strncmp(buf,"OLYMP",5))
5571 parse_thumb_note (base, 257, 258);
5572 if (tag == 0x2040)
5573 parse_makernote (base, 0x2040);
5574 if (tag == 0xb028) {
5575 fseek (ifp, get4()+base, SEEK_SET);
5576 parse_thumb_note (base, 136, 137);
5577 }
5578 if (tag == 0x4001 && len > 500) {
5579 i = len == 582 ? 50 : len == 653 ? 68 : len == 5120 ? 142 : 126;
5580 fseek (ifp, i, SEEK_CUR);
5581 FORC4 cam_mul[c ^ (c >> 1)] = get2();
5582 for (i+=18; i <= len; i+=10) {
5583 get2();
5584 FORC4 sraw_mul[c ^ (c >> 1)] = get2();
5585 if (sraw_mul[1] == 1170) break;
5586 }
5587 }
5588 if (tag == 0x4021 && get4() && get4())
5589 FORC4 cam_mul[c] = 1024;
5590 if (tag == 0xa021)
5591 FORC4 cam_mul[c ^ (c >> 1)] = get4();
5592 if (tag == 0xa028)
5593 FORC4 cam_mul[c ^ (c >> 1)] -= get4();
5594 if (tag == 0xb001)
5595 unique_id = get2();
5596 next:
5597 fseek (ifp, save, SEEK_SET);
5598 }
5599 quit:
5600 order = sorder;
5601 }
5602
5603 /*
5604 Since the TIFF DateTime string has no timezone information,
5605 assume that the camera's clock was set to Universal Time.
5606 */
get_timestamp(int reversed)5607 void CLASS get_timestamp (int reversed)
5608 {
5609 struct tm t;
5610 char str[20];
5611 int i;
5612
5613 str[19] = 0;
5614 if (reversed)
5615 for (i=19; i--; ) str[i] = fgetc(ifp);
5616 else
5617 fread (str, 19, 1, ifp);
5618 memset (&t, 0, sizeof t);
5619 if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
5620 &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
5621 return;
5622 t.tm_year -= 1900;
5623 t.tm_mon -= 1;
5624 t.tm_isdst = -1;
5625 if (mktime(&t) > 0)
5626 timestamp = mktime(&t);
5627 }
5628
parse_exif(int base)5629 void CLASS parse_exif (int base)
5630 {
5631 unsigned kodak, entries, tag, type, len, save, c;
5632 double expo;
5633
5634 kodak = !strncmp(make,"EASTMAN",7) && tiff_nifds < 3;
5635 entries = get2();
5636 while (entries--) {
5637 tiff_get (base, &tag, &type, &len, &save);
5638 switch (tag) {
5639 case 33434: tiff_ifd[tiff_nifds-1].shutter =
5640 shutter = getreal(type); break;
5641 case 33437: aperture = getreal(type); break;
5642 case 34855: iso_speed = get2(); break;
5643 case 36867:
5644 case 36868: get_timestamp(0); break;
5645 case 37377: if ((expo = -getreal(type)) < 128)
5646 tiff_ifd[tiff_nifds-1].shutter =
5647 shutter = pow (2, expo); break;
5648 case 37378: aperture = pow (2, getreal(type)/2); break;
5649 case 37386: focal_len = getreal(type); break;
5650 case 37500: parse_makernote (base, 0); break;
5651 case 40962: if (kodak) raw_width = get4(); break;
5652 case 40963: if (kodak) raw_height = get4(); break;
5653 case 41730:
5654 if (get4() == 0x20002)
5655 for (exif_cfa=c=0; c < 8; c+=2)
5656 exif_cfa |= fgetc(ifp) * 0x01010101 << c;
5657 }
5658 fseek (ifp, save, SEEK_SET);
5659 }
5660 }
5661
parse_gps(int base)5662 void CLASS parse_gps (int base)
5663 {
5664 unsigned entries, tag, type, len, save, c;
5665
5666 entries = get2();
5667 while (entries--) {
5668 tiff_get (base, &tag, &type, &len, &save);
5669 switch (tag) {
5670 case 1: case 3: case 5:
5671 gpsdata[29+tag/2] = getc(ifp); break;
5672 case 2: case 4: case 7:
5673 FORC(6) gpsdata[tag/3*6+c] = get4(); break;
5674 case 6:
5675 FORC(2) gpsdata[18+c] = get4(); break;
5676 case 18: case 29:
5677 fgets ((char *) (gpsdata+14+tag/3), MIN(len,12), ifp);
5678 }
5679 fseek (ifp, save, SEEK_SET);
5680 }
5681 }
5682
romm_coeff(float romm_cam[3][3])5683 void CLASS romm_coeff (float romm_cam[3][3])
5684 {
5685 static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
5686 { { 2.034193, -0.727420, -0.306766 },
5687 { -0.228811, 1.231729, -0.002922 },
5688 { -0.008565, -0.153273, 1.161839 } };
5689 int i, j, k;
5690
5691 for (i=0; i < 3; i++)
5692 for (j=0; j < 3; j++)
5693 for (cmatrix[i][j] = k=0; k < 3; k++)
5694 cmatrix[i][j] += rgb_romm[i][k] * romm_cam[k][j];
5695 }
5696
parse_mos(int offset)5697 void CLASS parse_mos (int offset)
5698 {
5699 char data[40];
5700 int skip, from, i, c, neut[4], planes=0, frot=0;
5701 static const char *mod[] =
5702 { "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
5703 "Valeo 11p","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65",
5704 "Aptus 54S","Aptus 65S","Aptus 75S","AFi 5","AFi 6","AFi 7",
5705 "AFi-II 7","Aptus-II 7","","Aptus-II 6","","","Aptus-II 10","Aptus-II 5",
5706 "","","","","Aptus-II 10R","Aptus-II 8","","Aptus-II 12","","AFi-II 12" };
5707 float romm_cam[3][3];
5708
5709 fseek (ifp, offset, SEEK_SET);
5710 while (1) {
5711 if (get4() != 0x504b5453) break;
5712 get4();
5713 fread (data, 1, 40, ifp);
5714 skip = get4();
5715 from = ftell(ifp);
5716 if (!strcmp(data,"JPEG_preview_data")) {
5717 thumb_offset = from;
5718 thumb_length = skip;
5719 }
5720 if (!strcmp(data,"icc_camera_profile")) {
5721 profile_offset = from;
5722 profile_length = skip;
5723 }
5724 if (!strcmp(data,"ShootObj_back_type")) {
5725 fscanf (ifp, "%d", &i);
5726 if ((unsigned) i < sizeof mod / sizeof (*mod))
5727 strcpy (model, mod[i]);
5728 }
5729 if (!strcmp(data,"icc_camera_to_tone_matrix")) {
5730 for (i=0; i < 9; i++)
5731 ((float *)romm_cam)[i] = int_to_float(get4());
5732 romm_coeff (romm_cam);
5733 }
5734 if (!strcmp(data,"CaptProf_color_matrix")) {
5735 for (i=0; i < 9; i++)
5736 fscanf (ifp, "%f", (float *)romm_cam + i);
5737 romm_coeff (romm_cam);
5738 }
5739 if (!strcmp(data,"CaptProf_number_of_planes"))
5740 fscanf (ifp, "%d", &planes);
5741 if (!strcmp(data,"CaptProf_raw_data_rotation"))
5742 fscanf (ifp, "%d", &flip);
5743 if (!strcmp(data,"CaptProf_mosaic_pattern"))
5744 FORC4 {
5745 fscanf (ifp, "%d", &i);
5746 if (i == 1) frot = c ^ (c >> 1);
5747 }
5748 if (!strcmp(data,"ImgProf_rotation_angle")) {
5749 fscanf (ifp, "%d", &i);
5750 flip = i - flip;
5751 }
5752 if (!strcmp(data,"NeutObj_neutrals") && !cam_mul[0]) {
5753 FORC4 fscanf (ifp, "%d", neut+c);
5754 FORC3 cam_mul[c] = (float) neut[0] / neut[c+1];
5755 }
5756 if (!strcmp(data,"Rows_data"))
5757 load_flags = get4();
5758 parse_mos (from);
5759 fseek (ifp, skip+from, SEEK_SET);
5760 }
5761 if (planes)
5762 filters = (planes == 1) * 0x01010101 *
5763 (uchar) "\x94\x61\x16\x49"[(flip/90 + frot) & 3];
5764 }
5765
linear_table(unsigned len)5766 void CLASS linear_table (unsigned len)
5767 {
5768 int i;
5769 if (len > 0x1000) len = 0x1000;
5770 read_shorts (curve, len);
5771 for (i=len; i < 0x1000; i++)
5772 curve[i] = curve[i-1];
5773 maximum = curve[0xfff];
5774 }
5775
parse_kodak_ifd(int base)5776 void CLASS parse_kodak_ifd (int base)
5777 {
5778 unsigned entries, tag, type, len, save;
5779 int i, c, wbi=-2, wbtemp=6500;
5780 float mul[3]={1,1,1}, num;
5781 static const int wbtag[] = { 64037,64040,64039,64041,-1,-1,64042 };
5782
5783 entries = get2();
5784 if (entries > 1024) return;
5785 while (entries--) {
5786 tiff_get (base, &tag, &type, &len, &save);
5787 if (tag == 1020) wbi = getint(type);
5788 if (tag == 1021 && len == 72) { /* WB set in software */
5789 fseek (ifp, 40, SEEK_CUR);
5790 FORC3 cam_mul[c] = 2048.0 / get2();
5791 wbi = -2;
5792 }
5793 if (tag == 2118) wbtemp = getint(type);
5794 if (tag == 2120 + wbi && wbi >= 0)
5795 FORC3 cam_mul[c] = 2048.0 / getreal(type);
5796 if (tag == 2130 + wbi)
5797 FORC3 mul[c] = getreal(type);
5798 if (tag == 2140 + wbi && wbi >= 0)
5799 FORC3 {
5800 for (num=i=0; i < 4; i++)
5801 num += getreal(type) * pow (wbtemp/100.0, i);
5802 cam_mul[c] = 2048 / (num * mul[c]);
5803 }
5804 if (tag == 2317) linear_table (len);
5805 if (tag == 6020) iso_speed = getint(type);
5806 if (tag == 64013) wbi = fgetc(ifp);
5807 if ((unsigned) wbi < 7 && tag == wbtag[wbi])
5808 FORC3 cam_mul[c] = get4();
5809 if (tag == 64019) width = getint(type);
5810 if (tag == 64020) height = (getint(type)+1) & -2;
5811 fseek (ifp, save, SEEK_SET);
5812 }
5813 }
5814
5815 void CLASS parse_minolta (int base);
5816 int CLASS parse_tiff (int base);
5817
parse_tiff_ifd(int base)5818 int CLASS parse_tiff_ifd (int base)
5819 {
5820 unsigned entries, tag, type, len, plen=16, save;
5821 int ifd, use_cm=0, cfa, i, j, c, ima_len=0;
5822 char software[64], *cbuf, *cp;
5823 uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
5824 double cc[4][4], cm[4][3], cam_xyz[4][3], num;
5825 double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
5826 unsigned sony_curve[] = { 0,0,0,0,0,4095 };
5827 unsigned *buf, sony_offset=0, sony_length=0, sony_key=0;
5828 struct jhead jh;
5829 FILE *sfp;
5830
5831 if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
5832 return 1;
5833 ifd = tiff_nifds++;
5834 for (j=0; j < 4; j++)
5835 for (i=0; i < 4; i++)
5836 cc[j][i] = i == j;
5837 entries = get2();
5838 if (entries > 512) return 1;
5839 while (entries--) {
5840 tiff_get (base, &tag, &type, &len, &save);
5841 switch (tag) {
5842 case 5: width = get2(); break;
5843 case 6: height = get2(); break;
5844 case 7: width += get2(); break;
5845 case 9: if ((i = get2())) filters = i; break;
5846 case 17: case 18:
5847 if (type == 3 && len == 1)
5848 cam_mul[(tag-17)*2] = get2() / 256.0;
5849 break;
5850 case 23:
5851 if (type == 3) iso_speed = get2();
5852 break;
5853 case 28: case 29: case 30:
5854 cblack[tag-28] = get2();
5855 cblack[3] = cblack[1];
5856 break;
5857 case 36: case 37: case 38:
5858 cam_mul[tag-36] = get2();
5859 break;
5860 case 39:
5861 if (len < 50 || cam_mul[0]) break;
5862 fseek (ifp, 12, SEEK_CUR);
5863 FORC3 cam_mul[c] = get2();
5864 break;
5865 case 46:
5866 if (type != 7 || fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) break;
5867 thumb_offset = ftell(ifp) - 2;
5868 thumb_length = len;
5869 break;
5870 case 61440: /* Fuji HS10 table */
5871 fseek (ifp, get4()+base, SEEK_SET);
5872 parse_tiff_ifd (base);
5873 break;
5874 case 2: case 256: case 61441: /* ImageWidth */
5875 tiff_ifd[ifd].width = getint(type);
5876 break;
5877 case 3: case 257: case 61442: /* ImageHeight */
5878 tiff_ifd[ifd].height = getint(type);
5879 break;
5880 case 258: /* BitsPerSample */
5881 case 61443:
5882 tiff_ifd[ifd].samples = len & 7;
5883 if ((tiff_ifd[ifd].bps = getint(type)) > 32)
5884 tiff_ifd[ifd].bps = 8;
5885 if (tiff_bps < tiff_ifd[ifd].bps)
5886 tiff_bps = tiff_ifd[ifd].bps;
5887 break;
5888 case 61446:
5889 raw_height = 0;
5890 load_flags = get4() ? 24:80;
5891 break;
5892 case 259: /* Compression */
5893 tiff_ifd[ifd].comp = getint(type);
5894 break;
5895 case 262: /* PhotometricInterpretation */
5896 tiff_ifd[ifd].phint = get2();
5897 break;
5898 case 270: /* ImageDescription */
5899 fread (desc, 512, 1, ifp);
5900 break;
5901 case 271: /* Make */
5902 fgets (make, 64, ifp);
5903 break;
5904 case 272: /* Model */
5905 fgets (model, 64, ifp);
5906 break;
5907 case 280: /* Panasonic RW2 offset */
5908 if (type != 4) break;
5909 load_raw = &CLASS panasonic_load_raw;
5910 load_flags = 0x2008;
5911 case 273: /* StripOffset */
5912 case 513: /* JpegIFOffset */
5913 case 61447:
5914 tiff_ifd[ifd].offset = get4()+base;
5915 if (!tiff_ifd[ifd].bps && tiff_ifd[ifd].offset > 0) {
5916 fseek (ifp, tiff_ifd[ifd].offset, SEEK_SET);
5917 if (ljpeg_start (&jh, 1)) {
5918 tiff_ifd[ifd].comp = 6;
5919 tiff_ifd[ifd].width = jh.wide;
5920 tiff_ifd[ifd].height = jh.high;
5921 tiff_ifd[ifd].bps = jh.bits;
5922 tiff_ifd[ifd].samples = jh.clrs;
5923 if (!(jh.sraw || (jh.clrs & 1)))
5924 tiff_ifd[ifd].width *= jh.clrs;
5925 if ((tiff_ifd[ifd].width > 4*tiff_ifd[ifd].height) & ~jh.clrs) {
5926 tiff_ifd[ifd].width /= 2;
5927 tiff_ifd[ifd].height *= 2;
5928 }
5929 i = order;
5930 parse_tiff (tiff_ifd[ifd].offset + 12);
5931 order = i;
5932 }
5933 }
5934 break;
5935 case 274: /* Orientation */
5936 tiff_ifd[ifd].flip = "50132467"[get2() & 7]-'0';
5937 break;
5938 case 277: /* SamplesPerPixel */
5939 tiff_ifd[ifd].samples = getint(type) & 7;
5940 break;
5941 case 279: /* StripByteCounts */
5942 case 514:
5943 case 61448:
5944 tiff_ifd[ifd].bytes = get4();
5945 break;
5946 case 61454:
5947 FORC3 cam_mul[(4-c) % 3] = getint(type);
5948 break;
5949 case 305: case 11: /* Software */
5950 fgets (software, 64, ifp);
5951 if (!strncmp(software,"Adobe",5) ||
5952 !strncmp(software,"dcraw",5) ||
5953 !strncmp(software,"UFRaw",5) ||
5954 !strncmp(software,"Bibble",6) ||
5955 !strncmp(software,"Nikon Scan",10) ||
5956 !strcmp (software,"Digital Photo Professional"))
5957 is_raw = 0;
5958 break;
5959 case 306: /* DateTime */
5960 get_timestamp(0);
5961 break;
5962 case 315: /* Artist */
5963 fread (artist, 64, 1, ifp);
5964 break;
5965 case 322: /* TileWidth */
5966 tiff_ifd[ifd].tile_width = getint(type);
5967 break;
5968 case 323: /* TileLength */
5969 tiff_ifd[ifd].tile_length = getint(type);
5970 break;
5971 case 324: /* TileOffsets */
5972 tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
5973 if (len == 1)
5974 tiff_ifd[ifd].tile_width = tiff_ifd[ifd].tile_length = 0;
5975 if (len == 4) {
5976 load_raw = &CLASS sinar_4shot_load_raw;
5977 is_raw = 5;
5978 }
5979 break;
5980 case 330: /* SubIFDs */
5981 if (!strcmp(model,"DSLR-A100") && tiff_ifd[ifd].width == 3872) {
5982 load_raw = &CLASS sony_arw_load_raw;
5983 data_offset = get4()+base;
5984 ifd++; break;
5985 }
5986 while (len--) {
5987 i = ftell(ifp);
5988 fseek (ifp, get4()+base, SEEK_SET);
5989 if (parse_tiff_ifd (base)) break;
5990 fseek (ifp, i+4, SEEK_SET);
5991 }
5992 break;
5993 case 400:
5994 strcpy (make, "Sarnoff");
5995 maximum = 0xfff;
5996 break;
5997 case 28688:
5998 FORC4 sony_curve[c+1] = get2() >> 2 & 0xfff;
5999 for (i=0; i < 5; i++)
6000 for (j = sony_curve[i]+1; j <= sony_curve[i+1]; j++)
6001 curve[j] = curve[j-1] + (1 << i);
6002 break;
6003 case 29184: sony_offset = get4(); break;
6004 case 29185: sony_length = get4(); break;
6005 case 29217: sony_key = get4(); break;
6006 case 29264:
6007 parse_minolta (ftell(ifp));
6008 raw_width = 0;
6009 break;
6010 case 29443:
6011 FORC4 cam_mul[c ^ (c < 2)] = get2();
6012 break;
6013 case 29459:
6014 FORC4 cam_mul[c] = get2();
6015 i = (cam_mul[1] == 1024 && cam_mul[2] == 1024) << 1;
6016 SWAP (cam_mul[i],cam_mul[i+1])
6017 break;
6018 case 33405: /* Model2 */
6019 fgets (model2, 64, ifp);
6020 break;
6021 case 33421: /* CFARepeatPatternDim */
6022 if (get2() == 6 && get2() == 6)
6023 filters = 9;
6024 break;
6025 case 33422: /* CFAPattern */
6026 if (filters == 9) {
6027 FORC(36) ((char *)xtrans)[c] = fgetc(ifp) & 3;
6028 break;
6029 }
6030 case 64777: /* Kodak P-series */
6031 if ((plen=len) > 16) plen = 16;
6032 fread (cfa_pat, 1, plen, ifp);
6033 for (colors=cfa=i=0; i < plen && colors < 4; i++) {
6034 colors += !(cfa & (1 << cfa_pat[i]));
6035 cfa |= 1 << cfa_pat[i];
6036 }
6037 if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
6038 if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
6039 goto guess_cfa_pc;
6040 case 33424:
6041 case 65024:
6042 fseek (ifp, get4()+base, SEEK_SET);
6043 parse_kodak_ifd (base);
6044 break;
6045 case 33434: /* ExposureTime */
6046 tiff_ifd[ifd].shutter = shutter = getreal(type);
6047 break;
6048 case 33437: /* FNumber */
6049 aperture = getreal(type);
6050 break;
6051 case 34306: /* Leaf white balance */
6052 FORC4 cam_mul[c ^ 1] = 4096.0 / get2();
6053 break;
6054 case 34307: /* Leaf CatchLight color matrix */
6055 fread (software, 1, 7, ifp);
6056 if (strncmp(software,"MATRIX",6)) break;
6057 colors = 4;
6058 for (raw_color = i=0; i < 3; i++) {
6059 FORC4 fscanf (ifp, "%f", &rgb_cam[i][c^1]);
6060 if (!use_camera_wb) continue;
6061 num = 0;
6062 FORC4 num += rgb_cam[i][c];
6063 FORC4 rgb_cam[i][c] /= num;
6064 }
6065 break;
6066 case 34310: /* Leaf metadata */
6067 parse_mos (ftell(ifp));
6068 case 34303:
6069 strcpy (make, "Leaf");
6070 break;
6071 case 34665: /* EXIF tag */
6072 fseek (ifp, get4()+base, SEEK_SET);
6073 parse_exif (base);
6074 break;
6075 case 34853: /* GPSInfo tag */
6076 fseek (ifp, get4()+base, SEEK_SET);
6077 parse_gps (base);
6078 break;
6079 case 34675: /* InterColorProfile */
6080 case 50831: /* AsShotICCProfile */
6081 profile_offset = ftell(ifp);
6082 profile_length = len;
6083 break;
6084 case 37122: /* CompressedBitsPerPixel */
6085 kodak_cbpp = get4();
6086 break;
6087 case 37386: /* FocalLength */
6088 focal_len = getreal(type);
6089 break;
6090 case 37393: /* ImageNumber */
6091 shot_order = getint(type);
6092 break;
6093 case 37400: /* old Kodak KDC tag */
6094 for (raw_color = i=0; i < 3; i++) {
6095 getreal(type);
6096 FORC3 rgb_cam[i][c] = getreal(type);
6097 }
6098 break;
6099 case 40976:
6100 strip_offset = get4();
6101 switch (tiff_ifd[ifd].comp) {
6102 case 32770: load_raw = &CLASS samsung_load_raw; break;
6103 case 32772: load_raw = &CLASS samsung2_load_raw; break;
6104 case 32773: load_raw = &CLASS samsung3_load_raw; break;
6105 }
6106 break;
6107 case 46275: /* Imacon tags */
6108 strcpy (make, "Imacon");
6109 data_offset = ftell(ifp);
6110 ima_len = len;
6111 break;
6112 case 46279:
6113 if (!ima_len) break;
6114 fseek (ifp, 38, SEEK_CUR);
6115 case 46274:
6116 fseek (ifp, 40, SEEK_CUR);
6117 raw_width = get4();
6118 raw_height = get4();
6119 left_margin = get4() & 7;
6120 width = raw_width - left_margin - (get4() & 7);
6121 top_margin = get4() & 7;
6122 height = raw_height - top_margin - (get4() & 7);
6123 if (raw_width == 7262) {
6124 height = 5444;
6125 width = 7244;
6126 left_margin = 7;
6127 }
6128 fseek (ifp, 52, SEEK_CUR);
6129 FORC3 cam_mul[c] = getreal(11);
6130 fseek (ifp, 114, SEEK_CUR);
6131 flip = (get2() >> 7) * 90;
6132 if (width * height * 6 == ima_len) {
6133 if (flip % 180 == 90) SWAP(width,height);
6134 raw_width = width;
6135 raw_height = height;
6136 left_margin = top_margin = filters = flip = 0;
6137 }
6138 sprintf (model, "Ixpress %d-Mp", height*width/1000000);
6139 load_raw = &CLASS imacon_full_load_raw;
6140 if (filters) {
6141 if (left_margin & 1) filters = 0x61616161;
6142 load_raw = &CLASS unpacked_load_raw;
6143 }
6144 maximum = 0xffff;
6145 break;
6146 case 50454: /* Sinar tag */
6147 case 50455:
6148 if (!(cbuf = (char *) malloc(len))) break;
6149 fread (cbuf, 1, len, ifp);
6150 for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
6151 if (!strncmp (++cp,"Neutral ",8))
6152 sscanf (cp+8, "%f %f %f", cam_mul, cam_mul+1, cam_mul+2);
6153 free (cbuf);
6154 break;
6155 case 50458:
6156 if (!make[0]) strcpy (make, "Hasselblad");
6157 break;
6158 case 50459: /* Hasselblad tag */
6159 i = order;
6160 j = ftell(ifp);
6161 c = tiff_nifds;
6162 order = get2();
6163 fseek (ifp, j+(get2(),get4()), SEEK_SET);
6164 parse_tiff_ifd (j);
6165 maximum = 0xffff;
6166 tiff_nifds = c;
6167 order = i;
6168 break;
6169 case 50706: /* DNGVersion */
6170 FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
6171 if (!make[0]) strcpy (make, "DNG");
6172 is_raw = 1;
6173 break;
6174 case 50708: /* UniqueCameraModel */
6175 if (model[0]) break;
6176 fgets (make, 64, ifp);
6177 if ((cp = strchr(make,' '))) {
6178 strcpy(model,cp+1);
6179 *cp = 0;
6180 }
6181 break;
6182 case 50710: /* CFAPlaneColor */
6183 if (filters == 9) break;
6184 if (len > 4) len = 4;
6185 colors = len;
6186 fread (cfa_pc, 1, colors, ifp);
6187 guess_cfa_pc:
6188 FORCC tab[cfa_pc[c]] = c;
6189 cdesc[c] = 0;
6190 for (i=16; i--; )
6191 filters = filters << 2 | tab[cfa_pat[i % plen]];
6192 filters -= !filters;
6193 break;
6194 case 50711: /* CFALayout */
6195 if (get2() == 2) fuji_width = 1;
6196 break;
6197 case 291:
6198 case 50712: /* LinearizationTable */
6199 linear_table (len);
6200 break;
6201 case 50713: /* BlackLevelRepeatDim */
6202 cblack[4] = get2();
6203 cblack[5] = get2();
6204 if (cblack[4] * cblack[5] > sizeof cblack / sizeof *cblack - 6)
6205 cblack[4] = cblack[5] = 1;
6206 break;
6207 case 61450:
6208 cblack[4] = cblack[5] = MIN(sqrt(len),64);
6209 case 50714: /* BlackLevel */
6210 if (!(cblack[4] * cblack[5]))
6211 cblack[4] = cblack[5] = 1;
6212 FORC (cblack[4] * cblack[5])
6213 cblack[6+c] = getreal(type);
6214 black = 0;
6215 break;
6216 case 50715: /* BlackLevelDeltaH */
6217 case 50716: /* BlackLevelDeltaV */
6218 for (num=i=0; i < (len & 0xffff); i++)
6219 num += getreal(type);
6220 black += num/len + 0.5;
6221 break;
6222 case 50717: /* WhiteLevel */
6223 maximum = getint(type);
6224 break;
6225 case 50718: /* DefaultScale */
6226 pixel_aspect = getreal(type);
6227 pixel_aspect /= getreal(type);
6228 break;
6229 case 50721: /* ColorMatrix1 */
6230 case 50722: /* ColorMatrix2 */
6231 FORCC for (j=0; j < 3; j++)
6232 cm[c][j] = getreal(type);
6233 use_cm = 1;
6234 break;
6235 case 50723: /* CameraCalibration1 */
6236 case 50724: /* CameraCalibration2 */
6237 for (i=0; i < colors; i++)
6238 FORCC cc[i][c] = getreal(type);
6239 break;
6240 case 50727: /* AnalogBalance */
6241 FORCC ab[c] = getreal(type);
6242 break;
6243 case 50728: /* AsShotNeutral */
6244 FORCC asn[c] = getreal(type);
6245 break;
6246 case 50729: /* AsShotWhiteXY */
6247 xyz[0] = getreal(type);
6248 xyz[1] = getreal(type);
6249 xyz[2] = 1 - xyz[0] - xyz[1];
6250 FORC3 xyz[c] /= d65_white[c];
6251 break;
6252 case 50740: /* DNGPrivateData */
6253 if (dng_version) break;
6254 parse_minolta (j = get4()+base);
6255 fseek (ifp, j, SEEK_SET);
6256 parse_tiff_ifd (base);
6257 break;
6258 case 50752:
6259 read_shorts (cr2_slice, 3);
6260 break;
6261 case 50829: /* ActiveArea */
6262 top_margin = getint(type);
6263 left_margin = getint(type);
6264 height = getint(type) - top_margin;
6265 width = getint(type) - left_margin;
6266 break;
6267 case 50830: /* MaskedAreas */
6268 for (i=0; i < len && i < 32; i++)
6269 ((int *)mask)[i] = getint(type);
6270 black = 0;
6271 break;
6272 case 51009: /* OpcodeList2 */
6273 meta_offset = ftell(ifp);
6274 break;
6275 case 64772: /* Kodak P-series */
6276 if (len < 13) break;
6277 fseek (ifp, 16, SEEK_CUR);
6278 data_offset = get4();
6279 fseek (ifp, 28, SEEK_CUR);
6280 data_offset += get4();
6281 load_raw = &CLASS packed_load_raw;
6282 break;
6283 case 65026:
6284 if (type == 2) fgets (model2, 64, ifp);
6285 }
6286 fseek (ifp, save, SEEK_SET);
6287 }
6288 if (sony_length && (buf = (unsigned *) malloc(sony_length))) {
6289 fseek (ifp, sony_offset, SEEK_SET);
6290 fread (buf, sony_length, 1, ifp);
6291 sony_decrypt (buf, sony_length/4, 1, sony_key);
6292 sfp = ifp;
6293 if ((ifp = tmpfile())) {
6294 fwrite (buf, sony_length, 1, ifp);
6295 fseek (ifp, 0, SEEK_SET);
6296 parse_tiff_ifd (-sony_offset);
6297 fclose (ifp);
6298 }
6299 ifp = sfp;
6300 free (buf);
6301 }
6302 for (i=0; i < colors; i++)
6303 FORCC cc[i][c] *= ab[i];
6304 if (use_cm) {
6305 FORCC for (i=0; i < 3; i++)
6306 for (cam_xyz[c][i]=j=0; j < colors; j++)
6307 cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
6308 cam_xyz_coeff (cmatrix, cam_xyz);
6309 }
6310 if (asn[0]) {
6311 cam_mul[3] = 0;
6312 FORCC cam_mul[c] = 1 / asn[c];
6313 }
6314 if (!use_cm)
6315 FORCC pre_mul[c] /= cc[c][c];
6316 return 0;
6317 }
6318
parse_tiff(int base)6319 int CLASS parse_tiff (int base)
6320 {
6321 int doff;
6322
6323 fseek (ifp, base, SEEK_SET);
6324 order = get2();
6325 if (order != 0x4949 && order != 0x4d4d) return 0;
6326 get2();
6327 while ((doff = get4())) {
6328 fseek (ifp, doff+base, SEEK_SET);
6329 if (parse_tiff_ifd (base)) break;
6330 }
6331 return 1;
6332 }
6333
apply_tiff()6334 void CLASS apply_tiff()
6335 {
6336 int max_samp=0, ties=0, os, ns, raw=-1, thm=-1, i;
6337 struct jhead jh;
6338
6339 thumb_misc = 16;
6340 if (thumb_offset) {
6341 fseek (ifp, thumb_offset, SEEK_SET);
6342 if (ljpeg_start (&jh, 1)) {
6343 thumb_misc = jh.bits;
6344 thumb_width = jh.wide;
6345 thumb_height = jh.high;
6346 }
6347 }
6348 for (i=tiff_nifds; i--; ) {
6349 if (tiff_ifd[i].shutter)
6350 shutter = tiff_ifd[i].shutter;
6351 tiff_ifd[i].shutter = shutter;
6352 }
6353 for (i=0; i < tiff_nifds; i++) {
6354 if (max_samp < tiff_ifd[i].samples)
6355 max_samp = tiff_ifd[i].samples;
6356 if (max_samp > 3) max_samp = 3;
6357 os = raw_width*raw_height;
6358 ns = tiff_ifd[i].width*tiff_ifd[i].height;
6359 if (tiff_bps) {
6360 os *= tiff_bps;
6361 ns *= tiff_ifd[i].bps;
6362 }
6363 if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
6364 (tiff_ifd[i].width | tiff_ifd[i].height) < 0x10000 &&
6365 ns && ((ns > os && (ties = 1)) ||
6366 (ns == os && shot_select == ties++))) {
6367 raw_width = tiff_ifd[i].width;
6368 raw_height = tiff_ifd[i].height;
6369 tiff_bps = tiff_ifd[i].bps;
6370 tiff_compress = tiff_ifd[i].comp;
6371 data_offset = tiff_ifd[i].offset;
6372 tiff_flip = tiff_ifd[i].flip;
6373 tiff_samples = tiff_ifd[i].samples;
6374 tile_width = tiff_ifd[i].tile_width;
6375 tile_length = tiff_ifd[i].tile_length;
6376 shutter = tiff_ifd[i].shutter;
6377 raw = i;
6378 }
6379 }
6380 if (is_raw == 1 && ties) is_raw = ties;
6381 if (!tile_width ) tile_width = INT_MAX;
6382 if (!tile_length) tile_length = INT_MAX;
6383 for (i=tiff_nifds; i--; )
6384 if (tiff_ifd[i].flip) tiff_flip = tiff_ifd[i].flip;
6385 if (raw >= 0 && !load_raw)
6386 switch (tiff_compress) {
6387 case 32767:
6388 if (tiff_ifd[raw].bytes == raw_width*raw_height) {
6389 tiff_bps = 12;
6390 maximum = 4095;
6391 load_raw = &CLASS sony_arw2_load_raw; break;
6392 }
6393 if (tiff_ifd[raw].bytes*8 != raw_width*raw_height*tiff_bps) {
6394 raw_height += 8;
6395 load_raw = &CLASS sony_arw_load_raw; break;
6396 }
6397 load_flags = 79;
6398 case 32769:
6399 load_flags++;
6400 case 32770:
6401 case 32773: goto slr;
6402 case 0: case 1:
6403 if (!strncmp(make,"OLYMPUS",7) &&
6404 tiff_ifd[raw].bytes*2 == raw_width*raw_height*3)
6405 load_flags = 24;
6406 if (!strcmp(make,"SONY") && tiff_bps < 14 &&
6407 tiff_ifd[raw].bytes == raw_width*raw_height*2)
6408 tiff_bps = 14;
6409 if (tiff_ifd[raw].bytes*5 == raw_width*raw_height*8) {
6410 load_flags = 81;
6411 tiff_bps = 12;
6412 } slr:
6413 switch (tiff_bps) {
6414 case 8: load_raw = &CLASS eight_bit_load_raw; break;
6415 case 12: if (tiff_ifd[raw].phint == 2)
6416 load_flags = 6;
6417 load_raw = &CLASS packed_load_raw; break;
6418 case 14: load_raw = &CLASS packed_load_raw;
6419 if (tiff_ifd[raw].bytes*4 == raw_width*raw_height*7) break;
6420 load_flags = 0;
6421 case 16: load_raw = &CLASS unpacked_load_raw;
6422 if (!strncmp(make,"OLYMPUS",7) &&
6423 tiff_ifd[raw].bytes*7 > raw_width*raw_height)
6424 load_raw = &CLASS olympus_load_raw;
6425 }
6426 if (filters == 9 && tiff_ifd[raw].bytes*8 < raw_width*raw_height*tiff_bps)
6427 load_raw = &CLASS fuji_xtrans_load_raw;
6428 break;
6429 case 6: case 7: case 99:
6430 load_raw = &CLASS lossless_jpeg_load_raw; break;
6431 case 262:
6432 load_raw = &CLASS kodak_262_load_raw; break;
6433 case 34713:
6434 if ((raw_width+9)/10*16*raw_height == tiff_ifd[raw].bytes) {
6435 load_raw = &CLASS packed_load_raw;
6436 load_flags = 1;
6437 } else if (raw_width*raw_height*3 == tiff_ifd[raw].bytes*2) {
6438 load_raw = &CLASS packed_load_raw;
6439 if (model[0] == 'N') load_flags = 80;
6440 } else if (raw_width*raw_height*3 == tiff_ifd[raw].bytes) {
6441 load_raw = &CLASS nikon_yuv_load_raw;
6442 gamma_curve (1/2.4, 12.92, 1, 4095);
6443 memset (cblack, 0, sizeof cblack);
6444 filters = 0;
6445 } else if (raw_width*raw_height*2 == tiff_ifd[raw].bytes) {
6446 load_raw = &CLASS unpacked_load_raw;
6447 load_flags = 4;
6448 order = 0x4d4d;
6449 } else
6450 load_raw = &CLASS nikon_load_raw; break;
6451 case 65535:
6452 load_raw = &CLASS pentax_load_raw; break;
6453 case 65000:
6454 switch (tiff_ifd[raw].phint) {
6455 case 2: load_raw = &CLASS kodak_rgb_load_raw; filters = 0; break;
6456 case 6: load_raw = &CLASS kodak_ycbcr_load_raw; filters = 0; break;
6457 case 32803: load_raw = &CLASS kodak_65000_load_raw;
6458 }
6459 case 32867: case 34892: break;
6460 default: is_raw = 0;
6461 }
6462 if (!dng_version)
6463 if ( (tiff_samples == 3 && tiff_ifd[raw].bytes && tiff_bps != 14 &&
6464 (tiff_compress & -16) != 32768)
6465 || (tiff_bps == 8 && strncmp(make,"Phase",5) &&
6466 !strcasestr(make,"Kodak") && !strstr(model2,"DEBUG RAW")))
6467 is_raw = 0;
6468 for (i=0; i < tiff_nifds; i++)
6469 if (i != raw && tiff_ifd[i].samples == max_samp &&
6470 tiff_ifd[i].width * tiff_ifd[i].height / (SQR(tiff_ifd[i].bps)+1) >
6471 thumb_width * thumb_height / (SQR(thumb_misc)+1)
6472 && tiff_ifd[i].comp != 34892) {
6473 thumb_width = tiff_ifd[i].width;
6474 thumb_height = tiff_ifd[i].height;
6475 thumb_offset = tiff_ifd[i].offset;
6476 thumb_length = tiff_ifd[i].bytes;
6477 thumb_misc = tiff_ifd[i].bps;
6478 thm = i;
6479 }
6480 if (thm >= 0) {
6481 thumb_misc |= tiff_ifd[thm].samples << 5;
6482 switch (tiff_ifd[thm].comp) {
6483 case 0:
6484 write_thumb = &CLASS layer_thumb;
6485 break;
6486 case 1:
6487 if (tiff_ifd[thm].bps <= 8)
6488 write_thumb = &CLASS ppm_thumb;
6489 else if (!strcmp(make,"Imacon"))
6490 write_thumb = &CLASS ppm16_thumb;
6491 else
6492 thumb_load_raw = &CLASS kodak_thumb_load_raw;
6493 break;
6494 case 65000:
6495 thumb_load_raw = tiff_ifd[thm].phint == 6 ?
6496 &CLASS kodak_ycbcr_load_raw : &CLASS kodak_rgb_load_raw;
6497 }
6498 }
6499 }
6500
parse_minolta(int base)6501 void CLASS parse_minolta (int base)
6502 {
6503 int save, tag, len, offset, high=0, wide=0, i, c;
6504 short sorder=order;
6505
6506 fseek (ifp, base, SEEK_SET);
6507 if (fgetc(ifp) || fgetc(ifp)-'M' || fgetc(ifp)-'R') return;
6508 order = fgetc(ifp) * 0x101;
6509 offset = base + get4() + 8;
6510 while ((save=ftell(ifp)) < offset) {
6511 for (tag=i=0; i < 4; i++)
6512 tag = tag << 8 | fgetc(ifp);
6513 len = get4();
6514 switch (tag) {
6515 case 0x505244: /* PRD */
6516 fseek (ifp, 8, SEEK_CUR);
6517 high = get2();
6518 wide = get2();
6519 break;
6520 case 0x574247: /* WBG */
6521 get4();
6522 i = strcmp(model,"DiMAGE A200") ? 0:3;
6523 FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
6524 break;
6525 case 0x545457: /* TTW */
6526 parse_tiff (ftell(ifp));
6527 data_offset = offset;
6528 }
6529 fseek (ifp, save+len+8, SEEK_SET);
6530 }
6531 raw_height = high;
6532 raw_width = wide;
6533 order = sorder;
6534 }
6535
6536 /*
6537 Many cameras have a "debug mode" that writes JPEG and raw
6538 at the same time. The raw file has no header, so try to
6539 to open the matching JPEG file and read its metadata.
6540 */
parse_external_jpeg()6541 void CLASS parse_external_jpeg()
6542 {
6543 const char *file, *ext;
6544 char *jname, *jfile, *jext;
6545 FILE *save=ifp;
6546
6547 ext = strrchr (ifname, '.');
6548 file = strrchr (ifname, '/');
6549 if (!file) file = strrchr (ifname, '\\');
6550 if (!file) file = ifname-1;
6551 file++;
6552 if (!ext || strlen(ext) != 4 || ext-file != 8) return;
6553 jname = (char *) malloc (strlen(ifname) + 1);
6554 merror (jname, "parse_external_jpeg()");
6555 strcpy (jname, ifname);
6556 jfile = file - ifname + jname;
6557 jext = ext - ifname + jname;
6558 if (strcasecmp (ext, ".jpg")) {
6559 strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
6560 if (isdigit(*file)) {
6561 memcpy (jfile, file+4, 4);
6562 memcpy (jfile+4, file, 4);
6563 }
6564 } else
6565 while (isdigit(*--jext)) {
6566 if (*jext != '9') {
6567 (*jext)++;
6568 break;
6569 }
6570 *jext = '0';
6571 }
6572 if (strcmp (jname, ifname)) {
6573 if ((ifp = fopen (jname, "rb"))) {
6574 if (verbose)
6575 fprintf (stderr,_("Reading metadata from %s ...\n"), jname);
6576 parse_tiff (12);
6577 thumb_offset = 0;
6578 is_raw = 1;
6579 fclose (ifp);
6580 }
6581 }
6582 if (!timestamp)
6583 fprintf (stderr,_("Failed to read metadata from %s\n"), jname);
6584 free (jname);
6585 ifp = save;
6586 }
6587
6588 /*
6589 CIFF block 0x1030 contains an 8x8 white sample.
6590 Load this into white[][] for use in scale_colors().
6591 */
ciff_block_1030()6592 void CLASS ciff_block_1030()
6593 {
6594 static const ushort key[] = { 0x410, 0x45f3 };
6595 int i, bpp, row, col, vbits=0;
6596 unsigned long bitbuf=0;
6597
6598 if ((get2(),get4()) != 0x80008 || !get4()) return;
6599 bpp = get2();
6600 if (bpp != 10 && bpp != 12) return;
6601 for (i=row=0; row < 8; row++)
6602 for (col=0; col < 8; col++) {
6603 if (vbits < bpp) {
6604 bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
6605 vbits += 16;
6606 }
6607 white[row][col] = bitbuf >> (vbits -= bpp) & ~(-1 << bpp);
6608 }
6609 }
6610
6611 /*
6612 Parse a CIFF file, better known as Canon CRW format.
6613 */
parse_ciff(int offset,int length,int depth)6614 void CLASS parse_ciff (int offset, int length, int depth)
6615 {
6616 int tboff, nrecs, c, type, len, save, wbi=-1;
6617 ushort key[] = { 0x410, 0x45f3 };
6618
6619 fseek (ifp, offset+length-4, SEEK_SET);
6620 tboff = get4() + offset;
6621 fseek (ifp, tboff, SEEK_SET);
6622 nrecs = get2();
6623 if ((nrecs | depth) > 127) return;
6624 while (nrecs--) {
6625 type = get2();
6626 len = get4();
6627 save = ftell(ifp) + 4;
6628 fseek (ifp, offset+get4(), SEEK_SET);
6629 if ((((type >> 8) + 8) | 8) == 0x38)
6630 parse_ciff (ftell(ifp), len, depth+1); /* Parse a sub-table */
6631 if (type == 0x0810)
6632 fread (artist, 64, 1, ifp);
6633 if (type == 0x080a) {
6634 fread (make, 64, 1, ifp);
6635 fseek (ifp, strlen(make) - 63, SEEK_CUR);
6636 fread (model, 64, 1, ifp);
6637 }
6638 if (type == 0x1810) {
6639 width = get4();
6640 height = get4();
6641 pixel_aspect = int_to_float(get4());
6642 flip = get4();
6643 }
6644 if (type == 0x1835) /* Get the decoder table */
6645 tiff_compress = get4();
6646 if (type == 0x2007) {
6647 thumb_offset = ftell(ifp);
6648 thumb_length = len;
6649 }
6650 if (type == 0x1818) {
6651 shutter = pow (2, -int_to_float((get4(),get4())));
6652 aperture = pow (2, int_to_float(get4())/2);
6653 }
6654 if (type == 0x102a) {
6655 iso_speed = pow (2, (get4(),get2())/32.0 - 4) * 50;
6656 aperture = pow (2, (get2(),(short)get2())/64.0);
6657 shutter = pow (2,-((short)get2())/32.0);
6658 wbi = (get2(),get2());
6659 if (wbi > 17) wbi = 0;
6660 fseek (ifp, 32, SEEK_CUR);
6661 if (shutter > 1e6) shutter = get2()/10.0;
6662 }
6663 if (type == 0x102c) {
6664 if (get2() > 512) { /* Pro90, G1 */
6665 fseek (ifp, 118, SEEK_CUR);
6666 FORC4 cam_mul[c ^ 2] = get2();
6667 } else { /* G2, S30, S40 */
6668 fseek (ifp, 98, SEEK_CUR);
6669 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2();
6670 }
6671 }
6672 if (type == 0x0032) {
6673 if (len == 768) { /* EOS D30 */
6674 fseek (ifp, 72, SEEK_CUR);
6675 FORC4 cam_mul[c ^ (c >> 1)] = 1024.0 / get2();
6676 if (!wbi) cam_mul[0] = -1; /* use my auto white balance */
6677 } else if (!cam_mul[0]) {
6678 if (get2() == key[0]) /* Pro1, G6, S60, S70 */
6679 c = (strstr(model,"Pro1") ?
6680 "012346000000000000":"01345:000000006008")[wbi]-'0'+ 2;
6681 else { /* G3, G5, S45, S50 */
6682 c = "023457000000006000"[wbi]-'0';
6683 key[0] = key[1] = 0;
6684 }
6685 fseek (ifp, 78 + c*8, SEEK_CUR);
6686 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2() ^ key[c & 1];
6687 if (!wbi) cam_mul[0] = -1;
6688 }
6689 }
6690 if (type == 0x10a9) { /* D60, 10D, 300D, and clones */
6691 if (len > 66) wbi = "0134567028"[wbi]-'0';
6692 fseek (ifp, 2 + wbi*8, SEEK_CUR);
6693 FORC4 cam_mul[c ^ (c >> 1)] = get2();
6694 }
6695 if (type == 0x1030 && (0x18040 >> wbi & 1))
6696 ciff_block_1030(); /* all that don't have 0x10a9 */
6697 if (type == 0x1031) {
6698 raw_width = (get2(),get2());
6699 raw_height = get2();
6700 }
6701 if (type == 0x5029) {
6702 focal_len = len >> 16;
6703 if ((len & 0xffff) == 2) focal_len /= 32;
6704 }
6705 if (type == 0x5813) flash_used = int_to_float(len);
6706 if (type == 0x5814) canon_ev = int_to_float(len);
6707 if (type == 0x5817) shot_order = len;
6708 if (type == 0x5834) unique_id = len;
6709 if (type == 0x580e) timestamp = len;
6710 if (type == 0x180e) timestamp = get4();
6711 #ifdef LOCALTIME
6712 if ((type | 0x4000) == 0x580e)
6713 timestamp = mktime (gmtime (×tamp));
6714 #endif
6715 fseek (ifp, save, SEEK_SET);
6716 }
6717 }
6718
parse_rollei()6719 void CLASS parse_rollei()
6720 {
6721 char line[128], *val;
6722 struct tm t;
6723
6724 fseek (ifp, 0, SEEK_SET);
6725 memset (&t, 0, sizeof t);
6726 do {
6727 fgets (line, 128, ifp);
6728 if ((val = strchr(line,'=')))
6729 *val++ = 0;
6730 else
6731 val = line + strlen(line);
6732 if (!strcmp(line,"DAT"))
6733 sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
6734 if (!strcmp(line,"TIM"))
6735 sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
6736 if (!strcmp(line,"HDR"))
6737 thumb_offset = atoi(val);
6738 if (!strcmp(line,"X "))
6739 raw_width = atoi(val);
6740 if (!strcmp(line,"Y "))
6741 raw_height = atoi(val);
6742 if (!strcmp(line,"TX "))
6743 thumb_width = atoi(val);
6744 if (!strcmp(line,"TY "))
6745 thumb_height = atoi(val);
6746 } while (strncmp(line,"EOHD",4));
6747 data_offset = thumb_offset + thumb_width * thumb_height * 2;
6748 t.tm_year -= 1900;
6749 t.tm_mon -= 1;
6750 if (mktime(&t) > 0)
6751 timestamp = mktime(&t);
6752 strcpy (make, "Rollei");
6753 strcpy (model,"d530flex");
6754 write_thumb = &CLASS rollei_thumb;
6755 }
6756
parse_sinar_ia()6757 void CLASS parse_sinar_ia()
6758 {
6759 int entries, off;
6760 char str[8], *cp;
6761
6762 order = 0x4949;
6763 fseek (ifp, 4, SEEK_SET);
6764 entries = get4();
6765 fseek (ifp, get4(), SEEK_SET);
6766 while (entries--) {
6767 off = get4(); get4();
6768 fread (str, 8, 1, ifp);
6769 if (!strcmp(str,"META")) meta_offset = off;
6770 if (!strcmp(str,"THUMB")) thumb_offset = off;
6771 if (!strcmp(str,"RAW0")) data_offset = off;
6772 }
6773 fseek (ifp, meta_offset+20, SEEK_SET);
6774 fread (make, 64, 1, ifp);
6775 make[63] = 0;
6776 if ((cp = strchr(make,' '))) {
6777 strcpy (model, cp+1);
6778 *cp = 0;
6779 }
6780 raw_width = get2();
6781 raw_height = get2();
6782 load_raw = &CLASS unpacked_load_raw;
6783 thumb_width = (get4(),get2());
6784 thumb_height = get2();
6785 write_thumb = &CLASS ppm_thumb;
6786 maximum = 0x3fff;
6787 }
6788
parse_phase_one(int base)6789 void CLASS parse_phase_one (int base)
6790 {
6791 unsigned entries, tag, type, len, data, save, i, c;
6792 float romm_cam[3][3];
6793 char *cp;
6794
6795 memset (&ph1, 0, sizeof ph1);
6796 fseek (ifp, base, SEEK_SET);
6797 order = get4() & 0xffff;
6798 if (get4() >> 8 != 0x526177) return; /* "Raw" */
6799 fseek (ifp, get4()+base, SEEK_SET);
6800 entries = get4();
6801 get4();
6802 while (entries--) {
6803 tag = get4();
6804 type = get4();
6805 len = get4();
6806 data = get4();
6807 save = ftell(ifp);
6808 fseek (ifp, base+data, SEEK_SET);
6809 switch (tag) {
6810 case 0x100: flip = "0653"[data & 3]-'0'; break;
6811 case 0x106:
6812 for (i=0; i < 9; i++)
6813 ((float *)romm_cam)[i] = getreal(11);
6814 romm_coeff (romm_cam);
6815 break;
6816 case 0x107:
6817 FORC3 cam_mul[c] = getreal(11);
6818 break;
6819 case 0x108: raw_width = data; break;
6820 case 0x109: raw_height = data; break;
6821 case 0x10a: left_margin = data; break;
6822 case 0x10b: top_margin = data; break;
6823 case 0x10c: width = data; break;
6824 case 0x10d: height = data; break;
6825 case 0x10e: ph1.format = data; break;
6826 case 0x10f: data_offset = data+base; break;
6827 case 0x110: meta_offset = data+base;
6828 meta_length = len; break;
6829 case 0x112: ph1.key_off = save - 4; break;
6830 case 0x210: ph1.tag_210 = int_to_float(data); break;
6831 case 0x21a: ph1.tag_21a = data; break;
6832 case 0x21c: strip_offset = data+base; break;
6833 case 0x21d: ph1.black = data; break;
6834 case 0x222: ph1.split_col = data; break;
6835 case 0x223: ph1.black_col = data+base; break;
6836 case 0x224: ph1.split_row = data; break;
6837 case 0x225: ph1.black_row = data+base; break;
6838 case 0x301:
6839 model[63] = 0;
6840 fread (model, 1, 63, ifp);
6841 if ((cp = strstr(model," camera"))) *cp = 0;
6842 }
6843 fseek (ifp, save, SEEK_SET);
6844 }
6845 load_raw = ph1.format < 3 ?
6846 &CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
6847 maximum = 0xffff;
6848 strcpy (make, "Phase One");
6849 if (model[0]) return;
6850 switch (raw_height) {
6851 case 2060: strcpy (model,"LightPhase"); break;
6852 case 2682: strcpy (model,"H 10"); break;
6853 case 4128: strcpy (model,"H 20"); break;
6854 case 5488: strcpy (model,"H 25"); break;
6855 }
6856 }
6857
parse_fuji(int offset)6858 void CLASS parse_fuji (int offset)
6859 {
6860 unsigned entries, tag, len, save, c;
6861
6862 fseek (ifp, offset, SEEK_SET);
6863 entries = get4();
6864 if (entries > 255) return;
6865 while (entries--) {
6866 tag = get2();
6867 len = get2();
6868 save = ftell(ifp);
6869 if (tag == 0x100) {
6870 raw_height = get2();
6871 raw_width = get2();
6872 } else if (tag == 0x121) {
6873 height = get2();
6874 if ((width = get2()) == 4284) width += 3;
6875 } else if (tag == 0x130) {
6876 fuji_layout = fgetc(ifp) >> 7;
6877 fuji_width = !(fgetc(ifp) & 8);
6878 } else if (tag == 0x131) {
6879 filters = 9;
6880 FORC(36) xtrans_abs[0][35-c] = fgetc(ifp) & 3;
6881 } else if (tag == 0x2ff0) {
6882 FORC4 cam_mul[c ^ 1] = get2();
6883 } else if (tag == 0xc000 && len > 20000) {
6884 c = order;
6885 order = 0x4949;
6886 while ((tag = get4()) > raw_width);
6887 width = tag;
6888 height = get4();
6889 order = c;
6890 }
6891 fseek (ifp, save+len, SEEK_SET);
6892 }
6893 height <<= fuji_layout;
6894 width >>= fuji_layout;
6895 }
6896
parse_jpeg(int offset)6897 int CLASS parse_jpeg (int offset)
6898 {
6899 int len, save, hlen, mark;
6900
6901 fseek (ifp, offset, SEEK_SET);
6902 if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
6903
6904 while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
6905 order = 0x4d4d;
6906 len = get2() - 2;
6907 save = ftell(ifp);
6908 if (mark == 0xc0 || mark == 0xc3 || mark == 0xc9) {
6909 fgetc(ifp);
6910 raw_height = get2();
6911 raw_width = get2();
6912 }
6913 order = get2();
6914 hlen = get4();
6915 if (get4() == 0x48454150) /* "HEAP" */
6916 parse_ciff (save+hlen, len-hlen, 0);
6917 if (parse_tiff (save+6)) apply_tiff();
6918 fseek (ifp, save+len, SEEK_SET);
6919 }
6920 return 1;
6921 }
6922
parse_riff()6923 void CLASS parse_riff()
6924 {
6925 unsigned i, size, end;
6926 char tag[4], date[64], month[64];
6927 static const char mon[12][4] =
6928 { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
6929 struct tm t;
6930
6931 order = 0x4949;
6932 fread (tag, 4, 1, ifp);
6933 size = get4();
6934 end = ftell(ifp) + size;
6935 if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
6936 get4();
6937 while (ftell(ifp)+7 < end && !feof(ifp))
6938 parse_riff();
6939 } else if (!memcmp(tag,"nctg",4)) {
6940 while (ftell(ifp)+7 < end) {
6941 i = get2();
6942 size = get2();
6943 if ((i+1) >> 1 == 10 && size == 20)
6944 get_timestamp(0);
6945 else fseek (ifp, size, SEEK_CUR);
6946 }
6947 } else if (!memcmp(tag,"IDIT",4) && size < 64) {
6948 fread (date, 64, 1, ifp);
6949 date[size] = 0;
6950 memset (&t, 0, sizeof t);
6951 if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
6952 &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
6953 for (i=0; i < 12 && strcasecmp(mon[i],month); i++);
6954 t.tm_mon = i;
6955 t.tm_year -= 1900;
6956 if (mktime(&t) > 0)
6957 timestamp = mktime(&t);
6958 }
6959 } else
6960 fseek (ifp, size, SEEK_CUR);
6961 }
6962
parse_crx(int end)6963 void CLASS parse_crx (int end)
6964 {
6965 unsigned i, save, size, tag, base;
6966 static int index=0, wide, high, off, len;
6967
6968 order = 0x4d4d;
6969 while (ftell(ifp)+7 < end) {
6970 save = ftell(ifp);
6971 if ((size = get4()) < 8) break;
6972 switch (tag = get4()) {
6973 case 0x6d6f6f76: /* moov */
6974 case 0x7472616b: /* trak */
6975 case 0x6d646961: /* mdia */
6976 case 0x6d696e66: /* minf */
6977 case 0x7374626c: /* stbl */
6978 parse_crx (save+size);
6979 break;
6980 case 0x75756964: /* uuid */
6981 switch (i=get4()) {
6982 case 0xeaf42b5e: fseek (ifp, 8, SEEK_CUR);
6983 case 0x85c0b687: fseek (ifp, 12, SEEK_CUR);
6984 parse_crx (save+size);
6985 }
6986 break;
6987 case 0x434d5431: /* CMT1 */
6988 case 0x434d5432: /* CMT2 */
6989 base = ftell(ifp);
6990 order = get2();
6991 fseek (ifp, 6, SEEK_CUR);
6992 tag & 1 ? parse_tiff_ifd (base) : parse_exif (base);
6993 order = 0x4d4d;
6994 break;
6995 case 0x746b6864: /* tkhd */
6996 fseek (ifp, 12, SEEK_CUR);
6997 index = get4();
6998 fseek (ifp, 58, SEEK_CUR);
6999 wide = get4();
7000 high = get4();
7001 break;
7002 case 0x7374737a: /* stsz */
7003 len = (get4(),get4());
7004 break;
7005 case 0x636f3634: /* co64 */
7006 fseek (ifp, 12, SEEK_CUR);
7007 off = get4();
7008 switch (index) {
7009 case 1: /* 1 = full size, 2 = 27% size */
7010 thumb_width = wide;
7011 thumb_height = high;
7012 thumb_length = len;
7013 thumb_offset = off;
7014 break;
7015 case 3:
7016 raw_width = wide;
7017 raw_height = high;
7018 data_offset = off;
7019 load_raw = &CLASS canon_crx_load_raw;
7020 }
7021 break;
7022 case 0x50525657: /* PRVW */
7023 fseek (ifp, 6, SEEK_CUR);
7024 }
7025 fseek (ifp, save+size, SEEK_SET);
7026 }
7027 }
7028
parse_qt(int end)7029 void CLASS parse_qt (int end)
7030 {
7031 unsigned save, size;
7032 char tag[4];
7033
7034 order = 0x4d4d;
7035 while (ftell(ifp)+7 < end) {
7036 save = ftell(ifp);
7037 if ((size = get4()) < 8) return;
7038 fread (tag, 4, 1, ifp);
7039 if (!memcmp(tag,"moov",4) ||
7040 !memcmp(tag,"udta",4) ||
7041 !memcmp(tag,"CNTH",4))
7042 parse_qt (save+size);
7043 if (!memcmp(tag,"CNDA",4))
7044 parse_jpeg (ftell(ifp));
7045 fseek (ifp, save+size, SEEK_SET);
7046 }
7047 }
7048
parse_smal(int offset,int fsize)7049 void CLASS parse_smal (int offset, int fsize)
7050 {
7051 int ver;
7052
7053 fseek (ifp, offset+2, SEEK_SET);
7054 order = 0x4949;
7055 ver = fgetc(ifp);
7056 if (ver == 6)
7057 fseek (ifp, 5, SEEK_CUR);
7058 if (get4() != fsize) return;
7059 if (ver > 6) data_offset = get4();
7060 raw_height = height = get2();
7061 raw_width = width = get2();
7062 strcpy (make, "SMaL");
7063 sprintf (model, "v%d %dx%d", ver, width, height);
7064 if (ver == 6) load_raw = &CLASS smal_v6_load_raw;
7065 if (ver == 9) load_raw = &CLASS smal_v9_load_raw;
7066 }
7067
parse_cine()7068 void CLASS parse_cine()
7069 {
7070 unsigned off_head, off_setup, off_image, i;
7071
7072 order = 0x4949;
7073 fseek (ifp, 4, SEEK_SET);
7074 is_raw = get2() == 2;
7075 fseek (ifp, 14, SEEK_CUR);
7076 is_raw *= get4();
7077 off_head = get4();
7078 off_setup = get4();
7079 off_image = get4();
7080 timestamp = get4();
7081 if ((i = get4())) timestamp = i;
7082 fseek (ifp, off_head+4, SEEK_SET);
7083 raw_width = get4();
7084 raw_height = get4();
7085 switch (get2(),get2()) {
7086 case 8: load_raw = &CLASS eight_bit_load_raw; break;
7087 case 16: load_raw = &CLASS unpacked_load_raw;
7088 }
7089 fseek (ifp, off_setup+792, SEEK_SET);
7090 strcpy (make, "CINE");
7091 sprintf (model, "%d", get4());
7092 fseek (ifp, 12, SEEK_CUR);
7093 switch ((i=get4()) & 0xffffff) {
7094 case 3: filters = 0x94949494; break;
7095 case 4: filters = 0x49494949; break;
7096 default: is_raw = 0;
7097 }
7098 fseek (ifp, 72, SEEK_CUR);
7099 switch ((get4()+3600) % 360) {
7100 case 270: flip = 4; break;
7101 case 180: flip = 1; break;
7102 case 90: flip = 7; break;
7103 case 0: flip = 2;
7104 }
7105 cam_mul[0] = getreal(11);
7106 cam_mul[2] = getreal(11);
7107 maximum = ~(-1 << get4());
7108 fseek (ifp, 668, SEEK_CUR);
7109 shutter = get4()/1000000000.0;
7110 fseek (ifp, off_image, SEEK_SET);
7111 if (shot_select < is_raw)
7112 fseek (ifp, shot_select*8, SEEK_CUR);
7113 data_offset = (INT64) get4() + 8;
7114 data_offset += (INT64) get4() << 32;
7115 }
7116
parse_redcine()7117 void CLASS parse_redcine()
7118 {
7119 unsigned i, len, rdvo;
7120
7121 order = 0x4d4d;
7122 is_raw = 0;
7123 fseek (ifp, 52, SEEK_SET);
7124 width = get4();
7125 height = get4();
7126 fseek (ifp, 0, SEEK_END);
7127 fseek (ifp, -(i = ftello(ifp) & 511), SEEK_CUR);
7128 if (get4() != i || get4() != 0x52454f42) {
7129 fprintf (stderr,_("%s: Tail is missing, parsing from head...\n"), ifname);
7130 fseek (ifp, 0, SEEK_SET);
7131 while ((len = get4()) != EOF) {
7132 if (get4() == 0x52454456)
7133 if (is_raw++ == shot_select)
7134 data_offset = ftello(ifp) - 8;
7135 fseek (ifp, len-8, SEEK_CUR);
7136 }
7137 } else {
7138 rdvo = get4();
7139 fseek (ifp, 12, SEEK_CUR);
7140 is_raw = get4();
7141 fseeko (ifp, rdvo+8 + shot_select*4, SEEK_SET);
7142 data_offset = get4();
7143 }
7144 }
7145
foveon_gets(int offset,char * str,int len)7146 char * CLASS foveon_gets (int offset, char *str, int len)
7147 {
7148 int i;
7149 fseek (ifp, offset, SEEK_SET);
7150 for (i=0; i < len-1; i++)
7151 if ((str[i] = get2()) == 0) break;
7152 str[i] = 0;
7153 return str;
7154 }
7155
parse_foveon()7156 void CLASS parse_foveon()
7157 {
7158 int entries, img=0, off, len, tag, save, i, wide, high, pent, poff[256][2];
7159 char name[64], value[64];
7160
7161 order = 0x4949; /* Little-endian */
7162 fseek (ifp, 36, SEEK_SET);
7163 flip = get4();
7164 fseek (ifp, -4, SEEK_END);
7165 fseek (ifp, get4(), SEEK_SET);
7166 if (get4() != 0x64434553) return; /* SECd */
7167 entries = (get4(),get4());
7168 while (entries--) {
7169 off = get4();
7170 len = get4();
7171 tag = get4();
7172 save = ftell(ifp);
7173 fseek (ifp, off, SEEK_SET);
7174 if (get4() != (0x20434553 | (tag << 24))) return;
7175 switch (tag) {
7176 case 0x47414d49: /* IMAG */
7177 case 0x32414d49: /* IMA2 */
7178 fseek (ifp, 8, SEEK_CUR);
7179 pent = get4();
7180 wide = get4();
7181 high = get4();
7182 if (wide > raw_width && high > raw_height) {
7183 switch (pent) {
7184 case 5: load_flags = 1;
7185 case 6: load_raw = &CLASS foveon_sd_load_raw; break;
7186 case 30: load_raw = &CLASS foveon_dp_load_raw; break;
7187 default: load_raw = 0;
7188 }
7189 raw_width = wide;
7190 raw_height = high;
7191 data_offset = off+28;
7192 is_foveon = 1;
7193 }
7194 fseek (ifp, off+28, SEEK_SET);
7195 if (fgetc(ifp) == 0xff && fgetc(ifp) == 0xd8
7196 && thumb_length < len-28) {
7197 thumb_offset = off+28;
7198 thumb_length = len-28;
7199 write_thumb = &CLASS jpeg_thumb;
7200 }
7201 if (++img == 2 && !thumb_length) {
7202 thumb_offset = off+24;
7203 thumb_width = wide;
7204 thumb_height = high;
7205 write_thumb = &CLASS foveon_thumb;
7206 }
7207 break;
7208 case 0x464d4143: /* CAMF */
7209 meta_offset = off+8;
7210 meta_length = len-28;
7211 break;
7212 case 0x504f5250: /* PROP */
7213 pent = (get4(),get4());
7214 fseek (ifp, 12, SEEK_CUR);
7215 off += pent*8 + 24;
7216 if ((unsigned) pent > 256) pent=256;
7217 for (i=0; i < pent*2; i++)
7218 ((int *)poff)[i] = off + get4()*2;
7219 for (i=0; i < pent; i++) {
7220 foveon_gets (poff[i][0], name, 64);
7221 foveon_gets (poff[i][1], value, 64);
7222 if (!strcmp (name, "ISO"))
7223 iso_speed = atoi(value);
7224 if (!strcmp (name, "CAMMANUF"))
7225 strcpy (make, value);
7226 if (!strcmp (name, "CAMMODEL"))
7227 strcpy (model, value);
7228 if (!strcmp (name, "WB_DESC"))
7229 strcpy (model2, value);
7230 if (!strcmp (name, "TIME"))
7231 timestamp = atoi(value);
7232 if (!strcmp (name, "EXPTIME"))
7233 shutter = atoi(value) / 1000000.0;
7234 if (!strcmp (name, "APERTURE"))
7235 aperture = atof(value);
7236 if (!strcmp (name, "FLENGTH"))
7237 focal_len = atof(value);
7238 }
7239 #ifdef LOCALTIME
7240 timestamp = mktime (gmtime (×tamp));
7241 #endif
7242 }
7243 fseek (ifp, save, SEEK_SET);
7244 }
7245 }
7246
7247 /*
7248 All matrices are from Adobe DNG Converter unless otherwise noted.
7249 */
adobe_coeff(const char * make,const char * model)7250 void CLASS adobe_coeff (const char *make, const char *model)
7251 {
7252 static const struct {
7253 const char *prefix;
7254 short black, maximum, trans[12];
7255 } table[] = {
7256 { "AgfaPhoto DC-833m", 0, 0, /* DJC */
7257 { 11438,-3762,-1115,-2409,9914,2497,-1227,2295,5300 } },
7258 { "Apple QuickTake", 0, 0, /* DJC */
7259 { 21392,-5653,-3353,2406,8010,-415,7166,1427,2078 } },
7260 { "Canon EOS D2000", 0, 0,
7261 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
7262 { "Canon EOS D6000", 0, 0,
7263 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
7264 { "Canon EOS D30", 0, 0,
7265 { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
7266 { "Canon EOS D60", 0, 0xfa0,
7267 { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
7268 { "Canon EOS 5DS", 0, 0x3c96,
7269 { 6250,-711,-808,-5153,12794,2636,-1249,2198,5610 } },
7270 { "Canon EOS 5D Mark IV", 0, 0,
7271 { 6446,-366,-864,-4436,12204,2513,-952,2496,6348 } },
7272 { "Canon EOS 5D Mark III", 0, 0x3c80,
7273 { 6722,-635,-963,-4287,12460,2028,-908,2162,5668 } },
7274 { "Canon EOS 5D Mark II", 0, 0x3cf0,
7275 { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
7276 { "Canon EOS 5D", 0, 0xe6c,
7277 { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
7278 { "Canon EOS 6D Mark II", 0, 0,
7279 { 6875,-970,-932,-4691,12459,2501,-874,1953,5809 } },
7280 { "Canon EOS 6D", 0, 0x3c82,
7281 { 7034,-804,-1014,-4420,12564,2058,-851,1994,5758 } },
7282 { "Canon EOS 7D Mark II", 0, 0x3510,
7283 { 7268,-1082,-969,-4186,11839,2663,-825,2029,5839 } },
7284 { "Canon EOS 7D", 0, 0x3510,
7285 { 6844,-996,-856,-3876,11761,2396,-593,1772,6198 } },
7286 { "Canon EOS 10D", 0, 0xfa0,
7287 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
7288 { "Canon EOS 20Da", 0, 0,
7289 { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
7290 { "Canon EOS 20D", 0, 0xfff,
7291 { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
7292 { "Canon EOS 30D", 0, 0,
7293 { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
7294 { "Canon EOS 40D", 0, 0x3f60,
7295 { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
7296 { "Canon EOS 50D", 0, 0x3d93,
7297 { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
7298 { "Canon EOS 60D", 0, 0x2ff7,
7299 { 6719,-994,-925,-4408,12426,2211,-887,2129,6051 } },
7300 { "Canon EOS 70D", 0, 0x3bc7,
7301 { 7034,-804,-1014,-4420,12564,2058,-851,1994,5758 } },
7302 { "Canon EOS 77D", 0, 0,
7303 { 7377,-742,-998,-4235,11981,2549,-673,1918,5538 } },
7304 { "Canon EOS 80D", 0, 0,
7305 { 7457,-671,-937,-4849,12495,2643,-1213,2354,5492 } },
7306 { "Canon EOS 100D", 0, 0x350f,
7307 { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
7308 { "Canon EOS 200D", 0, 0,
7309 { 7377,-742,-998,-4235,11981,2549,-673,1918,5538 } },
7310 { "Canon EOS 300D", 0, 0xfa0,
7311 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
7312 { "Canon EOS 350D", 0, 0xfff,
7313 { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
7314 { "Canon EOS 400D", 0, 0xe8e,
7315 { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
7316 { "Canon EOS 450D", 0, 0x390d,
7317 { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
7318 { "Canon EOS 500D", 0, 0x3479,
7319 { 4763,712,-646,-6821,14399,2640,-1921,3276,6561 } },
7320 { "Canon EOS 550D", 0, 0x3dd7,
7321 { 6941,-1164,-857,-3825,11597,2534,-416,1540,6039 } },
7322 { "Canon EOS 600D", 0, 0x3510,
7323 { 6461,-907,-882,-4300,12184,2378,-819,1944,5931 } },
7324 { "Canon EOS 650D", 0, 0x354d,
7325 { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
7326 { "Canon EOS 700D", 0, 0x3c00,
7327 { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
7328 { "Canon EOS 750D", 0, 0x368e,
7329 { 6362,-823,-847,-4426,12109,2616,-743,1857,5635 } },
7330 { "Canon EOS 760D", 0, 0x350f,
7331 { 6362,-823,-847,-4426,12109,2616,-743,1857,5635 } },
7332 { "Canon EOS 800D", 0, 0,
7333 { 6970,-512,-968,-4425,12161,2553,-739,1982,5601 } },
7334 { "Canon EOS 1000D", 0, 0xe43,
7335 { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
7336 { "Canon EOS 1100D", 0, 0x3510,
7337 { 6444,-904,-893,-4563,12308,2535,-903,2016,6728 } },
7338 { "Canon EOS 1200D", 0, 0x37c2,
7339 { 6461,-907,-882,-4300,12184,2378,-819,1944,5931 } },
7340 { "Canon EOS 1300D", 0, 0x3510,
7341 { 6939,-1016,-866,-4428,12473,2177,-1175,2178,6162 } },
7342 { "Canon EOS 1500D", 0, 0,
7343 { 8532,-701,-1167,-4095,11879,2508,-797,2424,7010 } },
7344 { "Canon EOS 3000D", 0, 0,
7345 { 6939,-1016,-866,-4428,12473,2177,-1175,2178,6162 } },
7346 { "Canon EOS M6", 0, 0,
7347 { 8532,-701,-1167,-4095,11879,2508,-797,2424,7010 } },
7348 { "Canon EOS M5", 0, 0, /* also M50 */
7349 { 8532,-701,-1167,-4095,11879,2508,-797,2424,7010 } },
7350 { "Canon EOS M3", 0, 0,
7351 { 6362,-823,-847,-4426,12109,2616,-743,1857,5635 } },
7352 { "Canon EOS M100", 0, 0,
7353 { 8532,-701,-1167,-4095,11879,2508,-797,2424,7010 } },
7354 { "Canon EOS M10", 0, 0,
7355 { 6400,-480,-888,-5294,13416,2047,-1296,2203,6137 } },
7356 { "Canon EOS M", 0, 0,
7357 { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
7358 { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
7359 { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
7360 { "Canon EOS-1Ds Mark II", 0, 0xe80,
7361 { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
7362 { "Canon EOS-1D Mark IV", 0, 0x3bb0,
7363 { 6014,-220,-795,-4109,12014,2361,-561,1824,5787 } },
7364 { "Canon EOS-1D Mark III", 0, 0x3bb0,
7365 { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
7366 { "Canon EOS-1D Mark II N", 0, 0xe80,
7367 { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
7368 { "Canon EOS-1D Mark II", 0, 0xe80,
7369 { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
7370 { "Canon EOS-1DS", 0, 0xe20,
7371 { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
7372 { "Canon EOS-1D C", 0, 0x3c4e,
7373 { 6847,-614,-1014,-4669,12737,2139,-1197,2488,6846 } },
7374 { "Canon EOS-1D X Mark II", 0, 0,
7375 { 7596,-978,-967,-4808,12571,2503,-1398,2567,5752 } },
7376 { "Canon EOS-1D X", 0, 0x3c4e,
7377 { 6847,-614,-1014,-4669,12737,2139,-1197,2488,6846 } },
7378 { "Canon EOS-1D", 0, 0xe20,
7379 { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
7380 { "Canon EOS C500", 853, 0, /* DJC */
7381 { 17851,-10604,922,-7425,16662,763,-3660,3636,22278 } },
7382 { "Canon PowerShot A530", 0, 0,
7383 { 0 } }, /* don't want the A5 matrix */
7384 { "Canon PowerShot A50", 0, 0,
7385 { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
7386 { "Canon PowerShot A5", 0, 0,
7387 { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
7388 { "Canon PowerShot G10", 0, 0,
7389 { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
7390 { "Canon PowerShot G11", 0, 0,
7391 { 12177,-4817,-1069,-1612,9864,2049,-98,850,4471 } },
7392 { "Canon PowerShot G12", 0, 0,
7393 { 13244,-5501,-1248,-1508,9858,1935,-270,1083,4366 } },
7394 { "Canon PowerShot G15", 0, 0,
7395 { 7474,-2301,-567,-4056,11456,2975,-222,716,4181 } },
7396 { "Canon PowerShot G16", 0, 0,
7397 { 8020,-2687,-682,-3704,11879,2052,-965,1921,5556 } },
7398 { "Canon PowerShot G1 X Mark III", 0, 0,
7399 { 8532,-701,-1167,-4095,11879,2508,-797,2424,7010 } },
7400 { "Canon PowerShot G1 X", 0, 0,
7401 { 7378,-1255,-1043,-4088,12251,2048,-876,1946,5805 } },
7402 { "Canon PowerShot G1", 0, 0,
7403 { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
7404 { "Canon PowerShot G2", 0, 0,
7405 { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
7406 { "Canon PowerShot G3 X", 0, 0,
7407 { 9701,-3857,-921,-3149,11537,1817,-786,1817,5147 } },
7408 { "Canon PowerShot G3", 0, 0,
7409 { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
7410 { "Canon PowerShot G5 X", 0, 0,
7411 { 9602,-3823,-937,-2984,11495,1675,-407,1415,5049 } },
7412 { "Canon PowerShot G5", 0, 0,
7413 { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
7414 { "Canon PowerShot G6", 0, 0,
7415 { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
7416 { "Canon PowerShot G7 X", 0, 0,
7417 { 9602,-3823,-937,-2984,11495,1675,-407,1415,5049 } },
7418 { "Canon PowerShot G9 X Mark II", 0, 0,
7419 { 10056,-4131,-944,-2576,11143,1625,-238,1294,5179 } },
7420 { "Canon PowerShot G9 X", 0, 0,
7421 { 9602,-3823,-937,-2984,11495,1675,-407,1415,5049 } },
7422 { "Canon PowerShot G9", 0, 0,
7423 { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
7424 { "Canon PowerShot Pro1", 0, 0,
7425 { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
7426 { "Canon PowerShot Pro70", 34, 0,
7427 { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
7428 { "Canon PowerShot Pro90", 0, 0,
7429 { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
7430 { "Canon PowerShot S30", 0, 0,
7431 { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
7432 { "Canon PowerShot S40", 0, 0,
7433 { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
7434 { "Canon PowerShot S45", 0, 0,
7435 { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
7436 { "Canon PowerShot S50", 0, 0,
7437 { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
7438 { "Canon PowerShot S60", 0, 0,
7439 { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
7440 { "Canon PowerShot S70", 0, 0,
7441 { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
7442 { "Canon PowerShot S90", 0, 0,
7443 { 12374,-5016,-1049,-1677,9902,2078,-83,852,4683 } },
7444 { "Canon PowerShot S95", 0, 0,
7445 { 13440,-5896,-1279,-1236,9598,1931,-180,1001,4651 } },
7446 { "Canon PowerShot S100", 0, 0,
7447 { 7968,-2565,-636,-2873,10697,2513,180,667,4211 } },
7448 { "Canon PowerShot S110", 0, 0,
7449 { 8039,-2643,-654,-3783,11230,2930,-206,690,4194 } },
7450 { "Canon PowerShot S120", 0, 0,
7451 { 6961,-1685,-695,-4625,12945,1836,-1114,2152,5518 } },
7452 { "Canon PowerShot SX1 IS", 0, 0,
7453 { 6578,-259,-502,-5974,13030,3309,-308,1058,4970 } },
7454 { "Canon PowerShot SX50 HS", 0, 0,
7455 { 12432,-4753,-1247,-2110,10691,1629,-412,1623,4926 } },
7456 { "Canon PowerShot SX60 HS", 0, 0,
7457 { 13161,-5451,-1344,-1989,10654,1531,-47,1271,4955 } },
7458 { "Canon PowerShot A3300", 0, 0, /* DJC */
7459 { 10826,-3654,-1023,-3215,11310,1906,0,999,4960 } },
7460 { "Canon PowerShot A470", 0, 0, /* DJC */
7461 { 12513,-4407,-1242,-2680,10276,2405,-878,2215,4734 } },
7462 { "Canon PowerShot A610", 0, 0, /* DJC */
7463 { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
7464 { "Canon PowerShot A620", 0, 0, /* DJC */
7465 { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
7466 { "Canon PowerShot A630", 0, 0, /* DJC */
7467 { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
7468 { "Canon PowerShot A640", 0, 0, /* DJC */
7469 { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
7470 { "Canon PowerShot A650", 0, 0, /* DJC */
7471 { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
7472 { "Canon PowerShot A720", 0, 0, /* DJC */
7473 { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
7474 { "Canon PowerShot S3 IS", 0, 0, /* DJC */
7475 { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
7476 { "Canon PowerShot SX110 IS", 0, 0, /* DJC */
7477 { 14134,-5576,-1527,-1991,10719,1273,-1158,1929,3581 } },
7478 { "Canon PowerShot SX220", 0, 0, /* DJC */
7479 { 13898,-5076,-1447,-1405,10109,1297,-244,1860,3687 } },
7480 { "Canon IXUS 160", 0, 0, /* DJC */
7481 { 11657,-3781,-1136,-3544,11262,2283,-160,1219,4700 } },
7482 { "Casio EX-S20", 0, 0, /* DJC */
7483 { 11634,-3924,-1128,-4968,12954,2015,-1588,2648,7206 } },
7484 { "Casio EX-Z750", 0, 0, /* DJC */
7485 { 10819,-3873,-1099,-4903,13730,1175,-1755,3751,4632 } },
7486 { "Casio EX-Z10", 128, 0xfff, /* DJC */
7487 { 9790,-3338,-603,-2321,10222,2099,-344,1273,4799 } },
7488 { "CINE 650", 0, 0,
7489 { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
7490 { "CINE 660", 0, 0,
7491 { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
7492 { "CINE", 0, 0,
7493 { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
7494 { "Contax N Digital", 0, 0xf1e,
7495 { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
7496 { "DXO ONE", 0, 0,
7497 { 6596,-2079,-562,-4782,13016,1933,-970,1581,5181 } },
7498 { "Epson R-D1", 0, 0,
7499 { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
7500 { "Fujifilm E550", 0, 0,
7501 { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
7502 { "Fujifilm E900", 0, 0,
7503 { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
7504 { "Fujifilm F5", 0, 0,
7505 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7506 { "Fujifilm F6", 0, 0,
7507 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7508 { "Fujifilm F77", 0, 0xfe9,
7509 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7510 { "Fujifilm F7", 0, 0,
7511 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
7512 { "Fujifilm F8", 0, 0,
7513 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7514 { "Fujifilm GFX 50S", 0, 0,
7515 { 11756,-4754,-874,-3056,11045,2305,-381,1457,6006 } },
7516 { "Fujifilm S100FS", 514, 0,
7517 { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
7518 { "Fujifilm S1", 0, 0,
7519 { 12297,-4882,-1202,-2106,10691,1623,-88,1312,4790 } },
7520 { "Fujifilm S20Pro", 0, 0,
7521 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
7522 { "Fujifilm S20", 512, 0x3fff,
7523 { 11401,-4498,-1312,-5088,12751,2613,-838,1568,5941 } },
7524 { "Fujifilm S2Pro", 128, 0xf15,
7525 { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
7526 { "Fujifilm S3Pro", 0, 0x3dff,
7527 { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
7528 { "Fujifilm S5Pro", 0, 0,
7529 { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
7530 { "Fujifilm S5000", 0, 0,
7531 { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
7532 { "Fujifilm S5100", 0, 0,
7533 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
7534 { "Fujifilm S5500", 0, 0,
7535 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
7536 { "Fujifilm S5200", 0, 0,
7537 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
7538 { "Fujifilm S5600", 0, 0,
7539 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
7540 { "Fujifilm S6", 0, 0,
7541 { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
7542 { "Fujifilm S7000", 0, 0,
7543 { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
7544 { "Fujifilm S9000", 0, 0,
7545 { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
7546 { "Fujifilm S9500", 0, 0,
7547 { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
7548 { "Fujifilm S9100", 0, 0,
7549 { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
7550 { "Fujifilm S9600", 0, 0,
7551 { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
7552 { "Fujifilm SL1000", 0, 0,
7553 { 11705,-4262,-1107,-2282,10791,1709,-555,1713,4945 } },
7554 { "Fujifilm IS-1", 0, 0,
7555 { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
7556 { "Fujifilm IS Pro", 0, 0,
7557 { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
7558 { "Fujifilm HS10 HS11", 0, 0xf68,
7559 { 12440,-3954,-1183,-1123,9674,1708,-83,1614,4086 } },
7560 { "Fujifilm HS2", 0, 0xfef,
7561 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7562 { "Fujifilm HS3", 0, 0,
7563 { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
7564 { "Fujifilm HS50EXR", 0, 0,
7565 { 12085,-4727,-953,-3257,11489,2002,-511,2046,4592 } },
7566 { "Fujifilm F900EXR", 0, 0,
7567 { 12085,-4727,-953,-3257,11489,2002,-511,2046,4592 } },
7568 { "Fujifilm X100F", 0, 0,
7569 { 11434,-4948,-1210,-3746,12042,1903,-666,1479,5235 } },
7570 { "Fujifilm X100S", 0, 0,
7571 { 10592,-4262,-1008,-3514,11355,2465,-870,2025,6386 } },
7572 { "Fujifilm X100T", 0, 0,
7573 { 10592,-4262,-1008,-3514,11355,2465,-870,2025,6386 } },
7574 { "Fujifilm X100", 0, 0,
7575 { 12161,-4457,-1069,-5034,12874,2400,-795,1724,6904 } },
7576 { "Fujifilm X10", 0, 0,
7577 { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
7578 { "Fujifilm X20", 0, 0,
7579 { 11768,-4971,-1133,-4904,12927,2183,-480,1723,4605 } },
7580 { "Fujifilm X30", 0, 0,
7581 { 12328,-5256,-1144,-4469,12927,1675,-87,1291,4351 } },
7582 { "Fujifilm X70", 0, 0,
7583 { 10450,-4329,-878,-3217,11105,2421,-752,1758,6519 } },
7584 { "Fujifilm X-Pro1", 0, 0,
7585 { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
7586 { "Fujifilm X-Pro2", 0, 0,
7587 { 11434,-4948,-1210,-3746,12042,1903,-666,1479,5235 } },
7588 { "Fujifilm X-A10", 0, 0,
7589 { 11540,-4999,-991,-2949,10963,2278,-382,1049,5605 } },
7590 { "Fujifilm X-A20", 0, 0,
7591 { 11540,-4999,-991,-2949,10963,2278,-382,1049,5605 } },
7592 { "Fujifilm X-A1", 0, 0,
7593 { 11086,-4555,-839,-3512,11310,2517,-815,1341,5940 } },
7594 { "Fujifilm X-A2", 0, 0,
7595 { 10763,-4560,-917,-3346,11311,2322,-475,1135,5843 } },
7596 { "Fujifilm X-A3", 0, 0,
7597 { 12407,-5222,-1086,-2971,11116,2120,-294,1029,5284 } },
7598 { "Fujifilm X-A5", 0, 0,
7599 { 11673,-4760,-1041,-3988,12058,2166,-771,1417,5569 } },
7600 { "Fujifilm X-E1", 0, 0,
7601 { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
7602 { "Fujifilm X-E2S", 0, 0,
7603 { 11562,-5118,-961,-3022,11007,2311,-525,1569,6097 } },
7604 { "Fujifilm X-E2", 0, 0,
7605 { 8458,-2451,-855,-4597,12447,2407,-1475,2482,6526 } },
7606 { "Fujifilm X-E3", 0, 0,
7607 { 11434,-4948,-1210,-3746,12042,1903,-666,1479,5235 } },
7608 { "Fujifilm X-H1", 0, 0,
7609 { 11434,-4948,-1210,-3746,12042,1903,-666,1479,5235 } },
7610 { "Fujifilm X-M1", 0, 0,
7611 { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
7612 { "Fujifilm X-S1", 0, 0,
7613 { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
7614 { "Fujifilm X-T1", 0, 0, /* also X-T10 */
7615 { 8458,-2451,-855,-4597,12447,2407,-1475,2482,6526 } },
7616 { "Fujifilm X-T2", 0, 0, /* also X-T20 */
7617 { 11434,-4948,-1210,-3746,12042,1903,-666,1479,5235 } },
7618 { "Fujifilm XF1", 0, 0,
7619 { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
7620 { "Fujifilm XQ", 0, 0, /* XQ1 and XQ2 */
7621 { 9252,-2704,-1064,-5893,14265,1717,-1101,2341,4349 } },
7622 { "GoPro HERO5 Black", 0, 0,
7623 { 10344,-4210,-620,-2315,10625,1948,93,1058,5541 } },
7624 { "Imacon Ixpress", 0, 0, /* DJC */
7625 { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
7626 { "Kodak NC2000", 0, 0,
7627 { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
7628 { "Kodak DCS315C", 8, 0,
7629 { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
7630 { "Kodak DCS330C", 8, 0,
7631 { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
7632 { "Kodak DCS420", 0, 0,
7633 { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
7634 { "Kodak DCS460", 0, 0,
7635 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
7636 { "Kodak EOSDCS1", 0, 0,
7637 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
7638 { "Kodak EOSDCS3B", 0, 0,
7639 { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
7640 { "Kodak DCS520C", 178, 0,
7641 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
7642 { "Kodak DCS560C", 177, 0,
7643 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
7644 { "Kodak DCS620C", 177, 0,
7645 { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
7646 { "Kodak DCS620X", 176, 0,
7647 { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
7648 { "Kodak DCS660C", 173, 0,
7649 { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
7650 { "Kodak DCS720X", 0, 0,
7651 { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
7652 { "Kodak DCS760C", 0, 0,
7653 { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
7654 { "Kodak DCS Pro SLR", 0, 0,
7655 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
7656 { "Kodak DCS Pro 14nx", 0, 0,
7657 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
7658 { "Kodak DCS Pro 14", 0, 0,
7659 { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
7660 { "Kodak ProBack645", 0, 0,
7661 { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
7662 { "Kodak ProBack", 0, 0,
7663 { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
7664 { "Kodak P712", 0, 0,
7665 { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
7666 { "Kodak P850", 0, 0xf7c,
7667 { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
7668 { "Kodak P880", 0, 0xfff,
7669 { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
7670 { "Kodak EasyShare Z980", 0, 0,
7671 { 11313,-3559,-1101,-3893,11891,2257,-1214,2398,4908 } },
7672 { "Kodak EasyShare Z981", 0, 0,
7673 { 12729,-4717,-1188,-1367,9187,2582,274,860,4411 } },
7674 { "Kodak EasyShare Z990", 0, 0xfed,
7675 { 11749,-4048,-1309,-1867,10572,1489,-138,1449,4522 } },
7676 { "Kodak EASYSHARE Z1015", 0, 0xef1,
7677 { 11265,-4286,-992,-4694,12343,2647,-1090,1523,5447 } },
7678 { "Leaf CMost", 0, 0,
7679 { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
7680 { "Leaf Valeo 6", 0, 0,
7681 { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
7682 { "Leaf Aptus 54S", 0, 0,
7683 { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
7684 { "Leaf Aptus 65", 0, 0,
7685 { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
7686 { "Leaf Aptus 75", 0, 0,
7687 { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
7688 { "Leaf", 0, 0,
7689 { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
7690 { "Mamiya ZD", 0, 0,
7691 { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
7692 { "Micron 2010", 110, 0, /* DJC */
7693 { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
7694 { "Minolta DiMAGE 5", 0, 0xf7d,
7695 { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
7696 { "Minolta DiMAGE 7Hi", 0, 0xf7d,
7697 { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
7698 { "Minolta DiMAGE 7", 0, 0xf7d,
7699 { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
7700 { "Minolta DiMAGE A1", 0, 0xf8b,
7701 { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
7702 { "Minolta DiMAGE A200", 0, 0,
7703 { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
7704 { "Minolta DiMAGE A2", 0, 0xf8f,
7705 { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
7706 { "Minolta DiMAGE Z2", 0, 0, /* DJC */
7707 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
7708 { "Minolta DYNAX 5", 0, 0xffb,
7709 { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
7710 { "Minolta DYNAX 7", 0, 0xffb,
7711 { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
7712 { "Motorola PIXL", 0, 0, /* DJC */
7713 { 8898,-989,-1033,-3292,11619,1674,-661,3178,5216 } },
7714 { "Nikon D100", 0, 0,
7715 { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
7716 { "Nikon D1H", 0, 0,
7717 { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
7718 { "Nikon D1X", 0, 0,
7719 { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
7720 { "Nikon D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
7721 { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
7722 { "Nikon D200", 0, 0xfbc,
7723 { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
7724 { "Nikon D2H", 0, 0,
7725 { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
7726 { "Nikon D2X", 0, 0,
7727 { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
7728 { "Nikon D3000", 0, 0,
7729 { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
7730 { "Nikon D3100", 0, 0,
7731 { 7911,-2167,-813,-5327,13150,2408,-1288,2483,7968 } },
7732 { "Nikon D3200", 0, 0xfb9,
7733 { 7013,-1408,-635,-5268,12902,2640,-1470,2801,7379 } },
7734 { "Nikon D3300", 0, 0,
7735 { 6988,-1384,-714,-5631,13410,2447,-1485,2204,7318 } },
7736 { "Nikon D3400", 0, 0,
7737 { 6988,-1384,-714,-5631,13410,2447,-1485,2204,7318 } },
7738 { "Nikon D300", 0, 0,
7739 { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
7740 { "Nikon D3X", 0, 0,
7741 { 7171,-1986,-648,-8085,15555,2718,-2170,2512,7457 } },
7742 { "Nikon D3S", 0, 0,
7743 { 8828,-2406,-694,-4874,12603,2541,-660,1509,7587 } },
7744 { "Nikon D3", 0, 0,
7745 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
7746 { "Nikon D40X", 0, 0,
7747 { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
7748 { "Nikon D40", 0, 0,
7749 { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
7750 { "Nikon D4S", 0, 0,
7751 { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
7752 { "Nikon D4", 0, 0,
7753 { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
7754 { "Nikon Df", 0, 0,
7755 { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
7756 { "Nikon D5000", 0, 0xf00,
7757 { 7309,-1403,-519,-8474,16008,2622,-2433,2826,8064 } },
7758 { "Nikon D5100", 0, 0x3de6,
7759 { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
7760 { "Nikon D5200", 0, 0,
7761 { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
7762 { "Nikon D5300", 0, 0,
7763 { 6988,-1384,-714,-5631,13410,2447,-1485,2204,7318 } },
7764 { "Nikon D5500", 0, 0,
7765 { 8821,-2938,-785,-4178,12142,2287,-824,1651,6860 } },
7766 { "Nikon D5600", 0, 0,
7767 { 8821,-2938,-785,-4178,12142,2287,-824,1651,6860 } },
7768 { "Nikon D500", 0, 0,
7769 { 8813,-3210,-1036,-4703,12868,2021,-1054,1940,6129 } },
7770 { "Nikon D50", 0, 0,
7771 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
7772 { "Nikon D5", 0, 0,
7773 { 9200,-3522,-992,-5755,13803,2117,-753,1486,6338 } },
7774 { "Nikon D600", 0, 0x3e07,
7775 { 8178,-2245,-609,-4857,12394,2776,-1207,2086,7298 } },
7776 { "Nikon D610", 0, 0,
7777 { 8178,-2245,-609,-4857,12394,2776,-1207,2086,7298 } },
7778 { "Nikon D60", 0, 0,
7779 { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
7780 { "Nikon D7000", 0, 0,
7781 { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
7782 { "Nikon D7100", 0, 0,
7783 { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
7784 { "Nikon D7200", 0, 0,
7785 { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
7786 { "Nikon D7500", 0, 0,
7787 { 8813,-3210,-1036,-4703,12868,2021,-1054,1940,6129 } },
7788 { "Nikon D750", 0, 0,
7789 { 9020,-2890,-715,-4535,12436,2348,-934,1919,7086 } },
7790 { "Nikon D700", 0, 0,
7791 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
7792 { "Nikon D70", 0, 0,
7793 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
7794 { "Nikon D850", 0, 0,
7795 { 10405,-3755,-1270,-5461,13787,1793,-1040,2015,6785 } },
7796 { "Nikon D810", 0, 0,
7797 { 9369,-3195,-791,-4488,12430,2301,-893,1796,6872 } },
7798 { "Nikon D800", 0, 0,
7799 { 7866,-2108,-555,-4869,12483,2681,-1176,2069,7501 } },
7800 { "Nikon D80", 0, 0,
7801 { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
7802 { "Nikon D90", 0, 0xf00,
7803 { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
7804 { "Nikon E700", 0, 0x3dd, /* DJC */
7805 { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
7806 { "Nikon E800", 0, 0x3dd, /* DJC */
7807 { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
7808 { "Nikon E950", 0, 0x3dd, /* DJC */
7809 { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
7810 { "Nikon E995", 0, 0, /* copied from E5000 */
7811 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
7812 { "Nikon E2100", 0, 0, /* copied from Z2, new white balance */
7813 { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711} },
7814 { "Nikon E2500", 0, 0,
7815 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
7816 { "Nikon E3200", 0, 0, /* DJC */
7817 { 9846,-2085,-1019,-3278,11109,2170,-774,2134,5745 } },
7818 { "Nikon E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
7819 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
7820 { "Nikon E4500", 0, 0,
7821 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
7822 { "Nikon E5000", 0, 0,
7823 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
7824 { "Nikon E5400", 0, 0,
7825 { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
7826 { "Nikon E5700", 0, 0,
7827 { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
7828 { "Nikon E8400", 0, 0,
7829 { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
7830 { "Nikon E8700", 0, 0,
7831 { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
7832 { "Nikon E8800", 0, 0,
7833 { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
7834 { "Nikon COOLPIX A", 0, 0,
7835 { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
7836 { "Nikon COOLPIX B700", 200, 0,
7837 { 14387,-6014,-1299,-1357,9975,1616,467,1047,4744 } },
7838 { "Nikon COOLPIX P330", 200, 0,
7839 { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
7840 { "Nikon COOLPIX P340", 200, 0,
7841 { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
7842 { "Nikon COOLPIX P6000", 0, 0,
7843 { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
7844 { "Nikon COOLPIX P7000", 0, 0,
7845 { 11432,-3679,-1111,-3169,11239,2202,-791,1380,4455 } },
7846 { "Nikon COOLPIX P7100", 0, 0,
7847 { 11053,-4269,-1024,-1976,10182,2088,-526,1263,4469 } },
7848 { "Nikon COOLPIX P7700", 200, 0,
7849 { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
7850 { "Nikon COOLPIX P7800", 200, 0,
7851 { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
7852 { "Nikon 1 V3", 0, 0,
7853 { 5958,-1559,-571,-4021,11453,2939,-634,1548,5087 } },
7854 { "Nikon 1 J4", 0, 0,
7855 { 5958,-1559,-571,-4021,11453,2939,-634,1548,5087 } },
7856 { "Nikon 1 J5", 0, 0,
7857 { 7520,-2518,-645,-3844,12102,1945,-913,2249,6835 } },
7858 { "Nikon 1 S2", 200, 0,
7859 { 6612,-1342,-618,-3338,11055,2623,-174,1792,5075 } },
7860 { "Nikon 1 V2", 0, 0,
7861 { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
7862 { "Nikon 1 J3", 0, 0,
7863 { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
7864 { "Nikon 1 AW1", 0, 0,
7865 { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
7866 { "Nikon 1 ", 0, 0, /* J1, J2, S1, V1 */
7867 { 8994,-2667,-865,-4594,12324,2552,-699,1786,6260 } },
7868 { "Olympus AIR A01", 0, 0,
7869 { 8992,-3093,-639,-2563,10721,2122,-437,1270,5473 } },
7870 { "Olympus C5050", 0, 0,
7871 { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
7872 { "Olympus C5060", 0, 0,
7873 { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
7874 { "Olympus C7070", 0, 0,
7875 { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
7876 { "Olympus C70", 0, 0,
7877 { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
7878 { "Olympus C80", 0, 0,
7879 { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
7880 { "Olympus E-10", 0, 0xffc,
7881 { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
7882 { "Olympus E-1", 0, 0,
7883 { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
7884 { "Olympus E-20", 0, 0xffc,
7885 { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
7886 { "Olympus E-300", 0, 0,
7887 { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
7888 { "Olympus E-330", 0, 0,
7889 { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
7890 { "Olympus E-30", 0, 0xfbc,
7891 { 8144,-1861,-1111,-7763,15894,1929,-1865,2542,7607 } },
7892 { "Olympus E-3", 0, 0xf99,
7893 { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
7894 { "Olympus E-400", 0, 0,
7895 { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
7896 { "Olympus E-410", 0, 0xf6a,
7897 { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
7898 { "Olympus E-420", 0, 0xfd7,
7899 { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
7900 { "Olympus E-450", 0, 0xfd2,
7901 { 8745,-2425,-1095,-7594,15613,2073,-1780,2309,7416 } },
7902 { "Olympus E-500", 0, 0,
7903 { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
7904 { "Olympus E-510", 0, 0xf6a,
7905 { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
7906 { "Olympus E-520", 0, 0xfd2,
7907 { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
7908 { "Olympus E-5", 0, 0xeec,
7909 { 11200,-3783,-1325,-4576,12593,2206,-695,1742,7504 } },
7910 { "Olympus E-600", 0, 0xfaf,
7911 { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
7912 { "Olympus E-620", 0, 0xfaf,
7913 { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
7914 { "Olympus E-P1", 0, 0xffd,
7915 { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
7916 { "Olympus E-P2", 0, 0xffd,
7917 { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
7918 { "Olympus E-P3", 0, 0,
7919 { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
7920 { "Olympus E-P5", 0, 0,
7921 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7922 { "Olympus E-PL1s", 0, 0,
7923 { 11409,-3872,-1393,-4572,12757,2003,-709,1810,7415 } },
7924 { "Olympus E-PL1", 0, 0,
7925 { 11408,-4289,-1215,-4286,12385,2118,-387,1467,7787 } },
7926 { "Olympus E-PL2", 0, 0xcf3,
7927 { 15030,-5552,-1806,-3987,12387,1767,-592,1670,7023 } },
7928 { "Olympus E-PL3", 0, 0,
7929 { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
7930 { "Olympus E-PL5", 0, 0xfcb,
7931 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7932 { "Olympus E-PL6", 0, 0,
7933 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7934 { "Olympus E-PL7", 0, 0,
7935 { 9197,-3190,-659,-2606,10830,2039,-458,1250,5458 } },
7936 { "Olympus E-PL8", 0, 0,
7937 { 9197,-3190,-659,-2606,10830,2039,-458,1250,5458 } },
7938 { "Olympus E-PL9", 0, 0,
7939 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7940 { "Olympus E-PM1", 0, 0,
7941 { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
7942 { "Olympus E-PM2", 0, 0,
7943 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7944 { "Olympus E-M10", 0, 0, /* also E-M10 Mark II & III */
7945 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7946 { "Olympus E-M1Mark II", 0, 0,
7947 { 9383,-3170,-763,-2457,10702,2020,-384,1236,5552 } },
7948 { "Olympus E-M1", 0, 0,
7949 { 7687,-1984,-606,-4327,11928,2721,-1381,2339,6452 } },
7950 { "Olympus E-M5MarkII", 0, 0,
7951 { 9422,-3258,-711,-2655,10898,2015,-512,1354,5512 } },
7952 { "Olympus E-M5", 0, 0xfe1,
7953 { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
7954 { "Olympus PEN-F", 0, 0,
7955 { 9476,-3182,-765,-2613,10958,1893,-449,1315,5268 } },
7956 { "Olympus SH-2", 0, 0,
7957 { 10156,-3425,-1077,-2611,11177,1624,-385,1592,5080 } },
7958 { "Olympus SP350", 0, 0,
7959 { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
7960 { "Olympus SP3", 0, 0,
7961 { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
7962 { "Olympus SP500UZ", 0, 0xfff,
7963 { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
7964 { "Olympus SP510UZ", 0, 0xffe,
7965 { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
7966 { "Olympus SP550UZ", 0, 0xffe,
7967 { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
7968 { "Olympus SP560UZ", 0, 0xff9,
7969 { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
7970 { "Olympus SP570UZ", 0, 0,
7971 { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
7972 { "Olympus STYLUS1", 0, 0,
7973 { 8360,-2420,-880,-3928,12353,1739,-1381,2416,5173 } },
7974 { "Olympus TG-4", 0, 0,
7975 { 11426,-4159,-1126,-2066,10678,1593,-120,1327,4998 } },
7976 { "Olympus TG-5", 0, 0,
7977 { 10899,-3833,-1082,-2112,10736,1575,-267,1452,5269 } },
7978 { "Olympus XZ-10", 0, 0,
7979 { 9777,-3483,-925,-2886,11297,1800,-602,1663,5134 } },
7980 { "Olympus XZ-1", 0, 0,
7981 { 10901,-4095,-1074,-1141,9208,2293,-62,1417,5158 } },
7982 { "Olympus XZ-2", 0, 0,
7983 { 9777,-3483,-925,-2886,11297,1800,-602,1663,5134 } },
7984 { "OmniVision", 0, 0, /* DJC */
7985 { 12782,-4059,-379,-478,9066,1413,1340,1513,5176 } },
7986 { "Pentax *ist DL2", 0, 0,
7987 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
7988 { "Pentax *ist DL", 0, 0,
7989 { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
7990 { "Pentax *ist DS2", 0, 0,
7991 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
7992 { "Pentax *ist DS", 0, 0,
7993 { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
7994 { "Pentax *ist D", 0, 0,
7995 { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
7996 { "Pentax K10D", 0, 0,
7997 { 9566,-2863,-803,-7170,15172,2112,-818,803,9705 } },
7998 { "Pentax K1", 0, 0,
7999 { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
8000 { "Pentax K20D", 0, 0,
8001 { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
8002 { "Pentax K200D", 0, 0,
8003 { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
8004 { "Pentax K2000", 0, 0,
8005 { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
8006 { "Pentax K-m", 0, 0,
8007 { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
8008 { "Pentax K-x", 0, 0,
8009 { 8843,-2837,-625,-5025,12644,2668,-411,1234,7410 } },
8010 { "Pentax K-r", 0, 0,
8011 { 9895,-3077,-850,-5304,13035,2521,-883,1768,6936 } },
8012 { "Pentax K-1", 0, 0,
8013 { 8596,-2981,-639,-4202,12046,2431,-685,1424,6122 } },
8014 { "Pentax K-30", 0, 0,
8015 { 8710,-2632,-1167,-3995,12301,1881,-981,1719,6535 } },
8016 { "Pentax K-3 II", 0, 0,
8017 { 8626,-2607,-1155,-3995,12301,1881,-1039,1822,6925 } },
8018 { "Pentax K-3", 0, 0,
8019 { 7415,-2052,-721,-5186,12788,2682,-1446,2157,6773 } },
8020 { "Pentax K-5 II", 0, 0,
8021 { 8170,-2725,-639,-4440,12017,2744,-771,1465,6599 } },
8022 { "Pentax K-5", 0, 0,
8023 { 8713,-2833,-743,-4342,11900,2772,-722,1543,6247 } },
8024 { "Pentax K-70", 0, 0,
8025 { 8270,-2117,-1299,-4359,12953,1515,-1078,1933,5975 } },
8026 { "Pentax K-7", 0, 0,
8027 { 9142,-2947,-678,-8648,16967,1663,-2224,2898,8615 } },
8028 { "Pentax K-S1", 0, 0,
8029 { 8512,-3211,-787,-4167,11966,2487,-638,1288,6054 } },
8030 { "Pentax K-S2", 0, 0,
8031 { 8662,-3280,-798,-3928,11771,2444,-586,1232,6054 } },
8032 { "Pentax KP", 0, 0,
8033 { 8617,-3228,-1034,-4674,12821,2044,-803,1577,5728 } },
8034 { "Pentax Q-S1", 0, 0,
8035 { 12995,-5593,-1107,-1879,10139,2027,-64,1233,4919 } },
8036 { "Pentax 645D", 0, 0x3e00,
8037 { 10646,-3593,-1158,-3329,11699,1831,-667,2874,6287 } },
8038 { "Panasonic DMC-CM1", 15, 0,
8039 { 8770,-3194,-820,-2871,11281,1803,-513,1552,4434 } },
8040 { "Panasonic DC-FZ80", 0, 0,
8041 { 8550,-2908,-842,-3195,11529,1881,-338,1603,4631 } },
8042 { "Panasonic DMC-FZ8", 0, 0xf7f,
8043 { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
8044 { "Panasonic DMC-FZ18", 0, 0,
8045 { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
8046 { "Panasonic DMC-FZ28", 15, 0xf96,
8047 { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
8048 { "Panasonic DMC-FZ2500", 15, 0,
8049 { 7386,-2443,-743,-3437,11864,1757,-608,1660,4766 } },
8050 { "Panasonic DMC-FZ330", 15, 0,
8051 { 8378,-2798,-769,-3068,11410,1877,-538,1792,4623 } },
8052 { "Panasonic DMC-FZ300", 15, 0,
8053 { 8378,-2798,-769,-3068,11410,1877,-538,1792,4623 } },
8054 { "Panasonic DMC-FZ30", 0, 0xf94,
8055 { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
8056 { "Panasonic DMC-FZ3", 15, 0, /* FZ35, FZ38 */
8057 { 9938,-2780,-890,-4604,12393,2480,-1117,2304,4620 } },
8058 { "Panasonic DMC-FZ4", 15, 0, /* FZ40, FZ45 */
8059 { 13639,-5535,-1371,-1698,9633,2430,316,1152,4108 } },
8060 { "Panasonic DMC-FZ50", 0, 0,
8061 { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
8062 { "Panasonic DMC-FZ7", 15, 0, /* FZ70, FZ72 */
8063 { 11532,-4324,-1066,-2375,10847,1749,-564,1699,4351 } },
8064 { "Leica V-LUX1", 0, 0,
8065 { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
8066 { "Panasonic DMC-L10", 15, 0xf96,
8067 { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
8068 { "Panasonic DMC-L1", 0, 0xf7f,
8069 { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
8070 { "Leica DIGILUX 3", 0, 0xf7f,
8071 { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
8072 { "Panasonic DMC-LC1", 0, 0,
8073 { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
8074 { "Leica DIGILUX 2", 0, 0,
8075 { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
8076 { "Panasonic DMC-LX100", 15, 0,
8077 { 8844,-3538,-768,-3709,11762,2200,-698,1792,5220 } },
8078 { "Leica D-LUX (Typ 109)", 15, 0,
8079 { 8844,-3538,-768,-3709,11762,2200,-698,1792,5220 } },
8080 { "Panasonic DMC-LF1", 15, 0,
8081 { 9379,-3267,-816,-3227,11560,1881,-926,1928,5340 } },
8082 { "Leica C (Typ 112)", 15, 0,
8083 { 9379,-3267,-816,-3227,11560,1881,-926,1928,5340 } },
8084 { "Panasonic DMC-LX1", 0, 0xf7f,
8085 { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
8086 { "Leica D-LUX2", 0, 0xf7f,
8087 { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
8088 { "Panasonic DMC-LX2", 0, 0,
8089 { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
8090 { "Leica D-LUX3", 0, 0,
8091 { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
8092 { "Panasonic DMC-LX3", 15, 0,
8093 { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
8094 { "Leica D-LUX 4", 15, 0,
8095 { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
8096 { "Panasonic DMC-LX5", 15, 0,
8097 { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
8098 { "Leica D-LUX 5", 15, 0,
8099 { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
8100 { "Panasonic DMC-LX7", 15, 0,
8101 { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
8102 { "Leica D-LUX 6", 15, 0,
8103 { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
8104 { "Panasonic DMC-LX9", 15, 0,
8105 { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
8106 { "Panasonic DMC-FZ1000", 15, 0,
8107 { 7830,-2696,-763,-3325,11667,1866,-641,1712,4824 } },
8108 { "Leica V-LUX (Typ 114)", 15, 0,
8109 { 7830,-2696,-763,-3325,11667,1866,-641,1712,4824 } },
8110 { "Panasonic DMC-FZ100", 15, 0xfff,
8111 { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
8112 { "Leica V-LUX 2", 15, 0xfff,
8113 { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
8114 { "Panasonic DMC-FZ150", 15, 0xfff,
8115 { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
8116 { "Leica V-LUX 3", 15, 0xfff,
8117 { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
8118 { "Panasonic DMC-FZ200", 15, 0xfff,
8119 { 8112,-2563,-740,-3730,11784,2197,-941,2075,4933 } },
8120 { "Leica V-LUX 4", 15, 0xfff,
8121 { 8112,-2563,-740,-3730,11784,2197,-941,2075,4933 } },
8122 { "Panasonic DMC-FX150", 15, 0xfff,
8123 { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
8124 { "Panasonic DMC-G10", 0, 0,
8125 { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
8126 { "Panasonic DMC-G1", 15, 0xf94,
8127 { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
8128 { "Panasonic DMC-G2", 15, 0xf3c,
8129 { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
8130 { "Panasonic DMC-G3", 15, 0xfff,
8131 { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
8132 { "Panasonic DMC-G5", 15, 0xfff,
8133 { 7798,-2562,-740,-3879,11584,2613,-1055,2248,5434 } },
8134 { "Panasonic DMC-G6", 15, 0xfff,
8135 { 8294,-2891,-651,-3869,11590,2595,-1183,2267,5352 } },
8136 { "Panasonic DMC-G7", 15, 0xfff,
8137 { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
8138 { "Panasonic DMC-G8", 15, 0xfff, /* G8, G80, G81, G85 */
8139 { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
8140 { "Panasonic DC-G9", 15, 0xfff,
8141 { 7685,-2375,-634,-3687,11700,2249,-748,1546,5111 } },
8142 { "Panasonic DMC-GF1", 15, 0xf92,
8143 { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
8144 { "Panasonic DMC-GF2", 15, 0xfff,
8145 { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
8146 { "Panasonic DMC-GF3", 15, 0xfff,
8147 { 9051,-2468,-1204,-5212,13276,2121,-1197,2510,6890 } },
8148 { "Panasonic DMC-GF5", 15, 0xfff,
8149 { 8228,-2945,-660,-3938,11792,2430,-1094,2278,5793 } },
8150 { "Panasonic DMC-GF6", 15, 0,
8151 { 8130,-2801,-946,-3520,11289,2552,-1314,2511,5791 } },
8152 { "Panasonic DMC-GF7", 15, 0,
8153 { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
8154 { "Panasonic DMC-GF8", 15, 0,
8155 { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
8156 { "Panasonic DC-GF9", 15, 0,
8157 { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
8158 { "Panasonic DMC-GH1", 15, 0xf92,
8159 { 6299,-1466,-532,-6535,13852,2969,-2331,3112,5984 } },
8160 { "Panasonic DMC-GH2", 15, 0xf95,
8161 { 7780,-2410,-806,-3913,11724,2484,-1018,2390,5298 } },
8162 { "Panasonic DMC-GH3", 15, 0,
8163 { 6559,-1752,-491,-3672,11407,2586,-962,1875,5130 } },
8164 { "Panasonic DMC-GH4", 15, 0,
8165 { 7122,-2108,-512,-3155,11201,2231,-541,1423,5045 } },
8166 { "Panasonic DC-GH5S", 15, 0,
8167 { 6929,-2355,-708,-4192,12534,1828,-1097,1989,5195 } },
8168 { "Panasonic DC-GH5", 15, 0,
8169 { 7641,-2336,-605,-3218,11299,2187,-485,1338,5121 } },
8170 { "Panasonic DMC-GM1", 15, 0,
8171 { 6770,-1895,-744,-5232,13145,2303,-1664,2691,5703 } },
8172 { "Panasonic DMC-GM5", 15, 0,
8173 { 8238,-3244,-679,-3921,11814,2384,-836,2022,5852 } },
8174 { "Panasonic DMC-GX1", 15, 0,
8175 { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
8176 { "Panasonic DMC-GX7", 15, 0,
8177 { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
8178 { "Panasonic DMC-GX85", 15, 0,
8179 { 7771,-3020,-629,-4029,11950,2345,-821,1977,6119 } },
8180 { "Panasonic DMC-GX8", 15, 0,
8181 { 7564,-2263,-606,-3148,11239,2177,-540,1435,4853 } },
8182 { "Panasonic DC-GX9", 15, 0,
8183 { 7564,-2263,-606,-3148,11239,2177,-540,1435,4853 } },
8184 { "Panasonic DMC-ZS100", 15, 0,
8185 { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
8186 { "Panasonic DC-ZS200", 15, 0,
8187 { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
8188 { "Panasonic DMC-ZS40", 15, 0,
8189 { 8607,-2822,-808,-3755,11930,2049,-820,2060,5224 } },
8190 { "Panasonic DMC-ZS50", 15, 0,
8191 { 8802,-3135,-789,-3151,11468,1904,-550,1745,4810 } },
8192 { "Panasonic DMC-TZ82", 15, 0,
8193 { 8550,-2908,-842,-3195,11529,1881,-338,1603,4631 } },
8194 { "Panasonic DMC-ZS6", 15, 0,
8195 { 8550,-2908,-842,-3195,11529,1881,-338,1603,4631 } },
8196 { "Panasonic DMC-ZS70", 15, 0,
8197 { 9052,-3117,-883,-3045,11346,1927,-205,1520,4730 } },
8198 { "Leica S (Typ 007)", 0, 0,
8199 { 6063,-2234,-231,-5210,13787,1500,-1043,2866,6997 } },
8200 { "Leica X", 0, 0, /* X and X-U, both (Typ 113) */
8201 { 7712,-2059,-653,-3882,11494,2726,-710,1332,5958 } },
8202 { "Leica Q (Typ 116)", 0, 0,
8203 { 11865,-4523,-1441,-5423,14458,935,-1587,2687,4830 } },
8204 { "Leica M (Typ 262)", 0, 0,
8205 { 6653,-1486,-611,-4221,13303,929,-881,2416,7226 } },
8206 { "Leica SL (Typ 601)", 0, 0,
8207 { 11865,-4523,-1441,-5423,14458,935,-1587,2687,4830 } },
8208 { "Leica TL2", 0, 0,
8209 { 5836,-1626,-647,-5384,13326,2261,-1207,2129,5861 } },
8210 { "Leica TL", 0, 0,
8211 { 5463,-988,-364,-4634,12036,2946,-766,1389,6522 } },
8212 { "Leica CL", 0, 0,
8213 { 7414,-2393,-840,-5127,13180,2138,-1585,2468,5064 } },
8214 { "Leica M10", 0, 0,
8215 { 8249,-2849,-620,-5415,14756,565,-957,3074,6517 } },
8216 { "Phase One H 20", 0, 0, /* DJC */
8217 { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
8218 { "Phase One H 25", 0, 0,
8219 { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
8220 { "Phase One P 2", 0, 0,
8221 { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
8222 { "Phase One P 30", 0, 0,
8223 { 4516,-245,-37,-7020,14976,2173,-3206,4671,7087 } },
8224 { "Phase One P 45", 0, 0,
8225 { 5053,-24,-117,-5684,14076,1702,-2619,4492,5849 } },
8226 { "Phase One P40", 0, 0,
8227 { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
8228 { "Phase One P65", 0, 0,
8229 { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
8230 { "Photron BC2-HD", 0, 0, /* DJC */
8231 { 14603,-4122,-528,-1810,9794,2017,-297,2763,5936 } },
8232 { "Red One", 704, 0xffff, /* DJC */
8233 { 21014,-7891,-2613,-3056,12201,856,-2203,5125,8042 } },
8234 { "Ricoh GR II", 0, 0,
8235 { 4630,-834,-423,-4977,12805,2417,-638,1467,6115 } },
8236 { "Ricoh GR", 0, 0,
8237 { 3708,-543,-160,-5381,12254,3556,-1471,1929,8234 } },
8238 { "Samsung EX1", 0, 0x3e00,
8239 { 8898,-2498,-994,-3144,11328,2066,-760,1381,4576 } },
8240 { "Samsung EX2F", 0, 0x7ff,
8241 { 10648,-3897,-1055,-2022,10573,1668,-492,1611,4742 } },
8242 { "Samsung EK-GN120", 0, 0,
8243 { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
8244 { "Samsung NX mini", 0, 0,
8245 { 5222,-1196,-550,-6540,14649,2009,-1666,2819,5657 } },
8246 { "Samsung NX3300", 0, 0,
8247 { 8060,-2933,-761,-4504,12890,1762,-630,1489,5227 } },
8248 { "Samsung NX3000", 0, 0,
8249 { 8060,-2933,-761,-4504,12890,1762,-630,1489,5227 } },
8250 { "Samsung NX30", 0, 0, /* NX30, NX300, NX300M */
8251 { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
8252 { "Samsung NX2000", 0, 0,
8253 { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
8254 { "Samsung NX2", 0, 0xfff, /* NX20, NX200, NX210 */
8255 { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
8256 { "Samsung NX1000", 0, 0,
8257 { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
8258 { "Samsung NX1100", 0, 0,
8259 { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
8260 { "Samsung NX11", 0, 0,
8261 { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
8262 { "Samsung NX10", 0, 0, /* also NX100 */
8263 { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
8264 { "Samsung NX500", 0, 0,
8265 { 10686,-4042,-1052,-3595,13238,276,-464,1259,5931 } },
8266 { "Samsung NX5", 0, 0,
8267 { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
8268 { "Samsung NX1", 0, 0,
8269 { 10686,-4042,-1052,-3595,13238,276,-464,1259,5931 } },
8270 { "Samsung WB2000", 0, 0xfff,
8271 { 12093,-3557,-1155,-1000,9534,1733,-22,1787,4576 } },
8272 { "Samsung GX-1", 0, 0,
8273 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
8274 { "Samsung GX20", 0, 0, /* copied from Pentax K20D */
8275 { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
8276 { "Samsung S85", 0, 0, /* DJC */
8277 { 11885,-3968,-1473,-4214,12299,1916,-835,1655,5549 } },
8278 { "Sinar", 0, 0, /* DJC */
8279 { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
8280 { "Sony DSC-F828", 0, 0,
8281 { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
8282 { "Sony DSC-R1", 0, 0,
8283 { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
8284 { "Sony DSC-V3", 0, 0,
8285 { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
8286 { "Sony DSC-RX100M", 0, 0, /* M2, M3, M4, and M5 */
8287 { 6596,-2079,-562,-4782,13016,1933,-970,1581,5181 } },
8288 { "Sony DSC-RX100", 0, 0,
8289 { 8651,-2754,-1057,-3464,12207,1373,-568,1398,4434 } },
8290 { "Sony DSC-RX10M4", 0, 0,
8291 { 7699,-2566,-629,-2967,11270,1928,-378,1286,4807 } },
8292 { "Sony DSC-RX10", 0, 0, /* also RX10M2, RX10M3 */
8293 { 6679,-1825,-745,-5047,13256,1953,-1580,2422,5183 } },
8294 { "Sony DSC-RX1RM2", 0, 0,
8295 { 6629,-1900,-483,-4618,12349,2550,-622,1381,6514 } },
8296 { "Sony DSC-RX1", 0, 0,
8297 { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
8298 { "Sony DSC-RX0", 200, 0,
8299 { 9396,-3507,-843,-2497,11111,1572,-343,1355,5089 } },
8300 { "Sony DSLR-A100", 0, 0xfeb,
8301 { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
8302 { "Sony DSLR-A290", 0, 0,
8303 { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
8304 { "Sony DSLR-A2", 0, 0,
8305 { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
8306 { "Sony DSLR-A300", 0, 0,
8307 { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
8308 { "Sony DSLR-A330", 0, 0,
8309 { 9847,-3091,-929,-8485,16346,2225,-714,595,7103 } },
8310 { "Sony DSLR-A350", 0, 0xffc,
8311 { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
8312 { "Sony DSLR-A380", 0, 0,
8313 { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
8314 { "Sony DSLR-A390", 0, 0,
8315 { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
8316 { "Sony DSLR-A450", 0, 0xfeb,
8317 { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
8318 { "Sony DSLR-A580", 0, 0xfeb,
8319 { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
8320 { "Sony DSLR-A500", 0, 0xfeb,
8321 { 6046,-1127,-278,-5574,13076,2786,-691,1419,7625 } },
8322 { "Sony DSLR-A5", 0, 0xfeb,
8323 { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
8324 { "Sony DSLR-A700", 0, 0,
8325 { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
8326 { "Sony DSLR-A850", 0, 0,
8327 { 5413,-1162,-365,-5665,13098,2866,-608,1179,8440 } },
8328 { "Sony DSLR-A900", 0, 0,
8329 { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } },
8330 { "Sony ILCA-68", 0, 0,
8331 { 6435,-1903,-536,-4722,12449,2550,-663,1363,6517 } },
8332 { "Sony ILCA-77M2", 0, 0,
8333 { 5991,-1732,-443,-4100,11989,2381,-704,1467,5992 } },
8334 { "Sony ILCA-99M2", 0, 0,
8335 { 6660,-1918,-471,-4613,12398,2485,-649,1433,6447 } },
8336 { "Sony ILCE-6", 0, 0, /* 6300, 6500 */
8337 { 5973,-1695,-419,-3826,11797,2293,-639,1398,5789 } },
8338 { "Sony ILCE-7M2", 0, 0,
8339 { 5271,-712,-347,-6153,13653,2763,-1601,2366,7242 } },
8340 { "Sony ILCE-7M3", 0, 0,
8341 { 7374,-2389,-551,-5435,13162,2519,-1006,1795,6552 } },
8342 { "Sony ILCE-7S", 0, 0, /* also ILCE-7SM2 */
8343 { 5838,-1430,-246,-3497,11477,2297,-748,1885,5778 } },
8344 { "Sony ILCE-7RM3", 0, 0,
8345 { 6640,-1847,-503,-5238,13010,2474,-993,1673,6527 } },
8346 { "Sony ILCE-7RM2", 0, 0,
8347 { 6629,-1900,-483,-4618,12349,2550,-622,1381,6514 } },
8348 { "Sony ILCE-7R", 0, 0,
8349 { 4913,-541,-202,-6130,13513,2906,-1564,2151,7183 } },
8350 { "Sony ILCE-7", 0, 0,
8351 { 5271,-712,-347,-6153,13653,2763,-1601,2366,7242 } },
8352 { "Sony ILCE-9", 0, 0,
8353 { 6389,-1703,-378,-4562,12265,2587,-670,1489,6550 } },
8354 { "Sony ILCE", 0, 0, /* 3000, 5000, 5100, 6000, and QX1 */
8355 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8356 { "Sony NEX-5N", 0, 0,
8357 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8358 { "Sony NEX-5R", 0, 0,
8359 { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
8360 { "Sony NEX-5T", 0, 0,
8361 { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
8362 { "Sony NEX-3N", 0, 0,
8363 { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
8364 { "Sony NEX-3", 138, 0, /* DJC */
8365 { 6907,-1256,-645,-4940,12621,2320,-1710,2581,6230 } },
8366 { "Sony NEX-5", 116, 0, /* DJC */
8367 { 6807,-1350,-342,-4216,11649,2567,-1089,2001,6420 } },
8368 { "Sony NEX-3", 0, 0, /* Adobe */
8369 { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
8370 { "Sony NEX-5", 0, 0, /* Adobe */
8371 { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
8372 { "Sony NEX-6", 0, 0,
8373 { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
8374 { "Sony NEX-7", 0, 0,
8375 { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
8376 { "Sony NEX", 0, 0, /* NEX-C3, NEX-F3 */
8377 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8378 { "Sony SLT-A33", 0, 0,
8379 { 6069,-1221,-366,-5221,12779,2734,-1024,2066,6834 } },
8380 { "Sony SLT-A35", 0, 0,
8381 { 5986,-1618,-415,-4557,11820,3120,-681,1404,6971 } },
8382 { "Sony SLT-A37", 0, 0,
8383 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8384 { "Sony SLT-A55", 0, 0,
8385 { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
8386 { "Sony SLT-A57", 0, 0,
8387 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8388 { "Sony SLT-A58", 0, 0,
8389 { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
8390 { "Sony SLT-A65", 0, 0,
8391 { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
8392 { "Sony SLT-A77", 0, 0,
8393 { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
8394 { "Sony SLT-A99", 0, 0,
8395 { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
8396 { "YI M1", 0, 0,
8397 { 7712,-2059,-653,-3882,11494,2726,-710,1332,5958 } },
8398 };
8399 double cam_xyz[4][3];
8400 char name[130];
8401 int i, j;
8402
8403 sprintf (name, "%s %s", make, model);
8404 for (i=0; i < sizeof table / sizeof *table; i++)
8405 if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
8406 if (table[i].black) black = (ushort) table[i].black;
8407 if (table[i].maximum) maximum = (ushort) table[i].maximum;
8408 if (table[i].trans[0]) {
8409 for (raw_color = j=0; j < 12; j++)
8410 ((double *)cam_xyz)[j] = table[i].trans[j] / 10000.0;
8411 cam_xyz_coeff (rgb_cam, cam_xyz);
8412 }
8413 break;
8414 }
8415 }
8416
simple_coeff(int index)8417 void CLASS simple_coeff (int index)
8418 {
8419 static const float table[][12] = {
8420 /* index 0 -- all Foveon cameras */
8421 { 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
8422 /* index 1 -- Kodak DC20 and DC25 */
8423 { 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
8424 /* index 2 -- Logitech Fotoman Pixtura */
8425 { 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
8426 /* index 3 -- Nikon E880, E900, and E990 */
8427 { -1.936280, 1.800443, -1.448486, 2.584324,
8428 1.405365, -0.524955, -0.289090, 0.408680,
8429 -1.204965, 1.082304, 2.941367, -1.818705 }
8430 };
8431 int i, c;
8432
8433 for (raw_color = i=0; i < 3; i++)
8434 FORCC rgb_cam[i][c] = table[index][i*colors+c];
8435 }
8436
guess_byte_order(int words)8437 short CLASS guess_byte_order (int words)
8438 {
8439 uchar test[4][2];
8440 int t=2, msb;
8441 double diff, sum[2] = {0,0};
8442
8443 fread (test[0], 2, 2, ifp);
8444 for (words-=2; words--; ) {
8445 fread (test[t], 2, 1, ifp);
8446 for (msb=0; msb < 2; msb++) {
8447 diff = (test[t^2][msb] << 8 | test[t^2][!msb])
8448 - (test[t ][msb] << 8 | test[t ][!msb]);
8449 sum[msb] += diff*diff;
8450 }
8451 t = (t+1) & 3;
8452 }
8453 return sum[0] < sum[1] ? 0x4d4d : 0x4949;
8454 }
8455
find_green(int bps,int bite,int off0,int off1)8456 float CLASS find_green (int bps, int bite, int off0, int off1)
8457 {
8458 UINT64 bitbuf=0;
8459 int vbits, col, i, c;
8460 ushort img[2][2064];
8461 double sum[]={0,0};
8462
8463 FORC(2) {
8464 fseek (ifp, c ? off1:off0, SEEK_SET);
8465 for (vbits=col=0; col < width; col++) {
8466 for (vbits -= bps; vbits < 0; vbits += bite) {
8467 bitbuf <<= bite;
8468 for (i=0; i < bite; i+=8)
8469 bitbuf |= (unsigned) (fgetc(ifp) << i);
8470 }
8471 img[c][col] = bitbuf << (64-bps-vbits) >> (64-bps);
8472 }
8473 }
8474 FORC(width-1) {
8475 sum[ c & 1] += ABS(img[0][c]-img[1][c+1]);
8476 sum[~c & 1] += ABS(img[1][c]-img[0][c+1]);
8477 }
8478 return 100 * log(sum[0]/sum[1]);
8479 }
8480
8481 /*
8482 Identify which camera created this file, and set global variables
8483 accordingly.
8484 */
identify()8485 void CLASS identify()
8486 {
8487 static const short pana[][6] = {
8488 { 3130, 1743, 4, 0, -6, 0 },
8489 { 3130, 2055, 4, 0, -6, 0 },
8490 { 3130, 2319, 4, 0, -6, 0 },
8491 { 3170, 2103, 18, 0,-42, 20 },
8492 { 3170, 2367, 18, 13,-42,-21 },
8493 { 3177, 2367, 0, 0, -1, 0 },
8494 { 3304, 2458, 0, 0, -1, 0 },
8495 { 3330, 2463, 9, 0, -5, 0 },
8496 { 3330, 2479, 9, 0,-17, 4 },
8497 { 3370, 1899, 15, 0,-44, 20 },
8498 { 3370, 2235, 15, 0,-44, 20 },
8499 { 3370, 2511, 15, 10,-44,-21 },
8500 { 3690, 2751, 3, 0, -8, -3 },
8501 { 3710, 2751, 0, 0, -3, 0 },
8502 { 3724, 2450, 0, 0, 0, -2 },
8503 { 3770, 2487, 17, 0,-44, 19 },
8504 { 3770, 2799, 17, 15,-44,-19 },
8505 { 3880, 2170, 6, 0, -6, 0 },
8506 { 4060, 3018, 0, 0, 0, -2 },
8507 { 4290, 2391, 3, 0, -8, -1 },
8508 { 4330, 2439, 17, 15,-44,-19 },
8509 { 4508, 2962, 0, 0, -3, -4 },
8510 { 4508, 3330, 0, 0, -3, -6 },
8511 };
8512 static const ushort canon[][11] = {
8513 { 1944, 1416, 0, 0, 48, 0 },
8514 { 2144, 1560, 4, 8, 52, 2, 0, 0, 0, 25 },
8515 { 2224, 1456, 48, 6, 0, 2 },
8516 { 2376, 1728, 12, 6, 52, 2 },
8517 { 2672, 1968, 12, 6, 44, 2 },
8518 { 3152, 2068, 64, 12, 0, 0, 16 },
8519 { 3160, 2344, 44, 12, 4, 4 },
8520 { 3344, 2484, 4, 6, 52, 6 },
8521 { 3516, 2328, 42, 14, 0, 0 },
8522 { 3596, 2360, 74, 12, 0, 0 },
8523 { 3744, 2784, 52, 12, 8, 12 },
8524 { 3944, 2622, 30, 18, 6, 2 },
8525 { 3948, 2622, 42, 18, 0, 2 },
8526 { 3984, 2622, 76, 20, 0, 2, 14 },
8527 { 4104, 3048, 48, 12, 24, 12 },
8528 { 4116, 2178, 4, 2, 0, 0 },
8529 { 4152, 2772, 192, 12, 0, 0 },
8530 { 4160, 3124, 104, 11, 8, 65 },
8531 { 4176, 3062, 96, 17, 8, 0, 0, 16, 0, 7, 0x49 },
8532 { 4192, 3062, 96, 17, 24, 0, 0, 16, 0, 0, 0x49 },
8533 { 4312, 2876, 22, 18, 0, 2 },
8534 { 4352, 2874, 62, 18, 0, 0 },
8535 { 4476, 2954, 90, 34, 0, 0 },
8536 { 4480, 3348, 12, 10, 36, 12, 0, 0, 0, 18, 0x49 },
8537 { 4480, 3366, 80, 50, 0, 0 },
8538 { 4496, 3366, 80, 50, 12, 0 },
8539 { 4768, 3516, 96, 16, 0, 0, 0, 16 },
8540 { 4832, 3204, 62, 26, 0, 0 },
8541 { 4832, 3228, 62, 51, 0, 0 },
8542 { 5108, 3349, 98, 13, 0, 0 },
8543 { 5120, 3318, 142, 45, 62, 0 },
8544 { 5280, 3528, 72, 52, 0, 0 },
8545 { 5344, 3516, 142, 51, 0, 0 },
8546 { 5344, 3584, 126,100, 0, 2 },
8547 { 5360, 3516, 158, 51, 0, 0 },
8548 { 5568, 3708, 72, 38, 0, 0 },
8549 { 5632, 3710, 96, 17, 0, 0, 0, 16, 0, 0, 0x49 },
8550 { 5712, 3774, 62, 20, 10, 2 },
8551 { 5792, 3804, 158, 51, 0, 0 },
8552 { 5920, 3950, 122, 80, 2, 0 },
8553 { 6096, 4051, 76, 35, 0, 0 },
8554 { 6096, 4056, 72, 34, 0, 0 },
8555 { 6288, 4056, 264, 36, 0, 0 },
8556 { 6384, 4224, 120, 44, 0, 0 },
8557 { 6880, 4544, 136, 42, 0, 0 },
8558 { 8896, 5920, 160, 64, 0, 0 },
8559 };
8560 static const struct {
8561 ushort id;
8562 char model[20];
8563 } unique[] = {
8564 { 0x168, "EOS 10D" }, { 0x001, "EOS-1D" },
8565 { 0x175, "EOS 20D" }, { 0x174, "EOS-1D Mark II" },
8566 { 0x234, "EOS 30D" }, { 0x232, "EOS-1D Mark II N" },
8567 { 0x190, "EOS 40D" }, { 0x169, "EOS-1D Mark III" },
8568 { 0x261, "EOS 50D" }, { 0x281, "EOS-1D Mark IV" },
8569 { 0x287, "EOS 60D" }, { 0x167, "EOS-1DS" },
8570 { 0x325, "EOS 70D" },
8571 { 0x408, "EOS 77D" }, { 0x331, "EOS M" },
8572 { 0x350, "EOS 80D" }, { 0x328, "EOS-1D X Mark II" },
8573 { 0x346, "EOS 100D" },
8574 { 0x417, "EOS 200D" },
8575 { 0x170, "EOS 300D" }, { 0x188, "EOS-1Ds Mark II" },
8576 { 0x176, "EOS 450D" }, { 0x215, "EOS-1Ds Mark III" },
8577 { 0x189, "EOS 350D" }, { 0x324, "EOS-1D C" },
8578 { 0x236, "EOS 400D" }, { 0x269, "EOS-1D X" },
8579 { 0x252, "EOS 500D" }, { 0x213, "EOS 5D" },
8580 { 0x270, "EOS 550D" }, { 0x218, "EOS 5D Mark II" },
8581 { 0x286, "EOS 600D" }, { 0x285, "EOS 5D Mark III" },
8582 { 0x301, "EOS 650D" }, { 0x302, "EOS 6D" },
8583 { 0x326, "EOS 700D" }, { 0x250, "EOS 7D" },
8584 { 0x393, "EOS 750D" }, { 0x289, "EOS 7D Mark II" },
8585 { 0x347, "EOS 760D" }, { 0x406, "EOS 6D Mark II" },
8586 { 0x405, "EOS 800D" }, { 0x349, "EOS 5D Mark IV" },
8587 { 0x254, "EOS 1000D" },
8588 { 0x288, "EOS 1100D" },
8589 { 0x327, "EOS 1200D" }, { 0x382, "EOS 5DS" },
8590 { 0x404, "EOS 1300D" }, { 0x401, "EOS 5DS R" },
8591 { 0x422, "EOS 1500D" },
8592 { 0x432, "EOS 3000D" },
8593 }, sonique[] = {
8594 { 0x002, "DSC-R1" }, { 0x100, "DSLR-A100" },
8595 { 0x101, "DSLR-A900" }, { 0x102, "DSLR-A700" },
8596 { 0x103, "DSLR-A200" }, { 0x104, "DSLR-A350" },
8597 { 0x105, "DSLR-A300" }, { 0x108, "DSLR-A330" },
8598 { 0x109, "DSLR-A230" }, { 0x10a, "DSLR-A290" },
8599 { 0x10d, "DSLR-A850" }, { 0x111, "DSLR-A550" },
8600 { 0x112, "DSLR-A500" }, { 0x113, "DSLR-A450" },
8601 { 0x116, "NEX-5" }, { 0x117, "NEX-3" },
8602 { 0x118, "SLT-A33" }, { 0x119, "SLT-A55V" },
8603 { 0x11a, "DSLR-A560" }, { 0x11b, "DSLR-A580" },
8604 { 0x11c, "NEX-C3" }, { 0x11d, "SLT-A35" },
8605 { 0x11e, "SLT-A65V" }, { 0x11f, "SLT-A77V" },
8606 { 0x120, "NEX-5N" }, { 0x121, "NEX-7" },
8607 { 0x123, "SLT-A37" }, { 0x124, "SLT-A57" },
8608 { 0x125, "NEX-F3" }, { 0x126, "SLT-A99V" },
8609 { 0x127, "NEX-6" }, { 0x128, "NEX-5R" },
8610 { 0x129, "DSC-RX100" }, { 0x12a, "DSC-RX1" },
8611 { 0x12e, "ILCE-3000" }, { 0x12f, "SLT-A58" },
8612 { 0x131, "NEX-3N" }, { 0x132, "ILCE-7" },
8613 { 0x133, "NEX-5T" }, { 0x134, "DSC-RX100M2" },
8614 { 0x135, "DSC-RX10" }, { 0x136, "DSC-RX1R" },
8615 { 0x137, "ILCE-7R" }, { 0x138, "ILCE-6000" },
8616 { 0x139, "ILCE-5000" }, { 0x13d, "DSC-RX100M3" },
8617 { 0x13e, "ILCE-7S" }, { 0x13f, "ILCA-77M2" },
8618 { 0x153, "ILCE-5100" }, { 0x154, "ILCE-7M2" },
8619 { 0x155, "DSC-RX100M4" },{ 0x156, "DSC-RX10M2" },
8620 { 0x158, "DSC-RX1RM2" }, { 0x15a, "ILCE-QX1" },
8621 { 0x15b, "ILCE-7RM2" }, { 0x15e, "ILCE-7SM2" },
8622 { 0x161, "ILCA-68" }, { 0x162, "ILCA-99M2" },
8623 { 0x163, "DSC-RX10M3" }, { 0x164, "DSC-RX100M5" },
8624 { 0x165, "ILCE-6300" }, { 0x166, "ILCE-9" },
8625 { 0x168, "ILCE-6500" }, { 0x16a, "ILCE-7RM3" },
8626 { 0x16b, "ILCE-7M3" }, { 0x16c, "DSC-RX0" },
8627 { 0x16d, "DSC-RX10M4" },
8628 };
8629 static const char *orig, panalias[][12] = {
8630 "@DC-FZ80", "DC-FZ82", "DC-FZ85",
8631 "@DC-FZ81", "DC-FZ83",
8632 "@DC-GF9", "DC-GX800", "DC-GX850",
8633 "@DC-GF10", "DC-GF90",
8634 "@DC-GX9", "DC-GX7MK3",
8635 "@DC-ZS70", "DC-TZ90", "DC-TZ91", "DC-TZ92", "DC-TZ93",
8636 "@DMC-FZ40", "DMC-FZ45",
8637 "@DMC-FZ2500", "DMC-FZ2000", "DMC-FZH1",
8638 "@DMC-G8", "DMC-G80", "DMC-G81", "DMC-G85",
8639 "@DMC-GX85", "DMC-GX80", "DMC-GX7MK2",
8640 "@DMC-LX9", "DMC-LX10", "DMC-LX15",
8641 "@DMC-ZS40", "DMC-TZ60", "DMC-TZ61",
8642 "@DMC-ZS50", "DMC-TZ70", "DMC-TZ71",
8643 "@DMC-ZS60", "DMC-TZ80", "DMC-TZ81", "DMC-TZ85",
8644 "@DMC-ZS100", "DMC-ZS110", "DMC-TZ100", "DMC-TZ101", "DMC-TZ110", "DMC-TX1",
8645 "@DC-ZS200", "DC-TX2", "DC-TZ200", "DC-TZ202", "DC-TZ220", "DC-ZS220",
8646 };
8647 static const struct {
8648 unsigned fsize;
8649 ushort rw, rh;
8650 uchar lm, tm, rm, bm, lf, cf, max, flags;
8651 char make[10], model[20];
8652 ushort offset;
8653 } table[] = {
8654 { 786432,1024, 768, 0, 0, 0, 0, 0,0x94,0,0,"AVT","F-080C" },
8655 { 1447680,1392,1040, 0, 0, 0, 0, 0,0x94,0,0,"AVT","F-145C" },
8656 { 1920000,1600,1200, 0, 0, 0, 0, 0,0x94,0,0,"AVT","F-201C" },
8657 { 5067304,2588,1958, 0, 0, 0, 0, 0,0x94,0,0,"AVT","F-510C" },
8658 { 5067316,2588,1958, 0, 0, 0, 0, 0,0x94,0,0,"AVT","F-510C",12 },
8659 { 10134608,2588,1958, 0, 0, 0, 0, 9,0x94,0,0,"AVT","F-510C" },
8660 { 10134620,2588,1958, 0, 0, 0, 0, 9,0x94,0,0,"AVT","F-510C",12 },
8661 { 16157136,3272,2469, 0, 0, 0, 0, 9,0x94,0,0,"AVT","F-810C" },
8662 { 15980544,3264,2448, 0, 0, 0, 0, 8,0x61,0,1,"AgfaPhoto","DC-833m" },
8663 { 9631728,2532,1902, 0, 0, 0, 0,96,0x61,0,0,"Alcatel","5035D" },
8664 { 2868726,1384,1036, 0, 0, 0, 0,64,0x49,0,8,"Baumer","TXG14",1078 },
8665 { 5298000,2400,1766,12,12,44, 2, 8,0x94,0,2,"Canon","PowerShot SD300" },
8666 { 6553440,2664,1968, 4, 4,44, 4, 8,0x94,0,2,"Canon","PowerShot A460" },
8667 { 6573120,2672,1968,12, 8,44, 0, 8,0x94,0,2,"Canon","PowerShot A610" },
8668 { 6653280,2672,1992,10, 6,42, 2, 8,0x94,0,2,"Canon","PowerShot A530" },
8669 { 7710960,2888,2136,44, 8, 4, 0, 8,0x94,0,2,"Canon","PowerShot S3 IS" },
8670 { 9219600,3152,2340,36,12, 4, 0, 8,0x94,0,2,"Canon","PowerShot A620" },
8671 { 9243240,3152,2346,12, 7,44,13, 8,0x49,0,2,"Canon","PowerShot A470" },
8672 { 10341600,3336,2480, 6, 5,32, 3, 8,0x94,0,2,"Canon","PowerShot A720 IS" },
8673 { 10383120,3344,2484,12, 6,44, 6, 8,0x94,0,2,"Canon","PowerShot A630" },
8674 { 12945240,3736,2772,12, 6,52, 6, 8,0x94,0,2,"Canon","PowerShot A640" },
8675 { 15636240,4104,3048,48,12,24,12, 8,0x94,0,2,"Canon","PowerShot A650" },
8676 { 15467760,3720,2772, 6,12,30, 0, 8,0x94,0,2,"Canon","PowerShot SX110 IS" },
8677 { 15534576,3728,2778,12, 9,44, 9, 8,0x94,0,2,"Canon","PowerShot SX120 IS" },
8678 { 18653760,4080,3048,24,12,24,12, 8,0x94,0,2,"Canon","PowerShot SX20 IS" },
8679 { 19131120,4168,3060,92,16, 4, 1, 8,0x94,0,2,"Canon","PowerShot SX220 HS" },
8680 { 21936096,4464,3276,25,10,73,12, 8,0x16,0,2,"Canon","PowerShot SX30 IS" },
8681 { 24724224,4704,3504, 8,16,56, 8, 8,0x94,0,2,"Canon","PowerShot A3300 IS" },
8682 { 30858240,5248,3920, 8,16,56,16, 8,0x94,0,2,"Canon","IXUS 160" },
8683 { 1976352,1632,1211, 0, 2, 0, 1, 0,0x94,0,1,"Casio","QV-2000UX" },
8684 { 3217760,2080,1547, 0, 0,10, 1, 0,0x94,0,1,"Casio","QV-3*00EX" },
8685 { 6218368,2585,1924, 0, 0, 9, 0, 0,0x94,0,1,"Casio","QV-5700" },
8686 { 7816704,2867,2181, 0, 0,34,36, 0,0x16,0,1,"Casio","EX-Z60" },
8687 { 2937856,1621,1208, 0, 0, 1, 0, 0,0x94,7,13,"Casio","EX-S20" },
8688 { 4948608,2090,1578, 0, 0,32,34, 0,0x94,7,1,"Casio","EX-S100" },
8689 { 6054400,2346,1720, 2, 0,32, 0, 0,0x94,7,1,"Casio","QV-R41" },
8690 { 7426656,2568,1928, 0, 0, 0, 0, 0,0x94,0,1,"Casio","EX-P505" },
8691 { 7530816,2602,1929, 0, 0,22, 0, 0,0x94,7,1,"Casio","QV-R51" },
8692 { 7542528,2602,1932, 0, 0,32, 0, 0,0x94,7,1,"Casio","EX-Z50" },
8693 { 7562048,2602,1937, 0, 0,25, 0, 0,0x16,7,1,"Casio","EX-Z500" },
8694 { 7753344,2602,1986, 0, 0,32,26, 0,0x94,7,1,"Casio","EX-Z55" },
8695 { 9313536,2858,2172, 0, 0,14,30, 0,0x94,7,1,"Casio","EX-P600" },
8696 { 10834368,3114,2319, 0, 0,27, 0, 0,0x94,0,1,"Casio","EX-Z750" },
8697 { 10843712,3114,2321, 0, 0,25, 0, 0,0x94,0,1,"Casio","EX-Z75" },
8698 { 10979200,3114,2350, 0, 0,32,32, 0,0x94,7,1,"Casio","EX-P700" },
8699 { 12310144,3285,2498, 0, 0, 6,30, 0,0x94,0,1,"Casio","EX-Z850" },
8700 { 12489984,3328,2502, 0, 0,47,35, 0,0x94,0,1,"Casio","EX-Z8" },
8701 { 15499264,3754,2752, 0, 0,82, 0, 0,0x94,0,1,"Casio","EX-Z1050" },
8702 { 18702336,4096,3044, 0, 0,24, 0,80,0x94,7,1,"Casio","EX-ZR100" },
8703 { 7684000,2260,1700, 0, 0, 0, 0,13,0x94,0,1,"Casio","QV-4000" },
8704 { 787456,1024, 769, 0, 1, 0, 0, 0,0x49,0,0,"Creative","PC-CAM 600" },
8705 { 28829184,4384,3288, 0, 0, 0, 0,36,0x61,0,0,"DJI" },
8706 { 15151104,4608,3288, 0, 0, 0, 0, 0,0x94,0,0,"Matrix" },
8707 { 3840000,1600,1200, 0, 0, 0, 0,65,0x49,0,0,"Foculus","531C" },
8708 { 307200, 640, 480, 0, 0, 0, 0, 0,0x94,0,0,"Generic" },
8709 { 62464, 256, 244, 1, 1, 6, 1, 0,0x8d,0,0,"Kodak","DC20" },
8710 { 124928, 512, 244, 1, 1,10, 1, 0,0x8d,0,0,"Kodak","DC20" },
8711 { 1652736,1536,1076, 0,52, 0, 0, 0,0x61,0,0,"Kodak","DCS200" },
8712 { 4159302,2338,1779, 1,33, 1, 2, 0,0x94,0,0,"Kodak","C330" },
8713 { 4162462,2338,1779, 1,33, 1, 2, 0,0x94,0,0,"Kodak","C330",3160 },
8714 { 2247168,1232, 912, 0, 0,16, 0, 0,0x00,0,0,"Kodak","C330" },
8715 { 3370752,1232, 912, 0, 0,16, 0, 0,0x00,0,0,"Kodak","C330" },
8716 { 6163328,2864,2152, 0, 0, 0, 0, 0,0x94,0,0,"Kodak","C603" },
8717 { 6166488,2864,2152, 0, 0, 0, 0, 0,0x94,0,0,"Kodak","C603",3160 },
8718 { 460800, 640, 480, 0, 0, 0, 0, 0,0x00,0,0,"Kodak","C603" },
8719 { 9116448,2848,2134, 0, 0, 0, 0, 0,0x00,0,0,"Kodak","C603" },
8720 { 12241200,4040,3030, 2, 0, 0,13, 0,0x49,0,0,"Kodak","12MP" },
8721 { 12272756,4040,3030, 2, 0, 0,13, 0,0x49,0,0,"Kodak","12MP",31556 },
8722 { 18000000,4000,3000, 0, 0, 0, 0, 0,0x00,0,0,"Kodak","12MP" },
8723 { 614400, 640, 480, 0, 3, 0, 0,64,0x94,0,0,"Kodak","KAI-0340" },
8724 { 15360000,3200,2400, 0, 0, 0, 0,96,0x16,0,0,"Lenovo","A820" },
8725 { 3884928,1608,1207, 0, 0, 0, 0,96,0x16,0,0,"Micron","2010",3212 },
8726 { 1138688,1534, 986, 0, 0, 0, 0, 0,0x61,0,0,"Minolta","RD175",513 },
8727 { 1581060,1305, 969, 0, 0,18, 6, 6,0x1e,4,1,"Nikon","E900" },
8728 { 2465792,1638,1204, 0, 0,22, 1, 6,0x4b,5,1,"Nikon","E950" },
8729 { 2940928,1616,1213, 0, 0, 0, 7,30,0x94,0,1,"Nikon","E2100" },
8730 { 4771840,2064,1541, 0, 0, 0, 1, 6,0xe1,0,1,"Nikon","E990" },
8731 { 4775936,2064,1542, 0, 0, 0, 0,30,0x94,0,1,"Nikon","E3700" },
8732 { 5865472,2288,1709, 0, 0, 0, 1, 6,0xb4,0,1,"Nikon","E4500" },
8733 { 5869568,2288,1710, 0, 0, 0, 0, 6,0x16,0,1,"Nikon","E4300" },
8734 { 7438336,2576,1925, 0, 0, 0, 1, 6,0xb4,0,1,"Nikon","E5000" },
8735 { 8998912,2832,2118, 0, 0, 0, 0,30,0x94,7,1,"Nikon","COOLPIX S6" },
8736 { 5939200,2304,1718, 0, 0, 0, 0,30,0x16,0,0,"Olympus","C770UZ" },
8737 { 3178560,2064,1540, 0, 0, 0, 0, 0,0x94,0,1,"Pentax","Optio S" },
8738 { 4841984,2090,1544, 0, 0,22, 0, 0,0x94,7,1,"Pentax","Optio S" },
8739 { 6114240,2346,1737, 0, 0,22, 0, 0,0x94,7,1,"Pentax","Optio S4" },
8740 { 10702848,3072,2322, 0, 0, 0,21,30,0x94,0,1,"Pentax","Optio 750Z" },
8741 { 4147200,1920,1080, 0, 0, 0, 0, 0,0x49,0,0,"Photron","BC2-HD" },
8742 { 4151666,1920,1080, 0, 0, 0, 0, 0,0x49,0,0,"Photron","BC2-HD",8 },
8743 { 13248000,2208,3000, 0, 0, 0, 0,13,0x61,0,0,"Pixelink","A782" },
8744 { 6291456,2048,1536, 0, 0, 0, 0,96,0x61,0,0,"RoverShot","3320AF" },
8745 { 311696, 644, 484, 0, 0, 0, 0, 0,0x16,0,8,"ST Micro","STV680 VGA" },
8746 { 16098048,3288,2448, 0, 0,24, 0, 9,0x94,0,1,"Samsung","S85" },
8747 { 16215552,3312,2448, 0, 0,48, 0, 9,0x94,0,1,"Samsung","S85" },
8748 { 20487168,3648,2808, 0, 0, 0, 0,13,0x94,5,1,"Samsung","WB550" },
8749 { 24000000,4000,3000, 0, 0, 0, 0,13,0x94,5,1,"Samsung","WB550" },
8750 { 12582980,3072,2048, 0, 0, 0, 0,33,0x61,0,0,"Sinar","",68 },
8751 { 33292868,4080,4080, 0, 0, 0, 0,33,0x61,0,0,"Sinar","",68 },
8752 { 44390468,4080,5440, 0, 0, 0, 0,33,0x61,0,0,"Sinar","",68 },
8753 { 1409024,1376,1024, 0, 0, 1, 0, 0,0x49,0,0,"Sony","XCD-SX910CR" },
8754 { 2818048,1376,1024, 0, 0, 1, 0,97,0x49,0,0,"Sony","XCD-SX910CR" },
8755 { 17496000,4320,3240, 0, 0, 0,0,224,0x94,0,0,"Xiro","Xplorer V" },
8756 };
8757 static const char *corp[] =
8758 { "AgfaPhoto", "Canon", "Casio", "Epson", "Fujifilm",
8759 "Mamiya", "Minolta", "Motorola", "Kodak", "Konica", "Leica",
8760 "Nikon", "Nokia", "Olympus", "Ricoh", "Pentax", "Phase One",
8761 "Samsung", "Sigma", "Sinar", "Sony", "YI" };
8762 char head[32], *cp;
8763 int hlen, flen, fsize, zero_fsize=1, i, c;
8764 struct jhead jh;
8765
8766 tiff_flip = flip = filters = UINT_MAX; /* unknown */
8767 raw_height = raw_width = fuji_width = fuji_layout = cr2_slice[0] = 0;
8768 maximum = height = width = top_margin = left_margin = 0;
8769 cdesc[0] = desc[0] = artist[0] = make[0] = model[0] = model2[0] = 0;
8770 iso_speed = shutter = aperture = focal_len = unique_id = 0;
8771 tiff_nifds = 0;
8772 memset (tiff_ifd, 0, sizeof tiff_ifd);
8773 memset (gpsdata, 0, sizeof gpsdata);
8774 memset (cblack, 0, sizeof cblack);
8775 memset (white, 0, sizeof white);
8776 memset (mask, 0, sizeof mask);
8777 thumb_offset = thumb_length = thumb_width = thumb_height = 0;
8778 load_raw = thumb_load_raw = 0;
8779 write_thumb = &CLASS jpeg_thumb;
8780 data_offset = meta_offset = meta_length = tiff_bps = tiff_compress = 0;
8781 kodak_cbpp = zero_after_ff = dng_version = load_flags = 0;
8782 timestamp = shot_order = tiff_samples = black = is_foveon = 0;
8783 mix_green = profile_length = data_error = zero_is_bad = 0;
8784 pixel_aspect = is_raw = raw_color = 1;
8785 tile_width = tile_length = 0;
8786 for (i=0; i < 4; i++) {
8787 cam_mul[i] = i == 1;
8788 pre_mul[i] = i < 3;
8789 FORC3 cmatrix[c][i] = 0;
8790 FORC3 rgb_cam[c][i] = c == i;
8791 }
8792 colors = 3;
8793 for (i=0; i < 0x10000; i++) curve[i] = i;
8794
8795 order = get2();
8796 hlen = get4();
8797 fseek (ifp, 0, SEEK_SET);
8798 fread (head, 1, 32, ifp);
8799 fseek (ifp, 0, SEEK_END);
8800 flen = fsize = ftell(ifp);
8801 if ((cp = (char *) memmem (head, 32, "MMMM", 4)) ||
8802 (cp = (char *) memmem (head, 32, "IIII", 4))) {
8803 parse_phase_one (cp-head);
8804 if (cp-head && parse_tiff(0)) apply_tiff();
8805 } else if (order == 0x4949 || order == 0x4d4d) {
8806 if (!memcmp (head+6,"HEAPCCDR",8)) {
8807 data_offset = hlen;
8808 parse_ciff (hlen, flen-hlen, 0);
8809 load_raw = &CLASS canon_load_raw;
8810 } else if (parse_tiff(0)) apply_tiff();
8811 } else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
8812 !memcmp (head+6,"Exif",4)) {
8813 fseek (ifp, 4, SEEK_SET);
8814 data_offset = 4 + get2();
8815 fseek (ifp, data_offset, SEEK_SET);
8816 if (fgetc(ifp) != 0xff)
8817 parse_tiff(12);
8818 thumb_offset = 0;
8819 } else if (!memcmp (head+25,"ARECOYK",7)) {
8820 strcpy (make, "Contax");
8821 strcpy (model,"N Digital");
8822 fseek (ifp, 33, SEEK_SET);
8823 get_timestamp(1);
8824 fseek (ifp, 60, SEEK_SET);
8825 FORC4 cam_mul[c ^ (c >> 1)] = get4();
8826 } else if (!strcmp (head, "PXN")) {
8827 strcpy (make, "Logitech");
8828 strcpy (model,"Fotoman Pixtura");
8829 } else if (!strcmp (head, "qktk")) {
8830 strcpy (make, "Apple");
8831 strcpy (model,"QuickTake 100");
8832 load_raw = &CLASS quicktake_100_load_raw;
8833 } else if (!strcmp (head, "qktn")) {
8834 strcpy (make, "Apple");
8835 strcpy (model,"QuickTake 150");
8836 load_raw = &CLASS kodak_radc_load_raw;
8837 } else if (!memcmp (head,"FUJIFILM",8)) {
8838 fseek (ifp, 84, SEEK_SET);
8839 thumb_offset = get4();
8840 thumb_length = get4();
8841 fseek (ifp, 92, SEEK_SET);
8842 parse_fuji (get4());
8843 if (thumb_offset > 120) {
8844 fseek (ifp, 120, SEEK_SET);
8845 is_raw += (i = get4()) && 1;
8846 if (is_raw == 2 && shot_select)
8847 parse_fuji (i);
8848 }
8849 fseek (ifp, 100+28*(shot_select > 0), SEEK_SET);
8850 parse_tiff (data_offset = get4());
8851 parse_tiff (thumb_offset+12);
8852 apply_tiff();
8853 if (!load_raw) {
8854 load_raw = &CLASS unpacked_load_raw;
8855 tiff_bps = 14;
8856 }
8857 } else if (!memcmp (head,"RIFF",4)) {
8858 fseek (ifp, 0, SEEK_SET);
8859 parse_riff();
8860 } else if (!memcmp (head+4,"ftypcrx ",8)) {
8861 fseek (ifp, 0, SEEK_SET);
8862 parse_crx (fsize);
8863 } else if (!memcmp (head+4,"ftypqt ",9)) {
8864 fseek (ifp, 0, SEEK_SET);
8865 parse_qt (fsize);
8866 is_raw = 0;
8867 } else if (!memcmp (head,"\0\001\0\001\0@",6)) {
8868 fseek (ifp, 6, SEEK_SET);
8869 fread (make, 1, 8, ifp);
8870 fread (model, 1, 8, ifp);
8871 fread (model2, 1, 16, ifp);
8872 data_offset = get2();
8873 get2();
8874 raw_width = get2();
8875 raw_height = get2();
8876 load_raw = &CLASS nokia_load_raw;
8877 filters = 0x61616161;
8878 } else if (!memcmp (head,"NOKIARAW",8)) {
8879 strcpy (make, "NOKIA");
8880 order = 0x4949;
8881 fseek (ifp, 300, SEEK_SET);
8882 data_offset = get4();
8883 i = get4();
8884 width = get2();
8885 height = get2();
8886 switch (tiff_bps = i*8 / (width * height)) {
8887 case 8: load_raw = &CLASS eight_bit_load_raw; break;
8888 case 10: load_raw = &CLASS nokia_load_raw;
8889 }
8890 raw_height = height + (top_margin = i / (width * tiff_bps/8) - height);
8891 mask[0][3] = 1;
8892 filters = 0x61616161;
8893 } else if (!memcmp (head,"ARRI",4)) {
8894 order = 0x4949;
8895 fseek (ifp, 20, SEEK_SET);
8896 width = get4();
8897 height = get4();
8898 strcpy (make, "ARRI");
8899 fseek (ifp, 668, SEEK_SET);
8900 fread (model, 1, 64, ifp);
8901 data_offset = 4096;
8902 load_raw = &CLASS packed_load_raw;
8903 load_flags = 88;
8904 filters = 0x61616161;
8905 } else if (!memcmp (head,"XPDS",4)) {
8906 order = 0x4949;
8907 fseek (ifp, 0x800, SEEK_SET);
8908 fread (make, 1, 41, ifp);
8909 raw_height = get2();
8910 raw_width = get2();
8911 fseek (ifp, 56, SEEK_CUR);
8912 fread (model, 1, 30, ifp);
8913 data_offset = 0x10000;
8914 load_raw = &CLASS canon_rmf_load_raw;
8915 gamma_curve (0, 12.25, 1, 1023);
8916 } else if (!memcmp (head+4,"RED1",4)) {
8917 strcpy (make, "Red");
8918 strcpy (model,"One");
8919 parse_redcine();
8920 load_raw = &CLASS redcine_load_raw;
8921 gamma_curve (1/2.4, 12.92, 1, 4095);
8922 filters = 0x49494949;
8923 } else if (!memcmp (head,"DSC-Image",9))
8924 parse_rollei();
8925 else if (!memcmp (head,"PWAD",4))
8926 parse_sinar_ia();
8927 else if (!memcmp (head,"\0MRM",4))
8928 parse_minolta(0);
8929 else if (!memcmp (head,"FOVb",4))
8930 parse_foveon();
8931 else if (!memcmp (head,"CI",2))
8932 parse_cine();
8933 if (make[0] == 0)
8934 for (zero_fsize=i=0; i < sizeof table / sizeof *table; i++)
8935 if (fsize == table[i].fsize) {
8936 strcpy (make, table[i].make );
8937 strcpy (model, table[i].model);
8938 flip = table[i].flags >> 2;
8939 zero_is_bad = table[i].flags & 2;
8940 if (table[i].flags & 1)
8941 parse_external_jpeg();
8942 data_offset = table[i].offset;
8943 raw_width = table[i].rw;
8944 raw_height = table[i].rh;
8945 left_margin = table[i].lm;
8946 top_margin = table[i].tm;
8947 width = raw_width - left_margin - table[i].rm;
8948 height = raw_height - top_margin - table[i].bm;
8949 filters = 0x1010101 * table[i].cf;
8950 colors = 4 - !((filters & filters >> 1) & 0x5555);
8951 load_flags = table[i].lf;
8952 switch (tiff_bps = (fsize-data_offset)*8 / (raw_width*raw_height)) {
8953 case 6:
8954 load_raw = &CLASS minolta_rd175_load_raw; break;
8955 case 8:
8956 load_raw = &CLASS eight_bit_load_raw; break;
8957 case 10: case 12:
8958 load_flags |= 512;
8959 if (!strcmp(make,"Canon")) load_flags |= 256;
8960 load_raw = &CLASS packed_load_raw; break;
8961 case 16:
8962 order = 0x4949 | 0x404 * (load_flags & 1);
8963 tiff_bps -= load_flags >> 4;
8964 tiff_bps -= load_flags = load_flags >> 1 & 7;
8965 load_raw = &CLASS unpacked_load_raw;
8966 }
8967 maximum = (1 << tiff_bps) - (1 << table[i].max);
8968 }
8969 if (zero_fsize) fsize = 0;
8970 if (make[0] == 0) parse_smal (0, flen);
8971 if (make[0] == 0) {
8972 parse_jpeg(0);
8973 if (!(strncmp(model,"ov",2) && strncmp(model,"RP_OV",5)) &&
8974 !fseek (ifp, -6404096, SEEK_END) &&
8975 fread (head, 1, 32, ifp) && !strcmp(head,"BRCMn")) {
8976 strcpy (make, "OmniVision");
8977 data_offset = ftell(ifp) + 0x8000-32;
8978 width = raw_width;
8979 raw_width = 2611;
8980 load_raw = &CLASS nokia_load_raw;
8981 filters = 0x16161616;
8982 } else is_raw = 0;
8983 }
8984
8985 for (i=0; i < sizeof corp / sizeof *corp; i++)
8986 if (strcasestr (make, corp[i])) /* Simplify company names */
8987 strcpy (make, corp[i]);
8988 if ((!strcmp(make,"Kodak") || !strcmp(make,"Leica")) &&
8989 ((cp = strcasestr(model," DIGITAL CAMERA")) ||
8990 (cp = strstr(model,"FILE VERSION"))))
8991 *cp = 0;
8992 if (!strncasecmp(model,"PENTAX",6))
8993 strcpy (make, "Pentax");
8994 cp = make + strlen(make); /* Remove trailing spaces */
8995 while (*--cp == ' ') *cp = 0;
8996 cp = model + strlen(model);
8997 while (*--cp == ' ') *cp = 0;
8998 i = strlen(make); /* Remove make from model */
8999 if (!strncasecmp (model, make, i) && model[i++] == ' ')
9000 memmove (model, model+i, 64-i);
9001 if (!strncmp (model,"FinePix ",8))
9002 strcpy (model, model+8);
9003 if (!strncmp (model,"Digital Camera ",15))
9004 strcpy (model, model+15);
9005 desc[511] = artist[63] = make[63] = model[63] = model2[63] = 0;
9006 if (!is_raw) goto notraw;
9007
9008 if (!height) height = raw_height;
9009 if (!width) width = raw_width;
9010 if (height == 2624 && width == 3936) /* Pentax K10D and Samsung GX10 */
9011 { height = 2616; width = 3896; }
9012 if (height == 3136 && width == 4864) /* Pentax K20D and Samsung GX20 */
9013 { height = 3124; width = 4688; filters = 0x16161616; }
9014 if (raw_height == 2868 && (!strcmp(model,"K-r") || !strcmp(model,"K-x")))
9015 { width = 4309; filters = 0x16161616; }
9016 if (raw_height == 3136 && !strcmp(model,"K-7"))
9017 { height = 3122; width = 4684; filters = 0x16161616; top_margin = 2; }
9018 if (raw_height == 3284 && !strncmp(model,"K-5",3))
9019 { left_margin = 10; width = 4950; filters = 0x16161616; }
9020 if (raw_height == 3300 && !strncmp(model,"K-50",4))
9021 { height = 3288, width = 4952; left_margin = 0; top_margin = 12; }
9022 if (raw_height == 3664 && !strncmp(model,"K-S",3))
9023 { width = 5492; left_margin = 0; }
9024 if (raw_height == 4032 && !strcmp(model,"K-3"))
9025 { height = 4032; width = 6040; left_margin = 4; }
9026 if (raw_height == 4060 && !strcmp(model,"KP"))
9027 { height = 4032; width = 6032; left_margin = 52; top_margin = 28; }
9028 if (raw_height == 4950 && !strcmp(model,"K-1"))
9029 { height = 4932; width = 7380; left_margin = 4; top_margin = 18; }
9030 if (raw_height == 5552 && !strcmp(model,"645D"))
9031 { height = 5502; width = 7328; left_margin = 48; top_margin = 29;
9032 filters = 0x61616161; }
9033 if (height == 3014 && width == 4096) /* Ricoh GX200 */
9034 width = 4014;
9035 if (dng_version) {
9036 if (filters == UINT_MAX) filters = 0;
9037 if (filters) is_raw *= tiff_samples;
9038 else colors = tiff_samples;
9039 switch (tiff_compress) {
9040 case 0:
9041 case 1: load_raw = &CLASS packed_dng_load_raw; break;
9042 case 7: load_raw = &CLASS lossless_dng_load_raw; break;
9043 case 34892: load_raw = &CLASS lossy_dng_load_raw; break;
9044 default: load_raw = 0;
9045 }
9046 goto dng_skip;
9047 }
9048 if (!strcmp(make,"Canon") && !fsize && tiff_bps != 15) {
9049 if (!load_raw)
9050 load_raw = &CLASS lossless_jpeg_load_raw;
9051 for (i=0; i < sizeof canon / sizeof *canon; i++)
9052 if (raw_width == canon[i][0] && raw_height == canon[i][1]) {
9053 width = raw_width - (left_margin = canon[i][2]);
9054 height = raw_height - (top_margin = canon[i][3]);
9055 width -= canon[i][4];
9056 height -= canon[i][5];
9057 mask[0][1] = canon[i][6];
9058 mask[0][3] = -canon[i][7];
9059 mask[1][1] = canon[i][8];
9060 mask[1][3] = -canon[i][9];
9061 if (canon[i][10]) filters = canon[i][10] * 0x01010101;
9062 }
9063 if ((unique_id | 0x20000) == 0x2720000) {
9064 left_margin = 8;
9065 top_margin = 16;
9066 }
9067 }
9068 for (i=0; i < sizeof unique / sizeof *unique; i++)
9069 if (unique_id == 0x80000000 + unique[i].id) {
9070 adobe_coeff ("Canon", unique[i].model);
9071 if (model[4] == 'K' && strlen(model) == 8)
9072 strcpy (model, unique[i].model);
9073 }
9074 for (i=0; i < sizeof sonique / sizeof *sonique; i++)
9075 if (unique_id == sonique[i].id)
9076 strcpy (model, sonique[i].model);
9077 for (i=0; i < sizeof panalias / sizeof *panalias; i++)
9078 if (panalias[i][0] == '@') orig = panalias[i]+1;
9079 else if (!strcmp(model,panalias[i]))
9080 adobe_coeff ("Panasonic", orig);
9081 if (!strcmp(make,"Nikon")) {
9082 if (!load_raw)
9083 load_raw = &CLASS packed_load_raw;
9084 if (model[0] == 'E')
9085 load_flags |= !data_offset << 2 | 2;
9086 }
9087
9088 /* Set parameters based on camera name (for non-DNG files). */
9089
9090 if (!strcmp(model,"KAI-0340")
9091 && find_green (16, 16, 3840, 5120) < 25) {
9092 height = 480;
9093 top_margin = filters = 0;
9094 strcpy (model,"C603");
9095 }
9096 if (!strcmp(make,"Sony") && raw_width > 3888)
9097 black = 128 << (tiff_bps - 12);
9098 if (is_foveon) {
9099 if (height*2 < width) pixel_aspect = 0.5;
9100 if (height > width) pixel_aspect = 2;
9101 filters = 0;
9102 simple_coeff(0);
9103 } else if (!strcmp(make,"Canon") && tiff_bps == 15) {
9104 switch (width) {
9105 case 3344: width -= 66;
9106 case 3872: width -= 6;
9107 }
9108 if (height > width) {
9109 SWAP(height,width);
9110 SWAP(raw_height,raw_width);
9111 }
9112 if (width == 7200 && height == 3888) {
9113 raw_width = width = 6480;
9114 raw_height = height = 4320;
9115 }
9116 filters = 0;
9117 tiff_samples = colors = 3;
9118 load_raw = &CLASS canon_sraw_load_raw;
9119 } else if (!strcmp(model,"PowerShot 600")) {
9120 height = 613;
9121 width = 854;
9122 raw_width = 896;
9123 colors = 4;
9124 filters = 0xe1e4e1e4;
9125 load_raw = &CLASS canon_600_load_raw;
9126 } else if (!strcmp(model,"PowerShot A5") ||
9127 !strcmp(model,"PowerShot A5 Zoom")) {
9128 height = 773;
9129 width = 960;
9130 raw_width = 992;
9131 pixel_aspect = 256/235.0;
9132 filters = 0x1e4e1e4e;
9133 goto canon_a5;
9134 } else if (!strcmp(model,"PowerShot A50")) {
9135 height = 968;
9136 width = 1290;
9137 raw_width = 1320;
9138 filters = 0x1b4e4b1e;
9139 goto canon_a5;
9140 } else if (!strcmp(model,"PowerShot Pro70")) {
9141 height = 1024;
9142 width = 1552;
9143 filters = 0x1e4b4e1b;
9144 canon_a5:
9145 colors = 4;
9146 tiff_bps = 10;
9147 load_raw = &CLASS packed_load_raw;
9148 load_flags = 264;
9149 } else if (!strcmp(model,"PowerShot Pro90 IS") ||
9150 !strcmp(model,"PowerShot G1")) {
9151 colors = 4;
9152 filters = 0xb4b4b4b4;
9153 } else if (!strcmp(model,"PowerShot A610")) {
9154 if (canon_s2is()) strcpy (model+10, "S2 IS");
9155 } else if (!strcmp(model,"PowerShot SX220 HS")) {
9156 mask[1][3] = -4;
9157 } else if (!strcmp(model,"EOS D2000C")) {
9158 filters = 0x61616161;
9159 black = curve[200];
9160 } else if (!strcmp(model,"EOS 80D")) {
9161 top_margin -= 2;
9162 height += 2;
9163 } else if (!strcmp(model,"D1")) {
9164 cam_mul[0] *= 256/527.0;
9165 cam_mul[2] *= 256/317.0;
9166 } else if (!strcmp(model,"D1X")) {
9167 width -= 4;
9168 pixel_aspect = 0.5;
9169 } else if (!strcmp(model,"D40X") ||
9170 !strcmp(model,"D60") ||
9171 !strcmp(model,"D80") ||
9172 !strcmp(model,"D3000")) {
9173 height -= 3;
9174 width -= 4;
9175 } else if (!strcmp(model,"D3") ||
9176 !strcmp(model,"D3S") ||
9177 !strcmp(model,"D700")) {
9178 width -= 4;
9179 left_margin = 2;
9180 } else if (!strcmp(model,"D3100")) {
9181 width -= 28;
9182 left_margin = 6;
9183 } else if (!strcmp(model,"D5000") ||
9184 !strcmp(model,"D90")) {
9185 width -= 42;
9186 } else if (!strcmp(model,"D5100") ||
9187 !strcmp(model,"D7000") ||
9188 !strcmp(model,"COOLPIX A")) {
9189 width -= 44;
9190 } else if (!strcmp(model,"D3200") ||
9191 !strncmp(model,"D6",2) ||
9192 !strncmp(model,"D800",4)) {
9193 width -= 46;
9194 } else if (!strcmp(model,"D4") ||
9195 !strcmp(model,"Df")) {
9196 width -= 52;
9197 left_margin = 2;
9198 } else if (!strncmp(model,"D40",3) ||
9199 !strncmp(model,"D50",3) ||
9200 !strncmp(model,"D70",3)) {
9201 width--;
9202 } else if (!strcmp(model,"D100")) {
9203 if (load_flags)
9204 raw_width = (width += 3) + 3;
9205 } else if (!strcmp(model,"D200")) {
9206 left_margin = 1;
9207 width -= 4;
9208 filters = 0x94949494;
9209 } else if (!strncmp(model,"D2H",3)) {
9210 left_margin = 6;
9211 width -= 14;
9212 } else if (!strncmp(model,"D2X",3)) {
9213 if (width == 3264) width -= 32;
9214 else width -= 8;
9215 } else if (!strncmp(model,"D300",4)) {
9216 width -= 32;
9217 } else if (!strncmp(model,"COOLPIX B",9)) {
9218 load_flags = 24;
9219 } else if (!strncmp(model,"COOLPIX P",9) && raw_width != 4032) {
9220 load_flags = 24;
9221 filters = 0x94949494;
9222 if (model[9] == '7' && iso_speed >= 400)
9223 black = 255;
9224 } else if (!strncmp(model,"1 ",2)) {
9225 height -= 2;
9226 } else if (fsize == 1581060) {
9227 simple_coeff(3);
9228 pre_mul[0] = 1.2085;
9229 pre_mul[1] = 1.0943;
9230 pre_mul[3] = 1.1103;
9231 } else if (fsize == 3178560) {
9232 cam_mul[0] *= 4;
9233 cam_mul[2] *= 4;
9234 } else if (fsize == 4771840) {
9235 if (!timestamp && nikon_e995())
9236 strcpy (model, "E995");
9237 if (strcmp(model,"E995")) {
9238 filters = 0xb4b4b4b4;
9239 simple_coeff(3);
9240 pre_mul[0] = 1.196;
9241 pre_mul[1] = 1.246;
9242 pre_mul[2] = 1.018;
9243 }
9244 } else if (fsize == 2940928) {
9245 if (!timestamp && !nikon_e2100())
9246 strcpy (model,"E2500");
9247 if (!strcmp(model,"E2500")) {
9248 height -= 2;
9249 load_flags = 6;
9250 colors = 4;
9251 filters = 0x4b4b4b4b;
9252 }
9253 } else if (fsize == 4775936) {
9254 if (!timestamp) nikon_3700();
9255 if (model[0] == 'E' && atoi(model+1) < 3700)
9256 filters = 0x49494949;
9257 if (!strcmp(model,"Optio 33WR")) {
9258 flip = 1;
9259 filters = 0x16161616;
9260 }
9261 if (make[0] == 'O') {
9262 i = find_green (12, 32, 1188864, 3576832);
9263 c = find_green (12, 32, 2383920, 2387016);
9264 if (abs(i) < abs(c)) {
9265 SWAP(i,c);
9266 load_flags = 24;
9267 }
9268 if (i < 0) filters = 0x61616161;
9269 }
9270 } else if (fsize == 5869568) {
9271 if (!timestamp && minolta_z2()) {
9272 strcpy (make, "Minolta");
9273 strcpy (model,"DiMAGE Z2");
9274 }
9275 load_flags = 6 + 24*(make[0] == 'M');
9276 } else if (fsize == 6291456) {
9277 fseek (ifp, 0x300000, SEEK_SET);
9278 if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
9279 height -= (top_margin = 16);
9280 width -= (left_margin = 28);
9281 maximum = 0xf5c0;
9282 strcpy (make, "ISG");
9283 model[0] = 0;
9284 }
9285 } else if (!strcmp(make,"Fujifilm")) {
9286 if (!strcmp(model+7,"S2Pro")) {
9287 strcpy (model,"S2Pro");
9288 height = 2144;
9289 width = 2880;
9290 flip = 6;
9291 }
9292 top_margin = (raw_height - height) >> 2 << 1;
9293 left_margin = (raw_width - width ) >> 2 << 1;
9294 if (width == 2848 || width == 3664) filters = 0x16161616;
9295 if (width == 4032 || width == 4952 || width == 6032 || width == 8280) left_margin = 0;
9296 if (width == 3328 && (width -= 66)) left_margin = 34;
9297 if (width == 4936) left_margin = 4;
9298 if (!strcmp(model,"HS50EXR") ||
9299 !strcmp(model,"F900EXR")) {
9300 width += 2;
9301 left_margin = 0;
9302 filters = 0x16161616;
9303 }
9304 if (fuji_layout) raw_width *= is_raw;
9305 if (filters == 9)
9306 FORC(36) ((char *)xtrans)[c] =
9307 xtrans_abs[(c/6+top_margin) % 6][(c+left_margin) % 6];
9308 } else if (!strcmp(model,"KD-400Z")) {
9309 height = 1712;
9310 width = 2312;
9311 raw_width = 2336;
9312 goto konica_400z;
9313 } else if (!strcmp(model,"KD-510Z")) {
9314 goto konica_510z;
9315 } else if (!strcasecmp(make,"Minolta")) {
9316 if (!load_raw && (maximum = 0xfff))
9317 load_raw = &CLASS unpacked_load_raw;
9318 if (!strncmp(model,"DiMAGE A",8)) {
9319 if (!strcmp(model,"DiMAGE A200"))
9320 filters = 0x49494949;
9321 tiff_bps = 12;
9322 load_raw = &CLASS packed_load_raw;
9323 } else if (!strncmp(model,"ALPHA",5) ||
9324 !strncmp(model,"DYNAX",5) ||
9325 !strncmp(model,"MAXXUM",6)) {
9326 sprintf (model+20, "DYNAX %-10s", model+6+(model[0]=='M'));
9327 adobe_coeff (make, model+20);
9328 load_raw = &CLASS packed_load_raw;
9329 } else if (!strncmp(model,"DiMAGE G",8)) {
9330 if (model[8] == '4') {
9331 height = 1716;
9332 width = 2304;
9333 } else if (model[8] == '5') {
9334 konica_510z:
9335 height = 1956;
9336 width = 2607;
9337 raw_width = 2624;
9338 } else if (model[8] == '6') {
9339 height = 2136;
9340 width = 2848;
9341 }
9342 data_offset += 14;
9343 filters = 0x61616161;
9344 konica_400z:
9345 load_raw = &CLASS unpacked_load_raw;
9346 maximum = 0x3df;
9347 order = 0x4d4d;
9348 }
9349 } else if (!strcmp(model,"*ist D")) {
9350 load_raw = &CLASS unpacked_load_raw;
9351 data_error = -1;
9352 } else if (!strcmp(model,"*ist DS")) {
9353 height -= 2;
9354 } else if (!strcmp(make,"Samsung") && raw_width == 4704) {
9355 height -= top_margin = 8;
9356 width -= 2 * (left_margin = 8);
9357 load_flags = 256;
9358 } else if (!strcmp(make,"Samsung") && raw_height == 3714) {
9359 height -= top_margin = 18;
9360 left_margin = raw_width - (width = 5536);
9361 if (raw_width != 5600)
9362 left_margin = top_margin = 0;
9363 filters = 0x61616161;
9364 colors = 3;
9365 } else if (!strcmp(make,"Samsung") && raw_width == 5632) {
9366 order = 0x4949;
9367 height = 3694;
9368 top_margin = 2;
9369 width = 5574 - (left_margin = 32 + tiff_bps);
9370 if (tiff_bps == 12) load_flags = 80;
9371 } else if (!strcmp(make,"Samsung") && raw_width == 5664) {
9372 height -= top_margin = 17;
9373 left_margin = 96;
9374 width = 5544;
9375 filters = 0x49494949;
9376 } else if (!strcmp(make,"Samsung") && raw_width == 6496) {
9377 filters = 0x61616161;
9378 black = 1 << (tiff_bps - 7);
9379 } else if (!strcmp(model,"EX1")) {
9380 order = 0x4949;
9381 height -= 20;
9382 top_margin = 2;
9383 if ((width -= 6) > 3682) {
9384 height -= 10;
9385 width -= 46;
9386 top_margin = 8;
9387 }
9388 } else if (!strcmp(model,"WB2000")) {
9389 order = 0x4949;
9390 height -= 3;
9391 top_margin = 2;
9392 if ((width -= 10) > 3718) {
9393 height -= 28;
9394 width -= 56;
9395 top_margin = 8;
9396 }
9397 } else if (strstr(model,"WB550")) {
9398 strcpy (model, "WB550");
9399 } else if (!strcmp(model,"EX2F")) {
9400 height = 3045;
9401 width = 4070;
9402 top_margin = 3;
9403 order = 0x4949;
9404 filters = 0x49494949;
9405 load_raw = &CLASS unpacked_load_raw;
9406 } else if (!strcmp(model,"STV680 VGA")) {
9407 black = 16;
9408 } else if (!strcmp(model,"N95")) {
9409 height = raw_height - (top_margin = 2);
9410 } else if (!strcmp(model,"640x480")) {
9411 gamma_curve (0.45, 4.5, 1, 255);
9412 } else if (!strcmp(make,"Hasselblad")) {
9413 if (load_raw == &CLASS lossless_jpeg_load_raw)
9414 load_raw = &CLASS hasselblad_load_raw;
9415 if (raw_width == 7262) {
9416 height = 5444;
9417 width = 7248;
9418 top_margin = 4;
9419 left_margin = 7;
9420 filters = 0x61616161;
9421 } else if (raw_width == 7410 || raw_width == 8282) {
9422 height -= 84;
9423 width -= 82;
9424 top_margin = 4;
9425 left_margin = 41;
9426 filters = 0x61616161;
9427 } else if (raw_width == 8384) {
9428 height = 6208;
9429 width = 8280;
9430 top_margin = 96;
9431 left_margin = 46;
9432 } else if (raw_width == 9044) {
9433 height = 6716;
9434 width = 8964;
9435 top_margin = 8;
9436 left_margin = 40;
9437 black += load_flags = 256;
9438 maximum = 0x8101;
9439 } else if (raw_width == 4090) {
9440 strcpy (model, "V96C");
9441 height -= (top_margin = 6);
9442 width -= (left_margin = 3) + 7;
9443 filters = 0x61616161;
9444 }
9445 if (tiff_samples > 1) {
9446 is_raw = tiff_samples+1;
9447 if (!shot_select && !half_size) filters = 0;
9448 }
9449 } else if (!strcmp(make,"Sinar")) {
9450 if (!load_raw) load_raw = &CLASS unpacked_load_raw;
9451 if (is_raw > 1 && !shot_select && !half_size) filters = 0;
9452 maximum = 0x3fff;
9453 } else if (!strcmp(make,"Leaf")) {
9454 maximum = 0x3fff;
9455 fseek (ifp, data_offset, SEEK_SET);
9456 if (ljpeg_start (&jh, 1) && jh.bits == 15)
9457 maximum = 0x1fff;
9458 if (tiff_samples > 1) filters = 0;
9459 if (tiff_samples > 1 || tile_length < raw_height) {
9460 load_raw = &CLASS leaf_hdr_load_raw;
9461 raw_width = tile_width;
9462 }
9463 if ((width | height) == 2048) {
9464 if (tiff_samples == 1) {
9465 filters = 1;
9466 strcpy (cdesc, "RBTG");
9467 strcpy (model, "CatchLight");
9468 top_margin = 8; left_margin = 18; height = 2032; width = 2016;
9469 } else {
9470 strcpy (model, "DCB2");
9471 top_margin = 10; left_margin = 16; height = 2028; width = 2022;
9472 }
9473 } else if (width+height == 3144+2060) {
9474 if (!model[0]) strcpy (model, "Cantare");
9475 if (width > height) {
9476 top_margin = 6; left_margin = 32; height = 2048; width = 3072;
9477 filters = 0x61616161;
9478 } else {
9479 left_margin = 6; top_margin = 32; width = 2048; height = 3072;
9480 filters = 0x16161616;
9481 }
9482 if (!cam_mul[0] || model[0] == 'V') filters = 0;
9483 else is_raw = tiff_samples;
9484 } else if (width == 2116) {
9485 strcpy (model, "Valeo 6");
9486 height -= 2 * (top_margin = 30);
9487 width -= 2 * (left_margin = 55);
9488 filters = 0x49494949;
9489 } else if (width == 3171) {
9490 strcpy (model, "Valeo 6");
9491 height -= 2 * (top_margin = 24);
9492 width -= 2 * (left_margin = 24);
9493 filters = 0x16161616;
9494 }
9495 } else if (!strcmp(make,"Leica") || !strcmp(make,"Panasonic")) {
9496 if ((flen - data_offset) / (raw_width*8/7) == raw_height)
9497 load_raw = &CLASS panasonic_load_raw;
9498 if (!load_raw) {
9499 load_raw = &CLASS unpacked_load_raw;
9500 load_flags = 4;
9501 }
9502 zero_is_bad = 1;
9503 if ((height += 12) > raw_height) height = raw_height;
9504 for (i=0; i < sizeof pana / sizeof *pana; i++)
9505 if (raw_width == pana[i][0] && raw_height == pana[i][1]) {
9506 left_margin = pana[i][2];
9507 top_margin = pana[i][3];
9508 width += pana[i][4];
9509 height += pana[i][5];
9510 }
9511 filters = 0x01010101 * (uchar) "\x94\x61\x49\x16"
9512 [((filters-1) ^ (left_margin & 1) ^ (top_margin << 1)) & 3];
9513 } else if (!strcmp(model,"C770UZ")) {
9514 height = 1718;
9515 width = 2304;
9516 filters = 0x16161616;
9517 load_raw = &CLASS packed_load_raw;
9518 load_flags = 30;
9519 } else if (!strcmp(make,"Olympus")) {
9520 height += height & 1;
9521 if (exif_cfa) filters = exif_cfa;
9522 if (width == 4100) width -= 4;
9523 if (width == 4080) width -= 24;
9524 if (width == 9280) { width -= 6; height -= 6; }
9525 if (load_raw == &CLASS unpacked_load_raw)
9526 load_flags = 4;
9527 tiff_bps = 12;
9528 if (!strcmp(model,"E-300") ||
9529 !strcmp(model,"E-500")) {
9530 width -= 20;
9531 if (load_raw == &CLASS unpacked_load_raw) {
9532 maximum = 0xfc3;
9533 memset (cblack, 0, sizeof cblack);
9534 }
9535 } else if (!strcmp(model,"E-330")) {
9536 width -= 30;
9537 if (load_raw == &CLASS unpacked_load_raw)
9538 maximum = 0xf79;
9539 } else if (!strcmp(model,"SP550UZ")) {
9540 thumb_length = flen - (thumb_offset = 0xa39800);
9541 thumb_height = 480;
9542 thumb_width = 640;
9543 } else if (!strcmp(model,"TG-4")) {
9544 width -= 16;
9545 } else if (!strcmp(model,"TG-5")) {
9546 width -= 6;
9547 }
9548 } else if (!strcmp(model,"N Digital")) {
9549 height = 2047;
9550 width = 3072;
9551 filters = 0x61616161;
9552 data_offset = 0x1a00;
9553 load_raw = &CLASS packed_load_raw;
9554 } else if (!strcmp(model,"DSC-F828")) {
9555 width = 3288;
9556 left_margin = 5;
9557 mask[1][3] = -17;
9558 data_offset = 862144;
9559 load_raw = &CLASS sony_load_raw;
9560 filters = 0x9c9c9c9c;
9561 colors = 4;
9562 strcpy (cdesc, "RGBE");
9563 } else if (!strcmp(model,"DSC-V3")) {
9564 width = 3109;
9565 left_margin = 59;
9566 mask[0][1] = 9;
9567 data_offset = 787392;
9568 load_raw = &CLASS sony_load_raw;
9569 } else if (!strcmp(make,"Sony") && raw_width == 3984) {
9570 width = 3925;
9571 order = 0x4d4d;
9572 } else if (!strcmp(make,"Sony") && raw_width == 4288) {
9573 width -= 32;
9574 } else if (!strcmp(make,"Sony") && raw_width == 4600) {
9575 if (!strcmp(model,"DSLR-A350"))
9576 height -= 4;
9577 black = 0;
9578 } else if (!strcmp(make,"Sony") && raw_width == 4928) {
9579 if (height < 3280) width -= 8;
9580 } else if (!strcmp(make,"Sony") && raw_width == 5504) {
9581 width -= height > 3664 ? 8 : 32;
9582 if (!strncmp(model,"DSC",3))
9583 black = 200 << (tiff_bps - 12);
9584 } else if (!strcmp(make,"Sony") && raw_width == 6048) {
9585 width -= 24;
9586 if (strstr(model,"RX1") || strstr(model,"A99"))
9587 width -= 6;
9588 } else if (!strcmp(make,"Sony") && raw_width == 7392) {
9589 width -= 30;
9590 } else if (!strcmp(make,"Sony") && raw_width == 8000) {
9591 width -= 32;
9592 } else if (!strcmp(model,"DSLR-A100")) {
9593 if (width == 3880) {
9594 height--;
9595 width = ++raw_width;
9596 } else {
9597 height -= 4;
9598 width -= 4;
9599 order = 0x4d4d;
9600 load_flags = 2;
9601 }
9602 filters = 0x61616161;
9603 } else if (!strcmp(model,"PIXL")) {
9604 height -= top_margin = 4;
9605 width -= left_margin = 32;
9606 gamma_curve (0, 7, 1, 255);
9607 } else if (!strcmp(model,"C603") || !strcmp(model,"C330")
9608 || !strcmp(model,"12MP")) {
9609 order = 0x4949;
9610 if (filters && data_offset) {
9611 fseek (ifp, data_offset < 4096 ? 168 : 5252, SEEK_SET);
9612 read_shorts (curve, 256);
9613 } else gamma_curve (0, 3.875, 1, 255);
9614 load_raw = filters ? &CLASS eight_bit_load_raw :
9615 strcmp(model,"C330") ? &CLASS kodak_c603_load_raw :
9616 &CLASS kodak_c330_load_raw;
9617 load_flags = tiff_bps > 16;
9618 tiff_bps = 8;
9619 } else if (!strncasecmp(model,"EasyShare",9)) {
9620 data_offset = data_offset < 0x15000 ? 0x15000 : 0x17000;
9621 load_raw = &CLASS packed_load_raw;
9622 } else if (!strcasecmp(make,"Kodak")) {
9623 if (filters == UINT_MAX) filters = 0x61616161;
9624 if (!strncmp(model,"NC2000",6) ||
9625 !strncmp(model,"EOSDCS",6) ||
9626 !strncmp(model,"DCS4",4)) {
9627 width -= 4;
9628 left_margin = 2;
9629 if (model[6] == ' ') model[6] = 0;
9630 if (!strcmp(model,"DCS460A")) goto bw;
9631 } else if (!strcmp(model,"DCS660M")) {
9632 black = 214;
9633 goto bw;
9634 } else if (!strcmp(model,"DCS760M")) {
9635 bw: colors = 1;
9636 filters = 0;
9637 }
9638 if (!strcmp(model+4,"20X"))
9639 strcpy (cdesc, "MYCY");
9640 if (strstr(model,"DC25")) {
9641 strcpy (model, "DC25");
9642 data_offset = 15424;
9643 }
9644 if (!strncmp(model,"DC2",3)) {
9645 raw_height = 2 + (height = 242);
9646 if (flen < 100000) {
9647 raw_width = 256; width = 249;
9648 pixel_aspect = (4.0*height) / (3.0*width);
9649 } else {
9650 raw_width = 512; width = 501;
9651 pixel_aspect = (493.0*height) / (373.0*width);
9652 }
9653 top_margin = left_margin = 1;
9654 colors = 4;
9655 filters = 0x8d8d8d8d;
9656 simple_coeff(1);
9657 pre_mul[1] = 1.179;
9658 pre_mul[2] = 1.209;
9659 pre_mul[3] = 1.036;
9660 load_raw = &CLASS eight_bit_load_raw;
9661 } else if (!strcmp(model,"40")) {
9662 strcpy (model, "DC40");
9663 height = 512;
9664 width = 768;
9665 data_offset = 1152;
9666 load_raw = &CLASS kodak_radc_load_raw;
9667 tiff_bps = 12;
9668 } else if (strstr(model,"DC50")) {
9669 strcpy (model, "DC50");
9670 height = 512;
9671 width = 768;
9672 data_offset = 19712;
9673 load_raw = &CLASS kodak_radc_load_raw;
9674 } else if (strstr(model,"DC120")) {
9675 strcpy (model, "DC120");
9676 height = 976;
9677 width = 848;
9678 pixel_aspect = height/0.75/width;
9679 load_raw = tiff_compress == 7 ?
9680 &CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
9681 } else if (!strcmp(model,"DCS200")) {
9682 thumb_height = 128;
9683 thumb_width = 192;
9684 thumb_offset = 6144;
9685 thumb_misc = 360;
9686 write_thumb = &CLASS layer_thumb;
9687 black = 17;
9688 }
9689 } else if (!strcmp(model,"Fotoman Pixtura")) {
9690 height = 512;
9691 width = 768;
9692 data_offset = 3632;
9693 load_raw = &CLASS kodak_radc_load_raw;
9694 filters = 0x61616161;
9695 simple_coeff(2);
9696 } else if (!strncmp(model,"QuickTake",9)) {
9697 if (head[5]) strcpy (model+10, "200");
9698 fseek (ifp, 544, SEEK_SET);
9699 height = get2();
9700 width = get2();
9701 data_offset = (get4(),get2()) == 30 ? 738:736;
9702 if (height > width) {
9703 SWAP(height,width);
9704 fseek (ifp, data_offset-6, SEEK_SET);
9705 flip = ~get2() & 3 ? 5:6;
9706 }
9707 filters = 0x61616161;
9708 } else if (!strcmp(make,"Rollei") && !load_raw) {
9709 switch (raw_width) {
9710 case 1316:
9711 height = 1030;
9712 width = 1300;
9713 top_margin = 1;
9714 left_margin = 6;
9715 break;
9716 case 2568:
9717 height = 1960;
9718 width = 2560;
9719 top_margin = 2;
9720 left_margin = 8;
9721 }
9722 filters = 0x16161616;
9723 load_raw = &CLASS rollei_load_raw;
9724 }
9725 if (!model[0])
9726 sprintf (model, "%dx%d", width, height);
9727 if (filters == UINT_MAX) filters = 0x94949494;
9728 if (thumb_offset && !thumb_height) {
9729 fseek (ifp, thumb_offset, SEEK_SET);
9730 if (ljpeg_start (&jh, 1)) {
9731 thumb_width = jh.wide;
9732 thumb_height = jh.high;
9733 }
9734 }
9735 dng_skip:
9736 if ((use_camera_matrix & (use_camera_wb || dng_version))
9737 && cmatrix[0][0] > 0.125) {
9738 memcpy (rgb_cam, cmatrix, sizeof cmatrix);
9739 raw_color = 0;
9740 }
9741 if (raw_color) adobe_coeff (make, model);
9742 if (load_raw == &CLASS kodak_radc_load_raw)
9743 if (raw_color) adobe_coeff ("Apple","Quicktake");
9744 if (fuji_width) {
9745 fuji_width = width >> !fuji_layout;
9746 filters = fuji_width & 1 ? 0x94949494 : 0x49494949;
9747 width = (height >> fuji_layout) + fuji_width;
9748 height = width - 1;
9749 pixel_aspect = 1;
9750 } else {
9751 if (raw_height < height) raw_height = height;
9752 if (raw_width < width ) raw_width = width;
9753 }
9754 if (!tiff_bps) tiff_bps = 12;
9755 if (!maximum) maximum = (1 << tiff_bps) - 1;
9756 if (!load_raw || height < 22 || width < 22 ||
9757 tiff_bps > 16 || tiff_samples > 6 || colors > 4)
9758 is_raw = 0;
9759 #ifdef NO_JASPER
9760 if (load_raw == &CLASS redcine_load_raw) {
9761 fprintf (stderr,_("%s: You must link dcraw with %s!!\n"),
9762 ifname, "libjasper");
9763 is_raw = 0;
9764 }
9765 #endif
9766 #ifdef NO_JPEG
9767 if (load_raw == &CLASS kodak_jpeg_load_raw ||
9768 load_raw == &CLASS lossy_dng_load_raw) {
9769 fprintf (stderr,_("%s: You must link dcraw with %s!!\n"),
9770 ifname, "libjpeg");
9771 is_raw = 0;
9772 }
9773 #endif
9774 if (!cdesc[0])
9775 strcpy (cdesc, colors == 3 ? "RGBG":"GMCY");
9776 if (!raw_height) raw_height = height;
9777 if (!raw_width ) raw_width = width;
9778 if (filters > 999 && colors == 3)
9779 filters |= ((filters >> 2 & 0x22222222) |
9780 (filters << 2 & 0x88888888)) & filters << 1;
9781 notraw:
9782 if (flip == UINT_MAX) flip = tiff_flip;
9783 if (flip == UINT_MAX) flip = 0;
9784 }
9785
9786 #ifndef NO_LCMS
apply_profile(const char * input,const char * output)9787 void CLASS apply_profile (const char *input, const char *output)
9788 {
9789 char *prof;
9790 cmsHPROFILE hInProfile=0, hOutProfile=0;
9791 cmsHTRANSFORM hTransform;
9792 FILE *fp;
9793 unsigned size;
9794
9795 if (strcmp (input, "embed"))
9796 hInProfile = cmsOpenProfileFromFile (input, "r");
9797 else if (profile_length) {
9798 prof = (char *) malloc (profile_length);
9799 merror (prof, "apply_profile()");
9800 fseek (ifp, profile_offset, SEEK_SET);
9801 fread (prof, 1, profile_length, ifp);
9802 hInProfile = cmsOpenProfileFromMem (prof, profile_length);
9803 free (prof);
9804 } else
9805 fprintf (stderr,_("%s has no embedded profile.\n"), ifname);
9806 if (!hInProfile) return;
9807 if (!output)
9808 hOutProfile = cmsCreate_sRGBProfile();
9809 else if ((fp = fopen (output, "rb"))) {
9810 fread (&size, 4, 1, fp);
9811 fseek (fp, 0, SEEK_SET);
9812 oprof = (unsigned *) malloc (size = ntohl(size));
9813 merror (oprof, "apply_profile()");
9814 fread (oprof, 1, size, fp);
9815 fclose (fp);
9816 if (!(hOutProfile = cmsOpenProfileFromMem (oprof, size))) {
9817 free (oprof);
9818 oprof = 0;
9819 }
9820 } else
9821 fprintf (stderr,_("Cannot open file %s!\n"), output);
9822 if (!hOutProfile) goto quit;
9823 if (verbose)
9824 fprintf (stderr,_("Applying color profile...\n"));
9825 hTransform = cmsCreateTransform (hInProfile, TYPE_RGBA_16,
9826 hOutProfile, TYPE_RGBA_16, INTENT_PERCEPTUAL, 0);
9827 cmsDoTransform (hTransform, image, image, width*height);
9828 raw_color = 1; /* Don't use rgb_cam with a profile */
9829 cmsDeleteTransform (hTransform);
9830 cmsCloseProfile (hOutProfile);
9831 quit:
9832 cmsCloseProfile (hInProfile);
9833 }
9834 #endif
9835
convert_to_rgb()9836 void CLASS convert_to_rgb()
9837 {
9838 int row, col, c, i, j, k;
9839 ushort *img;
9840 float out[3], out_cam[3][4];
9841 double num, inverse[3][3];
9842 static const double xyzd50_srgb[3][3] =
9843 { { 0.436083, 0.385083, 0.143055 },
9844 { 0.222507, 0.716888, 0.060608 },
9845 { 0.013930, 0.097097, 0.714022 } };
9846 static const double rgb_rgb[3][3] =
9847 { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
9848 static const double adobe_rgb[3][3] =
9849 { { 0.715146, 0.284856, 0.000000 },
9850 { 0.000000, 1.000000, 0.000000 },
9851 { 0.000000, 0.041166, 0.958839 } };
9852 static const double wide_rgb[3][3] =
9853 { { 0.593087, 0.404710, 0.002206 },
9854 { 0.095413, 0.843149, 0.061439 },
9855 { 0.011621, 0.069091, 0.919288 } };
9856 static const double prophoto_rgb[3][3] =
9857 { { 0.529317, 0.330092, 0.140588 },
9858 { 0.098368, 0.873465, 0.028169 },
9859 { 0.016879, 0.117663, 0.865457 } };
9860 static const double aces_rgb[3][3] =
9861 { { 0.432996, 0.375380, 0.189317 },
9862 { 0.089427, 0.816523, 0.102989 },
9863 { 0.019165, 0.118150, 0.941914 } };
9864 static const double (*out_rgb[])[3] =
9865 { rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb, aces_rgb };
9866 static const char *name[] =
9867 { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ", "ACES" };
9868 static const unsigned phead[] =
9869 { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
9870 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
9871 unsigned pbody[] =
9872 { 10, 0x63707274, 0, 36, /* cprt */
9873 0x64657363, 0, 40, /* desc */
9874 0x77747074, 0, 20, /* wtpt */
9875 0x626b7074, 0, 20, /* bkpt */
9876 0x72545243, 0, 14, /* rTRC */
9877 0x67545243, 0, 14, /* gTRC */
9878 0x62545243, 0, 14, /* bTRC */
9879 0x7258595a, 0, 20, /* rXYZ */
9880 0x6758595a, 0, 20, /* gXYZ */
9881 0x6258595a, 0, 20 }; /* bXYZ */
9882 static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc };
9883 unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 };
9884
9885 gamma_curve (gamm[0], gamm[1], 0, 0);
9886 memcpy (out_cam, rgb_cam, sizeof out_cam);
9887 raw_color |= colors == 1 || document_mode ||
9888 output_color < 1 || output_color > 6;
9889 if (!raw_color) {
9890 oprof = (unsigned *) calloc (phead[0], 1);
9891 merror (oprof, "convert_to_rgb()");
9892 memcpy (oprof, phead, sizeof phead);
9893 if (output_color == 5) oprof[4] = oprof[5];
9894 oprof[0] = 132 + 12*pbody[0];
9895 for (i=0; i < pbody[0]; i++) {
9896 oprof[oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
9897 pbody[i*3+2] = oprof[0];
9898 oprof[0] += (pbody[i*3+3] + 3) & -4;
9899 }
9900 memcpy (oprof+32, pbody, sizeof pbody);
9901 oprof[pbody[5]/4+2] = strlen(name[output_color-1]) + 1;
9902 memcpy ((char *)oprof+pbody[8]+8, pwhite, sizeof pwhite);
9903 pcurve[3] = (short)(256/gamm[5]+0.5) << 16;
9904 for (i=4; i < 7; i++)
9905 memcpy ((char *)oprof+pbody[i*3+2], pcurve, sizeof pcurve);
9906 pseudoinverse ((double (*)[3]) out_rgb[output_color-1], inverse, 3);
9907 for (i=0; i < 3; i++)
9908 for (j=0; j < 3; j++) {
9909 for (num = k=0; k < 3; k++)
9910 num += xyzd50_srgb[i][k] * inverse[j][k];
9911 oprof[pbody[j*3+23]/4+i+2] = num * 0x10000 + 0.5;
9912 }
9913 for (i=0; i < phead[0]/4; i++)
9914 oprof[i] = htonl(oprof[i]);
9915 strcpy ((char *)oprof+pbody[2]+8, "auto-generated by dcraw");
9916 strcpy ((char *)oprof+pbody[5]+12, name[output_color-1]);
9917 for (i=0; i < 3; i++)
9918 for (j=0; j < colors; j++)
9919 for (out_cam[i][j] = k=0; k < 3; k++)
9920 out_cam[i][j] += out_rgb[output_color-1][i][k] * rgb_cam[k][j];
9921 }
9922 if (verbose)
9923 fprintf (stderr, raw_color ? _("Building histograms...\n") :
9924 _("Converting to %s colorspace...\n"), name[output_color-1]);
9925
9926 memset (histogram, 0, sizeof histogram);
9927 for (img=image[0], row=0; row < height; row++)
9928 for (col=0; col < width; col++, img+=4) {
9929 if (!raw_color) {
9930 out[0] = out[1] = out[2] = 0;
9931 FORCC {
9932 out[0] += out_cam[0][c] * img[c];
9933 out[1] += out_cam[1][c] * img[c];
9934 out[2] += out_cam[2][c] * img[c];
9935 }
9936 FORC3 img[c] = CLIP((int) out[c]);
9937 }
9938 else if (document_mode)
9939 img[0] = img[fcol(row,col)];
9940 FORCC histogram[c][img[c] >> 3]++;
9941 }
9942 if (colors == 4 && output_color) colors = 3;
9943 if (document_mode && filters) colors = 1;
9944 }
9945
fuji_rotate()9946 void CLASS fuji_rotate()
9947 {
9948 int i, row, col;
9949 double step;
9950 float r, c, fr, fc;
9951 unsigned ur, uc;
9952 ushort wide, high, (*img)[4], (*pix)[4];
9953
9954 if (!fuji_width) return;
9955 if (verbose)
9956 fprintf (stderr,_("Rotating image 45 degrees...\n"));
9957 fuji_width = (fuji_width - 1 + shrink) >> shrink;
9958 step = sqrt(0.5);
9959 wide = fuji_width / step;
9960 high = (height - fuji_width) / step;
9961 img = (ushort (*)[4]) calloc (high, wide*sizeof *img);
9962 merror (img, "fuji_rotate()");
9963
9964 #pragma omp parallel for default(shared) private(row,col,ur,uc,r,c,fr,fc,pix,i)
9965 for (row=0; row < high; row++)
9966 for (col=0; col < wide; col++) {
9967 ur = r = fuji_width + (row-col)*step;
9968 uc = c = (row+col)*step;
9969 if (ur > height-2 || uc > width-2) continue;
9970 fr = r - ur;
9971 fc = c - uc;
9972 pix = image + ur*width + uc;
9973 for (i=0; i < colors; i++)
9974 img[row*wide+col][i] =
9975 (pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
9976 (pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr;
9977 }
9978 free (image);
9979 width = wide;
9980 height = high;
9981 image = img;
9982 fuji_width = 0;
9983 }
9984
stretch()9985 void CLASS stretch()
9986 {
9987 ushort newdim, (*img)[4], *pix0, *pix1;
9988 int row, col, c;
9989 double rc, frac;
9990
9991 if (pixel_aspect == 1) return;
9992 if (verbose) fprintf (stderr,_("Stretching the image...\n"));
9993 if (pixel_aspect < 1) {
9994 newdim = height / pixel_aspect + 0.5;
9995 img = (ushort (*)[4]) calloc (width, newdim*sizeof *img);
9996 merror (img, "stretch()");
9997 for (rc=row=0; row < newdim; row++, rc+=pixel_aspect) {
9998 frac = rc - (c = rc);
9999 pix0 = pix1 = image[c*width];
10000 if (c+1 < height) pix1 += width*4;
10001 for (col=0; col < width; col++, pix0+=4, pix1+=4)
10002 FORCC img[row*width+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
10003 }
10004 height = newdim;
10005 } else {
10006 newdim = width * pixel_aspect + 0.5;
10007 img = (ushort (*)[4]) calloc (height, newdim*sizeof *img);
10008 merror (img, "stretch()");
10009 for (rc=col=0; col < newdim; col++, rc+=1/pixel_aspect) {
10010 frac = rc - (c = rc);
10011 pix0 = pix1 = image[c];
10012 if (c+1 < width) pix1 += 4;
10013 for (row=0; row < height; row++, pix0+=width*4, pix1+=width*4)
10014 FORCC img[row*newdim+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
10015 }
10016 width = newdim;
10017 }
10018 free (image);
10019 image = img;
10020 }
10021
flip_index(int row,int col)10022 int CLASS flip_index (int row, int col)
10023 {
10024 if (flip & 4) SWAP(row,col);
10025 if (flip & 2) row = iheight - 1 - row;
10026 if (flip & 1) col = iwidth - 1 - col;
10027 return row * iwidth + col;
10028 }
10029
10030 struct tiff_tag {
10031 ushort tag, type;
10032 int count;
10033 union { char c[4]; short s[2]; int i; } val;
10034 };
10035
10036 struct tiff_hdr {
10037 ushort order, magic;
10038 int ifd;
10039 ushort pad, ntag;
10040 struct tiff_tag tag[23];
10041 int nextifd;
10042 ushort pad2, nexif;
10043 struct tiff_tag exif[4];
10044 ushort pad3, ngps;
10045 struct tiff_tag gpst[10];
10046 short bps[4];
10047 int rat[10];
10048 unsigned gps[26];
10049 char desc[512], make[64], model[64], soft[32], date[20], artist[64];
10050 };
10051
10052 #if defined(__MINGW32__) || defined(__sun) || defined(__APPLE__)
strnlen(const char * str,size_t maxlen)10053 size_t strnlen(const char *str, size_t maxlen) {
10054 const char *end = memchr(str, 0, maxlen);
10055 return end ? (size_t)(end - str) : maxlen;
10056 }
10057 #endif
10058
tiff_set(struct tiff_hdr * th,ushort * ntag,ushort tag,ushort type,int count,int val)10059 void CLASS tiff_set (struct tiff_hdr *th, ushort *ntag,
10060 ushort tag, ushort type, int count, int val)
10061 {
10062 struct tiff_tag *tt;
10063 int c;
10064
10065 tt = (struct tiff_tag *)(ntag+1) + (*ntag)++;
10066 tt->val.i = val;
10067 if (type == 1 && count <= 4)
10068 FORC(4) tt->val.c[c] = val >> (c << 3);
10069 else if (type == 2) {
10070 count = strnlen((char *)th + val, count-1) + 1;
10071 if (count <= 4)
10072 FORC(4) tt->val.c[c] = ((char *)th)[val+c];
10073 } else if (type == 3 && count <= 2)
10074 FORC(2) tt->val.s[c] = val >> (c << 4);
10075 tt->count = count;
10076 tt->type = type;
10077 tt->tag = tag;
10078 }
10079
10080 #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
10081
tiff_head(struct tiff_hdr * th,int full)10082 void CLASS tiff_head (struct tiff_hdr *th, int full)
10083 {
10084 int c, psize=0;
10085 struct tm *t;
10086
10087 memset (th, 0, sizeof *th);
10088 th->order = htonl(0x4d4d4949) >> 16;
10089 th->magic = 42;
10090 th->ifd = 10;
10091 th->rat[0] = th->rat[2] = 300;
10092 th->rat[1] = th->rat[3] = 1;
10093 FORC(6) th->rat[4+c] = 1000000;
10094 th->rat[4] *= shutter;
10095 th->rat[6] *= aperture;
10096 th->rat[8] *= focal_len;
10097 strncpy (th->desc, desc, 512);
10098 strncpy (th->make, make, 64);
10099 strncpy (th->model, model, 64);
10100 strcpy (th->soft, "dcraw v"DCRAW_VERSION);
10101 t = localtime (×tamp);
10102 sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
10103 t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
10104 strncpy (th->artist, artist, 64);
10105 if (full) {
10106 tiff_set (th, &th->ntag, 254, 4, 1, 0);
10107 tiff_set (th, &th->ntag, 256, 4, 1, width);
10108 tiff_set (th, &th->ntag, 257, 4, 1, height);
10109 tiff_set (th, &th->ntag, 258, 3, colors, output_bps);
10110 if (colors > 2)
10111 th->tag[th->ntag-1].val.i = TOFF(th->bps);
10112 FORC4 th->bps[c] = output_bps;
10113 tiff_set (th, &th->ntag, 259, 3, 1, 1);
10114 tiff_set (th, &th->ntag, 262, 3, 1, 1 + (colors > 1));
10115 }
10116 tiff_set (th, &th->ntag, 270, 2, 512, TOFF(th->desc));
10117 tiff_set (th, &th->ntag, 271, 2, 64, TOFF(th->make));
10118 tiff_set (th, &th->ntag, 272, 2, 64, TOFF(th->model));
10119 if (full) {
10120 if (oprof) psize = ntohl(oprof[0]);
10121 tiff_set (th, &th->ntag, 273, 4, 1, sizeof *th + psize);
10122 tiff_set (th, &th->ntag, 277, 3, 1, colors);
10123 tiff_set (th, &th->ntag, 278, 4, 1, height);
10124 tiff_set (th, &th->ntag, 279, 4, 1, height*width*colors*output_bps/8);
10125 } else
10126 tiff_set (th, &th->ntag, 274, 3, 1, "12435867"[flip]-'0');
10127 tiff_set (th, &th->ntag, 282, 5, 1, TOFF(th->rat[0]));
10128 tiff_set (th, &th->ntag, 283, 5, 1, TOFF(th->rat[2]));
10129 tiff_set (th, &th->ntag, 284, 3, 1, 1);
10130 tiff_set (th, &th->ntag, 296, 3, 1, 2);
10131 tiff_set (th, &th->ntag, 305, 2, 32, TOFF(th->soft));
10132 tiff_set (th, &th->ntag, 306, 2, 20, TOFF(th->date));
10133 tiff_set (th, &th->ntag, 315, 2, 64, TOFF(th->artist));
10134 tiff_set (th, &th->ntag, 34665, 4, 1, TOFF(th->nexif));
10135 if (psize) tiff_set (th, &th->ntag, 34675, 7, psize, sizeof *th);
10136 tiff_set (th, &th->nexif, 33434, 5, 1, TOFF(th->rat[4]));
10137 tiff_set (th, &th->nexif, 33437, 5, 1, TOFF(th->rat[6]));
10138 tiff_set (th, &th->nexif, 34855, 3, 1, iso_speed);
10139 tiff_set (th, &th->nexif, 37386, 5, 1, TOFF(th->rat[8]));
10140 if (gpsdata[1]) {
10141 tiff_set (th, &th->ntag, 34853, 4, 1, TOFF(th->ngps));
10142 tiff_set (th, &th->ngps, 0, 1, 4, 0x202);
10143 tiff_set (th, &th->ngps, 1, 2, 2, gpsdata[29]);
10144 tiff_set (th, &th->ngps, 2, 5, 3, TOFF(th->gps[0]));
10145 tiff_set (th, &th->ngps, 3, 2, 2, gpsdata[30]);
10146 tiff_set (th, &th->ngps, 4, 5, 3, TOFF(th->gps[6]));
10147 tiff_set (th, &th->ngps, 5, 1, 1, gpsdata[31]);
10148 tiff_set (th, &th->ngps, 6, 5, 1, TOFF(th->gps[18]));
10149 tiff_set (th, &th->ngps, 7, 5, 3, TOFF(th->gps[12]));
10150 tiff_set (th, &th->ngps, 18, 2, 12, TOFF(th->gps[20]));
10151 tiff_set (th, &th->ngps, 29, 2, 12, TOFF(th->gps[23]));
10152 memcpy (th->gps, gpsdata, sizeof th->gps);
10153 }
10154 }
10155
jpeg_thumb()10156 void CLASS jpeg_thumb()
10157 {
10158 char *thumb;
10159 ushort exif[5];
10160 struct tiff_hdr th;
10161
10162 thumb = (char *) malloc (thumb_length);
10163 merror (thumb, "jpeg_thumb()");
10164 fread (thumb, 1, thumb_length, ifp);
10165 fputc (0xff, ofp);
10166 fputc (0xd8, ofp);
10167 if (strcmp (thumb+6, "Exif")) {
10168 memcpy (exif, "\xff\xe1 Exif\0\0", 10);
10169 exif[1] = htons (8 + sizeof th);
10170 fwrite (exif, 1, sizeof exif, ofp);
10171 tiff_head (&th, 0);
10172 fwrite (&th, 1, sizeof th, ofp);
10173 }
10174 fwrite (thumb+2, 1, thumb_length-2, ofp);
10175 free (thumb);
10176 }
10177
write_ppm_tiff()10178 void CLASS write_ppm_tiff()
10179 {
10180 struct tiff_hdr th;
10181 uchar *ppm;
10182 ushort *ppm2;
10183 int c, row, col, soff, rstep, cstep;
10184 int perc, val, total, white=0x2000;
10185
10186 perc = width * height * 0.01; /* 99th percentile white level */
10187 if (fuji_width) perc /= 2;
10188 if (!((highlight & ~2) || no_auto_bright))
10189 for (white=c=0; c < colors; c++) {
10190 for (val=0x2000, total=0; --val > 32; )
10191 if ((total += histogram[c][val]) > perc) break;
10192 if (white < val) white = val;
10193 }
10194 gamma_curve (gamm[0], gamm[1], 2, (white << 3)/bright);
10195 iheight = height;
10196 iwidth = width;
10197 if (flip & 4) SWAP(height,width);
10198 ppm = (uchar *) calloc (width, colors*output_bps/8);
10199 ppm2 = (ushort *) ppm;
10200 merror (ppm, "write_ppm_tiff()");
10201 if (output_tiff) {
10202 tiff_head (&th, 1);
10203 fwrite (&th, sizeof th, 1, ofp);
10204 if (oprof)
10205 fwrite (oprof, ntohl(oprof[0]), 1, ofp);
10206 } else if (colors > 3)
10207 fprintf (ofp,
10208 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
10209 width, height, colors, (1 << output_bps)-1, cdesc);
10210 else
10211 fprintf (ofp, "P%d\n%d %d\n%d\n",
10212 colors/2+5, width, height, (1 << output_bps)-1);
10213 soff = flip_index (0, 0);
10214 cstep = flip_index (0, 1) - soff;
10215 rstep = flip_index (1, 0) - flip_index (0, width);
10216 for (row=0; row < height; row++, soff += rstep) {
10217 for (col=0; col < width; col++, soff += cstep)
10218 if (output_bps == 8)
10219 FORCC ppm [col*colors+c] = curve[image[soff][c]] >> 8;
10220 else FORCC ppm2[col*colors+c] = curve[image[soff][c]];
10221 if (output_bps == 16 && !output_tiff && htons(0x55aa) != 0x55aa)
10222 swab (ppm2, ppm2, width*colors*2);
10223 fwrite (ppm, colors*output_bps/8, width, ofp);
10224 }
10225 free (ppm);
10226 }
10227
main(int argc,const char ** argv)10228 int CLASS main (int argc, const char **argv)
10229 {
10230 int arg, status=0, quality, i, c;
10231 int timestamp_only=0, thumbnail_only=0, identify_only=0;
10232 int user_qual=-1, user_black=-1, user_sat=-1, user_flip=-1;
10233 int use_fuji_rotate=1, write_to_stdout=0, read_from_stdin=0;
10234 const char *sp, *bpfile=0, *dark_frame=0, *write_ext;
10235 char opm, opt, *ofname, *cp;
10236 struct utimbuf ut;
10237 #ifndef NO_LCMS
10238 const char *cam_profile=0, *out_profile=0;
10239 #endif
10240 #ifdef LIGHTZONE
10241 const char *ofbase = 0;
10242 #endif
10243
10244 #ifndef LOCALTIME
10245 putenv ((char *) "TZ=UTC");
10246 #endif
10247 #ifdef LOCALEDIR
10248 setlocale (LC_CTYPE, "");
10249 setlocale (LC_MESSAGES, "");
10250 bindtextdomain ("dcraw", LOCALEDIR);
10251 textdomain ("dcraw");
10252 #endif
10253
10254 if (argc == 1) {
10255 printf(_("\nRaw photo decoder \"dcraw\" v%s"), DCRAW_VERSION);
10256 printf(_("\nby Dave Coffin, dcoffin a cybercom o net\n"));
10257 printf(_("\nUsage: %s [OPTION]... [FILE]...\n\n"), argv[0]);
10258 puts(_("-v Print verbose messages"));
10259 puts(_("-c Write image data to standard output"));
10260 puts(_("-e Extract embedded thumbnail image"));
10261 puts(_("-i Identify files without decoding them"));
10262 puts(_("-i -v Identify files and show metadata"));
10263 puts(_("-z Change file dates to camera timestamp"));
10264 puts(_("-w Use camera white balance, if possible"));
10265 puts(_("-a Average the whole image for white balance"));
10266 puts(_("-A <x y w h> Average a grey box for white balance"));
10267 puts(_("-r <r g b g> Set custom white balance"));
10268 puts(_("+M/-M Use/don't use an embedded color matrix"));
10269 puts(_("-C <r b> Correct chromatic aberration"));
10270 puts(_("-P <file> Fix the dead pixels listed in this file"));
10271 puts(_("-K <file> Subtract dark frame (16-bit raw PGM)"));
10272 puts(_("-k <num> Set the darkness level"));
10273 puts(_("-S <num> Set the saturation level"));
10274 puts(_("-n <num> Set threshold for wavelet denoising"));
10275 puts(_("-H [0-9] Highlight mode (0=clip, 1=unclip, 2=blend, 3+=rebuild)"));
10276 puts(_("-t [0-7] Flip image (0=none, 3=180, 5=90CCW, 6=90CW)"));
10277 puts(_("-o [0-6] Output colorspace (raw,sRGB,Adobe,Wide,ProPhoto,XYZ,ACES)"));
10278 #ifndef NO_LCMS
10279 puts(_("-o <file> Apply output ICC profile from file"));
10280 puts(_("-p <file> Apply camera ICC profile from file or \"embed\""));
10281 #endif
10282 puts(_("-d Document mode (no color, no interpolation)"));
10283 puts(_("-D Document mode without scaling (totally raw)"));
10284 puts(_("-j Don't stretch or rotate raw pixels"));
10285 puts(_("-W Don't automatically brighten the image"));
10286 puts(_("-b <num> Adjust brightness (default = 1.0)"));
10287 puts(_("-g <p ts> Set custom gamma curve (default = 2.222 4.5)"));
10288 puts(_("-q [0-3] Set the interpolation quality"));
10289 puts(_("-h Half-size color image (twice as fast as \"-q 0\")"));
10290 puts(_("-f Interpolate RGGB as four colors"));
10291 puts(_("-m <num> Apply a 3x3 median filter to R-G and B-G"));
10292 puts(_("-s [0..N-1] Select one raw image or \"all\" from each file"));
10293 puts(_("-6 Write 16-bit instead of 8-bit"));
10294 puts(_("-4 Linear 16-bit, same as \"-6 -W -g 1 1\""));
10295 puts(_("-T Write TIFF instead of PPM"));
10296 puts("");
10297 return 1;
10298 }
10299 argv[argc] = "";
10300 for (arg=1; (((opm = argv[arg][0]) - 2) | 2) == '+'; ) {
10301 opt = argv[arg++][1];
10302 if ((cp = (char *) strchr (sp="nbrkStqmHACg", opt)))
10303 for (i=0; i < "114111111422"[cp-sp]-'0'; i++)
10304 if (!isdigit(argv[arg+i][0])) {
10305 fprintf (stderr,_("Non-numeric argument to \"-%c\"\n"), opt);
10306 return 1;
10307 }
10308 switch (opt) {
10309 case 'n': threshold = atof(argv[arg++]); break;
10310 case 'b': bright = atof(argv[arg++]); break;
10311 case 'r':
10312 FORC4 user_mul[c] = atof(argv[arg++]); break;
10313 case 'C': aber[0] = 1 / atof(argv[arg++]);
10314 aber[2] = 1 / atof(argv[arg++]); break;
10315 case 'g': gamm[0] = atof(argv[arg++]);
10316 gamm[1] = atof(argv[arg++]);
10317 if (gamm[0]) gamm[0] = 1/gamm[0]; break;
10318 case 'k': user_black = atoi(argv[arg++]); break;
10319 case 'S': user_sat = atoi(argv[arg++]); break;
10320 case 't': user_flip = atoi(argv[arg++]); break;
10321 case 'q': user_qual = atoi(argv[arg++]); break;
10322 case 'm': med_passes = atoi(argv[arg++]); break;
10323 case 'H': highlight = atoi(argv[arg++]); break;
10324 case 's':
10325 shot_select = abs(atoi(argv[arg]));
10326 multi_out = !strcmp(argv[arg++],"all");
10327 break;
10328 case 'o':
10329 if (isdigit(argv[arg][0]) && !argv[arg][1])
10330 output_color = atoi(argv[arg++]);
10331 #ifndef NO_LCMS
10332 else out_profile = argv[arg++];
10333 break;
10334 case 'p': cam_profile = argv[arg++];
10335 #endif
10336 break;
10337 #ifdef LIGHTZONE
10338 case 'F': ofbase = argv[arg++]; break;
10339 #endif
10340 case 'P': bpfile = argv[arg++]; break;
10341 case 'K': dark_frame = argv[arg++]; break;
10342 case 'z': timestamp_only = 1; break;
10343 case 'e': thumbnail_only = 1; break;
10344 case 'i': identify_only = 1; break;
10345 case 'c': write_to_stdout = 1; break;
10346 case 'v': verbose = 1; break;
10347 case 'h': half_size = 1; break;
10348 case 'f': four_color_rgb = 1; break;
10349 case 'A': FORC4 greybox[c] = atoi(argv[arg++]);
10350 case 'a': use_auto_wb = 1; break;
10351 case 'w': use_camera_wb = 1; break;
10352 case 'M': use_camera_matrix = 3 * (opm == '+'); break;
10353 case 'I': read_from_stdin = 1; break;
10354 case 'E': document_mode++;
10355 case 'D': document_mode++;
10356 case 'd': document_mode++;
10357 case 'j': use_fuji_rotate = 0; break;
10358 case 'W': no_auto_bright = 1; break;
10359 case 'T': output_tiff = 1; break;
10360 case '4': gamm[0] = gamm[1] =
10361 no_auto_bright = 1;
10362 case '6': output_bps = 16; break;
10363 default:
10364 fprintf (stderr,_("Unknown option \"-%c\".\n"), opt);
10365 return 1;
10366 }
10367 }
10368 #ifdef LIGHTZONE
10369 if (!half_size && !thumbnail_only) {
10370 float rimm[3][3] = {
10371 { 0.7977, 0.2880, 0.0000 },
10372 { 0.1352, 0.7119, 0.0000 },
10373 { 0.0313, 0.0001, 0.8249 } };
10374 int i, j;
10375
10376 for (i=0; i < 3; ++i)
10377 for (j=0; j < 3; ++j)
10378 xyz_rgb[i][j] = rimm[j][i];
10379 }
10380 #endif
10381 if (arg == argc) {
10382 fprintf (stderr,_("No files to process.\n"));
10383 return 1;
10384 }
10385 if (write_to_stdout) {
10386 if (isatty(1)) {
10387 fprintf (stderr,_("Will not write an image to the terminal!\n"));
10388 return 1;
10389 }
10390 #if defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__)
10391 if (setmode(1,O_BINARY) < 0) {
10392 perror ("setmode()");
10393 return 1;
10394 }
10395 #endif
10396 }
10397 for ( ; arg < argc; arg++) {
10398 status = 1;
10399 raw_image = 0;
10400 image = 0;
10401 oprof = 0;
10402 meta_data = ofname = 0;
10403 ofp = stdout;
10404 if (setjmp (failure)) {
10405 if (fileno(ifp) > 2) fclose(ifp);
10406 if (fileno(ofp) > 2) fclose(ofp);
10407 status = 1;
10408 goto cleanup;
10409 }
10410 ifname = argv[arg];
10411 if (!(ifp = fopen (ifname, "rb"))) {
10412 perror (ifname);
10413 continue;
10414 }
10415 status = (identify(),!is_raw);
10416 if (user_flip >= 0)
10417 flip = user_flip;
10418 switch ((flip+3600) % 360) {
10419 case 270: flip = 5; break;
10420 case 180: flip = 3; break;
10421 case 90: flip = 6;
10422 }
10423 if (timestamp_only) {
10424 if ((status = !timestamp))
10425 fprintf (stderr,_("%s has no timestamp.\n"), ifname);
10426 else if (identify_only)
10427 printf ("%10ld%10d %s\n", (long) timestamp, shot_order, ifname);
10428 else {
10429 if (verbose)
10430 fprintf (stderr,_("%s time set to %d.\n"), ifname, (int) timestamp);
10431 ut.actime = ut.modtime = timestamp;
10432 utime (ifname, &ut);
10433 }
10434 goto next;
10435 }
10436 write_fun = &CLASS write_ppm_tiff;
10437 if (thumbnail_only) {
10438 if ((status = !thumb_offset)) {
10439 fprintf (stderr,_("%s has no thumbnail.\n"), ifname);
10440 goto next;
10441 } else if (thumb_load_raw) {
10442 load_raw = thumb_load_raw;
10443 data_offset = thumb_offset;
10444 height = thumb_height;
10445 width = thumb_width;
10446 filters = 0;
10447 colors = 3;
10448 } else {
10449 fseek (ifp, thumb_offset, SEEK_SET);
10450 write_fun = write_thumb;
10451 goto thumbnail;
10452 }
10453 }
10454 if (load_raw == &CLASS kodak_ycbcr_load_raw) {
10455 height += height & 1;
10456 width += width & 1;
10457 }
10458 if (identify_only && verbose && make[0]) {
10459 printf (_("\nFilename: %s\n"), ifname);
10460 printf (_("Timestamp: %s"), ctime(×tamp));
10461 printf (_("Camera: %s %s\n"), make, model);
10462 if (artist[0])
10463 printf (_("Owner: %s\n"), artist);
10464 if (dng_version) {
10465 printf (_("DNG Version: "));
10466 for (i=24; i >= 0; i -= 8)
10467 printf ("%d%c", dng_version >> i & 255, i ? '.':'\n');
10468 }
10469 printf (_("ISO speed: %d\n"), (int) iso_speed);
10470 printf (_("Shutter: "));
10471 if (shutter > 0 && shutter < 1)
10472 shutter = (printf ("1/"), 1 / shutter);
10473 printf (_("%0.1f sec\n"), shutter);
10474 printf (_("Aperture: f/%0.1f\n"), aperture);
10475 printf (_("Focal length: %0.1f mm\n"), focal_len);
10476 printf (_("Embedded ICC profile: %s\n"), profile_length ? _("yes"):_("no"));
10477 printf (_("Number of raw images: %d\n"), is_raw);
10478 if (pixel_aspect != 1)
10479 printf (_("Pixel Aspect Ratio: %0.6f\n"), pixel_aspect);
10480 if (thumb_offset)
10481 printf (_("Thumb size: %4d x %d\n"), thumb_width, thumb_height);
10482 printf (_("Full size: %4d x %d\n"), raw_width, raw_height);
10483 } else if (!is_raw)
10484 fprintf (stderr,_("Cannot decode file %s\n"), ifname);
10485 if (!is_raw) goto next;
10486 shrink = filters && (half_size || (!identify_only &&
10487 (threshold || aber[0] != 1 || aber[2] != 1)));
10488 iheight = (height + shrink) >> shrink;
10489 iwidth = (width + shrink) >> shrink;
10490 if (identify_only) {
10491 if (verbose) {
10492 if (document_mode == 3) {
10493 top_margin = left_margin = fuji_width = 0;
10494 height = raw_height;
10495 width = raw_width;
10496 }
10497 iheight = (height + shrink) >> shrink;
10498 iwidth = (width + shrink) >> shrink;
10499 if (use_fuji_rotate) {
10500 if (fuji_width) {
10501 fuji_width = (fuji_width - 1 + shrink) >> shrink;
10502 iwidth = fuji_width / sqrt(0.5);
10503 iheight = (iheight - fuji_width) / sqrt(0.5);
10504 } else {
10505 if (pixel_aspect < 1) iheight = iheight / pixel_aspect + 0.5;
10506 if (pixel_aspect > 1) iwidth = iwidth * pixel_aspect + 0.5;
10507 }
10508 }
10509 if (flip & 4)
10510 SWAP(iheight,iwidth);
10511 printf (_("Image size: %4d x %d\n"), width, height);
10512 printf (_("Output size: %4d x %d\n"), iwidth, iheight);
10513 printf (_("Raw colors: %d"), colors);
10514 if (filters) {
10515 int fhigh = 2, fwide = 2;
10516 if ((filters ^ (filters >> 8)) & 0xff) fhigh = 4;
10517 if ((filters ^ (filters >> 16)) & 0xffff) fhigh = 8;
10518 if (filters == 1) fhigh = fwide = 16;
10519 if (filters == 9) fhigh = fwide = 6;
10520 printf (_("\nFilter pattern: "));
10521 for (i=0; i < fhigh; i++)
10522 for (c = i && putchar('/') && 0; c < fwide; c++)
10523 putchar (cdesc[fcol(i,c)]);
10524 }
10525 #ifdef LIGHTZONE
10526 printf(_("\nCamera RGB Profile:"));
10527 for (i=0; i<3; ++i) FORCC printf(" %f", rgb_cam[i][c]);
10528 #endif
10529 printf (_("\nDaylight multipliers:"));
10530 FORCC printf (" %f", pre_mul[c]);
10531 if (cam_mul[0] > 0) {
10532 printf (_("\nCamera multipliers:"));
10533 FORC4 printf (" %f", cam_mul[c]);
10534 }
10535 putchar ('\n');
10536 } else
10537 printf (_("%s is a %s %s image.\n"), ifname, make, model);
10538 next:
10539 fclose(ifp);
10540 continue;
10541 }
10542 if (meta_length) {
10543 meta_data = (char *) malloc (meta_length);
10544 merror (meta_data, "main()");
10545 }
10546 if (filters || colors == 1) {
10547 raw_image = (ushort *) calloc ((raw_height+7), raw_width*2);
10548 merror (raw_image, "main()");
10549 } else {
10550 image = (ushort (*)[4]) calloc (iheight, iwidth*sizeof *image);
10551 merror (image, "main()");
10552 }
10553 if (verbose)
10554 fprintf (stderr,_("Loading %s %s image from %s ...\n"),
10555 make, model, ifname);
10556 if (shot_select >= is_raw)
10557 fprintf (stderr,_("%s: \"-s %d\" requests a nonexistent image!\n"),
10558 ifname, shot_select);
10559 fseeko (ifp, data_offset, SEEK_SET);
10560 if (raw_image && read_from_stdin)
10561 fread (raw_image, 2, raw_height*raw_width, stdin);
10562 else (*load_raw)();
10563 if (document_mode == 3) {
10564 top_margin = left_margin = fuji_width = 0;
10565 height = raw_height;
10566 width = raw_width;
10567 }
10568 iheight = (height + shrink) >> shrink;
10569 iwidth = (width + shrink) >> shrink;
10570 if (raw_image) {
10571 image = (ushort (*)[4]) calloc (iheight, iwidth*sizeof *image);
10572 merror (image, "main()");
10573 crop_masked_pixels();
10574 free (raw_image);
10575 }
10576 if (zero_is_bad) remove_zeroes();
10577 bad_pixels (bpfile);
10578 if (dark_frame) subtract (dark_frame);
10579 quality = 2 + !fuji_width;
10580 if (user_qual >= 0) quality = user_qual;
10581 i = cblack[3];
10582 FORC3 if (i > cblack[c]) i = cblack[c];
10583 FORC4 cblack[c] -= i;
10584 black += i;
10585 i = cblack[6];
10586 FORC (cblack[4] * cblack[5])
10587 if (i > cblack[6+c]) i = cblack[6+c];
10588 FORC (cblack[4] * cblack[5])
10589 cblack[6+c] -= i;
10590 black += i;
10591 if (user_black >= 0) black = user_black;
10592 FORC4 cblack[c] += black;
10593 if (user_sat > 0) maximum = user_sat;
10594 #ifdef COLORCHECK
10595 colorcheck();
10596 #endif
10597 if (is_foveon) {
10598 if (document_mode || load_raw == &CLASS foveon_dp_load_raw) {
10599 for (i=0; i < height*width*4; i++)
10600 if ((short) image[0][i] < 0) image[0][i] = 0;
10601 } else foveon_interpolate();
10602 } else if (document_mode < 2)
10603 scale_colors();
10604 pre_interpolate();
10605 if (filters && !document_mode) {
10606 if (quality == 0)
10607 lin_interpolate();
10608 else if (quality == 1 || colors > 3)
10609 vng_interpolate();
10610 else if (quality == 2 && filters > 1000)
10611 ppg_interpolate();
10612 else if (filters == 9)
10613 xtrans_interpolate (quality*2-3);
10614 else
10615 ahd_interpolate();
10616 }
10617 if (mix_green)
10618 for (colors=3, i=0; i < height*width; i++)
10619 image[i][1] = (image[i][1] + image[i][3]) >> 1;
10620 if (!is_foveon && colors == 3) median_filter();
10621 if (!is_foveon && highlight == 2) blend_highlights();
10622 if (!is_foveon && highlight > 2) recover_highlights();
10623 if (use_fuji_rotate) fuji_rotate();
10624 #ifndef NO_LCMS
10625 if (cam_profile) apply_profile (cam_profile, out_profile);
10626 #endif
10627 convert_to_rgb();
10628 if (use_fuji_rotate) stretch();
10629 thumbnail:
10630 if (write_fun == &CLASS jpeg_thumb)
10631 write_ext = ".jpg";
10632 else if (output_tiff && write_fun == &CLASS write_ppm_tiff)
10633 write_ext = ".tiff";
10634 else
10635 write_ext = ".pgm\0.ppm\0.ppm\0.pam" + colors*5-5;
10636 ofname = (char *) malloc (strlen(ifname) + 64);
10637 merror (ofname, "main()");
10638 if (write_to_stdout)
10639 strcpy (ofname,_("standard output"));
10640 else {
10641 #ifdef LIGHTZONE
10642 if (ofbase)
10643 strcpy (ofname, ofbase);
10644 else
10645 #endif
10646 strcpy (ofname, ifname);
10647 if ((cp = strrchr (ofname, '.'))) *cp = 0;
10648 if (multi_out)
10649 sprintf (ofname+strlen(ofname), "_%0*d",
10650 snprintf(0,0,"%d",is_raw-1), shot_select);
10651 if (thumbnail_only)
10652 strcat (ofname, ".thumb");
10653 strcat (ofname, write_ext);
10654 ofp = fopen (ofname, "wb");
10655 if (!ofp) {
10656 status = 1;
10657 perror (ofname);
10658 goto cleanup;
10659 }
10660 }
10661 if (verbose)
10662 fprintf (stderr,_("Writing data to %s ...\n"), ofname);
10663 (*write_fun)();
10664 fclose(ifp);
10665 if (ofp != stdout) fclose(ofp);
10666 cleanup:
10667 if (meta_data) free (meta_data);
10668 if (ofname) free (ofname);
10669 if (oprof) free (oprof);
10670 if (image) free (image);
10671 if (multi_out) {
10672 if (++shot_select < is_raw) arg--;
10673 else shot_select = 0;
10674 }
10675 }
10676 return status;
10677 }
10678
10679 /* vim:set noet sw=8 ts=8: */
10680