1 /*
2 dcraw.cc - Dave Coffin's raw photo decoder - C++ adaptation
3 Copyright 1997-2010 by Dave Coffin, dcoffin a cybercom o net
4 Copyright 2004-2010 by Udi Fuchs, udifuchs a gmail o com
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This is a adaptation of Dave Coffin's original dcraw.c to C++.
12 It can work as either a command-line tool or called by other programs.
13
14 $Revision: 1.439 $
15 $Date: 2010/11/11 12:43:11 $
16 */
17
18 /*
19 Note for Rawstudio developers:
20
21 dcraw is copied from UFRaw CVS at:
22 pserver:anonymous@ufraw.cvs.sourceforge.net:/cvsroot/ufraw
23
24 Current revisions:
25 dcraw.cc [1.235]
26 dcraw.h [1.65]
27 dcraw_api.cc [1.72]
28 dcraw_api.h [1.42]
29
30 - Thanks Dave and Udi, you rock!
31
32 */
33
34 #ifdef HAVE_CONFIG_H /*For UFRaw config system - NKBJ*/
35 #include "config.h"
36 #endif
37
38 #define DCRAW_VERSION "9.05"
39
40 //#ifndef _GNU_SOURCE
41 //#define _GNU_SOURCE
42 //#endif
43 #define _USE_MATH_DEFINES
44 #include <ctype.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <float.h>
48 #include <limits.h>
49 #include <math.h>
50 #include <setjmp.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <time.h>
55 #ifdef WITH_MMAP_HACK
56 #include "mmap-hack.h"
57 #endif
58 #ifdef HAVE_SYS_TYPES_H
59 #include <sys/types.h> /*For off_t */
60 #endif
61 /*
62 NO_LCMS disables the "-p" option.
63 */
64 #ifndef NO_LCMS
65 #include <lcms.h>
66 #endif
67 #ifdef HAVE_LIBJPEG
68 extern "C" {
69 #include <jpeglib.h>
70 }
71 #endif
72 //#ifdef LOCALEDIR
73 //#include <libintl.h>
74 //#define _(String) gettext(String)
75 //#else
76 //#define _(String) (String)
77 //#endif
78 #include <glib/gi18n.h> /*For _(String) definition - NKBJ*/
79 //#define fgetc getc_unlocked
80 //#ifdef DJGPP
81 //#define fseeko fseek
82 //#define ftello ftell
83 //#else
84 //#define fgetc getc_unlocked
85 //#endif
86 #ifdef __CYGWIN__
87 #include <io.h>
88 #endif
89 #ifdef WIN32
90 #include <sys/utime.h>
91 #include <winsock2.h>
92 #ifndef __MINGW32__ /* causes warnings in cygwin's gcc -mno-cygwin UF*/
93 #pragma comment(lib, "ws2_32.lib")
94 #endif /* __MINGW32__ UF*/
95 #define snprintf _snprintf
96 #define strcasecmp stricmp
97 #define strncasecmp strnicmp
98 typedef __int64 INT64;
99 typedef unsigned __int64 UINT64;
100 #else
101 #include <unistd.h>
102 #include <utime.h>
103 #include <netinet/in.h>
104 typedef long long INT64;
105 typedef unsigned long long UINT64;
106 #endif
107
108 #ifdef LJPEG_DECODE
109 #error Please compile dcraw.c by itself.
110 #error Do not link it with ljpeg_decode.
111 #endif
112
113 #ifndef LONG_BIT
114 #define LONG_BIT (8 * sizeof (long))
115 #endif
116
117 /* All definitions of global variables are defined inside a class in dcraw.h */
118 #include "dcraw.h"
119
120 #ifdef DCRAW_NOMAIN
121 extern
122 #endif
123 const double xyz_rgb[3][3] = { /* XYZ from RGB */
124 { 0.412453, 0.357580, 0.180423 },
125 { 0.212671, 0.715160, 0.072169 },
126 { 0.019334, 0.119193, 0.950227 } };
127 #ifdef DCRAW_NOMAIN
128 extern
129 #endif
130 const float d65_white[3] = { 0.950456, 1, 1.088754 };
131
132 #define DCRAW_SUCCESS 0 /* Centralize the error handling - UF*/
133 #define DCRAW_ERROR 1
134 #define DCRAW_UNSUPPORTED 2
135 #define DCRAW_NO_CAMERA_WB 3
136 #define DCRAW_VERBOSE 4
137 #define DCRAW_WARNING 5
138
139 #define CLASS DCRaw::
140
DCRaw()141 CLASS DCRaw()
142 {
143 order=0; /* Suppress valgrind error. */
144 shot_select=0, multi_out=0, aber[0] = aber[1] = aber[2] = aber[3] = 1;
145 gamm[0] = 0.45, gamm[1] = 4.5, gamm[2] = gamm[3] = gamm[4] = gamm[5] = 0;
146 bright=1, user_mul[0] = user_mul[1] = user_mul[2] = user_mul[3] = 0;
147 threshold=0, half_size=0, four_color_rgb=0, document_mode=0, highlight=0;
148 verbose=0, use_auto_wb=0, use_camera_wb=0, use_camera_matrix=-1;
149 output_color=1, output_bps=8, output_tiff=0, med_passes=0, no_auto_bright=0;
150 greybox[0] = greybox[1] = 0, greybox[2] = greybox[3] = UINT_MAX;
151 tone_curve_size = 0, tone_curve_offset = 0; /* Nikon Tone Curves UF*/
152 tone_mode_offset = 0, tone_mode_size = 0; /* Nikon ToneComp UF*/
153 messageBuffer = NULL;
154 lastStatus = DCRAW_SUCCESS;
155 ifname = NULL;
156 ifname_display = NULL;
157 ifpReadCount = 0;
158 ifpSize = 0;
159 ifpStepProgress = 0;
160 }
161
~DCRaw()162 CLASS ~DCRaw()
163 {
164 free(ifname);
165 free(ifname_display);
166 }
167
ifpProgress(unsigned readCount)168 void CLASS ifpProgress(unsigned readCount) {
169 /* We don't use this in Rawstudio */
170 #if 0
171 ifpReadCount += readCount;
172 if (ifpSize==0) return;
173 unsigned newStepProgress = STEPS * ifpReadCount / ifpSize;
174 if (newStepProgress > ifpStepProgress) {
175 #ifdef DCRAW_NOMAIN
176 if (ifpStepProgress)
177 progress(PROGRESS_LOAD, newStepProgress - ifpStepProgress);
178 else
179 progress(PROGRESS_LOAD, -STEPS);
180 #endif
181 }
182 ifpStepProgress = newStepProgress;
183 #endif
184 }
185
186 #ifndef WITH_MMAP_HACK
187
fread(void * ptr,size_t size,size_t nmemb,FILE * stream)188 size_t CLASS fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
189 size_t num = ::fread(ptr, size, nmemb, stream);
190 if ( num != nmemb )
191 // Maybe this should be a DCRAW_WARNING
192 dcraw_message(DCRAW_VERBOSE, "%s: fread %d != %d\n",
193 ifname_display, num, nmemb);
194 if (stream==ifp) ifpProgress(size*nmemb);
195 return num;
196 }
197
fwrite(const void * ptr,size_t size,size_t nmemb,FILE * stream)198 size_t CLASS fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
199 size_t num = ::fwrite(ptr, size, nmemb, stream);
200 if ( num != nmemb )
201 dcraw_message(DCRAW_WARNING, "%s: fwrite %d != %d\n",
202 ifname_display, num, nmemb);
203 return num;
204 }
205
fgets(char * s,int size,FILE * stream)206 char *CLASS fgets(char *s, int size, FILE *stream) {
207 char *str = ::fgets(s, size, stream);
208 if ( str==NULL )
209 // Maybe this should be a DCRAW_WARNING
210 dcraw_message(DCRAW_VERBOSE, "%s: fgets returned NULL\n",
211 ifname_display);
212 if (stream==ifp) ifpProgress(strlen(s));
213 return str;
214 }
215
fgetc(FILE * stream)216 int CLASS fgetc(FILE *stream) {
217 int chr = ::fgetc(stream);
218 if (stream==ifp) ifpProgress(1);
219 return chr;
220 }
221
fscanf(FILE * stream,const char * format,void * ptr)222 int CLASS fscanf(FILE *stream, const char *format, void *ptr) {
223 int count = ::fscanf(stream, format, ptr);
224 if ( count != 1 )
225 dcraw_message(DCRAW_WARNING, "%s: fscanf %d != 1\n",
226 ifname_display, count);
227 return 1;
228 }
229
230 #endif /* #ifndef WITH_MMAP_HACK */
231
232 #define FORC(cnt) for (c=0; c < cnt; c++)
233 #define FORC3 FORC(3)
234 #define FORC4 FORC(4)
235 #define FORCC FORC(colors)
236
237 #define SQR(x) ((x)*(x))
238 #ifndef DCRAW_NOMAIN
239 #define ABS(x) (((int)(x) ^ ((int)(x) >> 31)) - ((int)(x) >> 31))
240 #define MIN(a,b) ((a) < (b) ? (a) : (b))
241 #define MAX(a,b) ((a) > (b) ? (a) : (b))
242 #endif
243 #define LIM(x,min,max) MAX(min,MIN(x,max))
244 #define ULIM(x,y,z) ((y) < (z) ? LIM(x,y,z) : LIM(x,z,y))
245 #define CLIP(x) LIM(x,0,65535)
246 #define SWAP(a,b) { a=a+b; b=a-b; a=a-b; }
247
248 /*
249 In order to inline this calculation, I make the risky
250 assumption that all filter patterns can be described
251 by a repeating pattern of eight rows and two columns
252
253 Do not use the FC or BAYER macros with the Leaf CatchLight,
254 because its pattern is 16x16, not 2x8.
255
256 Return values are either 0/1/2/3 = G/M/C/Y or 0/1/2/3 = R/G1/B/G2
257
258 PowerShot 600 PowerShot A50 PowerShot Pro70 Pro90 & G1
259 0xe1e4e1e4: 0x1b4e4b1e: 0x1e4b4e1b: 0xb4b4b4b4:
260
261 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
262 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
263 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
264 2 M G M G M G 2 Y C Y C Y C 2 C Y C Y C Y
265 3 C Y C Y C Y 3 G M G M G M 3 G M G M G M
266 4 C Y C Y C Y 4 Y C Y C Y C
267 PowerShot A5 5 G M G M G M 5 G M G M G M
268 0x1e4e1e4e: 6 Y C Y C Y C 6 C Y C Y C Y
269 7 M G M G M G 7 M G M G M G
270 0 1 2 3 4 5
271 0 C Y C Y C Y
272 1 G M G M G M
273 2 C Y C Y C Y
274 3 M G M G M G
275
276 All RGB cameras use one of these Bayer grids:
277
278 0x16161616: 0x61616161: 0x49494949: 0x94949494:
279
280 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
281 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
282 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
283 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
284 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
285 */
286
287 #define FC(row,col) \
288 (filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
289
290 #define BAYER(row,col) \
291 image[((row) >> shrink)*iwidth + ((col) >> shrink)][FC(row,col)]
292
293 #define BAYER2(row,col) \
294 image[((row) >> shrink)*iwidth + ((col) >> shrink)][fc(row,col)]
295
fc(int row,int col)296 int CLASS fc (int row, int col)
297 {
298 static const char filter[16][16] =
299 { { 2,1,1,3,2,3,2,0,3,2,3,0,1,2,1,0 },
300 { 0,3,0,2,0,1,3,1,0,1,1,2,0,3,3,2 },
301 { 2,3,3,2,3,1,1,3,3,1,2,1,2,0,0,3 },
302 { 0,1,0,1,0,2,0,2,2,0,3,0,1,3,2,1 },
303 { 3,1,1,2,0,1,0,2,1,3,1,3,0,1,3,0 },
304 { 2,0,0,3,3,2,3,1,2,0,2,0,3,2,2,1 },
305 { 2,3,3,1,2,1,2,1,2,1,1,2,3,0,0,1 },
306 { 1,0,0,2,3,0,0,3,0,3,0,3,2,1,2,3 },
307 { 2,3,3,1,1,2,1,0,3,2,3,0,2,3,1,3 },
308 { 1,0,2,0,3,0,3,2,0,1,1,2,0,1,0,2 },
309 { 0,1,1,3,3,2,2,1,1,3,3,0,2,1,3,2 },
310 { 2,3,2,0,0,1,3,0,2,0,1,2,3,0,1,0 },
311 { 1,3,1,2,3,2,3,2,0,2,0,1,1,0,3,0 },
312 { 0,2,0,3,1,0,0,1,1,3,3,2,3,2,2,1 },
313 { 2,1,3,2,3,1,2,1,0,3,0,2,0,2,0,2 },
314 { 0,3,1,0,0,2,0,3,2,1,3,1,1,3,1,3 } };
315
316 if (filters != 1) return FC(row,col);
317 return filter[(row+top_margin) & 15][(col+left_margin) & 15];
318 }
319
320 #ifndef HAVE_MEMMEM
memmem(const char * haystack,size_t haystacklen,const char * needle,size_t needlelen)321 const char *memmem (const char *haystack, size_t haystacklen,
322 const char *needle, size_t needlelen)
323 {
324 const char *c;
325 for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
326 if (!memcmp (c, needle, needlelen))
327 return c;
328 return 0;
329 }
330 #endif
331
332 #ifndef DCRAW_NOMAIN
333 #include <stdarg.h>
dcraw_message(int code,const char * format,...)334 void CLASS dcraw_message(int code, const char *format, ...) {
335 if (verbose || code!=DCRAW_VERBOSE) {
336 va_list ap;
337 va_start(ap, format);
338 vfprintf(stderr, format, ap);
339 va_end(ap);
340 }
341 }
342 #endif /*DCRAW_NOMAIN*/
343
merror(void * ptr,const char * where)344 void CLASS merror (void *ptr, const char *where)
345 {
346 if (ptr) return;
347 dcraw_message (DCRAW_ERROR,_("%s: Out of memory in %s\n"), ifname_display, where);
348 longjmp (failure, 1);
349 }
350
derror()351 void CLASS derror()
352 {
353 if (!data_error) {
354 dcraw_message (DCRAW_WARNING, "%s: ", ifname_display);
355 if (feof(ifp))
356 dcraw_message (DCRAW_WARNING,_("Unexpected end of file\n"));
357 else
358 #ifdef HAVE_FSEEKO
359 dcraw_message (DCRAW_WARNING,_("Corrupt data near 0x%llx\n"),
360 (INT64) ftello(ifp));
361 #else
362 dcraw_message (DCRAW_WARNING,_("Corrupt data near 0x%lx\n"), ftell(ifp));
363 #endif
364 }
365 data_error++;
366 }
367
sget2(uchar * s)368 ushort CLASS sget2 (uchar *s)
369 {
370 if (order == 0x4949) /* "II" means little-endian */
371 return s[0] | s[1] << 8;
372 else /* "MM" means big-endian */
373 return s[0] << 8 | s[1];
374 }
375
get2()376 ushort CLASS get2()
377 {
378 uchar str[2] = { 0xff,0xff };
379 fread (str, 1, 2, ifp);
380 return sget2(str);
381 }
382
sget4(uchar * s)383 unsigned CLASS sget4 (uchar *s)
384 {
385 if (order == 0x4949)
386 return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
387 else
388 return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
389 }
390 #define sget4(s) sget4((uchar *)s)
391
get4()392 unsigned CLASS get4()
393 {
394 uchar str[4] = { 0xff,0xff,0xff,0xff };
395 fread (str, 1, 4, ifp);
396 return sget4(str);
397 }
398
getint(int type)399 unsigned CLASS getint (int type)
400 {
401 return type == 3 ? get2() : get4();
402 }
403
int_to_float(int i)404 float CLASS int_to_float (int i)
405 {
406 union { int i; float f; } u;
407 u.i = i;
408 return u.f;
409 }
410
getreal(int type)411 double CLASS getreal (int type)
412 {
413 union { char c[8]; double d; } u;
414 int i, rev;
415
416 switch (type) {
417 case 3: return (unsigned short) get2();
418 case 4: return (unsigned int) get4();
419 case 5: u.d = (unsigned int) get4();
420 return u.d / (unsigned int) get4();
421 case 8: return (signed short) get2();
422 case 9: return (signed int) get4();
423 case 10: u.d = (signed int) get4();
424 return u.d / (signed int) get4();
425 case 11: return int_to_float (get4());
426 case 12:
427 rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
428 for (i=0; i < 8; i++)
429 u.c[i ^ rev] = fgetc(ifp);
430 return u.d;
431 default: return fgetc(ifp);
432 }
433 }
434
read_shorts(ushort * pixel,unsigned count)435 void CLASS read_shorts (ushort *pixel, unsigned count)
436 {
437 if (fread (pixel, 2, count, ifp) < count) derror();
438 if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
439 swab ((const char *)pixel, (char *)pixel, count*2); /*mingw support UF*/
440 }
441
canon_600_fixed_wb(int temp)442 void CLASS canon_600_fixed_wb (int temp)
443 {
444 static const short mul[4][5] = {
445 { 667, 358,397,565,452 },
446 { 731, 390,367,499,517 },
447 { 1119, 396,348,448,537 },
448 { 1399, 485,431,508,688 } };
449 int lo, hi, i;
450 float frac=0;
451
452 for (lo=4; --lo; )
453 if (*mul[lo] <= temp) break;
454 for (hi=0; hi < 3; hi++)
455 if (*mul[hi] >= temp) break;
456 if (lo != hi)
457 frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
458 for (i=1; i < 5; i++)
459 pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
460 }
461
462 /* Return values: 0 = white 1 = near white 2 = not white */
canon_600_color(int ratio[2],int mar)463 int CLASS canon_600_color (int ratio[2], int mar)
464 {
465 int clipped=0, target, miss;
466
467 if (flash_used) {
468 if (ratio[1] < -104)
469 { ratio[1] = -104; clipped = 1; }
470 if (ratio[1] > 12)
471 { ratio[1] = 12; clipped = 1; }
472 } else {
473 if (ratio[1] < -264 || ratio[1] > 461) return 2;
474 if (ratio[1] < -50)
475 { ratio[1] = -50; clipped = 1; }
476 if (ratio[1] > 307)
477 { ratio[1] = 307; clipped = 1; }
478 }
479 target = flash_used || ratio[1] < 197
480 ? -38 - (398 * ratio[1] >> 10)
481 : -123 + (48 * ratio[1] >> 10);
482 if (target - mar <= ratio[0] &&
483 target + 20 >= ratio[0] && !clipped) return 0;
484 miss = target - ratio[0];
485 if (abs(miss) >= mar*4) return 2;
486 if (miss < -20) miss = -20;
487 if (miss > mar) miss = mar;
488 ratio[0] = target - miss;
489 return 1;
490 }
491
canon_600_auto_wb()492 void CLASS canon_600_auto_wb()
493 {
494 int mar, row, col, i, j, st, count[] = { 0,0 };
495 int test[8], total[2][8], ratio[2][2], stat[2];
496
497 memset (&total, 0, sizeof total);
498 i = canon_ev + 0.5;
499 if (i < 10) mar = 150;
500 else if (i > 12) mar = 20;
501 else mar = 280 - 20 * i;
502 if (flash_used) mar = 80;
503 for (row=14; row < height-14; row+=4)
504 for (col=10; col < width; col+=2) {
505 for (i=0; i < 8; i++)
506 test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
507 BAYER(row+(i >> 1),col+(i & 1));
508 for (i=0; i < 8; i++)
509 if (test[i] < 150 || test[i] > 1500) goto next;
510 for (i=0; i < 4; i++)
511 if (abs(test[i] - test[i+4]) > 50) goto next;
512 for (i=0; i < 2; i++) {
513 for (j=0; j < 4; j+=2)
514 ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
515 stat[i] = canon_600_color (ratio[i], mar);
516 }
517 if ((st = stat[0] | stat[1]) > 1) goto next;
518 for (i=0; i < 2; i++)
519 if (stat[i])
520 for (j=0; j < 2; j++)
521 test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
522 for (i=0; i < 8; i++)
523 total[st][i] += test[i];
524 count[st]++;
525 next: ;
526 }
527 if (count[0] | count[1]) {
528 st = count[0]*200 < count[1];
529 for (i=0; i < 4; i++)
530 pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
531 }
532 }
533
canon_600_coeff()534 void CLASS canon_600_coeff()
535 {
536 static const short table[6][12] = {
537 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
538 { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
539 { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
540 { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
541 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
542 { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
543 int t=0, i, c;
544 float mc, yc;
545
546 mc = pre_mul[1] / pre_mul[2];
547 yc = pre_mul[3] / pre_mul[2];
548 if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
549 if (mc > 1.28 && mc <= 2) {
550 if (yc < 0.8789) t=3;
551 else if (yc <= 2) t=4;
552 }
553 if (flash_used) t=5;
554 for (raw_color = i=0; i < 3; i++)
555 FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
556 }
557
canon_600_load_raw()558 void CLASS canon_600_load_raw()
559 {
560 uchar data[1120], *dp;
561 ushort pixel[896], *pix;
562 int irow, row, col, val;
563 static const short mul[4][2] =
564 { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
565
566 for (irow=row=0; irow < height; irow++) {
567 if ((int)fread (data, 1, raw_width*5/4, ifp) < raw_width*5/4) derror();
568 for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8) {
569 pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
570 pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
571 pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
572 pix[3] = (dp[4] << 2) + (dp[1] & 3);
573 pix[4] = (dp[5] << 2) + (dp[9] & 3);
574 pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
575 pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
576 pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
577 }
578 for (col=0; col < width; col++)
579 BAYER(row,col) = pixel[col];
580 for (col=width; col < raw_width; col++)
581 black += pixel[col];
582 if ((row+=2) > height) row = 1;
583 }
584 if (raw_width > width)
585 black = black / ((raw_width - width) * height) - 4;
586 for (row=0; row < height; row++)
587 for (col=0; col < width; col++) {
588 if ((val = BAYER(row,col) - black) < 0) val = 0;
589 val = val * mul[row & 3][col & 1] >> 9;
590 BAYER(row,col) = val;
591 }
592 canon_600_fixed_wb(1311);
593 canon_600_auto_wb();
594 canon_600_coeff();
595 maximum = (0x3ff - black) * 1109 >> 9;
596 black = 0;
597 }
598
remove_zeroes()599 void CLASS remove_zeroes()
600 {
601 unsigned row, col, tot, n, r, c;
602
603 for (row=0; row < height; row++)
604 for (col=0; col < width; col++)
605 if (BAYER(row,col) == 0) {
606 tot = n = 0;
607 for (r = row-2; r <= row+2; r++)
608 for (c = col-2; c <= col+2; c++)
609 if (r < height && c < width &&
610 FC(r,c) == FC(row,col) && BAYER(r,c))
611 tot += (n++,BAYER(r,c));
612 if (n) BAYER(row,col) = tot/n;
613 }
614 }
615
canon_s2is()616 int CLASS canon_s2is()
617 {
618 unsigned row;
619
620 for (row=0; row < 100; row++) {
621 fseek (ifp, row*3340 + 3284, SEEK_SET);
622 if (getc(ifp) > 15) return 1;
623 }
624 return 0;
625 }
626
627 /*
628 getbits(-1) initializes the buffer
629 getbits(n) where 0 <= n <= 25 returns an n-bit integer
630 */
getbithuff(int nbits,ushort * huff)631 unsigned CLASS getbithuff (int nbits, ushort *huff)
632 {
633 int c;
634
635 if (nbits == -1)
636 return bitbuf = vbits = reset = 0;
637 if (nbits == 0 || vbits < 0) return 0;
638 while (!reset && vbits < nbits && (c = fgetc(ifp)) != EOF &&
639 !(reset = zero_after_ff && c == 0xff && fgetc(ifp))) {
640 bitbuf = (bitbuf << 8) + (uchar) c;
641 vbits += 8;
642 }
643 c = bitbuf << (32-vbits) >> (32-nbits);
644 if (huff) {
645 vbits -= huff[c] >> 8;
646 c = (uchar) huff[c];
647 } else
648 vbits -= nbits;
649 if (vbits < 0) derror();
650 return c;
651 }
652
653 #define getbits(n) getbithuff(n,0)
654 #define gethuff(h) getbithuff(*h,h+1)
655
656 /*
657 Construct a decode tree according the specification in *source.
658 The first 16 bytes specify how many codes should be 1-bit, 2-bit
659 3-bit, etc. Bytes after that are the leaf values.
660
661 For example, if the source is
662
663 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
664 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
665
666 then the code is
667
668 00 0x04
669 010 0x03
670 011 0x05
671 100 0x06
672 101 0x02
673 1100 0x07
674 1101 0x01
675 11100 0x08
676 11101 0x09
677 11110 0x00
678 111110 0x0a
679 1111110 0x0b
680 1111111 0xff
681 */
make_decoder_ref(const uchar ** source)682 ushort * CLASS make_decoder_ref (const uchar **source)
683 {
684 int max, len, h, i, j;
685 const uchar *count;
686 ushort *huff;
687
688 count = (*source += 16) - 17;
689 for (max=16; max && !count[max]; max--);
690 huff = (ushort *) calloc (1 + (1 << max), sizeof *huff);
691 merror (huff, "make_decoder()");
692 huff[0] = max;
693 for (h=len=1; len <= max; len++)
694 for (i=0; i < count[len]; i++, ++*source)
695 for (j=0; j < 1 << (max-len); j++)
696 if (h <= 1 << max)
697 huff[h++] = len << 8 | **source;
698 return huff;
699 }
700
make_decoder(const uchar * source)701 ushort * CLASS make_decoder (const uchar *source)
702 {
703 return make_decoder_ref (&source);
704 }
705
crw_init_tables(unsigned table,ushort * huff[2])706 void CLASS crw_init_tables (unsigned table, ushort *huff[2])
707 {
708 static const uchar first_tree[3][29] = {
709 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
710 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
711 { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
712 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
713 { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
714 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
715 };
716 static const uchar second_tree[3][180] = {
717 { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
718 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
719 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
720 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
721 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
722 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
723 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
724 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
725 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
726 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
727 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
728 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
729 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
730 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
731 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
732 { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
733 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
734 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
735 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
736 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
737 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
738 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
739 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
740 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
741 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
742 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
743 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
744 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
745 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
746 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
747 { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
748 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
749 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
750 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
751 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
752 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
753 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
754 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
755 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
756 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
757 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
758 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
759 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
760 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
761 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
762 };
763 if (table > 2) table = 2;
764 huff[0] = make_decoder ( first_tree[table]);
765 huff[1] = make_decoder (second_tree[table]);
766 }
767
768 /*
769 Return 0 if the image starts with compressed data,
770 1 if it starts with uncompressed low-order bits.
771
772 In Canon compressed data, 0xff is always followed by 0x00.
773 */
canon_has_lowbits()774 int CLASS canon_has_lowbits()
775 {
776 uchar test[0x4000];
777 int ret=1, i;
778
779 fseek (ifp, 0, SEEK_SET);
780 fread (test, 1, sizeof test, ifp);
781 for (i=540; i < (int) sizeof test - 1; i++)
782 if (test[i] == 0xff) {
783 if (test[i+1]) return 1;
784 ret=0;
785 }
786 return ret;
787 }
788
canon_compressed_load_raw()789 void CLASS canon_compressed_load_raw()
790 {
791 ushort *pixel, *prow, *huff[2];
792 int nblocks, lowbits, i, c, row, r, col, save, val;
793 unsigned irow, icol;
794 int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
795
796 crw_init_tables (tiff_compress, huff);
797 pixel = (ushort *) calloc (raw_width*8, sizeof *pixel);
798 merror (pixel, "canon_compressed_load_raw()");
799 lowbits = canon_has_lowbits();
800 if (!lowbits) maximum = 0x3ff;
801 fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
802 zero_after_ff = 1;
803 getbits(-1);
804 for (row=0; row < raw_height; row+=8) {
805 nblocks = MIN (8, raw_height-row) * raw_width >> 6;
806 for (block=0; block < nblocks; block++) {
807 memset (diffbuf, 0, sizeof diffbuf);
808 for (i=0; i < 64; i++ ) {
809 leaf = gethuff(huff[i > 0]);
810 if (leaf == 0 && i) break;
811 if (leaf == 0xff) continue;
812 i += leaf >> 4;
813 len = leaf & 15;
814 if (len == 0) continue;
815 diff = getbits(len);
816 if ((diff & (1 << (len-1))) == 0)
817 diff -= (1 << len) - 1;
818 if (i < 64) diffbuf[i] = diff;
819 }
820 diffbuf[0] += carry;
821 carry = diffbuf[0];
822 for (i=0; i < 64; i++ ) {
823 if (pnum++ % raw_width == 0)
824 base[0] = base[1] = 512;
825 if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
826 derror();
827 }
828 }
829 if (lowbits) {
830 save = ftell(ifp);
831 fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
832 for (prow=pixel, i=0; i < raw_width*2; i++) {
833 c = fgetc(ifp);
834 for (r=0; r < 8; r+=2, prow++) {
835 val = (*prow << 2) + ((c >> r) & 3);
836 if (raw_width == 2672 && val < 512) val += 2;
837 *prow = val;
838 }
839 }
840 fseek (ifp, save, SEEK_SET);
841 }
842 for (r=0; r < 8; r++) {
843 irow = row - top_margin + r;
844 if (irow >= height) continue;
845 for (col=0; col < raw_width; col++) {
846 icol = col - left_margin;
847 c = FC(irow,icol);
848 if (icol < width)
849 BAYER(irow,icol) = pixel[r*raw_width+col];
850 else if (col > 1 && (unsigned) (col-left_margin+2) >
851 (unsigned) (width+3))
852 cblack[c] += (cblack[4+c]++,pixel[r*raw_width+col]);
853 }
854 }
855 }
856 free (pixel);
857 FORC(2) free (huff[c]);
858 FORC4 if (cblack[4+c]) cblack[c] /= cblack[4+c];
859 }
860
861 /*
862 Not a full implementation of Lossless JPEG, just
863 enough to decode Canon, Kodak and Adobe DNG images.
864 */
865 struct jhead {
866 int bits, high, wide, clrs, sraw, psv, restart, vpred[6];
867 ushort *huff[6], *free[4], *row;
868 };
869
ljpeg_start(struct jhead * jh,int info_only)870 int CLASS ljpeg_start (struct jhead *jh, int info_only)
871 {
872 int c, tag;
873 ushort len;
874 uchar data[0x10000];
875 const uchar *dp;
876
877 memset (jh, 0, sizeof *jh);
878 jh->restart = INT_MAX;
879 fread (data, 2, 1, ifp);
880 if (data[1] != 0xd8) return 0;
881 do {
882 fread (data, 2, 2, ifp);
883 tag = data[0] << 8 | data[1];
884 len = (data[2] << 8 | data[3]) - 2;
885 if (tag <= 0xff00) return 0;
886 fread (data, 1, len, ifp);
887 switch (tag) {
888 case 0xffc3:
889 jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
890 case 0xffc0:
891 jh->bits = data[0];
892 jh->high = data[1] << 8 | data[2];
893 jh->wide = data[3] << 8 | data[4];
894 jh->clrs = data[5] + jh->sraw;
895 if (len == 9 && !dng_version) getc(ifp);
896 break;
897 case 0xffc4:
898 if (info_only) break;
899 for (dp = data; dp < data+len && (c = *dp++) < 4; )
900 jh->free[c] = jh->huff[c] = make_decoder_ref (&dp);
901 break;
902 case 0xffda:
903 jh->psv = data[1+data[0]*2];
904 jh->bits -= data[3+data[0]*2] & 15;
905 break;
906 case 0xffdd:
907 jh->restart = data[0] << 8 | data[1];
908 }
909 } while (tag != 0xffda);
910 if (info_only) return 1;
911 FORC(5) if (!jh->huff[c+1]) jh->huff[c+1] = jh->huff[c];
912 if (jh->sraw) {
913 FORC(4) jh->huff[2+c] = jh->huff[1];
914 FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
915 }
916 jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
917 merror (jh->row, "ljpeg_start()");
918 return zero_after_ff = 1;
919 }
920
ljpeg_end(struct jhead * jh)921 void CLASS ljpeg_end (struct jhead *jh)
922 {
923 int c;
924 FORC4 if (jh->free[c]) free (jh->free[c]);
925 free (jh->row);
926 }
927
ljpeg_diff(ushort * huff)928 int CLASS ljpeg_diff (ushort *huff)
929 {
930 int len, diff;
931
932 len = gethuff(huff);
933 if (len == 16 && (!dng_version || dng_version >= 0x1010000))
934 return -32768;
935 diff = getbits(len);
936 if ((diff & (1 << (len-1))) == 0)
937 diff -= (1 << len) - 1;
938 return diff;
939 }
940
ljpeg_row(int jrow,struct jhead * jh)941 ushort * CLASS ljpeg_row (int jrow, struct jhead *jh)
942 {
943 int col, c, diff, pred, spred=0;
944 ushort mark=0, *row[3];
945
946 if (jrow * jh->wide % jh->restart == 0) {
947 FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
948 if (jrow) {
949 fseek (ifp, -2, SEEK_CUR);
950 do mark = (mark << 8) + (c = fgetc(ifp));
951 while (c != EOF && mark >> 4 != 0xffd);
952 }
953 getbits(-1);
954 }
955 FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
956 for (col=0; col < jh->wide; col++)
957 FORC(jh->clrs) {
958 diff = ljpeg_diff (jh->huff[c]);
959 if (jh->sraw && c <= jh->sraw && (col | c))
960 pred = spred;
961 else if (col) pred = row[0][-jh->clrs];
962 else pred = (jh->vpred[c] += diff) - diff;
963 if (jrow && col) switch (jh->psv) {
964 case 1: break;
965 case 2: pred = row[1][0]; break;
966 case 3: pred = row[1][-jh->clrs]; break;
967 case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
968 case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
969 case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
970 case 7: pred = (pred + row[1][0]) >> 1; break;
971 default: pred = 0;
972 }
973 if ((**row = pred + diff) >> jh->bits) derror();
974 if (c <= jh->sraw) spred = **row;
975 row[0]++; row[1]++;
976 }
977 return row[2];
978 }
979
lossless_jpeg_load_raw()980 void CLASS lossless_jpeg_load_raw()
981 {
982 int jwide, jrow, jcol, val, jidx, c, i, j, row=0, col=0;
983 struct jhead jh;
984 int min=INT_MAX;
985 ushort *rp;
986
987 if (!ljpeg_start (&jh, 0)) return;
988 jwide = jh.wide * jh.clrs;
989
990 for (jrow=0; jrow < jh.high; jrow++) {
991 rp = ljpeg_row (jrow, &jh);
992 for (jcol=0; jcol < jwide; jcol++) {
993 val = *rp++;
994 if (jh.bits <= 12)
995 val = curve[val & 0xfff];
996 if (cr2_slice[0]) {
997 jidx = jrow*jwide + jcol;
998 i = jidx / (cr2_slice[1]*jh.high);
999 if ((j = i >= cr2_slice[0]))
1000 i = cr2_slice[0];
1001 jidx -= i * (cr2_slice[1]*jh.high);
1002 row = jidx / cr2_slice[1+j];
1003 col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
1004 }
1005 if (raw_width == 3984 && (col -= 2) < 0)
1006 col += (row--,raw_width);
1007 if ((unsigned) (row-top_margin) < height) {
1008 c = FC(row-top_margin,col-left_margin);
1009 if ((unsigned) (col-left_margin) < width) {
1010 BAYER(row-top_margin,col-left_margin) = val;
1011 if (min > val) min = val;
1012 } else if (col > 1 && (unsigned) (col-left_margin+2) >
1013 (unsigned) (width+3))
1014 cblack[c] += (cblack[4+c]++,val);
1015 }
1016 if (++col >= raw_width)
1017 col = (row++,0);
1018 }
1019 }
1020 ljpeg_end (&jh);
1021 FORC4 if (cblack[4+c]) cblack[c] /= cblack[4+c];
1022 if (!strcasecmp(make,"KODAK"))
1023 black = min;
1024 }
1025
canon_sraw_load_raw()1026 void CLASS canon_sraw_load_raw()
1027 {
1028 struct jhead jh;
1029 short *rp=0, (*ip)[4];
1030 int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
1031 int v[3]={0,0,0}, ver, hue;
1032 char *cp;
1033
1034 if (!ljpeg_start (&jh, 0)) return;
1035 jwide = (jh.wide >>= 1) * jh.clrs;
1036
1037 for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
1038 scol = ecol;
1039 ecol += cr2_slice[1] * 2 / jh.clrs;
1040 if (!cr2_slice[0] || ecol > raw_width-1) ecol = raw_width & -2;
1041 for (row=0; row < height; row += (jh.clrs >> 1) - 1) {
1042 ip = (short (*)[4]) image + row*width;
1043 for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
1044 if ((jcol %= jwide) == 0)
1045 rp = (short *) ljpeg_row (jrow++, &jh);
1046 if (col >= width) continue;
1047 FORC (jh.clrs-2)
1048 ip[col + (c >> 1)*width + (c & 1)][0] = rp[jcol+c];
1049 ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
1050 ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
1051 }
1052 }
1053 }
1054 for (cp=model2; *cp && !isdigit(*cp); cp++);
1055 sscanf (cp, "%d.%d.%d", v, v+1, v+2);
1056 ver = (v[0]*1000 + v[1])*1000 + v[2];
1057 hue = (jh.sraw+1) << 2;
1058 if (unique_id == 0x80000218 && ver > 1000006 && ver < 3000000)
1059 hue = jh.sraw << 1;
1060 ip = (short (*)[4]) image;
1061 rp = ip[0];
1062 for (row=0; row < height; row++, ip+=width) {
1063 if (row & (jh.sraw >> 1))
1064 for (col=0; col < width; col+=2)
1065 for (c=1; c < 3; c++)
1066 if (row == height-1)
1067 ip[col][c] = ip[col-width][c];
1068 else ip[col][c] = (ip[col-width][c] + ip[col+width][c] + 1) >> 1;
1069 for (col=1; col < width; col+=2)
1070 for (c=1; c < 3; c++)
1071 if (col == width-1)
1072 ip[col][c] = ip[col-1][c];
1073 else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
1074 }
1075 for ( ; rp < ip[0]; rp+=4) {
1076 if (unique_id < 0x80000218) {
1077 pix[0] = rp[0] + rp[2] - 512;
1078 pix[2] = rp[0] + rp[1] - 512;
1079 pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12) - 512;
1080 } else {
1081 rp[1] = (rp[1] << 2) + hue;
1082 rp[2] = (rp[2] << 2) + hue;
1083 pix[0] = rp[0] + (( 200*rp[1] + 22929*rp[2]) >> 14);
1084 pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
1085 pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
1086 }
1087 FORC3 rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
1088 }
1089 ljpeg_end (&jh);
1090 maximum = 0x3fff;
1091 }
1092
adobe_copy_pixel(int row,int col,ushort ** rp)1093 void CLASS adobe_copy_pixel (int row, int col, ushort **rp)
1094 {
1095 unsigned r, c;
1096
1097 r = row -= top_margin;
1098 c = col -= left_margin;
1099 if (is_raw == 2 && shot_select) (*rp)++;
1100 if (filters) {
1101 if (fuji_width) {
1102 r = row + fuji_width - 1 - (col >> 1);
1103 c = row + ((col+1) >> 1);
1104 }
1105 if (r < height && c < width)
1106 BAYER(r,c) = **rp < 0x1000 ? curve[**rp] : **rp;
1107 *rp += is_raw;
1108 } else {
1109 if (r < height && c < width)
1110 FORC(tiff_samples)
1111 image[row*width+col][c] = (*rp)[c] < 0x1000 ? curve[(*rp)[c]]:(*rp)[c];
1112 *rp += tiff_samples;
1113 }
1114 if (is_raw == 2 && shot_select) (*rp)--;
1115 }
1116
adobe_dng_load_raw_lj()1117 void CLASS adobe_dng_load_raw_lj()
1118 {
1119 unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col;
1120 struct jhead jh;
1121 ushort *rp;
1122
1123 while (trow < raw_height) {
1124 save = ftell(ifp);
1125 if (tile_length < INT_MAX)
1126 fseek (ifp, get4(), SEEK_SET);
1127 if (!ljpeg_start (&jh, 0)) break;
1128 jwide = jh.wide;
1129 if (filters) jwide *= jh.clrs;
1130 jwide /= is_raw;
1131 for (row=col=jrow=0; (int) jrow < jh.high; jrow++) {
1132 rp = ljpeg_row (jrow, &jh);
1133 for (jcol=0; jcol < jwide; jcol++) {
1134 adobe_copy_pixel (trow+row, tcol+col, &rp);
1135 if (++col >= tile_width || col >= raw_width)
1136 row += 1 + (col = 0);
1137 }
1138 }
1139 fseek (ifp, save+4, SEEK_SET);
1140 if ((tcol += tile_width) >= raw_width)
1141 trow += tile_length + (tcol = 0);
1142 ljpeg_end (&jh);
1143 }
1144 }
1145
adobe_dng_load_raw_nc()1146 void CLASS adobe_dng_load_raw_nc()
1147 {
1148 ushort *pixel, *rp;
1149 unsigned row, col;
1150
1151 pixel = (ushort *) calloc (raw_width * tiff_samples, sizeof *pixel);
1152 merror (pixel, "adobe_dng_load_raw_nc()");
1153 for (row=0; row < raw_height; row++) {
1154 if (tiff_bps == 16)
1155 read_shorts (pixel, raw_width * tiff_samples);
1156 else {
1157 getbits(-1);
1158 for (col=0; col < raw_width * tiff_samples; col++)
1159 pixel[col] = getbits(tiff_bps);
1160 }
1161 for (rp=pixel, col=0; col < raw_width; col++)
1162 adobe_copy_pixel (row, col, &rp);
1163 }
1164 free (pixel);
1165 }
1166
pentax_load_raw()1167 void CLASS pentax_load_raw()
1168 {
1169 ushort bit[2][15], huff[4097];
1170 int dep, row, col, diff, c, i;
1171 ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
1172
1173 fseek (ifp, meta_offset, SEEK_SET);
1174 dep = (get2() + 12) & 15;
1175 fseek (ifp, 12, SEEK_CUR);
1176 FORC(dep) bit[0][c] = get2();
1177 FORC(dep) bit[1][c] = fgetc(ifp);
1178 FORC(dep)
1179 for (i=bit[0][c]; i <= ((bit[0][c]+(4096 >> bit[1][c])-1) & 4095); )
1180 huff[++i] = bit[1][c] << 8 | c;
1181 huff[0] = 12;
1182 fseek (ifp, data_offset, SEEK_SET);
1183 getbits(-1);
1184 for (row=0; row < raw_height; row++)
1185 for (col=0; col < raw_width; col++) {
1186 diff = ljpeg_diff (huff);
1187 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
1188 else hpred[col & 1] += diff;
1189 if ((unsigned) (row-top_margin) < height &&
1190 (unsigned) (col-left_margin) < width)
1191 BAYER(row-top_margin,col-left_margin) = hpred[col & 1];
1192 if (hpred[col & 1] >> tiff_bps) derror();
1193 }
1194 }
1195
nikon_compressed_load_raw()1196 void CLASS nikon_compressed_load_raw()
1197 {
1198 static const uchar nikon_tree[][32] = {
1199 { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
1200 5,4,3,6,2,7,1,0,8,9,11,10,12 },
1201 { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
1202 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
1203 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
1204 5,4,6,3,7,2,8,1,9,0,10,11,12 },
1205 { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
1206 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
1207 { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
1208 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
1209 { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
1210 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
1211 ushort *huff, ver0, ver1, vpred[2][2], hpred[2], csize;
1212 int i, min, max, step=0, tree=0, split=0, row, col, len, shl, diff;
1213
1214 fseek (ifp, meta_offset, SEEK_SET);
1215 ver0 = fgetc(ifp);
1216 ver1 = fgetc(ifp);
1217 if (ver0 == 0x49 || ver1 == 0x58)
1218 fseek (ifp, 2110, SEEK_CUR);
1219 if (ver0 == 0x46) tree = 2;
1220 if (tiff_bps == 14) tree += 3;
1221 read_shorts (vpred[0], 4);
1222 max = 1 << tiff_bps & 0x7fff;
1223 if ((csize = get2()) > 1)
1224 step = max / (csize-1);
1225 if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
1226 for (i=0; i < csize; i++)
1227 curve[i*step] = get2();
1228 for (i=0; i < max; i++)
1229 curve[i] = ( curve[i-i%step]*(step-i%step) +
1230 curve[i-i%step+step]*(i%step) ) / step;
1231 fseek (ifp, meta_offset+562, SEEK_SET);
1232 split = get2();
1233 } else if (ver0 != 0x46 && csize <= 0x4001)
1234 read_shorts (curve, max=csize);
1235 while (curve[max-2] == curve[max-1]) max--;
1236 huff = make_decoder (nikon_tree[tree]);
1237 fseek (ifp, data_offset, SEEK_SET);
1238 getbits(-1);
1239 for (min=row=0; row < height; row++) {
1240 if (split && row == split) {
1241 free (huff);
1242 huff = make_decoder (nikon_tree[tree+1]);
1243 max += (min = 16) << 1;
1244 }
1245 for (col=0; col < raw_width; col++) {
1246 i = gethuff(huff);
1247 len = i & 15;
1248 shl = i >> 4;
1249 diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
1250 if ((diff & (1 << (len-1))) == 0)
1251 diff -= (1 << len) - !shl;
1252 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
1253 else hpred[col & 1] += diff;
1254 if ((ushort)(hpred[col & 1] + min) >= max) derror();
1255 if ((unsigned) (col-left_margin) < width)
1256 BAYER(row,col-left_margin) = curve[LIM((short)hpred[col & 1],0,0x3fff)];
1257 }
1258 }
1259 free (huff);
1260 }
1261
1262 /*
1263 Figure out if a NEF file is compressed. These fancy heuristics
1264 are only needed for the D100, thanks to a bug in some cameras
1265 that tags all images as "compressed".
1266 */
nikon_is_compressed()1267 int CLASS nikon_is_compressed()
1268 {
1269 uchar test[256];
1270 int i;
1271
1272 fseek (ifp, data_offset, SEEK_SET);
1273 fread (test, 1, 256, ifp);
1274 for (i=15; i < 256; i+=16)
1275 if (test[i]) return 1;
1276 return 0;
1277 }
1278
1279 /*
1280 Returns 1 for a Coolpix 995, 0 for anything else.
1281 */
nikon_e995()1282 int CLASS nikon_e995()
1283 {
1284 int i, histo[256];
1285 const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
1286
1287 memset (histo, 0, sizeof histo);
1288 fseek (ifp, -2000, SEEK_END);
1289 for (i=0; i < 2000; i++)
1290 histo[fgetc(ifp)]++;
1291 for (i=0; i < 4; i++)
1292 if (histo[often[i]] < 200)
1293 return 0;
1294 return 1;
1295 }
1296
1297 /*
1298 Returns 1 for a Coolpix 2100, 0 for anything else.
1299 */
nikon_e2100()1300 int CLASS nikon_e2100()
1301 {
1302 uchar t[12];
1303 int i;
1304
1305 fseek (ifp, 0, SEEK_SET);
1306 for (i=0; i < 1024; i++) {
1307 fread (t, 1, 12, ifp);
1308 if (((t[2] & t[4] & t[7] & t[9]) >> 4
1309 & t[1] & t[6] & t[8] & t[11] & 3) != 3)
1310 return 0;
1311 }
1312 return 1;
1313 }
1314
nikon_3700()1315 void CLASS nikon_3700()
1316 {
1317 int bits, i;
1318 uchar dp[24];
1319 static const struct {
1320 int bits;
1321 char make[12], model[15];
1322 } table[] = {
1323 { 0x00, "PENTAX", "Optio 33WR" },
1324 { 0x03, "NIKON", "E3200" },
1325 { 0x32, "NIKON", "E3700" },
1326 { 0x33, "OLYMPUS", "C740UZ" } };
1327
1328 fseek (ifp, 3072, SEEK_SET);
1329 fread (dp, 1, 24, ifp);
1330 bits = (dp[8] & 3) << 4 | (dp[20] & 3);
1331 for (i=0; i < (int) sizeof table / (int) sizeof *table; i++)
1332 if (bits == table[i].bits) {
1333 strcpy (make, table[i].make );
1334 strcpy (model, table[i].model);
1335 }
1336 }
1337
1338 /*
1339 Separates a Minolta DiMAGE Z2 from a Nikon E4300.
1340 */
minolta_z2()1341 int CLASS minolta_z2()
1342 {
1343 int i, nz;
1344 char tail[424];
1345
1346 fseek (ifp, -sizeof tail, SEEK_END);
1347 fread (tail, 1, sizeof tail, ifp);
1348 for (nz=i=0; i < (int) sizeof tail; i++)
1349 if (tail[i]) nz++;
1350 return nz > 20;
1351 }
1352
1353 /*
1354 The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
1355 */
fuji_load_raw()1356 void CLASS fuji_load_raw()
1357 {
1358 ushort *pixel;
1359 int wide, row, col, r, c;
1360
1361 fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
1362 wide = fuji_width << !fuji_layout;
1363 pixel = (ushort *) calloc (wide, sizeof *pixel);
1364 merror (pixel, "fuji_load_raw()");
1365 for (row=0; row < raw_height; row++) {
1366 read_shorts (pixel, wide);
1367 fseek (ifp, 2*(raw_width - wide), SEEK_CUR);
1368 for (col=0; col < wide; col++) {
1369 if (fuji_layout) {
1370 r = fuji_width - 1 - col + (row >> 1);
1371 c = col + ((row+1) >> 1);
1372 } else {
1373 r = fuji_width - 1 + row - (col >> 1);
1374 c = row + ((col+1) >> 1);
1375 }
1376 BAYER(r,c) = pixel[col];
1377 }
1378 }
1379 free (pixel);
1380 }
1381
1382 //void CLASS jpeg_thumb();
1383
ppm_thumb()1384 void CLASS ppm_thumb()
1385 {
1386 char *thumb;
1387 thumb_length = thumb_width*thumb_height*3;
1388 thumb = (char *) malloc (thumb_length);
1389 merror (thumb, "ppm_thumb()");
1390 fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1391 fread (thumb, 1, thumb_length, ifp);
1392 fwrite (thumb, 1, thumb_length, ofp);
1393 free (thumb);
1394 }
1395
layer_thumb()1396 void CLASS layer_thumb()
1397 {
1398 unsigned i;
1399 int c;
1400 char *thumb, map[][4] = { "012","102" };
1401
1402 colors = thumb_misc >> 5 & 7;
1403 thumb_length = thumb_width*thumb_height;
1404 thumb = (char *) calloc (colors, thumb_length);
1405 merror (thumb, "layer_thumb()");
1406 fprintf (ofp, "P%d\n%d %d\n255\n",
1407 5 + (colors >> 1), thumb_width, thumb_height);
1408 fread (thumb, thumb_length, colors, ifp);
1409 for (i=0; i < thumb_length; i++)
1410 FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], ofp);
1411 free (thumb);
1412 }
1413
rollei_thumb()1414 void CLASS rollei_thumb()
1415 {
1416 unsigned i;
1417 ushort *thumb;
1418
1419 thumb_length = thumb_width * thumb_height;
1420 thumb = (ushort *) calloc (thumb_length, 2);
1421 merror (thumb, "rollei_thumb()");
1422 fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1423 read_shorts (thumb, thumb_length);
1424 for (i=0; i < thumb_length; i++) {
1425 putc (thumb[i] << 3, ofp);
1426 putc (thumb[i] >> 5 << 2, ofp);
1427 putc (thumb[i] >> 11 << 3, ofp);
1428 }
1429 free (thumb);
1430 }
1431
rollei_load_raw()1432 void CLASS rollei_load_raw()
1433 {
1434 uchar pixel[10];
1435 unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
1436
1437 isix = raw_width * raw_height * 5 / 8;
1438 while (fread (pixel, 1, 10, ifp) == 10) {
1439 for (i=0; i < 10; i+=2) {
1440 todo[i] = iten++;
1441 todo[i+1] = pixel[i] << 8 | pixel[i+1];
1442 buffer = pixel[i] >> 2 | buffer << 6;
1443 }
1444 for ( ; i < 16; i+=2) {
1445 todo[i] = isix++;
1446 todo[i+1] = buffer >> (14-i)*5;
1447 }
1448 for (i=0; i < 16; i+=2) {
1449 row = todo[i] / raw_width - top_margin;
1450 col = todo[i] % raw_width - left_margin;
1451 if (row < height && col < width)
1452 BAYER(row,col) = (todo[i+1] & 0x3ff);
1453 }
1454 }
1455 maximum = 0x3ff;
1456 }
1457
bayer(unsigned row,unsigned col)1458 int CLASS bayer (unsigned row, unsigned col)
1459 {
1460 return (row < height && col < width) ? BAYER(row,col) : 0;
1461 }
1462
phase_one_flat_field(int is_float,int nc)1463 void CLASS phase_one_flat_field (int is_float, int nc)
1464 {
1465 ushort head[8];
1466 unsigned wide, y, x, rend, cend, row, col;
1467 int c;
1468 float *mrow, num, mult[4];
1469
1470 read_shorts (head, 8);
1471 wide = head[2] / head[4];
1472 mrow = (float *) calloc (nc*wide, sizeof *mrow);
1473 merror (mrow, "phase_one_flat_field()");
1474 for (y=0; y < head[3] / head[5]; y++) {
1475 for (x=0; x < wide; x++)
1476 for (c=0; c < nc; c+=2) {
1477 num = is_float ? getreal(11) : get2()/32768.0;
1478 if (y==0) mrow[c*wide+x] = num;
1479 else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
1480 }
1481 if (y==0) continue;
1482 rend = head[1]-top_margin + y*head[5];
1483 for (row = rend-head[5]; row < height && row < rend; row++) {
1484 for (x=1; x < wide; x++) {
1485 for (c=0; c < nc; c+=2) {
1486 mult[c] = mrow[c*wide+x-1];
1487 mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
1488 }
1489 cend = head[0]-left_margin + x*head[4];
1490 for (col = cend-head[4]; col < width && col < cend; col++) {
1491 c = nc > 2 ? FC(row,col) : 0;
1492 if (!(c & 1)) {
1493 c = BAYER(row,col) * mult[c];
1494 BAYER(row,col) = LIM(c,0,65535);
1495 }
1496 for (c=0; c < nc; c+=2)
1497 mult[c] += mult[c+1];
1498 }
1499 }
1500 for (x=0; x < wide; x++)
1501 for (c=0; c < nc; c+=2)
1502 mrow[c*wide+x] += mrow[(c+1)*wide+x];
1503 }
1504 }
1505 free (mrow);
1506 }
1507
phase_one_correct()1508 void CLASS phase_one_correct()
1509 {
1510 unsigned entries, tag, data, save, col, row, type;
1511 int len, i, j, k, cip, val[4], dev[4], sum, max;
1512 int head[9], diff, mindiff=INT_MAX, off_412=0;
1513 static const signed char dir[12][2] =
1514 { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
1515 {-2,-2}, {-2,2}, {2,-2}, {2,2} };
1516 float poly[8], num, cfrac, frac, mult[2], *yval[2];
1517 ushort *xval[2];
1518
1519 if (half_size || !meta_length) return;
1520 dcraw_message (DCRAW_VERBOSE,_("Phase One correction...\n"));
1521 fseek (ifp, meta_offset, SEEK_SET);
1522 order = get2();
1523 fseek (ifp, 6, SEEK_CUR);
1524 fseek (ifp, meta_offset+get4(), SEEK_SET);
1525 entries = get4(); get4();
1526 while (entries--) {
1527 tag = get4();
1528 len = get4();
1529 data = get4();
1530 save = ftell(ifp);
1531 fseek (ifp, meta_offset+data, SEEK_SET);
1532 if (tag == 0x419) { /* Polynomial curve */
1533 for (get4(), i=0; i < 8; i++)
1534 poly[i] = getreal(11);
1535 poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
1536 for (i=0; i < 0x10000; i++) {
1537 num = (poly[5]*i + poly[3])*i + poly[1];
1538 curve[i] = LIM(num,0,65535);
1539 } goto apply; /* apply to right half */
1540 } else if (tag == 0x41a) { /* Polynomial curve */
1541 for (i=0; i < 4; i++)
1542 poly[i] = getreal(11);
1543 for (i=0; i < 0x10000; i++) {
1544 for (num=0, j=4; j--; )
1545 num = num * i + poly[j];
1546 curve[i] = LIM(num+i,0,65535);
1547 } apply: /* apply to whole image */
1548 for (row=0; row < height; row++)
1549 for (col = (tag & 1)*ph1.split_col; col < width; col++)
1550 BAYER(row,col) = curve[BAYER(row,col)];
1551 } else if (tag == 0x400) { /* Sensor defects */
1552 while ((len -= 8) >= 0) {
1553 col = get2() - left_margin;
1554 row = get2() - top_margin;
1555 type = get2(); get2();
1556 if (col >= width) continue;
1557 if (type == 131) /* Bad column */
1558 for (row=0; row < height; row++)
1559 if (FC(row,col) == 1) {
1560 for (sum=i=0; i < 4; i++)
1561 sum += val[i] = bayer (row+dir[i][0], col+dir[i][1]);
1562 for (max=i=0; i < 4; i++) {
1563 dev[i] = abs((val[i] << 2) - sum);
1564 if (dev[max] < dev[i]) max = i;
1565 }
1566 BAYER(row,col) = (sum - val[max])/3.0 + 0.5;
1567 } else {
1568 for (sum=0, i=8; i < 12; i++)
1569 sum += bayer (row+dir[i][0], col+dir[i][1]);
1570 BAYER(row,col) = 0.5 + sum * 0.0732233 +
1571 (bayer(row,col-2) + bayer(row,col+2)) * 0.3535534;
1572 }
1573 else if (type == 129) { /* Bad pixel */
1574 if (row >= height) continue;
1575 j = (FC(row,col) != 1) * 4;
1576 for (sum=0, i=j; i < j+8; i++)
1577 sum += bayer (row+dir[i][0], col+dir[i][1]);
1578 BAYER(row,col) = (sum + 4) >> 3;
1579 }
1580 }
1581 } else if (tag == 0x401) { /* All-color flat fields */
1582 phase_one_flat_field (1, 2);
1583 } else if (tag == 0x416 || tag == 0x410) {
1584 phase_one_flat_field (0, 2);
1585 } else if (tag == 0x40b) { /* Red+blue flat field */
1586 phase_one_flat_field (0, 4);
1587 } else if (tag == 0x412) {
1588 fseek (ifp, 36, SEEK_CUR);
1589 diff = abs (get2() - ph1.tag_21a);
1590 if (mindiff > diff) {
1591 mindiff = diff;
1592 off_412 = ftell(ifp) - 38;
1593 }
1594 }
1595 fseek (ifp, save, SEEK_SET);
1596 }
1597 if (off_412) {
1598 fseek (ifp, off_412, SEEK_SET);
1599 for (i=0; i < 9; i++) head[i] = get4() & 0x7fff;
1600 yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
1601 merror (yval[0], "phase_one_correct()");
1602 yval[1] = (float *) (yval[0] + head[1]*head[3]);
1603 xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
1604 xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
1605 get2();
1606 for (i=0; i < 2; i++)
1607 for (j=0; j < head[i+1]*head[i+3]; j++)
1608 yval[i][j] = getreal(11);
1609 for (i=0; i < 2; i++)
1610 for (j=0; j < head[i+1]*head[i+3]; j++)
1611 xval[i][j] = get2();
1612 for (row=0; row < height; row++)
1613 for (col=0; col < width; col++) {
1614 cfrac = (float) col * head[3] / raw_width;
1615 cfrac -= cip = cfrac;
1616 num = BAYER(row,col) * 0.5;
1617 for (i=cip; i < cip+2; i++) {
1618 for (k=j=0; j < head[1]; j++)
1619 if (num < xval[0][k = head[1]*i+j]) break;
1620 frac = (j == 0 || j == head[1]) ? 0 :
1621 (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
1622 mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
1623 }
1624 i = ((mult[0] * (1-cfrac) + mult[1] * cfrac)
1625 * (row + top_margin) + num) * 2;
1626 BAYER(row,col) = LIM(i,0,65535);
1627 }
1628 free (yval[0]);
1629 }
1630 }
1631
phase_one_load_raw()1632 void CLASS phase_one_load_raw()
1633 {
1634 int row, col, a, b;
1635 ushort *pixel, akey, bkey, mask;
1636
1637 fseek (ifp, ph1.key_off, SEEK_SET);
1638 akey = get2();
1639 bkey = get2();
1640 mask = ph1.format == 1 ? 0x5555:0x1354;
1641 fseek (ifp, data_offset + top_margin*raw_width*2, SEEK_SET);
1642 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1643 merror (pixel, "phase_one_load_raw()");
1644 for (row=0; row < height; row++) {
1645 read_shorts (pixel, raw_width);
1646 for (col=0; col < raw_width; col+=2) {
1647 a = pixel[col+0] ^ akey;
1648 b = pixel[col+1] ^ bkey;
1649 pixel[col+0] = (a & mask) | (b & ~mask);
1650 pixel[col+1] = (b & mask) | (a & ~mask);
1651 }
1652 for (col=0; col < width; col++)
1653 BAYER(row,col) = pixel[col+left_margin];
1654 }
1655 free (pixel);
1656 phase_one_correct();
1657 }
1658
ph1_bithuff(int nbits,ushort * huff)1659 unsigned CLASS ph1_bithuff (int nbits, ushort *huff)
1660 {
1661 // TODO: The following static variables are not thread-safe
1662 static UINT64 bitbuf=0;
1663 static int vbits=0;
1664 unsigned c;
1665
1666 if (nbits == -1)
1667 return bitbuf = vbits = 0;
1668 if (nbits == 0) return 0;
1669 if (vbits < nbits) {
1670 bitbuf = bitbuf << 32 | get4();
1671 vbits += 32;
1672 }
1673 c = bitbuf << (64-vbits) >> (64-nbits);
1674 if (huff) {
1675 vbits -= huff[c] >> 8;
1676 return (uchar) huff[c];
1677 }
1678 vbits -= nbits;
1679 return c;
1680 }
1681 #define ph1_bits(n) ph1_bithuff(n,0)
1682 #define ph1_huff(h) ph1_bithuff(*h,h+1)
1683
phase_one_load_raw_c()1684 void CLASS phase_one_load_raw_c()
1685 {
1686 static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
1687 int *offset, len[2], pred[2], row, col, i, j;
1688 ushort *pixel;
1689 short (*black)[2];
1690
1691 pixel = (ushort *) calloc (raw_width + raw_height*4, 2);
1692 merror (pixel, "phase_one_load_raw_c()");
1693 offset = (int *) (pixel + raw_width);
1694 fseek (ifp, strip_offset, SEEK_SET);
1695 for (row=0; row < raw_height; row++)
1696 offset[row] = get4();
1697 black = (short (*)[2]) offset + raw_height;
1698 fseek (ifp, ph1.black_off, SEEK_SET);
1699 if (ph1.black_off)
1700 read_shorts ((ushort *) black[0], raw_height*2);
1701 for (i=0; i < 256; i++)
1702 curve[i] = i*i / 3.969 + 0.5;
1703 for (row=0; row < raw_height; row++) {
1704 fseek (ifp, data_offset + offset[row], SEEK_SET);
1705 ph1_bits(-1);
1706 pred[0] = pred[1] = 0;
1707 for (col=0; col < raw_width; col++) {
1708 if (col >= (raw_width & -8))
1709 len[0] = len[1] = 14;
1710 else if ((col & 7) == 0)
1711 for (i=0; i < 2; i++) {
1712 for (j=0; j < 5 && !ph1_bits(1); j++);
1713 if (j--) len[i] = length[j*2 + ph1_bits(1)];
1714 }
1715 if ((i = len[col & 1]) == 14)
1716 pixel[col] = pred[col & 1] = ph1_bits(16);
1717 else
1718 pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
1719 if (pred[col & 1] >> 16) derror();
1720 if (ph1.format == 5 && pixel[col] < 256)
1721 pixel[col] = curve[pixel[col]];
1722 }
1723 if ((unsigned) (row-top_margin) < height)
1724 for (col=0; col < width; col++) {
1725 i = (pixel[col+left_margin] << 2)
1726 - ph1.black + black[row][col >= ph1.split_col];
1727 if (i > 0) BAYER(row-top_margin,col) = i;
1728 }
1729 }
1730 free (pixel);
1731 phase_one_correct();
1732 maximum = 0xfffc - ph1.black;
1733 }
1734
hasselblad_load_raw()1735 void CLASS hasselblad_load_raw()
1736 {
1737 struct jhead jh;
1738 int row, col, pred[2], len[2], diff, c;
1739
1740 if (!ljpeg_start (&jh, 0)) return;
1741 order = 0x4949;
1742 ph1_bits(-1);
1743 for (row=-top_margin; row < height; row++) {
1744 pred[0] = pred[1] = 0x8000;
1745 for (col=-left_margin; col < raw_width-left_margin; col+=2) {
1746 FORC(2) len[c] = ph1_huff(jh.huff[0]);
1747 FORC(2) {
1748 diff = ph1_bits(len[c]);
1749 if ((diff & (1 << (len[c]-1))) == 0)
1750 diff -= (1 << len[c]) - 1;
1751 if (diff == 65535) diff = -32768;
1752 pred[c] += diff;
1753 if (row >= 0 && (unsigned)(col+c) < width)
1754 BAYER(row,col+c) = pred[c];
1755 }
1756 }
1757 }
1758 ljpeg_end (&jh);
1759 maximum = 0xffff;
1760 }
1761
leaf_hdr_load_raw()1762 void CLASS leaf_hdr_load_raw()
1763 {
1764 ushort *pixel;
1765 unsigned tile=0, r, c, row, col;
1766
1767 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1768 merror (pixel, "leaf_hdr_load_raw()");
1769 FORC(tiff_samples)
1770 for (r=0; r < raw_height; r++) {
1771 if (r % tile_length == 0) {
1772 fseek (ifp, data_offset + 4*tile++, SEEK_SET);
1773 fseek (ifp, get4() + 2*left_margin, SEEK_SET);
1774 }
1775 if (filters && c != shot_select) continue;
1776 read_shorts (pixel, raw_width);
1777 if ((row = r - top_margin) >= height) continue;
1778 for (col=0; col < width; col++)
1779 if (filters) BAYER(row,col) = pixel[col];
1780 else image[row*width+col][c] = pixel[col];
1781 }
1782 free (pixel);
1783 if (!filters) {
1784 maximum = 0xffff;
1785 raw_color = 1;
1786 }
1787 }
1788
1789 //void CLASS unpacked_load_raw();
1790
sinar_4shot_load_raw()1791 void CLASS sinar_4shot_load_raw()
1792 {
1793 ushort *pixel;
1794 unsigned shot, row, col, r, c;
1795
1796 if ((shot = shot_select) || half_size) {
1797 if (shot) shot--;
1798 if (shot > 3) shot = 3;
1799 fseek (ifp, data_offset + shot*4, SEEK_SET);
1800 fseek (ifp, get4(), SEEK_SET);
1801 unpacked_load_raw();
1802 return;
1803 }
1804 free (image);
1805 image = (ushort (*)[4])
1806 calloc ((iheight=height)*(iwidth=width), sizeof *image);
1807 merror (image, "sinar_4shot_load_raw()");
1808 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1809 merror (pixel, "sinar_4shot_load_raw()");
1810 for (shot=0; shot < 4; shot++) {
1811 fseek (ifp, data_offset + shot*4, SEEK_SET);
1812 fseek (ifp, get4(), SEEK_SET);
1813 for (row=0; row < raw_height; row++) {
1814 read_shorts (pixel, raw_width);
1815 if ((r = row-top_margin - (shot >> 1 & 1)) >= height) continue;
1816 for (col=0; col < raw_width; col++) {
1817 if ((c = col-left_margin - (shot & 1)) >= width) continue;
1818 image[r*width+c][FC(row,col)] = pixel[col];
1819 }
1820 }
1821 }
1822 free (pixel);
1823 shrink = filters = 0;
1824 }
1825
imacon_full_load_raw()1826 void CLASS imacon_full_load_raw()
1827 {
1828 int row, col;
1829
1830 for (row=0; row < height; row++)
1831 for (col=0; col < width; col++)
1832 read_shorts (image[row*width+col], 3);
1833 }
1834
packed_load_raw()1835 void CLASS packed_load_raw()
1836 {
1837 int vbits=0, bwide, pwide, rbits, bite, half, irow, row, col, val, i;
1838 UINT64 bitbuf=0;
1839
1840 if ((ushort)(raw_width * 8) >= width * tiff_bps) /* Is raw_width in bytes? */
1841 pwide = (bwide = raw_width) * 8 / tiff_bps;
1842 else bwide = (pwide = raw_width) * tiff_bps / 8;
1843 rbits = bwide * 8 - pwide * tiff_bps;
1844 if (load_flags & 1) bwide = bwide * 16 / 15;
1845 fseek (ifp, top_margin*bwide, SEEK_CUR);
1846 bite = 8 + (load_flags & 24);
1847 half = (height+1) >> 1;
1848 for (irow=0; irow < height; irow++) {
1849 row = irow;
1850 if (load_flags & 2 &&
1851 (row = irow % half * 2 + irow / half) == 1 &&
1852 load_flags & 4) {
1853 if (vbits=0, tiff_compress)
1854 fseek (ifp, data_offset - (-half*bwide & -2048), SEEK_SET);
1855 else {
1856 fseek (ifp, 0, SEEK_END);
1857 fseek (ifp, ftell(ifp) >> 3 << 2, SEEK_SET);
1858 }
1859 }
1860 for (col=0; col < pwide; col++) {
1861 for (vbits -= tiff_bps; vbits < 0; vbits += bite) {
1862 bitbuf <<= bite;
1863 for (i=0; i < bite; i+=8)
1864 bitbuf |= (unsigned) (fgetc(ifp) << i);
1865 }
1866 val = bitbuf << (64-tiff_bps-vbits) >> (64-tiff_bps);
1867 i = (col ^ (bite == 24)) - left_margin;
1868 if ((unsigned) i < width)
1869 BAYER(row,i) = val;
1870 else if (load_flags & 32)
1871 black += val;
1872 if (load_flags & 1 && (col % 10) == 9 &&
1873 fgetc(ifp) && col < width+left_margin) derror();
1874 }
1875 vbits -= rbits;
1876 }
1877 if (load_flags & 32 && pwide > width)
1878 black /= (pwide - width) * height;
1879 }
1880
unpacked_load_raw()1881 void CLASS unpacked_load_raw()
1882 {
1883 ushort *pixel;
1884 int row, col, bits=0;
1885
1886 while (1 << ++bits < (int) maximum);
1887 fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
1888 pixel = (ushort *) calloc (width, sizeof *pixel);
1889 merror (pixel, "unpacked_load_raw()");
1890 for (row=0; row < height; row++) {
1891 read_shorts (pixel, width);
1892 fseek (ifp, 2*(raw_width - width), SEEK_CUR);
1893 for (col=0; col < width; col++)
1894 if ((BAYER2(row,col) = pixel[col] >> load_flags) >> bits) derror();
1895 }
1896 free (pixel);
1897 }
1898
nokia_load_raw()1899 void CLASS nokia_load_raw()
1900 {
1901 uchar *data, *dp;
1902 ushort *pixel, *pix;
1903 int rev, dwide, row, c;
1904
1905 rev = 3 * (order == 0x4949);
1906 dwide = raw_width * 5 / 4;
1907 data = (uchar *) malloc (dwide + raw_width*2);
1908 merror (data, "nokia_load_raw()");
1909 pixel = (ushort *) (data + dwide);
1910 for (row=0; row < raw_height; row++) {
1911 if ((int) fread (data+dwide, 1, dwide, ifp) < dwide) derror();
1912 FORC(dwide) data[c] = data[dwide+(c ^ rev)];
1913 for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=5, pix+=4)
1914 FORC4 pix[c] = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
1915 if (row < top_margin)
1916 FORC(width) black += pixel[c];
1917 else
1918 FORC(width) BAYER(row-top_margin,c) = pixel[c];
1919 }
1920 free (data);
1921 if (top_margin) black /= top_margin * width;
1922 maximum = 0x3ff;
1923 }
1924
pana_bits(int nbits)1925 unsigned CLASS pana_bits (int nbits)
1926 {
1927 static uchar buf[0x4000];
1928 static int vbits;
1929 int byte;
1930
1931 if (!nbits) return vbits=0;
1932 if (!vbits) {
1933 fread (buf+load_flags, 1, 0x4000-load_flags, ifp);
1934 fread (buf, 1, load_flags, ifp);
1935 }
1936 vbits = (vbits - nbits) & 0x1ffff;
1937 byte = vbits >> 3 ^ 0x3ff0;
1938 return (buf[byte] | buf[byte+1] << 8) >> (vbits & 7) & ~(-1 << nbits);
1939 }
1940
panasonic_load_raw()1941 void CLASS panasonic_load_raw()
1942 {
1943 int row, col, i, j, sh=0, pred[2], nonz[2];
1944
1945 pana_bits(0);
1946 for (row=0; row < height; row++)
1947 for (col=0; col < raw_width; col++) {
1948 if ((i = col % 14) == 0)
1949 pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
1950 if (i % 3 == 2) sh = 4 >> (3 - pana_bits(2));
1951 if (nonz[i & 1]) {
1952 if ((j = pana_bits(8))) {
1953 if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
1954 pred[i & 1] &= ~(-1 << sh);
1955 pred[i & 1] += j << sh;
1956 }
1957 } else if ((nonz[i & 1] = pana_bits(8)) || i > 11)
1958 pred[i & 1] = nonz[i & 1] << 4 | pana_bits(4);
1959 if (col < width)
1960 if ((BAYER(row,col) = pred[col & 1]) > 4098) derror();
1961 }
1962 }
1963
olympus_load_raw()1964 void CLASS olympus_load_raw()
1965 {
1966 ushort huff[4096];
1967 int row, col, nbits, sign, low, high, i, c, w, n, nw;
1968 int acarry[2][3], *carry, pred, diff;
1969
1970 huff[n=0] = 0xc0c;
1971 for (i=12; i--; )
1972 FORC(2048 >> i) huff[++n] = (i+1) << 8 | i;
1973 fseek (ifp, 7, SEEK_CUR);
1974 getbits(-1);
1975 for (row=0; row < height; row++) {
1976 memset (acarry, 0, sizeof acarry);
1977 for (col=0; col < raw_width; col++) {
1978 carry = acarry[col & 1];
1979 i = 2 * (carry[2] < 3);
1980 for (nbits=2+i; (ushort) carry[0] >> (nbits+i); nbits++);
1981 low = (sign = getbits(3)) & 3;
1982 sign = sign << 29 >> 31;
1983 if ((high = getbithuff(12,huff)) == 12)
1984 high = getbits(16-nbits) >> 1;
1985 carry[0] = (high << nbits) | getbits(nbits);
1986 diff = (carry[0] ^ sign) + carry[1];
1987 carry[1] = (diff*3 + carry[1]) >> 5;
1988 carry[2] = carry[0] > 16 ? 0 : carry[2]+1;
1989 if (col >= width) continue;
1990 if (row < 2 && col < 2) pred = 0;
1991 else if (row < 2) pred = BAYER(row,col-2);
1992 else if (col < 2) pred = BAYER(row-2,col);
1993 else {
1994 w = BAYER(row,col-2);
1995 n = BAYER(row-2,col);
1996 nw = BAYER(row-2,col-2);
1997 if ((w < nw && nw < n) || (n < nw && nw < w)) {
1998 if (ABS(w-nw) > 32 || ABS(n-nw) > 32)
1999 pred = w + n - nw;
2000 else pred = (w + n) >> 1;
2001 } else pred = ABS(w-nw) > ABS(n-nw) ? w : n;
2002 }
2003 if ((BAYER(row,col) = pred + ((diff << 2) | low)) >> 12) derror();
2004 }
2005 }
2006 }
2007
minolta_rd175_load_raw()2008 void CLASS minolta_rd175_load_raw()
2009 {
2010 uchar pixel[768];
2011 unsigned irow, box, row, col;
2012
2013 for (irow=0; irow < 1481; irow++) {
2014 if (fread (pixel, 1, 768, ifp) < 768) derror();
2015 box = irow / 82;
2016 row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
2017 switch (irow) {
2018 case 1477: case 1479: continue;
2019 case 1476: row = 984; break;
2020 case 1480: row = 985; break;
2021 case 1478: row = 985; box = 1;
2022 }
2023 if ((box < 12) && (box & 1)) {
2024 for (col=0; col < 1533; col++, row ^= 1)
2025 if (col != 1) BAYER(row,col) = (col+1) & 2 ?
2026 pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
2027 BAYER(row,1) = pixel[1] << 1;
2028 BAYER(row,1533) = pixel[765] << 1;
2029 } else
2030 for (col=row & 1; col < 1534; col+=2)
2031 BAYER(row,col) = pixel[col/2] << 1;
2032 }
2033 maximum = 0xff << 1;
2034 }
2035
quicktake_100_load_raw()2036 void CLASS quicktake_100_load_raw()
2037 {
2038 uchar pixel[484][644];
2039 static const short gstep[16] =
2040 { -89,-60,-44,-32,-22,-15,-8,-2,2,8,15,22,32,44,60,89 };
2041 static const short rstep[6][4] =
2042 { { -3,-1,1,3 }, { -5,-1,1,5 }, { -8,-2,2,8 },
2043 { -13,-3,3,13 }, { -19,-4,4,19 }, { -28,-6,6,28 } };
2044 static const short curve[256] =
2045 { 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,
2046 28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,53,
2047 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,
2048 79,80,81,82,83,84,86,88,90,92,94,97,99,101,103,105,107,110,112,114,116,
2049 118,120,123,125,127,129,131,134,136,138,140,142,144,147,149,151,153,155,
2050 158,160,162,164,166,168,171,173,175,177,179,181,184,186,188,190,192,195,
2051 197,199,201,203,205,208,210,212,214,216,218,221,223,226,230,235,239,244,
2052 248,252,257,261,265,270,274,278,283,287,291,296,300,305,309,313,318,322,
2053 326,331,335,339,344,348,352,357,361,365,370,374,379,383,387,392,396,400,
2054 405,409,413,418,422,426,431,435,440,444,448,453,457,461,466,470,474,479,
2055 483,487,492,496,500,508,519,531,542,553,564,575,587,598,609,620,631,643,
2056 654,665,676,687,698,710,721,732,743,754,766,777,788,799,810,822,833,844,
2057 855,866,878,889,900,911,922,933,945,956,967,978,989,1001,1012,1023 };
2058 int rb, row, col, sharp, val=0;
2059
2060 getbits(-1);
2061 memset (pixel, 0x80, sizeof pixel);
2062 for (row=2; row < height+2; row++) {
2063 for (col=2+(row & 1); col < width+2; col+=2) {
2064 val = ((pixel[row-1][col-1] + 2*pixel[row-1][col+1] +
2065 pixel[row][col-2]) >> 2) + gstep[getbits(4)];
2066 pixel[row][col] = val = LIM(val,0,255);
2067 if (col < 4)
2068 pixel[row][col-2] = pixel[row+1][~row & 1] = val;
2069 if (row == 2)
2070 pixel[row-1][col+1] = pixel[row-1][col+3] = val;
2071 }
2072 pixel[row][col] = val;
2073 }
2074 for (rb=0; rb < 2; rb++)
2075 for (row=2+rb; row < height+2; row+=2)
2076 for (col=3-(row & 1); col < width+2; col+=2) {
2077 if (row < 4 || col < 4) sharp = 2;
2078 else {
2079 val = ABS(pixel[row-2][col] - pixel[row][col-2])
2080 + ABS(pixel[row-2][col] - pixel[row-2][col-2])
2081 + ABS(pixel[row][col-2] - pixel[row-2][col-2]);
2082 sharp = val < 4 ? 0 : val < 8 ? 1 : val < 16 ? 2 :
2083 val < 32 ? 3 : val < 48 ? 4 : 5;
2084 }
2085 val = ((pixel[row-2][col] + pixel[row][col-2]) >> 1)
2086 + rstep[sharp][getbits(2)];
2087 pixel[row][col] = val = LIM(val,0,255);
2088 if (row < 4) pixel[row-2][col+2] = val;
2089 if (col < 4) pixel[row+2][col-2] = val;
2090 }
2091 for (row=2; row < height+2; row++)
2092 for (col=3-(row & 1); col < width+2; col+=2) {
2093 val = ((pixel[row][col-1] + (pixel[row][col] << 2) +
2094 pixel[row][col+1]) >> 1) - 0x100;
2095 pixel[row][col] = LIM(val,0,255);
2096 }
2097 for (row=0; row < height; row++)
2098 for (col=0; col < width; col++)
2099 BAYER(row,col) = curve[pixel[row+2][col+2]];
2100 maximum = 0x3ff;
2101 }
2102
2103 #define radc_token(tree) ((signed char) getbithuff(8,huff[tree]))
2104
2105 #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
2106
2107 #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
2108 : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
2109
kodak_radc_load_raw()2110 void CLASS kodak_radc_load_raw()
2111 {
2112 static const char src[] = {
2113 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
2114 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
2115 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
2116 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
2117 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
2118 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
2119 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
2120 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
2121 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
2122 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
2123 1,0, 2,2, 2,-2,
2124 1,-3, 1,3,
2125 2,-17, 2,-5, 2,5, 2,17,
2126 2,-7, 2,2, 2,9, 2,18,
2127 2,-18, 2,-9, 2,-2, 2,7,
2128 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
2129 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
2130 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
2131 };
2132 ushort huff[19][256];
2133 int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
2134 short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
2135 static const ushort pt[] =
2136 { 0,0, 1280,1344, 2320,3616, 3328,8000, 4095,16383, 65535,16383 };
2137
2138 for (i=2; i < 12; i+=2)
2139 for (c=pt[i-2]; c <= pt[i]; c++)
2140 curve[c] = (float)
2141 (c-pt[i-2]) / (pt[i]-pt[i-2]) * (pt[i+1]-pt[i-1]) + pt[i-1] + 0.5;
2142 for (s=i=0; i < (int) sizeof src; i+=2)
2143 FORC(256 >> src[i])
2144 huff[0][s++] = src[i] << 8 | (uchar) src[i+1];
2145 s = kodak_cbpp == 243 ? 2 : 3;
2146 FORC(256) huff[18][c] = (8-s) << 8 | c >> s << s | 1 << (s-1);
2147 getbits(-1);
2148 for (i=0; i < (int) sizeof(buf)/(int) sizeof(short); i++)
2149 buf[0][0][i] = 2048;
2150 for (row=0; row < height; row+=4) {
2151 FORC3 mul[c] = getbits(6);
2152 FORC3 {
2153 val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
2154 s = val > 65564 ? 10:12;
2155 x = ~(-1 << (s-1));
2156 val <<= 12-s;
2157 for (i=0; i < (int) sizeof(buf[0])/(int) sizeof(short); i++)
2158 buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
2159 last[c] = mul[c];
2160 for (r=0; r <= !c; r++) {
2161 buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
2162 for (tree=1, col=width/2; col > 0; ) {
2163 if ((tree = radc_token(tree))) {
2164 col -= 2;
2165 if (tree == 8)
2166 FORYX buf[c][y][x] = (uchar) radc_token(18) * mul[c];
2167 else
2168 FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
2169 } else
2170 do {
2171 nreps = (col > 2) ? radc_token(9) + 1 : 1;
2172 for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
2173 col -= 2;
2174 FORYX buf[c][y][x] = PREDICTOR;
2175 if (rep & 1) {
2176 step = radc_token(10) << 4;
2177 FORYX buf[c][y][x] += step;
2178 }
2179 }
2180 } while (nreps == 9);
2181 }
2182 for (y=0; y < 2; y++)
2183 for (x=0; x < width/2; x++) {
2184 val = (buf[c][y+1][x] << 4) / mul[c];
2185 if (val < 0) val = 0;
2186 if (c) BAYER(row+y*2+c-1,x*2+2-c) = val;
2187 else BAYER(row+r*2+y,x*2+y) = val;
2188 }
2189 memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
2190 }
2191 }
2192 for (y=row; y < row+4; y++)
2193 for (x=0; x < width; x++)
2194 if ((x+y) & 1) {
2195 r = x ? x-1 : x+1;
2196 s = x+1 < width ? x+1 : x-1;
2197 val = (BAYER(y,x)-2048)*2 + (BAYER(y,r)+BAYER(y,s))/2;
2198 if (val < 0) val = 0;
2199 BAYER(y,x) = val;
2200 }
2201 }
2202 for (i=0; i < iheight*iwidth*4; i++)
2203 image[0][i] = curve[image[0][i]];
2204 maximum = 0x3fff;
2205 }
2206
2207 #undef FORYX
2208 #undef PREDICTOR
2209
2210 #ifndef HAVE_LIBJPEG
kodak_jpeg_load_raw()2211 void CLASS kodak_jpeg_load_raw() {}
2212 #else
2213
2214 METHODDEF(boolean)
fill_input_buffer(j_decompress_ptr cinfo)2215 fill_input_buffer (j_decompress_ptr cinfo)
2216 {
2217 // TODO: The following static variable is not thread-safe
2218 static uchar jpeg_buffer[4096];
2219 size_t nbytes;
2220 DCRaw *d = (DCRaw*)cinfo->client_data;
2221
2222 nbytes = fread (jpeg_buffer, 1, 4096, d->ifp);
2223 swab ((const char *)jpeg_buffer, (char *)jpeg_buffer, nbytes); /*mingw UF*/
2224 cinfo->src->next_input_byte = jpeg_buffer;
2225 cinfo->src->bytes_in_buffer = nbytes;
2226 return TRUE;
2227 }
2228
kodak_jpeg_load_raw()2229 void CLASS kodak_jpeg_load_raw()
2230 {
2231 struct jpeg_decompress_struct cinfo;
2232 struct jpeg_error_mgr jerr;
2233 JSAMPARRAY buf;
2234 JSAMPLE (*pixel)[3];
2235 int row, col;
2236
2237 cinfo.err = jpeg_std_error (&jerr);
2238 cinfo.client_data = this;
2239 jpeg_create_decompress (&cinfo);
2240 jpeg_stdio_src (&cinfo, ifp);
2241 cinfo.src->fill_input_buffer = fill_input_buffer;
2242 jpeg_read_header (&cinfo, TRUE);
2243 jpeg_start_decompress (&cinfo);
2244 if ((cinfo.output_width != width ) ||
2245 (cinfo.output_height*2 != height ) ||
2246 (cinfo.output_components != 3 )) {
2247 dcraw_message (DCRAW_ERROR,_("%s: incorrect JPEG dimensions\n"), ifname_display);
2248 jpeg_destroy_decompress (&cinfo);
2249 longjmp (failure, 3);
2250 }
2251 buf = (*cinfo.mem->alloc_sarray)
2252 ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
2253
2254 while (cinfo.output_scanline < cinfo.output_height) {
2255 row = cinfo.output_scanline * 2;
2256 jpeg_read_scanlines (&cinfo, buf, 1);
2257 pixel = (JSAMPLE (*)[3]) buf[0];
2258 for (col=0; col < width; col+=2) {
2259 BAYER(row+0,col+0) = pixel[col+0][1] << 1;
2260 BAYER(row+1,col+1) = pixel[col+1][1] << 1;
2261 BAYER(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
2262 BAYER(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
2263 }
2264 }
2265 jpeg_finish_decompress (&cinfo);
2266 jpeg_destroy_decompress (&cinfo);
2267 maximum = 0xff << 1;
2268 }
2269 #endif
2270
kodak_dc120_load_raw()2271 void CLASS kodak_dc120_load_raw()
2272 {
2273 static const int mul[4] = { 162, 192, 187, 92 };
2274 static const int add[4] = { 0, 636, 424, 212 };
2275 uchar pixel[848];
2276 int row, shift, col;
2277
2278 for (row=0; row < height; row++) {
2279 if (fread (pixel, 1, 848, ifp) < 848) derror();
2280 shift = row * mul[row & 3] + add[row & 3];
2281 for (col=0; col < width; col++)
2282 BAYER(row,col) = (ushort) pixel[(col + shift) % 848];
2283 }
2284 maximum = 0xff;
2285 }
2286
eight_bit_load_raw()2287 void CLASS eight_bit_load_raw()
2288 {
2289 uchar *pixel;
2290 unsigned row, col, val, lblack=0;
2291
2292 pixel = (uchar *) calloc (raw_width, sizeof *pixel);
2293 merror (pixel, "eight_bit_load_raw()");
2294 fseek (ifp, top_margin*raw_width, SEEK_CUR);
2295 for (row=0; row < height; row++) {
2296 if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
2297 for (col=0; col < raw_width; col++) {
2298 val = curve[pixel[col]];
2299 if ((unsigned) (col-left_margin) < width)
2300 BAYER(row,col-left_margin) = val;
2301 else lblack += val;
2302 }
2303 }
2304 free (pixel);
2305 if (raw_width > width+1)
2306 black = lblack / ((raw_width - width) * height);
2307 if (!strncmp(model,"DC2",3))
2308 black = 0;
2309 maximum = curve[0xff];
2310 }
2311
kodak_yrgb_load_raw()2312 void CLASS kodak_yrgb_load_raw()
2313 {
2314 uchar *pixel;
2315 int row, col, y, cb, cr, rgb[3], c;
2316
2317 pixel = (uchar *) calloc (raw_width, 3*sizeof *pixel);
2318 merror (pixel, "kodak_yrgb_load_raw()");
2319 for (row=0; row < height; row++) {
2320 if (~row & 1)
2321 if (fread (pixel, raw_width, 3, ifp) < 3) derror();
2322 for (col=0; col < raw_width; col++) {
2323 y = pixel[width*2*(row & 1) + col];
2324 cb = pixel[width + (col & -2)] - 128;
2325 cr = pixel[width + (col & -2)+1] - 128;
2326 rgb[1] = y-((cb + cr + 2) >> 2);
2327 rgb[2] = rgb[1] + cb;
2328 rgb[0] = rgb[1] + cr;
2329 FORC3 image[row*width+col][c] = curve[LIM(rgb[c],0,255)];
2330 }
2331 }
2332 free (pixel);
2333 maximum = curve[0xff];
2334 }
2335
kodak_262_load_raw()2336 void CLASS kodak_262_load_raw()
2337 {
2338 static const uchar kodak_tree[2][26] =
2339 { { 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 },
2340 { 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 } };
2341 ushort *huff[2];
2342 uchar *pixel;
2343 int *strip, ns, c, row, col, chess, pi=0, pi1, pi2, pred, val;
2344
2345 FORC(2) huff[c] = make_decoder (kodak_tree[c]);
2346 ns = (raw_height+63) >> 5;
2347 pixel = (uchar *) malloc (raw_width*32 + ns*4);
2348 merror (pixel, "kodak_262_load_raw()");
2349 strip = (int *) (pixel + raw_width*32);
2350 order = 0x4d4d;
2351 FORC(ns) strip[c] = get4();
2352 for (row=0; row < raw_height; row++) {
2353 if ((row & 31) == 0) {
2354 fseek (ifp, strip[row >> 5], SEEK_SET);
2355 getbits(-1);
2356 pi = 0;
2357 }
2358 for (col=0; col < raw_width; col++) {
2359 chess = (row + col) & 1;
2360 pi1 = chess ? pi-2 : pi-raw_width-1;
2361 pi2 = chess ? pi-2*raw_width : pi-raw_width+1;
2362 if (col <= chess) pi1 = -1;
2363 if (pi1 < 0) pi1 = pi2;
2364 if (pi2 < 0) pi2 = pi1;
2365 if (pi1 < 0 && col > 1) pi1 = pi2 = pi-2;
2366 pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
2367 pixel[pi] = val = pred + ljpeg_diff (huff[chess]);
2368 if (val >> 8) derror();
2369 val = curve[pixel[pi++]];
2370 if ((unsigned) (col-left_margin) < width)
2371 BAYER(row,col-left_margin) = val;
2372 else black += val;
2373 }
2374 }
2375 free (pixel);
2376 FORC(2) free (huff[c]);
2377 if (raw_width > width)
2378 black /= (raw_width - width) * height;
2379 }
2380
kodak_65000_decode(short * out,int bsize)2381 int CLASS kodak_65000_decode (short *out, int bsize)
2382 {
2383 uchar c, blen[768];
2384 ushort raw[6];
2385 INT64 bitbuf=0;
2386 int save, bits=0, i, j, len, diff;
2387
2388 save = ftell(ifp);
2389 bsize = (bsize + 3) & -4;
2390 for (i=0; i < bsize; i+=2) {
2391 c = fgetc(ifp);
2392 if ((blen[i ] = c & 15) > 12 ||
2393 (blen[i+1] = c >> 4) > 12 ) {
2394 fseek (ifp, save, SEEK_SET);
2395 for (i=0; i < bsize; i+=8) {
2396 read_shorts (raw, 6);
2397 out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
2398 out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
2399 for (j=0; j < 6; j++)
2400 out[i+2+j] = raw[j] & 0xfff;
2401 }
2402 return 1;
2403 }
2404 }
2405 if ((bsize & 7) == 4) {
2406 bitbuf = fgetc(ifp) << 8;
2407 bitbuf += fgetc(ifp);
2408 bits = 16;
2409 }
2410 for (i=0; i < bsize; i++) {
2411 len = blen[i];
2412 if (bits < len) {
2413 for (j=0; j < 32; j+=8)
2414 bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
2415 bits += 32;
2416 }
2417 diff = bitbuf & (0xffff >> (16-len));
2418 bitbuf >>= len;
2419 bits -= len;
2420 if ((diff & (1 << (len-1))) == 0)
2421 diff -= (1 << len) - 1;
2422 out[i] = diff;
2423 }
2424 return 0;
2425 }
2426
kodak_65000_load_raw()2427 void CLASS kodak_65000_load_raw()
2428 {
2429 short buf[256];
2430 int row, col, len, pred[2], ret, i;
2431
2432 for (row=0; row < height; row++)
2433 for (col=0; col < width; col+=256) {
2434 pred[0] = pred[1] = 0;
2435 len = MIN (256, width-col);
2436 ret = kodak_65000_decode (buf, len);
2437 for (i=0; i < len; i++)
2438 if ((BAYER(row,col+i) = curve[ret ? buf[i] :
2439 (pred[i & 1] += buf[i])]) >> 12) derror();
2440 }
2441 }
2442
kodak_ycbcr_load_raw()2443 void CLASS kodak_ycbcr_load_raw()
2444 {
2445 short buf[384], *bp;
2446 int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
2447 ushort *ip;
2448
2449 for (row=0; row < height; row+=2)
2450 for (col=0; col < width; col+=128) {
2451 len = MIN (128, width-col);
2452 kodak_65000_decode (buf, len*3);
2453 y[0][1] = y[1][1] = cb = cr = 0;
2454 for (bp=buf, i=0; i < len; i+=2, bp+=2) {
2455 cb += bp[4];
2456 cr += bp[5];
2457 rgb[1] = -((cb + cr + 2) >> 2);
2458 rgb[2] = rgb[1] + cb;
2459 rgb[0] = rgb[1] + cr;
2460 for (j=0; j < 2; j++)
2461 for (k=0; k < 2; k++) {
2462 if ((y[j][k] = y[j][k^1] + *bp++) >> 10) derror();
2463 ip = image[(row+j)*width + col+i+k];
2464 FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
2465 }
2466 }
2467 }
2468 }
2469
kodak_rgb_load_raw()2470 void CLASS kodak_rgb_load_raw()
2471 {
2472 short buf[768], *bp;
2473 int row, col, len, c, i, rgb[3];
2474 ushort *ip=image[0];
2475
2476 for (row=0; row < height; row++)
2477 for (col=0; col < width; col+=256) {
2478 len = MIN (256, width-col);
2479 kodak_65000_decode (buf, len*3);
2480 memset (rgb, 0, sizeof rgb);
2481 for (bp=buf, i=0; i < len; i++, ip+=4)
2482 FORC3 if ((ip[c] = rgb[c] += *bp++) >> 12) derror();
2483 }
2484 }
2485
kodak_thumb_load_raw()2486 void CLASS kodak_thumb_load_raw()
2487 {
2488 int row, col;
2489 colors = thumb_misc >> 5;
2490 for (row=0; row < height; row++)
2491 for (col=0; col < width; col++)
2492 read_shorts (image[row*width+col], colors);
2493 maximum = (1 << (thumb_misc & 31)) - 1;
2494 }
2495
sony_decrypt(unsigned * data,int len,int start,int key)2496 void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
2497 {
2498 // TODO: The following static variables are not thread-safe
2499 static unsigned pad[128], p;
2500
2501 if (start) {
2502 for (p=0; p < 4; p++)
2503 pad[p] = key = key * 48828125 + 1;
2504 pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
2505 for (p=4; p < 127; p++)
2506 pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
2507 for (p=0; p < 127; p++)
2508 pad[p] = htonl(pad[p]);
2509 }
2510 while (len--)
2511 *data++ ^= pad[p++ & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
2512 }
2513
sony_load_raw()2514 void CLASS sony_load_raw()
2515 {
2516 uchar head[40];
2517 ushort *pixel;
2518 unsigned i, key, row, col;
2519
2520 fseek (ifp, 200896, SEEK_SET);
2521 fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
2522 order = 0x4d4d;
2523 key = get4();
2524 fseek (ifp, 164600, SEEK_SET);
2525 fread (head, 1, 40, ifp);
2526 sony_decrypt ((unsigned int *) head, 10, 1, key);
2527 for (i=26; i-- > 22; )
2528 key = key << 8 | head[i];
2529 fseek (ifp, data_offset, SEEK_SET);
2530 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
2531 merror (pixel, "sony_load_raw()");
2532 for (row=0; row < height; row++) {
2533 if (fread (pixel, 2, raw_width, ifp) < raw_width) derror();
2534 sony_decrypt ((unsigned int *) pixel, raw_width/2, !row, key);
2535 for (col=9; col < left_margin; col++)
2536 black += ntohs(pixel[col]);
2537 for (col=0; col < width; col++)
2538 if ((BAYER(row,col) = ntohs(pixel[col+left_margin])) >> 14)
2539 derror();
2540 }
2541 free (pixel);
2542 if (left_margin > 9)
2543 black /= (left_margin-9) * height;
2544 maximum = 0x3ff0;
2545 }
2546
sony_arw_load_raw()2547 void CLASS sony_arw_load_raw()
2548 {
2549 ushort huff[32768];
2550 static const ushort tab[18] =
2551 { 0xf11,0xf10,0xe0f,0xd0e,0xc0d,0xb0c,0xa0b,0x90a,0x809,
2552 0x708,0x607,0x506,0x405,0x304,0x303,0x300,0x202,0x201 };
2553 int i, c, n, col, row, len, diff, sum=0;
2554
2555 for (n=i=0; i < 18; i++)
2556 FORC(32768 >> (tab[i] >> 8)) huff[n++] = tab[i];
2557 getbits(-1);
2558 for (col = raw_width; col--; )
2559 for (row=0; row < raw_height+1; row+=2) {
2560 if (row == raw_height) row = 1;
2561 len = getbithuff(15,huff);
2562 diff = getbits(len);
2563 if ((diff & (1 << (len-1))) == 0)
2564 diff -= (1 << len) - 1;
2565 if ((sum += diff) >> 12) derror();
2566 if (row < height) BAYER(row,col) = sum;
2567 }
2568 }
2569
sony_arw2_load_raw()2570 void CLASS sony_arw2_load_raw()
2571 {
2572 uchar *data, *dp;
2573 ushort pix[16];
2574 int row, col, val, max, min, imax, imin, sh, bit, i;
2575
2576 data = (uchar *) malloc (raw_width);
2577 merror (data, "sony_arw2_load_raw()");
2578 for (row=0; row < height; row++) {
2579 fread (data, 1, raw_width, ifp);
2580 for (dp=data, col=0; col < width-30; dp+=16) {
2581 max = 0x7ff & (val = sget4(dp));
2582 min = 0x7ff & val >> 11;
2583 imax = 0x0f & val >> 22;
2584 imin = 0x0f & val >> 26;
2585 for (sh=0; sh < 4 && 0x80 << sh <= max-min; sh++);
2586 for (bit=30, i=0; i < 16; i++)
2587 if (i == imax) pix[i] = max;
2588 else if (i == imin) pix[i] = min;
2589 else {
2590 pix[i] = ((sget2(dp+(bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
2591 if (pix[i] > 0x7ff) pix[i] = 0x7ff;
2592 bit += 7;
2593 }
2594 for (i=0; i < 16; i++, col+=2)
2595 BAYER(row,col) = curve[pix[i] << 1] >> 2;
2596 col -= col & 1 ? 1:31;
2597 }
2598 }
2599 free (data);
2600 }
2601
2602 #define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
2603
2604 /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
smal_decode_segment(unsigned seg[2][2],int holes)2605 void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
2606 {
2607 uchar hist[3][13] = {
2608 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2609 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2610 { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
2611 int low, high=0xff, carry=0, nbits=8;
2612 int s, count, bin, next, i, sym[3];
2613 uchar diff, pred[]={0,0};
2614 ushort data=0, range=0;
2615 unsigned pix, row, col;
2616
2617 fseek (ifp, seg[0][1]+1, SEEK_SET);
2618 getbits(-1);
2619 for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
2620 for (s=0; s < 3; s++) {
2621 data = data << nbits | getbits(nbits);
2622 if (carry < 0)
2623 carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
2624 while (--nbits >= 0)
2625 if ((data >> nbits & 0xff) == 0xff) break;
2626 if (nbits > 0)
2627 data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
2628 ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
2629 if (nbits >= 0) {
2630 data += getbits(1);
2631 carry = nbits - 8;
2632 }
2633 count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
2634 for (bin=0; hist[s][bin+5] > count; bin++);
2635 low = hist[s][bin+5] * (high >> 4) >> 2;
2636 if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
2637 high -= low;
2638 for (nbits=0; high << nbits < 128; nbits++);
2639 range = (range+low) << nbits;
2640 high <<= nbits;
2641 next = hist[s][1];
2642 if (++hist[s][2] > hist[s][3]) {
2643 next = (next+1) & hist[s][0];
2644 hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
2645 hist[s][2] = 1;
2646 }
2647 if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
2648 if (bin < hist[s][1])
2649 for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
2650 else if (next <= bin)
2651 for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
2652 }
2653 hist[s][1] = next;
2654 sym[s] = bin;
2655 }
2656 diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
2657 if (sym[0] & 4)
2658 diff = diff ? -diff : 0x80;
2659 if ((unsigned) ftell(ifp) + 12 >= seg[1][1])
2660 diff = 0;
2661 pred[pix & 1] += diff;
2662 row = pix / raw_width - top_margin;
2663 col = pix % raw_width - left_margin;
2664 if (row < height && col < width)
2665 BAYER(row,col) = pred[pix & 1];
2666 if (!(pix & 1) && HOLE(row)) pix += 2;
2667 }
2668 maximum = 0xff;
2669 }
2670
smal_v6_load_raw()2671 void CLASS smal_v6_load_raw()
2672 {
2673 unsigned seg[2][2];
2674
2675 fseek (ifp, 16, SEEK_SET);
2676 seg[0][0] = 0;
2677 seg[0][1] = get2();
2678 seg[1][0] = raw_width * raw_height;
2679 seg[1][1] = INT_MAX;
2680 smal_decode_segment (seg, 0);
2681 }
2682
median4(int * p)2683 int CLASS median4 (int *p)
2684 {
2685 int min, max, sum, i;
2686
2687 min = max = sum = p[0];
2688 for (i=1; i < 4; i++) {
2689 sum += p[i];
2690 if (min > p[i]) min = p[i];
2691 if (max < p[i]) max = p[i];
2692 }
2693 return (sum - min - max) >> 1;
2694 }
2695
fill_holes(int holes)2696 void CLASS fill_holes (int holes)
2697 {
2698 int row, col, val[4];
2699
2700 for (row=2; row < height-2; row++) {
2701 if (!HOLE(row)) continue;
2702 for (col=1; col < width-1; col+=4) {
2703 val[0] = BAYER(row-1,col-1);
2704 val[1] = BAYER(row-1,col+1);
2705 val[2] = BAYER(row+1,col-1);
2706 val[3] = BAYER(row+1,col+1);
2707 BAYER(row,col) = median4(val);
2708 }
2709 for (col=2; col < width-2; col+=4)
2710 if (HOLE(row-2) || HOLE(row+2))
2711 BAYER(row,col) = (BAYER(row,col-2) + BAYER(row,col+2)) >> 1;
2712 else {
2713 val[0] = BAYER(row,col-2);
2714 val[1] = BAYER(row,col+2);
2715 val[2] = BAYER(row-2,col);
2716 val[3] = BAYER(row+2,col);
2717 BAYER(row,col) = median4(val);
2718 }
2719 }
2720 }
2721
smal_v9_load_raw()2722 void CLASS smal_v9_load_raw()
2723 {
2724 unsigned seg[256][2], offset, nseg, holes, i;
2725
2726 fseek (ifp, 67, SEEK_SET);
2727 offset = get4();
2728 nseg = fgetc(ifp);
2729 fseek (ifp, offset, SEEK_SET);
2730 for (i=0; i < nseg*2; i++)
2731 seg[0][i] = get4() + data_offset*(i & 1);
2732 fseek (ifp, 78, SEEK_SET);
2733 holes = fgetc(ifp);
2734 fseek (ifp, 88, SEEK_SET);
2735 seg[nseg][0] = raw_height * raw_width;
2736 seg[nseg][1] = get4() + data_offset;
2737 for (i=0; i < nseg; i++)
2738 smal_decode_segment (seg+i, holes);
2739 if (holes) fill_holes (holes);
2740 }
2741
2742 /* RESTRICTED code starts here */
2743
foveon_decoder(unsigned size,unsigned code)2744 void CLASS foveon_decoder (unsigned size, unsigned code)
2745 {
2746 // TODO: The following static variable is not thread-safe
2747 static unsigned huff[1024];
2748 struct decode *cur;
2749 unsigned i, len;
2750
2751 if (!code) {
2752 for (i=0; i < size; i++)
2753 huff[i] = get4();
2754 memset (first_decode, 0, sizeof first_decode);
2755 free_decode = first_decode;
2756 }
2757 cur = free_decode++;
2758 if (free_decode > first_decode+2048) {
2759 dcraw_message (DCRAW_ERROR,_("%s: decoder table overflow\n"), ifname_display);
2760 longjmp (failure, 2);
2761 }
2762 if (code)
2763 for (i=0; i < size; i++)
2764 if (huff[i] == code) {
2765 cur->leaf = i;
2766 return;
2767 }
2768 if ((len = code >> 27) > 26) return;
2769 code = (len+1) << 27 | (code & 0x3ffffff) << 1;
2770
2771 cur->branch[0] = free_decode;
2772 foveon_decoder (size, code);
2773 cur->branch[1] = free_decode;
2774 foveon_decoder (size, code+1);
2775 }
2776
foveon_thumb()2777 void CLASS foveon_thumb()
2778 {
2779 int bwide;
2780 unsigned row, col, bitbuf=0, bit=1, c, i;
2781 char *buf;
2782 struct decode *dindex;
2783 short pred[3];
2784
2785 bwide = get4();
2786 fprintf (ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
2787 if (bwide > 0) {
2788 if (bwide < thumb_width*3) return;
2789 buf = (char *) malloc (bwide);
2790 merror (buf, "foveon_thumb()");
2791 for (row=0; row < thumb_height; row++) {
2792 fread (buf, 1, bwide, ifp);
2793 fwrite (buf, 3, thumb_width, ofp);
2794 }
2795 free (buf);
2796 return;
2797 }
2798 foveon_decoder (256, 0);
2799
2800 for (row=0; row < thumb_height; row++) {
2801 memset (pred, 0, sizeof pred);
2802 if (!bit) get4();
2803 for (bit=col=0; col < thumb_width; col++)
2804 FORC3 {
2805 for (dindex=first_decode; dindex->branch[0]; ) {
2806 if ((bit = (bit-1) & 31) == 31)
2807 for (i=0; i < 4; i++)
2808 bitbuf = (bitbuf << 8) + fgetc(ifp);
2809 dindex = dindex->branch[bitbuf >> bit & 1];
2810 }
2811 pred[c] += dindex->leaf;
2812 fputc (pred[c], ofp);
2813 }
2814 }
2815 }
2816
foveon_load_camf()2817 void CLASS foveon_load_camf()
2818 {
2819 unsigned key, i, val;
2820
2821 fseek (ifp, meta_offset, SEEK_SET);
2822 key = get4();
2823 fread (meta_data, 1, meta_length, ifp);
2824 for (i=0; i < meta_length; i++) {
2825 key = (key * 1597 + 51749) % 244944;
2826 val = key * (INT64) 301593171 >> 24;
2827 meta_data[i] ^= ((((key << 8) - val) >> 1) + val) >> 17;
2828 }
2829 }
2830
foveon_load_raw()2831 void CLASS foveon_load_raw()
2832 {
2833 struct decode *dindex;
2834 short diff[1024];
2835 unsigned bitbuf=0;
2836 int pred[3], fixed, row, col, bit=-1, c, i;
2837
2838 fixed = get4();
2839 read_shorts ((ushort *) diff, 1024);
2840 if (!fixed) foveon_decoder (1024, 0);
2841
2842 for (row=0; row < height; row++) {
2843 memset (pred, 0, sizeof pred);
2844 if (!bit && !fixed && atoi(model+2) < 14) get4();
2845 for (col=bit=0; col < width; col++) {
2846 if (fixed) {
2847 bitbuf = get4();
2848 FORC3 pred[2-c] += diff[bitbuf >> c*10 & 0x3ff];
2849 }
2850 else FORC3 {
2851 for (dindex=first_decode; dindex->branch[0]; ) {
2852 if ((bit = (bit-1) & 31) == 31)
2853 for (i=0; i < 4; i++)
2854 bitbuf = (bitbuf << 8) + fgetc(ifp);
2855 dindex = dindex->branch[bitbuf >> bit & 1];
2856 }
2857 pred[c] += diff[dindex->leaf];
2858 if (pred[c] >> 16 && ~pred[c] >> 16) derror();
2859 }
2860 FORC3 image[row*width+col][c] = pred[c];
2861 }
2862 }
2863 if (document_mode)
2864 for (i=0; i < height*width*4; i++)
2865 if ((short) image[0][i] < 0) image[0][i] = 0;
2866 foveon_load_camf();
2867 }
2868
foveon_camf_param(const char * block,const char * param)2869 const char * CLASS foveon_camf_param (const char *block, const char *param)
2870 {
2871 unsigned idx, num;
2872 char *pos, *cp, *dp;
2873
2874 for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
2875 pos = meta_data + idx;
2876 if (strncmp (pos, "CMb", 3)) break;
2877 if (pos[3] != 'P') continue;
2878 if (strcmp (block, pos+sget4(pos+12))) continue;
2879 cp = pos + sget4(pos+16);
2880 num = sget4(cp);
2881 dp = pos + sget4(cp+4);
2882 while (num--) {
2883 cp += 8;
2884 if (!strcmp (param, dp+sget4(cp)))
2885 return dp+sget4(cp+4);
2886 }
2887 }
2888 return 0;
2889 }
2890
foveon_camf_matrix(unsigned dim[3],const char * name)2891 void * CLASS foveon_camf_matrix (unsigned dim[3], const char *name)
2892 {
2893 unsigned i, idx, type, ndim, size, *mat;
2894 char *pos, *cp, *dp;
2895 double dsize;
2896
2897 for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
2898 pos = meta_data + idx;
2899 if (strncmp (pos, "CMb", 3)) break;
2900 if (pos[3] != 'M') continue;
2901 if (strcmp (name, pos+sget4(pos+12))) continue;
2902 dim[0] = dim[1] = dim[2] = 1;
2903 cp = pos + sget4(pos+16);
2904 type = sget4(cp);
2905 if ((ndim = sget4(cp+4)) > 3) break;
2906 dp = pos + sget4(cp+8);
2907 for (i=ndim; i--; ) {
2908 cp += 12;
2909 dim[i] = sget4(cp);
2910 }
2911 if ((dsize = (double) dim[0]*dim[1]*dim[2]) > meta_length/4) break;
2912 mat = (unsigned *) malloc ((size = dsize) * 4);
2913 merror (mat, "foveon_camf_matrix()");
2914 for (i=0; i < size; i++)
2915 if (type && type != 6)
2916 mat[i] = sget4(dp + i*4);
2917 else
2918 mat[i] = sget4(dp + i*2) & 0xffff;
2919 return mat;
2920 }
2921 dcraw_message (DCRAW_WARNING,_("%s: \"%s\" matrix not found!\n"), ifname_display, name);
2922 return 0;
2923 }
2924
foveon_fixed(void * ptr,int size,const char * name)2925 int CLASS foveon_fixed (void *ptr, int size, const char *name)
2926 {
2927 void *dp;
2928 unsigned dim[3];
2929
2930 dp = foveon_camf_matrix (dim, name);
2931 if (!dp) return 0;
2932 memcpy (ptr, dp, size*4);
2933 free (dp);
2934 return 1;
2935 }
2936
foveon_avg(short * pix,int range[2],float cfilt)2937 float CLASS foveon_avg (short *pix, int range[2], float cfilt)
2938 {
2939 int i;
2940 float val, min=FLT_MAX, max=-FLT_MAX, sum=0;
2941
2942 for (i=range[0]; i <= range[1]; i++) {
2943 sum += val = pix[i*4] + (pix[i*4]-pix[(i-1)*4]) * cfilt;
2944 if (min > val) min = val;
2945 if (max < val) max = val;
2946 }
2947 if (range[1] - range[0] == 1) return sum/2;
2948 return (sum - min - max) / (range[1] - range[0] - 1);
2949 }
2950
foveon_make_curve(double max,double mul,double filt)2951 short * CLASS foveon_make_curve (double max, double mul, double filt)
2952 {
2953 short *curve;
2954 unsigned i, size;
2955 double x;
2956
2957 if (!filt) filt = 0.8;
2958 size = 4*M_PI*max / filt;
2959 if (size == UINT_MAX) size--;
2960 curve = (short *) calloc (size+1, sizeof *curve);
2961 merror (curve, "foveon_make_curve()");
2962 curve[0] = size;
2963 for (i=0; i < size; i++) {
2964 x = i*filt/max/4;
2965 curve[i+1] = (cos(x)+1)/2 * tanh(i*filt/mul) * mul + 0.5;
2966 }
2967 return curve;
2968 }
2969
foveon_make_curves(short ** curvep,float dq[3],float div[3],float filt)2970 void CLASS foveon_make_curves
2971 (short **curvep, float dq[3], float div[3], float filt)
2972 {
2973 double mul[3], max=0;
2974 int c;
2975
2976 FORC3 mul[c] = dq[c]/div[c];
2977 FORC3 if (max < mul[c]) max = mul[c];
2978 FORC3 curvep[c] = foveon_make_curve (max, mul[c], filt);
2979 }
2980
foveon_apply_curve(short * curve,int i)2981 int CLASS foveon_apply_curve (short *curve, int i)
2982 {
2983 if (abs(i) >= curve[0]) return 0;
2984 return i < 0 ? -curve[1-i] : curve[1+i];
2985 }
2986
2987 #define image ((short (*)[4]) image)
2988
foveon_interpolate()2989 void CLASS foveon_interpolate()
2990 {
2991 static const short hood[] = { -1,-1, -1,0, -1,1, 0,-1, 0,1, 1,-1, 1,0, 1,1 };
2992 short *pix, prev[3], *curve[8], (*shrink)[3];
2993 float cfilt=0, ddft[3][3][2], ppm[3][3][3];
2994 float cam_xyz[3][3], correct[3][3], last[3][3], trans[3][3];
2995 float chroma_dq[3], color_dq[3], diag[3][3], div[3];
2996 float (*black)[3], (*sgain)[3], (*sgrow)[3];
2997 float fsum[3], val, frow, num;
2998 int row, col, c, i, j, diff, sgx, irow, sum, min, max, limit;
2999 int dscr[2][2], dstb[4], (*smrow[7])[3], total[4], ipix[3];
3000 int work[3][3], smlast, smred, smred_p=0, dev[3];
3001 int satlev[3], keep[4], active[4];
3002 unsigned dim[3], *badpix;
3003 double dsum=0, trsum[3];
3004 char str[128];
3005 const char* cp;
3006
3007 dcraw_message (DCRAW_VERBOSE,_("Foveon interpolation...\n"));
3008
3009 foveon_fixed (dscr, 4, "DarkShieldColRange");
3010 foveon_fixed (ppm[0][0], 27, "PostPolyMatrix");
3011 foveon_fixed (satlev, 3, "SaturationLevel");
3012 foveon_fixed (keep, 4, "KeepImageArea");
3013 foveon_fixed (active, 4, "ActiveImageArea");
3014 foveon_fixed (chroma_dq, 3, "ChromaDQ");
3015 foveon_fixed (color_dq, 3,
3016 foveon_camf_param ("IncludeBlocks", "ColorDQ") ?
3017 "ColorDQ" : "ColorDQCamRGB");
3018 if (foveon_camf_param ("IncludeBlocks", "ColumnFilter"))
3019 foveon_fixed (&cfilt, 1, "ColumnFilter");
3020
3021 memset (ddft, 0, sizeof ddft);
3022 if (!foveon_camf_param ("IncludeBlocks", "DarkDrift")
3023 || !foveon_fixed (ddft[1][0], 12, "DarkDrift"))
3024 for (i=0; i < 2; i++) {
3025 foveon_fixed (dstb, 4, i ? "DarkShieldBottom":"DarkShieldTop");
3026 for (row = dstb[1]; row <= dstb[3]; row++)
3027 for (col = dstb[0]; col <= dstb[2]; col++)
3028 FORC3 ddft[i+1][c][1] += (short) image[row*width+col][c];
3029 FORC3 ddft[i+1][c][1] /= (dstb[3]-dstb[1]+1) * (dstb[2]-dstb[0]+1);
3030 }
3031
3032 if (!(cp = foveon_camf_param ("WhiteBalanceIlluminants", model2)))
3033 { dcraw_message (DCRAW_ERROR,_("%s: Invalid white balance \"%s\"\n"), ifname_display, model2);
3034 return; }
3035 foveon_fixed (cam_xyz, 9, cp);
3036 foveon_fixed (correct, 9,
3037 foveon_camf_param ("WhiteBalanceCorrections", model2));
3038 memset (last, 0, sizeof last);
3039 for (i=0; i < 3; i++)
3040 for (j=0; j < 3; j++)
3041 FORC3 last[i][j] += correct[i][c] * cam_xyz[c][j];
3042
3043 #define LAST(x,y) last[(i+x)%3][(c+y)%3]
3044 for (i=0; i < 3; i++)
3045 FORC3 diag[c][i] = LAST(1,1)*LAST(2,2) - LAST(1,2)*LAST(2,1);
3046 #undef LAST
3047 FORC3 div[c] = diag[c][0]*0.3127 + diag[c][1]*0.329 + diag[c][2]*0.3583;
3048 sprintf (str, "%sRGBNeutral", model2);
3049 if (foveon_camf_param ("IncludeBlocks", str))
3050 foveon_fixed (div, 3, str);
3051 num = 0;
3052 FORC3 if (num < div[c]) num = div[c];
3053 FORC3 div[c] /= num;
3054
3055 memset (trans, 0, sizeof trans);
3056 for (i=0; i < 3; i++)
3057 for (j=0; j < 3; j++)
3058 FORC3 trans[i][j] += rgb_cam[i][c] * last[c][j] * div[j];
3059 FORC3 trsum[c] = trans[c][0] + trans[c][1] + trans[c][2];
3060 dsum = (6*trsum[0] + 11*trsum[1] + 3*trsum[2]) / 20;
3061 for (i=0; i < 3; i++)
3062 FORC3 last[i][c] = trans[i][c] * dsum / trsum[i];
3063 memset (trans, 0, sizeof trans);
3064 for (i=0; i < 3; i++)
3065 for (j=0; j < 3; j++)
3066 FORC3 trans[i][j] += (i==c ? 32 : -1) * last[c][j] / 30;
3067
3068 foveon_make_curves (curve, color_dq, div, cfilt);
3069 FORC3 chroma_dq[c] /= 3;
3070 foveon_make_curves (curve+3, chroma_dq, div, cfilt);
3071 FORC3 dsum += chroma_dq[c] / div[c];
3072 curve[6] = foveon_make_curve (dsum, dsum, cfilt);
3073 curve[7] = foveon_make_curve (dsum*2, dsum*2, cfilt);
3074
3075 sgain = (float (*)[3]) foveon_camf_matrix (dim, "SpatialGain");
3076 if (!sgain) return;
3077 sgrow = (float (*)[3]) calloc (dim[1], sizeof *sgrow);
3078 sgx = (width + dim[1]-2) / (dim[1]-1);
3079
3080 black = (float (*)[3]) calloc (height, sizeof *black);
3081 for (row=0; row < height; row++) {
3082 for (i=0; i < 6; i++)
3083 ddft[0][0][i] = ddft[1][0][i] +
3084 row / (height-1.0) * (ddft[2][0][i] - ddft[1][0][i]);
3085 FORC3 black[row][c] =
3086 ( foveon_avg (image[row*width]+c, dscr[0], cfilt) +
3087 foveon_avg (image[row*width]+c, dscr[1], cfilt) * 3
3088 - ddft[0][c][0] ) / 4 - ddft[0][c][1];
3089 }
3090 memcpy (black, black+8, sizeof *black*8);
3091 memcpy (black+height-11, black+height-22, 11*sizeof *black);
3092 memcpy (last, black, sizeof last);
3093
3094 for (row=1; row < height-1; row++) {
3095 FORC3 if (last[1][c] > last[0][c]) {
3096 if (last[1][c] > last[2][c])
3097 black[row][c] = (last[0][c] > last[2][c]) ? last[0][c]:last[2][c];
3098 } else
3099 if (last[1][c] < last[2][c])
3100 black[row][c] = (last[0][c] < last[2][c]) ? last[0][c]:last[2][c];
3101 memmove (last, last+1, 2*sizeof last[0]);
3102 memcpy (last[2], black[row+1], sizeof last[2]);
3103 }
3104 FORC3 black[row][c] = (last[0][c] + last[1][c])/2;
3105 FORC3 black[0][c] = (black[1][c] + black[3][c])/2;
3106
3107 val = 1 - exp(-1/24.0);
3108 memcpy (fsum, black, sizeof fsum);
3109 for (row=1; row < height; row++)
3110 FORC3 fsum[c] += black[row][c] =
3111 (black[row][c] - black[row-1][c])*val + black[row-1][c];
3112 memcpy (last[0], black[height-1], sizeof last[0]);
3113 FORC3 fsum[c] /= height;
3114 for (row = height; row--; )
3115 FORC3 last[0][c] = black[row][c] =
3116 (black[row][c] - fsum[c] - last[0][c])*val + last[0][c];
3117
3118 memset (total, 0, sizeof total);
3119 for (row=2; row < height; row+=4)
3120 for (col=2; col < width; col+=4) {
3121 FORC3 total[c] += (short) image[row*width+col][c];
3122 total[3]++;
3123 }
3124 for (row=0; row < height; row++)
3125 FORC3 black[row][c] += fsum[c]/2 + total[c]/(total[3]*100.0);
3126
3127 for (row=0; row < height; row++) {
3128 for (i=0; i < 6; i++)
3129 ddft[0][0][i] = ddft[1][0][i] +
3130 row / (height-1.0) * (ddft[2][0][i] - ddft[1][0][i]);
3131 pix = image[row*width];
3132 memcpy (prev, pix, sizeof prev);
3133 frow = row / (height-1.0) * (dim[2]-1);
3134 if ((irow = frow) == (int) dim[2]-1) irow--;
3135 frow -= irow;
3136 for (i=0; i < (int) dim[1]; i++)
3137 FORC3 sgrow[i][c] = sgain[ irow *dim[1]+i][c] * (1-frow) +
3138 sgain[(irow+1)*dim[1]+i][c] * frow;
3139 for (col=0; col < width; col++) {
3140 FORC3 {
3141 diff = pix[c] - prev[c];
3142 prev[c] = pix[c];
3143 ipix[c] = pix[c] + floor ((diff + (diff*diff >> 14)) * cfilt
3144 - ddft[0][c][1] - ddft[0][c][0] * ((float) col/width - 0.5)
3145 - black[row][c] );
3146 }
3147 FORC3 {
3148 work[0][c] = ipix[c] * ipix[c] >> 14;
3149 work[2][c] = ipix[c] * work[0][c] >> 14;
3150 work[1][2-c] = ipix[(c+1) % 3] * ipix[(c+2) % 3] >> 14;
3151 }
3152 FORC3 {
3153 for (val=i=0; i < 3; i++)
3154 for ( j=0; j < 3; j++)
3155 val += ppm[c][i][j] * work[i][j];
3156 ipix[c] = floor ((ipix[c] + floor(val)) *
3157 ( sgrow[col/sgx ][c] * (sgx - col%sgx) +
3158 sgrow[col/sgx+1][c] * (col%sgx) ) / sgx / div[c]);
3159 if (ipix[c] > 32000) ipix[c] = 32000;
3160 pix[c] = ipix[c];
3161 }
3162 pix += 4;
3163 }
3164 }
3165 free (black);
3166 free (sgrow);
3167 free (sgain);
3168
3169 if ((badpix = (unsigned int *) foveon_camf_matrix (dim, "BadPixels"))) {
3170 for (i=0; i < (int) dim[0]; i++) {
3171 col = (badpix[i] >> 8 & 0xfff) - keep[0];
3172 row = (badpix[i] >> 20 ) - keep[1];
3173 if (row-1 < 0 || row-1 > height-3 || col-1 < 0 || col-1 > width-3)
3174 continue;
3175 memset (fsum, 0, sizeof fsum);
3176 for (sum=j=0; j < 8; j++)
3177 if (badpix[i] & (1 << j)) {
3178 FORC3 fsum[c] += (short)
3179 image[(row+hood[j*2])*width+col+hood[j*2+1]][c];
3180 sum++;
3181 }
3182 if (sum) FORC3 image[row*width+col][c] = fsum[c]/sum;
3183 }
3184 free (badpix);
3185 }
3186
3187 /* Array for 5x5 Gaussian averaging of red values */
3188 smrow[6] = (int (*)[3]) calloc (width*5, sizeof **smrow);
3189 merror (smrow[6], "foveon_interpolate()");
3190 for (i=0; i < 5; i++)
3191 smrow[i] = smrow[6] + i*width;
3192
3193 /* Sharpen the reds against these Gaussian averages */
3194 for (smlast=-1, row=2; row < height-2; row++) {
3195 while (smlast < row+2) {
3196 for (i=0; i < 6; i++)
3197 smrow[(i+5) % 6] = smrow[i];
3198 pix = image[++smlast*width+2];
3199 for (col=2; col < width-2; col++) {
3200 smrow[4][col][0] =
3201 (pix[0]*6 + (pix[-4]+pix[4])*4 + pix[-8]+pix[8] + 8) >> 4;
3202 pix += 4;
3203 }
3204 }
3205 pix = image[row*width+2];
3206 for (col=2; col < width-2; col++) {
3207 smred = ( 6 * smrow[2][col][0]
3208 + 4 * (smrow[1][col][0] + smrow[3][col][0])
3209 + smrow[0][col][0] + smrow[4][col][0] + 8 ) >> 4;
3210 if (col == 2)
3211 smred_p = smred;
3212 i = pix[0] + ((pix[0] - ((smred*7 + smred_p) >> 3)) >> 3);
3213 if (i > 32000) i = 32000;
3214 pix[0] = i;
3215 smred_p = smred;
3216 pix += 4;
3217 }
3218 }
3219
3220 /* Adjust the brighter pixels for better linearity */
3221 min = 0xffff;
3222 FORC3 {
3223 i = satlev[c] / div[c];
3224 if (min > i) min = i;
3225 }
3226 limit = min * 9 >> 4;
3227 for (pix=image[0]; pix < image[height*width]; pix+=4) {
3228 if (pix[0] <= limit || pix[1] <= limit || pix[2] <= limit)
3229 continue;
3230 min = max = pix[0];
3231 for (c=1; c < 3; c++) {
3232 if (min > pix[c]) min = pix[c];
3233 if (max < pix[c]) max = pix[c];
3234 }
3235 if (min >= limit*2) {
3236 pix[0] = pix[1] = pix[2] = max;
3237 } else {
3238 i = 0x4000 - ((min - limit) << 14) / limit;
3239 i = 0x4000 - (i*i >> 14);
3240 i = i*i >> 14;
3241 FORC3 pix[c] += (max - pix[c]) * i >> 14;
3242 }
3243 }
3244 /*
3245 Because photons that miss one detector often hit another,
3246 the sum R+G+B is much less noisy than the individual colors.
3247 So smooth the hues without smoothing the total.
3248 */
3249 for (smlast=-1, row=2; row < height-2; row++) {
3250 while (smlast < row+2) {
3251 for (i=0; i < 6; i++)
3252 smrow[(i+5) % 6] = smrow[i];
3253 pix = image[++smlast*width+2];
3254 for (col=2; col < width-2; col++) {
3255 FORC3 smrow[4][col][c] = (pix[c-4]+2*pix[c]+pix[c+4]+2) >> 2;
3256 pix += 4;
3257 }
3258 }
3259 pix = image[row*width+2];
3260 for (col=2; col < width-2; col++) {
3261 FORC3 dev[c] = -foveon_apply_curve (curve[7], pix[c] -
3262 ((smrow[1][col][c] + 2*smrow[2][col][c] + smrow[3][col][c]) >> 2));
3263 sum = (dev[0] + dev[1] + dev[2]) >> 3;
3264 FORC3 pix[c] += dev[c] - sum;
3265 pix += 4;
3266 }
3267 }
3268 for (smlast=-1, row=2; row < height-2; row++) {
3269 while (smlast < row+2) {
3270 for (i=0; i < 6; i++)
3271 smrow[(i+5) % 6] = smrow[i];
3272 pix = image[++smlast*width+2];
3273 for (col=2; col < width-2; col++) {
3274 FORC3 smrow[4][col][c] =
3275 (pix[c-8]+pix[c-4]+pix[c]+pix[c+4]+pix[c+8]+2) >> 2;
3276 pix += 4;
3277 }
3278 }
3279 pix = image[row*width+2];
3280 for (col=2; col < width-2; col++) {
3281 for (total[3]=375, sum=60, c=0; c < 3; c++) {
3282 for (total[c]=i=0; i < 5; i++)
3283 total[c] += smrow[i][col][c];
3284 total[3] += total[c];
3285 sum += pix[c];
3286 }
3287 if (sum < 0) sum = 0;
3288 j = total[3] > 375 ? (sum << 16) / total[3] : sum * 174;
3289 FORC3 pix[c] += foveon_apply_curve (curve[6],
3290 ((j*total[c] + 0x8000) >> 16) - pix[c]);
3291 pix += 4;
3292 }
3293 }
3294
3295 /* Transform the image to a different colorspace */
3296 for (pix=image[0]; pix < image[height*width]; pix+=4) {
3297 FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]);
3298 sum = (pix[0]+pix[1]+pix[1]+pix[2]) >> 2;
3299 FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]-sum);
3300 FORC3 {
3301 for (dsum=i=0; i < 3; i++)
3302 dsum += trans[c][i] * pix[i];
3303 if (dsum < 0) dsum = 0;
3304 if (dsum > 24000) dsum = 24000;
3305 ipix[c] = dsum + 0.5;
3306 }
3307 FORC3 pix[c] = ipix[c];
3308 }
3309
3310 /* Smooth the image bottom-to-top and save at 1/4 scale */
3311 shrink = (short (*)[3]) calloc ((width/4) * (height/4), sizeof *shrink);
3312 merror (shrink, "foveon_interpolate()");
3313 for (row = height/4; row--; )
3314 for (col=0; col < width/4; col++) {
3315 ipix[0] = ipix[1] = ipix[2] = 0;
3316 for (i=0; i < 4; i++)
3317 for (j=0; j < 4; j++)
3318 FORC3 ipix[c] += image[(row*4+i)*width+col*4+j][c];
3319 FORC3
3320 if (row+2 > height/4)
3321 shrink[row*(width/4)+col][c] = ipix[c] >> 4;
3322 else
3323 shrink[row*(width/4)+col][c] =
3324 (shrink[(row+1)*(width/4)+col][c]*1840 + ipix[c]*141 + 2048) >> 12;
3325 }
3326 /* From the 1/4-scale image, smooth right-to-left */
3327 for (row=0; row < (height & ~3); row++) {
3328 ipix[0] = ipix[1] = ipix[2] = 0;
3329 if ((row & 3) == 0)
3330 for (col = width & ~3 ; col--; )
3331 FORC3 smrow[0][col][c] = ipix[c] =
3332 (shrink[(row/4)*(width/4)+col/4][c]*1485 + ipix[c]*6707 + 4096) >> 13;
3333
3334 /* Then smooth left-to-right */
3335 ipix[0] = ipix[1] = ipix[2] = 0;
3336 for (col=0; col < (width & ~3); col++)
3337 FORC3 smrow[1][col][c] = ipix[c] =
3338 (smrow[0][col][c]*1485 + ipix[c]*6707 + 4096) >> 13;
3339
3340 /* Smooth top-to-bottom */
3341 if (row == 0)
3342 memcpy (smrow[2], smrow[1], sizeof **smrow * width);
3343 else
3344 for (col=0; col < (width & ~3); col++)
3345 FORC3 smrow[2][col][c] =
3346 (smrow[2][col][c]*6707 + smrow[1][col][c]*1485 + 4096) >> 13;
3347
3348 /* Adjust the chroma toward the smooth values */
3349 for (col=0; col < (width & ~3); col++) {
3350 for (i=j=30, c=0; c < 3; c++) {
3351 i += smrow[2][col][c];
3352 j += image[row*width+col][c];
3353 }
3354 j = (j << 16) / i;
3355 for (sum=c=0; c < 3; c++) {
3356 ipix[c] = foveon_apply_curve (curve[c+3],
3357 ((smrow[2][col][c] * j + 0x8000) >> 16) - image[row*width+col][c]);
3358 sum += ipix[c];
3359 }
3360 sum >>= 3;
3361 FORC3 {
3362 i = image[row*width+col][c] + ipix[c] - sum;
3363 if (i < 0) i = 0;
3364 image[row*width+col][c] = i;
3365 }
3366 }
3367 }
3368 free (shrink);
3369 free (smrow[6]);
3370 for (i=0; i < 8; i++)
3371 free (curve[i]);
3372
3373 /* Trim off the black border */
3374 active[1] -= keep[1];
3375 active[3] -= 2;
3376 i = active[2] - active[0];
3377 for (row=0; row < active[3]-active[1]; row++)
3378 memcpy (image[row*i], image[(row+active[1])*width+active[0]],
3379 i * sizeof *image);
3380 width = i;
3381 height = row;
3382 }
3383 #undef image
3384
3385 /* RESTRICTED code ends here */
3386
3387 /*
3388 Seach from the current directory up to the root looking for
3389 a ".badpixels" file, and fix those pixels now.
3390 */
bad_pixels(const char * cfname)3391 void CLASS bad_pixels (const char *cfname)
3392 {
3393 FILE *fp=0;
3394 char *fname, *cp, line[128];
3395 int len, time, row, col, r, c, rad, tot, n, fixed=0;
3396
3397 if (!filters) return;
3398 if (cfname)
3399 fp = fopen (cfname, "r");
3400 else {
3401 for (len=32 ; ; len *= 2) {
3402 fname = (char *) malloc (len);
3403 if (!fname) return;
3404 if (getcwd (fname, len-16)) break;
3405 free (fname);
3406 if (errno != ERANGE) return;
3407 }
3408 #if defined(WIN32) || defined(DJGPP)
3409 if (fname[1] == ':')
3410 memmove (fname, fname+2, len-2);
3411 for (cp=fname; *cp; cp++)
3412 if (*cp == '\\') *cp = '/';
3413 #endif
3414 cp = fname + strlen(fname);
3415 if (cp[-1] == '/') cp--;
3416 while (*fname == '/') {
3417 strcpy (cp, "/.badpixels");
3418 if ((fp = fopen (fname, "r"))) break;
3419 if (cp == fname) break;
3420 while (*--cp != '/');
3421 }
3422 free (fname);
3423 }
3424 if (!fp) return;
3425 while (::fgets (line, 128, fp)) {
3426 cp = strchr (line, '#');
3427 if (cp) *cp = 0;
3428 if (sscanf (line, "%d %d %d", &col, &row, &time) != 3) continue;
3429 if ((unsigned) col >= width || (unsigned) row >= height) continue;
3430 if (time > timestamp) continue;
3431 for (tot=n=0, rad=1; rad < 3 && n==0; rad++)
3432 for (r = row-rad; r <= row+rad; r++)
3433 for (c = col-rad; c <= col+rad; c++)
3434 if ((unsigned) r < height && (unsigned) c < width &&
3435 (r != row || c != col) && fc(r,c) == fc(row,col)) {
3436 tot += BAYER2(r,c);
3437 n++;
3438 }
3439 BAYER2(row,col) = tot/n;
3440 if (!fixed++) dcraw_message(DCRAW_VERBOSE,_("Fixed dead pixels at:"));
3441 dcraw_message(DCRAW_VERBOSE, " %d,%d", col, row);
3442 }
3443 if (fixed) dcraw_message(DCRAW_VERBOSE, "\n");
3444 fclose (fp);
3445 }
3446
subtract(const char * fname)3447 void CLASS subtract (const char *fname)
3448 {
3449 FILE *fp;
3450 int dim[3]={0,0,0}, comment=0, number=0, error=0, nd=0, c, row, col;
3451 ushort *pixel;
3452
3453 if (!(fp = fopen (fname, "rb"))) {
3454 perror (fname); return;
3455 }
3456 if (fgetc(fp) != 'P' || fgetc(fp) != '5') error = 1;
3457 while (!error && nd < 3 && (c = fgetc(fp)) != EOF) {
3458 if (c == '#') comment = 1;
3459 if (c == '\n') comment = 0;
3460 if (comment) continue;
3461 if (isdigit(c)) number = 1;
3462 if (number) {
3463 if (isdigit(c)) dim[nd] = dim[nd]*10 + c -'0';
3464 else if (isspace(c)) {
3465 number = 0; nd++;
3466 } else error = 1;
3467 }
3468 }
3469 if (error || nd < 3) {
3470 dcraw_message (DCRAW_ERROR,_("%s is not a valid PGM file!\n"), fname);
3471 fclose (fp); return;
3472 } else if (dim[0] != width || dim[1] != height || dim[2] != 65535) {
3473 dcraw_message (DCRAW_ERROR,_("%s has the wrong dimensions!\n"), fname);
3474 fclose (fp); return;
3475 }
3476 pixel = (ushort *) calloc (width, sizeof *pixel);
3477 merror (pixel, "subtract()");
3478 for (row=0; row < height; row++) {
3479 fread (pixel, 2, width, fp);
3480 for (col=0; col < width; col++)
3481 BAYER(row,col) = MAX (BAYER(row,col) - ntohs(pixel[col]), 0);
3482 }
3483 free (pixel);
3484 fclose (fp);
3485 memset (cblack, 0, sizeof cblack);
3486 black = 0;
3487 }
3488
gamma_curve(double pwr,double ts,int mode,int imax)3489 void CLASS gamma_curve (double pwr, double ts, int mode, int imax)
3490 {
3491 int i;
3492 double g[6], bnd[2]={0,0}, r;
3493
3494 g[0] = pwr;
3495 g[1] = ts;
3496 g[2] = g[3] = g[4] = 0;
3497 bnd[g[1] >= 1] = 1;
3498 if (g[1] && (g[1]-1)*(g[0]-1) <= 0) {
3499 for (i=0; i < 48; i++) {
3500 g[2] = (bnd[0] + bnd[1])/2;
3501 if (g[0]) bnd[(pow(g[2]/g[1],-g[0]) - 1)/g[0] - 1/g[2] > -1] = g[2];
3502 else bnd[g[2]/exp(1-1/g[2]) < g[1]] = g[2];
3503 }
3504 g[3] = g[2] / g[1];
3505 if (g[0]) g[4] = g[2] * (1/g[0] - 1);
3506 }
3507 if (g[0]) g[5] = 1 / (g[1]*SQR(g[3])/2 - g[4]*(1 - g[3]) +
3508 (1 - pow(g[3],1+g[0]))*(1 + g[4])/(1 + g[0])) - 1;
3509 else g[5] = 1 / (g[1]*SQR(g[3])/2 + 1
3510 - g[2] - g[3] - g[2]*g[3]*(log(g[3]) - 1)) - 1;
3511 if (!mode--) {
3512 memcpy (gamm, g, sizeof gamm);
3513 return;
3514 }
3515 for (i=0; i < 0x10000; i++) {
3516 curve[i] = 0xffff;
3517 if ((r = (double) i / imax) < 1)
3518 curve[i] = 0x10000 * ( mode
3519 ? (r < g[3] ? r*g[1] : (g[0] ? pow( r,g[0])*(1+g[4])-g[4] : log(r)*g[2]+1))
3520 : (r < g[2] ? r/g[1] : (g[0] ? pow((r+g[4])/(1+g[4]),1/g[0]) : exp((r-1)/g[2]))));
3521 }
3522 }
3523
pseudoinverse(double (* in)[3],double (* out)[3],int size)3524 void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
3525 {
3526 double work[3][6], num;
3527 int i, j, k;
3528
3529 for (i=0; i < 3; i++) {
3530 for (j=0; j < 6; j++)
3531 work[i][j] = j == i+3;
3532 for (j=0; j < 3; j++)
3533 for (k=0; k < size; k++)
3534 work[i][j] += in[k][i] * in[k][j];
3535 }
3536 for (i=0; i < 3; i++) {
3537 num = work[i][i];
3538 for (j=0; j < 6; j++)
3539 work[i][j] /= num;
3540 for (k=0; k < 3; k++) {
3541 if (k==i) continue;
3542 num = work[k][i];
3543 for (j=0; j < 6; j++)
3544 work[k][j] -= work[i][j] * num;
3545 }
3546 }
3547 for (i=0; i < size; i++)
3548 for (j=0; j < 3; j++)
3549 for (out[i][j]=k=0; k < 3; k++)
3550 out[i][j] += work[j][k+3] * in[i][k];
3551 }
3552
cam_xyz_coeff(double cam_xyz[4][3])3553 void CLASS cam_xyz_coeff (double cam_xyz[4][3])
3554 {
3555 double cam_rgb[4][3], inverse[4][3], num;
3556 int i, j, k;
3557
3558 for (i=0; i < colors; i++) /* Multiply out XYZ colorspace */
3559 for (j=0; j < 3; j++)
3560 for (cam_rgb[i][j] = k=0; k < 3; k++)
3561 cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
3562
3563 for (i=0; i < colors; i++) { /* Normalize cam_rgb so that */
3564 for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
3565 num += cam_rgb[i][j];
3566 for (j=0; j < 3; j++)
3567 cam_rgb[i][j] /= num;
3568 pre_mul[i] = 1 / num;
3569 }
3570 pseudoinverse (cam_rgb, inverse, colors);
3571 for (raw_color = i=0; i < 3; i++)
3572 for (j=0; j < colors; j++)
3573 rgb_cam[i][j] = inverse[j][i];
3574 }
3575
3576 #ifdef COLORCHECK
colorcheck()3577 void CLASS colorcheck()
3578 {
3579 #define NSQ 24
3580 // Coordinates of the GretagMacbeth ColorChecker squares
3581 // width, height, 1st_column, 1st_row
3582 int cut[NSQ][4]; // you must set these
3583 // ColorChecker Chart under 6500-kelvin illumination
3584 static const double gmb_xyY[NSQ][3] = {
3585 { 0.400, 0.350, 10.1 }, // Dark Skin
3586 { 0.377, 0.345, 35.8 }, // Light Skin
3587 { 0.247, 0.251, 19.3 }, // Blue Sky
3588 { 0.337, 0.422, 13.3 }, // Foliage
3589 { 0.265, 0.240, 24.3 }, // Blue Flower
3590 { 0.261, 0.343, 43.1 }, // Bluish Green
3591 { 0.506, 0.407, 30.1 }, // Orange
3592 { 0.211, 0.175, 12.0 }, // Purplish Blue
3593 { 0.453, 0.306, 19.8 }, // Moderate Red
3594 { 0.285, 0.202, 6.6 }, // Purple
3595 { 0.380, 0.489, 44.3 }, // Yellow Green
3596 { 0.473, 0.438, 43.1 }, // Orange Yellow
3597 { 0.187, 0.129, 6.1 }, // Blue
3598 { 0.305, 0.478, 23.4 }, // Green
3599 { 0.539, 0.313, 12.0 }, // Red
3600 { 0.448, 0.470, 59.1 }, // Yellow
3601 { 0.364, 0.233, 19.8 }, // Magenta
3602 { 0.196, 0.252, 19.8 }, // Cyan
3603 { 0.310, 0.316, 90.0 }, // White
3604 { 0.310, 0.316, 59.1 }, // Neutral 8
3605 { 0.310, 0.316, 36.2 }, // Neutral 6.5
3606 { 0.310, 0.316, 19.8 }, // Neutral 5
3607 { 0.310, 0.316, 9.0 }, // Neutral 3.5
3608 { 0.310, 0.316, 3.1 } }; // Black
3609 double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
3610 double inverse[NSQ][3], cam_xyz[4][3], num;
3611 int c, i, j, k, sq, row, col, count[4];
3612
3613 memset (gmb_cam, 0, sizeof gmb_cam);
3614 for (sq=0; sq < NSQ; sq++) {
3615 FORCC count[c] = 0;
3616 for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
3617 for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
3618 c = FC(row,col);
3619 if (c >= colors) c -= 2;
3620 gmb_cam[sq][c] += BAYER(row,col);
3621 count[c]++;
3622 }
3623 FORCC gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - black;
3624 gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
3625 gmb_xyz[sq][1] = gmb_xyY[sq][2];
3626 gmb_xyz[sq][2] = gmb_xyY[sq][2] *
3627 (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
3628 }
3629 pseudoinverse (gmb_xyz, inverse, NSQ);
3630 for (i=0; i < colors; i++)
3631 for (j=0; j < 3; j++)
3632 for (cam_xyz[i][j] = k=0; k < NSQ; k++)
3633 cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
3634 cam_xyz_coeff (cam_xyz);
3635 if (verbose) {
3636 dcraw_message (DCRAW_VERBOSE, " { \"%s %s\", %d,\n\t{", make, model, black);
3637 num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
3638 FORCC for (j=0; j < 3; j++)
3639 dcraw_message (DCRAW_VERBOSE, "%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
3640 dcraw_message (DCRAW_VERBOSE, " } },\n");
3641 }
3642 #undef NSQ
3643 }
3644 #endif
3645
3646 /* Start of functions copied to dcraw_indi.c (UF) */
hat_transform(float * temp,float * base,int st,int size,int sc)3647 void CLASS hat_transform (float *temp, float *base, int st, int size, int sc)
3648 {
3649 int i;
3650 for (i=0; i < sc; i++)
3651 temp[i] = 2*base[st*i] + base[st*(sc-i)] + base[st*(i+sc)];
3652 for (; i+sc < size; i++)
3653 temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(i+sc)];
3654 for (; i < size; i++)
3655 temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(2*size-2-(i+sc))];
3656 }
3657
wavelet_denoise()3658 void CLASS wavelet_denoise()
3659 {
3660 float *fimg=0, *temp, thold, mul[2], avg, diff;
3661 int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
3662 ushort *window[4];
3663 static const float noise[] =
3664 { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
3665
3666 dcraw_message (DCRAW_VERBOSE,_("Wavelet denoising...\n"));
3667
3668 while (maximum << scale < 0x10000) scale++;
3669 maximum <<= --scale;
3670 black <<= scale;
3671 FORC4 cblack[c] <<= scale;
3672 if ((size = iheight*iwidth) < 0x15550000)
3673 fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
3674 merror (fimg, "wavelet_denoise()");
3675 temp = fimg + size*3;
3676 if ((nc = colors) == 3 && filters) nc++;
3677 FORC(nc) { /* denoise R,G1,B,G3 individually */
3678 for (i=0; i < size; i++)
3679 fimg[i] = 256 * sqrt((float) (image[i][c] << scale));
3680 for (hpass=lev=0; lev < 5; lev++) {
3681 lpass = size*((lev & 1)+1);
3682 for (row=0; row < iheight; row++) {
3683 hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
3684 for (col=0; col < iwidth; col++)
3685 fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
3686 }
3687 for (col=0; col < iwidth; col++) {
3688 hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
3689 for (row=0; row < iheight; row++)
3690 fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
3691 }
3692 thold = threshold * noise[lev];
3693 for (i=0; i < size; i++) {
3694 fimg[hpass+i] -= fimg[lpass+i];
3695 if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
3696 else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
3697 else fimg[hpass+i] = 0;
3698 if (hpass) fimg[i] += fimg[hpass+i];
3699 }
3700 hpass = lpass;
3701 }
3702 for (i=0; i < size; i++)
3703 image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
3704 }
3705 if (filters && colors == 3) { /* pull G1 and G3 closer together */
3706 for (row=0; row < 2; row++) {
3707 mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
3708 blk[row] = cblack[FC(row,0) | 1];
3709 }
3710 for (i=0; i < 4; i++)
3711 window[i] = (ushort *) fimg + width*i;
3712 for (wlast=-1, row=1; row < height-1; row++) {
3713 while (wlast < row+1) {
3714 for (wlast++, i=0; i < 4; i++)
3715 window[(i+3) & 3] = window[i];
3716 for (col = FC(wlast,1) & 1; col < width; col+=2)
3717 window[2][col] = BAYER(wlast,col);
3718 }
3719 thold = threshold/512;
3720 for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
3721 avg = ( window[0][col-1] + window[0][col+1] +
3722 window[2][col-1] + window[2][col+1] - blk[~row & 1]*4 )
3723 * mul[row & 1] + (window[1][col] + blk[row & 1]) * 0.5;
3724 avg = avg < 0 ? 0 : sqrt(avg);
3725 diff = sqrt((float) BAYER(row,col)) - avg;
3726 if (diff < -thold) diff += thold;
3727 else if (diff > thold) diff -= thold;
3728 else diff = 0;
3729 BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
3730 }
3731 }
3732 }
3733 free (fimg);
3734 }
3735
scale_colors()3736 void CLASS scale_colors()
3737 {
3738 unsigned bottom, right, size, row, col, ur, uc, i, x, y, c, sum[8];
3739 int val, dark, sat;
3740 double dsum[8], dmin, dmax;
3741 float scale_mul[4], fr, fc;
3742 ushort *img=0, *pix;
3743
3744 FORC4 cblack[c] += black;
3745 if (user_mul[0])
3746 memcpy (pre_mul, user_mul, sizeof pre_mul);
3747 if (use_auto_wb || (use_camera_wb && cam_mul[0] == -1)) {
3748 memset (dsum, 0, sizeof dsum);
3749 bottom = MIN (greybox[1]+greybox[3], height);
3750 right = MIN (greybox[0]+greybox[2], width);
3751 for (row=greybox[1]; row < bottom; row += 8)
3752 for (col=greybox[0]; col < right; col += 8) {
3753 memset (sum, 0, sizeof sum);
3754 for (y=row; y < row+8 && y < bottom; y++)
3755 for (x=col; x < col+8 && x < right; x++)
3756 FORC4 {
3757 if (filters) {
3758 c = FC(y,x);
3759 val = BAYER(y,x);
3760 } else
3761 val = image[y*width+x][c];
3762 if (val > (int)(maximum-25)) goto skip_block;
3763 if ((val -= cblack[c]) < 0) val = 0;
3764 sum[c] += val;
3765 sum[c+4]++;
3766 if (filters) break;
3767 }
3768 FORC(8) dsum[c] += sum[c];
3769 skip_block: ;
3770 }
3771 FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
3772 }
3773 if (use_camera_wb && cam_mul[0] != -1) {
3774 memset (sum, 0, sizeof sum);
3775 for (row=0; row < 8; row++)
3776 for (col=0; col < 8; col++) {
3777 c = FC(row,col);
3778 if ((val = white[row][col] - cblack[c]) > 0)
3779 sum[c] += val;
3780 sum[c+4]++;
3781 }
3782 if (sum[0] && sum[1] && sum[2] && sum[3])
3783 FORC4 pre_mul[c] = (float) sum[c+4] / sum[c];
3784 else if (cam_mul[0] && cam_mul[2])
3785 memcpy (pre_mul, cam_mul, sizeof pre_mul);
3786 else
3787 dcraw_message (DCRAW_NO_CAMERA_WB,_("%s: Cannot use camera white balance.\n"), ifname_display);
3788 }
3789 if (pre_mul[3] == 0) pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
3790 dark = black;
3791 sat = maximum;
3792 if (threshold) wavelet_denoise();
3793 maximum -= black;
3794 for (dmin=DBL_MAX, dmax=c=0; c < 4; c++) {
3795 if (dmin > pre_mul[c])
3796 dmin = pre_mul[c];
3797 if (dmax < pre_mul[c])
3798 dmax = pre_mul[c];
3799 }
3800 if (!highlight) dmax = dmin;
3801 FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
3802 dcraw_message(DCRAW_VERBOSE,
3803 _("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark, sat);
3804 FORC4 dcraw_message(DCRAW_VERBOSE, " %f", pre_mul[c]);
3805 dcraw_message(DCRAW_VERBOSE, "\n");
3806 size = iheight*iwidth;
3807 for (i=0; i < size*4; i++) {
3808 val = image[0][i];
3809 if (!val) continue;
3810 val -= cblack[i & 3];
3811 val *= scale_mul[i & 3];
3812 image[0][i] = CLIP(val);
3813 }
3814 if ((aber[0] != 1 || aber[2] != 1) && colors == 3) {
3815 dcraw_message (DCRAW_VERBOSE,_("Correcting chromatic aberration...\n"));
3816 for (c=0; c < 4; c+=2) {
3817 if (aber[c] == 1) continue;
3818 img = (ushort *) malloc (size * sizeof *img);
3819 merror (img, "scale_colors()");
3820 for (i=0; i < size; i++)
3821 img[i] = image[i][c];
3822 for (row=0; row < iheight; row++) {
3823 ur = fr = (row - iheight*0.5) * aber[c] + iheight*0.5;
3824 if (ur > (unsigned)(iheight-2)) continue;
3825 fr -= ur;
3826 for (col=0; col < iwidth; col++) {
3827 uc = fc = (col - iwidth*0.5) * aber[c] + iwidth*0.5;
3828 if (uc > (unsigned)(iwidth-2)) continue;
3829 fc -= uc;
3830 pix = img + ur*iwidth + uc;
3831 image[row*iwidth+col][c] =
3832 (pix[ 0]*(1-fc) + pix[ 1]*fc) * (1-fr) +
3833 (pix[iwidth]*(1-fc) + pix[iwidth+1]*fc) * fr;
3834 }
3835 }
3836 free(img);
3837 }
3838 }
3839 }
3840 /* End of functions copied to dcraw_indi.c (UF) */
3841
pre_interpolate()3842 void CLASS pre_interpolate()
3843 {
3844 ushort (*img)[4];
3845 int row, col, c;
3846
3847 if (shrink) {
3848 if (half_size) {
3849 height = iheight;
3850 width = iwidth;
3851 } else {
3852 img = (ushort (*)[4]) calloc (height*width, sizeof *img);
3853 merror (img, "pre_interpolate()");
3854 for (row=0; row < height; row++)
3855 for (col=0; col < width; col++) {
3856 c = fc(row,col);
3857 img[row*width+col][c] = image[(row >> 1)*iwidth+(col >> 1)][c];
3858 }
3859 free (image);
3860 image = img;
3861 shrink = 0;
3862 }
3863 }
3864 if (filters && colors == 3) {
3865 if ((mix_green = four_color_rgb)) colors++;
3866 else {
3867 for (row = FC(1,0) >> 1; row < height; row+=2)
3868 for (col = FC(row,1) & 1; col < width; col+=2)
3869 image[row*width+col][1] = image[row*width+col][3];
3870 filters &= ~((filters & 0x55555555) << 1);
3871 }
3872 }
3873 if (half_size) filters = 0;
3874 }
3875
3876 /* Start of functions copied to dcraw_indi.c (UF) */
border_interpolate(int border)3877 void CLASS border_interpolate (int border)
3878 {
3879 int row, col, y, x, f, c, sum[8];
3880
3881 for (row=0; row < height; row++)
3882 for (col=0; col < width; col++) {
3883 if (col==border && row >= border && row < height-border)
3884 col = width-border;
3885 memset (sum, 0, sizeof sum);
3886 for (y=row-1; y != row+2; y++)
3887 for (x=col-1; x != col+2; x++)
3888 if (y >= 0 && y < height && x >= 0 && x < width) {
3889 f = fc(y,x);
3890 sum[f] += image[y*width+x][f];
3891 sum[f+4]++;
3892 }
3893 f = fc(row,col);
3894 FORCC if (c != f && sum[c+4])
3895 image[row*width+col][c] = sum[c] / sum[c+4];
3896 }
3897 }
3898
lin_interpolate()3899 void CLASS lin_interpolate()
3900 {
3901 int code[16][16][32], *ip, sum[4];
3902 int c, i, x, y, row, col, shift, color;
3903 ushort *pix;
3904
3905 dcraw_message (DCRAW_VERBOSE,_("Bilinear interpolation...\n"));
3906
3907 border_interpolate(1);
3908 for (row=0; row < 16; row++)
3909 for (col=0; col < 16; col++) {
3910 ip = code[row][col];
3911 memset (sum, 0, sizeof sum);
3912 for (y=-1; y <= 1; y++)
3913 for (x=-1; x <= 1; x++) {
3914 shift = (y==0) + (x==0);
3915 if (shift == 2) continue;
3916 color = fc(row+y,col+x);
3917 *ip++ = (width*y + x)*4 + color;
3918 *ip++ = shift;
3919 *ip++ = color;
3920 sum[color] += 1 << shift;
3921 }
3922 FORCC
3923 if (c != fc(row,col)) {
3924 *ip++ = c;
3925 *ip++ = 256 / sum[c];
3926 }
3927 }
3928 for (row=1; row < height-1; row++)
3929 for (col=1; col < width-1; col++) {
3930 pix = image[row*width+col];
3931 ip = code[row & 15][col & 15];
3932 memset (sum, 0, sizeof sum);
3933 for (i=8; i--; ip+=3)
3934 sum[ip[2]] += pix[ip[0]] << ip[1];
3935 for (i=colors; --i; ip+=2)
3936 pix[ip[0]] = sum[ip[0]] * ip[1] >> 8;
3937 }
3938 }
3939
3940 /*
3941 This algorithm is officially called:
3942
3943 "Interpolation using a Threshold-based variable number of gradients"
3944
3945 described in http://scien.stanford.edu/pages/labsite/1999/psych221/projects/99/tingchen/algodep/vargra.html
3946
3947 I've extended the basic idea to work with non-Bayer filter arrays.
3948 Gradients are numbered clockwise from NW=0 to W=7.
3949 */
vng_interpolate()3950 void CLASS vng_interpolate()
3951 {
3952 static const signed char *cp, terms[] = {
3953 -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
3954 -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
3955 -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
3956 -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
3957 -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
3958 -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
3959 -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
3960 -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
3961 -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
3962 -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
3963 -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
3964 -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
3965 -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
3966 +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
3967 +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
3968 +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
3969 +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
3970 +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
3971 +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
3972 +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
3973 +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
3974 +1,+0,+2,+1,0,0x10
3975 }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
3976 ushort (*brow[5])[4], *pix;
3977 int prow=7, pcol=1, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
3978 int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
3979 int g, diff, thold, num, c;
3980
3981 lin_interpolate();
3982 dcraw_message (DCRAW_VERBOSE,_("VNG interpolation...\n"));
3983
3984 if (filters == 1) prow = pcol = 15;
3985 ip = (int *) calloc ((prow+1)*(pcol+1), 1280);
3986 merror (ip, "vng_interpolate()");
3987 for (row=0; row <= prow; row++) /* Precalculate for VNG */
3988 for (col=0; col <= pcol; col++) {
3989 code[row][col] = ip;
3990 for (cp=terms, t=0; t < 64; t++) {
3991 y1 = *cp++; x1 = *cp++;
3992 y2 = *cp++; x2 = *cp++;
3993 weight = *cp++;
3994 grads = *cp++;
3995 color = fc(row+y1,col+x1);
3996 if (fc(row+y2,col+x2) != color) continue;
3997 diag = (fc(row,col+1) == color && fc(row+1,col) == color) ? 2:1;
3998 if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
3999 *ip++ = (y1*width + x1)*4 + color;
4000 *ip++ = (y2*width + x2)*4 + color;
4001 *ip++ = weight;
4002 for (g=0; g < 8; g++)
4003 if (grads & 1<<g) *ip++ = g;
4004 *ip++ = -1;
4005 }
4006 *ip++ = INT_MAX;
4007 for (cp=chood, g=0; g < 8; g++) {
4008 y = *cp++; x = *cp++;
4009 *ip++ = (y*width + x) * 4;
4010 color = fc(row,col);
4011 if (fc(row+y,col+x) != color && fc(row+y*2,col+x*2) == color)
4012 *ip++ = (y*width + x) * 8 + color;
4013 else
4014 *ip++ = 0;
4015 }
4016 }
4017 brow[4] = (ushort (*)[4]) calloc (width*3, sizeof **brow);
4018 merror (brow[4], "vng_interpolate()");
4019 for (row=0; row < 3; row++)
4020 brow[row] = brow[4] + row*width;
4021 for (row=2; row < height-2; row++) { /* Do VNG interpolation */
4022 for (col=2; col < width-2; col++) {
4023 pix = image[row*width+col];
4024 ip = code[row & prow][col & pcol];
4025 memset (gval, 0, sizeof gval);
4026 while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
4027 diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
4028 gval[ip[3]] += diff;
4029 ip += 5;
4030 if ((g = ip[-1]) == -1) continue;
4031 gval[g] += diff;
4032 while ((g = *ip++) != -1)
4033 gval[g] += diff;
4034 }
4035 ip++;
4036 gmin = gmax = gval[0]; /* Choose a threshold */
4037 for (g=1; g < 8; g++) {
4038 if (gmin > gval[g]) gmin = gval[g];
4039 if (gmax < gval[g]) gmax = gval[g];
4040 }
4041 if (gmax == 0) {
4042 memcpy (brow[2][col], pix, sizeof *image);
4043 continue;
4044 }
4045 thold = gmin + (gmax >> 1);
4046 memset (sum, 0, sizeof sum);
4047 color = fc(row,col);
4048 for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
4049 if (gval[g] <= thold) {
4050 FORCC
4051 if (c == color && ip[1])
4052 sum[c] += (pix[c] + pix[ip[1]]) >> 1;
4053 else
4054 sum[c] += pix[ip[0] + c];
4055 num++;
4056 }
4057 }
4058 FORCC { /* Save to buffer */
4059 t = pix[color];
4060 if (c != color)
4061 t += (sum[c] - sum[color]) / num;
4062 brow[2][col][c] = CLIP(t);
4063 }
4064 }
4065 if (row > 3) /* Write buffer to image */
4066 memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
4067 for (g=0; g < 4; g++)
4068 brow[(g-1) & 3] = brow[g];
4069 }
4070 memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
4071 memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image);
4072 free (brow[4]);
4073 free (code[0][0]);
4074 }
4075
4076 /*
4077 Patterned Pixel Grouping Interpolation by Alain Desbiolles
4078 */
ppg_interpolate()4079 void CLASS ppg_interpolate()
4080 {
4081 int dir[5] = { 1, width, -1, -width, 1 };
4082 int row, col, diff[2], guess[2], c, d, i;
4083 ushort (*pix)[4];
4084
4085 border_interpolate(3);
4086 dcraw_message (DCRAW_VERBOSE,_("PPG interpolation...\n"));
4087
4088 /* Fill in the green layer with gradients and pattern recognition: */
4089 for (row=3; row < height-3; row++)
4090 for (col=3+(FC(row,3) & 1), c=FC(row,col); col < width-3; col+=2) {
4091 pix = image + row*width+col;
4092 for (i=0; (d=dir[i]) > 0; i++) {
4093 guess[i] = (pix[-d][1] + pix[0][c] + pix[d][1]) * 2
4094 - pix[-2*d][c] - pix[2*d][c];
4095 diff[i] = ( ABS(pix[-2*d][c] - pix[ 0][c]) +
4096 ABS(pix[ 2*d][c] - pix[ 0][c]) +
4097 ABS(pix[ -d][1] - pix[ d][1]) ) * 3 +
4098 ( ABS(pix[ 3*d][1] - pix[ d][1]) +
4099 ABS(pix[-3*d][1] - pix[-d][1]) ) * 2;
4100 }
4101 d = dir[i = diff[0] > diff[1]];
4102 pix[0][1] = ULIM(guess[i] >> 2, pix[d][1], pix[-d][1]);
4103 }
4104 /* Calculate red and blue for each green pixel: */
4105 for (row=1; row < height-1; row++)
4106 for (col=1+(FC(row,2) & 1), c=FC(row,col+1); col < width-1; col+=2) {
4107 pix = image + row*width+col;
4108 for (i=0; (d=dir[i]) > 0; c=2-c, i++)
4109 pix[0][c] = CLIP((pix[-d][c] + pix[d][c] + 2*pix[0][1]
4110 - pix[-d][1] - pix[d][1]) >> 1);
4111 }
4112 /* Calculate blue for red pixels and vice versa: */
4113 for (row=1; row < height-1; row++)
4114 for (col=1+(FC(row,1) & 1), c=2-FC(row,col); col < width-1; col+=2) {
4115 pix = image + row*width+col;
4116 for (i=0; (d=dir[i]+dir[i+1]) > 0; i++) {
4117 diff[i] = ABS(pix[-d][c] - pix[d][c]) +
4118 ABS(pix[-d][1] - pix[0][1]) +
4119 ABS(pix[ d][1] - pix[0][1]);
4120 guess[i] = pix[-d][c] + pix[d][c] + 2*pix[0][1]
4121 - pix[-d][1] - pix[d][1];
4122 }
4123 if (diff[0] != diff[1])
4124 pix[0][c] = CLIP(guess[diff[0] > diff[1]] >> 1);
4125 else
4126 pix[0][c] = CLIP((guess[0]+guess[1]) >> 2);
4127 }
4128 }
4129
4130 /*
4131 Adaptive Homogeneity-Directed interpolation is based on
4132 the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
4133 */
4134 #define TS 256 /* Tile Size */
4135
ahd_interpolate()4136 void CLASS ahd_interpolate()
4137 {
4138 int i, j, k, top, left, row, col, tr, tc, c, d, val, hm[2];
4139 ushort (*pix)[4], (*rix)[3];
4140 static const int dir[4] = { -1, 1, -TS, TS };
4141 unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
4142 float r, cbrt[0x10000], xyz[3], xyz_cam[3][4];
4143 ushort (*rgb)[TS][TS][3];
4144 short (*lab)[TS][TS][3], (*lix)[3];
4145 char (*homo)[TS][TS], *buffer;
4146
4147 dcraw_message (DCRAW_VERBOSE,_("AHD interpolation...\n"));
4148
4149 for (i=0; i < 0x10000; i++) {
4150 r = i / 65535.0;
4151 cbrt[i] = r > 0.008856 ? pow(r, (float) (1/3.0)) : 7.787*r + 16/116.0;
4152 }
4153 for (i=0; i < 3; i++)
4154 for (j=0; j < colors; j++)
4155 for (xyz_cam[i][j] = k=0; k < 3; k++)
4156 xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
4157
4158 border_interpolate(5);
4159 buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
4160 merror (buffer, "ahd_interpolate()");
4161 rgb = (ushort(*)[TS][TS][3]) buffer;
4162 lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
4163 homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
4164
4165 for (top=2; top < height-5; top += TS-6)
4166 for (left=2; left < width-5; left += TS-6) {
4167
4168 /* Interpolate green horizontally and vertically: */
4169 for (row = top; row < top+TS && row < height-2; row++) {
4170 col = left + (FC(row,left) & 1);
4171 for (c = FC(row,col); col < left+TS && col < width-2; col+=2) {
4172 pix = image + row*width+col;
4173 val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
4174 - pix[-2][c] - pix[2][c]) >> 2;
4175 rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
4176 val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
4177 - pix[-2*width][c] - pix[2*width][c]) >> 2;
4178 rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
4179 }
4180 }
4181 /* Interpolate red and blue, and convert to CIELab: */
4182 for (d=0; d < 2; d++)
4183 for (row=top+1; row < top+TS-1 && row < height-3; row++)
4184 for (col=left+1; col < left+TS-1 && col < width-3; col++) {
4185 pix = image + row*width+col;
4186 rix = &rgb[d][row-top][col-left];
4187 lix = &lab[d][row-top][col-left];
4188 if ((c = 2 - FC(row,col)) == 1) {
4189 c = FC(row+1,col);
4190 val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
4191 - rix[-1][1] - rix[1][1] ) >> 1);
4192 rix[0][2-c] = CLIP(val);
4193 val = pix[0][1] + (( pix[-width][c] + pix[width][c]
4194 - rix[-TS][1] - rix[TS][1] ) >> 1);
4195 } else
4196 val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
4197 + pix[+width-1][c] + pix[+width+1][c]
4198 - rix[-TS-1][1] - rix[-TS+1][1]
4199 - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
4200 rix[0][c] = CLIP(val);
4201 c = FC(row,col);
4202 rix[0][c] = pix[0][c];
4203 xyz[0] = xyz[1] = xyz[2] = 0.5;
4204 FORCC {
4205 xyz[0] += xyz_cam[0][c] * rix[0][c];
4206 xyz[1] += xyz_cam[1][c] * rix[0][c];
4207 xyz[2] += xyz_cam[2][c] * rix[0][c];
4208 }
4209 xyz[0] = cbrt[CLIP((int) xyz[0])];
4210 xyz[1] = cbrt[CLIP((int) xyz[1])];
4211 xyz[2] = cbrt[CLIP((int) xyz[2])];
4212 lix[0][0] = 64 * (116 * xyz[1] - 16);
4213 lix[0][1] = 64 * 500 * (xyz[0] - xyz[1]);
4214 lix[0][2] = 64 * 200 * (xyz[1] - xyz[2]);
4215 }
4216 /* Build homogeneity maps from the CIELab images: */
4217 memset (homo, 0, 2*TS*TS);
4218 for (row=top+2; row < top+TS-2 && row < height-4; row++) {
4219 tr = row-top;
4220 for (col=left+2; col < left+TS-2 && col < width-4; col++) {
4221 tc = col-left;
4222 for (d=0; d < 2; d++) {
4223 lix = &lab[d][tr][tc];
4224 for (i=0; i < 4; i++) {
4225 ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
4226 abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
4227 + SQR(lix[0][2]-lix[dir[i]][2]);
4228 }
4229 }
4230 leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
4231 MAX(ldiff[1][2],ldiff[1][3]));
4232 abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
4233 MAX(abdiff[1][2],abdiff[1][3]));
4234 for (d=0; d < 2; d++)
4235 for (i=0; i < 4; i++)
4236 if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
4237 homo[d][tr][tc]++;
4238 }
4239 }
4240 /* Combine the most homogenous pixels for the final result: */
4241 for (row=top+3; row < top+TS-3 && row < height-5; row++) {
4242 tr = row-top;
4243 for (col=left+3; col < left+TS-3 && col < width-5; col++) {
4244 tc = col-left;
4245 for (d=0; d < 2; d++)
4246 for (hm[d]=0, i=tr-1; i <= tr+1; i++)
4247 for (j=tc-1; j <= tc+1; j++)
4248 hm[d] += homo[d][i][j];
4249 if (hm[0] != hm[1])
4250 FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
4251 else
4252 FORC3 image[row*width+col][c] =
4253 (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
4254 }
4255 }
4256 }
4257 free (buffer);
4258 }
4259 #undef TS
4260 /* End of functions copied to dcraw_indi.c (UF) */
4261
median_filter()4262 void CLASS median_filter()
4263 {
4264 ushort (*pix)[4];
4265 int pass, c, i, j, k, med[9];
4266 static const uchar opt[] = /* Optimal 9-element median search */
4267 { 1,2, 4,5, 7,8, 0,1, 3,4, 6,7, 1,2, 4,5, 7,8,
4268 0,3, 5,8, 4,7, 3,6, 1,4, 2,5, 4,7, 4,2, 6,4, 4,2 };
4269
4270 for (pass=1; pass <= med_passes; pass++) {
4271 dcraw_message (DCRAW_VERBOSE,_("Median filter pass %d...\n"), pass);
4272 for (c=0; c < 3; c+=2) {
4273 for (pix = image; pix < image+width*height; pix++)
4274 pix[0][3] = pix[0][c];
4275 for (pix = image+width; pix < image+width*(height-1); pix++) {
4276 if ((pix-image+1) % width < 2) continue;
4277 for (k=0, i = -width; i <= width; i += width)
4278 for (j = i-1; j <= i+1; j++)
4279 med[k++] = pix[j][3] - pix[j][1];
4280 for (i=0; i < (int) sizeof opt; i+=2)
4281 if (med[opt[i]] > med[opt[i+1]])
4282 SWAP (med[opt[i]] , med[opt[i+1]]);
4283 pix[0][c] = CLIP(med[4] + pix[0][1]);
4284 }
4285 }
4286 }
4287 }
4288
blend_highlights()4289 void CLASS blend_highlights()
4290 {
4291 int clip=INT_MAX, row, col, c, i, j;
4292 static const float trans[2][4][4] =
4293 { { { 1,1,1 }, { 1.7320508,-1.7320508,0 }, { -1,-1,2 } },
4294 { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
4295 static const float itrans[2][4][4] =
4296 { { { 1,0.8660254,-0.5 }, { 1,-0.8660254,-0.5 }, { 1,0,1 } },
4297 { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
4298 float cam[2][4], lab[2][4], sum[2], chratio;
4299
4300 if ((unsigned) (colors-3) > 1) return;
4301 dcraw_message (DCRAW_VERBOSE,_("Blending highlights...\n"));
4302 FORCC if (clip > (i = 65535*pre_mul[c])) clip = i;
4303 for (row=0; row < height; row++)
4304 for (col=0; col < width; col++) {
4305 FORCC if (image[row*width+col][c] > clip) break;
4306 if (c == colors) continue;
4307 FORCC {
4308 cam[0][c] = image[row*width+col][c];
4309 cam[1][c] = MIN(cam[0][c],clip);
4310 }
4311 for (i=0; i < 2; i++) {
4312 FORCC for (lab[i][c]=j=0; j < colors; j++)
4313 lab[i][c] += trans[colors-3][c][j] * cam[i][j];
4314 for (sum[i]=0,c=1; c < colors; c++)
4315 sum[i] += SQR(lab[i][c]);
4316 }
4317 chratio = sqrt(sum[1]/sum[0]);
4318 for (c=1; c < colors; c++)
4319 lab[0][c] *= chratio;
4320 FORCC for (cam[0][c]=j=0; j < colors; j++)
4321 cam[0][c] += itrans[colors-3][c][j] * lab[0][j];
4322 FORCC image[row*width+col][c] = cam[0][c] / colors;
4323 }
4324 }
4325
4326 #define SCALE (4 >> shrink)
recover_highlights()4327 void CLASS recover_highlights()
4328 {
4329 float *map, sum, wgt, grow;
4330 int hsat[4], count, spread, change, val, kc, c;
4331 unsigned high, wide, mrow, mcol, row, col, d, y, x, i;
4332 ushort *pixel;
4333 static const signed char dir[8][2] =
4334 { {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
4335
4336 dcraw_message (DCRAW_VERBOSE,_("Rebuilding highlights...\n"));
4337
4338 grow = pow (2.0, 4-highlight);
4339 FORCC hsat[c] = 32000 * pre_mul[c];
4340 for (kc=0, c=1; c < colors; c++)
4341 if (pre_mul[kc] < pre_mul[c]) kc = c;
4342 high = height / SCALE;
4343 wide = width / SCALE;
4344 map = (float *) calloc (high*wide, sizeof *map);
4345 merror (map, "recover_highlights()");
4346 FORCC if (c != kc) {
4347 memset (map, 0, high*wide*sizeof *map);
4348 for (mrow=0; mrow < high; mrow++)
4349 for (mcol=0; mcol < wide; mcol++) {
4350 sum = wgt = count = 0;
4351 for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
4352 for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
4353 pixel = image[row*width+col];
4354 if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000) {
4355 sum += pixel[c];
4356 wgt += pixel[kc];
4357 count++;
4358 }
4359 }
4360 if (count == SCALE*SCALE)
4361 map[mrow*wide+mcol] = sum / wgt;
4362 }
4363 for (spread = 32/grow; spread--; ) {
4364 for (mrow=0; mrow < high; mrow++)
4365 for (mcol=0; mcol < wide; mcol++) {
4366 if (map[mrow*wide+mcol]) continue;
4367 sum = count = 0;
4368 for (d=0; d < 8; d++) {
4369 y = mrow + dir[d][0];
4370 x = mcol + dir[d][1];
4371 if (y < high && x < wide && map[y*wide+x] > 0) {
4372 sum += (1 + (d & 1)) * map[y*wide+x];
4373 count += 1 + (d & 1);
4374 }
4375 }
4376 if (count > 3)
4377 map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
4378 }
4379 for (change=i=0; i < high*wide; i++)
4380 if (map[i] < 0) {
4381 map[i] = -map[i];
4382 change = 1;
4383 }
4384 if (!change) break;
4385 }
4386 for (i=0; i < high*wide; i++)
4387 if (map[i] == 0) map[i] = 1;
4388 for (mrow=0; mrow < high; mrow++)
4389 for (mcol=0; mcol < wide; mcol++) {
4390 for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
4391 for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
4392 pixel = image[row*width+col];
4393 if (pixel[c] / hsat[c] > 1) {
4394 val = pixel[kc] * map[mrow*wide+mcol];
4395 if (pixel[c] < val) pixel[c] = CLIP(val);
4396 }
4397 }
4398 }
4399 }
4400 free (map);
4401 }
4402 #undef SCALE
4403
tiff_get(unsigned base,unsigned * tag,unsigned * type,unsigned * len,unsigned * save)4404 void CLASS tiff_get (unsigned base,
4405 unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
4406 {
4407 *tag = get2();
4408 *type = get2();
4409 *len = get4();
4410 *save = ftell(ifp) + 4;
4411 if (*len * ("11124811248488"[*type < 14 ? *type:0]-'0') > 4)
4412 fseek (ifp, get4()+base, SEEK_SET);
4413 }
4414
parse_thumb_note(int base,unsigned toff,unsigned tlen)4415 void CLASS parse_thumb_note (int base, unsigned toff, unsigned tlen)
4416 {
4417 unsigned entries, tag, type, len, save;
4418
4419 entries = get2();
4420 while (entries--) {
4421 tiff_get (base, &tag, &type, &len, &save);
4422 if (tag == toff) thumb_offset = get4()+base;
4423 if (tag == tlen) thumb_length = get4();
4424 fseek (ifp, save, SEEK_SET);
4425 }
4426 }
4427
4428 //int CLASS parse_tiff_ifd (int base);
4429
parse_makernote(int base,int uptag)4430 void CLASS parse_makernote (int base, int uptag)
4431 {
4432 static const uchar xlat[2][256] = {
4433 { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
4434 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
4435 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
4436 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
4437 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
4438 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
4439 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
4440 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
4441 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
4442 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
4443 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
4444 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
4445 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
4446 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
4447 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
4448 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
4449 { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
4450 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
4451 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
4452 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
4453 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
4454 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
4455 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
4456 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
4457 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
4458 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
4459 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
4460 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
4461 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
4462 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
4463 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
4464 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
4465 int c, i;
4466 unsigned offset=0, entries, tag, type, len, save;
4467 unsigned ver97=0, serial=0, wbi=0, wb[4]={0,0,0,0};
4468 uchar buf97[324], ci, cj, ck;
4469 short sorder=order;
4470 char buf[10];
4471 /*
4472 The MakerNote might have its own TIFF header (possibly with
4473 its own byte-order!), or it might just be a table.
4474 */
4475 fread (buf, 1, 10, ifp);
4476 if (!strncmp (buf,"KDK" ,3) || /* these aren't TIFF tables */
4477 !strncmp (buf,"VER" ,3) ||
4478 !strncmp (buf,"IIII",4) ||
4479 !strncmp (buf,"MMMM",4)) return;
4480 if (!strncmp (buf,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
4481 !strncmp (buf,"MLY" ,3)) { /* Minolta DiMAGE G series */
4482 order = 0x4d4d;
4483 while ((i=ftell(ifp)) < data_offset && i < 16384) {
4484 wb[0] = wb[2]; wb[2] = wb[1]; wb[1] = wb[3];
4485 wb[3] = get2();
4486 if (wb[1] == 256 && wb[3] == 256 &&
4487 wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
4488 FORC4 cam_mul[c] = wb[c];
4489 }
4490 goto quit;
4491 }
4492 if (!strcmp (buf,"Nikon")) {
4493 base = ftell(ifp);
4494 order = get2();
4495 if (get2() != 42) goto quit;
4496 offset = get4();
4497 fseek (ifp, offset-8, SEEK_CUR);
4498 } else if (!strcmp (buf,"OLYMPUS")) {
4499 base = ftell(ifp)-10;
4500 fseek (ifp, -2, SEEK_CUR);
4501 order = get2(); get2();
4502 } else if (!strncmp (buf,"FUJIFILM",8) ||
4503 !strncmp (buf,"SONY",4) ||
4504 !strcmp (buf,"Panasonic")) {
4505 order = 0x4949;
4506 fseek (ifp, 2, SEEK_CUR);
4507 } else if (!strcmp (buf,"OLYMP") ||
4508 !strcmp (buf,"LEICA") ||
4509 !strcmp (buf,"Ricoh") ||
4510 !strcmp (buf,"EPSON"))
4511 fseek (ifp, -2, SEEK_CUR);
4512 else if (!strcmp (buf,"AOC") ||
4513 !strcmp (buf,"QVC"))
4514 fseek (ifp, -4, SEEK_CUR);
4515 else fseek (ifp, -10, SEEK_CUR);
4516
4517 entries = get2();
4518 if (entries > 1000) return;
4519 while (entries--) {
4520 tiff_get (base, &tag, &type, &len, &save);
4521 tag |= uptag << 16;
4522 if (tag == 2 && strstr(make,"NIKON"))
4523 iso_speed = (get2(),get2());
4524 if (tag == 4 && len > 26 && len < 35) {
4525 if ((i=(get4(),get2())) != 0x7fff && !iso_speed)
4526 iso_speed = 50 * pow (2, i/32.0 - 4);
4527 if ((i=(get2(),get2())) != 0x7fff && !aperture)
4528 aperture = pow (2, i/64.0);
4529 if ((i=get2()) != 0xffff && !shutter)
4530 shutter = pow (2, (short) i/-32.0);
4531 wbi = (get2(),get2());
4532 shot_order = (get2(),get2());
4533 }
4534 if ((tag == 4 || tag == 0x114) && !strncmp(make,"KONICA",6)) {
4535 fseek (ifp, tag == 4 ? 140:160, SEEK_CUR);
4536 switch (get2()) {
4537 case 72: flip = 0; break;
4538 case 76: flip = 6; break;
4539 case 82: flip = 5; break;
4540 }
4541 }
4542 if (tag == 7 && type == 2 && len > 20)
4543 fgets (model2, 64, ifp);
4544 if (tag == 8 && type == 4)
4545 shot_order = get4();
4546 if (tag == 9 && !strcmp(make,"Canon"))
4547 fread (artist, 64, 1, ifp);
4548 if (tag == 0xc && len == 4) {
4549 cam_mul[0] = getreal(type);
4550 cam_mul[2] = getreal(type);
4551 }
4552 if (tag == 0xd && type == 7 && get2() == 0xaaaa) {
4553 fread (buf97, 1, sizeof buf97, ifp);
4554 i = (uchar *) memmem (buf97, sizeof buf97,"\xbb\xbb",2) - buf97 + 10;
4555 if (i < 70 && buf97[i] < 3)
4556 flip = "065"[buf97[i]]-'0';
4557 }
4558 if (tag == 0x10 && type == 4)
4559 unique_id = get4();
4560 if (tag == 0x11 && is_raw && !strncmp(make,"NIKON",5)) {
4561 fseek (ifp, get4()+base, SEEK_SET);
4562 parse_tiff_ifd (base);
4563 }
4564 if (tag == 0x14 && len == 2560 && type == 7) {
4565 fseek (ifp, 1248, SEEK_CUR);
4566 goto get2_256;
4567 }
4568 if (tag == 0x15 && type == 2 && is_raw)
4569 fread (model, 64, 1, ifp);
4570 if (strstr(make,"PENTAX")) {
4571 if (tag == 0x1b) tag = 0x1018;
4572 if (tag == 0x1c) tag = 0x1017;
4573 }
4574 if (tag == 0x1d)
4575 while ((c = fgetc(ifp)) && c != EOF)
4576 serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
4577 if (tag == 0x81) { /* NTC UF*/
4578 tone_mode_offset = ftell(ifp);
4579 tone_mode_size = len;
4580 } /* NTC UF*/
4581 if (tag == 0x81 && type == 4) {
4582 data_offset = get4();
4583 fseek (ifp, data_offset + 41, SEEK_SET);
4584 raw_height = get2() * 2;
4585 raw_width = get2();
4586 filters = 0x61616161;
4587 }
4588 if (tag == 0x29 && type == 1) {
4589 c = wbi < 18 ? "012347800000005896"[wbi]-'0' : 0;
4590 fseek (ifp, 8 + c*32, SEEK_CUR);
4591 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get4();
4592 }
4593 if ((tag == 0x81 && type == 7) ||
4594 (tag == 0x100 && type == 7) ||
4595 (tag == 0x280 && type == 1)) {
4596 thumb_offset = ftell(ifp);
4597 thumb_length = len;
4598 }
4599 if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
4600 thumb_offset += base;
4601 if (tag == 0x89 && type == 4)
4602 thumb_length = get4();
4603 if (tag == 0x8c || tag == 0x96)
4604 meta_offset = ftell(ifp);
4605 if (tag == 0x8c) { /* NTC UF*/
4606 tone_curve_offset = ftell(ifp);
4607 tone_curve_size = len;
4608 } /* NTC UF*/
4609 if (tag == 0x97) {
4610 for (i=0; i < 4; i++)
4611 ver97 = ver97 * 10 + fgetc(ifp)-'0';
4612 switch (ver97) {
4613 case 100:
4614 fseek (ifp, 68, SEEK_CUR);
4615 FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
4616 break;
4617 case 102:
4618 fseek (ifp, 6, SEEK_CUR);
4619 goto get2_rggb;
4620 case 103:
4621 fseek (ifp, 16, SEEK_CUR);
4622 FORC4 cam_mul[c] = get2();
4623 }
4624 if (ver97 >= 200) {
4625 if (ver97 != 205) fseek (ifp, 280, SEEK_CUR);
4626 fread (buf97, 324, 1, ifp);
4627 }
4628 }
4629 if (tag == 0xa1 && type == 7) {
4630 type = order;
4631 order = 0x4949;
4632 fseek (ifp, 140, SEEK_CUR);
4633 FORC3 cam_mul[c] = get4();
4634 order = type;
4635 }
4636 if (tag == 0xa4 && type == 3) {
4637 fseek (ifp, wbi*48, SEEK_CUR);
4638 FORC3 cam_mul[c] = get2();
4639 }
4640 if (tag == 0xa7 && (unsigned) (ver97-200) < 12 && !cam_mul[0]) {
4641 ci = xlat[0][serial & 0xff];
4642 cj = xlat[1][fgetc(ifp)^fgetc(ifp)^fgetc(ifp)^fgetc(ifp)];
4643 ck = 0x60;
4644 for (i=0; i < 324; i++)
4645 buf97[i] ^= (cj += ci * ck++);
4646 i = "66666>666;6A"[ver97-200] - '0';
4647 FORC4 cam_mul[c ^ (c >> 1) ^ (i & 1)] =
4648 sget2 (buf97 + (i & -2) + c*2);
4649 }
4650 if (tag == 0x200 && len == 3)
4651 shot_order = (get4(),get4());
4652 if (tag == 0x200 && len == 4)
4653 FORC4 cblack[c ^ c >> 1] = get2();
4654 if (tag == 0x201 && len == 4)
4655 goto get2_rggb;
4656 if (tag == 0x220 && type == 7)
4657 meta_offset = ftell(ifp);
4658 if (tag == 0x401 && type == 4 && len == 4)
4659 FORC4 cblack[c ^ c >> 1] = get4();
4660 if (tag == 0xe01) { /* Nikon Capture Note */
4661 type = order;
4662 order = 0x4949;
4663 fseek (ifp, 22, SEEK_CUR);
4664 for (offset=22; offset+22 < len; offset += 22+i) {
4665 tag = get4();
4666 fseek (ifp, 14, SEEK_CUR);
4667 i = get4()-4;
4668 if (tag == 0x76a43207) flip = get2();
4669 else fseek (ifp, i, SEEK_CUR);
4670 }
4671 order = type;
4672 }
4673 if (tag == 0xe80 && len == 256 && type == 7) {
4674 fseek (ifp, 48, SEEK_CUR);
4675 cam_mul[0] = get2() * 508 * 1.078 / 0x10000;
4676 cam_mul[2] = get2() * 382 * 1.173 / 0x10000;
4677 }
4678 if (tag == 0xf00 && type == 7) {
4679 if (len == 614)
4680 fseek (ifp, 176, SEEK_CUR);
4681 else if (len == 734 || len == 1502)
4682 fseek (ifp, 148, SEEK_CUR);
4683 else goto next;
4684 goto get2_256;
4685 }
4686 if ((tag == 0x1011 && len == 9) || tag == 0x20400200)
4687 for (i=0; i < 3; i++)
4688 FORC3 cmatrix[i][c] = ((short) get2()) / 256.0;
4689 if ((tag == 0x1012 || tag == 0x20400600) && len == 4)
4690 FORC4 cblack[c ^ c >> 1] = get2();
4691 if (tag == 0x1017 || tag == 0x20400100)
4692 cam_mul[0] = get2() / 256.0;
4693 if (tag == 0x1018 || tag == 0x20400100)
4694 cam_mul[2] = get2() / 256.0;
4695 if (tag == 0x2011 && len == 2) {
4696 get2_256:
4697 order = 0x4d4d;
4698 cam_mul[0] = get2() / 256.0;
4699 cam_mul[2] = get2() / 256.0;
4700 }
4701 if ((tag | 0x70) == 0x2070 && type == 4)
4702 fseek (ifp, get4()+base, SEEK_SET);
4703 if (tag == 0x2010 && type != 7)
4704 load_raw = &CLASS olympus_load_raw;
4705 if (tag == 0x2020)
4706 parse_thumb_note (base, 257, 258);
4707 if (tag == 0x2040)
4708 parse_makernote (base, 0x2040);
4709 if (tag == 0xb028) {
4710 fseek (ifp, get4()+base, SEEK_SET);
4711 parse_thumb_note (base, 136, 137);
4712 }
4713 if (tag == 0x4001 && len > 500) {
4714 i = len == 582 ? 50 : len == 653 ? 68 : len == 5120 ? 142 : 126;
4715 fseek (ifp, i, SEEK_CUR);
4716 get2_rggb:
4717 FORC4 cam_mul[c ^ (c >> 1)] = get2();
4718 fseek (ifp, 22, SEEK_CUR);
4719 FORC4 sraw_mul[c ^ (c >> 1)] = get2();
4720 }
4721 next:
4722 fseek (ifp, save, SEEK_SET);
4723 }
4724 quit:
4725 order = sorder;
4726 }
4727
4728 /*
4729 Since the TIFF DateTime string has no timezone information,
4730 assume that the camera's clock was set to Universal Time.
4731 */
get_timestamp(int reversed)4732 void CLASS get_timestamp (int reversed)
4733 {
4734 struct tm t;
4735 char str[20];
4736 int i;
4737
4738 str[19] = 0;
4739 if (reversed)
4740 for (i=19; i--; ) str[i] = fgetc(ifp);
4741 else
4742 fread (str, 19, 1, ifp);
4743 memset (&t, 0, sizeof t);
4744 if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
4745 &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
4746 return;
4747 t.tm_year -= 1900;
4748 t.tm_mon -= 1;
4749 if (mktime(&t) > 0)
4750 timestamp = mktime(&t);
4751 }
4752
parse_exif(int base)4753 void CLASS parse_exif (int base)
4754 {
4755 unsigned kodak, entries, tag, type, len, save, c;
4756 double expo;
4757
4758 kodak = !strncmp(make,"EASTMAN",7) && tiff_nifds < 3;
4759 entries = get2();
4760 while (entries--) {
4761 tiff_get (base, &tag, &type, &len, &save);
4762 switch (tag) {
4763 case 33434: shutter = getreal(type); break;
4764 case 33437: aperture = getreal(type); break;
4765 case 34855: iso_speed = get2(); break;
4766 case 36867:
4767 case 36868: get_timestamp(0); break;
4768 case 37377: if ((expo = -getreal(type)) < 128)
4769 shutter = pow (2, expo); break;
4770 case 37378: aperture = pow (2, getreal(type)/2); break;
4771 case 37386: focal_len = getreal(type); break;
4772 case 37500: parse_makernote (base, 0); break;
4773 case 40962: if (kodak) raw_width = get4(); break;
4774 case 40963: if (kodak) raw_height = get4(); break;
4775 case 41730:
4776 if (get4() == 0x20002)
4777 for (exif_cfa=c=0; c < 8; c+=2)
4778 exif_cfa |= fgetc(ifp) * 0x01010101 << c;
4779 }
4780 fseek (ifp, save, SEEK_SET);
4781 }
4782 }
4783
parse_gps(int base)4784 void CLASS parse_gps (int base)
4785 {
4786 unsigned entries, tag, type, len, save, c;
4787
4788 entries = get2();
4789 while (entries--) {
4790 tiff_get (base, &tag, &type, &len, &save);
4791 switch (tag) {
4792 case 1: case 3: case 5:
4793 gpsdata[29+tag/2] = getc(ifp); break;
4794 case 2: case 4: case 7:
4795 FORC(6) gpsdata[tag/3*6+c] = get4(); break;
4796 case 6:
4797 FORC(2) gpsdata[18+c] = get4(); break;
4798 case 18: case 29:
4799 fgets ((char *) (gpsdata+14+tag/3), MIN(len,12), ifp);
4800 }
4801 fseek (ifp, save, SEEK_SET);
4802 }
4803 }
4804
romm_coeff(float romm_cam[3][3])4805 void CLASS romm_coeff (float romm_cam[3][3])
4806 {
4807 static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
4808 { { 2.034193, -0.727420, -0.306766 },
4809 { -0.228811, 1.231729, -0.002922 },
4810 { -0.008565, -0.153273, 1.161839 } };
4811 int i, j, k;
4812
4813 for (i=0; i < 3; i++)
4814 for (j=0; j < 3; j++)
4815 for (cmatrix[i][j] = k=0; k < 3; k++)
4816 cmatrix[i][j] += rgb_romm[i][k] * romm_cam[k][j];
4817 }
4818
parse_mos(int offset)4819 void CLASS parse_mos (int offset)
4820 {
4821 char data[40];
4822 int skip, from, i, c, neut[4], planes=0, frot=0;
4823 static const char *mod[] =
4824 { "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
4825 "Valeo 11p","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65",
4826 "Aptus 54S","Aptus 65S","Aptus 75S","AFi 5","AFi 6","AFi 7" };
4827 float romm_cam[3][3];
4828
4829 fseek (ifp, offset, SEEK_SET);
4830 while (1) {
4831 if (get4() != 0x504b5453) break;
4832 get4();
4833 fread (data, 1, 40, ifp);
4834 skip = get4();
4835 from = ftell(ifp);
4836 if (!strcmp(data,"JPEG_preview_data")) {
4837 thumb_offset = from;
4838 thumb_length = skip;
4839 }
4840 if (!strcmp(data,"icc_camera_profile")) {
4841 profile_offset = from;
4842 profile_length = skip;
4843 }
4844 if (!strcmp(data,"ShootObj_back_type")) {
4845 fscanf (ifp, "%d", &i);
4846 if ((unsigned) i < sizeof mod / sizeof (*mod))
4847 strcpy (model, mod[i]);
4848 }
4849 if (!strcmp(data,"icc_camera_to_tone_matrix")) {
4850 for (i=0; i < 9; i++)
4851 romm_cam[0][i] = int_to_float(get4());
4852 romm_coeff (romm_cam);
4853 }
4854 if (!strcmp(data,"CaptProf_color_matrix")) {
4855 for (i=0; i < 9; i++)
4856 fscanf (ifp, "%f", &romm_cam[0][i]);
4857 romm_coeff (romm_cam);
4858 }
4859 if (!strcmp(data,"CaptProf_number_of_planes"))
4860 fscanf (ifp, "%d", &planes);
4861 if (!strcmp(data,"CaptProf_raw_data_rotation"))
4862 fscanf (ifp, "%d", &flip);
4863 if (!strcmp(data,"CaptProf_mosaic_pattern"))
4864 FORC4 {
4865 fscanf (ifp, "%d", &i);
4866 if (i == 1) frot = c ^ (c >> 1);
4867 }
4868 if (!strcmp(data,"ImgProf_rotation_angle")) {
4869 fscanf (ifp, "%d", &i);
4870 flip = i - flip;
4871 }
4872 if (!strcmp(data,"NeutObj_neutrals") && !cam_mul[0]) {
4873 FORC4 fscanf (ifp, "%d", neut+c);
4874 FORC3 cam_mul[c] = (float) neut[0] / neut[c+1];
4875 }
4876 parse_mos (from);
4877 fseek (ifp, skip+from, SEEK_SET);
4878 }
4879 if (planes)
4880 filters = (planes == 1) * 0x01010101 *
4881 (uchar) "\x94\x61\x16\x49"[(flip/90 + frot) & 3];
4882 }
4883
linear_table(unsigned len)4884 void CLASS linear_table (unsigned len)
4885 {
4886 int i;
4887 if (len > 0x1000) len = 0x1000;
4888 read_shorts (curve, len);
4889 for (i=len; i < 0x1000; i++)
4890 curve[i] = curve[i-1];
4891 maximum = curve[0xfff];
4892 }
4893
parse_kodak_ifd(int base)4894 void CLASS parse_kodak_ifd (int base)
4895 {
4896 unsigned entries, tag, type, len, save;
4897 int i, c, wbi=-2, wbtemp=6500;
4898 float mul[3]={1,1,1}, num;
4899 static const unsigned wbtag[] = { 64037,64040,64039,64041,-1,-1,64042 };
4900
4901 entries = get2();
4902 if (entries > 1024) return;
4903 while (entries--) {
4904 tiff_get (base, &tag, &type, &len, &save);
4905 if (tag == 1020) wbi = getint(type);
4906 if (tag == 1021 && len == 72) { /* WB set in software */
4907 fseek (ifp, 40, SEEK_CUR);
4908 FORC3 cam_mul[c] = 2048.0 / get2();
4909 wbi = -2;
4910 }
4911 if (tag == 2118) wbtemp = getint(type);
4912 if (tag == (unsigned)(2130 + wbi))
4913 FORC3 mul[c] = getreal(type);
4914 if (tag == (unsigned)(2140 + wbi) && wbi >= 0)
4915 FORC3 {
4916 for (num=i=0; i < 4; i++)
4917 num += getreal(type) * pow (wbtemp/100.0, i);
4918 cam_mul[c] = 2048 / (num * mul[c]);
4919 }
4920 if (tag == 2317) linear_table (len);
4921 if (tag == 6020) iso_speed = getint(type);
4922 if (tag == 64013) wbi = fgetc(ifp);
4923 if ((unsigned) wbi < 7 && tag == wbtag[wbi])
4924 FORC3 cam_mul[c] = get4();
4925 if (tag == 64019) width = getint(type);
4926 if (tag == 64020) height = (getint(type)+1) & -2;
4927 fseek (ifp, save, SEEK_SET);
4928 }
4929 }
4930
4931 //void CLASS parse_minolta (int base);
4932 //int CLASS parse_tiff (int base);
4933
parse_tiff_ifd(int base)4934 int CLASS parse_tiff_ifd (int base)
4935 {
4936 unsigned entries, tag, type, len, plen=16, save;
4937 int ifd, use_cm=0, cfa, i, j, c, ima_len=0;
4938 int blrr=1, blrc=1, dblack[] = { 0,0,0,0 };
4939 char software[64], *cbuf, *cp;
4940 uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
4941 double cc[4][4], cm[4][3], cam_xyz[4][3], num;
4942 double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
4943 int sony_curve[] = { 0,0,0,0,0,4095 };
4944 unsigned sony_offset=0, sony_length=0, sony_key=0;
4945 #ifndef WITH_MMAP_HACK
4946 unsigned *buf;
4947 FILE *sfp;
4948 #endif /* WITH_MMAP_HACK */
4949 struct jhead jh;
4950
4951 if (tiff_nifds >= (int) sizeof tiff_ifd / (int) sizeof tiff_ifd[0])
4952 return 1;
4953 ifd = tiff_nifds++;
4954 for (j=0; j < 4; j++)
4955 for (i=0; i < 4; i++)
4956 cc[j][i] = i == j;
4957 entries = get2();
4958 if (entries > 512) return 1;
4959 while (entries--) {
4960 tiff_get (base, &tag, &type, &len, &save);
4961 switch (tag) {
4962 case 5: width = get2(); break;
4963 case 6: height = get2(); break;
4964 case 7: width += get2(); break;
4965 case 9: filters = get2(); break;
4966 case 17: case 18:
4967 if (type == 3 && len == 1)
4968 cam_mul[(tag-17)*2] = get2() / 256.0;
4969 break;
4970 case 23:
4971 if (type == 3) iso_speed = get2();
4972 break;
4973 case 36: case 37: case 38:
4974 cam_mul[tag-0x24] = get2();
4975 break;
4976 case 39:
4977 if (len < 50 || cam_mul[0]) break;
4978 fseek (ifp, 12, SEEK_CUR);
4979 FORC3 cam_mul[c] = get2();
4980 break;
4981 case 46:
4982 if (type != 7 || fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) break;
4983 thumb_offset = ftell(ifp) - 2;
4984 thumb_length = len;
4985 break;
4986 case 61440: /* Fuji HS10 table */
4987 parse_tiff_ifd (base);
4988 break;
4989 case 2: case 256: case 61441: /* ImageWidth */
4990 tiff_ifd[ifd].width = getint(type);
4991 break;
4992 case 3: case 257: case 61442: /* ImageHeight */
4993 tiff_ifd[ifd].height = getint(type);
4994 break;
4995 case 258: /* BitsPerSample */
4996 case 61443:
4997 tiff_ifd[ifd].samples = len & 7;
4998 tiff_ifd[ifd].bps = getint(type);
4999 break;
5000 case 259: /* Compression */
5001 tiff_ifd[ifd].comp = get2();
5002 break;
5003 case 262: /* PhotometricInterpretation */
5004 tiff_ifd[ifd].phint = get2();
5005 break;
5006 case 270: /* ImageDescription */
5007 fread (desc, 512, 1, ifp);
5008 break;
5009 case 271: /* Make */
5010 fgets (make, 64, ifp);
5011 break;
5012 case 272: /* Model */
5013 fgets (model, 64, ifp);
5014 break;
5015 case 280: /* Panasonic RW2 offset */
5016 if (type != 4) break;
5017 load_raw = &CLASS panasonic_load_raw;
5018 load_flags = 0x2008;
5019 case 273: /* StripOffset */
5020 case 513: /* JpegIFOffset */
5021 case 61447:
5022 tiff_ifd[ifd].offset = get4()+base;
5023 if (!tiff_ifd[ifd].bps) {
5024 fseek (ifp, tiff_ifd[ifd].offset, SEEK_SET);
5025 if (ljpeg_start (&jh, 1)) {
5026 tiff_ifd[ifd].comp = 6;
5027 tiff_ifd[ifd].width = jh.wide;
5028 tiff_ifd[ifd].height = jh.high;
5029 tiff_ifd[ifd].bps = jh.bits;
5030 tiff_ifd[ifd].samples = jh.clrs;
5031 if (!(jh.sraw || (jh.clrs & 1)))
5032 tiff_ifd[ifd].width *= jh.clrs;
5033 i = order;
5034 parse_tiff (tiff_ifd[ifd].offset + 12);
5035 order = i;
5036 }
5037 }
5038 break;
5039 case 274: /* Orientation */
5040 tiff_ifd[ifd].flip = "50132467"[get2() & 7]-'0';
5041 break;
5042 case 277: /* SamplesPerPixel */
5043 tiff_ifd[ifd].samples = getint(type) & 7;
5044 break;
5045 case 279: /* StripByteCounts */
5046 case 514:
5047 case 61448:
5048 tiff_ifd[ifd].bytes = get4();
5049 break;
5050 case 61454:
5051 FORC3 cam_mul[(4-c) % 3] = getint(type);
5052 break;
5053 case 305: case 11: /* Software */
5054 fgets (software, 64, ifp);
5055 if (!strncmp(software,"Adobe",5) ||
5056 !strncmp(software,"dcraw",5) ||
5057 !strncmp(software,"UFRaw",5) ||
5058 !strncmp(software,"Bibble",6) ||
5059 !strncmp(software,"Nikon Scan",10) ||
5060 !strcmp (software,"Digital Photo Professional"))
5061 is_raw = 0;
5062 break;
5063 case 306: /* DateTime */
5064 get_timestamp(0);
5065 break;
5066 case 315: /* Artist */
5067 fread (artist, 64, 1, ifp);
5068 break;
5069 case 322: /* TileWidth */
5070 tile_width = getint(type);
5071 break;
5072 case 323: /* TileLength */
5073 tile_length = getint(type);
5074 break;
5075 case 324: /* TileOffsets */
5076 tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
5077 if (len == 4) {
5078 load_raw = &CLASS sinar_4shot_load_raw;
5079 is_raw = 5;
5080 }
5081 break;
5082 case 330: /* SubIFDs */
5083 if (!strcmp(model,"DSLR-A100") && tiff_ifd[ifd].width == 3872) {
5084 load_raw = &CLASS sony_arw_load_raw;
5085 data_offset = get4()+base;
5086 ifd++; break;
5087 }
5088 while (len--) {
5089 i = ftell(ifp);
5090 fseek (ifp, get4()+base, SEEK_SET);
5091 if (parse_tiff_ifd (base)) break;
5092 fseek (ifp, i+4, SEEK_SET);
5093 }
5094 break;
5095 case 400:
5096 strcpy (make, "Sarnoff");
5097 maximum = 0xfff;
5098 break;
5099 case 28688:
5100 FORC4 sony_curve[c+1] = get2() >> 2 & 0xfff;
5101 for (i=0; i < 5; i++)
5102 for (j = sony_curve[i]+1; j <= sony_curve[i+1]; j++)
5103 curve[j] = curve[j-1] + (1 << i);
5104 break;
5105 case 29184: sony_offset = get4(); break;
5106 case 29185: sony_length = get4(); break;
5107 case 29217: sony_key = get4(); break;
5108 case 29264:
5109 parse_minolta (ftell(ifp));
5110 raw_width = 0;
5111 break;
5112 case 29443:
5113 FORC4 cam_mul[c ^ (c < 2)] = get2();
5114 break;
5115 case 29459:
5116 FORC4 cam_mul[c] = get2();
5117 i = (cam_mul[1] == 1024 && cam_mul[2] == 1024) << 1;
5118 SWAP (cam_mul[i],cam_mul[i+1])
5119 break;
5120 case 33405: /* Model2 */
5121 fgets (model2, 64, ifp);
5122 break;
5123 case 33422: /* CFAPattern */
5124 case 64777: /* Kodak P-series */
5125 if ((plen=len) > 16) plen = 16;
5126 fread (cfa_pat, 1, plen, ifp);
5127 for (colors=cfa=i=0; i < (int) plen; i++) {
5128 colors += !(cfa & (1 << cfa_pat[i]));
5129 cfa |= 1 << cfa_pat[i];
5130 }
5131 if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
5132 if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
5133 goto guess_cfa_pc;
5134 case 33424:
5135 case 65024:
5136 fseek (ifp, get4()+base, SEEK_SET);
5137 parse_kodak_ifd (base);
5138 break;
5139 case 33434: /* ExposureTime */
5140 shutter = getreal(type);
5141 break;
5142 case 33437: /* FNumber */
5143 aperture = getreal(type);
5144 break;
5145 case 34306: /* Leaf white balance */
5146 FORC4 cam_mul[c ^ 1] = 4096.0 / get2();
5147 break;
5148 case 34307: /* Leaf CatchLight color matrix */
5149 fread (software, 1, 7, ifp);
5150 if (strncmp(software,"MATRIX",6)) break;
5151 colors = 4;
5152 for (raw_color = i=0; i < 3; i++) {
5153 FORC4 fscanf (ifp, "%f", &rgb_cam[i][c^1]);
5154 if (!use_camera_wb) continue;
5155 num = 0;
5156 FORC4 num += rgb_cam[i][c];
5157 FORC4 rgb_cam[i][c] /= num;
5158 }
5159 break;
5160 case 34310: /* Leaf metadata */
5161 parse_mos (ftell(ifp));
5162 case 34303:
5163 strcpy (make, "Leaf");
5164 break;
5165 case 34665: /* EXIF tag */
5166 fseek (ifp, get4()+base, SEEK_SET);
5167 parse_exif (base);
5168 break;
5169 case 34853: /* GPSInfo tag */
5170 fseek (ifp, get4()+base, SEEK_SET);
5171 parse_gps (base);
5172 break;
5173 case 34675: /* InterColorProfile */
5174 case 50831: /* AsShotICCProfile */
5175 profile_offset = ftell(ifp);
5176 profile_length = len;
5177 break;
5178 case 37122: /* CompressedBitsPerPixel */
5179 kodak_cbpp = get4();
5180 break;
5181 case 37386: /* FocalLength */
5182 focal_len = getreal(type);
5183 break;
5184 case 37393: /* ImageNumber */
5185 shot_order = getint(type);
5186 break;
5187 case 37400: /* old Kodak KDC tag */
5188 for (raw_color = i=0; i < 3; i++) {
5189 getreal(type);
5190 FORC3 rgb_cam[i][c] = getreal(type);
5191 }
5192 break;
5193 case 46275: /* Imacon tags */
5194 strcpy (make, "Imacon");
5195 data_offset = ftell(ifp);
5196 ima_len = len;
5197 break;
5198 case 46279:
5199 if (!ima_len) break;
5200 fseek (ifp, 78, SEEK_CUR);
5201 raw_width = get4();
5202 raw_height = get4();
5203 left_margin = get4() & 7;
5204 width = raw_width - left_margin - (get4() & 7);
5205 top_margin = get4() & 7;
5206 height = raw_height - top_margin - (get4() & 7);
5207 if (raw_width == 7262) {
5208 height = 5444;
5209 width = 7244;
5210 left_margin = 7;
5211 }
5212 fseek (ifp, 52, SEEK_CUR);
5213 FORC3 cam_mul[c] = getreal(11);
5214 fseek (ifp, 114, SEEK_CUR);
5215 flip = (get2() >> 7) * 90;
5216 if (width * height * 6 == ima_len) {
5217 if (flip % 180 == 90) SWAP(width,height);
5218 filters = flip = 0;
5219 }
5220 sprintf (model, "Ixpress %d-Mp", height*width/1000000);
5221 load_raw = &CLASS imacon_full_load_raw;
5222 if (filters) {
5223 if (left_margin & 1) filters = 0x61616161;
5224 load_raw = &CLASS unpacked_load_raw;
5225 }
5226 maximum = 0xffff;
5227 break;
5228 case 50454: /* Sinar tag */
5229 case 50455:
5230 if (!(cbuf = (char *) malloc(len))) break;
5231 fread (cbuf, 1, len, ifp);
5232 for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
5233 if (!strncmp (++cp,"Neutral ",8))
5234 sscanf (cp+8, "%f %f %f", cam_mul, cam_mul+1, cam_mul+2);
5235 free (cbuf);
5236 break;
5237 case 50458:
5238 if (!make[0]) strcpy (make, "Hasselblad");
5239 break;
5240 case 50459: /* Hasselblad tag */
5241 i = order;
5242 j = ftell(ifp);
5243 c = tiff_nifds;
5244 order = get2();
5245 fseek (ifp, j+(get2(),get4()), SEEK_SET);
5246 parse_tiff_ifd (j);
5247 maximum = 0xffff;
5248 tiff_nifds = c;
5249 order = i;
5250 break;
5251 case 50706: /* DNGVersion */
5252 FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
5253 if (!make[0]) strcpy (make, "DNG");
5254 is_raw = 1;
5255 break;
5256 case 50710: /* CFAPlaneColor */
5257 if (len > 4) len = 4;
5258 colors = len;
5259 fread (cfa_pc, 1, colors, ifp);
5260 guess_cfa_pc:
5261 FORCC tab[cfa_pc[c]] = c;
5262 cdesc[c] = 0;
5263 for (i=16; i--; )
5264 filters = filters << 2 | tab[cfa_pat[i % plen]];
5265 break;
5266 case 50711: /* CFALayout */
5267 if (get2() == 2) {
5268 fuji_width = 1;
5269 filters = 0x49494949;
5270 }
5271 break;
5272 case 291:
5273 case 50712: /* LinearizationTable */
5274 linear_table (len);
5275 break;
5276 case 50713: /* BlackLevelRepeatDim */
5277 blrr = get2();
5278 blrc = get2();
5279 break;
5280 case 61450:
5281 blrr = blrc = 2;
5282 case 50714: /* BlackLevel */
5283 black = getreal(type);
5284 if (!filters || !~filters) break;
5285 dblack[0] = black;
5286 dblack[1] = (blrc == 2) ? getreal(type):dblack[0];
5287 dblack[2] = (blrr == 2) ? getreal(type):dblack[0];
5288 dblack[3] = (blrc == 2 && blrr == 2) ? getreal(type):dblack[1];
5289 if (colors == 3)
5290 filters |= ((filters >> 2 & 0x22222222) |
5291 (filters << 2 & 0x88888888)) & filters << 1;
5292 FORC4 cblack[filters >> (c << 1) & 3] = dblack[c];
5293 black = 0;
5294 break;
5295 case 50715: /* BlackLevelDeltaH */
5296 case 50716: /* BlackLevelDeltaV */
5297 for (num=i=0; i < (int) len; i++)
5298 num += getreal(type);
5299 black += num/len + 0.5;
5300 break;
5301 case 50717: /* WhiteLevel */
5302 maximum = getint(type);
5303 break;
5304 case 50718: /* DefaultScale */
5305 pixel_aspect = getreal(type);
5306 pixel_aspect /= getreal(type);
5307 break;
5308 case 50721: /* ColorMatrix1 */
5309 if (use_cm) break; /* Prioritize Matrix2 over Matrix1 (UF) */
5310 case 50722: /* ColorMatrix2 */
5311 FORCC for (j=0; j < 3; j++)
5312 cm[c][j] = getreal(type);
5313 use_cm = 1;
5314 break;
5315 case 50723: /* CameraCalibration1 */
5316 case 50724: /* CameraCalibration2 */
5317 for (i=0; i < colors; i++)
5318 FORCC cc[i][c] = getreal(type);
5319 break;
5320 case 50727: /* AnalogBalance */
5321 FORCC ab[c] = getreal(type);
5322 break;
5323 case 50728: /* AsShotNeutral */
5324 FORCC asn[c] = getreal(type);
5325 break;
5326 case 50729: /* AsShotWhiteXY */
5327 xyz[0] = getreal(type);
5328 xyz[1] = getreal(type);
5329 xyz[2] = 1 - xyz[0] - xyz[1];
5330 FORC3 xyz[c] /= d65_white[c];
5331 break;
5332 case 50740: /* DNGPrivateData */
5333 if (dng_version) break;
5334 parse_minolta (j = get4()+base);
5335 fseek (ifp, j, SEEK_SET);
5336 parse_tiff_ifd (base);
5337 break;
5338 case 50752:
5339 read_shorts (cr2_slice, 3);
5340 break;
5341 case 50829: /* ActiveArea */
5342 top_margin = getint(type);
5343 left_margin = getint(type);
5344 height = getint(type) - top_margin;
5345 width = getint(type) - left_margin;
5346 break;
5347 case 64772: /* Kodak P-series */
5348 if (len < 13) break;
5349 fseek (ifp, 16, SEEK_CUR);
5350 data_offset = get4();
5351 fseek (ifp, 28, SEEK_CUR);
5352 data_offset += get4();
5353 load_raw = &CLASS packed_load_raw;
5354 break;
5355 case 65026:
5356 if (type == 2) fgets (model2, 64, ifp);
5357 }
5358 fseek (ifp, save, SEEK_SET);
5359 }
5360 #ifndef WITH_MMAP_HACK
5361 if (sony_length && (buf = (unsigned *) malloc(sony_length))) {
5362 fseek (ifp, sony_offset, SEEK_SET);
5363 fread (buf, sony_length, 1, ifp);
5364 sony_decrypt (buf, sony_length/4, 1, sony_key);
5365 sfp = ifp;
5366 if ((ifp = tmpfile())) {
5367 fwrite (buf, sony_length, 1, ifp);
5368 fseek (ifp, 0, SEEK_SET);
5369 parse_tiff_ifd (-sony_offset);
5370 fclose (ifp);
5371 }
5372 ifp = sfp;
5373 free (buf);
5374 }
5375 #endif /* WITH_MMAP_HACK */
5376 for (i=0; i < colors; i++)
5377 FORCC cc[i][c] *= ab[i];
5378 if (use_cm) {
5379 FORCC for (i=0; i < 3; i++)
5380 for (cam_xyz[c][i]=j=0; j < colors; j++)
5381 cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
5382 cam_xyz_coeff (cam_xyz);
5383 }
5384 if (asn[0]) {
5385 cam_mul[3] = 0;
5386 FORCC cam_mul[c] = 1 / asn[c];
5387 }
5388 if (!use_cm)
5389 FORCC pre_mul[c] /= cc[c][c];
5390 return 0;
5391 }
5392
parse_tiff(int base)5393 int CLASS parse_tiff (int base)
5394 {
5395 int doff;
5396
5397 fseek (ifp, base, SEEK_SET);
5398 order = get2();
5399 if (order != 0x4949 && order != 0x4d4d) return 0;
5400 get2();
5401 while ((doff = get4())) {
5402 fseek (ifp, doff+base, SEEK_SET);
5403 if (parse_tiff_ifd (base)) break;
5404 }
5405 return 1;
5406 }
5407
apply_tiff()5408 void CLASS apply_tiff()
5409 {
5410 int max_samp=0, raw=-1, thm=-1, i;
5411 struct jhead jh;
5412
5413 thumb_misc = 16;
5414 if (thumb_offset) {
5415 fseek (ifp, thumb_offset, SEEK_SET);
5416 if (ljpeg_start (&jh, 1)) {
5417 thumb_misc = jh.bits;
5418 thumb_width = jh.wide;
5419 thumb_height = jh.high;
5420 }
5421 }
5422 for (i=0; i < (int) tiff_nifds; i++) {
5423 if (max_samp < tiff_ifd[i].samples)
5424 max_samp = tiff_ifd[i].samples;
5425 if (max_samp > 3) max_samp = 3;
5426 if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
5427 (tiff_ifd[i].width | tiff_ifd[i].height) < 0x10000 &&
5428 tiff_ifd[i].width*tiff_ifd[i].height > raw_width*raw_height) {
5429 raw_width = tiff_ifd[i].width;
5430 raw_height = tiff_ifd[i].height;
5431 tiff_bps = tiff_ifd[i].bps;
5432 tiff_compress = tiff_ifd[i].comp;
5433 data_offset = tiff_ifd[i].offset;
5434 tiff_flip = tiff_ifd[i].flip;
5435 tiff_samples = tiff_ifd[i].samples;
5436 raw = i;
5437 }
5438 }
5439 for (i=tiff_nifds; i--; )
5440 if (tiff_ifd[i].flip) tiff_flip = tiff_ifd[i].flip;
5441 if (raw >= 0 && !load_raw)
5442 switch (tiff_compress) {
5443 case 0: case 1:
5444 switch (tiff_bps) {
5445 case 8: load_raw = &CLASS eight_bit_load_raw; break;
5446 case 12: load_raw = &CLASS packed_load_raw;
5447 if (tiff_ifd[raw].phint == 2)
5448 load_flags = 6;
5449 if (strncmp(make,"PENTAX",6)) break;
5450 case 14:
5451 case 16: load_raw = &CLASS unpacked_load_raw; break;
5452 }
5453 if (tiff_ifd[raw].bytes*5 == raw_width*raw_height*8) {
5454 tiff_bps = 12;
5455 load_raw = &CLASS packed_load_raw;
5456 load_flags = 17;
5457 }
5458 break;
5459 case 6: case 7: case 99:
5460 load_raw = &CLASS lossless_jpeg_load_raw; break;
5461 case 262:
5462 load_raw = &CLASS kodak_262_load_raw; break;
5463 case 32767:
5464 if (tiff_ifd[raw].bytes == raw_width*raw_height) {
5465 tiff_bps = 12;
5466 load_raw = &CLASS sony_arw2_load_raw; break;
5467 }
5468 if (tiff_ifd[raw].bytes*8 != (int)(raw_width*raw_height*tiff_bps)) {
5469 raw_height += 8;
5470 load_raw = &CLASS sony_arw_load_raw; break;
5471 }
5472 load_flags = 15;
5473 case 32769:
5474 load_flags++;
5475 case 32770:
5476 case 32773:
5477 load_raw = &CLASS packed_load_raw; break;
5478 case 34713:
5479 load_raw = &CLASS nikon_compressed_load_raw; break;
5480 case 65535:
5481 load_raw = &CLASS pentax_load_raw; break;
5482 case 65000:
5483 switch (tiff_ifd[raw].phint) {
5484 case 2: load_raw = &CLASS kodak_rgb_load_raw; filters = 0; break;
5485 case 6: load_raw = &CLASS kodak_ycbcr_load_raw; filters = 0; break;
5486 case 32803: load_raw = &CLASS kodak_65000_load_raw;
5487 }
5488 case 32867: break;
5489 default: is_raw = 0;
5490 }
5491 if (!dng_version)
5492 if ( (tiff_samples == 3 && tiff_ifd[raw].bytes &&
5493 tiff_bps != 14 && tiff_bps != 2048)
5494 || (tiff_bps == 8 && !strstr(make,"KODAK") && !strstr(make,"Kodak") &&
5495 !strstr(model2,"DEBUG RAW")))
5496 is_raw = 0;
5497 for (i=0; i < (int) tiff_nifds; i++)
5498 if (i != raw && tiff_ifd[i].samples == max_samp &&
5499 tiff_ifd[i].width * tiff_ifd[i].height / SQR(tiff_ifd[i].bps+1) >
5500 (int)(thumb_width * thumb_height / SQR(thumb_misc+1))) {
5501 thumb_width = tiff_ifd[i].width;
5502 thumb_height = tiff_ifd[i].height;
5503 thumb_offset = tiff_ifd[i].offset;
5504 thumb_length = tiff_ifd[i].bytes;
5505 thumb_misc = tiff_ifd[i].bps;
5506 thm = i;
5507 }
5508 if (thm >= 0) {
5509 thumb_misc |= tiff_ifd[thm].samples << 5;
5510 switch (tiff_ifd[thm].comp) {
5511 case 0:
5512 write_thumb = &CLASS layer_thumb;
5513 break;
5514 case 1:
5515 if (tiff_ifd[thm].bps > 8)
5516 thumb_load_raw = &CLASS kodak_thumb_load_raw;
5517 else
5518 write_thumb = &CLASS ppm_thumb;
5519 break;
5520 case 65000:
5521 thumb_load_raw = tiff_ifd[thm].phint == 6 ?
5522 &CLASS kodak_ycbcr_load_raw : &CLASS kodak_rgb_load_raw;
5523 }
5524 }
5525 }
5526
parse_minolta(int base)5527 void CLASS parse_minolta (int base)
5528 {
5529 int save, tag, len, offset, high=0, wide=0, i, c;
5530 short sorder=order;
5531
5532 fseek (ifp, base, SEEK_SET);
5533 if (fgetc(ifp) || fgetc(ifp)-'M' || fgetc(ifp)-'R') return;
5534 order = fgetc(ifp) * 0x101;
5535 offset = base + get4() + 8;
5536 while ((save=ftell(ifp)) < offset) {
5537 for (tag=i=0; i < 4; i++)
5538 tag = tag << 8 | fgetc(ifp);
5539 len = get4();
5540 switch (tag) {
5541 case 0x505244: /* PRD */
5542 fseek (ifp, 8, SEEK_CUR);
5543 high = get2();
5544 wide = get2();
5545 break;
5546 case 0x574247: /* WBG */
5547 get4();
5548 i = strcmp(model,"DiMAGE A200") ? 0:3;
5549 FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
5550 break;
5551 case 0x545457: /* TTW */
5552 parse_tiff (ftell(ifp));
5553 data_offset = offset;
5554 }
5555 fseek (ifp, save+len+8, SEEK_SET);
5556 }
5557 raw_height = high;
5558 raw_width = wide;
5559 order = sorder;
5560 }
5561
5562 /*
5563 Many cameras have a "debug mode" that writes JPEG and raw
5564 at the same time. The raw file has no header, so try to
5565 to open the matching JPEG file and read its metadata.
5566 */
parse_external_jpeg()5567 void CLASS parse_external_jpeg()
5568 {
5569 const char *file, *ext;
5570 char *jname, *jfile, *jext;
5571 FILE *save=ifp;
5572
5573 ext = strrchr (ifname, '.');
5574 file = strrchr (ifname, '/');
5575 if (!file) file = strrchr (ifname, '\\');
5576 if (!file) file = ifname-1;
5577 file++;
5578 if (!ext || strlen(ext) != 4 || ext-file != 8) return;
5579 jname = (char *) malloc (strlen(ifname) + 1);
5580 merror (jname, "parse_external_jpeg()");
5581 strcpy (jname, ifname);
5582 jfile = file - ifname + jname;
5583 jext = ext - ifname + jname;
5584 if (strcasecmp (ext, ".jpg")) {
5585 strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
5586 if (isdigit(*file)) {
5587 memcpy (jfile, file+4, 4);
5588 memcpy (jfile+4, file, 4);
5589 }
5590 } else
5591 while (isdigit(*--jext)) {
5592 if (*jext != '9') {
5593 (*jext)++;
5594 break;
5595 }
5596 *jext = '0';
5597 }
5598 if (strcmp (jname, ifname)) {
5599 if ((ifp = fopen (jname, "rb"))) {
5600 dcraw_message (DCRAW_VERBOSE,_("Reading metadata from %s ...\n"), jname);
5601 parse_tiff (12);
5602 thumb_offset = 0;
5603 is_raw = 1;
5604 fclose (ifp);
5605 }
5606 }
5607 if (!timestamp)
5608 dcraw_message (DCRAW_WARNING,_("Failed to read metadata from %s\n"), jname);
5609 free (jname);
5610 ifp = save;
5611 }
5612
5613 /*
5614 CIFF block 0x1030 contains an 8x8 white sample.
5615 Load this into white[][] for use in scale_colors().
5616 */
ciff_block_1030()5617 void CLASS ciff_block_1030()
5618 {
5619 static const ushort key[] = { 0x410, 0x45f3 };
5620 int i, bpp, row, col, vbits=0;
5621 unsigned long bitbuf=0;
5622
5623 if ((get2(),get4()) != 0x80008 || !get4()) return;
5624 bpp = get2();
5625 if (bpp != 10 && bpp != 12) return;
5626 for (i=row=0; row < 8; row++)
5627 for (col=0; col < 8; col++) {
5628 if (vbits < bpp) {
5629 bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
5630 vbits += 16;
5631 }
5632 white[row][col] =
5633 bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp);
5634 vbits -= bpp;
5635 }
5636 }
5637
5638 /*
5639 Parse a CIFF file, better known as Canon CRW format.
5640 */
parse_ciff(int offset,int length)5641 void CLASS parse_ciff (int offset, int length)
5642 {
5643 int tboff, nrecs, c, type, len, save, wbi=-1;
5644 ushort key[] = { 0x410, 0x45f3 };
5645
5646 fseek (ifp, offset+length-4, SEEK_SET);
5647 tboff = get4() + offset;
5648 fseek (ifp, tboff, SEEK_SET);
5649 nrecs = get2();
5650 if (nrecs > 100) return;
5651 while (nrecs--) {
5652 type = get2();
5653 len = get4();
5654 save = ftell(ifp) + 4;
5655 fseek (ifp, offset+get4(), SEEK_SET);
5656 if ((((type >> 8) + 8) | 8) == 0x38)
5657 parse_ciff (ftell(ifp), len); /* Parse a sub-table */
5658
5659 if (type == 0x0810)
5660 fread (artist, 64, 1, ifp);
5661 if (type == 0x080a) {
5662 fread (make, 64, 1, ifp);
5663 fseek (ifp, strlen(make) - 63, SEEK_CUR);
5664 fread (model, 64, 1, ifp);
5665 }
5666 if (type == 0x1810) {
5667 fseek (ifp, 12, SEEK_CUR);
5668 flip = get4();
5669 }
5670 if (type == 0x1835) /* Get the decoder table */
5671 tiff_compress = get4();
5672 if (type == 0x2007) {
5673 thumb_offset = ftell(ifp);
5674 thumb_length = len;
5675 }
5676 if (type == 0x1818) {
5677 shutter = pow (2, -int_to_float((get4(),get4())));
5678 aperture = pow (2, int_to_float(get4())/2);
5679 }
5680 if (type == 0x102a) {
5681 iso_speed = pow (2, (get4(),get2())/32.0 - 4) * 50;
5682 aperture = pow (2, (get2(),(short)get2())/64.0);
5683 shutter = pow (2,-((short)get2())/32.0);
5684 wbi = (get2(),get2());
5685 if (wbi > 17) wbi = 0;
5686 fseek (ifp, 32, SEEK_CUR);
5687 if (shutter > 1e6) shutter = get2()/10.0;
5688 }
5689 if (type == 0x102c) {
5690 if (get2() > 512) { /* Pro90, G1 */
5691 fseek (ifp, 118, SEEK_CUR);
5692 FORC4 cam_mul[c ^ 2] = get2();
5693 } else { /* G2, S30, S40 */
5694 fseek (ifp, 98, SEEK_CUR);
5695 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2();
5696 }
5697 }
5698 if (type == 0x0032) {
5699 if (len == 768) { /* EOS D30 */
5700 fseek (ifp, 72, SEEK_CUR);
5701 FORC4 cam_mul[c ^ (c >> 1)] = 1024.0 / get2();
5702 if (!wbi) cam_mul[0] = -1; /* use my auto white balance */
5703 } else if (!cam_mul[0]) {
5704 if (get2() == key[0]) /* Pro1, G6, S60, S70 */
5705 c = (strstr(model,"Pro1") ?
5706 "012346000000000000":"01345:000000006008")[wbi]-'0'+ 2;
5707 else { /* G3, G5, S45, S50 */
5708 c = "023457000000006000"[wbi]-'0';
5709 key[0] = key[1] = 0;
5710 }
5711 fseek (ifp, 78 + c*8, SEEK_CUR);
5712 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2() ^ key[c & 1];
5713 if (!wbi) cam_mul[0] = -1;
5714 }
5715 }
5716 if (type == 0x10a9) { /* D60, 10D, 300D, and clones */
5717 if (len > 66) wbi = "0134567028"[wbi]-'0';
5718 fseek (ifp, 2 + wbi*8, SEEK_CUR);
5719 FORC4 cam_mul[c ^ (c >> 1)] = get2();
5720 }
5721 if (type == 0x1030 && (0x18040 >> wbi & 1))
5722 ciff_block_1030(); /* all that don't have 0x10a9 */
5723 if (type == 0x1031) {
5724 raw_width = (get2(),get2());
5725 raw_height = get2();
5726 }
5727 if (type == 0x5029) {
5728 focal_len = len >> 16;
5729 if ((len & 0xffff) == 2) focal_len /= 32;
5730 }
5731 if (type == 0x5813) flash_used = int_to_float(len);
5732 if (type == 0x5814) canon_ev = int_to_float(len);
5733 if (type == 0x5817) shot_order = len;
5734 if (type == 0x5834) unique_id = len;
5735 if (type == 0x580e) timestamp = len;
5736 if (type == 0x180e) timestamp = get4();
5737 #ifdef LOCALTIME
5738 if ((type | 0x4000) == 0x580e)
5739 timestamp = mktime (gmtime (×tamp));
5740 #endif
5741 fseek (ifp, save, SEEK_SET);
5742 }
5743 }
5744
parse_rollei()5745 void CLASS parse_rollei()
5746 {
5747 char line[128], *val;
5748 struct tm t;
5749
5750 fseek (ifp, 0, SEEK_SET);
5751 memset (&t, 0, sizeof t);
5752 do {
5753 fgets (line, 128, ifp);
5754 if ((val = strchr(line,'=')))
5755 *val++ = 0;
5756 else
5757 val = line + strlen(line);
5758 if (!strcmp(line,"DAT"))
5759 sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
5760 if (!strcmp(line,"TIM"))
5761 sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
5762 if (!strcmp(line,"HDR"))
5763 thumb_offset = atoi(val);
5764 if (!strcmp(line,"X "))
5765 raw_width = atoi(val);
5766 if (!strcmp(line,"Y "))
5767 raw_height = atoi(val);
5768 if (!strcmp(line,"TX "))
5769 thumb_width = atoi(val);
5770 if (!strcmp(line,"TY "))
5771 thumb_height = atoi(val);
5772 } while (strncmp(line,"EOHD",4));
5773 data_offset = thumb_offset + thumb_width * thumb_height * 2;
5774 t.tm_year -= 1900;
5775 t.tm_mon -= 1;
5776 if (mktime(&t) > 0)
5777 timestamp = mktime(&t);
5778 strcpy (make, "Rollei");
5779 strcpy (model,"d530flex");
5780 write_thumb = &CLASS rollei_thumb;
5781 }
5782
parse_sinar_ia()5783 void CLASS parse_sinar_ia()
5784 {
5785 int entries, off;
5786 char str[8], *cp;
5787
5788 order = 0x4949;
5789 fseek (ifp, 4, SEEK_SET);
5790 entries = get4();
5791 fseek (ifp, get4(), SEEK_SET);
5792 while (entries--) {
5793 off = get4(); get4();
5794 fread (str, 8, 1, ifp);
5795 if (!strcmp(str,"META")) meta_offset = off;
5796 if (!strcmp(str,"THUMB")) thumb_offset = off;
5797 if (!strcmp(str,"RAW0")) data_offset = off;
5798 }
5799 fseek (ifp, meta_offset+20, SEEK_SET);
5800 fread (make, 64, 1, ifp);
5801 make[63] = 0;
5802 if ((cp = strchr(make,' '))) {
5803 strcpy (model, cp+1);
5804 *cp = 0;
5805 }
5806 raw_width = get2();
5807 raw_height = get2();
5808 load_raw = &CLASS unpacked_load_raw;
5809 thumb_width = (get4(),get2());
5810 thumb_height = get2();
5811 write_thumb = &CLASS ppm_thumb;
5812 maximum = 0x3fff;
5813 }
5814
parse_phase_one(int base)5815 void CLASS parse_phase_one (int base)
5816 {
5817 unsigned entries, tag, type, len, data, save, i, j, c;
5818 float romm_cam[3][3];
5819 char *cp;
5820
5821 memset (&ph1, 0, sizeof ph1);
5822 fseek (ifp, base, SEEK_SET);
5823 order = get4() & 0xffff;
5824 if (get4() >> 8 != 0x526177) return; /* "Raw" */
5825 fseek (ifp, get4()+base, SEEK_SET);
5826 entries = get4();
5827 get4();
5828 while (entries--) {
5829 tag = get4();
5830 type = get4();
5831 len = get4();
5832 data = get4();
5833 save = ftell(ifp);
5834 fseek (ifp, base+data, SEEK_SET);
5835 switch (tag) {
5836 case 0x100: flip = "0653"[data & 3]-'0'; break;
5837 case 0x106:
5838 for (i=0; i < 3; i++)
5839 for (j=0; j < 3; j++)
5840 romm_cam[i][j] = getreal(11);
5841 romm_coeff (romm_cam);
5842 break;
5843 case 0x107:
5844 FORC3 cam_mul[c] = getreal(11);
5845 break;
5846 case 0x108: raw_width = data; break;
5847 case 0x109: raw_height = data; break;
5848 case 0x10a: left_margin = data; break;
5849 case 0x10b: top_margin = data; break;
5850 case 0x10c: width = data; break;
5851 case 0x10d: height = data; break;
5852 case 0x10e: ph1.format = data; break;
5853 case 0x10f: data_offset = data+base; break;
5854 case 0x110: meta_offset = data+base;
5855 meta_length = len; break;
5856 case 0x112: ph1.key_off = save - 4; break;
5857 case 0x210: ph1.tag_210 = int_to_float(data); break;
5858 case 0x21a: ph1.tag_21a = data; break;
5859 case 0x21c: strip_offset = data+base; break;
5860 case 0x21d: ph1.black = data; break;
5861 case 0x222: ph1.split_col = data - left_margin; break;
5862 case 0x223: ph1.black_off = data+base; break;
5863 case 0x301:
5864 model[63] = 0;
5865 fread (model, 1, 63, ifp);
5866 if ((cp = strstr(model," camera"))) *cp = 0;
5867 }
5868 fseek (ifp, save, SEEK_SET);
5869 }
5870 load_raw = ph1.format < 3 ?
5871 &CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
5872 maximum = 0xffff;
5873 strcpy (make, "Phase One");
5874 if (model[0]) return;
5875 switch (raw_height) {
5876 case 2060: strcpy (model,"LightPhase"); break;
5877 case 2682: strcpy (model,"H 10"); break;
5878 case 4128: strcpy (model,"H 20"); break;
5879 case 5488: strcpy (model,"H 25"); break;
5880 }
5881 }
5882
parse_fuji(int offset)5883 void CLASS parse_fuji (int offset)
5884 {
5885 unsigned entries, tag, len, save, c;
5886
5887 fseek (ifp, offset, SEEK_SET);
5888 entries = get4();
5889 if (entries > 255) return;
5890 while (entries--) {
5891 tag = get2();
5892 len = get2();
5893 save = ftell(ifp);
5894 if (tag == 0x100) {
5895 raw_height = get2();
5896 raw_width = get2();
5897 } else if (tag == 0x121) {
5898 height = get2();
5899 if ((width = get2()) == 4284) width += 3;
5900 } else if (tag == 0x130) {
5901 fuji_layout = fgetc(ifp) >> 7;
5902 load_raw = fgetc(ifp) & 8 ?
5903 &CLASS unpacked_load_raw : &CLASS fuji_load_raw;
5904 }
5905 if (tag == 0x2ff0)
5906 FORC4 cam_mul[c ^ 1] = get2();
5907 fseek (ifp, save+len, SEEK_SET);
5908 }
5909 if (!raw_height) {
5910 filters = 0x16161616;
5911 load_raw = &CLASS packed_load_raw;
5912 load_flags = 24;
5913 }
5914 height <<= fuji_layout;
5915 width >>= fuji_layout;
5916 }
5917
parse_jpeg(int offset)5918 int CLASS parse_jpeg (int offset)
5919 {
5920 int len, save, hlen, mark;
5921
5922 fseek (ifp, offset, SEEK_SET);
5923 if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
5924
5925 while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
5926 order = 0x4d4d;
5927 len = get2() - 2;
5928 save = ftell(ifp);
5929 if (mark == 0xc0 || mark == 0xc3) {
5930 fgetc(ifp);
5931 raw_height = get2();
5932 raw_width = get2();
5933 }
5934 order = get2();
5935 hlen = get4();
5936 if (get4() == 0x48454150) /* "HEAP" */
5937 parse_ciff (save+hlen, len-hlen);
5938 if (parse_tiff (save+6)) apply_tiff();
5939 fseek (ifp, save+len, SEEK_SET);
5940 }
5941 return 1;
5942 }
5943
parse_riff()5944 void CLASS parse_riff()
5945 {
5946 unsigned i, size, end;
5947 char tag[4], date[64], month[64];
5948 static const char mon[12][4] =
5949 { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
5950 struct tm t;
5951
5952 order = 0x4949;
5953 fread (tag, 4, 1, ifp);
5954 size = get4();
5955 end = ftell(ifp) + size;
5956 if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
5957 get4();
5958 while ((unsigned)(ftell(ifp)+7) < end)
5959 parse_riff();
5960 } else if (!memcmp(tag,"nctg",4)) {
5961 while ((unsigned)(ftell(ifp)+7) < end) {
5962 i = get2();
5963 size = get2();
5964 if ((i+1) >> 1 == 10 && size == 20)
5965 get_timestamp(0);
5966 else fseek (ifp, size, SEEK_CUR);
5967 }
5968 } else if (!memcmp(tag,"IDIT",4) && size < 64) {
5969 fread (date, 64, 1, ifp);
5970 date[size] = 0;
5971 memset (&t, 0, sizeof t);
5972 if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
5973 &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
5974 for (i=0; i < 12 && strcasecmp(mon[i],month); i++);
5975 t.tm_mon = i;
5976 t.tm_year -= 1900;
5977 if (mktime(&t) > 0)
5978 timestamp = mktime(&t);
5979 }
5980 } else
5981 fseek (ifp, size, SEEK_CUR);
5982 }
5983
parse_smal(int offset,unsigned fsize)5984 void CLASS parse_smal (int offset, unsigned fsize)
5985 {
5986 int ver;
5987
5988 fseek (ifp, offset+2, SEEK_SET);
5989 order = 0x4949;
5990 ver = fgetc(ifp);
5991 if (ver == 6)
5992 fseek (ifp, 5, SEEK_CUR);
5993 if (get4() != fsize) return;
5994 if (ver > 6) data_offset = get4();
5995 raw_height = height = get2();
5996 raw_width = width = get2();
5997 strcpy (make, "SMaL");
5998 sprintf (model, "v%d %dx%d", ver, width, height);
5999 if (ver == 6) load_raw = &CLASS smal_v6_load_raw;
6000 if (ver == 9) load_raw = &CLASS smal_v9_load_raw;
6001 }
6002
parse_cine()6003 void CLASS parse_cine()
6004 {
6005 unsigned off_head, off_setup, off_image, i;
6006
6007 order = 0x4949;
6008 fseek (ifp, 4, SEEK_SET);
6009 is_raw = get2() == 2;
6010 fseek (ifp, 14, SEEK_CUR);
6011 is_raw *= get4();
6012 off_head = get4();
6013 off_setup = get4();
6014 off_image = get4();
6015 timestamp = get4();
6016 if ((i = get4())) timestamp = i;
6017 fseek (ifp, off_head+4, SEEK_SET);
6018 raw_width = get4();
6019 raw_height = get4();
6020 switch (get2(),get2()) {
6021 case 8: load_raw = &CLASS eight_bit_load_raw; break;
6022 case 16: load_raw = &CLASS unpacked_load_raw;
6023 }
6024 fseek (ifp, off_setup+792, SEEK_SET);
6025 strcpy (make, "CINE");
6026 sprintf (model, "%d", get4());
6027 fseek (ifp, 12, SEEK_CUR);
6028 switch ((i=get4()) & 0xffffff) {
6029 case 3: filters = 0x94949494; break;
6030 case 4: filters = 0x49494949; break;
6031 default: is_raw = 0;
6032 }
6033 fseek (ifp, 72, SEEK_CUR);
6034 switch ((get4()+3600) % 360) {
6035 case 270: flip = 4; break;
6036 case 180: flip = 1; break;
6037 case 90: flip = 7; break;
6038 case 0: flip = 2;
6039 }
6040 cam_mul[0] = getreal(11);
6041 cam_mul[2] = getreal(11);
6042 maximum = ~(-1 << get4());
6043 fseek (ifp, 668, SEEK_CUR);
6044 shutter = get4()/1000000000.0;
6045 fseek (ifp, off_image, SEEK_SET);
6046 if (shot_select < is_raw)
6047 fseek (ifp, shot_select*8, SEEK_CUR);
6048 data_offset = (INT64) get4() + 8;
6049 data_offset += (INT64) get4() << 32;
6050 }
6051
foveon_gets(int offset,char * str,int len)6052 char * CLASS foveon_gets (int offset, char *str, int len)
6053 {
6054 int i;
6055 fseek (ifp, offset, SEEK_SET);
6056 for (i=0; i < len-1; i++)
6057 if ((str[i] = get2()) == 0) break;
6058 str[i] = 0;
6059 return str;
6060 }
6061
parse_foveon()6062 void CLASS parse_foveon()
6063 {
6064 int entries, img=0, off, len, tag, save, i, wide, high, pent, poff[256][2];
6065 char name[64], value[64];
6066
6067 order = 0x4949; /* Little-endian */
6068 fseek (ifp, 36, SEEK_SET);
6069 flip = get4();
6070 fseek (ifp, -4, SEEK_END);
6071 fseek (ifp, get4(), SEEK_SET);
6072 if (get4() != 0x64434553) return; /* SECd */
6073 entries = (get4(),get4());
6074 while (entries--) {
6075 off = get4();
6076 len = get4();
6077 tag = get4();
6078 save = ftell(ifp);
6079 fseek (ifp, off, SEEK_SET);
6080 if (get4() != (unsigned)(0x20434553 | (tag << 24))) return;
6081 switch (tag) {
6082 case 0x47414d49: /* IMAG */
6083 case 0x32414d49: /* IMA2 */
6084 fseek (ifp, 8, SEEK_CUR);
6085 if (get4() == 30) { /* SIGMA DP* and SD15 are unsupported */
6086 is_foveon = 0;
6087 return;
6088 }
6089 wide = get4();
6090 high = get4();
6091 if (wide > raw_width && high > raw_height) {
6092 raw_width = wide;
6093 raw_height = high;
6094 data_offset = off+24;
6095 }
6096 fseek (ifp, off+28, SEEK_SET);
6097 if (fgetc(ifp) == 0xff && fgetc(ifp) == 0xd8
6098 && thumb_length < (unsigned)(len-28)) {
6099 thumb_offset = off+28;
6100 thumb_length = len-28;
6101 write_thumb = &CLASS jpeg_thumb;
6102 }
6103 if (++img == 2 && !thumb_length) {
6104 thumb_offset = off+24;
6105 thumb_width = wide;
6106 thumb_height = high;
6107 write_thumb = &CLASS foveon_thumb;
6108 }
6109 break;
6110 case 0x464d4143: /* CAMF */
6111 meta_offset = off+24;
6112 meta_length = len-28;
6113 if (meta_length > 0x20000)
6114 meta_length = 0x20000;
6115 break;
6116 case 0x504f5250: /* PROP */
6117 pent = (get4(),get4());
6118 fseek (ifp, 12, SEEK_CUR);
6119 off += pent*8 + 24;
6120 if ((unsigned) pent > 256) pent=256;
6121 for (i=0; i < pent*2; i++)
6122 poff[0][i] = off + get4()*2;
6123 for (i=0; i < pent; i++) {
6124 foveon_gets (poff[i][0], name, 64);
6125 foveon_gets (poff[i][1], value, 64);
6126 if (!strcmp (name, "ISO"))
6127 iso_speed = atoi(value);
6128 if (!strcmp (name, "CAMMANUF"))
6129 strcpy (make, value);
6130 if (!strcmp (name, "CAMMODEL"))
6131 strcpy (model, value);
6132 if (!strcmp (name, "WB_DESC"))
6133 strcpy (model2, value);
6134 if (!strcmp (name, "TIME"))
6135 timestamp = atoi(value);
6136 if (!strcmp (name, "EXPTIME"))
6137 shutter = atoi(value) / 1000000.0;
6138 if (!strcmp (name, "APERTURE"))
6139 aperture = atof(value);
6140 if (!strcmp (name, "FLENGTH"))
6141 focal_len = atof(value);
6142 }
6143 #ifdef LOCALTIME
6144 timestamp = mktime (gmtime (×tamp));
6145 #endif
6146 }
6147 fseek (ifp, save, SEEK_SET);
6148 }
6149 is_foveon = 1;
6150 }
6151
6152 /*
6153 All matrices are from Adobe DNG Converter unless otherwise noted.
6154 */
adobe_coeff(const char * make,const char * model)6155 void CLASS adobe_coeff (const char *make, const char *model)
6156 {
6157 static const struct {
6158 const char *prefix;
6159 short black, maximum, trans[12];
6160 } table[] = {
6161 { "AGFAPHOTO DC-833m", 0, 0, /* DJC */
6162 { 11438,-3762,-1115,-2409,9914,2497,-1227,2295,5300 } },
6163 { "Apple QuickTake", 0, 0, /* DJC */
6164 { 21392,-5653,-3353,2406,8010,-415,7166,1427,2078 } },
6165 { "Canon EOS D2000", 0, 0,
6166 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
6167 { "Canon EOS D6000", 0, 0,
6168 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
6169 { "Canon EOS D30", 0, 0,
6170 { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
6171 { "Canon EOS D60", 0, 0xfa0,
6172 { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
6173 { "Canon EOS 5D Mark II", 0, 0x3cf0,
6174 { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
6175 { "Canon EOS 5D", 0, 0xe6c,
6176 { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
6177 { "Canon EOS 7D", 0, 0x3510,
6178 { 6844,-996,-856,-3876,11761,2396,-593,1772,6198 } },
6179 { "Canon EOS 10D", 0, 0xfa0,
6180 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
6181 { "Canon EOS 20Da", 0, 0,
6182 { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
6183 { "Canon EOS 20D", 0, 0xfff,
6184 { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
6185 { "Canon EOS 30D", 0, 0,
6186 { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
6187 { "Canon EOS 40D", 0, 0x3f60,
6188 { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
6189 { "Canon EOS 50D", 0, 0x3d93,
6190 { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
6191 { "Canon EOS 60D", 0, 0x2ff7,
6192 { 6719,-994,-925,-4408,12426,2211,-887,2129,6051 } },
6193 { "Canon EOS 300D", 0, 0xfa0,
6194 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
6195 { "Canon EOS 350D", 0, 0xfff,
6196 { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
6197 { "Canon EOS 400D", 0, 0xe8e,
6198 { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
6199 { "Canon EOS 450D", 0, 0x390d,
6200 { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
6201 { "Canon EOS 500D", 0, 0x3479,
6202 { 4763,712,-646,-6821,14399,2640,-1921,3276,6561 } },
6203 { "Canon EOS 550D", 0, 0x3dd7,
6204 { 6941,-1164,-857,-3825,11597,2534,-416,1540,6039 } },
6205 { "Canon EOS 1000D", 0, 0xe43,
6206 { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
6207 { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
6208 { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
6209 { "Canon EOS-1Ds Mark II", 0, 0xe80,
6210 { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
6211 { "Canon EOS-1D Mark IV", 0, 0x3bb0,
6212 { 6014,-220,-795,-4109,12014,2361,-561,1824,5787 } },
6213 { "Canon EOS-1D Mark III", 0, 0x3bb0,
6214 { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
6215 { "Canon EOS-1D Mark II N", 0, 0xe80,
6216 { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
6217 { "Canon EOS-1D Mark II", 0, 0xe80,
6218 { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
6219 { "Canon EOS-1DS", 0, 0xe20,
6220 { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
6221 { "Canon EOS-1D", 0, 0xe20,
6222 { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
6223 { "Canon EOS", 0, 0,
6224 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
6225 { "Canon PowerShot A530", 0, 0,
6226 { 0 } }, /* don't want the A5 matrix */
6227 { "Canon PowerShot A50", 0, 0,
6228 { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
6229 { "Canon PowerShot A5", 0, 0,
6230 { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
6231 { "Canon PowerShot G10", 0, 0,
6232 { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
6233 { "Canon PowerShot G11", 0, 0,
6234 { 12177,-4817,-1069,-1612,9864,2049,-98,850,4471 } },
6235 { "Canon PowerShot G12", 0, 0,
6236 { 0 } },
6237 { "Canon PowerShot G1", 0, 0,
6238 { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
6239 { "Canon PowerShot G2", 0, 0,
6240 { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
6241 { "Canon PowerShot G3", 0, 0,
6242 { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
6243 { "Canon PowerShot G5", 0, 0,
6244 { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
6245 { "Canon PowerShot G6", 0, 0,
6246 { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
6247 { "Canon PowerShot G9", 0, 0,
6248 { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
6249 { "Canon PowerShot Pro1", 0, 0,
6250 { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
6251 { "Canon PowerShot Pro70", 34, 0,
6252 { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
6253 { "Canon PowerShot Pro90", 0, 0,
6254 { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
6255 { "Canon PowerShot S30", 0, 0,
6256 { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
6257 { "Canon PowerShot S40", 0, 0,
6258 { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
6259 { "Canon PowerShot S45", 0, 0,
6260 { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
6261 { "Canon PowerShot S50", 0, 0,
6262 { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
6263 { "Canon PowerShot S60", 0, 0,
6264 { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
6265 { "Canon PowerShot S70", 0, 0,
6266 { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
6267 { "Canon PowerShot S90", 0, 0,
6268 { 12374,-5016,-1049,-1677,9902,2078,-83,852,4683 } },
6269 { "Canon PowerShot S95", 0, 0,
6270 { 13610,-5487,-1268,-1373,9773,1601,-824,1545,3772 } },
6271 { "Canon PowerShot A470", 0, 0, /* DJC */
6272 { 12513,-4407,-1242,-2680,10276,2405,-878,2215,4734 } },
6273 { "Canon PowerShot A610", 0, 0, /* DJC */
6274 { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
6275 { "Canon PowerShot A620", 0, 0, /* DJC */
6276 { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
6277 { "Canon PowerShot A630", 0, 0, /* DJC */
6278 { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
6279 { "Canon PowerShot A640", 0, 0, /* DJC */
6280 { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
6281 { "Canon PowerShot A650", 0, 0, /* DJC */
6282 { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
6283 { "Canon PowerShot A720", 0, 0, /* DJC */
6284 { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
6285 { "Canon PowerShot S3 IS", 0, 0, /* DJC */
6286 { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
6287 { "Canon PowerShot SX1 IS", 0, 0,
6288 { 6578,-259,-502,-5974,13030,3309,-308,1058,4970 } },
6289 { "Canon PowerShot SX110 IS", 0, 0, /* DJC */
6290 { 14134,-5576,-1527,-1991,10719,1273,-1158,1929,3581 } },
6291 { "CASIO EX-S20", 0, 0, /* DJC */
6292 { 11634,-3924,-1128,-4968,12954,2015,-1588,2648,7206 } },
6293 { "CASIO EX-Z750", 0, 0, /* DJC */
6294 { 10819,-3873,-1099,-4903,13730,1175,-1755,3751,4632 } },
6295 { "CINE 650", 0, 0,
6296 { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
6297 { "CINE 660", 0, 0,
6298 { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
6299 { "CINE", 0, 0,
6300 { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
6301 { "Contax N Digital", 0, 0xf1e,
6302 { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
6303 { "EPSON R-D1", 0, 0,
6304 { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
6305 { "FUJIFILM FinePix E550", 0, 0,
6306 { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
6307 { "FUJIFILM FinePix E900", 0, 0,
6308 { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
6309 { "FUJIFILM FinePix F8", 0, 0,
6310 { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
6311 { "FUJIFILM FinePix F7", 0, 0,
6312 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
6313 { "FUJIFILM FinePix S100FS", 514, 0,
6314 { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
6315 { "FUJIFILM FinePix S200EXR", 512, 0x3fff,
6316 { 0 } },
6317 { "FUJIFILM FinePix S20Pro", 0, 0,
6318 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
6319 { "FUJIFILM FinePix S2Pro", 128, 0,
6320 { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
6321 { "FUJIFILM FinePix S3Pro", 0, 0,
6322 { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
6323 { "FUJIFILM FinePix S5Pro", 0, 0,
6324 { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
6325 { "FUJIFILM FinePix S5000", 0, 0,
6326 { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
6327 { "FUJIFILM FinePix S5100", 0, 0,
6328 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
6329 { "FUJIFILM FinePix S5500", 0, 0,
6330 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
6331 { "FUJIFILM FinePix S5200", 0, 0,
6332 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
6333 { "FUJIFILM FinePix S5600", 0, 0,
6334 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
6335 { "FUJIFILM FinePix S6", 0, 0,
6336 { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
6337 { "FUJIFILM FinePix S7000", 0, 0,
6338 { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
6339 { "FUJIFILM FinePix S9000", 0, 0,
6340 { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
6341 { "FUJIFILM FinePix S9500", 0, 0,
6342 { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
6343 { "FUJIFILM FinePix S9100", 0, 0,
6344 { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
6345 { "FUJIFILM FinePix S9600", 0, 0,
6346 { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
6347 { "FUJIFILM IS-1", 0, 0,
6348 { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
6349 { "FUJIFILM IS Pro", 0, 0,
6350 { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
6351 { "FUJIFILM FinePix HS10 HS11", 0, 0xf68,
6352 { 12440,-3954,-1183,-1123,9674,1708,-83,1614,4086 } },
6353 { "Imacon Ixpress", 0, 0, /* DJC */
6354 { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
6355 { "KODAK NC2000", 0, 0,
6356 { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
6357 { "Kodak DCS315C", 8, 0,
6358 { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
6359 { "Kodak DCS330C", 8, 0,
6360 { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
6361 { "KODAK DCS420", 0, 0,
6362 { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
6363 { "KODAK DCS460", 0, 0,
6364 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
6365 { "KODAK EOSDCS1", 0, 0,
6366 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
6367 { "KODAK EOSDCS3B", 0, 0,
6368 { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
6369 { "Kodak DCS520C", 180, 0,
6370 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
6371 { "Kodak DCS560C", 188, 0,
6372 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
6373 { "Kodak DCS620C", 180, 0,
6374 { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
6375 { "Kodak DCS620X", 185, 0,
6376 { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
6377 { "Kodak DCS660C", 214, 0,
6378 { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
6379 { "Kodak DCS720X", 0, 0,
6380 { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
6381 { "Kodak DCS760C", 0, 0,
6382 { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
6383 { "Kodak DCS Pro SLR", 0, 0,
6384 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
6385 { "Kodak DCS Pro 14nx", 0, 0,
6386 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
6387 { "Kodak DCS Pro 14", 0, 0,
6388 { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
6389 { "Kodak ProBack645", 0, 0,
6390 { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
6391 { "Kodak ProBack", 0, 0,
6392 { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
6393 { "KODAK P712", 0, 0,
6394 { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
6395 { "KODAK P850", 0, 0xf7c,
6396 { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
6397 { "KODAK P880", 0, 0xfff,
6398 { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
6399 { "KODAK EasyShare Z980", 0, 0,
6400 { 11313,-3559,-1101,-3893,11891,2257,-1214,2398,4908 } },
6401 { "KODAK EasyShare Z981", 0, 0,
6402 { 12729,-4717,-1188,-1367,9187,2582,274,860,4411 } },
6403 { "KODAK EASYSHARE Z1015", 0, 0xef1,
6404 { 11265,-4286,-992,-4694,12343,2647,-1090,1523,5447 } },
6405 { "Leaf CMost", 0, 0,
6406 { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
6407 { "Leaf Valeo 6", 0, 0,
6408 { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
6409 { "Leaf Aptus 54S", 0, 0,
6410 { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
6411 { "Leaf Aptus 65", 0, 0,
6412 { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
6413 { "Leaf Aptus 75", 0, 0,
6414 { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
6415 { "Leaf", 0, 0,
6416 { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
6417 { "Mamiya ZD", 0, 0,
6418 { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
6419 { "Micron 2010", 110, 0, /* DJC */
6420 { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
6421 { "Minolta DiMAGE 5", 0, 0xf7d,
6422 { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
6423 { "Minolta DiMAGE 7Hi", 0, 0xf7d,
6424 { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
6425 { "Minolta DiMAGE 7", 0, 0xf7d,
6426 { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
6427 { "Minolta DiMAGE A1", 0, 0xf8b,
6428 { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
6429 { "MINOLTA DiMAGE A200", 0, 0,
6430 { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
6431 { "Minolta DiMAGE A2", 0, 0xf8f,
6432 { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
6433 { "Minolta DiMAGE Z2", 0, 0, /* DJC */
6434 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
6435 { "MINOLTA DYNAX 5", 0, 0xffb,
6436 { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
6437 { "MINOLTA DYNAX 7", 0, 0xffb,
6438 { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
6439 { "MOTOROLA PIXL", 0, 0, /* DJC */
6440 { 8898,-989,-1033,-3292,11619,1674,-661,3178,5216 } },
6441 { "NIKON D100", 0, 0,
6442 { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
6443 { "NIKON D1H", 0, 0,
6444 { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
6445 { "NIKON D1X", 0, 0,
6446 { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
6447 { "NIKON D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
6448 { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
6449 { "NIKON D200", 0, 0xfbc,
6450 { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
6451 { "NIKON D2H", 0, 0,
6452 { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
6453 { "NIKON D2X", 0, 0,
6454 { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
6455 { "NIKON D3000", 0, 0,
6456 { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
6457 { "NIKON D300", 0, 0,
6458 { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
6459 { "NIKON D3X", 0, 0,
6460 { 7171,-1986,-648,-8085,15555,2718,-2170,2512,7457 } },
6461 { "NIKON D3S", 0, 0,
6462 { 8828,-2406,-694,-4874,12603,2541,-660,1509,7587 } },
6463 { "NIKON D3", 0, 0,
6464 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
6465 { "NIKON D40X", 0, 0,
6466 { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
6467 { "NIKON D40", 0, 0,
6468 { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
6469 { "NIKON D5000", 0, 0xf00,
6470 { 7309,-1403,-519,-8474,16008,2622,-2433,2826,8064 } },
6471 { "NIKON D50", 0, 0,
6472 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
6473 { "NIKON D60", 0, 0,
6474 { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
6475 { "NIKON D7000", 0, 0, /* DJC */
6476 { 6629,-2254,-2,-3468,9387,4081,-760,2102,7574 } },
6477 { "NIKON D700", 0, 0,
6478 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
6479 { "NIKON D70", 0, 0,
6480 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
6481 { "NIKON D80", 0, 0,
6482 { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
6483 { "NIKON D90", 0, 0xf00,
6484 { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
6485 { "NIKON E950", 0, 0x3dd, /* DJC */
6486 { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
6487 { "NIKON E995", 0, 0, /* copied from E5000 */
6488 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6489 { "NIKON E2100", 0, 0, /* copied from Z2, new white balance */
6490 { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711} },
6491 { "NIKON E2500", 0, 0,
6492 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6493 { "NIKON E3200", 0, 0, /* DJC */
6494 { 9846,-2085,-1019,-3278,11109,2170,-774,2134,5745 } },
6495 { "NIKON E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
6496 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
6497 { "NIKON E4500", 0, 0,
6498 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6499 { "NIKON E5000", 0, 0,
6500 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6501 { "NIKON E5400", 0, 0,
6502 { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
6503 { "NIKON E5700", 0, 0,
6504 { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
6505 { "NIKON E8400", 0, 0,
6506 { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
6507 { "NIKON E8700", 0, 0,
6508 { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
6509 { "NIKON E8800", 0, 0,
6510 { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
6511 { "NIKON COOLPIX P6000", 0, 0,
6512 { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
6513 { "OLYMPUS C5050", 0, 0,
6514 { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
6515 { "OLYMPUS C5060", 0, 0,
6516 { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
6517 { "OLYMPUS C7070", 0, 0,
6518 { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
6519 { "OLYMPUS C70", 0, 0,
6520 { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
6521 { "OLYMPUS C80", 0, 0,
6522 { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
6523 { "OLYMPUS E-10", 0, 0xffc,
6524 { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
6525 { "OLYMPUS E-1", 0, 0,
6526 { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
6527 { "OLYMPUS E-20", 0, 0xffc,
6528 { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
6529 { "OLYMPUS E-300", 0, 0,
6530 { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
6531 { "OLYMPUS E-330", 0, 0,
6532 { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
6533 { "OLYMPUS E-30", 0, 0xfbc,
6534 { 8144,-1861,-1111,-7763,15894,1929,-1865,2542,7607 } },
6535 { "OLYMPUS E-3", 0, 0xf99,
6536 { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
6537 { "OLYMPUS E-400", 0, 0,
6538 { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
6539 { "OLYMPUS E-410", 0, 0xf6a,
6540 { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
6541 { "OLYMPUS E-420", 0, 0xfd7,
6542 { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
6543 { "OLYMPUS E-450", 0, 0xfd2,
6544 { 8745,-2425,-1095,-7594,15613,2073,-1780,2309,7416 } },
6545 { "OLYMPUS E-500", 0, 0,
6546 { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
6547 { "OLYMPUS E-510", 0, 0xf6a,
6548 { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
6549 { "OLYMPUS E-520", 0, 0xfd2,
6550 { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
6551 { "OLYMPUS E-5", 0, 0, /* DJC */
6552 { 10033,-4067,-600,-3784,10494,3291,-923,2594,7744 } },
6553 { "OLYMPUS E-600", 0, 0xfaf,
6554 { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
6555 { "OLYMPUS E-620", 0, 0xfaf,
6556 { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
6557 { "OLYMPUS E-P1", 0, 0xffd,
6558 { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
6559 { "OLYMPUS E-P2", 0, 0xffd,
6560 { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
6561 { "OLYMPUS E-PL1", 0, 0,
6562 { 11408,-4289,-1215,-4286,12385,2118,-387,1467,7787 } },
6563 { "OLYMPUS SP350", 0, 0,
6564 { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
6565 { "OLYMPUS SP3", 0, 0,
6566 { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
6567 { "OLYMPUS SP500UZ", 0, 0xfff,
6568 { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
6569 { "OLYMPUS SP510UZ", 0, 0xffe,
6570 { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
6571 { "OLYMPUS SP550UZ", 0, 0xffe,
6572 { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
6573 { "OLYMPUS SP560UZ", 0, 0xff9,
6574 { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
6575 { "OLYMPUS SP570UZ", 0, 0,
6576 { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
6577 { "PENTAX *ist DL2", 0, 0,
6578 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
6579 { "PENTAX *ist DL", 0, 0,
6580 { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
6581 { "PENTAX *ist DS2", 0, 0,
6582 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
6583 { "PENTAX *ist DS", 0, 0,
6584 { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
6585 { "PENTAX *ist D", 0, 0,
6586 { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
6587 { "PENTAX K10D", 0, 0,
6588 { 9566,-2863,-803,-7170,15172,2112,-818,803,9705 } },
6589 { "PENTAX K1", 0, 0,
6590 { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
6591 { "PENTAX K20D", 0, 0,
6592 { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
6593 { "PENTAX K200D", 0, 0,
6594 { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
6595 { "PENTAX K2000", 0, 0,
6596 { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
6597 { "PENTAX K-m", 0, 0,
6598 { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
6599 { "PENTAX K-x", 0, 0,
6600 { 8843,-2837,-625,-5025,12644,2668,-411,1234,7410 } },
6601 { "PENTAX K-r", 0, 0, /* Pentax DNG */
6602 { 21746,-6684,-1521,-10014,26601,6573,-1920,3062,19415 } },
6603 { "PENTAX K-5", 0, 0, /* Pentax DNG */
6604 { 19331,-5842,-2589,-9103,28027,4285,-2216,3884,14767 } },
6605 { "PENTAX K-7", 0, 0,
6606 { 9142,-2947,-678,-8648,16967,1663,-2224,2898,8615 } },
6607 { "PENTAX 645D", 0, 0x3e00,
6608 { 10646,-3593,-1158,-3329,11699,1831,-667,2874,6287 } },
6609 { "Panasonic DMC-FZ8", 0, 0xf7f,
6610 { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
6611 { "Panasonic DMC-FZ18", 0, 0,
6612 { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
6613 { "Panasonic DMC-FZ28", 15, 0xf96,
6614 { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
6615 { "Panasonic DMC-FZ30", 0, 0xf94,
6616 { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
6617 { "Panasonic DMC-FZ3", 143, 0,
6618 { 9938,-2780,-890,-4604,12393,2480,-1117,2304,4620 } },
6619 { "Panasonic DMC-FZ40", 143, 0,
6620 { 13639,-5535,-1371,-1698,9633,2430,316,1152,4108 } },
6621 { "Panasonic DMC-FZ50", 0, 0,
6622 { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
6623 { "LEICA V-LUX1", 0, 0,
6624 { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
6625 { "Panasonic DMC-L10", 15, 0xf96,
6626 { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
6627 { "Panasonic DMC-L1", 0, 0xf7f,
6628 { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
6629 { "LEICA DIGILUX 3", 0, 0xf7f,
6630 { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
6631 { "Panasonic DMC-LC1", 0, 0,
6632 { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
6633 { "LEICA DIGILUX 2", 0, 0,
6634 { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
6635 { "Panasonic DMC-LX1", 0, 0xf7f,
6636 { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
6637 { "LEICA D-LUX2", 0, 0xf7f,
6638 { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
6639 { "Panasonic DMC-LX2", 0, 0,
6640 { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
6641 { "LEICA D-LUX3", 0, 0,
6642 { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
6643 { "Panasonic DMC-LX3", 15, 0,
6644 { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
6645 { "LEICA D-LUX4", 15, 0,
6646 { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
6647 { "Panasonic DMC-LX5", 143, 0,
6648 { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
6649 { "Panasonic DMC-FZ100", 143, 0xfff,
6650 { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
6651 { "Panasonic DMC-FX150", 15, 0xfff,
6652 { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
6653 { "Panasonic DMC-G10", 0, 0,
6654 { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
6655 { "Panasonic DMC-G1", 15, 0xf94,
6656 { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
6657 { "Panasonic DMC-G2", 15, 0xf3c,
6658 { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
6659 { "Panasonic DMC-GF1", 15, 0xf92,
6660 { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
6661 { "Panasonic DMC-GH1", 15, 0xf92,
6662 { 6299,-1466,-532,-6535,13852,2969,-2331,3112,5984 } },
6663 { "Phase One H 20", 0, 0, /* DJC */
6664 { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
6665 { "Phase One P 2", 0, 0,
6666 { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
6667 { "Phase One P 30", 0, 0,
6668 { 4516,-245,-37,-7020,14976,2173,-3206,4671,7087 } },
6669 { "Phase One P 45", 0, 0,
6670 { 5053,-24,-117,-5684,14076,1702,-2619,4492,5849 } },
6671 { "Phase One P65", 0, 0,
6672 { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
6673 { "SAMSUNG EX1", 0, 0x3e00,
6674 { 8898,-2498,-994,-3144,11328,2066,-760,1381,4576 } },
6675 { "SAMSUNG NX10", 20, 0xea6,
6676 { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
6677 { "SAMSUNG WB2000", 0, 0xfff,
6678 { 0 } },
6679 { "SAMSUNG GX-1", 0, 0,
6680 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
6681 { "SAMSUNG S85", 0, 0xffff, /* DJC */
6682 { 11885,-3968,-1473,-4214,12299,1916,-835,1655,5549 } },
6683 { "Sinar", 0, 0, /* DJC */
6684 { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
6685 { "SONY DSC-F828", 491, 0,
6686 { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
6687 { "SONY DSC-R1", 512, 0,
6688 { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
6689 { "SONY DSC-V3", 0, 0,
6690 { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
6691 { "SONY DSLR-A100", 0, 0xfeb,
6692 { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
6693 { "SONY DSLR-A2", 0, 0,
6694 { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
6695 { "SONY DSLR-A300", 0, 0,
6696 { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
6697 { "SONY DSLR-A330", 0, 0,
6698 { 9847,-3091,-929,-8485,16346,2225,-714,595,7103 } },
6699 { "SONY DSLR-A350", 0, 0xffc,
6700 { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
6701 { "SONY DSLR-A380", 0, 0,
6702 { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
6703 { "SONY DSLR-A450", 128, 0xfeb,
6704 { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
6705 { "SONY DSLR-A5", 128, 0xfeb,
6706 { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
6707 { "SONY DSLR-A700", 126, 0,
6708 { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
6709 { "SONY DSLR-A850", 128, 0,
6710 { 5413,-1162,-365,-5665,13098,2866,-608,1179,8440 } },
6711 { "SONY DSLR-A900", 128, 0,
6712 { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } },
6713 { "SONY NEX-3", 138, 0, /* DJC */
6714 { 6907,-1256,-645,-4940,12621,2320,-1710,2581,6230 } },
6715 { "SONY NEX-5", 116, 0, /* DJC */
6716 { 6807,-1350,-342,-4216,11649,2567,-1089,2001,6420 } },
6717 { "SONY NEX", 128, 0, /* Adobe's matrix */
6718 { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
6719 { "SONY SLT-A33", 128, 0,
6720 { 6069,-1221,-366,-5221,12779,2734,-1024,2066,6834 } },
6721 { "SONY SLT-A55", 128, 0,
6722 { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } }
6723 };
6724 double cam_xyz[4][3];
6725 char name[130];
6726 int i, j, k;
6727
6728 sprintf (name, "%s %s", make, model);
6729 for (i=0; i < (int) sizeof table / (int) sizeof *table; i++)
6730 if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
6731 if (table[i].black) black = (ushort) table[i].black;
6732 if (table[i].maximum) maximum = (ushort) table[i].maximum;
6733 if (table[i].trans[0]) {
6734 for (j=0; j < 4; j++)
6735 for (k=0; k < 3; k++)
6736 cam_xyz[j][k] = table[i].trans[3*j+k] / 10000.0;
6737 cam_xyz_coeff (cam_xyz);
6738 }
6739 break;
6740 }
6741 }
6742
simple_coeff(int index)6743 void CLASS simple_coeff (int index)
6744 {
6745 static const float table[][12] = {
6746 /* index 0 -- all Foveon cameras */
6747 { 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
6748 /* index 1 -- Kodak DC20 and DC25 */
6749 { 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
6750 /* index 2 -- Logitech Fotoman Pixtura */
6751 { 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
6752 /* index 3 -- Nikon E880, E900, and E990 */
6753 { -1.936280, 1.800443, -1.448486, 2.584324,
6754 1.405365, -0.524955, -0.289090, 0.408680,
6755 -1.204965, 1.082304, 2.941367, -1.818705 }
6756 };
6757 int i, c;
6758
6759 for (raw_color = i=0; i < 3; i++)
6760 FORCC rgb_cam[i][c] = table[index][i*colors+c];
6761 }
6762
guess_byte_order(int words)6763 short CLASS guess_byte_order (int words)
6764 {
6765 uchar test[4][2];
6766 int t=2, msb;
6767 double diff, sum[2] = {0,0};
6768
6769 fread (test[0], 2, 2, ifp);
6770 for (words-=2; words--; ) {
6771 fread (test[t], 2, 1, ifp);
6772 for (msb=0; msb < 2; msb++) {
6773 diff = (test[t^2][msb] << 8 | test[t^2][!msb])
6774 - (test[t ][msb] << 8 | test[t ][!msb]);
6775 sum[msb] += diff*diff;
6776 }
6777 t = (t+1) & 3;
6778 }
6779 return sum[0] < sum[1] ? 0x4d4d : 0x4949;
6780 }
6781
find_green(int bps,int bite,int off0,int off1)6782 float CLASS find_green (int bps, int bite, int off0, int off1)
6783 {
6784 UINT64 bitbuf=0;
6785 int vbits, col, i, c;
6786 ushort img[2][2064];
6787 double sum[]={0,0};
6788
6789 FORC(2) {
6790 fseek (ifp, c ? off1:off0, SEEK_SET);
6791 for (vbits=col=0; col < width; col++) {
6792 for (vbits -= bps; vbits < 0; vbits += bite) {
6793 bitbuf <<= bite;
6794 for (i=0; i < bite; i+=8)
6795 bitbuf |= (unsigned) (fgetc(ifp) << i);
6796 }
6797 img[c][col] = bitbuf << (64-bps-vbits) >> (64-bps);
6798 }
6799 }
6800 FORC(width-1) {
6801 sum[ c & 1] += ABS(img[0][c]-img[1][c+1]);
6802 sum[~c & 1] += ABS(img[1][c]-img[0][c+1]);
6803 }
6804 return 100 * log(sum[0]/sum[1]);
6805 }
6806
6807 /*
6808 Identify which camera created this file, and set global variables
6809 accordingly.
6810 */
identify()6811 void CLASS identify()
6812 {
6813 char head[32], *cp;
6814 int hlen, flen, fsize, zero_fsize=1, i, c, is_canon;
6815 struct jhead jh;
6816 short pana[][6] = {
6817 { 3130, 1743, 4, 0, -6, 0 },
6818 { 3130, 2055, 4, 0, -6, 0 },
6819 { 3130, 2319, 4, 0, -6, 0 },
6820 { 3170, 2103, 18, 0,-42, 20 },
6821 { 3170, 2367, 18, 13,-42,-21 },
6822 { 3177, 2367, 0, 0, -1, 0 },
6823 { 3304, 2458, 0, 0, -1, 0 },
6824 { 3330, 2463, 9, 0, -5, 0 },
6825 { 3330, 2479, 9, 0,-17, 4 },
6826 { 3370, 1899, 15, 0,-44, 20 },
6827 { 3370, 2235, 15, 0,-44, 20 },
6828 { 3370, 2511, 15, 10,-44,-21 },
6829 { 3690, 2751, 3, 0, -8, -3 },
6830 { 3710, 2751, 0, 0, -3, 0 },
6831 { 3724, 2450, 0, 0, 0, -2 },
6832 { 3770, 2487, 17, 0,-44, 19 },
6833 { 3770, 2799, 17, 15,-44,-19 },
6834 { 3880, 2170, 6, 0, -6, 0 },
6835 { 4060, 3018, 0, 0, 0, -2 },
6836 { 4290, 2391, 3, 0, -8, -1 },
6837 { 4330, 2439, 17, 15,-44,-19 },
6838 { 4508, 2962, 0, 0, -3, -4 },
6839 { 4508, 3330, 0, 0, -3, -6 } };
6840 static const struct {
6841 int fsize;
6842 const char make[12], model[19], withjpeg;
6843 } table[] = {
6844 { 62464, "Kodak", "DC20" ,0 },
6845 { 124928, "Kodak", "DC20" ,0 },
6846 { 1652736, "Kodak", "DCS200" ,0 },
6847 { 4159302, "Kodak", "C330" ,0 },
6848 { 4162462, "Kodak", "C330" ,0 },
6849 { 460800, "Kodak", "C603v" ,0 },
6850 { 614400, "Kodak", "C603v" ,0 },
6851 { 6163328, "Kodak", "C603" ,0 },
6852 { 6166488, "Kodak", "C603" ,0 },
6853 { 9116448, "Kodak", "C603y" ,0 },
6854 { 311696, "ST Micro", "STV680 VGA" ,0 }, /* SPYz */
6855 { 787456, "Creative", "PC-CAM 600" ,0 },
6856 { 1138688, "Minolta", "RD175" ,0 },
6857 { 3840000, "Foculus", "531C" ,0 },
6858 { 786432, "AVT", "F-080C" ,0 },
6859 { 1447680, "AVT", "F-145C" ,0 },
6860 { 1920000, "AVT", "F-201C" ,0 },
6861 { 5067304, "AVT", "F-510C" ,0 },
6862 { 5067316, "AVT", "F-510C" ,0 },
6863 { 10134608, "AVT", "F-510C" ,0 },
6864 { 10134620, "AVT", "F-510C" ,0 },
6865 { 16157136, "AVT", "F-810C" ,0 },
6866 { 1409024, "Sony", "XCD-SX910CR" ,0 },
6867 { 2818048, "Sony", "XCD-SX910CR" ,0 },
6868 { 3884928, "Micron", "2010" ,0 },
6869 { 6624000, "Pixelink", "A782" ,0 },
6870 { 13248000, "Pixelink", "A782" ,0 },
6871 { 6291456, "RoverShot","3320AF" ,0 },
6872 { 6553440, "Canon", "PowerShot A460" ,0 },
6873 { 6653280, "Canon", "PowerShot A530" ,0 },
6874 { 6573120, "Canon", "PowerShot A610" ,0 },
6875 { 9219600, "Canon", "PowerShot A620" ,0 },
6876 { 9243240, "Canon", "PowerShot A470" ,0 },
6877 { 10341600, "Canon", "PowerShot A720 IS",0 },
6878 { 10383120, "Canon", "PowerShot A630" ,0 },
6879 { 12945240, "Canon", "PowerShot A640" ,0 },
6880 { 15636240, "Canon", "PowerShot A650" ,0 },
6881 { 5298000, "Canon", "PowerShot SD300" ,0 },
6882 { 7710960, "Canon", "PowerShot S3 IS" ,0 },
6883 { 15467760, "Canon", "PowerShot SX110 IS",0 },
6884 { 15534576, "Canon", "PowerShot SX120 IS",0 },
6885 { 18653760, "Canon", "PowerShot SX20 IS",0 },
6886 { 5939200, "OLYMPUS", "C770UZ" ,0 },
6887 { 1581060, "NIKON", "E900" ,1 }, /* or E900s,E910 */
6888 { 2465792, "NIKON", "E950" ,1 }, /* or E800,E700 */
6889 { 2940928, "NIKON", "E2100" ,1 }, /* or E2500 */
6890 { 4771840, "NIKON", "E990" ,1 }, /* or E995, Oly C3030Z */
6891 { 4775936, "NIKON", "E3700" ,1 }, /* or Optio 33WR */
6892 { 5869568, "NIKON", "E4300" ,1 }, /* or DiMAGE Z2 */
6893 { 5865472, "NIKON", "E4500" ,1 },
6894 { 7438336, "NIKON", "E5000" ,1 }, /* or E5700 */
6895 { 8998912, "NIKON", "COOLPIX S6" ,1 },
6896 { 1976352, "CASIO", "QV-2000UX" ,1 },
6897 { 3217760, "CASIO", "QV-3*00EX" ,1 },
6898 { 6218368, "CASIO", "QV-5700" ,1 },
6899 { 6054400, "CASIO", "QV-R41" ,1 },
6900 { 7530816, "CASIO", "QV-R51" ,1 },
6901 { 7684000, "CASIO", "QV-4000" ,1 },
6902 { 2937856, "CASIO", "EX-S20" ,1 },
6903 { 4948608, "CASIO", "EX-S100" ,1 },
6904 { 7542528, "CASIO", "EX-Z50" ,1 },
6905 { 7753344, "CASIO", "EX-Z55" ,1 },
6906 { 7816704, "CASIO", "EX-Z60" ,1 },
6907 { 10843712, "CASIO", "EX-Z75" ,1 },
6908 { 10834368, "CASIO", "EX-Z750" ,1 },
6909 { 12310144, "CASIO", "EX-Z850" ,1 },
6910 { 15499264, "CASIO", "EX-Z1050" ,1 },
6911 { 7426656, "CASIO", "EX-P505" ,1 },
6912 { 9313536, "CASIO", "EX-P600" ,1 },
6913 { 10979200, "CASIO", "EX-P700" ,1 },
6914 { 3178560, "PENTAX", "Optio S" ,1 },
6915 { 4841984, "PENTAX", "Optio S" ,1 },
6916 { 6114240, "PENTAX", "Optio S4" ,1 }, /* or S4i, CASIO EX-Z4 */
6917 { 10702848, "PENTAX", "Optio 750Z" ,1 },
6918 { 15980544, "AGFAPHOTO","DC-833m" ,1 },
6919 { 16098048, "SAMSUNG", "S85" ,1 },
6920 { 16215552, "SAMSUNG", "S85" ,1 },
6921 { 20487168, "SAMSUNG", "WB550" ,1 },
6922 { 24000000, "SAMSUNG", "WB550" ,1 },
6923 { 12582980, "Sinar", "" ,0 },
6924 { 33292868, "Sinar", "" ,0 },
6925 { 44390468, "Sinar", "" ,0 } };
6926 static const char *corp[] =
6927 { "Canon", "NIKON", "EPSON", "KODAK", "Kodak", "OLYMPUS", "PENTAX",
6928 "MINOLTA", "Minolta", "Konica", "CASIO", "Sinar", "Phase One",
6929 "SAMSUNG", "Mamiya", "MOTOROLA" };
6930
6931 tiff_flip = flip = filters = -1; /* 0 is valid, so -1 is unknown */
6932 raw_height = raw_width = fuji_width = fuji_layout = cr2_slice[0] = 0;
6933 maximum = height = width = top_margin = left_margin = 0;
6934 cdesc[0] = desc[0] = artist[0] = make[0] = model[0] = model2[0] = 0;
6935 iso_speed = shutter = aperture = focal_len = unique_id = 0;
6936 tiff_nifds = 0;
6937 memset (tiff_ifd, 0, sizeof tiff_ifd);
6938 memset (gpsdata, 0, sizeof gpsdata);
6939 memset (cblack, 0, sizeof cblack);
6940 memset (white, 0, sizeof white);
6941 thumb_offset = thumb_length = thumb_width = thumb_height = 0;
6942 load_raw = thumb_load_raw = 0;
6943 write_thumb = &CLASS jpeg_thumb;
6944 data_offset = meta_length = tiff_bps = tiff_compress = 0;
6945 kodak_cbpp = zero_after_ff = dng_version = load_flags = 0;
6946 timestamp = shot_order = tiff_samples = black = is_foveon = 0;
6947 mix_green = profile_length = data_error = zero_is_bad = 0;
6948 pixel_aspect = is_raw = raw_color = 1;
6949 tile_width = tile_length = INT_MAX;
6950 for (i=0; i < 4; i++) {
6951 cam_mul[i] = i == 1;
6952 pre_mul[i] = i < 3;
6953 FORC3 cmatrix[c][i] = 0;
6954 FORC3 rgb_cam[c][i] = c == i;
6955 }
6956 colors = 3;
6957 for (i=0; i < 0x4000; i++) curve[i] = i;
6958
6959 order = get2();
6960 hlen = get4();
6961 fseek (ifp, 0, SEEK_SET);
6962 fread (head, 1, 32, ifp);
6963 fseek (ifp, 0, SEEK_END);
6964 flen = fsize = ftell(ifp);
6965 /* Note for Rawstudio maintainers, this check is not present in upstream dcraw */
6966 if (fsize < 32)
6967 return;
6968 if ((cp = (char *) memmem (head, 32, "MMMM", 4)) ||
6969 (cp = (char *) memmem (head, 32, "IIII", 4))) {
6970 parse_phase_one (cp-head);
6971 if (cp-head && parse_tiff(0)) apply_tiff();
6972 } else if (order == 0x4949 || order == 0x4d4d) {
6973 if (!memcmp (head+6,"HEAPCCDR",8)) {
6974 data_offset = hlen;
6975 parse_ciff (hlen, flen - hlen);
6976 } else if (parse_tiff(0)) apply_tiff();
6977 } else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
6978 !memcmp (head+6,"Exif",4)) {
6979 fseek (ifp, 4, SEEK_SET);
6980 data_offset = 4 + get2();
6981 fseek (ifp, data_offset, SEEK_SET);
6982 if (fgetc(ifp) != 0xff)
6983 parse_tiff(12);
6984 thumb_offset = 0;
6985 } else if (!memcmp (head+25,"ARECOYK",7)) {
6986 strcpy (make, "Contax");
6987 strcpy (model,"N Digital");
6988 fseek (ifp, 33, SEEK_SET);
6989 get_timestamp(1);
6990 fseek (ifp, 60, SEEK_SET);
6991 FORC4 cam_mul[c ^ (c >> 1)] = get4();
6992 } else if (!strcmp (head, "PXN")) {
6993 strcpy (make, "Logitech");
6994 strcpy (model,"Fotoman Pixtura");
6995 } else if (!strcmp (head, "qktk")) {
6996 strcpy (make, "Apple");
6997 strcpy (model,"QuickTake 100");
6998 } else if (!strcmp (head, "qktn")) {
6999 strcpy (make, "Apple");
7000 strcpy (model,"QuickTake 150");
7001 } else if (!memcmp (head,"FUJIFILM",8)) {
7002 fseek (ifp, 84, SEEK_SET);
7003 thumb_offset = get4();
7004 thumb_length = get4();
7005 fseek (ifp, 92, SEEK_SET);
7006 parse_fuji (get4());
7007 if (thumb_offset > 120) {
7008 fseek (ifp, 120, SEEK_SET);
7009 is_raw += (i = get4()) && 1;
7010 if (is_raw == 2 && shot_select)
7011 parse_fuji (i);
7012 }
7013 fseek (ifp, 100, SEEK_SET);
7014 parse_tiff (data_offset = get4());
7015 parse_tiff (thumb_offset+12);
7016 apply_tiff();
7017 } else if (!memcmp (head,"RIFF",4)) {
7018 fseek (ifp, 0, SEEK_SET);
7019 parse_riff();
7020 } else if (!memcmp (head,"\0\001\0\001\0@",6)) {
7021 fseek (ifp, 6, SEEK_SET);
7022 fread (make, 1, 8, ifp);
7023 fread (model, 1, 8, ifp);
7024 fread (model2, 1, 16, ifp);
7025 data_offset = get2();
7026 get2();
7027 raw_width = get2();
7028 raw_height = get2();
7029 load_raw = &CLASS nokia_load_raw;
7030 filters = 0x61616161;
7031 } else if (!memcmp (head,"NOKIARAW",8)) {
7032 strcpy (make, "NOKIA");
7033 strcpy (model, "X2");
7034 order = 0x4949;
7035 fseek (ifp, 300, SEEK_SET);
7036 data_offset = get4();
7037 i = get4();
7038 width = get2();
7039 height = get2();
7040 data_offset += i - width * 5 / 4 * height;
7041 load_raw = &CLASS nokia_load_raw;
7042 filters = 0x61616161;
7043 } else if (!memcmp (head,"DSC-Image",9))
7044 parse_rollei();
7045 else if (!memcmp (head,"PWAD",4))
7046 parse_sinar_ia();
7047 else if (!memcmp (head,"\0MRM",4))
7048 parse_minolta(0);
7049 else if (!memcmp (head,"FOVb",4))
7050 parse_foveon();
7051 else if (!memcmp (head,"CI",2))
7052 parse_cine();
7053 else
7054 for (zero_fsize=i=0; i < (int) sizeof table / (int) sizeof *table; i++)
7055 if (fsize == table[i].fsize) {
7056 strcpy (make, table[i].make );
7057 strcpy (model, table[i].model);
7058 if (table[i].withjpeg)
7059 parse_external_jpeg();
7060 }
7061 if (zero_fsize) fsize = 0;
7062 if (make[0] == 0) parse_smal (0, flen);
7063 if (make[0] == 0) parse_jpeg (is_raw = 0);
7064
7065 for (i=0; i < (int) sizeof corp / (int) sizeof *corp; i++)
7066 if (strstr (make, corp[i])) /* Simplify company names */
7067 strcpy (make, corp[i]);
7068 if (!strncmp (make,"KODAK",5) &&
7069 ((cp = strstr(model," DIGITAL CAMERA")) ||
7070 (cp = strstr(model," Digital Camera")) ||
7071 (cp = strstr(model,"FILE VERSION"))))
7072 *cp = 0;
7073 cp = make + strlen(make); /* Remove trailing spaces */
7074 while (*--cp == ' ') *cp = 0;
7075 cp = model + strlen(model);
7076 while (*--cp == ' ') *cp = 0;
7077 i = strlen(make); /* Remove make from model */
7078 if (!strncasecmp (model, make, i) && model[i++] == ' ')
7079 memmove (model, model+i, 64-i);
7080 if (!strncmp (model,"Digital Camera ",15))
7081 strcpy (model, model+15);
7082 desc[511] = artist[63] = make[63] = model[63] = model2[63] = 0;
7083 if (!is_raw) goto notraw;
7084
7085 if (!height) height = raw_height;
7086 if (!width) width = raw_width;
7087 if (fuji_width) {
7088 fuji_width = (raw_width+1)/2;
7089 width = height + fuji_width;
7090 height = width - 1;
7091 pixel_aspect = 1;
7092 }
7093 if (height == 2624 && width == 3936) /* Pentax K10D and Samsung GX10 */
7094 { height = 2616; width = 3896; }
7095 if (height == 3136 && width == 4864) /* Pentax K20D and Samsung GX20 */
7096 { height = 3124; width = 4688; filters = 0x16161616; }
7097 if (!strcmp(model,"K-r") || !strcmp(model,"K-x"))
7098 { width = 4309; filters = 0x16161616; }
7099 if (!strcmp(model,"K-5"))
7100 { left_margin = 10; width = 4950; filters = 0x16161616; }
7101 if (!strcmp(model,"K-7"))
7102 { height = 3122; width = 4684; filters = 0x16161616; top_margin = 2; }
7103 if (!strcmp(model,"645D"))
7104 { height = 5502; width = 7328; filters = 0x61616161; top_margin = 29;
7105 left_margin = 48; }
7106 if (height == 3014 && width == 4096) /* Ricoh GX200 */
7107 width = 4014;
7108 if (dng_version) {
7109 if (filters == UINT_MAX) filters = 0;
7110 if (filters) is_raw = tiff_samples;
7111 else colors = tiff_samples;
7112 if (tiff_compress == 1)
7113 load_raw = &CLASS adobe_dng_load_raw_nc;
7114 if (tiff_compress == 7)
7115 load_raw = &CLASS adobe_dng_load_raw_lj;
7116 goto dng_skip;
7117 }
7118 if ((is_canon = !strcmp(make,"Canon")))
7119 load_raw = memcmp (head+6,"HEAPCCDR",8) ?
7120 &CLASS lossless_jpeg_load_raw : &CLASS canon_compressed_load_raw;
7121 if (!strcmp(make,"NIKON")) {
7122 if (!load_raw)
7123 load_raw = &CLASS packed_load_raw;
7124 if (model[0] == 'E')
7125 load_flags |= !data_offset << 2 | 2;
7126 }
7127 if (!strcmp(make,"CASIO")) {
7128 load_raw = &CLASS packed_load_raw;
7129 maximum = 0xf7f;
7130 }
7131
7132 /* Set parameters based on camera name (for non-DNG files). */
7133
7134 if (is_foveon) {
7135 if (height*2 < width) pixel_aspect = 0.5;
7136 if (height > width) pixel_aspect = 2;
7137 filters = 0;
7138 load_raw = &CLASS foveon_load_raw;
7139 simple_coeff(0);
7140 } else if (is_canon && tiff_bps == 15) {
7141 switch (width) {
7142 case 3344: width -= 66;
7143 case 3872: width -= 6;
7144 }
7145 filters = 0;
7146 load_raw = &CLASS canon_sraw_load_raw;
7147 } else if (!strcmp(model,"PowerShot 600")) {
7148 height = 613;
7149 width = 854;
7150 raw_width = 896;
7151 pixel_aspect = 607/628.0;
7152 colors = 4;
7153 filters = 0xe1e4e1e4;
7154 load_raw = &CLASS canon_600_load_raw;
7155 } else if (!strcmp(model,"PowerShot A5") ||
7156 !strcmp(model,"PowerShot A5 Zoom")) {
7157 height = 773;
7158 width = 960;
7159 raw_width = 992;
7160 pixel_aspect = 256/235.0;
7161 colors = 4;
7162 filters = 0x1e4e1e4e;
7163 goto canon_a5;
7164 } else if (!strcmp(model,"PowerShot A50")) {
7165 height = 968;
7166 width = 1290;
7167 raw_width = 1320;
7168 colors = 4;
7169 filters = 0x1b4e4b1e;
7170 goto canon_a5;
7171 } else if (!strcmp(model,"PowerShot Pro70")) {
7172 height = 1024;
7173 width = 1552;
7174 colors = 4;
7175 filters = 0x1e4b4e1b;
7176 goto canon_a5;
7177 } else if (!strcmp(model,"PowerShot SD300")) {
7178 height = 1752;
7179 width = 2344;
7180 raw_height = 1766;
7181 raw_width = 2400;
7182 top_margin = 12;
7183 left_margin = 12;
7184 goto canon_a5;
7185 } else if (!strcmp(model,"PowerShot A460")) {
7186 height = 1960;
7187 width = 2616;
7188 raw_height = 1968;
7189 raw_width = 2664;
7190 top_margin = 4;
7191 left_margin = 4;
7192 goto canon_a5;
7193 } else if (!strcmp(model,"PowerShot A530")) {
7194 height = 1984;
7195 width = 2620;
7196 raw_height = 1992;
7197 raw_width = 2672;
7198 top_margin = 6;
7199 left_margin = 10;
7200 goto canon_a5;
7201 } else if (!strcmp(model,"PowerShot A610")) {
7202 if (canon_s2is()) strcpy (model+10, "S2 IS");
7203 height = 1960;
7204 width = 2616;
7205 raw_height = 1968;
7206 raw_width = 2672;
7207 top_margin = 8;
7208 left_margin = 12;
7209 goto canon_a5;
7210 } else if (!strcmp(model,"PowerShot A620")) {
7211 height = 2328;
7212 width = 3112;
7213 raw_height = 2340;
7214 raw_width = 3152;
7215 top_margin = 12;
7216 left_margin = 36;
7217 goto canon_a5;
7218 } else if (!strcmp(model,"PowerShot A470")) {
7219 height = 2328;
7220 width = 3096;
7221 raw_height = 2346;
7222 raw_width = 3152;
7223 top_margin = 6;
7224 left_margin = 12;
7225 goto canon_a5;
7226 } else if (!strcmp(model,"PowerShot A720 IS")) {
7227 height = 2472;
7228 width = 3298;
7229 raw_height = 2480;
7230 raw_width = 3336;
7231 top_margin = 5;
7232 left_margin = 6;
7233 goto canon_a5;
7234 } else if (!strcmp(model,"PowerShot A630")) {
7235 height = 2472;
7236 width = 3288;
7237 raw_height = 2484;
7238 raw_width = 3344;
7239 top_margin = 6;
7240 left_margin = 12;
7241 goto canon_a5;
7242 } else if (!strcmp(model,"PowerShot A640")) {
7243 height = 2760;
7244 width = 3672;
7245 raw_height = 2772;
7246 raw_width = 3736;
7247 top_margin = 6;
7248 left_margin = 12;
7249 goto canon_a5;
7250 } else if (!strcmp(model,"PowerShot A650")) {
7251 height = 3024;
7252 width = 4032;
7253 raw_height = 3048;
7254 raw_width = 4104;
7255 top_margin = 12;
7256 left_margin = 48;
7257 goto canon_a5;
7258 } else if (!strcmp(model,"PowerShot S3 IS")) {
7259 height = 2128;
7260 width = 2840;
7261 raw_height = 2136;
7262 raw_width = 2888;
7263 top_margin = 8;
7264 left_margin = 44;
7265 canon_a5:
7266 tiff_bps = 10;
7267 load_raw = &CLASS packed_load_raw;
7268 load_flags = 40;
7269 if (raw_width > 1600) zero_is_bad = 1;
7270 } else if (!strcmp(model,"PowerShot SX110 IS")) {
7271 height = 2760;
7272 width = 3684;
7273 raw_height = 2772;
7274 raw_width = 3720;
7275 top_margin = 12;
7276 left_margin = 6;
7277 load_raw = &CLASS packed_load_raw;
7278 load_flags = 40;
7279 zero_is_bad = 1;
7280 } else if (!strcmp(model,"PowerShot SX120 IS")) {
7281 height = 2742;
7282 width = 3664;
7283 raw_height = 2778;
7284 raw_width = 3728;
7285 top_margin = 18;
7286 left_margin = 16;
7287 filters = 0x49494949;
7288 load_raw = &CLASS packed_load_raw;
7289 load_flags = 40;
7290 zero_is_bad = 1;
7291 } else if (!strcmp(model,"PowerShot SX20 IS")) {
7292 height = 3024;
7293 width = 4032;
7294 raw_height = 3048;
7295 raw_width = 4080;
7296 top_margin = 12;
7297 left_margin = 24;
7298 load_raw = &CLASS packed_load_raw;
7299 load_flags = 40;
7300 zero_is_bad = 1;
7301 } else if (!strcmp(model,"PowerShot Pro90 IS")) {
7302 width = 1896;
7303 colors = 4;
7304 filters = 0xb4b4b4b4;
7305 } else if (is_canon && raw_width == 2144) {
7306 height = 1550;
7307 width = 2088;
7308 top_margin = 8;
7309 left_margin = 4;
7310 if (!strcmp(model,"PowerShot G1")) {
7311 colors = 4;
7312 filters = 0xb4b4b4b4;
7313 }
7314 } else if (is_canon && raw_width == 2224) {
7315 height = 1448;
7316 width = 2176;
7317 top_margin = 6;
7318 left_margin = 48;
7319 } else if (is_canon && raw_width == 2376) {
7320 height = 1720;
7321 width = 2312;
7322 top_margin = 6;
7323 left_margin = 12;
7324 } else if (is_canon && raw_width == 2672) {
7325 height = 1960;
7326 width = 2616;
7327 top_margin = 6;
7328 left_margin = 12;
7329 } else if (is_canon && raw_width == 3152) {
7330 height = 2056;
7331 width = 3088;
7332 top_margin = 12;
7333 left_margin = 64;
7334 if (unique_id == 0x80000170)
7335 adobe_coeff ("Canon","EOS 300D");
7336 } else if (is_canon && raw_width == 3160) {
7337 height = 2328;
7338 width = 3112;
7339 top_margin = 12;
7340 left_margin = 44;
7341 } else if (is_canon && raw_width == 3344) {
7342 height = 2472;
7343 width = 3288;
7344 top_margin = 6;
7345 left_margin = 4;
7346 } else if (!strcmp(model,"EOS D2000C")) {
7347 filters = 0x61616161;
7348 black = curve[200];
7349 } else if (is_canon && raw_width == 3516) {
7350 top_margin = 14;
7351 left_margin = 42;
7352 if (unique_id == 0x80000189)
7353 adobe_coeff ("Canon","EOS 350D");
7354 goto canon_cr2;
7355 } else if (is_canon && raw_width == 3596) {
7356 top_margin = 12;
7357 left_margin = 74;
7358 goto canon_cr2;
7359 } else if (is_canon && raw_width == 3744) {
7360 height = 2760;
7361 width = 3684;
7362 top_margin = 16;
7363 left_margin = 8;
7364 if (unique_id > 0x2720000) {
7365 top_margin = 12;
7366 left_margin = 52;
7367 }
7368 } else if (is_canon && raw_width == 3944) {
7369 height = 2602;
7370 width = 3908;
7371 top_margin = 18;
7372 left_margin = 30;
7373 } else if (is_canon && raw_width == 3948) {
7374 top_margin = 18;
7375 left_margin = 42;
7376 height -= 2;
7377 if (unique_id == 0x80000236)
7378 adobe_coeff ("Canon","EOS 400D");
7379 if (unique_id == 0x80000254)
7380 adobe_coeff ("Canon","EOS 1000D");
7381 goto canon_cr2;
7382 } else if (is_canon && raw_width == 3984) {
7383 top_margin = 20;
7384 left_margin = 76;
7385 height -= 2;
7386 goto canon_cr2;
7387 } else if (is_canon && raw_width == 4104) {
7388 height = 3024;
7389 width = 4032;
7390 top_margin = 12;
7391 left_margin = 48;
7392 } else if (is_canon && raw_width == 4152) {
7393 top_margin = 12;
7394 left_margin = 192;
7395 goto canon_cr2;
7396 } else if (is_canon && raw_width == 4312) {
7397 top_margin = 18;
7398 left_margin = 22;
7399 height -= 2;
7400 if (unique_id == 0x80000176)
7401 adobe_coeff ("Canon","EOS 450D");
7402 goto canon_cr2;
7403 } else if (is_canon && raw_width == 4476) {
7404 top_margin = 34;
7405 left_margin = 90;
7406 goto canon_cr2;
7407 } else if (is_canon && raw_width == 4480) {
7408 height = 3326;
7409 width = 4432;
7410 top_margin = 10;
7411 left_margin = 12;
7412 filters = 0x49494949;
7413 } else if (is_canon && raw_width == 4832) {
7414 top_margin = unique_id == 0x80000261 ? 51:26;
7415 left_margin = 62;
7416 if (unique_id == 0x80000252)
7417 adobe_coeff ("Canon","EOS 500D");
7418 goto canon_cr2;
7419 } else if (is_canon && raw_width == 5120) {
7420 height -= top_margin = 45;
7421 left_margin = 142;
7422 width = 4916;
7423 } else if (is_canon && raw_width == 5344) {
7424 top_margin = 51;
7425 left_margin = 142;
7426 if (unique_id == 0x80000270)
7427 adobe_coeff ("Canon","EOS 550D");
7428 goto canon_cr2;
7429 } else if (is_canon && raw_width == 5360) {
7430 top_margin = 51;
7431 left_margin = 158;
7432 goto canon_cr2;
7433 } else if (is_canon && raw_width == 5792) {
7434 top_margin = 51;
7435 left_margin = 158;
7436 goto canon_cr2;
7437 } else if (is_canon && raw_width == 5108) {
7438 top_margin = 13;
7439 left_margin = 98;
7440 canon_cr2:
7441 height -= top_margin;
7442 width -= left_margin;
7443 } else if (is_canon && raw_width == 5712) {
7444 height = 3752;
7445 width = 5640;
7446 top_margin = 20;
7447 left_margin = 62;
7448 } else if (!strcmp(model,"D1")) {
7449 cam_mul[0] *= 256/527.0;
7450 cam_mul[2] *= 256/317.0;
7451 } else if (!strcmp(model,"D1X")) {
7452 width -= 4;
7453 pixel_aspect = 0.5;
7454 } else if (!strcmp(model,"D40X") ||
7455 !strcmp(model,"D60") ||
7456 !strcmp(model,"D80") ||
7457 !strcmp(model,"D3000")) {
7458 height -= 3;
7459 width -= 4;
7460 } else if (!strcmp(model,"D3") ||
7461 !strcmp(model,"D3S") ||
7462 !strcmp(model,"D700")) {
7463 width -= 4;
7464 left_margin = 2;
7465 } else if (!strcmp(model,"D5000")) {
7466 width -= 42;
7467 } else if (!strcmp(model,"D7000")) {
7468 width -= 44;
7469 } else if (!strcmp(model,"D3100")) {
7470 width -= 28;
7471 left_margin = 6;
7472 } else if (!strncmp(model,"D40",3) ||
7473 !strncmp(model,"D50",3) ||
7474 !strncmp(model,"D70",3)) {
7475 width--;
7476 } else if (!strcmp(model,"D90")) {
7477 width -= 42;
7478 } else if (!strcmp(model,"D100")) {
7479 if (tiff_compress == 34713 && !nikon_is_compressed()) {
7480 load_raw = &CLASS packed_load_raw;
7481 load_flags |= 1;
7482 raw_width = (width += 3) + 3;
7483 }
7484 } else if (!strcmp(model,"D200")) {
7485 left_margin = 1;
7486 width -= 4;
7487 filters = 0x94949494;
7488 } else if (!strncmp(model,"D2H",3)) {
7489 left_margin = 6;
7490 width -= 14;
7491 } else if (!strncmp(model,"D2X",3)) {
7492 if (width == 3264) width -= 32;
7493 else width -= 8;
7494 } else if (!strncmp(model,"D300",4)) {
7495 width -= 32;
7496 } else if (!strncmp(model,"COOLPIX P",9)) {
7497 load_flags = 24;
7498 filters = 0x94949494;
7499 } else if (fsize == 1581060) {
7500 height = 963;
7501 width = 1287;
7502 raw_width = 1632;
7503 maximum = 0x3f4;
7504 colors = 4;
7505 filters = 0x1e1e1e1e;
7506 simple_coeff(3);
7507 pre_mul[0] = 1.2085;
7508 pre_mul[1] = 1.0943;
7509 pre_mul[3] = 1.1103;
7510 goto e900;
7511 } else if (fsize == 2465792) {
7512 height = 1203;
7513 width = 1616;
7514 raw_width = 2048;
7515 colors = 4;
7516 filters = 0x4b4b4b4b;
7517 adobe_coeff ("NIKON","E950");
7518 e900:
7519 tiff_bps = 10;
7520 load_raw = &CLASS packed_load_raw;
7521 load_flags = 6;
7522 } else if (fsize == 4771840) {
7523 height = 1540;
7524 width = 2064;
7525 colors = 4;
7526 filters = 0xe1e1e1e1;
7527 load_raw = &CLASS packed_load_raw;
7528 load_flags = 6;
7529 if (!timestamp && nikon_e995())
7530 strcpy (model, "E995");
7531 if (strcmp(model,"E995")) {
7532 filters = 0xb4b4b4b4;
7533 simple_coeff(3);
7534 pre_mul[0] = 1.196;
7535 pre_mul[1] = 1.246;
7536 pre_mul[2] = 1.018;
7537 }
7538 } else if (!strcmp(model,"E2100")) {
7539 if (!timestamp && !nikon_e2100()) goto cp_e2500;
7540 height = 1206;
7541 width = 1616;
7542 load_flags = 30;
7543 } else if (!strcmp(model,"E2500")) {
7544 cp_e2500:
7545 strcpy (model, "E2500");
7546 height = 1204;
7547 width = 1616;
7548 colors = 4;
7549 filters = 0x4b4b4b4b;
7550 } else if (fsize == 4775936) {
7551 height = 1542;
7552 width = 2064;
7553 load_raw = &CLASS packed_load_raw;
7554 load_flags = 30;
7555 if (!timestamp) nikon_3700();
7556 if (model[0] == 'E' && atoi(model+1) < 3700)
7557 filters = 0x49494949;
7558 if (!strcmp(model,"Optio 33WR")) {
7559 flip = 1;
7560 filters = 0x16161616;
7561 }
7562 if (make[0] == 'O') {
7563 i = find_green (12, 32, 1188864, 3576832);
7564 c = find_green (12, 32, 2383920, 2387016);
7565 if (abs(i) < abs(c)) {
7566 SWAP(i,c);
7567 load_flags = 24;
7568 }
7569 if (i < 0) filters = 0x61616161;
7570 }
7571 } else if (fsize == 5869568) {
7572 height = 1710;
7573 width = 2288;
7574 filters = 0x16161616;
7575 if (!timestamp && minolta_z2()) {
7576 strcpy (make, "Minolta");
7577 strcpy (model,"DiMAGE Z2");
7578 }
7579 load_raw = &CLASS packed_load_raw;
7580 load_flags = 6 + 24*(make[0] == 'M');
7581 } else if (!strcmp(model,"E4500")) {
7582 height = 1708;
7583 width = 2288;
7584 colors = 4;
7585 filters = 0xb4b4b4b4;
7586 } else if (fsize == 7438336) {
7587 height = 1924;
7588 width = 2576;
7589 colors = 4;
7590 filters = 0xb4b4b4b4;
7591 } else if (fsize == 8998912) {
7592 height = 2118;
7593 width = 2832;
7594 maximum = 0xf83;
7595 load_raw = &CLASS packed_load_raw;
7596 load_flags = 30;
7597 } else if (!strcmp(model,"FinePix S5100") ||
7598 !strcmp(model,"FinePix S5500")) {
7599 height -= top_margin = 6;
7600 } else if (!strcmp(make,"FUJIFILM")) {
7601 if (!strcmp(model+7,"S2Pro")) {
7602 strcpy (model+7," S2Pro");
7603 height = 2144;
7604 width = 2880;
7605 flip = 6;
7606 } else if (load_raw != &CLASS packed_load_raw)
7607 maximum = 0x3e00;
7608 if (is_raw == 2 && shot_select)
7609 maximum = 0x2f00;
7610 top_margin = (raw_height - height)/2;
7611 left_margin = (raw_width - width )/2;
7612 if (is_raw == 2)
7613 data_offset += (shot_select > 0) * ( fuji_layout ?
7614 (raw_width *= 2) : raw_height*raw_width*2 );
7615 if (load_raw == &CLASS fuji_load_raw) {
7616 fuji_width = width >> !fuji_layout;
7617 width = (height >> fuji_layout) + fuji_width;
7618 raw_height = height;
7619 height = width - 1;
7620 if (~fuji_width & 1) filters = 0x49494949;
7621 }
7622 } else if (!strcmp(model,"RD175")) {
7623 height = 986;
7624 width = 1534;
7625 data_offset = 513;
7626 filters = 0x61616161;
7627 load_raw = &CLASS minolta_rd175_load_raw;
7628 } else if (!strcmp(model,"KD-400Z")) {
7629 height = 1712;
7630 width = 2312;
7631 raw_width = 2336;
7632 goto konica_400z;
7633 } else if (!strcmp(model,"KD-510Z")) {
7634 goto konica_510z;
7635 } else if (!strcasecmp(make,"MINOLTA")) {
7636 load_raw = &CLASS unpacked_load_raw;
7637 maximum = 0xfff;
7638 if (!strncmp(model,"DiMAGE A",8)) {
7639 if (!strcmp(model,"DiMAGE A200"))
7640 filters = 0x49494949;
7641 tiff_bps = 12;
7642 load_raw = &CLASS packed_load_raw;
7643 } else if (!strncmp(model,"ALPHA",5) ||
7644 !strncmp(model,"DYNAX",5) ||
7645 !strncmp(model,"MAXXUM",6)) {
7646 sprintf (model+20, "DYNAX %-10s", model+6+(model[0]=='M'));
7647 adobe_coeff (make, model+20);
7648 load_raw = &CLASS packed_load_raw;
7649 } else if (!strncmp(model,"DiMAGE G",8)) {
7650 if (model[8] == '4') {
7651 height = 1716;
7652 width = 2304;
7653 } else if (model[8] == '5') {
7654 konica_510z:
7655 height = 1956;
7656 width = 2607;
7657 raw_width = 2624;
7658 } else if (model[8] == '6') {
7659 height = 2136;
7660 width = 2848;
7661 }
7662 data_offset += 14;
7663 filters = 0x61616161;
7664 konica_400z:
7665 load_raw = &CLASS unpacked_load_raw;
7666 maximum = 0x3df;
7667 order = 0x4d4d;
7668 }
7669 } else if (!strcmp(model,"*ist D")) {
7670 data_error = -1;
7671 } else if (!strcmp(model,"*ist DS")) {
7672 height -= 2;
7673 } else if (!strcmp(model,"Optio S")) {
7674 if (fsize == 3178560) {
7675 height = 1540;
7676 width = 2064;
7677 load_raw = &CLASS eight_bit_load_raw;
7678 cam_mul[0] *= 4;
7679 cam_mul[2] *= 4;
7680 } else {
7681 height = 1544;
7682 width = 2068;
7683 raw_width = 3136;
7684 load_raw = &CLASS packed_load_raw;
7685 maximum = 0xf7c;
7686 }
7687 } else if (fsize == 6114240) {
7688 height = 1737;
7689 width = 2324;
7690 raw_width = 3520;
7691 load_raw = &CLASS packed_load_raw;
7692 maximum = 0xf7a;
7693 } else if (!strcmp(model,"Optio 750Z")) {
7694 height = 2302;
7695 width = 3072;
7696 load_raw = &CLASS packed_load_raw;
7697 load_flags = 30;
7698 } else if (!strcmp(model,"DC-833m")) {
7699 height = 2448;
7700 width = 3264;
7701 order = 0x4949;
7702 filters = 0x61616161;
7703 load_raw = &CLASS unpacked_load_raw;
7704 maximum = 0xfc00;
7705 } else if (!strncmp(model,"S85",3)) {
7706 height = 2448;
7707 width = 3264;
7708 raw_width = fsize/height/2;
7709 order = 0x4d4d;
7710 load_raw = &CLASS unpacked_load_raw;
7711 } else if (!strcmp(model,"NX10")) {
7712 height -= top_margin = 4;
7713 width -= 2 * (left_margin = 8);
7714 } else if (!strcmp(model,"EX1")) {
7715 order = 0x4949;
7716 height = 2760;
7717 top_margin = 2;
7718 if ((width -= 6) > 3682) {
7719 height = 2750;
7720 width = 3668;
7721 top_margin = 8;
7722 }
7723 } else if (!strcmp(model,"WB2000")) {
7724 order = 0x4949;
7725 height -= 3;
7726 width -= 10;
7727 top_margin = 2;
7728 } else if (fsize == 20487168) {
7729 height = 2808;
7730 width = 3648;
7731 goto wb550;
7732 } else if (fsize == 24000000) {
7733 height = 3000;
7734 width = 4000;
7735 wb550:
7736 strcpy (model, "WB550");
7737 order = 0x4d4d;
7738 load_raw = &CLASS unpacked_load_raw;
7739 load_flags = 6;
7740 maximum = 0x3df;
7741 } else if (!strcmp(model,"STV680 VGA")) {
7742 height = 484;
7743 width = 644;
7744 load_raw = &CLASS eight_bit_load_raw;
7745 flip = 2;
7746 filters = 0x16161616;
7747 black = 16;
7748 } else if (!strcmp(model,"N95")) {
7749 height = raw_height - (top_margin = 2);
7750 } else if (!strcmp(model,"531C")) {
7751 height = 1200;
7752 width = 1600;
7753 load_raw = &CLASS unpacked_load_raw;
7754 filters = 0x49494949;
7755 } else if (!strcmp(model,"F-080C")) {
7756 height = 768;
7757 width = 1024;
7758 load_raw = &CLASS eight_bit_load_raw;
7759 } else if (!strcmp(model,"F-145C")) {
7760 height = 1040;
7761 width = 1392;
7762 load_raw = &CLASS eight_bit_load_raw;
7763 } else if (!strcmp(model,"F-201C")) {
7764 height = 1200;
7765 width = 1600;
7766 load_raw = &CLASS eight_bit_load_raw;
7767 } else if (!strcmp(model,"F-510C")) {
7768 height = 1958;
7769 width = 2588;
7770 load_raw = fsize < 7500000 ?
7771 &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
7772 data_offset = fsize - width*height*(fsize >> 22);
7773 maximum = 0xfff0;
7774 } else if (!strcmp(model,"F-810C")) {
7775 height = 2469;
7776 width = 3272;
7777 load_raw = &CLASS unpacked_load_raw;
7778 maximum = 0xfff0;
7779 } else if (!strcmp(model,"XCD-SX910CR")) {
7780 height = 1024;
7781 width = 1375;
7782 raw_width = 1376;
7783 filters = 0x49494949;
7784 maximum = 0x3ff;
7785 load_raw = fsize < 2000000 ?
7786 &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
7787 } else if (!strcmp(model,"2010")) {
7788 height = 1207;
7789 width = 1608;
7790 order = 0x4949;
7791 filters = 0x16161616;
7792 data_offset = 3212;
7793 maximum = 0x3ff;
7794 load_raw = &CLASS unpacked_load_raw;
7795 } else if (!strcmp(model,"A782")) {
7796 height = 3000;
7797 width = 2208;
7798 filters = 0x61616161;
7799 load_raw = fsize < 10000000 ?
7800 &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
7801 maximum = 0xffc0;
7802 } else if (!strcmp(model,"3320AF")) {
7803 height = 1536;
7804 raw_width = width = 2048;
7805 filters = 0x61616161;
7806 load_raw = &CLASS unpacked_load_raw;
7807 maximum = 0x3ff;
7808 fseek (ifp, 0x300000, SEEK_SET);
7809 if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
7810 height -= (top_margin = 16);
7811 width -= (left_margin = 28);
7812 maximum = 0xf5c0;
7813 strcpy (make, "ISG");
7814 model[0] = 0;
7815 }
7816 } else if (!strcmp(make,"Hasselblad")) {
7817 if (load_raw == &CLASS lossless_jpeg_load_raw)
7818 load_raw = &CLASS hasselblad_load_raw;
7819 if (raw_width == 7262) {
7820 height = 5444;
7821 width = 7248;
7822 top_margin = 4;
7823 left_margin = 7;
7824 filters = 0x61616161;
7825 } else if (raw_width == 7410) {
7826 height = 5502;
7827 width = 7328;
7828 top_margin = 4;
7829 left_margin = 41;
7830 filters = 0x61616161;
7831 } else if (raw_width == 4090) {
7832 strcpy (model, "V96C");
7833 height -= (top_margin = 6);
7834 width -= (left_margin = 3) + 7;
7835 filters = 0x61616161;
7836 }
7837 } else if (!strcmp(make,"Sinar")) {
7838 if (!memcmp(head,"8BPS",4)) {
7839 fseek (ifp, 14, SEEK_SET);
7840 height = get4();
7841 width = get4();
7842 filters = 0x61616161;
7843 data_offset = 68;
7844 }
7845 if (!load_raw) load_raw = &CLASS unpacked_load_raw;
7846 maximum = 0x3fff;
7847 } else if (!strcmp(make,"Leaf")) {
7848 maximum = 0x3fff;
7849 fseek (ifp, data_offset, SEEK_SET);
7850 if (ljpeg_start (&jh, 1) && jh.bits == 15)
7851 maximum = 0x1fff;
7852 if (tiff_samples > 1) filters = 0;
7853 if (tiff_samples > 1 || tile_length < raw_height)
7854 load_raw = &CLASS leaf_hdr_load_raw;
7855 if ((width | height) == 2048) {
7856 if (tiff_samples == 1) {
7857 filters = 1;
7858 strcpy (cdesc, "RBTG");
7859 strcpy (model, "CatchLight");
7860 top_margin = 8; left_margin = 18; height = 2032; width = 2016;
7861 } else {
7862 strcpy (model, "DCB2");
7863 top_margin = 10; left_margin = 16; height = 2028; width = 2022;
7864 }
7865 } else if (width+height == 3144+2060) {
7866 if (!model[0]) strcpy (model, "Cantare");
7867 if (width > height) {
7868 top_margin = 6; left_margin = 32; height = 2048; width = 3072;
7869 filters = 0x61616161;
7870 } else {
7871 left_margin = 6; top_margin = 32; width = 2048; height = 3072;
7872 filters = 0x16161616;
7873 }
7874 if (!cam_mul[0] || model[0] == 'V') filters = 0;
7875 else is_raw = tiff_samples;
7876 } else if (width == 2116) {
7877 strcpy (model, "Valeo 6");
7878 height -= 2 * (top_margin = 30);
7879 width -= 2 * (left_margin = 55);
7880 filters = 0x49494949;
7881 } else if (width == 3171) {
7882 strcpy (model, "Valeo 6");
7883 height -= 2 * (top_margin = 24);
7884 width -= 2 * (left_margin = 24);
7885 filters = 0x16161616;
7886 }
7887 } else if (!strcmp(make,"LEICA") || !strcmp(make,"Panasonic")) {
7888 if ((flen - data_offset) / (raw_width*8/7) == raw_height)
7889 load_raw = &CLASS panasonic_load_raw;
7890 if (!load_raw) {
7891 load_raw = &CLASS unpacked_load_raw;
7892 load_flags = 4;
7893 }
7894 zero_is_bad = 1;
7895 if ((height += 12) > raw_height) height = raw_height;
7896 for (i=0; i < (int) sizeof pana / (int) sizeof *pana; i++)
7897 if (raw_width == pana[i][0] && raw_height == pana[i][1]) {
7898 left_margin = pana[i][2];
7899 top_margin = pana[i][3];
7900 width += pana[i][4];
7901 height += pana[i][5];
7902 }
7903 filters = 0x01010101 * (uchar) "\x94\x61\x49\x16"
7904 [((filters-1) ^ (left_margin & 1) ^ (top_margin << 1)) & 3];
7905 } else if (!strcmp(model,"C770UZ")) {
7906 height = 1718;
7907 width = 2304;
7908 filters = 0x16161616;
7909 load_raw = &CLASS packed_load_raw;
7910 load_flags = 30;
7911 } else if (!strcmp(make,"OLYMPUS")) {
7912 height += height & 1;
7913 filters = exif_cfa;
7914 if (width == 4100) width -= 4;
7915 if (load_raw == &CLASS unpacked_load_raw)
7916 load_flags = 4;
7917 tiff_bps = 12;
7918 if (!strcmp(model,"E-300") ||
7919 !strcmp(model,"E-500")) {
7920 width -= 20;
7921 if (load_raw == &CLASS unpacked_load_raw) {
7922 maximum = 0xfc3;
7923 memset (cblack, 0, sizeof cblack);
7924 }
7925 } else if (!strcmp(model,"E-330")) {
7926 width -= 30;
7927 if (load_raw == &CLASS unpacked_load_raw)
7928 maximum = 0xf79;
7929 } else if (!strcmp(model,"SP550UZ")) {
7930 thumb_length = flen - (thumb_offset = 0xa39800);
7931 thumb_height = 480;
7932 thumb_width = 640;
7933 }
7934 } else if (!strcmp(model,"N Digital")) {
7935 height = 2047;
7936 width = 3072;
7937 filters = 0x61616161;
7938 data_offset = 0x1a00;
7939 load_raw = &CLASS packed_load_raw;
7940 } else if (!strcmp(model,"DSC-F828")) {
7941 width = 3288;
7942 left_margin = 5;
7943 data_offset = 862144;
7944 load_raw = &CLASS sony_load_raw;
7945 filters = 0x9c9c9c9c;
7946 colors = 4;
7947 strcpy (cdesc, "RGBE");
7948 } else if (!strcmp(model,"DSC-V3")) {
7949 width = 3109;
7950 left_margin = 59;
7951 data_offset = 787392;
7952 load_raw = &CLASS sony_load_raw;
7953 } else if (!strcmp(make,"SONY") && raw_width == 3984) {
7954 adobe_coeff ("SONY","DSC-R1");
7955 width = 3925;
7956 order = 0x4d4d;
7957 } else if (!strcmp(model,"DSLR-A100")) {
7958 if (width == 3880) {
7959 height--;
7960 width = ++raw_width;
7961 } else {
7962 order = 0x4d4d;
7963 load_flags = 2;
7964 }
7965 filters = 0x61616161;
7966 } else if (!strcmp(model,"DSLR-A350")) {
7967 height -= 4;
7968 } else if (!strcmp(model,"PIXL")) {
7969 height -= top_margin = 4;
7970 width -= left_margin = 32;
7971 gamma_curve (0, 7, 1, 255);
7972 } else if (!strcmp(model,"C603v")) {
7973 height = 480;
7974 width = 640;
7975 if (fsize < 614400 || find_green (16, 16, 3840, 5120) < 25) goto c603v;
7976 strcpy (model,"KAI-0340");
7977 height -= 3;
7978 data_offset = 3840;
7979 order = 0x4949;
7980 load_raw = &CLASS unpacked_load_raw;
7981 } else if (!strcmp(model,"C603y")) {
7982 height = 2134;
7983 width = 2848;
7984 c603v:
7985 filters = 0;
7986 load_raw = &CLASS kodak_yrgb_load_raw;
7987 gamma_curve (0, 3.875, 1, 255);
7988 } else if (!strcmp(model,"C603")) {
7989 raw_height = height = 2152;
7990 raw_width = width = 2864;
7991 goto c603;
7992 } else if (!strcmp(model,"C330")) {
7993 height = 1744;
7994 width = 2336;
7995 raw_height = 1779;
7996 raw_width = 2338;
7997 top_margin = 33;
7998 left_margin = 1;
7999 c603:
8000 order = 0x4949;
8001 if ((data_offset = fsize - raw_height*raw_width)) {
8002 fseek (ifp, 168, SEEK_SET);
8003 read_shorts (curve, 256);
8004 } else gamma_curve (0, 3.875, 1, 255);
8005 load_raw = &CLASS eight_bit_load_raw;
8006 } else if (!strncasecmp(model,"EasyShare",9)) {
8007 data_offset = 0x15000;
8008 load_raw = &CLASS packed_load_raw;
8009 } else if (!strcasecmp(make,"KODAK")) {
8010 if (filters == UINT_MAX) filters = 0x61616161;
8011 if (!strncmp(model,"NC2000",6)) {
8012 width -= 4;
8013 left_margin = 2;
8014 } else if (!strcmp(model,"EOSDCS3B")) {
8015 width -= 4;
8016 left_margin = 2;
8017 } else if (!strcmp(model,"EOSDCS1")) {
8018 width -= 4;
8019 left_margin = 2;
8020 } else if (!strcmp(model,"DCS420")) {
8021 width -= 4;
8022 left_margin = 2;
8023 } else if (!strncmp(model,"DCS460 ",7)) {
8024 model[6] = 0;
8025 width -= 4;
8026 left_margin = 2;
8027 } else if (!strcmp(model,"DCS460A")) {
8028 width -= 4;
8029 left_margin = 2;
8030 colors = 1;
8031 filters = 0;
8032 } else if (!strcmp(model,"DCS660M")) {
8033 black = 214;
8034 colors = 1;
8035 filters = 0;
8036 } else if (!strcmp(model,"DCS760M")) {
8037 colors = 1;
8038 filters = 0;
8039 }
8040 if (!strcmp(model+4,"20X"))
8041 strcpy (cdesc, "MYCY");
8042 if (strstr(model,"DC25")) {
8043 strcpy (model, "DC25");
8044 data_offset = 15424;
8045 }
8046 if (!strncmp(model,"DC2",3)) {
8047 height = 242;
8048 if (flen < 100000) {
8049 raw_width = 256; width = 249;
8050 pixel_aspect = (4.0*height) / (3.0*width);
8051 } else {
8052 raw_width = 512; width = 501;
8053 pixel_aspect = (493.0*height) / (373.0*width);
8054 }
8055 data_offset += raw_width + 1;
8056 colors = 4;
8057 filters = 0x8d8d8d8d;
8058 simple_coeff(1);
8059 pre_mul[1] = 1.179;
8060 pre_mul[2] = 1.209;
8061 pre_mul[3] = 1.036;
8062 load_raw = &CLASS eight_bit_load_raw;
8063 } else if (!strcmp(model,"40")) {
8064 strcpy (model, "DC40");
8065 height = 512;
8066 width = 768;
8067 data_offset = 1152;
8068 load_raw = &CLASS kodak_radc_load_raw;
8069 } else if (strstr(model,"DC50")) {
8070 strcpy (model, "DC50");
8071 height = 512;
8072 width = 768;
8073 data_offset = 19712;
8074 load_raw = &CLASS kodak_radc_load_raw;
8075 } else if (strstr(model,"DC120")) {
8076 strcpy (model, "DC120");
8077 height = 976;
8078 width = 848;
8079 pixel_aspect = height/0.75/width;
8080 load_raw = tiff_compress == 7 ?
8081 &CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
8082 } else if (!strcmp(model,"DCS200")) {
8083 thumb_height = 128;
8084 thumb_width = 192;
8085 thumb_offset = 6144;
8086 thumb_misc = 360;
8087 write_thumb = &CLASS layer_thumb;
8088 height = 1024;
8089 width = 1536;
8090 data_offset = 79872;
8091 load_raw = &CLASS eight_bit_load_raw;
8092 black = 17;
8093 }
8094 } else if (!strcmp(model,"Fotoman Pixtura")) {
8095 height = 512;
8096 width = 768;
8097 data_offset = 3632;
8098 load_raw = &CLASS kodak_radc_load_raw;
8099 filters = 0x61616161;
8100 simple_coeff(2);
8101 } else if (!strcmp(model,"QuickTake 100")) {
8102 fseek (ifp, 544, SEEK_SET);
8103 height = get2();
8104 width = get2();
8105 data_offset = (get4(),get2()) == 30 ? 738:736;
8106 if (height > width) {
8107 SWAP(height,width);
8108 fseek (ifp, data_offset-6, SEEK_SET);
8109 flip = ~get2() & 3 ? 5:6;
8110 }
8111 load_raw = &CLASS quicktake_100_load_raw;
8112 filters = 0x61616161;
8113 } else if (!strcmp(model,"QuickTake 150")) {
8114 data_offset = 738 - head[5];
8115 if (head[5]) strcpy (model+10, "200");
8116 load_raw = &CLASS kodak_radc_load_raw;
8117 height = 480;
8118 width = 640;
8119 filters = 0x61616161;
8120 } else if (!strcmp(make,"Rollei") && !load_raw) {
8121 switch (raw_width) {
8122 case 1316:
8123 height = 1030;
8124 width = 1300;
8125 top_margin = 1;
8126 left_margin = 6;
8127 break;
8128 case 2568:
8129 height = 1960;
8130 width = 2560;
8131 top_margin = 2;
8132 left_margin = 8;
8133 }
8134 filters = 0x16161616;
8135 load_raw = &CLASS rollei_load_raw;
8136 } else if (!strcmp(model,"PC-CAM 600")) {
8137 height = 768;
8138 data_offset = width = 1024;
8139 filters = 0x49494949;
8140 load_raw = &CLASS eight_bit_load_raw;
8141 } else if (!strcmp(model,"QV-2000UX")) {
8142 height = 1208;
8143 width = 1632;
8144 data_offset = width * 2;
8145 load_raw = &CLASS eight_bit_load_raw;
8146 } else if (fsize == 3217760) {
8147 height = 1546;
8148 width = 2070;
8149 raw_width = 2080;
8150 load_raw = &CLASS eight_bit_load_raw;
8151 } else if (!strcmp(model,"QV-4000")) {
8152 height = 1700;
8153 width = 2260;
8154 load_raw = &CLASS unpacked_load_raw;
8155 maximum = 0xffff;
8156 } else if (!strcmp(model,"QV-5700")) {
8157 height = 1924;
8158 width = 2576;
8159 raw_width = 3232;
8160 tiff_bps = 10;
8161 } else if (!strcmp(model,"QV-R41")) {
8162 height = 1720;
8163 width = 2312;
8164 raw_width = 3520;
8165 left_margin = 2;
8166 } else if (!strcmp(model,"QV-R51")) {
8167 height = 1926;
8168 width = 2580;
8169 raw_width = 3904;
8170 } else if (!strcmp(model,"EX-S20")) {
8171 height = 1208;
8172 width = 1620;
8173 raw_width = 2432;
8174 flip = 3;
8175 } else if (!strcmp(model,"EX-S100")) {
8176 height = 1544;
8177 width = 2058;
8178 raw_width = 3136;
8179 } else if (!strcmp(model,"EX-Z50")) {
8180 height = 1931;
8181 width = 2570;
8182 raw_width = 3904;
8183 } else if (!strcmp(model,"EX-Z55")) {
8184 height = 1960;
8185 width = 2570;
8186 raw_width = 3904;
8187 } else if (!strcmp(model,"EX-Z60")) {
8188 height = 2145;
8189 width = 2833;
8190 raw_width = 3584;
8191 filters = 0x16161616;
8192 tiff_bps = 10;
8193 } else if (!strcmp(model,"EX-Z75")) {
8194 height = 2321;
8195 width = 3089;
8196 raw_width = 4672;
8197 maximum = 0xfff;
8198 } else if (!strcmp(model,"EX-Z750")) {
8199 height = 2319;
8200 width = 3087;
8201 raw_width = 4672;
8202 maximum = 0xfff;
8203 } else if (!strcmp(model,"EX-Z850")) {
8204 height = 2468;
8205 width = 3279;
8206 raw_width = 4928;
8207 maximum = 0xfff;
8208 } else if (!strcmp(model,"EX-Z1050")) {
8209 height = 2752;
8210 width = 3672;
8211 raw_width = 5632;
8212 maximum = 0xffc;
8213 } else if (!strcmp(model,"EX-P505")) {
8214 height = 1928;
8215 width = 2568;
8216 raw_width = 3852;
8217 maximum = 0xfff;
8218 } else if (fsize == 9313536) { /* EX-P600 or QV-R61 */
8219 height = 2142;
8220 width = 2844;
8221 raw_width = 4288;
8222 } else if (!strcmp(model,"EX-P700")) {
8223 height = 2318;
8224 width = 3082;
8225 raw_width = 4672;
8226 }
8227 if (!model[0])
8228 sprintf (model, "%dx%d", width, height);
8229 if (filters == UINT_MAX) filters = 0x94949494;
8230 if (raw_color) adobe_coeff (make, model);
8231 if (load_raw == &CLASS kodak_radc_load_raw)
8232 if (raw_color) adobe_coeff ("Apple","Quicktake");
8233 if (thumb_offset && !thumb_height) {
8234 fseek (ifp, thumb_offset, SEEK_SET);
8235 if (ljpeg_start (&jh, 1)) {
8236 thumb_width = jh.wide;
8237 thumb_height = jh.high;
8238 }
8239 }
8240 dng_skip:
8241 if (!tiff_bps) tiff_bps = 12;
8242 if (!maximum) maximum = (1 << tiff_bps) - 1;
8243 if (!load_raw || height < 22) is_raw = 0;
8244 #ifndef HAVE_LIBJPEG
8245 if (load_raw == &CLASS kodak_jpeg_load_raw) {
8246 dcraw_message (DCRAW_ERROR,_("%s: You must link dcraw with libjpeg!!\n"), ifname_display);
8247 is_raw = 0;
8248 }
8249 #endif
8250 if (!cdesc[0])
8251 strcpy (cdesc, colors == 3 ? "RGB":"GMCY");
8252 if (!raw_height) raw_height = height;
8253 if (!raw_width ) raw_width = width;
8254 if (filters && colors == 3)
8255 filters |= ((filters >> 2 & 0x22222222) |
8256 (filters << 2 & 0x88888888)) & filters << 1;
8257 notraw:
8258 if (flip == -1) flip = tiff_flip;
8259 if (flip == -1) flip = 0;
8260 }
8261
8262 #ifndef NO_LCMS
apply_profile(const char * input,const char * output)8263 void CLASS apply_profile (const char *input, const char *output)
8264 {
8265 char *prof;
8266 cmsHPROFILE hInProfile=0, hOutProfile=0;
8267 cmsHTRANSFORM hTransform;
8268 FILE *fp;
8269 unsigned size;
8270
8271 cmsErrorAction (LCMS_ERROR_SHOW);
8272 if (strcmp (input, "embed"))
8273 hInProfile = cmsOpenProfileFromFile (input, "r");
8274 else if (profile_length) {
8275 prof = (char *) malloc (profile_length);
8276 merror (prof, "apply_profile()");
8277 fseek (ifp, profile_offset, SEEK_SET);
8278 fread (prof, 1, profile_length, ifp);
8279 hInProfile = cmsOpenProfileFromMem (prof, profile_length);
8280 free (prof);
8281 } else
8282 dcraw_message (DCRAW_ERROR,_("%s has no embedded profile.\n"), ifname_display);
8283 if (!hInProfile) return;
8284 if (!output)
8285 hOutProfile = cmsCreate_sRGBProfile();
8286 else if ((fp = fopen (output, "rb"))) {
8287 fread (&size, 4, 1, fp);
8288 fseek (fp, 0, SEEK_SET);
8289 oprof = (unsigned *) malloc (size = ntohl(size));
8290 merror (oprof, "apply_profile()");
8291 fread (oprof, 1, size, fp);
8292 fclose (fp);
8293 if (!(hOutProfile = cmsOpenProfileFromMem (oprof, size))) {
8294 free (oprof);
8295 oprof = 0;
8296 }
8297 } else
8298 dcraw_message (DCRAW_ERROR,_("Cannot open file %s!\n"), output);
8299 if (!hOutProfile) goto quit;
8300 dcraw_message (DCRAW_VERBOSE,_("Applying color profile...\n"));
8301 hTransform = cmsCreateTransform (hInProfile, TYPE_RGBA_16,
8302 hOutProfile, TYPE_RGBA_16, INTENT_PERCEPTUAL, 0);
8303 cmsDoTransform (hTransform, image, image, width*height);
8304 raw_color = 1; /* Don't use rgb_cam with a profile */
8305 cmsDeleteTransform (hTransform);
8306 cmsCloseProfile (hOutProfile);
8307 quit:
8308 cmsCloseProfile (hInProfile);
8309 }
8310 #endif
8311
convert_to_rgb()8312 void CLASS convert_to_rgb()
8313 {
8314 int row, col, c, i, j, k;
8315 ushort *img;
8316 float out[3], out_cam[3][4];
8317 double num, inverse[3][3];
8318 static const double xyzd50_srgb[3][3] =
8319 { { 0.436083, 0.385083, 0.143055 },
8320 { 0.222507, 0.716888, 0.060608 },
8321 { 0.013930, 0.097097, 0.714022 } };
8322 static const double rgb_rgb[3][3] =
8323 { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
8324 static const double adobe_rgb[3][3] =
8325 { { 0.715146, 0.284856, 0.000000 },
8326 { 0.000000, 1.000000, 0.000000 },
8327 { 0.000000, 0.041166, 0.958839 } };
8328 static const double wide_rgb[3][3] =
8329 { { 0.593087, 0.404710, 0.002206 },
8330 { 0.095413, 0.843149, 0.061439 },
8331 { 0.011621, 0.069091, 0.919288 } };
8332 static const double prophoto_rgb[3][3] =
8333 { { 0.529317, 0.330092, 0.140588 },
8334 { 0.098368, 0.873465, 0.028169 },
8335 { 0.016879, 0.117663, 0.865457 } };
8336 static const double (*out_rgb[])[3] =
8337 { rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb };
8338 static const char *name[] =
8339 { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" };
8340 static const unsigned phead[] =
8341 { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
8342 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
8343 unsigned pbody[] =
8344 { 10, 0x63707274, 0, 36, /* cprt */
8345 0x64657363, 0, 40, /* desc */
8346 0x77747074, 0, 20, /* wtpt */
8347 0x626b7074, 0, 20, /* bkpt */
8348 0x72545243, 0, 14, /* rTRC */
8349 0x67545243, 0, 14, /* gTRC */
8350 0x62545243, 0, 14, /* bTRC */
8351 0x7258595a, 0, 20, /* rXYZ */
8352 0x6758595a, 0, 20, /* gXYZ */
8353 0x6258595a, 0, 20 }; /* bXYZ */
8354 static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc };
8355 unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 };
8356
8357 gamma_curve (gamm[0], gamm[1], 0, 0);
8358 memcpy (out_cam, rgb_cam, sizeof out_cam);
8359 raw_color |= colors == 1 || document_mode ||
8360 output_color < 1 || output_color > 5;
8361 if (!raw_color) {
8362 oprof = (unsigned *) calloc (phead[0], 1);
8363 merror (oprof, "convert_to_rgb()");
8364 memcpy (oprof, phead, sizeof phead);
8365 if (output_color == 5) oprof[4] = oprof[5];
8366 oprof[0] = 132 + 12*pbody[0];
8367 for (i=0; i < (int)pbody[0]; i++) {
8368 oprof[oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
8369 pbody[i*3+2] = oprof[0];
8370 oprof[0] += (pbody[i*3+3] + 3) & -4;
8371 }
8372 memcpy (oprof+32, pbody, sizeof pbody);
8373 oprof[pbody[5]/4+2] = strlen(name[output_color-1]) + 1;
8374 memcpy ((char *)oprof+pbody[8]+8, pwhite, sizeof pwhite);
8375 pcurve[3] = (short)(256/gamm[5]+0.5) << 16;
8376 for (i=4; i < 7; i++)
8377 memcpy ((char *)oprof+pbody[i*3+2], pcurve, sizeof pcurve);
8378 pseudoinverse ((double (*)[3]) out_rgb[output_color-1], inverse, 3);
8379 for (i=0; i < 3; i++)
8380 for (j=0; j < 3; j++) {
8381 for (num = k=0; k < 3; k++)
8382 num += xyzd50_srgb[i][k] * inverse[j][k];
8383 oprof[pbody[j*3+23]/4+i+2] = num * 0x10000 + 0.5;
8384 }
8385 for (i=0; i < (int)phead[0]/4; i++)
8386 oprof[i] = htonl(oprof[i]);
8387 strcpy ((char *)oprof+pbody[2]+8, "auto-generated by dcraw");
8388 strcpy ((char *)oprof+pbody[5]+12, name[output_color-1]);
8389 for (i=0; i < 3; i++)
8390 for (j=0; j < colors; j++)
8391 for (out_cam[i][j] = k=0; k < 3; k++)
8392 out_cam[i][j] += out_rgb[output_color-1][i][k] * rgb_cam[k][j];
8393 }
8394 dcraw_message (DCRAW_VERBOSE, raw_color ? _("Building histograms...\n") :
8395 _("Converting to %s colorspace...\n"), name[output_color-1]);
8396
8397 memset (histogram, 0, sizeof histogram);
8398 for (img=image[0], row=0; row < height; row++)
8399 for (col=0; col < width; col++, img+=4) {
8400 if (!raw_color) {
8401 out[0] = out[1] = out[2] = 0;
8402 FORCC {
8403 out[0] += out_cam[0][c] * img[c];
8404 out[1] += out_cam[1][c] * img[c];
8405 out[2] += out_cam[2][c] * img[c];
8406 }
8407 FORC3 img[c] = CLIP((int) out[c]);
8408 }
8409 else if (document_mode)
8410 img[0] = img[FC(row,col)];
8411 FORCC histogram[c][img[c] >> 3]++;
8412 }
8413 if (colors == 4 && output_color) colors = 3;
8414 if (document_mode && filters) colors = 1;
8415 }
8416
8417 /* Start of functions copied to dcraw_indi.c (UF) */
fuji_rotate()8418 void CLASS fuji_rotate()
8419 {
8420 int i, row, col;
8421 double step;
8422 float r, c, fr, fc;
8423 int ur, uc;
8424 ushort wide, high, (*img)[4], (*pix)[4];
8425
8426 if (!fuji_width) return;
8427 dcraw_message (DCRAW_VERBOSE,_("Rotating image 45 degrees...\n"));
8428 fuji_width = (fuji_width - 1 + shrink) >> shrink;
8429 step = sqrt(0.5);
8430 wide = fuji_width / step;
8431 high = (height - fuji_width) / step;
8432 img = (ushort (*)[4]) calloc (wide*high, sizeof *img);
8433 merror (img, "fuji_rotate()");
8434
8435 for (row=0; row < high; row++)
8436 for (col=0; col < wide; col++) {
8437 ur = r = fuji_width + (row-col)*step;
8438 uc = c = (row+col)*step;
8439 if (ur > height-2 || uc > width-2) continue;
8440 fr = r - ur;
8441 fc = c - uc;
8442 pix = image + ur*width + uc;
8443 for (i=0; i < colors; i++)
8444 img[row*wide+col][i] =
8445 (pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
8446 (pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr;
8447 }
8448 free (image);
8449 width = wide;
8450 height = high;
8451 image = img;
8452 fuji_width = 0;
8453 }
8454 /* End of functions copied to dcraw_indi.c (UF) */
8455
stretch()8456 void CLASS stretch()
8457 {
8458 ushort newdim, (*img)[4], *pix0, *pix1;
8459 int row, col, c;
8460 double rc, frac;
8461
8462 if (pixel_aspect == 1) return;
8463 dcraw_message (DCRAW_VERBOSE,_("Stretching the image...\n"));
8464 if (pixel_aspect < 1) {
8465 newdim = height / pixel_aspect + 0.5;
8466 img = (ushort (*)[4]) calloc (width*newdim, sizeof *img);
8467 merror (img, "stretch()");
8468 for (rc=row=0; row < newdim; row++, rc+=pixel_aspect) {
8469 frac = rc - (c = rc);
8470 pix0 = pix1 = image[c*width];
8471 if (c+1 < height) pix1 += width*4;
8472 for (col=0; col < width; col++, pix0+=4, pix1+=4)
8473 FORCC img[row*width+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
8474 }
8475 height = newdim;
8476 } else {
8477 newdim = width * pixel_aspect + 0.5;
8478 img = (ushort (*)[4]) calloc (height*newdim, sizeof *img);
8479 merror (img, "stretch()");
8480 for (rc=col=0; col < newdim; col++, rc+=1/pixel_aspect) {
8481 frac = rc - (c = rc);
8482 pix0 = pix1 = image[c];
8483 if (c+1 < width) pix1 += 4;
8484 for (row=0; row < height; row++, pix0+=width*4, pix1+=width*4)
8485 FORCC img[row*newdim+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
8486 }
8487 width = newdim;
8488 }
8489 free (image);
8490 image = img;
8491 }
8492
flip_index(int row,int col)8493 int CLASS flip_index (int row, int col)
8494 {
8495 if (flip & 4) SWAP(row,col);
8496 if (flip & 2) row = iheight - 1 - row;
8497 if (flip & 1) col = iwidth - 1 - col;
8498 return row * iwidth + col;
8499 }
8500
8501 struct tiff_tag {
8502 ushort tag, type;
8503 int count;
8504 union { char c[4]; short s[2]; int i; } val;
8505 };
8506
8507 struct tiff_hdr {
8508 ushort order, magic;
8509 int ifd;
8510 ushort pad, ntag;
8511 struct tiff_tag tag[23];
8512 int nextifd;
8513 ushort pad2, nexif;
8514 struct tiff_tag exif[4];
8515 ushort pad3, ngps;
8516 struct tiff_tag gpst[10];
8517 short bps[4];
8518 int rat[10];
8519 unsigned gps[26];
8520 char desc[512], make[64], model[64], soft[32], date[20], artist[64];
8521 };
8522
tiff_set(ushort * ntag,ushort tag,ushort type,int count,int val)8523 void CLASS tiff_set (ushort *ntag,
8524 ushort tag, ushort type, int count, int val)
8525 {
8526 struct tiff_tag *tt;
8527 int c;
8528
8529 tt = (struct tiff_tag *)(ntag+1) + (*ntag)++;
8530 tt->tag = tag;
8531 tt->type = type;
8532 tt->count = count;
8533 if (type < 3 && count <= 4)
8534 FORC(4) tt->val.c[c] = val >> (c << 3);
8535 else if (type == 3 && count <= 2)
8536 FORC(2) tt->val.s[c] = val >> (c << 4);
8537 else tt->val.i = val;
8538 }
8539
8540 #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
8541
tiff_head(struct tiff_hdr * th,int full)8542 void CLASS tiff_head (struct tiff_hdr *th, int full)
8543 {
8544 int c, psize=0;
8545 struct tm *t;
8546
8547 memset (th, 0, sizeof *th);
8548 th->order = htonl(0x4d4d4949) >> 16;
8549 th->magic = 42;
8550 th->ifd = 10;
8551 if (full) {
8552 tiff_set (&th->ntag, 254, 4, 1, 0);
8553 tiff_set (&th->ntag, 256, 4, 1, width);
8554 tiff_set (&th->ntag, 257, 4, 1, height);
8555 tiff_set (&th->ntag, 258, 3, colors, output_bps);
8556 if (colors > 2)
8557 th->tag[th->ntag-1].val.i = TOFF(th->bps);
8558 FORC4 th->bps[c] = output_bps;
8559 tiff_set (&th->ntag, 259, 3, 1, 1);
8560 tiff_set (&th->ntag, 262, 3, 1, 1 + (colors > 1));
8561 }
8562 tiff_set (&th->ntag, 270, 2, 512, TOFF(th->desc));
8563 tiff_set (&th->ntag, 271, 2, 64, TOFF(th->make));
8564 tiff_set (&th->ntag, 272, 2, 64, TOFF(th->model));
8565 if (full) {
8566 if (oprof) psize = ntohl(oprof[0]);
8567 tiff_set (&th->ntag, 273, 4, 1, sizeof *th + psize);
8568 tiff_set (&th->ntag, 277, 3, 1, colors);
8569 tiff_set (&th->ntag, 278, 4, 1, height);
8570 tiff_set (&th->ntag, 279, 4, 1, height*width*colors*output_bps/8);
8571 } else
8572 tiff_set (&th->ntag, 274, 3, 1, "12435867"[flip]-'0');
8573 tiff_set (&th->ntag, 282, 5, 1, TOFF(th->rat[0]));
8574 tiff_set (&th->ntag, 283, 5, 1, TOFF(th->rat[2]));
8575 tiff_set (&th->ntag, 284, 3, 1, 1);
8576 tiff_set (&th->ntag, 296, 3, 1, 2);
8577 tiff_set (&th->ntag, 305, 2, 32, TOFF(th->soft));
8578 tiff_set (&th->ntag, 306, 2, 20, TOFF(th->date));
8579 tiff_set (&th->ntag, 315, 2, 64, TOFF(th->artist));
8580 tiff_set (&th->ntag, 34665, 4, 1, TOFF(th->nexif));
8581 if (psize) tiff_set (&th->ntag, 34675, 7, psize, sizeof *th);
8582 tiff_set (&th->nexif, 33434, 5, 1, TOFF(th->rat[4]));
8583 tiff_set (&th->nexif, 33437, 5, 1, TOFF(th->rat[6]));
8584 tiff_set (&th->nexif, 34855, 3, 1, iso_speed);
8585 tiff_set (&th->nexif, 37386, 5, 1, TOFF(th->rat[8]));
8586 if (gpsdata[1]) {
8587 tiff_set (&th->ntag, 34853, 4, 1, TOFF(th->ngps));
8588 tiff_set (&th->ngps, 0, 1, 4, 0x202);
8589 tiff_set (&th->ngps, 1, 2, 2, gpsdata[29]);
8590 tiff_set (&th->ngps, 2, 5, 3, TOFF(th->gps[0]));
8591 tiff_set (&th->ngps, 3, 2, 2, gpsdata[30]);
8592 tiff_set (&th->ngps, 4, 5, 3, TOFF(th->gps[6]));
8593 tiff_set (&th->ngps, 5, 1, 1, gpsdata[31]);
8594 tiff_set (&th->ngps, 6, 5, 1, TOFF(th->gps[18]));
8595 tiff_set (&th->ngps, 7, 5, 3, TOFF(th->gps[12]));
8596 tiff_set (&th->ngps, 18, 2, 12, TOFF(th->gps[20]));
8597 tiff_set (&th->ngps, 29, 2, 12, TOFF(th->gps[23]));
8598 memcpy (th->gps, gpsdata, sizeof th->gps);
8599 }
8600 th->rat[0] = th->rat[2] = 300;
8601 th->rat[1] = th->rat[3] = 1;
8602 FORC(6) th->rat[4+c] = 1000000;
8603 th->rat[4] *= shutter;
8604 th->rat[6] *= aperture;
8605 th->rat[8] *= focal_len;
8606 strncpy (th->desc, desc, 512);
8607 strncpy (th->make, make, 64);
8608 strncpy (th->model, model, 64);
8609 strcpy (th->soft, "dcraw v" DCRAW_VERSION);
8610 t = gmtime (×tamp);
8611 sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
8612 t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
8613 strncpy (th->artist, artist, 64);
8614 }
8615
jpeg_thumb()8616 void CLASS jpeg_thumb()
8617 {
8618 char *thumb;
8619 ushort exif[5];
8620 struct tiff_hdr th;
8621
8622 thumb = (char *) malloc (thumb_length);
8623 merror (thumb, "jpeg_thumb()");
8624 fread (thumb, 1, thumb_length, ifp);
8625 fputc (0xff, ofp);
8626 fputc (0xd8, ofp);
8627 if (strcmp (thumb+6, "Exif")) {
8628 memcpy (exif, "\xff\xe1 Exif\0\0", 10);
8629 exif[1] = htons (8 + sizeof th);
8630 fwrite (exif, 1, sizeof exif, ofp);
8631 tiff_head (&th, 0);
8632 fwrite (&th, 1, sizeof th, ofp);
8633 }
8634 fwrite (thumb+2, 1, thumb_length-2, ofp);
8635 free (thumb);
8636 }
8637
write_ppm_tiff()8638 void CLASS write_ppm_tiff()
8639 {
8640 struct tiff_hdr th;
8641 uchar *ppm;
8642 ushort *ppm2;
8643 int c, row, col, soff, rstep, cstep;
8644 int perc, val, total, white=0x2000;
8645
8646 perc = width * height * 0.01; /* 99th percentile white level */
8647 if (fuji_width) perc /= 2;
8648 if (!((highlight & ~2) || no_auto_bright))
8649 for (white=c=0; c < colors; c++) {
8650 for (val=0x2000, total=0; --val > 32; )
8651 if ((total += histogram[c][val]) > perc) break;
8652 if (white < val) white = val;
8653 }
8654 gamma_curve (gamm[0], gamm[1], 2, (white << 3)/bright);
8655 iheight = height;
8656 iwidth = width;
8657 if (flip & 4) SWAP(height,width);
8658 ppm = (uchar *) calloc (width, colors*output_bps/8);
8659 ppm2 = (ushort *) ppm;
8660 merror (ppm, "write_ppm_tiff()");
8661 if (output_tiff) {
8662 tiff_head (&th, 1);
8663 fwrite (&th, sizeof th, 1, ofp);
8664 if (oprof)
8665 fwrite (oprof, ntohl(oprof[0]), 1, ofp);
8666 } else if (colors > 3)
8667 fprintf (ofp,
8668 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
8669 width, height, colors, (1 << output_bps)-1, cdesc);
8670 else
8671 fprintf (ofp, "P%d\n%d %d\n%d\n",
8672 colors/2+5, width, height, (1 << output_bps)-1);
8673 soff = flip_index (0, 0);
8674 cstep = flip_index (0, 1) - soff;
8675 rstep = flip_index (1, 0) - flip_index (0, width);
8676 for (row=0; row < height; row++, soff += rstep) {
8677 for (col=0; col < width; col++, soff += cstep)
8678 if (output_bps == 8)
8679 FORCC ppm [col*colors+c] = curve[image[soff][c]] >> 8;
8680 else FORCC ppm2[col*colors+c] = curve[image[soff][c]];
8681 if (output_bps == 16 && !output_tiff && htons(0x55aa) != 0x55aa)
8682 swab ((const char *)ppm2, (char *)ppm2, width*colors*2); /*mingw UF*/
8683 fwrite (ppm, colors*output_bps/8, width, ofp);
8684 }
8685 free (ppm);
8686 }
8687
main(int argc,const char ** argv)8688 int CLASS main (int argc, const char **argv)
8689 {
8690 // The following variables are static to supress clobbering warnings.
8691 // They are not thread-safe, but main() should never be called in a thread.
8692 static int arg, status=0;
8693 static int timestamp_only=0, thumbnail_only=0, identify_only=0;
8694 static int user_qual=-1, user_black=-1, user_sat=-1, user_flip=-1;
8695 static int use_fuji_rotate=1, write_to_stdout=0, quality, i, c;
8696 static const char *sp, *bpfile=0, *dark_frame=0, *write_ext;
8697 static char opm, opt, *ofname, *cp;
8698 static struct utimbuf ut;
8699 #ifndef NO_LCMS
8700 static const char *cam_profile=0, *out_profile=0;
8701 #endif
8702
8703 #ifndef LOCALTIME
8704 putenv ((char *) "TZ=UTC");
8705 #endif
8706 #ifdef LOCALEDIR
8707 setlocale (LC_CTYPE, "");
8708 setlocale (LC_MESSAGES, "");
8709 bindtextdomain ("dcraw", LOCALEDIR);
8710 textdomain ("dcraw");
8711 #endif
8712
8713 if (argc == 1) {
8714 printf(_("\nRaw photo decoder \"dcraw\" v%s"), DCRAW_VERSION);
8715 printf(_("\nby Dave Coffin, dcoffin a cybercom o net\n"));
8716 printf(_("\nUsage: %s [OPTION]... [FILE]...\n\n"), argv[0]);
8717 puts(_("-v Print verbose messages"));
8718 puts(_("-c Write image data to standard output"));
8719 puts(_("-e Extract embedded thumbnail image"));
8720 puts(_("-i Identify files without decoding them"));
8721 puts(_("-i -v Identify files and show metadata"));
8722 puts(_("-z Change file dates to camera timestamp"));
8723 puts(_("-w Use camera white balance, if possible"));
8724 puts(_("-a Average the whole image for white balance"));
8725 puts(_("-A <x y w h> Average a grey box for white balance"));
8726 puts(_("-r <r g b g> Set custom white balance"));
8727 puts(_("+M/-M Use/don't use an embedded color matrix"));
8728 puts(_("-C <r b> Correct chromatic aberration"));
8729 puts(_("-P <file> Fix the dead pixels listed in this file"));
8730 puts(_("-K <file> Subtract dark frame (16-bit raw PGM)"));
8731 puts(_("-k <num> Set the darkness level"));
8732 puts(_("-S <num> Set the saturation level"));
8733 puts(_("-n <num> Set threshold for wavelet denoising"));
8734 puts(_("-H [0-9] Highlight mode (0=clip, 1=unclip, 2=blend, 3+=rebuild)"));
8735 puts(_("-t [0-7] Flip image (0=none, 3=180, 5=90CCW, 6=90CW)"));
8736 puts(_("-o [0-5] Output colorspace (raw,sRGB,Adobe,Wide,ProPhoto,XYZ)"));
8737 #ifndef NO_LCMS
8738 puts(_("-o <file> Apply output ICC profile from file"));
8739 puts(_("-p <file> Apply camera ICC profile from file or \"embed\""));
8740 #endif
8741 puts(_("-d Document mode (no color, no interpolation)"));
8742 puts(_("-D Document mode without scaling (totally raw)"));
8743 puts(_("-j Don't stretch or rotate raw pixels"));
8744 puts(_("-W Don't automatically brighten the image"));
8745 puts(_("-b <num> Adjust brightness (default = 1.0)"));
8746 puts(_("-g <p ts> Set custom gamma curve (default = 2.222 4.5)"));
8747 puts(_("-q [0-3] Set the interpolation quality"));
8748 puts(_("-h Half-size color image (twice as fast as \"-q 0\")"));
8749 puts(_("-f Interpolate RGGB as four colors"));
8750 puts(_("-m <num> Apply a 3x3 median filter to R-G and B-G"));
8751 puts(_("-s [0..N-1] Select one raw image or \"all\" from each file"));
8752 puts(_("-6 Write 16-bit instead of 8-bit"));
8753 puts(_("-4 Linear 16-bit, same as \"-6 -W -g 1 1\""));
8754 puts(_("-T Write TIFF instead of PPM"));
8755 puts("");
8756 return 1;
8757 }
8758 argv[argc] = "";
8759 for (arg=1; (((opm = argv[arg][0]) - 2) | 2) == '+'; ) {
8760 opt = argv[arg++][1];
8761 if ((cp = (char *) strchr (sp="nbrkStqmHACg", opt)))
8762 for (i=0; i < "114111111422"[cp-sp]-'0'; i++)
8763 if (!isdigit(argv[arg+i][0])) {
8764 dcraw_message (DCRAW_ERROR,_("Non-numeric argument to \"-%c\"\n"), opt);
8765 return 1;
8766 }
8767 switch (opt) {
8768 case 'n': threshold = atof(argv[arg++]); break;
8769 case 'b': bright = atof(argv[arg++]); break;
8770 case 'r':
8771 FORC4 user_mul[c] = atof(argv[arg++]); break;
8772 case 'C': aber[0] = 1 / atof(argv[arg++]);
8773 aber[2] = 1 / atof(argv[arg++]); break;
8774 case 'g': gamm[0] = atof(argv[arg++]);
8775 gamm[1] = atof(argv[arg++]);
8776 if (gamm[0]) gamm[0] = 1/gamm[0]; break;
8777 case 'k': user_black = atoi(argv[arg++]); break;
8778 case 'S': user_sat = atoi(argv[arg++]); break;
8779 case 't': user_flip = atoi(argv[arg++]); break;
8780 case 'q': user_qual = atoi(argv[arg++]); break;
8781 case 'm': med_passes = atoi(argv[arg++]); break;
8782 case 'H': highlight = atoi(argv[arg++]); break;
8783 case 's':
8784 shot_select = abs(atoi(argv[arg]));
8785 multi_out = !strcmp(argv[arg++],"all");
8786 break;
8787 case 'o':
8788 if (isdigit(argv[arg][0]) && !argv[arg][1])
8789 output_color = atoi(argv[arg++]);
8790 #ifndef NO_LCMS
8791 else out_profile = argv[arg++];
8792 break;
8793 case 'p': cam_profile = argv[arg++];
8794 #endif
8795 break;
8796 case 'P': bpfile = const_cast<char *>(argv[arg++]); break;
8797 case 'K': dark_frame = argv[arg++]; break;
8798 case 'z': timestamp_only = 1; break;
8799 case 'e': thumbnail_only = 1; break;
8800 case 'i': identify_only = 1; break;
8801 case 'c': write_to_stdout = 1; break;
8802 case 'v': verbose = 1; break;
8803 case 'h': half_size = 1; /* "-h" implies "-f" */
8804 case 'f': four_color_rgb = 1; break;
8805 case 'A': FORC4 greybox[c] = atoi(argv[arg++]);
8806 case 'a': use_auto_wb = 1; break;
8807 case 'w': use_camera_wb = 1; break;
8808 case 'M': use_camera_matrix = (opm == '+'); break;
8809 case 'D':
8810 case 'd': document_mode = 1 + (opt == 'D');
8811 case 'j': use_fuji_rotate = 0; break;
8812 case 'W': no_auto_bright = 1; break;
8813 case 'T': output_tiff = 1; break;
8814 case '4': gamm[0] = gamm[1] =
8815 no_auto_bright = 1;
8816 case '6': output_bps = 16; break;
8817 default:
8818 dcraw_message (DCRAW_ERROR,_("Unknown option \"-%c\".\n"), opt);
8819 return 1;
8820 }
8821 }
8822 if (use_camera_matrix < 0)
8823 use_camera_matrix = use_camera_wb;
8824 if (arg == argc) {
8825 dcraw_message (DCRAW_ERROR,_("No files to process.\n"));
8826 return 1;
8827 }
8828 if (write_to_stdout) {
8829 if (isatty(1)) {
8830 dcraw_message (DCRAW_ERROR,_("Will not write an image to the terminal!\n"));
8831 return 1;
8832 }
8833 #if defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__)
8834 if (setmode(1,O_BINARY) < 0) {
8835 perror ("setmode()");
8836 return 1;
8837 }
8838 #endif
8839 }
8840 for ( ; arg < argc; arg++) {
8841 status = 1;
8842 image = 0;
8843 oprof = 0;
8844 meta_data = ofname = 0;
8845 ofp = stdout;
8846 if (setjmp (failure)) {
8847 if (fileno(ifp) > 2) fclose(ifp);
8848 if (fileno(ofp) > 2) fclose(ofp);
8849 status = 1;
8850 goto cleanup;
8851 }
8852 ifname = const_cast<char *>(argv[arg]);
8853 ifname_display = ifname;
8854 if (!(ifp = fopen (ifname, "rb"))) {
8855 perror (ifname);
8856 continue;
8857 }
8858 status = (identify(),!is_raw);
8859 if (user_flip >= 0)
8860 flip = user_flip;
8861 switch ((flip+3600) % 360) {
8862 case 270: flip = 5; break;
8863 case 180: flip = 3; break;
8864 case 90: flip = 6;
8865 }
8866 if (timestamp_only) {
8867 if ((status = !timestamp))
8868 dcraw_message (DCRAW_ERROR,_("%s has no timestamp.\n"), ifname);
8869 else if (identify_only)
8870 printf ("%10ld%10d %s\n", (long) timestamp, shot_order, ifname);
8871 else {
8872 dcraw_message (DCRAW_VERBOSE,_("%s time set to %d.\n"), ifname, (int) timestamp);
8873 ut.actime = ut.modtime = timestamp;
8874 utime (ifname, &ut);
8875 }
8876 goto next;
8877 }
8878 write_fun = &CLASS write_ppm_tiff;
8879 if (thumbnail_only) {
8880 if ((status = !thumb_offset)) {
8881 dcraw_message (DCRAW_ERROR,_("%s has no thumbnail.\n"), ifname);
8882 goto next;
8883 } else if (thumb_load_raw) {
8884 load_raw = thumb_load_raw;
8885 data_offset = thumb_offset;
8886 height = thumb_height;
8887 width = thumb_width;
8888 filters = 0;
8889 } else {
8890 fseek (ifp, thumb_offset, SEEK_SET);
8891 write_fun = write_thumb;
8892 goto thumbnail;
8893 }
8894 }
8895 if (load_raw == &CLASS kodak_ycbcr_load_raw) {
8896 height += height & 1;
8897 width += width & 1;
8898 }
8899 if (identify_only && verbose && make[0]) {
8900 printf (_("\nFilename: %s\n"), ifname);
8901 printf (_("Timestamp: %s"), ctime(×tamp));
8902 printf (_("Camera: %s %s\n"), make, model);
8903 if (artist[0])
8904 printf (_("Owner: %s\n"), artist);
8905 if (dng_version) {
8906 printf (_("DNG Version: "));
8907 for (i=24; i >= 0; i -= 8)
8908 printf ("%d%c", dng_version >> i & 255, i ? '.':'\n');
8909 }
8910 printf (_("ISO speed: %d\n"), (int) iso_speed);
8911 printf (_("Shutter: "));
8912 if (shutter > 0 && shutter < 1)
8913 shutter = (printf ("1/"), 1 / shutter);
8914 printf (_("%0.1f sec\n"), shutter);
8915 printf (_("Aperture: f/%0.1f\n"), aperture);
8916 printf (_("Focal length: %0.1f mm\n"), focal_len);
8917 printf (_("Embedded ICC profile: %s\n"), profile_length ? _("yes"):_("no"));
8918 printf (_("Number of raw images: %d\n"), is_raw);
8919 if (pixel_aspect != 1)
8920 printf (_("Pixel Aspect Ratio: %0.6f\n"), pixel_aspect);
8921 if (thumb_offset)
8922 printf (_("Thumb size: %4d x %d\n"), thumb_width, thumb_height);
8923 printf (_("Full size: %4d x %d\n"), raw_width, raw_height);
8924 } else if (!is_raw)
8925 dcraw_message (DCRAW_ERROR,_("Cannot decode file %s\n"), ifname);
8926 if (!is_raw) goto next;
8927 shrink = filters && (half_size ||
8928 ((threshold || aber[0] != 1 || aber[2] != 1) && !identify_only));
8929 iheight = (height + shrink) >> shrink;
8930 iwidth = (width + shrink) >> shrink;
8931 if (identify_only) {
8932 if (verbose) {
8933 if (use_fuji_rotate) {
8934 if (fuji_width) {
8935 fuji_width = (fuji_width - 1 + shrink) >> shrink;
8936 iwidth = fuji_width / sqrt(0.5);
8937 iheight = (iheight - fuji_width) / sqrt(0.5);
8938 } else {
8939 if (pixel_aspect < 1) iheight = iheight / pixel_aspect + 0.5;
8940 if (pixel_aspect > 1) iwidth = iwidth * pixel_aspect + 0.5;
8941 }
8942 }
8943 if (flip & 4)
8944 SWAP(iheight,iwidth);
8945 printf (_("Image size: %4d x %d\n"), width, height);
8946 printf (_("Output size: %4d x %d\n"), iwidth, iheight);
8947 printf (_("Raw colors: %d"), colors);
8948 if (filters) {
8949 printf (_("\nFilter pattern: "));
8950 if (!cdesc[3]) cdesc[3] = 'G';
8951 for (i=0; i < 16; i++)
8952 putchar (cdesc[fc(i >> 1,i & 1)]);
8953 }
8954 printf (_("\nDaylight multipliers:"));
8955 FORCC printf (" %f", pre_mul[c]);
8956 if (cam_mul[0] > 0) {
8957 printf (_("\nCamera multipliers:"));
8958 FORC4 printf (" %f", cam_mul[c]);
8959 }
8960 putchar ('\n');
8961 } else
8962 printf (_("%s is a %s %s image.\n"), ifname, make, model);
8963 next:
8964 fclose(ifp);
8965 continue;
8966 }
8967 if (use_camera_matrix && cmatrix[0][0] > 0.25) {
8968 memcpy (rgb_cam, cmatrix, sizeof cmatrix);
8969 raw_color = 0;
8970 }
8971 image = (ushort (*)[4]) calloc (iheight*iwidth, sizeof *image);
8972 merror (image, "main()");
8973 if (meta_length) {
8974 meta_data = (char *) malloc (meta_length);
8975 merror (meta_data, "main()");
8976 }
8977 dcraw_message (DCRAW_VERBOSE,_("Loading %s %s image from %s ...\n"),
8978 make, model, ifname);
8979 if (shot_select >= is_raw)
8980 dcraw_message (DCRAW_ERROR,_("%s: \"-s %d\" requests a nonexistent image!\n"),
8981 ifname, shot_select);
8982 #ifdef HAVE_FSEEKO
8983 fseeko (ifp, data_offset, SEEK_SET);
8984 #else
8985 fseek (ifp, data_offset, SEEK_SET);
8986 #endif
8987 (*this.*load_raw)();
8988 if (zero_is_bad) remove_zeroes();
8989 bad_pixels (bpfile);
8990 if (dark_frame) subtract (dark_frame);
8991 quality = 2 + !fuji_width;
8992 if (user_qual >= 0) quality = user_qual;
8993 i = cblack[3];
8994 FORC3 if (i > (int) cblack[c]) i = cblack[c];
8995 FORC4 cblack[c] -= i;
8996 black += i;
8997 if (user_black >= 0) black = user_black;
8998 if (user_sat > 0) maximum = user_sat;
8999 #ifdef COLORCHECK
9000 colorcheck();
9001 #endif
9002 if (is_foveon && !document_mode) foveon_interpolate();
9003 if (!is_foveon && document_mode < 2) scale_colors();
9004 pre_interpolate();
9005 if (filters && !document_mode) {
9006 if (quality == 0)
9007 lin_interpolate();
9008 else if (quality == 1 || colors > 3)
9009 vng_interpolate();
9010 else if (quality == 2)
9011 ppg_interpolate();
9012 else ahd_interpolate();
9013 }
9014 if (mix_green)
9015 for (colors=3, i=0; i < height*width; i++)
9016 image[i][1] = (image[i][1] + image[i][3]) >> 1;
9017 if (!is_foveon && colors == 3) median_filter();
9018 if (!is_foveon && highlight == 2) blend_highlights();
9019 if (!is_foveon && highlight > 2) recover_highlights();
9020 if (use_fuji_rotate) fuji_rotate();
9021 #ifndef NO_LCMS
9022 if (cam_profile) apply_profile (cam_profile, out_profile);
9023 #endif
9024 convert_to_rgb();
9025 if (use_fuji_rotate) stretch();
9026 thumbnail:
9027 if (write_fun == &CLASS jpeg_thumb)
9028 write_ext = ".jpg";
9029 else if (output_tiff && write_fun == &CLASS write_ppm_tiff)
9030 write_ext = ".tiff";
9031 else
9032 write_ext = ".pgm\0.ppm\0.ppm\0.pam" + colors*5-5;
9033 ofname = (char *) malloc (strlen(ifname) + 64);
9034 merror (ofname, "main()");
9035 if (write_to_stdout)
9036 strcpy (ofname,_("standard output"));
9037 else {
9038 strcpy (ofname, ifname);
9039 if ((cp = strrchr (ofname, '.'))) *cp = 0;
9040 if (multi_out)
9041 sprintf (ofname+strlen(ofname), "_%0*d",
9042 snprintf(0,0,"%d",is_raw-1), shot_select);
9043 if (thumbnail_only)
9044 strcat (ofname, ".thumb");
9045 strcat (ofname, write_ext);
9046 ofp = fopen (ofname, "wb");
9047 if (!ofp) {
9048 status = 1;
9049 perror (ofname);
9050 goto cleanup;
9051 }
9052 }
9053 dcraw_message (DCRAW_VERBOSE,_("Writing data to %s ...\n"), ofname);
9054 (*this.*write_fun)();
9055 fclose(ifp);
9056 if (ofp != stdout) fclose(ofp);
9057 cleanup:
9058 if (meta_data) free (meta_data);
9059 if (ofname) free (ofname);
9060 if (oprof) free (oprof);
9061 if (image) free (image);
9062 if (multi_out) {
9063 if (++shot_select < is_raw) arg--;
9064 else shot_select = 0;
9065 }
9066 }
9067 /* Make sure ifname are not free()'d (UF) */
9068 ifname = NULL;
9069 ifname_display = NULL;
9070 return status;
9071 }
9072
9073 #ifndef DCRAW_NOMAIN /*UF*/
main(int argc,const char ** argv)9074 int main(int argc, const char **argv)
9075 {
9076 DCRaw *d = new DCRaw;
9077 return d->main(argc, argv);
9078 }
9079 #endif /*DCRAW_NOMAIN*/ /*UF*/
9080