2
dcraw.c -- Dave Coffin's raw photo decoder
3
Copyright 1997-2007 by Dave Coffin, dcoffin a cybercom o net
5
This is a command-line ANSI C program to convert raw photos from
6
any digital camera on any computer running any operating system.
8
Attention! Some parts of this program are restricted under the
9
terms of the GNU General Public License. Such code is enclosed
10
in "BEGIN GPL BLOCK" and "END GPL BLOCK" declarations.
11
Any code not declared GPL is free for all uses.
13
Starting in Revision 1.237, the code to support Foveon cameras
16
To lawfully redistribute dcraw.c, you must either (a) include
17
full source code for all executable files containing restricted
18
functions, (b) remove these functions, re-implement them, or
19
copy them from an earlier, non-GPL Revision of dcraw.c, or (c)
20
purchase a license from the author.
23
$Date: 2007/01/18 23:57:32 $
24
grota: added a couple of functions to integrate in qtpfsgui
27
#include "../Libpfs/pfs.h"
29
#include "pfsindcraw.h"
30
#define DCRAWVERSION "8.53"
33
// #define _GNU_SOURCE
34
#define _USE_MATH_DEFINES
47
NO_JPEG disables decoding of compressed Kodak DC120 files.
48
NO_LCMS disables the "-p" option.
58
#define _(String) gettext(String)
60
#define _(String) (String)
67
#include <sys/utime.h>
69
#pragma comment(lib, "ws2_32.lib")
70
#define strcasecmp stricmp
71
typedef __int64 INT64;
72
typedef unsigned __int64 UINT64;
76
#include <netinet/in.h>
77
typedef long long INT64;
78
typedef unsigned long long UINT64;
82
#error Please compile dcraw.c by itself.
83
#error Do not link it with ljpeg_decode.
87
#define LONG_BIT (8 * sizeof (long))
91
typedef unsigned char uchar;
92
typedef unsigned short ushort;
95
All global variables are defined here, and all functions that
96
access them are prefixed with "CLASS". Note that a thread-safe
97
C++ class cannot have non-const static local variables.
101
char *ifname, make[64], model[64], model2[64], *meta_data, cdesc[5];
102
float flash_used, canon_ev, iso_speed, shutter, aperture, focal_len;
104
unsigned shot_order, kodak_cbpp, filters, unique_id, *oprof;
105
int profile_offset, profile_length;
106
int thumb_offset, thumb_length, thumb_width, thumb_height, thumb_misc;
107
int data_offset, strip_offset, curve_offset, meta_offset, meta_length;
108
int tiff_nifds, tiff_flip, tiff_bps, tiff_compress, tile_length;
109
int raw_height, raw_width, top_margin, left_margin;
110
int height, width, fuji_width, colors, tiff_samples;
111
int black, maximum, raw_color, use_gamma;
112
int iheight, iwidth, shrink, flip;
114
int zero_after_ff, is_raw, dng_version, is_foveon;
115
ushort (*image)[4], white[8][8], curve[0x1000], cr2_slice[3];
116
float bright=1, user_mul[4]={0,0,0,0}, sigma_d=0, sigma_r=0;
117
int four_color_rgb=0, document_mode=0, highlight=0;
118
int verbose=0, use_auto_wb=0, use_camera_wb=0;
119
int output_color=1, output_bps=8, output_tiff=0;
120
int fuji_layout, fuji_secondary, shot_select=0;
121
float cam_mul[4], pre_mul[4], rgb_cam[3][4]; /* RGB from camera color */
122
const double xyz_rgb[3][3] = { /* XYZ from RGB */
123
{ 0.412453, 0.357580, 0.180423 },
124
{ 0.212671, 0.715160, 0.072169 },
125
{ 0.019334, 0.119193, 0.950227 } };
126
const float d65_white[3] = { 0.950456, 1, 1.088754 };
127
int histogram[4][0x2000];
128
void (*write_thumb)(FILE *), (*write_fun)(FILE *);
129
void (*load_raw)(), (*thumb_load_raw)();
133
struct decode *branch[2];
135
} first_decode[2048], *second_decode, *free_decode;
138
int width, height, bps, comp, phint, offset, flip, samples, bytes;
142
int format, key_off, black, black_off, split_col, tag_21a;
150
#define fgetc getc_unlocked
154
#define FORC3 for (c=0; c < 3; c++)
155
#define FORC4 for (c=0; c < 4; c++)
156
#define FORCC for (c=0; c < colors; c++)
158
#define SQR(x) ((x)*(x))
159
#define ABS(x) (((int)(x) ^ ((int)(x) >> 31)) - ((int)(x) >> 31))
160
#define MIN(a,b) ((a) < (b) ? (a) : (b))
161
#define MAX(a,b) ((a) > (b) ? (a) : (b))
162
#define LIM(x,min,max) MAX(min,MIN(x,max))
163
#define ULIM(x,y,z) ((y) < (z) ? LIM(x,y,z) : LIM(x,z,y))
164
#define CLIP(x) LIM(x,0,65535)
165
#define SWAP(a,b) { a ^= b; a ^= (b ^= a); }
168
In order to inline this calculation, I make the risky
169
assumption that all filter patterns can be described
170
by a repeating pattern of eight rows and two columns
172
Do not use the FC or BAYER macros with the Leaf CatchLight,
173
because its pattern is 16x16, not 2x8.
175
Return values are either 0/1/2/3 = G/M/C/Y or 0/1/2/3 = R/G1/B/G2
177
PowerShot 600 PowerShot A50 PowerShot Pro70 Pro90 & G1
178
0xe1e4e1e4: 0x1b4e4b1e: 0x1e4b4e1b: 0xb4b4b4b4:
180
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
181
0 G M G M G M 0 C Y C Y C Y 0 Y C Y C Y C 0 G M G M G M
182
1 C Y C Y C Y 1 M G M G M G 1 M G M G M G 1 Y C Y C Y C
183
2 M G M G M G 2 Y C Y C Y C 2 C Y C Y C Y
184
3 C Y C Y C Y 3 G M G M G M 3 G M G M G M
185
4 C Y C Y C Y 4 Y C Y C Y C
186
PowerShot A5 5 G M G M G M 5 G M G M G M
187
0x1e4e1e4e: 6 Y C Y C Y C 6 C Y C Y C Y
188
7 M G M G M G 7 M G M G M G
195
All RGB cameras use one of these Bayer grids:
197
0x16161616: 0x61616161: 0x49494949: 0x94949494:
199
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
200
0 B G B G B G 0 G R G R G R 0 G B G B G B 0 R G R G R G
201
1 G R G R G R 1 B G B G B G 1 R G R G R G 1 G B G B G B
202
2 B G B G B G 2 G R G R G R 2 G B G B G B 2 R G R G R G
203
3 G R G R G R 3 B G B G B G 3 R G R G R G 3 G B G B G B
206
#define FC(row,col) \
207
(filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
209
#define BAYER(row,col) \
210
image[((row) >> shrink)*iwidth + ((col) >> shrink)][FC(row,col)]
212
#define BAYER2(row,col) \
213
image[((row) >> shrink)*iwidth + ((col) >> shrink)][fc(row,col)]
215
int CLASS fc (int row, int col)
217
static const char filter[16][16] =
218
{ { 2,1,1,3,2,3,2,0,3,2,3,0,1,2,1,0 },
219
{ 0,3,0,2,0,1,3,1,0,1,1,2,0,3,3,2 },
220
{ 2,3,3,2,3,1,1,3,3,1,2,1,2,0,0,3 },
221
{ 0,1,0,1,0,2,0,2,2,0,3,0,1,3,2,1 },
222
{ 3,1,1,2,0,1,0,2,1,3,1,3,0,1,3,0 },
223
{ 2,0,0,3,3,2,3,1,2,0,2,0,3,2,2,1 },
224
{ 2,3,3,1,2,1,2,1,2,1,1,2,3,0,0,1 },
225
{ 1,0,0,2,3,0,0,3,0,3,0,3,2,1,2,3 },
226
{ 2,3,3,1,1,2,1,0,3,2,3,0,2,3,1,3 },
227
{ 1,0,2,0,3,0,3,2,0,1,1,2,0,1,0,2 },
228
{ 0,1,1,3,3,2,2,1,1,3,3,0,2,1,3,2 },
229
{ 2,3,2,0,0,1,3,0,2,0,1,2,3,0,1,0 },
230
{ 1,3,1,2,3,2,3,2,0,2,0,1,1,0,3,0 },
231
{ 0,2,0,3,1,0,0,1,1,3,3,2,3,2,2,1 },
232
{ 2,1,3,2,3,1,2,1,0,3,0,2,0,2,0,2 },
233
{ 0,3,1,0,0,2,0,3,2,1,3,1,1,3,1,3 } };
235
if (filters != 1) return FC(row,col);
236
return filter[(row+top_margin) & 15][(col+left_margin) & 15];
240
char *my_memmem (char *haystack, size_t haystacklen,
241
char *needle, size_t needlelen)
244
for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
245
if (!memcmp (c, needle, needlelen))
249
#define memmem my_memmem
252
void CLASS merror (void *ptr, char *where)
255
fprintf (stderr,_("%s: Out of memory in %s\n"), ifname, where);
256
longjmp (failure, 1);
259
ushort CLASS sget2 (uchar *s)
261
if (order == 0x4949) /* "II" means little-endian */
262
return s[0] | s[1] << 8;
263
else /* "MM" means big-endian */
264
return s[0] << 8 | s[1];
269
uchar str[2] = { 0xff,0xff };
270
fread (str, 1, 2, ifp);
274
int CLASS sget4 (uchar *s)
277
return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
279
return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
281
#define sget4(s) sget4((uchar *)s)
285
uchar str[4] = { 0xff,0xff,0xff,0xff };
286
fread (str, 1, 4, ifp);
290
int CLASS getint (int type)
292
return type == 3 ? get2() : get4();
295
float CLASS int_to_float (int i)
297
union { int i; float f; } u;
302
double CLASS getreal (int type)
304
union { char c[8]; double d; } u;
308
case 3: return (unsigned short) get2();
309
case 4: return (unsigned int) get4();
310
case 5: u.d = (unsigned int) get4();
311
return u.d / (unsigned int) get4();
312
case 8: return (signed short) get2();
313
case 9: return (signed int) get4();
314
case 10: u.d = (signed int) get4();
315
return u.d / (signed int) get4();
316
case 11: return int_to_float (get4());
318
rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
319
for (i=0; i < 8; i++)
320
u.c[i ^ rev] = fgetc(ifp);
322
default: return fgetc(ifp);
325
#define getrat() getreal(10)
327
void CLASS read_shorts (ushort *pixel, int count)
329
fread (pixel, 2, count, ifp);
330
if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
331
swab ((const char*)pixel, (char*)pixel, count*2);
334
void CLASS canon_600_fixed_wb (int temp)
336
static const short mul[4][5] = {
337
{ 667, 358,397,565,452 },
338
{ 731, 390,367,499,517 },
339
{ 1119, 396,348,448,537 },
340
{ 1399, 485,431,508,688 } };
345
if (*mul[lo] <= temp) break;
346
for (hi=0; hi < 3; hi++)
347
if (*mul[hi] >= temp) break;
349
frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
350
for (i=1; i < 5; i++)
351
pre_mul[i-1] = 1 / (frac * mul[hi][i] + (1-frac) * mul[lo][i]);
354
/* Return values: 0 = white 1 = near white 2 = not white */
355
int CLASS canon_600_color (int ratio[2], int mar)
357
int clipped=0, target, miss;
361
{ ratio[1] = -104; clipped = 1; }
363
{ ratio[1] = 12; clipped = 1; }
365
if (ratio[1] < -264 || ratio[1] > 461) return 2;
367
{ ratio[1] = -50; clipped = 1; }
369
{ ratio[1] = 307; clipped = 1; }
371
target = flash_used || ratio[1] < 197
372
? -38 - (398 * ratio[1] >> 10)
373
: -123 + (48 * ratio[1] >> 10);
374
if (target - mar <= ratio[0] &&
375
target + 20 >= ratio[0] && !clipped) return 0;
376
miss = target - ratio[0];
377
if (abs(miss) >= mar*4) return 2;
378
if (miss < -20) miss = -20;
379
if (miss > mar) miss = mar;
380
ratio[0] = target - miss;
384
void CLASS canon_600_auto_wb()
386
int mar, row, col, i, j, st, count[] = { 0,0 };
387
int test[8], total[2][8], ratio[2][2], stat[2];
389
memset (&total, 0, sizeof total);
391
if (i < 10) mar = 150;
392
else if (i > 12) mar = 20;
393
else mar = 280 - 20 * i;
394
if (flash_used) mar = 80;
395
for (row=14; row < height-14; row+=4)
396
for (col=10; col < width; col+=2) {
397
for (i=0; i < 8; i++)
398
test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
399
BAYER(row+(i >> 1),col+(i & 1));
400
for (i=0; i < 8; i++)
401
if (test[i] < 150 || test[i] > 1500) goto next;
402
for (i=0; i < 4; i++)
403
if (abs(test[i] - test[i+4]) > 50) goto next;
404
for (i=0; i < 2; i++) {
405
for (j=0; j < 4; j+=2)
406
ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
407
stat[i] = canon_600_color (ratio[i], mar);
409
if ((st = stat[0] | stat[1]) > 1) goto next;
410
for (i=0; i < 2; i++)
412
for (j=0; j < 2; j++)
413
test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
414
for (i=0; i < 8; i++)
415
total[st][i] += test[i];
419
if (count[0] | count[1]) {
420
st = count[0]*200 < count[1];
421
for (i=0; i < 4; i++)
422
pre_mul[i] = 1.0 / (total[st][i] + total[st][i+4]);
426
void CLASS canon_600_coeff()
428
static const short table[6][12] = {
429
{ -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
430
{ -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
431
{ -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
432
{ -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
433
{ -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
434
{ -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
438
mc = pre_mul[1] / pre_mul[2];
439
yc = pre_mul[3] / pre_mul[2];
440
if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
441
if (mc > 1.28 && mc <= 2) {
442
if (yc < 0.8789) t=3;
443
else if (yc <= 2) t=4;
446
for (raw_color = i=0; i < 3; i++)
447
FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
450
void CLASS canon_600_load_raw()
452
uchar data[1120], *dp;
453
ushort pixel[896], *pix;
454
int irow, row, col, val;
455
static const short mul[4][2] =
456
{ { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
458
for (irow=row=0; irow < height; irow++) {
459
fread (data, raw_width * 10 / 8, 1, ifp);
460
for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8) {
461
pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
462
pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
463
pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
464
pix[3] = (dp[4] << 2) + (dp[1] & 3);
465
pix[4] = (dp[5] << 2) + (dp[9] & 3);
466
pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
467
pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
468
pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
470
for (col=0; col < width; col++)
471
BAYER(row,col) = pixel[col];
472
for (col=width; col < raw_width; col++)
474
if ((row+=2) > height) row = 1;
476
if (raw_width > width)
477
black = black / ((raw_width - width) * height) - 4;
478
for (row=0; row < height; row++)
479
for (col=0; col < width; col++) {
480
val = (BAYER(row,col) - black) * mul[row & 3][col & 1] >> 9;
481
if (val < 0) val = 0;
482
BAYER(row,col) = val;
484
canon_600_fixed_wb(1311);
487
maximum = (0x3ff - black) * 1109 >> 9;
491
void CLASS remove_zeroes()
493
unsigned row, col, tot, n, r, c;
495
for (row=0; row < height; row++)
496
for (col=0; col < width; col++)
497
if (BAYER(row,col) == 0) {
499
for (r = row-2; r <= row+2; r++)
500
for (c = col-2; c <= col+2; c++)
501
if (r < height && c < width &&
502
FC(r,c) == FC(row,col) && BAYER(r,c))
503
tot += (n++,BAYER(r,c));
504
if (n) BAYER(row,col) = tot/n;
508
void CLASS canon_a5_load_raw()
510
ushort data[1970], *dp, pixel;
511
int vbits=0, buf=0, row, col, bc=0;
514
for (row=-top_margin; row < raw_height-top_margin; row++) {
515
read_shorts (dp=data, raw_width * 10 / 16);
516
for (col=-left_margin; col < raw_width-left_margin; col++) {
518
buf = (vbits += 16, (buf << 16) + *dp++);
519
pixel = buf >> (vbits -= 10) & 0x3ff;
520
if ((unsigned) row < height && (unsigned) col < width)
521
BAYER(row,col) = pixel;
522
else black += (bc++,pixel);
527
if (raw_width > 1600) remove_zeroes();
531
getbits(-1) initializes the buffer
532
getbits(n) where 0 <= n <= 25 returns an n-bit integer
534
unsigned CLASS getbits (int nbits)
536
static unsigned bitbuf=0;
537
static int vbits=0, reset=0;
541
return bitbuf = vbits = reset = 0;
542
if (nbits == 0 || reset) return 0;
543
while (vbits < nbits) {
545
if ((reset = zero_after_ff && c == 0xff && fgetc(ifp))) return 0;
546
bitbuf = (bitbuf << 8) + c;
550
return bitbuf << (32-nbits-vbits) >> (32-nbits);
553
void CLASS init_decoder()
555
memset (first_decode, 0, sizeof first_decode);
556
free_decode = first_decode;
560
Construct a decode tree according the specification in *source.
561
The first 16 bytes specify how many codes should be 1-bit, 2-bit
562
3-bit, etc. Bytes after that are the leaf values.
564
For example, if the source is
566
{ 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
567
0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
585
uchar * CLASS make_decoder (const uchar *source, int level)
591
if (level==0) leaf=0;
593
if (free_decode > first_decode+2048) {
594
fprintf (stderr,_("%s: decoder table overflow\n"), ifname);
595
longjmp (failure, 2);
597
for (i=next=0; i <= leaf && next < 16; )
601
cur->branch[0] = free_decode;
602
make_decoder (source, level+1);
603
cur->branch[1] = free_decode;
604
make_decoder (source, level+1);
606
cur->leaf = source[16 + leaf++];
608
return (uchar *) source + 16 + leaf;
611
void CLASS crw_init_tables (unsigned table)
613
static const uchar first_tree[3][29] = {
614
{ 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
615
0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
616
{ 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
617
0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
618
{ 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
619
0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
621
static const uchar second_tree[3][180] = {
622
{ 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
623
0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
624
0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
625
0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
626
0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
627
0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
628
0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
629
0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
630
0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
631
0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
632
0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
633
0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
634
0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
635
0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
636
0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
637
{ 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
638
0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
639
0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
640
0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
641
0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
642
0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
643
0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
644
0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
645
0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
646
0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
647
0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
648
0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
649
0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
650
0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
651
0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
652
{ 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
653
0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
654
0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
655
0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
656
0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
657
0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
658
0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
659
0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
660
0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
661
0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
662
0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
663
0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
664
0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
665
0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
666
0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
668
if (table > 2) table = 2;
670
make_decoder ( first_tree[table], 0);
671
second_decode = free_decode;
672
make_decoder (second_tree[table], 0);
676
Return 0 if the image starts with compressed data,
677
1 if it starts with uncompressed low-order bits.
679
In Canon compressed data, 0xff is always followed by 0x00.
681
int CLASS canon_has_lowbits()
686
fseek (ifp, 0, SEEK_SET);
687
fread (test, 1, sizeof test, ifp);
688
for (i=540; i < sizeof test - 1; i++)
689
if (test[i] == 0xff) {
690
if (test[i+1]) return 1;
696
void CLASS canon_compressed_load_raw()
698
ushort *pixel, *prow;
699
int lowbits, i, row, r, col, save, val;
701
struct decode *decode, *dindex;
702
int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
705
crw_init_tables (tiff_compress);
706
pixel = (ushort *) calloc (raw_width*8, sizeof *pixel);
707
merror (pixel, "canon_compressed_load_raw()");
708
lowbits = canon_has_lowbits();
709
if (!lowbits) maximum = 0x3ff;
710
fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
713
for (row=0; row < raw_height; row+=8) {
714
for (block=0; block < raw_width >> 3; block++) {
715
memset (diffbuf, 0, sizeof diffbuf);
716
decode = first_decode;
717
for (i=0; i < 64; i++ ) {
718
for (dindex=decode; dindex->branch[0]; )
719
dindex = dindex->branch[getbits(1)];
721
decode = second_decode;
722
if (leaf == 0 && i) break;
723
if (leaf == 0xff) continue;
726
if (len == 0) continue;
728
if ((diff & (1 << (len-1))) == 0)
729
diff -= (1 << len) - 1;
730
if (i < 64) diffbuf[i] = diff;
734
for (i=0; i < 64; i++ ) {
735
if (pnum++ % raw_width == 0)
736
base[0] = base[1] = 512;
737
pixel[(block << 6) + i] = ( base[i & 1] += diffbuf[i] );
742
fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
743
for (prow=pixel, i=0; i < raw_width*2; i++) {
745
for (r=0; r < 8; r+=2, prow++) {
746
val = (*prow << 2) + ((c >> r) & 3);
747
if (raw_width == 2672 && val < 512) val += 2;
751
fseek (ifp, save, SEEK_SET);
753
for (r=0; r < 8; r++) {
754
irow = row - top_margin + r;
755
if (irow >= height) continue;
756
for (col=0; col < raw_width; col++) {
757
icol = col - left_margin;
759
BAYER(irow,icol) = pixel[r*raw_width+col];
761
black += pixel[r*raw_width+col];
766
if (raw_width > width)
767
black /= (raw_width - width) * height;
771
Not a full implementation of Lossless JPEG, just
772
enough to decode Canon, Kodak and Adobe DNG images.
775
int bits, high, wide, clrs, restart, vpred[4];
776
struct decode *huff[4];
780
int CLASS ljpeg_start (struct jhead *jh, int info_only)
783
uchar data[0x10000], *dp;
786
for (i=0; i < 4; i++)
787
jh->huff[i] = free_decode;
788
jh->restart = INT_MAX;
789
fread (data, 2, 1, ifp);
790
if (data[1] != 0xd8) return 0;
792
fread (data, 2, 2, ifp);
793
tag = data[0] << 8 | data[1];
794
len = (data[2] << 8 | data[3]) - 2;
795
if (tag <= 0xff00) return 0;
796
fread (data, 1, len, ifp);
801
jh->high = data[1] << 8 | data[2];
802
jh->wide = data[3] << 8 | data[4];
806
if (info_only) break;
807
for (dp = data; dp < data+len && *dp < 4; ) {
808
jh->huff[*dp] = free_decode;
809
dp = make_decoder (++dp, 0);
813
jh->restart = data[0] << 8 | data[1];
815
} while (tag != 0xffda);
816
if (info_only) return 1;
817
jh->row = (ushort *) calloc (jh->wide*jh->clrs, 2);
818
merror (jh->row, " jpeg_start()");
819
return zero_after_ff = 1;
822
int CLASS ljpeg_diff (struct decode *dindex)
826
while (dindex->branch[0])
827
dindex = dindex->branch[getbits(1)];
829
if (len == 16 && (!dng_version || dng_version >= 0x1010000))
832
if ((diff & (1 << (len-1))) == 0)
833
diff -= (1 << len) - 1;
837
void CLASS ljpeg_row (int jrow, struct jhead *jh)
840
ushort mark=0, *outp=jh->row;
842
if (jrow * jh->wide % jh->restart == 0) {
843
FORC4 jh->vpred[c] = 1 << (jh->bits-1);
845
do mark = (mark << 8) + (c = fgetc(ifp));
846
while (c != EOF && mark >> 4 != 0xffd);
849
for (col=0; col < jh->wide; col++)
850
for (c=0; c < jh->clrs; c++) {
851
diff = ljpeg_diff (jh->huff[c]);
852
*outp = col ? outp[-jh->clrs]+diff : (jh->vpred[c] += diff);
857
void CLASS lossless_jpeg_load_raw()
859
int jwide, jrow, jcol, val, jidx, i, j, row=0, col=0;
863
if (!ljpeg_start (&jh, 0)) return;
864
jwide = jh.wide * jh.clrs;
866
for (jrow=0; jrow < jh.high; jrow++) {
867
ljpeg_row (jrow, &jh);
868
for (jcol=0; jcol < jwide; jcol++) {
873
jidx = jrow*jwide + jcol;
874
i = jidx / (cr2_slice[1]*jh.high);
875
if ((j = i >= cr2_slice[0]))
877
jidx -= i * (cr2_slice[1]*jh.high);
878
row = jidx / cr2_slice[1+j];
879
col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
881
if ((unsigned) (row-top_margin) < height) {
882
if ((unsigned) (col-left_margin) < width) {
883
BAYER(row-top_margin,col-left_margin) = val;
884
if (min > val) min = val;
887
if (++col >= raw_width)
892
if (raw_width > width)
893
black /= (raw_width - width) * height;
894
if (!strcasecmp(make,"KODAK"))
898
void CLASS adobe_copy_pixel (int row, int col, ushort **rp)
902
r = row -= top_margin;
903
c = col -= left_margin;
904
if (fuji_secondary && shot_select) (*rp)++;
907
r = row + fuji_width - 1 - (col >> 1);
908
c = row + ((col+1) >> 1);
910
if (r < height && c < width)
911
BAYER(r,c) = **rp < 0x1000 ? curve[**rp] : **rp;
912
*rp += 1 + fuji_secondary;
914
if (r < height && c < width)
915
for (c=0; c < tiff_samples; c++)
916
image[row*width+col][c] = (*rp)[c] < 0x1000 ? curve[(*rp)[c]]:(*rp)[c];
919
if (fuji_secondary && shot_select) (*rp)--;
922
void CLASS adobe_dng_load_raw_lj()
924
int save, twide, trow=0, tcol=0, jrow, jcol;
930
fseek (ifp, get4(), SEEK_SET);
931
if (!ljpeg_start (&jh, 0)) break;
932
if (trow >= raw_height) break;
933
if (jh.high > raw_height-trow)
934
jh.high = raw_height-trow;
936
if (filters) twide *= jh.clrs;
937
else colors = jh.clrs;
938
if (fuji_secondary) twide /= 2;
939
if (twide > raw_width-tcol)
940
twide = raw_width-tcol;
942
for (jrow=0; jrow < jh.high; jrow++) {
943
ljpeg_row (jrow, &jh);
944
for (rp=jh.row, jcol=0; jcol < twide; jcol++)
945
adobe_copy_pixel (trow+jrow, tcol+jcol, &rp);
947
fseek (ifp, save+4, SEEK_SET);
948
if ((tcol += twide) >= raw_width) {
956
void CLASS adobe_dng_load_raw_nc()
961
pixel = (ushort *) calloc (raw_width * tiff_samples, sizeof *pixel);
962
merror (pixel, "adobe_dng_load_raw_nc()");
963
for (row=0; row < raw_height; row++) {
965
read_shorts (pixel, raw_width * tiff_samples);
968
for (col=0; col < raw_width * tiff_samples; col++)
969
pixel[col] = getbits(tiff_bps);
971
for (rp=pixel, col=0; col < raw_width; col++)
972
adobe_copy_pixel (row, col, &rp);
977
void CLASS pentax_k10_load_raw()
979
static const uchar pentax_tree[] =
980
{ 0,2,3,1,1,1,1,1,1,2,0,0,0,0,0,0,
981
3,4,2,5,1,6,0,7,8,9,10,11,12 };
982
int row, col, diff, i;
983
ushort vpred[4] = {0,0,0,0}, hpred[2];
986
make_decoder (pentax_tree, 0);
988
for (row=0; row < height; row++)
989
for (col=0; col < raw_width; col++) {
990
diff = ljpeg_diff (first_decode);
992
i = 2*(row & 1) + (col & 1);
994
hpred[col] = vpred[i];
996
hpred[col & 1] += diff;
998
BAYER(row,col) = hpred[col & 1];
1002
void CLASS nikon_compressed_load_raw()
1004
static const uchar nikon_tree[] =
1005
{ 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0,
1006
5,4,3,6,2,7,1,0,8,9,11,10,12 };
1007
int csize, row, col, diff, i;
1008
ushort vpred[4], hpred[2], *curve;
1011
make_decoder (nikon_tree, 0);
1013
fseek (ifp, curve_offset, SEEK_SET);
1014
read_shorts (vpred, 4);
1016
curve = (ushort *) calloc (csize, sizeof *curve);
1017
merror (curve, "nikon_compressed_load_raw()");
1018
read_shorts (curve, csize);
1020
fseek (ifp, data_offset, SEEK_SET);
1022
for (row=0; row < height; row++)
1023
for (col=0; col < raw_width; col++) {
1024
diff = ljpeg_diff (first_decode);
1026
i = 2*(row & 1) + (col & 1);
1028
hpred[col] = vpred[i];
1030
hpred[col & 1] += diff;
1031
if ((unsigned) (col-left_margin) >= width) continue;
1032
diff = hpred[col & 1];
1033
if (diff >= csize) diff = csize-1;
1034
BAYER(row,col-left_margin) = curve[diff];
1039
void CLASS nikon_load_raw()
1041
int irow, row, col, i;
1044
for (irow=0; irow < height; irow++) {
1046
if (make[0] == 'O' || model[0] == 'E') {
1047
row = irow * 2 % height + irow / (height/2);
1048
if (row == 1 && data_offset == 0) {
1049
fseek (ifp, 0, SEEK_END);
1050
fseek (ifp, ftell(ifp)/2, SEEK_SET);
1054
for (col=0; col < raw_width; col++) {
1056
if ((unsigned) (col-left_margin) < width)
1057
BAYER(row,col-left_margin) = i;
1058
if (tiff_compress == 34713 && (col % 10) == 9)
1065
Figure out if a NEF file is compressed. These fancy heuristics
1066
are only needed for the D100, thanks to a bug in some cameras
1067
that tags all images as "compressed".
1069
int CLASS nikon_is_compressed()
1074
if (tiff_compress != 34713)
1076
if (strcmp(model,"D100"))
1078
fseek (ifp, data_offset, SEEK_SET);
1079
fread (test, 1, 256, ifp);
1080
for (i=15; i < 256; i+=16)
1081
if (test[i]) return 1;
1086
Returns 1 for a Coolpix 995, 0 for anything else.
1088
int CLASS nikon_e995()
1091
const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
1093
memset (histo, 0, sizeof histo);
1094
fseek (ifp, -2000, SEEK_END);
1095
for (i=0; i < 2000; i++)
1096
histo[fgetc(ifp)]++;
1097
for (i=0; i < 4; i++)
1098
if (histo[often[i]] < 200)
1104
Returns 1 for a Coolpix 2100, 0 for anything else.
1106
int CLASS nikon_e2100()
1111
fseek (ifp, 0, SEEK_SET);
1112
for (i=0; i < 1024; i++) {
1113
fread (t, 1, 12, ifp);
1114
if (((t[2] & t[4] & t[7] & t[9]) >> 4
1115
& t[1] & t[6] & t[8] & t[11] & 3) != 3)
1121
void CLASS nikon_3700()
1125
static const struct {
1127
char make[12], model[15];
1129
{ 0x00, "PENTAX", "Optio 33WR" },
1130
{ 0x03, "NIKON", "E3200" },
1131
{ 0x32, "NIKON", "E3700" },
1132
{ 0x33, "OLYMPUS", "C740UZ" } };
1134
fseek (ifp, 3072, SEEK_SET);
1135
fread (dp, 1, 24, ifp);
1136
bits = (dp[8] & 3) << 4 | (dp[20] & 3);
1137
for (i=0; i < sizeof table / sizeof *table; i++)
1138
if (bits == table[i].bits) {
1139
strcpy (make, table[i].make );
1140
strcpy (model, table[i].model);
1145
Separates a Minolta DiMAGE Z2 from a Nikon E4300.
1147
int CLASS minolta_z2()
1152
fseek (ifp, -sizeof tail, SEEK_END);
1153
fread (tail, 1, sizeof tail, ifp);
1154
for (i=0; i < sizeof tail; i++)
1155
if (tail[i]) return 1;
1159
/* Here raw_width is in bytes, not pixels. */
1160
void CLASS nikon_e900_load_raw()
1162
int offset=0, irow, row, col;
1164
for (irow=0; irow < height; irow++) {
1165
row = irow * 2 % height;
1167
offset = - (-offset & -4096);
1168
fseek (ifp, offset, SEEK_SET);
1169
offset += raw_width;
1171
for (col=0; col < width; col++)
1172
BAYER(row,col) = getbits(10);
1176
void CLASS nikon_e2100_load_raw()
1178
uchar data[3456], *dp;
1179
ushort pixel[2304], *pix;
1182
for (row=0; row <= height; row+=2) {
1183
if (row == height) {
1184
fseek (ifp, ((width==1616) << 13) - (-ftell(ifp) & -2048), SEEK_SET);
1187
fread (data, 1, width*3/2, ifp);
1188
for (dp=data, pix=pixel; pix < pixel+width; dp+=12, pix+=8) {
1189
pix[0] = (dp[2] >> 4) + (dp[ 3] << 4);
1190
pix[1] = (dp[2] << 8) + dp[ 1];
1191
pix[2] = (dp[7] >> 4) + (dp[ 0] << 4);
1192
pix[3] = (dp[7] << 8) + dp[ 6];
1193
pix[4] = (dp[4] >> 4) + (dp[ 5] << 4);
1194
pix[5] = (dp[4] << 8) + dp[11];
1195
pix[6] = (dp[9] >> 4) + (dp[10] << 4);
1196
pix[7] = (dp[9] << 8) + dp[ 8];
1198
for (col=0; col < width; col++)
1199
BAYER(row,col) = (pixel[col] & 0xfff);
1204
The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
1206
void CLASS fuji_load_raw()
1211
fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
1212
pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1213
merror (pixel, "fuji_load_raw()");
1214
for (row=0; row < raw_height; row++) {
1215
read_shorts (pixel, raw_width);
1216
for (col=0; col < fuji_width << !fuji_layout; col++) {
1218
r = fuji_width - 1 - col + (row >> 1);
1219
c = col + ((row+1) >> 1);
1221
r = fuji_width - 1 + row - (col >> 1);
1222
c = row + ((col+1) >> 1);
1224
BAYER(r,c) = pixel[col];
1230
void CLASS jpeg_thumb (FILE *tfp);
1232
void CLASS ppm_thumb (FILE *tfp)
1235
thumb_length = thumb_width*thumb_height*3;
1236
thumb = (char *) malloc (thumb_length);
1237
merror (thumb, "ppm_thumb()");
1238
fprintf (tfp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1239
fread (thumb, 1, thumb_length, ifp);
1240
fwrite (thumb, 1, thumb_length, tfp);
1244
void CLASS layer_thumb (FILE *tfp)
1247
char *thumb, map[][4] = { "012","102" };
1249
colors = thumb_misc >> 5 & 7;
1250
thumb_length = thumb_width*thumb_height;
1251
thumb = (char *) malloc (thumb_length*colors);
1252
merror (thumb, "layer_thumb()");
1253
fprintf (tfp, "P%d\n%d %d\n255\n",
1254
5 + (colors >> 1), thumb_width, thumb_height);
1255
fread (thumb, thumb_length, colors, ifp);
1256
for (i=0; i < thumb_length; i++)
1257
FORCC putc (thumb[i+thumb_length*(map[thumb_misc >> 8][c]-'0')], tfp);
1261
void CLASS rollei_thumb (FILE *tfp)
1263
int i, size = thumb_width * thumb_height;
1264
ushort *thumb = (ushort *) calloc (size, 2);
1265
merror (thumb, "rollei_thumb()");
1266
fprintf (tfp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1267
read_shorts (thumb, size);
1268
for (i=0; i < size; i++) {
1269
putc (thumb[i] << 3, tfp);
1270
putc (thumb[i] >> 5 << 2, tfp);
1271
putc (thumb[i] >> 11 << 3, tfp);
1276
void CLASS rollei_load_raw()
1279
unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
1281
isix = raw_width * raw_height * 5 / 8;
1282
while (fread (pixel, 1, 10, ifp) == 10) {
1283
for (i=0; i < 10; i+=2) {
1285
todo[i+1] = pixel[i] << 8 | pixel[i+1];
1286
buffer = pixel[i] >> 2 | buffer << 6;
1288
for ( ; i < 16; i+=2) {
1290
todo[i+1] = buffer >> (14-i)*5;
1292
for (i=0; i < 16; i+=2) {
1293
row = todo[i] / raw_width - top_margin;
1294
col = todo[i] % raw_width - left_margin;
1295
if (row < height && col < width)
1296
BAYER(row,col) = (todo[i+1] & 0x3ff);
1302
int CLASS bayer (unsigned row, unsigned col)
1304
return (row < height && col < width) ? BAYER(row,col) : 0;
1307
void CLASS phase_one_flat_field (int is_float, int nc)
1310
unsigned wide, y, x, c, rend, cend, row, col;
1311
float *mrow, num, mult[4];
1313
read_shorts (head, 8);
1314
wide = head[2] / head[4];
1315
mrow = (float *) calloc (nc*wide, sizeof *mrow);
1316
merror (mrow, "phase_one_flat_field()");
1317
for (y=0; y < head[3] / head[5]; y++) {
1318
for (x=0; x < wide; x++)
1319
for (c=0; c < nc; c+=2) {
1320
num = is_float ? getreal(11) : get2()/32768.0;
1321
if (y==0) mrow[c*wide+x] = num;
1322
else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
1325
rend = head[1]-top_margin + y*head[5];
1326
for (row = rend-head[5]; row < height && row < rend; row++) {
1327
for (x=1; x < wide; x++) {
1328
for (c=0; c < nc; c+=2) {
1329
mult[c] = mrow[c*wide+x-1];
1330
mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
1332
cend = head[0]-left_margin + x*head[4];
1333
for (col = cend-head[4]; col < width && col < cend; col++) {
1334
c = nc > 2 ? FC(row,col) : 0;
1336
c = BAYER(row,col) * mult[c];
1337
BAYER(row,col) = LIM(c,0,65535);
1339
for (c=0; c < nc; c+=2)
1340
mult[c] += mult[c+1];
1343
for (x=0; x < wide; x++)
1344
for (c=0; c < nc; c+=2)
1345
mrow[c*wide+x] += mrow[(c+1)*wide+x];
1351
void CLASS phase_one_correct()
1353
unsigned entries, tag, data, save, col, row, type;
1354
int len, i, j, k, cip, val[4], dev[4], sum, max;
1355
int head[9], diff, mindiff=INT_MAX, off_412=0;
1356
static const signed char dir[12][2] =
1357
{ {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
1358
{-2,-2}, {-2,2}, {2,-2}, {2,2} };
1359
float poly[8], num, cfrac, frac, mult[2], *yval[2];
1360
ushort curve[0x10000], *xval[2];
1362
if (shrink || !meta_length) return;
1363
if (verbose) fprintf (stderr,_("Phase One correction...\n"));
1364
fseek (ifp, meta_offset, SEEK_SET);
1366
fseek (ifp, 6, SEEK_CUR);
1367
fseek (ifp, meta_offset+get4(), SEEK_SET);
1368
entries = get4(); get4();
1374
fseek (ifp, meta_offset+data, SEEK_SET);
1375
if (tag == 0x419) { /* Polynomial curve */
1376
for (get4(), i=0; i < 8; i++)
1377
poly[i] = getreal(11);
1378
poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
1379
for (i=0; i < 0x10000; i++) {
1380
num = (poly[5]*i + poly[3])*i + poly[1];
1381
curve[i] = LIM(num,0,65535);
1382
} goto apply; /* apply to right half */
1383
} else if (tag == 0x41a) { /* Polynomial curve */
1384
for (i=0; i < 4; i++)
1385
poly[i] = getreal(11);
1386
for (i=0; i < 0x10000; i++) {
1387
for (num=0, j=4; j--; )
1388
num = num * i + poly[j];
1389
curve[i] = LIM(num+i,0,65535);
1390
} apply: /* apply to whole image */
1391
for (row=0; row < height; row++)
1392
for (col = (tag & 1)*ph1.split_col; col < width; col++)
1393
BAYER(row,col) = curve[BAYER(row,col)];
1394
} else if (tag == 0x400) { /* Sensor defects */
1395
while ((len -= 8) >= 0) {
1396
col = get2() - left_margin;
1397
row = get2() - top_margin;
1398
type = get2(); get2();
1399
if (col >= width) continue;
1400
if (type == 131) /* Bad column */
1401
for (row=0; row < height; row++)
1402
if (FC(row,col) == 1) {
1403
for (sum=i=0; i < 4; i++)
1404
sum += val[i] = bayer (row+dir[i][0], col+dir[i][1]);
1405
for (max=i=0; i < 4; i++) {
1406
dev[i] = abs((val[i] << 2) - sum);
1407
if (dev[max] < dev[i]) max = i;
1409
BAYER(row,col) = (sum - val[max])/3.0 + 0.5;
1411
for (sum=0, i=8; i < 12; i++)
1412
sum += bayer (row+dir[i][0], col+dir[i][1]);
1413
BAYER(row,col) = 0.5 + sum * 0.0732233 +
1414
(bayer(row,col-2) + bayer(row,col+2)) * 0.3535534;
1416
else if (type == 129) { /* Bad pixel */
1417
if (row >= height) continue;
1418
j = (FC(row,col) != 1) * 4;
1419
for (sum=0, i=j; i < j+8; i++)
1420
sum += bayer (row+dir[i][0], col+dir[i][1]);
1421
BAYER(row,col) = (sum + 4) >> 3;
1424
} else if (tag == 0x401) { /* All-color flat fields */
1425
phase_one_flat_field (1, 2);
1426
} else if (tag == 0x416 || tag == 0x410) {
1427
phase_one_flat_field (0, 2);
1428
} else if (tag == 0x40b) { /* Red+blue flat field */
1429
phase_one_flat_field (0, 4);
1430
} else if (tag == 0x412) {
1431
fseek (ifp, 36, SEEK_CUR);
1432
diff = abs (get2() - ph1.tag_21a);
1433
if (mindiff > diff) {
1435
off_412 = ftell(ifp) - 38;
1438
fseek (ifp, save, SEEK_SET);
1441
fseek (ifp, off_412, SEEK_SET);
1442
for (i=0; i < 9; i++) head[i] = get4();
1443
yval[0] = (float *) calloc (head[1]*head[3] + head[2]*head[4], 6);
1444
merror (yval[0], "phase_one_correct()");
1445
yval[1] = (float *) (yval[0] + head[1]*head[3]);
1446
xval[0] = (ushort *) (yval[1] + head[2]*head[4]);
1447
xval[1] = (ushort *) (xval[0] + head[1]*head[3]);
1449
for (i=0; i < 2; i++)
1450
for (j=0; j < head[i+1]*head[i+3]; j++)
1451
yval[i][j] = getreal(11);
1452
for (i=0; i < 2; i++)
1453
for (j=0; j < head[i+1]*head[i+3]; j++)
1454
xval[i][j] = get2();
1455
for (row=0; row < height; row++)
1456
for (col=0; col < width; col++) {
1457
cfrac = (float) col * head[3] / raw_width;
1458
cfrac -= cip = cfrac;
1459
num = BAYER(row,col) * 0.5;
1460
for (i=cip; i < cip+2; i++) {
1461
for (k=j=0; j < head[1]; j++)
1462
if (num < xval[0][k = head[1]*i+j]) break;
1463
frac = (j == 0 || j == head[1]) ? 0 :
1464
(xval[0][k] - num) / (xval[0][k] - xval[0][k-1]);
1465
mult[i-cip] = yval[0][k-1] * frac + yval[0][k] * (1-frac);
1467
i = ((mult[0] * (1-cfrac) + mult[1] * cfrac)
1468
* (row + top_margin) + num) * 2;
1469
BAYER(row,col) = LIM(i,0,65535);
1475
void CLASS phase_one_load_raw()
1478
ushort *pixel, akey, bkey, mask;
1480
fseek (ifp, ph1.key_off, SEEK_SET);
1483
mask = ph1.format == 1 ? 0x5555:0x1354;
1484
fseek (ifp, data_offset + top_margin*raw_width*2, SEEK_SET);
1485
pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1486
merror (pixel, "phase_one_load_raw()");
1487
for (row=0; row < height; row++) {
1488
read_shorts (pixel, raw_width);
1489
for (col=0; col < raw_width; col+=2) {
1490
a = pixel[col+0] ^ akey;
1491
b = pixel[col+1] ^ bkey;
1492
pixel[col+0] = (a & mask) | (b & ~mask);
1493
pixel[col+1] = (b & mask) | (a & ~mask);
1495
for (col=0; col < width; col++)
1496
BAYER(row,col) = pixel[col+left_margin];
1499
phase_one_correct();
1502
unsigned CLASS ph1_bits (int nbits)
1504
static UINT64 bitbuf=0;
1508
return bitbuf = vbits = 0;
1509
if (vbits < nbits) {
1510
bitbuf = bitbuf << 32 | (unsigned) get4();
1514
return bitbuf << (64 - nbits - vbits) >> (64 - nbits);
1517
void CLASS phase_one_load_raw_c()
1519
static const int length[] = { 8,7,6,9,11,10,5,12,14,13 };
1520
int *offset, len[2], pred[2], row, col, i, j;
1524
pixel = (ushort *) calloc (raw_width + raw_height*4, 2);
1525
merror (pixel, "phase_one_load_raw_c()");
1526
offset = (int *) (pixel + raw_width);
1527
fseek (ifp, strip_offset, SEEK_SET);
1528
for (row=0; row < raw_height; row++)
1529
offset[row] = get4();
1530
black = (short (*)[2]) offset + raw_height;
1531
fseek (ifp, ph1.black_off, SEEK_SET);
1533
read_shorts ((ushort *) black[0], raw_height*2);
1534
for (i=0; i < 256; i++)
1535
curve[i] = i*i / 3.969 + 0.5;
1536
for (row=0; row < raw_height; row++) {
1537
fseek (ifp, data_offset + offset[row], SEEK_SET);
1539
pred[0] = pred[1] = 0;
1540
for (col=0; col < raw_width; col++) {
1541
if (col >= (raw_width & -8))
1542
len[0] = len[1] = 14;
1543
else if ((col & 7) == 0)
1544
for (i=0; i < 2; i++) {
1545
for (j=0; j < 5 && !ph1_bits(1); j++);
1546
if (j--) len[i] = length[j*2 + ph1_bits(1)];
1548
if ((i = len[col & 1]) == 14)
1549
pixel[col] = pred[col & 1] = ph1_bits(16);
1551
pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
1552
if (ph1.format == 5 && pixel[col] < 256)
1553
pixel[col] = curve[pixel[col]];
1555
if ((unsigned) (row-top_margin) < height)
1556
for (col=0; col < width; col++) {
1557
i = (pixel[col+left_margin] << 2)
1558
- ph1.black + black[row][col >= ph1.split_col];
1559
if (i > 0) BAYER(row-top_margin,col) = i;
1563
phase_one_correct();
1564
maximum = 0xfffc - ph1.black;
1567
void CLASS leaf_hdr_load_raw()
1570
unsigned tile=0, r, c, row, col;
1572
pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1573
merror (pixel, "leaf_hdr_load_raw()");
1574
for (c=0; c < tiff_samples; c++) {
1575
for (r=0; r < raw_height; r++) {
1576
if (r % tile_length == 0) {
1577
fseek (ifp, data_offset + 4*tile++, SEEK_SET);
1578
fseek (ifp, get4() + 2*left_margin, SEEK_SET);
1580
if (filters && c != shot_select) continue;
1581
read_shorts (pixel, raw_width);
1582
if ((row = r - top_margin) >= height) continue;
1583
for (col=0; col < width; col++)
1584
if (filters) BAYER(row,col) = pixel[col];
1585
else image[row*width+col][c] = pixel[col];
1595
void CLASS unpacked_load_raw();
1597
void CLASS sinar_4shot_load_raw()
1600
unsigned shot, row, col, r, c;
1602
if ((shot = shot_select) || shrink) {
1604
if (shot > 3) shot = 3;
1605
fseek (ifp, data_offset + shot*4, SEEK_SET);
1606
fseek (ifp, get4(), SEEK_SET);
1607
unpacked_load_raw();
1610
pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1611
merror (pixel, "sinar_4shot_load_raw()");
1612
for (shot=0; shot < 4; shot++) {
1613
fseek (ifp, data_offset + shot*4, SEEK_SET);
1614
fseek (ifp, get4(), SEEK_SET);
1615
for (row=0; row < raw_height; row++) {
1616
read_shorts (pixel, raw_width);
1617
if ((r = row-top_margin - (shot >> 1 & 1)) >= height) continue;
1618
for (col=0; col < raw_width; col++) {
1619
if ((c = col-left_margin - (shot & 1)) >= width) continue;
1620
image[r*width+c][FC(row,col)] = pixel[col];
1628
void CLASS imacon_full_load_raw()
1632
for (row=0; row < height; row++)
1633
for (col=0; col < width; col++)
1634
read_shorts (image[row*width+col], 3);
1637
/* Here raw_width is in bytes, not pixels. */
1638
void CLASS packed_12_load_raw()
1643
for (row=0; row < height; row++) {
1644
for (col=0; col < width; col++)
1645
BAYER(row,col) = getbits(12);
1646
for (col = width*3/2; col < raw_width; col++)
1651
void CLASS unpacked_load_raw()
1656
fseek (ifp, (top_margin*raw_width + left_margin) * 2, SEEK_CUR);
1657
pixel = (ushort *) calloc (raw_width, sizeof *pixel);
1658
merror (pixel, "unpacked_load_raw()");
1659
for (row=0; row < height; row++) {
1660
read_shorts (pixel, raw_width);
1661
for (col=0; col < width; col++)
1662
BAYER2(row,col) = pixel[col];
1667
void CLASS panasonic_load_raw()
1669
unpacked_load_raw();
1673
void CLASS olympus_e300_load_raw()
1676
ushort *pixel, *pix;
1677
int dwide, row, col;
1679
dwide = raw_width * 16 / 10;
1680
data = (uchar *) malloc (dwide + raw_width*2);
1681
merror (data, "olympus_e300_load_raw()");
1682
pixel = (ushort *) (data + dwide);
1683
for (row=0; row < height; row++) {
1684
fread (data, 1, dwide, ifp);
1685
for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=3, pix+=2) {
1686
if (((dp-data) & 15) == 15) dp++;
1687
pix[0] = dp[1] << 8 | dp[0];
1688
pix[1] = dp[2] << 4 | dp[1] >> 4;
1690
for (col=0; col < width; col++)
1691
BAYER(row,col) = (pixel[col] & 0xfff);
1698
void CLASS olympus_cseries_load_raw()
1702
for (irow=0; irow < height; irow++) {
1703
row = irow * 2 % height + irow / (height/2);
1705
fseek (ifp, data_offset - row*(-width*height*3/4 & -2048), SEEK_SET);
1708
for (col=0; col < width; col++)
1709
BAYER(row,col) = getbits(12);
1714
void CLASS minolta_rd175_load_raw()
1717
unsigned irow, box, row, col;
1719
for (irow=0; irow < 1481; irow++) {
1720
fread (pixel, 1, 768, ifp);
1722
row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
1724
case 1477: case 1479: continue;
1725
case 1476: row = 984; break;
1726
case 1480: row = 985; break;
1727
case 1478: row = 985; box = 1;
1729
if ((box < 12) && (box & 1)) {
1730
for (col=0; col < 1533; col++, row ^= 1)
1731
if (col != 1) BAYER(row,col) = (col+1) & 2 ?
1732
pixel[col/2-1] + pixel[col/2+1] : pixel[col/2] << 1;
1733
BAYER(row,1) = pixel[1] << 1;
1734
BAYER(row,1533) = pixel[765] << 1;
1736
for (col=row & 1; col < 1534; col+=2)
1737
BAYER(row,col) = pixel[col/2] << 1;
1739
maximum = 0xff << 1;
1742
void CLASS eight_bit_load_raw()
1747
pixel = (uchar *) calloc (raw_width, sizeof *pixel);
1748
merror (pixel, "eight_bit_load_raw()");
1749
for (row=0; row < height; row++) {
1750
fread (pixel, 1, raw_width, ifp);
1751
for (col=0; col < width; col++)
1752
BAYER(row,col) = pixel[col];
1758
void CLASS casio_qv5700_load_raw()
1760
uchar data[3232], *dp;
1761
ushort pixel[2576], *pix;
1764
for (row=0; row < height; row++) {
1765
fread (data, 1, 3232, ifp);
1766
for (dp=data, pix=pixel; dp < data+3220; dp+=5, pix+=4) {
1767
pix[0] = (dp[0] << 2) + (dp[1] >> 6);
1768
pix[1] = (dp[1] << 4) + (dp[2] >> 4);
1769
pix[2] = (dp[2] << 6) + (dp[3] >> 2);
1770
pix[3] = (dp[3] << 8) + (dp[4] );
1772
for (col=0; col < width; col++)
1773
BAYER(row,col) = (pixel[col] & 0x3ff);
1778
void CLASS nucore_load_raw()
1783
pixel = (ushort *) calloc (width, 2);
1784
merror (pixel, "nucore_load_raw()");
1785
for (irow=0; irow < height; irow++) {
1786
read_shorts (pixel, width);
1787
row = irow/2 + height/2 * (irow & 1);
1788
for (col=0; col < width; col++)
1789
BAYER(row,col) = pixel[col];
1794
const int * CLASS make_decoder_int (const int *source, int level)
1798
cur = free_decode++;
1799
if (level < source[0]) {
1800
cur->branch[0] = free_decode;
1801
source = make_decoder_int (source, level+1);
1802
cur->branch[1] = free_decode;
1803
source = make_decoder_int (source, level+1);
1805
cur->leaf = source[1];
1811
int CLASS radc_token (int tree)
1814
static struct decode *dstart[18], *dindex;
1815
static const int *s, source[] = {
1816
1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
1817
1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
1818
2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
1819
2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
1820
2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
1821
2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
1822
2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
1823
2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
1824
2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
1825
2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
1828
2,-17, 2,-5, 2,5, 2,17,
1829
2,-7, 2,2, 2,9, 2,18,
1830
2,-18, 2,-9, 2,-2, 2,7,
1831
2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
1832
2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
1833
2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
1836
if (free_decode == first_decode)
1837
for (s=source, t=0; t < 18; t++) {
1838
dstart[t] = free_decode;
1839
s = make_decoder_int (s, 0);
1842
if (kodak_cbpp == 243)
1843
return (getbits(6) << 2) + 2; /* most DC50 photos */
1845
return (getbits(5) << 3) + 4; /* DC40, Fotoman Pixtura */
1847
for (dindex = dstart[tree]; dindex->branch[0]; )
1848
dindex = dindex->branch[getbits(1)];
1849
return dindex->leaf;
1852
#define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
1854
#define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
1855
: (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
1857
void CLASS kodak_radc_load_raw()
1859
int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
1860
short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
1864
for (i=0; i < sizeof(buf)/sizeof(short); i++)
1865
buf[0][0][i] = 2048;
1866
for (row=0; row < height; row+=4) {
1867
FORC3 mul[c] = getbits(6);
1869
val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
1870
s = val > 65564 ? 10:12;
1873
for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
1874
buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
1876
for (r=0; r <= !c; r++) {
1877
buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
1878
for (tree=1, col=width/2; col > 0; ) {
1879
if ((tree = radc_token(tree))) {
1882
FORYX buf[c][y][x] = radc_token(tree+10) * mul[c];
1884
FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
1887
nreps = (col > 2) ? radc_token(9) + 1 : 1;
1888
for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
1890
FORYX buf[c][y][x] = PREDICTOR;
1892
step = radc_token(10) << 4;
1893
FORYX buf[c][y][x] += step;
1896
} while (nreps == 9);
1898
for (y=0; y < 2; y++)
1899
for (x=0; x < width/2; x++) {
1900
val = (buf[c][y+1][x] << 4) / mul[c];
1901
if (val < 0) val = 0;
1903
BAYER(row+y*2+c-1,x*2+2-c) = val;
1905
BAYER(row+r*2+y,x*2+y) = val;
1907
memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
1910
for (y=row; y < row+4; y++)
1911
for (x=0; x < width; x++)
1913
val = (BAYER(y,x)-2048)*2 + (BAYER(y,x-1)+BAYER(y,x+1))/2;
1914
if (val < 0) val = 0;
1925
void CLASS kodak_jpeg_load_raw() {}
1929
fill_input_buffer (j_decompress_ptr cinfo)
1931
static uchar jpeg_buffer[4096];
1934
nbytes = fread (jpeg_buffer, 1, 4096, ifp);
1935
swab ((const char*)jpeg_buffer, (char*)jpeg_buffer, nbytes);
1936
cinfo->src->next_input_byte = jpeg_buffer;
1937
cinfo->src->bytes_in_buffer = nbytes;
1941
void CLASS kodak_jpeg_load_raw()
1943
struct jpeg_decompress_struct cinfo;
1944
struct jpeg_error_mgr jerr;
1946
JSAMPLE (*pixel)[3];
1949
cinfo.err = jpeg_std_error (&jerr);
1950
jpeg_create_decompress (&cinfo);
1951
jpeg_stdio_src (&cinfo, ifp);
1952
cinfo.src->fill_input_buffer = fill_input_buffer;
1953
jpeg_read_header (&cinfo, TRUE);
1954
jpeg_start_decompress (&cinfo);
1955
if ((cinfo.output_width != width ) ||
1956
(cinfo.output_height*2 != height ) ||
1957
(cinfo.output_components != 3 )) {
1958
fprintf (stderr,_("%s: incorrect JPEG dimensions\n"), ifname);
1959
jpeg_destroy_decompress (&cinfo);
1960
longjmp (failure, 3);
1962
buf = (*cinfo.mem->alloc_sarray)
1963
((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
1965
while (cinfo.output_scanline < cinfo.output_height) {
1966
row = cinfo.output_scanline * 2;
1967
jpeg_read_scanlines (&cinfo, buf, 1);
1968
pixel = (JSAMPLE (*)[3]) buf[0];
1969
for (col=0; col < width; col+=2) {
1970
BAYER(row+0,col+0) = pixel[col+0][1] << 1;
1971
BAYER(row+1,col+1) = pixel[col+1][1] << 1;
1972
BAYER(row+0,col+1) = pixel[col][0] + pixel[col+1][0];
1973
BAYER(row+1,col+0) = pixel[col][2] + pixel[col+1][2];
1976
jpeg_finish_decompress (&cinfo);
1977
jpeg_destroy_decompress (&cinfo);
1978
maximum = 0xff << 1;
1982
void CLASS kodak_dc120_load_raw()
1984
static const int mul[4] = { 162, 192, 187, 92 };
1985
static const int add[4] = { 0, 636, 424, 212 };
1987
int row, shift, col;
1989
for (row=0; row < height; row++) {
1990
fread (pixel, 848, 1, ifp);
1991
shift = row * mul[row & 3] + add[row & 3];
1992
for (col=0; col < width; col++)
1993
BAYER(row,col) = (ushort) pixel[(col + shift) % 848];
1998
void CLASS kodak_easy_load_raw()
2001
unsigned row, col, val;
2003
if (raw_width > width)
2005
pixel = (uchar *) calloc (raw_width, sizeof *pixel);
2006
merror (pixel, "kodak_easy_load_raw()");
2007
for (row=0; row < height; row++) {
2008
fread (pixel, 1, raw_width, ifp);
2009
for (col=0; col < raw_width; col++) {
2010
val = curve[pixel[col]];
2011
if ((unsigned) (col-left_margin) < width)
2012
BAYER(row,col-left_margin) = val;
2017
if (raw_width > width)
2018
black /= (raw_width - width) * height;
2019
if (!strncmp(model,"DC2",3))
2021
maximum = curve[0xff];
2024
void CLASS kodak_262_load_raw()
2026
static const uchar kodak_tree[2][26] =
2027
{ { 0,1,5,1,1,2,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9 },
2028
{ 0,3,1,1,1,1,1,2,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9 } };
2029
struct decode *decode[2];
2031
int *strip, ns, i, row, col, chess, pi=0, pi1, pi2, pred, val;
2034
for (i=0; i < 2; i++) {
2035
decode[i] = free_decode;
2036
make_decoder (kodak_tree[i], 0);
2038
ns = (raw_height+63) >> 5;
2039
pixel = (uchar *) malloc (raw_width*32 + ns*4);
2040
merror (pixel, "kodak_262_load_raw()");
2041
strip = (int *) (pixel + raw_width*32);
2043
for (i=0; i < ns; i++)
2045
for (row=0; row < raw_height; row++) {
2046
if ((row & 31) == 0) {
2047
fseek (ifp, strip[row >> 5], SEEK_SET);
2051
for (col=0; col < raw_width; col++) {
2052
chess = (row + col) & 1;
2053
pi1 = chess ? pi-2 : pi-raw_width-1;
2054
pi2 = chess ? pi-2*raw_width : pi-raw_width+1;
2055
if (col <= chess) pi1 = -1;
2056
if (pi1 < 0) pi1 = pi2;
2057
if (pi2 < 0) pi2 = pi1;
2058
if (pi1 < 0 && col > 1) pi1 = pi2 = pi-2;
2059
pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
2060
pixel[pi] = pred + ljpeg_diff (decode[chess]);
2061
val = curve[pixel[pi++]];
2062
if ((unsigned) (col-left_margin) < width)
2063
BAYER(row,col-left_margin) = val;
2068
if (raw_width > width)
2069
black /= (raw_width - width) * height;
2072
int CLASS kodak_65000_decode (short *out, int bsize)
2077
int save, bits=0, i, j, len, diff;
2080
bsize = (bsize + 3) & -4;
2081
for (i=0; i < bsize; i+=2) {
2083
if ((blen[i ] = c & 15) > 12 ||
2084
(blen[i+1] = c >> 4) > 12 ) {
2085
fseek (ifp, save, SEEK_SET);
2086
for (i=0; i < bsize; i+=8) {
2087
read_shorts (raw, 6);
2088
out[i ] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
2089
out[i+1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
2090
for (j=0; j < 6; j++)
2091
out[i+2+j] = raw[j] & 0xfff;
2096
if ((bsize & 7) == 4) {
2097
bitbuf = fgetc(ifp) << 8;
2098
bitbuf += fgetc(ifp);
2101
for (i=0; i < bsize; i++) {
2104
for (j=0; j < 32; j+=8)
2105
bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
2108
diff = bitbuf & (0xffff >> (16-len));
2111
if ((diff & (1 << (len-1))) == 0)
2112
diff -= (1 << len) - 1;
2118
void CLASS kodak_65000_load_raw()
2121
int row, col, len, pred[2], ret, i;
2123
for (row=0; row < height; row++)
2124
for (col=0; col < width; col+=256) {
2125
pred[0] = pred[1] = 0;
2126
len = MIN (256, width-col);
2127
ret = kodak_65000_decode (buf, len);
2128
for (i=0; i < len; i++)
2129
BAYER(row,col+i) = curve[ret ? buf[i] : (pred[i & 1] += buf[i])];
2133
void CLASS kodak_ycbcr_load_raw()
2135
short buf[384], *bp;
2136
int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
2139
for (row=0; row < height; row+=2)
2140
for (col=0; col < width; col+=128) {
2141
len = MIN (128, width-col);
2142
kodak_65000_decode (buf, len*3);
2143
y[0][1] = y[1][1] = cb = cr = 0;
2144
for (bp=buf, i=0; i < len; i+=2, bp+=2) {
2147
rgb[1] = -((cb + cr + 2) >> 2);
2148
rgb[2] = rgb[1] + cb;
2149
rgb[0] = rgb[1] + cr;
2150
for (j=0; j < 2; j++)
2151
for (k=0; k < 2; k++) {
2152
y[j][k] = y[j][k^1] + *bp++;
2153
ip = image[(row+j)*width + col+i+k];
2154
FORC3 ip[c] = curve[LIM(y[j][k]+rgb[c], 0, 0xfff)];
2160
void CLASS kodak_rgb_load_raw()
2162
short buf[768], *bp;
2163
int row, col, len, c, i, rgb[3];
2164
ushort *ip=image[0];
2166
for (row=0; row < height; row++)
2167
for (col=0; col < width; col+=256) {
2168
len = MIN (256, width-col);
2169
kodak_65000_decode (buf, len*3);
2170
memset (rgb, 0, sizeof rgb);
2171
for (bp=buf, i=0; i < len; i++, ip+=4)
2172
FORC3 ip[c] = (rgb[c] += *bp++) & 0xfff;
2176
void CLASS kodak_thumb_load_raw()
2179
colors = thumb_misc >> 5;
2180
for (row=0; row < height; row++)
2181
for (col=0; col < width; col++)
2182
read_shorts (image[row*width+col], colors);
2183
maximum = (1 << (thumb_misc & 31)) - 1;
2186
void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
2188
static unsigned pad[128], p;
2191
for (p=0; p < 4; p++)
2192
pad[p] = key = key * 48828125 + 1;
2193
pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
2194
for (p=4; p < 127; p++)
2195
pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
2196
for (p=0; p < 127; p++)
2197
pad[p] = htonl(pad[p]);
2200
*data++ ^= pad[p++ & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
2203
void CLASS sony_load_raw()
2207
unsigned i, key, row, col;
2209
fseek (ifp, 200896, SEEK_SET);
2210
fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
2213
fseek (ifp, 164600, SEEK_SET);
2214
fread (head, 1, 40, ifp);
2215
sony_decrypt ((unsigned int *) head, 10, 1, key);
2216
for (i=26; i-- > 22; )
2217
key = key << 8 | head[i];
2218
fseek (ifp, data_offset, SEEK_SET);
2219
pixel = (ushort *) calloc (raw_width, sizeof *pixel);
2220
merror (pixel, "sony_load_raw()");
2221
for (row=0; row < height; row++) {
2222
fread (pixel, 2, raw_width, ifp);
2223
sony_decrypt ((unsigned int *) pixel, raw_width/2, !row, key);
2224
for (col=9; col < left_margin; col++)
2225
black += ntohs(pixel[col]);
2226
for (col=0; col < width; col++)
2227
BAYER(row,col) = ntohs(pixel[col+left_margin]);
2230
if (left_margin > 9)
2231
black /= (left_margin-9) * height;
2235
void CLASS sony_arw_load_raw()
2237
int col, row, len, diff, sum=0;
2240
for (col = raw_width; col--; )
2241
for (row=0; row < raw_height+1; row+=2) {
2242
if (row == raw_height) row = 1;
2243
len = 4 - getbits(2);
2244
if (len == 3 && getbits(1)) len = 0;
2246
while (len < 17 && !getbits(1)) len++;
2247
diff = getbits(len);
2248
if ((diff & (1 << (len-1))) == 0)
2249
diff -= (1 << len) - 1;
2251
if (row < height) BAYER(row,col) = sum;
2255
#define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
2257
/* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
2258
void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
2260
uchar hist[3][13] = {
2261
{ 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2262
{ 7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0 },
2263
{ 3, 3, 0, 0, 63, 47, 31, 15, 0 } };
2264
int low, high=0xff, carry=0, nbits=8;
2265
int s, count, bin, next, i, sym[3];
2266
uchar diff, pred[]={0,0};
2267
ushort data=0, range=0;
2268
unsigned pix, row, col;
2270
fseek (ifp, seg[0][1]+1, SEEK_SET);
2272
for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
2273
for (s=0; s < 3; s++) {
2274
data = data << nbits | getbits(nbits);
2276
carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
2277
while (--nbits >= 0)
2278
if ((data >> nbits & 0xff) == 0xff) break;
2280
data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
2281
((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
2286
count = ((((data-range+1) & 0xffff) << 2) - 1) / (high >> 4);
2287
for (bin=0; hist[s][bin+5] > count; bin++);
2288
low = hist[s][bin+5] * (high >> 4) >> 2;
2289
if (bin) high = hist[s][bin+4] * (high >> 4) >> 2;
2291
for (nbits=0; high << nbits < 128; nbits++);
2292
range = (range+low) << nbits;
2295
if (++hist[s][2] > hist[s][3]) {
2296
next = (next+1) & hist[s][0];
2297
hist[s][3] = (hist[s][next+4] - hist[s][next+5]) >> 2;
2300
if (hist[s][hist[s][1]+4] - hist[s][hist[s][1]+5] > 1) {
2301
if (bin < hist[s][1])
2302
for (i=bin; i < hist[s][1]; i++) hist[s][i+5]--;
2303
else if (next <= bin)
2304
for (i=hist[s][1]; i < bin; i++) hist[s][i+5]++;
2309
diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
2311
diff = diff ? -diff : 0x80;
2312
if (ftell(ifp) + 12 >= seg[1][1])
2314
pred[pix & 1] += diff;
2315
row = pix / raw_width - top_margin;
2316
col = pix % raw_width - left_margin;
2317
if (row < height && col < width)
2318
BAYER(row,col) = pred[pix & 1];
2319
if (!(pix & 1) && HOLE(row)) pix += 2;
2324
void CLASS smal_v6_load_raw()
2328
fseek (ifp, 16, SEEK_SET);
2331
seg[1][0] = raw_width * raw_height;
2332
seg[1][1] = INT_MAX;
2333
smal_decode_segment (seg, 0);
2337
int CLASS median4 (int *p)
2339
int min, max, sum, i;
2341
min = max = sum = p[0];
2342
for (i=1; i < 4; i++) {
2344
if (min > p[i]) min = p[i];
2345
if (max < p[i]) max = p[i];
2347
return (sum - min - max) >> 1;
2350
void CLASS fill_holes (int holes)
2352
int row, col, val[4];
2354
for (row=2; row < height-2; row++) {
2355
if (!HOLE(row)) continue;
2356
for (col=1; col < width-1; col+=4) {
2357
val[0] = BAYER(row-1,col-1);
2358
val[1] = BAYER(row-1,col+1);
2359
val[2] = BAYER(row+1,col-1);
2360
val[3] = BAYER(row+1,col+1);
2361
BAYER(row,col) = median4(val);
2363
for (col=2; col < width-2; col+=4)
2364
if (HOLE(row-2) || HOLE(row+2))
2365
BAYER(row,col) = (BAYER(row,col-2) + BAYER(row,col+2)) >> 1;
2367
val[0] = BAYER(row,col-2);
2368
val[1] = BAYER(row,col+2);
2369
val[2] = BAYER(row-2,col);
2370
val[3] = BAYER(row+2,col);
2371
BAYER(row,col) = median4(val);
2376
void CLASS smal_v9_load_raw()
2378
unsigned seg[256][2], offset, nseg, holes, i;
2380
fseek (ifp, 67, SEEK_SET);
2383
fseek (ifp, offset, SEEK_SET);
2384
for (i=0; i < nseg*2; i++)
2385
seg[0][i] = get4() + data_offset*(i & 1);
2386
fseek (ifp, 78, SEEK_SET);
2388
fseek (ifp, 88, SEEK_SET);
2389
seg[nseg][0] = raw_height * raw_width;
2390
seg[nseg][1] = get4() + data_offset;
2391
for (i=0; i < nseg; i++)
2392
smal_decode_segment (seg+i, holes);
2393
if (holes) fill_holes (holes);
2396
/* BEGIN GPL BLOCK */
2398
void CLASS foveon_decoder (unsigned size, unsigned code)
2400
static unsigned huff[1024];
2405
for (i=0; i < size; i++)
2409
cur = free_decode++;
2410
if (free_decode > first_decode+2048) {
2411
fprintf (stderr,_("%s: decoder table overflow\n"), ifname);
2412
longjmp (failure, 2);
2415
for (i=0; i < size; i++)
2416
if (huff[i] == code) {
2420
if ((len = code >> 27) > 26) return;
2421
code = (len+1) << 27 | (code & 0x3ffffff) << 1;
2423
cur->branch[0] = free_decode;
2424
foveon_decoder (size, code);
2425
cur->branch[1] = free_decode;
2426
foveon_decoder (size, code+1);
2429
void CLASS foveon_thumb (FILE *tfp)
2431
int bwide, row, col, bit=-1, c, i;
2433
struct decode *dindex;
2438
fprintf (tfp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
2440
buf = (char *) malloc (bwide);
2441
merror (buf, "foveon_thumb()");
2442
for (row=0; row < thumb_height; row++) {
2443
fread (buf, 1, bwide, ifp);
2444
fwrite (buf, 3, thumb_width, tfp);
2449
foveon_decoder (256, 0);
2451
for (row=0; row < thumb_height; row++) {
2452
memset (pred, 0, sizeof pred);
2454
for (col=bit=0; col < thumb_width; col++)
2456
for (dindex=first_decode; dindex->branch[0]; ) {
2457
if ((bit = (bit-1) & 31) == 31)
2458
for (i=0; i < 4; i++)
2459
bitbuf = (bitbuf << 8) + fgetc(ifp);
2460
dindex = dindex->branch[bitbuf >> bit & 1];
2462
pred[c] += dindex->leaf;
2463
fputc (pred[c], tfp);
2468
void CLASS foveon_load_camf()
2470
unsigned key, i, val;
2472
fseek (ifp, meta_offset, SEEK_SET);
2474
fread (meta_data, 1, meta_length, ifp);
2475
for (i=0; i < meta_length; i++) {
2476
key = (key * 1597 + 51749) % 244944;
2477
val = key * (INT64) 301593171 >> 24;
2478
meta_data[i] ^= ((((key << 8) - val) >> 1) + val) >> 17;
2482
void CLASS foveon_load_raw()
2484
struct decode *dindex;
2485
short diff[1024], pred[3];
2487
int fixed, row, col, bit=-1, c, i;
2490
read_shorts ((ushort *) diff, 1024);
2491
if (!fixed) foveon_decoder (1024, 0);
2493
for (row=0; row < height; row++) {
2494
memset (pred, 0, sizeof pred);
2495
if (!bit && !fixed) get4();
2496
for (col=bit=0; col < width; col++) {
2499
FORC3 pred[2-c] += diff[bitbuf >> c*10 & 0x3ff];
2502
for (dindex=first_decode; dindex->branch[0]; ) {
2503
if ((bit = (bit-1) & 31) == 31)
2504
for (i=0; i < 4; i++)
2505
bitbuf = (bitbuf << 8) + fgetc(ifp);
2506
dindex = dindex->branch[bitbuf >> bit & 1];
2508
pred[c] += diff[dindex->leaf];
2510
FORC3 image[row*width+col][c] = pred[c];
2514
for (i=0; i < height*width*4; i++)
2515
if ((short) image[0][i] < 0) image[0][i] = 0;
2519
const char * CLASS foveon_camf_param (const char *block, const char *param)
2522
char *pos, *cp, *dp;
2524
for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
2525
pos = meta_data + idx;
2526
if (strncmp (pos, "CMb", 3)) break;
2527
if (pos[3] != 'P') continue;
2528
if (strcmp (block, pos+sget4(pos+12))) continue;
2529
cp = pos + sget4(pos+16);
2531
dp = pos + sget4(cp+4);
2534
if (!strcmp (param, dp+sget4(cp)))
2535
return dp+sget4(cp+4);
2541
void * CLASS foveon_camf_matrix (int dim[3], const char *name)
2543
unsigned i, idx, type, ndim, size, *mat;
2544
char *pos, *cp, *dp;
2546
for (idx=0; idx < meta_length; idx += sget4(pos+8)) {
2547
pos = meta_data + idx;
2548
if (strncmp (pos, "CMb", 3)) break;
2549
if (pos[3] != 'M') continue;
2550
if (strcmp (name, pos+sget4(pos+12))) continue;
2551
dim[0] = dim[1] = dim[2] = 1;
2552
cp = pos + sget4(pos+16);
2554
if ((ndim = sget4(cp+4)) > 3) break;
2555
dp = pos + sget4(cp+8);
2556
for (i=ndim; i--; ) {
2560
if ((size = dim[0]*dim[1]*dim[2]) > meta_length/4) break;
2561
mat = (unsigned *) malloc (size * 4);
2562
merror (mat, "foveon_camf_matrix()");
2563
for (i=0; i < size; i++)
2564
if (type && type != 6)
2565
mat[i] = sget4(dp + i*4);
2567
mat[i] = sget4(dp + i*2) & 0xffff;
2570
fprintf (stderr,_("%s: \"%s\" matrix not found!\n"), ifname, name);
2574
int CLASS foveon_fixed (void *ptr, int size, const char *name)
2579
dp = foveon_camf_matrix (dim, name);
2581
memcpy (ptr, dp, size*4);
2586
float CLASS foveon_avg (short *pix, int range[2], float cfilt)
2589
float val, min=FLT_MAX, max=-FLT_MAX, sum=0;
2591
for (i=range[0]; i <= range[1]; i++) {
2592
sum += val = pix[i*4] + (pix[i*4]-pix[(i-1)*4]) * cfilt;
2593
if (min > val) min = val;
2594
if (max < val) max = val;
2596
return (sum - min - max) / (range[1] - range[0] - 1);
2599
short * CLASS foveon_make_curve (double max, double mul, double filt)
2605
if (!filt) filt = 0.8;
2606
size = 4*M_PI*max / filt;
2607
curve = (short *) calloc (size+1, sizeof *curve);
2608
merror (curve, "foveon_make_curve()");
2610
for (i=0; i < size; i++) {
2612
curve[i+1] = (cos(x)+1)/2 * tanh(i*filt/mul) * mul + 0.5;
2617
void CLASS foveon_make_curves
2618
(short **curvep, float dq[3], float div[3], float filt)
2620
double mul[3], max=0;
2623
FORC3 mul[c] = dq[c]/div[c];
2624
FORC3 if (max < mul[c]) max = mul[c];
2625
FORC3 curvep[c] = foveon_make_curve (max, mul[c], filt);
2628
int CLASS foveon_apply_curve (short *curve, int i)
2630
if (abs(i) >= curve[0]) return 0;
2631
return i < 0 ? -curve[1-i] : curve[1+i];
2634
#define image ((short (*)[4]) image)
2636
void CLASS foveon_interpolate()
2638
static const short hood[] = { -1,-1, -1,0, -1,1, 0,-1, 0,1, 1,-1, 1,0, 1,1 };
2639
short *pix, prev[3], *curve[8], (*shrink)[3];
2640
float cfilt=0, ddft[3][3][2], ppm[3][3][3];
2641
float cam_xyz[3][3], correct[3][3], last[3][3], trans[3][3];
2642
float chroma_dq[3], color_dq[3], diag[3][3], div[3];
2643
float (*black)[3], (*sgain)[3], (*sgrow)[3];
2644
float fsum[3], val, frow, num;
2645
int row, col, c, i, j, diff, sgx, irow, sum, min, max, limit;
2646
int dim[3], dscr[2][2], dstb[4], (*smrow[7])[3], total[4], ipix[3];
2647
int work[3][3], smlast, smred, smred_p=0, dev[3];
2648
int satlev[3], keep[4], active[4];
2650
double dsum=0, trsum[3];
2655
fprintf (stderr,_("Foveon interpolation...\n"));
2657
foveon_fixed (dscr, 4, "DarkShieldColRange");
2658
foveon_fixed (ppm[0][0], 27, "PostPolyMatrix");
2659
foveon_fixed (satlev, 3, "SaturationLevel");
2660
foveon_fixed (keep, 4, "KeepImageArea");
2661
foveon_fixed (active, 4, "ActiveImageArea");
2662
foveon_fixed (chroma_dq, 3, "ChromaDQ");
2663
foveon_fixed (color_dq, 3,
2664
foveon_camf_param ("IncludeBlocks", "ColorDQ") ?
2665
"ColorDQ" : "ColorDQCamRGB");
2666
if (foveon_camf_param ("IncludeBlocks", "ColumnFilter"))
2667
foveon_fixed (&cfilt, 1, "ColumnFilter");
2669
memset (ddft, 0, sizeof ddft);
2670
if (!foveon_camf_param ("IncludeBlocks", "DarkDrift")
2671
|| !foveon_fixed (ddft[1][0], 12, "DarkDrift"))
2672
for (i=0; i < 2; i++) {
2673
foveon_fixed (dstb, 4, i ? "DarkShieldBottom":"DarkShieldTop");
2674
for (row = dstb[1]; row <= dstb[3]; row++)
2675
for (col = dstb[0]; col <= dstb[2]; col++)
2676
FORC3 ddft[i+1][c][1] += (short) image[row*width+col][c];
2677
FORC3 ddft[i+1][c][1] /= (dstb[3]-dstb[1]+1) * (dstb[2]-dstb[0]+1);
2680
if (!(cp = foveon_camf_param ("WhiteBalanceIlluminants", model2)))
2681
{ fprintf (stderr,_("%s: Invalid white balance \"%s\"\n"), ifname, model2);
2683
foveon_fixed (cam_xyz, 9, cp);
2684
foveon_fixed (correct, 9,
2685
foveon_camf_param ("WhiteBalanceCorrections", model2));
2686
memset (last, 0, sizeof last);
2687
for (i=0; i < 3; i++)
2688
for (j=0; j < 3; j++)
2689
FORC3 last[i][j] += correct[i][c] * cam_xyz[c][j];
2691
sprintf (str, "%sRGBNeutral", model2);
2692
if (foveon_camf_param ("IncludeBlocks", str))
2693
foveon_fixed (div, 3, str);
2695
#define LAST(x,y) last[(i+x)%3][(c+y)%3]
2696
for (i=0; i < 3; i++)
2697
FORC3 diag[c][i] = LAST(1,1)*LAST(2,2) - LAST(1,2)*LAST(2,1);
2699
FORC3 div[c] = diag[c][0]*0.3127 + diag[c][1]*0.329 + diag[c][2]*0.3583;
2702
FORC3 if (num < div[c]) num = div[c];
2703
FORC3 div[c] /= num;
2705
memset (trans, 0, sizeof trans);
2706
for (i=0; i < 3; i++)
2707
for (j=0; j < 3; j++)
2708
FORC3 trans[i][j] += rgb_cam[i][c] * last[c][j] * div[j];
2709
FORC3 trsum[c] = trans[c][0] + trans[c][1] + trans[c][2];
2710
dsum = (6*trsum[0] + 11*trsum[1] + 3*trsum[2]) / 20;
2711
for (i=0; i < 3; i++)
2712
FORC3 last[i][c] = trans[i][c] * dsum / trsum[i];
2713
memset (trans, 0, sizeof trans);
2714
for (i=0; i < 3; i++)
2715
for (j=0; j < 3; j++)
2716
FORC3 trans[i][j] += (i==c ? 32 : -1) * last[c][j] / 30;
2718
foveon_make_curves (curve, color_dq, div, cfilt);
2719
FORC3 chroma_dq[c] /= 3;
2720
foveon_make_curves (curve+3, chroma_dq, div, cfilt);
2721
FORC3 dsum += chroma_dq[c] / div[c];
2722
curve[6] = foveon_make_curve (dsum, dsum, cfilt);
2723
curve[7] = foveon_make_curve (dsum*2, dsum*2, cfilt);
2725
sgain = (float (*)[3]) foveon_camf_matrix (dim, "SpatialGain");
2727
sgrow = (float (*)[3]) calloc (dim[1], sizeof *sgrow);
2728
sgx = (width + dim[1]-2) / (dim[1]-1);
2730
black = (float (*)[3]) calloc (height, sizeof *black);
2731
for (row=0; row < height; row++) {
2732
for (i=0; i < 6; i++)
2733
ddft[0][0][i] = ddft[1][0][i] +
2734
row / (height-1.0) * (ddft[2][0][i] - ddft[1][0][i]);
2735
FORC3 black[row][c] =
2736
( foveon_avg (image[row*width]+c, dscr[0], cfilt) +
2737
foveon_avg (image[row*width]+c, dscr[1], cfilt) * 3
2738
- ddft[0][c][0] ) / 4 - ddft[0][c][1];
2740
memcpy (black, black+8, sizeof *black*8);
2741
memcpy (black+height-11, black+height-22, 11*sizeof *black);
2742
memcpy (last, black, sizeof last);
2744
for (row=1; row < height-1; row++) {
2745
FORC3 if (last[1][c] > last[0][c]) {
2746
if (last[1][c] > last[2][c])
2747
black[row][c] = (last[0][c] > last[2][c]) ? last[0][c]:last[2][c];
2749
if (last[1][c] < last[2][c])
2750
black[row][c] = (last[0][c] < last[2][c]) ? last[0][c]:last[2][c];
2751
memmove (last, last+1, 2*sizeof last[0]);
2752
memcpy (last[2], black[row+1], sizeof last[2]);
2754
FORC3 black[row][c] = (last[0][c] + last[1][c])/2;
2755
FORC3 black[0][c] = (black[1][c] + black[3][c])/2;
2757
val = 1 - exp(-1/24.0);
2758
memcpy (fsum, black, sizeof fsum);
2759
for (row=1; row < height; row++)
2760
FORC3 fsum[c] += black[row][c] =
2761
(black[row][c] - black[row-1][c])*val + black[row-1][c];
2762
memcpy (last[0], black[height-1], sizeof last[0]);
2763
FORC3 fsum[c] /= height;
2764
for (row = height; row--; )
2765
FORC3 last[0][c] = black[row][c] =
2766
(black[row][c] - fsum[c] - last[0][c])*val + last[0][c];
2768
memset (total, 0, sizeof total);
2769
for (row=2; row < height; row+=4)
2770
for (col=2; col < width; col+=4) {
2771
FORC3 total[c] += (short) image[row*width+col][c];
2774
for (row=0; row < height; row++)
2775
FORC3 black[row][c] += fsum[c]/2 + total[c]/(total[3]*100.0);
2777
for (row=0; row < height; row++) {
2778
for (i=0; i < 6; i++)
2779
ddft[0][0][i] = ddft[1][0][i] +
2780
row / (height-1.0) * (ddft[2][0][i] - ddft[1][0][i]);
2781
pix = image[row*width];
2782
memcpy (prev, pix, sizeof prev);
2783
frow = row / (height-1.0) * (dim[2]-1);
2784
if ((irow = frow) == dim[2]-1) irow--;
2786
for (i=0; i < dim[1]; i++)
2787
FORC3 sgrow[i][c] = sgain[ irow *dim[1]+i][c] * (1-frow) +
2788
sgain[(irow+1)*dim[1]+i][c] * frow;
2789
for (col=0; col < width; col++) {
2791
diff = pix[c] - prev[c];
2793
ipix[c] = pix[c] + floor ((diff + (diff*diff >> 14)) * cfilt
2794
- ddft[0][c][1] - ddft[0][c][0] * ((float) col/width - 0.5)
2798
work[0][c] = ipix[c] * ipix[c] >> 14;
2799
work[2][c] = ipix[c] * work[0][c] >> 14;
2800
work[1][2-c] = ipix[(c+1) % 3] * ipix[(c+2) % 3] >> 14;
2803
for (val=i=0; i < 3; i++)
2804
for ( j=0; j < 3; j++)
2805
val += ppm[c][i][j] * work[i][j];
2806
ipix[c] = floor ((ipix[c] + floor(val)) *
2807
( sgrow[col/sgx ][c] * (sgx - col%sgx) +
2808
sgrow[col/sgx+1][c] * (col%sgx) ) / sgx / div[c]);
2809
if (ipix[c] > 32000) ipix[c] = 32000;
2819
if ((badpix = (unsigned int *) foveon_camf_matrix (dim, "BadPixels"))) {
2820
for (i=0; i < dim[0]; i++) {
2821
col = (badpix[i] >> 8 & 0xfff) - keep[0];
2822
row = (badpix[i] >> 20 ) - keep[1];
2823
if ((unsigned)(row-1) > height-3 || (unsigned)(col-1) > width-3)
2825
memset (fsum, 0, sizeof fsum);
2826
for (sum=j=0; j < 8; j++)
2827
if (badpix[i] & (1 << j)) {
2828
FORC3 fsum[c] += (short)
2829
image[(row+hood[j*2])*width+col+hood[j*2+1]][c];
2832
if (sum) FORC3 image[row*width+col][c] = fsum[c]/sum;
2837
/* Array for 5x5 Gaussian averaging of red values */
2838
smrow[6] = (int (*)[3]) calloc (width*5, sizeof **smrow);
2839
merror (smrow[6], "foveon_interpolate()");
2840
for (i=0; i < 5; i++)
2841
smrow[i] = smrow[6] + i*width;
2843
/* Sharpen the reds against these Gaussian averages */
2844
for (smlast=-1, row=2; row < height-2; row++) {
2845
while (smlast < row+2) {
2846
for (i=0; i < 6; i++)
2847
smrow[(i+5) % 6] = smrow[i];
2848
pix = image[++smlast*width+2];
2849
for (col=2; col < width-2; col++) {
2851
(pix[0]*6 + (pix[-4]+pix[4])*4 + pix[-8]+pix[8] + 8) >> 4;
2855
pix = image[row*width+2];
2856
for (col=2; col < width-2; col++) {
2857
smred = ( 6 * smrow[2][col][0]
2858
+ 4 * (smrow[1][col][0] + smrow[3][col][0])
2859
+ smrow[0][col][0] + smrow[4][col][0] + 8 ) >> 4;
2862
i = pix[0] + ((pix[0] - ((smred*7 + smred_p) >> 3)) >> 3);
2863
if (i > 32000) i = 32000;
2870
/* Adjust the brighter pixels for better linearity */
2873
i = satlev[c] / div[c];
2874
if (min > i) min = i;
2876
limit = min * 9 >> 4;
2877
for (pix=image[0]; pix < image[height*width]; pix+=4) {
2878
if (pix[0] <= limit || pix[1] <= limit || pix[2] <= limit)
2881
for (c=1; c < 3; c++) {
2882
if (min > pix[c]) min = pix[c];
2883
if (max < pix[c]) max = pix[c];
2885
if (min >= limit*2) {
2886
pix[0] = pix[1] = pix[2] = max;
2888
i = 0x4000 - ((min - limit) << 14) / limit;
2889
i = 0x4000 - (i*i >> 14);
2891
FORC3 pix[c] += (max - pix[c]) * i >> 14;
2895
Because photons that miss one detector often hit another,
2896
the sum R+G+B is much less noisy than the individual colors.
2897
So smooth the hues without smoothing the total.
2899
for (smlast=-1, row=2; row < height-2; row++) {
2900
while (smlast < row+2) {
2901
for (i=0; i < 6; i++)
2902
smrow[(i+5) % 6] = smrow[i];
2903
pix = image[++smlast*width+2];
2904
for (col=2; col < width-2; col++) {
2905
FORC3 smrow[4][col][c] = (pix[c-4]+2*pix[c]+pix[c+4]+2) >> 2;
2909
pix = image[row*width+2];
2910
for (col=2; col < width-2; col++) {
2911
FORC3 dev[c] = -foveon_apply_curve (curve[7], pix[c] -
2912
((smrow[1][col][c] + 2*smrow[2][col][c] + smrow[3][col][c]) >> 2));
2913
sum = (dev[0] + dev[1] + dev[2]) >> 3;
2914
FORC3 pix[c] += dev[c] - sum;
2918
for (smlast=-1, row=2; row < height-2; row++) {
2919
while (smlast < row+2) {
2920
for (i=0; i < 6; i++)
2921
smrow[(i+5) % 6] = smrow[i];
2922
pix = image[++smlast*width+2];
2923
for (col=2; col < width-2; col++) {
2924
FORC3 smrow[4][col][c] =
2925
(pix[c-8]+pix[c-4]+pix[c]+pix[c+4]+pix[c+8]+2) >> 2;
2929
pix = image[row*width+2];
2930
for (col=2; col < width-2; col++) {
2931
for (total[3]=375, sum=60, c=0; c < 3; c++) {
2932
for (total[c]=i=0; i < 5; i++)
2933
total[c] += smrow[i][col][c];
2934
total[3] += total[c];
2937
if (sum < 0) sum = 0;
2938
j = total[3] > 375 ? (sum << 16) / total[3] : sum * 174;
2939
FORC3 pix[c] += foveon_apply_curve (curve[6],
2940
((j*total[c] + 0x8000) >> 16) - pix[c]);
2945
/* Transform the image to a different colorspace */
2946
for (pix=image[0]; pix < image[height*width]; pix+=4) {
2947
FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]);
2948
sum = (pix[0]+pix[1]+pix[1]+pix[2]) >> 2;
2949
FORC3 pix[c] -= foveon_apply_curve (curve[c], pix[c]-sum);
2951
for (dsum=i=0; i < 3; i++)
2952
dsum += trans[c][i] * pix[i];
2953
if (dsum < 0) dsum = 0;
2954
if (dsum > 24000) dsum = 24000;
2955
ipix[c] = dsum + 0.5;
2957
FORC3 pix[c] = ipix[c];
2960
/* Smooth the image bottom-to-top and save at 1/4 scale */
2961
shrink = (short (*)[3]) calloc ((width/4) * (height/4), sizeof *shrink);
2962
merror (shrink, "foveon_interpolate()");
2963
for (row = height/4; row--; )
2964
for (col=0; col < width/4; col++) {
2965
ipix[0] = ipix[1] = ipix[2] = 0;
2966
for (i=0; i < 4; i++)
2967
for (j=0; j < 4; j++)
2968
FORC3 ipix[c] += image[(row*4+i)*width+col*4+j][c];
2970
if (row+2 > height/4)
2971
shrink[row*(width/4)+col][c] = ipix[c] >> 4;
2973
shrink[row*(width/4)+col][c] =
2974
(shrink[(row+1)*(width/4)+col][c]*1840 + ipix[c]*141 + 2048) >> 12;
2976
/* From the 1/4-scale image, smooth right-to-left */
2977
for (row=0; row < (height & ~3); row++) {
2978
ipix[0] = ipix[1] = ipix[2] = 0;
2980
for (col = width & ~3 ; col--; )
2981
FORC3 smrow[0][col][c] = ipix[c] =
2982
(shrink[(row/4)*(width/4)+col/4][c]*1485 + ipix[c]*6707 + 4096) >> 13;
2984
/* Then smooth left-to-right */
2985
ipix[0] = ipix[1] = ipix[2] = 0;
2986
for (col=0; col < (width & ~3); col++)
2987
FORC3 smrow[1][col][c] = ipix[c] =
2988
(smrow[0][col][c]*1485 + ipix[c]*6707 + 4096) >> 13;
2990
/* Smooth top-to-bottom */
2992
memcpy (smrow[2], smrow[1], sizeof **smrow * width);
2994
for (col=0; col < (width & ~3); col++)
2995
FORC3 smrow[2][col][c] =
2996
(smrow[2][col][c]*6707 + smrow[1][col][c]*1485 + 4096) >> 13;
2998
/* Adjust the chroma toward the smooth values */
2999
for (col=0; col < (width & ~3); col++) {
3000
for (i=j=30, c=0; c < 3; c++) {
3001
i += smrow[2][col][c];
3002
j += image[row*width+col][c];
3005
for (sum=c=0; c < 3; c++) {
3006
ipix[c] = foveon_apply_curve (curve[c+3],
3007
((smrow[2][col][c] * j + 0x8000) >> 16) - image[row*width+col][c]);
3012
i = image[row*width+col][c] + ipix[c] - sum;
3014
image[row*width+col][c] = i;
3020
for (i=0; i < 8; i++)
3023
/* Trim off the black border */
3024
active[1] -= keep[1];
3026
i = active[2] - active[0];
3027
for (row=0; row < active[3]-active[1]; row++)
3028
memcpy (image[row*i], image[(row+active[1])*width+active[0]],
3038
Seach from the current directory up to the root looking for
3039
a ".badpixels" file, and fix those pixels now.
3041
void CLASS bad_pixels()
3044
char *fname, *cp, line[128];
3045
int len, time, row, col, r, c, rad, tot, n, fixed=0;
3047
if (!filters) return;
3048
for (len=32 ; ; len *= 2) {
3049
fname = (char *) malloc (len);
3051
if (getcwd (fname, len-16)) break;
3053
if (errno != ERANGE) return;
3055
#if defined(WIN32) || defined(DJGPP)
3056
if (fname[1] == ':')
3057
memmove (fname, fname+2, len-2);
3058
for (cp=fname; *cp; cp++)
3059
if (*cp == '\\') *cp = '/';
3061
cp = fname + strlen(fname);
3062
if (cp[-1] == '/') cp--;
3063
while (*fname == '/') {
3064
strcpy (cp, "/.badpixels");
3065
if ((fp = fopen (fname, "r"))) break;
3066
if (cp == fname) break;
3067
while (*--cp != '/');
3071
while (fgets (line, 128, fp)) {
3072
cp = strchr (line, '#');
3074
if (sscanf (line, "%d %d %d", &col, &row, &time) != 3) continue;
3075
if ((unsigned) col >= width || (unsigned) row >= height) continue;
3076
if (time > timestamp) continue;
3077
for (tot=n=0, rad=1; rad < 3 && n==0; rad++)
3078
for (r = row-rad; r <= row+rad; r++)
3079
for (c = col-rad; c <= col+rad; c++)
3080
if ((unsigned) r < height && (unsigned) c < width &&
3081
(r != row || c != col) && fc(r,c) == fc(row,col)) {
3085
BAYER2(row,col) = tot/n;
3088
fprintf (stderr,_("Fixed bad pixels at:"));
3089
fprintf (stderr, " %d,%d", col, row);
3092
if (fixed) fputc ('\n', stderr);
3096
void CLASS subtract (char *fname)
3099
int dim[3]={0,0,0}, comment=0, number=0, error=0, nd=0, c, row, col;
3102
if (!(fp = fopen (fname, "rb"))) {
3103
perror (fname); return;
3105
if (fgetc(fp) != 'P' || fgetc(fp) != '5') error = 1;
3106
while (!error && nd < 3 && (c = fgetc(fp)) != EOF) {
3107
if (c == '#') comment = 1;
3108
if (c == '\n') comment = 0;
3109
if (comment) continue;
3110
if (isdigit(c)) number = 1;
3112
if (isdigit(c)) dim[nd] = dim[nd]*10 + c -'0';
3113
else if (isspace(c)) {
3118
if (error || nd < 3) {
3119
fprintf (stderr,_("%s is not a valid PGM file!\n"), fname);
3120
fclose (fp); return;
3121
} else if (dim[0] != width || dim[1] != height || dim[2] != 65535) {
3122
fprintf (stderr,_("%s has the wrong dimensions!\n"), fname);
3123
fclose (fp); return;
3125
pixel = (ushort *) calloc (width, sizeof *pixel);
3126
merror (pixel, "subtract()");
3127
for (row=0; row < height; row++) {
3128
fread (pixel, 2, width, fp);
3129
for (col=0; col < width; col++)
3130
BAYER(row,col) = MAX (BAYER(row,col) - ntohs(pixel[col]), 0);
3136
void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
3138
double work[3][6], num;
3141
for (i=0; i < 3; i++) {
3142
for (j=0; j < 6; j++)
3143
work[i][j] = j == i+3;
3144
for (j=0; j < 3; j++)
3145
for (k=0; k < size; k++)
3146
work[i][j] += in[k][i] * in[k][j];
3148
for (i=0; i < 3; i++) {
3150
for (j=0; j < 6; j++)
3152
for (k=0; k < 3; k++) {
3155
for (j=0; j < 6; j++)
3156
work[k][j] -= work[i][j] * num;
3159
for (i=0; i < size; i++)
3160
for (j=0; j < 3; j++)
3161
for (out[i][j]=k=0; k < 3; k++)
3162
out[i][j] += work[j][k+3] * in[i][k];
3165
void CLASS cam_xyz_coeff (double cam_xyz[4][3])
3167
double cam_rgb[4][3], inverse[4][3], num;
3170
for (i=0; i < colors; i++) /* Multiply out XYZ colorspace */
3171
for (j=0; j < 3; j++)
3172
for (cam_rgb[i][j] = k=0; k < 3; k++)
3173
cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
3175
for (i=0; i < colors; i++) { /* Normalize cam_rgb so that */
3176
for (num=j=0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
3177
num += cam_rgb[i][j];
3178
for (j=0; j < 3; j++)
3179
cam_rgb[i][j] /= num;
3180
pre_mul[i] = 1 / num;
3182
pseudoinverse (cam_rgb, inverse, colors);
3183
for (raw_color = i=0; i < 3; i++)
3184
for (j=0; j < colors; j++)
3185
rgb_cam[i][j] = inverse[j][i];
3189
void CLASS colorcheck()
3192
// Coordinates of the GretagMacbeth ColorChecker squares
3193
// width, height, 1st_column, 1st_row
3194
static const int cut[NSQ][4] = {
3195
{ 241, 231, 234, 274 },
3196
{ 251, 235, 534, 274 },
3197
{ 255, 239, 838, 272 },
3198
{ 255, 240, 1146, 274 },
3199
{ 251, 237, 1452, 278 },
3200
{ 243, 238, 1758, 288 },
3201
{ 253, 253, 218, 558 },
3202
{ 255, 249, 524, 562 },
3203
{ 261, 253, 830, 562 },
3204
{ 260, 255, 1144, 564 },
3205
{ 261, 255, 1450, 566 },
3206
{ 247, 247, 1764, 576 },
3207
{ 255, 251, 212, 862 },
3208
{ 259, 259, 518, 862 },
3209
{ 263, 261, 826, 864 },
3210
{ 265, 263, 1138, 866 },
3211
{ 265, 257, 1450, 872 },
3212
{ 257, 255, 1762, 874 },
3213
{ 257, 253, 212, 1164 },
3214
{ 262, 251, 516, 1172 },
3215
{ 263, 257, 826, 1172 },
3216
{ 263, 255, 1136, 1176 },
3217
{ 255, 252, 1452, 1182 },
3218
{ 257, 253, 1760, 1180 } };
3219
// ColorChecker Chart under 6500-kelvin illumination
3220
static const double gmb_xyY[NSQ][3] = {
3221
{ 0.400, 0.350, 10.1 }, // Dark Skin
3222
{ 0.377, 0.345, 35.8 }, // Light Skin
3223
{ 0.247, 0.251, 19.3 }, // Blue Sky
3224
{ 0.337, 0.422, 13.3 }, // Foliage
3225
{ 0.265, 0.240, 24.3 }, // Blue Flower
3226
{ 0.261, 0.343, 43.1 }, // Bluish Green
3227
{ 0.506, 0.407, 30.1 }, // Orange
3228
{ 0.211, 0.175, 12.0 }, // Purplish Blue
3229
{ 0.453, 0.306, 19.8 }, // Moderate Red
3230
{ 0.285, 0.202, 6.6 }, // Purple
3231
{ 0.380, 0.489, 44.3 }, // Yellow Green
3232
{ 0.473, 0.438, 43.1 }, // Orange Yellow
3233
{ 0.187, 0.129, 6.1 }, // Blue
3234
{ 0.305, 0.478, 23.4 }, // Green
3235
{ 0.539, 0.313, 12.0 }, // Red
3236
{ 0.448, 0.470, 59.1 }, // Yellow
3237
{ 0.364, 0.233, 19.8 }, // Magenta
3238
{ 0.196, 0.252, 19.8 }, // Cyan
3239
{ 0.310, 0.316, 90.0 }, // White
3240
{ 0.310, 0.316, 59.1 }, // Neutral 8
3241
{ 0.310, 0.316, 36.2 }, // Neutral 6.5
3242
{ 0.310, 0.316, 19.8 }, // Neutral 5
3243
{ 0.310, 0.316, 9.0 }, // Neutral 3.5
3244
{ 0.310, 0.316, 3.1 } }; // Black
3245
double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
3246
double inverse[NSQ][3], cam_xyz[4][3], num;
3247
int c, i, j, k, sq, row, col, count[4];
3249
memset (gmb_cam, 0, sizeof gmb_cam);
3250
for (sq=0; sq < NSQ; sq++) {
3252
for (row=cut[sq][3]; row < cut[sq][3]+cut[sq][1]; row++)
3253
for (col=cut[sq][2]; col < cut[sq][2]+cut[sq][0]; col++) {
3255
if (c >= colors) c -= 2;
3256
gmb_cam[sq][c] += BAYER(row,col);
3259
FORCC gmb_cam[sq][c] = gmb_cam[sq][c]/count[c] - black;
3260
gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
3261
gmb_xyz[sq][1] = gmb_xyY[sq][2];
3262
gmb_xyz[sq][2] = gmb_xyY[sq][2] *
3263
(1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
3265
pseudoinverse (gmb_xyz, inverse, NSQ);
3266
for (i=0; i < colors; i++)
3267
for (j=0; j < 3; j++)
3268
for (cam_xyz[i][j] = k=0; k < NSQ; k++)
3269
cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
3270
cam_xyz_coeff (cam_xyz);
3272
printf (" { \"%s %s\", %d,\n\t{", make, model, black);
3273
num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
3274
FORCC for (j=0; j < 3; j++)
3275
printf ("%c%d", (c | j) ? ',':' ', (int) (cam_xyz[c][j] * num + 0.5));
3282
void CLASS scale_colors()
3284
int row, col, x, y, c, val, sum[8];
3285
double dsum[8], dmin, dmax;
3289
if (use_auto_wb || (use_camera_wb && cam_mul[0] == -1)) {
3290
memset (dsum, 0, sizeof dsum);
3291
for (row=0; row < height-7; row += 8)
3292
for (col=0; col < width-7; col += 8) {
3293
memset (sum, 0, sizeof sum);
3294
for (y=row; y < row+8; y++)
3295
for (x=col; x < col+8; x++)
3297
val = image[y*width+x][c];
3300
if (val > maximum-25) goto skip_block;
3301
if (val < 0) val = 0;
3305
for (c=0; c < 8; c++) dsum[c] += sum[c];
3309
FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
3311
if (use_camera_wb && cam_mul[0] != -1) {
3312
memset (sum, 0, sizeof sum);
3313
for (row=0; row < 8; row++)
3314
for (col=0; col < 8; col++) {
3316
if ((val = white[row][col] - black) > 0)
3320
if (sum[0] && sum[1] && sum[2] && sum[3])
3321
FORC4 pre_mul[c] = (float) sum[c+4] / sum[c];
3322
else if (cam_mul[0] && cam_mul[2])
3323
memcpy (pre_mul, cam_mul, sizeof pre_mul);
3325
fprintf (stderr,_("%s: Cannot use camera white balance.\n"), ifname);
3328
memcpy (pre_mul, user_mul, sizeof pre_mul);
3329
if (pre_mul[3] == 0) pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
3330
for (dmin=DBL_MAX, dmax=c=0; c < 4; c++) {
3331
if (dmin > pre_mul[c])
3333
if (dmax < pre_mul[c])
3336
if (!highlight) dmax = dmin;
3337
FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
3339
fprintf (stderr,_("Scaling with black %d, multipliers"), black);
3340
FORC4 fprintf (stderr, " %f", pre_mul[c]);
3341
fputc ('\n', stderr);
3343
for (row=0; row < height; row++)
3344
for (col=0; col < width; col++)
3346
val = image[row*width+col][c];
3349
val *= scale_mul[c];
3350
image[row*width+col][c] = CLIP(val);
3352
if (filters && colors == 3) {
3353
if (four_color_rgb) {
3354
fprintf(stderr,"dcraw::scale colors using 4colors\n");
3356
FORC3 rgb_cam[c][3] = rgb_cam[c][1] /= 2;
3358
for (row = FC(1,0) >> 1; row < height; row+=2)
3359
for (col = FC(row,1) & 1; col < width; col+=2)
3360
image[row*width+col][1] = image[row*width+col][3];
3361
filters &= ~((filters & 0x55555555) << 1);
3366
void CLASS border_interpolate (int border)
3368
unsigned row, col, y, x, f, c, sum[8];
3370
for (row=0; row < height; row++)
3371
for (col=0; col < width; col++) {
3372
if (col==border && row >= border && row < height-border)
3374
memset (sum, 0, sizeof sum);
3375
for (y=row-1; y != row+2; y++)
3376
for (x=col-1; x != col+2; x++)
3377
if (y < height && x < width) {
3379
sum[f] += image[y*width+x][f];
3383
FORCC if (c != f && sum[c+4])
3384
image[row*width+col][c] = sum[c] / sum[c+4];
3388
void CLASS lin_interpolate()
3390
int code[16][16][32], *ip, sum[4];
3391
int c, i, x, y, row, col, shift, color;
3394
if (verbose) fprintf (stderr,_("Bilinear interpolation...\n"));
3396
border_interpolate(1);
3397
for (row=0; row < 16; row++)
3398
for (col=0; col < 16; col++) {
3399
ip = code[row][col];
3400
memset (sum, 0, sizeof sum);
3401
for (y=-1; y <= 1; y++)
3402
for (x=-1; x <= 1; x++) {
3403
shift = (y==0) + (x==0);
3404
if (shift == 2) continue;
3405
color = fc(row+y,col+x);
3406
*ip++ = (width*y + x)*4 + color;
3409
sum[color] += 1 << shift;
3412
if (c != fc(row,col)) {
3417
for (row=1; row < height-1; row++)
3418
for (col=1; col < width-1; col++) {
3419
pix = image[row*width+col];
3420
ip = code[row & 15][col & 15];
3421
memset (sum, 0, sizeof sum);
3422
for (i=8; i--; ip+=3)
3423
sum[ip[2]] += pix[ip[0]] << ip[1];
3424
for (i=colors; --i; ip+=2)
3425
pix[ip[0]] = sum[ip[0]] / ip[1];
3430
This algorithm is officially called:
3432
"Interpolation using a Threshold-based variable number of gradients"
3434
described in http://scien.stanford.edu/class/psych221/projects/99/tingchen/algodep/vargra.html
3436
I've extended the basic idea to work with non-Bayer filter arrays.
3437
Gradients are numbered clockwise from NW=0 to W=7.
3439
void CLASS vng_interpolate()
3441
static const signed char *cp, terms[] = {
3442
-2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
3443
-2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
3444
-2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
3445
-2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
3446
-2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
3447
-1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
3448
-1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
3449
-1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
3450
-1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
3451
-1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
3452
-1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
3453
-1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
3454
-1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
3455
+0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
3456
+0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
3457
+0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
3458
+0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
3459
+0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
3460
+0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
3461
+0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
3462
+1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
3464
}, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
3465
ushort (*brow[5])[4], *pix;
3466
int prow=7, pcol=1, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
3467
int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
3468
int g, diff, thold, num, c;
3471
if (verbose) fprintf (stderr,_("VNG interpolation...\n"));
3473
if (filters == 1) prow = pcol = 15;
3474
ip = (int *) calloc ((prow+1)*(pcol+1), 1280);
3475
merror (ip, "vng_interpolate()");
3476
for (row=0; row <= prow; row++) /* Precalculate for VNG */
3477
for (col=0; col <= pcol; col++) {
3478
code[row][col] = ip;
3479
for (cp=terms, t=0; t < 64; t++) {
3480
y1 = *cp++; x1 = *cp++;
3481
y2 = *cp++; x2 = *cp++;
3484
color = fc(row+y1,col+x1);
3485
if (fc(row+y2,col+x2) != color) continue;
3486
diag = (fc(row,col+1) == color && fc(row+1,col) == color) ? 2:1;
3487
if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
3488
*ip++ = (y1*width + x1)*4 + color;
3489
*ip++ = (y2*width + x2)*4 + color;
3491
for (g=0; g < 8; g++)
3492
if (grads & 1<<g) *ip++ = g;
3496
for (cp=chood, g=0; g < 8; g++) {
3497
y = *cp++; x = *cp++;
3498
*ip++ = (y*width + x) * 4;
3499
color = fc(row,col);
3500
if (fc(row+y,col+x) != color && fc(row+y*2,col+x*2) == color)
3501
*ip++ = (y*width + x) * 8 + color;
3506
brow[4] = (ushort (*)[4]) calloc (width*3, sizeof **brow);
3507
merror (brow[4], "vng_interpolate()");
3508
for (row=0; row < 3; row++)
3509
brow[row] = brow[4] + row*width;
3510
for (row=2; row < height-2; row++) { /* Do VNG interpolation */
3511
for (col=2; col < width-2; col++) {
3512
pix = image[row*width+col];
3513
ip = code[row & prow][col & pcol];
3514
memset (gval, 0, sizeof gval);
3515
while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
3516
diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
3517
gval[ip[3]] += diff;
3519
if ((g = ip[-1]) == -1) continue;
3521
while ((g = *ip++) != -1)
3525
gmin = gmax = gval[0]; /* Choose a threshold */
3526
for (g=1; g < 8; g++) {
3527
if (gmin > gval[g]) gmin = gval[g];
3528
if (gmax < gval[g]) gmax = gval[g];
3531
memcpy (brow[2][col], pix, sizeof *image);
3534
thold = gmin + (gmax >> 1);
3535
memset (sum, 0, sizeof sum);
3536
color = fc(row,col);
3537
for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
3538
if (gval[g] <= thold) {
3540
if (c == color && ip[1])
3541
sum[c] += (pix[c] + pix[ip[1]]) >> 1;
3543
sum[c] += pix[ip[0] + c];
3547
FORCC { /* Save to buffer */
3550
t += (sum[c] - sum[color]) / num;
3551
brow[2][col][c] = CLIP(t);
3554
if (row > 3) /* Write buffer to image */
3555
memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
3556
for (g=0; g < 4; g++)
3557
brow[(g-1) & 3] = brow[g];
3559
memcpy (image[(row-2)*width+2], brow[0]+2, (width-4)*sizeof *image);
3560
memcpy (image[(row-1)*width+2], brow[1]+2, (width-4)*sizeof *image);
3565
void CLASS cam_to_cielab (ushort cam[4], float lab[3])
3569
static float cbrt[0x10000], xyz_cam[3][4];
3572
for (i=0; i < 0x10000; i++) {
3574
cbrt[i] = r > 0.008856 ? pow(r,1/3.0) : 7.787*r + 16/116.0;
3576
for (i=0; i < 3; i++)
3577
for (j=0; j < colors; j++)
3578
for (xyz_cam[i][j] = k=0; k < 3; k++)
3579
xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
3581
xyz[0] = xyz[1] = xyz[2] = 0.5;
3583
xyz[0] += xyz_cam[0][c] * cam[c];
3584
xyz[1] += xyz_cam[1][c] * cam[c];
3585
xyz[2] += xyz_cam[2][c] * cam[c];
3587
xyz[0] = cbrt[CLIP((int) xyz[0])];
3588
xyz[1] = cbrt[CLIP((int) xyz[1])];
3589
xyz[2] = cbrt[CLIP((int) xyz[2])];
3590
lab[0] = 116 * xyz[1] - 16;
3591
lab[1] = 500 * (xyz[0] - xyz[1]);
3592
lab[2] = 200 * (xyz[1] - xyz[2]);
3597
Adaptive Homogeneity-Directed interpolation is based on
3598
the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
3600
#define TS 256 /* Tile Size */
3602
void CLASS ahd_interpolate()
3604
int i, j, top, left, row, col, tr, tc, fc, c, d, val, hm[2];
3605
ushort (*pix)[4], (*rix)[3];
3606
static const int dir[4] = { -1, 1, -TS, TS };
3607
unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
3609
ushort (*rgb)[TS][TS][3];
3610
short (*lab)[TS][TS][3];
3611
char (*homo)[TS][TS], *buffer;
3613
if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
3615
border_interpolate(3);
3616
buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
3617
merror (buffer, "ahd_interpolate()");
3618
rgb = (ushort(*)[TS][TS][3]) buffer;
3619
lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
3620
homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
3622
for (top=0; top < height; top += TS-6)
3623
for (left=0; left < width; left += TS-6) {
3624
memset (rgb, 0, 12*TS*TS);
3626
/* Interpolate green horizontally and vertically: */
3627
for (row = top < 2 ? 2:top; row < top+TS && row < height-2; row++) {
3628
col = left + (FC(row,left) == 1);
3629
if (col < 2) col += 2;
3630
for (fc = FC(row,col); col < left+TS && col < width-2; col+=2) {
3631
pix = image + row*width+col;
3632
val = ((pix[-1][1] + pix[0][fc] + pix[1][1]) * 2
3633
- pix[-2][fc] - pix[2][fc]) >> 2;
3634
rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
3635
val = ((pix[-width][1] + pix[0][fc] + pix[width][1]) * 2
3636
- pix[-2*width][fc] - pix[2*width][fc]) >> 2;
3637
rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
3640
/* Interpolate red and blue, and convert to CIELab: */
3641
for (d=0; d < 2; d++)
3642
for (row=top+1; row < top+TS-1 && row < height-1; row++)
3643
for (col=left+1; col < left+TS-1 && col < width-1; col++) {
3644
pix = image + row*width+col;
3645
rix = &rgb[d][row-top][col-left];
3646
if ((c = 2 - FC(row,col)) == 1) {
3648
val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
3649
- rix[-1][1] - rix[1][1] ) >> 1);
3650
rix[0][2-c] = CLIP(val);
3651
val = pix[0][1] + (( pix[-width][c] + pix[width][c]
3652
- rix[-TS][1] - rix[TS][1] ) >> 1);
3654
val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
3655
+ pix[+width-1][c] + pix[+width+1][c]
3656
- rix[-TS-1][1] - rix[-TS+1][1]
3657
- rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
3658
rix[0][c] = CLIP(val);
3660
rix[0][c] = pix[0][c];
3661
cam_to_cielab (rix[0], flab);
3662
FORC3 lab[d][row-top][col-left][c] = 64*flab[c];
3664
/* Build homogeneity maps from the CIELab images: */
3665
memset (homo, 0, 2*TS*TS);
3666
for (row=top+2; row < top+TS-2 && row < height; row++) {
3668
for (col=left+2; col < left+TS-2 && col < width; col++) {
3670
for (d=0; d < 2; d++)
3671
for (i=0; i < 4; i++)
3672
ldiff[d][i] = ABS(lab[d][tr][tc][0]-lab[d][tr][tc+dir[i]][0]);
3673
leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
3674
MAX(ldiff[1][2],ldiff[1][3]));
3675
for (d=0; d < 2; d++)
3676
for (i=0; i < 4; i++)
3677
if (i >> 1 == d || ldiff[d][i] <= leps)
3678
abdiff[d][i] = SQR(lab[d][tr][tc][1]-lab[d][tr][tc+dir[i]][1])
3679
+ SQR(lab[d][tr][tc][2]-lab[d][tr][tc+dir[i]][2]);
3680
abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
3681
MAX(abdiff[1][2],abdiff[1][3]));
3682
for (d=0; d < 2; d++)
3683
for (i=0; i < 4; i++)
3684
if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
3688
/* Combine the most homogenous pixels for the final result: */
3689
for (row=top+3; row < top+TS-3 && row < height-3; row++) {
3691
for (col=left+3; col < left+TS-3 && col < width-3; col++) {
3693
for (d=0; d < 2; d++)
3694
for (hm[d]=0, i=tr-1; i <= tr+1; i++)
3695
for (j=tc-1; j <= tc+1; j++)
3696
hm[d] += homo[d][i][j];
3698
FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
3700
FORC3 image[row*width+col][c] =
3701
(rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
3710
Bilateral Filtering was developed by C. Tomasi and R. Manduchi.
3712
void CLASS bilateral_filter()
3714
float (**window)[7], *kernel, scale_r, elut[1024], sum[5];
3715
int c, i, wr, ws, wlast, row, col, y, x;
3718
if (verbose) fprintf (stderr,_("Bilateral filtering...\n"));
3720
wr = ceil(sigma_d*2); /* window radius */
3721
ws = 2*wr + 1; /* window size */
3722
window = (float (**)[7]) calloc ((ws+1)*sizeof *window +
3723
ws*width*sizeof **window + ws*sizeof *kernel, 1);
3724
merror (window, "bilateral_filter()");
3725
for (i=0; i <= ws; i++)
3726
window[i] = (float (*)[7]) (window+ws+1) + i*width;
3727
kernel = (float *) window[ws] + wr;
3728
for (i=-wr; i <= wr; i++)
3729
kernel[i] = 256 / (2*SQR(sigma_d)) * i*i + 0.25;
3730
scale_r = 256 / (2*SQR(sigma_r));
3731
for (i=0; i < 1024; i++)
3732
elut[i] = exp (-i/256.0);
3734
for (wlast=-1, row=0; row < height; row++) {
3735
while (wlast < row+wr) {
3737
for (i=0; i <= ws; i++) /* rotate window rows */
3738
window[(ws+i) % (ws+1)] = window[i];
3740
for (col=0; col < width; col++) {
3741
FORCC window[ws-1][col][c] = image[wlast*width+col][c];
3742
cam_to_cielab (image[wlast*width+col], window[ws-1][col]+4);
3745
for (col=0; col < width; col++) {
3746
memset (sum, 0, sizeof sum);
3747
for (y=-wr; y <= wr; y++)
3748
if ((unsigned)(row+y) < height)
3749
for (x=-wr; x <= wr; x++)
3750
if ((unsigned)(col+x) < width) {
3751
sep = ( SQR(window[wr+y][col+x][4] - window[wr][col][4])
3752
+ SQR(window[wr+y][col+x][5] - window[wr][col][5])
3753
+ SQR(window[wr+y][col+x][6] - window[wr][col][6]) )
3754
* scale_r + kernel[y] + kernel[x];
3756
FORCC sum[c] += elut[sep] * window[wr+y][col+x][c];
3757
sum[4] += elut[sep];
3760
FORCC image[row*width+col][c] = sum[c]/sum[4];
3766
#define SCALE (4 >> shrink)
3767
void CLASS recover_highlights()
3769
float *map, sum, wgt, grow;
3770
int hsat[4], count, spread, change, val, i;
3771
unsigned high, wide, mrow, mcol, row, col, kc, c, d, y, x;
3773
static const signed char dir[8][2] =
3774
{ {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1} };
3776
if (verbose) fprintf (stderr,_("Highlight recovery...\n"));
3778
grow = pow (2, 4-highlight);
3779
FORCC hsat[c] = 32000 * pre_mul[c];
3780
for (kc=0, c=1; c < colors; c++)
3781
if (pre_mul[kc] < pre_mul[c]) kc = c;
3782
high = height / SCALE;
3783
wide = width / SCALE;
3784
map = (float *) calloc (high*wide, sizeof *map);
3785
merror (map, "recover_highlights()");
3786
FORCC if (c != kc) {
3787
memset (map, 0, high*wide*sizeof *map);
3788
for (mrow=0; mrow < high; mrow++)
3789
for (mcol=0; mcol < wide; mcol++) {
3790
sum = wgt = count = 0;
3791
for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
3792
for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
3793
pixel = image[row*width+col];
3794
if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000) {
3800
if (count == SCALE*SCALE)
3801
map[mrow*wide+mcol] = sum / wgt;
3803
for (spread = 32/grow; spread--; ) {
3804
for (mrow=0; mrow < high; mrow++)
3805
for (mcol=0; mcol < wide; mcol++) {
3806
if (map[mrow*wide+mcol]) continue;
3808
for (d=0; d < 8; d++) {
3809
y = mrow + dir[d][0];
3810
x = mcol + dir[d][1];
3811
if (y < high && x < wide && map[y*wide+x] > 0) {
3812
sum += (1 + (d & 1)) * map[y*wide+x];
3813
count += 1 + (d & 1);
3817
map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
3819
for (change=i=0; i < high*wide; i++)
3826
for (i=0; i < high*wide; i++)
3827
if (map[i] == 0) map[i] = 1;
3828
for (mrow=0; mrow < high; mrow++)
3829
for (mcol=0; mcol < wide; mcol++) {
3830
for (row = mrow*SCALE; row < (mrow+1)*SCALE; row++)
3831
for (col = mcol*SCALE; col < (mcol+1)*SCALE; col++) {
3832
pixel = image[row*width+col];
3833
if (pixel[c] / hsat[c] > 1) {
3834
val = pixel[kc] * map[mrow*wide+mcol];
3835
if (pixel[c] < val) pixel[c] = CLIP(val);
3844
void CLASS tiff_get (unsigned base,
3845
unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
3850
*save = ftell(ifp) + 4;
3851
if (*len * ("1112481124848"[*type < 13 ? *type:0]-'0') > 4)
3852
fseek (ifp, get4()+base, SEEK_SET);
3855
void CLASS parse_thumb_note (int base, unsigned toff, unsigned tlen)
3857
unsigned entries, tag, type, len, save;
3861
tiff_get (base, &tag, &type, &len, &save);
3862
if (tag == toff) thumb_offset = get4();
3863
if (tag == tlen) thumb_length = get4();
3864
fseek (ifp, save, SEEK_SET);
3868
void CLASS parse_makernote (int base)
3870
static const uchar xlat[2][256] = {
3871
{ 0xc1,0xbf,0x6d,0x0d,0x59,0xc5,0x13,0x9d,0x83,0x61,0x6b,0x4f,0xc7,0x7f,0x3d,0x3d,
3872
0x53,0x59,0xe3,0xc7,0xe9,0x2f,0x95,0xa7,0x95,0x1f,0xdf,0x7f,0x2b,0x29,0xc7,0x0d,
3873
0xdf,0x07,0xef,0x71,0x89,0x3d,0x13,0x3d,0x3b,0x13,0xfb,0x0d,0x89,0xc1,0x65,0x1f,
3874
0xb3,0x0d,0x6b,0x29,0xe3,0xfb,0xef,0xa3,0x6b,0x47,0x7f,0x95,0x35,0xa7,0x47,0x4f,
3875
0xc7,0xf1,0x59,0x95,0x35,0x11,0x29,0x61,0xf1,0x3d,0xb3,0x2b,0x0d,0x43,0x89,0xc1,
3876
0x9d,0x9d,0x89,0x65,0xf1,0xe9,0xdf,0xbf,0x3d,0x7f,0x53,0x97,0xe5,0xe9,0x95,0x17,
3877
0x1d,0x3d,0x8b,0xfb,0xc7,0xe3,0x67,0xa7,0x07,0xf1,0x71,0xa7,0x53,0xb5,0x29,0x89,
3878
0xe5,0x2b,0xa7,0x17,0x29,0xe9,0x4f,0xc5,0x65,0x6d,0x6b,0xef,0x0d,0x89,0x49,0x2f,
3879
0xb3,0x43,0x53,0x65,0x1d,0x49,0xa3,0x13,0x89,0x59,0xef,0x6b,0xef,0x65,0x1d,0x0b,
3880
0x59,0x13,0xe3,0x4f,0x9d,0xb3,0x29,0x43,0x2b,0x07,0x1d,0x95,0x59,0x59,0x47,0xfb,
3881
0xe5,0xe9,0x61,0x47,0x2f,0x35,0x7f,0x17,0x7f,0xef,0x7f,0x95,0x95,0x71,0xd3,0xa3,
3882
0x0b,0x71,0xa3,0xad,0x0b,0x3b,0xb5,0xfb,0xa3,0xbf,0x4f,0x83,0x1d,0xad,0xe9,0x2f,
3883
0x71,0x65,0xa3,0xe5,0x07,0x35,0x3d,0x0d,0xb5,0xe9,0xe5,0x47,0x3b,0x9d,0xef,0x35,
3884
0xa3,0xbf,0xb3,0xdf,0x53,0xd3,0x97,0x53,0x49,0x71,0x07,0x35,0x61,0x71,0x2f,0x43,
3885
0x2f,0x11,0xdf,0x17,0x97,0xfb,0x95,0x3b,0x7f,0x6b,0xd3,0x25,0xbf,0xad,0xc7,0xc5,
3886
0xc5,0xb5,0x8b,0xef,0x2f,0xd3,0x07,0x6b,0x25,0x49,0x95,0x25,0x49,0x6d,0x71,0xc7 },
3887
{ 0xa7,0xbc,0xc9,0xad,0x91,0xdf,0x85,0xe5,0xd4,0x78,0xd5,0x17,0x46,0x7c,0x29,0x4c,
3888
0x4d,0x03,0xe9,0x25,0x68,0x11,0x86,0xb3,0xbd,0xf7,0x6f,0x61,0x22,0xa2,0x26,0x34,
3889
0x2a,0xbe,0x1e,0x46,0x14,0x68,0x9d,0x44,0x18,0xc2,0x40,0xf4,0x7e,0x5f,0x1b,0xad,
3890
0x0b,0x94,0xb6,0x67,0xb4,0x0b,0xe1,0xea,0x95,0x9c,0x66,0xdc,0xe7,0x5d,0x6c,0x05,
3891
0xda,0xd5,0xdf,0x7a,0xef,0xf6,0xdb,0x1f,0x82,0x4c,0xc0,0x68,0x47,0xa1,0xbd,0xee,
3892
0x39,0x50,0x56,0x4a,0xdd,0xdf,0xa5,0xf8,0xc6,0xda,0xca,0x90,0xca,0x01,0x42,0x9d,
3893
0x8b,0x0c,0x73,0x43,0x75,0x05,0x94,0xde,0x24,0xb3,0x80,0x34,0xe5,0x2c,0xdc,0x9b,
3894
0x3f,0xca,0x33,0x45,0xd0,0xdb,0x5f,0xf5,0x52,0xc3,0x21,0xda,0xe2,0x22,0x72,0x6b,
3895
0x3e,0xd0,0x5b,0xa8,0x87,0x8c,0x06,0x5d,0x0f,0xdd,0x09,0x19,0x93,0xd0,0xb9,0xfc,
3896
0x8b,0x0f,0x84,0x60,0x33,0x1c,0x9b,0x45,0xf1,0xf0,0xa3,0x94,0x3a,0x12,0x77,0x33,
3897
0x4d,0x44,0x78,0x28,0x3c,0x9e,0xfd,0x65,0x57,0x16,0x94,0x6b,0xfb,0x59,0xd0,0xc8,
3898
0x22,0x36,0xdb,0xd2,0x63,0x98,0x43,0xa1,0x04,0x87,0x86,0xf7,0xa6,0x26,0xbb,0xd6,
3899
0x59,0x4d,0xbf,0x6a,0x2e,0xaa,0x2b,0xef,0xe6,0x78,0xb6,0x4e,0xe0,0x2f,0xdc,0x7c,
3900
0xbe,0x57,0x19,0x32,0x7e,0x2a,0xd0,0xb8,0xba,0x29,0x00,0x3c,0x52,0x7d,0xa8,0x49,
3901
0x3b,0x2d,0xeb,0x25,0x49,0xfa,0xa3,0xaa,0x39,0xa7,0xc5,0xa7,0x50,0x11,0x36,0xfb,
3902
0xc6,0x67,0x4a,0xf5,0xa5,0x12,0x65,0x7e,0xb0,0xdf,0xaf,0x4e,0xb3,0x61,0x7f,0x2f } };
3903
unsigned offset=0, entries, tag, type, len, save, c;
3904
unsigned ver97=0, serial=0, i, wb[4]={0,0,0,0};
3905
uchar buf97[324], ci, cj, ck;
3909
The MakerNote might have its own TIFF header (possibly with
3910
its own byte-order!), or it might just be a table.
3913
fread (buf, 1, 10, ifp);
3914
if (!strncmp (buf,"KDK" ,3) || /* these aren't TIFF tables */
3915
!strncmp (buf,"VER" ,3) ||
3916
!strncmp (buf,"IIII",4) ||
3917
!strncmp (buf,"MMMM",4)) return;
3918
if (!strncmp (buf,"KC" ,2) || /* Konica KD-400Z, KD-510Z */
3919
!strncmp (buf,"MLY" ,3)) { /* Minolta DiMAGE G series */
3921
while ((i=ftell(ifp)) < data_offset && i < 16384) {
3922
wb[0] = wb[2]; wb[2] = wb[1]; wb[1] = wb[3];
3924
if (wb[1] == 256 && wb[3] == 256 &&
3925
wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
3926
FORC4 cam_mul[c] = wb[c];
3930
if (!strcmp (buf,"Nikon")) {
3933
if (get2() != 42) goto quit;
3935
fseek (ifp, offset-8, SEEK_CUR);
3936
} else if (!strncmp (buf,"FUJIFILM",8) ||
3937
!strncmp (buf,"SONY",4) ||
3938
!strcmp (buf,"Panasonic")) {
3940
fseek (ifp, 2, SEEK_CUR);
3941
} else if (!strcmp (buf,"OLYMP") ||
3942
!strcmp (buf,"LEICA") ||
3943
!strcmp (buf,"Ricoh") ||
3944
!strcmp (buf,"EPSON"))
3945
fseek (ifp, -2, SEEK_CUR);
3946
else if (!strcmp (buf,"AOC") ||
3947
!strcmp (buf,"QVC"))
3948
fseek (ifp, -4, SEEK_CUR);
3949
else fseek (ifp, -10, SEEK_CUR);
3952
if (entries > 1000) return;
3954
tiff_get (base, &tag, &type, &len, &save);
3955
if (tag == 2 && strstr(make,"NIKON"))
3956
iso_speed = (get2(),get2());
3957
if (tag == 4 && len == 27) {
3958
iso_speed = 50 * pow (2, (get4(),get2())/32.0 - 4);
3959
aperture = (get2(), pow (2, get2()/64.0));
3960
shutter = pow (2, ((short) get2())/-32.0);
3962
if (tag == 8 && type == 4)
3963
shot_order = get4();
3964
if (tag == 0xc && len == 4) {
3965
cam_mul[0] = getrat();
3966
cam_mul[2] = getrat();
3968
if (tag == 0x10 && type == 4)
3970
if (tag == 0x14 && len == 2560 && type == 7) {
3971
fseek (ifp, 1248, SEEK_CUR);
3974
if (strstr(make,"PENTAX")) {
3975
if (tag == 0x1b) tag = 0x1018;
3976
if (tag == 0x1c) tag = 0x1017;
3979
while ((c = fgetc(ifp)))
3980
serial = serial*10 + (isdigit(c) ? c - '0' : c % 10);
3981
if (tag == 0x81 && type == 4) {
3982
data_offset = get4();
3983
fseek (ifp, data_offset + 41, SEEK_SET);
3984
raw_height = get2() * 2;
3986
filters = 0x61616161;
3988
if ((tag == 0x81 && type == 7) ||
3989
(tag == 0x100 && type == 7) ||
3990
(tag == 0x280 && type == 1)) {
3991
thumb_offset = ftell(ifp);
3994
if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
3995
thumb_offset += base;
3996
if (tag == 0x89 && type == 4)
3997
thumb_length = get4();
3999
curve_offset = ftell(ifp) + 2112;
4001
curve_offset = ftell(ifp) + 2;
4003
for (i=0; i < 4; i++)
4004
ver97 = (ver97 << 4) + fgetc(ifp)-'0';
4007
fseek (ifp, 68, SEEK_CUR);
4008
FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
4011
fseek (ifp, 6, SEEK_CUR);
4014
fseek (ifp, 16, SEEK_CUR);
4015
FORC4 cam_mul[c] = get2();
4017
if (ver97 >> 8 == 2) {
4018
if (ver97 != 0x205) fseek (ifp, 280, SEEK_CUR);
4019
fread (buf97, 324, 1, ifp);
4022
if (tag == 0xa7 && ver97 >> 8 == 2) {
4023
ci = xlat[0][serial & 0xff];
4024
cj = xlat[1][fgetc(ifp)^fgetc(ifp)^fgetc(ifp)^fgetc(ifp)];
4026
for (i=0; i < 324; i++)
4027
buf97[i] ^= (cj += ci * ck++);
4028
FORC4 cam_mul[c ^ (c >> 1)] =
4029
sget2 (buf97 + (ver97 == 0x205 ? 14:6) + c*2);
4031
if (tag == 0x200 && len == 4)
4032
black = (get2()+get2()+get2()+get2())/4;
4033
if (tag == 0x201 && len == 4)
4035
if (tag == 0x401 && len == 4) {
4036
black = (get4()+get4()+get4()+get4())/4;
4038
if (tag == 0xe01) { /* Nikon Capture Note */
4041
fseek (ifp, 22, SEEK_CUR);
4042
for (offset=22; offset+22 < len; offset += 22+i) {
4044
fseek (ifp, 14, SEEK_CUR);
4046
if (tag == 0x76a43207) flip = get2();
4047
else fseek (ifp, i, SEEK_CUR);
4051
if (tag == 0xe80 && len == 256 && type == 7) {
4052
fseek (ifp, 48, SEEK_CUR);
4053
cam_mul[0] = get2() * 508 * 1.078 / 0x10000;
4054
cam_mul[2] = get2() * 382 * 1.173 / 0x10000;
4056
if (tag == 0xf00 && type == 7) {
4058
fseek (ifp, 176, SEEK_CUR);
4059
else if (len == 734 || len == 1502)
4060
fseek (ifp, 148, SEEK_CUR);
4064
if (tag == 0x1011 && len == 9 && use_camera_wb) {
4065
for (i=0; i < 3; i++)
4066
FORC3 rgb_cam[i][c] = ((short) get2()) / 256.0;
4067
raw_color = rgb_cam[0][0] < 1;
4069
if (tag == 0x1012 && len == 4)
4070
for (black = i=0; i < 4; i++)
4071
black += get2() << 2;
4073
cam_mul[0] = get2() / 256.0;
4075
cam_mul[2] = get2() / 256.0;
4076
if (tag == 0x2011 && len == 2) {
4079
cam_mul[0] = get2() / 256.0;
4080
cam_mul[2] = get2() / 256.0;
4083
parse_thumb_note (base, 257, 258);
4084
if (tag == 0xb028) {
4085
fseek (ifp, get4(), SEEK_SET);
4086
parse_thumb_note (base, 136, 137);
4088
if (tag == 0x4001) {
4089
i = len == 582 ? 50 : len == 653 ? 68 : len == 796 ? 126 : 0;
4090
fseek (ifp, i, SEEK_CUR);
4092
FORC4 cam_mul[c ^ (c >> 1)] = get2();
4095
fseek (ifp, save, SEEK_SET);
4102
Since the TIFF DateTime string has no timezone information,
4103
assume that the camera's clock was set to Universal Time.
4105
void CLASS get_timestamp (int reversed)
4111
if (timestamp) return;
4114
for (i=19; i--; ) str[i] = fgetc(ifp);
4116
fread (str, 19, 1, ifp);
4117
memset (&t, 0, sizeof t);
4118
if (sscanf (str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon,
4119
&t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
4124
timestamp = mktime(&t);
4127
void CLASS parse_exif (int base)
4129
unsigned kodak, entries, tag, type, len, save;
4132
kodak = !strncmp(make,"EASTMAN",7);
4135
tiff_get (base, &tag, &type, &len, &save);
4137
case 33434: shutter = getrat(); break;
4138
case 33437: aperture = getrat(); break;
4139
case 34855: iso_speed = get2(); break;
4141
case 36868: get_timestamp(0); break;
4142
case 37377: if ((expo = -getrat()) < 128)
4143
shutter = pow (2, expo); break;
4144
case 37378: aperture = pow (2, getrat()/2); break;
4145
case 37386: focal_len = getrat(); break;
4146
case 37500: parse_makernote (base); break;
4147
case 40962: if (kodak) raw_width = get4(); break;
4148
case 40963: if (kodak) raw_height = get4(); break;
4150
fseek (ifp, save, SEEK_SET);
4154
void CLASS romm_coeff (float romm_cam[3][3])
4156
static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
4157
{ { 2.034193, -0.727420, -0.306766 },
4158
{ -0.228811, 1.231729, -0.002922 },
4159
{ -0.008565, -0.153273, 1.161839 } };
4162
for (raw_color = i=0; i < 3; i++)
4163
for (j=0; j < 3; j++)
4164
for (rgb_cam[i][j] = k=0; k < 3; k++)
4165
rgb_cam[i][j] += rgb_romm[i][k] * romm_cam[k][j];
4168
void CLASS parse_mos (int offset)
4171
int skip, from, i, c, neut[4], planes=0, frot=0;
4172
static const char *mod[] =
4173
{ "","DCB2","Volare","Cantare","CMost","Valeo 6","Valeo 11","Valeo 22",
4174
"","Valeo 17","","Aptus 17","Aptus 22","Aptus 75","Aptus 65" };
4175
float romm_cam[3][3];
4177
fseek (ifp, offset, SEEK_SET);
4179
if (get4() != 0x504b5453) break;
4181
fread (data, 1, 40, ifp);
4184
if (!strcmp(data,"JPEG_preview_data")) {
4185
thumb_offset = from;
4186
thumb_length = skip;
4188
if (!strcmp(data,"icc_camera_profile")) {
4189
profile_offset = from;
4190
profile_length = skip;
4192
if (!strcmp(data,"ShootObj_back_type")) {
4193
fscanf (ifp, "%d", &i);
4194
if ((unsigned) i < sizeof mod / sizeof (*mod))
4195
strcpy (model, mod[i]);
4197
if (!strcmp(data,"CaptProf_color_matrix") && use_camera_wb) {
4198
for (i=0; i < 9; i++)
4199
fscanf (ifp, "%f", &romm_cam[0][i]);
4200
romm_coeff (romm_cam);
4202
if (!strcmp(data,"CaptProf_number_of_planes"))
4203
fscanf (ifp, "%d", &planes);
4204
if (!strcmp(data,"CaptProf_raw_data_rotation"))
4205
fscanf (ifp, "%d", &flip);
4206
if (!strcmp(data,"CaptProf_mosaic_pattern"))
4208
fscanf (ifp, "%d", &i);
4209
if (i == 1) frot = c ^ (c >> 1);
4211
if (!strcmp(data,"ImgProf_rotation_angle")) {
4212
fscanf (ifp, "%d", &i);
4215
if (!strcmp(data,"NeutObj_neutrals") && !cam_mul[0]) {
4216
FORC4 fscanf (ifp, "%d", neut+c);
4217
FORC3 cam_mul[c] = (float) neut[0] / neut[c+1];
4220
fseek (ifp, skip+from, SEEK_SET);
4223
filters = (planes == 1) * 0x01010101 *
4224
(uchar) "\x94\x61\x16\x49"[(flip/90 + frot) & 3];
4227
void CLASS linear_table (unsigned len)
4230
if (len > 0x1000) len = 0x1000;
4231
read_shorts (curve, len);
4232
for (i=len; i < 0x1000; i++)
4233
curve[i] = curve[i-1];
4234
maximum = curve[0xfff];
4237
void CLASS parse_kodak_ifd (int base)
4239
unsigned entries, tag, type, len, save;
4240
int i, c, wbi=-2, wbtemp=6500;
4244
if (entries > 1024) return;
4246
tiff_get (base, &tag, &type, &len, &save);
4247
if (tag == 1020) wbi = getint(type);
4248
if (tag == 1021 && len == 72) { /* WB set in software */
4249
fseek (ifp, 40, SEEK_CUR);
4250
FORC3 cam_mul[c] = 2048.0 / get2();
4253
if (tag == 2118) wbtemp = getint(type);
4254
if (tag == 2130 + wbi)
4255
FORC3 mul[c] = getreal(type);
4256
if (tag == 2140 + wbi && wbi >= 0)
4258
for (num=i=0; i < 4; i++)
4259
num += getreal(type) * pow (wbtemp/100.0, i);
4260
cam_mul[c] = 2048 / (num * mul[c]);
4262
if (tag == 2317) linear_table (len);
4263
if (tag == 6020) iso_speed = getint(type);
4264
fseek (ifp, save, SEEK_SET);
4268
void CLASS parse_minolta (int base);
4270
int CLASS parse_tiff_ifd (int base, int level)
4272
unsigned entries, tag, type, len, plen=16, save;
4273
int ifd, use_cm=0, cfa, i, j, c, ima_len=0;
4274
char software[64], *cbuf, *cp;
4275
uchar cfa_pat[16], cfa_pc[] = { 0,1,2,3 }, tab[256];
4276
double dblack, cc[4][4], cm[4][3], cam_xyz[4][3], num;
4277
double ab[]={ 1,1,1,1 }, asn[] = { 0,0,0,0 }, xyz[] = { 1,1,1 };
4278
unsigned *buf, sony_offset=0, sony_length=0, sony_key=0;
4282
if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
4285
for (j=0; j < 4; j++)
4286
for (i=0; i < 4; i++)
4289
if (entries > 512) return 1;
4291
tiff_get (base, &tag, &type, &len, &save);
4294
if (type == 3 && len == 1)
4295
cam_mul[(tag-17)*2] = get2() / 256.0;
4298
if (type == 3) iso_speed = get2();
4300
case 36: case 37: case 38:
4301
cam_mul[tag-0x24] = get2();
4304
if (len < 50 || cam_mul[0]) break;
4305
fseek (ifp, 12, SEEK_CUR);
4306
FORC3 cam_mul[c] = get2();
4308
case 2: case 256: /* ImageWidth */
4309
tiff_ifd[ifd].width = getint(type);
4311
case 3: case 257: /* ImageHeight */
4312
tiff_ifd[ifd].height = getint(type);
4314
case 258: /* BitsPerSample */
4315
tiff_ifd[ifd].samples = len;
4316
tiff_ifd[ifd].bps = get2();
4318
case 259: /* Compression */
4319
tiff_ifd[ifd].comp = get2();
4321
case 262: /* PhotometricInterpretation */
4322
tiff_ifd[ifd].phint = get2();
4324
case 271: /* Make */
4325
fgets (make, 64, ifp);
4327
case 272: /* Model */
4328
fgets (model, 64, ifp);
4330
case 273: /* StripOffset */
4332
tiff_ifd[ifd].offset = get4()+base;
4333
if (!tiff_ifd[ifd].bps) {
4334
fseek (ifp, tiff_ifd[ifd].offset, SEEK_SET);
4335
if (ljpeg_start (&jh, 1)) {
4336
tiff_ifd[ifd].comp = 6;
4337
tiff_ifd[ifd].width = jh.wide << (jh.clrs == 2);
4338
tiff_ifd[ifd].height = jh.high;
4339
tiff_ifd[ifd].bps = jh.bits;
4340
tiff_ifd[ifd].samples = jh.clrs;
4344
case 274: /* Orientation */
4345
tiff_ifd[ifd].flip = "50132467"[get2() & 7]-'0';
4347
case 277: /* SamplesPerPixel */
4348
tiff_ifd[ifd].samples = getint(type);
4350
case 279: /* StripByteCounts */
4352
tiff_ifd[ifd].bytes = get4();
4354
case 305: /* Software */
4355
fgets (software, 64, ifp);
4356
if (!strncmp(software,"Adobe",5) ||
4357
!strncmp(software,"dcraw",5) ||
4358
!strncmp(software,"Bibble",6) ||
4359
!strncmp(software,"Nikon Scan",10) ||
4360
!strcmp (software,"Digital Photo Professional"))
4363
case 306: /* DateTime */
4366
case 323: /* TileLength */
4367
tile_length = getint(type);
4369
case 324: /* TileOffsets */
4370
tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
4371
if (len == 4) load_raw = &CLASS sinar_4shot_load_raw;
4373
case 330: /* SubIFDs */
4374
if (!strcmp(model,"DSLR-A100") && tiff_ifd[ifd].width == 3872) {
4375
data_offset = get4()+base;
4380
fseek (ifp, get4()+base, SEEK_SET);
4381
if (parse_tiff_ifd (base, level+1)) break;
4382
fseek (ifp, i+4, SEEK_SET);
4386
strcpy (make, "Sarnoff");
4389
case 29184: sony_offset = get4(); break;
4390
case 29185: sony_length = get4(); break;
4391
case 29217: sony_key = get4(); break;
4393
FORC4 cam_mul[c ^ (c < 2)] = get2();
4395
case 33405: /* Model2 */
4396
fgets (model2, 64, ifp);
4398
case 33422: /* CFAPattern */
4399
case 64777: /* Kodak P-series */
4400
if ((plen=len) > 16) plen = 16;
4401
fread (cfa_pat, 1, plen, ifp);
4402
for (colors=cfa=i=0; i < plen; i++) {
4403
colors += !(cfa & (1 << cfa_pat[i]));
4404
cfa |= 1 << cfa_pat[i];
4406
if (cfa == 070) memcpy (cfa_pc,"\003\004\005",3); /* CMY */
4407
if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4); /* GMCY */
4410
fseek (ifp, get4()+base, SEEK_SET);
4411
parse_kodak_ifd (base);
4413
case 33434: /* ExposureTime */
4416
case 33437: /* FNumber */
4417
aperture = getrat();
4419
case 34306: /* Leaf white balance */
4420
FORC4 cam_mul[c ^ 1] = 4096.0 / get2();
4422
case 34307: /* Leaf CatchLight color matrix */
4423
fread (software, 1, 7, ifp);
4424
if (strncmp(software,"MATRIX",6)) break;
4426
for (raw_color = i=0; i < 3; i++) {
4427
FORC4 fscanf (ifp, "%f", &rgb_cam[i][c^1]);
4428
if (!use_camera_wb) continue;
4430
FORC4 num += rgb_cam[i][c];
4431
FORC4 rgb_cam[i][c] /= num;
4434
case 34310: /* Leaf metadata */
4435
parse_mos (ftell(ifp));
4437
strcpy (make, "Leaf");
4439
case 34665: /* EXIF tag */
4440
fseek (ifp, get4()+base, SEEK_SET);
4443
case 34675: /* InterColorProfile */
4444
case 50831: /* AsShotICCProfile */
4445
profile_offset = ftell(ifp);
4446
profile_length = len;
4448
case 37122: /* CompressedBitsPerPixel */
4449
kodak_cbpp = get4();
4451
case 37386: /* FocalLength */
4452
focal_len = getrat();
4454
case 37393: /* ImageNumber */
4455
shot_order = getint(type);
4457
case 37400: /* old Kodak KDC tag */
4458
for (raw_color = i=0; i < 3; i++) {
4460
FORC3 rgb_cam[i][c] = getrat();
4463
case 46275: /* Imacon tags */
4464
strcpy (make, "Imacon");
4465
data_offset = ftell(ifp);
4469
fseek (ifp, 78, SEEK_CUR);
4471
raw_height = get4();
4472
left_margin = get4() & 7;
4473
width = raw_width - left_margin - (get4() & 7);
4474
top_margin = get4() & 7;
4475
height = raw_height - top_margin - (get4() & 7);
4476
fseek (ifp, 52, SEEK_CUR);
4477
FORC3 cam_mul[c] = getreal(11);
4478
fseek (ifp, 114, SEEK_CUR);
4479
flip = (get2() >> 7) * 90;
4480
if (width * height * 6 == ima_len) {
4481
if (flip % 180 == 90) SWAP(width,height);
4485
case 50454: /* Sinar tag */
4487
if (!(cbuf = (char *) malloc(len))) break;
4488
fread (cbuf, 1, len, ifp);
4489
for (cp = cbuf-1; cp && cp < cbuf+len; cp = strchr(cp,'\n'))
4490
if (!strncmp (++cp,"Neutral ",8))
4491
sscanf (cp+8, "%f %f %f", cam_mul, cam_mul+1, cam_mul+2);
4494
case 50706: /* DNGVersion */
4495
FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
4497
case 50710: /* CFAPlaneColor */
4498
if (len > 4) len = 4;
4500
fread (cfa_pc, 1, colors, ifp);
4502
FORCC tab[cfa_pc[c]] = c;
4505
filters = filters << 2 | tab[cfa_pat[i % plen]];
4507
case 50711: /* CFALayout */
4510
filters = 0x49494949;
4514
case 50712: /* LinearizationTable */
4517
case 50714: /* BlackLevel */
4518
case 50715: /* BlackLevelDeltaH */
4519
case 50716: /* BlackLevelDeltaV */
4520
for (dblack=i=0; i < len; i++)
4521
dblack += getreal(type);
4522
black += dblack/len + 0.5;
4524
case 50717: /* WhiteLevel */
4525
maximum = getint(type);
4527
case 50718: /* DefaultScale */
4528
pixel_aspect = getrat();
4529
pixel_aspect /= getrat();
4531
case 50721: /* ColorMatrix1 */
4532
case 50722: /* ColorMatrix2 */
4533
FORCC for (j=0; j < 3; j++)
4534
cm[c][j] = getrat();
4537
case 50723: /* CameraCalibration1 */
4538
case 50724: /* CameraCalibration2 */
4539
for (i=0; i < colors; i++)
4540
FORCC cc[i][c] = getrat();
4541
case 50727: /* AnalogBalance */
4542
FORCC ab[c] = getrat();
4544
case 50728: /* AsShotNeutral */
4545
FORCC asn[c] = getreal(type);
4547
case 50729: /* AsShotWhiteXY */
4550
xyz[2] = 1 - xyz[0] - xyz[1];
4551
FORC3 xyz[c] /= d65_white[c];
4553
case 50740: /* DNGPrivateData */
4554
if (dng_version) break;
4556
parse_minolta (j = get4()+base);
4558
fseek (ifp, j, SEEK_SET);
4559
parse_tiff_ifd (base, level+1);
4562
read_shorts (cr2_slice, 3);
4564
case 50829: /* ActiveArea */
4565
top_margin = getint(type);
4566
left_margin = getint(type);
4567
height = getint(type) - top_margin;
4568
width = getint(type) - left_margin;
4570
case 64772: /* Kodak P-series */
4571
fseek (ifp, 16, SEEK_CUR);
4572
data_offset = get4();
4573
fseek (ifp, 28, SEEK_CUR);
4574
data_offset += get4();
4575
load_raw = &CLASS packed_12_load_raw;
4577
fseek (ifp, save, SEEK_SET);
4579
if (sony_length && (buf = (unsigned *) malloc(sony_length))) {
4580
fseek (ifp, sony_offset, SEEK_SET);
4581
fread (buf, sony_length, 1, ifp);
4582
sony_decrypt (buf, sony_length/4, 1, sony_key);
4584
if ((ifp = tmpfile())) {
4585
fwrite (buf, sony_length, 1, ifp);
4586
fseek (ifp, 0, SEEK_SET);
4587
parse_tiff_ifd (-sony_offset, level);
4593
for (i=0; i < colors; i++)
4594
FORCC cc[i][c] *= ab[i];
4596
FORCC for (i=0; i < 3; i++)
4597
for (cam_xyz[c][i]=j=0; j < colors; j++)
4598
cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
4599
cam_xyz_coeff (cam_xyz);
4602
FORCC pre_mul[c] = 1 / asn[c];
4604
FORCC pre_mul[c] /= cc[c][c];
4608
void CLASS parse_tiff (int base)
4610
int doff, max_samp=0, raw=-1, thm=-1, i;
4613
fseek (ifp, base, SEEK_SET);
4615
if (order != 0x4949 && order != 0x4d4d) return;
4617
memset (tiff_ifd, 0, sizeof tiff_ifd);
4619
while ((doff = get4())) {
4620
fseek (ifp, doff+base, SEEK_SET);
4621
if (parse_tiff_ifd (base, 0)) break;
4625
fseek (ifp, thumb_offset, SEEK_SET);
4626
if (ljpeg_start (&jh, 1)) {
4627
thumb_misc = jh.bits;
4628
thumb_width = jh.wide;
4629
thumb_height = jh.high;
4632
for (i=0; i < tiff_nifds; i++) {
4633
if (max_samp < tiff_ifd[i].samples)
4634
max_samp = tiff_ifd[i].samples;
4635
if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
4636
tiff_ifd[i].width*tiff_ifd[i].height > raw_width*raw_height) {
4637
raw_width = tiff_ifd[i].width;
4638
raw_height = tiff_ifd[i].height;
4639
tiff_bps = tiff_ifd[i].bps;
4640
tiff_compress = tiff_ifd[i].comp;
4641
data_offset = tiff_ifd[i].offset;
4642
tiff_flip = tiff_ifd[i].flip;
4643
tiff_samples = tiff_ifd[i].samples;
4644
fuji_secondary = tiff_samples == 2;
4648
fuji_width *= (raw_width+1)/2;
4649
if (tiff_ifd[0].flip) tiff_flip = tiff_ifd[0].flip;
4650
if (raw >= 0 && !load_raw)
4651
switch (tiff_compress) {
4653
load_raw = tiff_bps > 8 ?
4654
&CLASS unpacked_load_raw : &CLASS eight_bit_load_raw;
4655
if (tiff_ifd[raw].bytes * 5 == raw_width * raw_height * 8)
4656
load_raw = &CLASS olympus_e300_load_raw;
4658
case 6: case 7: case 99:
4659
load_raw = &CLASS lossless_jpeg_load_raw; break;
4661
load_raw = &CLASS kodak_262_load_raw; break;
4663
load_raw = &CLASS packed_12_load_raw; break;
4665
load_raw = &CLASS pentax_k10_load_raw; break;
4667
switch (tiff_ifd[raw].phint) {
4668
case 2: load_raw = &CLASS kodak_rgb_load_raw; filters = 0; break;
4669
case 6: load_raw = &CLASS kodak_ycbcr_load_raw; filters = 0; break;
4670
case 32803: load_raw = &CLASS kodak_65000_load_raw;
4673
if (tiff_samples == 3 && tiff_bps == 8)
4674
if (!dng_version) is_raw = 0;
4675
for (i=0; i < tiff_nifds; i++)
4676
if (i != raw && tiff_ifd[i].samples == max_samp &&
4677
tiff_ifd[i].width * tiff_ifd[i].height / SQR(tiff_ifd[i].bps+1) >
4678
thumb_width * thumb_height / SQR(thumb_misc+1)) {
4679
thumb_width = tiff_ifd[i].width;
4680
thumb_height = tiff_ifd[i].height;
4681
thumb_offset = tiff_ifd[i].offset;
4682
thumb_length = tiff_ifd[i].bytes;
4683
thumb_misc = tiff_ifd[i].bps;
4687
thumb_misc |= tiff_ifd[thm].samples << 5;
4688
switch (tiff_ifd[thm].comp) {
4690
write_thumb = &CLASS layer_thumb;
4693
if (tiff_ifd[thm].bps > 8)
4694
thumb_load_raw = &CLASS kodak_thumb_load_raw;
4696
write_thumb = &CLASS ppm_thumb;
4699
thumb_load_raw = tiff_ifd[thm].phint == 6 ?
4700
&CLASS kodak_ycbcr_load_raw : &CLASS kodak_rgb_load_raw;
4705
void CLASS parse_minolta (int base)
4707
int save, tag, len, offset, high=0, wide=0, i, c;
4709
fseek (ifp, base, SEEK_SET);
4710
if (fgetc(ifp) || fgetc(ifp)-'M' || fgetc(ifp)-'R') return;
4711
order = fgetc(ifp) * 0x101;
4712
offset = base + get4() + 8;
4713
while ((save=ftell(ifp)) < offset) {
4714
for (tag=i=0; i < 4; i++)
4715
tag = tag << 8 | fgetc(ifp);
4718
case 0x505244: /* PRD */
4719
fseek (ifp, 8, SEEK_CUR);
4723
case 0x574247: /* WBG */
4725
i = strstr(model,"A200") ? 3:0;
4726
FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
4728
case 0x545457: /* TTW */
4729
parse_tiff (ftell(ifp));
4730
data_offset = offset;
4732
fseek (ifp, save+len+8, SEEK_SET);
4739
Many cameras have a "debug mode" that writes JPEG and raw
4740
at the same time. The raw file has no header, so try to
4741
to open the matching JPEG file and read its metadata.
4743
void CLASS parse_external_jpeg()
4745
char *file, *ext, *jname, *jfile, *jext;
4748
ext = strrchr (ifname, '.');
4749
file = strrchr (ifname, '/');
4750
if (!file) file = strrchr (ifname, '\\');
4751
if (!file) file = ifname-1;
4753
if (!ext || strlen(ext) != 4 || ext-file != 8) return;
4754
jname = (char *) malloc (strlen(ifname) + 1);
4755
merror (jname, "parse_external()");
4756
strcpy (jname, ifname);
4757
jfile = file - ifname + jname;
4758
jext = ext - ifname + jname;
4759
if (strcasecmp (ext, ".jpg")) {
4760
strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
4761
memcpy (jfile, file+4, 4);
4762
memcpy (jfile+4, file, 4);
4764
while (isdigit(*--jext)) {
4771
if (strcmp (jname, ifname)) {
4772
if ((ifp = fopen (jname, "rb"))) {
4774
fprintf (stderr,_("Reading metadata from %s ...\n"), jname);
4782
fprintf (stderr,_("Failed to read metadata from %s\n"), jname);
4788
CIFF block 0x1030 contains an 8x8 white sample.
4789
Load this into white[][] for use in scale_colors().
4791
void CLASS ciff_block_1030()
4793
static const ushort key[] = { 0x410, 0x45f3 };
4794
int i, bpp, row, col, vbits=0;
4795
unsigned long bitbuf=0;
4797
if ((get2(),get4()) != 0x80008 || !get4()) return;
4799
if (bpp != 10 && bpp != 12) return;
4800
for (i=row=0; row < 8; row++)
4801
for (col=0; col < 8; col++) {
4803
bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
4807
bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp);
4813
Parse a CIFF file, better known as Canon CRW format.
4815
void CLASS parse_ciff (int offset, int length)
4817
int tboff, nrecs, c, type, len, save, wbi=-1;
4818
ushort key[] = { 0x410, 0x45f3 };
4820
fseek (ifp, offset+length-4, SEEK_SET);
4821
tboff = get4() + offset;
4822
fseek (ifp, tboff, SEEK_SET);
4824
if (nrecs > 100) return;
4828
save = ftell(ifp) + 4;
4829
fseek (ifp, offset+get4(), SEEK_SET);
4830
if ((((type >> 8) + 8) | 8) == 0x38)
4831
parse_ciff (ftell(ifp), len); /* Parse a sub-table */
4833
if (type == 0x080a) {
4834
fread (make, 64, 1, ifp);
4835
fseek (ifp, strlen(make) - 63, SEEK_CUR);
4836
fread (model, 64, 1, ifp);
4838
if (type == 0x1810) {
4839
fseek (ifp, 12, SEEK_CUR);
4842
if (type == 0x1835) /* Get the decoder table */
4843
tiff_compress = get4();
4844
if (type == 0x2007) {
4845
thumb_offset = ftell(ifp);
4848
if (type == 0x1818) {
4849
shutter = pow (2, -int_to_float((get4(),get4())));
4850
aperture = pow (2, int_to_float(get4())/2);
4852
if (type == 0x102a) {
4853
iso_speed = pow (2, (get4(),get2())/32.0 - 4) * 50;
4854
aperture = pow (2, (get2(),(short)get2())/64.0);
4855
shutter = pow (2,-((short)get2())/32.0);
4856
wbi = (get2(),get2());
4857
if (wbi > 17) wbi = 0;
4858
fseek (ifp, 32, SEEK_CUR);
4859
if (shutter > 1e6) shutter = get2()/10.0;
4861
if (type == 0x102c) {
4862
if (get2() > 512) { /* Pro90, G1 */
4863
fseek (ifp, 118, SEEK_CUR);
4864
FORC4 cam_mul[c ^ 2] = get2();
4865
} else { /* G2, S30, S40 */
4866
fseek (ifp, 98, SEEK_CUR);
4867
FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2();
4870
if (type == 0x0032) {
4871
if (len == 768) { /* EOS D30 */
4872
fseek (ifp, 72, SEEK_CUR);
4873
FORC4 cam_mul[c ^ (c >> 1)] = 1024.0 / get2();
4874
if (!wbi) cam_mul[0] = -1; /* use my auto white balance */
4875
} else if (!cam_mul[0]) {
4876
if (get2() == key[0]) /* Pro1, G6, S60, S70 */
4877
c = (strstr(model,"Pro1") ?
4878
"012346000000000000":"01345:000000006008")[wbi]-'0'+ 2;
4879
else { /* G3, G5, S45, S50 */
4880
c = "023457000000006000"[wbi]-'0';
4881
key[0] = key[1] = 0;
4883
fseek (ifp, 78 + c*8, SEEK_CUR);
4884
FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2() ^ key[c & 1];
4885
if (!wbi) cam_mul[0] = -1;
4888
if (type == 0x10a9) { /* D60, 10D, 300D, and clones */
4889
if (len > 66) wbi = "0134567028"[wbi]-'0';
4890
fseek (ifp, 2 + wbi*8, SEEK_CUR);
4891
FORC4 cam_mul[c ^ (c >> 1)] = get2();
4893
if (type == 0x1030 && (0x18040 >> wbi & 1))
4894
ciff_block_1030(); /* all that don't have 0x10a9 */
4895
if (type == 0x1031) {
4896
raw_width = (get2(),get2());
4897
raw_height = get2();
4899
if (type == 0x5029) {
4900
focal_len = len >> 16;
4901
if ((len & 0xffff) == 2) focal_len /= 32;
4903
if (type == 0x5813) flash_used = int_to_float(len);
4904
if (type == 0x5814) canon_ev = int_to_float(len);
4905
if (type == 0x5817) shot_order = len;
4906
if (type == 0x5834) unique_id = len;
4907
if (type == 0x580e) timestamp = len;
4908
if (type == 0x180e) timestamp = get4();
4910
if ((type | 0x4000) == 0x580e)
4911
timestamp = mktime (gmtime (×tamp));
4913
fseek (ifp, save, SEEK_SET);
4917
void CLASS parse_rollei()
4919
char line[128], *val;
4922
fseek (ifp, 0, SEEK_SET);
4923
memset (&t, 0, sizeof t);
4925
fgets (line, 128, ifp);
4926
if ((val = strchr(line,'=')))
4929
val = line + strlen(line);
4930
if (!strcmp(line,"DAT"))
4931
sscanf (val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
4932
if (!strcmp(line,"TIM"))
4933
sscanf (val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
4934
if (!strcmp(line,"HDR"))
4935
thumb_offset = atoi(val);
4936
if (!strcmp(line,"X "))
4937
raw_width = atoi(val);
4938
if (!strcmp(line,"Y "))
4939
raw_height = atoi(val);
4940
if (!strcmp(line,"TX "))
4941
thumb_width = atoi(val);
4942
if (!strcmp(line,"TY "))
4943
thumb_height = atoi(val);
4944
} while (strncmp(line,"EOHD",4));
4945
data_offset = thumb_offset + thumb_width * thumb_height * 2;
4949
timestamp = mktime(&t);
4950
strcpy (make, "Rollei");
4951
strcpy (model,"d530flex");
4952
write_thumb = &CLASS rollei_thumb;
4955
void CLASS parse_sinar_ia()
4961
fseek (ifp, 4, SEEK_SET);
4963
fseek (ifp, get4(), SEEK_SET);
4965
off = get4(); get4();
4966
fread (str, 8, 1, ifp);
4967
if (!strcmp(str,"META")) meta_offset = off;
4968
if (!strcmp(str,"THUMB")) thumb_offset = off;
4969
if (!strcmp(str,"RAW0")) data_offset = off;
4971
fseek (ifp, meta_offset+20, SEEK_SET);
4972
fread (make, 64, 1, ifp);
4974
if ((cp = strchr(make,' '))) {
4975
strcpy (model, cp+1);
4979
raw_height = get2();
4980
load_raw = &CLASS unpacked_load_raw;
4981
thumb_width = (get4(),get2());
4982
thumb_height = get2();
4983
write_thumb = &CLASS ppm_thumb;
4987
void CLASS parse_phase_one (int base)
4989
unsigned entries, tag, type, len, data, save, i, c;
4990
float romm_cam[3][3];
4993
memset (&ph1, 0, sizeof ph1);
4994
fseek (ifp, base, SEEK_SET);
4995
order = get4() & 0xffff;
4996
if (get4() >> 8 != 0x526177) return; /* "Raw" */
4997
fseek (ifp, base+get4(), SEEK_SET);
5006
fseek (ifp, base+data, SEEK_SET);
5008
case 0x100: flip = "0653"[data & 3]-'0'; break;
5010
for (i=0; i < 9; i++)
5011
romm_cam[0][i] = getreal(11);
5012
romm_coeff (romm_cam);
5015
FORC3 cam_mul[c] = pre_mul[c] = getreal(11);
5017
case 0x108: raw_width = data; break;
5018
case 0x109: raw_height = data; break;
5019
case 0x10a: left_margin = data; break;
5020
case 0x10b: top_margin = data; break;
5021
case 0x10c: width = data; break;
5022
case 0x10d: height = data; break;
5023
case 0x10e: ph1.format = data; break;
5024
case 0x10f: data_offset = data+base; break;
5025
case 0x110: meta_offset = data+base;
5026
meta_length = len; break;
5027
case 0x112: ph1.key_off = save - 4; break;
5028
case 0x210: ph1.tag_210 = int_to_float(data); break;
5029
case 0x21a: ph1.tag_21a = data; break;
5030
case 0x21c: strip_offset = data+base; break;
5031
case 0x21d: ph1.black = data; break;
5032
case 0x222: ph1.split_col = data - left_margin; break;
5033
case 0x223: ph1.black_off = data+base; break;
5036
fread (model, 1, 63, ifp);
5037
if ((cp = strstr(model," camera"))) *cp = 0;
5039
fseek (ifp, save, SEEK_SET);
5041
load_raw = ph1.format < 3 ?
5042
&CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
5044
strcpy (make, "Phase One");
5045
if (model[0]) return;
5046
switch (raw_height) {
5047
case 2060: strcpy (model,"LightPhase"); break;
5048
case 2682: strcpy (model,"H 10"); break;
5049
case 4128: strcpy (model,"H 20"); break;
5050
case 5488: strcpy (model,"H 25"); break;
5054
void CLASS parse_fuji (int offset)
5056
unsigned entries, tag, len, save, c;
5058
fseek (ifp, offset, SEEK_SET);
5060
if (entries > 255) return;
5066
raw_height = get2();
5068
} else if (tag == 0x121) {
5070
if ((width = get2()) == 4284) width += 3;
5071
} else if (tag == 0x130)
5072
fuji_layout = fgetc(ifp) >> 7;
5074
FORC4 cam_mul[c ^ 1] = get2();
5075
fseek (ifp, save+len, SEEK_SET);
5083
int CLASS parse_jpeg (int offset)
5085
int len, save, hlen, mark;
5087
fseek (ifp, offset, SEEK_SET);
5088
if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
5090
while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
5094
if (mark == 0xc0 || mark == 0xc3) {
5096
raw_height = get2();
5101
if (get4() == 0x48454150) /* "HEAP" */
5102
parse_ciff (save+hlen, len-hlen);
5103
parse_tiff (save+6);
5104
fseek (ifp, save+len, SEEK_SET);
5109
void CLASS parse_riff()
5111
unsigned i, size, end;
5112
char tag[4], date[64], month[64];
5113
static const char mon[12][4] =
5114
{ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
5118
fread (tag, 4, 1, ifp);
5120
if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
5121
end = ftell(ifp) + size;
5123
while (ftell(ifp) < end)
5125
} else if (!memcmp(tag,"IDIT",4) && size < 64) {
5126
fread (date, 64, 1, ifp);
5128
memset (&t, 0, sizeof t);
5129
if (sscanf (date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday,
5130
&t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6) {
5131
for (i=0; i < 12 && strcmp(mon[i],month); i++);
5135
timestamp = mktime(&t);
5138
fseek (ifp, size, SEEK_CUR);
5141
void CLASS parse_smal (int offset, int fsize)
5145
fseek (ifp, offset+2, SEEK_SET);
5149
fseek (ifp, 5, SEEK_CUR);
5150
if (get4() != fsize) return;
5151
if (ver > 6) data_offset = get4();
5152
raw_height = height = get2();
5153
raw_width = width = get2();
5154
strcpy (make, "SMaL");
5155
sprintf (model, "v%d %dx%d", ver, width, height);
5156
if (ver == 6) load_raw = &CLASS smal_v6_load_raw;
5157
if (ver == 9) load_raw = &CLASS smal_v9_load_raw;
5160
char * CLASS foveon_gets (int offset, char *str, int len)
5163
fseek (ifp, offset, SEEK_SET);
5164
for (i=0; i < len-1; i++)
5165
if ((str[i] = get2()) == 0) break;
5170
void CLASS parse_foveon()
5172
int entries, img=0, off, len, tag, save, i, wide, high, pent, poff[256][2];
5173
char name[64], value[64];
5175
order = 0x4949; /* Little-endian */
5176
fseek (ifp, 36, SEEK_SET);
5178
fseek (ifp, -4, SEEK_END);
5179
fseek (ifp, get4(), SEEK_SET);
5180
if (get4() != 0x64434553) return; /* SECd */
5181
entries = (get4(),get4());
5187
fseek (ifp, off, SEEK_SET);
5188
if (get4() != (0x20434553 | (tag << 24))) return;
5190
case 0x47414d49: /* IMAG */
5191
case 0x32414d49: /* IMA2 */
5192
fseek (ifp, 12, SEEK_CUR);
5195
if (wide > raw_width && high > raw_height) {
5198
data_offset = off+24;
5200
fseek (ifp, off+28, SEEK_SET);
5201
if (fgetc(ifp) == 0xff && fgetc(ifp) == 0xd8) {
5202
thumb_offset = off+28;
5203
thumb_length = len-28;
5205
if (++img == 2 && !thumb_length) {
5206
thumb_offset = off+24;
5208
thumb_height = high;
5209
write_thumb = &CLASS foveon_thumb;
5212
case 0x464d4143: /* CAMF */
5213
meta_offset = off+24;
5214
meta_length = len-28;
5215
if (meta_length > 0x20000)
5216
meta_length = 0x20000;
5218
case 0x504f5250: /* PROP */
5219
pent = (get4(),get4());
5220
fseek (ifp, 12, SEEK_CUR);
5222
if (pent > 256) pent=256;
5223
for (i=0; i < pent*2; i++)
5224
poff[0][i] = off + get4()*2;
5225
for (i=0; i < pent; i++) {
5226
foveon_gets (poff[i][0], name, 64);
5227
foveon_gets (poff[i][1], value, 64);
5228
if (!strcmp (name, "ISO"))
5229
iso_speed = atoi(value);
5230
if (!strcmp (name, "CAMMANUF"))
5231
strcpy (make, value);
5232
if (!strcmp (name, "CAMMODEL"))
5233
strcpy (model, value);
5234
if (!strcmp (name, "WB_DESC"))
5235
strcpy (model2, value);
5236
if (!strcmp (name, "TIME"))
5237
timestamp = atoi(value);
5238
if (!strcmp (name, "EXPTIME"))
5239
shutter = atoi(value) / 1000000.0;
5240
if (!strcmp (name, "APERTURE"))
5241
aperture = atof(value);
5242
if (!strcmp (name, "FLENGTH"))
5243
focal_len = atof(value);
5246
timestamp = mktime (gmtime (×tamp));
5249
fseek (ifp, save, SEEK_SET);
5255
Thanks to Adobe for providing these excellent CAM -> XYZ matrices!
5257
void CLASS adobe_coeff (char *make, char *model)
5259
static const struct {
5261
short black, trans[12];
5263
{ "Canon EOS D2000", 0,
5264
{ 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
5265
{ "Canon EOS D6000", 0,
5266
{ 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
5267
{ "Canon EOS D30", 0,
5268
{ 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
5269
{ "Canon EOS D60", 0,
5270
{ 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
5271
{ "Canon EOS 5D", 0,
5272
{ 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
5273
{ "Canon EOS 20Da", 0,
5274
{ 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
5275
{ "Canon EOS 20D", 0,
5276
{ 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
5277
{ "Canon EOS 30D", 0,
5278
{ 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
5279
{ "Canon EOS 350D", 0,
5280
{ 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
5281
{ "Canon EOS 400D", 0,
5282
{ 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
5283
{ "Canon EOS-1Ds Mark II", 0,
5284
{ 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
5285
{ "Canon EOS-1D Mark II N", 0,
5286
{ 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
5287
{ "Canon EOS-1D Mark II", 0,
5288
{ 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
5289
{ "Canon EOS-1DS", 0,
5290
{ 4374,3631,-1743,-7520,15212,2472,-2892,3632,8161 } },
5291
{ "Canon EOS-1D", 0,
5292
{ 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
5294
{ 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
5295
{ "Canon PowerShot A50", 0,
5296
{ -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
5297
{ "Canon PowerShot A5", 0,
5298
{ -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
5299
{ "Canon PowerShot G1", 0,
5300
{ -4778,9467,2172,4743,-1141,4344,-5146,9908,6077,-1566,11051,557 } },
5301
{ "Canon PowerShot G2", 0,
5302
{ 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
5303
{ "Canon PowerShot G3", 0,
5304
{ 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
5305
{ "Canon PowerShot G5", 0,
5306
{ 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
5307
{ "Canon PowerShot G6", 0,
5308
{ 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
5309
{ "Canon PowerShot Pro1", 0,
5310
{ 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
5311
{ "Canon PowerShot Pro70", 34,
5312
{ -4155,9818,1529,3939,-25,4522,-5521,9870,6610,-2238,10873,1342 } },
5313
{ "Canon PowerShot Pro90", 0,
5314
{ -4963,9896,2235,4642,-987,4294,-5162,10011,5859,-1770,11230,577 } },
5315
{ "Canon PowerShot S30", 0,
5316
{ 10566,-3652,-1129,-6552,14662,2006,-2197,2581,7670 } },
5317
{ "Canon PowerShot S40", 0,
5318
{ 8510,-2487,-940,-6869,14231,2900,-2318,2829,9013 } },
5319
{ "Canon PowerShot S45", 0,
5320
{ 8163,-2333,-955,-6682,14174,2751,-2077,2597,8041 } },
5321
{ "Canon PowerShot S50", 0,
5322
{ 8882,-2571,-863,-6348,14234,2288,-1516,2172,6569 } },
5323
{ "Canon PowerShot S60", 0,
5324
{ 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
5325
{ "Canon PowerShot S70", 0,
5326
{ 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
5327
{ "Canon PowerShot A610", 0, /* DJC */
5328
{ 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
5329
{ "Canon PowerShot A620", 0, /* DJC */
5330
{ 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
5331
{ "Canon PowerShot S3 IS", 0, /* DJC */
5332
{ 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
5333
{ "Contax N Digital", 0,
5334
{ 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
5336
{ 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
5337
{ "FUJIFILM FinePix E550", 0,
5338
{ 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
5339
{ "FUJIFILM FinePix E900", 0,
5340
{ 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
5341
{ "FUJIFILM FinePix F8", 0,
5342
{ 11044,-3888,-1120,-7248,15168,2208,-1531,2277,8069 } },
5343
{ "FUJIFILM FinePix F7", 0,
5344
{ 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
5345
{ "FUJIFILM FinePix S20Pro", 0,
5346
{ 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
5347
{ "FUJIFILM FinePix S2Pro", 128,
5348
{ 12492,-4690,-1402,-7033,15423,1647,-1507,2111,7697 } },
5349
{ "FUJIFILM FinePix S3Pro", 0,
5350
{ 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
5351
{ "FUJIFILM FinePix S5000", 0,
5352
{ 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
5353
{ "FUJIFILM FinePix S5100", 0,
5354
{ 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
5355
{ "FUJIFILM FinePix S5500", 0,
5356
{ 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
5357
{ "FUJIFILM FinePix S5200", 0,
5358
{ 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
5359
{ "FUJIFILM FinePix S5600", 0,
5360
{ 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
5361
{ "FUJIFILM FinePix S7000", 0,
5362
{ 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
5363
{ "FUJIFILM FinePix S9000", 0,
5364
{ 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
5365
{ "FUJIFILM FinePix S9500", 0,
5366
{ 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
5367
{ "FUJIFILM FinePix S9100", 0,
5368
{ 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
5369
{ "FUJIFILM FinePix S9600", 0,
5370
{ 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
5371
{ "Imacon Ixpress", 0, /* DJC */
5372
{ 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
5373
{ "KODAK NC2000", 0, /* DJC */
5374
{ 16475,-6903,-1218,-851,10375,477,2505,-7,1020 } },
5375
{ "Kodak DCS315C", 8,
5376
{ 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
5377
{ "Kodak DCS330C", 8,
5378
{ 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
5379
{ "KODAK DCS420", 0,
5380
{ 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
5381
{ "KODAK DCS460", 0,
5382
{ 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
5383
{ "KODAK EOSDCS1", 0,
5384
{ 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
5385
{ "KODAK EOSDCS3B", 0,
5386
{ 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
5387
{ "Kodak DCS520C", 180,
5388
{ 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
5389
{ "Kodak DCS560C", 188,
5390
{ 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
5391
{ "Kodak DCS620C", 180,
5392
{ 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
5393
{ "Kodak DCS620X", 185,
5394
{ 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
5395
{ "Kodak DCS660C", 214,
5396
{ 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
5397
{ "Kodak DCS720X", 0,
5398
{ 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
5399
{ "Kodak DCS760C", 0,
5400
{ 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
5401
{ "Kodak DCS Pro SLR", 0,
5402
{ 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
5403
{ "Kodak DCS Pro 14nx", 0,
5404
{ 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
5405
{ "Kodak DCS Pro 14", 0,
5406
{ 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
5407
{ "Kodak ProBack645", 0,
5408
{ 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
5409
{ "Kodak ProBack", 0,
5410
{ 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
5412
{ 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
5414
{ 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
5416
{ 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
5418
{ 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
5419
{ "Leaf Valeo 6", 0,
5420
{ 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
5421
{ "Leaf Aptus 65", 0,
5422
{ 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
5423
{ "Leaf Aptus 75", 0,
5424
{ 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
5426
{ 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
5427
{ "Micron 2010", 110, /* DJC */
5428
{ 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
5429
{ "Minolta DiMAGE 5", 0,
5430
{ 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
5431
{ "Minolta DiMAGE 7Hi", 0,
5432
{ 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
5433
{ "Minolta DiMAGE 7", 0,
5434
{ 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
5435
{ "Minolta DiMAGE A1", 0,
5436
{ 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
5437
{ "MINOLTA DiMAGE A200", 0,
5438
{ 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
5439
{ "Minolta DiMAGE A2", 0,
5440
{ 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
5441
{ "Minolta DiMAGE Z2", 0, /* DJC */
5442
{ 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
5443
{ "MINOLTA DYNAX 5", 0,
5444
{ 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
5445
{ "MINOLTA DYNAX 7", 0,
5446
{ 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
5448
{ 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
5450
{ 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
5452
{ 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
5453
{ "NIKON D1", 0, /* multiplied by 2.218750, 1.0, 1.148438 */
5454
{ 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
5456
{ 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
5458
{ 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
5460
{ 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
5462
{ 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
5464
{ 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
5466
{ 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
5467
{ "NIKON E950", 0, /* DJC */
5468
{ -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
5469
{ "NIKON E995", 0, /* copied from E5000 */
5470
{ -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
5472
{ -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
5473
{ "NIKON E4300", 0, /* copied from Minolta DiMAGE Z2 */
5474
{ 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
5476
{ -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
5478
{ -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
5480
{ 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
5482
{ -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
5484
{ 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
5486
{ 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
5488
{ 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
5489
{ "OLYMPUS C5050", 0,
5490
{ 10508,-3124,-1273,-6079,14294,1901,-1653,2306,6237 } },
5491
{ "OLYMPUS C5060", 0,
5492
{ 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
5493
{ "OLYMPUS C7070", 0,
5494
{ 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
5496
{ 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
5498
{ 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
5499
{ "OLYMPUS E-10", 0,
5500
{ 12745,-4500,-1416,-6062,14542,1580,-1934,2256,6603 } },
5502
{ 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
5503
{ "OLYMPUS E-20", 0,
5504
{ 13173,-4732,-1499,-5807,14036,1895,-2045,2452,7142 } },
5505
{ "OLYMPUS E-300", 0,
5506
{ 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
5507
{ "OLYMPUS E-330", 0,
5508
{ 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
5509
{ "OLYMPUS E-400", 0,
5510
{ 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
5511
{ "OLYMPUS E-500", 0,
5512
{ 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
5513
{ "OLYMPUS SP350", 0,
5514
{ 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
5516
{ 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
5517
{ "OLYMPUS SP500UZ", 0,
5518
{ 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
5519
{ "OLYMPUS SP510UZ", 0,
5520
{ 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
5521
{ "PENTAX *ist DL2", 0,
5522
{ 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
5523
{ "PENTAX *ist DL", 0,
5524
{ 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
5525
{ "PENTAX *ist DS2", 0,
5526
{ 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
5527
{ "PENTAX *ist DS", 0,
5528
{ 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
5529
{ "PENTAX *ist D", 0,
5530
{ 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
5532
{ 28402,-6651,-983,-14699,32553,6467,-1746,1571,25283 } },
5534
{ 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
5535
{ "Panasonic DMC-FZ30", 0,
5536
{ 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
5537
{ "Panasonic DMC-FZ50", 0,
5538
{ 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
5539
{ "Panasonic DMC-L1", 0,
5540
{ 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
5541
{ "Panasonic DMC-LC1", 0,
5542
{ 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
5543
{ "Panasonic DMC-LX1", 0,
5544
{ 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
5545
{ "Panasonic DMC-LX2", 0,
5546
{ 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
5547
{ "SAMSUNG GX-1", 0,
5548
{ 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
5549
{ "Sinar", 0, /* DJC */
5550
{ 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
5551
{ "SONY DSC-F828", 491,
5552
{ 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
5553
{ "SONY DSC-R1", 512,
5554
{ 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
5556
{ 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
5557
{ "SONY DSLR-A100", 0,
5558
{ 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } }
5560
double cam_xyz[4][3];
5564
sprintf (name, "%s %s", make, model);
5565
for (i=0; i < sizeof table / sizeof *table; i++)
5566
if (!strncmp (name, table[i].prefix, strlen(table[i].prefix))) {
5568
black = table[i].black;
5569
for (j=0; j < 12; j++)
5570
cam_xyz[0][j] = table[i].trans[j] / 10000.0;
5571
cam_xyz_coeff (cam_xyz);
5576
void CLASS simple_coeff (int index)
5578
static const float table[][12] = {
5579
/* index 0 -- all Foveon cameras */
5580
{ 1.4032,-0.2231,-0.1016,-0.5263,1.4816,0.017,-0.0112,0.0183,0.9113 },
5581
/* index 1 -- Kodak DC20 and DC25 */
5582
{ 2.25,0.75,-1.75,-0.25,-0.25,0.75,0.75,-0.25,-0.25,-1.75,0.75,2.25 },
5583
/* index 2 -- Logitech Fotoman Pixtura */
5584
{ 1.893,-0.418,-0.476,-0.495,1.773,-0.278,-1.017,-0.655,2.672 },
5585
/* index 3 -- Nikon E880, E900, and E990 */
5586
{ -1.936280, 1.800443, -1.448486, 2.584324,
5587
1.405365, -0.524955, -0.289090, 0.408680,
5588
-1.204965, 1.082304, 2.941367, -1.818705 }
5592
for (raw_color = i=0; i < 3; i++)
5593
FORCC rgb_cam[i][c] = table[index][i*colors+c];
5596
short CLASS guess_byte_order (int words)
5600
double diff, sum[2] = {0,0};
5602
fread (test[0], 2, 2, ifp);
5603
for (words-=2; words--; ) {
5604
fread (test[t], 2, 1, ifp);
5605
for (msb=0; msb < 2; msb++) {
5606
diff = (test[t^2][msb] << 8 | test[t^2][!msb])
5607
- (test[t ][msb] << 8 | test[t ][!msb]);
5608
sum[msb] += diff*diff;
5612
return sum[0] < sum[1] ? 0x4d4d : 0x4949;
5616
Identify which camera created this file, and set global variables
5619
void CLASS identify()
5622
unsigned hlen, fsize, i, c, is_canon;
5624
static const struct {
5626
char make[12], model[19], withjpeg;
5628
{ 62464, "Kodak", "DC20" ,0 },
5629
{ 124928, "Kodak", "DC20" ,0 },
5630
{ 1652736, "Kodak", "DCS200" ,0 },
5631
{ 311696, "ST Micro", "STV680 VGA" ,0 }, /* SPYz */
5632
{ 614400, "Kodak", "KAI-0340" ,0 },
5633
{ 787456, "Creative", "PC-CAM 600" ,0 },
5634
{ 1138688, "Minolta", "RD175" ,0 },
5635
{ 3840000, "Foculus", "531C" ,0 },
5636
{ 1447680, "AVT", "F-145C" ,0 },
5637
{ 1920000, "AVT", "F-201C" ,0 },
5638
{ 5067304, "AVT", "F-510C" ,0 },
5639
{ 10134608, "AVT", "F-510C" ,0 },
5640
{ 16157136, "AVT", "F-810C" ,0 },
5641
{ 1409024, "Sony", "XCD-SX910CR",0 },
5642
{ 2818048, "Sony", "XCD-SX910CR",0 },
5643
{ 3884928, "Micron", "2010" ,0 },
5644
{ 6624000, "Pixelink", "A782" ,0 },
5645
{ 13248000, "Pixelink", "A782" ,0 },
5646
{ 6291456, "RoverShot","3320AF" ,0 },
5647
{ 6573120, "Canon", "PowerShot A610",0 },
5648
{ 9219600, "Canon", "PowerShot A620",0 },
5649
{ 7710960, "Canon", "PowerShot S3 IS",0 },
5650
{ 5939200, "OLYMPUS", "C770UZ" ,0 },
5651
{ 1581060, "NIKON", "E900" ,1 }, /* or E900s,E910 */
5652
{ 2465792, "NIKON", "E950" ,1 }, /* or E800,E700 */
5653
{ 2940928, "NIKON", "E2100" ,1 }, /* or E2500 */
5654
{ 4771840, "NIKON", "E990" ,1 }, /* or E995, Oly C3030Z */
5655
{ 4775936, "NIKON", "E3700" ,1 }, /* or Optio 33WR */
5656
{ 5869568, "NIKON", "E4300" ,1 }, /* or DiMAGE Z2 */
5657
{ 5865472, "NIKON", "E4500" ,1 },
5658
{ 7438336, "NIKON", "E5000" ,1 }, /* or E5700 */
5659
{ 1976352, "CASIO", "QV-2000UX" ,1 },
5660
{ 3217760, "CASIO", "QV-3*00EX" ,1 },
5661
{ 6218368, "CASIO", "QV-5700" ,1 },
5662
{ 7530816, "CASIO", "QV-R51" ,1 },
5663
{ 7684000, "CASIO", "QV-4000" ,1 },
5664
{ 4948608, "CASIO", "EX-S100" ,1 },
5665
{ 7542528, "CASIO", "EX-Z50" ,1 },
5666
{ 7753344, "CASIO", "EX-Z55" ,1 },
5667
{ 7426656, "CASIO", "EX-P505" ,1 },
5668
{ 9313536, "CASIO", "EX-P600" ,1 },
5669
{ 10979200, "CASIO", "EX-P700" ,1 },
5670
{ 3178560, "PENTAX", "Optio S" ,1 },
5671
{ 4841984, "PENTAX", "Optio S" ,1 },
5672
{ 6114240, "PENTAX", "Optio S4" ,1 }, /* or S4i, CASIO EX-Z4 */
5673
{ 12582980, "Sinar", "" ,0 },
5674
{ 33292868, "Sinar", "" ,0 },
5675
{ 44390468, "Sinar", "" ,0 } };
5676
static const char *corp[] =
5677
{ "Canon", "NIKON", "EPSON", "KODAK", "Kodak", "OLYMPUS", "PENTAX",
5678
"MINOLTA", "Minolta", "Konica", "CASIO", "Sinar", "Phase One",
5681
tiff_flip = flip = filters = -1; /* 0 is valid, so -1 is unknown */
5682
raw_height = raw_width = fuji_width = cr2_slice[0] = 0;
5683
maximum = height = width = top_margin = left_margin = 0;
5684
make[0] = model[0] = model2[0] = cdesc[0] = 0;
5685
iso_speed = shutter = aperture = focal_len = unique_id = 0;
5686
memset (white, 0, sizeof white);
5687
thumb_offset = thumb_length = thumb_width = thumb_height = 0;
5688
load_raw = thumb_load_raw = NULL;
5689
write_thumb = &CLASS jpeg_thumb;
5690
data_offset = meta_length = tiff_bps = tiff_compress = 0;
5691
kodak_cbpp = zero_after_ff = dng_version = fuji_secondary = 0;
5692
timestamp = shot_order = tiff_samples = black = is_foveon = 0;
5693
pixel_aspect = is_raw = raw_color = use_gamma = 1;
5694
tile_length = INT_MAX;
5695
for (i=0; i < 4; i++) {
5696
cam_mul[i] = i == 1;
5698
FORC3 rgb_cam[c][i] = c == i;
5702
for (i=0; i < 0x1000; i++) curve[i] = i;
5707
fseek (ifp, 0, SEEK_SET);
5708
fread (head, 1, 32, ifp);
5709
fseek (ifp, 0, SEEK_END);
5711
if ((cp = (char *) memmem (head, 32, "MMMM", 4)) ||
5712
(cp = (char *) memmem (head, 32, "IIII", 4))) {
5713
parse_phase_one (cp-head);
5714
if (cp-head) parse_tiff(0);
5715
} else if (order == 0x4949 || order == 0x4d4d) {
5716
if (!memcmp (head+6,"HEAPCCDR",8)) {
5718
parse_ciff (hlen, fsize - hlen);
5722
} else if (!memcmp (head,"\xff\xd8\xff\xe1",4) &&
5723
!memcmp (head+6,"Exif",4)) {
5724
fseek (ifp, 4, SEEK_SET);
5725
data_offset = 4 + get2();
5726
fseek (ifp, data_offset, SEEK_SET);
5727
if (fgetc(ifp) != 0xff)
5730
} else if (!memcmp (head,"BM",2) &&
5731
head[26] == 1 && head[28] == 16 && head[30] == 0) {
5732
data_offset = 0x1000;
5734
fseek (ifp, 38, SEEK_SET);
5735
if (get4() == 2834 && get4() == 2834 && get4() == 0 && get4() == 4096) {
5736
strcpy (model, "BMQ");
5740
} else if (!memcmp (head,"BR",2)) {
5741
strcpy (model, "RAW");
5743
strcpy (make, "Nucore");
5745
fseek (ifp, 10, SEEK_SET);
5746
data_offset += get4();
5747
raw_width = (get4(),get4());
5748
raw_height = get4();
5749
if (model[0] == 'B' && raw_width == 2597) {
5751
data_offset -= 0x1000;
5753
} else if (!memcmp (head+25,"ARECOYK",7)) {
5754
strcpy (make, "Contax");
5755
strcpy (model,"N Digital");
5756
fseek (ifp, 33, SEEK_SET);
5758
fseek (ifp, 60, SEEK_SET);
5759
FORC4 cam_mul[c ^ (c >> 1)] = get4();
5760
} else if (!strcmp (head, "PXN")) {
5761
strcpy (make, "Logitech");
5762
strcpy (model,"Fotoman Pixtura");
5763
} else if (!memcmp (head,"FUJIFILM",8)) {
5764
fseek (ifp, 84, SEEK_SET);
5765
thumb_offset = get4();
5766
thumb_length = get4();
5767
fseek (ifp, 92, SEEK_SET);
5768
parse_fuji (get4());
5769
if (thumb_offset > 120) {
5770
fseek (ifp, 120, SEEK_SET);
5771
fuji_secondary = (i = get4()) && 1;
5772
if (fuji_secondary && shot_select)
5775
fseek (ifp, 100, SEEK_SET);
5776
data_offset = get4();
5777
parse_tiff (thumb_offset+12);
5778
} else if (!memcmp (head,"RIFF",4)) {
5779
fseek (ifp, 0, SEEK_SET);
5781
} else if (!memcmp (head,"DSC-Image",9))
5783
else if (!memcmp (head,"PWAD",4))
5785
else if (!memcmp (head,"\0MRM",4))
5787
else if (!memcmp (head,"FOVb",4))
5790
for (i=0; i < sizeof table / sizeof *table; i++)
5791
if (fsize == table[i].fsize) {
5792
strcpy (make, table[i].make );
5793
strcpy (model, table[i].model);
5794
if (table[i].withjpeg)
5795
parse_external_jpeg();
5797
if (make[0] == 0) parse_smal (0, fsize);
5798
if (make[0] == 0) parse_jpeg (is_raw = 0);
5800
for (i=0; i < sizeof corp / sizeof *corp; i++)
5801
if (strstr (make, corp[i])) /* Simplify company names */
5802
strcpy (make, corp[i]);
5803
if (!strncmp (make,"KODAK",5))
5804
make[16] = model[16] = 0;
5805
cp = make + strlen(make); /* Remove trailing spaces */
5806
while (*--cp == ' ') *cp = 0;
5807
cp = model + strlen(model);
5808
while (*--cp == ' ') *cp = 0;
5809
i = strlen(make); /* Remove make from model */
5810
if (!strncmp (model, make, i) && model[i++] == ' ')
5811
memmove (model, model+i, 64-i);
5812
if (!strncmp (model,"Digital Camera ",15))
5813
strcpy (model, model+15);
5814
make[63] = model[63] = model2[63] = 0;
5815
if (!is_raw) goto notraw;
5817
if ((raw_height | raw_width) < 0)
5818
raw_height = raw_width = 0;
5819
if (!maximum) maximum = (1 << tiff_bps) - 1;
5820
if (!height) height = raw_height;
5821
if (!width) width = raw_width;
5823
width = height + fuji_width;
5827
if (!strcmp(model,"K10D")) { /* Camera DNGs are not cropped! */
5832
if (filters == UINT_MAX) filters = 0;
5834
colors = tiff_samples;
5835
if (tiff_compress == 1)
5836
load_raw = &CLASS adobe_dng_load_raw_nc;
5837
if (tiff_compress == 7)
5838
load_raw = &CLASS adobe_dng_load_raw_lj;
5839
FORC4 cam_mul[c] = pre_mul[c];
5843
/* We'll try to decode anything from Canon or Nikon. */
5845
if ((is_canon = !strcmp(make,"Canon"))) {
5846
load_raw = memcmp (head+6,"HEAPCCDR",8) ?
5847
&CLASS lossless_jpeg_load_raw : &CLASS canon_compressed_load_raw;
5850
if (!strcmp(make,"NIKON"))
5851
load_raw = nikon_is_compressed() ?
5852
&CLASS nikon_compressed_load_raw : &CLASS nikon_load_raw;
5853
if (!strncmp (make,"OLYMPUS",7))
5854
height += height & 1;
5856
/* Set parameters based on camera name (for non-DNG files). */
5859
if (height*2 < width) pixel_aspect = 0.5;
5860
if (height > width) pixel_aspect = 2;
5862
load_raw = &CLASS foveon_load_raw;
5864
} else if (!strcmp(model,"PowerShot 600")) {
5868
pixel_aspect = 607/628.0;
5870
filters = 0xe1e4e1e4;
5871
load_raw = &CLASS canon_600_load_raw;
5872
} else if (!strcmp(model,"PowerShot A5") ||
5873
!strcmp(model,"PowerShot A5 Zoom")) {
5877
pixel_aspect = 256/235.0;
5879
filters = 0x1e4e1e4e;
5880
load_raw = &CLASS canon_a5_load_raw;
5881
} else if (!strcmp(model,"PowerShot A50")) {
5886
filters = 0x1b4e4b1e;
5887
load_raw = &CLASS canon_a5_load_raw;
5888
} else if (!strcmp(model,"PowerShot Pro70")) {
5892
filters = 0x1e4b4e1b;
5893
load_raw = &CLASS canon_a5_load_raw;
5894
} else if (!strcmp(model,"PowerShot A610")) {
5901
load_raw = &CLASS canon_a5_load_raw;
5902
} else if (!strcmp(model,"PowerShot A620")) {
5909
load_raw = &CLASS canon_a5_load_raw;
5910
} else if (!strcmp(model,"PowerShot S3 IS")) {
5917
load_raw = &CLASS canon_a5_load_raw;
5918
} else if (!strcmp(model,"PowerShot Pro90 IS")) {
5921
filters = 0xb4b4b4b4;
5922
} else if (is_canon && raw_width == 2144) {
5927
if (!strcmp(model,"PowerShot G1")) {
5929
filters = 0xb4b4b4b4;
5931
} else if (is_canon && raw_width == 2224) {
5936
} else if (is_canon && raw_width == 2376) {
5941
} else if (is_canon && raw_width == 2672) {
5946
} else if (is_canon && raw_width == 3152) {
5951
if (unique_id == 0x80000170)
5952
adobe_coeff ("Canon","EOS 300D");
5954
} else if (is_canon && raw_width == 3160) {
5959
} else if (is_canon && raw_width == 3344) {
5964
} else if (!strcmp(model,"EOS D2000C")) {
5965
filters = 0x61616161;
5967
} else if (is_canon && raw_width == 3516) {
5970
if (unique_id == 0x80000189)
5971
adobe_coeff ("Canon","EOS 350D");
5973
} else if (is_canon && raw_width == 3596) {
5977
} else if (is_canon && raw_width == 3948) {
5981
if (unique_id == 0x80000236)
5982
adobe_coeff ("Canon","EOS 400D");
5984
} else if (is_canon && raw_width == 4476) {
5989
} else if (is_canon && raw_width == 5108) {
5994
height -= top_margin;
5995
width -= left_margin;
5996
} else if (!strcmp(model,"D1")) {
5997
cam_mul[0] *= 256/527.0;
5998
cam_mul[2] *= 256/317.0;
5999
} else if (!strcmp(model,"D1X")) {
6002
} else if (!strncmp(model,"D40",3)) {
6004
} else if (!strncmp(model,"D50",3) || !strncmp(model,"D70",3)) {
6007
} else if (!strcmp(model,"D80")) {
6010
} else if (!strcmp(model,"D100")) {
6011
if (tiff_compress == 34713 && load_raw == &CLASS nikon_load_raw)
6012
raw_width = (width += 3) + 3;
6014
} else if (!strcmp(model,"D200")) {
6018
filters = 0x94949494;
6019
} else if (!strncmp(model,"D2H",3)) {
6022
} else if (!strcmp(model,"D2X")) {
6025
} else if (fsize == 1581060) {
6029
load_raw = &CLASS nikon_e900_load_raw;
6032
filters = 0x1e1e1e1e;
6034
pre_mul[0] = 1.2085;
6035
pre_mul[1] = 1.0943;
6036
pre_mul[3] = 1.1103;
6037
} else if (fsize == 2465792) {
6041
load_raw = &CLASS nikon_e900_load_raw;
6044
filters = 0x4b4b4b4b;
6045
adobe_coeff ("NIKON","E950");
6046
} else if (fsize == 4771840) {
6050
filters = 0xe1e1e1e1;
6051
load_raw = &CLASS nikon_load_raw;
6052
if (!timestamp && nikon_e995())
6053
strcpy (model, "E995");
6054
if (strcmp(model,"E995")) {
6055
filters = 0xb4b4b4b4;
6061
} else if (!strcmp(model,"E2100")) {
6062
if (!timestamp && !nikon_e2100()) goto cp_e2500;
6065
load_raw = &CLASS nikon_e2100_load_raw;
6068
} else if (!strcmp(model,"E2500")) {
6070
strcpy (model, "E2500");
6074
filters = 0x4b4b4b4b;
6075
} else if (fsize == 4775936) {
6078
load_raw = &CLASS nikon_e2100_load_raw;
6081
if (!timestamp) nikon_3700();
6082
if (model[0] == 'E' && atoi(model+1) < 3700)
6083
filters = 0x49494949;
6084
if (!strcmp(model,"Optio 33WR")) {
6086
filters = 0x16161616;
6090
} else if (fsize == 5869568) {
6093
filters = 0x16161616;
6094
if (!timestamp && minolta_z2()) {
6095
strcpy (make, "Minolta");
6096
strcpy (model,"DiMAGE Z2");
6099
load_raw = &CLASS nikon_e2100_load_raw;
6100
} else if (!strcmp(model,"E4500")) {
6104
filters = 0xb4b4b4b4;
6105
} else if (fsize == 7438336) {
6109
filters = 0xb4b4b4b4;
6110
} else if (!strncmp(model,"R-D1",4)) {
6111
tiff_compress = 34713;
6112
load_raw = &CLASS nikon_load_raw;
6113
} else if (!strcmp(model,"FinePix S5100") ||
6114
!strcmp(model,"FinePix S5500")) {
6115
load_raw = &CLASS unpacked_load_raw;
6117
} else if (!strncmp(model,"FinePix",7)) {
6118
if (!strcmp(model+7,"S2Pro")) {
6119
strcpy (model+7," S2Pro");
6125
if (fuji_secondary && shot_select)
6127
top_margin = (raw_height - height)/2;
6128
left_margin = (raw_width - width )/2;
6130
data_offset += (shot_select > 0) * ( strcmp(model+7," S3Pro")
6131
? (raw_width *= 2) : raw_height*raw_width*2 );
6132
fuji_width = width >> !fuji_layout;
6133
width = (height >> fuji_layout) + fuji_width;
6134
raw_height = height;
6136
load_raw = &CLASS fuji_load_raw;
6137
if (!(fuji_width & 1)) filters = 0x49494949;
6138
} else if (!strcmp(model,"RD175")) {
6142
filters = 0x61616161;
6143
load_raw = &CLASS minolta_rd175_load_raw;
6144
} else if (!strcmp(model,"KD-400Z")) {
6149
} else if (!strcmp(model,"KD-510Z")) {
6151
} else if (!strcasecmp(make,"MINOLTA")) {
6152
load_raw = &CLASS unpacked_load_raw;
6154
if (!strncmp(model,"DiMAGE A",8)) {
6155
if (!strcmp(model,"DiMAGE A200"))
6156
filters = 0x49494949;
6157
load_raw = &CLASS packed_12_load_raw;
6158
maximum = model[8] == '1' ? 0xf8b : 0xfff;
6159
} else if (!strncmp(model,"ALPHA",5) ||
6160
!strncmp(model,"DYNAX",5) ||
6161
!strncmp(model,"MAXXUM",6)) {
6162
sprintf (model+20, "DYNAX %-10s", model+6+(model[0]=='M'));
6163
adobe_coeff (make, model+20);
6164
load_raw = &CLASS packed_12_load_raw;
6166
} else if (!strncmp(model,"DiMAGE G",8)) {
6167
if (model[8] == '4') {
6170
} else if (model[8] == '5') {
6175
} else if (model[8] == '6') {
6180
filters = 0x61616161;
6182
load_raw = &CLASS unpacked_load_raw;
6186
} else if (!strcmp(model,"*ist DS")) {
6188
} else if (!strcmp(model,"Optio S")) {
6189
if (fsize == 3178560) {
6192
load_raw = &CLASS eight_bit_load_raw;
6201
load_raw = &CLASS packed_12_load_raw;
6206
} else if (fsize == 6114240) {
6210
load_raw = &CLASS packed_12_load_raw;
6214
} else if (!strcmp(model,"STV680 VGA")) {
6217
load_raw = &CLASS eight_bit_load_raw;
6219
filters = 0x16161616;
6223
} else if (!strcmp(model,"KAI-0340")) {
6228
load_raw = &CLASS unpacked_load_raw;
6231
} else if (!strcmp(model,"531C")) {
6234
load_raw = &CLASS unpacked_load_raw;
6235
filters = 0x49494949;
6237
} else if (!strcmp(model,"F-145C")) {
6240
load_raw = &CLASS eight_bit_load_raw;
6241
} else if (!strcmp(model,"F-201C")) {
6244
load_raw = &CLASS eight_bit_load_raw;
6245
} else if (!strcmp(model,"F-510C")) {
6248
load_raw = fsize < 7500000 ?
6249
&CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
6251
} else if (!strcmp(model,"F-810C")) {
6254
load_raw = &CLASS unpacked_load_raw;
6256
} else if (!strcmp(model,"XCD-SX910CR")) {
6260
filters = 0x49494949;
6262
load_raw = fsize < 2000000 ?
6263
&CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
6264
} else if (!strcmp(model,"2010")) {
6268
filters = 0x16161616;
6271
load_raw = &CLASS unpacked_load_raw;
6272
} else if (!strcmp(model,"A782")) {
6275
filters = 0x61616161;
6276
load_raw = fsize < 10000000 ?
6277
&CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
6279
} else if (!strcmp(model,"3320AF")) {
6281
raw_width = width = 2048;
6282
filters = 0x61616161;
6283
load_raw = &CLASS unpacked_load_raw;
6287
fseek (ifp, 0x300000, SEEK_SET);
6288
if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
6289
height -= (top_margin = 16);
6290
width -= (left_margin = 28);
6292
strcpy (make, "ISG");
6295
} else if (!strcmp(make,"Imacon")) {
6296
sprintf (model, "Ixpress %d-Mp", height*width/1000000);
6297
load_raw = &CLASS imacon_full_load_raw;
6299
if (left_margin & 1) filters = 0x61616161;
6300
load_raw = &CLASS unpacked_load_raw;
6303
} else if (!strcmp(make,"Sinar")) {
6304
if (!memcmp(head,"8BPS",4)) {
6305
fseek (ifp, 14, SEEK_SET);
6308
filters = 0x61616161;
6311
if (!load_raw) load_raw = &CLASS unpacked_load_raw;
6313
} else if (!strcmp(make,"Leaf")) {
6315
if (tiff_samples > 1) filters = 0;
6316
if (tiff_samples > 1 || tile_length < raw_height)
6317
load_raw = &CLASS leaf_hdr_load_raw;
6318
if ((width | height) == 2048) {
6319
if (tiff_samples == 1) {
6321
strcpy (cdesc, "RBTG");
6322
strcpy (model, "CatchLight");
6323
top_margin = 8; left_margin = 18; height = 2032; width = 2016;
6325
strcpy (model, "DCB2");
6326
top_margin = 10; left_margin = 16; height = 2028; width = 2022;
6328
} else if (width+height == 3144+2060) {
6329
if (!model[0]) strcpy (model, "Cantare");
6330
if (width > height) {
6331
top_margin = 6; left_margin = 32; height = 2048; width = 3072;
6332
filters = 0x61616161;
6334
left_margin = 6; top_margin = 32; width = 2048; height = 3072;
6335
filters = 0x16161616;
6337
if (!cam_mul[0] || model[0] == 'V') filters = 0;
6338
} else if (width == 2116) {
6339
strcpy (model, "Valeo 6");
6340
height -= 2 * (top_margin = 30);
6341
width -= 2 * (left_margin = 55);
6342
filters = 0x49494949;
6343
} else if (width == 3171) {
6344
strcpy (model, "Valeo 6");
6345
height -= 2 * (top_margin = 24);
6346
width -= 2 * (left_margin = 24);
6347
filters = 0x16161616;
6349
} else if (!strcmp(make,"LEICA") || !strcmp(make,"Panasonic")) {
6351
load_raw = &CLASS unpacked_load_raw;
6352
if (width == 2568) {
6353
adobe_coeff ("Panasonic","DMC-LC1");
6354
} else if (width == 3177) {
6357
filters = 0x49494949;
6358
adobe_coeff ("Panasonic","DMC-L1");
6359
load_raw = &CLASS panasonic_load_raw;
6360
} else if (width == 3304) {
6363
adobe_coeff ("Panasonic","DMC-FZ30");
6364
load_raw = &CLASS panasonic_load_raw;
6365
} else if (width == 3690) {
6370
filters = 0x49494949;
6371
adobe_coeff ("Panasonic","DMC-FZ50");
6372
load_raw = &CLASS panasonic_load_raw;
6373
} else if (width == 3770) {
6378
adobe_coeff ("Panasonic","DMC-FZ50");
6379
load_raw = &CLASS panasonic_load_raw;
6380
} else if (width == 3880) {
6384
adobe_coeff ("Panasonic","DMC-LX1");
6385
load_raw = &CLASS panasonic_load_raw;
6386
} else if (width == 4290) {
6390
filters = 0x49494949;
6391
adobe_coeff ("Panasonic","DMC-LX2");
6392
} else if (width == 4330) {
6397
adobe_coeff ("Panasonic","DMC-LX2");
6399
} else if (!strcmp(model,"E-1") ||
6400
!strcmp(model,"E-400")) {
6401
filters = 0x61616161;
6403
} else if (!strcmp(model,"E-10") ||
6404
!strncmp(model,"E-20",4)) {
6407
} else if (!strcmp(model,"E-300") ||
6408
!strcmp(model,"E-500")) {
6411
if (load_raw == &CLASS unpacked_load_raw) black = 0;
6412
} else if (!strcmp(model,"E-330")) {
6414
} else if (!strcmp(model,"C770UZ")) {
6417
filters = 0x16161616;
6418
load_raw = &CLASS nikon_e2100_load_raw;
6419
} else if (!strcmp(make,"OLYMPUS")) {
6420
load_raw = &CLASS olympus_cseries_load_raw;
6421
if (!strcmp(model,"C5050Z") ||
6422
!strcmp(model,"C8080WZ"))
6423
filters = 0x16161616;
6424
if (!strncmp(model,"SP5",3))
6425
filters = 0x49494949;
6426
} else if (!strcmp(model,"N Digital")) {
6429
filters = 0x61616161;
6430
data_offset = 0x1a00;
6431
load_raw = &CLASS packed_12_load_raw;
6433
} else if (!strcmp(model,"DSC-F828")) {
6436
data_offset = 862144;
6437
load_raw = &CLASS sony_load_raw;
6438
filters = 0x9c9c9c9c;
6440
strcpy (cdesc, "RGBE");
6441
} else if (!strcmp(model,"DSC-V3")) {
6444
data_offset = 787392;
6445
load_raw = &CLASS sony_load_raw;
6446
} else if (!strcmp(make,"SONY") && raw_width == 3984) {
6447
adobe_coeff ("SONY","DSC-R1");
6450
} else if (!strcmp(model,"DSLR-A100")) {
6452
load_raw = &CLASS sony_arw_load_raw;
6454
} else if (!strncmp(model,"P850",4)) {
6456
} else if (!strcasecmp(make,"KODAK")) {
6457
if (filters == UINT_MAX) filters = 0x61616161;
6458
if (!strncmp(model,"NC2000",6)) {
6461
} else if (!strcmp(model,"EOSDCS3B")) {
6464
} else if (!strcmp(model,"EOSDCS1")) {
6467
} else if (!strcmp(model,"DCS420")) {
6470
} else if (!strcmp(model,"DCS460")) {
6473
} else if (!strcmp(model,"DCS460A")) {
6478
} else if (!strcmp(model,"DCS660M")) {
6482
} else if (!strcmp(model,"DCS760M")) {
6486
if (load_raw == &CLASS eight_bit_load_raw)
6487
load_raw = &CLASS kodak_easy_load_raw;
6488
if (strstr(model,"DC25")) {
6489
strcpy (model, "DC25");
6490
data_offset = 15424;
6492
if (!strncmp(model,"DC2",3)) {
6494
if (fsize < 100000) {
6495
raw_width = 256; width = 249;
6497
raw_width = 512; width = 501;
6499
data_offset += raw_width + 1;
6501
filters = 0x8d8d8d8d;
6506
load_raw = &CLASS kodak_easy_load_raw;
6507
} else if (!strcmp(model,"40")) {
6508
strcpy (model, "DC40");
6512
load_raw = &CLASS kodak_radc_load_raw;
6513
} else if (strstr(model,"DC50")) {
6514
strcpy (model, "DC50");
6517
data_offset = 19712;
6518
load_raw = &CLASS kodak_radc_load_raw;
6519
} else if (strstr(model,"DC120")) {
6520
strcpy (model, "DC120");
6523
pixel_aspect = height/0.75/width;
6524
load_raw = tiff_compress == 7 ?
6525
&CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
6526
} else if (!strcmp(model,"DCS200")) {
6529
thumb_offset = 6144;
6531
write_thumb = &CLASS layer_thumb;
6534
data_offset = 79872;
6535
load_raw = &CLASS eight_bit_load_raw;
6538
} else if (!strcmp(model,"Fotoman Pixtura")) {
6542
load_raw = &CLASS kodak_radc_load_raw;
6543
filters = 0x61616161;
6545
} else if (!strcmp(make,"Rollei") && !load_raw) {
6546
switch (raw_width) {
6559
filters = 0x16161616;
6560
load_raw = &CLASS rollei_load_raw;
6563
} else if (!strcmp(model,"PC-CAM 600")) {
6565
data_offset = width = 1024;
6566
filters = 0x49494949;
6567
load_raw = &CLASS eight_bit_load_raw;
6570
} else if (!strcmp(model,"QV-2000UX")) {
6573
data_offset = width * 2;
6574
load_raw = &CLASS eight_bit_load_raw;
6575
} else if (fsize == 3217760) {
6579
load_raw = &CLASS eight_bit_load_raw;
6580
} else if (!strcmp(model,"QV-4000")) {
6583
load_raw = &CLASS unpacked_load_raw;
6585
} else if (!strcmp(model,"QV-5700")) {
6588
load_raw = &CLASS casio_qv5700_load_raw;
6589
} else if (!strcmp(model,"QV-R51")) {
6593
load_raw = &CLASS packed_12_load_raw;
6596
} else if (!strcmp(model,"EX-S100")) {
6600
load_raw = &CLASS packed_12_load_raw;
6603
} else if (!strcmp(model,"EX-Z50")) {
6607
load_raw = &CLASS packed_12_load_raw;
6610
} else if (!strcmp(model,"EX-Z55")) {
6614
load_raw = &CLASS packed_12_load_raw;
6617
} else if (!strcmp(model,"EX-P505")) {
6621
load_raw = &CLASS packed_12_load_raw;
6624
} else if (fsize == 9313536) { /* EX-P600 or QV-R61 */
6628
load_raw = &CLASS packed_12_load_raw;
6631
} else if (!strcmp(model,"EX-P700")) {
6635
load_raw = &CLASS packed_12_load_raw;
6638
} else if (!strcmp(make,"Nucore")) {
6639
filters = 0x61616161;
6640
load_raw = &CLASS unpacked_load_raw;
6641
if (width == 2598) {
6642
filters = 0x16161616;
6643
load_raw = &CLASS nucore_load_raw;
6648
sprintf (model, "%dx%d", width, height);
6649
if (filters == UINT_MAX) filters = 0x94949494;
6650
if (raw_color) adobe_coeff (make, model);
6651
if (thumb_offset && !thumb_height) {
6652
fseek (ifp, thumb_offset, SEEK_SET);
6653
if (ljpeg_start (&jh, 1)) {
6654
thumb_width = jh.wide;
6655
thumb_height = jh.high;
6659
if (!load_raw || !height) is_raw = 0;
6661
if (load_raw == kodak_jpeg_load_raw) {
6662
fprintf (stderr,_("%s: You must link dcraw with libjpeg!!\n"), ifname);
6667
strcpy (cdesc, colors == 3 ? "RGB":"GMCY");
6668
if (!raw_height) raw_height = height;
6669
if (!raw_width ) raw_width = width;
6670
if (filters && colors == 3)
6671
for (i=0; i < 32; i+=4) {
6672
if ((filters >> i & 15) == 9)
6674
if ((filters >> i & 15) == 6)
6678
if (flip == -1) flip = tiff_flip;
6679
if (flip == -1) flip = 0;
6683
void CLASS apply_profile (char *input, char *output)
6686
cmsHPROFILE hInProfile=NULL, hOutProfile=NULL;
6687
cmsHTRANSFORM hTransform;
6691
cmsErrorAction (LCMS_ERROR_SHOW);
6692
if (strcmp (input, "embed"))
6693
hInProfile = cmsOpenProfileFromFile (input, "r");
6694
else if (profile_length) {
6695
prof = (char *) malloc (profile_length);
6696
merror (prof, "apply_profile()");
6697
fseek (ifp, profile_offset, SEEK_SET);
6698
fread (prof, 1, profile_length, ifp);
6699
hInProfile = cmsOpenProfileFromMem (prof, profile_length);
6702
fprintf (stderr,_("%s has no embedded profile.\n"), ifname);
6703
if (!hInProfile) return;
6705
hOutProfile = cmsCreate_sRGBProfile();
6706
else if ((fp = fopen (output, "rb"))) {
6707
fread (&size, 4, 1, fp);
6708
fseek (fp, 0, SEEK_SET);
6709
oprof = (unsigned *) malloc (size = ntohl(size));
6710
merror (oprof, "apply_profile()");
6711
fread (oprof, 1, size, fp);
6713
if (!(hOutProfile = cmsOpenProfileFromMem (oprof, size))) {
6718
fprintf (stderr,_("Cannot open file %s!\n"), output);
6719
if (!hOutProfile) goto quit;
6721
fprintf (stderr,_("Applying color profile...\n"));
6722
hTransform = cmsCreateTransform (hInProfile, TYPE_RGBA_16,
6723
hOutProfile, TYPE_RGBA_16, INTENT_PERCEPTUAL, 0);
6724
cmsDoTransform (hTransform, image, image, width*height);
6725
raw_color = 1; /* Don't use rgb_cam with a profile */
6726
cmsDeleteTransform (hTransform);
6727
cmsCloseProfile (hOutProfile);
6729
cmsCloseProfile (hInProfile);
6733
void CLASS convert_to_rgb()
6735
int mix_green, row, col, c, i, j, k;
6737
float out[3], out_cam[3][4];
6738
double num, inverse[3][3];
6739
static const double xyzd50_srgb[3][3] =
6740
{ { 0.436083, 0.385083, 0.143055 },
6741
{ 0.222507, 0.716888, 0.060608 },
6742
{ 0.013930, 0.097097, 0.714022 } };
6743
static const double rgb_rgb[3][3] =
6744
{ { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };
6745
static const double adobe_rgb[3][3] =
6746
{ { 0.715146, 0.284856, 0.000000 },
6747
{ 0.000000, 1.000000, 0.000000 },
6748
{ 0.000000, 0.041166, 0.958839 } };
6749
static const double wide_rgb[3][3] =
6750
{ { 0.593087, 0.404710, 0.002206 },
6751
{ 0.095413, 0.843149, 0.061439 },
6752
{ 0.011621, 0.069091, 0.919288 } };
6753
static const double prophoto_rgb[3][3] =
6754
{ { 0.529317, 0.330092, 0.140588 },
6755
{ 0.098368, 0.873465, 0.028169 },
6756
{ 0.016879, 0.117663, 0.865457 } };
6757
static const double (*out_rgb[])[3] =
6758
{ rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb };
6759
static const char *name[] =
6760
{ "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" };
6761
static const unsigned phead[] =
6762
{ 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0,
6763
0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d };
6765
{ 10, 0x63707274, 0, 36, /* cprt */
6766
0x64657363, 0, 40, /* desc */
6767
0x77747074, 0, 20, /* wtpt */
6768
0x626b7074, 0, 20, /* bkpt */
6769
0x72545243, 0, 14, /* rTRC */
6770
0x67545243, 0, 14, /* gTRC */
6771
0x62545243, 0, 14, /* bTRC */
6772
0x7258595a, 0, 20, /* rXYZ */
6773
0x6758595a, 0, 20, /* gXYZ */
6774
0x6258595a, 0, 20 }; /* bXYZ */
6775
static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc };
6776
unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 };
6778
memcpy (out_cam, rgb_cam, sizeof out_cam);
6779
raw_color |= colors == 1 || document_mode ||
6780
output_color < 1 || output_color > 5;
6782
oprof = (unsigned *) calloc (phead[0], 1);
6783
merror (oprof, "convert_to_rgb()");
6784
memcpy (oprof, phead, sizeof phead);
6785
if (output_color == 5) oprof[4] = oprof[5];
6786
oprof[0] = 132 + 12*pbody[0];
6787
for (i=0; i < pbody[0]; i++) {
6788
oprof[oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
6789
pbody[i*3+2] = oprof[0];
6790
oprof[0] += (pbody[i*3+3] + 3) & -4;
6792
memcpy (oprof+32, pbody, sizeof pbody);
6793
oprof[pbody[5]/4+2] = strlen(name[output_color-1]) + 1;
6794
memcpy ((char *)oprof+pbody[8]+8, pwhite, sizeof pwhite);
6795
if (output_bps == 8)
6797
pcurve[3] = 0x2330000;
6799
pcurve[3] = 0x1f00000;
6801
for (i=4; i < 7; i++)
6802
memcpy ((char *)oprof+pbody[i*3+2], pcurve, sizeof pcurve);
6803
pseudoinverse ((double (*)[3]) out_rgb[output_color-1], inverse, 3);
6804
for (i=0; i < 3; i++)
6805
for (j=0; j < 3; j++) {
6806
for (num = k=0; k < 3; k++)
6807
num += xyzd50_srgb[i][k] * inverse[j][k];
6808
oprof[pbody[j*3+23]/4+i+2] = num * 0x10000 + 0.5;
6810
for (i=0; i < phead[0]/4; i++)
6811
oprof[i] = htonl(oprof[i]);
6812
strcpy ((char *)oprof+pbody[2]+8, "auto-generated by dcraw");
6813
strcpy ((char *)oprof+pbody[5]+12, name[output_color-1]);
6814
for (i=0; i < 3; i++)
6815
for (j=0; j < colors; j++)
6816
for (out_cam[i][j] = k=0; k < 3; k++)
6817
out_cam[i][j] += out_rgb[output_color-1][i][k] * rgb_cam[k][j];
6820
fprintf (stderr, raw_color ? _("Building histograms...\n") :
6821
_("Converting to %s colorspace...\n"), name[output_color-1]);
6823
mix_green = rgb_cam[1][1] == rgb_cam[1][3];
6824
memset (histogram, 0, sizeof histogram);
6825
for (img=image[0], row=0; row < height; row++)
6826
for (col=0; col < width; col++, img+=4) {
6828
out[0] = out[1] = out[2] = 0;
6830
out[0] += out_cam[0][c] * img[c];
6831
out[1] += out_cam[1][c] * img[c];
6832
out[2] += out_cam[2][c] * img[c];
6834
FORC3 img[c] = CLIP((int) out[c]);
6836
else if (document_mode)
6837
img[0] = img[FC(row,col)];
6839
img[1] = (img[1] + img[3]) >> 1;
6840
FORCC histogram[c][img[c] >> 3]++;
6842
if (colors == 4 && (output_color || mix_green)) colors = 3;
6843
if (document_mode && filters) colors = 1;
6846
void CLASS fuji_rotate()
6848
int i, wide, high, row, col;
6852
ushort (*img)[4], (*pix)[4];
6854
if (!fuji_width) return;
6856
fprintf (stderr,_("Rotating image 45 degrees...\n"));
6857
fuji_width = (fuji_width - 1 + shrink) >> shrink;
6859
wide = fuji_width / step;
6860
high = (height - fuji_width) / step;
6861
img = (ushort (*)[4]) calloc (wide*high, sizeof *img);
6862
merror (img, "fuji_rotate()");
6864
for (row=0; row < high; row++)
6865
for (col=0; col < wide; col++) {
6866
ur = r = fuji_width + (row-col)*step;
6867
uc = c = (row+col)*step;
6868
if (ur > height-2 || uc > width-2) continue;
6871
pix = image + ur*width + uc;
6872
for (i=0; i < colors; i++)
6873
img[row*wide+col][i] =
6874
(pix[ 0][i]*(1-fc) + pix[ 1][i]*fc) * (1-fr) +
6875
(pix[width][i]*(1-fc) + pix[width+1][i]*fc) * fr;
6884
void CLASS stretch()
6886
int newdim, row, col, c;
6888
ushort (*img)[4], *pix0, *pix1;
6890
if (pixel_aspect == 1) return;
6891
if (verbose) fprintf (stderr,_("Stretching the image...\n"));
6892
if (pixel_aspect < 1) {
6893
newdim = height / pixel_aspect + 0.5;
6894
img = (ushort (*)[4]) calloc (width*newdim, sizeof *img);
6895
merror (img, "stretch()");
6896
for (rc=row=0; row < newdim; row++, rc+=pixel_aspect) {
6897
frac = rc - (c = rc);
6898
pix0 = pix1 = image[c*width];
6899
if (c+1 < height) pix1 += width*4;
6900
for (col=0; col < width; col++, pix0+=4, pix1+=4)
6901
FORCC img[row*width+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
6905
newdim = width * pixel_aspect + 0.5;
6906
img = (ushort (*)[4]) calloc (height*newdim, sizeof *img);
6907
merror (img, "stretch()");
6908
for (rc=col=0; col < newdim; col++, rc+=1/pixel_aspect) {
6909
frac = rc - (c = rc);
6910
pix0 = pix1 = image[c];
6911
if (c+1 < width) pix1 += 4;
6912
for (row=0; row < height; row++, pix0+=width*4, pix1+=width*4)
6913
FORCC img[row*newdim+col][c] = pix0[c]*(1-frac) + pix1[c]*frac + 0.5;
6921
int CLASS flip_index (int row, int col)
6923
if (flip & 4) SWAP(row,col);
6924
if (flip & 2) row = iheight - 1 - row;
6925
if (flip & 1) col = iwidth - 1 - col;
6926
return row * iwidth + col;
6929
void CLASS gamma_lut (uchar lut[0x10000])
6931
int perc, c, val, total, i;
6934
perc = width * height * 0.01; /* 99th percentile white point */
6935
if (fuji_width) perc /= 2;
6936
if (highlight) perc = 0;
6938
for (val=0x2000, total=0; --val > 32; )
6939
if ((total += histogram[c][val]) > perc) break;
6940
if (white < val) white = val;
6942
white *= 8 / bright;
6943
for (i=0; i < 0x10000; i++) {
6945
val = 256 * ( !use_gamma ? r :
6947
r <= 0.00304 ? r*12.92 : pow(r,2.5/6)*1.055-0.055 );
6949
r <= 0.018 ? r*4.5 : pow(r,0.45)*1.099-0.099 );
6951
if (val > 255) val = 255;
6959
union { short s0, s1; int i0; } val;
6963
ushort order, magic;
6966
struct tiff_tag tag[15];
6969
struct tiff_tag exif[4];
6972
char make[64], model[64], soft[32], date[20];
6975
void CLASS tiff_set (ushort *ntag,
6976
ushort tag, ushort type, int count, int val)
6978
struct tiff_tag *tt;
6980
tt = (struct tiff_tag *)(ntag+1) + (*ntag)++;
6984
if (type == 3 && count == 1)
6986
else tt->val.i0 = val;
6989
#define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
6991
void CLASS tiff_head (struct tiff_hdr *th, int full)
6996
memset (th, 0, sizeof *th);
6997
th->order = htonl(0x4d4d4949) >> 16;
7001
tiff_set (&th->ntag, 256, 4, 1, width);
7002
tiff_set (&th->ntag, 257, 4, 1, height);
7003
tiff_set (&th->ntag, 258, 3, colors, output_bps);
7005
th->tag[th->ntag-1].val.i0 = TOFF(th->bps);
7006
FORC4 th->bps[c] = output_bps;
7007
tiff_set (&th->ntag, 259, 3, 1, 1);
7008
tiff_set (&th->ntag, 262, 3, 1, 1 + (colors > 1));
7010
tiff_set (&th->ntag, 271, 2, 64, TOFF(th->make));
7011
tiff_set (&th->ntag, 272, 2, 64, TOFF(th->model));
7013
if (oprof) psize = ntohl(oprof[0]);
7014
tiff_set (&th->ntag, 273, 4, 1, sizeof *th + psize);
7015
tiff_set (&th->ntag, 277, 3, 1, colors);
7016
tiff_set (&th->ntag, 278, 4, 1, height);
7017
tiff_set (&th->ntag, 279, 4, 1, height*width*colors*output_bps/8);
7019
tiff_set (&th->ntag, 274, 3, 1, "12435867"[flip]-'0');
7020
tiff_set (&th->ntag, 305, 2, 32, TOFF(th->soft));
7021
tiff_set (&th->ntag, 306, 2, 20, TOFF(th->date));
7022
tiff_set (&th->ntag, 34665, 4, 1, TOFF(th->nexif));
7023
if (psize) tiff_set (&th->ntag, 34675, 7, psize, sizeof *th);
7024
tiff_set (&th->nexif, 33434, 5, 1, TOFF(th->rat[0]));
7025
tiff_set (&th->nexif, 33437, 5, 1, TOFF(th->rat[2]));
7026
tiff_set (&th->nexif, 34855, 3, 1, iso_speed);
7027
tiff_set (&th->nexif, 37386, 5, 1, TOFF(th->rat[4]));
7028
for (c=0; c < 6; c++) th->rat[c] = 1000000;
7029
th->rat[0] *= shutter;
7030
th->rat[2] *= aperture;
7031
th->rat[4] *= focal_len;
7032
strncpy (th->make, make, 64);
7033
strncpy (th->model, model, 64);
7034
strcpy (th->soft, "dcraw v"DCRAWVERSION);
7035
t = gmtime (×tamp);
7036
sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
7037
t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
7040
void CLASS jpeg_thumb (FILE *tfp)
7046
thumb = (char *) malloc (thumb_length);
7047
merror (thumb, "jpeg_thumb()");
7048
fread (thumb, 1, thumb_length, ifp);
7051
if (strcmp (thumb+6, "Exif")) {
7052
memcpy (exif, "\xff\xe1 Exif\0\0", 10);
7053
exif[1] = htons (8 + sizeof th);
7054
fwrite (exif, 1, sizeof exif, tfp);
7056
fwrite (&th, 1, sizeof th, tfp);
7058
fwrite (thumb+2, 1, thumb_length-2, tfp);
7062
// void CLASS write_ppm_tiff (FILE *ofp)
7064
// struct tiff_hdr th;
7065
// uchar *ppm, lut[0x10000];
7067
// int c, row, col, soff, rstep, cstep;
7069
// iheight = height;
7071
// if (flip & 4) SWAP(height,width);
7072
// ppm = (uchar *) calloc (width, colors*output_bps/8);
7073
// ppm2 = (ushort *) ppm;
7074
// merror (ppm, "write_ppm_tiff()");
7075
// if (output_tiff) {
7076
// tiff_head (&th, 1);
7077
// fwrite (&th, sizeof th, 1, ofp);
7079
// fwrite (oprof, ntohl(oprof[0]), 1, ofp);
7080
// } else if (colors > 3)
7082
// "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
7083
// width, height, colors, (1 << output_bps)-1, cdesc);
7085
// fprintf (ofp, "P%d\n%d %d\n%d\n",
7086
// colors/2+5, width, height, (1 << output_bps)-1);
7088
// if (output_bps == 8) gamma_lut (lut);
7089
// soff = flip_index (0, 0);
7090
// cstep = flip_index (0, 1) - soff;
7091
// rstep = flip_index (1, 0) - flip_index (0, width);
7092
// for (row=0; row < height; row++, soff += rstep) {
7093
// for (col=0; col < width; col++, soff += cstep)
7094
// if (output_bps == 8)
7095
// FORCC ppm [col*colors+c] = lut[image[soff][c]];
7096
// else FORCC ppm2[col*colors+c] = image[soff][c];
7097
// if (output_bps == 16 && !output_tiff && th.order == 0x4949)
7098
// swab (ppm2, ppm2, width*colors*2);
7099
// fwrite (ppm, colors*output_bps/8, width, ofp);
7104
// int CLASS PREVIOUSmain (int argc, char **argv)
7106
// int arg, status=0, user_flip=-1, user_black=-1, user_qual=-1;
7107
// int timestamp_only=0, thumbnail_only=0, identify_only=0, write_to_stdout=0;
7108
// int half_size=0, use_fuji_rotate=1, quality, i, c;
7109
// char opt, *ofname, *sp, *cp, *dark_frame = NULL;
7110
// const char *write_ext;
7111
// struct utimbuf ut;
7112
// FILE *ofp = stdout;
7114
// char *cam_profile = NULL, *out_profile = NULL;
7117
// #ifndef LOCALTIME
7118
// putenv ("TZ=UTC");
7121
// setlocale (LC_CTYPE, "");
7122
// setlocale (LC_MESSAGES, "");
7123
// bindtextdomain ("dcraw", LOCALEDIR);
7124
// textdomain ("dcraw");
7128
// printf(_("\nRaw photo decoder \"dcraw\" v%s"), DCRAWVERSION);
7129
// printf(_("\nby Dave Coffin, dcoffin a cybercom o net\n"));
7130
// printf(_("\nUsage: %s [OPTION]... [FILE]...\n\n"), argv[0]);
7131
// puts(_("-v Print verbose messages"));
7132
// puts(_("-c Write image data to standard output"));
7133
// puts(_("-e Extract embedded thumbnail image"));
7134
// puts(_("-i Identify files without decoding them"));
7135
// puts(_("-i -v Identify files and show metadata"));
7136
// puts(_("-z Change file dates to camera timestamp"));
7137
// puts(_("-a Use automatic white balance"));
7138
// puts(_("-w Use camera white balance, if possible"));
7139
// puts(_("-r <4 numbers> Set custom white balance"));
7140
// puts(_("-b <num> Adjust brightness (default = 1.0)"));
7141
// puts(_("-k <num> Set black point"));
7142
// puts(_("-K <file> Subtract dark frame (16-bit raw PGM)"));
7143
// puts(_("-H [0-9] Highlight mode (0=clip, 1=no clip, 2+=recover)"));
7144
// puts(_("-t [0-7] Flip image (0=none, 3=180, 5=90CCW, 6=90CW)"));
7145
// puts(_("-o [0-5] Output colorspace (raw,sRGB,Adobe,Wide,ProPhoto,XYZ)"));
7147
// puts(_("-o <file> Apply output ICC profile from file"));
7148
// puts(_("-p <file> Apply camera ICC profile from file or \"embed\""));
7150
// puts(_("-d Document mode (no color, no interpolation)"));
7151
// puts(_("-D Document mode without scaling (totally raw)"));
7152
// puts(_("-j Don't stretch or rotate raw pixels"));
7153
// puts(_("-q [0-3] Set the interpolation quality"));
7154
// puts(_("-h Half-size color image (twice as fast as \"-q 0\")"));
7155
// puts(_("-f Interpolate RGGB as four colors"));
7156
// puts(_("-B <domain> <range> Apply bilateral filter to smooth noise"));
7157
// puts(_("-s [0-99] Select a different raw image from the same file"));
7158
// puts(_("-4 Write 16-bit linear instead of 8-bit with gamma"));
7159
// puts(_("-T Write TIFF instead of PPM"));
7164
// for (arg=1; argv[arg][0] == '-'; ) {
7165
// opt = argv[arg++][1];
7166
// if ((cp = strchr (sp="BbrktqsH", opt)))
7167
// for (i=0; i < "21411111"[cp-sp]-'0'; i++)
7168
// if (!isdigit(argv[arg+i][0])) {
7169
// fprintf (stderr,_("Non-numeric argument to \"-%c\"\n"), opt);
7173
// case 'B': sigma_d = atof(argv[arg++]);
7174
// sigma_r = atof(argv[arg++]); break;
7175
// case 'b': bright = atof(argv[arg++]); break;
7177
// FORC4 user_mul[c] = atof(argv[arg++]); break;
7178
// case 'k': user_black = atoi(argv[arg++]); break;
7179
// case 't': user_flip = atoi(argv[arg++]); break;
7180
// case 'q': user_qual = atoi(argv[arg++]); break;
7181
// case 's': shot_select = atoi(argv[arg++]); break;
7182
// case 'H': highlight = atoi(argv[arg++]); break;
7184
// if (isdigit(argv[arg][0]) && !argv[arg][1])
7185
// output_color = atoi(argv[arg++]);
7187
// else out_profile = argv[arg++];
7189
// case 'p': cam_profile = argv[arg++];
7192
// case 'K': dark_frame = argv[arg++];
7194
// case 'z': timestamp_only = 1; break;
7195
// case 'e': thumbnail_only = 1; break;
7196
// case 'i': identify_only = 1; break;
7197
// case 'c': write_to_stdout = 1; break;
7198
// case 'v': verbose = 1; break;
7199
// case 'h': half_size = 1; /* "-h" implies "-f" */
7200
// case 'f': four_color_rgb = 1; break;
7201
// case 'a': use_auto_wb = 1; break;
7202
// case 'w': use_camera_wb = 1; break;
7204
// case 'd': document_mode = 1 + (opt == 'D');
7205
// case 'j': use_fuji_rotate = 0; break;
7206
// case 'm': output_color = 0; break;
7207
// case 'T': output_tiff = 1; break;
7208
// case '4': output_bps = 16; break;
7210
// fprintf (stderr,_("Unknown option \"-%c\".\n"), opt);
7214
// if (arg == argc) {
7215
// fprintf (stderr,_("No files to process.\n"));
7218
// if (write_to_stdout) {
7220
// fprintf (stderr,_("Will not write an image to the terminal!\n"));
7223
// #if defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__)
7224
// if (setmode(1,O_BINARY) < 0) {
7225
// perror("setmode()");
7230
// for ( ; arg < argc; arg++) {
7234
// if (setjmp (failure)) {
7235
// if (fileno(ifp) > 2) fclose(ifp);
7236
// if (fileno(ofp) > 2) fclose(ofp);
7237
// if (image) free (image);
7241
// ifname = argv[arg];
7242
// if (!(ifp = fopen (ifname, "rb"))) {
7246
// status = (identify(),!is_raw);
7247
// if (user_flip >= 0)
7248
// flip = user_flip;
7249
// switch ((flip+3600) % 360) {
7250
// case 270: flip = 5; break;
7251
// case 180: flip = 3; break;
7252
// case 90: flip = 6;
7254
// if (timestamp_only) {
7255
// if ((status = !timestamp))
7256
// fprintf (stderr,_("%s has no timestamp.\n"), ifname);
7257
// else if (identify_only)
7258
// printf ("%10ld%10d %s\n", (long) timestamp, shot_order, ifname);
7261
// fprintf (stderr,_("%s time set to %d.\n"), ifname, (int) timestamp);
7262
// ut.actime = ut.modtime = timestamp;
7263
// utime (ifname, &ut);
7267
// write_fun = &CLASS write_ppm_tiff;
7268
// if (thumbnail_only) {
7269
// if ((status = !thumb_offset)) {
7270
// fprintf (stderr,_("%s has no thumbnail.\n"), ifname);
7272
// } else if (thumb_load_raw) {
7273
// load_raw = thumb_load_raw;
7274
// data_offset = thumb_offset;
7275
// height = thumb_height;
7276
// width = thumb_width;
7279
// fseek (ifp, thumb_offset, SEEK_SET);
7280
// write_fun = write_thumb;
7284
// if (load_raw == &CLASS kodak_ycbcr_load_raw) {
7285
// height += height & 1;
7286
// width += width & 1;
7288
// if (identify_only && verbose && make[0]) {
7289
// printf (_("\nFilename: %s\n"), ifname);
7290
// printf (_("Timestamp: %s"), ctime(×tamp));
7291
// printf (_("Camera: %s %s\n"), make, model);
7292
// if (dng_version) {
7293
// printf (_("DNG Version: "));
7294
// for (i=24; i >= 0; i -= 8)
7295
// printf ("%d%c", dng_version >> i & 255, i ? '.':'\n');
7297
// printf (_("ISO speed: %d\n"), (int) iso_speed);
7298
// printf (_("Shutter: "));
7299
// if (shutter > 0 && shutter < 1)
7300
// shutter = (printf ("1/"), 1 / shutter);
7301
// printf (_("%0.1f sec\n"), shutter);
7302
// printf (_("Aperture: f/%0.1f\n"), aperture);
7303
// printf (_("Focal length: %0.1f mm\n"), focal_len);
7304
// printf (_("Secondary pixels: %s\n"), fuji_secondary ? _("yes"):_("no"));
7305
// printf (_("Embedded ICC profile: %s\n"), profile_length ? _("yes"):_("no"));
7306
// printf (_("Decodable with dcraw: %s\n"), is_raw ? _("yes"):_("no"));
7307
// if (pixel_aspect != 1)
7308
// printf (_("Pixel Aspect Ratio: %0.6f\n"), pixel_aspect);
7309
// if (thumb_offset)
7310
// printf (_("Thumb size: %4d x %d\n"), thumb_width, thumb_height);
7311
// printf (_("Full size: %4d x %d\n"), raw_width, raw_height);
7312
// } else if (!is_raw)
7313
// fprintf (stderr,_("Cannot decode file %s\n"), ifname);
7314
// if (!is_raw) goto next;
7315
// shrink = half_size && filters;
7316
// iheight = (height + shrink) >> shrink;
7317
// iwidth = (width + shrink) >> shrink;
7318
// if (identify_only) {
7320
// if (use_fuji_rotate) {
7321
// if (fuji_width) {
7322
// fuji_width = (fuji_width - 1 + shrink) >> shrink;
7323
// iwidth = fuji_width / sqrt(0.5);
7324
// iheight = (iheight - fuji_width) / sqrt(0.5);
7326
// if (pixel_aspect < 1) iheight = iheight / pixel_aspect + 0.5;
7327
// if (pixel_aspect > 1) iwidth = iwidth * pixel_aspect + 0.5;
7331
// SWAP(iheight,iwidth);
7332
// printf (_("Image size: %4d x %d\n"), width, height);
7333
// printf (_("Output size: %4d x %d\n"), iwidth, iheight);
7334
// printf (_("Raw colors: %d"), colors);
7336
// printf (_("\nFilter pattern: "));
7337
// if (!cdesc[3]) cdesc[3] = 'G';
7338
// for (i=0; i < 16; i++)
7339
// putchar (cdesc[fc(i >> 1,i & 1)]);
7341
// printf (_("\nDaylight multipliers:"));
7342
// FORCC printf (" %f", pre_mul[c]);
7343
// if (cam_mul[0] > 0) {
7344
// printf (_("\nCamera multipliers:"));
7345
// FORC4 printf (" %f", cam_mul[c]);
7349
// printf (_("%s is a %s %s image.\n"), ifname, make, model);
7354
// image = (ushort (*)[4])
7355
// calloc (iheight*iwidth*sizeof *image + meta_length, 1);
7356
// merror (image, "main()");
7357
// meta_data = (char *) (image + iheight*iwidth);
7360
// _("Loading %s %s image from %s ...\n"), make, model, ifname);
7361
// fseek (ifp, data_offset, SEEK_SET);
7364
// if (dark_frame) subtract (dark_frame);
7365
// height = iheight;
7367
// quality = 2 + !fuji_width;
7368
// if (user_qual >= 0) quality = user_qual;
7369
// if (user_black >= 0) black = user_black;
7370
// #ifdef COLORCHECK
7373
// if (is_foveon && !document_mode) foveon_interpolate();
7374
// if (!is_foveon && document_mode < 2) scale_colors();
7375
// if (shrink) filters = 0;
7376
// cam_to_cielab (NULL,NULL);
7377
// if (filters && !document_mode) {
7378
// if (quality == 0)
7379
// lin_interpolate();
7380
// else if (quality < 3 || colors > 3)
7381
// vng_interpolate();
7382
// else ahd_interpolate();
7384
// if (sigma_d > 0 && sigma_r > 0) bilateral_filter();
7385
// if (!is_foveon && highlight > 1) recover_highlights();
7386
// if (use_fuji_rotate) fuji_rotate();
7388
// if (cam_profile) apply_profile (cam_profile, out_profile);
7390
// convert_to_rgb();
7391
// if (use_fuji_rotate) stretch();
7393
// if (write_fun == &CLASS jpeg_thumb)
7394
// write_ext = ".jpg";
7395
// else if (output_tiff && write_fun == &CLASS write_ppm_tiff)
7396
// write_ext = ".tiff";
7398
// write_ext = ".pgm\0.ppm\0.ppm\0.pam" + colors*5-5;
7399
// ofname = (char *) malloc (strlen(ifname) + 16);
7400
// merror (ofname, "main()");
7401
// if (write_to_stdout)
7402
// strcpy (ofname,_("standard output"));
7404
// strcpy (ofname, ifname);
7405
// if ((cp = strrchr (ofname, '.'))) *cp = 0;
7406
// if (thumbnail_only)
7407
// strcat (ofname, ".thumb");
7408
// strcat (ofname, write_ext);
7409
// ofp = fopen (ofname, "wb");
7417
// fprintf (stderr,_("Writing data to %s ...\n"), ofname);
7418
// (*write_fun)(ofp);
7420
// if (ofp != stdout) fclose(ofp);
7422
// if (oprof) free(oprof);
7429
void writePfsFrame(pfs::Channel *R, pfs::Channel *G, pfs::Channel *B) {
7430
// uchar lut[0x10000];
7431
int /*c,*/row, col, soff, rstep, cstep;
7432
// if (output_bps == 8) gamma_lut (lut);
7433
soff = flip_index (0, 0);
7434
cstep = flip_index (0, 1) - soff;
7435
rstep = flip_index (1, 0) - flip_index (0, width);
7436
// #define FORCC for (c=0; c < colors; c++)
7437
for (row=0; row < height; row++, soff += rstep) {
7438
for (col=0; col < width; col++, soff += cstep) {
7439
// if (output_bps == 8)
7440
// FORCC ppm [col*colors+c] = lut[image[soff][c]];
7441
// else FORCC ppm2[col*colors+c] = image[soff][c];
7442
(*R)(col,row)=(float)(image[soff][0])/*/65535.0f*/;
7443
(*G)(col,row)=(float)(image[soff][1])/*/65535.0f*/;
7444
(*B)(col,row)=(float)(image[soff][2])/*/65535.0f*/;
7445
// if ( ((*R)(col,row)<0) || ((*G)(col,row)<0) || ((*B)(col,row)<0) ) {
7446
// fprintf(stderr,"<0");
7449
// if (output_bps == 16 && !output_tiff && th.order == 0x4949)
7450
// swab (ppm2, ppm2, width*colors*2);
7451
// fwrite (ppm, colors*output_bps/8, width, ofp);
7455
pfs::Frame *readRAWfile (const char * ifname, dcraw_opts* opts) {
7456
int status=0, /*user_flip=-1,*/ use_fuji_rotate=1;
7459
//get options from outer space :)
7460
use_auto_wb=opts->auto_wb;
7461
use_camera_wb=opts->camera_wb;
7462
highlight=opts->highlights;
7463
//opt->quality is accounted for below
7464
four_color_rgb=opts->four_colors;
7465
output_color=opts->output_color_space;
7466
fprintf(stderr,"dcraw: use_auto_wb: %s\n", use_auto_wb? "yes": "no");
7467
fprintf(stderr,"dcraw: use_camera_wb: %s\n", use_camera_wb? "yes": "no");
7468
fprintf(stderr,"dcraw: highlight: %d\n", highlight);
7469
fprintf(stderr,"dcraw: quality: %d\n", opts->quality);
7470
fprintf(stderr,"dcraw: four_colors: %s\n", four_color_rgb? "yes": "no");
7471
fprintf(stderr,"dcraw: color_space: %d\n", output_color);
7473
//we fix this value here, does it make sense to move it in the struct dcraw_opts?
7480
if (setjmp (failure)) {
7481
if (fileno(ifp) > 2) fclose(ifp);
7482
if (image) free (image);
7486
// ifname = argv[arg];
7487
if (!(ifp = fopen (ifname, "rb"))) {
7491
status = (identify(),!is_raw);
7492
// if (user_flip >= 0)
7493
// flip = user_flip;
7494
switch ((flip+3600) % 360) {
7495
case 270: flip = 5; break;
7496
case 180: flip = 3; break;
7500
// write_fun = &CLASS write_ppm_tiff;
7502
if (load_raw == &CLASS kodak_ycbcr_load_raw) {
7503
height += height & 1;
7507
shrink = /*half_size*/0 && filters;
7508
iheight = (height + shrink) >> shrink;
7509
iwidth = (width + shrink) >> shrink;
7510
// fprintf(stderr,"dcraw: width=%d, height=%d iwidth=%d, iheight=%d\n",width,height,iwidth,iheight);
7512
image = (ushort (*)[4])
7513
calloc (iheight*iwidth*sizeof *image + meta_length, 1);
7514
merror (image, "main()");
7515
meta_data = (char *) (image + iheight*iwidth);
7517
fseek (ifp, data_offset, SEEK_SET);
7523
// quality = 2 + !fuji_width;
7524
// fprintf(stderr,"dcraw: fuji_width: %d\n", fuji_width);
7525
quality = opts->quality;
7526
if (fuji_width && quality==2)
7528
fprintf(stderr,"dcraw: quality: %d\n", quality);
7529
// if (user_black >= 0) black = user_black;
7531
if (is_foveon && !document_mode) foveon_interpolate();
7532
if (!is_foveon && document_mode < 2) scale_colors();
7533
if (shrink) filters = 0;
7534
cam_to_cielab (NULL,NULL);
7535
if (filters && !document_mode) {
7536
fprintf(stderr,"dcraw: using filters\n");
7538
fprintf(stderr,"dcraw: bilinear, aka -q 0\n");
7540
} else if (quality == 1 || colors > 3) {
7541
fprintf(stderr,"dcraw: VNG, aka -q 2\n");
7544
fprintf(stderr,"dcraw: AHD, aka -q 3\n");
7548
// if (sigma_d > 0 && sigma_r > 0) bilateral_filter();
7549
if (!is_foveon && highlight > 1) recover_highlights();
7550
if (use_fuji_rotate) fuji_rotate();
7553
if (use_fuji_rotate) stretch();
7558
if (flip & 4) SWAP(height,width);
7559
pfs::Frame *frame = pfsio.createFrame( width, height );
7560
pfs::Channel *R, *G, *B;
7561
frame->createRGBChannels(R,G,B);
7562
writePfsFrame(R,G,B);
7564
if (oprof) free(oprof);