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