1 #define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
2 #define _XOPEN_SOURCE 500
3 /* Make sure putenv is in stdlib.h, strcaseeq is in nstring.h */
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include <assert.h>
8 #include <sys/types.h>
9
10 #ifdef HAVE_JPEG
11 #include <jpeglib.h>
12 #endif
13
14 #include "pm_config.h"
15 #include "pm.h"
16 #include "mallocvar.h"
17 #include "pm_c_util.h"
18 #include "nstring.h"
19
20 #include "global_variables.h"
21 #include "cameratopam.h"
22 #include "util.h"
23 #include "decode.h"
24 #include "bayer.h"
25 #include "ljpeg.h"
26 #include "dng.h"
27 #include "stdio_nofail.h"
28
29 #include "camera.h"
30
31 #if HAVE_INT64
32 typedef int64_t INT64;
33 static bool const have64BitArithmetic = true;
34 #else
35 /* We define INT64 to something that lets the code compile, but we
36 should not execute any INT64 code, because it will get the wrong
37 result.
38 */
39 typedef int INT64;
40 static bool const have64BitArithmetic = false;
41 #endif
42
43 #ifndef LONG_BIT
44 #define LONG_BIT (8 * sizeof (long))
45 #endif
46
47 #define FORC3 for (c=0; c < 3; c++)
48 #define FORC4 for (c=0; c < colors; c++)
49
50 static void
merror(const void * ptr,const char * where)51 merror (const void *ptr, const char *where)
52 {
53 if (ptr == NULL)
54 pm_error ("Out of memory in %s", where);
55 }
56
57
58
59
60 static void
adobeCopyPixel(Image const image,unsigned int const row,unsigned int const col,unsigned short ** const rp,bool const useSecondary)61 adobeCopyPixel(Image const image,
62 unsigned int const row,
63 unsigned int const col,
64 unsigned short ** const rp,
65 bool const useSecondary) {
66
67 unsigned r=row, c=col;
68
69 if (fuji_secondary && useSecondary)
70 ++(*rp);
71 if (filters) {
72 if (fuji_width) {
73 r = row + fuji_width - 1 - (col >> 1);
74 c = row + ((col+1) >> 1);
75 }
76 if (r < height && c < width)
77 BAYER(r,c) = **rp < 0x1000 ? curve[**rp] : **rp;
78 *rp += 1 + fuji_secondary;
79 } else {
80 unsigned int c;
81 for (c = 0; c < tiff_samples; ++c) {
82 image[row*width+col][c] = **rp < 0x1000 ? curve[**rp] : **rp;
83 ++(*rp);
84 }
85 }
86 if (fuji_secondary && useSecondary)
87 --(*rp);
88 }
89
90 void
adobe_dng_load_raw_lj(Image const image)91 adobe_dng_load_raw_lj(Image const image) {
92
93 int save, twide, trow=0, tcol=0, jrow, jcol;
94 struct jhead jh;
95 unsigned short *rp;
96
97 while (1) {
98 save = ftell_nofail(ifp);
99 fseek_nofail (ifp, get4(ifp), SEEK_SET);
100 if (!ljpeg_start (ifp, &jh)) break;
101 if (trow >= raw_height) break;
102 if (jh.high > raw_height-trow)
103 jh.high = raw_height-trow;
104 twide = jh.wide;
105 if (filters) twide *= jh.clrs;
106 else colors = jh.clrs;
107 if (fuji_secondary) twide /= 2;
108 if (twide > raw_width-tcol)
109 twide = raw_width-tcol;
110
111 for (jrow=0; jrow < jh.high; jrow++) {
112 ljpeg_row(ifp, &jh);
113 for (rp=jh.row, jcol=0; jcol < twide; jcol++)
114 adobeCopyPixel(image,
115 trow+jrow, tcol+jcol, &rp, use_secondary);
116 }
117 fseek_nofail (ifp, save+4, SEEK_SET);
118 if ((tcol += twide) >= raw_width) {
119 tcol = 0;
120 trow += jh.high;
121 }
122 free (jh.row);
123 }
124 }
125
126
127
128 void
adobe_dng_load_raw_nc(Image const image)129 adobe_dng_load_raw_nc(Image const image) {
130
131 unsigned short *pixel, *rp;
132 int row, col;
133
134 pixel = calloc (raw_width * tiff_samples, sizeof *pixel);
135 merror (pixel, "adobe_dng_load_raw_nc()");
136 for (row=0; row < raw_height; row++) {
137 read_shorts (ifp, pixel, raw_width * tiff_samples);
138 for (rp=pixel, col=0; col < raw_width; col++)
139 adobeCopyPixel(image, row, col, &rp, use_secondary);
140 }
141 free (pixel);
142 }
143
144
145
146 static int nikon_curve_offset;
147
148 void
nikon_compressed_load_raw(Image const image)149 nikon_compressed_load_raw(Image const image) {
150
151 static const unsigned char nikon_tree[] = {
152 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0,
153 5,4,3,6,2,7,1,0,8,9,11,10,12
154 };
155 int csize, row, col, i, diff;
156 unsigned short vpred[4], hpred[2], *curve;
157
158 init_decoder();
159 make_decoder (nikon_tree, 0);
160
161 fseek_nofail (ifp, nikon_curve_offset, SEEK_SET);
162 read_shorts (ifp, vpred, 4);
163 csize = get2(ifp);
164 curve = calloc (csize, sizeof *curve);
165 merror (curve, "nikon_compressed_load_raw()");
166 read_shorts (ifp, curve, csize);
167
168 fseek_nofail (ifp, data_offset, SEEK_SET);
169 getbits(ifp, -1);
170
171 for (row=0; row < height; row++)
172 for (col=0; col < raw_width; col++)
173 {
174 diff = ljpeg_diff (ifp, first_decode);
175 if (col < 2) {
176 i = 2*(row & 1) + (col & 1);
177 vpred[i] += diff;
178 hpred[col] = vpred[i];
179 } else
180 hpred[col & 1] += diff;
181 if ((unsigned) (col-left_margin) >= width) continue;
182 diff = hpred[col & 1];
183 if (diff >= csize) diff = csize-1;
184 BAYER(row,col-left_margin) = curve[diff];
185 }
186 maximum = curve[csize-1];
187 free (curve);
188 }
189
190 void
nikon_load_raw(Image const image)191 nikon_load_raw(Image const image) {
192
193 int irow, row, col, i;
194
195 getbits(ifp, -1);
196 for (irow=0; irow < height; irow++) {
197 row = irow;
198 if (model[0] == 'E') {
199 row = irow * 2 % height + irow / (height/2);
200 if (row == 1 && atoi(model+1) < 5000) {
201 fseek_nofail (ifp, 0, SEEK_END);
202 fseek_nofail (ifp, ftell_nofail(ifp)/2, SEEK_SET);
203 getbits(ifp, -1);
204 }
205 }
206 for (col=0; col < raw_width; col++) {
207 i = getbits(ifp, 12);
208 if ((unsigned) (col-left_margin) < width)
209 BAYER(row,col-left_margin) = i;
210 if (tiff_data_compression == 34713 && (col % 10) == 9)
211 getbits(ifp, 8);
212 }
213 }
214 }
215
216 /*
217 Figure out if a NEF file is compressed. These fancy heuristics
218 are only needed for the D100, thanks to a bug in some cameras
219 that tags all images as "compressed".
220 */
221 int
nikon_is_compressed()222 nikon_is_compressed()
223 {
224 unsigned char test[256];
225 int i;
226
227 if (tiff_data_compression != 34713)
228 return 0;
229 if (strcmp(model,"D100"))
230 return 1;
231 fseek_nofail (ifp, data_offset, SEEK_SET);
232 fread_nofail (test, 1, 256, ifp);
233 for (i=15; i < 256; i+=16)
234 if (test[i]) return 1;
235 return 0;
236 }
237
238 /*
239 Returns 1 for a Coolpix 990, 0 for a Coolpix 995.
240 */
241 int
nikon_e990()242 nikon_e990()
243 {
244 int i, histo[256];
245 const unsigned char often[] = { 0x00, 0x55, 0xaa, 0xff };
246
247 memset (histo, 0, sizeof histo);
248 fseek_nofail (ifp, 2064*1540*3/4, SEEK_SET);
249 for (i=0; i < 2000; i++)
250 histo[fgetc_nofail(ifp)]++;
251 for (i=0; i < 4; i++)
252 if (histo[often[i]] > 400)
253 return 1;
254 return 0;
255 }
256
257 /*
258 Returns 1 for a Coolpix 2100, 0 for anything else.
259 */
260 int
nikon_e2100()261 nikon_e2100()
262 {
263 unsigned char t[12];
264 int i;
265
266 fseek_nofail (ifp, 0, SEEK_SET);
267 for (i=0; i < 1024; i++) {
268 fread_nofail (t, 1, 12, ifp);
269 if (((t[2] & t[4] & t[7] & t[9]) >> 4
270 & t[1] & t[6] & t[8] & t[11] & 3) != 3)
271 return 0;
272 }
273 return 1;
274 }
275
276 /*
277 Separates a Pentax Optio 33WR from a Nikon E3700.
278 */
279 int
pentax_optio33()280 pentax_optio33()
281 {
282 int i, sum[] = { 0, 0 };
283 unsigned char tail[952];
284
285 fseek_nofail (ifp, -sizeof tail, SEEK_END);
286 fread_nofail (tail, 1, sizeof tail, ifp);
287 for (i=0; i < sizeof tail; i++)
288 sum[(i>>2) & 1] += tail[i];
289 return sum[0] < sum[1]*4;
290 }
291
292 /*
293 Separates a Minolta DiMAGE Z2 from a Nikon E4300.
294 */
295 int
minolta_z2()296 minolta_z2()
297 {
298 int i;
299 char tail[424];
300
301 fseek_nofail (ifp, -sizeof tail, SEEK_END);
302 fread_nofail (tail, 1, sizeof tail, ifp);
303 for (i=0; i < sizeof tail; i++)
304 if (tail[i]) return 1;
305 return 0;
306 }
307
308 void
nikon_e2100_load_raw(Image const image)309 nikon_e2100_load_raw(Image const image) {
310
311 unsigned char data[3432], *dp;
312 unsigned short pixel[2288], *pix;
313 int row, col;
314
315 for (row=0; row <= height; row+=2) {
316 if (row == height) {
317 fseek_nofail (ifp, ((width==1616) << 13) - (-ftell_nofail(ifp) & -2048), SEEK_SET);
318 row = 1;
319 }
320 fread_nofail (data, 1, width*3/2, ifp);
321 for (dp=data, pix=pixel; pix < pixel+width; dp+=12, pix+=8) {
322 pix[0] = (dp[2] >> 4) + (dp[ 3] << 4);
323 pix[1] = (dp[2] << 8) + dp[ 1];
324 pix[2] = (dp[7] >> 4) + (dp[ 0] << 4);
325 pix[3] = (dp[7] << 8) + dp[ 6];
326 pix[4] = (dp[4] >> 4) + (dp[ 5] << 4);
327 pix[5] = (dp[4] << 8) + dp[11];
328 pix[6] = (dp[9] >> 4) + (dp[10] << 4);
329 pix[7] = (dp[9] << 8) + dp[ 8];
330 }
331 for (col=0; col < width; col++)
332 BAYER(row,col) = (pixel[col] & 0xfff);
333 }
334 }
335
336 void
nikon_e950_load_raw(Image const image)337 nikon_e950_load_raw(Image const image) {
338
339 int irow, row, col;
340
341 getbits(ifp, -1);
342 for (irow=0; irow < height; irow++) {
343 row = irow * 2 % height;
344 for (col=0; col < width; col++)
345 BAYER(row,col) = getbits(ifp, 10);
346 for (col=28; col--; )
347 getbits(ifp, 8);
348 }
349 maximum = 0x3dd;
350 }
351
352 /*
353 The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
354 */
355 void
fuji_s2_load_raw(Image const image)356 fuji_s2_load_raw(Image const image) {
357 unsigned short pixel[2944];
358 int row, col, r, c;
359
360 fseek_nofail (ifp, (2944*24+32)*2, SEEK_CUR);
361 for (row=0; row < 2144; row++) {
362 read_shorts(ifp, pixel, 2944);
363 for (col=0; col < 2880; col++) {
364 r = row + ((col+1) >> 1);
365 c = 2143 - row + (col >> 1);
366 BAYER(r,c) = pixel[col];
367 }
368 }
369 }
370
371 void
fuji_s3_load_raw(Image const image)372 fuji_s3_load_raw(Image const image) {
373 unsigned short pixel[4352];
374 int row, col, r, c;
375
376 fseek_nofail (ifp, (4352*2+32)*2, SEEK_CUR);
377 for (row=0; row < 1440; row++) {
378 read_shorts(ifp, pixel, 4352);
379 for (col=0; col < 4288; col++) {
380 r = 2143 + row - (col >> 1);
381 c = row + ((col+1) >> 1);
382 BAYER(r,c) = pixel[col];
383 }
384 }
385 }
386
387 static void
fuji_common_load_raw(Image const image,unsigned int const ncol,unsigned int const icol,unsigned int const nrow)388 fuji_common_load_raw(Image const image,
389 unsigned int const ncol,
390 unsigned int const icol,
391 unsigned int const nrow) {
392
393 unsigned short pixel[2048];
394 unsigned int row;
395
396 for (row = 0; row < nrow; ++row) {
397 unsigned int col;
398 read_shorts(ifp, pixel, ncol);
399 for (col = 0; col <= icol; ++col) {
400 int const r = icol - col + (row >> 1);
401 int const c = col + ((row+1) >> 1);
402 BAYER(r,c) = pixel[col];
403 }
404 }
405 }
406
407
408
409 void
fuji_s5000_load_raw(Image const image)410 fuji_s5000_load_raw(Image const image) {
411
412 fseek_nofail (ifp, (1472*4+24)*2, SEEK_CUR);
413 fuji_common_load_raw(image, 1472, 1423, 2152);
414 }
415
416
417
418 void
fuji_s7000_load_raw(Image const image)419 fuji_s7000_load_raw(Image const image) {
420
421 fuji_common_load_raw(image, 2048, 2047, 3080);
422 }
423
424
425
426 /*
427 The Fuji Super CCD SR has two photodiodes for each pixel.
428 The secondary has about 1/16 the sensitivity of the primary,
429 but this ratio may vary.
430 */
431 void
fuji_f700_load_raw(Image const image)432 fuji_f700_load_raw(Image const image) {
433 unsigned short pixel[2944];
434 int row, col, r, c, val;
435
436 for (row=0; row < 2168; row++) {
437 read_shorts(ifp, pixel, 2944);
438 for (col=0; col < 1440; col++) {
439 r = 1439 - col + (row >> 1);
440 c = col + ((row+1) >> 1);
441 val = pixel[col+16 + use_secondary*1472];
442 BAYER(r,c) = val;
443 }
444 }
445 }
446
447 void
rollei_load_raw(Image const image)448 rollei_load_raw(Image const image) {
449 unsigned char pixel[10];
450 unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
451
452 isix = raw_width * raw_height * 5 / 8;
453 while (fread_or_eof_nofail (pixel, 1, 10, ifp) == 10) {
454 for (i=0; i < 10; i+=2) {
455 todo[i] = iten++;
456 todo[i+1] = pixel[i] << 8 | pixel[i+1];
457 buffer = pixel[i] >> 2 | buffer << 6;
458 }
459 for ( ; i < 16; i+=2) {
460 todo[i] = isix++;
461 todo[i+1] = buffer >> (14-i)*5;
462 }
463 for (i=0; i < 16; i+=2) {
464 row = todo[i] / raw_width - top_margin;
465 col = todo[i] % raw_width - left_margin;
466 if (row < height && col < width)
467 BAYER(row,col) = (todo[i+1] & 0x3ff);
468 }
469 }
470 maximum = 0x3ff;
471 }
472
473 void
phase_one_load_raw(Image const image)474 phase_one_load_raw(Image const image) {
475 int row, col, a, b;
476 unsigned short *pixel, akey, bkey;
477
478 fseek_nofail (ifp, 8, SEEK_CUR);
479 fseek_nofail (ifp, get4(ifp) + 296, SEEK_CUR);
480 akey = get2(ifp);
481 bkey = get2(ifp);
482 fseek_nofail (ifp, data_offset + 12 + top_margin*raw_width*2, SEEK_SET);
483 pixel = calloc (raw_width, sizeof *pixel);
484 merror (pixel, "phase_one_load_raw()");
485 for (row=0; row < height; row++) {
486 read_shorts(ifp, pixel, raw_width);
487 for (col=0; col < raw_width; col+=2) {
488 a = pixel[col+0] ^ akey;
489 b = pixel[col+1] ^ bkey;
490 pixel[col+0] = (b & 0xaaaa) | (a & 0x5555);
491 pixel[col+1] = (a & 0xaaaa) | (b & 0x5555);
492 }
493 for (col=0; col < width; col++)
494 BAYER(row,col) = pixel[col+left_margin];
495 }
496 free (pixel);
497 }
498
499 void
ixpress_load_raw(Image const image)500 ixpress_load_raw(Image const image) {
501 unsigned short pixel[4090];
502 int row, col;
503
504 order = 0x4949;
505 fseek_nofail (ifp, 304 + 6*2*4090, SEEK_SET);
506 for (row=height; --row >= 0; ) {
507 read_shorts(ifp, pixel, 4090);
508 for (col=0; col < width; col++)
509 BAYER(row,col) = pixel[width-1-col];
510 }
511 }
512
513 void
leaf_load_raw(Image const image)514 leaf_load_raw(Image const image) {
515 unsigned short *pixel;
516 int r, c, row, col;
517
518 pixel = calloc (raw_width, sizeof *pixel);
519 merror (pixel, "leaf_load_raw()");
520 for (r=0; r < height-32; r+=32)
521 FORC3 for (row=r; row < r+32; row++) {
522 read_shorts(ifp, pixel, raw_width);
523 for (col=0; col < width; col++)
524 image[row*width+col][c] = pixel[col];
525 }
526 free (pixel);
527 }
528
529 /*
530 For this function only, raw_width is in bytes, not pixels!
531 */
532 void
packed_12_load_raw(Image const image)533 packed_12_load_raw(Image const image) {
534 int row, col;
535
536 getbits(ifp, -1);
537 for (row=0; row < height; row++) {
538 for (col=0; col < width; col++)
539 BAYER(row,col) = getbits(ifp, 12);
540 for (col = width*3/2; col < raw_width; col++)
541 getbits(ifp, 8);
542 }
543 }
544
545 void
unpacked_load_raw(Image const image)546 unpacked_load_raw(Image const image) {
547 unsigned short *pixel;
548 int row, col;
549
550 pixel = calloc (raw_width, sizeof *pixel);
551 merror (pixel, "unpacked_load_raw()");
552 for (row=0; row < height; row++) {
553 read_shorts(ifp, pixel, raw_width);
554 for (col=0; col < width; col++)
555 BAYER(row,col) = pixel[col];
556 }
557 free (pixel);
558 }
559
560 void
olympus_e300_load_raw(Image const image)561 olympus_e300_load_raw(Image const image) {
562 unsigned char *data, *dp;
563 unsigned short *pixel, *pix;
564 int dwide, row, col;
565
566 dwide = raw_width * 16 / 10;
567 data = malloc (dwide + raw_width*2);
568 merror (data, "olympus_e300_load_raw()");
569 pixel = (unsigned short *) (data + dwide);
570 for (row=0; row < height; row++) {
571 fread_nofail (data, 1, dwide, ifp);
572 for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=3, pix+=2) {
573 if (((dp-data) & 15) == 15) dp++;
574 pix[0] = dp[1] << 8 | dp[0];
575 pix[1] = dp[2] << 4 | dp[1] >> 4;
576 }
577 for (col=0; col < width; col++)
578 BAYER(row,col) = (pixel[col] & 0xfff);
579 }
580 free (data);
581 }
582
583 void
olympus_cseries_load_raw(Image const image)584 olympus_cseries_load_raw(Image const image) {
585 int irow, row, col;
586
587 for (irow=0; irow < height; irow++) {
588 row = irow * 2 % height + irow / (height/2);
589 if (row < 2) {
590 fseek_nofail (ifp, data_offset - row*(-width*height*3/4 & -2048), SEEK_SET);
591 getbits(ifp, -1);
592 }
593 for (col=0; col < width; col++)
594 BAYER(row,col) = getbits(ifp, 12);
595 }
596 }
597
598 void
eight_bit_load_raw(Image const image)599 eight_bit_load_raw(Image const image) {
600 unsigned char *pixel;
601 int row, col;
602
603 pixel = calloc (raw_width, sizeof *pixel);
604 merror (pixel, "eight_bit_load_raw()");
605 for (row=0; row < height; row++) {
606 fread_nofail (pixel, 1, raw_width, ifp);
607 for (col=0; col < width; col++)
608 BAYER(row,col) = pixel[col];
609 }
610 free (pixel);
611 maximum = 0xff;
612 }
613
614 void
casio_qv5700_load_raw(Image const image)615 casio_qv5700_load_raw(Image const image) {
616 unsigned char data[3232], *dp;
617 unsigned short pixel[2576], *pix;
618 int row, col;
619
620 for (row=0; row < height; row++) {
621 fread_nofail (data, 1, 3232, ifp);
622 for (dp=data, pix=pixel; dp < data+3220; dp+=5, pix+=4) {
623 pix[0] = (dp[0] << 2) + (dp[1] >> 6);
624 pix[1] = (dp[1] << 4) + (dp[2] >> 4);
625 pix[2] = (dp[2] << 6) + (dp[3] >> 2);
626 pix[3] = (dp[3] << 8) + (dp[4] );
627 }
628 for (col=0; col < width; col++)
629 BAYER(row,col) = (pixel[col] & 0x3ff);
630 }
631 maximum = 0x3fc;
632 }
633
634 void
nucore_load_raw(Image const image)635 nucore_load_raw(Image const image) {
636 unsigned short *pixel;
637 int irow, row, col;
638
639 pixel = calloc (width, 2);
640 merror (pixel, "nucore_load_raw()");
641 for (irow=0; irow < height; irow++) {
642 read_shorts(ifp, pixel, width);
643 row = irow/2 + height/2 * (irow & 1);
644 for (col=0; col < width; col++)
645 BAYER(row,col) = pixel[col];
646 }
647 free (pixel);
648 }
649
radc_token(int tree)650 static int radc_token (int tree)
651 {
652 int t;
653 static struct decode *dstart[18], *dindex;
654 static const int *s, source[] = {
655 1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
656 1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
657 2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
658 2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
659 2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
660 2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
661 2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
662 2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
663 2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
664 2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
665 1,0, 2,2, 2,-2,
666 1,-3, 1,3,
667 2,-17, 2,-5, 2,5, 2,17,
668 2,-7, 2,2, 2,9, 2,18,
669 2,-18, 2,-9, 2,-2, 2,7,
670 2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
671 2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
672 2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
673 };
674
675 if (free_decode == first_decode)
676 for (s=source, t=0; t < 18; t++) {
677 dstart[t] = free_decode;
678 s = make_decoder_int (s, 0);
679 }
680 if (tree == 18) {
681 if (model[2] == '4')
682 return (getbits(ifp, 5) << 3) + 4; /* DC40 */
683 else
684 return (getbits(ifp, 6) << 2) + 2; /* DC50 */
685 }
686 for (dindex = dstart[tree]; dindex->branch[0]; )
687 dindex = dindex->branch[getbits(ifp, 1)];
688 return dindex->leaf;
689 }
690
691 #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
692
693 #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
694 : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
695
696 void
kodak_radc_load_raw(Image const image)697 kodak_radc_load_raw(Image const image) {
698 int row, col, tree, nreps, rep, step, c, s, r, x, y, val;
699 unsigned int i;
700 short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
701
702 init_decoder();
703 getbits(ifp, -1);
704 for (i = 0; i < ARRAY_SIZE(buf); ++i) {
705 unsigned int j;
706 for (j = 0; j < ARRAY_SIZE(buf[0]); ++j) {
707 unsigned int k;
708 for (k = 0; k < ARRAY_SIZE(buf[0][0]); ++k)
709 buf[i][j][k] = 2048;
710 }
711 }
712 for (row=0; row < height; row+=4) {
713 unsigned int i;
714 for (i = 0; i < 3; ++i)
715 mul[i] = getbits(ifp, 6);
716 FORC3 {
717 val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
718 s = val > 65564 ? 10:12;
719 x = ~(-1 << (s-1));
720 val <<= 12-s;
721 for (i=0; i < ARRAY_SIZE(buf[c][0]); i++)
722 buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
723 last[c] = mul[c];
724 for (r=0; r <= !c; r++) {
725 buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
726 for (tree=1, col=width/2; col > 0; ) {
727 if ((tree = radc_token(tree))) {
728 col -= 2;
729 if (tree == 8)
730 FORYX buf[c][y][x] =
731 radc_token(tree+10) * mul[c];
732 else
733 FORYX buf[c][y][x] =
734 radc_token(tree+10) * 16 + PREDICTOR;
735 } else
736 do {
737 nreps = (col > 2) ? radc_token(9) + 1 : 1;
738 for (rep=0;
739 rep < 8 && rep < nreps && col > 0;
740 rep++) {
741 col -= 2;
742 FORYX buf[c][y][x] = PREDICTOR;
743 if (rep & 1) {
744 step = radc_token(10) << 4;
745 FORYX buf[c][y][x] += step;
746 }
747 }
748 } while (nreps == 9);
749 }
750 for (y=0; y < 2; y++)
751 for (x=0; x < width/2; x++) {
752 val = (buf[c][y+1][x] << 4) / mul[c];
753 if (val < 0) val = 0;
754 if (c)
755 BAYER(row+y*2+c-1,x*2+2-c) = val;
756 else
757 BAYER(row+r*2+y,x*2+y) = val;
758 }
759 memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
760 }
761 }
762 for (y=row; y < row+4; y++)
763 for (x=0; x < width; x++)
764 if ((x+y) & 1) {
765 val = (BAYER(y,x)-2048)*2 + (BAYER(y,x-1)+BAYER(y,x+1))/2;
766 if (val < 0) val = 0;
767 BAYER(y,x) = val;
768 }
769 }
770 maximum = 0x1fff; /* wild guess */
771 }
772
773 #undef FORYX
774 #undef PREDICTOR
775
776 #ifndef HAVE_JPEG
777 void
kodak_jpeg_load_raw(Image const Image)778 kodak_jpeg_load_raw(Image const Image) {}
779 #else
780
781 static bool
fill_input_buffer(j_decompress_ptr cinfo)782 fill_input_buffer (j_decompress_ptr cinfo)
783 {
784 static char jpeg_buffer[4096];
785 size_t nbytes;
786
787 nbytes = fread_or_eof_nofail (jpeg_buffer, 1, 4096, ifp);
788 swab (jpeg_buffer, jpeg_buffer, nbytes);
789 cinfo->src->next_input_byte = jpeg_buffer;
790 cinfo->src->bytes_in_buffer = nbytes;
791 return TRUE;
792 }
793
794 void
kodak_jpeg_load_raw(Image const image)795 kodak_jpeg_load_raw(Image const image)
796 {
797 struct jpeg_decompress_struct cinfo;
798 struct jpeg_error_mgr jerr;
799 JSAMPARRAY buf;
800 JSAMPLE (*pixel)[3];
801 int row, col;
802
803 cinfo.err = jpeg_std_error (&jerr);
804 jpeg_create_decompress (&cinfo);
805 jpeg_stdio_src (&cinfo, ifp);
806 cinfo.src->fill_input_buffer = fill_input_buffer;
807 jpeg_read_header (&cinfo, TRUE);
808 jpeg_start_decompress (&cinfo);
809 if ((cinfo.output_width != width ) ||
810 (cinfo.output_height*2 != height ) ||
811 (cinfo.output_components != 3 )) {
812 pm_error ("incorrect JPEG dimensions");
813 }
814 buf = (*cinfo.mem->alloc_sarray)
815 ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
816
817 while (cinfo.output_scanline < cinfo.output_height) {
818 row = cinfo.output_scanline * 2;
819 jpeg_read_scanlines (&cinfo, buf, 1);
820 pixel = (void *) buf[0];
821 for (col=0; col < width; col+=2) {
822 BAYER(row+0,col+0) = pixel[col+0][1] << 1;
823 BAYER(row+1,col+1) = pixel[col+1][1] << 1;
824 BAYER(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
825 BAYER(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
826 }
827 }
828 jpeg_finish_decompress (&cinfo);
829 jpeg_destroy_decompress (&cinfo);
830 maximum = 0xff << 1;
831 }
832
833 #endif
834
835 void
kodak_dc120_load_raw(Image const image)836 kodak_dc120_load_raw(Image const image)
837 {
838 static const int mul[4] = { 162, 192, 187, 92 };
839 static const int add[4] = { 0, 636, 424, 212 };
840 unsigned char pixel[848];
841 int row, shift, col;
842
843 for (row=0; row < height; row++) {
844 fread_nofail (pixel, 848, 1, ifp);
845 shift = row * mul[row & 3] + add[row & 3];
846 for (col=0; col < width; col++)
847 BAYER(row,col) = (unsigned short) pixel[(col + shift) % 848];
848 }
849 maximum = 0xff;
850 }
851
852 void
kodak_dc20_coeff(float const juice)853 kodak_dc20_coeff (float const juice)
854 {
855 static const float my_coeff[3][4] =
856 { { 2.25, 0.75, -1.75, -0.25 },
857 { -0.25, 0.75, 0.75, -0.25 },
858 { -0.25, -1.75, 0.75, 2.25 } };
859 static const float flat[3][4] =
860 { { 1, 0, 0, 0 },
861 { 0, 0.5, 0.5, 0 },
862 { 0, 0, 0, 1 } };
863 int r, g;
864
865 for (r=0; r < 3; r++)
866 for (g=0; g < 4; g++)
867 coeff[r][g] = my_coeff[r][g] * juice + flat[r][g] * (1-juice);
868 use_coeff = 1;
869 }
870
871 void
kodak_easy_load_raw(Image const image)872 kodak_easy_load_raw(Image const image)
873 {
874 unsigned char *pixel;
875 unsigned row, col, icol;
876
877 if (raw_width > width)
878 black = 0;
879 pixel = calloc (raw_width, sizeof *pixel);
880 merror (pixel, "kodak_easy_load_raw()");
881 for (row=0; row < height; row++) {
882 fread_nofail (pixel, 1, raw_width, ifp);
883 for (col=0; col < raw_width; col++) {
884 icol = col - left_margin;
885 if (icol < width)
886 BAYER(row,icol) = (unsigned short) curve[pixel[col]];
887 else
888 black += curve[pixel[col]];
889 }
890 }
891 free (pixel);
892 if (raw_width > width)
893 black /= (raw_width - width) * height;
894 if (!strncmp(model,"DC2",3))
895 black = 0;
896 maximum = curve[0xff];
897 }
898
899 void
kodak_compressed_load_raw(Image const image)900 kodak_compressed_load_raw(Image const image)
901 {
902 unsigned char c, blen[256];
903 unsigned short raw[6];
904 unsigned row, col, len, save, i, israw=0, bits=0, pred[2];
905 INT64 bitbuf=0;
906 int diff;
907
908 assert(have64BitArithmetic);
909
910 for (row=0; row < height; row++)
911 for (col=0; col < width; col++)
912 {
913 if ((col & 255) == 0) { /* Get the bit-lengths of the */
914 len = width - col; /* next 256 pixel values */
915 if (len > 256) len = 256;
916 save = ftell_nofail(ifp);
917 for (israw=i=0; i < len; i+=2) {
918 c = fgetc_nofail(ifp);
919 if ((blen[i+0] = c & 15) > 12 ||
920 (blen[i+1] = c >> 4) > 12 )
921 israw = 1;
922 }
923 bitbuf = bits = pred[0] = pred[1] = 0;
924 if (len % 8 == 4) {
925 bitbuf = fgetc_nofail(ifp) << 8;
926 bitbuf += fgetc_nofail(ifp);
927 bits = 16;
928 }
929 if (israw)
930 fseek_nofail (ifp, save, SEEK_SET);
931 }
932 if (israw) { /* If the data is not compressed */
933 switch (col & 7) {
934 case 0:
935 read_shorts(ifp, raw, 6);
936 diff = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
937 break;
938 case 1:
939 diff = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
940 break;
941 default:
942 diff = raw[(col & 7) - 2] & 0xfff;
943 }
944 } else { /* If the data is compressed */
945 len = blen[col & 255]; /* Number of bits for this pixel */
946 if (bits < len) { /* Got enough bits in the buffer? */
947 for (i=0; i < 32; i+=8)
948 bitbuf += (INT64) fgetc_nofail(ifp) << (bits+(i^8));
949 bits += 32;
950 }
951 diff = bitbuf & (0xffff >> (16-len)); /* Pull bits from buffer */
952 bitbuf >>= len;
953 bits -= len;
954 if ((diff & (1 << (len-1))) == 0)
955 diff -= (1 << len) - 1;
956 pred[col & 1] += diff;
957 diff = pred[col & 1];
958 }
959 BAYER(row,col) = curve[diff];
960 }
961 }
962
963 void
kodak_yuv_load_raw(Image const image)964 kodak_yuv_load_raw(Image const image)
965 {
966 unsigned char c, blen[384];
967 unsigned row, col, len, bits=0;
968 INT64 bitbuf=0;
969 int i, li=0, si, diff, six[6], y[4], cb=0, cr=0, rgb[3];
970 unsigned short *ip;
971
972 assert(have64BitArithmetic);
973
974 for (row=0; row < height; row+=2)
975 for (col=0; col < width; col+=2) {
976 if ((col & 127) == 0) {
977 len = (width - col + 1) * 3 & -4;
978 if (len > 384) len = 384;
979 for (i=0; i < len; ) {
980 c = fgetc_nofail(ifp);
981 blen[i++] = c & 15;
982 blen[i++] = c >> 4;
983 }
984 li = bitbuf = bits = y[1] = y[3] = cb = cr = 0;
985 if (len % 8 == 4) {
986 bitbuf = fgetc_nofail(ifp) << 8;
987 bitbuf += fgetc_nofail(ifp);
988 bits = 16;
989 }
990 }
991 for (si=0; si < 6; si++) {
992 len = blen[li++];
993 if (bits < len) {
994 for (i=0; i < 32; i+=8)
995 bitbuf += (INT64) fgetc_nofail(ifp) << (bits+(i^8));
996 bits += 32;
997 }
998 diff = bitbuf & (0xffff >> (16-len));
999 bitbuf >>= len;
1000 bits -= len;
1001 if ((diff & (1 << (len-1))) == 0)
1002 diff -= (1 << len) - 1;
1003 six[si] = diff;
1004 }
1005 y[0] = six[0] + y[1];
1006 y[1] = six[1] + y[0];
1007 y[2] = six[2] + y[3];
1008 y[3] = six[3] + y[2];
1009 cb += six[4];
1010 cr += six[5];
1011 for (i=0; i < 4; i++) {
1012 ip = image[(row+(i >> 1))*width + col+(i & 1)];
1013 rgb[0] = y[i] + cr;
1014 rgb[1] = y[i];
1015 rgb[2] = y[i] + cb;
1016 FORC3 if (rgb[c] > 0) ip[c] = curve[rgb[c]];
1017 }
1018 }
1019 maximum = 0xe74;
1020 }
1021
sony_decrypt(unsigned * data,int len,int start,int key)1022 static void sony_decrypt (unsigned *data, int len, int start, int key)
1023 {
1024 static uint32_t pad[128];
1025 unsigned int p;
1026 unsigned int i;
1027
1028 if (start) {
1029 for (p=0; p < 4; p++)
1030 pad[p] = key = key * 48828125 + 1;
1031 pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
1032 for (p=4; p < 127; p++)
1033 pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
1034
1035 /* Convert to big-endian */
1036
1037 for (p=0; p < 127; p++) {
1038 union {
1039 unsigned char bytes[4];
1040 uint32_t word;
1041 } u;
1042
1043 u.bytes[0] = pad[p] >> 24;
1044 u.bytes[1] = pad[p] >> 16;
1045 u.bytes[2] = pad[p] >> 8;
1046 u.bytes[3] = pad[p] >> 0;
1047
1048 pad[p] = u.word;
1049 }
1050 }
1051 for (i = 0, p = 0; i < len; ++i, ++p) {
1052 *data++ ^= pad[p & 0x7f] = pad[(p+1) & 0x7f] ^ pad[(p+65) & 0x7f];
1053 }
1054 }
1055
1056 void
sony_load_raw(Image const image)1057 sony_load_raw(Image const image)
1058 {
1059 unsigned char head[40];
1060 struct pixel {
1061 unsigned char bytes[2];
1062 };
1063 struct pixel * pixelrow;
1064 unsigned i, key, row, col;
1065
1066 fseek_nofail (ifp, 200896, SEEK_SET);
1067 fseek_nofail (ifp, (unsigned) fgetc_nofail(ifp)*4 - 1, SEEK_CUR);
1068 order = 0x4d4d;
1069 key = get4(ifp);
1070 fseek_nofail (ifp, 164600, SEEK_SET);
1071 fread_nofail (head, 1, 40, ifp);
1072 sony_decrypt ((void *) head, 10, 1, key);
1073 for (i=26; i-- > 22; )
1074 key = key << 8 | head[i];
1075 fseek_nofail (ifp, data_offset, SEEK_SET);
1076 MALLOCARRAY(pixelrow, raw_width);
1077 merror (pixelrow, "sony_load_raw()");
1078 for (row=0; row < height; row++) {
1079 fread_nofail (pixelrow, 2, raw_width, ifp);
1080 sony_decrypt ((void *) pixelrow, raw_width/2, !row, key);
1081 for (col=9; col < left_margin; col++)
1082 black += pixelrow[col].bytes[0] * 256 + pixelrow[col].bytes[1];
1083 for (col=0; col < width; col++)
1084 BAYER(row,col) =
1085 pixelrow[col+left_margin].bytes[0] * 256 +
1086 pixelrow[col+left_margin].bytes[1];
1087 }
1088 free (pixelrow);
1089 if (left_margin > 9)
1090 black /= (left_margin-9) * height;
1091 maximum = 0x3ff0;
1092 }
1093
1094 void
parse_minolta(FILE * const ifp)1095 parse_minolta(FILE * const ifp)
1096 {
1097 int save, tag, len, offset, high=0, wide=0;
1098
1099 fseek_nofail (ifp, 4, SEEK_SET);
1100 offset = get4(ifp) + 8;
1101 while ((save=ftell_nofail(ifp)) < offset) {
1102 tag = get4(ifp);
1103 len = get4(ifp);
1104 switch (tag) {
1105 case 0x505244: /* PRD */
1106 fseek_nofail (ifp, 8, SEEK_CUR);
1107 high = get2(ifp);
1108 wide = get2(ifp);
1109 break;
1110 case 0x574247: /* WBG */
1111 get4(ifp);
1112 camera_red = get2(ifp);
1113 camera_red /= get2(ifp);
1114 camera_blue = get2(ifp);
1115 camera_blue = get2(ifp) / camera_blue;
1116 break;
1117 case 0x545457: /* TTW */
1118 parse_tiff(ifp, ftell_nofail(ifp));
1119 }
1120 fseek_nofail (ifp, save+len+8, SEEK_SET);
1121 }
1122 raw_height = high;
1123 raw_width = wide;
1124 data_offset = offset;
1125 }
1126
1127 /*
1128 CIFF block 0x1030 contains an 8x8 white sample.
1129 Load this into white[][] for use in scale_colors().
1130 */
ciff_block_1030()1131 static void ciff_block_1030()
1132 {
1133 static const unsigned short key[] = { 0x410, 0x45f3 };
1134 int i, bpp, row, col, vbits=0;
1135 unsigned long bitbuf=0;
1136
1137 get2(ifp);
1138 if (get4(ifp) != 0x80008) return;
1139 if (get4(ifp) == 0) return;
1140 bpp = get2(ifp);
1141 if (bpp != 10 && bpp != 12) return;
1142 for (i=row=0; row < 8; row++)
1143 for (col=0; col < 8; col++) {
1144 if (vbits < bpp) {
1145 bitbuf = bitbuf << 16 | (get2(ifp) ^ key[i++ & 1]);
1146 vbits += 16;
1147 }
1148 white[row][col] =
1149 bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp);
1150 vbits -= bpp;
1151 }
1152 }
1153
1154 /*
1155 Parse a CIFF file, better known as Canon CRW format.
1156 */
1157 void
parse_ciff(FILE * const ifp,int const offset,int const length)1158 parse_ciff(FILE * const ifp,
1159 int const offset,
1160 int const length)
1161 {
1162 int tboff, nrecs, i, type, len, roff, aoff, save, wbi=-1;
1163 static const int remap[] = { 1,2,3,4,5,1 };
1164 static const int remap_10d[] = { 0,1,3,4,5,6,0,0,2,8 };
1165 static const int remap_s70[] = { 0,1,2,9,4,3,6,7,8,9,10,0,0,0,7,0,0,8 };
1166 unsigned short key[] = { 0x410, 0x45f3 };
1167
1168 if (strcmp(model,"Canon PowerShot G6") &&
1169 strcmp(model,"Canon PowerShot S70") &&
1170 strcmp(model,"Canon PowerShot Pro1"))
1171 key[0] = key[1] = 0;
1172 fseek_nofail (ifp, offset+length-4, SEEK_SET);
1173 tboff = get4(ifp) + offset;
1174 fseek_nofail (ifp, tboff, SEEK_SET);
1175 nrecs = get2(ifp);
1176 for (i = 0; i < nrecs; i++) {
1177 type = get2(ifp);
1178 len = get4(ifp);
1179 roff = get4(ifp);
1180 aoff = offset + roff;
1181 save = ftell_nofail(ifp);
1182 if (type == 0x080a) { /* Get the camera make and model */
1183 fseek_nofail (ifp, aoff, SEEK_SET);
1184 fread_nofail (make, 64, 1, ifp);
1185 fseek_nofail (ifp, aoff+strlen(make)+1, SEEK_SET);
1186 fread_nofail (model, 64, 1, ifp);
1187 }
1188 if (type == 0x102a) { /* Find the White Balance index */
1189 fseek_nofail (ifp, aoff+14, SEEK_SET); /* 0=auto, 1=daylight, 2=cloudy ... */
1190 wbi = get2(ifp);
1191 if (((!strcmp(model,"Canon EOS DIGITAL REBEL") ||
1192 !strcmp(model,"Canon EOS 300D DIGITAL"))) && wbi == 6)
1193 wbi++;
1194 }
1195 if (type == 0x102c) { /* Get white balance (G2) */
1196 if (!strcmp(model,"Canon PowerShot G1") ||
1197 !strcmp(model,"Canon PowerShot Pro90 IS")) {
1198 fseek_nofail (ifp, aoff+120, SEEK_SET);
1199 white[0][1] = get2(ifp);
1200 white[0][0] = get2(ifp);
1201 white[1][0] = get2(ifp);
1202 white[1][1] = get2(ifp);
1203 } else {
1204 fseek_nofail (ifp, aoff+100, SEEK_SET);
1205 goto common;
1206 }
1207 }
1208 if (type == 0x0032) { /* Get white balance (D30 & G3) */
1209 if (!strcmp(model,"Canon EOS D30")) {
1210 fseek_nofail (ifp, aoff+72, SEEK_SET);
1211 common:
1212 camera_red = get2(ifp) ^ key[0];
1213 camera_red =(get2(ifp) ^ key[1]) / camera_red;
1214 camera_blue = get2(ifp) ^ key[0];
1215 camera_blue /= get2(ifp) ^ key[1];
1216 } else if (!strcmp(model,"Canon PowerShot G6") ||
1217 !strcmp(model,"Canon PowerShot S70")) {
1218 fseek_nofail (ifp, aoff+96 + remap_s70[wbi]*8, SEEK_SET);
1219 goto common;
1220 } else if (!strcmp(model,"Canon PowerShot Pro1")) {
1221 fseek_nofail (ifp, aoff+96 + wbi*8, SEEK_SET);
1222 goto common;
1223 } else {
1224 fseek_nofail (ifp, aoff+80 + (wbi < 6 ? remap[wbi]*8 : 0), SEEK_SET);
1225 if (!camera_red)
1226 goto common;
1227 }
1228 }
1229 if (type == 0x10a9) { /* Get white balance (D60) */
1230 if (!strcmp(model,"Canon EOS 10D"))
1231 wbi = remap_10d[wbi];
1232 fseek_nofail (ifp, aoff+2 + wbi*8, SEEK_SET);
1233 camera_red = get2(ifp);
1234 camera_red /= get2(ifp);
1235 camera_blue = get2(ifp);
1236 camera_blue = get2(ifp) / camera_blue;
1237 }
1238 if (type == 0x1030 && (wbi == 6 || wbi == 15)) {
1239 fseek_nofail (ifp, aoff, SEEK_SET); /* Get white sample */
1240 ciff_block_1030();
1241 }
1242 if (type == 0x1031) { /* Get the raw width and height */
1243 fseek_nofail (ifp, aoff+2, SEEK_SET);
1244 raw_width = get2(ifp);
1245 raw_height = get2(ifp);
1246 }
1247 if (type == 0x180e) { /* Get the timestamp */
1248 fseek_nofail (ifp, aoff, SEEK_SET);
1249 timestamp = get4(ifp);
1250 }
1251 if (type == 0x580e)
1252 timestamp = len;
1253 if (type == 0x1810) { /* Get the rotation */
1254 fseek_nofail (ifp, aoff+12, SEEK_SET);
1255 flip = get4(ifp);
1256 }
1257 if (type == 0x1835) { /* Get the decoder table */
1258 fseek_nofail (ifp, aoff, SEEK_SET);
1259 crw_init_tables (get4(ifp));
1260 }
1261 if (type >> 8 == 0x28 || type >> 8 == 0x30) /* Get sub-tables */
1262 parse_ciff(ifp, aoff, len);
1263 fseek_nofail (ifp, save, SEEK_SET);
1264 }
1265 if (wbi == 0 && !strcmp(model,"Canon EOS D30"))
1266 camera_red = -1; /* Use my auto WB for this photo */
1267 }
1268
1269 void
parse_rollei(FILE * const ifp)1270 parse_rollei(FILE * const ifp)
1271 {
1272 char line[128], *val;
1273 int tx=0, ty=0;
1274 struct tm t;
1275 time_t ts;
1276
1277 fseek_nofail (ifp, 0, SEEK_SET);
1278 do {
1279 fgets_nofail (line, 128, ifp);
1280 if ((val = strchr(line,'=')))
1281 *val++ = 0;
1282 else
1283 val = line + strlen(line);
1284 if (!strcmp(line,"DAT"))
1285 sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
1286 if (!strcmp(line,"TIM"))
1287 sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
1288 if (!strcmp(line,"HDR"))
1289 data_offset = atoi(val);
1290 if (!strcmp(line,"X "))
1291 raw_width = atoi(val);
1292 if (!strcmp(line,"Y "))
1293 raw_height = atoi(val);
1294 if (!strcmp(line,"TX "))
1295 tx = atoi(val);
1296 if (!strcmp(line,"TY "))
1297 ty = atoi(val);
1298 } while (strncmp(line,"EOHD",4));
1299 t.tm_year -= 1900;
1300 t.tm_mon -= 1;
1301 putenv((char*)"TZ=");
1302 if ((ts = mktime(&t)) > 0)
1303 timestamp = ts;
1304 data_offset += tx * ty * 2;
1305 strcpy (make, "Rollei");
1306 strcpy (model,"d530flex");
1307 }
1308
1309
1310
1311 void
parse_mos(FILE * const ifp,int const offset)1312 parse_mos(FILE * const ifp,
1313 int const offset)
1314 {
1315 char data[40];
1316 int skip, from, i, neut[4];
1317
1318 fseek_nofail (ifp, offset, SEEK_SET);
1319 while (1) {
1320 fread_nofail (data, 1, 8, ifp);
1321 if (strcmp(data,"PKTS")) break;
1322 fread_nofail (data, 1, 40, ifp);
1323 skip = get4(ifp);
1324 from = ftell_nofail(ifp);
1325 if (!strcmp(data,"NeutObj_neutrals")) {
1326 for (i=0; i < 4; i++)
1327 fscanf (ifp, "%d", neut+i);
1328 camera_red = (float) neut[2] / neut[1];
1329 camera_blue = (float) neut[2] / neut[3];
1330 }
1331 parse_mos(ifp, from);
1332 fseek_nofail (ifp, skip+from, SEEK_SET);
1333 }
1334 }
1335
1336 void
nikon_e950_coeff()1337 nikon_e950_coeff()
1338 {
1339 int r, g;
1340 static const float my_coeff[3][4] =
1341 { { -1.936280, 1.800443, -1.448486, 2.584324 },
1342 { 1.405365, -0.524955, -0.289090, 0.408680 },
1343 { -1.204965, 1.082304, 2.941367, -1.818705 } };
1344
1345 for (r=0; r < 3; r++)
1346 for (g=0; g < 4; g++)
1347 coeff[r][g] = my_coeff[r][g];
1348 use_coeff = 1;
1349 }
1350
1351
1352
getrat()1353 static double getrat()
1354 {
1355 double num = get4(ifp);
1356 return num / get4(ifp);
1357 }
1358
1359
1360
1361 static void
parse_makernote(FILE * const ifp)1362 parse_makernote(FILE * const ifp)
1363 {
1364 unsigned base=0, offset=0, entries, tag, type, len, save;
1365 static const int size[] = { 1,1,1,2,4,8,1,1,2,4,8,4,8 };
1366 short sorder;
1367 char buf[10];
1368 /*
1369 The MakerNote might have its own TIFF header (possibly with
1370 its own byte-order!), or it might just be a table.
1371 */
1372 sorder = order;
1373 fread_nofail (buf, 1, 10, ifp);
1374 if (!strncmp (buf,"KC" ,2) || /* these aren't TIFF format */
1375 !strncmp (buf,"MLY",3)) return;
1376 if (!strcmp (buf,"Nikon")) {
1377 base = ftell_nofail(ifp);
1378 order = get2(ifp);
1379 if (get2(ifp) != 42) goto quit;
1380 offset = get4(ifp);
1381 fseek_nofail (ifp, offset-8, SEEK_CUR);
1382 } else if (!strncmp (buf,"FUJIFILM",8) ||
1383 !strcmp (buf,"Panasonic")) {
1384 order = 0x4949;
1385 fseek_nofail (ifp, 2, SEEK_CUR);
1386 } else if (!strcmp (buf,"OLYMP") ||
1387 !strcmp (buf,"LEICA") ||
1388 !strcmp (buf,"EPSON"))
1389 fseek_nofail (ifp, -2, SEEK_CUR);
1390 else if (!strcmp (buf,"AOC") ||
1391 !strcmp (buf,"QVC"))
1392 fseek_nofail (ifp, -4, SEEK_CUR);
1393 else fseek_nofail (ifp, -10, SEEK_CUR);
1394
1395 entries = get2(ifp);
1396 while (entries--) {
1397 tag = get2(ifp);
1398 type = get2(ifp);
1399 len = get4(ifp);
1400 save = ftell_nofail(ifp);
1401 if (len * size[type < 13 ? type:0] > 4)
1402 fseek_nofail (ifp, get4(ifp)+base, SEEK_SET);
1403
1404 if (tag == 0xc && len == 4) {
1405 camera_red = getrat();
1406 camera_blue = getrat();
1407 }
1408 if (tag == 0x14 && len == 2560 && type == 7) {
1409 fseek_nofail (ifp, 1248, SEEK_CUR);
1410 goto get2_256;
1411 }
1412 if (strstr(make,"PENTAX")) {
1413 if (tag == 0x1b) tag = 0x1018;
1414 if (tag == 0x1c) tag = 0x1017;
1415 }
1416 if (tag == 0x8c)
1417 nikon_curve_offset = ftell_nofail(ifp) + 2112;
1418 if (tag == 0x96)
1419 nikon_curve_offset = ftell_nofail(ifp) + 2;
1420 if (tag == 0x97) {
1421 if (!strcmp(model,"NIKON D100 ")) {
1422 fseek_nofail (ifp, 72, SEEK_CUR);
1423 camera_red = get2(ifp) / 256.0;
1424 camera_blue = get2(ifp) / 256.0;
1425 } else if (!strcmp(model,"NIKON D2H")) {
1426 fseek_nofail (ifp, 10, SEEK_CUR);
1427 goto get2_rggb;
1428 } else if (!strcmp(model,"NIKON D70")) {
1429 fseek_nofail (ifp, 20, SEEK_CUR);
1430 camera_red = get2(ifp);
1431 camera_red /= get2(ifp);
1432 camera_blue = get2(ifp);
1433 camera_blue/= get2(ifp);
1434 }
1435 }
1436 if (tag == 0xe0 && len == 17) {
1437 get2(ifp);
1438 raw_width = get2(ifp);
1439 raw_height = get2(ifp);
1440 }
1441 if (tag == 0x200 && len == 4)
1442 black = (get2(ifp)+get2(ifp)+get2(ifp)+get2(ifp))/4;
1443 if (tag == 0x201 && len == 4) {
1444 camera_red = get2(ifp);
1445 camera_red /= get2(ifp);
1446 camera_blue = get2(ifp);
1447 camera_blue = get2(ifp) / camera_blue;
1448 }
1449 if (tag == 0x401 && len == 4) {
1450 black = (get4(ifp)+get4(ifp)+get4(ifp)+get4(ifp))/4;
1451 }
1452 if (tag == 0xe80 && len == 256 && type == 7) {
1453 fseek_nofail (ifp, 48, SEEK_CUR);
1454 camera_red = get2(ifp) * 508 * 1.078 / 0x10000;
1455 camera_blue = get2(ifp) * 382 * 1.173 / 0x10000;
1456 }
1457 if (tag == 0xf00 && len == 614 && type == 7) {
1458 fseek_nofail (ifp, 188, SEEK_CUR);
1459 goto get2_256;
1460 }
1461 if (tag == 0x1017)
1462 camera_red = get2(ifp) / 256.0;
1463 if (tag == 0x1018)
1464 camera_blue = get2(ifp) / 256.0;
1465 if (tag == 0x2011 && len == 2) {
1466 get2_256:
1467 order = 0x4d4d;
1468 camera_red = get2(ifp) / 256.0;
1469 camera_blue = get2(ifp) / 256.0;
1470 }
1471 if (tag == 0x4001) {
1472 fseek_nofail (ifp, strstr(model,"EOS-1D") ? 68:50, SEEK_CUR);
1473 get2_rggb:
1474 camera_red = get2(ifp);
1475 camera_red /= get2(ifp);
1476 camera_blue = get2(ifp);
1477 camera_blue = get2(ifp) / camera_blue;
1478 }
1479 fseek_nofail (ifp, save+4, SEEK_SET);
1480 }
1481 quit:
1482 order = sorder;
1483 }
1484
1485
1486
1487 static void
get_timestamp(FILE * const ifp)1488 get_timestamp(FILE * const ifp)
1489 {
1490 /*
1491 Since the TIFF DateTime string has no timezone information,
1492 assume that the camera's clock was set to Universal Time.
1493 */
1494 struct tm t;
1495 time_t ts;
1496
1497 if (fscanf (ifp, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
1498 &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
1499 return;
1500 t.tm_year -= 1900;
1501 t.tm_mon -= 1;
1502 putenv((char*)"TZ=UTC"); /* Remove this to assume local time */
1503 if ((ts = mktime(&t)) > 0)
1504 timestamp = ts;
1505 }
1506
1507 static void
parse_exif(FILE * const ifp,int base)1508 parse_exif(FILE * const ifp, int base)
1509 {
1510 int entries, tag, len, val, save;
1511
1512 entries = get2(ifp);
1513 while (entries--) {
1514 tag = get2(ifp);
1515 /* type = */ get2(ifp);
1516 len = get4(ifp);
1517 val = get4(ifp);
1518 save = ftell_nofail(ifp);
1519 fseek_nofail (ifp, base+val, SEEK_SET);
1520 if (tag == 0x9003 || tag == 0x9004)
1521 get_timestamp(ifp);
1522 if (tag == 0x927c) {
1523 if (!strncmp(make,"SONY",4))
1524 data_offset = base+val+len;
1525 else
1526 parse_makernote(ifp);
1527 }
1528 fseek_nofail (ifp, save, SEEK_SET);
1529 }
1530 }
1531
1532 static int
parse_tiff_ifd(FILE * const ifp,int base,int level)1533 parse_tiff_ifd(FILE * const ifp, int base, int level)
1534 {
1535 unsigned entries, tag, type, len, plen=16, save;
1536 int done=0, use_cm=0, cfa, i, j, c;
1537 static const int size[] = { 1,1,1,2,4,8,1,1,2,4,8,4,8 };
1538 char software[64];
1539 static const int flip_map[] = { 0,1,3,2,4,6,7,5 };
1540 unsigned char cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
1541 unsigned short scale[4];
1542 double dblack, cc[4][4], cm[4][3];
1543 double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
1544
1545 for (j=0; j < 4; j++)
1546 for (i=0; i < 4; i++)
1547 cc[j][i] = i == j;
1548 entries = get2(ifp);
1549 if (entries > 512) return 1;
1550 while (entries--) {
1551 tag = get2(ifp);
1552 type = get2(ifp);
1553 len = get4(ifp);
1554 save = ftell_nofail(ifp);
1555 if (tag > 50700 && tag < 50800) done = 1;
1556 if (len * size[type < 13 ? type:0] > 4)
1557 fseek_nofail (ifp, get4(ifp)+base, SEEK_SET);
1558 switch (tag) {
1559 case 0x11:
1560 camera_red = get4(ifp) / 256.0;
1561 break;
1562 case 0x12:
1563 camera_blue = get4(ifp) / 256.0;
1564 break;
1565 case 0x100: /* ImageWidth */
1566 if (strcmp(make,"Canon") || level)
1567 raw_width = type==3 ? get2(ifp) : get4(ifp);
1568 break;
1569 case 0x101: /* ImageHeight */
1570 if (strcmp(make,"Canon") || level)
1571 raw_height = type==3 ? get2(ifp) : get4(ifp);
1572 break;
1573 case 0x102: /* Bits per sample */
1574 fuji_secondary = len == 2;
1575 if (level) maximum = (1 << get2(ifp)) - 1;
1576 break;
1577 case 0x103: /* Compression */
1578 tiff_data_compression = get2(ifp);
1579 break;
1580 case 0x106: /* Kodak color format */
1581 kodak_data_compression = get2(ifp);
1582 break;
1583 case 0x10f: /* Make */
1584 fgets_nofail (make, 64, ifp);
1585 break;
1586 case 0x110: /* Model */
1587 fgets_nofail (model, 64, ifp);
1588 break;
1589 case 0x111: /* StripOffset */
1590 data_offset = get4(ifp);
1591 break;
1592 case 0x112: /* Orientation */
1593 flip = flip_map[(get2(ifp)-1) & 7];
1594 break;
1595 case 0x115: /* SamplesPerPixel */
1596 tiff_samples = get2(ifp);
1597 break;
1598 case 0x131: /* Software tag */
1599 fgets_nofail (software, 64, ifp);
1600 if (!strncmp(software,"Adobe",5))
1601 make[0] = 0;
1602 break;
1603 case 0x132: /* DateTime tag */
1604 get_timestamp(ifp);
1605 break;
1606 case 0x144: /* TileOffsets */
1607 if (level) {
1608 data_offset = ftell_nofail(ifp);
1609 } else {
1610 strcpy (make, "Leaf");
1611 data_offset = get4(ifp);
1612 }
1613 break;
1614 case 0x14a: /* SubIFD tag */
1615 if (len > 2 && !is_dng && !strcmp(make,"Kodak"))
1616 len = 2;
1617 while (len--) {
1618 i = ftell_nofail(ifp);
1619 fseek_nofail (ifp, get4(ifp)+base, SEEK_SET);
1620 if (parse_tiff_ifd(ifp, base, level+1)) break;
1621 fseek_nofail (ifp, i+4, SEEK_SET);
1622 }
1623 break;
1624 case 33405: /* Model2 */
1625 fgets_nofail (model2, 64, ifp);
1626 break;
1627 case 33422: /* CFAPattern */
1628 if ((plen=len) > 16) plen = 16;
1629 fread_nofail (cfa_pat, 1, plen, ifp);
1630 for (colors=cfa=i=0; i < plen; i++) {
1631 colors += !(cfa & (1 << cfa_pat[i]));
1632 cfa |= 1 << cfa_pat[i];
1633 }
1634 if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
1635 if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
1636 goto guess_cfa_pc;
1637 case 34665: /* EXIF tag */
1638 fseek_nofail (ifp, get4(ifp)+base, SEEK_SET);
1639 parse_exif(ifp, base);
1640 break;
1641 case 50706: /* DNGVersion */
1642 is_dng = 1;
1643 if (flip == 7) flip = 4; /* Adobe didn't read the TIFF spec. */
1644 break;
1645 case 50710: /* CFAPlaneColor */
1646 if (len > 4) len = 4;
1647 colors = len;
1648 fread_nofail (cfa_pc, 1, colors, ifp);
1649 guess_cfa_pc:
1650 FORC4 tab[cfa_pc[c]] = c;
1651 for (i=16; i--; )
1652 filters = filters << 2 | tab[cfa_pat[i % plen]];
1653 break;
1654 case 50711: /* CFALayout */
1655 if (get2(ifp) == 2) {
1656 fuji_width = (raw_width+1)/2;
1657 filters = 0x49494949;
1658 }
1659 break;
1660 case 0x123:
1661 case 0x90d:
1662 case 50712: /* LinearizationTable */
1663 if (len > 0x1000)
1664 len = 0x1000;
1665 read_shorts(ifp, curve, len);
1666 for (i=len; i < 0x1000; i++)
1667 maximum = curve[i] = curve[i-1];
1668 break;
1669 case 50714: /* BlackLevel */
1670 case 50715: /* BlackLevelDeltaH */
1671 case 50716: /* BlackLevelDeltaV */
1672 for (dblack=i=0; i < len; i++)
1673 dblack += getrat();
1674 black += dblack/len + 0.5;
1675 break;
1676 case 50717: /* WhiteLevel */
1677 maximum = get2(ifp);
1678 break;
1679 case 50718: /* DefaultScale */
1680 for (i=0; i < 4; i++)
1681 scale[i] = get4(ifp);
1682 if (scale[1]*scale[2] == 2*scale[0]*scale[3]) ymag = 2;
1683 break;
1684 case 50721: /* ColorMatrix1 */
1685 case 50722: /* ColorMatrix2 */
1686 FORC4 for (j=0; j < 3; j++)
1687 cm[c][j] = getrat();
1688 use_cm = 1;
1689 break;
1690 case 50723: /* CameraCalibration1 */
1691 case 50724: /* CameraCalibration2 */
1692 for (i=0; i < colors; i++)
1693 FORC4 cc[i][c] = getrat();
1694 case 50727: /* AnalogBalance */
1695 FORC4 ab[c] = getrat();
1696 break;
1697 case 50728: /* AsShotNeutral */
1698 FORC4 asn[c] = getrat();
1699 break;
1700 case 50729: /* AsShotWhiteXY */
1701 xyz[0] = getrat();
1702 xyz[1] = getrat();
1703 xyz[2] = 1 - xyz[0] - xyz[1];
1704 }
1705 fseek_nofail (ifp, save+4, SEEK_SET);
1706 }
1707 for (i=0; i < colors; i++)
1708 FORC4 cc[i][c] *= ab[i];
1709 if (use_cm)
1710 dng_coeff (cc, cm, xyz);
1711 if (asn[0])
1712 FORC4 pre_mul[c] = 1 / asn[c];
1713 if (!use_cm)
1714 FORC4 pre_mul[c] /= cc[c][c];
1715
1716 if (is_dng || level) return done;
1717
1718 if ((raw_height & 1) && !strncmp (make,"OLYMPUS",7))
1719 raw_height++;
1720
1721 if (make[0] == 0 && raw_width == 680 && raw_height == 680) {
1722 strcpy (make, "Imacon");
1723 strcpy (model,"Ixpress");
1724 }
1725 return done;
1726 }
1727
1728 void
parse_tiff(FILE * const ifp,int base)1729 parse_tiff(FILE * const ifp, int base)
1730 {
1731 int doff;
1732
1733 fseek_nofail (ifp, base, SEEK_SET);
1734 order = get2(ifp);
1735 if (order != 0x4949 && order != 0x4d4d) return;
1736 get2(ifp);
1737 while ((doff = get4(ifp))) {
1738 fseek_nofail (ifp, doff+base, SEEK_SET);
1739 if (parse_tiff_ifd(ifp, base, 0)) break;
1740 }
1741 if (!is_dng && !strncmp(make,"Kodak",5)) {
1742 fseek_nofail (ifp, 12+base, SEEK_SET);
1743 parse_tiff_ifd(ifp, base, 2);
1744 }
1745 }
1746
1747
1748
1749 /*
1750 Many cameras have a "debug mode" that writes JPEG and raw
1751 at the same time. The raw file has no header, so try to
1752 to open the matching JPEG file and read its metadata.
1753 */
1754 void
parse_external_jpeg(const char * const ifname)1755 parse_external_jpeg(const char * const ifname)
1756 {
1757 const char *file, *ext;
1758 char * jfile;
1759 char * jext;
1760 char * jname;
1761
1762 ext = strrchr (ifname, '.');
1763 file = strrchr (ifname, '/');
1764 if (!file) file = strrchr (ifname, '\\');
1765 if (!file) file = ifname-1;
1766 file++;
1767 if (strlen(ext) != 4 || ext-file != 8) return;
1768 jname = malloc (strlen(ifname) + 1);
1769 merror (jname, "parse_external()");
1770 strcpy (jname, ifname);
1771 jfile = jname + (file - ifname);
1772 jext = jname + (ext - ifname);
1773 if (!strcaseeq (ext, ".jpg")) {
1774 strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
1775 memcpy (jfile, file+4, 4);
1776 memcpy (jfile+4, file, 4);
1777 } else
1778 while (isdigit(*--jext)) {
1779 if (*jext != '9') {
1780 (*jext)++;
1781 break;
1782 }
1783 *jext = '0';
1784 }
1785 if (strcmp (jname, ifname)) {
1786 FILE * ifP;
1787 ifP = fopen (jname, "rb");
1788 if (ifP) {
1789 if (verbose)
1790 pm_message ("Reading metadata from %s...", jname);
1791 parse_tiff(ifP, 12);
1792 fclose (ifP);
1793 }
1794 }
1795 if (!timestamp)
1796 pm_message ( "Failed to read metadata from %s", jname);
1797 free (jname);
1798 }
1799
1800