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