Debian Codegraph @

converter/other/cameratopam/camera.c

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