2 GENERATED FILE, DO NOT EDIT
3 Generated from dcraw/dcraw.c at Fri Mar 13 22:27:06 2009
4 Look into original file (probably http://cybercom.net/~dcoffin/dcraw/dcraw.c)
5 for copyright information.
8 #line 260 "dcraw/dcraw.c"
10 #include "libraw/libraw_types.h"
11 #define LIBRAW_LIBRARY_BUILD
12 #define LIBRAW_IO_REDEFINED
13 #include "libraw/libraw.h"
14 #include "internal/defines.h"
15 #include "internal/var_defines.h"
17 #line 270 "dcraw/dcraw.c"
20 char *my_memmem (char *haystack
, size_t haystacklen
,
21 char *needle
, size_t needlelen
)
24 for (c
= haystack
; c
<= haystack
+ haystacklen
- needlelen
; c
++)
25 if (!memcmp (c
, needle
, needlelen
))
29 #define memmem my_memmem
32 #line 304 "dcraw/dcraw.c"
34 ushort CLASS
sget2 (uchar
*s
)
36 if (order
== 0x4949) /* "II" means little-endian */
37 return s
[0] | s
[1] << 8;
38 else /* "MM" means big-endian */
39 return s
[0] << 8 | s
[1];
44 uchar str
[2] = { 0xff,0xff };
45 fread (str
, 1, 2, ifp
);
49 unsigned CLASS
sget4 (uchar
*s
)
52 return s
[0] | s
[1] << 8 | s
[2] << 16 | s
[3] << 24;
54 return s
[0] << 24 | s
[1] << 16 | s
[2] << 8 | s
[3];
56 #define sget4(s) sget4((uchar *)s)
60 uchar str
[4] = { 0xff,0xff,0xff,0xff };
61 fread (str
, 1, 4, ifp
);
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
;
77 double CLASS
getreal (int type
)
79 union { char c
[8]; double d
; } u
;
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());
93 rev
= 7 * ((order
== 0x4949) == (ntohs(0x1234) == 0x1234));
95 u
.c
[i
^ rev
] = fgetc(ifp
);
97 default: return fgetc(ifp
);
101 void CLASS
read_shorts (ushort
*pixel
, int count
)
103 if (fread (pixel
, 2, count
, ifp
) < count
) derror();
104 if ((order
== 0x4949) == (ntohs(0x1234) == 0x1234))
105 swab ((char*)pixel
, (char*)pixel
, count
*2);
107 #line 381 "dcraw/dcraw.c"
108 void CLASS
canon_black (double dark
[2])
110 int c
, diff
, row
, col
;
112 if (raw_width
< width
+4) return;
113 FORC(2) dark
[c
] /= (raw_width
-width
-2) * height
>> 1;
114 #ifdef LIBRAW_LIBRARY_BUILD
115 if(!( filtering_mode
& LIBRAW_FILTERING_NOBLACKS
) )
118 if ((diff
= dark
[0] - dark
[1]))
119 for (row
=0; row
< height
; row
++)
120 for (col
=1; col
< width
; col
+=2)
121 BAYER(row
,col
) += diff
;
122 #ifdef LIBRAW_LIBRARY_BUILD
126 black
= (dark
[0] + dark
[1] + 1) / 2;
129 void CLASS
canon_600_fixed_wb (int temp
)
131 static const short mul
[4][5] = {
132 { 667, 358,397,565,452 },
133 { 731, 390,367,499,517 },
134 { 1119, 396,348,448,537 },
135 { 1399, 485,431,508,688 } };
140 if (*mul
[lo
] <= temp
) break;
141 for (hi
=0; hi
< 3; hi
++)
142 if (*mul
[hi
] >= temp
) break;
144 frac
= (float) (temp
- *mul
[lo
]) / (*mul
[hi
] - *mul
[lo
]);
145 for (i
=1; i
< 5; i
++)
146 pre_mul
[i
-1] = 1 / (frac
* mul
[hi
][i
] + (1-frac
) * mul
[lo
][i
]);
147 #ifdef LIBRAW_LIBRARY_BUILD
148 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
152 /* Return values: 0 = white 1 = near white 2 = not white */
153 int CLASS
canon_600_color (int ratio
[2], int mar
)
155 int clipped
=0, target
, miss
;
159 { ratio
[1] = -104; clipped
= 1; }
161 { ratio
[1] = 12; clipped
= 1; }
163 if (ratio
[1] < -264 || ratio
[1] > 461) return 2;
165 { ratio
[1] = -50; clipped
= 1; }
167 { ratio
[1] = 307; clipped
= 1; }
169 target
= flash_used
|| ratio
[1] < 197
170 ? -38 - (398 * ratio
[1] >> 10)
171 : -123 + (48 * ratio
[1] >> 10);
172 if (target
- mar
<= ratio
[0] &&
173 target
+ 20 >= ratio
[0] && !clipped
) return 0;
174 miss
= target
- ratio
[0];
175 if (abs(miss
) >= mar
*4) return 2;
176 if (miss
< -20) miss
= -20;
177 if (miss
> mar
) miss
= mar
;
178 ratio
[0] = target
- miss
;
182 void CLASS
canon_600_auto_wb()
184 int mar
, row
, col
, i
, j
, st
, count
[] = { 0,0 };
185 int test
[8], total
[2][8], ratio
[2][2], stat
[2];
187 memset (&total
, 0, sizeof total
);
189 if (i
< 10) mar
= 150;
190 else if (i
> 12) mar
= 20;
191 else mar
= 280 - 20 * i
;
192 if (flash_used
) mar
= 80;
193 for (row
=14; row
< height
-14; row
+=4)
194 for (col
=10; col
< width
; col
+=2) {
195 for (i
=0; i
< 8; i
++)
196 test
[(i
& 4) + FC(row
+(i
>> 1),col
+(i
& 1))] =
197 BAYER(row
+(i
>> 1),col
+(i
& 1));
198 for (i
=0; i
< 8; i
++)
199 if (test
[i
] < 150 || test
[i
] > 1500) goto next
;
200 for (i
=0; i
< 4; i
++)
201 if (abs(test
[i
] - test
[i
+4]) > 50) goto next
;
202 for (i
=0; i
< 2; i
++) {
203 for (j
=0; j
< 4; j
+=2)
204 ratio
[i
][j
>> 1] = ((test
[i
*4+j
+1]-test
[i
*4+j
]) << 10) / test
[i
*4+j
];
205 stat
[i
] = canon_600_color (ratio
[i
], mar
);
207 if ((st
= stat
[0] | stat
[1]) > 1) goto next
;
208 for (i
=0; i
< 2; i
++)
210 for (j
=0; j
< 2; j
++)
211 test
[i
*4+j
*2+1] = test
[i
*4+j
*2] * (0x400 + ratio
[i
][j
]) >> 10;
212 for (i
=0; i
< 8; i
++)
213 total
[st
][i
] += test
[i
];
217 if (count
[0] | count
[1]) {
218 st
= count
[0]*200 < count
[1];
219 for (i
=0; i
< 4; i
++)
220 pre_mul
[i
] = 1.0 / (total
[st
][i
] + total
[st
][i
+4]);
221 #ifdef LIBRAW_LIBRARY_BUILD
222 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CALCULATED
;
227 void CLASS
canon_600_coeff()
229 static const short table
[6][12] = {
230 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
231 { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
232 { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
233 { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
234 { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
235 { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
239 mc
= pre_mul
[1] / pre_mul
[2];
240 yc
= pre_mul
[3] / pre_mul
[2];
241 if (mc
> 1 && mc
<= 1.28 && yc
< 0.8789) t
=1;
242 if (mc
> 1.28 && mc
<= 2) {
243 if (yc
< 0.8789) t
=3;
244 else if (yc
<= 2) t
=4;
247 for (raw_color
= i
=0; i
< 3; i
++)
248 FORCC rgb_cam
[i
][c
] = table
[t
][i
*4 + c
] / 1024.0;
249 #ifdef LIBRAW_LIBRARY_BUILD
250 color_flags
.rgb_cam_state
= LIBRAW_COLORSTATE_CALCULATED
;
254 void CLASS
canon_600_load_raw()
256 uchar data
[1120], *dp
;
257 ushort pixel
[896], *pix
;
258 int irow
, row
, col
, val
;
259 static const short mul
[4][2] =
260 { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
262 for (irow
=row
=0; irow
< height
; irow
++) {
263 if (fread (data
, 1, raw_width
*5/4, ifp
) < raw_width
*5/4) derror();
264 for (dp
=data
, pix
=pixel
; dp
< data
+1120; dp
+=10, pix
+=8) {
265 pix
[0] = (dp
[0] << 2) + (dp
[1] >> 6 );
266 pix
[1] = (dp
[2] << 2) + (dp
[1] >> 4 & 3);
267 pix
[2] = (dp
[3] << 2) + (dp
[1] >> 2 & 3);
268 pix
[3] = (dp
[4] << 2) + (dp
[1] & 3);
269 pix
[4] = (dp
[5] << 2) + (dp
[9] & 3);
270 pix
[5] = (dp
[6] << 2) + (dp
[9] >> 2 & 3);
271 pix
[6] = (dp
[7] << 2) + (dp
[9] >> 4 & 3);
272 pix
[7] = (dp
[8] << 2) + (dp
[9] >> 6 );
274 for (col
=0; col
< width
; col
++)
275 BAYER(row
,col
) = pixel
[col
];
276 for (col
=width
; col
< raw_width
; col
++)
279 #ifdef LIBRAW_LIBRARY_BUILD
280 ushort
*dfp
= get_masked_pointer(row
,col
);
281 if(dfp
) *dfp
= pixel
[col
];
284 if ((row
+=2) > height
) row
= 1;
286 if (raw_width
> width
)
287 black
= black
/ ((raw_width
- width
) * height
) - 4;
288 for (row
=0; row
< height
; row
++)
289 for (col
=0; col
< width
; col
++) {
290 #ifdef LIBRAW_LIBRARY_BUILD
291 if( filtering_mode
& LIBRAW_FILTERING_NOBLACKS
)
292 val
= BAYER(row
,col
);
295 if ((val
= BAYER(row
,col
) - black
) < 0) val
= 0;
296 val
= val
* mul
[row
& 3][col
& 1] >> 9;
297 BAYER(row
,col
) = val
;
299 canon_600_fixed_wb(1311);
302 maximum
= (0x3ff - black
) * 1109 >> 9;
306 void CLASS
remove_zeroes()
308 unsigned row
, col
, tot
, n
, r
, c
;
310 #ifdef LIBRAW_LIBRARY_BUILD
311 RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES
,0,2);
313 for (row
=0; row
< height
; row
++)
314 for (col
=0; col
< width
; col
++)
315 if (BAYER(row
,col
) == 0) {
317 for (r
= row
-2; r
<= row
+2; r
++)
318 for (c
= col
-2; c
<= col
+2; c
++)
319 if (r
< height
&& c
< width
&&
320 FC(r
,c
) == FC(row
,col
) && BAYER(r
,c
))
321 tot
+= (n
++,BAYER(r
,c
));
322 if (n
) BAYER(row
,col
) = tot
/n
;
324 #ifdef LIBRAW_LIBRARY_BUILD
325 RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES
,1,2);
329 int CLASS
canon_s2is()
333 for (row
=0; row
< 100; row
++) {
334 fseek (ifp
, row
*3340 + 3284, SEEK_SET
);
335 if (getc(ifp
) > 15) return 1;
340 void CLASS
canon_a5_load_raw()
342 ushort data
[2565], *dp
, pixel
;
343 int vbits
=0, buf
=0, row
, col
, bc
=0;
346 for (row
=-top_margin
; row
< raw_height
-top_margin
; row
++) {
347 read_shorts (dp
=data
, raw_width
* 10 / 16);
348 for (col
=-left_margin
; col
< raw_width
-left_margin
; col
++) {
349 if ((vbits
-= 10) < 0)
350 buf
= (vbits
+= 16, (buf
<< 16) + *dp
++);
351 pixel
= buf
>> vbits
& 0x3ff;
352 #ifdef LIBRAW_LIBRARY_BUILD
353 ushort
*dfp
= get_masked_pointer(row
+top_margin
,col
+left_margin
);
354 if(dfp
) *dfp
= pixel
;
356 if ((unsigned) row
< height
&& (unsigned) col
< width
)
357 BAYER(row
,col
) = pixel
;
358 else if (col
> 1-left_margin
&& col
!= width
)
359 black
+= (bc
++,pixel
);
365 #ifdef LIBRAW_LIBRARY_BUILD
366 if(!(filtering_mode
& LIBRAW_FILTERING_NOZEROES
))
368 if (raw_width
> 1600) remove_zeroes();
372 getbits(-1) initializes the buffer
373 getbits(n) where 0 <= n <= 25 returns an n-bit integer
375 unsigned CLASS
getbits (int nbits
)
377 #ifdef LIBRAW_NOTHREADS
378 static unsigned bitbuf
=0;
379 static int vbits
=0, reset
=0;
381 #define bitbuf tls->getbits.bitbuf
382 #define vbits tls->getbits.vbits
383 #define reset tls->getbits.reset
388 return bitbuf
= vbits
= reset
= 0;
389 if (nbits
== 0 || reset
) return 0;
390 while (vbits
< nbits
) {
391 if ((c
= fgetc(ifp
)) == EOF
) derror();
392 if ((reset
= zero_after_ff
&& c
== 0xff && fgetc(ifp
))) return 0;
393 bitbuf
= (bitbuf
<< 8) + (uchar
) c
;
397 return bitbuf
<< (32-nbits
-vbits
) >> (32-nbits
);
398 #ifndef LIBRAW_NOTHREADS
405 void CLASS
init_decoder()
407 memset (first_decode
, 0, sizeof first_decode
);
408 free_decode
= first_decode
;
412 Construct a decode tree according to the specification in *source.
413 The first 16 bytes specify how many codes should be 1-bit, 2-bit
414 3-bit, etc. Bytes after that are the leaf values.
416 For example, if the source is
418 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
419 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
437 uchar
* CLASS
make_decoder (const uchar
*source
, int level
)
440 #ifndef LIBRAW_NOTHREADS
441 #define t_leaf tls->make_decoder_leaf
447 if (level
==0) t_leaf
=0;
449 if (free_decode
> first_decode
+2048) {
450 #ifdef LIBRAW_LIBRARY_BUILD
451 throw LIBRAW_EXCEPTION_DECODE_RAW
;
453 fprintf (stderr
,_("%s: decoder table overflow\n"), ifname
);
454 longjmp (failure
, 2);
457 for (i
=next
=0; i
<= t_leaf
&& next
< 16; )
461 cur
->branch
[0] = free_decode
;
462 make_decoder (source
, level
+1);
463 cur
->branch
[1] = free_decode
;
464 make_decoder (source
, level
+1);
466 cur
->leaf
= source
[16 + t_leaf
++];
468 return (uchar
*) source
+ 16 + t_leaf
;
469 #ifndef LIBRAW_NOTHREADS
474 void CLASS
crw_init_tables (unsigned table
)
476 static const uchar first_tree
[3][29] = {
477 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
478 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
479 { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
480 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
481 { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
482 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
484 static const uchar second_tree
[3][180] = {
485 { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
486 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
487 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
488 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
489 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
490 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
491 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
492 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
493 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
494 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
495 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
496 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
497 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
498 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
499 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
500 { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
501 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
502 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
503 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
504 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
505 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
506 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
507 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
508 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
509 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
510 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
511 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
512 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
513 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
514 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
515 { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
516 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
517 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
518 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
519 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
520 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
521 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
522 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
523 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
524 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
525 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
526 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
527 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
528 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
529 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
531 if (table
> 2) table
= 2;
533 make_decoder ( first_tree
[table
], 0);
534 second_decode
= free_decode
;
535 make_decoder (second_tree
[table
], 0);
539 Return 0 if the image starts with compressed data,
540 1 if it starts with uncompressed low-order bits.
542 In Canon compressed data, 0xff is always followed by 0x00.
544 int CLASS
canon_has_lowbits()
549 fseek (ifp
, 0, SEEK_SET
);
550 fread (test
, 1, sizeof test
, ifp
);
551 for (i
=540; i
< sizeof test
- 1; i
++)
552 if (test
[i
] == 0xff) {
553 if (test
[i
+1]) return 1;
559 void CLASS
canon_compressed_load_raw()
561 ushort
*pixel
, *prow
;
562 int nblocks
, lowbits
, i
, row
, r
, col
, save
, val
;
564 struct decode
*decode
, *dindex
;
565 int block
, diffbuf
[64], leaf
, len
, diff
, carry
=0, pnum
=0, base
[2];
566 double dark
[2] = { 0,0 };
569 crw_init_tables (tiff_compress
);
570 pixel
= (ushort
*) calloc (raw_width
*8, sizeof *pixel
);
571 merror (pixel
, "canon_compressed_load_raw()");
572 lowbits
= canon_has_lowbits();
573 if (!lowbits
) maximum
= 0x3ff;
574 fseek (ifp
, 540 + lowbits
*raw_height
*raw_width
/4, SEEK_SET
);
577 for (row
=0; row
< raw_height
; row
+=8) {
578 nblocks
= MIN (8, raw_height
-row
) * raw_width
>> 6;
579 for (block
=0; block
< nblocks
; block
++) {
580 memset (diffbuf
, 0, sizeof diffbuf
);
581 decode
= first_decode
;
582 for (i
=0; i
< 64; i
++ ) {
583 for (dindex
=decode
; dindex
->branch
[0]; )
584 dindex
= dindex
->branch
[getbits(1)];
586 decode
= second_decode
;
587 if (leaf
== 0 && i
) break;
588 if (leaf
== 0xff) continue;
591 if (len
== 0) continue;
593 if ((diff
& (1 << (len
-1))) == 0)
594 diff
-= (1 << len
) - 1;
595 if (i
< 64) diffbuf
[i
] = diff
;
599 for (i
=0; i
< 64; i
++ ) {
600 if (pnum
++ % raw_width
== 0)
601 base
[0] = base
[1] = 512;
602 if ((pixel
[(block
<< 6) + i
] = base
[i
& 1] += diffbuf
[i
]) >> 10)
608 fseek (ifp
, 26 + row
*raw_width
/4, SEEK_SET
);
609 for (prow
=pixel
, i
=0; i
< raw_width
*2; i
++) {
611 for (r
=0; r
< 8; r
+=2, prow
++) {
612 val
= (*prow
<< 2) + ((c
>> r
) & 3);
613 if (raw_width
== 2672 && val
< 512) val
+= 2;
617 fseek (ifp
, save
, SEEK_SET
);
619 for (r
=0; r
< 8; r
++) {
620 irow
= row
- top_margin
+ r
;
621 #ifndef LIBRAW_LIBRARY_BUILD
622 if (irow
>= height
) continue;
624 for (col
=0; col
< raw_width
; col
++) {
625 #ifdef LIBRAW_LIBRARY_BUILD
626 ushort
*dfp
= get_masked_pointer(row
+r
,col
);
627 if(dfp
) *dfp
= pixel
[r
*raw_width
+col
];
628 if (irow
>= height
) continue; // skip for top/bottom rows
630 icol
= col
- left_margin
;
632 BAYER(irow
,icol
) = pixel
[r
*raw_width
+col
];
634 dark
[icol
& 1] += pixel
[r
*raw_width
+col
];
642 #line 927 "dcraw/dcraw.c"
643 int CLASS
ljpeg_start (struct jhead
*jh
, int info_only
)
646 uchar data
[0x10000], *dp
;
648 if (!info_only
) init_decoder();
649 memset (jh
, 0, sizeof *jh
);
650 FORC(6) jh
->huff
[c
] = free_decode
;
651 jh
->restart
= INT_MAX
;
652 fread (data
, 2, 1, ifp
);
653 if (data
[1] != 0xd8) return 0;
655 fread (data
, 2, 2, ifp
);
656 tag
= data
[0] << 8 | data
[1];
657 len
= (data
[2] << 8 | data
[3]) - 2;
658 if (tag
<= 0xff00) return 0;
659 fread (data
, 1, len
, ifp
);
662 jh
->sraw
= ((data
[7] >> 4) * (data
[7] & 15) - 1) & 3;
665 jh
->high
= data
[1] << 8 | data
[2];
666 jh
->wide
= data
[3] << 8 | data
[4];
667 jh
->clrs
= data
[5] + jh
->sraw
;
668 if (len
== 9 && !dng_version
) getc(ifp
);
671 if (info_only
) break;
672 for (dp
= data
; dp
< data
+len
&& *dp
< 4; ) {
673 jh
->huff
[*dp
] = free_decode
;
674 dp
= make_decoder (++dp
, 0);
678 jh
->psv
= data
[1+data
[0]*2];
679 jh
->bits
-= data
[3+data
[0]*2] & 15;
682 jh
->restart
= data
[0] << 8 | data
[1];
684 } while (tag
!= 0xffda);
685 if (info_only
) return 1;
687 FORC(4) jh
->huff
[2+c
] = jh
->huff
[1];
688 FORC(jh
->sraw
) jh
->huff
[1+c
] = jh
->huff
[0];
690 jh
->row
= (ushort
*) calloc (jh
->wide
*jh
->clrs
, 4);
691 merror (jh
->row
, "ljpeg_start()");
692 return zero_after_ff
= 1;
695 int CLASS
ljpeg_diff (struct decode
*dindex
)
699 while (dindex
->branch
[0])
700 dindex
= dindex
->branch
[getbits(1)];
702 if (len
== 16 && (!dng_version
|| dng_version
>= 0x1010000))
705 if ((diff
& (1 << (len
-1))) == 0)
706 diff
-= (1 << len
) - 1;
710 ushort
* CLASS
ljpeg_row (int jrow
, struct jhead
*jh
)
712 int col
, c
, diff
, pred
, spred
=0;
713 ushort mark
=0, *row
[3];
715 if (jrow
* jh
->wide
% jh
->restart
== 0) {
716 FORC(6) jh
->vpred
[c
] = 1 << (jh
->bits
-1);
718 do mark
= (mark
<< 8) + (c
= fgetc(ifp
));
719 while (c
!= EOF
&& mark
>> 4 != 0xffd);
722 FORC3 row
[c
] = jh
->row
+ jh
->wide
*jh
->clrs
*((jrow
+c
) & 1);
723 for (col
=0; col
< jh
->wide
; col
++)
725 diff
= ljpeg_diff (jh
->huff
[c
]);
726 if (jh
->sraw
&& c
<= jh
->sraw
&& (col
| c
))
728 else if (col
) pred
= row
[0][-jh
->clrs
];
729 else pred
= (jh
->vpred
[c
] += diff
) - diff
;
730 if (jrow
&& col
) switch (jh
->psv
) {
732 case 2: pred
= row
[1][0]; break;
733 case 3: pred
= row
[1][-jh
->clrs
]; break;
734 case 4: pred
= pred
+ row
[1][0] - row
[1][-jh
->clrs
]; break;
735 case 5: pred
= pred
+ ((row
[1][0] - row
[1][-jh
->clrs
]) >> 1); break;
736 case 6: pred
= row
[1][0] + ((pred
- row
[1][-jh
->clrs
]) >> 1); break;
737 case 7: pred
= (pred
+ row
[1][0]) >> 1; break;
740 if ((**row
= pred
+ diff
) >> jh
->bits
) derror();
741 if (c
<= jh
->sraw
) spred
= **row
;
747 void CLASS
lossless_jpeg_load_raw()
749 int jwide
, jrow
, jcol
, val
, jidx
, i
, j
, row
=0, col
=0;
750 double dark
[2] = { 0,0 };
755 if (!ljpeg_start (&jh
, 0)) return;
756 jwide
= jh
.wide
* jh
.clrs
;
758 for (jrow
=0; jrow
< jh
.high
; jrow
++) {
759 rp
= ljpeg_row (jrow
, &jh
);
760 for (jcol
=0; jcol
< jwide
; jcol
++) {
763 #ifdef LIBRAW_LIBRARY_BUILD
764 if( !(filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
))
766 val
= curve
[val
& 0xfff];
768 jidx
= jrow
*jwide
+ jcol
;
769 i
= jidx
/ (cr2_slice
[1]*jh
.high
);
770 if ((j
= i
>= cr2_slice
[0]))
772 jidx
-= i
* (cr2_slice
[1]*jh
.high
);
773 row
= jidx
/ cr2_slice
[1+j
];
774 col
= jidx
% cr2_slice
[1+j
] + i
*cr2_slice
[1];
776 if (raw_width
== 3984 && (col
-= 2) < 0)
777 col
+= (row
--,raw_width
);
778 #ifdef LIBRAW_LIBRARY_BUILD
779 ushort
*dfp
= get_masked_pointer(row
,col
);
782 if ((unsigned) (row
-top_margin
) < height
) {
783 if ((unsigned) (col
-left_margin
) < width
) {
784 BAYER(row
-top_margin
,col
-left_margin
) = val
;
785 if (min
> val
) min
= val
;
787 dark
[(col
-left_margin
) & 1] += val
;
789 if (++col
>= raw_width
)
795 if (!strcasecmp(make
,"KODAK"))
799 void CLASS
canon_sraw_load_raw()
802 short *rp
=0, (*ip
)[4];
803 int jwide
, slice
, scol
, ecol
, row
, col
, jrow
=0, jcol
=0, pix
[3], c
;
804 int v
[3]={0,0,0}, ver
, hue
;
807 if (!ljpeg_start (&jh
, 0)) return;
808 jwide
= (jh
.wide
>>= 1) * jh
.clrs
;
810 for (ecol
=slice
=0; slice
<= cr2_slice
[0]; slice
++) {
812 ecol
+= cr2_slice
[1] * 2 / jh
.clrs
;
813 if (!cr2_slice
[0] || ecol
> raw_width
-1) ecol
= raw_width
& -2;
814 for (row
=0; row
< height
; row
+= (jh
.clrs
>> 1) - 1) {
815 ip
= (short (*)[4]) image
+ row
*width
;
816 for (col
=scol
; col
< ecol
; col
+=2, jcol
+=jh
.clrs
) {
817 if ((jcol
%= jwide
) == 0)
818 rp
= (short *) ljpeg_row (jrow
++, &jh
);
819 if (col
>= width
) continue;
821 ip
[col
+ (c
>> 1)*width
+ (c
& 1)][0] = rp
[jcol
+c
];
822 ip
[col
][1] = rp
[jcol
+jh
.clrs
-2] - 16384;
823 ip
[col
][2] = rp
[jcol
+jh
.clrs
-1] - 16384;
827 for (cp
=model2
; *cp
&& !isdigit(*cp
); cp
++);
828 sscanf (cp
, "%d.%d.%d", v
, v
+1, v
+2);
829 ver
= (v
[0]*1000 + v
[1])*1000 + v
[2];
830 hue
= (jh
.sraw
+1) << 2;
831 if (unique_id
== 0x80000218 && ver
> 1000006 && ver
< 3000000)
833 ip
= (short (*)[4]) image
;
835 for (row
=0; row
< height
; row
++, ip
+=width
) {
836 if (row
& (jh
.sraw
>> 1))
837 for (col
=0; col
< width
; col
+=2)
838 for (c
=1; c
< 3; c
++)
840 ip
[col
][c
] = ip
[col
-width
][c
];
841 else ip
[col
][c
] = (ip
[col
-width
][c
] + ip
[col
+width
][c
] + 1) >> 1;
842 for (col
=1; col
< width
; col
+=2)
843 for (c
=1; c
< 3; c
++)
845 ip
[col
][c
] = ip
[col
-1][c
];
846 else ip
[col
][c
] = (ip
[col
-1][c
] + ip
[col
+1][c
] + 1) >> 1;
848 for ( ; rp
< ip
[0]; rp
+=4) {
849 if (unique_id
< 0x80000200) {
850 pix
[0] = rp
[0] + rp
[2] - 512;
851 pix
[2] = rp
[0] + rp
[1] - 512;
852 pix
[1] = rp
[0] + ((-778*rp
[1] - (rp
[2] << 11)) >> 12) - 512;
854 rp
[1] = (rp
[1] << 2) + hue
;
855 rp
[2] = (rp
[2] << 2) + hue
;
856 pix
[0] = rp
[0] + (( 200*rp
[1] + 22929*rp
[2]) >> 14);
857 pix
[1] = rp
[0] + ((-5640*rp
[1] - 11751*rp
[2]) >> 14);
858 pix
[2] = rp
[0] + ((29040*rp
[1] - 101*rp
[2]) >> 14);
860 FORC3 rp
[c
] = CLIP(pix
[c
] * sraw_mul
[c
] >> 10);
866 void CLASS
adobe_copy_pixel (int row
, int col
, ushort
**rp
)
870 r
= row
-= top_margin
;
871 c
= col
-= left_margin
;
872 if (is_raw
== 2 && shot_select
) (*rp
)++;
874 #ifndef LIBRAW_LIBRARY_BUILD
876 r
= row
+ fuji_width
- 1 - (col
>> 1);
877 c
= row
+ ((col
+1) >> 1);
880 #ifdef LIBRAW_LIBRARY_BUILD
882 if(!(filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
))
883 val
= **rp
< 0x1000 ? curve
[**rp
] : **rp
;
884 if (r
< height
&& c
< width
)
888 ushort
*dfp
= get_masked_pointer(row
+top_margin
,col
+left_margin
);
892 if (r
< height
&& c
< width
)
893 BAYER(r
,c
) = **rp
< 0x1000 ? curve
[**rp
] : **rp
;
897 if (r
< height
&& c
< width
)
899 image
[row
*width
+col
][c
] = (*rp
)[c
] < 0x1000 ? curve
[(*rp
)[c
]]:(*rp
)[c
];
902 if (is_raw
== 2 && shot_select
) (*rp
)--;
905 void CLASS
adobe_dng_load_raw_lj()
907 unsigned save
, trow
=0, tcol
=0, jwide
, jrow
, jcol
, row
, col
;
911 while (trow
< raw_height
) {
913 if (tile_length
< INT_MAX
)
914 fseek (ifp
, get4(), SEEK_SET
);
915 if (!ljpeg_start (&jh
, 0)) break;
917 if (filters
) jwide
*= jh
.clrs
;
919 for (row
=col
=jrow
=0; jrow
< jh
.high
; jrow
++) {
920 rp
= ljpeg_row (jrow
, &jh
);
921 for (jcol
=0; jcol
< jwide
; jcol
++) {
922 adobe_copy_pixel (trow
+row
, tcol
+col
, &rp
);
923 if (++col
>= tile_width
|| col
>= raw_width
)
924 row
+= 1 + (col
= 0);
927 fseek (ifp
, save
+4, SEEK_SET
);
928 if ((tcol
+= tile_width
) >= raw_width
)
929 trow
+= tile_length
+ (tcol
= 0);
934 void CLASS
adobe_dng_load_raw_nc()
939 pixel
= (ushort
*) calloc (raw_width
* tiff_samples
, sizeof *pixel
);
940 merror (pixel
, "adobe_dng_load_raw_nc()");
941 for (row
=0; row
< raw_height
; row
++) {
943 read_shorts (pixel
, raw_width
* tiff_samples
);
946 for (col
=0; col
< raw_width
* tiff_samples
; col
++)
947 pixel
[col
] = getbits(tiff_bps
);
949 for (rp
=pixel
, col
=0; col
< raw_width
; col
++)
950 adobe_copy_pixel (row
, col
, &rp
);
955 void CLASS
pentax_tree()
962 FORC(13) bit
[0][c
] = get2();
963 FORC(13) bit
[1][c
] = fgetc(ifp
) & 15;
966 for (i
=0; i
< bit
[1][c
]; i
++) {
967 j
= bit
[0][c
] >> (11-i
) & 1;
968 if (!cur
->branch
[j
]) cur
->branch
[j
] = ++free_decode
;
969 cur
= cur
->branch
[j
];
975 void CLASS
pentax_k10_load_raw()
978 ushort vpred
[2][2] = {{0,0},{0,0}}, hpred
[2];
981 for (row
=0; row
< raw_height
; row
++)
983 #ifndef LIBRAW_LIBRARY_BUILD
984 if(row
>= height
) break;
986 for (col
=0; col
< raw_width
; col
++) {
987 diff
= ljpeg_diff (first_decode
);
988 if (col
< 2) hpred
[col
] = vpred
[row
& 1][col
] += diff
;
989 else hpred
[col
& 1] += diff
;
990 if (col
< width
&& row
< height
)
991 BAYER(row
,col
) = hpred
[col
& 1];
992 #ifdef LIBRAW_LIBRARY_BUILD
995 ushort
*dfp
= get_masked_pointer(row
,col
);
996 if(dfp
) *dfp
= hpred
[col
& 1];
999 if (col
< width
&& row
< height
)
1001 if (hpred
[col
& 1] >> 12) derror();
1006 void CLASS
nikon_compressed_load_raw()
1008 static const uchar nikon_tree
[][32] = {
1009 { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
1010 5,4,3,6,2,7,1,0,8,9,11,10,12 },
1011 { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
1012 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
1013 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
1014 5,4,6,3,7,2,8,1,9,0,10,11,12 },
1015 { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
1016 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
1017 { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
1018 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
1019 { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
1020 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
1021 struct decode
*dindex
;
1022 ushort ver0
, ver1
, vpred
[2][2], hpred
[2], csize
;
1023 int i
, min
, max
, step
=0, huff
=0, split
=0, row
, col
, len
, shl
, diff
;
1025 fseek (ifp
, meta_offset
, SEEK_SET
);
1028 if (ver0
== 0x49 || ver1
== 0x58)
1029 fseek (ifp
, 2110, SEEK_CUR
);
1030 if (ver0
== 0x46) huff
= 2;
1031 if (tiff_bps
== 14) huff
+= 3;
1032 read_shorts (vpred
[0], 4);
1033 max
= 1 << tiff_bps
& 0x7fff;
1034 if ((csize
= get2()) > 1)
1035 step
= max
/ (csize
-1);
1036 if (ver0
== 0x44 && ver1
== 0x20 && step
> 0) {
1037 for (i
=0; i
< csize
; i
++)
1038 curve
[i
*step
] = get2();
1039 for (i
=0; i
< max
; i
++)
1040 curve
[i
] = ( curve
[i
-i
%step
]*(step
-i
%step
) +
1041 curve
[i
-i
%step
+step
]*(i
%step
) ) / step
;
1042 #ifdef LIBRAW_LIBRARY_BUILD
1043 color_flags
.curve_state
= LIBRAW_COLORSTATE_LOADED
;
1045 fseek (ifp
, meta_offset
+562, SEEK_SET
);
1047 } else if (ver0
!= 0x46 && csize
<= 0x4001)
1049 read_shorts (curve
, max
=csize
);
1050 #ifdef LIBRAW_LIBRARY_BUILD
1051 color_flags
.curve_state
= LIBRAW_COLORSTATE_LOADED
;
1054 while (curve
[max
-2] == curve
[max
-1]) max
--;
1056 make_decoder (nikon_tree
[huff
], 0);
1057 fseek (ifp
, data_offset
, SEEK_SET
);
1059 for (min
=row
=0; row
< height
; row
++) {
1060 if (split
&& row
== split
) {
1062 make_decoder (nikon_tree
[huff
+1], 0);
1063 max
+= (min
= 16) << 1;
1065 for (col
=0; col
< raw_width
; col
++) {
1066 for (dindex
=first_decode
; dindex
->branch
[0]; )
1067 dindex
= dindex
->branch
[getbits(1)];
1068 len
= dindex
->leaf
& 15;
1069 shl
= dindex
->leaf
>> 4;
1070 diff
= ((getbits(len
-shl
) << 1) + 1) << shl
>> 1;
1071 if ((diff
& (1 << (len
-1))) == 0)
1072 diff
-= (1 << len
) - !shl
;
1073 if (col
< 2) hpred
[col
] = vpred
[row
& 1][col
] += diff
;
1074 else hpred
[col
& 1] += diff
;
1075 if ((ushort
)(hpred
[col
& 1] + min
) >= max
) derror();
1076 #ifndef LIBRAW_LIBRARY_BUILD
1077 if ((unsigned) (col
-left_margin
) < width
)
1078 BAYER(row
,col
-left_margin
) = curve
[LIM((short)hpred
[col
& 1],0,0x3fff)];
1080 ushort xval
= hpred
[col
& 1];
1081 if(!(filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
))
1082 xval
= curve
[LIM((short)xval
,0,0x3fff)];
1083 if ((unsigned) (col
-left_margin
) < width
)
1085 BAYER(row
,col
-left_margin
) = xval
;
1089 ushort
*dfp
= get_masked_pointer(row
,col
);
1090 if(dfp
) *dfp
= xval
;
1099 Figure out if a NEF file is compressed. These fancy heuristics
1100 are only needed for the D100, thanks to a bug in some cameras
1101 that tags all images as "compressed".
1103 int CLASS
nikon_is_compressed()
1108 fseek (ifp
, data_offset
, SEEK_SET
);
1109 fread (test
, 1, 256, ifp
);
1110 for (i
=15; i
< 256; i
+=16)
1111 if (test
[i
]) return 1;
1116 Returns 1 for a Coolpix 995, 0 for anything else.
1118 int CLASS
nikon_e995()
1121 const uchar often
[] = { 0x00, 0x55, 0xaa, 0xff };
1123 memset (histo
, 0, sizeof histo
);
1124 fseek (ifp
, -2000, SEEK_END
);
1125 for (i
=0; i
< 2000; i
++)
1126 histo
[fgetc(ifp
)]++;
1127 for (i
=0; i
< 4; i
++)
1128 if (histo
[often
[i
]] < 200)
1134 Returns 1 for a Coolpix 2100, 0 for anything else.
1136 int CLASS
nikon_e2100()
1141 fseek (ifp
, 0, SEEK_SET
);
1142 for (i
=0; i
< 1024; i
++) {
1143 fread (t
, 1, 12, ifp
);
1144 if (((t
[2] & t
[4] & t
[7] & t
[9]) >> 4
1145 & t
[1] & t
[6] & t
[8] & t
[11] & 3) != 3)
1151 void CLASS
nikon_3700()
1155 static const struct {
1157 char t_make
[12], t_model
[15];
1159 { 0x00, "PENTAX", "Optio 33WR" },
1160 { 0x03, "NIKON", "E3200" },
1161 { 0x32, "NIKON", "E3700" },
1162 { 0x33, "OLYMPUS", "C740UZ" } };
1164 fseek (ifp
, 3072, SEEK_SET
);
1165 fread (dp
, 1, 24, ifp
);
1166 bits
= (dp
[8] & 3) << 4 | (dp
[20] & 3);
1167 for (i
=0; i
< sizeof table
/ sizeof *table
; i
++)
1168 if (bits
== table
[i
].bits
) {
1169 strcpy (make
, table
[i
].t_make
);
1170 strcpy (model
, table
[i
].t_model
);
1175 Separates a Minolta DiMAGE Z2 from a Nikon E4300.
1177 int CLASS
minolta_z2()
1182 fseek (ifp
, -sizeof tail
, SEEK_END
);
1183 fread (tail
, 1, sizeof tail
, ifp
);
1184 for (nz
=i
=0; i
< sizeof tail
; i
++)
1189 /* Here raw_width is in bytes, not pixels. */
1190 void CLASS
nikon_e900_load_raw()
1192 int offset
=0, irow
, row
, col
;
1194 for (irow
=0; irow
< height
; irow
++) {
1195 row
= irow
* 2 % height
;
1197 offset
= - (-offset
& -4096);
1198 fseek (ifp
, offset
, SEEK_SET
);
1199 offset
+= raw_width
;
1201 for (col
=0; col
< width
; col
++)
1202 BAYER(row
,col
) = getbits(10);
1207 The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
1209 void CLASS
fuji_load_raw()
1212 #ifndef LIBRAW_LIBRARY_BUILD
1213 int wide
, row
, col
, r
, c
;
1215 fseek (ifp
, (top_margin
*raw_width
+ left_margin
) * 2, SEEK_CUR
);
1216 wide
= fuji_width
<< !fuji_layout
;
1217 pixel
= (ushort
*) calloc (wide
, sizeof *pixel
);
1218 merror (pixel
, "fuji_load_raw()");
1219 for (row
=0; row
< raw_height
; row
++) {
1220 read_shorts (pixel
, wide
);
1221 fseek (ifp
, 2*(raw_width
- wide
), SEEK_CUR
);
1222 for (col
=0; col
< wide
; col
++) {
1224 r
= fuji_width
- 1 - col
+ (row
>> 1);
1225 c
= col
+ ((row
+1) >> 1);
1227 r
= fuji_width
- 1 + row
- (col
>> 1);
1228 c
= row
+ ((col
+1) >> 1);
1230 BAYER(r
,c
) = pixel
[col
];
1236 pixel
= (ushort
*) calloc (raw_width
, sizeof *pixel
);
1237 merror (pixel
, "fuji_load_raw()");
1238 for (row
=0; row
< raw_height
; row
++) {
1239 read_shorts (pixel
, raw_width
);
1240 for (col
=0; col
< raw_width
; col
++) {
1241 if(col
>= left_margin
&& col
< width
+left_margin
1242 && row
>= top_margin
&& row
< height
+top_margin
)
1244 BAYER(row
-top_margin
,col
-left_margin
) = pixel
[col
];
1248 ushort
*dfp
= get_masked_pointer(row
,col
);
1249 if(dfp
) *dfp
= pixel
[col
];
1256 #line 1545 "dcraw/dcraw.c"
1257 void CLASS
ppm_thumb (FILE *tfp
)
1260 thumb_length
= thumb_width
*thumb_height
*3;
1261 thumb
= (char *) malloc (thumb_length
);
1262 merror (thumb
, "ppm_thumb()");
1263 fprintf (tfp
, "P6\n%d %d\n255\n", thumb_width
, thumb_height
);
1264 fread (thumb
, 1, thumb_length
, ifp
);
1265 fwrite (thumb
, 1, thumb_length
, tfp
);
1269 void CLASS
layer_thumb (FILE *tfp
)
1272 char *thumb
, map
[][4] = { "012","102" };
1274 colors
= thumb_misc
>> 5 & 7;
1275 thumb_length
= thumb_width
*thumb_height
;
1276 thumb
= (char *) calloc (colors
, thumb_length
);
1277 merror (thumb
, "layer_thumb()");
1278 fprintf (tfp
, "P%d\n%d %d\n255\n",
1279 5 + (colors
>> 1), thumb_width
, thumb_height
);
1280 fread (thumb
, thumb_length
, colors
, ifp
);
1281 for (i
=0; i
< thumb_length
; i
++)
1282 FORCC
putc (thumb
[i
+thumb_length
*(map
[thumb_misc
>> 8][c
]-'0')], tfp
);
1286 void CLASS
rollei_thumb (FILE *tfp
)
1291 thumb_length
= thumb_width
* thumb_height
;
1292 thumb
= (ushort
*) calloc (thumb_length
, 2);
1293 merror (thumb
, "rollei_thumb()");
1294 fprintf (tfp
, "P6\n%d %d\n255\n", thumb_width
, thumb_height
);
1295 read_shorts (thumb
, thumb_length
);
1296 for (i
=0; i
< thumb_length
; i
++) {
1297 putc (thumb
[i
] << 3, tfp
);
1298 putc (thumb
[i
] >> 5 << 2, tfp
);
1299 putc (thumb
[i
] >> 11 << 3, tfp
);
1304 void CLASS
rollei_load_raw()
1307 unsigned iten
=0, isix
, i
, buffer
=0, row
, col
, todo
[16];
1309 isix
= raw_width
* raw_height
* 5 / 8;
1310 while (fread (pixel
, 1, 10, ifp
) == 10) {
1311 for (i
=0; i
< 10; i
+=2) {
1313 todo
[i
+1] = pixel
[i
] << 8 | pixel
[i
+1];
1314 buffer
= pixel
[i
] >> 2 | buffer
<< 6;
1316 for ( ; i
< 16; i
+=2) {
1318 todo
[i
+1] = buffer
>> (14-i
)*5;
1320 for (i
=0; i
< 16; i
+=2) {
1321 row
= todo
[i
] / raw_width
- top_margin
;
1322 col
= todo
[i
] % raw_width
- left_margin
;
1323 if (row
< height
&& col
< width
)
1324 BAYER(row
,col
) = (todo
[i
+1] & 0x3ff);
1325 #ifdef LIBRAW_LIBRARY_BUILD
1328 ushort
*dfp
= get_masked_pointer(todo
[i
] / raw_width
,todo
[i
] % raw_width
);
1329 if(dfp
) *dfp
= (todo
[i
+1] & 0x3ff);
1337 int CLASS
bayer (unsigned row
, unsigned col
)
1339 return (row
< height
&& col
< width
) ? BAYER(row
,col
) : 0;
1342 void CLASS
phase_one_flat_field (int is_float
, int nc
)
1345 unsigned wide
, y
, x
, c
, rend
, cend
, row
, col
;
1346 float *mrow
, num
, mult
[4];
1348 read_shorts (head
, 8);
1349 wide
= head
[2] / head
[4];
1350 mrow
= (float *) calloc (nc
*wide
, sizeof *mrow
);
1351 merror (mrow
, "phase_one_flat_field()");
1352 for (y
=0; y
< head
[3] / head
[5]; y
++) {
1353 for (x
=0; x
< wide
; x
++)
1354 for (c
=0; c
< nc
; c
+=2) {
1355 num
= is_float
? getreal(11) : get2()/32768.0;
1356 if (y
==0) mrow
[c
*wide
+x
] = num
;
1357 else mrow
[(c
+1)*wide
+x
] = (num
- mrow
[c
*wide
+x
]) / head
[5];
1360 rend
= head
[1]-top_margin
+ y
*head
[5];
1361 for (row
= rend
-head
[5]; row
< height
&& row
< rend
; row
++) {
1362 for (x
=1; x
< wide
; x
++) {
1363 for (c
=0; c
< nc
; c
+=2) {
1364 mult
[c
] = mrow
[c
*wide
+x
-1];
1365 mult
[c
+1] = (mrow
[c
*wide
+x
] - mult
[c
]) / head
[4];
1367 cend
= head
[0]-left_margin
+ x
*head
[4];
1368 for (col
= cend
-head
[4]; col
< width
&& col
< cend
; col
++) {
1369 c
= nc
> 2 ? FC(row
,col
) : 0;
1371 c
= BAYER(row
,col
) * mult
[c
];
1372 BAYER(row
,col
) = LIM(c
,0,65535);
1374 for (c
=0; c
< nc
; c
+=2)
1375 mult
[c
] += mult
[c
+1];
1378 for (x
=0; x
< wide
; x
++)
1379 for (c
=0; c
< nc
; c
+=2)
1380 mrow
[c
*wide
+x
] += mrow
[(c
+1)*wide
+x
];
1386 void CLASS
phase_one_correct()
1388 unsigned entries
, tag
, data
, save
, col
, row
, type
;
1389 int len
, i
, j
, k
, cip
, val
[4], dev
[4], sum
, max
;
1390 int head
[9], diff
, mindiff
=INT_MAX
, off_412
=0;
1391 static const signed char dir
[12][2] =
1392 { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
1393 {-2,-2}, {-2,2}, {2,-2}, {2,2} };
1394 float poly
[8], num
, cfrac
, frac
, mult
[2], *yval
[2];
1395 ushort t_curve
[0x10000], *xval
[2];
1397 if (half_size
|| !meta_length
) return;
1398 #ifdef DCRAW_VERBOSE
1399 if (verbose
) fprintf (stderr
,_("Phase One correction...\n"));
1401 fseek (ifp
, meta_offset
, SEEK_SET
);
1403 fseek (ifp
, 6, SEEK_CUR
);
1404 fseek (ifp
, meta_offset
+get4(), SEEK_SET
);
1405 entries
= get4(); get4();
1411 fseek (ifp
, meta_offset
+data
, SEEK_SET
);
1412 if (tag
== 0x419) { /* Polynomial curve */
1413 for (get4(), i
=0; i
< 8; i
++)
1414 poly
[i
] = getreal(11);
1415 poly
[3] += (ph1
.tag_210
- poly
[7]) * poly
[6] + 1;
1416 for (i
=0; i
< 0x10000; i
++) {
1417 num
= (poly
[5]*i
+ poly
[3])*i
+ poly
[1];
1418 t_curve
[i
] = LIM(num
,0,65535);
1419 } goto apply
; /* apply to right half */
1420 } else if (tag
== 0x41a) { /* Polynomial curve */
1421 for (i
=0; i
< 4; i
++)
1422 poly
[i
] = getreal(11);
1423 for (i
=0; i
< 0x10000; i
++) {
1424 for (num
=0, j
=4; j
--; )
1425 num
= num
* i
+ poly
[j
];
1426 t_curve
[i
] = LIM(num
+i
,0,65535);
1427 } apply
: /* apply to whole image */
1428 for (row
=0; row
< height
; row
++)
1429 for (col
= (tag
& 1)*ph1
.split_col
; col
< width
; col
++)
1430 BAYER(row
,col
) = t_curve
[BAYER(row
,col
)];
1431 } else if (tag
== 0x400) { /* Sensor defects */
1432 while ((len
-= 8) >= 0) {
1433 col
= get2() - left_margin
;
1434 row
= get2() - top_margin
;
1435 type
= get2(); get2();
1436 if (col
>= width
) continue;
1437 if (type
== 131) /* Bad column */
1438 for (row
=0; row
< height
; row
++)
1439 if (FC(row
,col
) == 1) {
1440 for (sum
=i
=0; i
< 4; i
++)
1441 sum
+= val
[i
] = bayer (row
+dir
[i
][0], col
+dir
[i
][1]);
1442 for (max
=i
=0; i
< 4; i
++) {
1443 dev
[i
] = abs((val
[i
] << 2) - sum
);
1444 if (dev
[max
] < dev
[i
]) max
= i
;
1446 BAYER(row
,col
) = (sum
- val
[max
])/3.0 + 0.5;
1448 for (sum
=0, i
=8; i
< 12; i
++)
1449 sum
+= bayer (row
+dir
[i
][0], col
+dir
[i
][1]);
1450 BAYER(row
,col
) = 0.5 + sum
* 0.0732233 +
1451 (bayer(row
,col
-2) + bayer(row
,col
+2)) * 0.3535534;
1453 else if (type
== 129) { /* Bad pixel */
1454 if (row
>= height
) continue;
1455 j
= (FC(row
,col
) != 1) * 4;
1456 for (sum
=0, i
=j
; i
< j
+8; i
++)
1457 sum
+= bayer (row
+dir
[i
][0], col
+dir
[i
][1]);
1458 BAYER(row
,col
) = (sum
+ 4) >> 3;
1461 } else if (tag
== 0x401) { /* All-color flat fields */
1462 phase_one_flat_field (1, 2);
1463 } else if (tag
== 0x416 || tag
== 0x410) {
1464 phase_one_flat_field (0, 2);
1465 } else if (tag
== 0x40b) { /* Red+blue flat field */
1466 phase_one_flat_field (0, 4);
1467 } else if (tag
== 0x412) {
1468 fseek (ifp
, 36, SEEK_CUR
);
1469 diff
= abs (get2() - ph1
.tag_21a
);
1470 if (mindiff
> diff
) {
1472 off_412
= ftell(ifp
) - 38;
1475 fseek (ifp
, save
, SEEK_SET
);
1478 fseek (ifp
, off_412
, SEEK_SET
);
1479 for (i
=0; i
< 9; i
++) head
[i
] = get4() & 0x7fff;
1480 yval
[0] = (float *) calloc (head
[1]*head
[3] + head
[2]*head
[4], 6);
1481 merror (yval
[0], "phase_one_correct()");
1482 yval
[1] = (float *) (yval
[0] + head
[1]*head
[3]);
1483 xval
[0] = (ushort
*) (yval
[1] + head
[2]*head
[4]);
1484 xval
[1] = (ushort
*) (xval
[0] + head
[1]*head
[3]);
1486 for (i
=0; i
< 2; i
++)
1487 for (j
=0; j
< head
[i
+1]*head
[i
+3]; j
++)
1488 yval
[i
][j
] = getreal(11);
1489 for (i
=0; i
< 2; i
++)
1490 for (j
=0; j
< head
[i
+1]*head
[i
+3]; j
++)
1491 xval
[i
][j
] = get2();
1492 for (row
=0; row
< height
; row
++)
1493 for (col
=0; col
< width
; col
++) {
1494 cfrac
= (float) col
* head
[3] / raw_width
;
1495 cfrac
-= cip
= cfrac
;
1496 num
= BAYER(row
,col
) * 0.5;
1497 for (i
=cip
; i
< cip
+2; i
++) {
1498 for (k
=j
=0; j
< head
[1]; j
++)
1499 if (num
< xval
[0][k
= head
[1]*i
+j
]) break;
1500 frac
= (j
== 0 || j
== head
[1]) ? 0 :
1501 (xval
[0][k
] - num
) / (xval
[0][k
] - xval
[0][k
-1]);
1502 mult
[i
-cip
] = yval
[0][k
-1] * frac
+ yval
[0][k
] * (1-frac
);
1504 i
= ((mult
[0] * (1-cfrac
) + mult
[1] * cfrac
)
1505 * (row
+ top_margin
) + num
) * 2;
1506 BAYER(row
,col
) = LIM(i
,0,65535);
1512 void CLASS
phase_one_load_raw()
1515 ushort
*pixel
, akey
, bkey
, mask
;
1517 fseek (ifp
, ph1
.key_off
, SEEK_SET
);
1520 mask
= ph1
.format
== 1 ? 0x5555:0x1354;
1521 #ifndef LIBRAW_LIBRARY_BUILD
1522 fseek (ifp
, data_offset
+ top_margin
*raw_width
*2, SEEK_SET
);
1523 pixel
= (ushort
*) calloc (raw_width
, sizeof *pixel
);
1524 merror (pixel
, "phase_one_load_raw()");
1525 for (row
=0; row
< height
; row
++) {
1526 read_shorts (pixel
, raw_width
);
1527 for (col
=0; col
< raw_width
; col
+=2) {
1528 a
= pixel
[col
+0] ^ akey
;
1529 b
= pixel
[col
+1] ^ bkey
;
1530 pixel
[col
+0] = (a
& mask
) | (b
& ~mask
);
1531 pixel
[col
+1] = (b
& mask
) | (a
& ~mask
);
1533 for (col
=0; col
< width
; col
++)
1534 BAYER(row
,col
) = pixel
[col
+left_margin
];
1538 fseek (ifp
, data_offset
, SEEK_SET
);
1539 pixel
= (ushort
*) calloc (raw_width
, sizeof *pixel
);
1540 merror (pixel
, "phase_one_load_raw()");
1541 for (row
=0; row
< raw_height
; row
++) {
1542 read_shorts (pixel
, raw_width
);
1543 for (col
=0; col
< raw_width
; col
+=2) {
1544 a
= pixel
[col
+0] ^ akey
;
1545 b
= pixel
[col
+1] ^ bkey
;
1546 pixel
[col
+0] = (a
& mask
) | (b
& ~mask
);
1547 pixel
[col
+1] = (b
& mask
) | (a
& ~mask
);
1549 for (col
=0; col
< raw_width
; col
++)
1551 ushort
*dfp
= get_masked_pointer(row
,col
);
1555 BAYER(row
,col
-left_margin
) = pixel
[col
];
1559 if(!( filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
) )
1561 phase_one_correct();
1564 unsigned CLASS
ph1_bits (int nbits
)
1566 #ifndef LIBRAW_NOTHREADS
1567 #define bitbuf tls->ph1_bits.bitbuf
1568 #define vbits tls->ph1_bits.vbits
1570 static UINT64 bitbuf
=0;
1574 return bitbuf
= vbits
= 0;
1575 if (nbits
== 0) return 0;
1576 if ((vbits
-= nbits
) < 0) {
1577 bitbuf
= bitbuf
<< 32 | get4();
1580 return bitbuf
<< (64-nbits
-vbits
) >> (64-nbits
);
1581 #ifndef LIBRAW_NOTHREADS
1587 void CLASS
phase_one_load_raw_c()
1589 static const int length
[] = { 8,7,6,9,11,10,5,12,14,13 };
1590 int *offset
, len
[2], pred
[2], row
, col
, i
, j
;
1592 short (*t_black
)[2];
1594 pixel
= (ushort
*) calloc (raw_width
+ raw_height
*4, 2);
1595 merror (pixel
, "phase_one_load_raw_c()");
1596 offset
= (int *) (pixel
+ raw_width
);
1597 fseek (ifp
, strip_offset
, SEEK_SET
);
1598 for (row
=0; row
< raw_height
; row
++)
1599 offset
[row
] = get4();
1600 t_black
= (short (*)[2]) offset
+ raw_height
;
1601 fseek (ifp
, ph1
.black_off
, SEEK_SET
);
1604 read_shorts ((ushort
*) t_black
[0], raw_height
*2);
1605 #ifdef LIBRAW_LIBRARY_BUILD
1606 imgdata
.masked_pixels
.ph1_black
= (ushort (*)[2])calloc(raw_height
*2,sizeof(ushort
));
1607 merror (imgdata
.masked_pixels
.ph1_black
, "phase_one_load_raw_c()");
1608 memmove(imgdata
.masked_pixels
.ph1_black
,(ushort
*) t_black
[0],raw_height
*2*sizeof(ushort
));
1611 for (i
=0; i
< 256; i
++)
1612 curve
[i
] = i
*i
/ 3.969 + 0.5;
1613 #ifdef LIBRAW_LIBRARY_BUILD
1614 color_flags
.curve_state
= LIBRAW_COLORSTATE_CALCULATED
;
1616 for (row
=0; row
< raw_height
; row
++) {
1617 fseek (ifp
, data_offset
+ offset
[row
], SEEK_SET
);
1619 pred
[0] = pred
[1] = 0;
1620 for (col
=0; col
< raw_width
; col
++) {
1621 if (col
>= (raw_width
& -8))
1622 len
[0] = len
[1] = 14;
1623 else if ((col
& 7) == 0)
1624 for (i
=0; i
< 2; i
++) {
1625 for (j
=0; j
< 5 && !ph1_bits(1); j
++);
1626 if (j
--) len
[i
] = length
[j
*2 + ph1_bits(1)];
1628 if ((i
= len
[col
& 1]) == 14)
1629 pixel
[col
] = pred
[col
& 1] = ph1_bits(16);
1631 pixel
[col
] = pred
[col
& 1] += ph1_bits(i
) + 1 - (1 << (i
- 1));
1632 if (pred
[col
& 1] >> 16) derror();
1633 #ifdef LIBRAW_LIBRARY_BUILD
1634 if(!( filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
) )
1636 if (ph1
.format
== 5 && pixel
[col
] < 256)
1637 pixel
[col
] = curve
[pixel
[col
]];
1639 if ((unsigned) (row
-top_margin
) < height
)
1640 #ifndef LIBRAW_LIBRARY_BUILD
1641 for (col
=0; col
< width
; col
++) {
1642 i
= (pixel
[col
+left_margin
] << 2)
1643 - ph1
.t_black
+ t_black
[row
][col
>= ph1
.split_col
];
1644 if (i
> 0) BAYER(row
-top_margin
,col
) = i
;
1648 for (col
=0; col
< raw_width
; col
++) {
1649 if( filtering_mode
& LIBRAW_FILTERING_NOBLACKS
)
1650 i
= (pixel
[col
] << 2);
1652 i
= (pixel
[col
] << 2)
1653 - ph1
.t_black
+ t_black
[row
][(col
/* - left_margin */) >= ph1
.split_col
]; // changed to fix Coffin's bug!
1654 if(col
>= left_margin
&& col
< width
+left_margin
)
1656 if (i
> 0) BAYER(row
-top_margin
,col
-left_margin
) = i
;
1660 ushort
*dfp
= get_masked_pointer(row
,col
);
1661 if(i
>0 && dfp
) *dfp
= i
;
1667 // top-bottom fields
1668 for (col
=0; col
< raw_width
; col
++) {
1669 i
= (pixel
[col
] << 2)
1670 - ph1
.t_black
+ t_black
[row
][(col
+left_margin
) >= ph1
.split_col
];
1673 ushort
*dfp
= get_masked_pointer(row
,col
);
1681 #ifdef LIBRAW_LIBRARY_BUILD
1682 if(!( filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
) )
1684 phase_one_correct();
1685 maximum
= 0xfffc - ph1
.t_black
;
1688 void CLASS
hasselblad_load_raw()
1691 struct decode
*dindex
;
1692 int row
, col
, pred
[2], len
[2], diff
, i
;
1694 if (!ljpeg_start (&jh
, 0)) return;
1698 for (row
=-top_margin
; row
< raw_height
-top_margin
; row
++) {
1699 pred
[0] = pred
[1] = 0x8000;
1700 for (col
=-left_margin
; col
< raw_width
-left_margin
; col
+=2) {
1701 for (i
=0; i
< 2; i
++) {
1702 for (dindex
=jh
.huff
[0]; dindex
->branch
[0]; )
1703 dindex
= dindex
->branch
[ph1_bits(1)];
1704 len
[i
] = dindex
->leaf
;
1706 for (i
=0; i
< 2; i
++) {
1707 diff
= ph1_bits(len
[i
]);
1708 if ((diff
& (1 << (len
[i
]-1))) == 0)
1709 diff
-= (1 << len
[i
]) - 1;
1710 if (diff
== 65535) diff
= -32768;
1712 if (row
>= 0 && row
< height
&& (unsigned)(col
+i
) < width
)
1713 BAYER(row
,col
+i
) = pred
[i
];
1714 #ifdef LIBRAW_LIBRARY_BUILD
1717 ushort
*dfp
= get_masked_pointer(row
+top_margin
,col
+left_margin
);
1718 if(dfp
) *dfp
= pred
[i
];
1727 void CLASS
leaf_hdr_load_raw()
1730 unsigned tile
=0, r
, c
, row
, col
;
1732 pixel
= (ushort
*) calloc (raw_width
, sizeof *pixel
);
1733 merror (pixel
, "leaf_hdr_load_raw()");
1735 for (r
=0; r
< raw_height
; r
++) {
1736 if (r
% tile_length
== 0) {
1737 fseek (ifp
, data_offset
+ 4*tile
++, SEEK_SET
);
1738 fseek (ifp
, get4() + 2*left_margin
, SEEK_SET
);
1740 if (filters
&& c
!= shot_select
) continue;
1741 read_shorts (pixel
, raw_width
);
1742 if ((row
= r
- top_margin
) >= height
) continue;
1743 for (col
=0; col
< width
; col
++)
1744 if (filters
) BAYER(row
,col
) = pixel
[col
];
1745 else image
[row
*width
+col
][c
] = pixel
[col
];
1754 #line 2046 "dcraw/dcraw.c"
1755 void CLASS
sinar_4shot_load_raw()
1758 unsigned shot
, row
, col
, r
, c
;
1760 if ((shot
= shot_select
) || half_size
) {
1762 if (shot
> 3) shot
= 3;
1763 fseek (ifp
, data_offset
+ shot
*4, SEEK_SET
);
1764 fseek (ifp
, get4(), SEEK_SET
);
1765 unpacked_load_raw();
1769 image
= (ushort (*)[4])
1770 calloc ((iheight
=height
)*(iwidth
=width
), sizeof *image
);
1771 merror (image
, "sinar_4shot_load_raw()");
1772 pixel
= (ushort
*) calloc (raw_width
, sizeof *pixel
);
1773 merror (pixel
, "sinar_4shot_load_raw()");
1774 for (shot
=0; shot
< 4; shot
++) {
1775 fseek (ifp
, data_offset
+ shot
*4, SEEK_SET
);
1776 fseek (ifp
, get4(), SEEK_SET
);
1777 for (row
=0; row
< raw_height
; row
++) {
1778 read_shorts (pixel
, raw_width
);
1779 if ((r
= row
-top_margin
- (shot
>> 1 & 1)) >= height
) continue;
1780 for (col
=0; col
< raw_width
; col
++) {
1781 if ((c
= col
-left_margin
- (shot
& 1)) >= width
) continue;
1782 image
[r
*width
+c
][FC(row
,col
)] = pixel
[col
];
1787 shrink
= filters
= 0;
1790 void CLASS
imacon_full_load_raw()
1794 for (row
=0; row
< height
; row
++)
1795 for (col
=0; col
< width
; col
++)
1796 read_shorts (image
[row
*width
+col
], 3);
1799 void CLASS
packed_12_load_raw()
1801 int vbits
=0, rbits
=0, irow
, row
, col
;
1804 if (raw_width
* 2 >= width
* 3) { /* If raw_width is in bytes, */
1805 rbits
= raw_width
* 8;
1806 raw_width
= raw_width
* 2 / 3; /* convert it to pixels and */
1807 rbits
-= raw_width
* 12; /* save the remainder. */
1809 order
= load_flags
& 1 ? 0x4949 : 0x4d4d;
1810 for (irow
=0; irow
< height
; irow
++) {
1812 if (load_flags
& 2 &&
1813 (row
= irow
* 2 % height
+ irow
/ (height
/2)) == 1 &&
1815 if (vbits
=0, tiff_compress
)
1816 fseek (ifp
, data_offset
- (-width
*height
*3/4 & -2048), SEEK_SET
);
1818 fseek (ifp
, 0, SEEK_END
);
1819 fseek (ifp
, ftell(ifp
)/2, SEEK_SET
);
1822 for (col
=0; col
< raw_width
; col
++) {
1823 if ((vbits
-= 12) < 0) {
1824 bitbuf
= bitbuf
<< 32 | get4();
1827 if ((unsigned) (col
-left_margin
) < width
)
1828 BAYER(row
,col
-left_margin
) = bitbuf
<< (52-vbits
) >> 52;
1829 #ifdef LIBRAW_LIBRARY_BUILD
1832 ushort
*dfp
= get_masked_pointer(row
,col
);
1833 if(dfp
) *dfp
= bitbuf
<< (52-vbits
) >> 52;
1836 if (load_flags
& 8 && (col
% 10) == 9)
1837 if (vbits
=0, bitbuf
& 255) derror();
1841 if (!strcmp(make
,"OLYMPUS")) black
>>= 4;
1844 void CLASS
unpacked_load_raw()
1847 int row
, col
, bits
=0;
1849 while (1 << ++bits
< maximum
);
1850 #ifndef LIBRAW_LIBRARY_BUILD
1851 fseek (ifp
, (top_margin
*raw_width
+ left_margin
) * 2, SEEK_CUR
);
1852 pixel
= (ushort
*) calloc (width
, sizeof *pixel
);
1853 merror (pixel
, "unpacked_load_raw()");
1854 for (row
=0; row
< height
; row
++) {
1855 read_shorts (pixel
, width
);
1856 fseek (ifp
, 2*(raw_width
- width
), SEEK_CUR
);
1857 for (col
=0; col
< width
; col
++)
1858 if ((BAYER2(row
,col
) = pixel
[col
]) >> bits
) derror();
1862 // fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
1863 pixel
= (ushort
*) calloc (raw_width
, sizeof *pixel
);
1864 merror (pixel
, "unpacked_load_raw()");
1865 for (row
=0; row
< raw_height
; row
++) {
1866 read_shorts (pixel
, raw_width
);
1867 //fseek (ifp, 2*(raw_width - width), SEEK_CUR);
1868 for (col
=0; col
< raw_width
; col
++)
1870 ushort
*dfp
= get_masked_pointer(row
,col
);
1875 if ((BAYER2(row
-top_margin
,col
-left_margin
) = pixel
[col
]) >> bits
) derror();
1883 void CLASS
nokia_load_raw()
1886 ushort
*pixel
, *pix
;
1889 dwide
= raw_width
* 5 / 4;
1890 data
= (uchar
*) malloc (dwide
+ raw_width
*2);
1891 merror (data
, "nokia_load_raw()");
1892 pixel
= (ushort
*) (data
+ dwide
);
1893 for (row
=0; row
< raw_height
; row
++) {
1894 if (fread (data
, 1, dwide
, ifp
) < dwide
) derror();
1895 for (dp
=data
, pix
=pixel
; pix
< pixel
+raw_width
; dp
+=5, pix
+=4)
1896 FORC4 pix
[c
] = (dp
[c
] << 2) | (dp
[4] >> (c
<< 1) & 3);
1897 if (row
< top_margin
)
1898 #ifdef LIBRAW_LIBRARY_BUILD
1901 for(col
=0;col
<width
;col
++)
1903 ushort
*dfp
= get_masked_pointer(row
,col
);
1907 FORC(width
) black
+= pixel
[c
];
1910 FORC(width
) black
+= pixel
[c
];
1913 FORC(width
) BAYER(row
-top_margin
,c
) = pixel
[c
];
1916 if (top_margin
) black
/= top_margin
* width
;
1920 unsigned CLASS
pana_bits (int nbits
)
1922 #ifndef LIBRAW_NOTHREADS
1923 #define buf tls->pana_bits.buf
1924 #define vbits tls->pana_bits.vbits
1926 static uchar buf
[0x4000];
1931 if (!nbits
) return vbits
=0;
1933 fread (buf
+load_flags
, 1, 0x4000-load_flags
, ifp
);
1934 fread (buf
, 1, load_flags
, ifp
);
1936 vbits
= (vbits
- nbits
) & 0x1ffff;
1937 byte
= vbits
>> 3 ^ 0x3ff0;
1938 return (buf
[byte
] | buf
[byte
+1] << 8) >> (vbits
& 7) & ~(-1 << nbits
);
1939 #ifndef LIBRAW_NOTHREADS
1945 void CLASS
panasonic_load_raw()
1947 int row
, col
, i
, j
, sh
=0, pred
[2], nonz
[2];
1950 for (row
=0; row
< height
; row
++)
1951 for (col
=0; col
< raw_width
; col
++) {
1952 if ((i
= col
% 14) == 0)
1953 pred
[0] = pred
[1] = nonz
[0] = nonz
[1] = 0;
1954 if (i
% 3 == 2) sh
= 4 >> (3 - pana_bits(2));
1956 if ((j
= pana_bits(8))) {
1957 if ((pred
[i
& 1] -= 0x80 << sh
) < 0 || sh
== 4)
1958 pred
[i
& 1] &= ~(-1 << sh
);
1959 pred
[i
& 1] += j
<< sh
;
1961 } else if ((nonz
[i
& 1] = pana_bits(8)) || i
> 11)
1962 pred
[i
& 1] = nonz
[i
& 1] << 4 | pana_bits(4);
1964 if ((BAYER(row
,col
) = pred
[col
& 1]) > 4098) derror();
1965 #ifdef LIBRAW_LIBRARY_BUILD
1968 ushort
*dfp
= get_masked_pointer(row
,col
);
1969 if(dfp
)*dfp
= pred
[col
& 1];
1975 void CLASS
olympus_e300_load_raw()
1978 ushort
*pixel
, *pix
;
1979 int dwide
, row
, col
;
1981 dwide
= raw_width
* 16 / 10;
1982 #ifndef LIBRAW_LIBRARY_BUILD
1983 fseek (ifp
, dwide
*top_margin
, SEEK_CUR
);
1985 data
= (uchar
*) malloc (dwide
+ raw_width
*2);
1986 merror (data
, "olympus_e300_load_raw()");
1987 pixel
= (ushort
*) (data
+ dwide
);
1988 #ifndef LIBRAW_LIBRARY_BUILD
1989 for (row
=0; row
< height
; row
++)
1991 for (row
=0; row
< raw_height
; row
++)
1994 if (fread (data
, 1, dwide
, ifp
) < dwide
) derror();
1995 for (dp
=data
, pix
=pixel
; pix
< pixel
+raw_width
; dp
+=3, pix
+=2) {
1996 if (((dp
-data
) & 15) == 15)
1997 if (*dp
++ && pix
< pixel
+width
+left_margin
) derror();
1998 pix
[0] = dp
[1] << 8 | dp
[0];
1999 pix
[1] = dp
[2] << 4 | dp
[1] >> 4;
2001 #ifndef LIBRAW_LIBRARY_BUILD
2002 for (col
=0; col
< width
; col
++)
2003 BAYER(row
,col
) = (pixel
[col
+left_margin
] & 0xfff);
2005 for (col
=0; col
< raw_width
; col
++)
2007 ushort
*dfp
= get_masked_pointer(row
,col
);
2009 *dfp
= (pixel
[col
] & 0xfff);
2011 BAYER(row
-top_margin
,col
-left_margin
) = (pixel
[col
] & 0xfff);
2021 void CLASS
olympus_e410_load_raw()
2023 int row
, col
, nbits
, sign
, low
, high
, i
, w
, n
, nw
;
2024 int acarry
[2][3], *carry
, pred
, diff
;
2026 fseek (ifp
, 7, SEEK_CUR
);
2028 for (row
=0; row
< height
; row
++) {
2029 memset (acarry
, 0, sizeof acarry
);
2030 for (col
=0; col
< width
; col
++) {
2031 carry
= acarry
[col
& 1];
2032 i
= 2 * (carry
[2] < 3);
2033 for (nbits
=2+i
; (ushort
) carry
[0] >> (nbits
+i
); nbits
++);
2034 sign
= getbits(1) * -1;
2036 for (high
=0; high
< 12; high
++)
2037 if (getbits(1)) break;
2039 high
= getbits(16-nbits
) >> 1;
2040 carry
[0] = (high
<< nbits
) | getbits(nbits
);
2041 diff
= (carry
[0] ^ sign
) + carry
[1];
2042 carry
[1] = (diff
*3 + carry
[1]) >> 5;
2043 carry
[2] = carry
[0] > 16 ? 0 : carry
[2]+1;
2044 if (row
< 2 && col
< 2) pred
= 0;
2045 else if (row
< 2) pred
= BAYER(row
,col
-2);
2046 else if (col
< 2) pred
= BAYER(row
-2,col
);
2048 w
= BAYER(row
,col
-2);
2049 n
= BAYER(row
-2,col
);
2050 nw
= BAYER(row
-2,col
-2);
2051 if ((w
< nw
&& nw
< n
) || (n
< nw
&& nw
< w
)) {
2052 if (ABS(w
-nw
) > 32 || ABS(n
-nw
) > 32)
2054 else pred
= (w
+ n
) >> 1;
2055 } else pred
= ABS(w
-nw
) > ABS(n
-nw
) ? w
: n
;
2057 if ((BAYER(row
,col
) = pred
+ ((diff
<< 2) | low
)) >> 12) derror();
2062 void CLASS
minolta_rd175_load_raw()
2065 unsigned irow
, box
, row
, col
;
2067 for (irow
=0; irow
< 1481; irow
++) {
2068 if (fread (pixel
, 1, 768, ifp
) < 768) derror();
2070 row
= irow
% 82 * 12 + ((box
< 12) ? box
| 1 : (box
-12)*2);
2072 case 1477: case 1479: continue;
2073 case 1476: row
= 984; break;
2074 case 1480: row
= 985; break;
2075 case 1478: row
= 985; box
= 1;
2077 if ((box
< 12) && (box
& 1)) {
2078 for (col
=0; col
< 1533; col
++, row
^= 1)
2079 if (col
!= 1) BAYER(row
,col
) = (col
+1) & 2 ?
2080 pixel
[col
/2-1] + pixel
[col
/2+1] : pixel
[col
/2] << 1;
2081 BAYER(row
,1) = pixel
[1] << 1;
2082 BAYER(row
,1533) = pixel
[765] << 1;
2084 for (col
=row
& 1; col
< 1534; col
+=2)
2085 BAYER(row
,col
) = pixel
[col
/2] << 1;
2087 maximum
= 0xff << 1;
2090 void CLASS
casio_qv5700_load_raw()
2092 uchar data
[3232], *dp
;
2093 ushort pixel
[2576], *pix
;
2096 for (row
=0; row
< height
; row
++) {
2097 fread (data
, 1, 3232, ifp
);
2098 for (dp
=data
, pix
=pixel
; dp
< data
+3220; dp
+=5, pix
+=4) {
2099 pix
[0] = (dp
[0] << 2) + (dp
[1] >> 6);
2100 pix
[1] = (dp
[1] << 4) + (dp
[2] >> 4);
2101 pix
[2] = (dp
[2] << 6) + (dp
[3] >> 2);
2102 pix
[3] = (dp
[3] << 8) + (dp
[4] );
2104 for (col
=0; col
< width
; col
++)
2105 BAYER(row
,col
) = (pixel
[col
] & 0x3ff);
2110 void CLASS
quicktake_100_load_raw()
2112 uchar pixel
[484][644];
2113 static const short gstep
[16] =
2114 { -89,-60,-44,-32,-22,-15,-8,-2,2,8,15,22,32,44,60,89 };
2115 static const short rstep
[6][4] =
2116 { { -3,-1,1,3 }, { -5,-1,1,5 }, { -8,-2,2,8 },
2117 { -13,-3,3,13 }, { -19,-4,4,19 }, { -28,-6,6,28 } };
2118 static const short t_curve
[256] =
2119 { 0,1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
2120 28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,53,
2121 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,
2122 79,80,81,82,83,84,86,88,90,92,94,97,99,101,103,105,107,110,112,114,116,
2123 118,120,123,125,127,129,131,134,136,138,140,142,144,147,149,151,153,155,
2124 158,160,162,164,166,168,171,173,175,177,179,181,184,186,188,190,192,195,
2125 197,199,201,203,205,208,210,212,214,216,218,221,223,226,230,235,239,244,
2126 248,252,257,261,265,270,274,278,283,287,291,296,300,305,309,313,318,322,
2127 326,331,335,339,344,348,352,357,361,365,370,374,379,383,387,392,396,400,
2128 405,409,413,418,422,426,431,435,440,444,448,453,457,461,466,470,474,479,
2129 483,487,492,496,500,508,519,531,542,553,564,575,587,598,609,620,631,643,
2130 654,665,676,687,698,710,721,732,743,754,766,777,788,799,810,822,833,844,
2131 855,866,878,889,900,911,922,933,945,956,967,978,989,1001,1012,1023 };
2132 int rb
, row
, col
, sharp
, val
=0;
2135 memset (pixel
, 0x80, sizeof pixel
);
2136 for (row
=2; row
< height
+2; row
++) {
2137 for (col
=2+(row
& 1); col
< width
+2; col
+=2) {
2138 val
= ((pixel
[row
-1][col
-1] + 2*pixel
[row
-1][col
+1] +
2139 pixel
[row
][col
-2]) >> 2) + gstep
[getbits(4)];
2140 pixel
[row
][col
] = val
= LIM(val
,0,255);
2142 pixel
[row
][col
-2] = pixel
[row
+1][~row
& 1] = val
;
2144 pixel
[row
-1][col
+1] = pixel
[row
-1][col
+3] = val
;
2146 pixel
[row
][col
] = val
;
2148 for (rb
=0; rb
< 2; rb
++)
2149 for (row
=2+rb
; row
< height
+2; row
+=2)
2150 for (col
=3-(row
& 1); col
< width
+2; col
+=2) {
2151 if (row
< 4 || col
< 4) sharp
= 2;
2153 val
= ABS(pixel
[row
-2][col
] - pixel
[row
][col
-2])
2154 + ABS(pixel
[row
-2][col
] - pixel
[row
-2][col
-2])
2155 + ABS(pixel
[row
][col
-2] - pixel
[row
-2][col
-2]);
2156 sharp
= val
< 4 ? 0 : val
< 8 ? 1 : val
< 16 ? 2 :
2157 val
< 32 ? 3 : val
< 48 ? 4 : 5;
2159 val
= ((pixel
[row
-2][col
] + pixel
[row
][col
-2]) >> 1)
2160 + rstep
[sharp
][getbits(2)];
2161 pixel
[row
][col
] = val
= LIM(val
,0,255);
2162 if (row
< 4) pixel
[row
-2][col
+2] = val
;
2163 if (col
< 4) pixel
[row
+2][col
-2] = val
;
2165 for (row
=2; row
< height
+2; row
++)
2166 for (col
=3-(row
& 1); col
< width
+2; col
+=2) {
2167 val
= ((pixel
[row
][col
-1] + (pixel
[row
][col
] << 2) +
2168 pixel
[row
][col
+1]) >> 1) - 0x100;
2169 pixel
[row
][col
] = LIM(val
,0,255);
2171 for (row
=0; row
< height
; row
++)
2172 for (col
=0; col
< width
; col
++)
2173 BAYER(row
,col
) = t_curve
[pixel
[row
+2][col
+2]];
2177 const int * CLASS
make_decoder_int (const int *source
, int level
)
2181 cur
= free_decode
++;
2182 if (level
< source
[0]) {
2183 cur
->branch
[0] = free_decode
;
2184 source
= make_decoder_int (source
, level
+1);
2185 cur
->branch
[1] = free_decode
;
2186 source
= make_decoder_int (source
, level
+1);
2188 cur
->leaf
= source
[1];
2194 int CLASS
radc_token (int tree
)
2197 #ifndef LIBRAW_NOTHREADS
2198 #define dstart tls->radc_token.dstart
2199 #define dindex tls->radc_token.dindex
2200 #define s tls->radc_token.s
2202 static const int source
[] = {
2204 static struct decode
*dstart
[18], *dindex
;
2205 static const int *s
, source
[] = {
2207 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
2208 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
2209 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
2210 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
2211 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
2212 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
2213 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
2214 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
2215 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
2216 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
2219 2,-17, 2,-5, 2,5, 2,17,
2220 2,-7, 2,2, 2,9, 2,18,
2221 2,-18, 2,-9, 2,-2, 2,7,
2222 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
2223 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
2224 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
2227 if (free_decode
== first_decode
)
2228 for (s
=source
, t
=0; t
< 18; t
++) {
2229 dstart
[t
] = free_decode
;
2230 s
= make_decoder_int (s
, 0);
2233 if (kodak_cbpp
== 243)
2234 return (getbits(6) << 2) + 2; /* most DC50 photos */
2236 return (getbits(5) << 3) + 4; /* DC40, Fotoman Pixtura */
2238 for (dindex
= dstart
[tree
]; dindex
->branch
[0]; )
2239 dindex
= dindex
->branch
[getbits(1)];
2240 return dindex
->leaf
;
2242 #ifndef LIBRAW_NOTHREADS
2249 #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
2251 #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
2252 : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
2254 void CLASS
kodak_radc_load_raw()
2256 int row
, col
, tree
, nreps
, rep
, step
, i
, c
, s
, r
, x
, y
, val
;
2257 short last
[3] = { 16,16,16 }, mul
[3], buf
[3][3][386];
2261 for (i
=0; i
< sizeof(buf
)/sizeof(short); i
++)
2262 buf
[0][0][i
] = 2048;
2263 for (row
=0; row
< height
; row
+=4) {
2264 FORC3 mul
[c
] = getbits(6);
2266 val
= ((0x1000000/last
[c
] + 0x7ff) >> 12) * mul
[c
];
2267 s
= val
> 65564 ? 10:12;
2270 for (i
=0; i
< sizeof(buf
[0])/sizeof(short); i
++)
2271 buf
[c
][0][i
] = (buf
[c
][0][i
] * val
+ x
) >> s
;
2273 for (r
=0; r
<= !c
; r
++) {
2274 buf
[c
][1][width
/2] = buf
[c
][2][width
/2] = mul
[c
] << 7;
2275 for (tree
=1, col
=width
/2; col
> 0; ) {
2276 if ((tree
= radc_token(tree
))) {
2279 FORYX buf
[c
][y
][x
] = radc_token(tree
+10) * mul
[c
];
2281 FORYX buf
[c
][y
][x
] = radc_token(tree
+10) * 16 + PREDICTOR
;
2284 nreps
= (col
> 2) ? radc_token(9) + 1 : 1;
2285 for (rep
=0; rep
< 8 && rep
< nreps
&& col
> 0; rep
++) {
2287 FORYX buf
[c
][y
][x
] = PREDICTOR
;
2289 step
= radc_token(10) << 4;
2290 FORYX buf
[c
][y
][x
] += step
;
2293 } while (nreps
== 9);
2295 for (y
=0; y
< 2; y
++)
2296 for (x
=0; x
< width
/2; x
++) {
2297 val
= (buf
[c
][y
+1][x
] << 4) / mul
[c
];
2298 if (val
< 0) val
= 0;
2299 if (c
) BAYER(row
+y
*2+c
-1,x
*2+2-c
) = val
;
2300 else BAYER(row
+r
*2+y
,x
*2+y
) = val
;
2302 memcpy (buf
[c
][0]+!c
, buf
[c
][2], sizeof buf
[c
][0]-2*!c
);
2305 for (y
=row
; y
< row
+4; y
++)
2306 for (x
=0; x
< width
; x
++)
2309 s
= x
+1 < width
? x
+1 : x
-1;
2310 val
= (BAYER(y
,x
)-2048)*2 + (BAYER(y
,r
)+BAYER(y
,s
))/2;
2311 if (val
< 0) val
= 0;
2323 void CLASS
kodak_jpeg_load_raw() {}
2327 fill_input_buffer (j_decompress_ptr cinfo
)
2329 #ifndef LIBRAW_NOTHREADS
2330 #define jpeg_buffer tls->jpeg_buffer
2332 static uchar jpeg_buffer
[4096];
2336 nbytes
= fread (jpeg_buffer
, 1, 4096, ifp
);
2337 swab (jpeg_buffer
, jpeg_buffer
, nbytes
);
2338 cinfo
->src
->next_input_byte
= jpeg_buffer
;
2339 cinfo
->src
->bytes_in_buffer
= nbytes
;
2341 #ifndef LIBRAW_NOTHREADS
2346 void CLASS
kodak_jpeg_load_raw()
2348 struct jpeg_decompress_struct cinfo
;
2349 struct jpeg_error_mgr jerr
;
2351 JSAMPLE (*pixel
)[3];
2354 cinfo
.err
= jpeg_std_error (&jerr
);
2355 jpeg_create_decompress (&cinfo
);
2356 jpeg_stdio_src (&cinfo
, ifp
);
2357 cinfo
.src
->fill_input_buffer
= fill_input_buffer
;
2358 jpeg_read_header (&cinfo
, TRUE
);
2359 jpeg_start_decompress (&cinfo
);
2360 if ((cinfo
.output_width
!= width
) ||
2361 (cinfo
.output_height
*2 != height
) ||
2362 (cinfo
.output_components
!= 3 )) {
2363 #ifdef DCRAW_VERBOSE
2364 fprintf (stderr
,_("%s: incorrect JPEG dimensions\n"), ifname
);
2366 jpeg_destroy_decompress (&cinfo
);
2367 #ifdef LIBRAW_LIBRARY_BUILD
2368 throw LIBRAW_EXCEPTION_DECODE_JPEG
;
2370 longjmp (failure
, 3);
2373 buf
= (*cinfo
.mem
->alloc_sarray
)
2374 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, width
*3, 1);
2376 while (cinfo
.output_scanline
< cinfo
.output_height
) {
2377 row
= cinfo
.output_scanline
* 2;
2378 jpeg_read_scanlines (&cinfo
, buf
, 1);
2379 pixel
= (JSAMPLE (*)[3]) buf
[0];
2380 for (col
=0; col
< width
; col
+=2) {
2381 BAYER(row
+0,col
+0) = pixel
[col
+0][1] << 1;
2382 BAYER(row
+1,col
+1) = pixel
[col
+1][1] << 1;
2383 BAYER(row
+0,col
+1) = pixel
[col
][0] + pixel
[col
+1][0];
2384 BAYER(row
+1,col
+0) = pixel
[col
][2] + pixel
[col
+1][2];
2387 jpeg_finish_decompress (&cinfo
);
2388 jpeg_destroy_decompress (&cinfo
);
2389 maximum
= 0xff << 1;
2393 void CLASS
kodak_dc120_load_raw()
2395 static const int mul
[4] = { 162, 192, 187, 92 };
2396 static const int add
[4] = { 0, 636, 424, 212 };
2398 int row
, shift
, col
;
2400 for (row
=0; row
< height
; row
++) {
2401 if (fread (pixel
, 1, 848, ifp
) < 848) derror();
2402 shift
= row
* mul
[row
& 3] + add
[row
& 3];
2403 for (col
=0; col
< width
; col
++)
2404 BAYER(row
,col
) = (ushort
) pixel
[(col
+ shift
) % 848];
2409 void CLASS
eight_bit_load_raw()
2412 unsigned row
, col
, val
, lblack
=0;
2414 pixel
= (uchar
*) calloc (raw_width
, sizeof *pixel
);
2415 merror (pixel
, "eight_bit_load_raw()");
2416 #ifndef LIBRAW_LIBRARY_BUILD
2417 fseek (ifp
, top_margin
*raw_width
, SEEK_CUR
);
2418 for (row
=0; row
< height
; row
++) {
2419 if (fread (pixel
, 1, raw_width
, ifp
) < raw_width
) derror();
2420 for (col
=0; col
< raw_width
; col
++) {
2421 val
= curve
[pixel
[col
]];
2422 if ((unsigned) (col
-left_margin
) < width
)
2423 BAYER(row
,col
-left_margin
) = val
;
2428 for (row
=0; row
< raw_height
; row
++) {
2429 if (fread (pixel
, 1, raw_width
, ifp
) < raw_width
) derror();
2430 for (col
=0; col
< raw_width
; col
++) {
2431 if(filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
)
2434 if(val
>maximum
) maximum
= val
;
2437 val
= curve
[pixel
[col
]];
2438 if((unsigned) (row
-top_margin
)< height
)
2440 if ((unsigned) (col
-left_margin
) < width
)
2441 BAYER(row
,col
-left_margin
) = val
;
2444 ushort
*dfp
= get_masked_pointer(row
,col
);
2449 else // top/bottom margins
2451 ushort
*dfp
= get_masked_pointer(row
,col
);
2459 if (raw_width
> width
+1)
2460 black
= lblack
/ ((raw_width
- width
) * height
);
2461 if (!strncmp(model
,"DC2",3))
2463 #ifdef LIBRAW_LIBRARY_BUILD
2464 if(!(filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
))
2466 maximum
= curve
[0xff];
2469 void CLASS
kodak_yrgb_load_raw()
2472 int row
, col
, y
, cb
, cr
, rgb
[3], c
;
2474 pixel
= (uchar
*) calloc (raw_width
, 3*sizeof *pixel
);
2475 merror (pixel
, "kodak_yrgb_load_raw()");
2476 for (row
=0; row
< height
; row
++) {
2478 if (fread (pixel
, raw_width
, 3, ifp
) < 3) derror();
2479 for (col
=0; col
< raw_width
; col
++) {
2480 y
= pixel
[width
*2*(row
& 1) + col
];
2481 cb
= pixel
[width
+ (col
& -2)] - 128;
2482 cr
= pixel
[width
+ (col
& -2)+1] - 128;
2483 rgb
[1] = y
-((cb
+ cr
+ 2) >> 2);
2484 rgb
[2] = rgb
[1] + cb
;
2485 rgb
[0] = rgb
[1] + cr
;
2486 FORC3 image
[row
*width
+col
][c
] = LIM(rgb
[c
],0,255);
2493 void CLASS
kodak_262_load_raw()
2495 static const uchar kodak_tree
[2][26] =
2496 { { 0,1,5,1,1,2,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9 },
2497 { 0,3,1,1,1,1,1,2,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9 } };
2498 struct decode
*decode
[2];
2500 int *strip
, ns
, i
, row
, col
, chess
, pi
=0, pi1
, pi2
, pred
, val
;
2503 for (i
=0; i
< 2; i
++) {
2504 decode
[i
] = free_decode
;
2505 make_decoder (kodak_tree
[i
], 0);
2507 ns
= (raw_height
+63) >> 5;
2508 pixel
= (uchar
*) malloc (raw_width
*32 + ns
*4);
2509 merror (pixel
, "kodak_262_load_raw()");
2510 strip
= (int *) (pixel
+ raw_width
*32);
2512 for (i
=0; i
< ns
; i
++)
2514 for (row
=0; row
< raw_height
; row
++) {
2515 if ((row
& 31) == 0) {
2516 fseek (ifp
, strip
[row
>> 5], SEEK_SET
);
2520 for (col
=0; col
< raw_width
; col
++) {
2521 chess
= (row
+ col
) & 1;
2522 pi1
= chess
? pi
-2 : pi
-raw_width
-1;
2523 pi2
= chess
? pi
-2*raw_width
: pi
-raw_width
+1;
2524 if (col
<= chess
) pi1
= -1;
2525 if (pi1
< 0) pi1
= pi2
;
2526 if (pi2
< 0) pi2
= pi1
;
2527 if (pi1
< 0 && col
> 1) pi1
= pi2
= pi
-2;
2528 pred
= (pi1
< 0) ? 0 : (pixel
[pi1
] + pixel
[pi2
]) >> 1;
2529 pixel
[pi
] = val
= pred
+ ljpeg_diff (decode
[chess
]);
2530 if (val
>> 8) derror();
2531 #ifdef LIBRAW_LIBRARY_BUILD
2532 if(filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
)
2535 val
= curve
[pixel
[pi
++]];
2537 val
= curve
[pixel
[pi
++]];
2539 if ((unsigned) (col
-left_margin
) < width
)
2540 BAYER(row
,col
-left_margin
) = val
;
2542 #ifndef LIBRAW_LIBRARY_BUILD
2546 ushort
*dfp
= get_masked_pointer(row
,col
);
2554 if (raw_width
> width
)
2555 black
/= (raw_width
- width
) * height
;
2558 int CLASS
kodak_65000_decode (short *out
, int bsize
)
2563 int save
, bits
=0, i
, j
, len
, diff
;
2566 bsize
= (bsize
+ 3) & -4;
2567 for (i
=0; i
< bsize
; i
+=2) {
2569 if ((blen
[i
] = c
& 15) > 12 ||
2570 (blen
[i
+1] = c
>> 4) > 12 ) {
2571 fseek (ifp
, save
, SEEK_SET
);
2572 for (i
=0; i
< bsize
; i
+=8) {
2573 read_shorts (raw
, 6);
2574 out
[i
] = raw
[0] >> 12 << 8 | raw
[2] >> 12 << 4 | raw
[4] >> 12;
2575 out
[i
+1] = raw
[1] >> 12 << 8 | raw
[3] >> 12 << 4 | raw
[5] >> 12;
2576 for (j
=0; j
< 6; j
++)
2577 out
[i
+2+j
] = raw
[j
] & 0xfff;
2582 if ((bsize
& 7) == 4) {
2583 bitbuf
= fgetc(ifp
) << 8;
2584 bitbuf
+= fgetc(ifp
);
2587 for (i
=0; i
< bsize
; i
++) {
2590 for (j
=0; j
< 32; j
+=8)
2591 bitbuf
+= (INT64
) fgetc(ifp
) << (bits
+(j
^8));
2594 diff
= bitbuf
& (0xffff >> (16-len
));
2597 if ((diff
& (1 << (len
-1))) == 0)
2598 diff
-= (1 << len
) - 1;
2604 void CLASS
kodak_65000_load_raw()
2607 int row
, col
, len
, pred
[2], ret
, i
;
2609 for (row
=0; row
< height
; row
++)
2610 for (col
=0; col
< width
; col
+=256) {
2611 pred
[0] = pred
[1] = 0;
2612 len
= MIN (256, width
-col
);
2613 ret
= kodak_65000_decode (buf
, len
);
2614 for (i
=0; i
< len
; i
++)
2615 #ifndef LIBRAW_LIBRARY_BUILD
2616 if ((BAYER(row
,col
+i
) = curve
[ret
? buf
[i
] :
2617 (pred
[i
& 1] += buf
[i
])]) >> 12) derror();
2620 ushort val
= ret
? buf
[i
] : (pred
[i
& 1] += buf
[i
]);
2621 if(!(filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
))
2623 BAYER(row
,col
+i
)=val
;
2624 if(curve
[val
]>>12) derror();
2630 void CLASS
kodak_ycbcr_load_raw()
2632 short buf
[384], *bp
;
2633 int row
, col
, len
, c
, i
, j
, k
, y
[2][2], cb
, cr
, rgb
[3];
2636 for (row
=0; row
< height
; row
+=2)
2637 for (col
=0; col
< width
; col
+=128) {
2638 len
= MIN (128, width
-col
);
2639 kodak_65000_decode (buf
, len
*3);
2640 y
[0][1] = y
[1][1] = cb
= cr
= 0;
2641 for (bp
=buf
, i
=0; i
< len
; i
+=2, bp
+=2) {
2644 rgb
[1] = -((cb
+ cr
+ 2) >> 2);
2645 rgb
[2] = rgb
[1] + cb
;
2646 rgb
[0] = rgb
[1] + cr
;
2647 for (j
=0; j
< 2; j
++)
2648 for (k
=0; k
< 2; k
++) {
2649 if ((y
[j
][k
] = y
[j
][k
^1] + *bp
++) >> 10) derror();
2650 ip
= image
[(row
+j
)*width
+ col
+i
+k
];
2651 #ifndef LIBRAW_LIBRARY_BUILD
2652 FORC3 ip
[c
] = curve
[LIM(y
[j
][k
]+rgb
[c
], 0, 0xfff)];
2654 if(!(filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
))
2655 FORC3 ip
[c
] = curve
[LIM(y
[j
][k
]+rgb
[c
], 0, 0xfff)];
2657 FORC3 ip
[c
] = y
[j
][k
]+rgb
[c
];;
2664 void CLASS
kodak_rgb_load_raw()
2666 short buf
[768], *bp
;
2667 int row
, col
, len
, c
, i
, rgb
[3];
2668 ushort
*ip
=image
[0];
2670 for (row
=0; row
< height
; row
++)
2671 for (col
=0; col
< width
; col
+=256) {
2672 len
= MIN (256, width
-col
);
2673 kodak_65000_decode (buf
, len
*3);
2674 memset (rgb
, 0, sizeof rgb
);
2675 for (bp
=buf
, i
=0; i
< len
; i
++, ip
+=4)
2676 FORC3
if ((ip
[c
] = rgb
[c
] += *bp
++) >> 12) derror();
2680 void CLASS
kodak_thumb_load_raw()
2683 colors
= thumb_misc
>> 5;
2684 for (row
=0; row
< height
; row
++)
2685 for (col
=0; col
< width
; col
++)
2686 read_shorts (image
[row
*width
+col
], colors
);
2687 maximum
= (1 << (thumb_misc
& 31)) - 1;
2690 void CLASS
sony_decrypt (unsigned *data
, int len
, int start
, int key
)
2692 #ifndef LIBRAW_NOTHREADS
2693 #define pad tls->sony_decrypt.pad
2694 #define p tls->sony_decrypt.p
2696 static unsigned pad
[128], p
;
2700 for (p
=0; p
< 4; p
++)
2701 pad
[p
] = key
= key
* 48828125 + 1;
2702 pad
[3] = pad
[3] << 1 | (pad
[0]^pad
[2]) >> 31;
2703 for (p
=4; p
< 127; p
++)
2704 pad
[p
] = (pad
[p
-4]^pad
[p
-2]) << 1 | (pad
[p
-3]^pad
[p
-1]) >> 31;
2705 for (p
=0; p
< 127; p
++)
2706 pad
[p
] = htonl(pad
[p
]);
2709 *data
++ ^= pad
[p
++ & 127] = pad
[(p
+1) & 127] ^ pad
[(p
+65) & 127];
2710 #ifndef LIBRAW_NOTHREADS
2716 void CLASS
sony_load_raw()
2720 unsigned i
, key
, row
, col
;
2722 fseek (ifp
, 200896, SEEK_SET
);
2723 fseek (ifp
, (unsigned) fgetc(ifp
)*4 - 1, SEEK_CUR
);
2726 fseek (ifp
, 164600, SEEK_SET
);
2727 fread (head
, 1, 40, ifp
);
2728 sony_decrypt ((unsigned int *) head
, 10, 1, key
);
2729 for (i
=26; i
-- > 22; )
2730 key
= key
<< 8 | head
[i
];
2731 fseek (ifp
, data_offset
, SEEK_SET
);
2732 pixel
= (ushort
*) calloc (raw_width
, sizeof *pixel
);
2733 merror (pixel
, "sony_load_raw()");
2734 for (row
=0; row
< height
; row
++) {
2735 if (fread (pixel
, 2, raw_width
, ifp
) < raw_width
) derror();
2736 sony_decrypt ((unsigned int *) pixel
, raw_width
/2, !row
, key
);
2737 #ifdef LIBRAW_LIBRARY_BUILD
2738 for (col
=0; col
< left_margin
; col
++)
2740 ushort
*dfp
= get_masked_pointer(row
,col
);
2741 if(dfp
) *dfp
= ntohs(pixel
[col
]);
2743 for (col
=left_margin
+width
; col
< raw_width
; col
++)
2745 ushort
*dfp
= get_masked_pointer(row
,col
);
2746 if(dfp
) *dfp
= ntohs(pixel
[col
]);
2749 for (col
=9; col
< left_margin
; col
++)
2750 black
+= ntohs(pixel
[col
]);
2751 for (col
=0; col
< width
; col
++)
2752 if ((BAYER(row
,col
) = ntohs(pixel
[col
+left_margin
])) >> 14)
2756 if (left_margin
> 9)
2757 black
/= (left_margin
-9) * height
;
2761 void CLASS
sony_arw_load_raw()
2763 int col
, row
, len
, diff
, sum
=0;
2766 for (col
= raw_width
; col
--; )
2767 for (row
=0; row
< raw_height
+1; row
+=2) {
2768 if (row
== raw_height
) row
= 1;
2769 len
= 4 - getbits(2);
2770 if (len
== 3 && getbits(1)) len
= 0;
2772 while (len
< 17 && !getbits(1)) len
++;
2773 diff
= getbits(len
);
2774 if ((diff
& (1 << (len
-1))) == 0)
2775 diff
-= (1 << len
) - 1;
2776 if ((sum
+= diff
) >> 12) derror();
2777 if (row
< height
) BAYER(row
,col
) = sum
;
2778 #ifdef LIBRAW_LIBRARY_BUILD
2781 ushort
*dfp
= get_masked_pointer(row
,col
);
2788 void CLASS
sony_arw2_load_raw()
2792 int row
, col
, val
, max
, min
, imax
, imin
, sh
, bit
, i
;
2794 data
= (uchar
*) malloc (raw_width
*tiff_bps
>> 3);
2795 merror (data
, "sony_arw2_load_raw()");
2796 for (row
=0; row
< height
; row
++) {
2797 fread (data
, 1, raw_width
*tiff_bps
>> 3, ifp
);
2798 if (tiff_bps
== 8) {
2799 for (dp
=data
, col
=0; col
< width
-30; dp
+=16) {
2800 max
= 0x7ff & (val
= sget4(dp
));
2801 min
= 0x7ff & val
>> 11;
2802 imax
= 0x0f & val
>> 22;
2803 imin
= 0x0f & val
>> 26;
2804 for (sh
=0; sh
< 4 && 0x80 << sh
<= max
-min
; sh
++);
2805 for (bit
=30, i
=0; i
< 16; i
++)
2806 if (i
== imax
) pix
[i
] = max
;
2807 else if (i
== imin
) pix
[i
] = min
;
2809 pix
[i
] = ((sget2(dp
+(bit
>> 3)) >> (bit
& 7) & 0x7f) << sh
) + min
;
2810 if (pix
[i
] > 0x7ff) pix
[i
] = 0x7ff;
2813 for (i
=0; i
< 16; i
++, col
+=2)
2814 #ifndef LIBRAW_LIBRARY_BUILD
2815 BAYER(row
,col
) = curve
[pix
[i
] << 1] >> 1;
2818 ushort val
= pix
[i
];
2819 if(!(filtering_mode
& LIBRAW_FILTERING_NORAWCURVE
))
2820 val
= curve
[val
<<1]>>1;
2824 col
-= col
& 1 ? 1:31;
2826 } else if (tiff_bps
== 12)
2827 for (dp
=data
, col
=0; col
< width
; dp
+=3, col
+=2) {
2828 BAYER(row
,col
) = ((dp
[1] << 8 | dp
[0]) & 0xfff) << 1;
2829 BAYER(row
,col
+1) = (dp
[2] << 4 | dp
[1] >> 4) << 1;
2835 #define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
2837 /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
2838 void CLASS
smal_decode_segment (unsigned seg
[2][2], int holes
)
2840 uchar hist
[3][13] = {
2841 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2842 { 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2843 { 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
2844 int low
, high
=0xff, carry
=0, nbits
=8;
2845 int s
, count
, bin
, next
, i
, sym
[3];
2846 uchar diff
, pred
[]={0,0};
2847 ushort data
=0, range
=0;
2848 unsigned pix
, row
, col
;
2850 fseek (ifp
, seg
[0][1]+1, SEEK_SET
);
2852 for (pix
=seg
[0][0]; pix
< seg
[1][0]; pix
++) {
2853 for (s
=0; s
< 3; s
++) {
2854 data
= data
<< nbits
| getbits(nbits
);
2856 carry
= (nbits
+= carry
+1) < 1 ? nbits
-1 : 0;
2857 while (--nbits
>= 0)
2858 if ((data
>> nbits
& 0xff) == 0xff) break;
2860 data
= ((data
& ((1 << (nbits
-1)) - 1)) << 1) |
2861 ((data
+ (((data
& (1 << (nbits
-1)))) << 1)) & (-1 << nbits
));
2866 count
= ((((data
-range
+1) & 0xffff) << 2) - 1) / (high
>> 4);
2867 for (bin
=0; hist
[s
][bin
+5] > count
; bin
++);
2868 low
= hist
[s
][bin
+5] * (high
>> 4) >> 2;
2869 if (bin
) high
= hist
[s
][bin
+4] * (high
>> 4) >> 2;
2871 for (nbits
=0; high
<< nbits
< 128; nbits
++);
2872 range
= (range
+low
) << nbits
;
2875 if (++hist
[s
][2] > hist
[s
][3]) {
2876 next
= (next
+1) & hist
[s
][0];
2877 hist
[s
][3] = (hist
[s
][next
+4] - hist
[s
][next
+5]) >> 2;
2880 if (hist
[s
][hist
[s
][1]+4] - hist
[s
][hist
[s
][1]+5] > 1) {
2881 if (bin
< hist
[s
][1])
2882 for (i
=bin
; i
< hist
[s
][1]; i
++) hist
[s
][i
+5]--;
2883 else if (next
<= bin
)
2884 for (i
=hist
[s
][1]; i
< bin
; i
++) hist
[s
][i
+5]++;
2889 diff
= sym
[2] << 5 | sym
[1] << 2 | (sym
[0] & 3);
2891 diff
= diff
? -diff
: 0x80;
2892 if (ftell(ifp
) + 12 >= seg
[1][1])
2894 pred
[pix
& 1] += diff
;
2895 row
= pix
/ raw_width
- top_margin
;
2896 col
= pix
% raw_width
- left_margin
;
2897 if (row
< height
&& col
< width
)
2898 BAYER(row
,col
) = pred
[pix
& 1];
2899 #ifdef LIBRAW_LIBRARY_BUILD
2902 ushort
*dfp
= get_masked_pointer(row
+top_margin
,col
+left_margin
);
2903 if(dfp
) *dfp
= pred
[pix
&1];
2906 if (!(pix
& 1) && HOLE(row
)) pix
+= 2;
2911 void CLASS
smal_v6_load_raw()
2915 fseek (ifp
, 16, SEEK_SET
);
2918 seg
[1][0] = raw_width
* raw_height
;
2919 seg
[1][1] = INT_MAX
;
2920 smal_decode_segment (seg
, 0);
2924 int CLASS
median4 (int *p
)
2926 int min
, max
, sum
, i
;
2928 min
= max
= sum
= p
[0];
2929 for (i
=1; i
< 4; i
++) {
2931 if (min
> p
[i
]) min
= p
[i
];
2932 if (max
< p
[i
]) max
= p
[i
];
2934 return (sum
- min
- max
) >> 1;
2937 void CLASS
fill_holes (int holes
)
2939 int row
, col
, val
[4];
2941 for (row
=2; row
< height
-2; row
++) {
2942 if (!HOLE(row
)) continue;
2943 for (col
=1; col
< width
-1; col
+=4) {
2944 val
[0] = BAYER(row
-1,col
-1);
2945 val
[1] = BAYER(row
-1,col
+1);
2946 val
[2] = BAYER(row
+1,col
-1);
2947 val
[3] = BAYER(row
+1,col
+1);
2948 BAYER(row
,col
) = median4(val
);
2950 for (col
=2; col
< width
-2; col
+=4)
2951 if (HOLE(row
-2) || HOLE(row
+2))
2952 BAYER(row
,col
) = (BAYER(row
,col
-2) + BAYER(row
,col
+2)) >> 1;
2954 val
[0] = BAYER(row
,col
-2);
2955 val
[1] = BAYER(row
,col
+2);
2956 val
[2] = BAYER(row
-2,col
);
2957 val
[3] = BAYER(row
+2,col
);
2958 BAYER(row
,col
) = median4(val
);
2963 void CLASS
smal_v9_load_raw()
2965 unsigned seg
[256][2], offset
, nseg
, holes
, i
;
2967 fseek (ifp
, 67, SEEK_SET
);
2970 fseek (ifp
, offset
, SEEK_SET
);
2971 for (i
=0; i
< nseg
*2; i
++)
2972 seg
[0][i
] = get4() + data_offset
*(i
& 1);
2973 fseek (ifp
, 78, SEEK_SET
);
2975 fseek (ifp
, 88, SEEK_SET
);
2976 seg
[nseg
][0] = raw_height
* raw_width
;
2977 seg
[nseg
][1] = get4() + data_offset
;
2978 for (i
=0; i
< nseg
; i
++)
2979 smal_decode_segment (seg
+i
, holes
);
2980 if (holes
) fill_holes (holes
);
2982 #line 4147 "dcraw/dcraw.c"
2984 void CLASS
pseudoinverse (double (*in
)[3], double (*out
)[3], int size
)
2986 double work
[3][6], num
;
2989 for (i
=0; i
< 3; i
++) {
2990 for (j
=0; j
< 6; j
++)
2991 work
[i
][j
] = j
== i
+3;
2992 for (j
=0; j
< 3; j
++)
2993 for (k
=0; k
< size
; k
++)
2994 work
[i
][j
] += in
[k
][i
] * in
[k
][j
];
2996 for (i
=0; i
< 3; i
++) {
2998 for (j
=0; j
< 6; j
++)
3000 for (k
=0; k
< 3; k
++) {
3003 for (j
=0; j
< 6; j
++)
3004 work
[k
][j
] -= work
[i
][j
] * num
;
3007 for (i
=0; i
< size
; i
++)
3008 for (j
=0; j
< 3; j
++)
3009 for (out
[i
][j
]=k
=0; k
< 3; k
++)
3010 out
[i
][j
] += work
[j
][k
+3] * in
[i
][k
];
3013 void CLASS
cam_xyz_coeff (double cam_xyz
[4][3])
3015 double cam_rgb
[4][3], inverse
[4][3], num
;
3018 for (i
=0; i
< colors
; i
++) /* Multiply out XYZ colorspace */
3019 for (j
=0; j
< 3; j
++)
3020 for (cam_rgb
[i
][j
] = k
=0; k
< 3; k
++)
3021 cam_rgb
[i
][j
] += cam_xyz
[i
][k
] * xyz_rgb
[k
][j
];
3023 for (i
=0; i
< colors
; i
++) { /* Normalize cam_rgb so that */
3024 for (num
=j
=0; j
< 3; j
++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
3025 num
+= cam_rgb
[i
][j
];
3026 for (j
=0; j
< 3; j
++)
3027 cam_rgb
[i
][j
] /= num
;
3028 pre_mul
[i
] = 1 / num
;
3030 pseudoinverse (cam_rgb
, inverse
, colors
);
3031 for (raw_color
= i
=0; i
< 3; i
++)
3032 for (j
=0; j
< colors
; j
++)
3033 rgb_cam
[i
][j
] = inverse
[j
][i
];
3034 #ifdef LIBRAW_LIBRARY_BUILD
3035 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
3036 color_flags
.rgb_cam_state
= LIBRAW_COLORSTATE_CONST
;
3041 void CLASS
colorcheck()
3044 // Coordinates of the GretagMacbeth ColorChecker squares
3045 // width, height, 1st_column, 1st_row
3046 static const int cut
[NSQ
][4] = {
3047 { 241, 231, 234, 274 },
3048 { 251, 235, 534, 274 },
3049 { 255, 239, 838, 272 },
3050 { 255, 240, 1146, 274 },
3051 { 251, 237, 1452, 278 },
3052 { 243, 238, 1758, 288 },
3053 { 253, 253, 218, 558 },
3054 { 255, 249, 524, 562 },
3055 { 261, 253, 830, 562 },
3056 { 260, 255, 1144, 564 },
3057 { 261, 255, 1450, 566 },
3058 { 247, 247, 1764, 576 },
3059 { 255, 251, 212, 862 },
3060 { 259, 259, 518, 862 },
3061 { 263, 261, 826, 864 },
3062 { 265, 263, 1138, 866 },
3063 { 265, 257, 1450, 872 },
3064 { 257, 255, 1762, 874 },
3065 { 257, 253, 212, 1164 },
3066 { 262, 251, 516, 1172 },
3067 { 263, 257, 826, 1172 },
3068 { 263, 255, 1136, 1176 },
3069 { 255, 252, 1452, 1182 },
3070 { 257, 253, 1760, 1180 } };
3071 // ColorChecker Chart under 6500-kelvin illumination
3072 static const double gmb_xyY
[NSQ
][3] = {
3073 { 0.400, 0.350, 10.1 }, // Dark Skin
3074 { 0.377, 0.345, 35.8 }, // Light Skin
3075 { 0.247, 0.251, 19.3 }, // Blue Sky
3076 { 0.337, 0.422, 13.3 }, // Foliage
3077 { 0.265, 0.240, 24.3 }, // Blue Flower
3078 { 0.261, 0.343, 43.1 }, // Bluish Green
3079 { 0.506, 0.407, 30.1 }, // Orange
3080 { 0.211, 0.175, 12.0 }, // Purplish Blue
3081 { 0.453, 0.306, 19.8 }, // Moderate Red
3082 { 0.285, 0.202, 6.6 }, // Purple
3083 { 0.380, 0.489, 44.3 }, // Yellow Green
3084 { 0.473, 0.438, 43.1 }, // Orange Yellow
3085 { 0.187, 0.129, 6.1 }, // Blue
3086 { 0.305, 0.478, 23.4 }, // Green
3087 { 0.539, 0.313, 12.0 }, // Red
3088 { 0.448, 0.470, 59.1 }, // Yellow
3089 { 0.364, 0.233, 19.8 }, // Magenta
3090 { 0.196, 0.252, 19.8 }, // Cyan
3091 { 0.310, 0.316, 90.0 }, // White
3092 { 0.310, 0.316, 59.1 }, // Neutral 8
3093 { 0.310, 0.316, 36.2 }, // Neutral 6.5
3094 { 0.310, 0.316, 19.8 }, // Neutral 5
3095 { 0.310, 0.316, 9.0 }, // Neutral 3.5
3096 { 0.310, 0.316, 3.1 } }; // Black
3097 double gmb_cam
[NSQ
][4], gmb_xyz
[NSQ
][3];
3098 double inverse
[NSQ
][3], cam_xyz
[4][3], num
;
3099 int c
, i
, j
, k
, sq
, row
, col
, count
[4];
3101 memset (gmb_cam
, 0, sizeof gmb_cam
);
3102 for (sq
=0; sq
< NSQ
; sq
++) {
3104 for (row
=cut
[sq
][3]; row
< cut
[sq
][3]+cut
[sq
][1]; row
++)
3105 for (col
=cut
[sq
][2]; col
< cut
[sq
][2]+cut
[sq
][0]; col
++) {
3107 if (c
>= colors
) c
-= 2;
3108 gmb_cam
[sq
][c
] += BAYER(row
,col
);
3111 FORCC gmb_cam
[sq
][c
] = gmb_cam
[sq
][c
]/count
[c
] - black
;
3112 gmb_xyz
[sq
][0] = gmb_xyY
[sq
][2] * gmb_xyY
[sq
][0] / gmb_xyY
[sq
][1];
3113 gmb_xyz
[sq
][1] = gmb_xyY
[sq
][2];
3114 gmb_xyz
[sq
][2] = gmb_xyY
[sq
][2] *
3115 (1 - gmb_xyY
[sq
][0] - gmb_xyY
[sq
][1]) / gmb_xyY
[sq
][1];
3117 pseudoinverse (gmb_xyz
, inverse
, NSQ
);
3118 for (i
=0; i
< colors
; i
++)
3119 for (j
=0; j
< 3; j
++)
3120 for (cam_xyz
[i
][j
] = k
=0; k
< NSQ
; k
++)
3121 cam_xyz
[i
][j
] += gmb_cam
[k
][i
] * inverse
[k
][j
];
3122 cam_xyz_coeff (cam_xyz
);
3123 #ifdef DCRAW_VERBOSE
3125 printf (" { \"%s %s\", %d,\n\t{", make
, model
, black
);
3126 num
= 10000 / (cam_xyz
[1][0] + cam_xyz
[1][1] + cam_xyz
[1][2]);
3127 FORCC
for (j
=0; j
< 3; j
++)
3128 printf ("%c%d", (c
| j
) ? ',':' ', (int) (cam_xyz
[c
][j
] * num
+ 0.5));
3136 void CLASS
hat_transform (float *temp
, float *base
, int st
, int size
, int sc
)
3139 for (i
=0; i
< sc
; i
++)
3140 temp
[i
] = 2*base
[st
*i
] + base
[st
*(sc
-i
)] + base
[st
*(i
+sc
)];
3141 for (; i
+sc
< size
; i
++)
3142 temp
[i
] = 2*base
[st
*i
] + base
[st
*(i
-sc
)] + base
[st
*(i
+sc
)];
3143 for (; i
< size
; i
++)
3144 temp
[i
] = 2*base
[st
*i
] + base
[st
*(i
-sc
)] + base
[st
*(2*size
-2-(i
+sc
))];
3148 void CLASS
wavelet_denoise()
3150 float *fimg
=0, *temp
, thold
, mul
[2], avg
, diff
;
3151 int scale
=1, size
, lev
, hpass
, lpass
, row
, col
, nc
, c
, i
, wlast
;
3153 static const float noise
[] =
3154 { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
3156 #ifdef DCRAW_VERBOSE
3157 if (verbose
) fprintf (stderr
,_("Wavelet denoising...\n"));
3160 while (maximum
<< scale
< 0x10000) scale
++;
3161 maximum
<<= --scale
;
3163 if ((size
= iheight
*iwidth
) < 0x15550000)
3164 fimg
= (float *) malloc ((size
*3 + iheight
+ iwidth
) * sizeof *fimg
);
3165 merror (fimg
, "wavelet_denoise()");
3166 temp
= fimg
+ size
*3;
3167 if ((nc
= colors
) == 3 && filters
) nc
++;
3168 FORC(nc
) { /* denoise R,G1,B,G3 individually */
3169 for (i
=0; i
< size
; i
++)
3170 fimg
[i
] = 256 * sqrt((double)(image
[i
][c
] << scale
));
3171 for (hpass
=lev
=0; lev
< 5; lev
++) {
3172 lpass
= size
*((lev
& 1)+1);
3173 for (row
=0; row
< iheight
; row
++) {
3174 hat_transform (temp
, fimg
+hpass
+row
*iwidth
, 1, iwidth
, 1 << lev
);
3175 for (col
=0; col
< iwidth
; col
++)
3176 fimg
[lpass
+ row
*iwidth
+ col
] = temp
[col
] * 0.25;
3178 for (col
=0; col
< iwidth
; col
++) {
3179 hat_transform (temp
, fimg
+lpass
+col
, iwidth
, iheight
, 1 << lev
);
3180 for (row
=0; row
< iheight
; row
++)
3181 fimg
[lpass
+ row
*iwidth
+ col
] = temp
[row
] * 0.25;
3183 thold
= threshold
* noise
[lev
];
3184 for (i
=0; i
< size
; i
++) {
3185 fimg
[hpass
+i
] -= fimg
[lpass
+i
];
3186 if (fimg
[hpass
+i
] < -thold
) fimg
[hpass
+i
] += thold
;
3187 else if (fimg
[hpass
+i
] > thold
) fimg
[hpass
+i
] -= thold
;
3188 else fimg
[hpass
+i
] = 0;
3189 if (hpass
) fimg
[i
] += fimg
[hpass
+i
];
3193 for (i
=0; i
< size
; i
++)
3194 image
[i
][c
] = CLIP(SQR(fimg
[i
]+fimg
[lpass
+i
])/0x10000);
3196 if (filters
&& colors
== 3) { /* pull G1 and G3 closer together */
3197 for (row
=0; row
< 2; row
++)
3198 mul
[row
] = 0.125 * pre_mul
[FC(row
+1,0) | 1] / pre_mul
[FC(row
,0) | 1];
3199 for (i
=0; i
< 4; i
++)
3200 window
[i
] = (ushort
*) fimg
+ width
*i
;
3201 for (wlast
=-1, row
=1; row
< height
-1; row
++) {
3202 while (wlast
< row
+1) {
3203 for (wlast
++, i
=0; i
< 4; i
++)
3204 window
[(i
+3) & 3] = window
[i
];
3205 for (col
= FC(wlast
,1) & 1; col
< width
; col
+=2)
3206 window
[2][col
] = BAYER(wlast
,col
);
3208 thold
= threshold
/512;
3209 for (col
= (FC(row
,0) & 1)+1; col
< width
-1; col
+=2) {
3210 avg
= ( window
[0][col
-1] + window
[0][col
+1] +
3211 window
[2][col
-1] + window
[2][col
+1] - black
*4 )
3212 * mul
[row
& 1] + (window
[1][col
] - black
) * 0.5 + black
;
3213 avg
= avg
< 0 ? 0 : sqrt(avg
);
3214 diff
= sqrt((double)(BAYER(row
,col
))) - avg
;
3215 if (diff
< -thold
) diff
+= thold
;
3216 else if (diff
> thold
) diff
-= thold
;
3218 BAYER(row
,col
) = CLIP(SQR(avg
+diff
) + 0.5);
3225 void CLASS
wavelet_denoise()
3227 float *fimg
=0, *temp
, thold
, mul
[2], avg
, diff
;
3228 int scale
=1, size
, lev
, hpass
, lpass
, row
, col
, nc
, c
, i
, wlast
;
3230 static const float noise
[] =
3231 { 0.8002,0.2735,0.1202,0.0585,0.0291,0.0152,0.0080,0.0044 };
3233 #ifdef DCRAW_VERBOSE
3234 if (verbose
) fprintf (stderr
,_("Wavelet denoising...\n"));
3237 while (maximum
<< scale
< 0x10000) scale
++;
3238 maximum
<<= --scale
;
3240 if ((size
= iheight
*iwidth
) < 0x15550000)
3241 fimg
= (float *) malloc ((size
*3 + iheight
+ iwidth
) * sizeof *fimg
);
3242 merror (fimg
, "wavelet_denoise()");
3243 temp
= fimg
+ size
*3;
3244 if ((nc
= colors
) == 3 && filters
) nc
++;
3245 #ifdef LIBRAW_LIBRARY_BUILD
3246 #pragma omp parallel default(shared) private(i,col,row,thold,lev,lpass,hpass,temp) firstprivate(c,scale,size) */
3249 temp
= (float*)malloc( (iheight
+ iwidth
) * sizeof *fimg
);
3250 FORC(nc
) { /* denoise R,G1,B,G3 individually */
3251 #ifdef LIBRAW_LIBRARY_BUILD
3254 for (i
=0; i
< size
; i
++)
3255 fimg
[i
] = 256 * sqrt((double)(image
[i
][c
] << scale
));
3256 for (hpass
=lev
=0; lev
< 5; lev
++) {
3257 lpass
= size
*((lev
& 1)+1);
3258 #ifdef LIBRAW_LIBRARY_BUILD
3261 for (row
=0; row
< iheight
; row
++) {
3262 hat_transform (temp
, fimg
+hpass
+row
*iwidth
, 1, iwidth
, 1 << lev
);
3263 for (col
=0; col
< iwidth
; col
++)
3264 fimg
[lpass
+ row
*iwidth
+ col
] = temp
[col
] * 0.25;
3266 #ifdef LIBRAW_LIBRARY_BUILD
3269 for (col
=0; col
< iwidth
; col
++) {
3270 hat_transform (temp
, fimg
+lpass
+col
, iwidth
, iheight
, 1 << lev
);
3271 for (row
=0; row
< iheight
; row
++)
3272 fimg
[lpass
+ row
*iwidth
+ col
] = temp
[row
] * 0.25;
3274 thold
= threshold
* noise
[lev
];
3275 #ifdef LIBRAW_LIBRARY_BUILD
3278 for (i
=0; i
< size
; i
++) {
3279 fimg
[hpass
+i
] -= fimg
[lpass
+i
];
3280 if (fimg
[hpass
+i
] < -thold
) fimg
[hpass
+i
] += thold
;
3281 else if (fimg
[hpass
+i
] > thold
) fimg
[hpass
+i
] -= thold
;
3282 else fimg
[hpass
+i
] = 0;
3283 if (hpass
) fimg
[i
] += fimg
[hpass
+i
];
3287 #ifdef LIBRAW_LIBRARY_BUILD
3290 for (i
=0; i
< size
; i
++)
3291 image
[i
][c
] = CLIP(SQR(fimg
[i
]+fimg
[lpass
+i
])/0x10000);
3294 } /* end omp parallel */
3295 /* the following loops are hard to parallize, no idea yes,
3296 * problem is wlast which is carrying dependency
3297 * second part should be easyer, but did not yet get it right.
3299 if (filters
&& colors
== 3) { /* pull G1 and G3 closer together */
3300 for (row
=0; row
< 2; row
++)
3301 mul
[row
] = 0.125 * pre_mul
[FC(row
+1,0) | 1] / pre_mul
[FC(row
,0) | 1];
3302 for (i
=0; i
< 4; i
++)
3303 window
[i
] = (ushort
*) fimg
+ width
*i
;
3304 for (wlast
=-1, row
=1; row
< height
-1; row
++) {
3305 while (wlast
< row
+1) {
3306 for (wlast
++, i
=0; i
< 4; i
++)
3307 window
[(i
+3) & 3] = window
[i
];
3308 for (col
= FC(wlast
,1) & 1; col
< width
; col
+=2)
3309 window
[2][col
] = BAYER(wlast
,col
);
3311 thold
= threshold
/512;
3312 for (col
= (FC(row
,0) & 1)+1; col
< width
-1; col
+=2) {
3313 avg
= ( window
[0][col
-1] + window
[0][col
+1] +
3314 window
[2][col
-1] + window
[2][col
+1] - black
*4 )
3315 * mul
[row
& 1] + (window
[1][col
] - black
) * 0.5 + black
;
3316 avg
= avg
< 0 ? 0 : sqrt(avg
);
3317 diff
= sqrt(BAYER(row
,col
)) - avg
;
3318 if (diff
< -thold
) diff
+= thold
;
3319 else if (diff
> thold
) diff
-= thold
;
3321 BAYER(row
,col
) = CLIP(SQR(avg
+diff
) + 0.5);
3330 void CLASS
scale_colors()
3332 unsigned bottom
, right
, size
, row
, col
, ur
, uc
, i
, x
, y
, c
, sum
[8];
3334 double dsum
[8], dmin
, dmax
;
3335 float scale_mul
[4], fr
, fc
;
3336 ushort
*img
=0, *pix
;
3338 #ifdef LIBRAW_LIBRARY_BUILD
3339 RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS
,0,2);
3343 memcpy (pre_mul
, user_mul
, sizeof pre_mul
);
3344 if (use_auto_wb
|| (use_camera_wb
&& cam_mul
[0] == -1)) {
3345 memset (dsum
, 0, sizeof dsum
);
3346 bottom
= MIN (greybox
[1]+greybox
[3], height
);
3347 right
= MIN (greybox
[0]+greybox
[2], width
);
3348 for (row
=greybox
[1]; row
< bottom
; row
+= 8)
3349 for (col
=greybox
[0]; col
< right
; col
+= 8) {
3350 memset (sum
, 0, sizeof sum
);
3351 for (y
=row
; y
< row
+8 && y
< bottom
; y
++)
3352 for (x
=col
; x
< col
+8 && x
< right
; x
++)
3358 val
= image
[y
*width
+x
][c
];
3359 if (val
> maximum
-25) goto skip_block
;
3360 if ((val
-= black
) < 0) val
= 0;
3365 FORC(8) dsum
[c
] += sum
[c
];
3368 FORC4
if (dsum
[c
]) pre_mul
[c
] = dsum
[c
+4] / dsum
[c
];
3369 #ifdef LIBRAW_LIBRARY_BUILD
3370 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CALCULATED
;
3373 if (use_camera_wb
&& cam_mul
[0] != -1) {
3374 memset (sum
, 0, sizeof sum
);
3375 for (row
=0; row
< 8; row
++)
3376 for (col
=0; col
< 8; col
++) {
3378 if ((val
= white
[row
][col
] - black
) > 0)
3382 if (sum
[0] && sum
[1] && sum
[2] && sum
[3])
3384 FORC4 pre_mul
[c
] = (float) sum
[c
+4] / sum
[c
];
3385 #ifdef LIBRAW_LIBRARY_BUILD
3386 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CALCULATED
;
3389 else if (cam_mul
[0] && cam_mul
[2])
3391 memcpy (pre_mul
, cam_mul
, sizeof pre_mul
);
3392 #ifdef LIBRAW_LIBRARY_BUILD
3393 color_flags
.pre_mul_state
=color_flags
.pre_mul_state
;
3398 #ifdef LIBRAW_LIBRARY_BUILD
3399 imgdata
.process_warnings
|= LIBRAW_WARN_BAD_CAMERA_WB
;
3401 #ifdef DCRAW_VERBOSE
3402 fprintf (stderr
,_("%s: Cannot use camera white balance.\n"), ifname
);
3406 if (pre_mul
[3] == 0) pre_mul
[3] = colors
< 4 ? pre_mul
[1] : 1;
3409 if (threshold
) wavelet_denoise();
3411 for (dmin
=DBL_MAX
, dmax
=c
=0; c
< 4; c
++) {
3412 if (dmin
> pre_mul
[c
])
3414 if (dmax
< pre_mul
[c
])
3417 if (!highlight
) dmax
= dmin
;
3418 FORC4 scale_mul
[c
] = (pre_mul
[c
] /= dmax
) * 65535.0 / maximum
;
3419 #ifdef DCRAW_VERBOSE
3422 _("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark
, sat
);
3423 FORC4
fprintf (stderr
, " %f", pre_mul
[c
]);
3424 fputc ('\n', stderr
);
3427 size
= iheight
*iwidth
;
3428 for (i
=0; i
< size
*4; i
++) {
3432 val
*= scale_mul
[i
& 3];
3433 image
[0][i
] = CLIP(val
);
3435 if ((aber
[0] != 1 || aber
[2] != 1) && colors
== 3) {
3436 #ifdef DCRAW_VERBOSE
3438 fprintf (stderr
,_("Correcting chromatic aberration...\n"));
3440 for (c
=0; c
< 4; c
+=2) {
3441 if (aber
[c
] == 1) continue;
3442 img
= (ushort
*) malloc (size
* sizeof *img
);
3443 merror (img
, "scale_colors()");
3444 for (i
=0; i
< size
; i
++)
3445 img
[i
] = image
[i
][c
];
3446 for (row
=0; row
< iheight
; row
++) {
3447 ur
= fr
= (row
- iheight
*0.5) * aber
[c
] + iheight
*0.5;
3448 if (ur
> iheight
-2) continue;
3450 for (col
=0; col
< iwidth
; col
++) {
3451 uc
= fc
= (col
- iwidth
*0.5) * aber
[c
] + iwidth
*0.5;
3452 if (uc
> iwidth
-2) continue;
3454 pix
= img
+ ur
*iwidth
+ uc
;
3455 image
[row
*iwidth
+col
][c
] =
3456 (pix
[ 0]*(1-fc
) + pix
[ 1]*fc
) * (1-fr
) +
3457 (pix
[iwidth
]*(1-fc
) + pix
[iwidth
+1]*fc
) * fr
;
3463 #ifdef LIBRAW_LIBRARY_BUILD
3464 RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS
,1,2);
3468 void CLASS
pre_interpolate()
3473 #ifdef LIBRAW_LIBRARY_BUILD
3474 RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE
,0,2);
3481 img
= (ushort (*)[4]) calloc (height
*width
, sizeof *img
);
3482 merror (img
, "pre_interpolate()");
3483 for (row
=0; row
< height
; row
++)
3484 for (col
=0; col
< width
; col
++) {
3486 img
[row
*width
+col
][c
] = image
[(row
>> 1)*iwidth
+(col
>> 1)][c
];
3493 if (filters
&& colors
== 3) {
3494 if ((mix_green
= four_color_rgb
)) colors
++;
3496 for (row
= FC(1,0) >> 1; row
< height
; row
+=2)
3497 for (col
= FC(row
,1) & 1; col
< width
; col
+=2)
3498 image
[row
*width
+col
][1] = image
[row
*width
+col
][3];
3499 filters
&= ~((filters
& 0x55555555) << 1);
3502 if (half_size
) filters
= 0;
3503 #ifdef LIBRAW_LIBRARY_BUILD
3504 RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE
,1,2);
3508 void CLASS
border_interpolate (int border
)
3510 unsigned row
, col
, y
, x
, f
, c
, sum
[8];
3512 for (row
=0; row
< height
; row
++)
3513 for (col
=0; col
< width
; col
++) {
3514 if (col
==border
&& row
>= border
&& row
< height
-border
)
3516 memset (sum
, 0, sizeof sum
);
3517 for (y
=row
-1; y
!= row
+2; y
++)
3518 for (x
=col
-1; x
!= col
+2; x
++)
3519 if (y
< height
&& x
< width
) {
3521 sum
[f
] += image
[y
*width
+x
][f
];
3525 FORCC
if (c
!= f
&& sum
[c
+4])
3526 image
[row
*width
+col
][c
] = sum
[c
] / sum
[c
+4];
3530 void CLASS
lin_interpolate()
3532 int code
[16][16][32], *ip
, sum
[4];
3533 int c
, i
, x
, y
, row
, col
, shift
, color
;
3536 #ifdef DCRAW_VERBOSE
3537 if (verbose
) fprintf (stderr
,_("Bilinear interpolation...\n"));
3540 #ifdef LIBRAW_LIBRARY_BUILD
3541 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE
,0,3);
3543 border_interpolate(1);
3544 for (row
=0; row
< 16; row
++)
3545 for (col
=0; col
< 16; col
++) {
3546 ip
= code
[row
][col
];
3547 memset (sum
, 0, sizeof sum
);
3548 for (y
=-1; y
<= 1; y
++)
3549 for (x
=-1; x
<= 1; x
++) {
3550 shift
= (y
==0) + (x
==0);
3551 if (shift
== 2) continue;
3552 color
= fc(row
+y
,col
+x
);
3553 *ip
++ = (width
*y
+ x
)*4 + color
;
3556 sum
[color
] += 1 << shift
;
3559 if (c
!= fc(row
,col
)) {
3561 *ip
++ = 256 / sum
[c
];
3564 #ifdef LIBRAW_LIBRARY_BUILD
3565 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE
,1,3);
3567 for (row
=1; row
< height
-1; row
++)
3568 for (col
=1; col
< width
-1; col
++) {
3569 pix
= image
[row
*width
+col
];
3570 ip
= code
[row
& 15][col
& 15];
3571 memset (sum
, 0, sizeof sum
);
3572 for (i
=8; i
--; ip
+=3)
3573 sum
[ip
[2]] += pix
[ip
[0]] << ip
[1];
3574 for (i
=colors
; --i
; ip
+=2)
3575 pix
[ip
[0]] = sum
[ip
[0]] * ip
[1] >> 8;
3577 #ifdef LIBRAW_LIBRARY_BUILD
3578 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE
,2,3);
3583 This algorithm is officially called:
3585 "Interpolation using a Threshold-based variable number of gradients"
3587 described in http://scien.stanford.edu/class/psych221/projects/99/tingchen/algodep/vargra.html
3589 I've extended the basic idea to work with non-Bayer filter arrays.
3590 Gradients are numbered clockwise from NW=0 to W=7.
3592 void CLASS
vng_interpolate()
3594 static const signed char *cp
, terms
[] = {
3595 -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
3596 -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
3597 -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
3598 -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
3599 -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
3600 -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
3601 -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
3602 -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
3603 -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
3604 -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
3605 -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
3606 -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
3607 -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
3608 +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
3609 +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
3610 +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
3611 +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
3612 +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
3613 +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
3614 +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
3615 +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
3617 }, chood
[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
3618 ushort (*brow
[5])[4], *pix
;
3619 int prow
=7, pcol
=1, *ip
, *code
[16][16], gval
[8], gmin
, gmax
, sum
[4];
3620 int row
, col
, x
, y
, x1
, x2
, y1
, y2
, t
, weight
, grads
, color
, diag
;
3621 int g
, diff
, thold
, num
, c
;
3623 #ifdef DCRAW_VERBOSE
3624 if (verbose
) fprintf (stderr
,_("VNG interpolation...\n"));
3627 if (filters
== 1) prow
= pcol
= 15;
3628 ip
= (int *) calloc ((prow
+1)*(pcol
+1), 1280);
3629 merror (ip
, "vng_interpolate()");
3630 for (row
=0; row
<= prow
; row
++) /* Precalculate for VNG */
3631 for (col
=0; col
<= pcol
; col
++) {
3632 code
[row
][col
] = ip
;
3633 for (cp
=terms
, t
=0; t
< 64; t
++) {
3634 y1
= *cp
++; x1
= *cp
++;
3635 y2
= *cp
++; x2
= *cp
++;
3638 color
= fc(row
+y1
,col
+x1
);
3639 if (fc(row
+y2
,col
+x2
) != color
) continue;
3640 diag
= (fc(row
,col
+1) == color
&& fc(row
+1,col
) == color
) ? 2:1;
3641 if (abs(y1
-y2
) == diag
&& abs(x1
-x2
) == diag
) continue;
3642 *ip
++ = (y1
*width
+ x1
)*4 + color
;
3643 *ip
++ = (y2
*width
+ x2
)*4 + color
;
3645 for (g
=0; g
< 8; g
++)
3646 if (grads
& 1<<g
) *ip
++ = g
;
3650 for (cp
=chood
, g
=0; g
< 8; g
++) {
3651 y
= *cp
++; x
= *cp
++;
3652 *ip
++ = (y
*width
+ x
) * 4;
3653 color
= fc(row
,col
);
3654 if (fc(row
+y
,col
+x
) != color
&& fc(row
+y
*2,col
+x
*2) == color
)
3655 *ip
++ = (y
*width
+ x
) * 8 + color
;
3660 brow
[4] = (ushort (*)[4]) calloc (width
*3, sizeof **brow
);
3661 merror (brow
[4], "vng_interpolate()");
3662 for (row
=0; row
< 3; row
++)
3663 brow
[row
] = brow
[4] + row
*width
;
3664 for (row
=2; row
< height
-2; row
++) { /* Do VNG interpolation */
3665 #ifdef LIBRAW_LIBRARY_BUILD
3666 if(!((row
-2)%256))RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE
,(row
-2)/256+1,((height
-3)/256)+1);
3668 for (col
=2; col
< width
-2; col
++) {
3669 pix
= image
[row
*width
+col
];
3670 ip
= code
[row
& prow
][col
& pcol
];
3671 memset (gval
, 0, sizeof gval
);
3672 while ((g
= ip
[0]) != INT_MAX
) { /* Calculate gradients */
3673 diff
= ABS(pix
[g
] - pix
[ip
[1]]) << ip
[2];
3674 gval
[ip
[3]] += diff
;
3676 if ((g
= ip
[-1]) == -1) continue;
3678 while ((g
= *ip
++) != -1)
3682 gmin
= gmax
= gval
[0]; /* Choose a threshold */
3683 for (g
=1; g
< 8; g
++) {
3684 if (gmin
> gval
[g
]) gmin
= gval
[g
];
3685 if (gmax
< gval
[g
]) gmax
= gval
[g
];
3688 memcpy (brow
[2][col
], pix
, sizeof *image
);
3691 thold
= gmin
+ (gmax
>> 1);
3692 memset (sum
, 0, sizeof sum
);
3693 color
= fc(row
,col
);
3694 for (num
=g
=0; g
< 8; g
++,ip
+=2) { /* Average the neighbors */
3695 if (gval
[g
] <= thold
) {
3697 if (c
== color
&& ip
[1])
3698 sum
[c
] += (pix
[c
] + pix
[ip
[1]]) >> 1;
3700 sum
[c
] += pix
[ip
[0] + c
];
3704 FORCC
{ /* Save to buffer */
3707 t
+= (sum
[c
] - sum
[color
]) / num
;
3708 brow
[2][col
][c
] = CLIP(t
);
3711 if (row
> 3) /* Write buffer to image */
3712 memcpy (image
[(row
-2)*width
+2], brow
[0]+2, (width
-4)*sizeof *image
);
3713 for (g
=0; g
< 4; g
++)
3714 brow
[(g
-1) & 3] = brow
[g
];
3716 memcpy (image
[(row
-2)*width
+2], brow
[0]+2, (width
-4)*sizeof *image
);
3717 memcpy (image
[(row
-1)*width
+2], brow
[1]+2, (width
-4)*sizeof *image
);
3723 Patterned Pixel Grouping Interpolation by Alain Desbiolles
3725 void CLASS
ppg_interpolate()
3727 int dir
[5] = { 1, width
, -1, -width
, 1 };
3728 int row
, col
, diff
[2], guess
[2], c
, d
, i
;
3731 border_interpolate(3);
3732 #ifdef DCRAW_VERBOSE
3733 if (verbose
) fprintf (stderr
,_("PPG interpolation...\n"));
3736 /* Fill in the green layer with gradients and pattern recognition: */
3737 #ifdef LIBRAW_LIBRARY_BUILD
3738 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE
,0,3);
3740 for (row
=3; row
< height
-3; row
++)
3741 for (col
=3+(FC(row
,3) & 1), c
=FC(row
,col
); col
< width
-3; col
+=2) {
3742 pix
= image
+ row
*width
+col
;
3743 for (i
=0; (d
=dir
[i
]) > 0; i
++) {
3744 guess
[i
] = (pix
[-d
][1] + pix
[0][c
] + pix
[d
][1]) * 2
3745 - pix
[-2*d
][c
] - pix
[2*d
][c
];
3746 diff
[i
] = ( ABS(pix
[-2*d
][c
] - pix
[ 0][c
]) +
3747 ABS(pix
[ 2*d
][c
] - pix
[ 0][c
]) +
3748 ABS(pix
[ -d
][1] - pix
[ d
][1]) ) * 3 +
3749 ( ABS(pix
[ 3*d
][1] - pix
[ d
][1]) +
3750 ABS(pix
[-3*d
][1] - pix
[-d
][1]) ) * 2;
3752 d
= dir
[i
= diff
[0] > diff
[1]];
3753 pix
[0][1] = ULIM(guess
[i
] >> 2, pix
[d
][1], pix
[-d
][1]);
3755 /* Calculate red and blue for each green pixel: */
3756 #ifdef LIBRAW_LIBRARY_BUILD
3757 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE
,1,3);
3759 for (row
=1; row
< height
-1; row
++)
3760 for (col
=1+(FC(row
,2) & 1), c
=FC(row
,col
+1); col
< width
-1; col
+=2) {
3761 pix
= image
+ row
*width
+col
;
3762 for (i
=0; (d
=dir
[i
]) > 0; c
=2-c
, i
++)
3763 pix
[0][c
] = CLIP((pix
[-d
][c
] + pix
[d
][c
] + 2*pix
[0][1]
3764 - pix
[-d
][1] - pix
[d
][1]) >> 1);
3766 /* Calculate blue for red pixels and vice versa: */
3767 #ifdef LIBRAW_LIBRARY_BUILD
3768 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE
,2,3);
3770 for (row
=1; row
< height
-1; row
++)
3771 for (col
=1+(FC(row
,1) & 1), c
=2-FC(row
,col
); col
< width
-1; col
+=2) {
3772 pix
= image
+ row
*width
+col
;
3773 for (i
=0; (d
=dir
[i
]+dir
[i
+1]) > 0; i
++) {
3774 diff
[i
] = ABS(pix
[-d
][c
] - pix
[d
][c
]) +
3775 ABS(pix
[-d
][1] - pix
[0][1]) +
3776 ABS(pix
[ d
][1] - pix
[0][1]);
3777 guess
[i
] = pix
[-d
][c
] + pix
[d
][c
] + 2*pix
[0][1]
3778 - pix
[-d
][1] - pix
[d
][1];
3780 if (diff
[0] != diff
[1])
3781 pix
[0][c
] = CLIP(guess
[diff
[0] > diff
[1]] >> 1);
3783 pix
[0][c
] = CLIP((guess
[0]+guess
[1]) >> 2);
3788 Adaptive Homogeneity-Directed interpolation is based on
3789 the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
3791 #define TS 256 /* Tile Size */
3793 void CLASS
ahd_interpolate()
3795 int i
, j
, k
, top
, left
, row
, col
, tr
, tc
, c
, d
, val
, hm
[2];
3796 ushort (*pix
)[4], (*rix
)[3];
3797 static const int dir
[4] = { -1, 1, -TS
, TS
};
3798 unsigned ldiff
[2][4], abdiff
[2][4], leps
, abeps
;
3799 float r
, cbrt
[0x10000], xyz
[3], xyz_cam
[3][4];
3800 ushort (*rgb
)[TS
][TS
][3];
3801 short (*lab
)[TS
][TS
][3], (*lix
)[3];
3802 char (*homo
)[TS
][TS
], *buffer
;
3804 #ifdef DCRAW_VERBOSE
3805 if (verbose
) fprintf (stderr
,_("AHD interpolation...\n"));
3808 for (i
=0; i
< 0x10000; i
++) {
3810 cbrt
[i
] = r
> 0.008856 ? pow((double)r
,1/3.0) : 7.787*r
+ 16/116.0;
3812 for (i
=0; i
< 3; i
++)
3813 for (j
=0; j
< colors
; j
++)
3814 for (xyz_cam
[i
][j
] = k
=0; k
< 3; k
++)
3815 xyz_cam
[i
][j
] += xyz_rgb
[i
][k
] * rgb_cam
[k
][j
] / d65_white
[i
];
3817 border_interpolate(5);
3818 buffer
= (char *) malloc (26*TS
*TS
); /* 1664 kB */
3819 merror (buffer
, "ahd_interpolate()");
3820 rgb
= (ushort(*)[TS
][TS
][3]) buffer
;
3821 lab
= (short (*)[TS
][TS
][3])(buffer
+ 12*TS
*TS
);
3822 homo
= (char (*)[TS
][TS
]) (buffer
+ 24*TS
*TS
);
3824 for (top
=2; top
< height
-5; top
+= TS
-6)
3827 #ifdef LIBRAW_LIBRARY_BUILD
3828 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE
,(top
-2)/(TS
-6),(height
-7)/(TS
-6)+1);
3830 for (left
=2; left
< width
-5; left
+= TS
-6) {
3832 /* Interpolate green horizontally and vertically: */
3833 for (row
= top
; row
< top
+TS
&& row
< height
-2; row
++) {
3834 col
= left
+ (FC(row
,left
) & 1);
3835 for (c
= FC(row
,col
); col
< left
+TS
&& col
< width
-2; col
+=2) {
3836 pix
= image
+ row
*width
+col
;
3837 val
= ((pix
[-1][1] + pix
[0][c
] + pix
[1][1]) * 2
3838 - pix
[-2][c
] - pix
[2][c
]) >> 2;
3839 rgb
[0][row
-top
][col
-left
][1] = ULIM(val
,pix
[-1][1],pix
[1][1]);
3840 val
= ((pix
[-width
][1] + pix
[0][c
] + pix
[width
][1]) * 2
3841 - pix
[-2*width
][c
] - pix
[2*width
][c
]) >> 2;
3842 rgb
[1][row
-top
][col
-left
][1] = ULIM(val
,pix
[-width
][1],pix
[width
][1]);
3845 /* Interpolate red and blue, and convert to CIELab: */
3846 for (d
=0; d
< 2; d
++)
3847 for (row
=top
+1; row
< top
+TS
-1 && row
< height
-3; row
++)
3848 for (col
=left
+1; col
< left
+TS
-1 && col
< width
-3; col
++) {
3849 pix
= image
+ row
*width
+col
;
3850 rix
= &rgb
[d
][row
-top
][col
-left
];
3851 lix
= &lab
[d
][row
-top
][col
-left
];
3852 if ((c
= 2 - FC(row
,col
)) == 1) {
3854 val
= pix
[0][1] + (( pix
[-1][2-c
] + pix
[1][2-c
]
3855 - rix
[-1][1] - rix
[1][1] ) >> 1);
3856 rix
[0][2-c
] = CLIP(val
);
3857 val
= pix
[0][1] + (( pix
[-width
][c
] + pix
[width
][c
]
3858 - rix
[-TS
][1] - rix
[TS
][1] ) >> 1);
3860 val
= rix
[0][1] + (( pix
[-width
-1][c
] + pix
[-width
+1][c
]
3861 + pix
[+width
-1][c
] + pix
[+width
+1][c
]
3862 - rix
[-TS
-1][1] - rix
[-TS
+1][1]
3863 - rix
[+TS
-1][1] - rix
[+TS
+1][1] + 1) >> 2);
3864 rix
[0][c
] = CLIP(val
);
3866 rix
[0][c
] = pix
[0][c
];
3867 xyz
[0] = xyz
[1] = xyz
[2] = 0.5;
3869 xyz
[0] += xyz_cam
[0][c
] * rix
[0][c
];
3870 xyz
[1] += xyz_cam
[1][c
] * rix
[0][c
];
3871 xyz
[2] += xyz_cam
[2][c
] * rix
[0][c
];
3873 xyz
[0] = cbrt
[CLIP((int) xyz
[0])];
3874 xyz
[1] = cbrt
[CLIP((int) xyz
[1])];
3875 xyz
[2] = cbrt
[CLIP((int) xyz
[2])];
3876 lix
[0][0] = 64 * (116 * xyz
[1] - 16);
3877 lix
[0][1] = 64 * 500 * (xyz
[0] - xyz
[1]);
3878 lix
[0][2] = 64 * 200 * (xyz
[1] - xyz
[2]);
3880 /* Build homogeneity maps from the CIELab images: */
3881 memset (homo
, 0, 2*TS
*TS
);
3882 for (row
=top
+2; row
< top
+TS
-2 && row
< height
-4; row
++) {
3884 for (col
=left
+2; col
< left
+TS
-2 && col
< width
-4; col
++) {
3886 for (d
=0; d
< 2; d
++) {
3887 lix
= &lab
[d
][tr
][tc
];
3888 for (i
=0; i
< 4; i
++) {
3889 ldiff
[d
][i
] = ABS(lix
[0][0]-lix
[dir
[i
]][0]);
3890 abdiff
[d
][i
] = SQR(lix
[0][1]-lix
[dir
[i
]][1])
3891 + SQR(lix
[0][2]-lix
[dir
[i
]][2]);
3894 leps
= MIN(MAX(ldiff
[0][0],ldiff
[0][1]),
3895 MAX(ldiff
[1][2],ldiff
[1][3]));
3896 abeps
= MIN(MAX(abdiff
[0][0],abdiff
[0][1]),
3897 MAX(abdiff
[1][2],abdiff
[1][3]));
3898 for (d
=0; d
< 2; d
++)
3899 for (i
=0; i
< 4; i
++)
3900 if (ldiff
[d
][i
] <= leps
&& abdiff
[d
][i
] <= abeps
)
3904 /* Combine the most homogenous pixels for the final result: */
3905 for (row
=top
+3; row
< top
+TS
-3 && row
< height
-5; row
++) {
3907 for (col
=left
+3; col
< left
+TS
-3 && col
< width
-5; col
++) {
3909 for (d
=0; d
< 2; d
++)
3910 for (hm
[d
]=0, i
=tr
-1; i
<= tr
+1; i
++)
3911 for (j
=tc
-1; j
<= tc
+1; j
++)
3912 hm
[d
] += homo
[d
][i
][j
];
3914 FORC3 image
[row
*width
+col
][c
] = rgb
[hm
[1] > hm
[0]][tr
][tc
][c
];
3916 FORC3 image
[row
*width
+col
][c
] =
3917 (rgb
[0][tr
][tc
][c
] + rgb
[1][tr
][tc
][c
]) >> 1;
3925 void CLASS
ahd_interpolate()
3927 int i
, j
, k
, top
, left
, row
, col
, tr
, tc
, c
, d
, val
, hm
[2];
3928 ushort (*pix
)[4], (*rix
)[3];
3929 static const int dir
[4] = { -1, 1, -TS
, TS
};
3930 unsigned ldiff
[2][4], abdiff
[2][4], leps
, abeps
;
3931 float r
, cbrt
[0x10000], xyz
[3], xyz_cam
[3][4];
3932 ushort (*rgb
)[TS
][TS
][3];
3933 short (*lab
)[TS
][TS
][3], (*lix
)[3];
3934 char (*homo
)[TS
][TS
], *buffer
;
3936 #ifdef DCRAW_VERBOSE
3937 if (verbose
) fprintf (stderr
,_("AHD interpolation...\n"));
3940 for (i
=0; i
< 0x10000; i
++) {
3942 cbrt
[i
] = r
> 0.008856 ? pow(r
,1/3.0) : 7.787*r
+ 16/116.0;
3944 for (i
=0; i
< 3; i
++)
3945 for (j
=0; j
< colors
; j
++)
3946 for (xyz_cam
[i
][j
] = k
=0; k
< 3; k
++)
3947 xyz_cam
[i
][j
] += xyz_rgb
[i
][k
] * rgb_cam
[k
][j
] / d65_white
[i
];
3949 border_interpolate(5);
3951 #ifdef LIBRAW_LIBRARY_BUILD
3952 #pragma omp parallel private(buffer,rgb,lab,homo,top,left,row,c,col,pix,val,d,rix,xyz,lix,tc,tr,ldiff,abdiff,leps,abeps,hm,i,j) firstprivate(cbrt) shared(xyz_cam)
3955 buffer
= (char *) malloc (26*TS
*TS
); /* 1664 kB */
3956 merror (buffer
, "ahd_interpolate()");
3957 rgb
= (ushort(*)[TS
][TS
][3]) buffer
;
3958 lab
= (short (*)[TS
][TS
][3])(buffer
+ 12*TS
*TS
);
3959 homo
= (char (*)[TS
][TS
]) (buffer
+ 24*TS
*TS
);
3961 #pragma omp for schedule(dynamic)
3962 for (top
=2; top
< height
-5; top
+= TS
-6){
3963 #ifdef LIBRAW_LIBRARY_BUILD
3964 RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE
,(top
-2)/(TS
-6)+1,(height
-7)/(TS
-6)+1);
3966 for (left
=2; left
< width
-5; left
+= TS
-6) {
3968 /* Interpolate green horizontally and vertically: */
3969 for (row
= top
; row
< top
+TS
&& row
< height
-2; row
++) {
3970 col
= left
+ (FC(row
,left
) & 1);
3971 for (c
= FC(row
,col
); col
< left
+TS
&& col
< width
-2; col
+=2) {
3972 pix
= image
+ row
*width
+col
;
3973 val
= ((pix
[-1][1] + pix
[0][c
] + pix
[1][1]) * 2
3974 - pix
[-2][c
] - pix
[2][c
]) >> 2;
3975 rgb
[0][row
-top
][col
-left
][1] = ULIM(val
,pix
[-1][1],pix
[1][1]);
3976 val
= ((pix
[-width
][1] + pix
[0][c
] + pix
[width
][1]) * 2
3977 - pix
[-2*width
][c
] - pix
[2*width
][c
]) >> 2;
3978 rgb
[1][row
-top
][col
-left
][1] = ULIM(val
,pix
[-width
][1],pix
[width
][1]);
3981 /* Interpolate red and blue, and convert to CIELab: */
3982 for (d
=0; d
< 2; d
++)
3983 for (row
=top
+1; row
< top
+TS
-1 && row
< height
-3; row
++)
3984 for (col
=left
+1; col
< left
+TS
-1 && col
< width
-3; col
++) {
3985 pix
= image
+ row
*width
+col
;
3986 rix
= &rgb
[d
][row
-top
][col
-left
];
3987 lix
= &lab
[d
][row
-top
][col
-left
];
3988 if ((c
= 2 - FC(row
,col
)) == 1) {
3990 val
= pix
[0][1] + (( pix
[-1][2-c
] + pix
[1][2-c
]
3991 - rix
[-1][1] - rix
[1][1] ) >> 1);
3992 rix
[0][2-c
] = CLIP(val
);
3993 val
= pix
[0][1] + (( pix
[-width
][c
] + pix
[width
][c
]
3994 - rix
[-TS
][1] - rix
[TS
][1] ) >> 1);
3996 val
= rix
[0][1] + (( pix
[-width
-1][c
] + pix
[-width
+1][c
]
3997 + pix
[+width
-1][c
] + pix
[+width
+1][c
]
3998 - rix
[-TS
-1][1] - rix
[-TS
+1][1]
3999 - rix
[+TS
-1][1] - rix
[+TS
+1][1] + 1) >> 2);
4000 rix
[0][c
] = CLIP(val
);
4002 rix
[0][c
] = pix
[0][c
];
4003 xyz
[0] = xyz
[1] = xyz
[2] = 0.5;
4005 xyz
[0] += xyz_cam
[0][c
] * rix
[0][c
];
4006 xyz
[1] += xyz_cam
[1][c
] * rix
[0][c
];
4007 xyz
[2] += xyz_cam
[2][c
] * rix
[0][c
];
4009 xyz
[0] = cbrt
[CLIP((int) xyz
[0])];
4010 xyz
[1] = cbrt
[CLIP((int) xyz
[1])];
4011 xyz
[2] = cbrt
[CLIP((int) xyz
[2])];
4012 lix
[0][0] = 64 * (116 * xyz
[1] - 16);
4013 lix
[0][1] = 64 * 500 * (xyz
[0] - xyz
[1]);
4014 lix
[0][2] = 64 * 200 * (xyz
[1] - xyz
[2]);
4016 /* Build homogeneity maps from the CIELab images: */
4017 memset (homo
, 0, 2*TS
*TS
);
4018 for (row
=top
+2; row
< top
+TS
-2 && row
< height
-4; row
++) {
4020 for (col
=left
+2; col
< left
+TS
-2 && col
< width
-4; col
++) {
4022 for (d
=0; d
< 2; d
++) {
4023 lix
= &lab
[d
][tr
][tc
];
4024 for (i
=0; i
< 4; i
++) {
4025 ldiff
[d
][i
] = ABS(lix
[0][0]-lix
[dir
[i
]][0]);
4026 abdiff
[d
][i
] = SQR(lix
[0][1]-lix
[dir
[i
]][1])
4027 + SQR(lix
[0][2]-lix
[dir
[i
]][2]);
4030 leps
= MIN(MAX(ldiff
[0][0],ldiff
[0][1]),
4031 MAX(ldiff
[1][2],ldiff
[1][3]));
4032 abeps
= MIN(MAX(abdiff
[0][0],abdiff
[0][1]),
4033 MAX(abdiff
[1][2],abdiff
[1][3]));
4034 for (d
=0; d
< 2; d
++)
4035 for (i
=0; i
< 4; i
++)
4036 if (ldiff
[d
][i
] <= leps
&& abdiff
[d
][i
] <= abeps
)
4040 /* Combine the most homogenous pixels for the final result: */
4041 for (row
=top
+3; row
< top
+TS
-3 && row
< height
-5; row
++) {
4043 for (col
=left
+3; col
< left
+TS
-3 && col
< width
-5; col
++) {
4045 for (d
=0; d
< 2; d
++)
4046 for (hm
[d
]=0, i
=tr
-1; i
<= tr
+1; i
++)
4047 for (j
=tc
-1; j
<= tc
+1; j
++)
4048 hm
[d
] += homo
[d
][i
][j
];
4050 FORC3 image
[row
*width
+col
][c
] = rgb
[hm
[1] > hm
[0]][tr
][tc
][c
];
4052 FORC3 image
[row
*width
+col
][c
] =
4053 (rgb
[0][tr
][tc
][c
] + rgb
[1][tr
][tc
][c
]) >> 1;
4065 void CLASS
median_filter()
4068 int pass
, c
, i
, j
, k
, med
[9];
4069 static const uchar opt
[] = /* Optimal 9-element median search */
4070 { 1,2, 4,5, 7,8, 0,1, 3,4, 6,7, 1,2, 4,5, 7,8,
4071 0,3, 5,8, 4,7, 3,6, 1,4, 2,5, 4,7, 4,2, 6,4, 4,2 };
4073 for (pass
=1; pass
<= med_passes
; pass
++) {
4074 #ifdef LIBRAW_LIBRARY_BUILD
4075 RUN_CALLBACK(LIBRAW_PROGRESS_MEDIAN_FILTER
,pass
-1,med_passes
);
4077 #ifdef DCRAW_VERBOSE
4079 fprintf (stderr
,_("Median filter pass %d...\n"), pass
);
4081 for (c
=0; c
< 3; c
+=2) {
4082 for (pix
= image
; pix
< image
+width
*height
; pix
++)
4083 pix
[0][3] = pix
[0][c
];
4084 for (pix
= image
+width
; pix
< image
+width
*(height
-1); pix
++) {
4085 if ((pix
-image
+1) % width
< 2) continue;
4086 for (k
=0, i
= -width
; i
<= width
; i
+= width
)
4087 for (j
= i
-1; j
<= i
+1; j
++)
4088 med
[k
++] = pix
[j
][3] - pix
[j
][1];
4089 for (i
=0; i
< sizeof opt
; i
+=2)
4090 if (med
[opt
[i
]] > med
[opt
[i
+1]])
4091 SWAP (med
[opt
[i
]] , med
[opt
[i
+1]]);
4092 pix
[0][c
] = CLIP(med
[4] + pix
[0][1]);
4098 void CLASS
blend_highlights()
4100 int clip
=INT_MAX
, row
, col
, c
, i
, j
;
4101 static const float trans
[2][4][4] =
4102 { { { 1,1,1 }, { 1.7320508,-1.7320508,0 }, { -1,-1,2 } },
4103 { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
4104 static const float itrans
[2][4][4] =
4105 { { { 1,0.8660254,-0.5 }, { 1,-0.8660254,-0.5 }, { 1,0,1 } },
4106 { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } };
4107 float cam
[2][4], lab
[2][4], sum
[2], chratio
;
4109 if ((unsigned) (colors
-3) > 1) return;
4110 #ifdef DCRAW_VERBOSE
4111 if (verbose
) fprintf (stderr
,_("Blending highlights...\n"));
4113 FORCC
if (clip
> (i
= 65535*pre_mul
[c
])) clip
= i
;
4114 #ifdef LIBRAW_LIBRARY_BUILD
4115 RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS
,0,2);
4117 for (row
=0; row
< height
; row
++)
4118 for (col
=0; col
< width
; col
++) {
4119 FORCC
if (image
[row
*width
+col
][c
] > clip
) break;
4120 if (c
== colors
) continue;
4122 cam
[0][c
] = image
[row
*width
+col
][c
];
4123 cam
[1][c
] = MIN(cam
[0][c
],clip
);
4125 for (i
=0; i
< 2; i
++) {
4126 FORCC
for (lab
[i
][c
]=j
=0; j
< colors
; j
++)
4127 lab
[i
][c
] += trans
[colors
-3][c
][j
] * cam
[i
][j
];
4128 for (sum
[i
]=0,c
=1; c
< colors
; c
++)
4129 sum
[i
] += SQR(lab
[i
][c
]);
4131 chratio
= sqrt(sum
[1]/sum
[0]);
4132 for (c
=1; c
< colors
; c
++)
4133 lab
[0][c
] *= chratio
;
4134 FORCC
for (cam
[0][c
]=j
=0; j
< colors
; j
++)
4135 cam
[0][c
] += itrans
[colors
-3][c
][j
] * lab
[0][j
];
4136 FORCC image
[row
*width
+col
][c
] = cam
[0][c
] / colors
;
4138 #ifdef LIBRAW_LIBRARY_BUILD
4139 RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS
,1,2);
4143 #define SCALE (4 >> shrink)
4144 void CLASS
recover_highlights()
4146 float *map
, sum
, wgt
, grow
;
4147 int hsat
[4], count
, spread
, change
, val
, i
;
4148 unsigned high
, wide
, mrow
, mcol
, row
, col
, kc
, c
, d
, y
, x
;
4150 static const signed char dir
[8][2] =
4151 { {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
4153 #ifdef DCRAW_VERBOSE
4154 if (verbose
) fprintf (stderr
,_("Rebuilding highlights...\n"));
4157 grow
= pow (2.0, 4.0-highlight
);
4158 FORCC hsat
[c
] = 32000 * pre_mul
[c
];
4159 for (kc
=0, c
=1; c
< colors
; c
++)
4160 if (pre_mul
[kc
] < pre_mul
[c
]) kc
= c
;
4161 high
= height
/ SCALE
;
4162 wide
= width
/ SCALE
;
4163 map
= (float *) calloc (high
*wide
, sizeof *map
);
4164 merror (map
, "recover_highlights()");
4165 FORCC
if (c
!= kc
) {
4166 #ifdef LIBRAW_LIBRARY_BUILD
4167 RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS
,c
-1,colors
-1);
4169 memset (map
, 0, high
*wide
*sizeof *map
);
4170 for (mrow
=0; mrow
< high
; mrow
++)
4171 for (mcol
=0; mcol
< wide
; mcol
++) {
4172 sum
= wgt
= count
= 0;
4173 for (row
= mrow
*SCALE
; row
< (mrow
+1)*SCALE
; row
++)
4174 for (col
= mcol
*SCALE
; col
< (mcol
+1)*SCALE
; col
++) {
4175 pixel
= image
[row
*width
+col
];
4176 if (pixel
[c
] / hsat
[c
] == 1 && pixel
[kc
] > 24000) {
4182 if (count
== SCALE
*SCALE
)
4183 map
[mrow
*wide
+mcol
] = sum
/ wgt
;
4185 for (spread
= 32/grow
; spread
--; ) {
4186 for (mrow
=0; mrow
< high
; mrow
++)
4187 for (mcol
=0; mcol
< wide
; mcol
++) {
4188 if (map
[mrow
*wide
+mcol
]) continue;
4190 for (d
=0; d
< 8; d
++) {
4191 y
= mrow
+ dir
[d
][0];
4192 x
= mcol
+ dir
[d
][1];
4193 if (y
< high
&& x
< wide
&& map
[y
*wide
+x
] > 0) {
4194 sum
+= (1 + (d
& 1)) * map
[y
*wide
+x
];
4195 count
+= 1 + (d
& 1);
4199 map
[mrow
*wide
+mcol
] = - (sum
+grow
) / (count
+grow
);
4201 for (change
=i
=0; i
< high
*wide
; i
++)
4208 for (i
=0; i
< high
*wide
; i
++)
4209 if (map
[i
] == 0) map
[i
] = 1;
4210 for (mrow
=0; mrow
< high
; mrow
++)
4211 for (mcol
=0; mcol
< wide
; mcol
++) {
4212 for (row
= mrow
*SCALE
; row
< (mrow
+1)*SCALE
; row
++)
4213 for (col
= mcol
*SCALE
; col
< (mcol
+1)*SCALE
; col
++) {
4214 pixel
= image
[row
*width
+col
];
4215 if (pixel
[c
] / hsat
[c
] > 1) {
4216 val
= pixel
[kc
] * map
[mrow
*wide
+mcol
];
4217 if (pixel
[c
] < val
) pixel
[c
] = CLIP(val
);
4226 void CLASS
tiff_get (unsigned base
,
4227 unsigned *tag
, unsigned *type
, unsigned *len
, unsigned *save
)
4232 *save
= ftell(ifp
) + 4;
4233 if (*len
* ("11124811248488"[*type
< 14 ? *type
:0]-'0') > 4)
4234 fseek (ifp
, get4()+base
, SEEK_SET
);
4237 void CLASS
parse_thumb_note (int base
, unsigned toff
, unsigned tlen
)
4239 unsigned entries
, tag
, type
, len
, save
;
4243 tiff_get (base
, &tag
, &type
, &len
, &save
);
4244 if (tag
== toff
) thumb_offset
= get4()+base
;
4245 if (tag
== tlen
) thumb_length
= get4();
4246 fseek (ifp
, save
, SEEK_SET
);
4250 #line 5418 "dcraw/dcraw.c"
4251 void CLASS
parse_makernote (int base
, int uptag
)
4253 static const uchar xlat
[2][256] = {
4254 { 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
4255 0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
4256 0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
4257 0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
4258 0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
4259 0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
4260 0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
4261 0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
4262 0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
4263 0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
4264 0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
4265 0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
4266 0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
4267 0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
4268 0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
4269 0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
4270 { 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
4271 0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
4272 0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
4273 0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
4274 0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
4275 0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
4276 0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
4277 0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
4278 0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
4279 0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
4280 0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
4281 0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
4282 0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
4283 0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
4284 0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
4285 0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
4286 unsigned offset
=0, entries
, tag
, type
, len
, save
, c
;
4287 unsigned ver97
=0, serial
=0, i
, wbi
=0, wb
[4]={0,0,0,0};
4288 uchar buf97
[324], ci
, cj
, ck
;
4292 The MakerNote might have its own TIFF header (possibly with
4293 its own byte-order!), or it might just be a table.
4295 fread (buf
, 1, 10, ifp
);
4296 if (!strncmp (buf
,"KDK" ,3) || /* these aren't TIFF tables */
4297 !strncmp (buf
,"VER" ,3) ||
4298 !strncmp (buf
,"IIII",4) ||
4299 !strncmp (buf
,"MMMM",4)) return;
4300 if (!strncmp (buf
,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
4301 !strncmp (buf
,"MLY" ,3)) { /* Minolta DiMAGE G series */
4303 while ((i
=ftell(ifp
)) < data_offset
&& i
< 16384) {
4304 wb
[0] = wb
[2]; wb
[2] = wb
[1]; wb
[1] = wb
[3];
4306 if (wb
[1] == 256 && wb
[3] == 256 &&
4307 wb
[0] > 256 && wb
[0] < 640 && wb
[2] > 256 && wb
[2] < 640)
4308 FORC4 cam_mul
[c
] = wb
[c
];
4309 #ifdef LIBRAW_LIBRARY_BUILD
4310 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4315 if (!strcmp (buf
,"Nikon")) {
4318 if (get2() != 42) goto quit
;
4320 fseek (ifp
, offset
-8, SEEK_CUR
);
4321 } else if (!strcmp (buf
,"OLYMPUS")) {
4322 base
= ftell(ifp
)-10;
4323 fseek (ifp
, -2, SEEK_CUR
);
4324 order
= get2(); get2();
4325 } else if (!strncmp (buf
,"FUJIFILM",8) ||
4326 !strncmp (buf
,"SONY",4) ||
4327 !strcmp (buf
,"Panasonic")) {
4329 fseek (ifp
, 2, SEEK_CUR
);
4330 } else if (!strcmp (buf
,"OLYMP") ||
4331 !strcmp (buf
,"LEICA") ||
4332 !strcmp (buf
,"Ricoh") ||
4333 !strcmp (buf
,"EPSON"))
4334 fseek (ifp
, -2, SEEK_CUR
);
4335 else if (!strcmp (buf
,"AOC") ||
4336 !strcmp (buf
,"QVC"))
4337 fseek (ifp
, -4, SEEK_CUR
);
4338 else fseek (ifp
, -10, SEEK_CUR
);
4341 if (entries
> 1000) return;
4343 tiff_get (base
, &tag
, &type
, &len
, &save
);
4345 if (tag
== 2 && strstr(make
,"NIKON"))
4346 iso_speed
= (get2(),get2());
4347 if (tag
== 4 && len
> 26 && len
< 35) {
4348 if ((i
=(get4(),get2())) != 0x7fff && !iso_speed
)
4349 iso_speed
= 50 * pow (2, i
/32.0 - 4);
4350 if ((i
=(get2(),get2())) != 0x7fff && !aperture
)
4351 aperture
= pow (2, i
/64.0);
4352 if ((i
=get2()) != 0xffff && !shutter
)
4353 shutter
= pow (2, (short) i
/-32.0);
4354 wbi
= (get2(),get2());
4355 shot_order
= (get2(),get2());
4357 if (tag
== 7 && type
== 2 && len
> 20)
4358 fgets (model2
, 64, ifp
);
4359 if (tag
== 8 && type
== 4)
4360 shot_order
= get4();
4361 if (tag
== 9 && !strcmp(make
,"Canon"))
4362 fread (artist
, 64, 1, ifp
);
4363 if (tag
== 0xc && len
== 4) {
4364 cam_mul
[0] = getreal(type
);
4365 cam_mul
[2] = getreal(type
);
4366 #ifdef LIBRAW_LIBRARY_BUILD
4367 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4370 if (tag
== 0x10 && type
== 4)
4372 if (tag
== 0x11 && is_raw
&& !strncmp(make
,"NIKON",5)) {
4373 fseek (ifp
, get4()+base
, SEEK_SET
);
4374 parse_tiff_ifd (base
);
4376 if (tag
== 0x14 && len
== 2560 && type
== 7) {
4377 fseek (ifp
, 1248, SEEK_CUR
);
4380 if (tag
== 0x15 && type
== 2 && is_raw
)
4381 fread (model
, 64, 1, ifp
);
4382 if (strstr(make
,"PENTAX")) {
4383 if (tag
== 0x1b) tag
= 0x1018;
4384 if (tag
== 0x1c) tag
= 0x1017;
4387 while ((c
= fgetc(ifp
)) && c
!= EOF
)
4388 serial
= serial
*10 + (isdigit(c
) ? c
- '0' : c
% 10);
4389 if (tag
== 0x81 && type
== 4) {
4390 data_offset
= get4();
4391 fseek (ifp
, data_offset
+ 41, SEEK_SET
);
4392 raw_height
= get2() * 2;
4394 filters
= 0x61616161;
4396 if (tag
== 0x29 && type
== 1) {
4397 c
= wbi
< 18 ? "012347800000005896"[wbi
]-'0' : 0;
4398 fseek (ifp
, 8 + c
*32, SEEK_CUR
);
4399 FORC4 cam_mul
[c
^ (c
>> 1) ^ 1] = get4();
4400 #ifdef LIBRAW_LIBRARY_BUILD
4401 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4404 if ((tag
== 0x81 && type
== 7) ||
4405 (tag
== 0x100 && type
== 7) ||
4406 (tag
== 0x280 && type
== 1)) {
4407 thumb_offset
= ftell(ifp
);
4410 if (tag
== 0x88 && type
== 4 && (thumb_offset
= get4()))
4411 thumb_offset
+= base
;
4412 if (tag
== 0x89 && type
== 4)
4413 thumb_length
= get4();
4414 if (tag
== 0x8c || tag
== 0x96)
4415 meta_offset
= ftell(ifp
);
4417 for (i
=0; i
< 4; i
++)
4418 ver97
= ver97
* 10 + fgetc(ifp
)-'0';
4421 fseek (ifp
, 68, SEEK_CUR
);
4422 FORC4 cam_mul
[(c
>> 1) | ((c
& 1) << 1)] = get2();
4423 #ifdef LIBRAW_LIBRARY_BUILD
4424 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4428 fseek (ifp
, 6, SEEK_CUR
);
4431 fseek (ifp
, 16, SEEK_CUR
);
4432 FORC4 cam_mul
[c
] = get2();
4433 #ifdef LIBRAW_LIBRARY_BUILD
4434 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4438 if (ver97
!= 205) fseek (ifp
, 280, SEEK_CUR
);
4439 fread (buf97
, 324, 1, ifp
);
4442 if (tag
== 0xa4 && type
== 3) {
4443 fseek (ifp
, wbi
*48, SEEK_CUR
);
4444 FORC3 cam_mul
[c
] = get2();
4445 #ifdef LIBRAW_LIBRARY_BUILD
4446 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4449 if (tag
== 0xa7 && (unsigned) (ver97
-200) < 12 && !cam_mul
[0]) {
4450 ci
= xlat
[0][serial
& 0xff];
4451 cj
= xlat
[1][fgetc(ifp
)^fgetc(ifp
)^fgetc(ifp
)^fgetc(ifp
)];
4453 for (i
=0; i
< 324; i
++)
4454 buf97
[i
] ^= (cj
+= ci
* ck
++);
4455 i
= "66666>666;6A"[ver97
-200] - '0';
4456 FORC4 cam_mul
[c
^ (c
>> 1) ^ (i
& 1)] =
4457 sget2 (buf97
+ (i
& -2) + c
*2);
4458 #ifdef LIBRAW_LIBRARY_BUILD
4459 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4462 if (tag
== 0x200 && len
== 3)
4463 shot_order
= (get4(),get4());
4464 if (tag
== 0x200 && len
== 4)
4465 black
= (get2()+get2()+get2()+get2())/4;
4466 if (tag
== 0x201 && len
== 4)
4468 if (tag
== 0x220 && len
== 53) {
4469 fseek (ifp
, 14, SEEK_CUR
);
4472 if (tag
== 0x401 && len
== 4) {
4473 black
= (get4()+get4()+get4()+get4())/4;
4475 if (tag
== 0xe01) { /* Nikon Capture Note */
4478 fseek (ifp
, 22, SEEK_CUR
);
4479 for (offset
=22; offset
+22 < len
; offset
+= 22+i
) {
4481 fseek (ifp
, 14, SEEK_CUR
);
4483 if (tag
== 0x76a43207) flip
= get2();
4484 else fseek (ifp
, i
, SEEK_CUR
);
4488 if (tag
== 0xe80 && len
== 256 && type
== 7) {
4489 fseek (ifp
, 48, SEEK_CUR
);
4490 cam_mul
[0] = get2() * 508 * 1.078 / 0x10000;
4491 cam_mul
[2] = get2() * 382 * 1.173 / 0x10000;
4492 #ifdef LIBRAW_LIBRARY_BUILD
4493 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4496 if (tag
== 0xf00 && type
== 7) {
4498 fseek (ifp
, 176, SEEK_CUR
);
4499 else if (len
== 734 || len
== 1502)
4500 fseek (ifp
, 148, SEEK_CUR
);
4504 if ((tag
== 0x1011 && len
== 9) || tag
== 0x20400200)
4506 for (i
=0; i
< 3; i
++)
4507 FORC3 cmatrix
[i
][c
] = ((short) get2()) / 256.0;
4508 #ifdef LIBRAW_LIBRARY_BUILD
4509 color_flags
.cmatrix_state
= LIBRAW_COLORSTATE_LOADED
;
4512 if ((tag
== 0x1012 || tag
== 0x20400600) && len
== 4)
4513 for (black
= i
=0; i
< 4; i
++)
4514 black
+= get2() << 2;
4515 if (tag
== 0x1017 || tag
== 0x20400100)
4517 cam_mul
[0] = get2() / 256.0;
4518 #ifdef LIBRAW_LIBRARY_BUILD
4519 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4522 if (tag
== 0x1018 || tag
== 0x20400100)
4524 cam_mul
[2] = get2() / 256.0;
4525 #ifdef LIBRAW_LIBRARY_BUILD
4526 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4529 if (tag
== 0x2011 && len
== 2) {
4532 cam_mul
[0] = get2() / 256.0;
4533 cam_mul
[2] = get2() / 256.0;
4534 #ifdef LIBRAW_LIBRARY_BUILD
4535 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4538 if ((tag
| 0x70) == 0x2070 && type
== 4)
4539 fseek (ifp
, get4()+base
, SEEK_SET
);
4540 if (tag
== 0x2010 && type
!= 7)
4541 load_raw
= &CLASS olympus_e410_load_raw
;
4543 parse_thumb_note (base
, 257, 258);
4545 parse_makernote (base
, 0x2040);
4546 if (tag
== 0xb028) {
4547 fseek (ifp
, get4(), SEEK_SET
);
4548 parse_thumb_note (base
, 136, 137);
4550 if (tag
== 0x4001 && len
> 500) {
4551 i
= len
== 582 ? 50 : len
== 653 ? 68 : len
== 5120 ? 142 : 126;
4552 fseek (ifp
, i
, SEEK_CUR
);
4554 FORC4 cam_mul
[c
^ (c
>> 1)] = get2();
4555 #ifdef LIBRAW_LIBRARY_BUILD
4556 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4558 fseek (ifp
, 22, SEEK_CUR
);
4559 FORC4 sraw_mul
[c
^ (c
>> 1)] = get2();
4562 fseek (ifp
, save
, SEEK_SET
);
4569 Since the TIFF DateTime string has no timezone information,
4570 assume that the camera's clock was set to Universal Time.
4572 void CLASS
get_timestamp (int reversed
)
4580 for (i
=19; i
--; ) str
[i
] = fgetc(ifp
);
4582 fread (str
, 19, 1, ifp
);
4583 memset (&t
, 0, sizeof t
);
4584 if (sscanf (str
, "%d:%d:%d %d:%d:%d", &t
.tm_year
, &t
.tm_mon
,
4585 &t
.tm_mday
, &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
) != 6)
4590 timestamp
= mktime(&t
);
4593 void CLASS
parse_exif (int base
)
4595 unsigned kodak
, entries
, tag
, type
, len
, save
, c
;
4598 kodak
= !strncmp(make
,"EASTMAN",7);
4601 tiff_get (base
, &tag
, &type
, &len
, &save
);
4603 case 33434: shutter
= getreal(type
); break;
4604 case 33437: aperture
= getreal(type
); break;
4605 case 34855: iso_speed
= get2(); break;
4607 case 36868: get_timestamp(0); break;
4608 case 37377: if ((expo
= -getreal(type
)) < 128)
4609 shutter
= pow (2, expo
); break;
4610 case 37378: aperture
= pow (2, getreal(type
)/2); break;
4611 case 37386: focal_len
= getreal(type
); break;
4612 case 37500: parse_makernote (base
, 0); break;
4613 case 40962: if (kodak
) raw_width
= get4(); break;
4614 case 40963: if (kodak
) raw_height
= get4(); break;
4616 if (get4() == 0x20002)
4617 for (exif_cfa
=c
=0; c
< 8; c
+=2)
4618 exif_cfa
|= fgetc(ifp
) * 0x01010101 << c
;
4620 fseek (ifp
, save
, SEEK_SET
);
4624 void CLASS
parse_gps (int base
)
4626 unsigned entries
, tag
, type
, len
, save
, c
;
4630 tiff_get (base
, &tag
, &type
, &len
, &save
);
4632 case 1: case 3: case 5:
4633 gpsdata
[29+tag
/2] = getc(ifp
); break;
4634 case 2: case 4: case 7:
4635 FORC(6) gpsdata
[tag
/3*6+c
] = get4(); break;
4637 FORC(2) gpsdata
[18+c
] = get4(); break;
4639 fgets ((char *) (gpsdata
+14+tag
/3), MIN(len
,12), ifp
);
4641 fseek (ifp
, save
, SEEK_SET
);
4645 void CLASS
romm_coeff (float romm_cam
[3][3])
4647 static const float rgb_romm
[3][3] = /* ROMM == Kodak ProPhoto */
4648 { { 2.034193, -0.727420, -0.306766 },
4649 { -0.228811, 1.231729, -0.002922 },
4650 { -0.008565, -0.153273, 1.161839 } };
4653 for (i
=0; i
< 3; i
++)
4654 for (j
=0; j
< 3; j
++)
4655 for (cmatrix
[i
][j
] = k
=0; k
< 3; k
++)
4656 cmatrix
[i
][j
] += rgb_romm
[i
][k
] * romm_cam
[k
][j
];
4657 #ifdef LIBRAW_LIBRARY_BUILD
4658 color_flags
.cmatrix_state
= LIBRAW_COLORSTATE_CALCULATED
;
4662 void CLASS
parse_mos (int offset
)
4665 int skip
, from
, i
, c
, neut
[4], planes
=0, frot
=0;
4666 static const char *mod
[] =
4667 { "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
4668 "Valeo 11p","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65",
4669 "Aptus 54S","Aptus 65S","Aptus 75S","AFi 5","AFi 6","AFi 7" };
4670 float romm_cam
[3][3];
4672 fseek (ifp
, offset
, SEEK_SET
);
4674 if (get4() != 0x504b5453) break;
4676 fread (data
, 1, 40, ifp
);
4679 if (!strcmp(data
,"JPEG_preview_data")) {
4680 thumb_offset
= from
;
4681 thumb_length
= skip
;
4683 if (!strcmp(data
,"icc_camera_profile")) {
4684 profile_offset
= from
;
4685 profile_length
= skip
;
4687 if (!strcmp(data
,"ShootObj_back_type")) {
4688 fscanf (ifp
, "%d", &i
);
4689 if ((unsigned) i
< sizeof mod
/ sizeof (*mod
))
4690 strcpy (model
, mod
[i
]);
4692 if (!strcmp(data
,"icc_camera_to_tone_matrix")) {
4693 for (i
=0; i
< 9; i
++)
4694 romm_cam
[0][i
] = int_to_float(get4());
4695 romm_coeff (romm_cam
);
4697 if (!strcmp(data
,"CaptProf_color_matrix")) {
4698 for (i
=0; i
< 9; i
++)
4699 fscanf (ifp
, "%f", &romm_cam
[0][i
]);
4700 romm_coeff (romm_cam
);
4702 if (!strcmp(data
,"CaptProf_number_of_planes"))
4703 fscanf (ifp
, "%d", &planes
);
4704 if (!strcmp(data
,"CaptProf_raw_data_rotation"))
4705 fscanf (ifp
, "%d", &flip
);
4706 if (!strcmp(data
,"CaptProf_mosaic_pattern"))
4708 fscanf (ifp
, "%d", &i
);
4709 if (i
== 1) frot
= c
^ (c
>> 1);
4711 if (!strcmp(data
,"ImgProf_rotation_angle")) {
4712 fscanf (ifp
, "%d", &i
);
4715 if (!strcmp(data
,"NeutObj_neutrals") && !cam_mul
[0]) {
4716 FORC4
fscanf (ifp
, "%d", neut
+c
);
4717 FORC3 cam_mul
[c
] = (float) neut
[0] / neut
[c
+1];
4718 #ifdef LIBRAW_LIBRARY_BUILD
4719 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4723 fseek (ifp
, skip
+from
, SEEK_SET
);
4726 filters
= (planes
== 1) * 0x01010101 *
4727 (uchar
) "\x94\x61\x16\x49"[(flip
/90 + frot
) & 3];
4730 void CLASS
linear_table (unsigned len
)
4733 if (len
> 0x1000) len
= 0x1000;
4734 read_shorts (curve
, len
);
4735 #ifdef LIBRAW_LIBRARY_BUILD
4736 color_flags
.curve_state
= LIBRAW_COLORSTATE_LOADED
;
4738 for (i
=len
; i
< 0x1000; i
++)
4739 curve
[i
] = curve
[i
-1];
4740 maximum
= curve
[0xfff];
4743 void CLASS
parse_kodak_ifd (int base
)
4745 unsigned entries
, tag
, type
, len
, save
;
4746 int i
, c
, wbi
=-2, wbtemp
=6500;
4750 if (entries
> 1024) return;
4752 tiff_get (base
, &tag
, &type
, &len
, &save
);
4753 if (tag
== 1020) wbi
= getint(type
);
4754 if (tag
== 1021 && len
== 72) { /* WB set in software */
4755 fseek (ifp
, 40, SEEK_CUR
);
4756 FORC3 cam_mul
[c
] = 2048.0 / get2();
4757 #ifdef LIBRAW_LIBRARY_BUILD
4758 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4762 if (tag
== 2118) wbtemp
= getint(type
);
4763 if (tag
== 2130 + wbi
)
4764 FORC3 mul
[c
] = getreal(type
);
4765 if (tag
== 2140 + wbi
&& wbi
>= 0)
4768 for (num
=i
=0; i
< 4; i
++)
4769 num
+= getreal(type
) * pow (wbtemp
/100.0, i
);
4770 cam_mul
[c
] = 2048 / (num
* mul
[c
]);
4772 #ifdef LIBRAW_LIBRARY_BUILD
4773 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4776 if (tag
== 2317) linear_table (len
);
4777 if (tag
== 6020) iso_speed
= getint(type
);
4778 fseek (ifp
, save
, SEEK_SET
);
4782 #line 5953 "dcraw/dcraw.c"
4783 int CLASS
parse_tiff_ifd (int base
)
4785 unsigned entries
, tag
, type
, len
, plen
=16, save
;
4786 int ifd
, use_cm
=0, cfa
, i
, j
, c
, ima_len
=0;
4787 char software
[64], *cbuf
, *cp
;
4788 uchar cfa_pat
[16], cfa_pc
[] = { 0,1,2,3 }, tab
[256];
4789 double dblack
, cc
[4][4], cm
[4][3], cam_xyz
[4][3], num
;
4790 double ab
[]={ 1,1,1,1 }, asn
[] = { 0,0,0,0 }, xyz
[] = { 1,1,1 };
4791 unsigned sony_curve
[] = { 0,0,0,0,0,4095 };
4792 unsigned *buf
, sony_offset
=0, sony_length
=0, sony_key
=0;
4796 if (tiff_nifds
>= sizeof tiff_ifd
/ sizeof tiff_ifd
[0])
4799 for (j
=0; j
< 4; j
++)
4800 for (i
=0; i
< 4; i
++)
4803 if (entries
> 512) return 1;
4805 tiff_get (base
, &tag
, &type
, &len
, &save
);
4808 if (type
== 3 && len
== 1)
4810 cam_mul
[(tag
-17)*2] = get2() / 256.0;
4811 #ifdef LIBRAW_LIBRARY_BUILD
4812 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4817 if (type
== 3) iso_speed
= get2();
4819 case 36: case 37: case 38:
4820 cam_mul
[tag
-0x24] = get2();
4823 if (len
< 50 || cam_mul
[0]) break;
4824 fseek (ifp
, 12, SEEK_CUR
);
4825 FORC3 cam_mul
[c
] = get2();
4826 #ifdef LIBRAW_LIBRARY_BUILD
4827 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4831 if (type
!= 7 || fgetc(ifp
) != 0xff || fgetc(ifp
) != 0xd8) break;
4832 thumb_offset
= ftell(ifp
) - 2;
4835 case 2: case 256: /* ImageWidth */
4836 tiff_ifd
[ifd
].t_width
= getint(type
);
4838 case 3: case 257: /* ImageHeight */
4839 tiff_ifd
[ifd
].t_height
= getint(type
);
4841 case 258: /* BitsPerSample */
4842 tiff_ifd
[ifd
].samples
= len
& 7;
4843 tiff_ifd
[ifd
].bps
= get2();
4845 case 259: /* Compression */
4846 tiff_ifd
[ifd
].comp
= get2();
4848 case 262: /* PhotometricInterpretation */
4849 tiff_ifd
[ifd
].phint
= get2();
4851 case 270: /* ImageDescription */
4852 fread (desc
, 512, 1, ifp
);
4854 case 271: /* Make */
4855 fgets (make
, 64, ifp
);
4857 case 272: /* Model */
4858 fgets (model
, 64, ifp
);
4860 case 280: /* Panasonic RW2 offset */
4861 if (type
!= 4) break;
4862 load_raw
= &CLASS panasonic_load_raw
;
4863 load_flags
= 0x2008;
4864 case 273: /* StripOffset */
4866 tiff_ifd
[ifd
].offset
= get4()+base
;
4867 if (!tiff_ifd
[ifd
].bps
) {
4868 fseek (ifp
, tiff_ifd
[ifd
].offset
, SEEK_SET
);
4869 if (ljpeg_start (&jh
, 1)) {
4870 tiff_ifd
[ifd
].comp
= 6;
4871 tiff_ifd
[ifd
].t_width
= jh
.wide
<< (jh
.clrs
== 2);
4872 tiff_ifd
[ifd
].t_height
= jh
.high
;
4873 tiff_ifd
[ifd
].bps
= jh
.bits
;
4874 tiff_ifd
[ifd
].samples
= jh
.clrs
;
4878 case 274: /* Orientation */
4879 tiff_ifd
[ifd
].t_flip
= "50132467"[get2() & 7]-'0';
4881 case 277: /* SamplesPerPixel */
4882 tiff_ifd
[ifd
].samples
= getint(type
) & 7;
4884 case 279: /* StripByteCounts */
4886 tiff_ifd
[ifd
].bytes
= get4();
4888 case 305: case 11: /* Software */
4889 fgets (software
, 64, ifp
);
4890 if (!strncmp(software
,"Adobe",5) ||
4891 !strncmp(software
,"dcraw",5) ||
4892 !strncmp(software
,"UFRaw",5) ||
4893 !strncmp(software
,"Bibble",6) ||
4894 !strncmp(software
,"Nikon Scan",10) ||
4895 !strcmp (software
,"Digital Photo Professional"))
4898 case 306: /* DateTime */
4901 case 315: /* Artist */
4902 fread (artist
, 64, 1, ifp
);
4904 case 322: /* TileWidth */
4905 tile_width
= getint(type
);
4907 case 323: /* TileLength */
4908 tile_length
= getint(type
);
4910 case 324: /* TileOffsets */
4911 tiff_ifd
[ifd
].offset
= len
> 1 ? ftell(ifp
) : get4();
4913 load_raw
= &CLASS sinar_4shot_load_raw
;
4917 case 330: /* SubIFDs */
4918 if (!strcmp(model
,"DSLR-A100") && tiff_ifd
[ifd
].t_width
== 3872) {
4919 load_raw
= &CLASS sony_arw_load_raw
;
4920 data_offset
= get4()+base
;
4925 fseek (ifp
, get4()+base
, SEEK_SET
);
4926 if (parse_tiff_ifd (base
)) break;
4927 fseek (ifp
, i
+4, SEEK_SET
);
4931 strcpy (make
, "Sarnoff");
4935 FORC4 sony_curve
[c
+1] = get2() >> 2 & 0xfff;
4936 for (i
=0; i
< 5; i
++)
4937 for (j
= sony_curve
[i
]+1; j
<= sony_curve
[i
+1]; j
++)
4938 curve
[j
] = curve
[j
-1] + (1 << i
);
4939 #ifdef LIBRAW_LIBRARY_BUILD
4940 color_flags
.curve_state
= LIBRAW_COLORSTATE_LOADED
;
4943 case 29184: sony_offset
= get4(); break;
4944 case 29185: sony_length
= get4(); break;
4945 case 29217: sony_key
= get4(); break;
4947 parse_minolta (ftell(ifp
));
4951 FORC4 cam_mul
[c
^ (c
< 2)] = get2();
4952 #ifdef LIBRAW_LIBRARY_BUILD
4953 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4957 FORC4 cam_mul
[c
^ (c
>> 1)] = get2();
4958 #ifdef LIBRAW_LIBRARY_BUILD
4959 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4962 case 33405: /* Model2 */
4963 fgets (model2
, 64, ifp
);
4965 case 33422: /* CFAPattern */
4966 case 64777: /* Kodak P-series */
4967 if ((plen
=len
) > 16) plen
= 16;
4968 fread (cfa_pat
, 1, plen
, ifp
);
4969 for (colors
=cfa
=i
=0; i
< plen
; i
++) {
4970 colors
+= !(cfa
& (1 << cfa_pat
[i
]));
4971 cfa
|= 1 << cfa_pat
[i
];
4973 if (cfa
== 070) memcpy (cfa_pc
,"\003\004\005",3); /* CMY */
4974 if (cfa
== 072) memcpy (cfa_pc
,"\005\003\004\001",4); /* GMCY */
4977 fseek (ifp
, get4()+base
, SEEK_SET
);
4978 parse_kodak_ifd (base
);
4980 case 33434: /* ExposureTime */
4981 shutter
= getreal(type
);
4983 case 33437: /* FNumber */
4984 aperture
= getreal(type
);
4986 case 34306: /* Leaf white balance */
4987 FORC4 cam_mul
[c
^ 1] = 4096.0 / get2();
4988 #ifdef LIBRAW_LIBRARY_BUILD
4989 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
4992 case 34307: /* Leaf CatchLight color matrix */
4993 fread (software
, 1, 7, ifp
);
4994 if (strncmp(software
,"MATRIX",6)) break;
4996 for (raw_color
= i
=0; i
< 3; i
++) {
4997 FORC4
fscanf (ifp
, "%f", &rgb_cam
[i
][c
^1]);
4998 if (!use_camera_wb
) continue;
5000 FORC4 num
+= rgb_cam
[i
][c
];
5001 FORC4 rgb_cam
[i
][c
] /= num
;
5002 #ifdef LIBRAW_LIBRARY_BUILD
5003 color_flags
.rgb_cam_state
= LIBRAW_COLORSTATE_LOADED
;
5007 case 34310: /* Leaf metadata */
5008 parse_mos (ftell(ifp
));
5010 strcpy (make
, "Leaf");
5012 case 34665: /* EXIF tag */
5013 fseek (ifp
, get4()+base
, SEEK_SET
);
5016 case 34853: /* GPSInfo tag */
5017 fseek (ifp
, get4()+base
, SEEK_SET
);
5020 case 34675: /* InterColorProfile */
5021 case 50831: /* AsShotICCProfile */
5022 profile_offset
= ftell(ifp
);
5023 profile_length
= len
;
5025 case 37122: /* CompressedBitsPerPixel */
5026 kodak_cbpp
= get4();
5028 case 37386: /* FocalLength */
5029 focal_len
= getreal(type
);
5031 case 37393: /* ImageNumber */
5032 shot_order
= getint(type
);
5034 case 37400: /* old Kodak KDC tag */
5035 for (raw_color
= i
=0; i
< 3; i
++) {
5037 FORC3 rgb_cam
[i
][c
] = getreal(type
);
5039 #ifdef LIBRAW_LIBRARY_BUILD
5040 color_flags
.rgb_cam_state
= LIBRAW_COLORSTATE_LOADED
;
5043 case 46275: /* Imacon tags */
5044 strcpy (make
, "Imacon");
5045 data_offset
= ftell(ifp
);
5049 if (!ima_len
) break;
5050 fseek (ifp
, 78, SEEK_CUR
);
5052 raw_height
= get4();
5053 left_margin
= get4() & 7;
5054 width
= raw_width
- left_margin
- (get4() & 7);
5055 top_margin
= get4() & 7;
5056 height
= raw_height
- top_margin
- (get4() & 7);
5057 if (raw_width
== 7262) {
5062 fseek (ifp
, 52, SEEK_CUR
);
5063 FORC3 cam_mul
[c
] = getreal(11);
5064 #ifdef LIBRAW_LIBRARY_BUILD
5065 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5067 fseek (ifp
, 114, SEEK_CUR
);
5068 flip
= (get2() >> 7) * 90;
5069 if (width
* height
* 6 == ima_len
) {
5070 if (flip
% 180 == 90) SWAP(width
,height
);
5073 sprintf (model
, "Ixpress %d-Mp", height
*width
/1000000);
5074 load_raw
= &CLASS imacon_full_load_raw
;
5076 if (left_margin
& 1) filters
= 0x61616161;
5077 load_raw
= &CLASS unpacked_load_raw
;
5081 case 50454: /* Sinar tag */
5083 if (!(cbuf
= (char *) malloc(len
))) break;
5084 fread (cbuf
, 1, len
, ifp
);
5085 for (cp
= cbuf
-1; cp
&& cp
< cbuf
+len
; cp
= strchr(cp
,'\n'))
5086 if (!strncmp (++cp
,"Neutral ",8))
5088 sscanf (cp
+8, "%f %f %f", cam_mul
, cam_mul
+1, cam_mul
+2);
5089 #ifdef LIBRAW_LIBRARY_BUILD
5090 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5096 if (!make
[0]) strcpy (make
, "Hasselblad");
5098 case 50459: /* Hasselblad tag */
5103 fseek (ifp
, j
+(get2(),get4()), SEEK_SET
);
5109 case 50706: /* DNGVersion */
5110 FORC4 dng_version
= (dng_version
<< 8) + fgetc(ifp
);
5111 if (!make
[0]) strcpy (make
, "DNG");
5114 case 50710: /* CFAPlaneColor */
5115 if (len
> 4) len
= 4;
5117 fread (cfa_pc
, 1, colors
, ifp
);
5119 FORCC tab
[cfa_pc
[c
]] = c
;
5122 filters
= filters
<< 2 | tab
[cfa_pat
[i
% plen
]];
5124 case 50711: /* CFALayout */
5127 filters
= 0x49494949;
5131 case 50712: /* LinearizationTable */
5134 case 50714: /* BlackLevel */
5135 case 50715: /* BlackLevelDeltaH */
5136 case 50716: /* BlackLevelDeltaV */
5137 for (dblack
=i
=0; i
< len
; i
++)
5138 dblack
+= getreal(type
);
5139 black
+= dblack
/len
+ 0.5;
5141 case 50717: /* WhiteLevel */
5142 maximum
= getint(type
);
5144 case 50718: /* DefaultScale */
5145 pixel_aspect
= getreal(type
);
5146 pixel_aspect
/= getreal(type
);
5148 case 50721: /* ColorMatrix1 */
5149 case 50722: /* ColorMatrix2 */
5150 FORCC
for (j
=0; j
< 3; j
++)
5151 cm
[c
][j
] = getreal(type
);
5154 case 50723: /* CameraCalibration1 */
5155 case 50724: /* CameraCalibration2 */
5156 for (i
=0; i
< colors
; i
++)
5157 FORCC cc
[i
][c
] = getreal(type
);
5158 case 50727: /* AnalogBalance */
5159 FORCC ab
[c
] = getreal(type
);
5161 case 50728: /* AsShotNeutral */
5162 FORCC asn
[c
] = getreal(type
);
5164 case 50729: /* AsShotWhiteXY */
5165 xyz
[0] = getreal(type
);
5166 xyz
[1] = getreal(type
);
5167 xyz
[2] = 1 - xyz
[0] - xyz
[1];
5168 FORC3 xyz
[c
] /= d65_white
[c
];
5170 case 50740: /* DNGPrivateData */
5171 if (dng_version
) break;
5172 parse_minolta (j
= get4()+base
);
5173 fseek (ifp
, j
, SEEK_SET
);
5174 parse_tiff_ifd (base
);
5177 read_shorts (cr2_slice
, 3);
5179 case 50829: /* ActiveArea */
5180 top_margin
= getint(type
);
5181 left_margin
= getint(type
);
5182 height
= getint(type
) - top_margin
;
5183 width
= getint(type
) - left_margin
;
5185 case 64772: /* Kodak P-series */
5186 fseek (ifp
, 16, SEEK_CUR
);
5187 data_offset
= get4();
5188 fseek (ifp
, 28, SEEK_CUR
);
5189 data_offset
+= get4();
5190 load_raw
= &CLASS packed_12_load_raw
;
5192 fseek (ifp
, save
, SEEK_SET
);
5194 if (sony_length
&& (buf
= (unsigned *) malloc(sony_length
))) {
5195 fseek (ifp
, sony_offset
, SEEK_SET
);
5196 fread (buf
, sony_length
, 1, ifp
);
5197 sony_decrypt (buf
, sony_length
/4, 1, sony_key
);
5198 #ifndef LIBRAW_LIBRARY_BUILD
5200 if ((ifp
= tmpfile())) {
5201 fwrite (buf
, sony_length
, 1, ifp
);
5202 fseek (ifp
, 0, SEEK_SET
);
5203 parse_tiff_ifd (-sony_offset
);
5208 if( !ifp
->tempbuffer_open(buf
,sony_length
))
5210 parse_tiff_ifd(-sony_offset
);
5211 ifp
->tempbuffer_close();
5216 for (i
=0; i
< colors
; i
++)
5217 FORCC cc
[i
][c
] *= ab
[i
];
5219 FORCC
for (i
=0; i
< 3; i
++)
5220 for (cam_xyz
[c
][i
]=j
=0; j
< colors
; j
++)
5221 cam_xyz
[c
][i
] += cc
[c
][j
] * cm
[j
][i
] * xyz
[i
];
5222 cam_xyz_coeff (cam_xyz
);
5226 FORCC cam_mul
[c
] = 1 / asn
[c
];
5227 #ifdef LIBRAW_LIBRARY_BUILD
5228 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5233 FORCC pre_mul
[c
] /= cc
[c
][c
];
5234 #ifdef LIBRAW_LIBRARY_BUILD
5235 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5242 void CLASS
parse_tiff (int base
)
5244 int doff
, max_samp
=0, raw
=-1, thm
=-1, i
;
5247 fseek (ifp
, base
, SEEK_SET
);
5249 if (order
!= 0x4949 && order
!= 0x4d4d) return;
5251 memset (tiff_ifd
, 0, sizeof tiff_ifd
);
5253 while ((doff
= get4())) {
5254 fseek (ifp
, doff
+base
, SEEK_SET
);
5255 if (parse_tiff_ifd (base
)) break;
5259 fseek (ifp
, thumb_offset
, SEEK_SET
);
5260 if (ljpeg_start (&jh
, 1)) {
5261 thumb_misc
= jh
.bits
;
5262 thumb_width
= jh
.wide
;
5263 thumb_height
= jh
.high
;
5266 for (i
=0; i
< tiff_nifds
; i
++) {
5267 if (max_samp
< tiff_ifd
[i
].samples
)
5268 max_samp
= tiff_ifd
[i
].samples
;
5269 if (max_samp
> 3) max_samp
= 3;
5270 if ((tiff_ifd
[i
].comp
!= 6 || tiff_ifd
[i
].samples
!= 3) &&
5271 tiff_ifd
[i
].t_width
*tiff_ifd
[i
].t_height
> raw_width
*raw_height
) {
5272 raw_width
= tiff_ifd
[i
].t_width
;
5273 raw_height
= tiff_ifd
[i
].t_height
;
5274 tiff_bps
= tiff_ifd
[i
].bps
;
5275 tiff_compress
= tiff_ifd
[i
].comp
;
5276 data_offset
= tiff_ifd
[i
].offset
;
5277 tiff_flip
= tiff_ifd
[i
].t_flip
;
5278 tiff_samples
= tiff_ifd
[i
].samples
;
5282 fuji_width
*= (raw_width
+1)/2;
5283 if (tiff_ifd
[0].t_flip
) tiff_flip
= tiff_ifd
[0].t_flip
;
5284 if (raw
>= 0 && !load_raw
)
5285 switch (tiff_compress
) {
5288 case 8: load_raw
= &CLASS eight_bit_load_raw
; break;
5289 case 12: load_raw
= &CLASS packed_12_load_raw
;
5290 if (tiff_ifd
[raw
].phint
== 2)
5292 if (strncmp(make
,"PENTAX",6)) break;
5294 case 16: load_raw
= &CLASS unpacked_load_raw
; break;
5296 if (tiff_ifd
[raw
].bytes
*5 == raw_width
*raw_height
*8)
5297 load_raw
= &CLASS olympus_e300_load_raw
;
5299 case 6: case 7: case 99:
5300 load_raw
= &CLASS lossless_jpeg_load_raw
; break;
5302 load_raw
= &CLASS kodak_262_load_raw
; break;
5304 load_raw
= &CLASS sony_arw2_load_raw
;
5305 if (tiff_ifd
[raw
].bytes
*8 == raw_width
*raw_height
*tiff_bps
)
5308 load_raw
= &CLASS sony_arw_load_raw
; break;
5312 load_raw
= &CLASS packed_12_load_raw
; break;
5314 load_raw
= &CLASS nikon_compressed_load_raw
; break;
5316 load_raw
= &CLASS pentax_k10_load_raw
; break;
5318 switch (tiff_ifd
[raw
].phint
) {
5319 case 2: load_raw
= &CLASS kodak_rgb_load_raw
; filters
= 0; break;
5320 case 6: load_raw
= &CLASS kodak_ycbcr_load_raw
; filters
= 0; break;
5321 case 32803: load_raw
= &CLASS kodak_65000_load_raw
;
5324 default: is_raw
= 0;
5326 if (!dng_version
&& tiff_samples
== 3)
5327 if (tiff_ifd
[raw
].bytes
&& tiff_bps
!= 14 && tiff_bps
!= 2048)
5329 if (!dng_version
&& tiff_bps
== 8 && tiff_compress
== 1 &&
5330 tiff_ifd
[raw
].phint
== 1) is_raw
= 0;
5331 if (tiff_bps
== 8 && tiff_samples
== 4) is_raw
= 0;
5332 for (i
=0; i
< tiff_nifds
; i
++)
5333 if (i
!= raw
&& tiff_ifd
[i
].samples
== max_samp
&&
5334 tiff_ifd
[i
].t_width
* tiff_ifd
[i
].t_height
/ SQR(tiff_ifd
[i
].bps
+1) >
5335 thumb_width
* thumb_height
/ SQR(thumb_misc
+1)) {
5336 thumb_width
= tiff_ifd
[i
].t_width
;
5337 thumb_height
= tiff_ifd
[i
].t_height
;
5338 thumb_offset
= tiff_ifd
[i
].offset
;
5339 thumb_length
= tiff_ifd
[i
].bytes
;
5340 thumb_misc
= tiff_ifd
[i
].bps
;
5344 thumb_misc
|= tiff_ifd
[thm
].samples
<< 5;
5345 switch (tiff_ifd
[thm
].comp
) {
5347 write_thumb
= &CLASS layer_thumb
;
5350 if (tiff_ifd
[thm
].bps
> 8)
5351 thumb_load_raw
= &CLASS kodak_thumb_load_raw
;
5353 write_thumb
= &CLASS ppm_thumb
;
5356 thumb_load_raw
= tiff_ifd
[thm
].phint
== 6 ?
5357 &CLASS kodak_ycbcr_load_raw
: &CLASS kodak_rgb_load_raw
;
5362 void CLASS
parse_minolta (int base
)
5364 int save
, tag
, len
, offset
, high
=0, wide
=0, i
, c
;
5367 fseek (ifp
, base
, SEEK_SET
);
5368 if (fgetc(ifp
) || fgetc(ifp
)-'M' || fgetc(ifp
)-'R') return;
5369 order
= fgetc(ifp
) * 0x101;
5370 offset
= base
+ get4() + 8;
5371 while ((save
=ftell(ifp
)) < offset
) {
5372 for (tag
=i
=0; i
< 4; i
++)
5373 tag
= tag
<< 8 | fgetc(ifp
);
5376 case 0x505244: /* PRD */
5377 fseek (ifp
, 8, SEEK_CUR
);
5381 case 0x574247: /* WBG */
5383 i
= strcmp(model
,"DiMAGE A200") ? 0:3;
5384 FORC4 cam_mul
[c
^ (c
>> 1) ^ i
] = get2();
5385 #ifdef LIBRAW_LIBRARY_BUILD
5386 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5389 case 0x545457: /* TTW */
5390 parse_tiff (ftell(ifp
));
5391 data_offset
= offset
;
5393 fseek (ifp
, save
+len
+8, SEEK_SET
);
5401 Many cameras have a "debug mode" that writes JPEG and raw
5402 at the same time. The raw file has no header, so try to
5403 to open the matching JPEG file and read its metadata.
5405 void CLASS
parse_external_jpeg()
5407 char *file
, *ext
, *jname
, *jfile
, *jext
;
5408 #ifndef LIBRAW_LIBRARY_BUILD
5413 imgdata
.process_warnings
|= LIBRAW_WARN_NO_METADATA
;
5418 ext
= strrchr (ifname
, '.');
5419 file
= strrchr (ifname
, '/');
5420 if (!file
) file
= strrchr (ifname
, '\\');
5421 #ifndef LIBRAW_LIBRARY_BUILD
5422 if (!file
) file
= ifname
-1;
5424 if (!file
) file
= (char*)ifname
-1;
5427 if (!ext
|| strlen(ext
) != 4 || ext
-file
!= 8) return;
5428 jname
= (char *) malloc (strlen(ifname
) + 1);
5429 merror (jname
, "parse_external_jpeg()");
5430 strcpy (jname
, ifname
);
5431 jfile
= file
- ifname
+ jname
;
5432 jext
= ext
- ifname
+ jname
;
5433 if (strcasecmp (ext
, ".jpg")) {
5434 strcpy (jext
, isupper(ext
[1]) ? ".JPG":".jpg");
5435 if (isdigit(*file
)) {
5436 memcpy (jfile
, file
+4, 4);
5437 memcpy (jfile
+4, file
, 4);
5440 while (isdigit(*--jext
)) {
5447 #ifndef LIBRAW_LIBRARY_BUILD
5448 if (strcmp (jname
, ifname
)) {
5449 if ((ifp
= fopen (jname
, "rb"))) {
5450 #ifdef DCRAW_VERBOSE
5452 fprintf (stderr
,_("Reading metadata from %s ...\n"), jname
);
5461 if (strcmp (jname
, ifname
))
5463 if(!ifp
->subfile_open(jname
))
5468 ifp
->subfile_close();
5471 imgdata
.process_warnings
|= LIBRAW_WARN_NO_METADATA
;
5476 #ifdef LIBRAW_LIBRARY_BUILD
5477 imgdata
.process_warnings
|= LIBRAW_WARN_NO_METADATA
;
5479 #ifdef DCRAW_VERBOSE
5480 fprintf (stderr
,_("Failed to read metadata from %s\n"), jname
);
5484 #ifndef LIBRAW_LIBRARY_BUILD
5490 CIFF block 0x1030 contains an 8x8 white sample.
5491 Load this into white[][] for use in scale_colors().
5493 void CLASS
ciff_block_1030()
5495 static const ushort key
[] = { 0x410, 0x45f3 };
5496 int i
, bpp
, row
, col
, vbits
=0;
5497 unsigned long bitbuf
=0;
5499 if ((get2(),get4()) != 0x80008 || !get4()) return;
5501 if (bpp
!= 10 && bpp
!= 12) return;
5502 for (i
=row
=0; row
< 8; row
++)
5503 for (col
=0; col
< 8; col
++) {
5505 bitbuf
= bitbuf
<< 16 | (get2() ^ key
[i
++ & 1]);
5509 bitbuf
<< (LONG_BIT
- vbits
) >> (LONG_BIT
- bpp
);
5515 Parse a CIFF file, better known as Canon CRW format.
5517 void CLASS
parse_ciff (int offset
, int length
)
5519 int tboff
, nrecs
, c
, type
, len
, save
, wbi
=-1;
5520 ushort key
[] = { 0x410, 0x45f3 };
5522 fseek (ifp
, offset
+length
-4, SEEK_SET
);
5523 tboff
= get4() + offset
;
5524 fseek (ifp
, tboff
, SEEK_SET
);
5526 if (nrecs
> 100) return;
5530 save
= ftell(ifp
) + 4;
5531 fseek (ifp
, offset
+get4(), SEEK_SET
);
5532 if ((((type
>> 8) + 8) | 8) == 0x38)
5533 parse_ciff (ftell(ifp
), len
); /* Parse a sub-table */
5536 fread (artist
, 64, 1, ifp
);
5537 if (type
== 0x080a) {
5538 fread (make
, 64, 1, ifp
);
5539 fseek (ifp
, strlen(make
) - 63, SEEK_CUR
);
5540 fread (model
, 64, 1, ifp
);
5542 if (type
== 0x1810) {
5543 fseek (ifp
, 12, SEEK_CUR
);
5546 if (type
== 0x1835) /* Get the decoder table */
5547 tiff_compress
= get4();
5548 if (type
== 0x2007) {
5549 thumb_offset
= ftell(ifp
);
5552 if (type
== 0x1818) {
5553 shutter
= pow (2.0f
, -int_to_float((get4(),get4())));
5554 aperture
= pow (2.0f
, int_to_float(get4())/2);
5556 if (type
== 0x102a) {
5557 iso_speed
= pow (2, (get4(),get2())/32.0 - 4) * 50;
5558 aperture
= pow (2, (get2(),(short)get2())/64.0);
5559 shutter
= pow (2,-((short)get2())/32.0);
5560 wbi
= (get2(),get2());
5561 if (wbi
> 17) wbi
= 0;
5562 fseek (ifp
, 32, SEEK_CUR
);
5563 if (shutter
> 1e6
) shutter
= get2()/10.0;
5565 if (type
== 0x102c) {
5566 if (get2() > 512) { /* Pro90, G1 */
5567 fseek (ifp
, 118, SEEK_CUR
);
5568 FORC4 cam_mul
[c
^ 2] = get2();
5569 #ifdef LIBRAW_LIBRARY_BUILD
5570 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5572 } else { /* G2, S30, S40 */
5573 fseek (ifp
, 98, SEEK_CUR
);
5574 FORC4 cam_mul
[c
^ (c
>> 1) ^ 1] = get2();
5575 #ifdef LIBRAW_LIBRARY_BUILD
5576 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5580 if (type
== 0x0032) {
5581 if (len
== 768) { /* EOS D30 */
5582 fseek (ifp
, 72, SEEK_CUR
);
5583 FORC4 cam_mul
[c
^ (c
>> 1)] = 1024.0 / get2();
5584 #ifdef LIBRAW_LIBRARY_BUILD
5585 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5587 if (!wbi
) cam_mul
[0] = -1; /* use my auto white balance */
5588 } else if (!cam_mul
[0]) {
5589 if (get2() == key
[0]) /* Pro1, G6, S60, S70 */
5590 c
= (strstr(model
,"Pro1") ?
5591 "012346000000000000":"01345:000000006008")[wbi
]-'0'+ 2;
5592 else { /* G3, G5, S45, S50 */
5593 c
= "023457000000006000"[wbi
]-'0';
5594 key
[0] = key
[1] = 0;
5596 fseek (ifp
, 78 + c
*8, SEEK_CUR
);
5597 FORC4 cam_mul
[c
^ (c
>> 1) ^ 1] = get2() ^ key
[c
& 1];
5598 #ifdef LIBRAW_LIBRARY_BUILD
5599 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5601 if (!wbi
) cam_mul
[0] = -1;
5604 if (type
== 0x10a9) { /* D60, 10D, 300D, and clones */
5605 if (len
> 66) wbi
= "0134567028"[wbi
]-'0';
5606 fseek (ifp
, 2 + wbi
*8, SEEK_CUR
);
5607 FORC4 cam_mul
[c
^ (c
>> 1)] = get2();
5608 #ifdef LIBRAW_LIBRARY_BUILD
5609 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5612 if (type
== 0x1030 && (0x18040 >> wbi
& 1))
5613 ciff_block_1030(); /* all that don't have 0x10a9 */
5614 if (type
== 0x1031) {
5615 raw_width
= (get2(),get2());
5616 raw_height
= get2();
5618 if (type
== 0x5029) {
5619 focal_len
= len
>> 16;
5620 if ((len
& 0xffff) == 2) focal_len
/= 32;
5622 if (type
== 0x5813) flash_used
= int_to_float(len
);
5623 if (type
== 0x5814) canon_ev
= int_to_float(len
);
5624 if (type
== 0x5817) shot_order
= len
;
5625 if (type
== 0x5834) unique_id
= len
;
5626 if (type
== 0x580e) timestamp
= len
;
5627 if (type
== 0x180e) timestamp
= get4();
5629 if ((type
| 0x4000) == 0x580e)
5630 timestamp
= mktime (gmtime (×tamp
));
5632 fseek (ifp
, save
, SEEK_SET
);
5636 void CLASS
parse_rollei()
5638 char line
[128], *val
;
5641 fseek (ifp
, 0, SEEK_SET
);
5642 memset (&t
, 0, sizeof t
);
5644 fgets (line
, 128, ifp
);
5645 if ((val
= strchr(line
,'=')))
5648 val
= line
+ strlen(line
);
5649 if (!strcmp(line
,"DAT"))
5650 sscanf (val
, "%d.%d.%d", &t
.tm_mday
, &t
.tm_mon
, &t
.tm_year
);
5651 if (!strcmp(line
,"TIM"))
5652 sscanf (val
, "%d:%d:%d", &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
);
5653 if (!strcmp(line
,"HDR"))
5654 thumb_offset
= atoi(val
);
5655 if (!strcmp(line
,"X "))
5656 raw_width
= atoi(val
);
5657 if (!strcmp(line
,"Y "))
5658 raw_height
= atoi(val
);
5659 if (!strcmp(line
,"TX "))
5660 thumb_width
= atoi(val
);
5661 if (!strcmp(line
,"TY "))
5662 thumb_height
= atoi(val
);
5663 } while (strncmp(line
,"EOHD",4));
5664 data_offset
= thumb_offset
+ thumb_width
* thumb_height
* 2;
5668 timestamp
= mktime(&t
);
5669 strcpy (make
, "Rollei");
5670 strcpy (model
,"d530flex");
5671 write_thumb
= &CLASS rollei_thumb
;
5674 void CLASS
parse_sinar_ia()
5680 fseek (ifp
, 4, SEEK_SET
);
5682 fseek (ifp
, get4(), SEEK_SET
);
5684 off
= get4(); get4();
5685 fread (str
, 8, 1, ifp
);
5686 if (!strcmp(str
,"META")) meta_offset
= off
;
5687 if (!strcmp(str
,"THUMB")) thumb_offset
= off
;
5688 if (!strcmp(str
,"RAW0")) data_offset
= off
;
5690 fseek (ifp
, meta_offset
+20, SEEK_SET
);
5691 fread (make
, 64, 1, ifp
);
5693 if ((cp
= strchr(make
,' '))) {
5694 strcpy (model
, cp
+1);
5698 raw_height
= get2();
5699 load_raw
= &CLASS unpacked_load_raw
;
5700 thumb_width
= (get4(),get2());
5701 thumb_height
= get2();
5702 write_thumb
= &CLASS ppm_thumb
;
5706 void CLASS
parse_phase_one (int base
)
5708 unsigned entries
, tag
, type
, len
, data
, save
, i
, c
;
5709 float romm_cam
[3][3];
5712 memset (&ph1
, 0, sizeof ph1
);
5713 fseek (ifp
, base
, SEEK_SET
);
5714 order
= get4() & 0xffff;
5715 if (get4() >> 8 != 0x526177) return; /* "Raw" */
5716 fseek (ifp
, get4()+base
, SEEK_SET
);
5725 fseek (ifp
, base
+data
, SEEK_SET
);
5727 case 0x100: flip
= "0653"[data
& 3]-'0'; break;
5729 for (i
=0; i
< 9; i
++)
5730 romm_cam
[0][i
] = getreal(11);
5731 romm_coeff (romm_cam
);
5734 FORC3 cam_mul
[c
] = getreal(11);
5735 #ifdef LIBRAW_LIBRARY_BUILD
5736 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5739 case 0x108: raw_width
= data
; break;
5740 case 0x109: raw_height
= data
; break;
5741 case 0x10a: left_margin
= data
; break;
5742 case 0x10b: top_margin
= data
; break;
5743 case 0x10c: width
= data
; break;
5744 case 0x10d: height
= data
; break;
5745 case 0x10e: ph1
.format
= data
; break;
5746 case 0x10f: data_offset
= data
+base
; break;
5747 case 0x110: meta_offset
= data
+base
;
5748 meta_length
= len
; break;
5749 case 0x112: ph1
.key_off
= save
- 4; break;
5750 case 0x210: ph1
.tag_210
= int_to_float(data
); break;
5751 case 0x21a: ph1
.tag_21a
= data
; break;
5752 case 0x21c: strip_offset
= data
+base
; break;
5753 case 0x21d: ph1
.t_black
= data
; break;
5754 case 0x222: ph1
.split_col
= data
- left_margin
; break;
5755 case 0x223: ph1
.black_off
= data
+base
; break;
5758 fread (model
, 1, 63, ifp
);
5759 if ((cp
= strstr(model
," camera"))) *cp
= 0;
5761 fseek (ifp
, save
, SEEK_SET
);
5763 load_raw
= ph1
.format
< 3 ?
5764 &CLASS phase_one_load_raw
: &CLASS phase_one_load_raw_c
;
5766 strcpy (make
, "Phase One");
5767 if (model
[0]) return;
5768 switch (raw_height
) {
5769 case 2060: strcpy (model
,"LightPhase"); break;
5770 case 2682: strcpy (model
,"H 10"); break;
5771 case 4128: strcpy (model
,"H 20"); break;
5772 case 5488: strcpy (model
,"H 25"); break;
5776 void CLASS
parse_fuji (int offset
)
5778 unsigned entries
, tag
, len
, save
, c
;
5780 fseek (ifp
, offset
, SEEK_SET
);
5782 if (entries
> 255) return;
5788 raw_height
= get2();
5790 } else if (tag
== 0x121) {
5792 if ((width
= get2()) == 4284) width
+= 3;
5793 } else if (tag
== 0x130)
5794 fuji_layout
= fgetc(ifp
) >> 7;
5797 FORC4 cam_mul
[c
^ 1] = get2();
5798 #ifdef LIBRAW_LIBRARY_BUILD
5799 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5802 fseek (ifp
, save
+len
, SEEK_SET
);
5804 height
<<= fuji_layout
;
5805 width
>>= fuji_layout
;
5808 int CLASS
parse_jpeg (int offset
)
5810 int len
, save
, hlen
, mark
;
5812 fseek (ifp
, offset
, SEEK_SET
);
5813 if (fgetc(ifp
) != 0xff || fgetc(ifp
) != 0xd8) return 0;
5815 while (fgetc(ifp
) == 0xff && (mark
= fgetc(ifp
)) != 0xda) {
5819 if (mark
== 0xc0 || mark
== 0xc3) {
5821 raw_height
= get2();
5826 if (get4() == 0x48454150) /* "HEAP" */
5827 parse_ciff (save
+hlen
, len
-hlen
);
5828 parse_tiff (save
+6);
5829 fseek (ifp
, save
+len
, SEEK_SET
);
5834 void CLASS
parse_riff()
5836 unsigned i
, size
, end
;
5837 char tag
[4], date
[64], month
[64];
5838 static const char mon
[12][4] =
5839 { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
5843 fread (tag
, 4, 1, ifp
);
5845 end
= ftell(ifp
) + size
;
5846 if (!memcmp(tag
,"RIFF",4) || !memcmp(tag
,"LIST",4)) {
5848 while (ftell(ifp
)+7 < end
)
5850 } else if (!memcmp(tag
,"nctg",4)) {
5851 while (ftell(ifp
)+7 < end
) {
5854 if ((i
+1) >> 1 == 10 && size
== 20)
5856 else fseek (ifp
, size
, SEEK_CUR
);
5858 } else if (!memcmp(tag
,"IDIT",4) && size
< 64) {
5859 fread (date
, 64, 1, ifp
);
5861 memset (&t
, 0, sizeof t
);
5862 if (sscanf (date
, "%*s %s %d %d:%d:%d %d", month
, &t
.tm_mday
,
5863 &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
, &t
.tm_year
) == 6) {
5864 for (i
=0; i
< 12 && strcasecmp(mon
[i
],month
); i
++);
5868 timestamp
= mktime(&t
);
5871 fseek (ifp
, size
, SEEK_CUR
);
5874 void CLASS
parse_smal (int offset
, int fsize
)
5878 fseek (ifp
, offset
+2, SEEK_SET
);
5882 fseek (ifp
, 5, SEEK_CUR
);
5883 if (get4() != fsize
) return;
5884 if (ver
> 6) data_offset
= get4();
5885 raw_height
= height
= get2();
5886 raw_width
= width
= get2();
5887 strcpy (make
, "SMaL");
5888 sprintf (model
, "v%d %dx%d", ver
, width
, height
);
5889 if (ver
== 6) load_raw
= &CLASS smal_v6_load_raw
;
5890 if (ver
== 9) load_raw
= &CLASS smal_v9_load_raw
;
5893 void CLASS
parse_cine()
5895 unsigned off_head
, off_setup
, off_image
, i
;
5898 fseek (ifp
, 4, SEEK_SET
);
5899 is_raw
= get2() == 2;
5900 fseek (ifp
, 14, SEEK_CUR
);
5906 if ((i
= get4())) timestamp
= i
;
5907 fseek (ifp
, off_head
+4, SEEK_SET
);
5909 raw_height
= get4();
5910 switch (get2(),get2()) {
5911 case 8: load_raw
= &CLASS eight_bit_load_raw
; break;
5912 case 16: load_raw
= &CLASS unpacked_load_raw
;
5914 fseek (ifp
, off_setup
+792, SEEK_SET
);
5915 strcpy (make
, "CINE");
5916 sprintf (model
, "%d", get4());
5917 fseek (ifp
, 12, SEEK_CUR
);
5918 switch ((i
=get4()) & 0xffffff) {
5919 case 3: filters
= 0x94949494; break;
5920 case 4: filters
= 0x49494949; break;
5921 default: is_raw
= 0;
5923 fseek (ifp
, 72, SEEK_CUR
);
5924 switch ((get4()+3600) % 360) {
5925 case 270: flip
= 4; break;
5926 case 180: flip
= 1; break;
5927 case 90: flip
= 7; break;
5930 cam_mul
[0] = getreal(11);
5931 cam_mul
[2] = getreal(11);
5932 #ifdef LIBRAW_LIBRARY_BUILD
5933 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
5935 maximum
= ~(-1 << get4());
5936 fseek (ifp
, 668, SEEK_CUR
);
5937 shutter
= get4()/1000000000.0;
5938 fseek (ifp
, off_image
, SEEK_SET
);
5939 if (shot_select
< is_raw
)
5940 fseek (ifp
, shot_select
*8, SEEK_CUR
);
5941 data_offset
= (INT64
) get4() + 8;
5942 data_offset
+= (INT64
) get4() << 32;
5944 #line 7218 "dcraw/dcraw.c"
5945 #ifdef LIBRAW_LIBRARY_BUILD
5946 void CLASS
adobe_coeff (const char *p_make
, const char *p_model
)
5948 void CLASS
adobe_coeff (char *p_make
, char *p_model
)
5951 static const struct {
5953 short t_black
, t_maximum
, trans
[12];
5955 { "Apple QuickTake", 0, 0, /* DJC */
5956 { 17576,-3191,-3318,5210,6733,-1942,9031,1280,-124 } },
5957 { "Canon EOS D2000", 0, 0,
5958 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
5959 { "Canon EOS D6000", 0, 0,
5960 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
5961 { "Canon EOS D30", 0, 0,
5962 { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
5963 { "Canon EOS D60", 0, 0xfa0,
5964 { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
5965 { "Canon EOS 5D Mark II", 0, 0x3cf0,
5966 { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
5967 { "Canon EOS 5D", 0, 0xe6c,
5968 { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
5969 { "Canon EOS 10D", 0, 0xfa0,
5970 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
5971 { "Canon EOS 20Da", 0, 0,
5972 { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
5973 { "Canon EOS 20D", 0, 0xfff,
5974 { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
5975 { "Canon EOS 30D", 0, 0,
5976 { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
5977 { "Canon EOS 40D", 0, 0x3f60,
5978 { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
5979 { "Canon EOS 50D", 0, 0x3d93,
5980 { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
5981 { "Canon EOS 300D", 0, 0xfa0,
5982 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
5983 { "Canon EOS 350D", 0, 0xfff,
5984 { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
5985 { "Canon EOS 400D", 0, 0xe8e,
5986 { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
5987 { "Canon EOS 450D", 0, 0x390d,
5988 { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
5989 { "Canon EOS 1000D", 0, 0xe43,
5990 { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
5991 { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
5992 { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
5993 { "Canon EOS-1Ds Mark II", 0, 0xe80,
5994 { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
5995 { "Canon EOS-1D Mark II N", 0, 0xe80,
5996 { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
5997 { "Canon EOS-1D Mark III", 0, 0x3bb0,
5998 { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
5999 { "Canon EOS-1D Mark II", 0, 0xe80,
6000 { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
6001 { "Canon EOS-1DS", 0, 0xe20,
6002 { 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
6003 { "Canon EOS-1D", 0, 0xe20,
6004 { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
6005 { "Canon EOS", 0, 0,
6006 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
6007 { "Canon PowerShot A50", 0, 0,
6008 { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
6009 { "Canon PowerShot A5", 0, 0,
6010 { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
6011 { "Canon PowerShot G10", 0, 0,
6012 { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
6013 { "Canon PowerShot G1", 0, 0,
6014 { -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
6015 { "Canon PowerShot G2", 0, 0,
6016 { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
6017 { "Canon PowerShot G3", 0, 0,
6018 { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
6019 { "Canon PowerShot G5", 0, 0,
6020 { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
6021 { "Canon PowerShot G6", 0, 0,
6022 { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
6023 { "Canon PowerShot G9", 0, 0,
6024 { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
6025 { "Canon PowerShot Pro1", 0, 0,
6026 { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
6027 { "Canon PowerShot Pro70", 34, 0,
6028 { -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
6029 { "Canon PowerShot Pro90", 0, 0,
6030 { -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
6031 { "Canon PowerShot S30", 0, 0,
6032 { 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
6033 { "Canon PowerShot S40", 0, 0,
6034 { 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
6035 { "Canon PowerShot S45", 0, 0,
6036 { 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
6037 { "Canon PowerShot S50", 0, 0,
6038 { 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
6039 { "Canon PowerShot S60", 0, 0,
6040 { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
6041 { "Canon PowerShot S70", 0, 0,
6042 { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
6043 { "Canon PowerShot A610", 0, 0, /* DJC */
6044 { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
6045 { "Canon PowerShot A620", 0, 0, /* DJC */
6046 { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
6047 { "Canon PowerShot A630", 0, 0, /* DJC */
6048 { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
6049 { "Canon PowerShot A640", 0, 0, /* DJC */
6050 { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
6051 { "Canon PowerShot A650", 0, 0, /* DJC */
6052 { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
6053 { "Canon PowerShot A720", 0, 0, /* DJC */
6054 { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
6055 { "Canon PowerShot S3 IS", 0, 0, /* DJC */
6056 { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
6058 { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
6060 { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
6062 { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
6063 { "Contax N Digital", 0, 0xf1e,
6064 { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
6065 { "EPSON R-D1", 0, 0,
6066 { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
6067 { "FUJIFILM FinePix E550", 0, 0,
6068 { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
6069 { "FUJIFILM FinePix E900", 0, 0,
6070 { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
6071 { "FUJIFILM FinePix F8", 0, 0,
6072 { 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
6073 { "FUJIFILM FinePix F7", 0, 0,
6074 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
6075 { "FUJIFILM FinePix S100FS", 514, 0,
6076 { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
6077 { "FUJIFILM FinePix S20Pro", 0, 0,
6078 { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
6079 { "FUJIFILM FinePix S2Pro", 128, 0,
6080 { 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
6081 { "FUJIFILM FinePix S3Pro", 0, 0,
6082 { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
6083 { "FUJIFILM FinePix S5Pro", 0, 0,
6084 { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
6085 { "FUJIFILM FinePix S5000", 0, 0,
6086 { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
6087 { "FUJIFILM FinePix S5100", 0, 0x3e00,
6088 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
6089 { "FUJIFILM FinePix S5500", 0, 0x3e00,
6090 { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
6091 { "FUJIFILM FinePix S5200", 0, 0,
6092 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
6093 { "FUJIFILM FinePix S5600", 0, 0,
6094 { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
6095 { "FUJIFILM FinePix S6", 0, 0,
6096 { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
6097 { "FUJIFILM FinePix S7000", 0, 0,
6098 { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
6099 { "FUJIFILM FinePix S9000", 0, 0,
6100 { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
6101 { "FUJIFILM FinePix S9500", 0, 0,
6102 { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
6103 { "FUJIFILM FinePix S9100", 0, 0,
6104 { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
6105 { "FUJIFILM FinePix S9600", 0, 0,
6106 { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
6107 { "FUJIFILM IS-1", 0, 0,
6108 { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
6109 { "FUJIFILM IS Pro", 0, 0,
6110 { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
6111 { "Imacon Ixpress", 0, 0, /* DJC */
6112 { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
6113 { "KODAK NC2000", 0, 0,
6114 { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
6115 { "Kodak DCS315C", 8, 0,
6116 { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
6117 { "Kodak DCS330C", 8, 0,
6118 { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
6119 { "KODAK DCS420", 0, 0,
6120 { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
6121 { "KODAK DCS460", 0, 0,
6122 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
6123 { "KODAK EOSDCS1", 0, 0,
6124 { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
6125 { "KODAK EOSDCS3B", 0, 0,
6126 { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
6127 { "Kodak DCS520C", 180, 0,
6128 { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
6129 { "Kodak DCS560C", 188, 0,
6130 { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
6131 { "Kodak DCS620C", 180, 0,
6132 { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
6133 { "Kodak DCS620X", 185, 0,
6134 { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
6135 { "Kodak DCS660C", 214, 0,
6136 { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
6137 { "Kodak DCS720X", 0, 0,
6138 { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
6139 { "Kodak DCS760C", 0, 0,
6140 { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
6141 { "Kodak DCS Pro SLR", 0, 0,
6142 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
6143 { "Kodak DCS Pro 14nx", 0, 0,
6144 { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
6145 { "Kodak DCS Pro 14", 0, 0,
6146 { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
6147 { "Kodak ProBack645", 0, 0,
6148 { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
6149 { "Kodak ProBack", 0, 0,
6150 { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
6151 { "KODAK P712", 0, 0,
6152 { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
6153 { "KODAK P850", 0, 0xf7c,
6154 { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
6155 { "KODAK P880", 0, 0xfff,
6156 { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
6157 { "Leaf CMost", 0, 0,
6158 { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
6159 { "Leaf Valeo 6", 0, 0,
6160 { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
6161 { "Leaf Aptus 54S", 0, 0,
6162 { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
6163 { "Leaf Aptus 65", 0, 0,
6164 { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
6165 { "Leaf Aptus 75", 0, 0,
6166 { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
6168 { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
6169 { "Mamiya ZD", 0, 0,
6170 { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
6171 { "Micron 2010", 110, 0, /* DJC */
6172 { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
6173 { "Minolta DiMAGE 5", 0, 0xf7d,
6174 { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
6175 { "Minolta DiMAGE 7Hi", 0, 0xf7d,
6176 { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
6177 { "Minolta DiMAGE 7", 0, 0xf7d,
6178 { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
6179 { "Minolta DiMAGE A1", 0, 0xf8b,
6180 { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
6181 { "MINOLTA DiMAGE A200", 0, 0,
6182 { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
6183 { "Minolta DiMAGE A2", 0, 0xf8f,
6184 { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
6185 { "Minolta DiMAGE Z2", 0, 0, /* DJC */
6186 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
6187 { "MINOLTA DYNAX 5", 0, 0xffb,
6188 { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
6189 { "MINOLTA DYNAX 7", 0, 0xffb,
6190 { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
6191 { "NIKON D100", 0, 0,
6192 { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
6193 { "NIKON D1H", 0, 0,
6194 { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
6195 { "NIKON D1X", 0, 0,
6196 { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
6197 { "NIKON D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
6198 { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
6199 { "NIKON D2H", 0, 0,
6200 { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
6201 { "NIKON D2X", 0, 0,
6202 { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
6203 { "NIKON D40X", 0, 0,
6204 { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
6205 { "NIKON D40", 0, 0,
6206 { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
6207 { "NIKON D50", 0, 0,
6208 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
6209 { "NIKON D60", 0, 0,
6210 { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
6211 { "NIKON D700", 0, 0,
6212 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
6213 { "NIKON D70", 0, 0,
6214 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
6215 { "NIKON D80", 0, 0,
6216 { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
6217 { "NIKON D90", 0, 0xf00,
6218 { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
6219 { "NIKON D200", 0, 0xfbc,
6220 { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
6221 { "NIKON D300", 0, 0,
6222 { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
6224 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
6225 { "NIKON E950", 0, 0x3dd, /* DJC */
6226 { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
6227 { "NIKON E995", 0, 0, /* copied from E5000 */
6228 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6229 { "NIKON E2100", 0, 0, /* copied from Z2, new white balance */
6230 { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711} },
6231 { "NIKON E2500", 0, 0,
6232 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6233 { "NIKON E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
6234 { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
6235 { "NIKON E4500", 0, 0,
6236 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6237 { "NIKON E5000", 0, 0,
6238 { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
6239 { "NIKON E5400", 0, 0,
6240 { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
6241 { "NIKON E5700", 0, 0,
6242 { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
6243 { "NIKON E8400", 0, 0,
6244 { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
6245 { "NIKON E8700", 0, 0,
6246 { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
6247 { "NIKON E8800", 0, 0,
6248 { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
6249 { "NIKON COOLPIX P6000", 0, 0,
6250 { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
6251 { "OLYMPUS C5050", 0, 0,
6252 { 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
6253 { "OLYMPUS C5060", 0, 0,
6254 { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
6255 { "OLYMPUS C7070", 0, 0,
6256 { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
6257 { "OLYMPUS C70", 0, 0,
6258 { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
6259 { "OLYMPUS C80", 0, 0,
6260 { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
6261 { "OLYMPUS E-10", 0, 0xffc0,
6262 { 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
6263 { "OLYMPUS E-1", 0, 0xfff0,
6264 { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
6265 { "OLYMPUS E-20", 0, 0xffc0,
6266 { 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
6267 { "OLYMPUS E-300", 0, 0,
6268 { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
6269 { "OLYMPUS E-330", 0, 0,
6270 { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
6271 { "OLYMPUS E-3", 0, 0xf99,
6272 { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
6273 { "OLYMPUS E-400", 0, 0xfff0,
6274 { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
6275 { "OLYMPUS E-410", 0, 0xf6a,
6276 { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
6277 { "OLYMPUS E-420", 0, 0xfd7,
6278 { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
6279 { "OLYMPUS E-500", 0, 0,
6280 { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
6281 { "OLYMPUS E-510", 0, 0xf6a,
6282 { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
6283 { "OLYMPUS E-520", 0, 0xfd2,
6284 { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
6285 { "OLYMPUS SP350", 0, 0,
6286 { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
6287 { "OLYMPUS SP3", 0, 0,
6288 { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
6289 { "OLYMPUS SP500UZ", 0, 0xfff,
6290 { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
6291 { "OLYMPUS SP510UZ", 0, 0xffe,
6292 { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
6293 { "OLYMPUS SP550UZ", 0, 0xffe,
6294 { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
6295 { "OLYMPUS SP560UZ", 0, 0xff9,
6296 { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
6297 { "OLYMPUS SP570UZ", 0, 0,
6298 { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
6299 { "PENTAX *ist DL2", 0, 0,
6300 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
6301 { "PENTAX *ist DL", 0, 0,
6302 { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
6303 { "PENTAX *ist DS2", 0, 0,
6304 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
6305 { "PENTAX *ist DS", 0, 0,
6306 { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
6307 { "PENTAX *ist D", 0, 0,
6308 { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
6309 { "PENTAX K10D", 0, 0,
6310 { 9566,-2863,-803,-7170,15172,2112,-818,803,9705 } },
6311 { "PENTAX K1", 0, 0,
6312 { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
6313 { "PENTAX K20D", 0, 0,
6314 { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
6315 { "PENTAX K200D", 0, 0,
6316 { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
6317 { "PENTAX K2000", 0, 0,
6318 { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
6319 { "PENTAX K-m", 0, 0,
6320 { 11057,-3604,-1155,-5152,13046,2329,-282,375,8104 } },
6321 { "Panasonic DMC-FZ8", 0, 0xf7f0,
6322 { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
6323 { "Panasonic DMC-FZ18", 0, 0,
6324 { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
6325 { "Panasonic DMC-FZ28", 15, 0xfff,
6326 { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
6327 { "Panasonic DMC-FZ30", 0, 0xf94c,
6328 { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
6329 { "Panasonic DMC-FZ50", 0, 0xfff0, /* aka "LEICA V-LUX1" */
6330 { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
6331 { "Panasonic DMC-L10", 15, 0xf96,
6332 { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
6333 { "Panasonic DMC-L1", 0, 0xf7fc, /* aka "LEICA DIGILUX 3" */
6334 { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
6335 { "Panasonic DMC-LC1", 0, 0, /* aka "LEICA DIGILUX 2" */
6336 { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
6337 { "Panasonic DMC-LX1", 0, 0xf7f0, /* aka "LEICA D-LUX2" */
6338 { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
6339 { "Panasonic DMC-LX2", 0, 0, /* aka "LEICA D-LUX3" */
6340 { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
6341 { "Panasonic DMC-LX3", 15, 0xfff, /* aka "LEICA D-LUX4" */
6342 { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
6343 { "Panasonic DMC-FX150", 15, 0xfff,
6344 { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
6345 { "Panasonic DMC-G1", 15, 0xfff,
6346 { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
6347 { "Phase One H 20", 0, 0, /* DJC */
6348 { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
6349 { "Phase One P 2", 0, 0,
6350 { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
6351 { "Phase One P 30", 0, 0,
6352 { 4516,-245,-37,-7020,14976,2173,-3206,4671,7087 } },
6353 { "Phase One P 45", 0, 0,
6354 { 5053,-24,-117,-5684,14076,1702,-2619,4492,5849 } },
6355 { "SAMSUNG GX-1", 0, 0,
6356 { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
6357 { "Sinar", 0, 0, /* DJC */
6358 { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
6359 { "SONY DSC-F828", 491, 0,
6360 { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
6361 { "SONY DSC-R1", 512, 0,
6362 { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
6363 { "SONY DSC-V3", 0, 0,
6364 { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
6365 { "SONY DSLR-A100", 0, 0xfeb,
6366 { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
6367 { "SONY DSLR-A200", 0, 0,
6368 { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
6369 { "SONY DSLR-A300", 0, 0,
6370 { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
6371 { "SONY DSLR-A350", 0, 0xffc,
6372 { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
6373 { "SONY DSLR-A700", 254, 0x1ffe,
6374 { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
6375 { "SONY DSLR-A900", 254, 0x1ffe,
6376 { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } }
6378 double cam_xyz
[4][3];
6382 sprintf (name
, "%s %s", p_make
, p_model
);
6383 for (i
=0; i
< sizeof table
/ sizeof *table
; i
++)
6384 if (!strncmp (name
, table
[i
].prefix
, strlen(table
[i
].prefix
))) {
6385 if (table
[i
].t_black
) black
= (ushort
) table
[i
].t_black
;
6386 if (table
[i
].t_maximum
) maximum
= (ushort
) table
[i
].t_maximum
;
6387 for (j
=0; j
< 12; j
++)
6388 #ifdef LIBRAW_LIBRARY_BUILD
6389 imgdata
.color
.cam_xyz
[0][j
] =
6391 cam_xyz
[0][j
] = table
[i
].trans
[j
] / 10000.0;
6392 cam_xyz_coeff (cam_xyz
);
6397 void CLASS
simple_coeff (int index
)
6399 static const float table
[][12] = {
6400 /* index 0 -- all Foveon cameras */
6401 { 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
6402 /* index 1 -- Kodak DC20 and DC25 */
6403 { 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
6404 /* index 2 -- Logitech Fotoman Pixtura */
6405 { 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
6406 /* index 3 -- Nikon E880, E900, and E990 */
6407 { -1.936280, 1.800443, -1.448486, 2.584324,
6408 1.405365, -0.524955, -0.289090, 0.408680,
6409 -1.204965, 1.082304, 2.941367, -1.818705 }
6413 for (raw_color
= i
=0; i
< 3; i
++)
6414 FORCC rgb_cam
[i
][c
] = table
[index
][i
*colors
+c
];
6415 #ifdef LIBRAW_LIBRARY_BUILD
6416 color_flags
.rgb_cam_state
= LIBRAW_COLORSTATE_CALCULATED
;
6420 short CLASS
guess_byte_order (int words
)
6424 double diff
, sum
[2] = {0,0};
6426 fread (test
[0], 2, 2, ifp
);
6427 for (words
-=2; words
--; ) {
6428 fread (test
[t
], 2, 1, ifp
);
6429 for (msb
=0; msb
< 2; msb
++) {
6430 diff
= (test
[t
^2][msb
] << 8 | test
[t
^2][!msb
])
6431 - (test
[t
][msb
] << 8 | test
[t
][!msb
]);
6432 sum
[msb
] += diff
*diff
;
6436 return sum
[0] < sum
[1] ? 0x4d4d : 0x4949;
6440 Identify which camera created this file, and set global variables
6443 void CLASS
identify()
6446 unsigned hlen
, fsize
, i
, c
, is_canon
;
6448 static const struct {
6450 char t_make
[12], t_model
[19], withjpeg
;
6452 { 62464, "Kodak", "DC20" ,0 },
6453 { 124928, "Kodak", "DC20" ,0 },
6454 { 1652736, "Kodak", "DCS200" ,0 },
6455 { 4159302, "Kodak", "C330" ,0 },
6456 { 4162462, "Kodak", "C330" ,0 },
6457 { 460800, "Kodak", "C603v" ,0 },
6458 { 614400, "Kodak", "C603v" ,0 },
6459 { 6163328, "Kodak", "C603" ,0 },
6460 { 6166488, "Kodak", "C603" ,0 },
6461 { 9116448, "Kodak", "C603y" ,0 },
6462 { 311696, "ST Micro", "STV680 VGA" ,0 }, /* SPYz */
6463 { 614400, "Kodak", "KAI-0340" ,0 },
6464 { 787456, "Creative", "PC-CAM 600" ,0 },
6465 { 1138688, "Minolta", "RD175" ,0 },
6466 { 3840000, "Foculus", "531C" ,0 },
6467 { 786432, "AVT", "F-080C" ,0 },
6468 { 1447680, "AVT", "F-145C" ,0 },
6469 { 1920000, "AVT", "F-201C" ,0 },
6470 { 5067304, "AVT", "F-510C" ,0 },
6471 { 10134608, "AVT", "F-510C" ,0 },
6472 { 16157136, "AVT", "F-810C" ,0 },
6473 { 1409024, "Sony", "XCD-SX910CR" ,0 },
6474 { 2818048, "Sony", "XCD-SX910CR" ,0 },
6475 { 3884928, "Micron", "2010" ,0 },
6476 { 6624000, "Pixelink", "A782" ,0 },
6477 { 13248000, "Pixelink", "A782" ,0 },
6478 { 6291456, "RoverShot","3320AF" ,0 },
6479 { 6553440, "Canon", "PowerShot A460" ,0 },
6480 { 6653280, "Canon", "PowerShot A530" ,0 },
6481 { 6573120, "Canon", "PowerShot A610" ,0 },
6482 { 9219600, "Canon", "PowerShot A620" ,0 },
6483 { 10341600, "Canon", "PowerShot A720" ,0 },
6484 { 10383120, "Canon", "PowerShot A630" ,0 },
6485 { 12945240, "Canon", "PowerShot A640" ,0 },
6486 { 15636240, "Canon", "PowerShot A650" ,0 },
6487 { 5298000, "Canon", "PowerShot SD300" ,0 },
6488 { 7710960, "Canon", "PowerShot S3 IS" ,0 },
6489 { 5939200, "OLYMPUS", "C770UZ" ,0 },
6490 { 1581060, "NIKON", "E900" ,1 }, /* or E900s,E910 */
6491 { 2465792, "NIKON", "E950" ,1 }, /* or E800,E700 */
6492 { 2940928, "NIKON", "E2100" ,1 }, /* or E2500 */
6493 { 4771840, "NIKON", "E990" ,1 }, /* or E995, Oly C3030Z */
6494 { 4775936, "NIKON", "E3700" ,1 }, /* or Optio 33WR */
6495 { 5869568, "NIKON", "E4300" ,1 }, /* or DiMAGE Z2 */
6496 { 5865472, "NIKON", "E4500" ,1 },
6497 { 7438336, "NIKON", "E5000" ,1 }, /* or E5700 */
6498 { 8998912, "NIKON", "COOLPIX S6" ,1 },
6499 { 1976352, "CASIO", "QV-2000UX" ,1 },
6500 { 3217760, "CASIO", "QV-3*00EX" ,1 },
6501 { 6218368, "CASIO", "QV-5700" ,1 },
6502 { 6054400, "CASIO", "QV-R41" ,1 },
6503 { 7530816, "CASIO", "QV-R51" ,1 },
6504 { 7684000, "CASIO", "QV-4000" ,1 },
6505 { 4948608, "CASIO", "EX-S100" ,1 },
6506 { 7542528, "CASIO", "EX-Z50" ,1 },
6507 { 7753344, "CASIO", "EX-Z55" ,1 },
6508 { 7426656, "CASIO", "EX-P505" ,1 },
6509 { 9313536, "CASIO", "EX-P600" ,1 },
6510 { 10979200, "CASIO", "EX-P700" ,1 },
6511 { 3178560, "PENTAX", "Optio S" ,1 },
6512 { 4841984, "PENTAX", "Optio S" ,1 },
6513 { 6114240, "PENTAX", "Optio S4" ,1 }, /* or S4i, CASIO EX-Z4 */
6514 { 10702848, "PENTAX", "Optio 750Z" ,1 },
6515 { 16098048, "SAMSUNG", "S85" ,1 },
6516 { 16215552, "SAMSUNG", "S85" ,1 },
6517 { 12582980, "Sinar", "" ,0 },
6518 { 33292868, "Sinar", "" ,0 },
6519 { 44390468, "Sinar", "" ,0 } };
6520 static const char *corp
[] =
6521 { "Canon", "NIKON", "EPSON", "KODAK", "Kodak", "OLYMPUS", "PENTAX",
6522 "MINOLTA", "Minolta", "Konica", "CASIO", "Sinar", "Phase One",
6523 "SAMSUNG", "Mamiya" };
6525 #ifdef LIBRAW_LIBRARY_BUILD
6526 RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY
,0,2);
6529 tiff_flip
= flip
= filters
= -1; /* 0 is valid, so -1 is unknown */
6530 raw_height
= raw_width
= fuji_width
= fuji_layout
= cr2_slice
[0] = 0;
6531 maximum
= height
= width
= top_margin
= left_margin
= 0;
6532 cdesc
[0] = desc
[0] = artist
[0] = make
[0] = model
[0] = model2
[0] = 0;
6533 iso_speed
= shutter
= aperture
= focal_len
= unique_id
= 0;
6534 memset (gpsdata
, 0, sizeof gpsdata
);
6535 memset (white
, 0, sizeof white
);
6536 thumb_offset
= thumb_length
= thumb_width
= thumb_height
= 0;
6537 load_raw
= thumb_load_raw
= 0;
6538 write_thumb
= &CLASS jpeg_thumb
;
6539 data_offset
= meta_length
= tiff_bps
= tiff_compress
= 0;
6540 kodak_cbpp
= zero_after_ff
= dng_version
= load_flags
= 0;
6541 timestamp
= shot_order
= tiff_samples
= black
= is_foveon
= 0;
6542 mix_green
= profile_length
= data_error
= zero_is_bad
= 0;
6543 pixel_aspect
= is_raw
= raw_color
= use_gamma
= 1;
6544 tile_width
= tile_length
= INT_MAX
;
6545 for (i
=0; i
< 4; i
++) {
6546 cam_mul
[i
] = i
== 1;
6548 FORC3 cmatrix
[c
][i
] = 0;
6549 FORC3 rgb_cam
[c
][i
] = c
== i
;
6551 #ifdef LIBRAW_LIBRARY_BUILD
6552 color_flags
.cmatrix_state
= LIBRAW_COLORSTATE_INIT
;
6553 color_flags
.rgb_cam_state
= LIBRAW_COLORSTATE_INIT
;
6554 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_INIT
;
6555 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_INIT
;
6559 for (i
=0; i
< 0x4000; i
++) curve
[i
] = i
;
6560 #ifdef LIBRAW_LIBRARY_BUILD
6561 color_flags
.curve_state
= LIBRAW_COLORSTATE_INIT
;
6566 fseek (ifp
, 0, SEEK_SET
);
6567 fread (head
, 1, 32, ifp
);
6568 fseek (ifp
, 0, SEEK_END
);
6570 if ((cp
= (char *) memmem (head
, 32, "MMMM", 4)) ||
6571 (cp
= (char *) memmem (head
, 32, "IIII", 4))) {
6572 parse_phase_one (cp
-head
);
6573 if (cp
-head
) parse_tiff(0);
6574 } else if (order
== 0x4949 || order
== 0x4d4d) {
6575 if (!memcmp (head
+6,"HEAPCCDR",8)) {
6577 parse_ciff (hlen
, fsize
- hlen
);
6581 } else if (!memcmp (head
,"\xff\xd8\xff\xe1",4) &&
6582 !memcmp (head
+6,"Exif",4)) {
6583 fseek (ifp
, 4, SEEK_SET
);
6584 data_offset
= 4 + get2();
6585 fseek (ifp
, data_offset
, SEEK_SET
);
6586 if (fgetc(ifp
) != 0xff)
6589 } else if (!memcmp (head
+25,"ARECOYK",7)) {
6590 strcpy (make
, "Contax");
6591 strcpy (model
,"N Digital");
6592 fseek (ifp
, 33, SEEK_SET
);
6594 fseek (ifp
, 60, SEEK_SET
);
6595 FORC4 cam_mul
[c
^ (c
>> 1)] = get4();
6596 #ifdef LIBRAW_LIBRARY_BUILD
6597 color_flags
.cam_mul_state
= LIBRAW_COLORSTATE_LOADED
;
6599 } else if (!strcmp (head
, "PXN")) {
6600 strcpy (make
, "Logitech");
6601 strcpy (model
,"Fotoman Pixtura");
6602 } else if (!strcmp (head
, "qktk")) {
6603 strcpy (make
, "Apple");
6604 strcpy (model
,"QuickTake 100");
6605 } else if (!strcmp (head
, "qktn")) {
6606 strcpy (make
, "Apple");
6607 strcpy (model
,"QuickTake 150");
6608 } else if (!memcmp (head
,"FUJIFILM",8)) {
6609 fseek (ifp
, 84, SEEK_SET
);
6610 thumb_offset
= get4();
6611 thumb_length
= get4();
6612 fseek (ifp
, 92, SEEK_SET
);
6613 parse_fuji (get4());
6614 if (thumb_offset
> 120) {
6615 fseek (ifp
, 120, SEEK_SET
);
6616 is_raw
+= (i
= get4()) && 1;
6617 if (is_raw
== 2 && shot_select
)
6620 fseek (ifp
, 100, SEEK_SET
);
6621 data_offset
= get4();
6622 parse_tiff (thumb_offset
+12);
6623 } else if (!memcmp (head
,"RIFF",4)) {
6624 fseek (ifp
, 0, SEEK_SET
);
6626 } else if (!memcmp (head
,"\0\001\0\001\0@",6)) {
6627 fseek (ifp
, 6, SEEK_SET
);
6628 fread (make
, 1, 8, ifp
);
6629 fread (model
, 1, 8, ifp
);
6630 fread (model2
, 1, 16, ifp
);
6631 data_offset
= get2();
6634 raw_height
= get2();
6635 load_raw
= &CLASS nokia_load_raw
;
6636 filters
= 0x61616161;
6637 } else if (!memcmp (head
,"DSC-Image",9))
6639 else if (!memcmp (head
,"PWAD",4))
6641 else if (!memcmp (head
,"\0MRM",4))
6643 else if (!memcmp (head
,"FOVb",4))
6645 else if (!memcmp (head
,"CI",2))
6648 for (i
=0; i
< sizeof table
/ sizeof *table
; i
++)
6649 if (fsize
== table
[i
].fsize
) {
6650 strcpy (make
, table
[i
].t_make
);
6651 strcpy (model
, table
[i
].t_model
);
6652 if (table
[i
].withjpeg
)
6653 parse_external_jpeg();
6655 if (make
[0] == 0) parse_smal (0, fsize
);
6656 if (make
[0] == 0) parse_jpeg (is_raw
= 0);
6658 for (i
=0; i
< sizeof corp
/ sizeof *corp
; i
++)
6659 if (strstr (make
, corp
[i
])) /* Simplify company names */
6660 strcpy (make
, corp
[i
]);
6661 if (!strncmp (make
,"KODAK",5))
6662 make
[16] = model
[16] = 0;
6663 cp
= make
+ strlen(make
); /* Remove trailing spaces */
6664 while (*--cp
== ' ') *cp
= 0;
6665 cp
= model
+ strlen(model
);
6666 while (*--cp
== ' ') *cp
= 0;
6667 i
= strlen(make
); /* Remove make from model */
6668 if (!strncasecmp (model
, make
, i
) && model
[i
++] == ' ')
6669 memmove (model
, model
+i
, 64-i
);
6670 if (!strncmp (model
,"Digital Camera ",15))
6671 strcpy (model
, model
+15);
6672 desc
[511] = artist
[63] = make
[63] = model
[63] = model2
[63] = 0;
6673 if (!is_raw
) goto notraw
;
6675 if (!maximum
) maximum
= (1 << tiff_bps
) - 1;
6676 if (!height
) height
= raw_height
;
6677 if (!width
) width
= raw_width
;
6679 width
= height
+ fuji_width
;
6683 if (height
== 2624 && width
== 3936) /* Pentax K10D and Samsung GX10 */
6684 { height
= 2616; width
= 3896; }
6685 if (height
== 3136 && width
== 4864) /* Pentax K20D */
6686 { height
= 3124; width
= 4688; }
6687 if (height
== 3014 && width
== 4096) /* Ricoh GX200 */
6690 if (filters
== UINT_MAX
) filters
= 0;
6691 if (filters
) is_raw
= tiff_samples
;
6692 else colors
= tiff_samples
;
6693 if (tiff_compress
== 1)
6694 load_raw
= &CLASS adobe_dng_load_raw_nc
;
6695 if (tiff_compress
== 7)
6696 load_raw
= &CLASS adobe_dng_load_raw_lj
;
6699 if ((is_canon
= !strcmp(make
,"Canon")))
6700 load_raw
= memcmp (head
+6,"HEAPCCDR",8) ?
6701 &CLASS lossless_jpeg_load_raw
: &CLASS canon_compressed_load_raw
;
6702 if (!strcmp(make
,"NIKON")) {
6704 load_raw
= &CLASS packed_12_load_raw
;
6705 if (model
[0] == 'E')
6706 load_flags
|= !data_offset
<< 2 | 2;
6708 if (!strcmp(make
,"CASIO")) {
6709 load_raw
= &CLASS packed_12_load_raw
;
6713 /* Set parameters based on camera name (for non-DNG files). */
6716 if (height
*2 < width
) pixel_aspect
= 0.5;
6717 if (height
> width
) pixel_aspect
= 2;
6719 load_raw
= &CLASS foveon_load_raw
;
6721 } else if (is_canon
&& tiff_bps
== 15) {
6723 case 3344: width
-= 66;
6724 case 3872: width
-= 6;
6727 load_raw
= &CLASS canon_sraw_load_raw
;
6728 } else if (!strcmp(model
,"PowerShot 600")) {
6732 pixel_aspect
= 607/628.0;
6734 filters
= 0xe1e4e1e4;
6735 load_raw
= &CLASS canon_600_load_raw
;
6736 } else if (!strcmp(model
,"PowerShot A5") ||
6737 !strcmp(model
,"PowerShot A5 Zoom")) {
6741 pixel_aspect
= 256/235.0;
6743 filters
= 0x1e4e1e4e;
6744 load_raw
= &CLASS canon_a5_load_raw
;
6745 } else if (!strcmp(model
,"PowerShot A50")) {
6750 filters
= 0x1b4e4b1e;
6751 load_raw
= &CLASS canon_a5_load_raw
;
6752 } else if (!strcmp(model
,"PowerShot Pro70")) {
6756 filters
= 0x1e4b4e1b;
6757 load_raw
= &CLASS canon_a5_load_raw
;
6758 } else if (!strcmp(model
,"PowerShot SD300")) {
6765 load_raw
= &CLASS canon_a5_load_raw
;
6766 } else if (!strcmp(model
,"PowerShot A460")) {
6773 load_raw
= &CLASS canon_a5_load_raw
;
6774 } else if (!strcmp(model
,"PowerShot A530")) {
6781 load_raw
= &CLASS canon_a5_load_raw
;
6783 } else if (!strcmp(model
,"PowerShot A610")) {
6784 if (canon_s2is()) strcpy (model
+10, "S2 IS");
6791 load_raw
= &CLASS canon_a5_load_raw
;
6792 } else if (!strcmp(model
,"PowerShot A620")) {
6799 load_raw
= &CLASS canon_a5_load_raw
;
6800 } else if (!strcmp(model
,"PowerShot A720")) {
6807 load_raw
= &CLASS canon_a5_load_raw
;
6808 } else if (!strcmp(model
,"PowerShot A630")) {
6815 load_raw
= &CLASS canon_a5_load_raw
;
6816 } else if (!strcmp(model
,"PowerShot A640")) {
6823 load_raw
= &CLASS canon_a5_load_raw
;
6824 } else if (!strcmp(model
,"PowerShot A650")) {
6831 load_raw
= &CLASS canon_a5_load_raw
;
6832 } else if (!strcmp(model
,"PowerShot S3 IS")) {
6839 load_raw
= &CLASS canon_a5_load_raw
;
6840 } else if (!strcmp(model
,"PowerShot Pro90 IS")) {
6843 filters
= 0xb4b4b4b4;
6844 } else if (is_canon
&& raw_width
== 2144) {
6849 if (!strcmp(model
,"PowerShot G1")) {
6851 filters
= 0xb4b4b4b4;
6853 } else if (is_canon
&& raw_width
== 2224) {
6858 } else if (is_canon
&& raw_width
== 2376) {
6863 } else if (is_canon
&& raw_width
== 2672) {
6868 } else if (is_canon
&& raw_width
== 3152) {
6873 if (unique_id
== 0x80000170)
6874 adobe_coeff ("Canon","EOS 300D");
6875 } else if (is_canon
&& raw_width
== 3160) {
6880 } else if (is_canon
&& raw_width
== 3344) {
6885 } else if (!strcmp(model
,"EOS D2000C")) {
6886 filters
= 0x61616161;
6888 } else if (is_canon
&& raw_width
== 3516) {
6891 if (unique_id
== 0x80000189)
6892 adobe_coeff ("Canon","EOS 350D");
6894 } else if (is_canon
&& raw_width
== 3596) {
6898 } else if (is_canon
&& raw_width
== 3944) {
6903 } else if (is_canon
&& raw_width
== 3948) {
6907 if (unique_id
== 0x80000236)
6908 adobe_coeff ("Canon","EOS 400D");
6909 if (unique_id
== 0x80000254)
6910 adobe_coeff ("Canon","EOS 1000D");
6912 } else if (is_canon
&& raw_width
== 3984) {
6917 } else if (is_canon
&& raw_width
== 4104) {
6922 } else if (is_canon
&& raw_width
== 4312) {
6926 if (unique_id
== 0x80000176)
6927 adobe_coeff ("Canon","EOS 450D");
6929 } else if (is_canon
&& raw_width
== 4476) {
6933 } else if (is_canon
&& raw_width
== 4480) {
6938 filters
= 0x49494949;
6939 } else if (is_canon
&& raw_width
== 1208) {
6942 raw_width
= width
*= 4;
6944 } else if (is_canon
&& raw_width
== 1448) {
6947 raw_width
= width
*= 4;
6949 } else if (is_canon
&& raw_width
== 5108) {
6953 height
-= top_margin
;
6954 width
-= left_margin
;
6955 } else if (is_canon
&& raw_width
== 5712) {
6960 } else if (!strcmp(model
,"D1")) {
6961 cam_mul
[0] *= 256/527.0;
6962 cam_mul
[2] *= 256/317.0;
6963 } else if (!strcmp(model
,"D1X")) {
6966 } else if (!strcmp(model
,"D40X") ||
6967 !strcmp(model
,"D60") ||
6968 !strcmp(model
,"D80")) {
6971 } else if (!strcmp(model
,"D3") ||
6972 !strcmp(model
,"D700")) {
6975 } else if (!strncmp(model
,"D40",3) ||
6976 !strncmp(model
,"D50",3) ||
6977 !strncmp(model
,"D70",3)) {
6979 } else if (!strcmp(model
,"D90")) {
6981 } else if (!strcmp(model
,"D100")) {
6982 if (tiff_compress
== 34713 && !nikon_is_compressed()) {
6983 load_raw
= &CLASS packed_12_load_raw
;
6985 raw_width
= (width
+= 3) + 3;
6987 } else if (!strcmp(model
,"D200")) {
6990 filters
= 0x94949494;
6991 } else if (!strncmp(model
,"D2H",3)) {
6994 } else if (!strncmp(model
,"D2X",3)) {
6995 if (width
== 3264) width
-= 32;
6997 } else if (!strcmp(model
,"D300")) {
6999 } else if (!strcmp(model
,"COOLPIX P6000")) {
7001 filters
= 0x94949494;
7002 } else if (fsize
== 1581060) {
7006 load_raw
= &CLASS nikon_e900_load_raw
;
7009 filters
= 0x1e1e1e1e;
7011 pre_mul
[0] = 1.2085;
7012 pre_mul
[1] = 1.0943;
7013 pre_mul
[3] = 1.1103;
7014 #ifdef LIBRAW_LIBRARY_BUILD
7015 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7017 } else if (fsize
== 2465792) {
7021 load_raw
= &CLASS nikon_e900_load_raw
;
7023 filters
= 0x4b4b4b4b;
7024 adobe_coeff ("NIKON","E950");
7025 } else if (fsize
== 4771840) {
7029 filters
= 0xe1e1e1e1;
7030 load_raw
= &CLASS packed_12_load_raw
;
7032 if (!timestamp
&& nikon_e995())
7033 strcpy (model
, "E995");
7034 if (strcmp(model
,"E995")) {
7035 filters
= 0xb4b4b4b4;
7040 #ifdef LIBRAW_LIBRARY_BUILD
7041 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7044 } else if (!strcmp(model
,"E2100")) {
7045 if (!timestamp
&& !nikon_e2100()) goto cp_e2500
;
7049 } else if (!strcmp(model
,"E2500")) {
7051 strcpy (model
, "E2500");
7055 filters
= 0x4b4b4b4b;
7056 } else if (fsize
== 4775936) {
7059 load_raw
= &CLASS packed_12_load_raw
;
7063 #ifdef LIBRAW_LIBRARY_BUILD
7064 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7066 if (!timestamp
) nikon_3700();
7067 if (model
[0] == 'E' && atoi(model
+1) < 3700)
7068 filters
= 0x49494949;
7069 if (!strcmp(model
,"Optio 33WR")) {
7071 filters
= 0x16161616;
7074 #ifdef LIBRAW_LIBRARY_BUILD
7075 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7078 } else if (fsize
== 5869568) {
7081 filters
= 0x16161616;
7082 if (!timestamp
&& minolta_z2()) {
7083 strcpy (make
, "Minolta");
7084 strcpy (model
,"DiMAGE Z2");
7086 load_raw
= &CLASS packed_12_load_raw
;
7087 load_flags
= 6 + (make
[0] == 'M');
7088 } else if (!strcmp(model
,"E4500")) {
7092 filters
= 0xb4b4b4b4;
7093 } else if (fsize
== 7438336) {
7097 filters
= 0xb4b4b4b4;
7098 } else if (fsize
== 8998912) {
7102 load_raw
= &CLASS packed_12_load_raw
;
7104 } else if (!strcmp(model
,"FinePix S5100") ||
7105 !strcmp(model
,"FinePix S5500")) {
7106 load_raw
= &CLASS unpacked_load_raw
;
7107 } else if (!strcmp(make
,"FUJIFILM")) {
7108 if (!strcmp(model
+7,"S2Pro")) {
7109 strcpy (model
+7," S2Pro");
7115 if (is_raw
== 2 && shot_select
)
7117 top_margin
= (raw_height
- height
)/2;
7118 left_margin
= (raw_width
- width
)/2;
7120 data_offset
+= (shot_select
> 0) * ( fuji_layout
?
7121 (raw_width
*= 2) : raw_height
*raw_width
*2 );
7122 fuji_width
= width
>> !fuji_layout
;
7123 width
= (height
>> fuji_layout
) + fuji_width
;
7124 raw_height
= height
;
7126 load_raw
= &CLASS fuji_load_raw
;
7127 if (!(fuji_width
& 1)) filters
= 0x49494949;
7128 } else if (!strcmp(model
,"RD175")) {
7132 filters
= 0x61616161;
7133 load_raw
= &CLASS minolta_rd175_load_raw
;
7134 } else if (!strcmp(model
,"KD-400Z")) {
7139 } else if (!strcmp(model
,"KD-510Z")) {
7141 } else if (!strcasecmp(make
,"MINOLTA")) {
7142 load_raw
= &CLASS unpacked_load_raw
;
7144 if (!strncmp(model
,"DiMAGE A",8)) {
7145 if (!strcmp(model
,"DiMAGE A200"))
7146 filters
= 0x49494949;
7147 load_raw
= &CLASS packed_12_load_raw
;
7148 } else if (!strncmp(model
,"ALPHA",5) ||
7149 !strncmp(model
,"DYNAX",5) ||
7150 !strncmp(model
,"MAXXUM",6)) {
7151 sprintf (model
+20, "DYNAX %-10s", model
+6+(model
[0]=='M'));
7152 adobe_coeff (make
, model
+20);
7153 load_raw
= &CLASS packed_12_load_raw
;
7154 } else if (!strncmp(model
,"DiMAGE G",8)) {
7155 if (model
[8] == '4') {
7158 } else if (model
[8] == '5') {
7163 } else if (model
[8] == '6') {
7168 filters
= 0x61616161;
7170 load_raw
= &CLASS unpacked_load_raw
;
7174 } else if (!strcmp(model
,"*ist DS")) {
7176 } else if (!strcmp(model
,"K20D")) {
7177 filters
= 0x16161616;
7178 } else if (!strcmp(model
,"Optio S")) {
7179 if (fsize
== 3178560) {
7182 load_raw
= &CLASS eight_bit_load_raw
;
7187 #ifdef LIBRAW_LIBRARY_BUILD
7188 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7194 load_raw
= &CLASS packed_12_load_raw
;
7198 #ifdef LIBRAW_LIBRARY_BUILD
7199 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7202 } else if (fsize
== 6114240) {
7206 load_raw
= &CLASS packed_12_load_raw
;
7210 #ifdef LIBRAW_LIBRARY_BUILD
7211 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7213 } else if (!strcmp(model
,"Optio 750Z")) {
7216 load_raw
= &CLASS packed_12_load_raw
;
7218 } else if (!strcmp(model
,"S85")) {
7221 raw_width
= fsize
/height
/2;
7223 load_raw
= &CLASS unpacked_load_raw
;
7225 } else if (!strcmp(model
,"STV680 VGA")) {
7228 load_raw
= &CLASS eight_bit_load_raw
;
7230 filters
= 0x16161616;
7234 #ifdef LIBRAW_LIBRARY_BUILD
7235 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7237 } else if (!strcmp(model
,"KAI-0340")) {
7242 load_raw
= &CLASS unpacked_load_raw
;
7245 #ifdef LIBRAW_LIBRARY_BUILD
7246 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7248 } else if (!strcmp(model
,"N95")) {
7249 height
= raw_height
- (top_margin
= 2);
7250 } else if (!strcmp(model
,"531C")) {
7253 load_raw
= &CLASS unpacked_load_raw
;
7254 filters
= 0x49494949;
7256 #ifdef LIBRAW_LIBRARY_BUILD
7257 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7259 } else if (!strcmp(model
,"F-080C")) {
7262 load_raw
= &CLASS eight_bit_load_raw
;
7263 } else if (!strcmp(model
,"F-145C")) {
7266 load_raw
= &CLASS eight_bit_load_raw
;
7267 } else if (!strcmp(model
,"F-201C")) {
7270 load_raw
= &CLASS eight_bit_load_raw
;
7271 } else if (!strcmp(model
,"F-510C")) {
7274 load_raw
= fsize
< 7500000 ?
7275 &CLASS eight_bit_load_raw
: &CLASS unpacked_load_raw
;
7277 } else if (!strcmp(model
,"F-810C")) {
7280 load_raw
= &CLASS unpacked_load_raw
;
7282 } else if (!strcmp(model
,"XCD-SX910CR")) {
7286 filters
= 0x49494949;
7288 load_raw
= fsize
< 2000000 ?
7289 &CLASS eight_bit_load_raw
: &CLASS unpacked_load_raw
;
7290 } else if (!strcmp(model
,"2010")) {
7294 filters
= 0x16161616;
7297 load_raw
= &CLASS unpacked_load_raw
;
7298 } else if (!strcmp(model
,"A782")) {
7301 filters
= 0x61616161;
7302 load_raw
= fsize
< 10000000 ?
7303 &CLASS eight_bit_load_raw
: &CLASS unpacked_load_raw
;
7305 } else if (!strcmp(model
,"3320AF")) {
7307 raw_width
= width
= 2048;
7308 filters
= 0x61616161;
7309 load_raw
= &CLASS unpacked_load_raw
;
7313 #ifdef LIBRAW_LIBRARY_BUILD
7314 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7316 fseek (ifp
, 0x300000, SEEK_SET
);
7317 if ((order
= guess_byte_order(0x10000)) == 0x4d4d) {
7318 height
-= (top_margin
= 16);
7319 width
-= (left_margin
= 28);
7321 strcpy (make
, "ISG");
7324 } else if (!strcmp(make
,"Hasselblad")) {
7325 if (load_raw
== &CLASS lossless_jpeg_load_raw
)
7326 load_raw
= &CLASS hasselblad_load_raw
;
7327 if (raw_width
== 7262) {
7332 filters
= 0x61616161;
7333 } else if (raw_width
== 4090) {
7334 strcpy (model
, "V96C");
7335 height
-= (top_margin
= 6);
7336 width
-= (left_margin
= 3) + 7;
7337 filters
= 0x61616161;
7339 } else if (!strcmp(make
,"Sinar")) {
7340 if (!memcmp(head
,"8BPS",4)) {
7341 fseek (ifp
, 14, SEEK_SET
);
7344 filters
= 0x61616161;
7347 if (!load_raw
) load_raw
= &CLASS unpacked_load_raw
;
7349 } else if (!strcmp(make
,"Leaf")) {
7351 fseek (ifp
, data_offset
, SEEK_SET
);
7352 if (ljpeg_start (&jh
, 1) && jh
.bits
== 15)
7354 if (tiff_samples
> 1) filters
= 0;
7355 if (tiff_samples
> 1 || tile_length
< raw_height
)
7356 load_raw
= &CLASS leaf_hdr_load_raw
;
7357 if ((width
| height
) == 2048) {
7358 if (tiff_samples
== 1) {
7360 strcpy (cdesc
, "RBTG");
7361 strcpy (model
, "CatchLight");
7362 top_margin
= 8; left_margin
= 18; height
= 2032; width
= 2016;
7364 strcpy (model
, "DCB2");
7365 top_margin
= 10; left_margin
= 16; height
= 2028; width
= 2022;
7367 } else if (width
+height
== 3144+2060) {
7368 if (!model
[0]) strcpy (model
, "Cantare");
7369 if (width
> height
) {
7370 top_margin
= 6; left_margin
= 32; height
= 2048; width
= 3072;
7371 filters
= 0x61616161;
7373 left_margin
= 6; top_margin
= 32; width
= 2048; height
= 3072;
7374 filters
= 0x16161616;
7376 if (!cam_mul
[0] || model
[0] == 'V') filters
= 0;
7377 else is_raw
= tiff_samples
;
7378 } else if (width
== 2116) {
7379 strcpy (model
, "Valeo 6");
7380 height
-= 2 * (top_margin
= 30);
7381 width
-= 2 * (left_margin
= 55);
7382 filters
= 0x49494949;
7383 } else if (width
== 3171) {
7384 strcpy (model
, "Valeo 6");
7385 height
-= 2 * (top_margin
= 24);
7386 width
-= 2 * (left_margin
= 24);
7387 filters
= 0x16161616;
7389 } else if (!strcmp(make
,"LEICA") || !strcmp(make
,"Panasonic")) {
7391 if ((fsize
-data_offset
) / (width
*8/7) == height
)
7392 load_raw
= &CLASS panasonic_load_raw
;
7393 if (!load_raw
) load_raw
= &CLASS unpacked_load_raw
;
7396 adobe_coeff ("Panasonic","DMC-LC1"); break;
7402 if (height
> 2326) {
7405 filters
= 0x49494949;
7408 adobe_coeff ("Panasonic","DMC-FZ8"); break;
7413 filters
= 0x49494949;
7415 adobe_coeff ("Panasonic","DMC-L1"); break;
7419 adobe_coeff ("Panasonic","DMC-FZ30"); break;
7428 height
= 2480 - (top_margin
= 10);
7429 filters
= 0x49494949;
7431 adobe_coeff ("Panasonic","DMC-FZ18"); break;
7438 if (--height
== 2798 && (height
= 2760))
7440 else filters
= 0x49494949;
7443 adobe_coeff ("Panasonic","DMC-FZ50"); break;
7446 filters
= 0x49494949;
7447 adobe_coeff ("Panasonic","DMC-L10"); break;
7452 lx3
: filters
= 0x16161616;
7454 adobe_coeff ("Panasonic","DMC-LX3");
7460 adobe_coeff ("Panasonic","DMC-LX1"); break;
7463 if (height
== 2250) goto lx3
;
7465 filters
= 0x49494949;
7467 adobe_coeff ("Panasonic","DMC-G1"); break;
7471 filters
= 0x49494949;
7474 if ((height
-= 39) == 2400)
7477 adobe_coeff ("Panasonic","DMC-LX2"); break;
7481 filters
= 0x16161616;
7482 adobe_coeff ("Panasonic","DMC-FX150"); break;
7484 } else if (!strcmp(model
,"C770UZ")) {
7487 filters
= 0x16161616;
7488 load_raw
= &CLASS packed_12_load_raw
;
7490 } else if (!strcmp(make
,"OLYMPUS")) {
7491 height
+= height
& 1;
7493 if (load_raw
== &CLASS olympus_e410_load_raw
) {
7495 } else if (!strcmp(model
,"E-10") ||
7496 !strncmp(model
,"E-20",4)) {
7498 } else if (!strcmp(model
,"E-300") ||
7499 !strcmp(model
,"E-500")) {
7501 if (load_raw
== &CLASS unpacked_load_raw
) {
7505 } else if (!strcmp(model
,"E-330")) {
7507 if (load_raw
== &CLASS unpacked_load_raw
)
7509 } else if (!strcmp(model
,"SP550UZ")) {
7510 thumb_length
= fsize
- (thumb_offset
= 0xa39800);
7514 } else if (!strcmp(model
,"N Digital")) {
7517 filters
= 0x61616161;
7518 data_offset
= 0x1a00;
7519 load_raw
= &CLASS packed_12_load_raw
;
7520 } else if (!strcmp(model
,"DSC-F828")) {
7523 data_offset
= 862144;
7524 load_raw
= &CLASS sony_load_raw
;
7525 filters
= 0x9c9c9c9c;
7527 strcpy (cdesc
, "RGBE");
7528 } else if (!strcmp(model
,"DSC-V3")) {
7531 data_offset
= 787392;
7532 load_raw
= &CLASS sony_load_raw
;
7533 } else if (!strcmp(make
,"SONY") && raw_width
== 3984) {
7534 adobe_coeff ("SONY","DSC-R1");
7537 } else if (!strcmp(model
,"DSLR-A100")) {
7539 } else if (!strcmp(model
,"DSLR-A350")) {
7541 } else if (!strcmp(model
,"C603v")) {
7545 } else if (!strcmp(model
,"C603y")) {
7550 load_raw
= &CLASS kodak_yrgb_load_raw
;
7551 } else if (!strcmp(model
,"C603")) {
7552 raw_height
= height
= 2152;
7553 raw_width
= width
= 2864;
7555 } else if (!strcmp(model
,"C330")) {
7564 if ((data_offset
= fsize
- raw_height
*raw_width
)) {
7565 fseek (ifp
, 168, SEEK_SET
);
7566 read_shorts (curve
, 256);
7567 #ifdef LIBRAW_LIBRARY_BUILD
7568 color_flags
.curve_state
= LIBRAW_COLORSTATE_LOADED
;
7570 } else use_gamma
= 0;
7571 load_raw
= &CLASS eight_bit_load_raw
;
7572 } else if (!strcasecmp(make
,"KODAK")) {
7573 if (filters
== UINT_MAX
) filters
= 0x61616161;
7574 if (!strncmp(model
,"NC2000",6)) {
7577 } else if (!strcmp(model
,"EOSDCS3B")) {
7580 } else if (!strcmp(model
,"EOSDCS1")) {
7583 } else if (!strcmp(model
,"DCS420")) {
7586 } else if (!strcmp(model
,"DCS460")) {
7589 } else if (!strcmp(model
,"DCS460A")) {
7594 } else if (!strcmp(model
,"DCS660M")) {
7598 } else if (!strcmp(model
,"DCS760M")) {
7602 if (!strcmp(model
+4,"20X"))
7603 strcpy (cdesc
, "MYCY");
7604 if (strstr(model
,"DC25")) {
7605 strcpy (model
, "DC25");
7606 data_offset
= 15424;
7608 if (!strncmp(model
,"DC2",3)) {
7610 if (fsize
< 100000) {
7611 raw_width
= 256; width
= 249;
7612 pixel_aspect
= (4.0*height
) / (3.0*width
);
7614 raw_width
= 512; width
= 501;
7615 pixel_aspect
= (493.0*height
) / (373.0*width
);
7617 data_offset
+= raw_width
+ 1;
7619 filters
= 0x8d8d8d8d;
7624 #ifdef LIBRAW_LIBRARY_BUILD
7625 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7627 load_raw
= &CLASS eight_bit_load_raw
;
7628 } else if (!strcmp(model
,"40")) {
7629 strcpy (model
, "DC40");
7633 load_raw
= &CLASS kodak_radc_load_raw
;
7634 } else if (strstr(model
,"DC50")) {
7635 strcpy (model
, "DC50");
7638 data_offset
= 19712;
7639 load_raw
= &CLASS kodak_radc_load_raw
;
7640 } else if (strstr(model
,"DC120")) {
7641 strcpy (model
, "DC120");
7644 pixel_aspect
= height
/0.75/width
;
7645 load_raw
= tiff_compress
== 7 ?
7646 &CLASS kodak_jpeg_load_raw
: &CLASS kodak_dc120_load_raw
;
7647 } else if (!strcmp(model
,"DCS200")) {
7650 thumb_offset
= 6144;
7652 write_thumb
= &CLASS layer_thumb
;
7655 data_offset
= 79872;
7656 load_raw
= &CLASS eight_bit_load_raw
;
7659 } else if (!strcmp(model
,"Fotoman Pixtura")) {
7663 load_raw
= &CLASS kodak_radc_load_raw
;
7664 filters
= 0x61616161;
7666 } else if (!strcmp(model
,"QuickTake 100")) {
7667 fseek (ifp
, 544, SEEK_SET
);
7670 data_offset
= (get4(),get2()) == 30 ? 738:736;
7671 if (height
> width
) {
7673 fseek (ifp
, data_offset
-6, SEEK_SET
);
7674 flip
= ~get2() & 3 ? 5:6;
7676 load_raw
= &CLASS quicktake_100_load_raw
;
7677 filters
= 0x61616161;
7678 } else if (!strcmp(model
,"QuickTake 150")) {
7679 data_offset
= 738 - head
[5];
7680 if (head
[5]) strcpy (model
+10, "200");
7681 load_raw
= &CLASS kodak_radc_load_raw
;
7684 filters
= 0x61616161;
7685 } else if (!strcmp(make
,"Rollei") && !load_raw
) {
7686 switch (raw_width
) {
7699 filters
= 0x16161616;
7700 load_raw
= &CLASS rollei_load_raw
;
7703 #ifdef LIBRAW_LIBRARY_BUILD
7704 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7706 } else if (!strcmp(model
,"PC-CAM 600")) {
7708 data_offset
= width
= 1024;
7709 filters
= 0x49494949;
7710 load_raw
= &CLASS eight_bit_load_raw
;
7713 #ifdef LIBRAW_LIBRARY_BUILD
7714 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7716 } else if (!strcmp(model
,"QV-2000UX")) {
7719 data_offset
= width
* 2;
7720 load_raw
= &CLASS eight_bit_load_raw
;
7721 } else if (fsize
== 3217760) {
7725 load_raw
= &CLASS eight_bit_load_raw
;
7726 } else if (!strcmp(model
,"QV-4000")) {
7729 load_raw
= &CLASS unpacked_load_raw
;
7731 } else if (!strcmp(model
,"QV-5700")) {
7734 load_raw
= &CLASS casio_qv5700_load_raw
;
7735 } else if (!strcmp(model
,"QV-R41")) {
7740 } else if (!strcmp(model
,"QV-R51")) {
7746 #ifdef LIBRAW_LIBRARY_BUILD
7747 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7749 } else if (!strcmp(model
,"EX-S100")) {
7755 #ifdef LIBRAW_LIBRARY_BUILD
7756 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7758 } else if (!strcmp(model
,"EX-Z50")) {
7764 #ifdef LIBRAW_LIBRARY_BUILD
7765 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7767 } else if (!strcmp(model
,"EX-Z55")) {
7773 #ifdef LIBRAW_LIBRARY_BUILD
7774 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7776 } else if (!strcmp(model
,"EX-P505")) {
7783 #ifdef LIBRAW_LIBRARY_BUILD
7784 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7786 } else if (fsize
== 9313536) { /* EX-P600 or QV-R61 */
7792 #ifdef LIBRAW_LIBRARY_BUILD
7793 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7795 } else if (!strcmp(model
,"EX-P700")) {
7801 #ifdef LIBRAW_LIBRARY_BUILD
7802 color_flags
.pre_mul_state
= LIBRAW_COLORSTATE_CONST
;
7806 sprintf (model
, "%dx%d", width
, height
);
7807 if (filters
== UINT_MAX
) filters
= 0x94949494;
7808 if (raw_color
) adobe_coeff (make
, model
);
7809 if (thumb_offset
&& !thumb_height
) {
7810 fseek (ifp
, thumb_offset
, SEEK_SET
);
7811 if (ljpeg_start (&jh
, 1)) {
7812 thumb_width
= jh
.wide
;
7813 thumb_height
= jh
.high
;
7817 if (!load_raw
|| height
< 22) is_raw
= 0;
7819 if (load_raw
== &CLASS kodak_jpeg_load_raw
) {
7820 #ifdef DCRAW_VERBOSE
7821 fprintf (stderr
,_("%s: You must link dcraw with libjpeg!!\n"), ifname
);
7824 #ifdef LIBRAW_LIBRARY_BUILD
7825 imgdata
.process_warnings
|= LIBRAW_WARN_NO_JPEGLIB
;
7830 strcpy (cdesc
, colors
== 3 ? "RGB":"GMCY");
7831 if (!raw_height
) raw_height
= height
;
7832 if (!raw_width
) raw_width
= width
;
7833 if (filters
&& colors
== 3)
7834 for (i
=0; i
< 32; i
+=4) {
7835 if ((filters
>> i
& 15) == 9)
7837 if ((filters
>> i
& 15) == 6)
7841 if (flip
== -1) flip
= tiff_flip
;
7842 if (flip
== -1) flip
= 0;
7843 #ifdef LIBRAW_LIBRARY_BUILD
7844 RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY
,1,2);
7847 #line 9212 "dcraw/dcraw.c"
7848 void CLASS
convert_to_rgb()
7850 int row
, col
, c
, i
, j
, k
;
7852 float out
[3], out_cam
[3][4];
7853 double num
, inverse
[3][3], bnd
[2]={0,0};
7854 static const double xyzd50_srgb
[3][3] =
7855 { { 0.436083, 0.385083, 0.143055 },
7856 { 0.222507, 0.716888, 0.060608 },
7857 { 0.013930, 0.097097, 0.714022 } };
7858 static const double rgb_rgb
[3][3] =
7859 { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
7860 static const double adobe_rgb
[3][3] =
7861 { { 0.715146, 0.284856, 0.000000 },
7862 { 0.000000, 1.000000, 0.000000 },
7863 { 0.000000, 0.041166, 0.958839 } };
7864 static const double wide_rgb
[3][3] =
7865 { { 0.593087, 0.404710, 0.002206 },
7866 { 0.095413, 0.843149, 0.061439 },
7867 { 0.011621, 0.069091, 0.919288 } };
7868 static const double prophoto_rgb
[3][3] =
7869 { { 0.529317, 0.330092, 0.140588 },
7870 { 0.098368, 0.873465, 0.028169 },
7871 { 0.016879, 0.117663, 0.865457 } };
7872 static const double (*out_rgb
[])[3] =
7873 { rgb_rgb
, adobe_rgb
, wide_rgb
, prophoto_rgb
, xyz_rgb
};
7874 static const char *name
[] =
7875 { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" };
7876 static const unsigned phead
[] =
7877 { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
7878 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
7880 { 10, 0x63707274, 0, 36, /* cprt */
7881 0x64657363, 0, 40, /* desc */
7882 0x77747074, 0, 20, /* wtpt */
7883 0x626b7074, 0, 20, /* bkpt */
7884 0x72545243, 0, 14, /* rTRC */
7885 0x67545243, 0, 14, /* gTRC */
7886 0x62545243, 0, 14, /* bTRC */
7887 0x7258595a, 0, 20, /* rXYZ */
7888 0x6758595a, 0, 20, /* gXYZ */
7889 0x6258595a, 0, 20 }; /* bXYZ */
7890 static const unsigned pwhite
[] = { 0xf351, 0x10000, 0x116cc };
7891 unsigned pcurve
[] = { 0x63757276, 0, 1, 0x1000000 };
7893 #ifdef LIBRAW_LIBRARY_BUILD
7894 RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB
,0,2);
7897 bnd
[gamm
[1] >= 1] = 1;
7898 if (gamm
[1] && (gamm
[1]-1)*(gamm
[0]-1) <= 0) {
7899 for (i
=0; i
< 36; i
++) {
7900 gamm
[2] = (bnd
[0] + bnd
[1])/2;
7901 bnd
[(pow(gamm
[2]/gamm
[1],-gamm
[0])-1)/gamm
[0]-1/gamm
[2] > -1] = gamm
[2];
7903 gamm
[3] = gamm
[2]*(1/gamm
[0]-1);
7906 gamm
[4] = 1 / (gamm
[1]/2*SQR(gamm
[2]) - gamm
[3]*(1-gamm
[2]) +
7907 (1-pow(gamm
[2],1+gamm
[0]))*(1+gamm
[3])/(1+gamm
[0])) - 1;
7909 memcpy (out_cam
, rgb_cam
, sizeof out_cam
);
7910 raw_color
|= colors
== 1 || document_mode
||
7911 output_color
< 1 || output_color
> 5;
7913 oprof
= (unsigned *) calloc (phead
[0], 1);
7914 merror (oprof
, "convert_to_rgb()");
7915 memcpy (oprof
, phead
, sizeof phead
);
7916 if (output_color
== 5) oprof
[4] = oprof
[5];
7917 oprof
[0] = 132 + 12*pbody
[0];
7918 for (i
=0; i
< pbody
[0]; i
++) {
7919 oprof
[oprof
[0]/4] = i
? (i
> 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
7920 pbody
[i
*3+2] = oprof
[0];
7921 oprof
[0] += (pbody
[i
*3+3] + 3) & -4;
7923 memcpy (oprof
+32, pbody
, sizeof pbody
);
7924 oprof
[pbody
[5]/4+2] = strlen(name
[output_color
-1]) + 1;
7925 memcpy ((char *)oprof
+pbody
[8]+8, pwhite
, sizeof pwhite
);
7926 if (output_bps
== 8 | gamma_16bit
)
7927 pcurve
[3] = (short)(256/gamm
[4]+0.5) << 16;
7928 for (i
=4; i
< 7; i
++)
7929 memcpy ((char *)oprof
+pbody
[i
*3+2], pcurve
, sizeof pcurve
);
7930 pseudoinverse ((double (*)[3]) out_rgb
[output_color
-1], inverse
, 3);
7931 for (i
=0; i
< 3; i
++)
7932 for (j
=0; j
< 3; j
++) {
7933 for (num
= k
=0; k
< 3; k
++)
7934 num
+= xyzd50_srgb
[i
][k
] * inverse
[j
][k
];
7935 oprof
[pbody
[j
*3+23]/4+i
+2] = num
* 0x10000 + 0.5;
7937 for (i
=0; i
< phead
[0]/4; i
++)
7938 oprof
[i
] = htonl(oprof
[i
]);
7939 strcpy ((char *)oprof
+pbody
[2]+8, "auto-generated by dcraw");
7940 strcpy ((char *)oprof
+pbody
[5]+12, name
[output_color
-1]);
7941 for (i
=0; i
< 3; i
++)
7942 for (j
=0; j
< colors
; j
++)
7943 for (out_cam
[i
][j
] = k
=0; k
< 3; k
++)
7944 out_cam
[i
][j
] += out_rgb
[output_color
-1][i
][k
] * rgb_cam
[k
][j
];
7946 #ifdef DCRAW_VERBOSE
7948 fprintf (stderr
, raw_color
? _("Building histograms...\n") :
7949 _("Converting to %s colorspace...\n"), name
[output_color
-1]);
7952 #ifdef LIBRAW_LIBRARY_BUILD
7953 memset(histogram
,0,sizeof(int)*LIBRAW_HISTOGRAM_SIZE
*4);
7955 memset (histogram
, 0, sizeof histogram
);
7957 for (img
=image
[0], row
=0; row
< height
; row
++)
7958 for (col
=0; col
< width
; col
++, img
+=4) {
7960 out
[0] = out
[1] = out
[2] = 0;
7962 out
[0] += out_cam
[0][c
] * img
[c
];
7963 out
[1] += out_cam
[1][c
] * img
[c
];
7964 out
[2] += out_cam
[2][c
] * img
[c
];
7966 FORC3 img
[c
] = CLIP((int) out
[c
]);
7968 else if (document_mode
)
7969 img
[0] = img
[FC(row
,col
)];
7970 FORCC histogram
[c
][img
[c
] >> 3]++;
7972 if (colors
== 4 && output_color
) colors
= 3;
7973 if (document_mode
&& filters
) colors
= 1;
7974 #ifdef LIBRAW_LIBRARY_BUILD
7975 RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB
,1,2);
7979 void CLASS
fuji_rotate()
7985 ushort wide
, high
, (*img
)[4], (*pix
)[4];
7987 if (!fuji_width
) return;
7988 #ifdef DCRAW_VERBOSE
7990 fprintf (stderr
,_("Rotating image 45 degrees...\n"));
7992 fuji_width
= (fuji_width
- 1 + shrink
) >> shrink
;
7994 wide
= fuji_width
/ step
;
7995 high
= (height
- fuji_width
) / step
;
7996 img
= (ushort (*)[4]) calloc (wide
*high
, sizeof *img
);
7997 merror (img
, "fuji_rotate()");
7999 #ifdef LIBRAW_LIBRARY_BUILD
8000 RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE
,0,2);
8003 for (row
=0; row
< high
; row
++)
8004 for (col
=0; col
< wide
; col
++) {
8005 ur
= r
= fuji_width
+ (row
-col
)*step
;
8006 uc
= c
= (row
+col
)*step
;
8007 if (ur
> height
-2 || uc
> width
-2) continue;
8010 pix
= image
+ ur
*width
+ uc
;
8011 for (i
=0; i
< colors
; i
++)
8012 img
[row
*wide
+col
][i
] =
8013 (pix
[ 0][i
]*(1-fc
) + pix
[ 1][i
]*fc
) * (1-fr
) +
8014 (pix
[width
][i
]*(1-fc
) + pix
[width
+1][i
]*fc
) * fr
;
8021 #ifdef LIBRAW_LIBRARY_BUILD
8022 RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE
,1,2);
8026 void CLASS
stretch()
8028 ushort newdim
, (*img
)[4], *pix0
, *pix1
;
8032 if (pixel_aspect
== 1) return;
8033 #ifdef LIBRAW_LIBRARY_BUILD
8034 RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH
,0,2);
8036 #ifdef DCRAW_VERBOSE
8037 if (verbose
) fprintf (stderr
,_("Stretching the image...\n"));
8039 if (pixel_aspect
< 1) {
8040 newdim
= height
/ pixel_aspect
+ 0.5;
8041 img
= (ushort (*)[4]) calloc (width
*newdim
, sizeof *img
);
8042 merror (img
, "stretch()");
8043 for (rc
=row
=0; row
< newdim
; row
++, rc
+=pixel_aspect
) {
8044 frac
= rc
- (c
= rc
);
8045 pix0
= pix1
= image
[c
*width
];
8046 if (c
+1 < height
) pix1
+= width
*4;
8047 for (col
=0; col
< width
; col
++, pix0
+=4, pix1
+=4)
8048 FORCC img
[row
*width
+col
][c
] = pix0
[c
]*(1-frac
) + pix1
[c
]*frac
+ 0.5;
8052 newdim
= width
* pixel_aspect
+ 0.5;
8053 img
= (ushort (*)[4]) calloc (height
*newdim
, sizeof *img
);
8054 merror (img
, "stretch()");
8055 for (rc
=col
=0; col
< newdim
; col
++, rc
+=1/pixel_aspect
) {
8056 frac
= rc
- (c
= rc
);
8057 pix0
= pix1
= image
[c
];
8058 if (c
+1 < width
) pix1
+= 4;
8059 for (row
=0; row
< height
; row
++, pix0
+=width
*4, pix1
+=width
*4)
8060 FORCC img
[row
*newdim
+col
][c
] = pix0
[c
]*(1-frac
) + pix1
[c
]*frac
+ 0.5;
8066 #ifdef LIBRAW_LIBRARY_BUILD
8067 RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH
,1,2);
8071 int CLASS
flip_index (int row
, int col
)
8073 if (flip
& 4) SWAP(row
,col
);
8074 if (flip
& 2) row
= iheight
- 1 - row
;
8075 if (flip
& 1) col
= iwidth
- 1 - col
;
8076 return row
* iwidth
+ col
;
8079 void CLASS
gamma_lut (ushort lut
[0x10000])
8081 int perc
, c
, val
, total
, i
;
8084 perc
= width
* height
* 0.01; /* 99th percentile white level */
8085 if (fuji_width
) perc
/= 2;
8086 if ((highlight
& ~2) || no_auto_bright
) perc
= -1;
8088 for (val
=0x2000, total
=0; --val
> 32; )
8089 if ((total
+= histogram
[c
][val
]) > perc
) break;
8090 if (t_white
< val
) t_white
= val
;
8092 t_white
*= 8 / bright
;
8093 for (i
=0; i
< 0x10000; i
++) {
8095 val
= 65535 * ( !use_gamma
? r
:
8096 r
<= gamm
[2] ? r
*gamm
[1] : pow((double)r
,gamm
[0])*(1+gamm
[3])-gamm
[3]);
8097 if (val
> 65535) val
= 65535;
8103 #line 9492 "dcraw/dcraw.c"
8104 void CLASS
tiff_set (ushort
*ntag
,
8105 ushort tag
, ushort type
, int count
, int val
)
8107 struct tiff_tag
*tt
;
8110 tt
= (struct tiff_tag
*)(ntag
+1) + (*ntag
)++;
8114 if (type
< 3 && count
<= 4)
8115 FORC(4) tt
->val
.c
[c
] = val
>> (c
<< 3);
8116 else if (type
== 3 && count
<= 2)
8117 FORC(2) tt
->val
.s
[c
] = val
>> (c
<< 4);
8118 else tt
->val
.i
= val
;
8121 #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
8123 void CLASS
tiff_head (struct tiff_hdr
*th
, int full
)
8128 memset (th
, 0, sizeof *th
);
8129 th
->t_order
= htonl(0x4d4d4949) >> 16;
8133 tiff_set (&th
->ntag
, 254, 4, 1, 0);
8134 tiff_set (&th
->ntag
, 256, 4, 1, width
);
8135 tiff_set (&th
->ntag
, 257, 4, 1, height
);
8136 tiff_set (&th
->ntag
, 258, 3, colors
, output_bps
);
8138 th
->tag
[th
->ntag
-1].val
.i
= TOFF(th
->bps
);
8139 FORC4 th
->bps
[c
] = output_bps
;
8140 tiff_set (&th
->ntag
, 259, 3, 1, 1);
8141 tiff_set (&th
->ntag
, 262, 3, 1, 1 + (colors
> 1));
8143 tiff_set (&th
->ntag
, 270, 2, 512, TOFF(th
->t_desc
));
8144 tiff_set (&th
->ntag
, 271, 2, 64, TOFF(th
->t_make
));
8145 tiff_set (&th
->ntag
, 272, 2, 64, TOFF(th
->t_model
));
8147 if (oprof
) psize
= ntohl(oprof
[0]);
8148 tiff_set (&th
->ntag
, 273, 4, 1, sizeof *th
+ psize
);
8149 tiff_set (&th
->ntag
, 277, 3, 1, colors
);
8150 tiff_set (&th
->ntag
, 278, 4, 1, height
);
8151 tiff_set (&th
->ntag
, 279, 4, 1, height
*width
*colors
*output_bps
/8);
8153 tiff_set (&th
->ntag
, 274, 3, 1, "12435867"[flip
]-'0');
8154 tiff_set (&th
->ntag
, 282, 5, 1, TOFF(th
->rat
[0]));
8155 tiff_set (&th
->ntag
, 283, 5, 1, TOFF(th
->rat
[2]));
8156 tiff_set (&th
->ntag
, 284, 3, 1, 1);
8157 tiff_set (&th
->ntag
, 296, 3, 1, 2);
8158 tiff_set (&th
->ntag
, 305, 2, 32, TOFF(th
->soft
));
8159 tiff_set (&th
->ntag
, 306, 2, 20, TOFF(th
->date
));
8160 tiff_set (&th
->ntag
, 315, 2, 64, TOFF(th
->t_artist
));
8161 tiff_set (&th
->ntag
, 34665, 4, 1, TOFF(th
->nexif
));
8162 if (psize
) tiff_set (&th
->ntag
, 34675, 7, psize
, sizeof *th
);
8163 tiff_set (&th
->nexif
, 33434, 5, 1, TOFF(th
->rat
[4]));
8164 tiff_set (&th
->nexif
, 33437, 5, 1, TOFF(th
->rat
[6]));
8165 tiff_set (&th
->nexif
, 34855, 3, 1, iso_speed
);
8166 tiff_set (&th
->nexif
, 37386, 5, 1, TOFF(th
->rat
[8]));
8168 tiff_set (&th
->ntag
, 34853, 4, 1, TOFF(th
->ngps
));
8169 tiff_set (&th
->ngps
, 0, 1, 4, 0x202);
8170 tiff_set (&th
->ngps
, 1, 2, 2, gpsdata
[29]);
8171 tiff_set (&th
->ngps
, 2, 5, 3, TOFF(th
->gps
[0]));
8172 tiff_set (&th
->ngps
, 3, 2, 2, gpsdata
[30]);
8173 tiff_set (&th
->ngps
, 4, 5, 3, TOFF(th
->gps
[6]));
8174 tiff_set (&th
->ngps
, 5, 1, 1, gpsdata
[31]);
8175 tiff_set (&th
->ngps
, 6, 5, 1, TOFF(th
->gps
[18]));
8176 tiff_set (&th
->ngps
, 7, 5, 3, TOFF(th
->gps
[12]));
8177 tiff_set (&th
->ngps
, 18, 2, 12, TOFF(th
->gps
[20]));
8178 tiff_set (&th
->ngps
, 29, 2, 12, TOFF(th
->gps
[23]));
8179 memcpy (th
->gps
, gpsdata
, sizeof th
->gps
);
8181 th
->rat
[0] = th
->rat
[2] = 300;
8182 th
->rat
[1] = th
->rat
[3] = 1;
8183 FORC(6) th
->rat
[4+c
] = 1000000;
8184 th
->rat
[4] *= shutter
;
8185 th
->rat
[6] *= aperture
;
8186 th
->rat
[8] *= focal_len
;
8187 strncpy (th
->t_desc
, desc
, 512);
8188 strncpy (th
->t_make
, make
, 64);
8189 strncpy (th
->t_model
, model
, 64);
8190 strcpy (th
->soft
, "dcraw v"VERSION
);
8191 t
= gmtime (×tamp
);
8192 sprintf (th
->date
, "%04d:%02d:%02d %02d:%02d:%02d",
8193 t
->tm_year
+1900,t
->tm_mon
+1,t
->tm_mday
,t
->tm_hour
,t
->tm_min
,t
->tm_sec
);
8194 strncpy (th
->t_artist
, artist
, 64);
8197 void CLASS
jpeg_thumb_writer (FILE *tfp
,char *t_humb
,int t_humb_length
)
8203 if (strcmp (t_humb
+6, "Exif")) {
8204 memcpy (exif
, "\xff\xe1 Exif\0\0", 10);
8205 exif
[1] = htons (8 + sizeof th
);
8206 fwrite (exif
, 1, sizeof exif
, tfp
);
8208 fwrite (&th
, 1, sizeof th
, tfp
);
8210 fwrite (t_humb
+2, 1, t_humb_length
-2, tfp
);
8214 void CLASS
jpeg_thumb (FILE *tfp
)
8220 thumb
= (char *) malloc (thumb_length
);
8221 merror (thumb
, "jpeg_thumb()");
8222 fread (thumb
, 1, thumb_length
, ifp
);
8226 if (strcmp (thumb
+6, "Exif")) {
8227 memcpy (exif
, "\xff\xe1 Exif\0\0", 10);
8228 exif
[1] = htons (8 + sizeof th
);
8229 fwrite (exif
, 1, sizeof exif
, tfp
);
8231 fwrite (&th
, 1, sizeof th
, tfp
);
8233 fwrite (thumb
+2, 1, thumb_length
-2, tfp
);
8235 jpeg_thumb_writer(tfp
,thumb
,thumb_length
);
8240 void CLASS
write_ppm_tiff (FILE *ofp
)
8244 ushort
*ppm2
,lut16
[0x10000];
8245 int c
, row
, col
, soff
, rstep
, cstep
;
8249 if (flip
& 4) SWAP(height
,width
);
8250 ppm
= (uchar
*) calloc (width
, colors
*output_bps
/8);
8251 ppm2
= (ushort
*) ppm
;
8252 merror (ppm
, "write_ppm_tiff()");
8255 fwrite (&th
, sizeof th
, 1, ofp
);
8257 fwrite (oprof
, ntohl(oprof
[0]), 1, ofp
);
8258 } else if (colors
> 3)
8260 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
8261 width
, height
, colors
, (1 << output_bps
)-1, cdesc
);
8263 fprintf (ofp
, "P%d\n%d %d\n%d\n",
8264 colors
/2+5, width
, height
, (1 << output_bps
)-1);
8266 if (output_bps
== 8 || gamma_16bit
) gamma_lut (lut16
);
8268 soff
= flip_index (0, 0);
8269 cstep
= flip_index (0, 1) - soff
;
8270 rstep
= flip_index (1, 0) - flip_index (0, width
);
8271 for (row
=0; row
< height
; row
++, soff
+= rstep
) {
8272 for (col
=0; col
< width
; col
++, soff
+= cstep
)
8273 if (output_bps
== 8)
8274 FORCC ppm
[col
*colors
+c
] = lut16
[image
[soff
][c
]]/256;
8275 else if(gamma_16bit
) FORCC ppm2
[col
*colors
+c
] = lut16
[image
[soff
][c
]];
8276 else FORCC ppm2
[col
*colors
+c
] = image
[soff
][c
];
8277 if (output_bps
== 16 && !output_tiff
&& htons(0x55aa) != 0x55aa)
8278 swab ((char*)ppm2
, (char*)ppm2
, width
*colors
*2);
8279 fwrite (ppm
, colors
*output_bps
/8, width
, ofp
);