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