compile
[kdegraphics.git] / libs / libkdcraw / libraw / internal / dcraw_common.cpp
blob9c5f1983d5c20d02e7a631f11ee7bc7c9b6c218b
1 /*
2 GENERATED FILE, DO NOT EDIT
3 Generated from dcraw/dcraw.c at Sun Mar 15 13:33:55 2009
4 Look into original file (probably http://cybercom.net/~dcoffin/dcraw/dcraw.c)
5 for copyright information.
6 */
8 #line 260 "dcraw/dcraw.c"
9 #define CLASS LibRaw::
10 #include "libraw/libraw_types.h"
11 #define LIBRAW_LIBRARY_BUILD
12 #define LIBRAW_IO_REDEFINED
13 #include "libraw/libraw.h"
14 #include "internal/defines.h"
15 #include "internal/var_defines.h"
17 #line 270 "dcraw/dcraw.c"
19 #ifndef __GLIBC__
20 char *my_memmem (char *haystack, size_t haystacklen,
21 char *needle, size_t needlelen)
23 char *c;
24 for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
25 if (!memcmp (c, needle, needlelen))
26 return c;
27 return 0;
29 #define memmem my_memmem
30 #endif
32 #line 304 "dcraw/dcraw.c"
34 ushort CLASS sget2 (uchar *s)
36 if (order == 0x4949) /* "II" means little-endian */
37 return s[0] | s[1] << 8;
38 else /* "MM" means big-endian */
39 return s[0] << 8 | s[1];
42 ushort CLASS get2()
44 uchar str[2] = { 0xff,0xff };
45 fread (str, 1, 2, ifp);
46 return sget2(str);
49 unsigned CLASS sget4 (uchar *s)
51 if (order == 0x4949)
52 return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
53 else
54 return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
56 #define sget4(s) sget4((uchar *)s)
58 unsigned CLASS get4()
60 uchar str[4] = { 0xff,0xff,0xff,0xff };
61 fread (str, 1, 4, ifp);
62 return sget4(str);
65 unsigned CLASS getint (int type)
67 return type == 3 ? get2() : get4();
70 float CLASS int_to_float (int i)
72 union { int i; float f; } u;
73 u.i = i;
74 return u.f;
77 double CLASS getreal (int type)
79 union { char c[8]; double d; } u;
80 int i, rev;
82 switch (type) {
83 case 3: return (unsigned short) get2();
84 case 4: return (unsigned int) get4();
85 case 5: u.d = (unsigned int) get4();
86 return u.d / (unsigned int) get4();
87 case 8: return (signed short) get2();
88 case 9: return (signed int) get4();
89 case 10: u.d = (signed int) get4();
90 return u.d / (signed int) get4();
91 case 11: return int_to_float (get4());
92 case 12:
93 rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
94 for (i=0; i < 8; i++)
95 u.c[i ^ rev] = fgetc(ifp);
96 return u.d;
97 default: return fgetc(ifp);
101 void CLASS read_shorts (ushort *pixel, int count)
103 if (fread (pixel, 2, count, ifp) < count) derror();
104 if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
105 swab ((char*)pixel, (char*)pixel, count*2);
107 #line 381 "dcraw/dcraw.c"
108 void CLASS canon_black (double dark[2])
110 int c, diff, row, col;
112 if (raw_width < width+4) return;
113 FORC(2) dark[c] /= (raw_width-width-2) * height >> 1;
114 #ifdef LIBRAW_LIBRARY_BUILD
115 if(!( filtering_mode & LIBRAW_FILTERING_NOBLACKS) )
117 #endif
118 if ((diff = dark[0] - dark[1]))
119 for (row=0; row < height; row++)
120 for (col=1; col < width; col+=2)
121 BAYER(row,col) += diff;
122 #ifdef LIBRAW_LIBRARY_BUILD
124 #endif
125 dark[1] += diff;
126 black = (dark[0] + dark[1] + 1) / 2;
129 void CLASS canon_600_fixed_wb (int temp)
131 static const short mul[4][5] = {
132 { 667, 358,397,565,452 },
133 { 731, 390,367,499,517 },
134 { 1119, 396,348,448,537 },
135 { 1399, 485,431,508,688 } };
136 int lo, hi, i;
137 float frac=0;
139 for (lo=4; --lo; )
140 if (*mul[lo] <= temp) break;
141 for (hi=0; hi < 3; hi++)
142 if (*mul[hi] >= temp) break;
143 if (lo != hi)
144 frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
145 for (i=1; i < 5; i++)
146 pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
147 #ifdef LIBRAW_LIBRARY_BUILD
148 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
149 #endif
152 /* Return values: 0 = white 1 = near white 2 = not white */
153 int CLASS canon_600_color (int ratio[2], int mar)
155 int clipped=0, target, miss;
157 if (flash_used) {
158 if (ratio[1] < -104)
159 { ratio[1] = -104; clipped = 1; }
160 if (ratio[1] > 12)
161 { ratio[1] = 12; clipped = 1; }
162 } else {
163 if (ratio[1] < -264 || ratio[1] > 461) return 2;
164 if (ratio[1] < -50)
165 { ratio[1] = -50; clipped = 1; }
166 if (ratio[1] > 307)
167 { ratio[1] = 307; clipped = 1; }
169 target = flash_used || ratio[1] < 197
170 ? -38 - (398 * ratio[1] >> 10)
171 : -123 + (48 * ratio[1] >> 10);
172 if (target - mar <= ratio[0] &&
173 target + 20 >= ratio[0] && !clipped) return 0;
174 miss = target - ratio[0];
175 if (abs(miss) >= mar*4) return 2;
176 if (miss < -20) miss = -20;
177 if (miss > mar) miss = mar;
178 ratio[0] = target - miss;
179 return 1;
182 void CLASS canon_600_auto_wb()
184 int mar, row, col, i, j, st, count[] = { 0,0 };
185 int test[8], total[2][8], ratio[2][2], stat[2];
187 memset (&total, 0, sizeof total);
188 i = canon_ev + 0.5;
189 if (i < 10) mar = 150;
190 else if (i > 12) mar = 20;
191 else mar = 280 - 20 * i;
192 if (flash_used) mar = 80;
193 for (row=14; row < height-14; row+=4)
194 for (col=10; col < width; col+=2) {
195 for (i=0; i < 8; i++)
196 test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
197 BAYER(row+(i >> 1),col+(i & 1));
198 for (i=0; i < 8; i++)
199 if (test[i] < 150 || test[i] > 1500) goto next;
200 for (i=0; i < 4; i++)
201 if (abs(test[i] - test[i+4]) > 50) goto next;
202 for (i=0; i < 2; i++) {
203 for (j=0; j < 4; j+=2)
204 ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
205 stat[i] = canon_600_color (ratio[i], mar);
207 if ((st = stat[0] | stat[1]) > 1) goto next;
208 for (i=0; i < 2; i++)
209 if (stat[i])
210 for (j=0; j < 2; j++)
211 test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
212 for (i=0; i < 8; i++)
213 total[st][i] += test[i];
214 count[st]++;
215 next: ;
217 if (count[0] | count[1]) {
218 st = count[0]*200 < count[1];
219 for (i=0; i < 4; i++)
220 pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
221 #ifdef LIBRAW_LIBRARY_BUILD
222 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CALCULATED;
223 #endif
227 void CLASS canon_600_coeff()
229 static const short table[6][12] = {
230 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
231 { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
232 { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
233 { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
234 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
235 { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
236 int t=0, i, c;
237 float mc, yc;
239 mc = pre_mul[1] / pre_mul[2];
240 yc = pre_mul[3] / pre_mul[2];
241 if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
242 if (mc > 1.28 && mc <= 2) {
243 if (yc < 0.8789) t=3;
244 else if (yc <= 2) t=4;
246 if (flash_used) t=5;
247 for (raw_color = i=0; i < 3; i++)
248 FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
249 #ifdef LIBRAW_LIBRARY_BUILD
250 color_flags.rgb_cam_state = LIBRAW_COLORSTATE_CALCULATED;
251 #endif
254 void CLASS canon_600_load_raw()
256 uchar data[1120], *dp;
257 ushort pixel[896], *pix;
258 int irow, row, col, val;
259 static const short mul[4][2] =
260 { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
262 for (irow=row=0; irow < height; irow++) {
263 if (fread (data, 1, raw_width*5/4, ifp) < raw_width*5/4) derror();
264 for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8) {
265 pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
266 pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
267 pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
268 pix[3] = (dp[4] << 2) + (dp[1] & 3);
269 pix[4] = (dp[5] << 2) + (dp[9] & 3);
270 pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
271 pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
272 pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
274 for (col=0; col < width; col++)
275 BAYER(row,col) = pixel[col];
276 for (col=width; col < raw_width; col++)
278 black += pixel[col];
279 #ifdef LIBRAW_LIBRARY_BUILD
280 ushort *dfp = get_masked_pointer(row,col);
281 if(dfp) *dfp = pixel[col];
282 #endif
284 if ((row+=2) > height) row = 1;
286 if (raw_width > width)
287 black = black / ((raw_width - width) * height) - 4;
288 for (row=0; row < height; row++)
289 for (col=0; col < width; col++) {
290 #ifdef LIBRAW_LIBRARY_BUILD
291 if( filtering_mode & LIBRAW_FILTERING_NOBLACKS)
292 val = BAYER(row,col);
293 else
294 #endif
295 if ((val = BAYER(row,col) - black) < 0) val = 0;
296 val = val * mul[row & 3][col & 1] >> 9;
297 BAYER(row,col) = val;
299 canon_600_fixed_wb(1311);
300 canon_600_auto_wb();
301 canon_600_coeff();
302 maximum = (0x3ff - black) * 1109 >> 9;
303 black = 0;
306 void CLASS remove_zeroes()
308 unsigned row, col, tot, n, r, c;
310 #ifdef LIBRAW_LIBRARY_BUILD
311 RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,0,2);
312 #endif
313 for (row=0; row < height; row++)
314 for (col=0; col < width; col++)
315 if (BAYER(row,col) == 0) {
316 tot = n = 0;
317 for (r = row-2; r <= row+2; r++)
318 for (c = col-2; c <= col+2; c++)
319 if (r < height && c < width &&
320 FC(r,c) == FC(row,col) && BAYER(r,c))
321 tot += (n++,BAYER(r,c));
322 if (n) BAYER(row,col) = tot/n;
324 #ifdef LIBRAW_LIBRARY_BUILD
325 RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES,1,2);
326 #endif
329 int CLASS canon_s2is()
331 unsigned row;
333 for (row=0; row < 100; row++) {
334 fseek (ifp, row*3340 + 3284, SEEK_SET);
335 if (getc(ifp) > 15) return 1;
337 return 0;
340 void CLASS canon_a5_load_raw()
342 ushort data[2565], *dp, pixel;
343 int vbits=0, buf=0, row, col, bc=0;
345 order = 0x4949;
346 for (row=-top_margin; row < raw_height-top_margin; row++) {
347 read_shorts (dp=data, raw_width * 10 / 16);
348 for (col=-left_margin; col < raw_width-left_margin; col++) {
349 if ((vbits -= 10) < 0)
350 buf = (vbits += 16, (buf << 16) + *dp++);
351 pixel = buf >> vbits & 0x3ff;
352 #ifdef LIBRAW_LIBRARY_BUILD
353 ushort *dfp = get_masked_pointer(row+top_margin,col+left_margin);
354 if(dfp) *dfp = pixel;
355 #endif
356 if ((unsigned) row < height && (unsigned) col < width)
357 BAYER(row,col) = pixel;
358 else if (col > 1-left_margin && col != width)
359 black += (bc++,pixel);
362 if (bc) black /= bc;
363 maximum = 0x3ff;
365 #ifdef LIBRAW_LIBRARY_BUILD
366 if(!(filtering_mode & LIBRAW_FILTERING_NOZEROES))
367 #endif
368 if (raw_width > 1600) remove_zeroes();
372 getbits(-1) initializes the buffer
373 getbits(n) where 0 <= n <= 25 returns an n-bit integer
375 unsigned CLASS getbits (int nbits)
377 #ifdef LIBRAW_NOTHREADS
378 static unsigned bitbuf=0;
379 static int vbits=0, reset=0;
380 #else
381 #define bitbuf tls->getbits.bitbuf
382 #define vbits tls->getbits.vbits
383 #define reset tls->getbits.reset
384 #endif
385 unsigned c;
387 if (nbits == -1)
388 return bitbuf = vbits = reset = 0;
389 if (nbits == 0 || reset) return 0;
390 while (vbits < nbits) {
391 if ((c = fgetc(ifp)) == EOF) derror();
392 if ((reset = zero_after_ff && c == 0xff && fgetc(ifp))) return 0;
393 bitbuf = (bitbuf << 8) + (uchar) c;
394 vbits += 8;
396 vbits -= nbits;
397 return bitbuf << (32-nbits-vbits) >> (32-nbits);
398 #ifndef LIBRAW_NOTHREADS
399 #undef bitbuf
400 #undef vbits
401 #undef reset
402 #endif
405 void CLASS init_decoder()
407 memset (first_decode, 0, sizeof first_decode);
408 free_decode = first_decode;
412 Construct a decode tree according to the specification in *source.
413 The first 16 bytes specify how many codes should be 1-bit, 2-bit
414 3-bit, etc. Bytes after that are the leaf values.
416 For example, if the source is
418 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
419 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
421 then the code is
423 00 0x04
424 010 0x03
425 011 0x05
426 100 0x06
427 101 0x02
428 1100 0x07
429 1101 0x01
430 11100 0x08
431 11101 0x09
432 11110 0x00
433 111110 0x0a
434 1111110 0x0b
435 1111111 0xff
437 uchar * CLASS make_decoder (const uchar *source, int level)
439 struct decode *cur;
440 #ifndef LIBRAW_NOTHREADS
441 #define t_leaf tls->make_decoder_leaf
442 #else
443 static int t_leaf;
444 #endif
445 int i, next;
447 if (level==0) t_leaf=0;
448 cur = free_decode++;
449 if (free_decode > first_decode+2048) {
450 #ifdef LIBRAW_LIBRARY_BUILD
451 throw LIBRAW_EXCEPTION_DECODE_RAW;
452 #else
453 fprintf (stderr,_("%s: decoder table overflow\n"), ifname);
454 longjmp (failure, 2);
455 #endif
457 for (i=next=0; i <= t_leaf && next < 16; )
458 i += source[next++];
459 if (i > t_leaf) {
460 if (level < next) {
461 cur->branch[0] = free_decode;
462 make_decoder (source, level+1);
463 cur->branch[1] = free_decode;
464 make_decoder (source, level+1);
465 } else
466 cur->leaf = source[16 + t_leaf++];
468 return (uchar *) source + 16 + t_leaf;
469 #ifndef LIBRAW_NOTHREADS
470 #undef t_leaf
471 #endif
474 void CLASS crw_init_tables (unsigned table)
476 static const uchar first_tree[3][29] = {
477 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
478 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
479 { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
480 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
481 { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
482 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
484 static const uchar second_tree[3][180] = {
485 { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
486 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
487 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
488 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
489 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
490 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
491 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
492 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
493 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
494 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
495 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
496 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
497 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
498 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
499 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
500 { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
501 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
502 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
503 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
504 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
505 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
506 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
507 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
508 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
509 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
510 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
511 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
512 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
513 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
514 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
515 { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
516 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
517 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
518 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
519 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
520 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
521 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
522 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
523 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
524 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
525 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
526 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
527 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
528 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
529 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
531 if (table > 2) table = 2;
532 init_decoder();
533 make_decoder ( first_tree[table], 0);
534 second_decode = free_decode;
535 make_decoder (second_tree[table], 0);
539 Return 0 if the image starts with compressed data,
540 1 if it starts with uncompressed low-order bits.
542 In Canon compressed data, 0xff is always followed by 0x00.
544 int CLASS canon_has_lowbits()
546 uchar test[0x4000];
547 int ret=1, i;
549 fseek (ifp, 0, SEEK_SET);
550 fread (test, 1, sizeof test, ifp);
551 for (i=540; i < sizeof test - 1; i++)
552 if (test[i] == 0xff) {
553 if (test[i+1]) return 1;
554 ret=0;
556 return ret;
559 void CLASS canon_compressed_load_raw()
561 ushort *pixel, *prow;
562 int nblocks, lowbits, i, row, r, col, save, val;
563 unsigned irow, icol;
564 struct decode *decode, *dindex;
565 int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
566 double dark[2] = { 0,0 };
567 uchar c;
569 crw_init_tables (tiff_compress);
570 pixel = (ushort *) calloc (raw_width*8, sizeof *pixel);
571 merror (pixel, "canon_compressed_load_raw()");
572 lowbits = canon_has_lowbits();
573 if (!lowbits) maximum = 0x3ff;
574 fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
575 zero_after_ff = 1;
576 getbits(-1);
577 for (row=0; row < raw_height; row+=8) {
578 nblocks = MIN (8, raw_height-row) * raw_width >> 6;
579 for (block=0; block < nblocks; block++) {
580 memset (diffbuf, 0, sizeof diffbuf);
581 decode = first_decode;
582 for (i=0; i < 64; i++ ) {
583 for (dindex=decode; dindex->branch[0]; )
584 dindex = dindex->branch[getbits(1)];
585 leaf = dindex->leaf;
586 decode = second_decode;
587 if (leaf == 0 && i) break;
588 if (leaf == 0xff) continue;
589 i += leaf >> 4;
590 len = leaf & 15;
591 if (len == 0) continue;
592 diff = getbits(len);
593 if ((diff & (1 << (len-1))) == 0)
594 diff -= (1 << len) - 1;
595 if (i < 64) diffbuf[i] = diff;
597 diffbuf[0] += carry;
598 carry = diffbuf[0];
599 for (i=0; i < 64; i++ ) {
600 if (pnum++ % raw_width == 0)
601 base[0] = base[1] = 512;
602 if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
603 derror();
606 if (lowbits) {
607 save = ftell(ifp);
608 fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
609 for (prow=pixel, i=0; i < raw_width*2; i++) {
610 c = fgetc(ifp);
611 for (r=0; r < 8; r+=2, prow++) {
612 val = (*prow << 2) + ((c >> r) & 3);
613 if (raw_width == 2672 && val < 512) val += 2;
614 *prow = val;
617 fseek (ifp, save, SEEK_SET);
619 for (r=0; r < 8; r++) {
620 irow = row - top_margin + r;
621 #ifndef LIBRAW_LIBRARY_BUILD
622 if (irow >= height) continue;
623 #endif
624 for (col=0; col < raw_width; col++) {
625 #ifdef LIBRAW_LIBRARY_BUILD
626 ushort *dfp = get_masked_pointer(row+r,col);
627 if(dfp) *dfp = pixel[r*raw_width+col];
628 if (irow >= height) continue; // skip for top/bottom rows
629 #endif
630 icol = col - left_margin;
631 if (icol < width)
632 BAYER(irow,icol) = pixel[r*raw_width+col];
633 else if (col > 1)
634 dark[icol & 1] += pixel[r*raw_width+col];
638 free (pixel);
639 canon_black (dark);
642 #line 927 "dcraw/dcraw.c"
643 int CLASS ljpeg_start (struct jhead *jh, int info_only)
645 int c, tag, len;
646 uchar data[0x10000], *dp;
648 if (!info_only) init_decoder();
649 memset (jh, 0, sizeof *jh);
650 FORC(6) jh->huff[c] = free_decode;
651 jh->restart = INT_MAX;
652 fread (data, 2, 1, ifp);
653 if (data[1] != 0xd8) return 0;
654 do {
655 fread (data, 2, 2, ifp);
656 tag = data[0] << 8 | data[1];
657 len = (data[2] << 8 | data[3]) - 2;
658 if (tag <= 0xff00) return 0;
659 fread (data, 1, len, ifp);
660 switch (tag) {
661 case 0xffc3:
662 jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
663 case 0xffc0:
664 jh->bits = data[0];
665 jh->high = data[1] << 8 | data[2];
666 jh->wide = data[3] << 8 | data[4];
667 jh->clrs = data[5] + jh->sraw;
668 if (len == 9 && !dng_version) getc(ifp);
669 break;
670 case 0xffc4:
671 if (info_only) break;
672 for (dp = data; dp < data+len && *dp < 4; ) {
673 jh->huff[*dp] = free_decode;
674 dp = make_decoder (++dp, 0);
676 break;
677 case 0xffda:
678 jh->psv = data[1+data[0]*2];
679 jh->bits -= data[3+data[0]*2] & 15;
680 break;
681 case 0xffdd:
682 jh->restart = data[0] << 8 | data[1];
684 } while (tag != 0xffda);
685 if (info_only) return 1;
686 if (jh->sraw) {
687 FORC(4) jh->huff[2+c] = jh->huff[1];
688 FORC(jh->sraw) jh->huff[1+c] = jh->huff[0];
690 jh->row = (ushort *) calloc (jh->wide*jh->clrs, 4);
691 merror (jh->row, "ljpeg_start()");
692 return zero_after_ff = 1;
695 int CLASS ljpeg_diff (struct decode *dindex)
697 int len, diff;
699 while (dindex->branch[0])
700 dindex = dindex->branch[getbits(1)];
701 len = dindex->leaf;
702 if (len == 16 && (!dng_version || dng_version >= 0x1010000))
703 return -32768;
704 diff = getbits(len);
705 if ((diff & (1 << (len-1))) == 0)
706 diff -= (1 << len) - 1;
707 return diff;
710 ushort * CLASS ljpeg_row (int jrow, struct jhead *jh)
712 int col, c, diff, pred, spred=0;
713 ushort mark=0, *row[3];
715 if (jrow * jh->wide % jh->restart == 0) {
716 FORC(6) jh->vpred[c] = 1 << (jh->bits-1);
717 if (jrow)
718 do mark = (mark << 8) + (c = fgetc(ifp));
719 while (c != EOF && mark >> 4 != 0xffd);
720 getbits(-1);
722 FORC3 row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
723 for (col=0; col < jh->wide; col++)
724 FORC(jh->clrs) {
725 diff = ljpeg_diff (jh->huff[c]);
726 if (jh->sraw && c <= jh->sraw && (col | c))
727 pred = spred;
728 else if (col) pred = row[0][-jh->clrs];
729 else pred = (jh->vpred[c] += diff) - diff;
730 if (jrow && col) switch (jh->psv) {
731 case 1: break;
732 case 2: pred = row[1][0]; break;
733 case 3: pred = row[1][-jh->clrs]; break;
734 case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
735 case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
736 case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
737 case 7: pred = (pred + row[1][0]) >> 1; break;
738 default: pred = 0;
740 if ((**row = pred + diff) >> jh->bits) derror();
741 if (c <= jh->sraw) spred = **row;
742 row[0]++; row[1]++;
744 return row[2];
747 void CLASS lossless_jpeg_load_raw()
749 int jwide, jrow, jcol, val, jidx, i, j, row=0, col=0;
750 double dark[2] = { 0,0 };
751 struct jhead jh;
752 int min=INT_MAX;
753 ushort *rp;
755 if (!ljpeg_start (&jh, 0)) return;
756 jwide = jh.wide * jh.clrs;
758 for (jrow=0; jrow < jh.high; jrow++) {
759 rp = ljpeg_row (jrow, &jh);
760 for (jcol=0; jcol < jwide; jcol++) {
761 val = *rp++;
762 if (jh.bits <= 12)
763 #ifdef LIBRAW_LIBRARY_BUILD
764 if( !(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
765 #endif
766 val = curve[val & 0xfff];
767 if (cr2_slice[0]) {
768 jidx = jrow*jwide + jcol;
769 i = jidx / (cr2_slice[1]*jh.high);
770 if ((j = i >= cr2_slice[0]))
771 i = cr2_slice[0];
772 jidx -= i * (cr2_slice[1]*jh.high);
773 row = jidx / cr2_slice[1+j];
774 col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
776 if (raw_width == 3984 && (col -= 2) < 0)
777 col += (row--,raw_width);
778 #ifdef LIBRAW_LIBRARY_BUILD
779 ushort *dfp = get_masked_pointer(row,col);
780 if(dfp) *dfp = val;
781 #endif
782 if ((unsigned) (row-top_margin) < height) {
783 if ((unsigned) (col-left_margin) < width) {
784 BAYER(row-top_margin,col-left_margin) = val;
785 if (min > val) min = val;
786 } else if (col > 1)
787 dark[(col-left_margin) & 1] += val;
789 if (++col >= raw_width)
790 col = (row++,0);
793 free (jh.row);
794 canon_black (dark);
795 if (!strcasecmp(make,"KODAK"))
796 black = min;
799 void CLASS canon_sraw_load_raw()
801 struct jhead jh;
802 short *rp=0, (*ip)[4];
803 int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
804 int v[3]={0,0,0}, ver, hue;
805 char *cp;
807 if (!ljpeg_start (&jh, 0)) return;
808 jwide = (jh.wide >>= 1) * jh.clrs;
810 for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
811 scol = ecol;
812 ecol += cr2_slice[1] * 2 / jh.clrs;
813 if (!cr2_slice[0] || ecol > raw_width-1) ecol = raw_width & -2;
814 for (row=0; row < height; row += (jh.clrs >> 1) - 1) {
815 ip = (short (*)[4]) image + row*width;
816 for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
817 if ((jcol %= jwide) == 0)
818 rp = (short *) ljpeg_row (jrow++, &jh);
819 if (col >= width) continue;
820 FORC (jh.clrs-2)
821 ip[col + (c >> 1)*width + (c & 1)][0] = rp[jcol+c];
822 ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
823 ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
827 for (cp=model2; *cp && !isdigit(*cp); cp++);
828 sscanf (cp, "%d.%d.%d", v, v+1, v+2);
829 ver = (v[0]*1000 + v[1])*1000 + v[2];
830 hue = (jh.sraw+1) << 2;
831 if (unique_id == 0x80000218 && ver > 1000006 && ver < 3000000)
832 hue = jh.sraw << 1;
833 ip = (short (*)[4]) image;
834 rp = ip[0];
835 for (row=0; row < height; row++, ip+=width) {
836 if (row & (jh.sraw >> 1))
837 for (col=0; col < width; col+=2)
838 for (c=1; c < 3; c++)
839 if (row == height-1)
840 ip[col][c] = ip[col-width][c];
841 else ip[col][c] = (ip[col-width][c] + ip[col+width][c] + 1) >> 1;
842 for (col=1; col < width; col+=2)
843 for (c=1; c < 3; c++)
844 if (col == width-1)
845 ip[col][c] = ip[col-1][c];
846 else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
848 for ( ; rp < ip[0]; rp+=4) {
849 if (unique_id < 0x80000200) {
850 pix[0] = rp[0] + rp[2] - 512;
851 pix[2] = rp[0] + rp[1] - 512;
852 pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12) - 512;
853 } else {
854 rp[1] = (rp[1] << 2) + hue;
855 rp[2] = (rp[2] << 2) + hue;
856 pix[0] = rp[0] + (( 200*rp[1] + 22929*rp[2]) >> 14);
857 pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
858 pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
860 FORC3 rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
862 free (jh.row);
863 maximum = 0x3fff;
866 void CLASS adobe_copy_pixel (int row, int col, ushort **rp)
868 unsigned r, c;
870 r = row -= top_margin;
871 c = col -= left_margin;
872 if (is_raw == 2 && shot_select) (*rp)++;
873 if (filters) {
874 #ifndef LIBRAW_LIBRARY_BUILD
875 if (fuji_width) {
876 r = row + fuji_width - 1 - (col >> 1);
877 c = row + ((col+1) >> 1);
879 #endif
880 #ifdef LIBRAW_LIBRARY_BUILD
881 ushort val = **rp;
882 if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
883 val = **rp < 0x1000 ? curve[**rp] : **rp;
884 if (r < height && c < width)
885 BAYER(r,c) = val;
886 else
888 ushort *dfp = get_masked_pointer(row+top_margin,col+left_margin);
889 if(dfp) *dfp = val;
891 #else
892 if (r < height && c < width)
893 BAYER(r,c) = **rp < 0x1000 ? curve[**rp] : **rp;
894 #endif
895 *rp += is_raw;
896 } else {
897 if (r < height && c < width)
898 FORC(tiff_samples)
899 image[row*width+col][c] = (*rp)[c] < 0x1000 ? curve[(*rp)[c]]:(*rp)[c];
900 *rp += tiff_samples;
902 if (is_raw == 2 && shot_select) (*rp)--;
905 void CLASS adobe_dng_load_raw_lj()
907 unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col;
908 struct jhead jh;
909 ushort *rp;
911 while (trow < raw_height) {
912 save = ftell(ifp);
913 if (tile_length < INT_MAX)
914 fseek (ifp, get4(), SEEK_SET);
915 if (!ljpeg_start (&jh, 0)) break;
916 jwide = jh.wide;
917 if (filters) jwide *= jh.clrs;
918 jwide /= is_raw;
919 for (row=col=jrow=0; jrow < jh.high; jrow++) {
920 rp = ljpeg_row (jrow, &jh);
921 for (jcol=0; jcol < jwide; jcol++) {
922 adobe_copy_pixel (trow+row, tcol+col, &rp);
923 if (++col >= tile_width || col >= raw_width)
924 row += 1 + (col = 0);
927 fseek (ifp, save+4, SEEK_SET);
928 if ((tcol += tile_width) >= raw_width)
929 trow += tile_length + (tcol = 0);
930 free (jh.row);
934 void CLASS adobe_dng_load_raw_nc()
936 ushort *pixel, *rp;
937 int row, col;
939 pixel = (ushort *) calloc (raw_width * tiff_samples, sizeof *pixel);
940 merror (pixel, "adobe_dng_load_raw_nc()");
941 for (row=0; row < raw_height; row++) {
942 if (tiff_bps == 16)
943 read_shorts (pixel, raw_width * tiff_samples);
944 else {
945 getbits(-1);
946 for (col=0; col < raw_width * tiff_samples; col++)
947 pixel[col] = getbits(tiff_bps);
949 for (rp=pixel, col=0; col < raw_width; col++)
950 adobe_copy_pixel (row, col, &rp);
952 free (pixel);
955 void CLASS pentax_tree()
957 ushort bit[2][13];
958 struct decode *cur;
959 int c, i, j;
961 init_decoder();
962 FORC(13) bit[0][c] = get2();
963 FORC(13) bit[1][c] = fgetc(ifp) & 15;
964 FORC(13) {
965 cur = first_decode;
966 for (i=0; i < bit[1][c]; i++) {
967 j = bit[0][c] >> (11-i) & 1;
968 if (!cur->branch[j]) cur->branch[j] = ++free_decode;
969 cur = cur->branch[j];
971 cur->leaf = c;
975 void CLASS pentax_k10_load_raw()
977 int row, col, diff;
978 ushort vpred[2][2] = {{0,0},{0,0}}, hpred[2];
980 getbits(-1);
981 for (row=0; row < raw_height; row++)
983 #ifndef LIBRAW_LIBRARY_BUILD
984 if(row >= height) break;
985 #endif
986 for (col=0; col < raw_width; col++) {
987 diff = ljpeg_diff (first_decode);
988 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
989 else hpred[col & 1] += diff;
990 if (col < width && row < height)
991 BAYER(row,col) = hpred[col & 1];
992 #ifdef LIBRAW_LIBRARY_BUILD
993 else
995 ushort *dfp = get_masked_pointer(row,col);
996 if(dfp) *dfp = hpred[col & 1];
999 if (col < width && row < height)
1000 #endif
1001 if (hpred[col & 1] >> 12) derror();
1006 void CLASS nikon_compressed_load_raw()
1008 static const uchar nikon_tree[][32] = {
1009 { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
1010 5,4,3,6,2,7,1,0,8,9,11,10,12 },
1011 { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
1012 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
1013 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
1014 5,4,6,3,7,2,8,1,9,0,10,11,12 },
1015 { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
1016 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
1017 { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
1018 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
1019 { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
1020 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
1021 struct decode *dindex;
1022 ushort ver0, ver1, vpred[2][2], hpred[2], csize;
1023 int i, min, max, step=0, huff=0, split=0, row, col, len, shl, diff;
1025 fseek (ifp, meta_offset, SEEK_SET);
1026 ver0 = fgetc(ifp);
1027 ver1 = fgetc(ifp);
1028 if (ver0 == 0x49 || ver1 == 0x58)
1029 fseek (ifp, 2110, SEEK_CUR);
1030 if (ver0 == 0x46) huff = 2;
1031 if (tiff_bps == 14) huff += 3;
1032 read_shorts (vpred[0], 4);
1033 max = 1 << tiff_bps & 0x7fff;
1034 if ((csize = get2()) > 1)
1035 step = max / (csize-1);
1036 if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
1037 for (i=0; i < csize; i++)
1038 curve[i*step] = get2();
1039 for (i=0; i < max; i++)
1040 curve[i] = ( curve[i-i%step]*(step-i%step) +
1041 curve[i-i%step+step]*(i%step) ) / step;
1042 #ifdef LIBRAW_LIBRARY_BUILD
1043 color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
1044 #endif
1045 fseek (ifp, meta_offset+562, SEEK_SET);
1046 split = get2();
1047 } else if (ver0 != 0x46 && csize <= 0x4001)
1049 read_shorts (curve, max=csize);
1050 #ifdef LIBRAW_LIBRARY_BUILD
1051 color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
1052 #endif
1054 while (curve[max-2] == curve[max-1]) max--;
1055 init_decoder();
1056 make_decoder (nikon_tree[huff], 0);
1057 fseek (ifp, data_offset, SEEK_SET);
1058 getbits(-1);
1059 for (min=row=0; row < height; row++) {
1060 if (split && row == split) {
1061 init_decoder();
1062 make_decoder (nikon_tree[huff+1], 0);
1063 max += (min = 16) << 1;
1065 for (col=0; col < raw_width; col++) {
1066 for (dindex=first_decode; dindex->branch[0]; )
1067 dindex = dindex->branch[getbits(1)];
1068 len = dindex->leaf & 15;
1069 shl = dindex->leaf >> 4;
1070 diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
1071 if ((diff & (1 << (len-1))) == 0)
1072 diff -= (1 << len) - !shl;
1073 if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
1074 else hpred[col & 1] += diff;
1075 if ((ushort)(hpred[col & 1] + min) >= max) derror();
1076 #ifndef LIBRAW_LIBRARY_BUILD
1077 if ((unsigned) (col-left_margin) < width)
1078 BAYER(row,col-left_margin) = curve[LIM((short)hpred[col & 1],0,0x3fff)];
1079 #else
1080 ushort xval = hpred[col & 1];
1081 if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
1082 xval = curve[LIM((short)xval,0,0x3fff)];
1083 if ((unsigned) (col-left_margin) < width)
1085 BAYER(row,col-left_margin) = xval;
1087 else
1089 ushort *dfp = get_masked_pointer(row,col);
1090 if(dfp) *dfp = xval;
1092 #endif
1099 Figure out if a NEF file is compressed. These fancy heuristics
1100 are only needed for the D100, thanks to a bug in some cameras
1101 that tags all images as "compressed".
1103 int CLASS nikon_is_compressed()
1105 uchar test[256];
1106 int i;
1108 fseek (ifp, data_offset, SEEK_SET);
1109 fread (test, 1, 256, ifp);
1110 for (i=15; i < 256; i+=16)
1111 if (test[i]) return 1;
1112 return 0;
1116 Returns 1 for a Coolpix 995, 0 for anything else.
1118 int CLASS nikon_e995()
1120 int i, histo[256];
1121 const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
1123 memset (histo, 0, sizeof histo);
1124 fseek (ifp, -2000, SEEK_END);
1125 for (i=0; i < 2000; i++)
1126 histo[fgetc(ifp)]++;
1127 for (i=0; i < 4; i++)
1128 if (histo[often[i]] < 200)
1129 return 0;
1130 return 1;
1134 Returns 1 for a Coolpix 2100, 0 for anything else.
1136 int CLASS nikon_e2100()
1138 uchar t[12];
1139 int i;
1141 fseek (ifp, 0, SEEK_SET);
1142 for (i=0; i < 1024; i++) {
1143 fread (t, 1, 12, ifp);
1144 if (((t[2] & t[4] & t[7] & t[9]) >> 4
1145 & t[1] & t[6] & t[8] & t[11] & 3) != 3)
1146 return 0;
1148 return 1;
1151 void CLASS nikon_3700()
1153 int bits, i;
1154 uchar dp[24];
1155 static const struct {
1156 int bits;
1157 char t_make[12], t_model[15];
1158 } table[] = {
1159 { 0x00, "PENTAX", "Optio 33WR" },
1160 { 0x03, "NIKON", "E3200" },
1161 { 0x32, "NIKON", "E3700" },
1162 { 0x33, "OLYMPUS", "C740UZ" } };
1164 fseek (ifp, 3072, SEEK_SET);
1165 fread (dp, 1, 24, ifp);
1166 bits = (dp[8] & 3) << 4 | (dp[20] & 3);
1167 for (i=0; i < sizeof table / sizeof *table; i++)
1168 if (bits == table[i].bits) {
1169 strcpy (make, table[i].t_make );
1170 strcpy (model, table[i].t_model);
1175 Separates a Minolta DiMAGE Z2 from a Nikon E4300.
1177 int CLASS minolta_z2()
1179 int i, nz;
1180 char tail[424];
1182 fseek (ifp, -sizeof tail, SEEK_END);
1183 fread (tail, 1, sizeof tail, ifp);
1184 for (nz=i=0; i < sizeof tail; i++)
1185 if (tail[i]) nz++;
1186 return nz > 20;
1189 /* Here raw_width is in bytes, not pixels. */
1190 void CLASS nikon_e900_load_raw()
1192 int offset=0, irow, row, col;
1194 for (irow=0; irow < height; irow++) {
1195 row = irow * 2 % height;
1196 if (row == 1)
1197 offset = - (-offset & -4096);
1198 fseek (ifp, offset, SEEK_SET);
1199 offset += raw_width;
1200 getbits(-1);
1201 for (col=0; col < width; col++)
1202 BAYER(row,col) = getbits(10);
1207 The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
1209 void CLASS fuji_load_raw()
1211 ushort *pixel;
1212 #ifndef LIBRAW_LIBRARY_BUILD
1213 int wide, row, col, r, c;
1215 fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
1216 wide = fuji_width << !fuji_layout;
1217 pixel = (ushort *) calloc (wide, sizeof *pixel);
1218 merror (pixel, "fuji_load_raw()");
1219 for (row=0; row < raw_height; row++) {
1220 read_shorts (pixel, wide);
1221 fseek (ifp, 2*(raw_width - wide), SEEK_CUR);
1222 for (col=0; col < wide; col++) {
1223 if (fuji_layout) {
1224 r = fuji_width - 1 - col + (row >> 1);
1225 c = col + ((row+1) >> 1);
1226 } else {
1227 r = fuji_width - 1 + row - (col >> 1);
1228 c = row + ((col+1) >> 1);
1230 BAYER(r,c) = pixel[col];
1233 free (pixel);
1234 #else
1235 int row,col;
1236 int wide, r, c;
1237 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1238 merror (pixel, "fuji_load_raw()");
1239 for (row=0; row < raw_height; row++) {
1240 read_shorts (pixel, raw_width);
1241 for (col=0; col < raw_width; col++) {
1242 if(col >= left_margin && col < width+left_margin
1243 && row >= top_margin && row < height+top_margin)
1245 int rrow = row-top_margin;
1246 int ccol = col-left_margin;
1247 if (fuji_layout) {
1248 r = fuji_width - 1 - ccol + (rrow >> 1);
1249 c = ccol + ((rrow+1) >> 1);
1250 } else {
1251 r = fuji_width - 1 + rrow - (ccol >> 1);
1252 c = rrow + ((ccol+1) >> 1);
1255 image[((row-top_margin) >> shrink)*iwidth + ((col-left_margin) >> shrink)][FC(r,c)] = pixel[col];
1257 else
1259 ushort *dfp = get_masked_pointer(row,col);
1260 if(dfp) *dfp = pixel[col];
1264 free (pixel);
1265 #endif
1267 #line 1556 "dcraw/dcraw.c"
1268 void CLASS ppm_thumb (FILE *tfp)
1270 char *thumb;
1271 thumb_length = thumb_width*thumb_height*3;
1272 thumb = (char *) malloc (thumb_length);
1273 merror (thumb, "ppm_thumb()");
1274 fprintf (tfp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1275 fread (thumb, 1, thumb_length, ifp);
1276 fwrite (thumb, 1, thumb_length, tfp);
1277 free (thumb);
1280 void CLASS layer_thumb (FILE *tfp)
1282 int i, c;
1283 char *thumb, map[][4] = { "012","102" };
1285 colors = thumb_misc >> 5 & 7;
1286 thumb_length = thumb_width*thumb_height;
1287 thumb = (char *) calloc (colors, thumb_length);
1288 merror (thumb, "layer_thumb()");
1289 fprintf (tfp, "P%d\n%d %d\n255\n",
1290 5 + (colors >> 1), thumb_width, thumb_height);
1291 fread (thumb, thumb_length, colors, ifp);
1292 for (i=0; i < thumb_length; i++)
1293 FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], tfp);
1294 free (thumb);
1297 void CLASS rollei_thumb (FILE *tfp)
1299 unsigned i;
1300 ushort *thumb;
1302 thumb_length = thumb_width * thumb_height;
1303 thumb = (ushort *) calloc (thumb_length, 2);
1304 merror (thumb, "rollei_thumb()");
1305 fprintf (tfp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1306 read_shorts (thumb, thumb_length);
1307 for (i=0; i < thumb_length; i++) {
1308 putc (thumb[i] << 3, tfp);
1309 putc (thumb[i] >> 5 << 2, tfp);
1310 putc (thumb[i] >> 11 << 3, tfp);
1312 free (thumb);
1315 void CLASS rollei_load_raw()
1317 uchar pixel[10];
1318 unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
1320 isix = raw_width * raw_height * 5 / 8;
1321 while (fread (pixel, 1, 10, ifp) == 10) {
1322 for (i=0; i < 10; i+=2) {
1323 todo[i] = iten++;
1324 todo[i+1] = pixel[i] << 8 | pixel[i+1];
1325 buffer = pixel[i] >> 2 | buffer << 6;
1327 for ( ; i < 16; i+=2) {
1328 todo[i] = isix++;
1329 todo[i+1] = buffer >> (14-i)*5;
1331 for (i=0; i < 16; i+=2) {
1332 row = todo[i] / raw_width - top_margin;
1333 col = todo[i] % raw_width - left_margin;
1334 if (row < height && col < width)
1335 BAYER(row,col) = (todo[i+1] & 0x3ff);
1336 #ifdef LIBRAW_LIBRARY_BUILD
1337 else
1339 ushort *dfp = get_masked_pointer(todo[i] / raw_width,todo[i] % raw_width);
1340 if(dfp) *dfp = (todo[i+1] & 0x3ff);
1342 #endif
1345 maximum = 0x3ff;
1348 int CLASS bayer (unsigned row, unsigned col)
1350 return (row < height && col < width) ? BAYER(row,col) : 0;
1353 void CLASS phase_one_flat_field (int is_float, int nc)
1355 ushort head[8];
1356 unsigned wide, y, x, c, rend, cend, row, col;
1357 float *mrow, num, mult[4];
1359 read_shorts (head, 8);
1360 wide = head[2] / head[4];
1361 mrow = (float *) calloc (nc*wide, sizeof *mrow);
1362 merror (mrow, "phase_one_flat_field()");
1363 for (y=0; y < head[3] / head[5]; y++) {
1364 for (x=0; x < wide; x++)
1365 for (c=0; c < nc; c+=2) {
1366 num = is_float ? getreal(11) : get2()/32768.0;
1367 if (y==0) mrow[c*wide+x] = num;
1368 else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
1370 if (y==0) continue;
1371 rend = head[1]-top_margin + y*head[5];
1372 for (row = rend-head[5]; row < height && row < rend; row++) {
1373 for (x=1; x < wide; x++) {
1374 for (c=0; c < nc; c+=2) {
1375 mult[c] = mrow[c*wide+x-1];
1376 mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
1378 cend = head[0]-left_margin + x*head[4];
1379 for (col = cend-head[4]; col < width && col < cend; col++) {
1380 c = nc > 2 ? FC(row,col) : 0;
1381 if (!(c & 1)) {
1382 c = BAYER(row,col) * mult[c];
1383 BAYER(row,col) = LIM(c,0,65535);
1385 for (c=0; c < nc; c+=2)
1386 mult[c] += mult[c+1];
1389 for (x=0; x < wide; x++)
1390 for (c=0; c < nc; c+=2)
1391 mrow[c*wide+x] += mrow[(c+1)*wide+x];
1394 free (mrow);
1397 void CLASS phase_one_correct()
1399 unsigned entries, tag, data, save, col, row, type;
1400 int len, i, j, k, cip, val[4], dev[4], sum, max;
1401 int head[9], diff, mindiff=INT_MAX, off_412=0;
1402 static const signed char dir[12][2] =
1403 { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
1404 {-2,-2}, {-2,2}, {2,-2}, {2,2} };
1405 float poly[8], num, cfrac, frac, mult[2], *yval[2];
1406 ushort t_curve[0x10000], *xval[2];
1408 if (half_size || !meta_length) return;
1409 #ifdef DCRAW_VERBOSE
1410 if (verbose) fprintf (stderr,_("Phase One correction...\n"));
1411 #endif
1412 fseek (ifp, meta_offset, SEEK_SET);
1413 order = get2();
1414 fseek (ifp, 6, SEEK_CUR);
1415 fseek (ifp, meta_offset+get4(), SEEK_SET);
1416 entries = get4(); get4();
1417 while (entries--) {
1418 tag = get4();
1419 len = get4();
1420 data = get4();
1421 save = ftell(ifp);
1422 fseek (ifp, meta_offset+data, SEEK_SET);
1423 if (tag == 0x419) { /* Polynomial curve */
1424 for (get4(), i=0; i < 8; i++)
1425 poly[i] = getreal(11);
1426 poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
1427 for (i=0; i < 0x10000; i++) {
1428 num = (poly[5]*i + poly[3])*i + poly[1];
1429 t_curve[i] = LIM(num,0,65535);
1430 } goto apply; /* apply to right half */
1431 } else if (tag == 0x41a) { /* Polynomial curve */
1432 for (i=0; i < 4; i++)
1433 poly[i] = getreal(11);
1434 for (i=0; i < 0x10000; i++) {
1435 for (num=0, j=4; j--; )
1436 num = num * i + poly[j];
1437 t_curve[i] = LIM(num+i,0,65535);
1438 } apply: /* apply to whole image */
1439 for (row=0; row < height; row++)
1440 for (col = (tag & 1)*ph1.split_col; col < width; col++)
1441 BAYER(row,col) = t_curve[BAYER(row,col)];
1442 } else if (tag == 0x400) { /* Sensor defects */
1443 while ((len -= 8) >= 0) {
1444 col = get2() - left_margin;
1445 row = get2() - top_margin;
1446 type = get2(); get2();
1447 if (col >= width) continue;
1448 if (type == 131) /* Bad column */
1449 for (row=0; row < height; row++)
1450 if (FC(row,col) == 1) {
1451 for (sum=i=0; i < 4; i++)
1452 sum += val[i] = bayer (row+dir[i][0], col+dir[i][1]);
1453 for (max=i=0; i < 4; i++) {
1454 dev[i] = abs((val[i] << 2) - sum);
1455 if (dev[max] < dev[i]) max = i;
1457 BAYER(row,col) = (sum - val[max])/3.0 + 0.5;
1458 } else {
1459 for (sum=0, i=8; i < 12; i++)
1460 sum += bayer (row+dir[i][0], col+dir[i][1]);
1461 BAYER(row,col) = 0.5 + sum * 0.0732233 +
1462 (bayer(row,col-2) + bayer(row,col+2)) * 0.3535534;
1464 else if (type == 129) { /* Bad pixel */
1465 if (row >= height) continue;
1466 j = (FC(row,col) != 1) * 4;
1467 for (sum=0, i=j; i < j+8; i++)
1468 sum += bayer (row+dir[i][0], col+dir[i][1]);
1469 BAYER(row,col) = (sum + 4) >> 3;
1472 } else if (tag == 0x401) { /* All-color flat fields */
1473 phase_one_flat_field (1, 2);
1474 } else if (tag == 0x416 || tag == 0x410) {
1475 phase_one_flat_field (0, 2);
1476 } else if (tag == 0x40b) { /* Red+blue flat field */
1477 phase_one_flat_field (0, 4);
1478 } else if (tag == 0x412) {
1479 fseek (ifp, 36, SEEK_CUR);
1480 diff = abs (get2() - ph1.tag_21a);
1481 if (mindiff > diff) {
1482 mindiff = diff;
1483 off_412 = ftell(ifp) - 38;
1486 fseek (ifp, save, SEEK_SET);
1488 if (off_412) {
1489 fseek (ifp, off_412, SEEK_SET);
1490 for (i=0; i < 9; i++) head[i] = get4() & 0x7fff;
1491 yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
1492 merror (yval[0], "phase_one_correct()");
1493 yval[1] = (float *) (yval[0] + head[1]*head[3]);
1494 xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
1495 xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
1496 get2();
1497 for (i=0; i < 2; i++)
1498 for (j=0; j < head[i+1]*head[i+3]; j++)
1499 yval[i][j] = getreal(11);
1500 for (i=0; i < 2; i++)
1501 for (j=0; j < head[i+1]*head[i+3]; j++)
1502 xval[i][j] = get2();
1503 for (row=0; row < height; row++)
1504 for (col=0; col < width; col++) {
1505 cfrac = (float) col * head[3] / raw_width;
1506 cfrac -= cip = cfrac;
1507 num = BAYER(row,col) * 0.5;
1508 for (i=cip; i < cip+2; i++) {
1509 for (k=j=0; j < head[1]; j++)
1510 if (num < xval[0][k = head[1]*i+j]) break;
1511 frac = (j == 0 || j == head[1]) ? 0 :
1512 (xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
1513 mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
1515 i = ((mult[0] * (1-cfrac) + mult[1] * cfrac)
1516 * (row + top_margin) + num) * 2;
1517 BAYER(row,col) = LIM(i,0,65535);
1519 free (yval[0]);
1523 void CLASS phase_one_load_raw()
1525 int row, col, a, b;
1526 ushort *pixel, akey, bkey, mask;
1528 fseek (ifp, ph1.key_off, SEEK_SET);
1529 akey = get2();
1530 bkey = get2();
1531 mask = ph1.format == 1 ? 0x5555:0x1354;
1532 #ifndef LIBRAW_LIBRARY_BUILD
1533 fseek (ifp, data_offset + top_margin*raw_width*2, SEEK_SET);
1534 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1535 merror (pixel, "phase_one_load_raw()");
1536 for (row=0; row < height; row++) {
1537 read_shorts (pixel, raw_width);
1538 for (col=0; col < raw_width; col+=2) {
1539 a = pixel[col+0] ^ akey;
1540 b = pixel[col+1] ^ bkey;
1541 pixel[col+0] = (a & mask) | (b & ~mask);
1542 pixel[col+1] = (b & mask) | (a & ~mask);
1544 for (col=0; col < width; col++)
1545 BAYER(row,col) = pixel[col+left_margin];
1547 free (pixel);
1548 #else
1549 fseek (ifp, data_offset, SEEK_SET);
1550 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1551 merror (pixel, "phase_one_load_raw()");
1552 for (row=0; row < raw_height; row++) {
1553 read_shorts (pixel, raw_width);
1554 for (col=0; col < raw_width; col+=2) {
1555 a = pixel[col+0] ^ akey;
1556 b = pixel[col+1] ^ bkey;
1557 pixel[col+0] = (a & mask) | (b & ~mask);
1558 pixel[col+1] = (b & mask) | (a & ~mask);
1560 for (col=0; col < raw_width; col++)
1562 ushort *dfp = get_masked_pointer(row,col);
1563 if(dfp)
1564 *dfp = pixel[col];
1565 else
1566 BAYER(row,col-left_margin) = pixel[col];
1569 free (pixel);
1570 if(!( filtering_mode & LIBRAW_FILTERING_NORAWCURVE) )
1571 #endif
1572 phase_one_correct();
1575 unsigned CLASS ph1_bits (int nbits)
1577 #ifndef LIBRAW_NOTHREADS
1578 #define bitbuf tls->ph1_bits.bitbuf
1579 #define vbits tls->ph1_bits.vbits
1580 #else
1581 static UINT64 bitbuf=0;
1582 static int vbits=0;
1583 #endif
1584 if (nbits == -1)
1585 return bitbuf = vbits = 0;
1586 if (nbits == 0) return 0;
1587 if ((vbits -= nbits) < 0) {
1588 bitbuf = bitbuf << 32 | get4();
1589 vbits += 32;
1591 return bitbuf << (64-nbits-vbits) >> (64-nbits);
1592 #ifndef LIBRAW_NOTHREADS
1593 #undef bitbuf
1594 #undef vbits
1595 #endif
1598 void CLASS phase_one_load_raw_c()
1600 static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
1601 int *offset, len[2], pred[2], row, col, i, j;
1602 ushort *pixel;
1603 short (*t_black)[2];
1605 pixel = (ushort *) calloc (raw_width + raw_height*4, 2);
1606 merror (pixel, "phase_one_load_raw_c()");
1607 offset = (int *) (pixel + raw_width);
1608 fseek (ifp, strip_offset, SEEK_SET);
1609 for (row=0; row < raw_height; row++)
1610 offset[row] = get4();
1611 t_black = (short (*)[2]) offset + raw_height;
1612 fseek (ifp, ph1.black_off, SEEK_SET);
1613 if (ph1.black_off)
1615 read_shorts ((ushort *) t_black[0], raw_height*2);
1616 #ifdef LIBRAW_LIBRARY_BUILD
1617 imgdata.masked_pixels.ph1_black = (ushort (*)[2])calloc(raw_height*2,sizeof(ushort));
1618 merror (imgdata.masked_pixels.ph1_black, "phase_one_load_raw_c()");
1619 memmove(imgdata.masked_pixels.ph1_black,(ushort *) t_black[0],raw_height*2*sizeof(ushort));
1620 #endif
1622 for (i=0; i < 256; i++)
1623 curve[i] = i*i / 3.969 + 0.5;
1624 #ifdef LIBRAW_LIBRARY_BUILD
1625 color_flags.curve_state = LIBRAW_COLORSTATE_CALCULATED;
1626 #endif
1627 for (row=0; row < raw_height; row++) {
1628 fseek (ifp, data_offset + offset[row], SEEK_SET);
1629 ph1_bits(-1);
1630 pred[0] = pred[1] = 0;
1631 for (col=0; col < raw_width; col++) {
1632 if (col >= (raw_width & -8))
1633 len[0] = len[1] = 14;
1634 else if ((col & 7) == 0)
1635 for (i=0; i < 2; i++) {
1636 for (j=0; j < 5 && !ph1_bits(1); j++);
1637 if (j--) len[i] = length[j*2 + ph1_bits(1)];
1639 if ((i = len[col & 1]) == 14)
1640 pixel[col] = pred[col & 1] = ph1_bits(16);
1641 else
1642 pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
1643 if (pred[col & 1] >> 16) derror();
1644 #ifdef LIBRAW_LIBRARY_BUILD
1645 if(!( filtering_mode & LIBRAW_FILTERING_NORAWCURVE) )
1646 #endif
1647 if (ph1.format == 5 && pixel[col] < 256)
1648 pixel[col] = curve[pixel[col]];
1650 if ((unsigned) (row-top_margin) < height)
1651 #ifndef LIBRAW_LIBRARY_BUILD
1652 for (col=0; col < width; col++) {
1653 i = (pixel[col+left_margin] << 2)
1654 - ph1.t_black + t_black[row][col >= ph1.split_col];
1655 if (i > 0) BAYER(row-top_margin,col) = i;
1657 #else
1659 for (col=0; col < raw_width; col++) {
1660 if( filtering_mode & LIBRAW_FILTERING_NOBLACKS)
1661 i = (pixel[col] << 2);
1662 else
1663 i = (pixel[col] << 2)
1664 - ph1.t_black + t_black[row][(col /* - left_margin */) >= ph1.split_col]; // changed to fix Coffin's bug!
1665 if(col >= left_margin && col < width+left_margin)
1667 if (i > 0) BAYER(row-top_margin,col-left_margin) = i;
1669 else
1671 ushort *dfp = get_masked_pointer(row,col);
1672 if(i>0 && dfp) *dfp = i;
1676 else
1678 // top-bottom fields
1679 for (col=0; col < raw_width; col++) {
1680 i = (pixel[col] << 2)
1681 - ph1.t_black + t_black[row][(col+left_margin) >= ph1.split_col];
1682 if (i > 0)
1684 ushort *dfp = get_masked_pointer(row,col);
1685 if(dfp) *dfp = i;
1689 #endif
1691 free (pixel);
1692 #ifdef LIBRAW_LIBRARY_BUILD
1693 if(!( filtering_mode & LIBRAW_FILTERING_NORAWCURVE) )
1694 #endif
1695 phase_one_correct();
1696 maximum = 0xfffc - ph1.t_black;
1699 void CLASS hasselblad_load_raw()
1701 struct jhead jh;
1702 struct decode *dindex;
1703 int row, col, pred[2], len[2], diff, i;
1705 if (!ljpeg_start (&jh, 0)) return;
1706 free (jh.row);
1707 order = 0x4949;
1708 ph1_bits(-1);
1709 for (row=-top_margin; row < raw_height-top_margin; row++) {
1710 pred[0] = pred[1] = 0x8000;
1711 for (col=-left_margin; col < raw_width-left_margin; col+=2) {
1712 for (i=0; i < 2; i++) {
1713 for (dindex=jh.huff[0]; dindex->branch[0]; )
1714 dindex = dindex->branch[ph1_bits(1)];
1715 len[i] = dindex->leaf;
1717 for (i=0; i < 2; i++) {
1718 diff = ph1_bits(len[i]);
1719 if ((diff & (1 << (len[i]-1))) == 0)
1720 diff -= (1 << len[i]) - 1;
1721 if (diff == 65535) diff = -32768;
1722 pred[i] += diff;
1723 if (row >= 0 && row < height && (unsigned)(col+i) < width)
1724 BAYER(row,col+i) = pred[i];
1725 #ifdef LIBRAW_LIBRARY_BUILD
1726 else
1728 ushort *dfp = get_masked_pointer(row+top_margin,col+left_margin);
1729 if(dfp) *dfp = pred[i];
1731 #endif
1735 maximum = 0xffff;
1738 void CLASS leaf_hdr_load_raw()
1740 ushort *pixel;
1741 unsigned tile=0, r, c, row, col;
1743 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1744 merror (pixel, "leaf_hdr_load_raw()");
1745 FORC(tiff_samples)
1746 for (r=0; r < raw_height; r++) {
1747 if (r % tile_length == 0) {
1748 fseek (ifp, data_offset + 4*tile++, SEEK_SET);
1749 fseek (ifp, get4() + 2*left_margin, SEEK_SET);
1751 if (filters && c != shot_select) continue;
1752 read_shorts (pixel, raw_width);
1753 if ((row = r - top_margin) >= height) continue;
1754 for (col=0; col < width; col++)
1755 if (filters) BAYER(row,col) = pixel[col];
1756 else image[row*width+col][c] = pixel[col];
1758 free (pixel);
1759 if (!filters) {
1760 maximum = 0xffff;
1761 raw_color = 1;
1765 #line 2057 "dcraw/dcraw.c"
1766 void CLASS sinar_4shot_load_raw()
1768 ushort *pixel;
1769 unsigned shot, row, col, r, c;
1771 if ((shot = shot_select) || half_size) {
1772 if (shot) shot--;
1773 if (shot > 3) shot = 3;
1774 fseek (ifp, data_offset + shot*4, SEEK_SET);
1775 fseek (ifp, get4(), SEEK_SET);
1776 unpacked_load_raw();
1777 return;
1779 free (image);
1780 image = (ushort (*)[4])
1781 calloc ((iheight=height)*(iwidth=width), sizeof *image);
1782 merror (image, "sinar_4shot_load_raw()");
1783 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1784 merror (pixel, "sinar_4shot_load_raw()");
1785 for (shot=0; shot < 4; shot++) {
1786 fseek (ifp, data_offset + shot*4, SEEK_SET);
1787 fseek (ifp, get4(), SEEK_SET);
1788 for (row=0; row < raw_height; row++) {
1789 read_shorts (pixel, raw_width);
1790 if ((r = row-top_margin - (shot >> 1 & 1)) >= height) continue;
1791 for (col=0; col < raw_width; col++) {
1792 if ((c = col-left_margin - (shot & 1)) >= width) continue;
1793 image[r*width+c][FC(row,col)] = pixel[col];
1797 free (pixel);
1798 shrink = filters = 0;
1801 void CLASS imacon_full_load_raw()
1803 int row, col;
1805 for (row=0; row < height; row++)
1806 for (col=0; col < width; col++)
1807 read_shorts (image[row*width+col], 3);
1810 void CLASS packed_12_load_raw()
1812 int vbits=0, rbits=0, irow, row, col;
1813 UINT64 bitbuf=0;
1815 if (raw_width * 2 >= width * 3) { /* If raw_width is in bytes, */
1816 rbits = raw_width * 8;
1817 raw_width = raw_width * 2 / 3; /* convert it to pixels and */
1818 rbits -= raw_width * 12; /* save the remainder. */
1820 order = load_flags & 1 ? 0x4949 : 0x4d4d;
1821 for (irow=0; irow < height; irow++) {
1822 row = irow;
1823 if (load_flags & 2 &&
1824 (row = irow * 2 % height + irow / (height/2)) == 1 &&
1825 load_flags & 4) {
1826 if (vbits=0, tiff_compress)
1827 fseek (ifp, data_offset - (-width*height*3/4 & -2048), SEEK_SET);
1828 else {
1829 fseek (ifp, 0, SEEK_END);
1830 fseek (ifp, ftell(ifp)/2, SEEK_SET);
1833 for (col=0; col < raw_width; col++) {
1834 if ((vbits -= 12) < 0) {
1835 bitbuf = bitbuf << 32 | get4();
1836 vbits += 32;
1838 if ((unsigned) (col-left_margin) < width)
1839 BAYER(row,col-left_margin) = bitbuf << (52-vbits) >> 52;
1840 #ifdef LIBRAW_LIBRARY_BUILD
1841 else
1843 ushort *dfp = get_masked_pointer(row,col);
1844 if(dfp) *dfp = bitbuf << (52-vbits) >> 52;
1846 #endif
1847 if (load_flags & 8 && (col % 10) == 9)
1848 if (vbits=0, bitbuf & 255) derror();
1850 vbits -= rbits;
1852 if (!strcmp(make,"OLYMPUS")) black >>= 4;
1855 void CLASS unpacked_load_raw()
1857 ushort *pixel;
1858 int row, col, bits=0;
1860 while (1 << ++bits < maximum);
1861 #ifndef LIBRAW_LIBRARY_BUILD
1862 fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
1863 pixel = (ushort *) calloc (width, sizeof *pixel);
1864 merror (pixel, "unpacked_load_raw()");
1865 for (row=0; row < height; row++) {
1866 read_shorts (pixel, width);
1867 fseek (ifp, 2*(raw_width - width), SEEK_CUR);
1868 for (col=0; col < width; col++)
1869 if ((BAYER2(row,col) = pixel[col]) >> bits) derror();
1871 free (pixel);
1872 #else
1873 // fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
1874 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1875 merror (pixel, "unpacked_load_raw()");
1876 for (row=0; row < raw_height; row++) {
1877 read_shorts (pixel, raw_width);
1878 //fseek (ifp, 2*(raw_width - width), SEEK_CUR);
1879 for (col=0; col < raw_width; col++)
1881 ushort *dfp = get_masked_pointer(row,col);
1882 if(dfp)
1883 *dfp = pixel[col];
1884 else
1886 if ((BAYER2(row-top_margin,col-left_margin) = pixel[col]) >> bits) derror();
1890 free (pixel);
1891 #endif
1894 void CLASS nokia_load_raw()
1896 uchar *data, *dp;
1897 ushort *pixel, *pix;
1898 int dwide, row, c;
1900 dwide = raw_width * 5 / 4;
1901 data = (uchar *) malloc (dwide + raw_width*2);
1902 merror (data, "nokia_load_raw()");
1903 pixel = (ushort *) (data + dwide);
1904 for (row=0; row < raw_height; row++) {
1905 if (fread (data, 1, dwide, ifp) < dwide) derror();
1906 for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=5, pix+=4)
1907 FORC4 pix[c] = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
1908 if (row < top_margin)
1909 #ifdef LIBRAW_LIBRARY_BUILD
1911 int col;
1912 for(col=0;col<width;col++)
1914 ushort *dfp = get_masked_pointer(row,col);
1915 if(dfp)
1916 *dfp = pixel[col];
1918 FORC(width) black += pixel[c];
1920 #else
1921 FORC(width) black += pixel[c];
1922 #endif
1923 else
1924 FORC(width) BAYER(row-top_margin,c) = pixel[c];
1926 free (data);
1927 if (top_margin) black /= top_margin * width;
1928 maximum = 0x3ff;
1931 unsigned CLASS pana_bits (int nbits)
1933 #ifndef LIBRAW_NOTHREADS
1934 #define buf tls->pana_bits.buf
1935 #define vbits tls->pana_bits.vbits
1936 #else
1937 static uchar buf[0x4000];
1938 static int vbits;
1939 #endif
1940 int byte;
1942 if (!nbits) return vbits=0;
1943 if (!vbits) {
1944 fread (buf+load_flags, 1, 0x4000-load_flags, ifp);
1945 fread (buf, 1, load_flags, ifp);
1947 vbits = (vbits - nbits) & 0x1ffff;
1948 byte = vbits >> 3 ^ 0x3ff0;
1949 return (buf[byte] | buf[byte+1] << 8) >> (vbits & 7) & ~(-1 << nbits);
1950 #ifndef LIBRAW_NOTHREADS
1951 #undef buf
1952 #undef vbits
1953 #endif
1956 void CLASS panasonic_load_raw()
1958 int row, col, i, j, sh=0, pred[2], nonz[2];
1960 pana_bits(0);
1961 for (row=0; row < height; row++)
1962 for (col=0; col < raw_width; col++) {
1963 if ((i = col % 14) == 0)
1964 pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
1965 if (i % 3 == 2) sh = 4 >> (3 - pana_bits(2));
1966 if (nonz[i & 1]) {
1967 if ((j = pana_bits(8))) {
1968 if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
1969 pred[i & 1] &= ~(-1 << sh);
1970 pred[i & 1] += j << sh;
1972 } else if ((nonz[i & 1] = pana_bits(8)) || i > 11)
1973 pred[i & 1] = nonz[i & 1] << 4 | pana_bits(4);
1974 if (col < width)
1975 if ((BAYER(row,col) = pred[col & 1]) > 4098) derror();
1976 #ifdef LIBRAW_LIBRARY_BUILD
1977 if(col>=width)
1979 ushort *dfp = get_masked_pointer(row,col);
1980 if(dfp)*dfp = pred[col & 1];
1982 #endif
1986 void CLASS olympus_e300_load_raw()
1988 uchar *data, *dp;
1989 ushort *pixel, *pix;
1990 int dwide, row, col;
1992 dwide = raw_width * 16 / 10;
1993 #ifndef LIBRAW_LIBRARY_BUILD
1994 fseek (ifp, dwide*top_margin, SEEK_CUR);
1995 #endif
1996 data = (uchar *) malloc (dwide + raw_width*2);
1997 merror (data, "olympus_e300_load_raw()");
1998 pixel = (ushort *) (data + dwide);
1999 #ifndef LIBRAW_LIBRARY_BUILD
2000 for (row=0; row < height; row++)
2001 #else
2002 for (row=0; row < raw_height; row++)
2003 #endif
2005 if (fread (data, 1, dwide, ifp) < dwide) derror();
2006 for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=3, pix+=2) {
2007 if (((dp-data) & 15) == 15)
2008 if (*dp++ && pix < pixel+width+left_margin) derror();
2009 pix[0] = dp[1] << 8 | dp[0];
2010 pix[1] = dp[2] << 4 | dp[1] >> 4;
2012 #ifndef LIBRAW_LIBRARY_BUILD
2013 for (col=0; col < width; col++)
2014 BAYER(row,col) = (pixel[col+left_margin] & 0xfff);
2015 #else
2016 for (col=0; col < raw_width; col++)
2018 ushort *dfp = get_masked_pointer(row,col);
2019 if(dfp)
2020 *dfp = (pixel[col] & 0xfff);
2021 else
2022 BAYER(row-top_margin,col-left_margin) = (pixel[col] & 0xfff);
2025 #endif
2027 free (data);
2028 maximum >>= 4;
2029 black >>= 4;
2032 void CLASS olympus_e410_load_raw()
2034 int row, col, nbits, sign, low, high, i, w, n, nw;
2035 int acarry[2][3], *carry, pred, diff;
2037 fseek (ifp, 7, SEEK_CUR);
2038 getbits(-1);
2039 for (row=0; row < height; row++) {
2040 memset (acarry, 0, sizeof acarry);
2041 for (col=0; col < width; col++) {
2042 carry = acarry[col & 1];
2043 i = 2 * (carry[2] < 3);
2044 for (nbits=2+i; (ushort) carry[0] >> (nbits+i); nbits++);
2045 sign = getbits(1) * -1;
2046 low = getbits(2);
2047 for (high=0; high < 12; high++)
2048 if (getbits(1)) break;
2049 if (high == 12)
2050 high = getbits(16-nbits) >> 1;
2051 carry[0] = (high << nbits) | getbits(nbits);
2052 diff = (carry[0] ^ sign) + carry[1];
2053 carry[1] = (diff*3 + carry[1]) >> 5;
2054 carry[2] = carry[0] > 16 ? 0 : carry[2]+1;
2055 if (row < 2 && col < 2) pred = 0;
2056 else if (row < 2) pred = BAYER(row,col-2);
2057 else if (col < 2) pred = BAYER(row-2,col);
2058 else {
2059 w = BAYER(row,col-2);
2060 n = BAYER(row-2,col);
2061 nw = BAYER(row-2,col-2);
2062 if ((w < nw && nw < n) || (n < nw && nw < w)) {
2063 if (ABS(w-nw) > 32 || ABS(n-nw) > 32)
2064 pred = w + n - nw;
2065 else pred = (w + n) >> 1;
2066 } else pred = ABS(w-nw) > ABS(n-nw) ? w : n;
2068 if ((BAYER(row,col) = pred + ((diff << 2) | low)) >> 12) derror();
2073 void CLASS minolta_rd175_load_raw()
2075 uchar pixel[768];
2076 unsigned irow, box, row, col;
2078 for (irow=0; irow < 1481; irow++) {
2079 if (fread (pixel, 1, 768, ifp) < 768) derror();
2080 box = irow / 82;
2081 row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
2082 switch (irow) {
2083 case 1477: case 1479: continue;
2084 case 1476: row = 984; break;
2085 case 1480: row = 985; break;
2086 case 1478: row = 985; box = 1;
2088 if ((box < 12) && (box & 1)) {
2089 for (col=0; col < 1533; col++, row ^= 1)
2090 if (col != 1) BAYER(row,col) = (col+1) & 2 ?
2091 pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
2092 BAYER(row,1) = pixel[1] << 1;
2093 BAYER(row,1533) = pixel[765] << 1;
2094 } else
2095 for (col=row & 1; col < 1534; col+=2)
2096 BAYER(row,col) = pixel[col/2] << 1;
2098 maximum = 0xff << 1;
2101 void CLASS casio_qv5700_load_raw()
2103 uchar data[3232], *dp;
2104 ushort pixel[2576], *pix;
2105 int row, col;
2107 for (row=0; row < height; row++) {
2108 fread (data, 1, 3232, ifp);
2109 for (dp=data, pix=pixel; dp < data+3220; dp+=5, pix+=4) {
2110 pix[0] = (dp[0] << 2) + (dp[1] >> 6);
2111 pix[1] = (dp[1] << 4) + (dp[2] >> 4);
2112 pix[2] = (dp[2] << 6) + (dp[3] >> 2);
2113 pix[3] = (dp[3] << 8) + (dp[4] );
2115 for (col=0; col < width; col++)
2116 BAYER(row,col) = (pixel[col] & 0x3ff);
2118 maximum = 0x3fc;
2121 void CLASS quicktake_100_load_raw()
2123 uchar pixel[484][644];
2124 static const short gstep[16] =
2125 { -89,-60,-44,-32,-22,-15,-8,-2,2,8,15,22,32,44,60,89 };
2126 static const short rstep[6][4] =
2127 { { -3,-1,1,3 }, { -5,-1,1,5 }, { -8,-2,2,8 },
2128 { -13,-3,3,13 }, { -19,-4,4,19 }, { -28,-6,6,28 } };
2129 static const short t_curve[256] =
2130 { 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,
2131 28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,53,
2132 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,
2133 79,80,81,82,83,84,86,88,90,92,94,97,99,101,103,105,107,110,112,114,116,
2134 118,120,123,125,127,129,131,134,136,138,140,142,144,147,149,151,153,155,
2135 158,160,162,164,166,168,171,173,175,177,179,181,184,186,188,190,192,195,
2136 197,199,201,203,205,208,210,212,214,216,218,221,223,226,230,235,239,244,
2137 248,252,257,261,265,270,274,278,283,287,291,296,300,305,309,313,318,322,
2138 326,331,335,339,344,348,352,357,361,365,370,374,379,383,387,392,396,400,
2139 405,409,413,418,422,426,431,435,440,444,448,453,457,461,466,470,474,479,
2140 483,487,492,496,500,508,519,531,542,553,564,575,587,598,609,620,631,643,
2141 654,665,676,687,698,710,721,732,743,754,766,777,788,799,810,822,833,844,
2142 855,866,878,889,900,911,922,933,945,956,967,978,989,1001,1012,1023 };
2143 int rb, row, col, sharp, val=0;
2145 getbits(-1);
2146 memset (pixel, 0x80, sizeof pixel);
2147 for (row=2; row < height+2; row++) {
2148 for (col=2+(row & 1); col < width+2; col+=2) {
2149 val = ((pixel[row-1][col-1] + 2*pixel[row-1][col+1] +
2150 pixel[row][col-2]) >> 2) + gstep[getbits(4)];
2151 pixel[row][col] = val = LIM(val,0,255);
2152 if (col < 4)
2153 pixel[row][col-2] = pixel[row+1][~row & 1] = val;
2154 if (row == 2)
2155 pixel[row-1][col+1] = pixel[row-1][col+3] = val;
2157 pixel[row][col] = val;
2159 for (rb=0; rb < 2; rb++)
2160 for (row=2+rb; row < height+2; row+=2)
2161 for (col=3-(row & 1); col < width+2; col+=2) {
2162 if (row < 4 || col < 4) sharp = 2;
2163 else {
2164 val = ABS(pixel[row-2][col] - pixel[row][col-2])
2165 + ABS(pixel[row-2][col] - pixel[row-2][col-2])
2166 + ABS(pixel[row][col-2] - pixel[row-2][col-2]);
2167 sharp = val < 4 ? 0 : val < 8 ? 1 : val < 16 ? 2 :
2168 val < 32 ? 3 : val < 48 ? 4 : 5;
2170 val = ((pixel[row-2][col] + pixel[row][col-2]) >> 1)
2171 + rstep[sharp][getbits(2)];
2172 pixel[row][col] = val = LIM(val,0,255);
2173 if (row < 4) pixel[row-2][col+2] = val;
2174 if (col < 4) pixel[row+2][col-2] = val;
2176 for (row=2; row < height+2; row++)
2177 for (col=3-(row & 1); col < width+2; col+=2) {
2178 val = ((pixel[row][col-1] + (pixel[row][col] << 2) +
2179 pixel[row][col+1]) >> 1) - 0x100;
2180 pixel[row][col] = LIM(val,0,255);
2182 for (row=0; row < height; row++)
2183 for (col=0; col < width; col++)
2184 BAYER(row,col) = t_curve[pixel[row+2][col+2]];
2185 maximum = 0x3ff;
2188 const int * CLASS make_decoder_int (const int *source, int level)
2190 struct decode *cur;
2192 cur = free_decode++;
2193 if (level < source[0]) {
2194 cur->branch[0] = free_decode;
2195 source = make_decoder_int (source, level+1);
2196 cur->branch[1] = free_decode;
2197 source = make_decoder_int (source, level+1);
2198 } else {
2199 cur->leaf = source[1];
2200 source += 2;
2202 return source;
2205 int CLASS radc_token (int tree)
2207 int t;
2208 #ifndef LIBRAW_NOTHREADS
2209 #define dstart tls->radc_token.dstart
2210 #define dindex tls->radc_token.dindex
2211 #define s tls->radc_token.s
2213 static const int source[] = {
2214 #else
2215 static struct decode *dstart[18], *dindex;
2216 static const int *s, source[] = {
2217 #endif
2218 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
2219 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
2220 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
2221 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
2222 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
2223 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
2224 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
2225 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
2226 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
2227 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
2228 1,0, 2,2, 2,-2,
2229 1,-3, 1,3,
2230 2,-17, 2,-5, 2,5, 2,17,
2231 2,-7, 2,2, 2,9, 2,18,
2232 2,-18, 2,-9, 2,-2, 2,7,
2233 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
2234 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
2235 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
2238 if (free_decode == first_decode)
2239 for (s=source, t=0; t < 18; t++) {
2240 dstart[t] = free_decode;
2241 s = make_decoder_int (s, 0);
2243 if (tree == 18) {
2244 if (kodak_cbpp == 243)
2245 return (getbits(6) << 2) + 2; /* most DC50 photos */
2246 else
2247 return (getbits(5) << 3) + 4; /* DC40, Fotoman Pixtura */
2249 for (dindex = dstart[tree]; dindex->branch[0]; )
2250 dindex = dindex->branch[getbits(1)];
2251 return dindex->leaf;
2253 #ifndef LIBRAW_NOTHREADS
2254 #undef dstart
2255 #undef dindex
2256 #undef s
2257 #endif
2260 #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
2262 #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
2263 : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
2265 void CLASS kodak_radc_load_raw()
2267 int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
2268 short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
2270 init_decoder();
2271 getbits(-1);
2272 for (i=0; i < sizeof(buf)/sizeof(short); i++)
2273 buf[0][0][i] = 2048;
2274 for (row=0; row < height; row+=4) {
2275 FORC3 mul[c] = getbits(6);
2276 FORC3 {
2277 val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
2278 s = val > 65564 ? 10:12;
2279 x = ~(-1 << (s-1));
2280 val <<= 12-s;
2281 for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
2282 buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
2283 last[c] = mul[c];
2284 for (r=0; r <= !c; r++) {
2285 buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
2286 for (tree=1, col=width/2; col > 0; ) {
2287 if ((tree = radc_token(tree))) {
2288 col -= 2;
2289 if (tree == 8)
2290 FORYX buf[c][y][x] = radc_token(tree+10) * mul[c];
2291 else
2292 FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
2293 } else
2294 do {
2295 nreps = (col > 2) ? radc_token(9) + 1 : 1;
2296 for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
2297 col -= 2;
2298 FORYX buf[c][y][x] = PREDICTOR;
2299 if (rep & 1) {
2300 step = radc_token(10) << 4;
2301 FORYX buf[c][y][x] += step;
2304 } while (nreps == 9);
2306 for (y=0; y < 2; y++)
2307 for (x=0; x < width/2; x++) {
2308 val = (buf[c][y+1][x] << 4) / mul[c];
2309 if (val < 0) val = 0;
2310 if (c) BAYER(row+y*2+c-1,x*2+2-c) = val;
2311 else BAYER(row+r*2+y,x*2+y) = val;
2313 memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
2316 for (y=row; y < row+4; y++)
2317 for (x=0; x < width; x++)
2318 if ((x+y) & 1) {
2319 r = x ? x-1 : x+1;
2320 s = x+1 < width ? x+1 : x-1;
2321 val = (BAYER(y,x)-2048)*2 + (BAYER(y,r)+BAYER(y,s))/2;
2322 if (val < 0) val = 0;
2323 BAYER(y,x) = val;
2326 maximum = 0xfff;
2327 use_gamma = 0;
2330 #undef FORYX
2331 #undef PREDICTOR
2333 #ifdef NO_JPEG
2334 void CLASS kodak_jpeg_load_raw() {}
2335 #else
2337 METHODDEF(boolean)
2338 fill_input_buffer (j_decompress_ptr cinfo)
2340 #ifndef LIBRAW_NOTHREADS
2341 #define jpeg_buffer tls->jpeg_buffer
2342 #else
2343 static uchar jpeg_buffer[4096];
2344 #endif
2345 size_t nbytes;
2347 nbytes = fread (jpeg_buffer, 1, 4096, ifp);
2348 swab (jpeg_buffer, jpeg_buffer, nbytes);
2349 cinfo->src->next_input_byte = jpeg_buffer;
2350 cinfo->src->bytes_in_buffer = nbytes;
2351 return TRUE;
2352 #ifndef LIBRAW_NOTHREADS
2353 #undef jpeg_buffer
2354 #endif
2357 void CLASS kodak_jpeg_load_raw()
2359 struct jpeg_decompress_struct cinfo;
2360 struct jpeg_error_mgr jerr;
2361 JSAMPARRAY buf;
2362 JSAMPLE (*pixel)[3];
2363 int row, col;
2365 cinfo.err = jpeg_std_error (&jerr);
2366 jpeg_create_decompress (&cinfo);
2367 jpeg_stdio_src (&cinfo, ifp);
2368 cinfo.src->fill_input_buffer = fill_input_buffer;
2369 jpeg_read_header (&cinfo, TRUE);
2370 jpeg_start_decompress (&cinfo);
2371 if ((cinfo.output_width != width ) ||
2372 (cinfo.output_height*2 != height ) ||
2373 (cinfo.output_components != 3 )) {
2374 #ifdef DCRAW_VERBOSE
2375 fprintf (stderr,_("%s: incorrect JPEG dimensions\n"), ifname);
2376 #endif
2377 jpeg_destroy_decompress (&cinfo);
2378 #ifdef LIBRAW_LIBRARY_BUILD
2379 throw LIBRAW_EXCEPTION_DECODE_JPEG;
2380 #else
2381 longjmp (failure, 3);
2382 #endif
2384 buf = (*cinfo.mem->alloc_sarray)
2385 ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
2387 while (cinfo.output_scanline < cinfo.output_height) {
2388 row = cinfo.output_scanline * 2;
2389 jpeg_read_scanlines (&cinfo, buf, 1);
2390 pixel = (JSAMPLE (*)[3]) buf[0];
2391 for (col=0; col < width; col+=2) {
2392 BAYER(row+0,col+0) = pixel[col+0][1] << 1;
2393 BAYER(row+1,col+1) = pixel[col+1][1] << 1;
2394 BAYER(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
2395 BAYER(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
2398 jpeg_finish_decompress (&cinfo);
2399 jpeg_destroy_decompress (&cinfo);
2400 maximum = 0xff << 1;
2402 #endif
2404 void CLASS kodak_dc120_load_raw()
2406 static const int mul[4] = { 162, 192, 187, 92 };
2407 static const int add[4] = { 0, 636, 424, 212 };
2408 uchar pixel[848];
2409 int row, shift, col;
2411 for (row=0; row < height; row++) {
2412 if (fread (pixel, 1, 848, ifp) < 848) derror();
2413 shift = row * mul[row & 3] + add[row & 3];
2414 for (col=0; col < width; col++)
2415 BAYER(row,col) = (ushort) pixel[(col + shift) % 848];
2417 maximum = 0xff;
2420 void CLASS eight_bit_load_raw()
2422 uchar *pixel;
2423 unsigned row, col, val, lblack=0;
2425 pixel = (uchar *) calloc (raw_width, sizeof *pixel);
2426 merror (pixel, "eight_bit_load_raw()");
2427 #ifndef LIBRAW_LIBRARY_BUILD
2428 fseek (ifp, top_margin*raw_width, SEEK_CUR);
2429 for (row=0; row < height; row++) {
2430 if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
2431 for (col=0; col < raw_width; col++) {
2432 val = curve[pixel[col]];
2433 if ((unsigned) (col-left_margin) < width)
2434 BAYER(row,col-left_margin) = val;
2435 else lblack += val;
2438 #else
2439 for (row=0; row < raw_height; row++) {
2440 if (fread (pixel, 1, raw_width, ifp) < raw_width) derror();
2441 for (col=0; col < raw_width; col++) {
2442 if(filtering_mode & LIBRAW_FILTERING_NORAWCURVE)
2444 val = pixel[col];
2445 if(val>maximum) maximum = val;
2447 else
2448 val = curve[pixel[col]];
2449 if((unsigned) (row-top_margin)< height)
2451 if ((unsigned) (col-left_margin) < width)
2452 BAYER(row,col-left_margin) = val;
2453 else
2455 ushort *dfp = get_masked_pointer(row,col);
2456 if(dfp) *dfp = val;
2457 lblack += val;
2460 else // top/bottom margins
2462 ushort *dfp = get_masked_pointer(row,col);
2463 if(dfp) *dfp = val;
2467 #endif
2469 free (pixel);
2470 if (raw_width > width+1)
2471 black = lblack / ((raw_width - width) * height);
2472 if (!strncmp(model,"DC2",3))
2473 black = 0;
2474 #ifdef LIBRAW_LIBRARY_BUILD
2475 if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
2476 #endif
2477 maximum = curve[0xff];
2480 void CLASS kodak_yrgb_load_raw()
2482 uchar *pixel;
2483 int row, col, y, cb, cr, rgb[3], c;
2485 pixel = (uchar *) calloc (raw_width, 3*sizeof *pixel);
2486 merror (pixel, "kodak_yrgb_load_raw()");
2487 for (row=0; row < height; row++) {
2488 if (~row & 1)
2489 if (fread (pixel, raw_width, 3, ifp) < 3) derror();
2490 for (col=0; col < raw_width; col++) {
2491 y = pixel[width*2*(row & 1) + col];
2492 cb = pixel[width + (col & -2)] - 128;
2493 cr = pixel[width + (col & -2)+1] - 128;
2494 rgb[1] = y-((cb + cr + 2) >> 2);
2495 rgb[2] = rgb[1] + cb;
2496 rgb[0] = rgb[1] + cr;
2497 FORC3 image[row*width+col][c] = LIM(rgb[c],0,255);
2500 free (pixel);
2501 use_gamma = 0;
2504 void CLASS kodak_262_load_raw()
2506 static const uchar kodak_tree[2][26] =
2507 { { 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 },
2508 { 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 } };
2509 struct decode *decode[2];
2510 uchar *pixel;
2511 int *strip, ns, i, row, col, chess, pi=0, pi1, pi2, pred, val;
2513 init_decoder();
2514 for (i=0; i < 2; i++) {
2515 decode[i] = free_decode;
2516 make_decoder (kodak_tree[i], 0);
2518 ns = (raw_height+63) >> 5;
2519 pixel = (uchar *) malloc (raw_width*32 + ns*4);
2520 merror (pixel, "kodak_262_load_raw()");
2521 strip = (int *) (pixel + raw_width*32);
2522 order = 0x4d4d;
2523 for (i=0; i < ns; i++)
2524 strip[i] = get4();
2525 for (row=0; row < raw_height; row++) {
2526 if ((row & 31) == 0) {
2527 fseek (ifp, strip[row >> 5], SEEK_SET);
2528 getbits(-1);
2529 pi = 0;
2531 for (col=0; col < raw_width; col++) {
2532 chess = (row + col) & 1;
2533 pi1 = chess ? pi-2 : pi-raw_width-1;
2534 pi2 = chess ? pi-2*raw_width : pi-raw_width+1;
2535 if (col <= chess) pi1 = -1;
2536 if (pi1 < 0) pi1 = pi2;
2537 if (pi2 < 0) pi2 = pi1;
2538 if (pi1 < 0 && col > 1) pi1 = pi2 = pi-2;
2539 pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
2540 pixel[pi] = val = pred + ljpeg_diff (decode[chess]);
2541 if (val >> 8) derror();
2542 #ifdef LIBRAW_LIBRARY_BUILD
2543 if(filtering_mode & LIBRAW_FILTERING_NORAWCURVE)
2544 val = pixel[pi++];
2545 else
2546 val = curve[pixel[pi++]];
2547 #else
2548 val = curve[pixel[pi++]];
2549 #endif
2550 if ((unsigned) (col-left_margin) < width)
2551 BAYER(row,col-left_margin) = val;
2552 else
2553 #ifndef LIBRAW_LIBRARY_BUILD
2554 black += val;
2555 #else
2557 ushort *dfp = get_masked_pointer(row,col);
2558 if(dfp) *dfp = val;
2559 black += val;
2561 #endif
2564 free (pixel);
2565 if (raw_width > width)
2566 black /= (raw_width - width) * height;
2569 int CLASS kodak_65000_decode (short *out, int bsize)
2571 uchar c, blen[768];
2572 ushort raw[6];
2573 INT64 bitbuf=0;
2574 int save, bits=0, i, j, len, diff;
2576 save = ftell(ifp);
2577 bsize = (bsize + 3) & -4;
2578 for (i=0; i < bsize; i+=2) {
2579 c = fgetc(ifp);
2580 if ((blen[i ] = c & 15) > 12 ||
2581 (blen[i+1] = c >> 4) > 12 ) {
2582 fseek (ifp, save, SEEK_SET);
2583 for (i=0; i < bsize; i+=8) {
2584 read_shorts (raw, 6);
2585 out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
2586 out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
2587 for (j=0; j < 6; j++)
2588 out[i+2+j] = raw[j] & 0xfff;
2590 return 1;
2593 if ((bsize & 7) == 4) {
2594 bitbuf = fgetc(ifp) << 8;
2595 bitbuf += fgetc(ifp);
2596 bits = 16;
2598 for (i=0; i < bsize; i++) {
2599 len = blen[i];
2600 if (bits < len) {
2601 for (j=0; j < 32; j+=8)
2602 bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
2603 bits += 32;
2605 diff = bitbuf & (0xffff >> (16-len));
2606 bitbuf >>= len;
2607 bits -= len;
2608 if ((diff & (1 << (len-1))) == 0)
2609 diff -= (1 << len) - 1;
2610 out[i] = diff;
2612 return 0;
2615 void CLASS kodak_65000_load_raw()
2617 short buf[256];
2618 int row, col, len, pred[2], ret, i;
2620 for (row=0; row < height; row++)
2621 for (col=0; col < width; col+=256) {
2622 pred[0] = pred[1] = 0;
2623 len = MIN (256, width-col);
2624 ret = kodak_65000_decode (buf, len);
2625 for (i=0; i < len; i++)
2626 #ifndef LIBRAW_LIBRARY_BUILD
2627 if ((BAYER(row,col+i) = curve[ret ? buf[i] :
2628 (pred[i & 1] += buf[i])]) >> 12) derror();
2629 #else
2631 ushort val = ret ? buf[i] : (pred[i & 1] += buf[i]);
2632 if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
2633 val = curve[val];
2634 BAYER(row,col+i)=val;
2635 if(curve[val]>>12) derror();
2637 #endif
2641 void CLASS kodak_ycbcr_load_raw()
2643 short buf[384], *bp;
2644 int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
2645 ushort *ip;
2647 for (row=0; row < height; row+=2)
2648 for (col=0; col < width; col+=128) {
2649 len = MIN (128, width-col);
2650 kodak_65000_decode (buf, len*3);
2651 y[0][1] = y[1][1] = cb = cr = 0;
2652 for (bp=buf, i=0; i < len; i+=2, bp+=2) {
2653 cb += bp[4];
2654 cr += bp[5];
2655 rgb[1] = -((cb + cr + 2) >> 2);
2656 rgb[2] = rgb[1] + cb;
2657 rgb[0] = rgb[1] + cr;
2658 for (j=0; j < 2; j++)
2659 for (k=0; k < 2; k++) {
2660 if ((y[j][k] = y[j][k^1] + *bp++) >> 10) derror();
2661 ip = image[(row+j)*width + col+i+k];
2662 #ifndef LIBRAW_LIBRARY_BUILD
2663 FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
2664 #else
2665 if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
2666 FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
2667 else
2668 FORC3 ip[c] = y[j][k]+rgb[c];;
2669 #endif
2675 void CLASS kodak_rgb_load_raw()
2677 short buf[768], *bp;
2678 int row, col, len, c, i, rgb[3];
2679 ushort *ip=image[0];
2681 for (row=0; row < height; row++)
2682 for (col=0; col < width; col+=256) {
2683 len = MIN (256, width-col);
2684 kodak_65000_decode (buf, len*3);
2685 memset (rgb, 0, sizeof rgb);
2686 for (bp=buf, i=0; i < len; i++, ip+=4)
2687 FORC3 if ((ip[c] = rgb[c] += *bp++) >> 12) derror();
2691 void CLASS kodak_thumb_load_raw()
2693 int row, col;
2694 colors = thumb_misc >> 5;
2695 for (row=0; row < height; row++)
2696 for (col=0; col < width; col++)
2697 read_shorts (image[row*width+col], colors);
2698 maximum = (1 << (thumb_misc & 31)) - 1;
2701 void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
2703 #ifndef LIBRAW_NOTHREADS
2704 #define pad tls->sony_decrypt.pad
2705 #define p tls->sony_decrypt.p
2706 #else
2707 static unsigned pad[128], p;
2708 #endif
2710 if (start) {
2711 for (p=0; p < 4; p++)
2712 pad[p] = key = key * 48828125 + 1;
2713 pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
2714 for (p=4; p < 127; p++)
2715 pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
2716 for (p=0; p < 127; p++)
2717 pad[p] = htonl(pad[p]);
2719 while (len--)
2720 *data++ ^= pad[p++ & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
2721 #ifndef LIBRAW_NOTHREADS
2722 #undef pad
2723 #undef p
2724 #endif
2727 void CLASS sony_load_raw()
2729 uchar head[40];
2730 ushort *pixel;
2731 unsigned i, key, row, col;
2733 fseek (ifp, 200896, SEEK_SET);
2734 fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
2735 order = 0x4d4d;
2736 key = get4();
2737 fseek (ifp, 164600, SEEK_SET);
2738 fread (head, 1, 40, ifp);
2739 sony_decrypt ((unsigned int *) head, 10, 1, key);
2740 for (i=26; i-- > 22; )
2741 key = key << 8 | head[i];
2742 fseek (ifp, data_offset, SEEK_SET);
2743 pixel = (ushort *) calloc (raw_width, sizeof *pixel);
2744 merror (pixel, "sony_load_raw()");
2745 for (row=0; row < height; row++) {
2746 if (fread (pixel, 2, raw_width, ifp) < raw_width) derror();
2747 sony_decrypt ((unsigned int *) pixel, raw_width/2, !row, key);
2748 #ifdef LIBRAW_LIBRARY_BUILD
2749 for (col=0; col < left_margin; col++)
2751 ushort *dfp = get_masked_pointer(row,col);
2752 if(dfp) *dfp = ntohs(pixel[col]);
2754 for (col=left_margin+width; col < raw_width; col++)
2756 ushort *dfp = get_masked_pointer(row,col);
2757 if(dfp) *dfp = ntohs(pixel[col]);
2759 #endif
2760 for (col=9; col < left_margin; col++)
2761 black += ntohs(pixel[col]);
2762 for (col=0; col < width; col++)
2763 if ((BAYER(row,col) = ntohs(pixel[col+left_margin])) >> 14)
2764 derror();
2766 free (pixel);
2767 if (left_margin > 9)
2768 black /= (left_margin-9) * height;
2769 maximum = 0x3ff0;
2772 void CLASS sony_arw_load_raw()
2774 int col, row, len, diff, sum=0;
2776 getbits(-1);
2777 for (col = raw_width; col--; )
2778 for (row=0; row < raw_height+1; row+=2) {
2779 if (row == raw_height) row = 1;
2780 len = 4 - getbits(2);
2781 if (len == 3 && getbits(1)) len = 0;
2782 if (len == 4)
2783 while (len < 17 && !getbits(1)) len++;
2784 diff = getbits(len);
2785 if ((diff & (1 << (len-1))) == 0)
2786 diff -= (1 << len) - 1;
2787 if ((sum += diff) >> 12) derror();
2788 if (row < height) BAYER(row,col) = sum;
2789 #ifdef LIBRAW_LIBRARY_BUILD
2790 else
2792 ushort *dfp = get_masked_pointer(row,col);
2793 if(dfp) *dfp = sum;
2795 #endif
2799 void CLASS sony_arw2_load_raw()
2801 uchar *data, *dp;
2802 ushort pix[16];
2803 int row, col, val, max, min, imax, imin, sh, bit, i;
2805 data = (uchar *) malloc (raw_width*tiff_bps >> 3);
2806 merror (data, "sony_arw2_load_raw()");
2807 for (row=0; row < height; row++) {
2808 fread (data, 1, raw_width*tiff_bps >> 3, ifp);
2809 if (tiff_bps == 8) {
2810 for (dp=data, col=0; col < width-30; dp+=16) {
2811 max = 0x7ff & (val = sget4(dp));
2812 min = 0x7ff & val >> 11;
2813 imax = 0x0f & val >> 22;
2814 imin = 0x0f & val >> 26;
2815 for (sh=0; sh < 4 && 0x80 << sh <= max-min; sh++);
2816 for (bit=30, i=0; i < 16; i++)
2817 if (i == imax) pix[i] = max;
2818 else if (i == imin) pix[i] = min;
2819 else {
2820 pix[i] = ((sget2(dp+(bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
2821 if (pix[i] > 0x7ff) pix[i] = 0x7ff;
2822 bit += 7;
2824 for (i=0; i < 16; i++, col+=2)
2825 #ifndef LIBRAW_LIBRARY_BUILD
2826 BAYER(row,col) = curve[pix[i] << 1] >> 1;
2827 #else
2829 ushort val = pix[i];
2830 if(!(filtering_mode & LIBRAW_FILTERING_NORAWCURVE))
2831 val = curve[val<<1]>>1;
2832 BAYER(row,col)=val;
2834 #endif
2835 col -= col & 1 ? 1:31;
2837 } else if (tiff_bps == 12)
2838 for (dp=data, col=0; col < width; dp+=3, col+=2) {
2839 BAYER(row,col) = ((dp[1] << 8 | dp[0]) & 0xfff) << 1;
2840 BAYER(row,col+1) = (dp[2] << 4 | dp[1] >> 4) << 1;
2843 free (data);
2846 #define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
2848 /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
2849 void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
2851 uchar hist[3][13] = {
2852 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2853 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2854 { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
2855 int low, high=0xff, carry=0, nbits=8;
2856 int s, count, bin, next, i, sym[3];
2857 uchar diff, pred[]={0,0};
2858 ushort data=0, range=0;
2859 unsigned pix, row, col;
2861 fseek (ifp, seg[0][1]+1, SEEK_SET);
2862 getbits(-1);
2863 for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
2864 for (s=0; s < 3; s++) {
2865 data = data << nbits | getbits(nbits);
2866 if (carry < 0)
2867 carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
2868 while (--nbits >= 0)
2869 if ((data >> nbits & 0xff) == 0xff) break;
2870 if (nbits > 0)
2871 data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
2872 ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
2873 if (nbits >= 0) {
2874 data += getbits(1);
2875 carry = nbits - 8;
2877 count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
2878 for (bin=0; hist[s][bin+5] > count; bin++);
2879 low = hist[s][bin+5] * (high >> 4) >> 2;
2880 if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
2881 high -= low;
2882 for (nbits=0; high << nbits < 128; nbits++);
2883 range = (range+low) << nbits;
2884 high <<= nbits;
2885 next = hist[s][1];
2886 if (++hist[s][2] > hist[s][3]) {
2887 next = (next+1) & hist[s][0];
2888 hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
2889 hist[s][2] = 1;
2891 if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
2892 if (bin < hist[s][1])
2893 for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
2894 else if (next <= bin)
2895 for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
2897 hist[s][1] = next;
2898 sym[s] = bin;
2900 diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
2901 if (sym[0] & 4)
2902 diff = diff ? -diff : 0x80;
2903 if (ftell(ifp) + 12 >= seg[1][1])
2904 diff = 0;
2905 pred[pix & 1] += diff;
2906 row = pix / raw_width - top_margin;
2907 col = pix % raw_width - left_margin;
2908 if (row < height && col < width)
2909 BAYER(row,col) = pred[pix & 1];
2910 #ifdef LIBRAW_LIBRARY_BUILD
2911 else
2913 ushort *dfp = get_masked_pointer(row+top_margin,col+left_margin);
2914 if(dfp) *dfp = pred[pix &1];
2916 #endif
2917 if (!(pix & 1) && HOLE(row)) pix += 2;
2919 maximum = 0xff;
2922 void CLASS smal_v6_load_raw()
2924 unsigned seg[2][2];
2926 fseek (ifp, 16, SEEK_SET);
2927 seg[0][0] = 0;
2928 seg[0][1] = get2();
2929 seg[1][0] = raw_width * raw_height;
2930 seg[1][1] = INT_MAX;
2931 smal_decode_segment (seg, 0);
2932 use_gamma = 0;
2935 int CLASS median4 (int *p)
2937 int min, max, sum, i;
2939 min = max = sum = p[0];
2940 for (i=1; i < 4; i++) {
2941 sum += p[i];
2942 if (min > p[i]) min = p[i];
2943 if (max < p[i]) max = p[i];
2945 return (sum - min - max) >> 1;
2948 void CLASS fill_holes (int holes)
2950 int row, col, val[4];
2952 for (row=2; row < height-2; row++) {
2953 if (!HOLE(row)) continue;
2954 for (col=1; col < width-1; col+=4) {
2955 val[0] = BAYER(row-1,col-1);
2956 val[1] = BAYER(row-1,col+1);
2957 val[2] = BAYER(row+1,col-1);
2958 val[3] = BAYER(row+1,col+1);
2959 BAYER(row,col) = median4(val);
2961 for (col=2; col < width-2; col+=4)
2962 if (HOLE(row-2) || HOLE(row+2))
2963 BAYER(row,col) = (BAYER(row,col-2) + BAYER(row,col+2)) >> 1;
2964 else {
2965 val[0] = BAYER(row,col-2);
2966 val[1] = BAYER(row,col+2);
2967 val[2] = BAYER(row-2,col);
2968 val[3] = BAYER(row+2,col);
2969 BAYER(row,col) = median4(val);
2974 void CLASS smal_v9_load_raw()
2976 unsigned seg[256][2], offset, nseg, holes, i;
2978 fseek (ifp, 67, SEEK_SET);
2979 offset = get4();
2980 nseg = fgetc(ifp);
2981 fseek (ifp, offset, SEEK_SET);
2982 for (i=0; i < nseg*2; i++)
2983 seg[0][i] = get4() + data_offset*(i & 1);
2984 fseek (ifp, 78, SEEK_SET);
2985 holes = fgetc(ifp);
2986 fseek (ifp, 88, SEEK_SET);
2987 seg[nseg][0] = raw_height * raw_width;
2988 seg[nseg][1] = get4() + data_offset;
2989 for (i=0; i < nseg; i++)
2990 smal_decode_segment (seg+i, holes);
2991 if (holes) fill_holes (holes);
2993 #line 4158 "dcraw/dcraw.c"
2995 void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
2997 double work[3][6], num;
2998 int i, j, k;
3000 for (i=0; i < 3; i++) {
3001 for (j=0; j < 6; j++)
3002 work[i][j] = j == i+3;
3003 for (j=0; j < 3; j++)
3004 for (k=0; k < size; k++)
3005 work[i][j] += in[k][i] * in[k][j];
3007 for (i=0; i < 3; i++) {
3008 num = work[i][i];
3009 for (j=0; j < 6; j++)
3010 work[i][j] /= num;
3011 for (k=0; k < 3; k++) {
3012 if (k==i) continue;
3013 num = work[k][i];
3014 for (j=0; j < 6; j++)
3015 work[k][j] -= work[i][j] * num;
3018 for (i=0; i < size; i++)
3019 for (j=0; j < 3; j++)
3020 for (out[i][j]=k=0; k < 3; k++)
3021 out[i][j] += work[j][k+3] * in[i][k];
3024 void CLASS cam_xyz_coeff (double cam_xyz[4][3])
3026 double cam_rgb[4][3], inverse[4][3], num;
3027 int i, j, k;
3029 for (i=0; i < colors; i++) /* Multiply out XYZ colorspace */
3030 for (j=0; j < 3; j++)
3031 for (cam_rgb[i][j] = k=0; k < 3; k++)
3032 cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
3034 for (i=0; i < colors; i++) { /* Normalize cam_rgb so that */
3035 for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
3036 num += cam_rgb[i][j];
3037 for (j=0; j < 3; j++)
3038 cam_rgb[i][j] /= num;
3039 pre_mul[i] = 1 / num;
3041 pseudoinverse (cam_rgb, inverse, colors);
3042 for (raw_color = i=0; i < 3; i++)
3043 for (j=0; j < colors; j++)
3044 rgb_cam[i][j] = inverse[j][i];
3045 #ifdef LIBRAW_LIBRARY_BUILD
3046 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
3047 color_flags.rgb_cam_state = LIBRAW_COLORSTATE_CONST;
3048 #endif
3051 #ifdef COLORCHECK
3052 void CLASS colorcheck()
3054 #define NSQ 24
3055 // Coordinates of the GretagMacbeth ColorChecker squares
3056 // width, height, 1st_column, 1st_row
3057 static const int cut[NSQ][4] = {
3058 { 241, 231, 234, 274 },
3059 { 251, 235, 534, 274 },
3060 { 255, 239, 838, 272 },
3061 { 255, 240, 1146, 274 },
3062 { 251, 237, 1452, 278 },
3063 { 243, 238, 1758, 288 },
3064 { 253, 253, 218, 558 },
3065 { 255, 249, 524, 562 },
3066 { 261, 253, 830, 562 },
3067 { 260, 255, 1144, 564 },
3068 { 261, 255, 1450, 566 },
3069 { 247, 247, 1764, 576 },
3070 { 255, 251, 212, 862 },
3071 { 259, 259, 518, 862 },
3072 { 263, 261, 826, 864 },
3073 { 265, 263, 1138, 866 },
3074 { 265, 257, 1450, 872 },
3075 { 257, 255, 1762, 874 },
3076 { 257, 253, 212, 1164 },
3077 { 262, 251, 516, 1172 },
3078 { 263, 257, 826, 1172 },
3079 { 263, 255, 1136, 1176 },
3080 { 255, 252, 1452, 1182 },
3081 { 257, 253, 1760, 1180 } };
3082 // ColorChecker Chart under 6500-kelvin illumination
3083 static const double gmb_xyY[NSQ][3] = {
3084 { 0.400, 0.350, 10.1 }, // Dark Skin
3085 { 0.377, 0.345, 35.8 }, // Light Skin
3086 { 0.247, 0.251, 19.3 }, // Blue Sky
3087 { 0.337, 0.422, 13.3 }, // Foliage
3088 { 0.265, 0.240, 24.3 }, // Blue Flower
3089 { 0.261, 0.343, 43.1 }, // Bluish Green
3090 { 0.506, 0.407, 30.1 }, // Orange
3091 { 0.211, 0.175, 12.0 }, // Purplish Blue
3092 { 0.453, 0.306, 19.8 }, // Moderate Red
3093 { 0.285, 0.202, 6.6 }, // Purple
3094 { 0.380, 0.489, 44.3 }, // Yellow Green
3095 { 0.473, 0.438, 43.1 }, // Orange Yellow
3096 { 0.187, 0.129, 6.1 }, // Blue
3097 { 0.305, 0.478, 23.4 }, // Green
3098 { 0.539, 0.313, 12.0 }, // Red
3099 { 0.448, 0.470, 59.1 }, // Yellow
3100 { 0.364, 0.233, 19.8 }, // Magenta
3101 { 0.196, 0.252, 19.8 }, // Cyan
3102 { 0.310, 0.316, 90.0 }, // White
3103 { 0.310, 0.316, 59.1 }, // Neutral 8
3104 { 0.310, 0.316, 36.2 }, // Neutral 6.5
3105 { 0.310, 0.316, 19.8 }, // Neutral 5
3106 { 0.310, 0.316, 9.0 }, // Neutral 3.5
3107 { 0.310, 0.316, 3.1 } }; // Black
3108 double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
3109 double inverse[NSQ][3], cam_xyz[4][3], num;
3110 int c, i, j, k, sq, row, col, count[4];
3112 memset (gmb_cam, 0, sizeof gmb_cam);
3113 for (sq=0; sq < NSQ; sq++) {
3114 FORCC count[c] = 0;
3115 for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
3116 for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
3117 c = FC(row,col);
3118 if (c >= colors) c -= 2;
3119 gmb_cam[sq][c] += BAYER(row,col);
3120 count[c]++;
3122 FORCC gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - black;
3123 gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
3124 gmb_xyz[sq][1] = gmb_xyY[sq][2];
3125 gmb_xyz[sq][2] = gmb_xyY[sq][2] *
3126 (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
3128 pseudoinverse (gmb_xyz, inverse, NSQ);
3129 for (i=0; i < colors; i++)
3130 for (j=0; j < 3; j++)
3131 for (cam_xyz[i][j] = k=0; k < NSQ; k++)
3132 cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
3133 cam_xyz_coeff (cam_xyz);
3134 #ifdef DCRAW_VERBOSE
3135 if (verbose) {
3136 printf (" { \"%s %s\", %d,\n\t{", make, model, black);
3137 num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
3138 FORCC for (j=0; j < 3; j++)
3139 printf ("%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
3140 puts (" } },");
3142 #endif
3143 #undef NSQ
3145 #endif
3147 void CLASS hat_transform (float *temp, float *base, int st, int size, int sc)
3149 int i;
3150 for (i=0; i < sc; i++)
3151 temp[i] = 2*base[st*i] + base[st*(sc-i)] + base[st*(i+sc)];
3152 for (; i+sc < size; i++)
3153 temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(i+sc)];
3154 for (; i < size; i++)
3155 temp[i] = 2*base[st*i] + base[st*(i-sc)] + base[st*(2*size-2-(i+sc))];
3158 #ifndef _OPENMP
3159 void CLASS wavelet_denoise()
3161 float *fimg=0, *temp, thold, mul[2], avg, diff;
3162 int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast;
3163 ushort *window[4];
3164 static const float noise[] =
3165 { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
3167 #ifdef DCRAW_VERBOSE
3168 if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
3169 #endif
3171 while (maximum << scale < 0x10000) scale++;
3172 maximum <<= --scale;
3173 black <<= scale;
3174 if ((size = iheight*iwidth) < 0x15550000)
3175 fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
3176 merror (fimg, "wavelet_denoise()");
3177 temp = fimg + size*3;
3178 if ((nc = colors) == 3 && filters) nc++;
3179 FORC(nc) { /* denoise R,G1,B,G3 individually */
3180 for (i=0; i < size; i++)
3181 fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
3182 for (hpass=lev=0; lev < 5; lev++) {
3183 lpass = size*((lev & 1)+1);
3184 for (row=0; row < iheight; row++) {
3185 hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
3186 for (col=0; col < iwidth; col++)
3187 fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
3189 for (col=0; col < iwidth; col++) {
3190 hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
3191 for (row=0; row < iheight; row++)
3192 fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
3194 thold = threshold * noise[lev];
3195 for (i=0; i < size; i++) {
3196 fimg[hpass+i] -= fimg[lpass+i];
3197 if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
3198 else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
3199 else fimg[hpass+i] = 0;
3200 if (hpass) fimg[i] += fimg[hpass+i];
3202 hpass = lpass;
3204 for (i=0; i < size; i++)
3205 image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
3207 if (filters && colors == 3) { /* pull G1 and G3 closer together */
3208 for (row=0; row < 2; row++)
3209 mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
3210 for (i=0; i < 4; i++)
3211 window[i] = (ushort *) fimg + width*i;
3212 for (wlast=-1, row=1; row < height-1; row++) {
3213 while (wlast < row+1) {
3214 for (wlast++, i=0; i < 4; i++)
3215 window[(i+3) & 3] = window[i];
3216 for (col = FC(wlast,1) & 1; col < width; col+=2)
3217 window[2][col] = BAYER(wlast,col);
3219 thold = threshold/512;
3220 for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
3221 avg = ( window[0][col-1] + window[0][col+1] +
3222 window[2][col-1] + window[2][col+1] - black*4 )
3223 * mul[row & 1] + (window[1][col] - black) * 0.5 + black;
3224 avg = avg < 0 ? 0 : sqrt(avg);
3225 diff = sqrt((double)(BAYER(row,col))) - avg;
3226 if (diff < -thold) diff += thold;
3227 else if (diff > thold) diff -= thold;
3228 else diff = 0;
3229 BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
3233 free (fimg);
3235 #else
3236 void CLASS wavelet_denoise()
3238 float *fimg=0, *temp, thold, mul[2], avg, diff;
3239 int scale=1, size, lev, hpass, lpass, row, col, nc, c, i, wlast;
3240 ushort *window[4];
3241 static const float noise[] =
3242 { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
3244 #ifdef DCRAW_VERBOSE
3245 if (verbose) fprintf (stderr,_("Wavelet denoising...\n"));
3246 #endif
3248 while (maximum << scale < 0x10000) scale++;
3249 maximum <<= --scale;
3250 black <<= scale;
3251 if ((size = iheight*iwidth) < 0x15550000)
3252 fimg = (float *) malloc ((size*3 + iheight + iwidth) * sizeof *fimg);
3253 merror (fimg, "wavelet_denoise()");
3254 temp = fimg + size*3;
3255 if ((nc = colors) == 3 && filters) nc++;
3256 #ifdef LIBRAW_LIBRARY_BUILD
3257 #pragma omp parallel default(shared) private(i,col,row,thold,lev,lpass,hpass,temp) firstprivate(c,scale,size) */
3258 #endif
3260 temp = (float*)malloc( (iheight + iwidth) * sizeof *fimg);
3261 FORC(nc) { /* denoise R,G1,B,G3 individually */
3262 #ifdef LIBRAW_LIBRARY_BUILD
3263 #pragma omp for
3264 #endif
3265 for (i=0; i < size; i++)
3266 fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
3267 for (hpass=lev=0; lev < 5; lev++) {
3268 lpass = size*((lev & 1)+1);
3269 #ifdef LIBRAW_LIBRARY_BUILD
3270 #pragma omp for
3271 #endif
3272 for (row=0; row < iheight; row++) {
3273 hat_transform (temp, fimg+hpass+row*iwidth, 1, iwidth, 1 << lev);
3274 for (col=0; col < iwidth; col++)
3275 fimg[lpass + row*iwidth + col] = temp[col] * 0.25;
3277 #ifdef LIBRAW_LIBRARY_BUILD
3278 #pragma omp for
3279 #endif
3280 for (col=0; col < iwidth; col++) {
3281 hat_transform (temp, fimg+lpass+col, iwidth, iheight, 1 << lev);
3282 for (row=0; row < iheight; row++)
3283 fimg[lpass + row*iwidth + col] = temp[row] * 0.25;
3285 thold = threshold * noise[lev];
3286 #ifdef LIBRAW_LIBRARY_BUILD
3287 #pragma omp for
3288 #endif
3289 for (i=0; i < size; i++) {
3290 fimg[hpass+i] -= fimg[lpass+i];
3291 if (fimg[hpass+i] < -thold) fimg[hpass+i] += thold;
3292 else if (fimg[hpass+i] > thold) fimg[hpass+i] -= thold;
3293 else fimg[hpass+i] = 0;
3294 if (hpass) fimg[i] += fimg[hpass+i];
3296 hpass = lpass;
3298 #ifdef LIBRAW_LIBRARY_BUILD
3299 #pragma omp for
3300 #endif
3301 for (i=0; i < size; i++)
3302 image[i][c] = CLIP(SQR(fimg[i]+fimg[lpass+i])/0x10000);
3304 free(temp);
3305 } /* end omp parallel */
3306 /* the following loops are hard to parallize, no idea yes,
3307 * problem is wlast which is carrying dependency
3308 * second part should be easyer, but did not yet get it right.
3310 if (filters && colors == 3) { /* pull G1 and G3 closer together */
3311 for (row=0; row < 2; row++)
3312 mul[row] = 0.125 * pre_mul[FC(row+1,0) | 1] / pre_mul[FC(row,0) | 1];
3313 for (i=0; i < 4; i++)
3314 window[i] = (ushort *) fimg + width*i;
3315 for (wlast=-1, row=1; row < height-1; row++) {
3316 while (wlast < row+1) {
3317 for (wlast++, i=0; i < 4; i++)
3318 window[(i+3) & 3] = window[i];
3319 for (col = FC(wlast,1) & 1; col < width; col+=2)
3320 window[2][col] = BAYER(wlast,col);
3322 thold = threshold/512;
3323 for (col = (FC(row,0) & 1)+1; col < width-1; col+=2) {
3324 avg = ( window[0][col-1] + window[0][col+1] +
3325 window[2][col-1] + window[2][col+1] - black*4 )
3326 * mul[row & 1] + (window[1][col] - black) * 0.5 + black;
3327 avg = avg < 0 ? 0 : sqrt(avg);
3328 diff = sqrt(BAYER(row,col)) - avg;
3329 if (diff < -thold) diff += thold;
3330 else if (diff > thold) diff -= thold;
3331 else diff = 0;
3332 BAYER(row,col) = CLIP(SQR(avg+diff) + 0.5);
3336 free (fimg);
3339 #endif
3341 void CLASS scale_colors()
3343 unsigned bottom, right, size, row, col, ur, uc, i, x, y, c, sum[8];
3344 int val, dark, sat;
3345 double dsum[8], dmin, dmax;
3346 float scale_mul[4], fr, fc;
3347 ushort *img=0, *pix;
3349 #ifdef LIBRAW_LIBRARY_BUILD
3350 RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,0,2);
3351 #endif
3353 if (user_mul[0])
3354 memcpy (pre_mul, user_mul, sizeof pre_mul);
3355 if (use_auto_wb || (use_camera_wb && cam_mul[0] == -1)) {
3356 memset (dsum, 0, sizeof dsum);
3357 bottom = MIN (greybox[1]+greybox[3], height);
3358 right = MIN (greybox[0]+greybox[2], width);
3359 for (row=greybox[1]; row < bottom; row += 8)
3360 for (col=greybox[0]; col < right; col += 8) {
3361 memset (sum, 0, sizeof sum);
3362 for (y=row; y < row+8 && y < bottom; y++)
3363 for (x=col; x < col+8 && x < right; x++)
3364 FORC4 {
3365 if (filters) {
3366 c = FC(y,x);
3367 val = BAYER(y,x);
3368 } else
3369 val = image[y*width+x][c];
3370 if (val > maximum-25) goto skip_block;
3371 if ((val -= black) < 0) val = 0;
3372 sum[c] += val;
3373 sum[c+4]++;
3374 if (filters) break;
3376 FORC(8) dsum[c] += sum[c];
3377 skip_block: ;
3379 FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
3380 #ifdef LIBRAW_LIBRARY_BUILD
3381 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CALCULATED;
3382 #endif
3384 if (use_camera_wb && cam_mul[0] != -1) {
3385 memset (sum, 0, sizeof sum);
3386 for (row=0; row < 8; row++)
3387 for (col=0; col < 8; col++) {
3388 c = FC(row,col);
3389 if ((val = white[row][col] - black) > 0)
3390 sum[c] += val;
3391 sum[c+4]++;
3393 if (sum[0] && sum[1] && sum[2] && sum[3])
3395 FORC4 pre_mul[c] = (float) sum[c+4] / sum[c];
3396 #ifdef LIBRAW_LIBRARY_BUILD
3397 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CALCULATED;
3398 #endif
3400 else if (cam_mul[0] && cam_mul[2])
3402 memcpy (pre_mul, cam_mul, sizeof pre_mul);
3403 #ifdef LIBRAW_LIBRARY_BUILD
3404 color_flags.pre_mul_state =color_flags.pre_mul_state;
3405 #endif
3407 else
3409 #ifdef LIBRAW_LIBRARY_BUILD
3410 imgdata.process_warnings |= LIBRAW_WARN_BAD_CAMERA_WB;
3411 #endif
3412 #ifdef DCRAW_VERBOSE
3413 fprintf (stderr,_("%s: Cannot use camera white balance.\n"), ifname);
3414 #endif
3417 if (pre_mul[3] == 0) pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
3418 dark = black;
3419 sat = maximum;
3420 if (threshold) wavelet_denoise();
3421 maximum -= black;
3422 for (dmin=DBL_MAX, dmax=c=0; c < 4; c++) {
3423 if (dmin > pre_mul[c])
3424 dmin = pre_mul[c];
3425 if (dmax < pre_mul[c])
3426 dmax = pre_mul[c];
3428 if (!highlight) dmax = dmin;
3429 FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
3430 #ifdef DCRAW_VERBOSE
3431 if (verbose) {
3432 fprintf (stderr,
3433 _("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark, sat);
3434 FORC4 fprintf (stderr, " %f", pre_mul[c]);
3435 fputc ('\n', stderr);
3437 #endif
3438 size = iheight*iwidth;
3439 for (i=0; i < size*4; i++) {
3440 val = image[0][i];
3441 if (!val) continue;
3442 val -= black;
3443 val *= scale_mul[i & 3];
3444 image[0][i] = CLIP(val);
3446 if ((aber[0] != 1 || aber[2] != 1) && colors == 3) {
3447 #ifdef DCRAW_VERBOSE
3448 if (verbose)
3449 fprintf (stderr,_("Correcting chromatic aberration...\n"));
3450 #endif
3451 for (c=0; c < 4; c+=2) {
3452 if (aber[c] == 1) continue;
3453 img = (ushort *) malloc (size * sizeof *img);
3454 merror (img, "scale_colors()");
3455 for (i=0; i < size; i++)
3456 img[i] = image[i][c];
3457 for (row=0; row < iheight; row++) {
3458 ur = fr = (row - iheight*0.5) * aber[c] + iheight*0.5;
3459 if (ur > iheight-2) continue;
3460 fr -= ur;
3461 for (col=0; col < iwidth; col++) {
3462 uc = fc = (col - iwidth*0.5) * aber[c] + iwidth*0.5;
3463 if (uc > iwidth-2) continue;
3464 fc -= uc;
3465 pix = img + ur*iwidth + uc;
3466 image[row*iwidth+col][c] =
3467 (pix[ 0]*(1-fc) + pix[ 1]*fc) * (1-fr) +
3468 (pix[iwidth]*(1-fc) + pix[iwidth+1]*fc) * fr;
3471 free(img);
3474 #ifdef LIBRAW_LIBRARY_BUILD
3475 RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS,1,2);
3476 #endif
3479 void CLASS pre_interpolate()
3481 ushort (*img)[4];
3482 int row, col, c;
3484 #ifdef LIBRAW_LIBRARY_BUILD
3485 RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,0,2);
3486 #endif
3487 if (shrink) {
3488 if (half_size) {
3489 height = iheight;
3490 width = iwidth;
3491 } else {
3492 img = (ushort (*)[4]) calloc (height*width, sizeof *img);
3493 merror (img, "pre_interpolate()");
3494 for (row=0; row < height; row++)
3495 for (col=0; col < width; col++) {
3496 c = fc(row,col);
3497 img[row*width+col][c] = image[(row >> 1)*iwidth+(col >> 1)][c];
3499 free (image);
3500 image = img;
3501 shrink = 0;
3504 if (filters && colors == 3) {
3505 if ((mix_green = four_color_rgb)) colors++;
3506 else {
3507 for (row = FC(1,0) >> 1; row < height; row+=2)
3508 for (col = FC(row,1) & 1; col < width; col+=2)
3509 image[row*width+col][1] = image[row*width+col][3];
3510 filters &= ~((filters & 0x55555555) << 1);
3513 if (half_size) filters = 0;
3514 #ifdef LIBRAW_LIBRARY_BUILD
3515 RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE,1,2);
3516 #endif
3519 void CLASS border_interpolate (int border)
3521 unsigned row, col, y, x, f, c, sum[8];
3523 for (row=0; row < height; row++)
3524 for (col=0; col < width; col++) {
3525 if (col==border && row >= border && row < height-border)
3526 col = width-border;
3527 memset (sum, 0, sizeof sum);
3528 for (y=row-1; y != row+2; y++)
3529 for (x=col-1; x != col+2; x++)
3530 if (y < height && x < width) {
3531 f = fc(y,x);
3532 sum[f] += image[y*width+x][f];
3533 sum[f+4]++;
3535 f = fc(row,col);
3536 FORCC if (c != f && sum[c+4])
3537 image[row*width+col][c] = sum[c] / sum[c+4];
3541 void CLASS lin_interpolate()
3543 int code[16][16][32], *ip, sum[4];
3544 int c, i, x, y, row, col, shift, color;
3545 ushort *pix;
3547 #ifdef DCRAW_VERBOSE
3548 if (verbose) fprintf (stderr,_("Bilinear interpolation...\n"));
3549 #endif
3551 #ifdef LIBRAW_LIBRARY_BUILD
3552 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
3553 #endif
3554 border_interpolate(1);
3555 for (row=0; row < 16; row++)
3556 for (col=0; col < 16; col++) {
3557 ip = code[row][col];
3558 memset (sum, 0, sizeof sum);
3559 for (y=-1; y <= 1; y++)
3560 for (x=-1; x <= 1; x++) {
3561 shift = (y==0) + (x==0);
3562 if (shift == 2) continue;
3563 color = fc(row+y,col+x);
3564 *ip++ = (width*y + x)*4 + color;
3565 *ip++ = shift;
3566 *ip++ = color;
3567 sum[color] += 1 << shift;
3569 FORCC
3570 if (c != fc(row,col)) {
3571 *ip++ = c;
3572 *ip++ = 256 / sum[c];
3575 #ifdef LIBRAW_LIBRARY_BUILD
3576 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
3577 #endif
3578 for (row=1; row < height-1; row++)
3579 for (col=1; col < width-1; col++) {
3580 pix = image[row*width+col];
3581 ip = code[row & 15][col & 15];
3582 memset (sum, 0, sizeof sum);
3583 for (i=8; i--; ip+=3)
3584 sum[ip[2]] += pix[ip[0]] << ip[1];
3585 for (i=colors; --i; ip+=2)
3586 pix[ip[0]] = sum[ip[0]] * ip[1] >> 8;
3588 #ifdef LIBRAW_LIBRARY_BUILD
3589 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
3590 #endif
3594 This algorithm is officially called:
3596 "Interpolation using a Threshold-based variable number of gradients"
3598 described in http://scien.stanford.edu/class/psych221/projects/99/tingchen/algodep/vargra.html
3600 I've extended the basic idea to work with non-Bayer filter arrays.
3601 Gradients are numbered clockwise from NW=0 to W=7.
3603 void CLASS vng_interpolate()
3605 static const signed char *cp, terms[] = {
3606 -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
3607 -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
3608 -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
3609 -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
3610 -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
3611 -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
3612 -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
3613 -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
3614 -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
3615 -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
3616 -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
3617 -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
3618 -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
3619 +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
3620 +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
3621 +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
3622 +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
3623 +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
3624 +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
3625 +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
3626 +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
3627 +1,+0,+2,+1,0,0x10
3628 }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
3629 ushort (*brow[5])[4], *pix;
3630 int prow=7, pcol=1, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
3631 int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
3632 int g, diff, thold, num, c;
3633 lin_interpolate();
3634 #ifdef DCRAW_VERBOSE
3635 if (verbose) fprintf (stderr,_("VNG interpolation...\n"));
3636 #endif
3638 if (filters == 1) prow = pcol = 15;
3639 ip = (int *) calloc ((prow+1)*(pcol+1), 1280);
3640 merror (ip, "vng_interpolate()");
3641 for (row=0; row <= prow; row++) /* Precalculate for VNG */
3642 for (col=0; col <= pcol; col++) {
3643 code[row][col] = ip;
3644 for (cp=terms, t=0; t < 64; t++) {
3645 y1 = *cp++; x1 = *cp++;
3646 y2 = *cp++; x2 = *cp++;
3647 weight = *cp++;
3648 grads = *cp++;
3649 color = fc(row+y1,col+x1);
3650 if (fc(row+y2,col+x2) != color) continue;
3651 diag = (fc(row,col+1) == color && fc(row+1,col) == color) ? 2:1;
3652 if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
3653 *ip++ = (y1*width + x1)*4 + color;
3654 *ip++ = (y2*width + x2)*4 + color;
3655 *ip++ = weight;
3656 for (g=0; g < 8; g++)
3657 if (grads & 1<<g) *ip++ = g;
3658 *ip++ = -1;
3660 *ip++ = INT_MAX;
3661 for (cp=chood, g=0; g < 8; g++) {
3662 y = *cp++; x = *cp++;
3663 *ip++ = (y*width + x) * 4;
3664 color = fc(row,col);
3665 if (fc(row+y,col+x) != color && fc(row+y*2,col+x*2) == color)
3666 *ip++ = (y*width + x) * 8 + color;
3667 else
3668 *ip++ = 0;
3671 brow[4] = (ushort (*)[4]) calloc (width*3, sizeof **brow);
3672 merror (brow[4], "vng_interpolate()");
3673 for (row=0; row < 3; row++)
3674 brow[row] = brow[4] + row*width;
3675 for (row=2; row < height-2; row++) { /* Do VNG interpolation */
3676 #ifdef LIBRAW_LIBRARY_BUILD
3677 if(!((row-2)%256))RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,(row-2)/256+1,((height-3)/256)+1);
3678 #endif
3679 for (col=2; col < width-2; col++) {
3680 pix = image[row*width+col];
3681 ip = code[row & prow][col & pcol];
3682 memset (gval, 0, sizeof gval);
3683 while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
3684 diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
3685 gval[ip[3]] += diff;
3686 ip += 5;
3687 if ((g = ip[-1]) == -1) continue;
3688 gval[g] += diff;
3689 while ((g = *ip++) != -1)
3690 gval[g] += diff;
3692 ip++;
3693 gmin = gmax = gval[0]; /* Choose a threshold */
3694 for (g=1; g < 8; g++) {
3695 if (gmin > gval[g]) gmin = gval[g];
3696 if (gmax < gval[g]) gmax = gval[g];
3698 if (gmax == 0) {
3699 memcpy (brow[2][col], pix, sizeof *image);
3700 continue;
3702 thold = gmin + (gmax >> 1);
3703 memset (sum, 0, sizeof sum);
3704 color = fc(row,col);
3705 for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
3706 if (gval[g] <= thold) {
3707 FORCC
3708 if (c == color && ip[1])
3709 sum[c] += (pix[c] + pix[ip[1]]) >> 1;
3710 else
3711 sum[c] += pix[ip[0] + c];
3712 num++;
3715 FORCC { /* Save to buffer */
3716 t = pix[color];
3717 if (c != color)
3718 t += (sum[c] - sum[color]) / num;
3719 brow[2][col][c] = CLIP(t);
3722 if (row > 3) /* Write buffer to image */
3723 memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
3724 for (g=0; g < 4; g++)
3725 brow[(g-1) & 3] = brow[g];
3727 memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
3728 memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image);
3729 free (brow[4]);
3730 free (code[0][0]);
3734 Patterned Pixel Grouping Interpolation by Alain Desbiolles
3736 void CLASS ppg_interpolate()
3738 int dir[5] = { 1, width, -1, -width, 1 };
3739 int row, col, diff[2], guess[2], c, d, i;
3740 ushort (*pix)[4];
3742 border_interpolate(3);
3743 #ifdef DCRAW_VERBOSE
3744 if (verbose) fprintf (stderr,_("PPG interpolation...\n"));
3745 #endif
3747 /* Fill in the green layer with gradients and pattern recognition: */
3748 #ifdef LIBRAW_LIBRARY_BUILD
3749 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,0,3);
3750 #endif
3751 for (row=3; row < height-3; row++)
3752 for (col=3+(FC(row,3) & 1), c=FC(row,col); col < width-3; col+=2) {
3753 pix = image + row*width+col;
3754 for (i=0; (d=dir[i]) > 0; i++) {
3755 guess[i] = (pix[-d][1] + pix[0][c] + pix[d][1]) * 2
3756 - pix[-2*d][c] - pix[2*d][c];
3757 diff[i] = ( ABS(pix[-2*d][c] - pix[ 0][c]) +
3758 ABS(pix[ 2*d][c] - pix[ 0][c]) +
3759 ABS(pix[ -d][1] - pix[ d][1]) ) * 3 +
3760 ( ABS(pix[ 3*d][1] - pix[ d][1]) +
3761 ABS(pix[-3*d][1] - pix[-d][1]) ) * 2;
3763 d = dir[i = diff[0] > diff[1]];
3764 pix[0][1] = ULIM(guess[i] >> 2, pix[d][1], pix[-d][1]);
3766 /* Calculate red and blue for each green pixel: */
3767 #ifdef LIBRAW_LIBRARY_BUILD
3768 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,1,3);
3769 #endif
3770 for (row=1; row < height-1; row++)
3771 for (col=1+(FC(row,2) & 1), c=FC(row,col+1); col < width-1; col+=2) {
3772 pix = image + row*width+col;
3773 for (i=0; (d=dir[i]) > 0; c=2-c, i++)
3774 pix[0][c] = CLIP((pix[-d][c] + pix[d][c] + 2*pix[0][1]
3775 - pix[-d][1] - pix[d][1]) >> 1);
3777 /* Calculate blue for red pixels and vice versa: */
3778 #ifdef LIBRAW_LIBRARY_BUILD
3779 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,2,3);
3780 #endif
3781 for (row=1; row < height-1; row++)
3782 for (col=1+(FC(row,1) & 1), c=2-FC(row,col); col < width-1; col+=2) {
3783 pix = image + row*width+col;
3784 for (i=0; (d=dir[i]+dir[i+1]) > 0; i++) {
3785 diff[i] = ABS(pix[-d][c] - pix[d][c]) +
3786 ABS(pix[-d][1] - pix[0][1]) +
3787 ABS(pix[ d][1] - pix[0][1]);
3788 guess[i] = pix[-d][c] + pix[d][c] + 2*pix[0][1]
3789 - pix[-d][1] - pix[d][1];
3791 if (diff[0] != diff[1])
3792 pix[0][c] = CLIP(guess[diff[0] > diff[1]] >> 1);
3793 else
3794 pix[0][c] = CLIP((guess[0]+guess[1]) >> 2);
3799 Adaptive Homogeneity-Directed interpolation is based on
3800 the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
3802 #define TS 256 /* Tile Size */
3803 #ifndef _OPENMP
3804 void CLASS ahd_interpolate()
3806 int i, j, k, top, left, row, col, tr, tc, c, d, val, hm[2];
3807 ushort (*pix)[4], (*rix)[3];
3808 static const int dir[4] = { -1, 1, -TS, TS };
3809 unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
3810 float r, cbrt[0x10000], xyz[3], xyz_cam[3][4];
3811 ushort (*rgb)[TS][TS][3];
3812 short (*lab)[TS][TS][3], (*lix)[3];
3813 char (*homo)[TS][TS], *buffer;
3815 #ifdef DCRAW_VERBOSE
3816 if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
3817 #endif
3819 for (i=0; i < 0x10000; i++) {
3820 r = i / 65535.0;
3821 cbrt[i] = r > 0.008856 ? pow((double)r,1/3.0) : 7.787*r + 16/116.0;
3823 for (i=0; i < 3; i++)
3824 for (j=0; j < colors; j++)
3825 for (xyz_cam[i][j] = k=0; k < 3; k++)
3826 xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
3828 border_interpolate(5);
3829 buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
3830 merror (buffer, "ahd_interpolate()");
3831 rgb = (ushort(*)[TS][TS][3]) buffer;
3832 lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
3833 homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
3835 for (top=2; top < height-5; top += TS-6)
3838 #ifdef LIBRAW_LIBRARY_BUILD
3839 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,(top-2)/(TS-6),(height-7)/(TS-6)+1);
3840 #endif
3841 for (left=2; left < width-5; left += TS-6) {
3843 /* Interpolate green horizontally and vertically: */
3844 for (row = top; row < top+TS && row < height-2; row++) {
3845 col = left + (FC(row,left) & 1);
3846 for (c = FC(row,col); col < left+TS && col < width-2; col+=2) {
3847 pix = image + row*width+col;
3848 val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
3849 - pix[-2][c] - pix[2][c]) >> 2;
3850 rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
3851 val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
3852 - pix[-2*width][c] - pix[2*width][c]) >> 2;
3853 rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
3856 /* Interpolate red and blue, and convert to CIELab: */
3857 for (d=0; d < 2; d++)
3858 for (row=top+1; row < top+TS-1 && row < height-3; row++)
3859 for (col=left+1; col < left+TS-1 && col < width-3; col++) {
3860 pix = image + row*width+col;
3861 rix = &rgb[d][row-top][col-left];
3862 lix = &lab[d][row-top][col-left];
3863 if ((c = 2 - FC(row,col)) == 1) {
3864 c = FC(row+1,col);
3865 val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
3866 - rix[-1][1] - rix[1][1] ) >> 1);
3867 rix[0][2-c] = CLIP(val);
3868 val = pix[0][1] + (( pix[-width][c] + pix[width][c]
3869 - rix[-TS][1] - rix[TS][1] ) >> 1);
3870 } else
3871 val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
3872 + pix[+width-1][c] + pix[+width+1][c]
3873 - rix[-TS-1][1] - rix[-TS+1][1]
3874 - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
3875 rix[0][c] = CLIP(val);
3876 c = FC(row,col);
3877 rix[0][c] = pix[0][c];
3878 xyz[0] = xyz[1] = xyz[2] = 0.5;
3879 FORCC {
3880 xyz[0] += xyz_cam[0][c] * rix[0][c];
3881 xyz[1] += xyz_cam[1][c] * rix[0][c];
3882 xyz[2] += xyz_cam[2][c] * rix[0][c];
3884 xyz[0] = cbrt[CLIP((int) xyz[0])];
3885 xyz[1] = cbrt[CLIP((int) xyz[1])];
3886 xyz[2] = cbrt[CLIP((int) xyz[2])];
3887 lix[0][0] = 64 * (116 * xyz[1] - 16);
3888 lix[0][1] = 64 * 500 * (xyz[0] - xyz[1]);
3889 lix[0][2] = 64 * 200 * (xyz[1] - xyz[2]);
3891 /* Build homogeneity maps from the CIELab images: */
3892 memset (homo, 0, 2*TS*TS);
3893 for (row=top+2; row < top+TS-2 && row < height-4; row++) {
3894 tr = row-top;
3895 for (col=left+2; col < left+TS-2 && col < width-4; col++) {
3896 tc = col-left;
3897 for (d=0; d < 2; d++) {
3898 lix = &lab[d][tr][tc];
3899 for (i=0; i < 4; i++) {
3900 ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
3901 abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
3902 + SQR(lix[0][2]-lix[dir[i]][2]);
3905 leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
3906 MAX(ldiff[1][2],ldiff[1][3]));
3907 abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
3908 MAX(abdiff[1][2],abdiff[1][3]));
3909 for (d=0; d < 2; d++)
3910 for (i=0; i < 4; i++)
3911 if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
3912 homo[d][tr][tc]++;
3915 /* Combine the most homogenous pixels for the final result: */
3916 for (row=top+3; row < top+TS-3 && row < height-5; row++) {
3917 tr = row-top;
3918 for (col=left+3; col < left+TS-3 && col < width-5; col++) {
3919 tc = col-left;
3920 for (d=0; d < 2; d++)
3921 for (hm[d]=0, i=tr-1; i <= tr+1; i++)
3922 for (j=tc-1; j <= tc+1; j++)
3923 hm[d] += homo[d][i][j];
3924 if (hm[0] != hm[1])
3925 FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
3926 else
3927 FORC3 image[row*width+col][c] =
3928 (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
3933 free (buffer);
3935 #else
3936 void CLASS ahd_interpolate()
3938 int i, j, k, top, left, row, col, tr, tc, c, d, val, hm[2];
3939 ushort (*pix)[4], (*rix)[3];
3940 static const int dir[4] = { -1, 1, -TS, TS };
3941 unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
3942 float r, cbrt[0x10000], xyz[3], xyz_cam[3][4];
3943 ushort (*rgb)[TS][TS][3];
3944 short (*lab)[TS][TS][3], (*lix)[3];
3945 char (*homo)[TS][TS], *buffer;
3947 #ifdef DCRAW_VERBOSE
3948 if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
3949 #endif
3951 for (i=0; i < 0x10000; i++) {
3952 r = i / 65535.0;
3953 cbrt[i] = r > 0.008856 ? pow(r,1/3.0) : 7.787*r + 16/116.0;
3955 for (i=0; i < 3; i++)
3956 for (j=0; j < colors; j++)
3957 for (xyz_cam[i][j] = k=0; k < 3; k++)
3958 xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
3960 border_interpolate(5);
3962 #ifdef LIBRAW_LIBRARY_BUILD
3963 #pragma omp parallel private(buffer,rgb,lab,homo,top,left,row,c,col,pix,val,d,rix,xyz,lix,tc,tr,ldiff,abdiff,leps,abeps,hm,i,j) firstprivate(cbrt) shared(xyz_cam)
3964 #endif
3966 buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
3967 merror (buffer, "ahd_interpolate()");
3968 rgb = (ushort(*)[TS][TS][3]) buffer;
3969 lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
3970 homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
3972 #pragma omp for schedule(dynamic)
3973 for (top=2; top < height-5; top += TS-6){
3974 #ifdef LIBRAW_LIBRARY_BUILD
3975 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE,(top-2)/(TS-6)+1,(height-7)/(TS-6)+1);
3976 #endif
3977 for (left=2; left < width-5; left += TS-6) {
3979 /* Interpolate green horizontally and vertically: */
3980 for (row = top; row < top+TS && row < height-2; row++) {
3981 col = left + (FC(row,left) & 1);
3982 for (c = FC(row,col); col < left+TS && col < width-2; col+=2) {
3983 pix = image + row*width+col;
3984 val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
3985 - pix[-2][c] - pix[2][c]) >> 2;
3986 rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
3987 val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
3988 - pix[-2*width][c] - pix[2*width][c]) >> 2;
3989 rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
3992 /* Interpolate red and blue, and convert to CIELab: */
3993 for (d=0; d < 2; d++)
3994 for (row=top+1; row < top+TS-1 && row < height-3; row++)
3995 for (col=left+1; col < left+TS-1 && col < width-3; col++) {
3996 pix = image + row*width+col;
3997 rix = &rgb[d][row-top][col-left];
3998 lix = &lab[d][row-top][col-left];
3999 if ((c = 2 - FC(row,col)) == 1) {
4000 c = FC(row+1,col);
4001 val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
4002 - rix[-1][1] - rix[1][1] ) >> 1);
4003 rix[0][2-c] = CLIP(val);
4004 val = pix[0][1] + (( pix[-width][c] + pix[width][c]
4005 - rix[-TS][1] - rix[TS][1] ) >> 1);
4006 } else
4007 val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
4008 + pix[+width-1][c] + pix[+width+1][c]
4009 - rix[-TS-1][1] - rix[-TS+1][1]
4010 - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
4011 rix[0][c] = CLIP(val);
4012 c = FC(row,col);
4013 rix[0][c] = pix[0][c];
4014 xyz[0] = xyz[1] = xyz[2] = 0.5;
4015 FORCC {
4016 xyz[0] += xyz_cam[0][c] * rix[0][c];
4017 xyz[1] += xyz_cam[1][c] * rix[0][c];
4018 xyz[2] += xyz_cam[2][c] * rix[0][c];
4020 xyz[0] = cbrt[CLIP((int) xyz[0])];
4021 xyz[1] = cbrt[CLIP((int) xyz[1])];
4022 xyz[2] = cbrt[CLIP((int) xyz[2])];
4023 lix[0][0] = 64 * (116 * xyz[1] - 16);
4024 lix[0][1] = 64 * 500 * (xyz[0] - xyz[1]);
4025 lix[0][2] = 64 * 200 * (xyz[1] - xyz[2]);
4027 /* Build homogeneity maps from the CIELab images: */
4028 memset (homo, 0, 2*TS*TS);
4029 for (row=top+2; row < top+TS-2 && row < height-4; row++) {
4030 tr = row-top;
4031 for (col=left+2; col < left+TS-2 && col < width-4; col++) {
4032 tc = col-left;
4033 for (d=0; d < 2; d++) {
4034 lix = &lab[d][tr][tc];
4035 for (i=0; i < 4; i++) {
4036 ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
4037 abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
4038 + SQR(lix[0][2]-lix[dir[i]][2]);
4041 leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
4042 MAX(ldiff[1][2],ldiff[1][3]));
4043 abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
4044 MAX(abdiff[1][2],abdiff[1][3]));
4045 for (d=0; d < 2; d++)
4046 for (i=0; i < 4; i++)
4047 if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
4048 homo[d][tr][tc]++;
4051 /* Combine the most homogenous pixels for the final result: */
4052 for (row=top+3; row < top+TS-3 && row < height-5; row++) {
4053 tr = row-top;
4054 for (col=left+3; col < left+TS-3 && col < width-5; col++) {
4055 tc = col-left;
4056 for (d=0; d < 2; d++)
4057 for (hm[d]=0, i=tr-1; i <= tr+1; i++)
4058 for (j=tc-1; j <= tc+1; j++)
4059 hm[d] += homo[d][i][j];
4060 if (hm[0] != hm[1])
4061 FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
4062 else
4063 FORC3 image[row*width+col][c] =
4064 (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
4069 free (buffer);
4073 #endif
4074 #undef TS
4076 void CLASS median_filter()
4078 ushort (*pix)[4];
4079 int pass, c, i, j, k, med[9];
4080 static const uchar opt[] = /* Optimal 9-element median search */
4081 { 1,2, 4,5, 7,8, 0,1, 3,4, 6,7, 1,2, 4,5, 7,8,
4082 0,3, 5,8, 4,7, 3,6, 1,4, 2,5, 4,7, 4,2, 6,4, 4,2 };
4084 for (pass=1; pass <= med_passes; pass++) {
4085 #ifdef LIBRAW_LIBRARY_BUILD
4086 RUN_CALLBACK(LIBRAW_PROGRESS_MEDIAN_FILTER,pass-1,med_passes);
4087 #endif
4088 #ifdef DCRAW_VERBOSE
4089 if (verbose)
4090 fprintf (stderr,_("Median filter pass %d...\n"), pass);
4091 #endif
4092 for (c=0; c < 3; c+=2) {
4093 for (pix = image; pix < image+width*height; pix++)
4094 pix[0][3] = pix[0][c];
4095 for (pix = image+width; pix < image+width*(height-1); pix++) {
4096 if ((pix-image+1) % width < 2) continue;
4097 for (k=0, i = -width; i <= width; i += width)
4098 for (j = i-1; j <= i+1; j++)
4099 med[k++] = pix[j][3] - pix[j][1];
4100 for (i=0; i < sizeof opt; i+=2)
4101 if (med[opt[i]] > med[opt[i+1]])
4102 SWAP (med[opt[i]] , med[opt[i+1]]);
4103 pix[0][c] = CLIP(med[4] + pix[0][1]);
4109 void CLASS blend_highlights()
4111 int clip=INT_MAX, row, col, c, i, j;
4112 static const float trans[2][4][4] =
4113 { { { 1,1,1 }, { 1.7320508,-1.7320508,0 }, { -1,-1,2 } },
4114 { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
4115 static const float itrans[2][4][4] =
4116 { { { 1,0.8660254,-0.5 }, { 1,-0.8660254,-0.5 }, { 1,0,1 } },
4117 { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
4118 float cam[2][4], lab[2][4], sum[2], chratio;
4120 if ((unsigned) (colors-3) > 1) return;
4121 #ifdef DCRAW_VERBOSE
4122 if (verbose) fprintf (stderr,_("Blending highlights...\n"));
4123 #endif
4124 FORCC if (clip > (i = 65535*pre_mul[c])) clip = i;
4125 #ifdef LIBRAW_LIBRARY_BUILD
4126 RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,0,2);
4127 #endif
4128 for (row=0; row < height; row++)
4129 for (col=0; col < width; col++) {
4130 FORCC if (image[row*width+col][c] > clip) break;
4131 if (c == colors) continue;
4132 FORCC {
4133 cam[0][c] = image[row*width+col][c];
4134 cam[1][c] = MIN(cam[0][c],clip);
4136 for (i=0; i < 2; i++) {
4137 FORCC for (lab[i][c]=j=0; j < colors; j++)
4138 lab[i][c] += trans[colors-3][c][j] * cam[i][j];
4139 for (sum[i]=0,c=1; c < colors; c++)
4140 sum[i] += SQR(lab[i][c]);
4142 chratio = sqrt(sum[1]/sum[0]);
4143 for (c=1; c < colors; c++)
4144 lab[0][c] *= chratio;
4145 FORCC for (cam[0][c]=j=0; j < colors; j++)
4146 cam[0][c] += itrans[colors-3][c][j] * lab[0][j];
4147 FORCC image[row*width+col][c] = cam[0][c] / colors;
4149 #ifdef LIBRAW_LIBRARY_BUILD
4150 RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,1,2);
4151 #endif
4154 #define SCALE (4 >> shrink)
4155 void CLASS recover_highlights()
4157 float *map, sum, wgt, grow;
4158 int hsat[4], count, spread, change, val, i;
4159 unsigned high, wide, mrow, mcol, row, col, kc, c, d, y, x;
4160 ushort *pixel;
4161 static const signed char dir[8][2] =
4162 { {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
4164 #ifdef DCRAW_VERBOSE
4165 if (verbose) fprintf (stderr,_("Rebuilding highlights...\n"));
4166 #endif
4168 grow = pow (2.0, 4.0-highlight);
4169 FORCC hsat[c] = 32000 * pre_mul[c];
4170 for (kc=0, c=1; c < colors; c++)
4171 if (pre_mul[kc] < pre_mul[c]) kc = c;
4172 high = height / SCALE;
4173 wide = width / SCALE;
4174 map = (float *) calloc (high*wide, sizeof *map);
4175 merror (map, "recover_highlights()");
4176 FORCC if (c != kc) {
4177 #ifdef LIBRAW_LIBRARY_BUILD
4178 RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS,c-1,colors-1);
4179 #endif
4180 memset (map, 0, high*wide*sizeof *map);
4181 for (mrow=0; mrow < high; mrow++)
4182 for (mcol=0; mcol < wide; mcol++) {
4183 sum = wgt = count = 0;
4184 for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
4185 for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
4186 pixel = image[row*width+col];
4187 if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000) {
4188 sum += pixel[c];
4189 wgt += pixel[kc];
4190 count++;
4193 if (count == SCALE*SCALE)
4194 map[mrow*wide+mcol] = sum / wgt;
4196 for (spread = 32/grow; spread--; ) {
4197 for (mrow=0; mrow < high; mrow++)
4198 for (mcol=0; mcol < wide; mcol++) {
4199 if (map[mrow*wide+mcol]) continue;
4200 sum = count = 0;
4201 for (d=0; d < 8; d++) {
4202 y = mrow + dir[d][0];
4203 x = mcol + dir[d][1];
4204 if (y < high && x < wide && map[y*wide+x] > 0) {
4205 sum += (1 + (d & 1)) * map[y*wide+x];
4206 count += 1 + (d & 1);
4209 if (count > 3)
4210 map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
4212 for (change=i=0; i < high*wide; i++)
4213 if (map[i] < 0) {
4214 map[i] = -map[i];
4215 change = 1;
4217 if (!change) break;
4219 for (i=0; i < high*wide; i++)
4220 if (map[i] == 0) map[i] = 1;
4221 for (mrow=0; mrow < high; mrow++)
4222 for (mcol=0; mcol < wide; mcol++) {
4223 for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
4224 for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
4225 pixel = image[row*width+col];
4226 if (pixel[c] / hsat[c] > 1) {
4227 val = pixel[kc] * map[mrow*wide+mcol];
4228 if (pixel[c] < val) pixel[c] = CLIP(val);
4233 free (map);
4235 #undef SCALE
4237 void CLASS tiff_get (unsigned base,
4238 unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
4240 *tag = get2();
4241 *type = get2();
4242 *len = get4();
4243 *save = ftell(ifp) + 4;
4244 if (*len * ("11124811248488"[*type < 14 ? *type:0]-'0') > 4)
4245 fseek (ifp, get4()+base, SEEK_SET);
4248 void CLASS parse_thumb_note (int base, unsigned toff, unsigned tlen)
4250 unsigned entries, tag, type, len, save;
4252 entries = get2();
4253 while (entries--) {
4254 tiff_get (base, &tag, &type, &len, &save);
4255 if (tag == toff) thumb_offset = get4()+base;
4256 if (tag == tlen) thumb_length = get4();
4257 fseek (ifp, save, SEEK_SET);
4261 #line 5429 "dcraw/dcraw.c"
4262 void CLASS parse_makernote (int base, int uptag)
4264 static const uchar xlat[2][256] = {
4265 { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
4266 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
4267 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
4268 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
4269 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
4270 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
4271 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
4272 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
4273 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
4274 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
4275 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
4276 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
4277 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
4278 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
4279 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
4280 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
4281 { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
4282 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
4283 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
4284 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
4285 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
4286 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
4287 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
4288 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
4289 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
4290 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
4291 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
4292 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
4293 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
4294 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
4295 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
4296 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
4297 unsigned offset=0, entries, tag, type, len, save, c;
4298 unsigned ver97=0, serial=0, i, wbi=0, wb[4]={0,0,0,0};
4299 uchar buf97[324], ci, cj, ck;
4300 short sorder=order;
4301 char buf[10];
4303 The MakerNote might have its own TIFF header (possibly with
4304 its own byte-order!), or it might just be a table.
4306 fread (buf, 1, 10, ifp);
4307 if (!strncmp (buf,"KDK" ,3) || /* these aren't TIFF tables */
4308 !strncmp (buf,"VER" ,3) ||
4309 !strncmp (buf,"IIII",4) ||
4310 !strncmp (buf,"MMMM",4)) return;
4311 if (!strncmp (buf,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
4312 !strncmp (buf,"MLY" ,3)) { /* Minolta DiMAGE G series */
4313 order = 0x4d4d;
4314 while ((i=ftell(ifp)) < data_offset && i < 16384) {
4315 wb[0] = wb[2]; wb[2] = wb[1]; wb[1] = wb[3];
4316 wb[3] = get2();
4317 if (wb[1] == 256 && wb[3] == 256 &&
4318 wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
4319 FORC4 cam_mul[c] = wb[c];
4320 #ifdef LIBRAW_LIBRARY_BUILD
4321 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4322 #endif
4324 goto quit;
4326 if (!strcmp (buf,"Nikon")) {
4327 base = ftell(ifp);
4328 order = get2();
4329 if (get2() != 42) goto quit;
4330 offset = get4();
4331 fseek (ifp, offset-8, SEEK_CUR);
4332 } else if (!strcmp (buf,"OLYMPUS")) {
4333 base = ftell(ifp)-10;
4334 fseek (ifp, -2, SEEK_CUR);
4335 order = get2(); get2();
4336 } else if (!strncmp (buf,"FUJIFILM",8) ||
4337 !strncmp (buf,"SONY",4) ||
4338 !strcmp (buf,"Panasonic")) {
4339 order = 0x4949;
4340 fseek (ifp, 2, SEEK_CUR);
4341 } else if (!strcmp (buf,"OLYMP") ||
4342 !strcmp (buf,"LEICA") ||
4343 !strcmp (buf,"Ricoh") ||
4344 !strcmp (buf,"EPSON"))
4345 fseek (ifp, -2, SEEK_CUR);
4346 else if (!strcmp (buf,"AOC") ||
4347 !strcmp (buf,"QVC"))
4348 fseek (ifp, -4, SEEK_CUR);
4349 else fseek (ifp, -10, SEEK_CUR);
4351 entries = get2();
4352 if (entries > 1000) return;
4353 while (entries--) {
4354 tiff_get (base, &tag, &type, &len, &save);
4355 tag |= uptag << 16;
4356 if (tag == 2 && strstr(make,"NIKON"))
4357 iso_speed = (get2(),get2());
4358 if (tag == 4 && len > 26 && len < 35) {
4359 if ((i=(get4(),get2())) != 0x7fff && !iso_speed)
4360 iso_speed = 50 * pow (2, i/32.0 - 4);
4361 if ((i=(get2(),get2())) != 0x7fff && !aperture)
4362 aperture = pow (2, i/64.0);
4363 if ((i=get2()) != 0xffff && !shutter)
4364 shutter = pow (2, (short) i/-32.0);
4365 wbi = (get2(),get2());
4366 shot_order = (get2(),get2());
4368 if (tag == 7 && type == 2 && len > 20)
4369 fgets (model2, 64, ifp);
4370 if (tag == 8 && type == 4)
4371 shot_order = get4();
4372 if (tag == 9 && !strcmp(make,"Canon"))
4373 fread (artist, 64, 1, ifp);
4374 if (tag == 0xc && len == 4) {
4375 cam_mul[0] = getreal(type);
4376 cam_mul[2] = getreal(type);
4377 #ifdef LIBRAW_LIBRARY_BUILD
4378 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4379 #endif
4381 if (tag == 0x10 && type == 4)
4382 unique_id = get4();
4383 if (tag == 0x11 && is_raw && !strncmp(make,"NIKON",5)) {
4384 fseek (ifp, get4()+base, SEEK_SET);
4385 parse_tiff_ifd (base);
4387 if (tag == 0x14 && len == 2560 && type == 7) {
4388 fseek (ifp, 1248, SEEK_CUR);
4389 goto get2_256;
4391 if (tag == 0x15 && type == 2 && is_raw)
4392 fread (model, 64, 1, ifp);
4393 if (strstr(make,"PENTAX")) {
4394 if (tag == 0x1b) tag = 0x1018;
4395 if (tag == 0x1c) tag = 0x1017;
4397 if (tag == 0x1d)
4398 while ((c = fgetc(ifp)) && c != EOF)
4399 serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
4400 if (tag == 0x81 && type == 4) {
4401 data_offset = get4();
4402 fseek (ifp, data_offset + 41, SEEK_SET);
4403 raw_height = get2() * 2;
4404 raw_width = get2();
4405 filters = 0x61616161;
4407 if (tag == 0x29 && type == 1) {
4408 c = wbi < 18 ? "012347800000005896"[wbi]-'0' : 0;
4409 fseek (ifp, 8 + c*32, SEEK_CUR);
4410 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get4();
4411 #ifdef LIBRAW_LIBRARY_BUILD
4412 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4413 #endif
4415 if ((tag == 0x81 && type == 7) ||
4416 (tag == 0x100 && type == 7) ||
4417 (tag == 0x280 && type == 1)) {
4418 thumb_offset = ftell(ifp);
4419 thumb_length = len;
4421 if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
4422 thumb_offset += base;
4423 if (tag == 0x89 && type == 4)
4424 thumb_length = get4();
4425 if (tag == 0x8c || tag == 0x96)
4426 meta_offset = ftell(ifp);
4427 if (tag == 0x97) {
4428 for (i=0; i < 4; i++)
4429 ver97 = ver97 * 10 + fgetc(ifp)-'0';
4430 switch (ver97) {
4431 case 100:
4432 fseek (ifp, 68, SEEK_CUR);
4433 FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
4434 #ifdef LIBRAW_LIBRARY_BUILD
4435 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4436 #endif
4437 break;
4438 case 102:
4439 fseek (ifp, 6, SEEK_CUR);
4440 goto get2_rggb;
4441 case 103:
4442 fseek (ifp, 16, SEEK_CUR);
4443 FORC4 cam_mul[c] = get2();
4444 #ifdef LIBRAW_LIBRARY_BUILD
4445 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4446 #endif
4448 if (ver97 >= 200) {
4449 if (ver97 != 205) fseek (ifp, 280, SEEK_CUR);
4450 fread (buf97, 324, 1, ifp);
4453 if (tag == 0xa4 && type == 3) {
4454 fseek (ifp, wbi*48, SEEK_CUR);
4455 FORC3 cam_mul[c] = get2();
4456 #ifdef LIBRAW_LIBRARY_BUILD
4457 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4458 #endif
4460 if (tag == 0xa7 && (unsigned) (ver97-200) < 12 && !cam_mul[0]) {
4461 ci = xlat[0][serial & 0xff];
4462 cj = xlat[1][fgetc(ifp)^fgetc(ifp)^fgetc(ifp)^fgetc(ifp)];
4463 ck = 0x60;
4464 for (i=0; i < 324; i++)
4465 buf97[i] ^= (cj += ci * ck++);
4466 i = "66666>666;6A"[ver97-200] - '0';
4467 FORC4 cam_mul[c ^ (c >> 1) ^ (i & 1)] =
4468 sget2 (buf97 + (i & -2) + c*2);
4469 #ifdef LIBRAW_LIBRARY_BUILD
4470 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4471 #endif
4473 if (tag == 0x200 && len == 3)
4474 shot_order = (get4(),get4());
4475 if (tag == 0x200 && len == 4)
4476 black = (get2()+get2()+get2()+get2())/4;
4477 if (tag == 0x201 && len == 4)
4478 goto get2_rggb;
4479 if (tag == 0x220 && len == 53) {
4480 fseek (ifp, 14, SEEK_CUR);
4481 pentax_tree();
4483 if (tag == 0x401 && len == 4) {
4484 black = (get4()+get4()+get4()+get4())/4;
4486 if (tag == 0xe01) { /* Nikon Capture Note */
4487 type = order;
4488 order = 0x4949;
4489 fseek (ifp, 22, SEEK_CUR);
4490 for (offset=22; offset+22 < len; offset += 22+i) {
4491 tag = get4();
4492 fseek (ifp, 14, SEEK_CUR);
4493 i = get4()-4;
4494 if (tag == 0x76a43207) flip = get2();
4495 else fseek (ifp, i, SEEK_CUR);
4497 order = type;
4499 if (tag == 0xe80 && len == 256 && type == 7) {
4500 fseek (ifp, 48, SEEK_CUR);
4501 cam_mul[0] = get2() * 508 * 1.078 / 0x10000;
4502 cam_mul[2] = get2() * 382 * 1.173 / 0x10000;
4503 #ifdef LIBRAW_LIBRARY_BUILD
4504 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4505 #endif
4507 if (tag == 0xf00 && type == 7) {
4508 if (len == 614)
4509 fseek (ifp, 176, SEEK_CUR);
4510 else if (len == 734 || len == 1502)
4511 fseek (ifp, 148, SEEK_CUR);
4512 else goto next;
4513 goto get2_256;
4515 if ((tag == 0x1011 && len == 9) || tag == 0x20400200)
4517 for (i=0; i < 3; i++)
4518 FORC3 cmatrix[i][c] = ((short) get2()) / 256.0;
4519 #ifdef LIBRAW_LIBRARY_BUILD
4520 color_flags.cmatrix_state = LIBRAW_COLORSTATE_LOADED;
4521 #endif
4523 if ((tag == 0x1012 || tag == 0x20400600) && len == 4)
4524 for (black = i=0; i < 4; i++)
4525 black += get2() << 2;
4526 if (tag == 0x1017 || tag == 0x20400100)
4528 cam_mul[0] = get2() / 256.0;
4529 #ifdef LIBRAW_LIBRARY_BUILD
4530 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4531 #endif
4533 if (tag == 0x1018 || tag == 0x20400100)
4535 cam_mul[2] = get2() / 256.0;
4536 #ifdef LIBRAW_LIBRARY_BUILD
4537 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4538 #endif
4540 if (tag == 0x2011 && len == 2) {
4541 get2_256:
4542 order = 0x4d4d;
4543 cam_mul[0] = get2() / 256.0;
4544 cam_mul[2] = get2() / 256.0;
4545 #ifdef LIBRAW_LIBRARY_BUILD
4546 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4547 #endif
4549 if ((tag | 0x70) == 0x2070 && type == 4)
4550 fseek (ifp, get4()+base, SEEK_SET);
4551 if (tag == 0x2010 && type != 7)
4552 load_raw = &CLASS olympus_e410_load_raw;
4553 if (tag == 0x2020)
4554 parse_thumb_note (base, 257, 258);
4555 if (tag == 0x2040)
4556 parse_makernote (base, 0x2040);
4557 if (tag == 0xb028) {
4558 fseek (ifp, get4(), SEEK_SET);
4559 parse_thumb_note (base, 136, 137);
4561 if (tag == 0x4001 && len > 500) {
4562 i = len == 582 ? 50 : len == 653 ? 68 : len == 5120 ? 142 : 126;
4563 fseek (ifp, i, SEEK_CUR);
4564 get2_rggb:
4565 FORC4 cam_mul[c ^ (c >> 1)] = get2();
4566 #ifdef LIBRAW_LIBRARY_BUILD
4567 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4568 #endif
4569 fseek (ifp, 22, SEEK_CUR);
4570 FORC4 sraw_mul[c ^ (c >> 1)] = get2();
4572 next:
4573 fseek (ifp, save, SEEK_SET);
4575 quit:
4576 order = sorder;
4580 Since the TIFF DateTime string has no timezone information,
4581 assume that the camera's clock was set to Universal Time.
4583 void CLASS get_timestamp (int reversed)
4585 struct tm t;
4586 char str[20];
4587 int i;
4589 str[19] = 0;
4590 if (reversed)
4591 for (i=19; i--; ) str[i] = fgetc(ifp);
4592 else
4593 fread (str, 19, 1, ifp);
4594 memset (&t, 0, sizeof t);
4595 if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
4596 &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
4597 return;
4598 t.tm_year -= 1900;
4599 t.tm_mon -= 1;
4600 if (mktime(&t) > 0)
4601 timestamp = mktime(&t);
4604 void CLASS parse_exif (int base)
4606 unsigned kodak, entries, tag, type, len, save, c;
4607 double expo;
4609 kodak = !strncmp(make,"EASTMAN",7);
4610 entries = get2();
4611 while (entries--) {
4612 tiff_get (base, &tag, &type, &len, &save);
4613 switch (tag) {
4614 case 33434: shutter = getreal(type); break;
4615 case 33437: aperture = getreal(type); break;
4616 case 34855: iso_speed = get2(); break;
4617 case 36867:
4618 case 36868: get_timestamp(0); break;
4619 case 37377: if ((expo = -getreal(type)) < 128)
4620 shutter = pow (2, expo); break;
4621 case 37378: aperture = pow (2, getreal(type)/2); break;
4622 case 37386: focal_len = getreal(type); break;
4623 case 37500: parse_makernote (base, 0); break;
4624 case 40962: if (kodak) raw_width = get4(); break;
4625 case 40963: if (kodak) raw_height = get4(); break;
4626 case 41730:
4627 if (get4() == 0x20002)
4628 for (exif_cfa=c=0; c < 8; c+=2)
4629 exif_cfa |= fgetc(ifp) * 0x01010101 << c;
4631 fseek (ifp, save, SEEK_SET);
4635 void CLASS parse_gps (int base)
4637 unsigned entries, tag, type, len, save, c;
4639 entries = get2();
4640 while (entries--) {
4641 tiff_get (base, &tag, &type, &len, &save);
4642 switch (tag) {
4643 case 1: case 3: case 5:
4644 gpsdata[29+tag/2] = getc(ifp); break;
4645 case 2: case 4: case 7:
4646 FORC(6) gpsdata[tag/3*6+c] = get4(); break;
4647 case 6:
4648 FORC(2) gpsdata[18+c] = get4(); break;
4649 case 18: case 29:
4650 fgets ((char *) (gpsdata+14+tag/3), MIN(len,12), ifp);
4652 fseek (ifp, save, SEEK_SET);
4656 void CLASS romm_coeff (float romm_cam[3][3])
4658 static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
4659 { { 2.034193, -0.727420, -0.306766 },
4660 { -0.228811, 1.231729, -0.002922 },
4661 { -0.008565, -0.153273, 1.161839 } };
4662 int i, j, k;
4664 for (i=0; i < 3; i++)
4665 for (j=0; j < 3; j++)
4666 for (cmatrix[i][j] = k=0; k < 3; k++)
4667 cmatrix[i][j] += rgb_romm[i][k] * romm_cam[k][j];
4668 #ifdef LIBRAW_LIBRARY_BUILD
4669 color_flags.cmatrix_state = LIBRAW_COLORSTATE_CALCULATED;
4670 #endif
4673 void CLASS parse_mos (int offset)
4675 char data[40];
4676 int skip, from, i, c, neut[4], planes=0, frot=0;
4677 static const char *mod[] =
4678 { "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
4679 "Valeo 11p","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65",
4680 "Aptus 54S","Aptus 65S","Aptus 75S","AFi 5","AFi 6","AFi 7" };
4681 float romm_cam[3][3];
4683 fseek (ifp, offset, SEEK_SET);
4684 while (1) {
4685 if (get4() != 0x504b5453) break;
4686 get4();
4687 fread (data, 1, 40, ifp);
4688 skip = get4();
4689 from = ftell(ifp);
4690 if (!strcmp(data,"JPEG_preview_data")) {
4691 thumb_offset = from;
4692 thumb_length = skip;
4694 if (!strcmp(data,"icc_camera_profile")) {
4695 profile_offset = from;
4696 profile_length = skip;
4698 if (!strcmp(data,"ShootObj_back_type")) {
4699 fscanf (ifp, "%d", &i);
4700 if ((unsigned) i < sizeof mod / sizeof (*mod))
4701 strcpy (model, mod[i]);
4703 if (!strcmp(data,"icc_camera_to_tone_matrix")) {
4704 for (i=0; i < 9; i++)
4705 romm_cam[0][i] = int_to_float(get4());
4706 romm_coeff (romm_cam);
4708 if (!strcmp(data,"CaptProf_color_matrix")) {
4709 for (i=0; i < 9; i++)
4710 fscanf (ifp, "%f", &romm_cam[0][i]);
4711 romm_coeff (romm_cam);
4713 if (!strcmp(data,"CaptProf_number_of_planes"))
4714 fscanf (ifp, "%d", &planes);
4715 if (!strcmp(data,"CaptProf_raw_data_rotation"))
4716 fscanf (ifp, "%d", &flip);
4717 if (!strcmp(data,"CaptProf_mosaic_pattern"))
4718 FORC4 {
4719 fscanf (ifp, "%d", &i);
4720 if (i == 1) frot = c ^ (c >> 1);
4722 if (!strcmp(data,"ImgProf_rotation_angle")) {
4723 fscanf (ifp, "%d", &i);
4724 flip = i - flip;
4726 if (!strcmp(data,"NeutObj_neutrals") && !cam_mul[0]) {
4727 FORC4 fscanf (ifp, "%d", neut+c);
4728 FORC3 cam_mul[c] = (float) neut[0] / neut[c+1];
4729 #ifdef LIBRAW_LIBRARY_BUILD
4730 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4731 #endif
4733 parse_mos (from);
4734 fseek (ifp, skip+from, SEEK_SET);
4736 if (planes)
4737 filters = (planes == 1) * 0x01010101 *
4738 (uchar) "\x94\x61\x16\x49"[(flip/90 + frot) & 3];
4741 void CLASS linear_table (unsigned len)
4743 int i;
4744 if (len > 0x1000) len = 0x1000;
4745 read_shorts (curve, len);
4746 #ifdef LIBRAW_LIBRARY_BUILD
4747 color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
4748 #endif
4749 for (i=len; i < 0x1000; i++)
4750 curve[i] = curve[i-1];
4751 maximum = curve[0xfff];
4754 void CLASS parse_kodak_ifd (int base)
4756 unsigned entries, tag, type, len, save;
4757 int i, c, wbi=-2, wbtemp=6500;
4758 float mul[3], num;
4760 entries = get2();
4761 if (entries > 1024) return;
4762 while (entries--) {
4763 tiff_get (base, &tag, &type, &len, &save);
4764 if (tag == 1020) wbi = getint(type);
4765 if (tag == 1021 && len == 72) { /* WB set in software */
4766 fseek (ifp, 40, SEEK_CUR);
4767 FORC3 cam_mul[c] = 2048.0 / get2();
4768 #ifdef LIBRAW_LIBRARY_BUILD
4769 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4770 #endif
4771 wbi = -2;
4773 if (tag == 2118) wbtemp = getint(type);
4774 if (tag == 2130 + wbi)
4775 FORC3 mul[c] = getreal(type);
4776 if (tag == 2140 + wbi && wbi >= 0)
4778 FORC3 {
4779 for (num=i=0; i < 4; i++)
4780 num += getreal(type) * pow (wbtemp/100.0, i);
4781 cam_mul[c] = 2048 / (num * mul[c]);
4783 #ifdef LIBRAW_LIBRARY_BUILD
4784 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4785 #endif
4787 if (tag == 2317) linear_table (len);
4788 if (tag == 6020) iso_speed = getint(type);
4789 fseek (ifp, save, SEEK_SET);
4793 #line 5964 "dcraw/dcraw.c"
4794 int CLASS parse_tiff_ifd (int base)
4796 unsigned entries, tag, type, len, plen=16, save;
4797 int ifd, use_cm=0, cfa, i, j, c, ima_len=0;
4798 char software[64], *cbuf, *cp;
4799 uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
4800 double dblack, cc[4][4], cm[4][3], cam_xyz[4][3], num;
4801 double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
4802 unsigned sony_curve[] = { 0,0,0,0,0,4095 };
4803 unsigned *buf, sony_offset=0, sony_length=0, sony_key=0;
4804 struct jhead jh;
4805 FILE *sfp;
4807 if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
4808 return 1;
4809 ifd = tiff_nifds++;
4810 for (j=0; j < 4; j++)
4811 for (i=0; i < 4; i++)
4812 cc[j][i] = i == j;
4813 entries = get2();
4814 if (entries > 512) return 1;
4815 while (entries--) {
4816 tiff_get (base, &tag, &type, &len, &save);
4817 switch (tag) {
4818 case 17: case 18:
4819 if (type == 3 && len == 1)
4821 cam_mul[(tag-17)*2] = get2() / 256.0;
4822 #ifdef LIBRAW_LIBRARY_BUILD
4823 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4824 #endif
4826 break;
4827 case 23:
4828 if (type == 3) iso_speed = get2();
4829 break;
4830 case 36: case 37: case 38:
4831 cam_mul[tag-0x24] = get2();
4832 break;
4833 case 39:
4834 if (len < 50 || cam_mul[0]) break;
4835 fseek (ifp, 12, SEEK_CUR);
4836 FORC3 cam_mul[c] = get2();
4837 #ifdef LIBRAW_LIBRARY_BUILD
4838 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4839 #endif
4840 break;
4841 case 46:
4842 if (type != 7 || fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) break;
4843 thumb_offset = ftell(ifp) - 2;
4844 thumb_length = len;
4845 break;
4846 case 2: case 256: /* ImageWidth */
4847 tiff_ifd[ifd].t_width = getint(type);
4848 break;
4849 case 3: case 257: /* ImageHeight */
4850 tiff_ifd[ifd].t_height = getint(type);
4851 break;
4852 case 258: /* BitsPerSample */
4853 tiff_ifd[ifd].samples = len & 7;
4854 tiff_ifd[ifd].bps = get2();
4855 break;
4856 case 259: /* Compression */
4857 tiff_ifd[ifd].comp = get2();
4858 break;
4859 case 262: /* PhotometricInterpretation */
4860 tiff_ifd[ifd].phint = get2();
4861 break;
4862 case 270: /* ImageDescription */
4863 fread (desc, 512, 1, ifp);
4864 break;
4865 case 271: /* Make */
4866 fgets (make, 64, ifp);
4867 break;
4868 case 272: /* Model */
4869 fgets (model, 64, ifp);
4870 break;
4871 case 280: /* Panasonic RW2 offset */
4872 if (type != 4) break;
4873 load_raw = &CLASS panasonic_load_raw;
4874 load_flags = 0x2008;
4875 case 273: /* StripOffset */
4876 case 513:
4877 tiff_ifd[ifd].offset = get4()+base;
4878 if (!tiff_ifd[ifd].bps) {
4879 fseek (ifp, tiff_ifd[ifd].offset, SEEK_SET);
4880 if (ljpeg_start (&jh, 1)) {
4881 tiff_ifd[ifd].comp = 6;
4882 tiff_ifd[ifd].t_width = jh.wide << (jh.clrs == 2);
4883 tiff_ifd[ifd].t_height = jh.high;
4884 tiff_ifd[ifd].bps = jh.bits;
4885 tiff_ifd[ifd].samples = jh.clrs;
4888 break;
4889 case 274: /* Orientation */
4890 tiff_ifd[ifd].t_flip = "50132467"[get2() & 7]-'0';
4891 break;
4892 case 277: /* SamplesPerPixel */
4893 tiff_ifd[ifd].samples = getint(type) & 7;
4894 break;
4895 case 279: /* StripByteCounts */
4896 case 514:
4897 tiff_ifd[ifd].bytes = get4();
4898 break;
4899 case 305: case 11: /* Software */
4900 fgets (software, 64, ifp);
4901 if (!strncmp(software,"Adobe",5) ||
4902 !strncmp(software,"dcraw",5) ||
4903 !strncmp(software,"UFRaw",5) ||
4904 !strncmp(software,"Bibble",6) ||
4905 !strncmp(software,"Nikon Scan",10) ||
4906 !strcmp (software,"Digital Photo Professional"))
4907 is_raw = 0;
4908 break;
4909 case 306: /* DateTime */
4910 get_timestamp(0);
4911 break;
4912 case 315: /* Artist */
4913 fread (artist, 64, 1, ifp);
4914 break;
4915 case 322: /* TileWidth */
4916 tile_width = getint(type);
4917 break;
4918 case 323: /* TileLength */
4919 tile_length = getint(type);
4920 break;
4921 case 324: /* TileOffsets */
4922 tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
4923 if (len == 4) {
4924 load_raw = &CLASS sinar_4shot_load_raw;
4925 is_raw = 5;
4927 break;
4928 case 330: /* SubIFDs */
4929 if (!strcmp(model,"DSLR-A100") && tiff_ifd[ifd].t_width == 3872) {
4930 load_raw = &CLASS sony_arw_load_raw;
4931 data_offset = get4()+base;
4932 ifd++; break;
4934 while (len--) {
4935 i = ftell(ifp);
4936 fseek (ifp, get4()+base, SEEK_SET);
4937 if (parse_tiff_ifd (base)) break;
4938 fseek (ifp, i+4, SEEK_SET);
4940 break;
4941 case 400:
4942 strcpy (make, "Sarnoff");
4943 maximum = 0xfff;
4944 break;
4945 case 28688:
4946 FORC4 sony_curve[c+1] = get2() >> 2 & 0xfff;
4947 for (i=0; i < 5; i++)
4948 for (j = sony_curve[i]+1; j <= sony_curve[i+1]; j++)
4949 curve[j] = curve[j-1] + (1 << i);
4950 #ifdef LIBRAW_LIBRARY_BUILD
4951 color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
4952 #endif
4953 break;
4954 case 29184: sony_offset = get4(); break;
4955 case 29185: sony_length = get4(); break;
4956 case 29217: sony_key = get4(); break;
4957 case 29264:
4958 parse_minolta (ftell(ifp));
4959 raw_width = 0;
4960 break;
4961 case 29443:
4962 FORC4 cam_mul[c ^ (c < 2)] = get2();
4963 #ifdef LIBRAW_LIBRARY_BUILD
4964 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4965 #endif
4966 break;
4967 case 29459:
4968 FORC4 cam_mul[c ^ (c >> 1)] = get2();
4969 #ifdef LIBRAW_LIBRARY_BUILD
4970 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
4971 #endif
4972 break;
4973 case 33405: /* Model2 */
4974 fgets (model2, 64, ifp);
4975 break;
4976 case 33422: /* CFAPattern */
4977 case 64777: /* Kodak P-series */
4978 if ((plen=len) > 16) plen = 16;
4979 fread (cfa_pat, 1, plen, ifp);
4980 for (colors=cfa=i=0; i < plen; i++) {
4981 colors += !(cfa & (1 << cfa_pat[i]));
4982 cfa |= 1 << cfa_pat[i];
4984 if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
4985 if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
4986 goto guess_cfa_pc;
4987 case 33424:
4988 fseek (ifp, get4()+base, SEEK_SET);
4989 parse_kodak_ifd (base);
4990 break;
4991 case 33434: /* ExposureTime */
4992 shutter = getreal(type);
4993 break;
4994 case 33437: /* FNumber */
4995 aperture = getreal(type);
4996 break;
4997 case 34306: /* Leaf white balance */
4998 FORC4 cam_mul[c ^ 1] = 4096.0 / get2();
4999 #ifdef LIBRAW_LIBRARY_BUILD
5000 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5001 #endif
5002 break;
5003 case 34307: /* Leaf CatchLight color matrix */
5004 fread (software, 1, 7, ifp);
5005 if (strncmp(software,"MATRIX",6)) break;
5006 colors = 4;
5007 for (raw_color = i=0; i < 3; i++) {
5008 FORC4 fscanf (ifp, "%f", &rgb_cam[i][c^1]);
5009 if (!use_camera_wb) continue;
5010 num = 0;
5011 FORC4 num += rgb_cam[i][c];
5012 FORC4 rgb_cam[i][c] /= num;
5013 #ifdef LIBRAW_LIBRARY_BUILD
5014 color_flags.rgb_cam_state = LIBRAW_COLORSTATE_LOADED;
5015 #endif
5017 break;
5018 case 34310: /* Leaf metadata */
5019 parse_mos (ftell(ifp));
5020 case 34303:
5021 strcpy (make, "Leaf");
5022 break;
5023 case 34665: /* EXIF tag */
5024 fseek (ifp, get4()+base, SEEK_SET);
5025 parse_exif (base);
5026 break;
5027 case 34853: /* GPSInfo tag */
5028 fseek (ifp, get4()+base, SEEK_SET);
5029 parse_gps (base);
5030 break;
5031 case 34675: /* InterColorProfile */
5032 case 50831: /* AsShotICCProfile */
5033 profile_offset = ftell(ifp);
5034 profile_length = len;
5035 break;
5036 case 37122: /* CompressedBitsPerPixel */
5037 kodak_cbpp = get4();
5038 break;
5039 case 37386: /* FocalLength */
5040 focal_len = getreal(type);
5041 break;
5042 case 37393: /* ImageNumber */
5043 shot_order = getint(type);
5044 break;
5045 case 37400: /* old Kodak KDC tag */
5046 for (raw_color = i=0; i < 3; i++) {
5047 getreal(type);
5048 FORC3 rgb_cam[i][c] = getreal(type);
5050 #ifdef LIBRAW_LIBRARY_BUILD
5051 color_flags.rgb_cam_state = LIBRAW_COLORSTATE_LOADED;
5052 #endif
5053 break;
5054 case 46275: /* Imacon tags */
5055 strcpy (make, "Imacon");
5056 data_offset = ftell(ifp);
5057 ima_len = len;
5058 break;
5059 case 46279:
5060 if (!ima_len) break;
5061 fseek (ifp, 78, SEEK_CUR);
5062 raw_width = get4();
5063 raw_height = get4();
5064 left_margin = get4() & 7;
5065 width = raw_width - left_margin - (get4() & 7);
5066 top_margin = get4() & 7;
5067 height = raw_height - top_margin - (get4() & 7);
5068 if (raw_width == 7262) {
5069 height = 5444;
5070 width = 7244;
5071 left_margin = 7;
5073 fseek (ifp, 52, SEEK_CUR);
5074 FORC3 cam_mul[c] = getreal(11);
5075 #ifdef LIBRAW_LIBRARY_BUILD
5076 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5077 #endif
5078 fseek (ifp, 114, SEEK_CUR);
5079 flip = (get2() >> 7) * 90;
5080 if (width * height * 6 == ima_len) {
5081 if (flip % 180 == 90) SWAP(width,height);
5082 filters = flip = 0;
5084 sprintf (model, "Ixpress %d-Mp", height*width/1000000);
5085 load_raw = &CLASS imacon_full_load_raw;
5086 if (filters) {
5087 if (left_margin & 1) filters = 0x61616161;
5088 load_raw = &CLASS unpacked_load_raw;
5090 maximum = 0xffff;
5091 break;
5092 case 50454: /* Sinar tag */
5093 case 50455:
5094 if (!(cbuf = (char *) malloc(len))) break;
5095 fread (cbuf, 1, len, ifp);
5096 for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
5097 if (!strncmp (++cp,"Neutral ",8))
5099 sscanf (cp+8, "%f %f %f", cam_mul, cam_mul+1, cam_mul+2);
5100 #ifdef LIBRAW_LIBRARY_BUILD
5101 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5102 #endif
5104 free (cbuf);
5105 break;
5106 case 50458:
5107 if (!make[0]) strcpy (make, "Hasselblad");
5108 break;
5109 case 50459: /* Hasselblad tag */
5110 i = order;
5111 j = ftell(ifp);
5112 c = tiff_nifds;
5113 order = get2();
5114 fseek (ifp, j+(get2(),get4()), SEEK_SET);
5115 parse_tiff_ifd (j);
5116 maximum = 0xffff;
5117 tiff_nifds = c;
5118 order = i;
5119 break;
5120 case 50706: /* DNGVersion */
5121 FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
5122 if (!make[0]) strcpy (make, "DNG");
5123 is_raw = 1;
5124 break;
5125 case 50710: /* CFAPlaneColor */
5126 if (len > 4) len = 4;
5127 colors = len;
5128 fread (cfa_pc, 1, colors, ifp);
5129 guess_cfa_pc:
5130 FORCC tab[cfa_pc[c]] = c;
5131 cdesc[c] = 0;
5132 for (i=16; i--; )
5133 filters = filters << 2 | tab[cfa_pat[i % plen]];
5134 break;
5135 case 50711: /* CFALayout */
5136 if (get2() == 2) {
5137 fuji_width = 1;
5138 filters = 0x49494949;
5140 break;
5141 case 291:
5142 case 50712: /* LinearizationTable */
5143 linear_table (len);
5144 break;
5145 case 50714: /* BlackLevel */
5146 case 50715: /* BlackLevelDeltaH */
5147 case 50716: /* BlackLevelDeltaV */
5148 for (dblack=i=0; i < len; i++)
5149 dblack += getreal(type);
5150 black += dblack/len + 0.5;
5151 break;
5152 case 50717: /* WhiteLevel */
5153 maximum = getint(type);
5154 break;
5155 case 50718: /* DefaultScale */
5156 pixel_aspect = getreal(type);
5157 pixel_aspect /= getreal(type);
5158 break;
5159 case 50721: /* ColorMatrix1 */
5160 case 50722: /* ColorMatrix2 */
5161 FORCC for (j=0; j < 3; j++)
5162 cm[c][j] = getreal(type);
5163 use_cm = 1;
5164 break;
5165 case 50723: /* CameraCalibration1 */
5166 case 50724: /* CameraCalibration2 */
5167 for (i=0; i < colors; i++)
5168 FORCC cc[i][c] = getreal(type);
5169 case 50727: /* AnalogBalance */
5170 FORCC ab[c] = getreal(type);
5171 break;
5172 case 50728: /* AsShotNeutral */
5173 FORCC asn[c] = getreal(type);
5174 break;
5175 case 50729: /* AsShotWhiteXY */
5176 xyz[0] = getreal(type);
5177 xyz[1] = getreal(type);
5178 xyz[2] = 1 - xyz[0] - xyz[1];
5179 FORC3 xyz[c] /= d65_white[c];
5180 break;
5181 case 50740: /* DNGPrivateData */
5182 if (dng_version) break;
5183 parse_minolta (j = get4()+base);
5184 fseek (ifp, j, SEEK_SET);
5185 parse_tiff_ifd (base);
5186 break;
5187 case 50752:
5188 read_shorts (cr2_slice, 3);
5189 break;
5190 case 50829: /* ActiveArea */
5191 top_margin = getint(type);
5192 left_margin = getint(type);
5193 height = getint(type) - top_margin;
5194 width = getint(type) - left_margin;
5195 break;
5196 case 64772: /* Kodak P-series */
5197 fseek (ifp, 16, SEEK_CUR);
5198 data_offset = get4();
5199 fseek (ifp, 28, SEEK_CUR);
5200 data_offset += get4();
5201 load_raw = &CLASS packed_12_load_raw;
5203 fseek (ifp, save, SEEK_SET);
5205 if (sony_length && (buf = (unsigned *) malloc(sony_length))) {
5206 fseek (ifp, sony_offset, SEEK_SET);
5207 fread (buf, sony_length, 1, ifp);
5208 sony_decrypt (buf, sony_length/4, 1, sony_key);
5209 #ifndef LIBRAW_LIBRARY_BUILD
5210 sfp = ifp;
5211 if ((ifp = tmpfile())) {
5212 fwrite (buf, sony_length, 1, ifp);
5213 fseek (ifp, 0, SEEK_SET);
5214 parse_tiff_ifd (-sony_offset);
5215 fclose (ifp);
5217 ifp = sfp;
5218 #else
5219 if( !ifp->tempbuffer_open(buf,sony_length))
5221 parse_tiff_ifd(-sony_offset);
5222 ifp->tempbuffer_close();
5224 #endif
5225 free (buf);
5227 for (i=0; i < colors; i++)
5228 FORCC cc[i][c] *= ab[i];
5229 if (use_cm) {
5230 FORCC for (i=0; i < 3; i++)
5231 for (cam_xyz[c][i]=j=0; j < colors; j++)
5232 cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
5233 cam_xyz_coeff (cam_xyz);
5235 if (asn[0]) {
5236 cam_mul[3] = 0;
5237 FORCC cam_mul[c] = 1 / asn[c];
5238 #ifdef LIBRAW_LIBRARY_BUILD
5239 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5240 #endif
5242 if (!use_cm)
5244 FORCC pre_mul[c] /= cc[c][c];
5245 #ifdef LIBRAW_LIBRARY_BUILD
5246 color_flags.pre_mul_state = LIBRAW_COLORSTATE_LOADED;
5247 #endif
5250 return 0;
5253 void CLASS parse_tiff (int base)
5255 int doff, max_samp=0, raw=-1, thm=-1, i;
5256 struct jhead jh;
5258 fseek (ifp, base, SEEK_SET);
5259 order = get2();
5260 if (order != 0x4949 && order != 0x4d4d) return;
5261 get2();
5262 memset (tiff_ifd, 0, sizeof tiff_ifd);
5263 tiff_nifds = 0;
5264 while ((doff = get4())) {
5265 fseek (ifp, doff+base, SEEK_SET);
5266 if (parse_tiff_ifd (base)) break;
5268 thumb_misc = 16;
5269 if (thumb_offset) {
5270 fseek (ifp, thumb_offset, SEEK_SET);
5271 if (ljpeg_start (&jh, 1)) {
5272 thumb_misc = jh.bits;
5273 thumb_width = jh.wide;
5274 thumb_height = jh.high;
5277 for (i=0; i < tiff_nifds; i++) {
5278 if (max_samp < tiff_ifd[i].samples)
5279 max_samp = tiff_ifd[i].samples;
5280 if (max_samp > 3) max_samp = 3;
5281 if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
5282 tiff_ifd[i].t_width*tiff_ifd[i].t_height > raw_width*raw_height) {
5283 raw_width = tiff_ifd[i].t_width;
5284 raw_height = tiff_ifd[i].t_height;
5285 tiff_bps = tiff_ifd[i].bps;
5286 tiff_compress = tiff_ifd[i].comp;
5287 data_offset = tiff_ifd[i].offset;
5288 tiff_flip = tiff_ifd[i].t_flip;
5289 tiff_samples = tiff_ifd[i].samples;
5290 raw = i;
5293 fuji_width *= (raw_width+1)/2;
5294 if (tiff_ifd[0].t_flip) tiff_flip = tiff_ifd[0].t_flip;
5295 if (raw >= 0 && !load_raw)
5296 switch (tiff_compress) {
5297 case 0: case 1:
5298 switch (tiff_bps) {
5299 case 8: load_raw = &CLASS eight_bit_load_raw; break;
5300 case 12: load_raw = &CLASS packed_12_load_raw;
5301 if (tiff_ifd[raw].phint == 2)
5302 load_flags = 6;
5303 if (strncmp(make,"PENTAX",6)) break;
5304 case 14:
5305 case 16: load_raw = &CLASS unpacked_load_raw; break;
5307 if (tiff_ifd[raw].bytes*5 == raw_width*raw_height*8)
5308 load_raw = &CLASS olympus_e300_load_raw;
5309 break;
5310 case 6: case 7: case 99:
5311 load_raw = &CLASS lossless_jpeg_load_raw; break;
5312 case 262:
5313 load_raw = &CLASS kodak_262_load_raw; break;
5314 case 32767:
5315 load_raw = &CLASS sony_arw2_load_raw;
5316 if (tiff_ifd[raw].bytes*8 == raw_width*raw_height*tiff_bps)
5317 break;
5318 raw_height += 8;
5319 load_raw = &CLASS sony_arw_load_raw; break;
5320 case 32769:
5321 load_flags = 8;
5322 case 32773:
5323 load_raw = &CLASS packed_12_load_raw; break;
5324 case 34713:
5325 load_raw = &CLASS nikon_compressed_load_raw; break;
5326 case 65535:
5327 load_raw = &CLASS pentax_k10_load_raw; break;
5328 case 65000:
5329 switch (tiff_ifd[raw].phint) {
5330 case 2: load_raw = &CLASS kodak_rgb_load_raw; filters = 0; break;
5331 case 6: load_raw = &CLASS kodak_ycbcr_load_raw; filters = 0; break;
5332 case 32803: load_raw = &CLASS kodak_65000_load_raw;
5334 case 32867: break;
5335 default: is_raw = 0;
5337 if (!dng_version && tiff_samples == 3)
5338 if (tiff_ifd[raw].bytes && tiff_bps != 14 && tiff_bps != 2048)
5339 is_raw = 0;
5340 if (!dng_version && tiff_bps == 8 && tiff_compress == 1 &&
5341 tiff_ifd[raw].phint == 1) is_raw = 0;
5342 if (tiff_bps == 8 && tiff_samples == 4) is_raw = 0;
5343 for (i=0; i < tiff_nifds; i++)
5344 if (i != raw && tiff_ifd[i].samples == max_samp &&
5345 tiff_ifd[i].t_width * tiff_ifd[i].t_height / SQR(tiff_ifd[i].bps+1) >
5346 thumb_width * thumb_height / SQR(thumb_misc+1)) {
5347 thumb_width = tiff_ifd[i].t_width;
5348 thumb_height = tiff_ifd[i].t_height;
5349 thumb_offset = tiff_ifd[i].offset;
5350 thumb_length = tiff_ifd[i].bytes;
5351 thumb_misc = tiff_ifd[i].bps;
5352 thm = i;
5354 if (thm >= 0) {
5355 thumb_misc |= tiff_ifd[thm].samples << 5;
5356 switch (tiff_ifd[thm].comp) {
5357 case 0:
5358 write_thumb = &CLASS layer_thumb;
5359 break;
5360 case 1:
5361 if (tiff_ifd[thm].bps > 8)
5362 thumb_load_raw = &CLASS kodak_thumb_load_raw;
5363 else
5364 write_thumb = &CLASS ppm_thumb;
5365 break;
5366 case 65000:
5367 thumb_load_raw = tiff_ifd[thm].phint == 6 ?
5368 &CLASS kodak_ycbcr_load_raw : &CLASS kodak_rgb_load_raw;
5373 void CLASS parse_minolta (int base)
5375 int save, tag, len, offset, high=0, wide=0, i, c;
5376 short sorder=order;
5378 fseek (ifp, base, SEEK_SET);
5379 if (fgetc(ifp) || fgetc(ifp)-'M' || fgetc(ifp)-'R') return;
5380 order = fgetc(ifp) * 0x101;
5381 offset = base + get4() + 8;
5382 while ((save=ftell(ifp)) < offset) {
5383 for (tag=i=0; i < 4; i++)
5384 tag = tag << 8 | fgetc(ifp);
5385 len = get4();
5386 switch (tag) {
5387 case 0x505244: /* PRD */
5388 fseek (ifp, 8, SEEK_CUR);
5389 high = get2();
5390 wide = get2();
5391 break;
5392 case 0x574247: /* WBG */
5393 get4();
5394 i = strcmp(model,"DiMAGE A200") ? 0:3;
5395 FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
5396 #ifdef LIBRAW_LIBRARY_BUILD
5397 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5398 #endif
5399 break;
5400 case 0x545457: /* TTW */
5401 parse_tiff (ftell(ifp));
5402 data_offset = offset;
5404 fseek (ifp, save+len+8, SEEK_SET);
5406 raw_height = high;
5407 raw_width = wide;
5408 order = sorder;
5412 Many cameras have a "debug mode" that writes JPEG and raw
5413 at the same time. The raw file has no header, so try to
5414 to open the matching JPEG file and read its metadata.
5416 void CLASS parse_external_jpeg()
5418 char *file, *ext, *jname, *jfile, *jext;
5419 #ifndef LIBRAW_LIBRARY_BUILD
5420 FILE *save=ifp;
5421 #else
5422 if(!ifp->fname())
5424 imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
5425 return;
5427 #endif
5429 ext = strrchr (ifname, '.');
5430 file = strrchr (ifname, '/');
5431 if (!file) file = strrchr (ifname, '\\');
5432 #ifndef LIBRAW_LIBRARY_BUILD
5433 if (!file) file = ifname-1;
5434 #else
5435 if (!file) file = (char*)ifname-1;
5436 #endif
5437 file++;
5438 if (!ext || strlen(ext) != 4 || ext-file != 8) return;
5439 jname = (char *) malloc (strlen(ifname) + 1);
5440 merror (jname, "parse_external_jpeg()");
5441 strcpy (jname, ifname);
5442 jfile = file - ifname + jname;
5443 jext = ext - ifname + jname;
5444 if (strcasecmp (ext, ".jpg")) {
5445 strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
5446 if (isdigit(*file)) {
5447 memcpy (jfile, file+4, 4);
5448 memcpy (jfile+4, file, 4);
5450 } else
5451 while (isdigit(*--jext)) {
5452 if (*jext != '9') {
5453 (*jext)++;
5454 break;
5456 *jext = '0';
5458 #ifndef LIBRAW_LIBRARY_BUILD
5459 if (strcmp (jname, ifname)) {
5460 if ((ifp = fopen (jname, "rb"))) {
5461 #ifdef DCRAW_VERBOSE
5462 if (verbose)
5463 fprintf (stderr,_("Reading metadata from %s ...\n"), jname);
5464 #endif
5465 parse_tiff (12);
5466 thumb_offset = 0;
5467 is_raw = 1;
5468 fclose (ifp);
5471 #else
5472 if (strcmp (jname, ifname))
5474 if(!ifp->subfile_open(jname))
5476 parse_tiff (12);
5477 thumb_offset = 0;
5478 is_raw = 1;
5479 ifp->subfile_close();
5481 else
5482 imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
5484 #endif
5485 if (!timestamp)
5487 #ifdef LIBRAW_LIBRARY_BUILD
5488 imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA ;
5489 #endif
5490 #ifdef DCRAW_VERBOSE
5491 fprintf (stderr,_("Failed to read metadata from %s\n"), jname);
5492 #endif
5494 free (jname);
5495 #ifndef LIBRAW_LIBRARY_BUILD
5496 ifp = save;
5497 #endif
5501 CIFF block 0x1030 contains an 8x8 white sample.
5502 Load this into white[][] for use in scale_colors().
5504 void CLASS ciff_block_1030()
5506 static const ushort key[] = { 0x410, 0x45f3 };
5507 int i, bpp, row, col, vbits=0;
5508 unsigned long bitbuf=0;
5510 if ((get2(),get4()) != 0x80008 || !get4()) return;
5511 bpp = get2();
5512 if (bpp != 10 && bpp != 12) return;
5513 for (i=row=0; row < 8; row++)
5514 for (col=0; col < 8; col++) {
5515 if (vbits < bpp) {
5516 bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
5517 vbits += 16;
5519 white[row][col] =
5520 bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp);
5521 vbits -= bpp;
5526 Parse a CIFF file, better known as Canon CRW format.
5528 void CLASS parse_ciff (int offset, int length)
5530 int tboff, nrecs, c, type, len, save, wbi=-1;
5531 ushort key[] = { 0x410, 0x45f3 };
5533 fseek (ifp, offset+length-4, SEEK_SET);
5534 tboff = get4() + offset;
5535 fseek (ifp, tboff, SEEK_SET);
5536 nrecs = get2();
5537 if (nrecs > 100) return;
5538 while (nrecs--) {
5539 type = get2();
5540 len = get4();
5541 save = ftell(ifp) + 4;
5542 fseek (ifp, offset+get4(), SEEK_SET);
5543 if ((((type >> 8) + 8) | 8) == 0x38)
5544 parse_ciff (ftell(ifp), len); /* Parse a sub-table */
5546 if (type == 0x0810)
5547 fread (artist, 64, 1, ifp);
5548 if (type == 0x080a) {
5549 fread (make, 64, 1, ifp);
5550 fseek (ifp, strlen(make) - 63, SEEK_CUR);
5551 fread (model, 64, 1, ifp);
5553 if (type == 0x1810) {
5554 fseek (ifp, 12, SEEK_CUR);
5555 flip = get4();
5557 if (type == 0x1835) /* Get the decoder table */
5558 tiff_compress = get4();
5559 if (type == 0x2007) {
5560 thumb_offset = ftell(ifp);
5561 thumb_length = len;
5563 if (type == 0x1818) {
5564 shutter = pow (2.0f, -int_to_float((get4(),get4())));
5565 aperture = pow (2.0f, int_to_float(get4())/2);
5567 if (type == 0x102a) {
5568 iso_speed = pow (2, (get4(),get2())/32.0 - 4) * 50;
5569 aperture = pow (2, (get2(),(short)get2())/64.0);
5570 shutter = pow (2,-((short)get2())/32.0);
5571 wbi = (get2(),get2());
5572 if (wbi > 17) wbi = 0;
5573 fseek (ifp, 32, SEEK_CUR);
5574 if (shutter > 1e6) shutter = get2()/10.0;
5576 if (type == 0x102c) {
5577 if (get2() > 512) { /* Pro90, G1 */
5578 fseek (ifp, 118, SEEK_CUR);
5579 FORC4 cam_mul[c ^ 2] = get2();
5580 #ifdef LIBRAW_LIBRARY_BUILD
5581 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5582 #endif
5583 } else { /* G2, S30, S40 */
5584 fseek (ifp, 98, SEEK_CUR);
5585 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2();
5586 #ifdef LIBRAW_LIBRARY_BUILD
5587 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5588 #endif
5591 if (type == 0x0032) {
5592 if (len == 768) { /* EOS D30 */
5593 fseek (ifp, 72, SEEK_CUR);
5594 FORC4 cam_mul[c ^ (c >> 1)] = 1024.0 / get2();
5595 #ifdef LIBRAW_LIBRARY_BUILD
5596 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5597 #endif
5598 if (!wbi) cam_mul[0] = -1; /* use my auto white balance */
5599 } else if (!cam_mul[0]) {
5600 if (get2() == key[0]) /* Pro1, G6, S60, S70 */
5601 c = (strstr(model,"Pro1") ?
5602 "012346000000000000":"01345:000000006008")[wbi]-'0'+ 2;
5603 else { /* G3, G5, S45, S50 */
5604 c = "023457000000006000"[wbi]-'0';
5605 key[0] = key[1] = 0;
5607 fseek (ifp, 78 + c*8, SEEK_CUR);
5608 FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2() ^ key[c & 1];
5609 #ifdef LIBRAW_LIBRARY_BUILD
5610 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5611 #endif
5612 if (!wbi) cam_mul[0] = -1;
5615 if (type == 0x10a9) { /* D60, 10D, 300D, and clones */
5616 if (len > 66) wbi = "0134567028"[wbi]-'0';
5617 fseek (ifp, 2 + wbi*8, SEEK_CUR);
5618 FORC4 cam_mul[c ^ (c >> 1)] = get2();
5619 #ifdef LIBRAW_LIBRARY_BUILD
5620 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5621 #endif
5623 if (type == 0x1030 && (0x18040 >> wbi & 1))
5624 ciff_block_1030(); /* all that don't have 0x10a9 */
5625 if (type == 0x1031) {
5626 raw_width = (get2(),get2());
5627 raw_height = get2();
5629 if (type == 0x5029) {
5630 focal_len = len >> 16;
5631 if ((len & 0xffff) == 2) focal_len /= 32;
5633 if (type == 0x5813) flash_used = int_to_float(len);
5634 if (type == 0x5814) canon_ev = int_to_float(len);
5635 if (type == 0x5817) shot_order = len;
5636 if (type == 0x5834) unique_id = len;
5637 if (type == 0x580e) timestamp = len;
5638 if (type == 0x180e) timestamp = get4();
5639 #ifdef LOCALTIME
5640 if ((type | 0x4000) == 0x580e)
5641 timestamp = mktime (gmtime (&timestamp));
5642 #endif
5643 fseek (ifp, save, SEEK_SET);
5647 void CLASS parse_rollei()
5649 char line[128], *val;
5650 struct tm t;
5652 fseek (ifp, 0, SEEK_SET);
5653 memset (&t, 0, sizeof t);
5654 do {
5655 fgets (line, 128, ifp);
5656 if ((val = strchr(line,'=')))
5657 *val++ = 0;
5658 else
5659 val = line + strlen(line);
5660 if (!strcmp(line,"DAT"))
5661 sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
5662 if (!strcmp(line,"TIM"))
5663 sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
5664 if (!strcmp(line,"HDR"))
5665 thumb_offset = atoi(val);
5666 if (!strcmp(line,"X "))
5667 raw_width = atoi(val);
5668 if (!strcmp(line,"Y "))
5669 raw_height = atoi(val);
5670 if (!strcmp(line,"TX "))
5671 thumb_width = atoi(val);
5672 if (!strcmp(line,"TY "))
5673 thumb_height = atoi(val);
5674 } while (strncmp(line,"EOHD",4));
5675 data_offset = thumb_offset + thumb_width * thumb_height * 2;
5676 t.tm_year -= 1900;
5677 t.tm_mon -= 1;
5678 if (mktime(&t) > 0)
5679 timestamp = mktime(&t);
5680 strcpy (make, "Rollei");
5681 strcpy (model,"d530flex");
5682 write_thumb = &CLASS rollei_thumb;
5685 void CLASS parse_sinar_ia()
5687 int entries, off;
5688 char str[8], *cp;
5690 order = 0x4949;
5691 fseek (ifp, 4, SEEK_SET);
5692 entries = get4();
5693 fseek (ifp, get4(), SEEK_SET);
5694 while (entries--) {
5695 off = get4(); get4();
5696 fread (str, 8, 1, ifp);
5697 if (!strcmp(str,"META")) meta_offset = off;
5698 if (!strcmp(str,"THUMB")) thumb_offset = off;
5699 if (!strcmp(str,"RAW0")) data_offset = off;
5701 fseek (ifp, meta_offset+20, SEEK_SET);
5702 fread (make, 64, 1, ifp);
5703 make[63] = 0;
5704 if ((cp = strchr(make,' '))) {
5705 strcpy (model, cp+1);
5706 *cp = 0;
5708 raw_width = get2();
5709 raw_height = get2();
5710 load_raw = &CLASS unpacked_load_raw;
5711 thumb_width = (get4(),get2());
5712 thumb_height = get2();
5713 write_thumb = &CLASS ppm_thumb;
5714 maximum = 0x3fff;
5717 void CLASS parse_phase_one (int base)
5719 unsigned entries, tag, type, len, data, save, i, c;
5720 float romm_cam[3][3];
5721 char *cp;
5723 memset (&ph1, 0, sizeof ph1);
5724 fseek (ifp, base, SEEK_SET);
5725 order = get4() & 0xffff;
5726 if (get4() >> 8 != 0x526177) return; /* "Raw" */
5727 fseek (ifp, get4()+base, SEEK_SET);
5728 entries = get4();
5729 get4();
5730 while (entries--) {
5731 tag = get4();
5732 type = get4();
5733 len = get4();
5734 data = get4();
5735 save = ftell(ifp);
5736 fseek (ifp, base+data, SEEK_SET);
5737 switch (tag) {
5738 case 0x100: flip = "0653"[data & 3]-'0'; break;
5739 case 0x106:
5740 for (i=0; i < 9; i++)
5741 romm_cam[0][i] = getreal(11);
5742 romm_coeff (romm_cam);
5743 break;
5744 case 0x107:
5745 FORC3 cam_mul[c] = getreal(11);
5746 #ifdef LIBRAW_LIBRARY_BUILD
5747 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5748 #endif
5749 break;
5750 case 0x108: raw_width = data; break;
5751 case 0x109: raw_height = data; break;
5752 case 0x10a: left_margin = data; break;
5753 case 0x10b: top_margin = data; break;
5754 case 0x10c: width = data; break;
5755 case 0x10d: height = data; break;
5756 case 0x10e: ph1.format = data; break;
5757 case 0x10f: data_offset = data+base; break;
5758 case 0x110: meta_offset = data+base;
5759 meta_length = len; break;
5760 case 0x112: ph1.key_off = save - 4; break;
5761 case 0x210: ph1.tag_210 = int_to_float(data); break;
5762 case 0x21a: ph1.tag_21a = data; break;
5763 case 0x21c: strip_offset = data+base; break;
5764 case 0x21d: ph1.t_black = data; break;
5765 case 0x222: ph1.split_col = data - left_margin; break;
5766 case 0x223: ph1.black_off = data+base; break;
5767 case 0x301:
5768 model[63] = 0;
5769 fread (model, 1, 63, ifp);
5770 if ((cp = strstr(model," camera"))) *cp = 0;
5772 fseek (ifp, save, SEEK_SET);
5774 load_raw = ph1.format < 3 ?
5775 &CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
5776 maximum = 0xffff;
5777 strcpy (make, "Phase One");
5778 if (model[0]) return;
5779 switch (raw_height) {
5780 case 2060: strcpy (model,"LightPhase"); break;
5781 case 2682: strcpy (model,"H 10"); break;
5782 case 4128: strcpy (model,"H 20"); break;
5783 case 5488: strcpy (model,"H 25"); break;
5787 void CLASS parse_fuji (int offset)
5789 unsigned entries, tag, len, save, c;
5791 fseek (ifp, offset, SEEK_SET);
5792 entries = get4();
5793 if (entries > 255) return;
5794 while (entries--) {
5795 tag = get2();
5796 len = get2();
5797 save = ftell(ifp);
5798 if (tag == 0x100) {
5799 raw_height = get2();
5800 raw_width = get2();
5801 } else if (tag == 0x121) {
5802 height = get2();
5803 if ((width = get2()) == 4284) width += 3;
5804 } else if (tag == 0x130)
5805 fuji_layout = fgetc(ifp) >> 7;
5806 if (tag == 0x2ff0)
5808 FORC4 cam_mul[c ^ 1] = get2();
5809 #ifdef LIBRAW_LIBRARY_BUILD
5810 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5811 #endif
5813 fseek (ifp, save+len, SEEK_SET);
5815 height <<= fuji_layout;
5816 width >>= fuji_layout;
5819 int CLASS parse_jpeg (int offset)
5821 int len, save, hlen, mark;
5823 fseek (ifp, offset, SEEK_SET);
5824 if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
5826 while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
5827 order = 0x4d4d;
5828 len = get2() - 2;
5829 save = ftell(ifp);
5830 if (mark == 0xc0 || mark == 0xc3) {
5831 fgetc(ifp);
5832 raw_height = get2();
5833 raw_width = get2();
5835 order = get2();
5836 hlen = get4();
5837 if (get4() == 0x48454150) /* "HEAP" */
5838 parse_ciff (save+hlen, len-hlen);
5839 parse_tiff (save+6);
5840 fseek (ifp, save+len, SEEK_SET);
5842 return 1;
5845 void CLASS parse_riff()
5847 unsigned i, size, end;
5848 char tag[4], date[64], month[64];
5849 static const char mon[12][4] =
5850 { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
5851 struct tm t;
5853 order = 0x4949;
5854 fread (tag, 4, 1, ifp);
5855 size = get4();
5856 end = ftell(ifp) + size;
5857 if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
5858 get4();
5859 while (ftell(ifp)+7 < end)
5860 parse_riff();
5861 } else if (!memcmp(tag,"nctg",4)) {
5862 while (ftell(ifp)+7 < end) {
5863 i = get2();
5864 size = get2();
5865 if ((i+1) >> 1 == 10 && size == 20)
5866 get_timestamp(0);
5867 else fseek (ifp, size, SEEK_CUR);
5869 } else if (!memcmp(tag,"IDIT",4) && size < 64) {
5870 fread (date, 64, 1, ifp);
5871 date[size] = 0;
5872 memset (&t, 0, sizeof t);
5873 if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
5874 &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
5875 for (i=0; i < 12 && strcasecmp(mon[i],month); i++);
5876 t.tm_mon = i;
5877 t.tm_year -= 1900;
5878 if (mktime(&t) > 0)
5879 timestamp = mktime(&t);
5881 } else
5882 fseek (ifp, size, SEEK_CUR);
5885 void CLASS parse_smal (int offset, int fsize)
5887 int ver;
5889 fseek (ifp, offset+2, SEEK_SET);
5890 order = 0x4949;
5891 ver = fgetc(ifp);
5892 if (ver == 6)
5893 fseek (ifp, 5, SEEK_CUR);
5894 if (get4() != fsize) return;
5895 if (ver > 6) data_offset = get4();
5896 raw_height = height = get2();
5897 raw_width = width = get2();
5898 strcpy (make, "SMaL");
5899 sprintf (model, "v%d %dx%d", ver, width, height);
5900 if (ver == 6) load_raw = &CLASS smal_v6_load_raw;
5901 if (ver == 9) load_raw = &CLASS smal_v9_load_raw;
5904 void CLASS parse_cine()
5906 unsigned off_head, off_setup, off_image, i;
5908 order = 0x4949;
5909 fseek (ifp, 4, SEEK_SET);
5910 is_raw = get2() == 2;
5911 fseek (ifp, 14, SEEK_CUR);
5912 is_raw *= get4();
5913 off_head = get4();
5914 off_setup = get4();
5915 off_image = get4();
5916 timestamp = get4();
5917 if ((i = get4())) timestamp = i;
5918 fseek (ifp, off_head+4, SEEK_SET);
5919 raw_width = get4();
5920 raw_height = get4();
5921 switch (get2(),get2()) {
5922 case 8: load_raw = &CLASS eight_bit_load_raw; break;
5923 case 16: load_raw = &CLASS unpacked_load_raw;
5925 fseek (ifp, off_setup+792, SEEK_SET);
5926 strcpy (make, "CINE");
5927 sprintf (model, "%d", get4());
5928 fseek (ifp, 12, SEEK_CUR);
5929 switch ((i=get4()) & 0xffffff) {
5930 case 3: filters = 0x94949494; break;
5931 case 4: filters = 0x49494949; break;
5932 default: is_raw = 0;
5934 fseek (ifp, 72, SEEK_CUR);
5935 switch ((get4()+3600) % 360) {
5936 case 270: flip = 4; break;
5937 case 180: flip = 1; break;
5938 case 90: flip = 7; break;
5939 case 0: flip = 2;
5941 cam_mul[0] = getreal(11);
5942 cam_mul[2] = getreal(11);
5943 #ifdef LIBRAW_LIBRARY_BUILD
5944 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
5945 #endif
5946 maximum = ~(-1 << get4());
5947 fseek (ifp, 668, SEEK_CUR);
5948 shutter = get4()/1000000000.0;
5949 fseek (ifp, off_image, SEEK_SET);
5950 if (shot_select < is_raw)
5951 fseek (ifp, shot_select*8, SEEK_CUR);
5952 data_offset = (INT64) get4() + 8;
5953 data_offset += (INT64) get4() << 32;
5955 #line 7229 "dcraw/dcraw.c"
5956 #ifdef LIBRAW_LIBRARY_BUILD
5957 void CLASS adobe_coeff (const char *p_make, const char *p_model)
5958 #else
5959 void CLASS adobe_coeff (char *p_make, char *p_model)
5960 #endif
5962 static const struct {
5963 const char *prefix;
5964 short t_black, t_maximum, trans[12];
5965 } table[] = {
5966 { "Apple QuickTake", 0, 0, /* DJC */
5967 { 17576,-3191,-3318,5210,6733,-1942,9031,1280,-124 } },
5968 { "Canon EOS D2000", 0, 0,
5969 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
5970 { "Canon EOS D6000", 0, 0,
5971 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
5972 { "Canon EOS D30", 0, 0,
5973 { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
5974 { "Canon EOS D60", 0, 0xfa0,
5975 { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
5976 { "Canon EOS 5D Mark II", 0, 0x3cf0,
5977 { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
5978 { "Canon EOS 5D", 0, 0xe6c,
5979 { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
5980 { "Canon EOS 10D", 0, 0xfa0,
5981 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
5982 { "Canon EOS 20Da", 0, 0,
5983 { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
5984 { "Canon EOS 20D", 0, 0xfff,
5985 { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
5986 { "Canon EOS 30D", 0, 0,
5987 { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
5988 { "Canon EOS 40D", 0, 0x3f60,
5989 { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
5990 { "Canon EOS 50D", 0, 0x3d93,
5991 { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
5992 { "Canon EOS 300D", 0, 0xfa0,
5993 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
5994 { "Canon EOS 350D", 0, 0xfff,
5995 { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
5996 { "Canon EOS 400D", 0, 0xe8e,
5997 { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
5998 { "Canon EOS 450D", 0, 0x390d,
5999 { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
6000 { "Canon EOS 1000D", 0, 0xe43,
6001 { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
6002 { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
6003 { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
6004 { "Canon EOS-1Ds Mark II", 0, 0xe80,
6005 { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
6006 { "Canon EOS-1D Mark II N", 0, 0xe80,
6007 { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
6008 { "Canon EOS-1D Mark III", 0, 0x3bb0,
6009 { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
6010 { "Canon EOS-1D Mark II", 0, 0xe80,
6011 { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
6012 { "Canon EOS-1DS", 0, 0xe20,
6013 { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
6014 { "Canon EOS-1D", 0, 0xe20,
6015 { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
6016 { "Canon EOS", 0, 0,
6017 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
6018 { "Canon PowerShot A50", 0, 0,
6019 { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
6020 { "Canon PowerShot A5", 0, 0,
6021 { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
6022 { "Canon PowerShot G10", 0, 0,
6023 { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
6024 { "Canon PowerShot G1", 0, 0,
6025 { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
6026 { "Canon PowerShot G2", 0, 0,
6027 { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
6028 { "Canon PowerShot G3", 0, 0,
6029 { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
6030 { "Canon PowerShot G5", 0, 0,
6031 { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
6032 { "Canon PowerShot G6", 0, 0,
6033 { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
6034 { "Canon PowerShot G9", 0, 0,
6035 { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
6036 { "Canon PowerShot Pro1", 0, 0,
6037 { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
6038 { "Canon PowerShot Pro70", 34, 0,
6039 { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
6040 { "Canon PowerShot Pro90", 0, 0,
6041 { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
6042 { "Canon PowerShot S30", 0, 0,
6043 { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
6044 { "Canon PowerShot S40", 0, 0,
6045 { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
6046 { "Canon PowerShot S45", 0, 0,
6047 { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
6048 { "Canon PowerShot S50", 0, 0,
6049 { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
6050 { "Canon PowerShot S60", 0, 0,
6051 { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
6052 { "Canon PowerShot S70", 0, 0,
6053 { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
6054 { "Canon PowerShot A610", 0, 0, /* DJC */
6055 { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
6056 { "Canon PowerShot A620", 0, 0, /* DJC */
6057 { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
6058 { "Canon PowerShot A630", 0, 0, /* DJC */
6059 { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
6060 { "Canon PowerShot A640", 0, 0, /* DJC */
6061 { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
6062 { "Canon PowerShot A650", 0, 0, /* DJC */
6063 { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
6064 { "Canon PowerShot A720", 0, 0, /* DJC */
6065 { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
6066 { "Canon PowerShot S3 IS", 0, 0, /* DJC */
6067 { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
6068 { "CINE 650", 0, 0,
6069 { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
6070 { "CINE 660", 0, 0,
6071 { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
6072 { "CINE", 0, 0,
6073 { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
6074 { "Contax N Digital", 0, 0xf1e,
6075 { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
6076 { "EPSON R-D1", 0, 0,
6077 { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
6078 { "FUJIFILM FinePix E550", 0, 0,
6079 { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
6080 { "FUJIFILM FinePix E900", 0, 0,
6081 { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
6082 { "FUJIFILM FinePix F8", 0, 0,
6083 { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
6084 { "FUJIFILM FinePix F7", 0, 0,
6085 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
6086 { "FUJIFILM FinePix S100FS", 514, 0,
6087 { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
6088 { "FUJIFILM FinePix S20Pro", 0, 0,
6089 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
6090 { "FUJIFILM FinePix S2Pro", 128, 0,
6091 { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
6092 { "FUJIFILM FinePix S3Pro", 0, 0,
6093 { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
6094 { "FUJIFILM FinePix S5Pro", 0, 0,
6095 { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
6096 { "FUJIFILM FinePix S5000", 0, 0,
6097 { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
6098 { "FUJIFILM FinePix S5100", 0, 0x3e00,
6099 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
6100 { "FUJIFILM FinePix S5500", 0, 0x3e00,
6101 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
6102 { "FUJIFILM FinePix S5200", 0, 0,
6103 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
6104 { "FUJIFILM FinePix S5600", 0, 0,
6105 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
6106 { "FUJIFILM FinePix S6", 0, 0,
6107 { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
6108 { "FUJIFILM FinePix S7000", 0, 0,
6109 { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
6110 { "FUJIFILM FinePix S9000", 0, 0,
6111 { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
6112 { "FUJIFILM FinePix S9500", 0, 0,
6113 { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
6114 { "FUJIFILM FinePix S9100", 0, 0,
6115 { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
6116 { "FUJIFILM FinePix S9600", 0, 0,
6117 { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
6118 { "FUJIFILM IS-1", 0, 0,
6119 { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
6120 { "FUJIFILM IS Pro", 0, 0,
6121 { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
6122 { "Imacon Ixpress", 0, 0, /* DJC */
6123 { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
6124 { "KODAK NC2000", 0, 0,
6125 { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
6126 { "Kodak DCS315C", 8, 0,
6127 { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
6128 { "Kodak DCS330C", 8, 0,
6129 { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
6130 { "KODAK DCS420", 0, 0,
6131 { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
6132 { "KODAK DCS460", 0, 0,
6133 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
6134 { "KODAK EOSDCS1", 0, 0,
6135 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
6136 { "KODAK EOSDCS3B", 0, 0,
6137 { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
6138 { "Kodak DCS520C", 180, 0,
6139 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
6140 { "Kodak DCS560C", 188, 0,
6141 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
6142 { "Kodak DCS620C", 180, 0,
6143 { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
6144 { "Kodak DCS620X", 185, 0,
6145 { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
6146 { "Kodak DCS660C", 214, 0,
6147 { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
6148 { "Kodak DCS720X", 0, 0,
6149 { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
6150 { "Kodak DCS760C", 0, 0,
6151 { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
6152 { "Kodak DCS Pro SLR", 0, 0,
6153 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
6154 { "Kodak DCS Pro 14nx", 0, 0,
6155 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
6156 { "Kodak DCS Pro 14", 0, 0,
6157 { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
6158 { "Kodak ProBack645", 0, 0,
6159 { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
6160 { "Kodak ProBack", 0, 0,
6161 { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
6162 { "KODAK P712", 0, 0,
6163 { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
6164 { "KODAK P850", 0, 0xf7c,
6165 { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
6166 { "KODAK P880", 0, 0xfff,
6167 { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
6168 { "Leaf CMost", 0, 0,
6169 { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
6170 { "Leaf Valeo 6", 0, 0,
6171 { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
6172 { "Leaf Aptus 54S", 0, 0,
6173 { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
6174 { "Leaf Aptus 65", 0, 0,
6175 { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
6176 { "Leaf Aptus 75", 0, 0,
6177 { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
6178 { "Leaf", 0, 0,
6179 { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
6180 { "Mamiya ZD", 0, 0,
6181 { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
6182 { "Micron 2010", 110, 0, /* DJC */
6183 { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
6184 { "Minolta DiMAGE 5", 0, 0xf7d,
6185 { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
6186 { "Minolta DiMAGE 7Hi", 0, 0xf7d,
6187 { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
6188 { "Minolta DiMAGE 7", 0, 0xf7d,
6189 { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
6190 { "Minolta DiMAGE A1", 0, 0xf8b,
6191 { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
6192 { "MINOLTA DiMAGE A200", 0, 0,
6193 { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
6194 { "Minolta DiMAGE A2", 0, 0xf8f,
6195 { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
6196 { "Minolta DiMAGE Z2", 0, 0, /* DJC */
6197 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
6198 { "MINOLTA DYNAX 5", 0, 0xffb,
6199 { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
6200 { "MINOLTA DYNAX 7", 0, 0xffb,
6201 { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
6202 { "NIKON D100", 0, 0,
6203 { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
6204 { "NIKON D1H", 0, 0,
6205 { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
6206 { "NIKON D1X", 0, 0,
6207 { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
6208 { "NIKON D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
6209 { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
6210 { "NIKON D2H", 0, 0,
6211 { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
6212 { "NIKON D2X", 0, 0,
6213 { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
6214 { "NIKON D40X", 0, 0,
6215 { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
6216 { "NIKON D40", 0, 0,
6217 { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
6218 { "NIKON D50", 0, 0,
6219 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
6220 { "NIKON D60", 0, 0,
6221 { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
6222 { "NIKON D700", 0, 0,
6223 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
6224 { "NIKON D70", 0, 0,
6225 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
6226 { "NIKON D80", 0, 0,
6227 { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
6228 { "NIKON D90", 0, 0xf00,
6229 { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
6230 { "NIKON D200", 0, 0xfbc,
6231 { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
6232 { "NIKON D300", 0, 0,
6233 { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
6234 { "NIKON D3", 0, 0,
6235 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
6236 { "NIKON E950", 0, 0x3dd, /* DJC */
6237 { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
6238 { "NIKON E995", 0, 0, /* copied from E5000 */
6239 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6240 { "NIKON E2100", 0, 0, /* copied from Z2, new white balance */
6241 { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711} },
6242 { "NIKON E2500", 0, 0,
6243 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6244 { "NIKON E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
6245 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
6246 { "NIKON E4500", 0, 0,
6247 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6248 { "NIKON E5000", 0, 0,
6249 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6250 { "NIKON E5400", 0, 0,
6251 { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
6252 { "NIKON E5700", 0, 0,
6253 { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
6254 { "NIKON E8400", 0, 0,
6255 { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
6256 { "NIKON E8700", 0, 0,
6257 { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
6258 { "NIKON E8800", 0, 0,
6259 { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
6260 { "NIKON COOLPIX P6000", 0, 0,
6261 { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
6262 { "OLYMPUS C5050", 0, 0,
6263 { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
6264 { "OLYMPUS C5060", 0, 0,
6265 { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
6266 { "OLYMPUS C7070", 0, 0,
6267 { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
6268 { "OLYMPUS C70", 0, 0,
6269 { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
6270 { "OLYMPUS C80", 0, 0,
6271 { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
6272 { "OLYMPUS E-10", 0, 0xffc0,
6273 { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
6274 { "OLYMPUS E-1", 0, 0xfff0,
6275 { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
6276 { "OLYMPUS E-20", 0, 0xffc0,
6277 { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
6278 { "OLYMPUS E-300", 0, 0,
6279 { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
6280 { "OLYMPUS E-330", 0, 0,
6281 { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
6282 { "OLYMPUS E-3", 0, 0xf99,
6283 { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
6284 { "OLYMPUS E-400", 0, 0xfff0,
6285 { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
6286 { "OLYMPUS E-410", 0, 0xf6a,
6287 { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
6288 { "OLYMPUS E-420", 0, 0xfd7,
6289 { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
6290 { "OLYMPUS E-500", 0, 0,
6291 { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
6292 { "OLYMPUS E-510", 0, 0xf6a,
6293 { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
6294 { "OLYMPUS E-520", 0, 0xfd2,
6295 { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
6296 { "OLYMPUS SP350", 0, 0,
6297 { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
6298 { "OLYMPUS SP3", 0, 0,
6299 { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
6300 { "OLYMPUS SP500UZ", 0, 0xfff,
6301 { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
6302 { "OLYMPUS SP510UZ", 0, 0xffe,
6303 { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
6304 { "OLYMPUS SP550UZ", 0, 0xffe,
6305 { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
6306 { "OLYMPUS SP560UZ", 0, 0xff9,
6307 { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
6308 { "OLYMPUS SP570UZ", 0, 0,
6309 { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
6310 { "PENTAX *ist DL2", 0, 0,
6311 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
6312 { "PENTAX *ist DL", 0, 0,
6313 { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
6314 { "PENTAX *ist DS2", 0, 0,
6315 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
6316 { "PENTAX *ist DS", 0, 0,
6317 { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
6318 { "PENTAX *ist D", 0, 0,
6319 { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
6320 { "PENTAX K10D", 0, 0,
6321 { 9566,-2863,-803,-7170,15172,2112,-818,803,9705 } },
6322 { "PENTAX K1", 0, 0,
6323 { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
6324 { "PENTAX K20D", 0, 0,
6325 { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
6326 { "PENTAX K200D", 0, 0,
6327 { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
6328 { "PENTAX K2000", 0, 0,
6329 { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
6330 { "PENTAX K-m", 0, 0,
6331 { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
6332 { "Panasonic DMC-FZ8", 0, 0xf7f0,
6333 { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
6334 { "Panasonic DMC-FZ18", 0, 0,
6335 { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
6336 { "Panasonic DMC-FZ28", 15, 0xfff,
6337 { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
6338 { "Panasonic DMC-FZ30", 0, 0xf94c,
6339 { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
6340 { "Panasonic DMC-FZ50", 0, 0xfff0, /* aka "LEICA V-LUX1" */
6341 { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
6342 { "Panasonic DMC-L10", 15, 0xf96,
6343 { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
6344 { "Panasonic DMC-L1", 0, 0xf7fc, /* aka "LEICA DIGILUX 3" */
6345 { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
6346 { "Panasonic DMC-LC1", 0, 0, /* aka "LEICA DIGILUX 2" */
6347 { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
6348 { "Panasonic DMC-LX1", 0, 0xf7f0, /* aka "LEICA D-LUX2" */
6349 { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
6350 { "Panasonic DMC-LX2", 0, 0, /* aka "LEICA D-LUX3" */
6351 { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
6352 { "Panasonic DMC-LX3", 15, 0xfff, /* aka "LEICA D-LUX4" */
6353 { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
6354 { "Panasonic DMC-FX150", 15, 0xfff,
6355 { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
6356 { "Panasonic DMC-G1", 15, 0xfff,
6357 { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
6358 { "Phase One H 20", 0, 0, /* DJC */
6359 { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
6360 { "Phase One P 2", 0, 0,
6361 { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
6362 { "Phase One P 30", 0, 0,
6363 { 4516,-245,-37,-7020,14976,2173,-3206,4671,7087 } },
6364 { "Phase One P 45", 0, 0,
6365 { 5053,-24,-117,-5684,14076,1702,-2619,4492,5849 } },
6366 { "SAMSUNG GX-1", 0, 0,
6367 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
6368 { "Sinar", 0, 0, /* DJC */
6369 { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
6370 { "SONY DSC-F828", 491, 0,
6371 { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
6372 { "SONY DSC-R1", 512, 0,
6373 { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
6374 { "SONY DSC-V3", 0, 0,
6375 { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
6376 { "SONY DSLR-A100", 0, 0xfeb,
6377 { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
6378 { "SONY DSLR-A200", 0, 0,
6379 { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
6380 { "SONY DSLR-A300", 0, 0,
6381 { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
6382 { "SONY DSLR-A350", 0, 0xffc,
6383 { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
6384 { "SONY DSLR-A700", 254, 0x1ffe,
6385 { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
6386 { "SONY DSLR-A900", 254, 0x1ffe,
6387 { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } }
6389 double cam_xyz[4][3];
6390 char name[130];
6391 int i, j;
6393 sprintf (name, "%s %s", p_make, p_model);
6394 for (i=0; i < sizeof table / sizeof *table; i++)
6395 if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
6396 if (table[i].t_black) black = (ushort) table[i].t_black;
6397 if (table[i].t_maximum) maximum = (ushort) table[i].t_maximum;
6398 for (j=0; j < 12; j++)
6399 #ifdef LIBRAW_LIBRARY_BUILD
6400 imgdata.color.cam_xyz[0][j] =
6401 #endif
6402 cam_xyz[0][j] = table[i].trans[j] / 10000.0;
6403 cam_xyz_coeff (cam_xyz);
6404 break;
6408 void CLASS simple_coeff (int index)
6410 static const float table[][12] = {
6411 /* index 0 -- all Foveon cameras */
6412 { 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
6413 /* index 1 -- Kodak DC20 and DC25 */
6414 { 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
6415 /* index 2 -- Logitech Fotoman Pixtura */
6416 { 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
6417 /* index 3 -- Nikon E880, E900, and E990 */
6418 { -1.936280, 1.800443, -1.448486, 2.584324,
6419 1.405365, -0.524955, -0.289090, 0.408680,
6420 -1.204965, 1.082304, 2.941367, -1.818705 }
6422 int i, c;
6424 for (raw_color = i=0; i < 3; i++)
6425 FORCC rgb_cam[i][c] = table[index][i*colors+c];
6426 #ifdef LIBRAW_LIBRARY_BUILD
6427 color_flags.rgb_cam_state = LIBRAW_COLORSTATE_CALCULATED;
6428 #endif
6431 short CLASS guess_byte_order (int words)
6433 uchar test[4][2];
6434 int t=2, msb;
6435 double diff, sum[2] = {0,0};
6437 fread (test[0], 2, 2, ifp);
6438 for (words-=2; words--; ) {
6439 fread (test[t], 2, 1, ifp);
6440 for (msb=0; msb < 2; msb++) {
6441 diff = (test[t^2][msb] << 8 | test[t^2][!msb])
6442 - (test[t ][msb] << 8 | test[t ][!msb]);
6443 sum[msb] += diff*diff;
6445 t = (t+1) & 3;
6447 return sum[0] < sum[1] ? 0x4d4d : 0x4949;
6451 Identify which camera created this file, and set global variables
6452 accordingly.
6454 void CLASS identify()
6456 char head[32], *cp;
6457 unsigned hlen, fsize, i, c, is_canon;
6458 struct jhead jh;
6459 static const struct {
6460 int fsize;
6461 char t_make[12], t_model[19], withjpeg;
6462 } table[] = {
6463 { 62464, "Kodak", "DC20" ,0 },
6464 { 124928, "Kodak", "DC20" ,0 },
6465 { 1652736, "Kodak", "DCS200" ,0 },
6466 { 4159302, "Kodak", "C330" ,0 },
6467 { 4162462, "Kodak", "C330" ,0 },
6468 { 460800, "Kodak", "C603v" ,0 },
6469 { 614400, "Kodak", "C603v" ,0 },
6470 { 6163328, "Kodak", "C603" ,0 },
6471 { 6166488, "Kodak", "C603" ,0 },
6472 { 9116448, "Kodak", "C603y" ,0 },
6473 { 311696, "ST Micro", "STV680 VGA" ,0 }, /* SPYz */
6474 { 614400, "Kodak", "KAI-0340" ,0 },
6475 { 787456, "Creative", "PC-CAM 600" ,0 },
6476 { 1138688, "Minolta", "RD175" ,0 },
6477 { 3840000, "Foculus", "531C" ,0 },
6478 { 786432, "AVT", "F-080C" ,0 },
6479 { 1447680, "AVT", "F-145C" ,0 },
6480 { 1920000, "AVT", "F-201C" ,0 },
6481 { 5067304, "AVT", "F-510C" ,0 },
6482 { 10134608, "AVT", "F-510C" ,0 },
6483 { 16157136, "AVT", "F-810C" ,0 },
6484 { 1409024, "Sony", "XCD-SX910CR" ,0 },
6485 { 2818048, "Sony", "XCD-SX910CR" ,0 },
6486 { 3884928, "Micron", "2010" ,0 },
6487 { 6624000, "Pixelink", "A782" ,0 },
6488 { 13248000, "Pixelink", "A782" ,0 },
6489 { 6291456, "RoverShot","3320AF" ,0 },
6490 { 6553440, "Canon", "PowerShot A460" ,0 },
6491 { 6653280, "Canon", "PowerShot A530" ,0 },
6492 { 6573120, "Canon", "PowerShot A610" ,0 },
6493 { 9219600, "Canon", "PowerShot A620" ,0 },
6494 { 10341600, "Canon", "PowerShot A720" ,0 },
6495 { 10383120, "Canon", "PowerShot A630" ,0 },
6496 { 12945240, "Canon", "PowerShot A640" ,0 },
6497 { 15636240, "Canon", "PowerShot A650" ,0 },
6498 { 5298000, "Canon", "PowerShot SD300" ,0 },
6499 { 7710960, "Canon", "PowerShot S3 IS" ,0 },
6500 { 5939200, "OLYMPUS", "C770UZ" ,0 },
6501 { 1581060, "NIKON", "E900" ,1 }, /* or E900s,E910 */
6502 { 2465792, "NIKON", "E950" ,1 }, /* or E800,E700 */
6503 { 2940928, "NIKON", "E2100" ,1 }, /* or E2500 */
6504 { 4771840, "NIKON", "E990" ,1 }, /* or E995, Oly C3030Z */
6505 { 4775936, "NIKON", "E3700" ,1 }, /* or Optio 33WR */
6506 { 5869568, "NIKON", "E4300" ,1 }, /* or DiMAGE Z2 */
6507 { 5865472, "NIKON", "E4500" ,1 },
6508 { 7438336, "NIKON", "E5000" ,1 }, /* or E5700 */
6509 { 8998912, "NIKON", "COOLPIX S6" ,1 },
6510 { 1976352, "CASIO", "QV-2000UX" ,1 },
6511 { 3217760, "CASIO", "QV-3*00EX" ,1 },
6512 { 6218368, "CASIO", "QV-5700" ,1 },
6513 { 6054400, "CASIO", "QV-R41" ,1 },
6514 { 7530816, "CASIO", "QV-R51" ,1 },
6515 { 7684000, "CASIO", "QV-4000" ,1 },
6516 { 4948608, "CASIO", "EX-S100" ,1 },
6517 { 7542528, "CASIO", "EX-Z50" ,1 },
6518 { 7753344, "CASIO", "EX-Z55" ,1 },
6519 { 7426656, "CASIO", "EX-P505" ,1 },
6520 { 9313536, "CASIO", "EX-P600" ,1 },
6521 { 10979200, "CASIO", "EX-P700" ,1 },
6522 { 3178560, "PENTAX", "Optio S" ,1 },
6523 { 4841984, "PENTAX", "Optio S" ,1 },
6524 { 6114240, "PENTAX", "Optio S4" ,1 }, /* or S4i, CASIO EX-Z4 */
6525 { 10702848, "PENTAX", "Optio 750Z" ,1 },
6526 { 16098048, "SAMSUNG", "S85" ,1 },
6527 { 16215552, "SAMSUNG", "S85" ,1 },
6528 { 12582980, "Sinar", "" ,0 },
6529 { 33292868, "Sinar", "" ,0 },
6530 { 44390468, "Sinar", "" ,0 } };
6531 static const char *corp[] =
6532 { "Canon", "NIKON", "EPSON", "KODAK", "Kodak", "OLYMPUS", "PENTAX",
6533 "MINOLTA", "Minolta", "Konica", "CASIO", "Sinar", "Phase One",
6534 "SAMSUNG", "Mamiya" };
6536 #ifdef LIBRAW_LIBRARY_BUILD
6537 RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,0,2);
6538 #endif
6540 tiff_flip = flip = filters = -1; /* 0 is valid, so -1 is unknown */
6541 raw_height = raw_width = fuji_width = fuji_layout = cr2_slice[0] = 0;
6542 maximum = height = width = top_margin = left_margin = 0;
6543 cdesc[0] = desc[0] = artist[0] = make[0] = model[0] = model2[0] = 0;
6544 iso_speed = shutter = aperture = focal_len = unique_id = 0;
6545 memset (gpsdata, 0, sizeof gpsdata);
6546 memset (white, 0, sizeof white);
6547 thumb_offset = thumb_length = thumb_width = thumb_height = 0;
6548 load_raw = thumb_load_raw = 0;
6549 write_thumb = &CLASS jpeg_thumb;
6550 data_offset = meta_length = tiff_bps = tiff_compress = 0;
6551 kodak_cbpp = zero_after_ff = dng_version = load_flags = 0;
6552 timestamp = shot_order = tiff_samples = black = is_foveon = 0;
6553 mix_green = profile_length = data_error = zero_is_bad = 0;
6554 pixel_aspect = is_raw = raw_color = use_gamma = 1;
6555 tile_width = tile_length = INT_MAX;
6556 for (i=0; i < 4; i++) {
6557 cam_mul[i] = i == 1;
6558 pre_mul[i] = i < 3;
6559 FORC3 cmatrix[c][i] = 0;
6560 FORC3 rgb_cam[c][i] = c == i;
6562 #ifdef LIBRAW_LIBRARY_BUILD
6563 color_flags.cmatrix_state = LIBRAW_COLORSTATE_INIT;
6564 color_flags.rgb_cam_state = LIBRAW_COLORSTATE_INIT;
6565 color_flags.pre_mul_state = LIBRAW_COLORSTATE_INIT;
6566 color_flags.cam_mul_state = LIBRAW_COLORSTATE_INIT;
6567 #endif
6568 colors = 3;
6569 tiff_bps = 12;
6570 for (i=0; i < 0x4000; i++) curve[i] = i;
6571 #ifdef LIBRAW_LIBRARY_BUILD
6572 color_flags.curve_state = LIBRAW_COLORSTATE_INIT;
6573 #endif
6575 order = get2();
6576 hlen = get4();
6577 fseek (ifp, 0, SEEK_SET);
6578 fread (head, 1, 32, ifp);
6579 fseek (ifp, 0, SEEK_END);
6580 fsize = ftell(ifp);
6581 if ((cp = (char *) memmem (head, 32, "MMMM", 4)) ||
6582 (cp = (char *) memmem (head, 32, "IIII", 4))) {
6583 parse_phase_one (cp-head);
6584 if (cp-head) parse_tiff(0);
6585 } else if (order == 0x4949 || order == 0x4d4d) {
6586 if (!memcmp (head+6,"HEAPCCDR",8)) {
6587 data_offset = hlen;
6588 parse_ciff (hlen, fsize - hlen);
6589 } else {
6590 parse_tiff(0);
6592 } else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
6593 !memcmp (head+6,"Exif",4)) {
6594 fseek (ifp, 4, SEEK_SET);
6595 data_offset = 4 + get2();
6596 fseek (ifp, data_offset, SEEK_SET);
6597 if (fgetc(ifp) != 0xff)
6598 parse_tiff(12);
6599 thumb_offset = 0;
6600 } else if (!memcmp (head+25,"ARECOYK",7)) {
6601 strcpy (make, "Contax");
6602 strcpy (model,"N Digital");
6603 fseek (ifp, 33, SEEK_SET);
6604 get_timestamp(1);
6605 fseek (ifp, 60, SEEK_SET);
6606 FORC4 cam_mul[c ^ (c >> 1)] = get4();
6607 #ifdef LIBRAW_LIBRARY_BUILD
6608 color_flags.cam_mul_state = LIBRAW_COLORSTATE_LOADED;
6609 #endif
6610 } else if (!strcmp (head, "PXN")) {
6611 strcpy (make, "Logitech");
6612 strcpy (model,"Fotoman Pixtura");
6613 } else if (!strcmp (head, "qktk")) {
6614 strcpy (make, "Apple");
6615 strcpy (model,"QuickTake 100");
6616 } else if (!strcmp (head, "qktn")) {
6617 strcpy (make, "Apple");
6618 strcpy (model,"QuickTake 150");
6619 } else if (!memcmp (head,"FUJIFILM",8)) {
6620 fseek (ifp, 84, SEEK_SET);
6621 thumb_offset = get4();
6622 thumb_length = get4();
6623 fseek (ifp, 92, SEEK_SET);
6624 parse_fuji (get4());
6625 if (thumb_offset > 120) {
6626 fseek (ifp, 120, SEEK_SET);
6627 is_raw += (i = get4()) && 1;
6628 if (is_raw == 2 && shot_select)
6629 parse_fuji (i);
6631 fseek (ifp, 100, SEEK_SET);
6632 data_offset = get4();
6633 parse_tiff (thumb_offset+12);
6634 } else if (!memcmp (head,"RIFF",4)) {
6635 fseek (ifp, 0, SEEK_SET);
6636 parse_riff();
6637 } else if (!memcmp (head,"\0\001\0\001\0@",6)) {
6638 fseek (ifp, 6, SEEK_SET);
6639 fread (make, 1, 8, ifp);
6640 fread (model, 1, 8, ifp);
6641 fread (model2, 1, 16, ifp);
6642 data_offset = get2();
6643 get2();
6644 raw_width = get2();
6645 raw_height = get2();
6646 load_raw = &CLASS nokia_load_raw;
6647 filters = 0x61616161;
6648 } else if (!memcmp (head,"DSC-Image",9))
6649 parse_rollei();
6650 else if (!memcmp (head,"PWAD",4))
6651 parse_sinar_ia();
6652 else if (!memcmp (head,"\0MRM",4))
6653 parse_minolta(0);
6654 else if (!memcmp (head,"FOVb",4))
6655 parse_foveon();
6656 else if (!memcmp (head,"CI",2))
6657 parse_cine();
6658 else
6659 for (i=0; i < sizeof table / sizeof *table; i++)
6660 if (fsize == table[i].fsize) {
6661 strcpy (make, table[i].t_make );
6662 strcpy (model, table[i].t_model);
6663 if (table[i].withjpeg)
6664 parse_external_jpeg();
6666 if (make[0] == 0) parse_smal (0, fsize);
6667 if (make[0] == 0) parse_jpeg (is_raw = 0);
6669 for (i=0; i < sizeof corp / sizeof *corp; i++)
6670 if (strstr (make, corp[i])) /* Simplify company names */
6671 strcpy (make, corp[i]);
6672 if (!strncmp (make,"KODAK",5))
6673 make[16] = model[16] = 0;
6674 cp = make + strlen(make); /* Remove trailing spaces */
6675 while (*--cp == ' ') *cp = 0;
6676 cp = model + strlen(model);
6677 while (*--cp == ' ') *cp = 0;
6678 i = strlen(make); /* Remove make from model */
6679 if (!strncasecmp (model, make, i) && model[i++] == ' ')
6680 memmove (model, model+i, 64-i);
6681 if (!strncmp (model,"Digital Camera ",15))
6682 strcpy (model, model+15);
6683 desc[511] = artist[63] = make[63] = model[63] = model2[63] = 0;
6684 if (!is_raw) goto notraw;
6686 if (!maximum) maximum = (1 << tiff_bps) - 1;
6687 if (!height) height = raw_height;
6688 if (!width) width = raw_width;
6689 if (fuji_width) {
6690 width = height + fuji_width;
6691 height = width - 1;
6692 pixel_aspect = 1;
6694 if (height == 2624 && width == 3936) /* Pentax K10D and Samsung GX10 */
6695 { height = 2616; width = 3896; }
6696 if (height == 3136 && width == 4864) /* Pentax K20D */
6697 { height = 3124; width = 4688; }
6698 if (height == 3014 && width == 4096) /* Ricoh GX200 */
6699 width = 4014;
6700 if (dng_version) {
6701 if (filters == UINT_MAX) filters = 0;
6702 if (filters) is_raw = tiff_samples;
6703 else colors = tiff_samples;
6704 if (tiff_compress == 1)
6705 load_raw = &CLASS adobe_dng_load_raw_nc;
6706 if (tiff_compress == 7)
6707 load_raw = &CLASS adobe_dng_load_raw_lj;
6708 goto dng_skip;
6710 if ((is_canon = !strcmp(make,"Canon")))
6711 load_raw = memcmp (head+6,"HEAPCCDR",8) ?
6712 &CLASS lossless_jpeg_load_raw : &CLASS canon_compressed_load_raw;
6713 if (!strcmp(make,"NIKON")) {
6714 if (!load_raw)
6715 load_raw = &CLASS packed_12_load_raw;
6716 if (model[0] == 'E')
6717 load_flags |= !data_offset << 2 | 2;
6719 if (!strcmp(make,"CASIO")) {
6720 load_raw = &CLASS packed_12_load_raw;
6721 maximum = 0xf7f;
6724 /* Set parameters based on camera name (for non-DNG files). */
6726 if (is_foveon) {
6727 if (height*2 < width) pixel_aspect = 0.5;
6728 if (height > width) pixel_aspect = 2;
6729 filters = 0;
6730 load_raw = &CLASS foveon_load_raw;
6731 simple_coeff(0);
6732 } else if (is_canon && tiff_bps == 15) {
6733 switch (width) {
6734 case 3344: width -= 66;
6735 case 3872: width -= 6;
6737 filters = 0;
6738 load_raw = &CLASS canon_sraw_load_raw;
6739 } else if (!strcmp(model,"PowerShot 600")) {
6740 height = 613;
6741 width = 854;
6742 raw_width = 896;
6743 pixel_aspect = 607/628.0;
6744 colors = 4;
6745 filters = 0xe1e4e1e4;
6746 load_raw = &CLASS canon_600_load_raw;
6747 } else if (!strcmp(model,"PowerShot A5") ||
6748 !strcmp(model,"PowerShot A5 Zoom")) {
6749 height = 773;
6750 width = 960;
6751 raw_width = 992;
6752 pixel_aspect = 256/235.0;
6753 colors = 4;
6754 filters = 0x1e4e1e4e;
6755 load_raw = &CLASS canon_a5_load_raw;
6756 } else if (!strcmp(model,"PowerShot A50")) {
6757 height = 968;
6758 width = 1290;
6759 raw_width = 1320;
6760 colors = 4;
6761 filters = 0x1b4e4b1e;
6762 load_raw = &CLASS canon_a5_load_raw;
6763 } else if (!strcmp(model,"PowerShot Pro70")) {
6764 height = 1024;
6765 width = 1552;
6766 colors = 4;
6767 filters = 0x1e4b4e1b;
6768 load_raw = &CLASS canon_a5_load_raw;
6769 } else if (!strcmp(model,"PowerShot SD300")) {
6770 height = 1752;
6771 width = 2344;
6772 raw_height = 1766;
6773 raw_width = 2400;
6774 top_margin = 12;
6775 left_margin = 12;
6776 load_raw = &CLASS canon_a5_load_raw;
6777 } else if (!strcmp(model,"PowerShot A460")) {
6778 height = 1960;
6779 width = 2616;
6780 raw_height = 1968;
6781 raw_width = 2664;
6782 top_margin = 4;
6783 left_margin = 4;
6784 load_raw = &CLASS canon_a5_load_raw;
6785 } else if (!strcmp(model,"PowerShot A530")) {
6786 height = 1984;
6787 width = 2620;
6788 raw_height = 1992;
6789 raw_width = 2672;
6790 top_margin = 6;
6791 left_margin = 10;
6792 load_raw = &CLASS canon_a5_load_raw;
6793 raw_color = 0;
6794 } else if (!strcmp(model,"PowerShot A610")) {
6795 if (canon_s2is()) strcpy (model+10, "S2 IS");
6796 height = 1960;
6797 width = 2616;
6798 raw_height = 1968;
6799 raw_width = 2672;
6800 top_margin = 8;
6801 left_margin = 12;
6802 load_raw = &CLASS canon_a5_load_raw;
6803 } else if (!strcmp(model,"PowerShot A620")) {
6804 height = 2328;
6805 width = 3112;
6806 raw_height = 2340;
6807 raw_width = 3152;
6808 top_margin = 12;
6809 left_margin = 36;
6810 load_raw = &CLASS canon_a5_load_raw;
6811 } else if (!strcmp(model,"PowerShot A720")) {
6812 height = 2472;
6813 width = 3298;
6814 raw_height = 2480;
6815 raw_width = 3336;
6816 top_margin = 5;
6817 left_margin = 6;
6818 load_raw = &CLASS canon_a5_load_raw;
6819 } else if (!strcmp(model,"PowerShot A630")) {
6820 height = 2472;
6821 width = 3288;
6822 raw_height = 2484;
6823 raw_width = 3344;
6824 top_margin = 6;
6825 left_margin = 12;
6826 load_raw = &CLASS canon_a5_load_raw;
6827 } else if (!strcmp(model,"PowerShot A640")) {
6828 height = 2760;
6829 width = 3672;
6830 raw_height = 2772;
6831 raw_width = 3736;
6832 top_margin = 6;
6833 left_margin = 12;
6834 load_raw = &CLASS canon_a5_load_raw;
6835 } else if (!strcmp(model,"PowerShot A650")) {
6836 height = 3024;
6837 width = 4032;
6838 raw_height = 3048;
6839 raw_width = 4104;
6840 top_margin = 12;
6841 left_margin = 48;
6842 load_raw = &CLASS canon_a5_load_raw;
6843 } else if (!strcmp(model,"PowerShot S3 IS")) {
6844 height = 2128;
6845 width = 2840;
6846 raw_height = 2136;
6847 raw_width = 2888;
6848 top_margin = 8;
6849 left_margin = 44;
6850 load_raw = &CLASS canon_a5_load_raw;
6851 } else if (!strcmp(model,"PowerShot Pro90 IS")) {
6852 width = 1896;
6853 colors = 4;
6854 filters = 0xb4b4b4b4;
6855 } else if (is_canon && raw_width == 2144) {
6856 height = 1550;
6857 width = 2088;
6858 top_margin = 8;
6859 left_margin = 4;
6860 if (!strcmp(model,"PowerShot G1")) {
6861 colors = 4;
6862 filters = 0xb4b4b4b4;
6864 } else if (is_canon && raw_width == 2224) {
6865 height = 1448;
6866 width = 2176;
6867 top_margin = 6;
6868 left_margin = 48;
6869 } else if (is_canon && raw_width == 2376) {
6870 height = 1720;
6871 width = 2312;
6872 top_margin = 6;
6873 left_margin = 12;
6874 } else if (is_canon && raw_width == 2672) {
6875 height = 1960;
6876 width = 2616;
6877 top_margin = 6;
6878 left_margin = 12;
6879 } else if (is_canon && raw_width == 3152) {
6880 height = 2056;
6881 width = 3088;
6882 top_margin = 12;
6883 left_margin = 64;
6884 if (unique_id == 0x80000170)
6885 adobe_coeff ("Canon","EOS 300D");
6886 } else if (is_canon && raw_width == 3160) {
6887 height = 2328;
6888 width = 3112;
6889 top_margin = 12;
6890 left_margin = 44;
6891 } else if (is_canon && raw_width == 3344) {
6892 height = 2472;
6893 width = 3288;
6894 top_margin = 6;
6895 left_margin = 4;
6896 } else if (!strcmp(model,"EOS D2000C")) {
6897 filters = 0x61616161;
6898 black = curve[200];
6899 } else if (is_canon && raw_width == 3516) {
6900 top_margin = 14;
6901 left_margin = 42;
6902 if (unique_id == 0x80000189)
6903 adobe_coeff ("Canon","EOS 350D");
6904 goto canon_cr2;
6905 } else if (is_canon && raw_width == 3596) {
6906 top_margin = 12;
6907 left_margin = 74;
6908 goto canon_cr2;
6909 } else if (is_canon && raw_width == 3944) {
6910 height = 2602;
6911 width = 3908;
6912 top_margin = 18;
6913 left_margin = 30;
6914 } else if (is_canon && raw_width == 3948) {
6915 top_margin = 18;
6916 left_margin = 42;
6917 height -= 2;
6918 if (unique_id == 0x80000236)
6919 adobe_coeff ("Canon","EOS 400D");
6920 if (unique_id == 0x80000254)
6921 adobe_coeff ("Canon","EOS 1000D");
6922 goto canon_cr2;
6923 } else if (is_canon && raw_width == 3984) {
6924 top_margin = 20;
6925 left_margin = 76;
6926 height -= 2;
6927 goto canon_cr2;
6928 } else if (is_canon && raw_width == 4104) {
6929 height = 3024;
6930 width = 4032;
6931 top_margin = 12;
6932 left_margin = 48;
6933 } else if (is_canon && raw_width == 4312) {
6934 top_margin = 18;
6935 left_margin = 22;
6936 height -= 2;
6937 if (unique_id == 0x80000176)
6938 adobe_coeff ("Canon","EOS 450D");
6939 goto canon_cr2;
6940 } else if (is_canon && raw_width == 4476) {
6941 top_margin = 34;
6942 left_margin = 90;
6943 goto canon_cr2;
6944 } else if (is_canon && raw_width == 4480) {
6945 height = 3326;
6946 width = 4432;
6947 top_margin = 10;
6948 left_margin = 12;
6949 filters = 0x49494949;
6950 } else if (is_canon && raw_width == 1208) {
6951 top_margin = 51;
6952 left_margin = 62;
6953 raw_width = width *= 4;
6954 goto canon_cr2;
6955 } else if (is_canon && raw_width == 1448) {
6956 top_margin = 51;
6957 left_margin = 158;
6958 raw_width = width *= 4;
6959 goto canon_cr2;
6960 } else if (is_canon && raw_width == 5108) {
6961 top_margin = 13;
6962 left_margin = 98;
6963 canon_cr2:
6964 height -= top_margin;
6965 width -= left_margin;
6966 } else if (is_canon && raw_width == 5712) {
6967 height = 3752;
6968 width = 5640;
6969 top_margin = 20;
6970 left_margin = 62;
6971 } else if (!strcmp(model,"D1")) {
6972 cam_mul[0] *= 256/527.0;
6973 cam_mul[2] *= 256/317.0;
6974 } else if (!strcmp(model,"D1X")) {
6975 width -= 4;
6976 pixel_aspect = 0.5;
6977 } else if (!strcmp(model,"D40X") ||
6978 !strcmp(model,"D60") ||
6979 !strcmp(model,"D80")) {
6980 height -= 3;
6981 width -= 4;
6982 } else if (!strcmp(model,"D3") ||
6983 !strcmp(model,"D700")) {
6984 width -= 4;
6985 left_margin = 2;
6986 } else if (!strncmp(model,"D40",3) ||
6987 !strncmp(model,"D50",3) ||
6988 !strncmp(model,"D70",3)) {
6989 width--;
6990 } else if (!strcmp(model,"D90")) {
6991 width -= 42;
6992 } else if (!strcmp(model,"D100")) {
6993 if (tiff_compress == 34713 && !nikon_is_compressed()) {
6994 load_raw = &CLASS packed_12_load_raw;
6995 load_flags |= 8;
6996 raw_width = (width += 3) + 3;
6998 } else if (!strcmp(model,"D200")) {
6999 left_margin = 1;
7000 width -= 4;
7001 filters = 0x94949494;
7002 } else if (!strncmp(model,"D2H",3)) {
7003 left_margin = 6;
7004 width -= 14;
7005 } else if (!strncmp(model,"D2X",3)) {
7006 if (width == 3264) width -= 32;
7007 else width -= 8;
7008 } else if (!strcmp(model,"D300")) {
7009 width -= 32;
7010 } else if (!strcmp(model,"COOLPIX P6000")) {
7011 load_flags = 1;
7012 filters = 0x94949494;
7013 } else if (fsize == 1581060) {
7014 height = 963;
7015 width = 1287;
7016 raw_width = 1632;
7017 load_raw = &CLASS nikon_e900_load_raw;
7018 maximum = 0x3f4;
7019 colors = 4;
7020 filters = 0x1e1e1e1e;
7021 simple_coeff(3);
7022 pre_mul[0] = 1.2085;
7023 pre_mul[1] = 1.0943;
7024 pre_mul[3] = 1.1103;
7025 #ifdef LIBRAW_LIBRARY_BUILD
7026 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7027 #endif
7028 } else if (fsize == 2465792) {
7029 height = 1203;
7030 width = 1616;
7031 raw_width = 2048;
7032 load_raw = &CLASS nikon_e900_load_raw;
7033 colors = 4;
7034 filters = 0x4b4b4b4b;
7035 adobe_coeff ("NIKON","E950");
7036 } else if (fsize == 4771840) {
7037 height = 1540;
7038 width = 2064;
7039 colors = 4;
7040 filters = 0xe1e1e1e1;
7041 load_raw = &CLASS packed_12_load_raw;
7042 load_flags = 6;
7043 if (!timestamp && nikon_e995())
7044 strcpy (model, "E995");
7045 if (strcmp(model,"E995")) {
7046 filters = 0xb4b4b4b4;
7047 simple_coeff(3);
7048 pre_mul[0] = 1.196;
7049 pre_mul[1] = 1.246;
7050 pre_mul[2] = 1.018;
7051 #ifdef LIBRAW_LIBRARY_BUILD
7052 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7053 #endif
7055 } else if (!strcmp(model,"E2100")) {
7056 if (!timestamp && !nikon_e2100()) goto cp_e2500;
7057 height = 1206;
7058 width = 1616;
7059 load_flags = 7;
7060 } else if (!strcmp(model,"E2500")) {
7061 cp_e2500:
7062 strcpy (model, "E2500");
7063 height = 1204;
7064 width = 1616;
7065 colors = 4;
7066 filters = 0x4b4b4b4b;
7067 } else if (fsize == 4775936) {
7068 height = 1542;
7069 width = 2064;
7070 load_raw = &CLASS packed_12_load_raw;
7071 load_flags = 7;
7072 pre_mul[0] = 1.818;
7073 pre_mul[2] = 1.618;
7074 #ifdef LIBRAW_LIBRARY_BUILD
7075 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7076 #endif
7077 if (!timestamp) nikon_3700();
7078 if (model[0] == 'E' && atoi(model+1) < 3700)
7079 filters = 0x49494949;
7080 if (!strcmp(model,"Optio 33WR")) {
7081 flip = 1;
7082 filters = 0x16161616;
7083 pre_mul[0] = 1.331;
7084 pre_mul[2] = 1.820;
7085 #ifdef LIBRAW_LIBRARY_BUILD
7086 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7087 #endif
7089 } else if (fsize == 5869568) {
7090 height = 1710;
7091 width = 2288;
7092 filters = 0x16161616;
7093 if (!timestamp && minolta_z2()) {
7094 strcpy (make, "Minolta");
7095 strcpy (model,"DiMAGE Z2");
7097 load_raw = &CLASS packed_12_load_raw;
7098 load_flags = 6 + (make[0] == 'M');
7099 } else if (!strcmp(model,"E4500")) {
7100 height = 1708;
7101 width = 2288;
7102 colors = 4;
7103 filters = 0xb4b4b4b4;
7104 } else if (fsize == 7438336) {
7105 height = 1924;
7106 width = 2576;
7107 colors = 4;
7108 filters = 0xb4b4b4b4;
7109 } else if (fsize == 8998912) {
7110 height = 2118;
7111 width = 2832;
7112 maximum = 0xf83;
7113 load_raw = &CLASS packed_12_load_raw;
7114 load_flags = 7;
7115 } else if (!strcmp(model,"FinePix S5100") ||
7116 !strcmp(model,"FinePix S5500")) {
7117 load_raw = &CLASS unpacked_load_raw;
7118 } else if (!strcmp(make,"FUJIFILM")) {
7119 if (!strcmp(model+7,"S2Pro")) {
7120 strcpy (model+7," S2Pro");
7121 height = 2144;
7122 width = 2880;
7123 flip = 6;
7124 } else
7125 maximum = 0x3e00;
7126 if (is_raw == 2 && shot_select)
7127 maximum = 0x2f00;
7128 top_margin = (raw_height - height)/2;
7129 left_margin = (raw_width - width )/2;
7130 if (is_raw == 2)
7131 data_offset += (shot_select > 0) * ( fuji_layout ?
7132 (raw_width *= 2) : raw_height*raw_width*2 );
7133 fuji_width = width >> !fuji_layout;
7134 width = (height >> fuji_layout) + fuji_width;
7135 raw_height = height;
7136 height = width - 1;
7137 load_raw = &CLASS fuji_load_raw;
7138 if (!(fuji_width & 1)) filters = 0x49494949;
7139 } else if (!strcmp(model,"RD175")) {
7140 height = 986;
7141 width = 1534;
7142 data_offset = 513;
7143 filters = 0x61616161;
7144 load_raw = &CLASS minolta_rd175_load_raw;
7145 } else if (!strcmp(model,"KD-400Z")) {
7146 height = 1712;
7147 width = 2312;
7148 raw_width = 2336;
7149 goto konica_400z;
7150 } else if (!strcmp(model,"KD-510Z")) {
7151 goto konica_510z;
7152 } else if (!strcasecmp(make,"MINOLTA")) {
7153 load_raw = &CLASS unpacked_load_raw;
7154 maximum = 0xfff;
7155 if (!strncmp(model,"DiMAGE A",8)) {
7156 if (!strcmp(model,"DiMAGE A200"))
7157 filters = 0x49494949;
7158 load_raw = &CLASS packed_12_load_raw;
7159 } else if (!strncmp(model,"ALPHA",5) ||
7160 !strncmp(model,"DYNAX",5) ||
7161 !strncmp(model,"MAXXUM",6)) {
7162 sprintf (model+20, "DYNAX %-10s", model+6+(model[0]=='M'));
7163 adobe_coeff (make, model+20);
7164 load_raw = &CLASS packed_12_load_raw;
7165 } else if (!strncmp(model,"DiMAGE G",8)) {
7166 if (model[8] == '4') {
7167 height = 1716;
7168 width = 2304;
7169 } else if (model[8] == '5') {
7170 konica_510z:
7171 height = 1956;
7172 width = 2607;
7173 raw_width = 2624;
7174 } else if (model[8] == '6') {
7175 height = 2136;
7176 width = 2848;
7178 data_offset += 14;
7179 filters = 0x61616161;
7180 konica_400z:
7181 load_raw = &CLASS unpacked_load_raw;
7182 maximum = 0x3df;
7183 order = 0x4d4d;
7185 } else if (!strcmp(model,"*ist DS")) {
7186 height -= 2;
7187 } else if (!strcmp(model,"K20D")) {
7188 filters = 0x16161616;
7189 } else if (!strcmp(model,"Optio S")) {
7190 if (fsize == 3178560) {
7191 height = 1540;
7192 width = 2064;
7193 load_raw = &CLASS eight_bit_load_raw;
7194 cam_mul[0] *= 4;
7195 cam_mul[2] *= 4;
7196 pre_mul[0] = 1.391;
7197 pre_mul[2] = 1.188;
7198 #ifdef LIBRAW_LIBRARY_BUILD
7199 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7200 #endif
7201 } else {
7202 height = 1544;
7203 width = 2068;
7204 raw_width = 3136;
7205 load_raw = &CLASS packed_12_load_raw;
7206 maximum = 0xf7c;
7207 pre_mul[0] = 1.137;
7208 pre_mul[2] = 1.453;
7209 #ifdef LIBRAW_LIBRARY_BUILD
7210 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7211 #endif
7213 } else if (fsize == 6114240) {
7214 height = 1737;
7215 width = 2324;
7216 raw_width = 3520;
7217 load_raw = &CLASS packed_12_load_raw;
7218 maximum = 0xf7a;
7219 pre_mul[0] = 1.980;
7220 pre_mul[2] = 1.570;
7221 #ifdef LIBRAW_LIBRARY_BUILD
7222 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7223 #endif
7224 } else if (!strcmp(model,"Optio 750Z")) {
7225 height = 2302;
7226 width = 3072;
7227 load_raw = &CLASS packed_12_load_raw;
7228 load_flags = 7;
7229 } else if (!strcmp(model,"S85")) {
7230 height = 2448;
7231 width = 3264;
7232 raw_width = fsize/height/2;
7233 order = 0x4d4d;
7234 load_raw = &CLASS unpacked_load_raw;
7235 maximum = 0xffff;
7236 } else if (!strcmp(model,"STV680 VGA")) {
7237 height = 484;
7238 width = 644;
7239 load_raw = &CLASS eight_bit_load_raw;
7240 flip = 2;
7241 filters = 0x16161616;
7242 black = 16;
7243 pre_mul[0] = 1.097;
7244 pre_mul[2] = 1.128;
7245 #ifdef LIBRAW_LIBRARY_BUILD
7246 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7247 #endif
7248 } else if (!strcmp(model,"KAI-0340")) {
7249 height = 477;
7250 width = 640;
7251 order = 0x4949;
7252 data_offset = 3840;
7253 load_raw = &CLASS unpacked_load_raw;
7254 pre_mul[0] = 1.561;
7255 pre_mul[2] = 2.454;
7256 #ifdef LIBRAW_LIBRARY_BUILD
7257 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7258 #endif
7259 } else if (!strcmp(model,"N95")) {
7260 height = raw_height - (top_margin = 2);
7261 } else if (!strcmp(model,"531C")) {
7262 height = 1200;
7263 width = 1600;
7264 load_raw = &CLASS unpacked_load_raw;
7265 filters = 0x49494949;
7266 pre_mul[1] = 1.218;
7267 #ifdef LIBRAW_LIBRARY_BUILD
7268 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7269 #endif
7270 } else if (!strcmp(model,"F-080C")) {
7271 height = 768;
7272 width = 1024;
7273 load_raw = &CLASS eight_bit_load_raw;
7274 } else if (!strcmp(model,"F-145C")) {
7275 height = 1040;
7276 width = 1392;
7277 load_raw = &CLASS eight_bit_load_raw;
7278 } else if (!strcmp(model,"F-201C")) {
7279 height = 1200;
7280 width = 1600;
7281 load_raw = &CLASS eight_bit_load_raw;
7282 } else if (!strcmp(model,"F-510C")) {
7283 height = 1958;
7284 width = 2588;
7285 load_raw = fsize < 7500000 ?
7286 &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
7287 maximum = 0xfff0;
7288 } else if (!strcmp(model,"F-810C")) {
7289 height = 2469;
7290 width = 3272;
7291 load_raw = &CLASS unpacked_load_raw;
7292 maximum = 0xfff0;
7293 } else if (!strcmp(model,"XCD-SX910CR")) {
7294 height = 1024;
7295 width = 1375;
7296 raw_width = 1376;
7297 filters = 0x49494949;
7298 maximum = 0x3ff;
7299 load_raw = fsize < 2000000 ?
7300 &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
7301 } else if (!strcmp(model,"2010")) {
7302 height = 1207;
7303 width = 1608;
7304 order = 0x4949;
7305 filters = 0x16161616;
7306 data_offset = 3212;
7307 maximum = 0x3ff;
7308 load_raw = &CLASS unpacked_load_raw;
7309 } else if (!strcmp(model,"A782")) {
7310 height = 3000;
7311 width = 2208;
7312 filters = 0x61616161;
7313 load_raw = fsize < 10000000 ?
7314 &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
7315 maximum = 0xffc0;
7316 } else if (!strcmp(model,"3320AF")) {
7317 height = 1536;
7318 raw_width = width = 2048;
7319 filters = 0x61616161;
7320 load_raw = &CLASS unpacked_load_raw;
7321 maximum = 0x3ff;
7322 pre_mul[0] = 1.717;
7323 pre_mul[2] = 1.138;
7324 #ifdef LIBRAW_LIBRARY_BUILD
7325 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7326 #endif
7327 fseek (ifp, 0x300000, SEEK_SET);
7328 if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
7329 height -= (top_margin = 16);
7330 width -= (left_margin = 28);
7331 maximum = 0xf5c0;
7332 strcpy (make, "ISG");
7333 model[0] = 0;
7335 } else if (!strcmp(make,"Hasselblad")) {
7336 if (load_raw == &CLASS lossless_jpeg_load_raw)
7337 load_raw = &CLASS hasselblad_load_raw;
7338 if (raw_width == 7262) {
7339 height = 5444;
7340 width = 7248;
7341 top_margin = 4;
7342 left_margin = 7;
7343 filters = 0x61616161;
7344 } else if (raw_width == 4090) {
7345 strcpy (model, "V96C");
7346 height -= (top_margin = 6);
7347 width -= (left_margin = 3) + 7;
7348 filters = 0x61616161;
7350 } else if (!strcmp(make,"Sinar")) {
7351 if (!memcmp(head,"8BPS",4)) {
7352 fseek (ifp, 14, SEEK_SET);
7353 height = get4();
7354 width = get4();
7355 filters = 0x61616161;
7356 data_offset = 68;
7358 if (!load_raw) load_raw = &CLASS unpacked_load_raw;
7359 maximum = 0x3fff;
7360 } else if (!strcmp(make,"Leaf")) {
7361 maximum = 0x3fff;
7362 fseek (ifp, data_offset, SEEK_SET);
7363 if (ljpeg_start (&jh, 1) && jh.bits == 15)
7364 maximum = 0x1fff;
7365 if (tiff_samples > 1) filters = 0;
7366 if (tiff_samples > 1 || tile_length < raw_height)
7367 load_raw = &CLASS leaf_hdr_load_raw;
7368 if ((width | height) == 2048) {
7369 if (tiff_samples == 1) {
7370 filters = 1;
7371 strcpy (cdesc, "RBTG");
7372 strcpy (model, "CatchLight");
7373 top_margin = 8; left_margin = 18; height = 2032; width = 2016;
7374 } else {
7375 strcpy (model, "DCB2");
7376 top_margin = 10; left_margin = 16; height = 2028; width = 2022;
7378 } else if (width+height == 3144+2060) {
7379 if (!model[0]) strcpy (model, "Cantare");
7380 if (width > height) {
7381 top_margin = 6; left_margin = 32; height = 2048; width = 3072;
7382 filters = 0x61616161;
7383 } else {
7384 left_margin = 6; top_margin = 32; width = 2048; height = 3072;
7385 filters = 0x16161616;
7387 if (!cam_mul[0] || model[0] == 'V') filters = 0;
7388 else is_raw = tiff_samples;
7389 } else if (width == 2116) {
7390 strcpy (model, "Valeo 6");
7391 height -= 2 * (top_margin = 30);
7392 width -= 2 * (left_margin = 55);
7393 filters = 0x49494949;
7394 } else if (width == 3171) {
7395 strcpy (model, "Valeo 6");
7396 height -= 2 * (top_margin = 24);
7397 width -= 2 * (left_margin = 24);
7398 filters = 0x16161616;
7400 } else if (!strcmp(make,"LEICA") || !strcmp(make,"Panasonic")) {
7401 maximum = 0xfff0;
7402 if ((fsize-data_offset) / (width*8/7) == height)
7403 load_raw = &CLASS panasonic_load_raw;
7404 if (!load_raw) load_raw = &CLASS unpacked_load_raw;
7405 switch (width) {
7406 case 2568:
7407 adobe_coeff ("Panasonic","DMC-LC1"); break;
7408 case 3130:
7409 left_margin = -14;
7410 case 3170:
7411 left_margin += 18;
7412 width = 3096;
7413 if (height > 2326) {
7414 height = 2326;
7415 top_margin = 13;
7416 filters = 0x49494949;
7418 zero_is_bad = 1;
7419 adobe_coeff ("Panasonic","DMC-FZ8"); break;
7420 case 3213:
7421 width -= 27;
7422 case 3177:
7423 width -= 10;
7424 filters = 0x49494949;
7425 zero_is_bad = 1;
7426 adobe_coeff ("Panasonic","DMC-L1"); break;
7427 case 3304:
7428 width -= 17;
7429 zero_is_bad = 1;
7430 adobe_coeff ("Panasonic","DMC-FZ30"); break;
7431 case 3330:
7432 width += 43;
7433 left_margin = -6;
7434 maximum = 0xf7f0;
7435 case 3370:
7436 width -= 82;
7437 left_margin += 15;
7438 if (height > 2480)
7439 height = 2480 - (top_margin = 10);
7440 filters = 0x49494949;
7441 zero_is_bad = 1;
7442 adobe_coeff ("Panasonic","DMC-FZ18"); break;
7443 case 3690:
7444 height -= 2;
7445 left_margin = -14;
7446 maximum = 0xf7f0;
7447 case 3770:
7448 width = 3672;
7449 if (--height == 2798 && (height = 2760))
7450 top_margin = 15;
7451 else filters = 0x49494949;
7452 left_margin += 17;
7453 zero_is_bad = 1;
7454 adobe_coeff ("Panasonic","DMC-FZ50"); break;
7455 case 3710:
7456 width = 3682;
7457 filters = 0x49494949;
7458 adobe_coeff ("Panasonic","DMC-L10"); break;
7459 case 3724:
7460 width -= 14;
7461 case 3836:
7462 width -= 42;
7463 lx3: filters = 0x16161616;
7464 if (make[0] != 'P')
7465 adobe_coeff ("Panasonic","DMC-LX3");
7466 break;
7467 case 3880:
7468 width -= 22;
7469 left_margin = 6;
7470 zero_is_bad = 1;
7471 adobe_coeff ("Panasonic","DMC-LX1"); break;
7472 case 4060:
7473 width = 3982;
7474 if (height == 2250) goto lx3;
7475 width = 4018;
7476 filters = 0x49494949;
7477 zero_is_bad = 1;
7478 adobe_coeff ("Panasonic","DMC-G1"); break;
7479 case 4290:
7480 height += 38;
7481 left_margin = -14;
7482 filters = 0x49494949;
7483 case 4330:
7484 width = 4248;
7485 if ((height -= 39) == 2400)
7486 top_margin = 15;
7487 left_margin += 17;
7488 adobe_coeff ("Panasonic","DMC-LX2"); break;
7489 case 4508:
7490 height -= 6;
7491 width = 4429;
7492 filters = 0x16161616;
7493 adobe_coeff ("Panasonic","DMC-FX150"); break;
7495 } else if (!strcmp(model,"C770UZ")) {
7496 height = 1718;
7497 width = 2304;
7498 filters = 0x16161616;
7499 load_raw = &CLASS packed_12_load_raw;
7500 load_flags = 7;
7501 } else if (!strcmp(make,"OLYMPUS")) {
7502 height += height & 1;
7503 filters = exif_cfa;
7504 if (load_raw == &CLASS olympus_e410_load_raw) {
7505 black >>= 4;
7506 } else if (!strcmp(model,"E-10") ||
7507 !strncmp(model,"E-20",4)) {
7508 black <<= 2;
7509 } else if (!strcmp(model,"E-300") ||
7510 !strcmp(model,"E-500")) {
7511 width -= 20;
7512 if (load_raw == &CLASS unpacked_load_raw) {
7513 maximum = 0xfc30;
7514 black = 0;
7516 } else if (!strcmp(model,"E-330")) {
7517 width -= 30;
7518 if (load_raw == &CLASS unpacked_load_raw)
7519 maximum = 0xf790;
7520 } else if (!strcmp(model,"SP550UZ")) {
7521 thumb_length = fsize - (thumb_offset = 0xa39800);
7522 thumb_height = 480;
7523 thumb_width = 640;
7525 } else if (!strcmp(model,"N Digital")) {
7526 height = 2047;
7527 width = 3072;
7528 filters = 0x61616161;
7529 data_offset = 0x1a00;
7530 load_raw = &CLASS packed_12_load_raw;
7531 } else if (!strcmp(model,"DSC-F828")) {
7532 width = 3288;
7533 left_margin = 5;
7534 data_offset = 862144;
7535 load_raw = &CLASS sony_load_raw;
7536 filters = 0x9c9c9c9c;
7537 colors = 4;
7538 strcpy (cdesc, "RGBE");
7539 } else if (!strcmp(model,"DSC-V3")) {
7540 width = 3109;
7541 left_margin = 59;
7542 data_offset = 787392;
7543 load_raw = &CLASS sony_load_raw;
7544 } else if (!strcmp(make,"SONY") && raw_width == 3984) {
7545 adobe_coeff ("SONY","DSC-R1");
7546 width = 3925;
7547 order = 0x4d4d;
7548 } else if (!strcmp(model,"DSLR-A100")) {
7549 height--;
7550 } else if (!strcmp(model,"DSLR-A350")) {
7551 height -= 4;
7552 } else if (!strcmp(model,"C603v")) {
7553 height = 480;
7554 width = 640;
7555 goto c603v;
7556 } else if (!strcmp(model,"C603y")) {
7557 height = 2134;
7558 width = 2848;
7559 c603v:
7560 filters = 0;
7561 load_raw = &CLASS kodak_yrgb_load_raw;
7562 } else if (!strcmp(model,"C603")) {
7563 raw_height = height = 2152;
7564 raw_width = width = 2864;
7565 goto c603;
7566 } else if (!strcmp(model,"C330")) {
7567 height = 1744;
7568 width = 2336;
7569 raw_height = 1779;
7570 raw_width = 2338;
7571 top_margin = 33;
7572 left_margin = 1;
7573 c603:
7574 order = 0x4949;
7575 if ((data_offset = fsize - raw_height*raw_width)) {
7576 fseek (ifp, 168, SEEK_SET);
7577 read_shorts (curve, 256);
7578 #ifdef LIBRAW_LIBRARY_BUILD
7579 color_flags.curve_state = LIBRAW_COLORSTATE_LOADED;
7580 #endif
7581 } else use_gamma = 0;
7582 load_raw = &CLASS eight_bit_load_raw;
7583 } else if (!strcasecmp(make,"KODAK")) {
7584 if (filters == UINT_MAX) filters = 0x61616161;
7585 if (!strncmp(model,"NC2000",6)) {
7586 width -= 4;
7587 left_margin = 2;
7588 } else if (!strcmp(model,"EOSDCS3B")) {
7589 width -= 4;
7590 left_margin = 2;
7591 } else if (!strcmp(model,"EOSDCS1")) {
7592 width -= 4;
7593 left_margin = 2;
7594 } else if (!strcmp(model,"DCS420")) {
7595 width -= 4;
7596 left_margin = 2;
7597 } else if (!strcmp(model,"DCS460")) {
7598 width -= 4;
7599 left_margin = 2;
7600 } else if (!strcmp(model,"DCS460A")) {
7601 width -= 4;
7602 left_margin = 2;
7603 colors = 1;
7604 filters = 0;
7605 } else if (!strcmp(model,"DCS660M")) {
7606 black = 214;
7607 colors = 1;
7608 filters = 0;
7609 } else if (!strcmp(model,"DCS760M")) {
7610 colors = 1;
7611 filters = 0;
7613 if (!strcmp(model+4,"20X"))
7614 strcpy (cdesc, "MYCY");
7615 if (strstr(model,"DC25")) {
7616 strcpy (model, "DC25");
7617 data_offset = 15424;
7619 if (!strncmp(model,"DC2",3)) {
7620 height = 242;
7621 if (fsize < 100000) {
7622 raw_width = 256; width = 249;
7623 pixel_aspect = (4.0*height) / (3.0*width);
7624 } else {
7625 raw_width = 512; width = 501;
7626 pixel_aspect = (493.0*height) / (373.0*width);
7628 data_offset += raw_width + 1;
7629 colors = 4;
7630 filters = 0x8d8d8d8d;
7631 simple_coeff(1);
7632 pre_mul[1] = 1.179;
7633 pre_mul[2] = 1.209;
7634 pre_mul[3] = 1.036;
7635 #ifdef LIBRAW_LIBRARY_BUILD
7636 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7637 #endif
7638 load_raw = &CLASS eight_bit_load_raw;
7639 } else if (!strcmp(model,"40")) {
7640 strcpy (model, "DC40");
7641 height = 512;
7642 width = 768;
7643 data_offset = 1152;
7644 load_raw = &CLASS kodak_radc_load_raw;
7645 } else if (strstr(model,"DC50")) {
7646 strcpy (model, "DC50");
7647 height = 512;
7648 width = 768;
7649 data_offset = 19712;
7650 load_raw = &CLASS kodak_radc_load_raw;
7651 } else if (strstr(model,"DC120")) {
7652 strcpy (model, "DC120");
7653 height = 976;
7654 width = 848;
7655 pixel_aspect = height/0.75/width;
7656 load_raw = tiff_compress == 7 ?
7657 &CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
7658 } else if (!strcmp(model,"DCS200")) {
7659 thumb_height = 128;
7660 thumb_width = 192;
7661 thumb_offset = 6144;
7662 thumb_misc = 360;
7663 write_thumb = &CLASS layer_thumb;
7664 height = 1024;
7665 width = 1536;
7666 data_offset = 79872;
7667 load_raw = &CLASS eight_bit_load_raw;
7668 black = 17;
7670 } else if (!strcmp(model,"Fotoman Pixtura")) {
7671 height = 512;
7672 width = 768;
7673 data_offset = 3632;
7674 load_raw = &CLASS kodak_radc_load_raw;
7675 filters = 0x61616161;
7676 simple_coeff(2);
7677 } else if (!strcmp(model,"QuickTake 100")) {
7678 fseek (ifp, 544, SEEK_SET);
7679 height = get2();
7680 width = get2();
7681 data_offset = (get4(),get2()) == 30 ? 738:736;
7682 if (height > width) {
7683 SWAP(height,width);
7684 fseek (ifp, data_offset-6, SEEK_SET);
7685 flip = ~get2() & 3 ? 5:6;
7687 load_raw = &CLASS quicktake_100_load_raw;
7688 filters = 0x61616161;
7689 } else if (!strcmp(model,"QuickTake 150")) {
7690 data_offset = 738 - head[5];
7691 if (head[5]) strcpy (model+10, "200");
7692 load_raw = &CLASS kodak_radc_load_raw;
7693 height = 480;
7694 width = 640;
7695 filters = 0x61616161;
7696 } else if (!strcmp(make,"Rollei") && !load_raw) {
7697 switch (raw_width) {
7698 case 1316:
7699 height = 1030;
7700 width = 1300;
7701 top_margin = 1;
7702 left_margin = 6;
7703 break;
7704 case 2568:
7705 height = 1960;
7706 width = 2560;
7707 top_margin = 2;
7708 left_margin = 8;
7710 filters = 0x16161616;
7711 load_raw = &CLASS rollei_load_raw;
7712 pre_mul[0] = 1.8;
7713 pre_mul[2] = 1.3;
7714 #ifdef LIBRAW_LIBRARY_BUILD
7715 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7716 #endif
7717 } else if (!strcmp(model,"PC-CAM 600")) {
7718 height = 768;
7719 data_offset = width = 1024;
7720 filters = 0x49494949;
7721 load_raw = &CLASS eight_bit_load_raw;
7722 pre_mul[0] = 1.14;
7723 pre_mul[2] = 2.73;
7724 #ifdef LIBRAW_LIBRARY_BUILD
7725 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7726 #endif
7727 } else if (!strcmp(model,"QV-2000UX")) {
7728 height = 1208;
7729 width = 1632;
7730 data_offset = width * 2;
7731 load_raw = &CLASS eight_bit_load_raw;
7732 } else if (fsize == 3217760) {
7733 height = 1546;
7734 width = 2070;
7735 raw_width = 2080;
7736 load_raw = &CLASS eight_bit_load_raw;
7737 } else if (!strcmp(model,"QV-4000")) {
7738 height = 1700;
7739 width = 2260;
7740 load_raw = &CLASS unpacked_load_raw;
7741 maximum = 0xffff;
7742 } else if (!strcmp(model,"QV-5700")) {
7743 height = 1924;
7744 width = 2576;
7745 load_raw = &CLASS casio_qv5700_load_raw;
7746 } else if (!strcmp(model,"QV-R41")) {
7747 height = 1720;
7748 width = 2312;
7749 raw_width = 3520;
7750 left_margin = 2;
7751 } else if (!strcmp(model,"QV-R51")) {
7752 height = 1926;
7753 width = 2580;
7754 raw_width = 3904;
7755 pre_mul[0] = 1.340;
7756 pre_mul[2] = 1.672;
7757 #ifdef LIBRAW_LIBRARY_BUILD
7758 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7759 #endif
7760 } else if (!strcmp(model,"EX-S100")) {
7761 height = 1544;
7762 width = 2058;
7763 raw_width = 3136;
7764 pre_mul[0] = 1.631;
7765 pre_mul[2] = 1.106;
7766 #ifdef LIBRAW_LIBRARY_BUILD
7767 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7768 #endif
7769 } else if (!strcmp(model,"EX-Z50")) {
7770 height = 1931;
7771 width = 2570;
7772 raw_width = 3904;
7773 pre_mul[0] = 2.529;
7774 pre_mul[2] = 1.185;
7775 #ifdef LIBRAW_LIBRARY_BUILD
7776 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7777 #endif
7778 } else if (!strcmp(model,"EX-Z55")) {
7779 height = 1960;
7780 width = 2570;
7781 raw_width = 3904;
7782 pre_mul[0] = 1.520;
7783 pre_mul[2] = 1.316;
7784 #ifdef LIBRAW_LIBRARY_BUILD
7785 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7786 #endif
7787 } else if (!strcmp(model,"EX-P505")) {
7788 height = 1928;
7789 width = 2568;
7790 raw_width = 3852;
7791 maximum = 0xfff;
7792 pre_mul[0] = 2.07;
7793 pre_mul[2] = 1.88;
7794 #ifdef LIBRAW_LIBRARY_BUILD
7795 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7796 #endif
7797 } else if (fsize == 9313536) { /* EX-P600 or QV-R61 */
7798 height = 2142;
7799 width = 2844;
7800 raw_width = 4288;
7801 pre_mul[0] = 1.797;
7802 pre_mul[2] = 1.219;
7803 #ifdef LIBRAW_LIBRARY_BUILD
7804 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7805 #endif
7806 } else if (!strcmp(model,"EX-P700")) {
7807 height = 2318;
7808 width = 3082;
7809 raw_width = 4672;
7810 pre_mul[0] = 1.758;
7811 pre_mul[2] = 1.504;
7812 #ifdef LIBRAW_LIBRARY_BUILD
7813 color_flags.pre_mul_state = LIBRAW_COLORSTATE_CONST;
7814 #endif
7816 if (!model[0])
7817 sprintf (model, "%dx%d", width, height);
7818 if (filters == UINT_MAX) filters = 0x94949494;
7819 if (raw_color) adobe_coeff (make, model);
7820 if (thumb_offset && !thumb_height) {
7821 fseek (ifp, thumb_offset, SEEK_SET);
7822 if (ljpeg_start (&jh, 1)) {
7823 thumb_width = jh.wide;
7824 thumb_height = jh.high;
7827 dng_skip:
7828 if (!load_raw || height < 22) is_raw = 0;
7829 #ifdef NO_JPEG
7830 if (load_raw == &CLASS kodak_jpeg_load_raw) {
7831 #ifdef DCRAW_VERBOSE
7832 fprintf (stderr,_("%s: You must link dcraw with libjpeg!!\n"), ifname);
7833 #endif
7834 is_raw = 0;
7835 #ifdef LIBRAW_LIBRARY_BUILD
7836 imgdata.process_warnings |= LIBRAW_WARN_NO_JPEGLIB;
7837 #endif
7839 #endif
7840 if (!cdesc[0])
7841 strcpy (cdesc, colors == 3 ? "RGB":"GMCY");
7842 if (!raw_height) raw_height = height;
7843 if (!raw_width ) raw_width = width;
7844 if (filters && colors == 3)
7845 for (i=0; i < 32; i+=4) {
7846 if ((filters >> i & 15) == 9)
7847 filters |= 2 << i;
7848 if ((filters >> i & 15) == 6)
7849 filters |= 8 << i;
7851 notraw:
7852 if (flip == -1) flip = tiff_flip;
7853 if (flip == -1) flip = 0;
7854 #ifdef LIBRAW_LIBRARY_BUILD
7855 RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY,1,2);
7856 #endif
7858 #line 9223 "dcraw/dcraw.c"
7859 void CLASS convert_to_rgb()
7861 int row, col, c, i, j, k;
7862 ushort *img;
7863 float out[3], out_cam[3][4];
7864 double num, inverse[3][3], bnd[2]={0,0};
7865 static const double xyzd50_srgb[3][3] =
7866 { { 0.436083, 0.385083, 0.143055 },
7867 { 0.222507, 0.716888, 0.060608 },
7868 { 0.013930, 0.097097, 0.714022 } };
7869 static const double rgb_rgb[3][3] =
7870 { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
7871 static const double adobe_rgb[3][3] =
7872 { { 0.715146, 0.284856, 0.000000 },
7873 { 0.000000, 1.000000, 0.000000 },
7874 { 0.000000, 0.041166, 0.958839 } };
7875 static const double wide_rgb[3][3] =
7876 { { 0.593087, 0.404710, 0.002206 },
7877 { 0.095413, 0.843149, 0.061439 },
7878 { 0.011621, 0.069091, 0.919288 } };
7879 static const double prophoto_rgb[3][3] =
7880 { { 0.529317, 0.330092, 0.140588 },
7881 { 0.098368, 0.873465, 0.028169 },
7882 { 0.016879, 0.117663, 0.865457 } };
7883 static const double (*out_rgb[])[3] =
7884 { rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb };
7885 static const char *name[] =
7886 { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" };
7887 static const unsigned phead[] =
7888 { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
7889 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
7890 unsigned pbody[] =
7891 { 10, 0x63707274, 0, 36, /* cprt */
7892 0x64657363, 0, 40, /* desc */
7893 0x77747074, 0, 20, /* wtpt */
7894 0x626b7074, 0, 20, /* bkpt */
7895 0x72545243, 0, 14, /* rTRC */
7896 0x67545243, 0, 14, /* gTRC */
7897 0x62545243, 0, 14, /* bTRC */
7898 0x7258595a, 0, 20, /* rXYZ */
7899 0x6758595a, 0, 20, /* gXYZ */
7900 0x6258595a, 0, 20 }; /* bXYZ */
7901 static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc };
7902 unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 };
7904 #ifdef LIBRAW_LIBRARY_BUILD
7905 RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,0,2);
7906 #endif
7908 bnd[gamm[1] >= 1] = 1;
7909 if (gamm[1] && (gamm[1]-1)*(gamm[0]-1) <= 0) {
7910 for (i=0; i < 36; i++) {
7911 gamm[2] = (bnd[0] + bnd[1])/2;
7912 bnd[(pow(gamm[2]/gamm[1],-gamm[0])-1)/gamm[0]-1/gamm[2] > -1] = gamm[2];
7914 gamm[3] = gamm[2]*(1/gamm[0]-1);
7915 gamm[2] /= gamm[1];
7917 gamm[4] = 1 / (gamm[1]/2*SQR(gamm[2]) - gamm[3]*(1-gamm[2]) +
7918 (1-pow(gamm[2],1+gamm[0]))*(1+gamm[3])/(1+gamm[0])) - 1;
7920 memcpy (out_cam, rgb_cam, sizeof out_cam);
7921 raw_color |= colors == 1 || document_mode ||
7922 output_color < 1 || output_color > 5;
7923 if (!raw_color) {
7924 oprof = (unsigned *) calloc (phead[0], 1);
7925 merror (oprof, "convert_to_rgb()");
7926 memcpy (oprof, phead, sizeof phead);
7927 if (output_color == 5) oprof[4] = oprof[5];
7928 oprof[0] = 132 + 12*pbody[0];
7929 for (i=0; i < pbody[0]; i++) {
7930 oprof[oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
7931 pbody[i*3+2] = oprof[0];
7932 oprof[0] += (pbody[i*3+3] + 3) & -4;
7934 memcpy (oprof+32, pbody, sizeof pbody);
7935 oprof[pbody[5]/4+2] = strlen(name[output_color-1]) + 1;
7936 memcpy ((char *)oprof+pbody[8]+8, pwhite, sizeof pwhite);
7937 if (output_bps == 8 | gamma_16bit)
7938 pcurve[3] = (short)(256/gamm[4]+0.5) << 16;
7939 for (i=4; i < 7; i++)
7940 memcpy ((char *)oprof+pbody[i*3+2], pcurve, sizeof pcurve);
7941 pseudoinverse ((double (*)[3]) out_rgb[output_color-1], inverse, 3);
7942 for (i=0; i < 3; i++)
7943 for (j=0; j < 3; j++) {
7944 for (num = k=0; k < 3; k++)
7945 num += xyzd50_srgb[i][k] * inverse[j][k];
7946 oprof[pbody[j*3+23]/4+i+2] = num * 0x10000 + 0.5;
7948 for (i=0; i < phead[0]/4; i++)
7949 oprof[i] = htonl(oprof[i]);
7950 strcpy ((char *)oprof+pbody[2]+8, "auto-generated by dcraw");
7951 strcpy ((char *)oprof+pbody[5]+12, name[output_color-1]);
7952 for (i=0; i < 3; i++)
7953 for (j=0; j < colors; j++)
7954 for (out_cam[i][j] = k=0; k < 3; k++)
7955 out_cam[i][j] += out_rgb[output_color-1][i][k] * rgb_cam[k][j];
7957 #ifdef DCRAW_VERBOSE
7958 if (verbose)
7959 fprintf (stderr, raw_color ? _("Building histograms...\n") :
7960 _("Converting to %s colorspace...\n"), name[output_color-1]);
7962 #endif
7963 #ifdef LIBRAW_LIBRARY_BUILD
7964 memset(histogram,0,sizeof(int)*LIBRAW_HISTOGRAM_SIZE*4);
7965 #else
7966 memset (histogram, 0, sizeof histogram);
7967 #endif
7968 for (img=image[0], row=0; row < height; row++)
7969 for (col=0; col < width; col++, img+=4) {
7970 if (!raw_color) {
7971 out[0] = out[1] = out[2] = 0;
7972 FORCC {
7973 out[0] += out_cam[0][c] * img[c];
7974 out[1] += out_cam[1][c] * img[c];
7975 out[2] += out_cam[2][c] * img[c];
7977 FORC3 img[c] = CLIP((int) out[c]);
7979 else if (document_mode)
7980 img[0] = img[FC(row,col)];
7981 FORCC histogram[c][img[c] >> 3]++;
7983 if (colors == 4 && output_color) colors = 3;
7984 if (document_mode && filters) colors = 1;
7985 #ifdef LIBRAW_LIBRARY_BUILD
7986 RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB,1,2);
7987 #endif
7990 void CLASS fuji_rotate()
7992 int i, row, col;
7993 double step;
7994 float r, c, fr, fc;
7995 unsigned ur, uc;
7996 ushort wide, high, (*img)[4], (*pix)[4];
7998 if (!fuji_width) return;
7999 #ifdef DCRAW_VERBOSE
8000 if (verbose)
8001 fprintf (stderr,_("Rotating image 45 degrees...\n"));
8002 #endif
8003 fuji_width = (fuji_width - 1 + shrink) >> shrink;
8004 step = sqrt(0.5);
8005 wide = fuji_width / step;
8006 high = (height - fuji_width) / step;
8007 img = (ushort (*)[4]) calloc (wide*high, sizeof *img);
8008 merror (img, "fuji_rotate()");
8010 #ifdef LIBRAW_LIBRARY_BUILD
8011 RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,0,2);
8012 #endif
8014 for (row=0; row < high; row++)
8015 for (col=0; col < wide; col++) {
8016 ur = r = fuji_width + (row-col)*step;
8017 uc = c = (row+col)*step;
8018 if (ur > height-2 || uc > width-2) continue;
8019 fr = r - ur;
8020 fc = c - uc;
8021 pix = image + ur*width + uc;
8022 for (i=0; i < colors; i++)
8023 img[row*wide+col][i] =
8024 (pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
8025 (pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr;
8027 free (image);
8028 width = wide;
8029 height = high;
8030 image = img;
8031 fuji_width = 0;
8032 #ifdef LIBRAW_LIBRARY_BUILD
8033 RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE,1,2);
8034 #endif
8037 void CLASS stretch()
8039 ushort newdim, (*img)[4], *pix0, *pix1;
8040 int row, col, c;
8041 double rc, frac;
8043 if (pixel_aspect == 1) return;
8044 #ifdef LIBRAW_LIBRARY_BUILD
8045 RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,0,2);
8046 #endif
8047 #ifdef DCRAW_VERBOSE
8048 if (verbose) fprintf (stderr,_("Stretching the image...\n"));
8049 #endif
8050 if (pixel_aspect < 1) {
8051 newdim = height / pixel_aspect + 0.5;
8052 img = (ushort (*)[4]) calloc (width*newdim, sizeof *img);
8053 merror (img, "stretch()");
8054 for (rc=row=0; row < newdim; row++, rc+=pixel_aspect) {
8055 frac = rc - (c = rc);
8056 pix0 = pix1 = image[c*width];
8057 if (c+1 < height) pix1 += width*4;
8058 for (col=0; col < width; col++, pix0+=4, pix1+=4)
8059 FORCC img[row*width+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
8061 height = newdim;
8062 } else {
8063 newdim = width * pixel_aspect + 0.5;
8064 img = (ushort (*)[4]) calloc (height*newdim, sizeof *img);
8065 merror (img, "stretch()");
8066 for (rc=col=0; col < newdim; col++, rc+=1/pixel_aspect) {
8067 frac = rc - (c = rc);
8068 pix0 = pix1 = image[c];
8069 if (c+1 < width) pix1 += 4;
8070 for (row=0; row < height; row++, pix0+=width*4, pix1+=width*4)
8071 FORCC img[row*newdim+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
8073 width = newdim;
8075 free (image);
8076 image = img;
8077 #ifdef LIBRAW_LIBRARY_BUILD
8078 RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH,1,2);
8079 #endif
8082 int CLASS flip_index (int row, int col)
8084 if (flip & 4) SWAP(row,col);
8085 if (flip & 2) row = iheight - 1 - row;
8086 if (flip & 1) col = iwidth - 1 - col;
8087 return row * iwidth + col;
8090 void CLASS gamma_lut (ushort lut[0x10000])
8092 int perc, c, val, total, i;
8093 float t_white=0, r;
8095 perc = width * height * 0.01; /* 99th percentile white level */
8096 if (fuji_width) perc /= 2;
8097 if ((highlight & ~2) || no_auto_bright) perc = -1;
8098 FORCC {
8099 for (val=0x2000, total=0; --val > 32; )
8100 if ((total += histogram[c][val]) > perc) break;
8101 if (t_white < val) t_white = val;
8103 t_white *= 8 / bright;
8104 for (i=0; i < 0x10000; i++) {
8105 r = i / t_white;
8106 val = 65535 * ( !use_gamma ? r :
8107 r <= gamm[2] ? r*gamm[1] : pow((double)r,gamm[0])*(1+gamm[3])-gamm[3]);
8108 if (val > 65535) val = 65535;
8109 lut[i] = val;
8114 #line 9503 "dcraw/dcraw.c"
8115 void CLASS tiff_set (ushort *ntag,
8116 ushort tag, ushort type, int count, int val)
8118 struct tiff_tag *tt;
8119 int c;
8121 tt = (struct tiff_tag *)(ntag+1) + (*ntag)++;
8122 tt->tag = tag;
8123 tt->type = type;
8124 tt->count = count;
8125 if (type < 3 && count <= 4)
8126 FORC(4) tt->val.c[c] = val >> (c << 3);
8127 else if (type == 3 && count <= 2)
8128 FORC(2) tt->val.s[c] = val >> (c << 4);
8129 else tt->val.i = val;
8132 #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
8134 void CLASS tiff_head (struct tiff_hdr *th, int full)
8136 int c, psize=0;
8137 struct tm *t;
8139 memset (th, 0, sizeof *th);
8140 th->t_order = htonl(0x4d4d4949) >> 16;
8141 th->magic = 42;
8142 th->ifd = 10;
8143 if (full) {
8144 tiff_set (&th->ntag, 254, 4, 1, 0);
8145 tiff_set (&th->ntag, 256, 4, 1, width);
8146 tiff_set (&th->ntag, 257, 4, 1, height);
8147 tiff_set (&th->ntag, 258, 3, colors, output_bps);
8148 if (colors > 2)
8149 th->tag[th->ntag-1].val.i = TOFF(th->bps);
8150 FORC4 th->bps[c] = output_bps;
8151 tiff_set (&th->ntag, 259, 3, 1, 1);
8152 tiff_set (&th->ntag, 262, 3, 1, 1 + (colors > 1));
8154 tiff_set (&th->ntag, 270, 2, 512, TOFF(th->t_desc));
8155 tiff_set (&th->ntag, 271, 2, 64, TOFF(th->t_make));
8156 tiff_set (&th->ntag, 272, 2, 64, TOFF(th->t_model));
8157 if (full) {
8158 if (oprof) psize = ntohl(oprof[0]);
8159 tiff_set (&th->ntag, 273, 4, 1, sizeof *th + psize);
8160 tiff_set (&th->ntag, 277, 3, 1, colors);
8161 tiff_set (&th->ntag, 278, 4, 1, height);
8162 tiff_set (&th->ntag, 279, 4, 1, height*width*colors*output_bps/8);
8163 } else
8164 tiff_set (&th->ntag, 274, 3, 1, "12435867"[flip]-'0');
8165 tiff_set (&th->ntag, 282, 5, 1, TOFF(th->rat[0]));
8166 tiff_set (&th->ntag, 283, 5, 1, TOFF(th->rat[2]));
8167 tiff_set (&th->ntag, 284, 3, 1, 1);
8168 tiff_set (&th->ntag, 296, 3, 1, 2);
8169 tiff_set (&th->ntag, 305, 2, 32, TOFF(th->soft));
8170 tiff_set (&th->ntag, 306, 2, 20, TOFF(th->date));
8171 tiff_set (&th->ntag, 315, 2, 64, TOFF(th->t_artist));
8172 tiff_set (&th->ntag, 34665, 4, 1, TOFF(th->nexif));
8173 if (psize) tiff_set (&th->ntag, 34675, 7, psize, sizeof *th);
8174 tiff_set (&th->nexif, 33434, 5, 1, TOFF(th->rat[4]));
8175 tiff_set (&th->nexif, 33437, 5, 1, TOFF(th->rat[6]));
8176 tiff_set (&th->nexif, 34855, 3, 1, iso_speed);
8177 tiff_set (&th->nexif, 37386, 5, 1, TOFF(th->rat[8]));
8178 if (gpsdata[1]) {
8179 tiff_set (&th->ntag, 34853, 4, 1, TOFF(th->ngps));
8180 tiff_set (&th->ngps, 0, 1, 4, 0x202);
8181 tiff_set (&th->ngps, 1, 2, 2, gpsdata[29]);
8182 tiff_set (&th->ngps, 2, 5, 3, TOFF(th->gps[0]));
8183 tiff_set (&th->ngps, 3, 2, 2, gpsdata[30]);
8184 tiff_set (&th->ngps, 4, 5, 3, TOFF(th->gps[6]));
8185 tiff_set (&th->ngps, 5, 1, 1, gpsdata[31]);
8186 tiff_set (&th->ngps, 6, 5, 1, TOFF(th->gps[18]));
8187 tiff_set (&th->ngps, 7, 5, 3, TOFF(th->gps[12]));
8188 tiff_set (&th->ngps, 18, 2, 12, TOFF(th->gps[20]));
8189 tiff_set (&th->ngps, 29, 2, 12, TOFF(th->gps[23]));
8190 memcpy (th->gps, gpsdata, sizeof th->gps);
8192 th->rat[0] = th->rat[2] = 300;
8193 th->rat[1] = th->rat[3] = 1;
8194 FORC(6) th->rat[4+c] = 1000000;
8195 th->rat[4] *= shutter;
8196 th->rat[6] *= aperture;
8197 th->rat[8] *= focal_len;
8198 strncpy (th->t_desc, desc, 512);
8199 strncpy (th->t_make, make, 64);
8200 strncpy (th->t_model, model, 64);
8201 strcpy (th->soft, "dcraw v"VERSION);
8202 t = gmtime (&timestamp);
8203 sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
8204 t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
8205 strncpy (th->t_artist, artist, 64);
8208 void CLASS jpeg_thumb_writer (FILE *tfp,char *t_humb,int t_humb_length)
8210 ushort exif[5];
8211 struct tiff_hdr th;
8212 fputc (0xff, tfp);
8213 fputc (0xd8, tfp);
8214 if (strcmp (t_humb+6, "Exif")) {
8215 memcpy (exif, "\xff\xe1 Exif\0\0", 10);
8216 exif[1] = htons (8 + sizeof th);
8217 fwrite (exif, 1, sizeof exif, tfp);
8218 tiff_head (&th, 0);
8219 fwrite (&th, 1, sizeof th, tfp);
8221 fwrite (t_humb+2, 1, t_humb_length-2, tfp);
8225 void CLASS jpeg_thumb (FILE *tfp)
8227 char *thumb;
8228 ushort exif[5];
8229 struct tiff_hdr th;
8231 thumb = (char *) malloc (thumb_length);
8232 merror (thumb, "jpeg_thumb()");
8233 fread (thumb, 1, thumb_length, ifp);
8234 #if 0
8235 fputc (0xff, tfp);
8236 fputc (0xd8, tfp);
8237 if (strcmp (thumb+6, "Exif")) {
8238 memcpy (exif, "\xff\xe1 Exif\0\0", 10);
8239 exif[1] = htons (8 + sizeof th);
8240 fwrite (exif, 1, sizeof exif, tfp);
8241 tiff_head (&th, 0);
8242 fwrite (&th, 1, sizeof th, tfp);
8244 fwrite (thumb+2, 1, thumb_length-2, tfp);
8245 #else
8246 jpeg_thumb_writer(tfp,thumb,thumb_length);
8247 #endif
8248 free (thumb);
8251 void CLASS write_ppm_tiff (FILE *ofp)
8253 struct tiff_hdr th;
8254 uchar *ppm;
8255 ushort *ppm2,lut16[0x10000];
8256 int c, row, col, soff, rstep, cstep;
8258 iheight = height;
8259 iwidth = width;
8260 if (flip & 4) SWAP(height,width);
8261 ppm = (uchar *) calloc (width, colors*output_bps/8);
8262 ppm2 = (ushort *) ppm;
8263 merror (ppm, "write_ppm_tiff()");
8264 if (output_tiff) {
8265 tiff_head (&th, 1);
8266 fwrite (&th, sizeof th, 1, ofp);
8267 if (oprof)
8268 fwrite (oprof, ntohl(oprof[0]), 1, ofp);
8269 } else if (colors > 3)
8270 fprintf (ofp,
8271 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
8272 width, height, colors, (1 << output_bps)-1, cdesc);
8273 else
8274 fprintf (ofp, "P%d\n%d %d\n%d\n",
8275 colors/2+5, width, height, (1 << output_bps)-1);
8277 if (output_bps == 8 || gamma_16bit ) gamma_lut (lut16);
8279 soff = flip_index (0, 0);
8280 cstep = flip_index (0, 1) - soff;
8281 rstep = flip_index (1, 0) - flip_index (0, width);
8282 for (row=0; row < height; row++, soff += rstep) {
8283 for (col=0; col < width; col++, soff += cstep)
8284 if (output_bps == 8)
8285 FORCC ppm [col*colors+c] = lut16[image[soff][c]]/256;
8286 else if(gamma_16bit) FORCC ppm2[col*colors+c] = lut16[image[soff][c]];
8287 else FORCC ppm2[col*colors+c] = image[soff][c];
8288 if (output_bps == 16 && !output_tiff && htons(0x55aa) != 0x55aa)
8289 swab ((char*)ppm2, (char*)ppm2, width*colors*2);
8290 fwrite (ppm, colors*output_bps/8, width, ofp);
8292 free (ppm);