~ubuntu-branches/ubuntu/natty/qtpfsgui/natty

« back to all changes in this revision

Viewing changes to src/Fileformat/pfsindcraw.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-01-06 04:39:36 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080106043936-a9u9g7yih3w16ru5
Tags: 1.9.0-1
* New upstream release.
* Replace “COPYING” with “LICENSE” in the NOT_NEEDED variable of
  debian/rules, following upstream's renaming.
* Update debian/links accordingly.
* Delete the matching TODO item since there's no longer needed to have a
  patched (with HTML tags) license file to get a correct display in the
  “License agreement” tab.
* Update the gcc4.3 patch (drop the hunk touching src/Libpfs/pfs.cpp):
   - 20_gcc4.3_includes.
* Add a link from /usr/share/qtpfsgui/html to the HTML documentation
  under /usr/share/doc/qtpfsgui/html since the former is used at runtime
  to display the manual.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
   dcraw.c -- Dave Coffin's raw photo decoder
3
 
   Copyright 1997-2007 by Dave Coffin, dcoffin a cybercom o net
4
 
 
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.
7
 
 
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.
12
 
 
13
 
   Starting in Revision 1.237, the code to support Foveon cameras
14
 
   is under GPL.
15
 
 
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.
21
 
 
22
 
   $Revision: 1.363 $
23
 
   $Date: 2007/01/18 23:57:32 $
24
 
   grota: added a couple of functions to integrate in qtpfsgui
25
 
 */
26
 
#include <QImage>
27
 
#include "../Libpfs/pfs.h"
28
 
extern "C" {
29
 
#include "pfsindcraw.h"
30
 
#define DCRAWVERSION "8.53"
31
 
#define NO_LCMS
32
 
 
33
 
// #define _GNU_SOURCE
34
 
#define _USE_MATH_DEFINES
35
 
#include <ctype.h>
36
 
#include <errno.h>
37
 
#include <fcntl.h>
38
 
#include <float.h>
39
 
#include <limits.h>
40
 
#include <math.h>
41
 
#include <setjmp.h>
42
 
#include <stdio.h>
43
 
#include <stdlib.h>
44
 
#include <string.h>
45
 
#include <time.h>
46
 
/*
47
 
   NO_JPEG disables decoding of compressed Kodak DC120 files.
48
 
   NO_LCMS disables the "-p" option.
49
 
 */
50
 
#ifndef NO_JPEG
51
 
#include <jpeglib.h>
52
 
#endif
53
 
#ifndef NO_LCMS
54
 
#include <lcms.h>
55
 
#endif
56
 
#ifdef LOCALEDIR
57
 
#include <libintl.h>
58
 
#define _(String) gettext(String)
59
 
#else
60
 
#define _(String) (String)
61
 
#endif
62
 
 
63
 
//#ifdef __CYGWIN__
64
 
//#include <io.h>
65
 
//#endif
66
 
#ifdef WIN32
67
 
#include <sys/utime.h>
68
 
#include <winsock2.h>
69
 
#pragma comment(lib, "ws2_32.lib")
70
 
#define strcasecmp stricmp
71
 
typedef __int64 INT64;
72
 
typedef unsigned __int64 UINT64;
73
 
#else
74
 
#include <unistd.h>
75
 
#include <utime.h>
76
 
#include <netinet/in.h>
77
 
typedef long long INT64;
78
 
typedef unsigned long long UINT64;
79
 
#endif
80
 
 
81
 
#ifdef LJPEG_DECODE
82
 
#error Please compile dcraw.c by itself.
83
 
#error Do not link it with ljpeg_decode.
84
 
#endif
85
 
 
86
 
#ifndef LONG_BIT
87
 
#define LONG_BIT (8 * sizeof (long))
88
 
#endif
89
 
 
90
 
#define ushort UshORt
91
 
typedef unsigned char uchar;
92
 
typedef unsigned short ushort;
93
 
 
94
 
/*
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.
98
 
 */
99
 
FILE *ifp;
100
 
short order;
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;
103
 
time_t timestamp;
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;
113
 
double pixel_aspect;
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)();
130
 
jmp_buf failure;
131
 
 
132
 
struct decode {
133
 
  struct decode *branch[2];
134
 
  int leaf;
135
 
} first_decode[2048], *second_decode, *free_decode;
136
 
 
137
 
struct {
138
 
  int width, height, bps, comp, phint, offset, flip, samples, bytes;
139
 
} tiff_ifd[10];
140
 
 
141
 
struct {
142
 
  int format, key_off, black, black_off, split_col, tag_21a;
143
 
  float tag_210;
144
 
} ph1;
145
 
 
146
 
#define CLASS
147
 
#ifdef WIN32
148
 
#define fgetc getc
149
 
#else
150
 
#define fgetc getc_unlocked
151
 
#endif
152
 
 
153
 
 
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++)
157
 
 
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); }
166
 
 
167
 
/*
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
171
 
 
172
 
   Do not use the FC or BAYER macros with the Leaf CatchLight,
173
 
   because its pattern is 16x16, not 2x8.
174
 
 
175
 
   Return values are either 0/1/2/3 = G/M/C/Y or 0/1/2/3 = R/G1/B/G2
176
 
 
177
 
        PowerShot 600   PowerShot A50   PowerShot Pro70 Pro90 & G1
178
 
        0xe1e4e1e4:     0x1b4e4b1e:     0x1e4b4e1b:     0xb4b4b4b4:
179
 
 
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
189
 
          0 1 2 3 4 5
190
 
        0 C Y C Y C Y
191
 
        1 G M G M G M
192
 
        2 C Y C Y C Y
193
 
        3 M G M G M G
194
 
 
195
 
   All RGB cameras use one of these Bayer grids:
196
 
 
197
 
        0x16161616:     0x61616161:     0x49494949:     0x94949494:
198
 
 
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
204
 
 */
205
 
 
206
 
#define FC(row,col) \
207
 
        (filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
208
 
 
209
 
#define BAYER(row,col) \
210
 
        image[((row) >> shrink)*iwidth + ((col) >> shrink)][FC(row,col)]
211
 
 
212
 
#define BAYER2(row,col) \
213
 
        image[((row) >> shrink)*iwidth + ((col) >> shrink)][fc(row,col)]
214
 
 
215
 
int CLASS fc (int row, int col)
216
 
{
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 } };
234
 
 
235
 
  if (filters != 1) return FC(row,col);
236
 
  return filter[(row+top_margin) & 15][(col+left_margin) & 15];
237
 
}
238
 
 
239
 
#ifndef __GLIBC__
240
 
char *my_memmem (char *haystack, size_t haystacklen,
241
 
              char *needle, size_t needlelen)
242
 
{
243
 
  char *c;
244
 
  for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
245
 
    if (!memcmp (c, needle, needlelen))
246
 
      return c;
247
 
  return NULL;
248
 
}
249
 
#define memmem my_memmem
250
 
#endif
251
 
 
252
 
void CLASS merror (void *ptr, char *where)
253
 
{
254
 
  if (ptr) return;
255
 
  fprintf (stderr,_("%s: Out of memory in %s\n"), ifname, where);
256
 
  longjmp (failure, 1);
257
 
}
258
 
 
259
 
ushort CLASS sget2 (uchar *s)
260
 
{
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];
265
 
}
266
 
 
267
 
ushort CLASS get2()
268
 
{
269
 
  uchar str[2] = { 0xff,0xff };
270
 
  fread (str, 1, 2, ifp);
271
 
  return sget2(str);
272
 
}
273
 
 
274
 
int CLASS sget4 (uchar *s)
275
 
{
276
 
  if (order == 0x4949)
277
 
    return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
278
 
  else
279
 
    return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
280
 
}
281
 
#define sget4(s) sget4((uchar *)s)
282
 
 
283
 
int CLASS get4()
284
 
{
285
 
  uchar str[4] = { 0xff,0xff,0xff,0xff };
286
 
  fread (str, 1, 4, ifp);
287
 
  return sget4(str);
288
 
}
289
 
 
290
 
int CLASS getint (int type)
291
 
{
292
 
  return type == 3 ? get2() : get4();
293
 
}
294
 
 
295
 
float CLASS int_to_float (int i)
296
 
{
297
 
  union { int i; float f; } u;
298
 
  u.i = i;
299
 
  return u.f;
300
 
}
301
 
 
302
 
double CLASS getreal (int type)
303
 
{
304
 
  union { char c[8]; double d; } u;
305
 
  int i, rev;
306
 
 
307
 
  switch (type) {
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());
317
 
    case 12:
318
 
      rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
319
 
      for (i=0; i < 8; i++)
320
 
        u.c[i ^ rev] = fgetc(ifp);
321
 
      return u.d;
322
 
    default: return fgetc(ifp);
323
 
  }
324
 
}
325
 
#define getrat() getreal(10)
326
 
 
327
 
void CLASS read_shorts (ushort *pixel, int count)
328
 
{
329
 
  fread (pixel, 2, count, ifp);
330
 
  if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
331
 
    swab ((const char*)pixel, (char*)pixel, count*2);
332
 
}
333
 
 
334
 
void CLASS canon_600_fixed_wb (int temp)
335
 
{
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 } };
341
 
  int lo, hi, i;
342
 
  float frac=0;
343
 
 
344
 
  for (lo=4; --lo; )
345
 
    if (*mul[lo] <= temp) break;
346
 
  for (hi=0; hi < 3; hi++)
347
 
    if (*mul[hi] >= temp) break;
348
 
  if (lo != hi)
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]);
352
 
}
353
 
 
354
 
/* Return values:  0 = white  1 = near white  2 = not white */
355
 
int CLASS canon_600_color (int ratio[2], int mar)
356
 
{
357
 
  int clipped=0, target, miss;
358
 
 
359
 
  if (flash_used) {
360
 
    if (ratio[1] < -104)
361
 
      { ratio[1] = -104; clipped = 1; }
362
 
    if (ratio[1] >   12)
363
 
      { ratio[1] =   12; clipped = 1; }
364
 
  } else {
365
 
    if (ratio[1] < -264 || ratio[1] > 461) return 2;
366
 
    if (ratio[1] < -50)
367
 
      { ratio[1] = -50; clipped = 1; }
368
 
    if (ratio[1] > 307)
369
 
      { ratio[1] = 307; clipped = 1; }
370
 
  }
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;
381
 
  return 1;
382
 
}
383
 
 
384
 
void CLASS canon_600_auto_wb()
385
 
{
386
 
  int mar, row, col, i, j, st, count[] = { 0,0 };
387
 
  int test[8], total[2][8], ratio[2][2], stat[2];
388
 
 
389
 
  memset (&total, 0, sizeof total);
390
 
  i = canon_ev + 0.5;
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);
408
 
      }
409
 
      if ((st = stat[0] | stat[1]) > 1) goto next;
410
 
      for (i=0; i < 2; i++)
411
 
        if (stat[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];
416
 
      count[st]++;
417
 
next: continue;
418
 
    }
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]);
423
 
  }
424
 
}
425
 
 
426
 
void CLASS canon_600_coeff()
427
 
{
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  } };
435
 
  int t=0, i, c;
436
 
  float mc, yc;
437
 
 
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;
444
 
  }
445
 
  if (flash_used) t=5;
446
 
  for (raw_color = i=0; i < 3; i++)
447
 
    FORCC rgb_cam[i][c] = table[t][i*4 + c] / 1024.0;
448
 
}
449
 
 
450
 
void CLASS canon_600_load_raw()
451
 
{
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 } };
457
 
 
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    );
469
 
    }
470
 
    for (col=0; col < width; col++)
471
 
      BAYER(row,col) = pixel[col];
472
 
    for (col=width; col < raw_width; col++)
473
 
      black += pixel[col];
474
 
    if ((row+=2) > height) row = 1;
475
 
  }
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;
483
 
    }
484
 
  canon_600_fixed_wb(1311);
485
 
  canon_600_auto_wb();
486
 
  canon_600_coeff();
487
 
  maximum = (0x3ff - black) * 1109 >> 9;
488
 
  black = 0;
489
 
}
490
 
 
491
 
void CLASS remove_zeroes()
492
 
{
493
 
  unsigned row, col, tot, n, r, c;
494
 
 
495
 
  for (row=0; row < height; row++)
496
 
    for (col=0; col < width; col++)
497
 
      if (BAYER(row,col) == 0) {
498
 
        tot = n = 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;
505
 
      }
506
 
}
507
 
 
508
 
void CLASS canon_a5_load_raw()
509
 
{
510
 
  ushort data[1970], *dp, pixel;
511
 
  int vbits=0, buf=0, row, col, bc=0;
512
 
 
513
 
  order = 0x4949;
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++) {
517
 
      if (vbits < 10)
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);
523
 
    }
524
 
  }
525
 
  if (bc) black /= bc;
526
 
  maximum = 0x3ff;
527
 
  if (raw_width > 1600) remove_zeroes();
528
 
}
529
 
 
530
 
/*
531
 
   getbits(-1) initializes the buffer
532
 
   getbits(n) where 0 <= n <= 25 returns an n-bit integer
533
 
 */
534
 
unsigned CLASS getbits (int nbits)
535
 
{
536
 
  static unsigned bitbuf=0;
537
 
  static int vbits=0, reset=0;
538
 
  unsigned c;
539
 
 
540
 
  if (nbits == -1)
541
 
    return bitbuf = vbits = reset = 0;
542
 
  if (nbits == 0 || reset) return 0;
543
 
  while (vbits < nbits) {
544
 
    c = fgetc(ifp);
545
 
    if ((reset = zero_after_ff && c == 0xff && fgetc(ifp))) return 0;
546
 
    bitbuf = (bitbuf << 8) + c;
547
 
    vbits += 8;
548
 
  }
549
 
  vbits -= nbits;
550
 
  return bitbuf << (32-nbits-vbits) >> (32-nbits);
551
 
}
552
 
 
553
 
void CLASS init_decoder()
554
 
{
555
 
  memset (first_decode, 0, sizeof first_decode);
556
 
  free_decode = first_decode;
557
 
}
558
 
 
559
 
/*
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.
563
 
 
564
 
   For example, if the source is
565
 
 
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  },
568
 
 
569
 
   then the code is
570
 
 
571
 
        00              0x04
572
 
        010             0x03
573
 
        011             0x05
574
 
        100             0x06
575
 
        101             0x02
576
 
        1100            0x07
577
 
        1101            0x01
578
 
        11100           0x08
579
 
        11101           0x09
580
 
        11110           0x00
581
 
        111110          0x0a
582
 
        1111110         0x0b
583
 
        1111111         0xff
584
 
 */
585
 
uchar * CLASS make_decoder (const uchar *source, int level)
586
 
{
587
 
  struct decode *cur;
588
 
  static int leaf;
589
 
  int i, next;
590
 
 
591
 
  if (level==0) leaf=0;
592
 
  cur = free_decode++;
593
 
  if (free_decode > first_decode+2048) {
594
 
    fprintf (stderr,_("%s: decoder table overflow\n"), ifname);
595
 
    longjmp (failure, 2);
596
 
  }
597
 
  for (i=next=0; i <= leaf && next < 16; )
598
 
    i += source[next++];
599
 
  if (i > leaf) {
600
 
    if (level < next) {
601
 
      cur->branch[0] = free_decode;
602
 
      make_decoder (source, level+1);
603
 
      cur->branch[1] = free_decode;
604
 
      make_decoder (source, level+1);
605
 
    } else
606
 
      cur->leaf = source[16 + leaf++];
607
 
  }
608
 
  return (uchar *) source + 16 + leaf;
609
 
}
610
 
 
611
 
void CLASS crw_init_tables (unsigned table)
612
 
{
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  },
620
 
  };
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  }
667
 
  };
668
 
  if (table > 2) table = 2;
669
 
  init_decoder();
670
 
  make_decoder ( first_tree[table], 0);
671
 
  second_decode = free_decode;
672
 
  make_decoder (second_tree[table], 0);
673
 
}
674
 
 
675
 
/*
676
 
   Return 0 if the image starts with compressed data,
677
 
   1 if it starts with uncompressed low-order bits.
678
 
 
679
 
   In Canon compressed data, 0xff is always followed by 0x00.
680
 
 */
681
 
int CLASS canon_has_lowbits()
682
 
{
683
 
  uchar test[0x4000];
684
 
  int ret=1, i;
685
 
 
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;
691
 
      ret=0;
692
 
    }
693
 
  return ret;
694
 
}
695
 
 
696
 
void CLASS canon_compressed_load_raw()
697
 
{
698
 
  ushort *pixel, *prow;
699
 
  int lowbits, i, row, r, col, save, val;
700
 
  unsigned irow, icol;
701
 
  struct decode *decode, *dindex;
702
 
  int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
703
 
  uchar c;
704
 
 
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);
711
 
  zero_after_ff = 1;
712
 
  getbits(-1);
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)];
720
 
        leaf = dindex->leaf;
721
 
        decode = second_decode;
722
 
        if (leaf == 0 && i) break;
723
 
        if (leaf == 0xff) continue;
724
 
        i  += leaf >> 4;
725
 
        len = leaf & 15;
726
 
        if (len == 0) continue;
727
 
        diff = getbits(len);
728
 
        if ((diff & (1 << (len-1))) == 0)
729
 
          diff -= (1 << len) - 1;
730
 
        if (i < 64) diffbuf[i] = diff;
731
 
      }
732
 
      diffbuf[0] += carry;
733
 
      carry = diffbuf[0];
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] );
738
 
      }
739
 
    }
740
 
    if (lowbits) {
741
 
      save = ftell(ifp);
742
 
      fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
743
 
      for (prow=pixel, i=0; i < raw_width*2; i++) {
744
 
        c = fgetc(ifp);
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;
748
 
          *prow = val;
749
 
        }
750
 
      }
751
 
      fseek (ifp, save, SEEK_SET);
752
 
    }
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;
758
 
        if (icol < width)
759
 
          BAYER(irow,icol) = pixel[r*raw_width+col];
760
 
        else
761
 
          black += pixel[r*raw_width+col];
762
 
      }
763
 
    }
764
 
  }
765
 
  free (pixel);
766
 
  if (raw_width > width)
767
 
    black /= (raw_width - width) * height;
768
 
}
769
 
 
770
 
/*
771
 
   Not a full implementation of Lossless JPEG, just
772
 
   enough to decode Canon, Kodak and Adobe DNG images.
773
 
 */
774
 
struct jhead {
775
 
  int bits, high, wide, clrs, restart, vpred[4];
776
 
  struct decode *huff[4];
777
 
  ushort *row;
778
 
};
779
 
 
780
 
int CLASS ljpeg_start (struct jhead *jh, int info_only)
781
 
{
782
 
  int i, tag, len;
783
 
  uchar data[0x10000], *dp;
784
 
 
785
 
  init_decoder();
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;
791
 
  do {
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);
797
 
    switch (tag) {
798
 
      case 0xffc0:
799
 
      case 0xffc3:
800
 
        jh->bits = data[0];
801
 
        jh->high = data[1] << 8 | data[2];
802
 
        jh->wide = data[3] << 8 | data[4];
803
 
        jh->clrs = data[5];
804
 
        break;
805
 
      case 0xffc4:
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);
810
 
        }
811
 
        break;
812
 
      case 0xffdd:
813
 
        jh->restart = data[0] << 8 | data[1];
814
 
    }
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;
820
 
}
821
 
 
822
 
int CLASS ljpeg_diff (struct decode *dindex)
823
 
{
824
 
  int len, diff;
825
 
 
826
 
  while (dindex->branch[0])
827
 
    dindex = dindex->branch[getbits(1)];
828
 
  len = dindex->leaf;
829
 
  if (len == 16 && (!dng_version || dng_version >= 0x1010000))
830
 
    return -32768;
831
 
  diff = getbits(len);
832
 
  if ((diff & (1 << (len-1))) == 0)
833
 
    diff -= (1 << len) - 1;
834
 
  return diff;
835
 
}
836
 
 
837
 
void CLASS ljpeg_row (int jrow, struct jhead *jh)
838
 
{
839
 
  int col, c, diff;
840
 
  ushort mark=0, *outp=jh->row;
841
 
 
842
 
  if (jrow * jh->wide % jh->restart == 0) {
843
 
    FORC4 jh->vpred[c] = 1 << (jh->bits-1);
844
 
    if (jrow)
845
 
      do mark = (mark << 8) + (c = fgetc(ifp));
846
 
      while (c != EOF && mark >> 4 != 0xffd);
847
 
    getbits(-1);
848
 
  }
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);
853
 
      outp++;
854
 
    }
855
 
}
856
 
 
857
 
void CLASS lossless_jpeg_load_raw()
858
 
{
859
 
  int jwide, jrow, jcol, val, jidx, i, j, row=0, col=0;
860
 
  struct jhead jh;
861
 
  int min=INT_MAX;
862
 
 
863
 
  if (!ljpeg_start (&jh, 0)) return;
864
 
  jwide = jh.wide * jh.clrs;
865
 
 
866
 
  for (jrow=0; jrow < jh.high; jrow++) {
867
 
    ljpeg_row (jrow, &jh);
868
 
    for (jcol=0; jcol < jwide; jcol++) {
869
 
      val = jh.row[jcol];
870
 
      if (jh.bits <= 12)
871
 
        val = curve[val];
872
 
      if (cr2_slice[0]) {
873
 
        jidx = jrow*jwide + jcol;
874
 
        i = jidx / (cr2_slice[1]*jh.high);
875
 
        if ((j = i >= cr2_slice[0]))
876
 
                 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];
880
 
      }
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;
885
 
        } else black += val;
886
 
      }
887
 
      if (++col >= raw_width)
888
 
        col = (row++,0);
889
 
    }
890
 
  }
891
 
  free (jh.row);
892
 
  if (raw_width > width)
893
 
    black /= (raw_width - width) * height;
894
 
  if (!strcasecmp(make,"KODAK"))
895
 
    black = min;
896
 
}
897
 
 
898
 
void CLASS adobe_copy_pixel (int row, int col, ushort **rp)
899
 
{
900
 
  unsigned r, c;
901
 
 
902
 
  r = row -= top_margin;
903
 
  c = col -= left_margin;
904
 
  if (fuji_secondary && shot_select) (*rp)++;
905
 
  if (filters) {
906
 
    if (fuji_width) {
907
 
      r = row + fuji_width - 1 - (col >> 1);
908
 
      c = row + ((col+1) >> 1);
909
 
    }
910
 
    if (r < height && c < width)
911
 
      BAYER(r,c) = **rp < 0x1000 ? curve[**rp] : **rp;
912
 
    *rp += 1 + fuji_secondary;
913
 
  } else {
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];
917
 
    *rp += tiff_samples;
918
 
  }
919
 
  if (fuji_secondary && shot_select) (*rp)--;
920
 
}
921
 
 
922
 
void CLASS adobe_dng_load_raw_lj()
923
 
{
924
 
  int save, twide, trow=0, tcol=0, jrow, jcol;
925
 
  struct jhead jh;
926
 
  ushort *rp;
927
 
 
928
 
  while (1) {
929
 
    save = ftell(ifp);
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;
935
 
    twide = jh.wide;
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;
941
 
 
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);
946
 
    }
947
 
    fseek (ifp, save+4, SEEK_SET);
948
 
    if ((tcol += twide) >= raw_width) {
949
 
      tcol = 0;
950
 
      trow += jh.high;
951
 
    }
952
 
    free (jh.row);
953
 
  }
954
 
}
955
 
 
956
 
void CLASS adobe_dng_load_raw_nc()
957
 
{
958
 
  ushort *pixel, *rp;
959
 
  int row, col;
960
 
 
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++) {
964
 
    if (tiff_bps == 16)
965
 
      read_shorts (pixel, raw_width * tiff_samples);
966
 
    else {
967
 
      getbits(-1);
968
 
      for (col=0; col < raw_width * tiff_samples; col++)
969
 
        pixel[col] = getbits(tiff_bps);
970
 
    }
971
 
    for (rp=pixel, col=0; col < raw_width; col++)
972
 
      adobe_copy_pixel (row, col, &rp);
973
 
  }
974
 
  free (pixel);
975
 
}
976
 
 
977
 
void CLASS pentax_k10_load_raw()
978
 
{
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];
984
 
 
985
 
  init_decoder();
986
 
  make_decoder (pentax_tree, 0);
987
 
  getbits(-1);
988
 
  for (row=0; row < height; row++)
989
 
    for (col=0; col < raw_width; col++) {
990
 
      diff = ljpeg_diff (first_decode);
991
 
      if (col < 2) {
992
 
        i = 2*(row & 1) + (col & 1);
993
 
        vpred[i] += diff;
994
 
        hpred[col] = vpred[i];
995
 
      } else
996
 
        hpred[col & 1] += diff;
997
 
      if (col < width)
998
 
        BAYER(row,col) = hpred[col & 1];
999
 
    }
1000
 
}
1001
 
 
1002
 
void CLASS nikon_compressed_load_raw()
1003
 
{
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;
1009
 
 
1010
 
  init_decoder();
1011
 
  make_decoder (nikon_tree, 0);
1012
 
 
1013
 
  fseek (ifp, curve_offset, SEEK_SET);
1014
 
  read_shorts (vpred, 4);
1015
 
  csize = get2();
1016
 
  curve = (ushort *) calloc (csize, sizeof *curve);
1017
 
  merror (curve, "nikon_compressed_load_raw()");
1018
 
  read_shorts (curve, csize);
1019
 
 
1020
 
  fseek (ifp, data_offset, SEEK_SET);
1021
 
  getbits(-1);
1022
 
  for (row=0; row < height; row++)
1023
 
    for (col=0; col < raw_width; col++) {
1024
 
      diff = ljpeg_diff (first_decode);
1025
 
      if (col < 2) {
1026
 
        i = 2*(row & 1) + (col & 1);
1027
 
        vpred[i] += diff;
1028
 
        hpred[col] = vpred[i];
1029
 
      } else
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];
1035
 
    }
1036
 
  free (curve);
1037
 
}
1038
 
 
1039
 
void CLASS nikon_load_raw()
1040
 
{
1041
 
  int irow, row, col, i;
1042
 
 
1043
 
  getbits(-1);
1044
 
  for (irow=0; irow < height; irow++) {
1045
 
    row = 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);
1051
 
        getbits(-1);
1052
 
      }
1053
 
    }
1054
 
    for (col=0; col < raw_width; col++) {
1055
 
      i = getbits(12);
1056
 
      if ((unsigned) (col-left_margin) < width)
1057
 
        BAYER(row,col-left_margin) = i;
1058
 
      if (tiff_compress == 34713 && (col % 10) == 9)
1059
 
        getbits(8);
1060
 
    }
1061
 
  }
1062
 
}
1063
 
 
1064
 
/*
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".
1068
 
 */
1069
 
int CLASS nikon_is_compressed()
1070
 
{
1071
 
  uchar test[256];
1072
 
  int i;
1073
 
 
1074
 
  if (tiff_compress != 34713)
1075
 
    return 0;
1076
 
  if (strcmp(model,"D100"))
1077
 
    return 1;
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;
1082
 
  return 0;
1083
 
}
1084
 
 
1085
 
/*
1086
 
   Returns 1 for a Coolpix 995, 0 for anything else.
1087
 
 */
1088
 
int CLASS nikon_e995()
1089
 
{
1090
 
  int i, histo[256];
1091
 
  const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
1092
 
 
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)
1099
 
      return 0;
1100
 
  return 1;
1101
 
}
1102
 
 
1103
 
/*
1104
 
   Returns 1 for a Coolpix 2100, 0 for anything else.
1105
 
 */
1106
 
int CLASS nikon_e2100()
1107
 
{
1108
 
  uchar t[12];
1109
 
  int i;
1110
 
 
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)
1116
 
      return 0;
1117
 
  }
1118
 
  return 1;
1119
 
}
1120
 
 
1121
 
void CLASS nikon_3700()
1122
 
{
1123
 
  int bits, i;
1124
 
  uchar dp[24];
1125
 
  static const struct {
1126
 
    int bits;
1127
 
    char make[12], model[15];
1128
 
  } table[] = {
1129
 
    { 0x00, "PENTAX",  "Optio 33WR" },
1130
 
    { 0x03, "NIKON",   "E3200" },
1131
 
    { 0x32, "NIKON",   "E3700" },
1132
 
    { 0x33, "OLYMPUS", "C740UZ" } };
1133
 
 
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);
1141
 
    }
1142
 
}
1143
 
 
1144
 
/*
1145
 
   Separates a Minolta DiMAGE Z2 from a Nikon E4300.
1146
 
 */
1147
 
int CLASS minolta_z2()
1148
 
{
1149
 
  int i;
1150
 
  char tail[424];
1151
 
 
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;
1156
 
  return 0;
1157
 
}
1158
 
 
1159
 
/* Here raw_width is in bytes, not pixels. */
1160
 
void CLASS nikon_e900_load_raw()
1161
 
{
1162
 
  int offset=0, irow, row, col;
1163
 
 
1164
 
  for (irow=0; irow < height; irow++) {
1165
 
    row = irow * 2 % height;
1166
 
    if (row == 1)
1167
 
      offset = - (-offset & -4096);
1168
 
    fseek (ifp, offset, SEEK_SET);
1169
 
    offset += raw_width;
1170
 
    getbits(-1);
1171
 
    for (col=0; col < width; col++)
1172
 
      BAYER(row,col) = getbits(10);
1173
 
  }
1174
 
}
1175
 
 
1176
 
void CLASS nikon_e2100_load_raw()
1177
 
{
1178
 
  uchar   data[3456], *dp;
1179
 
  ushort pixel[2304], *pix;
1180
 
  int row, col;
1181
 
 
1182
 
  for (row=0; row <= height; row+=2) {
1183
 
    if (row == height) {
1184
 
      fseek (ifp, ((width==1616) << 13) - (-ftell(ifp) & -2048), SEEK_SET);
1185
 
      row = 1;
1186
 
    }
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];
1197
 
    }
1198
 
    for (col=0; col < width; col++)
1199
 
      BAYER(row,col) = (pixel[col] & 0xfff);
1200
 
  }
1201
 
}
1202
 
 
1203
 
/*
1204
 
   The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
1205
 
 */
1206
 
void CLASS fuji_load_raw()
1207
 
{
1208
 
  ushort *pixel;
1209
 
  int row, col, r, c;
1210
 
 
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++) {
1217
 
      if (fuji_layout) {
1218
 
        r = fuji_width - 1 - col + (row >> 1);
1219
 
        c = col + ((row+1) >> 1);
1220
 
      } else {
1221
 
        r = fuji_width - 1 + row - (col >> 1);
1222
 
        c = row + ((col+1) >> 1);
1223
 
      }
1224
 
      BAYER(r,c) = pixel[col];
1225
 
    }
1226
 
  }
1227
 
  free (pixel);
1228
 
}
1229
 
 
1230
 
void CLASS jpeg_thumb (FILE *tfp);
1231
 
 
1232
 
void CLASS ppm_thumb (FILE *tfp)
1233
 
{
1234
 
  char *thumb;
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);
1241
 
  free (thumb);
1242
 
}
1243
 
 
1244
 
void CLASS layer_thumb (FILE *tfp)
1245
 
{
1246
 
  int i, c;
1247
 
  char *thumb, map[][4] = { "012","102" };
1248
 
 
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);
1258
 
  free (thumb);
1259
 
}
1260
 
 
1261
 
void CLASS rollei_thumb (FILE *tfp)
1262
 
{
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);
1272
 
  }
1273
 
  free (thumb);
1274
 
}
1275
 
 
1276
 
void CLASS rollei_load_raw()
1277
 
{
1278
 
  uchar pixel[10];
1279
 
  unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
1280
 
 
1281
 
  isix = raw_width * raw_height * 5 / 8;
1282
 
  while (fread (pixel, 1, 10, ifp) == 10) {
1283
 
    for (i=0; i < 10; i+=2) {
1284
 
      todo[i]   = iten++;
1285
 
      todo[i+1] = pixel[i] << 8 | pixel[i+1];
1286
 
      buffer    = pixel[i] >> 2 | buffer << 6;
1287
 
    }
1288
 
    for (   ; i < 16; i+=2) {
1289
 
      todo[i]   = isix++;
1290
 
      todo[i+1] = buffer >> (14-i)*5;
1291
 
    }
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);
1297
 
    }
1298
 
  }
1299
 
  maximum = 0x3ff;
1300
 
}
1301
 
 
1302
 
int CLASS bayer (unsigned row, unsigned col)
1303
 
{
1304
 
  return (row < height && col < width) ? BAYER(row,col) : 0;
1305
 
}
1306
 
 
1307
 
void CLASS phase_one_flat_field (int is_float, int nc)
1308
 
{
1309
 
  ushort head[8];
1310
 
  unsigned wide, y, x, c, rend, cend, row, col;
1311
 
  float *mrow, num, mult[4];
1312
 
 
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];
1323
 
      }
1324
 
    if (y==0) continue;
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];
1331
 
        }
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;
1335
 
          if (!(c & 1)) {
1336
 
            c = BAYER(row,col) * mult[c];
1337
 
            BAYER(row,col) = LIM(c,0,65535);
1338
 
          }
1339
 
          for (c=0; c < nc; c+=2)
1340
 
            mult[c] += mult[c+1];
1341
 
        }
1342
 
      }
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];
1346
 
    }
1347
 
  }
1348
 
  free (mrow);
1349
 
}
1350
 
 
1351
 
void CLASS phase_one_correct()
1352
 
{
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];
1361
 
 
1362
 
  if (shrink || !meta_length) return;
1363
 
  if (verbose) fprintf (stderr,_("Phase One correction...\n"));
1364
 
  fseek (ifp, meta_offset, SEEK_SET);
1365
 
  order = get2();
1366
 
  fseek (ifp, 6, SEEK_CUR);
1367
 
  fseek (ifp, meta_offset+get4(), SEEK_SET);
1368
 
  entries = get4();  get4();
1369
 
  while (entries--) {
1370
 
    tag  = get4();
1371
 
    len  = get4();
1372
 
    data = get4();
1373
 
    save = ftell(ifp);
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;
1408
 
              }
1409
 
              BAYER(row,col) = (sum - val[max])/3.0 + 0.5;
1410
 
            } else {
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;
1415
 
            }
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;
1422
 
        }
1423
 
      }
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) {
1434
 
        mindiff = diff;
1435
 
        off_412 = ftell(ifp) - 38;
1436
 
      }
1437
 
    }
1438
 
    fseek (ifp, save, SEEK_SET);
1439
 
  }
1440
 
  if (off_412) {
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]);
1448
 
    get2();
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);
1466
 
        }
1467
 
        i = ((mult[0] * (1-cfrac) + mult[1] * cfrac)
1468
 
                * (row + top_margin) + num) * 2;
1469
 
        BAYER(row,col) = LIM(i,0,65535);
1470
 
      }
1471
 
    free (yval[0]);
1472
 
  }
1473
 
}
1474
 
 
1475
 
void CLASS phase_one_load_raw()
1476
 
{
1477
 
  int row, col, a, b;
1478
 
  ushort *pixel, akey, bkey, mask;
1479
 
 
1480
 
  fseek (ifp, ph1.key_off, SEEK_SET);
1481
 
  akey = get2();
1482
 
  bkey = get2();
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);
1494
 
    }
1495
 
    for (col=0; col < width; col++)
1496
 
      BAYER(row,col) = pixel[col+left_margin];
1497
 
  }
1498
 
  free (pixel);
1499
 
  phase_one_correct();
1500
 
}
1501
 
 
1502
 
unsigned CLASS ph1_bits (int nbits)
1503
 
{
1504
 
  static UINT64 bitbuf=0;
1505
 
  static int vbits=0;
1506
 
 
1507
 
  if (nbits == 0)
1508
 
    return bitbuf = vbits = 0;
1509
 
  if (vbits < nbits) {
1510
 
    bitbuf = bitbuf << 32 | (unsigned) get4();
1511
 
    vbits += 32;
1512
 
  }
1513
 
  vbits -= nbits;
1514
 
  return bitbuf << (64 - nbits - vbits) >> (64 - nbits);
1515
 
}
1516
 
 
1517
 
void CLASS phase_one_load_raw_c()
1518
 
{
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;
1521
 
  ushort *pixel;
1522
 
  short (*black)[2];
1523
 
 
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);
1532
 
  if (ph1.black_off)
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);
1538
 
    ph1_bits(0);
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)];
1547
 
        }
1548
 
      if ((i = len[col & 1]) == 14)
1549
 
        pixel[col] = pred[col & 1] = ph1_bits(16);
1550
 
      else
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]];
1554
 
    }
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;
1560
 
      }
1561
 
  }
1562
 
  free (pixel);
1563
 
  phase_one_correct();
1564
 
  maximum = 0xfffc - ph1.black;
1565
 
}
1566
 
 
1567
 
void CLASS leaf_hdr_load_raw()
1568
 
{
1569
 
  ushort *pixel;
1570
 
  unsigned tile=0, r, c, row, col;
1571
 
 
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);
1579
 
      }
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];
1586
 
    }
1587
 
  }
1588
 
  free (pixel);
1589
 
  if (!filters) {
1590
 
    maximum = 0xffff;
1591
 
    raw_color = 1;
1592
 
  }
1593
 
}
1594
 
 
1595
 
void CLASS unpacked_load_raw();
1596
 
 
1597
 
void CLASS sinar_4shot_load_raw()
1598
 
{
1599
 
  ushort *pixel;
1600
 
  unsigned shot, row, col, r, c;
1601
 
 
1602
 
  if ((shot = shot_select) || shrink) {
1603
 
    if (shot) shot--;
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();
1608
 
    return;
1609
 
  }
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];
1621
 
      }
1622
 
    }
1623
 
  }
1624
 
  free (pixel);
1625
 
  filters = 0;
1626
 
}
1627
 
 
1628
 
void CLASS imacon_full_load_raw()
1629
 
{
1630
 
  int row, col;
1631
 
 
1632
 
  for (row=0; row < height; row++)
1633
 
    for (col=0; col < width; col++)
1634
 
      read_shorts (image[row*width+col], 3);
1635
 
}
1636
 
 
1637
 
/* Here raw_width is in bytes, not pixels. */
1638
 
void CLASS packed_12_load_raw()
1639
 
{
1640
 
  int row, col;
1641
 
 
1642
 
  getbits(-1);
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++)
1647
 
      getbits(8);
1648
 
  }
1649
 
}
1650
 
 
1651
 
void CLASS unpacked_load_raw()
1652
 
{
1653
 
  ushort *pixel;
1654
 
  int row, col;
1655
 
 
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];
1663
 
  }
1664
 
  free (pixel);
1665
 
}
1666
 
 
1667
 
void CLASS panasonic_load_raw()
1668
 
{
1669
 
  unpacked_load_raw();
1670
 
  remove_zeroes();
1671
 
}
1672
 
 
1673
 
void CLASS olympus_e300_load_raw()
1674
 
{
1675
 
  uchar  *data,  *dp;
1676
 
  ushort *pixel, *pix;
1677
 
  int dwide, row, col;
1678
 
 
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;
1689
 
    }
1690
 
    for (col=0; col < width; col++)
1691
 
      BAYER(row,col) = (pixel[col] & 0xfff);
1692
 
  }
1693
 
  free (data);
1694
 
  maximum = 0xfff;
1695
 
  black >>= 4;
1696
 
}
1697
 
 
1698
 
void CLASS olympus_cseries_load_raw()
1699
 
{
1700
 
  int irow, row, col;
1701
 
 
1702
 
  for (irow=0; irow < height; irow++) {
1703
 
    row = irow * 2 % height + irow / (height/2);
1704
 
    if (row < 2) {
1705
 
      fseek (ifp, data_offset - row*(-width*height*3/4 & -2048), SEEK_SET);
1706
 
      getbits(-1);
1707
 
    }
1708
 
    for (col=0; col < width; col++)
1709
 
      BAYER(row,col) = getbits(12);
1710
 
  }
1711
 
  black >>= 4;
1712
 
}
1713
 
 
1714
 
void CLASS minolta_rd175_load_raw()
1715
 
{
1716
 
  uchar pixel[768];
1717
 
  unsigned irow, box, row, col;
1718
 
 
1719
 
  for (irow=0; irow < 1481; irow++) {
1720
 
    fread (pixel, 1, 768, ifp);
1721
 
    box = irow / 82;
1722
 
    row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box-12)*2);
1723
 
    switch (irow) {
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;
1728
 
    }
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;
1735
 
    } else
1736
 
      for (col=row & 1; col < 1534; col+=2)
1737
 
        BAYER(row,col) = pixel[col/2] << 1;
1738
 
  }
1739
 
  maximum = 0xff << 1;
1740
 
}
1741
 
 
1742
 
void CLASS eight_bit_load_raw()
1743
 
{
1744
 
  uchar *pixel;
1745
 
  int row, col;
1746
 
 
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];
1753
 
  }
1754
 
  free (pixel);
1755
 
  maximum = 0xff;
1756
 
}
1757
 
 
1758
 
void CLASS casio_qv5700_load_raw()
1759
 
{
1760
 
  uchar  data[3232],  *dp;
1761
 
  ushort pixel[2576], *pix;
1762
 
  int row, col;
1763
 
 
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]     );
1771
 
    }
1772
 
    for (col=0; col < width; col++)
1773
 
      BAYER(row,col) = (pixel[col] & 0x3ff);
1774
 
  }
1775
 
  maximum = 0x3fc;
1776
 
}
1777
 
 
1778
 
void CLASS nucore_load_raw()
1779
 
{
1780
 
  ushort *pixel;
1781
 
  int irow, row, col;
1782
 
 
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];
1790
 
  }
1791
 
  free (pixel);
1792
 
}
1793
 
 
1794
 
const int * CLASS make_decoder_int (const int *source, int level)
1795
 
{
1796
 
  struct decode *cur;
1797
 
 
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);
1804
 
  } else {
1805
 
    cur->leaf = source[1];
1806
 
    source += 2;
1807
 
  }
1808
 
  return source;
1809
 
}
1810
 
 
1811
 
int CLASS radc_token (int tree)
1812
 
{
1813
 
  int t;
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,
1826
 
    1,0, 2,2, 2,-2,
1827
 
    1,-3, 1,3,
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
1834
 
  };
1835
 
 
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);
1840
 
    }
1841
 
  if (tree == 18) {
1842
 
    if (kodak_cbpp == 243)
1843
 
      return (getbits(6) << 2) + 2;     /* most DC50 photos */
1844
 
    else
1845
 
      return (getbits(5) << 3) + 4;     /* DC40, Fotoman Pixtura */
1846
 
  }
1847
 
  for (dindex = dstart[tree]; dindex->branch[0]; )
1848
 
    dindex = dindex->branch[getbits(1)];
1849
 
  return dindex->leaf;
1850
 
}
1851
 
 
1852
 
#define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
1853
 
 
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)
1856
 
 
1857
 
void CLASS kodak_radc_load_raw()
1858
 
{
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];
1861
 
 
1862
 
  init_decoder();
1863
 
  getbits(-1);
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);
1868
 
    FORC3 {
1869
 
      val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
1870
 
      s = val > 65564 ? 10:12;
1871
 
      x = ~(-1 << (s-1));
1872
 
      val <<= 12-s;
1873
 
      for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
1874
 
        buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
1875
 
      last[c] = mul[c];
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))) {
1880
 
            col -= 2;
1881
 
            if (tree == 8)
1882
 
              FORYX buf[c][y][x] = radc_token(tree+10) * mul[c];
1883
 
            else
1884
 
              FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
1885
 
          } else
1886
 
            do {
1887
 
              nreps = (col > 2) ? radc_token(9) + 1 : 1;
1888
 
              for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
1889
 
                col -= 2;
1890
 
                FORYX buf[c][y][x] = PREDICTOR;
1891
 
                if (rep & 1) {
1892
 
                  step = radc_token(10) << 4;
1893
 
                  FORYX buf[c][y][x] += step;
1894
 
                }
1895
 
              }
1896
 
            } while (nreps == 9);
1897
 
        }
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;
1902
 
            if (c)
1903
 
              BAYER(row+y*2+c-1,x*2+2-c) = val;
1904
 
            else
1905
 
              BAYER(row+r*2+y,x*2+y) = val;
1906
 
          }
1907
 
        memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
1908
 
      }
1909
 
    }
1910
 
    for (y=row; y < row+4; y++)
1911
 
      for (x=0; x < width; x++)
1912
 
        if ((x+y) & 1) {
1913
 
          val = (BAYER(y,x)-2048)*2 + (BAYER(y,x-1)+BAYER(y,x+1))/2;
1914
 
          if (val < 0) val = 0;
1915
 
          BAYER(y,x) = val;
1916
 
        }
1917
 
  }
1918
 
  maximum = 10000;
1919
 
}
1920
 
 
1921
 
#undef FORYX
1922
 
#undef PREDICTOR
1923
 
 
1924
 
#ifdef NO_JPEG
1925
 
void CLASS kodak_jpeg_load_raw() {}
1926
 
#else
1927
 
 
1928
 
METHODDEF(boolean)
1929
 
fill_input_buffer (j_decompress_ptr cinfo)
1930
 
{
1931
 
  static uchar jpeg_buffer[4096];
1932
 
  size_t nbytes;
1933
 
 
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;
1938
 
  return TRUE;
1939
 
}
1940
 
 
1941
 
void CLASS kodak_jpeg_load_raw()
1942
 
{
1943
 
  struct jpeg_decompress_struct cinfo;
1944
 
  struct jpeg_error_mgr jerr;
1945
 
  JSAMPARRAY buf;
1946
 
  JSAMPLE (*pixel)[3];
1947
 
  int row, col;
1948
 
 
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);
1961
 
  }
1962
 
  buf = (*cinfo.mem->alloc_sarray)
1963
 
                ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
1964
 
 
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];
1974
 
    }
1975
 
  }
1976
 
  jpeg_finish_decompress (&cinfo);
1977
 
  jpeg_destroy_decompress (&cinfo);
1978
 
  maximum = 0xff << 1;
1979
 
}
1980
 
#endif
1981
 
 
1982
 
void CLASS kodak_dc120_load_raw()
1983
 
{
1984
 
  static const int mul[4] = { 162, 192, 187,  92 };
1985
 
  static const int add[4] = {   0, 636, 424, 212 };
1986
 
  uchar pixel[848];
1987
 
  int row, shift, col;
1988
 
 
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];
1994
 
  }
1995
 
  maximum = 0xff;
1996
 
}
1997
 
 
1998
 
void CLASS kodak_easy_load_raw()
1999
 
{
2000
 
  uchar *pixel;
2001
 
  unsigned row, col, val;
2002
 
 
2003
 
  if (raw_width > width)
2004
 
    black = 0;
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;
2013
 
      else black += val;
2014
 
    }
2015
 
  }
2016
 
  free (pixel);
2017
 
  if (raw_width > width)
2018
 
    black /= (raw_width - width) * height;
2019
 
  if (!strncmp(model,"DC2",3))
2020
 
    black = 0;
2021
 
  maximum = curve[0xff];
2022
 
}
2023
 
 
2024
 
void CLASS kodak_262_load_raw()
2025
 
{
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];
2030
 
  uchar *pixel;
2031
 
  int *strip, ns, i, row, col, chess, pi=0, pi1, pi2, pred, val;
2032
 
 
2033
 
  init_decoder();
2034
 
  for (i=0; i < 2; i++) {
2035
 
    decode[i] = free_decode;
2036
 
    make_decoder (kodak_tree[i], 0);
2037
 
  }
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);
2042
 
  order = 0x4d4d;
2043
 
  for (i=0; i < ns; i++)
2044
 
    strip[i] = get4();
2045
 
  for (row=0; row < raw_height; row++) {
2046
 
    if ((row & 31) == 0) {
2047
 
      fseek (ifp, strip[row >> 5], SEEK_SET);
2048
 
      getbits(-1);
2049
 
      pi = 0;
2050
 
    }
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;
2064
 
      else black += val;
2065
 
    }
2066
 
  }
2067
 
  free (pixel);
2068
 
  if (raw_width > width)
2069
 
    black /= (raw_width - width) * height;
2070
 
}
2071
 
 
2072
 
int CLASS kodak_65000_decode (short *out, int bsize)
2073
 
{
2074
 
  uchar c, blen[768];
2075
 
  ushort raw[6];
2076
 
  INT64 bitbuf=0;
2077
 
  int save, bits=0, i, j, len, diff;
2078
 
 
2079
 
  save = ftell(ifp);
2080
 
  bsize = (bsize + 3) & -4;
2081
 
  for (i=0; i < bsize; i+=2) {
2082
 
    c = fgetc(ifp);
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;
2092
 
      }
2093
 
      return 1;
2094
 
    }
2095
 
  }
2096
 
  if ((bsize & 7) == 4) {
2097
 
    bitbuf  = fgetc(ifp) << 8;
2098
 
    bitbuf += fgetc(ifp);
2099
 
    bits = 16;
2100
 
  }
2101
 
  for (i=0; i < bsize; i++) {
2102
 
    len = blen[i];
2103
 
    if (bits < len) {
2104
 
      for (j=0; j < 32; j+=8)
2105
 
        bitbuf += (INT64) fgetc(ifp) << (bits+(j^8));
2106
 
      bits += 32;
2107
 
    }
2108
 
    diff = bitbuf & (0xffff >> (16-len));
2109
 
    bitbuf >>= len;
2110
 
    bits -= len;
2111
 
    if ((diff & (1 << (len-1))) == 0)
2112
 
      diff -= (1 << len) - 1;
2113
 
    out[i] = diff;
2114
 
  }
2115
 
  return 0;
2116
 
}
2117
 
 
2118
 
void CLASS kodak_65000_load_raw()
2119
 
{
2120
 
  short buf[256];
2121
 
  int row, col, len, pred[2], ret, i;
2122
 
 
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])];
2130
 
    }
2131
 
}
2132
 
 
2133
 
void CLASS kodak_ycbcr_load_raw()
2134
 
{
2135
 
  short buf[384], *bp;
2136
 
  int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
2137
 
  ushort *ip;
2138
 
 
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) {
2145
 
        cb += bp[4];
2146
 
        cr += bp[5];
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)];
2155
 
          }
2156
 
      }
2157
 
    }
2158
 
}
2159
 
 
2160
 
void CLASS kodak_rgb_load_raw()
2161
 
{
2162
 
  short buf[768], *bp;
2163
 
  int row, col, len, c, i, rgb[3];
2164
 
  ushort *ip=image[0];
2165
 
 
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;
2173
 
    }
2174
 
}
2175
 
 
2176
 
void CLASS kodak_thumb_load_raw()
2177
 
{
2178
 
  int row, col;
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;
2184
 
}
2185
 
 
2186
 
void CLASS sony_decrypt (unsigned *data, int len, int start, int key)
2187
 
{
2188
 
  static unsigned pad[128], p;
2189
 
 
2190
 
  if (start) {
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]);
2198
 
  }
2199
 
  while (len--)
2200
 
    *data++ ^= pad[p++ & 127] = pad[(p+1) & 127] ^ pad[(p+65) & 127];
2201
 
}
2202
 
 
2203
 
void CLASS sony_load_raw()
2204
 
{
2205
 
  uchar head[40];
2206
 
  ushort *pixel;
2207
 
  unsigned i, key, row, col;
2208
 
 
2209
 
  fseek (ifp, 200896, SEEK_SET);
2210
 
  fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
2211
 
  order = 0x4d4d;
2212
 
  key = get4();
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]);
2228
 
  }
2229
 
  free (pixel);
2230
 
  if (left_margin > 9)
2231
 
    black /= (left_margin-9) * height;
2232
 
  maximum = 0x3ff0;
2233
 
}
2234
 
 
2235
 
void CLASS sony_arw_load_raw()
2236
 
{
2237
 
  int col, row, len, diff, sum=0;
2238
 
 
2239
 
  getbits(-1);
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;
2245
 
      if (len == 4)
2246
 
        while (len < 17 && !getbits(1)) len++;
2247
 
      diff = getbits(len);
2248
 
      if ((diff & (1 << (len-1))) == 0)
2249
 
        diff -= (1 << len) - 1;
2250
 
      sum += diff;
2251
 
      if (row < height) BAYER(row,col) = sum;
2252
 
    }
2253
 
}
2254
 
 
2255
 
#define HOLE(row) ((holes >> (((row) - raw_height) & 7)) & 1)
2256
 
 
2257
 
/* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
2258
 
void CLASS smal_decode_segment (unsigned seg[2][2], int holes)
2259
 
{
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;
2269
 
 
2270
 
  fseek (ifp, seg[0][1]+1, SEEK_SET);
2271
 
  getbits(-1);
2272
 
  for (pix=seg[0][0]; pix < seg[1][0]; pix++) {
2273
 
    for (s=0; s < 3; s++) {
2274
 
      data = data << nbits | getbits(nbits);
2275
 
      if (carry < 0)
2276
 
        carry = (nbits += carry+1) < 1 ? nbits-1 : 0;
2277
 
      while (--nbits >= 0)
2278
 
        if ((data >> nbits & 0xff) == 0xff) break;
2279
 
      if (nbits > 0)
2280
 
          data = ((data & ((1 << (nbits-1)) - 1)) << 1) |
2281
 
        ((data + (((data & (1 << (nbits-1)))) << 1)) & (-1 << nbits));
2282
 
      if (nbits >= 0) {
2283
 
        data += getbits(1);
2284
 
        carry = nbits - 8;
2285
 
      }
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;
2290
 
      high -= low;
2291
 
      for (nbits=0; high << nbits < 128; nbits++);
2292
 
      range = (range+low) << nbits;
2293
 
      high <<= nbits;
2294
 
      next = hist[s][1];
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;
2298
 
        hist[s][2] = 1;
2299
 
      }
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]++;
2305
 
      }
2306
 
      hist[s][1] = next;
2307
 
      sym[s] = bin;
2308
 
    }
2309
 
    diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
2310
 
    if (sym[0] & 4)
2311
 
      diff = diff ? -diff : 0x80;
2312
 
    if (ftell(ifp) + 12 >= seg[1][1])
2313
 
      diff = 0;
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;
2320
 
  }
2321
 
  maximum = 0xff;
2322
 
}
2323
 
 
2324
 
void CLASS smal_v6_load_raw()
2325
 
{
2326
 
  unsigned seg[2][2];
2327
 
 
2328
 
  fseek (ifp, 16, SEEK_SET);
2329
 
  seg[0][0] = 0;
2330
 
  seg[0][1] = get2();
2331
 
  seg[1][0] = raw_width * raw_height;
2332
 
  seg[1][1] = INT_MAX;
2333
 
  smal_decode_segment (seg, 0);
2334
 
  use_gamma = 0;
2335
 
}
2336
 
 
2337
 
int CLASS median4 (int *p)
2338
 
{
2339
 
  int min, max, sum, i;
2340
 
 
2341
 
  min = max = sum = p[0];
2342
 
  for (i=1; i < 4; i++) {
2343
 
    sum += p[i];
2344
 
    if (min > p[i]) min = p[i];
2345
 
    if (max < p[i]) max = p[i];
2346
 
  }
2347
 
  return (sum - min - max) >> 1;
2348
 
}
2349
 
 
2350
 
void CLASS fill_holes (int holes)
2351
 
{
2352
 
  int row, col, val[4];
2353
 
 
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);
2362
 
    }
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;
2366
 
      else {
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);
2372
 
      }
2373
 
  }
2374
 
}
2375
 
 
2376
 
void CLASS smal_v9_load_raw()
2377
 
{
2378
 
  unsigned seg[256][2], offset, nseg, holes, i;
2379
 
 
2380
 
  fseek (ifp, 67, SEEK_SET);
2381
 
  offset = get4();
2382
 
  nseg = fgetc(ifp);
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);
2387
 
  holes = fgetc(ifp);
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);
2394
 
}
2395
 
 
2396
 
/* BEGIN GPL BLOCK */
2397
 
 
2398
 
void CLASS foveon_decoder (unsigned size, unsigned code)
2399
 
{
2400
 
  static unsigned huff[1024];
2401
 
  struct decode *cur;
2402
 
  int i, len;
2403
 
 
2404
 
  if (!code) {
2405
 
    for (i=0; i < size; i++)
2406
 
      huff[i] = get4();
2407
 
    init_decoder();
2408
 
  }
2409
 
  cur = free_decode++;
2410
 
  if (free_decode > first_decode+2048) {
2411
 
    fprintf (stderr,_("%s: decoder table overflow\n"), ifname);
2412
 
    longjmp (failure, 2);
2413
 
  }
2414
 
  if (code)
2415
 
    for (i=0; i < size; i++)
2416
 
      if (huff[i] == code) {
2417
 
        cur->leaf = i;
2418
 
        return;
2419
 
      }
2420
 
  if ((len = code >> 27) > 26) return;
2421
 
  code = (len+1) << 27 | (code & 0x3ffffff) << 1;
2422
 
 
2423
 
  cur->branch[0] = free_decode;
2424
 
  foveon_decoder (size, code);
2425
 
  cur->branch[1] = free_decode;
2426
 
  foveon_decoder (size, code+1);
2427
 
}
2428
 
 
2429
 
void CLASS foveon_thumb (FILE *tfp)
2430
 
{
2431
 
  int bwide, row, col, bit=-1, c, i;
2432
 
  char *buf;
2433
 
  struct decode *dindex;
2434
 
  short pred[3];
2435
 
  unsigned bitbuf=0;
2436
 
 
2437
 
  bwide = get4();
2438
 
  fprintf (tfp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
2439
 
  if (bwide > 0) {
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);
2445
 
    }
2446
 
    free (buf);
2447
 
    return;
2448
 
  }
2449
 
  foveon_decoder (256, 0);
2450
 
 
2451
 
  for (row=0; row < thumb_height; row++) {
2452
 
    memset (pred, 0, sizeof pred);
2453
 
    if (!bit) get4();
2454
 
    for (col=bit=0; col < thumb_width; col++)
2455
 
      FORC3 {
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];
2461
 
        }
2462
 
        pred[c] += dindex->leaf;
2463
 
        fputc (pred[c], tfp);
2464
 
      }
2465
 
  }
2466
 
}
2467
 
 
2468
 
void CLASS foveon_load_camf()
2469
 
{
2470
 
  unsigned key, i, val;
2471
 
 
2472
 
  fseek (ifp, meta_offset, SEEK_SET);
2473
 
  key = get4();
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;
2479
 
  }
2480
 
}
2481
 
 
2482
 
void CLASS foveon_load_raw()
2483
 
{
2484
 
  struct decode *dindex;
2485
 
  short diff[1024], pred[3];
2486
 
  unsigned bitbuf=0;
2487
 
  int fixed, row, col, bit=-1, c, i;
2488
 
 
2489
 
  fixed = get4();
2490
 
  read_shorts ((ushort *) diff, 1024);
2491
 
  if (!fixed) foveon_decoder (1024, 0);
2492
 
 
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++) {
2497
 
      if (fixed) {
2498
 
        bitbuf = get4();
2499
 
        FORC3 pred[2-c] += diff[bitbuf >> c*10 & 0x3ff];
2500
 
      }
2501
 
      else FORC3 {
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];
2507
 
        }
2508
 
        pred[c] += diff[dindex->leaf];
2509
 
      }
2510
 
      FORC3 image[row*width+col][c] = pred[c];
2511
 
    }
2512
 
  }
2513
 
  if (document_mode)
2514
 
    for (i=0; i < height*width*4; i++)
2515
 
      if ((short) image[0][i] < 0) image[0][i] = 0;
2516
 
  foveon_load_camf();
2517
 
}
2518
 
 
2519
 
const char * CLASS foveon_camf_param (const char *block, const char *param)
2520
 
{
2521
 
  unsigned idx, num;
2522
 
  char *pos, *cp, *dp;
2523
 
 
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);
2530
 
    num = sget4(cp);
2531
 
    dp = pos + sget4(cp+4);
2532
 
    while (num--) {
2533
 
      cp += 8;
2534
 
      if (!strcmp (param, dp+sget4(cp)))
2535
 
        return dp+sget4(cp+4);
2536
 
    }
2537
 
  }
2538
 
  return NULL;
2539
 
}
2540
 
 
2541
 
void * CLASS foveon_camf_matrix (int dim[3], const char *name)
2542
 
{
2543
 
  unsigned i, idx, type, ndim, size, *mat;
2544
 
  char *pos, *cp, *dp;
2545
 
 
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);
2553
 
    type = sget4(cp);
2554
 
    if ((ndim = sget4(cp+4)) > 3) break;
2555
 
    dp = pos + sget4(cp+8);
2556
 
    for (i=ndim; i--; ) {
2557
 
      cp += 12;
2558
 
      dim[i] = sget4(cp);
2559
 
    }
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);
2566
 
      else
2567
 
        mat[i] = sget4(dp + i*2) & 0xffff;
2568
 
    return mat;
2569
 
  }
2570
 
  fprintf (stderr,_("%s: \"%s\" matrix not found!\n"), ifname, name);
2571
 
  return NULL;
2572
 
}
2573
 
 
2574
 
int CLASS foveon_fixed (void *ptr, int size, const char *name)
2575
 
{
2576
 
  void *dp;
2577
 
  int dim[3];
2578
 
 
2579
 
  dp = foveon_camf_matrix (dim, name);
2580
 
  if (!dp) return 0;
2581
 
  memcpy (ptr, dp, size*4);
2582
 
  free (dp);
2583
 
  return 1;
2584
 
}
2585
 
 
2586
 
float CLASS foveon_avg (short *pix, int range[2], float cfilt)
2587
 
{
2588
 
  int i;
2589
 
  float val, min=FLT_MAX, max=-FLT_MAX, sum=0;
2590
 
 
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;
2595
 
  }
2596
 
  return (sum - min - max) / (range[1] - range[0] - 1);
2597
 
}
2598
 
 
2599
 
short * CLASS foveon_make_curve (double max, double mul, double filt)
2600
 
{
2601
 
  short *curve;
2602
 
  int i, size;
2603
 
  double x;
2604
 
 
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()");
2609
 
  curve[0] = size;
2610
 
  for (i=0; i < size; i++) {
2611
 
    x = i*filt/max/4;
2612
 
    curve[i+1] = (cos(x)+1)/2 * tanh(i*filt/mul) * mul + 0.5;
2613
 
  }
2614
 
  return curve;
2615
 
}
2616
 
 
2617
 
void CLASS foveon_make_curves
2618
 
        (short **curvep, float dq[3], float div[3], float filt)
2619
 
{
2620
 
  double mul[3], max=0;
2621
 
  int c;
2622
 
 
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);
2626
 
}
2627
 
 
2628
 
int CLASS foveon_apply_curve (short *curve, int i)
2629
 
{
2630
 
  if (abs(i) >= curve[0]) return 0;
2631
 
  return i < 0 ? -curve[1-i] : curve[1+i];
2632
 
}
2633
 
 
2634
 
#define image ((short (*)[4]) image)
2635
 
 
2636
 
void CLASS foveon_interpolate()
2637
 
{
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];
2649
 
  unsigned *badpix;
2650
 
  double dsum=0, trsum[3];
2651
 
  char str[128];
2652
 
  const char* cp;
2653
 
 
2654
 
  if (verbose)
2655
 
    fprintf (stderr,_("Foveon interpolation...\n"));
2656
 
 
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");
2668
 
 
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);
2678
 
    }
2679
 
 
2680
 
  if (!(cp = foveon_camf_param ("WhiteBalanceIlluminants", model2)))
2681
 
  { fprintf (stderr,_("%s: Invalid white balance \"%s\"\n"), ifname, model2);
2682
 
    return; }
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];
2690
 
 
2691
 
  sprintf (str, "%sRGBNeutral", model2);
2692
 
  if (foveon_camf_param ("IncludeBlocks", str))
2693
 
    foveon_fixed (div, 3, str);
2694
 
  else {
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);
2698
 
    #undef LAST
2699
 
    FORC3 div[c] = diag[c][0]*0.3127 + diag[c][1]*0.329 + diag[c][2]*0.3583;
2700
 
  }
2701
 
  num = 0;
2702
 
  FORC3 if (num < div[c]) num = div[c];
2703
 
  FORC3 div[c] /= num;
2704
 
 
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;
2717
 
 
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);
2724
 
 
2725
 
  sgain = (float (*)[3]) foveon_camf_matrix (dim, "SpatialGain");
2726
 
  if (!sgain) return;
2727
 
  sgrow = (float (*)[3]) calloc (dim[1], sizeof *sgrow);
2728
 
  sgx = (width + dim[1]-2) / (dim[1]-1);
2729
 
 
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];
2739
 
  }
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);
2743
 
 
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];
2748
 
      } else
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]);
2753
 
  }
2754
 
  FORC3 black[row][c] = (last[0][c] + last[1][c])/2;
2755
 
  FORC3 black[0][c] = (black[1][c] + black[3][c])/2;
2756
 
 
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];
2767
 
 
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];
2772
 
      total[3]++;
2773
 
    }
2774
 
  for (row=0; row < height; row++)
2775
 
    FORC3 black[row][c] += fsum[c]/2 + total[c]/(total[3]*100.0);
2776
 
 
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--;
2785
 
    frow -= 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++) {
2790
 
      FORC3 {
2791
 
        diff = pix[c] - prev[c];
2792
 
        prev[c] = pix[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)
2795
 
                - black[row][c] );
2796
 
      }
2797
 
      FORC3 {
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;
2801
 
      }
2802
 
      FORC3 {
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;
2810
 
        pix[c] = ipix[c];
2811
 
      }
2812
 
      pix += 4;
2813
 
    }
2814
 
  }
2815
 
  free (black);
2816
 
  free (sgrow);
2817
 
  free (sgain);
2818
 
 
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)
2824
 
        continue;
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];
2830
 
          sum++;
2831
 
        }
2832
 
      if (sum) FORC3 image[row*width+col][c] = fsum[c]/sum;
2833
 
    }
2834
 
    free (badpix);
2835
 
  }
2836
 
 
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;
2842
 
 
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++) {
2850
 
        smrow[4][col][0] =
2851
 
          (pix[0]*6 + (pix[-4]+pix[4])*4 + pix[-8]+pix[8] + 8) >> 4;
2852
 
        pix += 4;
2853
 
      }
2854
 
    }
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;
2860
 
      if (col == 2)
2861
 
        smred_p = smred;
2862
 
      i = pix[0] + ((pix[0] - ((smred*7 + smred_p) >> 3)) >> 3);
2863
 
      if (i > 32000) i = 32000;
2864
 
      pix[0] = i;
2865
 
      smred_p = smred;
2866
 
      pix += 4;
2867
 
    }
2868
 
  }
2869
 
 
2870
 
  /* Adjust the brighter pixels for better linearity */
2871
 
  min = 0xffff;
2872
 
  FORC3 {
2873
 
    i = satlev[c] / div[c];
2874
 
    if (min > i) min = i;
2875
 
  }
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)
2879
 
      continue;
2880
 
    min = max = pix[0];
2881
 
    for (c=1; c < 3; c++) {
2882
 
      if (min > pix[c]) min = pix[c];
2883
 
      if (max < pix[c]) max = pix[c];
2884
 
    }
2885
 
    if (min >= limit*2) {
2886
 
      pix[0] = pix[1] = pix[2] = max;
2887
 
    } else {
2888
 
      i = 0x4000 - ((min - limit) << 14) / limit;
2889
 
      i = 0x4000 - (i*i >> 14);
2890
 
      i = i*i >> 14;
2891
 
      FORC3 pix[c] += (max - pix[c]) * i >> 14;
2892
 
    }
2893
 
  }
2894
 
/*
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.
2898
 
 */
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;
2906
 
        pix += 4;
2907
 
      }
2908
 
    }
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;
2915
 
      pix += 4;
2916
 
    }
2917
 
  }
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;
2926
 
        pix += 4;
2927
 
      }
2928
 
    }
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];
2935
 
        sum += pix[c];
2936
 
      }
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]);
2941
 
      pix += 4;
2942
 
    }
2943
 
  }
2944
 
 
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);
2950
 
    FORC3 {
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;
2956
 
    }
2957
 
    FORC3 pix[c] = ipix[c];
2958
 
  }
2959
 
 
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];
2969
 
      FORC3
2970
 
        if (row+2 > height/4)
2971
 
          shrink[row*(width/4)+col][c] = ipix[c] >> 4;
2972
 
        else
2973
 
          shrink[row*(width/4)+col][c] =
2974
 
            (shrink[(row+1)*(width/4)+col][c]*1840 + ipix[c]*141 + 2048) >> 12;
2975
 
    }
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;
2979
 
    if ((row & 3) == 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;
2983
 
 
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;
2989
 
 
2990
 
  /* Smooth top-to-bottom */
2991
 
    if (row == 0)
2992
 
      memcpy (smrow[2], smrow[1], sizeof **smrow * width);
2993
 
    else
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;
2997
 
 
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];
3003
 
      }
3004
 
      j = (j << 16) / i;
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]);
3008
 
        sum += ipix[c];
3009
 
      }
3010
 
      sum >>= 3;
3011
 
      FORC3 {
3012
 
        i = image[row*width+col][c] + ipix[c] - sum;
3013
 
        if (i < 0) i = 0;
3014
 
        image[row*width+col][c] = i;
3015
 
      }
3016
 
    }
3017
 
  }
3018
 
  free (shrink);
3019
 
  free (smrow[6]);
3020
 
  for (i=0; i < 8; i++)
3021
 
    free (curve[i]);
3022
 
 
3023
 
  /* Trim off the black border */
3024
 
  active[1] -= keep[1];
3025
 
  active[3] -= 2;
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]],
3029
 
         i * sizeof *image);
3030
 
  width = i;
3031
 
  height = row;
3032
 
}
3033
 
#undef image
3034
 
 
3035
 
/* END GPL BLOCK */
3036
 
 
3037
 
/*
3038
 
   Seach from the current directory up to the root looking for
3039
 
   a ".badpixels" file, and fix those pixels now.
3040
 
 */
3041
 
void CLASS bad_pixels()
3042
 
{
3043
 
  FILE *fp=NULL;
3044
 
  char *fname, *cp, line[128];
3045
 
  int len, time, row, col, r, c, rad, tot, n, fixed=0;
3046
 
 
3047
 
  if (!filters) return;
3048
 
  for (len=32 ; ; len *= 2) {
3049
 
    fname = (char *) malloc (len);
3050
 
    if (!fname) return;
3051
 
    if (getcwd (fname, len-16)) break;
3052
 
    free (fname);
3053
 
    if (errno != ERANGE) return;
3054
 
  }
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 = '/';
3060
 
#endif
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 != '/');
3068
 
  }
3069
 
  free (fname);
3070
 
  if (!fp) return;
3071
 
  while (fgets (line, 128, fp)) {
3072
 
    cp = strchr (line, '#');
3073
 
    if (cp) *cp = 0;
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)) {
3082
 
            tot += BAYER2(r,c);
3083
 
            n++;
3084
 
          }
3085
 
    BAYER2(row,col) = tot/n;
3086
 
    if (verbose) {
3087
 
      if (!fixed++)
3088
 
        fprintf (stderr,_("Fixed bad pixels at:"));
3089
 
      fprintf (stderr, " %d,%d", col, row);
3090
 
    }
3091
 
  }
3092
 
  if (fixed) fputc ('\n', stderr);
3093
 
  fclose (fp);
3094
 
}
3095
 
 
3096
 
void CLASS subtract (char *fname)
3097
 
{
3098
 
  FILE *fp;
3099
 
  int dim[3]={0,0,0}, comment=0, number=0, error=0, nd=0, c, row, col;
3100
 
  ushort *pixel;
3101
 
 
3102
 
  if (!(fp = fopen (fname, "rb"))) {
3103
 
    perror (fname);  return;
3104
 
  }
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;
3111
 
    if (number) {
3112
 
      if (isdigit(c)) dim[nd] = dim[nd]*10 + c -'0';
3113
 
      else if (isspace(c)) {
3114
 
        number = 0;  nd++;
3115
 
      } else error = 1;
3116
 
    }
3117
 
  }
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;
3124
 
  }
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);
3131
 
  }
3132
 
  free (pixel);
3133
 
  black = 0;
3134
 
}
3135
 
 
3136
 
void CLASS pseudoinverse (double (*in)[3], double (*out)[3], int size)
3137
 
{
3138
 
  double work[3][6], num;
3139
 
  int i, j, k;
3140
 
 
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];
3147
 
  }
3148
 
  for (i=0; i < 3; i++) {
3149
 
    num = work[i][i];
3150
 
    for (j=0; j < 6; j++)
3151
 
      work[i][j] /= num;
3152
 
    for (k=0; k < 3; k++) {
3153
 
      if (k==i) continue;
3154
 
      num = work[k][i];
3155
 
      for (j=0; j < 6; j++)
3156
 
        work[k][j] -= work[i][j] * num;
3157
 
    }
3158
 
  }
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];
3163
 
}
3164
 
 
3165
 
void CLASS cam_xyz_coeff (double cam_xyz[4][3])
3166
 
{
3167
 
  double cam_rgb[4][3], inverse[4][3], num;
3168
 
  int i, j, k;
3169
 
 
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];
3174
 
 
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;
3181
 
  }
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];
3186
 
}
3187
 
 
3188
 
#ifdef COLORCHECK
3189
 
void CLASS colorcheck()
3190
 
{
3191
 
#define NSQ 24
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];
3248
 
 
3249
 
  memset (gmb_cam, 0, sizeof gmb_cam);
3250
 
  for (sq=0; sq < NSQ; sq++) {
3251
 
    FORCC count[c] = 0;
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++) {
3254
 
        c = FC(row,col);
3255
 
        if (c >= colors) c -= 2;
3256
 
        gmb_cam[sq][c] += BAYER(row,col);
3257
 
        count[c]++;
3258
 
      }
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];
3264
 
  }
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);
3271
 
  if (verbose) {
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));
3276
 
    puts (" } },");
3277
 
  }
3278
 
#undef NSQ
3279
 
}
3280
 
#endif
3281
 
 
3282
 
void CLASS scale_colors()
3283
 
{
3284
 
  int row, col, x, y, c, val, sum[8];
3285
 
  double dsum[8], dmin, dmax;
3286
 
  float scale_mul[4];
3287
 
 
3288
 
  maximum -= black;
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++)
3296
 
            FORC4 {
3297
 
              val = image[y*width+x][c];
3298
 
              if (!val) continue;
3299
 
              val -= black;
3300
 
              if (val > maximum-25) goto skip_block;
3301
 
              if (val < 0) val = 0;
3302
 
              sum[c] += val;
3303
 
              sum[c+4]++;
3304
 
            }
3305
 
        for (c=0; c < 8; c++) dsum[c] += sum[c];
3306
 
skip_block:
3307
 
        continue;
3308
 
      }
3309
 
    FORC4 if (dsum[c]) pre_mul[c] = dsum[c+4] / dsum[c];
3310
 
  }
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++) {
3315
 
        c = FC(row,col);
3316
 
        if ((val = white[row][col] - black) > 0)
3317
 
          sum[c] += val;
3318
 
        sum[c+4]++;
3319
 
      }
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);
3324
 
    else
3325
 
      fprintf (stderr,_("%s: Cannot use camera white balance.\n"), ifname);
3326
 
  }
3327
 
  if (user_mul[0])
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])
3332
 
        dmin = pre_mul[c];
3333
 
    if (dmax < pre_mul[c])
3334
 
        dmax = pre_mul[c];
3335
 
  }
3336
 
  if (!highlight) dmax = dmin;
3337
 
  FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
3338
 
  if (verbose) {
3339
 
    fprintf (stderr,_("Scaling with black %d, multipliers"), black);
3340
 
    FORC4 fprintf (stderr, " %f", pre_mul[c]);
3341
 
    fputc ('\n', stderr);
3342
 
  }
3343
 
  for (row=0; row < height; row++)
3344
 
    for (col=0; col < width; col++)
3345
 
      FORC4 {
3346
 
        val = image[row*width+col][c];
3347
 
        if (!val) continue;
3348
 
        val -= black;
3349
 
        val *= scale_mul[c];
3350
 
        image[row*width+col][c] = CLIP(val);
3351
 
      }
3352
 
  if (filters && colors == 3) {
3353
 
    if (four_color_rgb) {
3354
 
        fprintf(stderr,"dcraw::scale colors using 4colors\n");
3355
 
      colors++;
3356
 
      FORC3 rgb_cam[c][3] = rgb_cam[c][1] /= 2;
3357
 
    } else {
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);
3362
 
    }
3363
 
  }
3364
 
}
3365
 
 
3366
 
void CLASS border_interpolate (int border)
3367
 
{
3368
 
  unsigned row, col, y, x, f, c, sum[8];
3369
 
 
3370
 
  for (row=0; row < height; row++)
3371
 
    for (col=0; col < width; col++) {
3372
 
      if (col==border && row >= border && row < height-border)
3373
 
        col = width-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) {
3378
 
            f = fc(y,x);
3379
 
            sum[f] += image[y*width+x][f];
3380
 
            sum[f+4]++;
3381
 
          }
3382
 
      f = fc(row,col);
3383
 
      FORCC if (c != f && sum[c+4])
3384
 
        image[row*width+col][c] = sum[c] / sum[c+4];
3385
 
    }
3386
 
}
3387
 
 
3388
 
void CLASS lin_interpolate()
3389
 
{
3390
 
  int code[16][16][32], *ip, sum[4];
3391
 
  int c, i, x, y, row, col, shift, color;
3392
 
  ushort *pix;
3393
 
 
3394
 
  if (verbose) fprintf (stderr,_("Bilinear interpolation...\n"));
3395
 
 
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;
3407
 
          *ip++ = shift;
3408
 
          *ip++ = color;
3409
 
          sum[color] += 1 << shift;
3410
 
        }
3411
 
      FORCC
3412
 
        if (c != fc(row,col)) {
3413
 
          *ip++ = c;
3414
 
          *ip++ = sum[c];
3415
 
        }
3416
 
    }
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];
3426
 
    }
3427
 
}
3428
 
 
3429
 
/*
3430
 
   This algorithm is officially called:
3431
 
 
3432
 
   "Interpolation using a Threshold-based variable number of gradients"
3433
 
 
3434
 
   described in http://scien.stanford.edu/class/psych221/projects/99/tingchen/algodep/vargra.html
3435
 
 
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.
3438
 
 */
3439
 
void CLASS vng_interpolate()
3440
 
{
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,
3463
 
    +1,+0,+2,+1,0,0x10
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;
3469
 
 
3470
 
  lin_interpolate();
3471
 
  if (verbose) fprintf (stderr,_("VNG interpolation...\n"));
3472
 
 
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++;
3482
 
        weight = *cp++;
3483
 
        grads = *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;
3490
 
        *ip++ = weight;
3491
 
        for (g=0; g < 8; g++)
3492
 
          if (grads & 1<<g) *ip++ = g;
3493
 
        *ip++ = -1;
3494
 
      }
3495
 
      *ip++ = INT_MAX;
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;
3502
 
        else
3503
 
          *ip++ = 0;
3504
 
      }
3505
 
    }
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;
3518
 
        ip += 5;
3519
 
        if ((g = ip[-1]) == -1) continue;
3520
 
        gval[g] += diff;
3521
 
        while ((g = *ip++) != -1)
3522
 
          gval[g] += diff;
3523
 
      }
3524
 
      ip++;
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];
3529
 
      }
3530
 
      if (gmax == 0) {
3531
 
        memcpy (brow[2][col], pix, sizeof *image);
3532
 
        continue;
3533
 
      }
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) {
3539
 
          FORCC
3540
 
            if (c == color && ip[1])
3541
 
              sum[c] += (pix[c] + pix[ip[1]]) >> 1;
3542
 
            else
3543
 
              sum[c] += pix[ip[0] + c];
3544
 
          num++;
3545
 
        }
3546
 
      }
3547
 
      FORCC {                                   /* Save to buffer */
3548
 
        t = pix[color];
3549
 
        if (c != color)
3550
 
          t += (sum[c] - sum[color]) / num;
3551
 
        brow[2][col][c] = CLIP(t);
3552
 
      }
3553
 
    }
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];
3558
 
  }
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);
3561
 
  free (brow[4]);
3562
 
  free (code[0][0]);
3563
 
}
3564
 
 
3565
 
void CLASS cam_to_cielab (ushort cam[4], float lab[3])
3566
 
{
3567
 
  int c, i, j, k;
3568
 
  float r, xyz[3];
3569
 
  static float cbrt[0x10000], xyz_cam[3][4];
3570
 
 
3571
 
  if (cam == NULL) {
3572
 
    for (i=0; i < 0x10000; i++) {
3573
 
      r = i / 65535.0;
3574
 
      cbrt[i] = r > 0.008856 ? pow(r,1/3.0) : 7.787*r + 16/116.0;
3575
 
    }
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];
3580
 
  } else {
3581
 
    xyz[0] = xyz[1] = xyz[2] = 0.5;
3582
 
    FORCC {
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];
3586
 
    }
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]);
3593
 
  }
3594
 
}
3595
 
 
3596
 
/*
3597
 
   Adaptive Homogeneity-Directed interpolation is based on
3598
 
   the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
3599
 
 */
3600
 
#define TS 256          /* Tile Size */
3601
 
 
3602
 
void CLASS ahd_interpolate()
3603
 
{
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;
3608
 
  float flab[3];
3609
 
  ushort (*rgb)[TS][TS][3];
3610
 
   short (*lab)[TS][TS][3];
3611
 
   char (*homo)[TS][TS], *buffer;
3612
 
 
3613
 
  if (verbose) fprintf (stderr,_("AHD interpolation...\n"));
3614
 
 
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);
3621
 
 
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);
3625
 
 
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]);
3638
 
        }
3639
 
      }
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) {
3647
 
              c = FC(row+1,col);
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);
3653
 
            } else
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);
3659
 
            c = FC(row,col);
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];
3663
 
          }
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++) {
3667
 
        tr = row-top;
3668
 
        for (col=left+2; col < left+TS-2 && col < width; col++) {
3669
 
          tc = col-left;
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)
3685
 
                homo[d][tr][tc]++;
3686
 
        }
3687
 
      }
3688
 
/*  Combine the most homogenous pixels for the final result:    */
3689
 
      for (row=top+3; row < top+TS-3 && row < height-3; row++) {
3690
 
        tr = row-top;
3691
 
        for (col=left+3; col < left+TS-3 && col < width-3; col++) {
3692
 
          tc = col-left;
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];
3697
 
          if (hm[0] != hm[1])
3698
 
            FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
3699
 
          else
3700
 
            FORC3 image[row*width+col][c] =
3701
 
                (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
3702
 
        }
3703
 
      }
3704
 
    }
3705
 
  free (buffer);
3706
 
}
3707
 
#undef TS
3708
 
 
3709
 
/*
3710
 
   Bilateral Filtering was developed by C. Tomasi and R. Manduchi.
3711
 
 */
3712
 
void CLASS bilateral_filter()
3713
 
{
3714
 
  float (**window)[7], *kernel, scale_r, elut[1024], sum[5];
3715
 
  int c, i, wr, ws, wlast, row, col, y, x;
3716
 
  unsigned sep;
3717
 
 
3718
 
  if (verbose) fprintf (stderr,_("Bilateral filtering...\n"));
3719
 
 
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);
3733
 
 
3734
 
  for (wlast=-1, row=0; row < height; row++) {
3735
 
    while (wlast < row+wr) {
3736
 
      wlast++;
3737
 
      for (i=0; i <= ws; i++)   /* rotate window rows */
3738
 
        window[(ws+i) % (ws+1)] = window[i];
3739
 
      if (wlast < height)
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);
3743
 
        }
3744
 
    }
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];
3755
 
              if (sep < 1024) {
3756
 
                FORCC sum[c] += elut[sep] * window[wr+y][col+x][c];
3757
 
                sum[4] += elut[sep];
3758
 
              }
3759
 
            }
3760
 
      FORCC image[row*width+col][c] = sum[c]/sum[4];
3761
 
    }
3762
 
  }
3763
 
  free (window);
3764
 
}
3765
 
 
3766
 
#define SCALE (4 >> shrink)
3767
 
void CLASS recover_highlights()
3768
 
{
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;
3772
 
  ushort *pixel;
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} };
3775
 
 
3776
 
  if (verbose) fprintf (stderr,_("Highlight recovery...\n"));
3777
 
 
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) {
3795
 
              sum += pixel[c];
3796
 
              wgt += pixel[kc];
3797
 
              count++;
3798
 
            }
3799
 
          }
3800
 
        if (count == SCALE*SCALE)
3801
 
          map[mrow*wide+mcol] = sum / wgt;
3802
 
      }
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;
3807
 
          sum = count = 0;
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);
3814
 
            }
3815
 
          }
3816
 
          if (count > 3)
3817
 
            map[mrow*wide+mcol] = - (sum+grow) / (count+grow);
3818
 
        }
3819
 
      for (change=i=0; i < high*wide; i++)
3820
 
        if (map[i] < 0) {
3821
 
          map[i] = -map[i];
3822
 
          change = 1;
3823
 
        }
3824
 
      if (!change) break;
3825
 
    }
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);
3836
 
            }
3837
 
          }
3838
 
      }
3839
 
  }
3840
 
  free (map);
3841
 
}
3842
 
#undef SCALE
3843
 
 
3844
 
void CLASS tiff_get (unsigned base,
3845
 
        unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
3846
 
{
3847
 
  *tag  = get2();
3848
 
  *type = get2();
3849
 
  *len  = get4();
3850
 
  *save = ftell(ifp) + 4;
3851
 
  if (*len * ("1112481124848"[*type < 13 ? *type:0]-'0') > 4)
3852
 
    fseek (ifp, get4()+base, SEEK_SET);
3853
 
}
3854
 
 
3855
 
void CLASS parse_thumb_note (int base, unsigned toff, unsigned tlen)
3856
 
{
3857
 
  unsigned entries, tag, type, len, save;
3858
 
 
3859
 
  entries = get2();
3860
 
  while (entries--) {
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);
3865
 
  }
3866
 
}
3867
 
 
3868
 
void CLASS parse_makernote (int base)
3869
 
{
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;
3906
 
  short sorder;
3907
 
  char buf[10];
3908
 
/*
3909
 
   The MakerNote might have its own TIFF header (possibly with
3910
 
   its own byte-order!), or it might just be a table.
3911
 
 */
3912
 
  sorder = order;
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 */
3920
 
    order = 0x4d4d;
3921
 
    while ((i=ftell(ifp)) < data_offset && i < 16384) {
3922
 
      wb[0] = wb[2];  wb[2] = wb[1];  wb[1] = wb[3];
3923
 
      wb[3] = get2();
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];
3927
 
    }
3928
 
    goto quit;
3929
 
  }
3930
 
  if (!strcmp (buf,"Nikon")) {
3931
 
    base = ftell(ifp);
3932
 
    order = get2();
3933
 
    if (get2() != 42) goto quit;
3934
 
    offset = get4();
3935
 
    fseek (ifp, offset-8, SEEK_CUR);
3936
 
  } else if (!strncmp (buf,"FUJIFILM",8) ||
3937
 
             !strncmp (buf,"SONY",4) ||
3938
 
             !strcmp  (buf,"Panasonic")) {
3939
 
    order = 0x4949;
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);
3950
 
 
3951
 
  entries = get2();
3952
 
  if (entries > 1000) return;
3953
 
  while (entries--) {
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);
3961
 
    }
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();
3967
 
    }
3968
 
    if (tag == 0x10 && type == 4)
3969
 
      unique_id = get4();
3970
 
    if (tag == 0x14 && len == 2560 && type == 7) {
3971
 
      fseek (ifp, 1248, SEEK_CUR);
3972
 
      goto get2_256;
3973
 
    }
3974
 
    if (strstr(make,"PENTAX")) {
3975
 
      if (tag == 0x1b) tag = 0x1018;
3976
 
      if (tag == 0x1c) tag = 0x1017;
3977
 
    }
3978
 
    if (tag == 0x1d)
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;
3985
 
      raw_width  = get2();
3986
 
      filters = 0x61616161;
3987
 
    }
3988
 
    if ((tag == 0x81  && type == 7) ||
3989
 
        (tag == 0x100 && type == 7) ||
3990
 
        (tag == 0x280 && type == 1)) {
3991
 
      thumb_offset = ftell(ifp);
3992
 
      thumb_length = len;
3993
 
    }
3994
 
    if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
3995
 
      thumb_offset += base;
3996
 
    if (tag == 0x89 && type == 4)
3997
 
      thumb_length = get4();
3998
 
    if (tag == 0x8c)
3999
 
      curve_offset = ftell(ifp) + 2112;
4000
 
    if (tag == 0x96)
4001
 
      curve_offset = ftell(ifp) + 2;
4002
 
    if (tag == 0x97) {
4003
 
      for (i=0; i < 4; i++)
4004
 
        ver97 = (ver97 << 4) + fgetc(ifp)-'0';
4005
 
      switch (ver97) {
4006
 
        case 0x100:
4007
 
          fseek (ifp, 68, SEEK_CUR);
4008
 
          FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
4009
 
          break;
4010
 
        case 0x102:
4011
 
          fseek (ifp, 6, SEEK_CUR);
4012
 
          goto get2_rggb;
4013
 
        case 0x103:
4014
 
          fseek (ifp, 16, SEEK_CUR);
4015
 
          FORC4 cam_mul[c] = get2();
4016
 
      }
4017
 
      if (ver97 >> 8 == 2) {
4018
 
        if (ver97 != 0x205) fseek (ifp, 280, SEEK_CUR);
4019
 
        fread (buf97, 324, 1, ifp);
4020
 
      }
4021
 
    }
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)];
4025
 
      ck = 0x60;
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);
4030
 
    }
4031
 
    if (tag == 0x200 && len == 4)
4032
 
      black = (get2()+get2()+get2()+get2())/4;
4033
 
    if (tag == 0x201 && len == 4)
4034
 
      goto get2_rggb;
4035
 
    if (tag == 0x401 && len == 4) {
4036
 
      black = (get4()+get4()+get4()+get4())/4;
4037
 
    }
4038
 
    if (tag == 0xe01) {         /* Nikon Capture Note */
4039
 
      type = order;
4040
 
      order = 0x4949;
4041
 
      fseek (ifp, 22, SEEK_CUR);
4042
 
      for (offset=22; offset+22 < len; offset += 22+i) {
4043
 
        tag = get4();
4044
 
        fseek (ifp, 14, SEEK_CUR);
4045
 
        i = get4()-4;
4046
 
        if (tag == 0x76a43207) flip = get2();
4047
 
        else fseek (ifp, i, SEEK_CUR);
4048
 
      }
4049
 
      order = type;
4050
 
    }
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;
4055
 
    }
4056
 
    if (tag == 0xf00 && type == 7) {
4057
 
      if (len == 614)
4058
 
        fseek (ifp, 176, SEEK_CUR);
4059
 
      else if (len == 734 || len == 1502)
4060
 
        fseek (ifp, 148, SEEK_CUR);
4061
 
      else goto next;
4062
 
      goto get2_256;
4063
 
    }
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;
4068
 
    }
4069
 
    if (tag == 0x1012 && len == 4)
4070
 
      for (black = i=0; i < 4; i++)
4071
 
        black += get2() << 2;
4072
 
    if (tag == 0x1017)
4073
 
      cam_mul[0] = get2() / 256.0;
4074
 
    if (tag == 0x1018)
4075
 
      cam_mul[2] = get2() / 256.0;
4076
 
    if (tag == 0x2011 && len == 2) {
4077
 
get2_256:
4078
 
      order = 0x4d4d;
4079
 
      cam_mul[0] = get2() / 256.0;
4080
 
      cam_mul[2] = get2() / 256.0;
4081
 
    }
4082
 
    if (tag == 0x2020)
4083
 
      parse_thumb_note (base, 257, 258);
4084
 
    if (tag == 0xb028) {
4085
 
      fseek (ifp, get4(), SEEK_SET);
4086
 
      parse_thumb_note (base, 136, 137);
4087
 
    }
4088
 
    if (tag == 0x4001) {
4089
 
      i = len == 582 ? 50 : len == 653 ? 68 : len == 796 ? 126 : 0;
4090
 
      fseek (ifp, i, SEEK_CUR);
4091
 
get2_rggb:
4092
 
      FORC4 cam_mul[c ^ (c >> 1)] = get2();
4093
 
    }
4094
 
next:
4095
 
    fseek (ifp, save, SEEK_SET);
4096
 
  }
4097
 
quit:
4098
 
  order = sorder;
4099
 
}
4100
 
 
4101
 
/*
4102
 
   Since the TIFF DateTime string has no timezone information,
4103
 
   assume that the camera's clock was set to Universal Time.
4104
 
 */
4105
 
void CLASS get_timestamp (int reversed)
4106
 
{
4107
 
  struct tm t;
4108
 
  char str[20];
4109
 
  int i;
4110
 
 
4111
 
  if (timestamp) return;
4112
 
  str[19] = 0;
4113
 
  if (reversed)
4114
 
    for (i=19; i--; ) str[i] = fgetc(ifp);
4115
 
  else
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)
4120
 
    return;
4121
 
  t.tm_year -= 1900;
4122
 
  t.tm_mon -= 1;
4123
 
  if (mktime(&t) > 0)
4124
 
    timestamp = mktime(&t);
4125
 
}
4126
 
 
4127
 
void CLASS parse_exif (int base)
4128
 
{
4129
 
  unsigned kodak, entries, tag, type, len, save;
4130
 
  double expo;
4131
 
 
4132
 
  kodak = !strncmp(make,"EASTMAN",7);
4133
 
  entries = get2();
4134
 
  while (entries--) {
4135
 
    tiff_get (base, &tag, &type, &len, &save);
4136
 
    switch (tag) {
4137
 
      case 33434:  shutter = getrat();                  break;
4138
 
      case 33437:  aperture = getrat();                 break;
4139
 
      case 34855:  iso_speed = get2();                  break;
4140
 
      case 36867:
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;
4149
 
    }
4150
 
    fseek (ifp, save, SEEK_SET);
4151
 
  }
4152
 
}
4153
 
 
4154
 
void CLASS romm_coeff (float romm_cam[3][3])
4155
 
{
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 } };
4160
 
  int i, j, k;
4161
 
 
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];
4166
 
}
4167
 
 
4168
 
void CLASS parse_mos (int offset)
4169
 
{
4170
 
  char data[40];
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];
4176
 
 
4177
 
  fseek (ifp, offset, SEEK_SET);
4178
 
  while (1) {
4179
 
    if (get4() != 0x504b5453) break;
4180
 
    get4();
4181
 
    fread (data, 1, 40, ifp);
4182
 
    skip = get4();
4183
 
    from = ftell(ifp);
4184
 
    if (!strcmp(data,"JPEG_preview_data")) {
4185
 
      thumb_offset = from;
4186
 
      thumb_length = skip;
4187
 
    }
4188
 
    if (!strcmp(data,"icc_camera_profile")) {
4189
 
      profile_offset = from;
4190
 
      profile_length = skip;
4191
 
    }
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]);
4196
 
    }
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);
4201
 
    }
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"))
4207
 
      FORC4 {
4208
 
        fscanf (ifp, "%d", &i);
4209
 
        if (i == 1) frot = c ^ (c >> 1);
4210
 
      }
4211
 
    if (!strcmp(data,"ImgProf_rotation_angle")) {
4212
 
      fscanf (ifp, "%d", &i);
4213
 
      flip = i - flip;
4214
 
    }
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];
4218
 
    }
4219
 
    parse_mos (from);
4220
 
    fseek (ifp, skip+from, SEEK_SET);
4221
 
  }
4222
 
  if (planes)
4223
 
    filters = (planes == 1) * 0x01010101 *
4224
 
        (uchar) "\x94\x61\x16\x49"[(flip/90 + frot) & 3];
4225
 
}
4226
 
 
4227
 
void CLASS linear_table (unsigned len)
4228
 
{
4229
 
  int i;
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];
4235
 
}
4236
 
 
4237
 
void CLASS parse_kodak_ifd (int base)
4238
 
{
4239
 
  unsigned entries, tag, type, len, save;
4240
 
  int i, c, wbi=-2, wbtemp=6500;
4241
 
  float mul[3], num;
4242
 
 
4243
 
  entries = get2();
4244
 
  if (entries > 1024) return;
4245
 
  while (entries--) {
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();
4251
 
      wbi = -2;
4252
 
    }
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)
4257
 
      FORC3 {
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]);
4261
 
      }
4262
 
    if (tag == 2317) linear_table (len);
4263
 
    if (tag == 6020) iso_speed = getint(type);
4264
 
    fseek (ifp, save, SEEK_SET);
4265
 
  }
4266
 
}
4267
 
 
4268
 
void CLASS parse_minolta (int base);
4269
 
 
4270
 
int CLASS parse_tiff_ifd (int base, int level)
4271
 
{
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;
4279
 
  struct jhead jh;
4280
 
  FILE *sfp;
4281
 
 
4282
 
  if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
4283
 
    return 1;
4284
 
  ifd = tiff_nifds++;
4285
 
  for (j=0; j < 4; j++)
4286
 
    for (i=0; i < 4; i++)
4287
 
      cc[j][i] = i == j;
4288
 
  entries = get2();
4289
 
  if (entries > 512) return 1;
4290
 
  while (entries--) {
4291
 
    tiff_get (base, &tag, &type, &len, &save);
4292
 
    switch (tag) {
4293
 
      case 17: case 18:
4294
 
        if (type == 3 && len == 1)
4295
 
          cam_mul[(tag-17)*2] = get2() / 256.0;
4296
 
        break;
4297
 
      case 23:
4298
 
        if (type == 3) iso_speed = get2();
4299
 
        break;
4300
 
      case 36: case 37: case 38:
4301
 
        cam_mul[tag-0x24] = get2();
4302
 
        break;
4303
 
      case 39:
4304
 
        if (len < 50 || cam_mul[0]) break;
4305
 
        fseek (ifp, 12, SEEK_CUR);
4306
 
        FORC3 cam_mul[c] = get2();
4307
 
        break;
4308
 
      case 2: case 256:                 /* ImageWidth */
4309
 
        tiff_ifd[ifd].width = getint(type);
4310
 
        break;
4311
 
      case 3: case 257:                 /* ImageHeight */
4312
 
        tiff_ifd[ifd].height = getint(type);
4313
 
        break;
4314
 
      case 258:                         /* BitsPerSample */
4315
 
        tiff_ifd[ifd].samples = len;
4316
 
        tiff_ifd[ifd].bps = get2();
4317
 
        break;
4318
 
      case 259:                         /* Compression */
4319
 
        tiff_ifd[ifd].comp = get2();
4320
 
        break;
4321
 
      case 262:                         /* PhotometricInterpretation */
4322
 
        tiff_ifd[ifd].phint = get2();
4323
 
        break;
4324
 
      case 271:                         /* Make */
4325
 
        fgets (make, 64, ifp);
4326
 
        break;
4327
 
      case 272:                         /* Model */
4328
 
        fgets (model, 64, ifp);
4329
 
        break;
4330
 
      case 273:                         /* StripOffset */
4331
 
      case 513:
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;
4341
 
          }
4342
 
        }
4343
 
        break;
4344
 
      case 274:                         /* Orientation */
4345
 
        tiff_ifd[ifd].flip = "50132467"[get2() & 7]-'0';
4346
 
        break;
4347
 
      case 277:                         /* SamplesPerPixel */
4348
 
        tiff_ifd[ifd].samples = getint(type);
4349
 
        break;
4350
 
      case 279:                         /* StripByteCounts */
4351
 
      case 514:
4352
 
        tiff_ifd[ifd].bytes = get4();
4353
 
        break;
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"))
4361
 
          is_raw = 0;
4362
 
        break;
4363
 
      case 306:                         /* DateTime */
4364
 
        get_timestamp(0);
4365
 
        break;
4366
 
      case 323:                         /* TileLength */
4367
 
        tile_length = getint(type);
4368
 
        break;
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;
4372
 
        break;
4373
 
      case 330:                         /* SubIFDs */
4374
 
        if (!strcmp(model,"DSLR-A100") && tiff_ifd[ifd].width == 3872) {
4375
 
          data_offset = get4()+base;
4376
 
          ifd++;  break;
4377
 
        }
4378
 
        while (len--) {
4379
 
          i = ftell(ifp);
4380
 
          fseek (ifp, get4()+base, SEEK_SET);
4381
 
          if (parse_tiff_ifd (base, level+1)) break;
4382
 
          fseek (ifp, i+4, SEEK_SET);
4383
 
        }
4384
 
        break;
4385
 
      case 400:
4386
 
        strcpy (make, "Sarnoff");
4387
 
        maximum = 0xfff;
4388
 
        break;
4389
 
      case 29184: sony_offset = get4();  break;
4390
 
      case 29185: sony_length = get4();  break;
4391
 
      case 29217: sony_key    = get4();  break;
4392
 
      case 29443:
4393
 
        FORC4 cam_mul[c ^ (c < 2)] = get2();
4394
 
        break;
4395
 
      case 33405:                       /* Model2 */
4396
 
        fgets (model2, 64, ifp);
4397
 
        break;
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];
4405
 
        }
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 */
4408
 
        goto guess_cfa_pc;
4409
 
      case 33424:
4410
 
        fseek (ifp, get4()+base, SEEK_SET);
4411
 
        parse_kodak_ifd (base);
4412
 
        break;
4413
 
      case 33434:                       /* ExposureTime */
4414
 
        shutter = getrat();
4415
 
        break;
4416
 
      case 33437:                       /* FNumber */
4417
 
        aperture = getrat();
4418
 
        break;
4419
 
      case 34306:                       /* Leaf white balance */
4420
 
        FORC4 cam_mul[c ^ 1] = 4096.0 / get2();
4421
 
        break;
4422
 
      case 34307:                       /* Leaf CatchLight color matrix */
4423
 
        fread (software, 1, 7, ifp);
4424
 
        if (strncmp(software,"MATRIX",6)) break;
4425
 
        colors = 4;
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;
4429
 
          num = 0;
4430
 
          FORC4 num += rgb_cam[i][c];
4431
 
          FORC4 rgb_cam[i][c] /= num;
4432
 
        }
4433
 
        break;
4434
 
      case 34310:                       /* Leaf metadata */
4435
 
        parse_mos (ftell(ifp));
4436
 
      case 34303:
4437
 
        strcpy (make, "Leaf");
4438
 
        break;
4439
 
      case 34665:                       /* EXIF tag */
4440
 
        fseek (ifp, get4()+base, SEEK_SET);
4441
 
        parse_exif (base);
4442
 
        break;
4443
 
      case 34675:                       /* InterColorProfile */
4444
 
      case 50831:                       /* AsShotICCProfile */
4445
 
        profile_offset = ftell(ifp);
4446
 
        profile_length = len;
4447
 
        break;
4448
 
      case 37122:                       /* CompressedBitsPerPixel */
4449
 
        kodak_cbpp = get4();
4450
 
        break;
4451
 
      case 37386:                       /* FocalLength */
4452
 
        focal_len = getrat();
4453
 
        break;
4454
 
      case 37393:                       /* ImageNumber */
4455
 
        shot_order = getint(type);
4456
 
        break;
4457
 
      case 37400:                       /* old Kodak KDC tag */
4458
 
        for (raw_color = i=0; i < 3; i++) {
4459
 
          getrat();
4460
 
          FORC3 rgb_cam[i][c] = getrat();
4461
 
        }
4462
 
        break;
4463
 
      case 46275:                       /* Imacon tags */
4464
 
        strcpy (make, "Imacon");
4465
 
        data_offset = ftell(ifp);
4466
 
        ima_len = len;
4467
 
        break;
4468
 
      case 46279:
4469
 
        fseek (ifp, 78, SEEK_CUR);
4470
 
        raw_width  = get4();
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);
4482
 
          filters = flip = 0;
4483
 
        }
4484
 
        break;
4485
 
      case 50454:                       /* Sinar tag */
4486
 
      case 50455:
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);
4492
 
        free (cbuf);
4493
 
        break;
4494
 
      case 50706:                       /* DNGVersion */
4495
 
        FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
4496
 
        break;
4497
 
      case 50710:                       /* CFAPlaneColor */
4498
 
        if (len > 4) len = 4;
4499
 
        colors = len;
4500
 
        fread (cfa_pc, 1, colors, ifp);
4501
 
guess_cfa_pc:
4502
 
        FORCC tab[cfa_pc[c]] = c;
4503
 
        cdesc[c] = 0;
4504
 
        for (i=16; i--; )
4505
 
          filters = filters << 2 | tab[cfa_pat[i % plen]];
4506
 
        break;
4507
 
      case 50711:                       /* CFALayout */
4508
 
        if (get2() == 2) {
4509
 
          fuji_width = 1;
4510
 
          filters = 0x49494949;
4511
 
        }
4512
 
        break;
4513
 
      case 291:
4514
 
      case 50712:                       /* LinearizationTable */
4515
 
        linear_table (len);
4516
 
        break;
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;
4523
 
        break;
4524
 
      case 50717:                       /* WhiteLevel */
4525
 
        maximum = getint(type);
4526
 
        break;
4527
 
      case 50718:                       /* DefaultScale */
4528
 
        pixel_aspect  = getrat();
4529
 
        pixel_aspect /= getrat();
4530
 
        break;
4531
 
      case 50721:                       /* ColorMatrix1 */
4532
 
      case 50722:                       /* ColorMatrix2 */
4533
 
        FORCC for (j=0; j < 3; j++)
4534
 
          cm[c][j] = getrat();
4535
 
        use_cm = 1;
4536
 
        break;
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();
4543
 
        break;
4544
 
      case 50728:                       /* AsShotNeutral */
4545
 
        FORCC asn[c] = getreal(type);
4546
 
        break;
4547
 
      case 50729:                       /* AsShotWhiteXY */
4548
 
        xyz[0] = getrat();
4549
 
        xyz[1] = getrat();
4550
 
        xyz[2] = 1 - xyz[0] - xyz[1];
4551
 
        FORC3 xyz[c] /= d65_white[c];
4552
 
        break;
4553
 
      case 50740:                       /* DNGPrivateData */
4554
 
        if (dng_version) break;
4555
 
        i = order;
4556
 
        parse_minolta (j = get4()+base);
4557
 
        order = i;
4558
 
        fseek (ifp, j, SEEK_SET);
4559
 
        parse_tiff_ifd (base, level+1);
4560
 
        break;
4561
 
      case 50752:
4562
 
        read_shorts (cr2_slice, 3);
4563
 
        break;
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;
4569
 
        break;
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;
4576
 
    }
4577
 
    fseek (ifp, save, SEEK_SET);
4578
 
  }
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);
4583
 
    sfp = ifp;
4584
 
    if ((ifp = tmpfile())) {
4585
 
      fwrite (buf, sony_length, 1, ifp);
4586
 
      fseek (ifp, 0, SEEK_SET);
4587
 
      parse_tiff_ifd (-sony_offset, level);
4588
 
      fclose (ifp);
4589
 
    }
4590
 
    ifp = sfp;
4591
 
    free (buf);
4592
 
  }
4593
 
  for (i=0; i < colors; i++)
4594
 
    FORCC cc[i][c] *= ab[i];
4595
 
  if (use_cm) {
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);
4600
 
  }
4601
 
  if (asn[0])
4602
 
    FORCC pre_mul[c] = 1 / asn[c];
4603
 
  if (!use_cm)
4604
 
    FORCC pre_mul[c] /= cc[c][c];
4605
 
  return 0;
4606
 
}
4607
 
 
4608
 
void CLASS parse_tiff (int base)
4609
 
{
4610
 
  int doff, max_samp=0, raw=-1, thm=-1, i;
4611
 
  struct jhead jh;
4612
 
 
4613
 
  fseek (ifp, base, SEEK_SET);
4614
 
  order = get2();
4615
 
  if (order != 0x4949 && order != 0x4d4d) return;
4616
 
  get2();
4617
 
  memset (tiff_ifd, 0, sizeof tiff_ifd);
4618
 
  tiff_nifds = 0;
4619
 
  while ((doff = get4())) {
4620
 
    fseek (ifp, doff+base, SEEK_SET);
4621
 
    if (parse_tiff_ifd (base, 0)) break;
4622
 
  }
4623
 
  thumb_misc = 16;
4624
 
  if (thumb_offset) {
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;
4630
 
    }
4631
 
  }
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;
4645
 
      raw = i;
4646
 
    }
4647
 
  }
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) {
4652
 
      case 0:  case 1:
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;
4657
 
        break;
4658
 
      case 6:  case 7:  case 99:
4659
 
        load_raw = &CLASS lossless_jpeg_load_raw;               break;
4660
 
      case 262:
4661
 
        load_raw = &CLASS kodak_262_load_raw;                   break;
4662
 
      case 32773:
4663
 
        load_raw = &CLASS packed_12_load_raw;                   break;
4664
 
      case 65535:
4665
 
        load_raw = &CLASS pentax_k10_load_raw;                  break;
4666
 
      case 65000:
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;
4671
 
        }
4672
 
    }
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;
4684
 
      thm = i;
4685
 
    }
4686
 
  if (thm >= 0) {
4687
 
    thumb_misc |= tiff_ifd[thm].samples << 5;
4688
 
    switch (tiff_ifd[thm].comp) {
4689
 
      case 0:
4690
 
        write_thumb = &CLASS layer_thumb;
4691
 
        break;
4692
 
      case 1:
4693
 
        if (tiff_ifd[thm].bps > 8)
4694
 
          thumb_load_raw = &CLASS kodak_thumb_load_raw;
4695
 
        else
4696
 
          write_thumb = &CLASS ppm_thumb;
4697
 
        break;
4698
 
      case 65000:
4699
 
        thumb_load_raw = tiff_ifd[thm].phint == 6 ?
4700
 
                &CLASS kodak_ycbcr_load_raw : &CLASS kodak_rgb_load_raw;
4701
 
    }
4702
 
  }
4703
 
}
4704
 
 
4705
 
void CLASS parse_minolta (int base)
4706
 
{
4707
 
  int save, tag, len, offset, high=0, wide=0, i, c;
4708
 
 
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);
4716
 
    len = get4();
4717
 
    switch (tag) {
4718
 
      case 0x505244:                            /* PRD */
4719
 
        fseek (ifp, 8, SEEK_CUR);
4720
 
        high = get2();
4721
 
        wide = get2();
4722
 
        break;
4723
 
      case 0x574247:                            /* WBG */
4724
 
        get4();
4725
 
        i = strstr(model,"A200") ? 3:0;
4726
 
        FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
4727
 
        break;
4728
 
      case 0x545457:                            /* TTW */
4729
 
        parse_tiff (ftell(ifp));
4730
 
        data_offset = offset;
4731
 
    }
4732
 
    fseek (ifp, save+len+8, SEEK_SET);
4733
 
  }
4734
 
  raw_height = high;
4735
 
  raw_width  = wide;
4736
 
}
4737
 
 
4738
 
/*
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.
4742
 
 */
4743
 
void CLASS parse_external_jpeg()
4744
 
{
4745
 
  char *file, *ext, *jname, *jfile, *jext;
4746
 
  FILE *save=ifp;
4747
 
 
4748
 
  ext  = strrchr (ifname, '.');
4749
 
  file = strrchr (ifname, '/');
4750
 
  if (!file) file = strrchr (ifname, '\\');
4751
 
  if (!file) file = ifname-1;
4752
 
  file++;
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);
4763
 
  } else
4764
 
    while (isdigit(*--jext)) {
4765
 
      if (*jext != '9') {
4766
 
        (*jext)++;
4767
 
        break;
4768
 
      }
4769
 
      *jext = '0';
4770
 
    }
4771
 
  if (strcmp (jname, ifname)) {
4772
 
    if ((ifp = fopen (jname, "rb"))) {
4773
 
      if (verbose)
4774
 
        fprintf (stderr,_("Reading metadata from %s ...\n"), jname);
4775
 
      parse_tiff (12);
4776
 
      thumb_offset = 0;
4777
 
      is_raw = 1;
4778
 
      fclose (ifp);
4779
 
    }
4780
 
  }
4781
 
  if (!timestamp)
4782
 
    fprintf (stderr,_("Failed to read metadata from %s\n"), jname);
4783
 
  free (jname);
4784
 
  ifp = save;
4785
 
}
4786
 
 
4787
 
/*
4788
 
   CIFF block 0x1030 contains an 8x8 white sample.
4789
 
   Load this into white[][] for use in scale_colors().
4790
 
 */
4791
 
void CLASS ciff_block_1030()
4792
 
{
4793
 
  static const ushort key[] = { 0x410, 0x45f3 };
4794
 
  int i, bpp, row, col, vbits=0;
4795
 
  unsigned long bitbuf=0;
4796
 
 
4797
 
  if ((get2(),get4()) != 0x80008 || !get4()) return;
4798
 
  bpp = get2();
4799
 
  if (bpp != 10 && bpp != 12) return;
4800
 
  for (i=row=0; row < 8; row++)
4801
 
    for (col=0; col < 8; col++) {
4802
 
      if (vbits < bpp) {
4803
 
        bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
4804
 
        vbits += 16;
4805
 
      }
4806
 
      white[row][col] =
4807
 
        bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - bpp);
4808
 
      vbits -= bpp;
4809
 
    }
4810
 
}
4811
 
 
4812
 
/*
4813
 
   Parse a CIFF file, better known as Canon CRW format.
4814
 
 */
4815
 
void CLASS parse_ciff (int offset, int length)
4816
 
{
4817
 
  int tboff, nrecs, c, type, len, save, wbi=-1;
4818
 
  ushort key[] = { 0x410, 0x45f3 };
4819
 
 
4820
 
  fseek (ifp, offset+length-4, SEEK_SET);
4821
 
  tboff = get4() + offset;
4822
 
  fseek (ifp, tboff, SEEK_SET);
4823
 
  nrecs = get2();
4824
 
  if (nrecs > 100) return;
4825
 
  while (nrecs--) {
4826
 
    type = get2();
4827
 
    len  = get4();
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 */
4832
 
 
4833
 
    if (type == 0x080a) {
4834
 
      fread (make, 64, 1, ifp);
4835
 
      fseek (ifp, strlen(make) - 63, SEEK_CUR);
4836
 
      fread (model, 64, 1, ifp);
4837
 
    }
4838
 
    if (type == 0x1810) {
4839
 
      fseek (ifp, 12, SEEK_CUR);
4840
 
      flip = get4();
4841
 
    }
4842
 
    if (type == 0x1835)                 /* Get the decoder table */
4843
 
      tiff_compress = get4();
4844
 
    if (type == 0x2007) {
4845
 
      thumb_offset = ftell(ifp);
4846
 
      thumb_length = len;
4847
 
    }
4848
 
    if (type == 0x1818) {
4849
 
      shutter = pow (2, -int_to_float((get4(),get4())));
4850
 
      aperture = pow (2, int_to_float(get4())/2);
4851
 
    }
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;
4860
 
    }
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();
4868
 
      }
4869
 
    }
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;
4882
 
        }
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;
4886
 
      }
4887
 
    }
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();
4892
 
    }
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();
4898
 
    }
4899
 
    if (type == 0x5029) {
4900
 
      focal_len = len >> 16;
4901
 
      if ((len & 0xffff) == 2) focal_len /= 32;
4902
 
    }
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();
4909
 
#ifdef LOCALTIME
4910
 
    if ((type | 0x4000) == 0x580e)
4911
 
      timestamp = mktime (gmtime (&timestamp));
4912
 
#endif
4913
 
    fseek (ifp, save, SEEK_SET);
4914
 
  }
4915
 
}
4916
 
 
4917
 
void CLASS parse_rollei()
4918
 
{
4919
 
  char line[128], *val;
4920
 
  struct tm t;
4921
 
 
4922
 
  fseek (ifp, 0, SEEK_SET);
4923
 
  memset (&t, 0, sizeof t);
4924
 
  do {
4925
 
    fgets (line, 128, ifp);
4926
 
    if ((val = strchr(line,'=')))
4927
 
      *val++ = 0;
4928
 
    else
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;
4946
 
  t.tm_year -= 1900;
4947
 
  t.tm_mon -= 1;
4948
 
  if (mktime(&t) > 0)
4949
 
    timestamp = mktime(&t);
4950
 
  strcpy (make, "Rollei");
4951
 
  strcpy (model,"d530flex");
4952
 
  write_thumb = &CLASS rollei_thumb;
4953
 
}
4954
 
 
4955
 
void CLASS parse_sinar_ia()
4956
 
{
4957
 
  int entries, off;
4958
 
  char str[8], *cp;
4959
 
 
4960
 
  order = 0x4949;
4961
 
  fseek (ifp, 4, SEEK_SET);
4962
 
  entries = get4();
4963
 
  fseek (ifp, get4(), SEEK_SET);
4964
 
  while (entries--) {
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;
4970
 
  }
4971
 
  fseek (ifp, meta_offset+20, SEEK_SET);
4972
 
  fread (make, 64, 1, ifp);
4973
 
  make[63] = 0;
4974
 
  if ((cp = strchr(make,' '))) {
4975
 
    strcpy (model, cp+1);
4976
 
    *cp = 0;
4977
 
  }
4978
 
  raw_width  = get2();
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;
4984
 
  maximum = 0x3fff;
4985
 
}
4986
 
 
4987
 
void CLASS parse_phase_one (int base)
4988
 
{
4989
 
  unsigned entries, tag, type, len, data, save, i, c;
4990
 
  float romm_cam[3][3];
4991
 
  char *cp;
4992
 
 
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);
4998
 
  entries = get4();
4999
 
  get4();
5000
 
  while (entries--) {
5001
 
    tag  = get4();
5002
 
    type = get4();
5003
 
    len  = get4();
5004
 
    data = get4();
5005
 
    save = ftell(ifp);
5006
 
    fseek (ifp, base+data, SEEK_SET);
5007
 
    switch (tag) {
5008
 
      case 0x100:  flip = "0653"[data & 3]-'0';  break;
5009
 
      case 0x106:
5010
 
        for (i=0; i < 9; i++)
5011
 
          romm_cam[0][i] = getreal(11);
5012
 
        romm_coeff (romm_cam);
5013
 
        break;
5014
 
      case 0x107:
5015
 
        FORC3 cam_mul[c] = pre_mul[c] = getreal(11);
5016
 
        break;
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;
5034
 
      case 0x301:
5035
 
        model[63] = 0;
5036
 
        fread (model, 1, 63, ifp);
5037
 
        if ((cp = strstr(model," camera"))) *cp = 0;
5038
 
    }
5039
 
    fseek (ifp, save, SEEK_SET);
5040
 
  }
5041
 
  load_raw = ph1.format < 3 ?
5042
 
        &CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
5043
 
  maximum = 0xffff;
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;
5051
 
  }
5052
 
}
5053
 
 
5054
 
void CLASS parse_fuji (int offset)
5055
 
{
5056
 
  unsigned entries, tag, len, save, c;
5057
 
 
5058
 
  fseek (ifp, offset, SEEK_SET);
5059
 
  entries = get4();
5060
 
  if (entries > 255) return;
5061
 
  while (entries--) {
5062
 
    tag = get2();
5063
 
    len = get2();
5064
 
    save = ftell(ifp);
5065
 
    if (tag == 0x100) {
5066
 
      raw_height = get2();
5067
 
      raw_width  = get2();
5068
 
    } else if (tag == 0x121) {
5069
 
      height = get2();
5070
 
      if ((width = get2()) == 4284) width += 3;
5071
 
    } else if (tag == 0x130)
5072
 
      fuji_layout = fgetc(ifp) >> 7;
5073
 
    if (tag == 0x2ff0)
5074
 
      FORC4 cam_mul[c ^ 1] = get2();
5075
 
    fseek (ifp, save+len, SEEK_SET);
5076
 
  }
5077
 
  if (fuji_layout) {
5078
 
    height *= 2;
5079
 
    width  /= 2;
5080
 
  }
5081
 
}
5082
 
 
5083
 
int CLASS parse_jpeg (int offset)
5084
 
{
5085
 
  int len, save, hlen, mark;
5086
 
 
5087
 
  fseek (ifp, offset, SEEK_SET);
5088
 
  if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8) return 0;
5089
 
 
5090
 
  while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda) {
5091
 
    order = 0x4d4d;
5092
 
    len   = get2() - 2;
5093
 
    save  = ftell(ifp);
5094
 
    if (mark == 0xc0 || mark == 0xc3) {
5095
 
      fgetc(ifp);
5096
 
      raw_height = get2();
5097
 
      raw_width  = get2();
5098
 
    }
5099
 
    order = get2();
5100
 
    hlen  = get4();
5101
 
    if (get4() == 0x48454150)           /* "HEAP" */
5102
 
      parse_ciff (save+hlen, len-hlen);
5103
 
    parse_tiff (save+6);
5104
 
    fseek (ifp, save+len, SEEK_SET);
5105
 
  }
5106
 
  return 1;
5107
 
}
5108
 
 
5109
 
void CLASS parse_riff()
5110
 
{
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" };
5115
 
  struct tm t;
5116
 
 
5117
 
  order = 0x4949;
5118
 
  fread (tag, 4, 1, ifp);
5119
 
  size = get4();
5120
 
  if (!memcmp(tag,"RIFF",4) || !memcmp(tag,"LIST",4)) {
5121
 
    end = ftell(ifp) + size;
5122
 
    get4();
5123
 
    while (ftell(ifp) < end)
5124
 
      parse_riff();
5125
 
  } else if (!memcmp(tag,"IDIT",4) && size < 64) {
5126
 
    fread (date, 64, 1, ifp);
5127
 
    date[size] = 0;
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++);
5132
 
      t.tm_mon = i;
5133
 
      t.tm_year -= 1900;
5134
 
      if (mktime(&t) > 0)
5135
 
        timestamp = mktime(&t);
5136
 
    }
5137
 
  } else
5138
 
    fseek (ifp, size, SEEK_CUR);
5139
 
}
5140
 
 
5141
 
void CLASS parse_smal (int offset, int fsize)
5142
 
{
5143
 
  int ver;
5144
 
 
5145
 
  fseek (ifp, offset+2, SEEK_SET);
5146
 
  order = 0x4949;
5147
 
  ver = fgetc(ifp);
5148
 
  if (ver == 6)
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;
5158
 
}
5159
 
 
5160
 
char * CLASS foveon_gets (int offset, char *str, int len)
5161
 
{
5162
 
  int i;
5163
 
  fseek (ifp, offset, SEEK_SET);
5164
 
  for (i=0; i < len-1; i++)
5165
 
    if ((str[i] = get2()) == 0) break;
5166
 
  str[i] = 0;
5167
 
  return str;
5168
 
}
5169
 
 
5170
 
void CLASS parse_foveon()
5171
 
{
5172
 
  int entries, img=0, off, len, tag, save, i, wide, high, pent, poff[256][2];
5173
 
  char name[64], value[64];
5174
 
 
5175
 
  order = 0x4949;                       /* Little-endian */
5176
 
  fseek (ifp, 36, SEEK_SET);
5177
 
  flip = get4();
5178
 
  fseek (ifp, -4, SEEK_END);
5179
 
  fseek (ifp, get4(), SEEK_SET);
5180
 
  if (get4() != 0x64434553) return;     /* SECd */
5181
 
  entries = (get4(),get4());
5182
 
  while (entries--) {
5183
 
    off = get4();
5184
 
    len = get4();
5185
 
    tag = get4();
5186
 
    save = ftell(ifp);
5187
 
    fseek (ifp, off, SEEK_SET);
5188
 
    if (get4() != (0x20434553 | (tag << 24))) return;
5189
 
    switch (tag) {
5190
 
      case 0x47414d49:                  /* IMAG */
5191
 
      case 0x32414d49:                  /* IMA2 */
5192
 
        fseek (ifp, 12, SEEK_CUR);
5193
 
        wide = get4();
5194
 
        high = get4();
5195
 
        if (wide > raw_width && high > raw_height) {
5196
 
          raw_width  = wide;
5197
 
          raw_height = high;
5198
 
          data_offset = off+24;
5199
 
        }
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;
5204
 
        }
5205
 
        if (++img == 2 && !thumb_length) {
5206
 
          thumb_offset = off+24;
5207
 
          thumb_width = wide;
5208
 
          thumb_height = high;
5209
 
          write_thumb = &CLASS foveon_thumb;
5210
 
        }
5211
 
        break;
5212
 
      case 0x464d4143:                  /* CAMF */
5213
 
        meta_offset = off+24;
5214
 
        meta_length = len-28;
5215
 
        if (meta_length > 0x20000)
5216
 
            meta_length = 0x20000;
5217
 
        break;
5218
 
      case 0x504f5250:                  /* PROP */
5219
 
        pent = (get4(),get4());
5220
 
        fseek (ifp, 12, SEEK_CUR);
5221
 
        off += pent*8 + 24;
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);
5244
 
        }
5245
 
#ifdef LOCALTIME
5246
 
        timestamp = mktime (gmtime (&timestamp));
5247
 
#endif
5248
 
    }
5249
 
    fseek (ifp, save, SEEK_SET);
5250
 
  }
5251
 
  is_foveon = 1;
5252
 
}
5253
 
 
5254
 
/*
5255
 
   Thanks to Adobe for providing these excellent CAM -> XYZ matrices!
5256
 
 */
5257
 
void CLASS adobe_coeff (char *make, char *model)
5258
 
{
5259
 
  static const struct {
5260
 
    const char *prefix;
5261
 
    short black, trans[12];
5262
 
  } table[] = {
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 } },
5293
 
    { "Canon EOS", 0,
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 } },
5335
 
    { "EPSON R-D1", 0,
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 } },
5411
 
    { "KODAK P712", 0,
5412
 
        { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
5413
 
    { "KODAK P850", 0,
5414
 
        { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
5415
 
    { "KODAK P880", 0,
5416
 
        { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
5417
 
    { "Leaf CMost", 0,
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 } },
5425
 
    { "Leaf", 0,
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 } },
5447
 
    { "NIKON D100", 0,
5448
 
        { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
5449
 
    { "NIKON D1H", 0,
5450
 
        { 7577,-2166,-926,-7454,15592,1934,-2377,2808,8606 } },
5451
 
    { "NIKON D1X", 0,
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 } },
5455
 
    { "NIKON D2H", 0,
5456
 
        { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
5457
 
    { "NIKON D2X", 0,
5458
 
        { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
5459
 
    { "NIKON D50", 0,
5460
 
        { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
5461
 
    { "NIKON D70", 0,
5462
 
        { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
5463
 
    { "NIKON D80", 0,
5464
 
        { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
5465
 
    { "NIKON D200", 0,
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 } },
5471
 
    { "NIKON E2500", 0,
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 } },
5475
 
    { "NIKON E4500", 0,
5476
 
        { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
5477
 
    { "NIKON E5000", 0,
5478
 
        { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
5479
 
    { "NIKON E5400", 0,
5480
 
        { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
5481
 
    { "NIKON E5700", 0,
5482
 
        { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
5483
 
    { "NIKON E8400", 0,
5484
 
        { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
5485
 
    { "NIKON E8700", 0,
5486
 
        { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
5487
 
    { "NIKON E8800", 0,
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 } },
5495
 
    { "OLYMPUS C70", 0,
5496
 
        { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
5497
 
    { "OLYMPUS C80", 0,
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 } },
5501
 
    { "OLYMPUS E-1", 0,
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 } },
5515
 
    { "OLYMPUS SP3", 0,
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 } },
5531
 
    { "PENTAX K10D", 0,
5532
 
        { 28402,-6651,-983,-14699,32553,6467,-1746,1571,25283 } },
5533
 
    { "PENTAX K1", 0,
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 } },
5555
 
    { "SONY DSC-V3", 0,
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 } }
5559
 
  };
5560
 
  double cam_xyz[4][3];
5561
 
  char name[130];
5562
 
  int i, j;
5563
 
 
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))) {
5567
 
      if (table[i].black)
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);
5572
 
      break;
5573
 
    }
5574
 
}
5575
 
 
5576
 
void CLASS simple_coeff (int index)
5577
 
{
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 }
5589
 
  };
5590
 
  int i, c;
5591
 
 
5592
 
  for (raw_color = i=0; i < 3; i++)
5593
 
    FORCC rgb_cam[i][c] = table[index][i*colors+c];
5594
 
}
5595
 
 
5596
 
short CLASS guess_byte_order (int words)
5597
 
{
5598
 
  uchar test[4][2];
5599
 
  int t=2, msb;
5600
 
  double diff, sum[2] = {0,0};
5601
 
 
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;
5609
 
    }
5610
 
    t = (t+1) & 3;
5611
 
  }
5612
 
  return sum[0] < sum[1] ? 0x4d4d : 0x4949;
5613
 
}
5614
 
 
5615
 
/*
5616
 
   Identify which camera created this file, and set global variables
5617
 
   accordingly.
5618
 
 */
5619
 
void CLASS identify()
5620
 
{
5621
 
  char head[32], *cp;
5622
 
  unsigned hlen, fsize, i, c, is_canon;
5623
 
  struct jhead jh;
5624
 
  static const struct {
5625
 
    int fsize;
5626
 
    char make[12], model[19], withjpeg;
5627
 
  } table[] = {
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",
5679
 
      "SAMSUNG" };
5680
 
 
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;
5697
 
    pre_mul[i] = i < 3;
5698
 
    FORC3 rgb_cam[c][i] = c == i;
5699
 
  }
5700
 
  colors = 3;
5701
 
  tiff_bps = 12;
5702
 
  for (i=0; i < 0x1000; i++) curve[i] = i;
5703
 
  profile_length = 0;
5704
 
 
5705
 
  order = get2();
5706
 
  hlen = get4();
5707
 
  fseek (ifp, 0, SEEK_SET);
5708
 
  fread (head, 1, 32, ifp);
5709
 
  fseek (ifp, 0, SEEK_END);
5710
 
  fsize = ftell(ifp);
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)) {
5717
 
      data_offset = hlen;
5718
 
      parse_ciff (hlen, fsize - hlen);
5719
 
    } else {
5720
 
      parse_tiff(0);
5721
 
    }
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)
5728
 
      parse_tiff(12);
5729
 
    thumb_offset = 0;
5730
 
  } else if (!memcmp (head,"BM",2) &&
5731
 
        head[26] == 1 && head[28] == 16 && head[30] == 0) {
5732
 
    data_offset = 0x1000;
5733
 
    order = 0x4949;
5734
 
    fseek (ifp, 38, SEEK_SET);
5735
 
    if (get4() == 2834 && get4() == 2834 && get4() == 0 && get4() == 4096) {
5736
 
      strcpy (model, "BMQ");
5737
 
      flip = 3;
5738
 
      goto nucore;
5739
 
    }
5740
 
  } else if (!memcmp (head,"BR",2)) {
5741
 
    strcpy (model, "RAW");
5742
 
nucore:
5743
 
    strcpy (make, "Nucore");
5744
 
    order = 0x4949;
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) {
5750
 
      raw_width++;
5751
 
      data_offset -= 0x1000;
5752
 
    }
5753
 
  } else if (!memcmp (head+25,"ARECOYK",7)) {
5754
 
    strcpy (make, "Contax");
5755
 
    strcpy (model,"N Digital");
5756
 
    fseek (ifp, 33, SEEK_SET);
5757
 
    get_timestamp(1);
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)
5773
 
        parse_fuji (i);
5774
 
    }
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);
5780
 
    parse_riff();
5781
 
  } else if (!memcmp (head,"DSC-Image",9))
5782
 
    parse_rollei();
5783
 
  else if (!memcmp (head,"PWAD",4))
5784
 
    parse_sinar_ia();
5785
 
  else if (!memcmp (head,"\0MRM",4))
5786
 
    parse_minolta(0);
5787
 
  else if (!memcmp (head,"FOVb",4))
5788
 
    parse_foveon();
5789
 
  else
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();
5796
 
      }
5797
 
  if (make[0] == 0) parse_smal (0, fsize);
5798
 
  if (make[0] == 0) parse_jpeg (is_raw = 0);
5799
 
 
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;
5816
 
 
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;
5822
 
  if (fuji_width) {
5823
 
    width = height + fuji_width;
5824
 
    height = width - 1;
5825
 
    pixel_aspect = 1;
5826
 
  }
5827
 
  if (!strcmp(model,"K10D")) {          /* Camera DNGs are not cropped! */
5828
 
    height = 2616;
5829
 
    width  = 3896;
5830
 
  }
5831
 
  if (dng_version) {
5832
 
    if (filters == UINT_MAX) filters = 0;
5833
 
    if (!filters)
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];
5840
 
    goto dng_skip;
5841
 
  }
5842
 
 
5843
 
/*  We'll try to decode anything from Canon or Nikon. */
5844
 
 
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;
5848
 
    maximum = 0xfff;
5849
 
  }
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;
5855
 
 
5856
 
/* Set parameters based on camera name (for non-DNG files). */
5857
 
 
5858
 
  if (is_foveon) {
5859
 
    if (height*2 < width) pixel_aspect = 0.5;
5860
 
    if (height   > width) pixel_aspect = 2;
5861
 
    filters = 0;
5862
 
    load_raw = &CLASS foveon_load_raw;
5863
 
    simple_coeff(0);
5864
 
  } else if (!strcmp(model,"PowerShot 600")) {
5865
 
    height = 613;
5866
 
    width  = 854;
5867
 
    raw_width = 896;
5868
 
    pixel_aspect = 607/628.0;
5869
 
    colors = 4;
5870
 
    filters = 0xe1e4e1e4;
5871
 
    load_raw = &CLASS canon_600_load_raw;
5872
 
  } else if (!strcmp(model,"PowerShot A5") ||
5873
 
             !strcmp(model,"PowerShot A5 Zoom")) {
5874
 
    height = 773;
5875
 
    width  = 960;
5876
 
    raw_width = 992;
5877
 
    pixel_aspect = 256/235.0;
5878
 
    colors = 4;
5879
 
    filters = 0x1e4e1e4e;
5880
 
    load_raw = &CLASS canon_a5_load_raw;
5881
 
  } else if (!strcmp(model,"PowerShot A50")) {
5882
 
    height =  968;
5883
 
    width  = 1290;
5884
 
    raw_width = 1320;
5885
 
    colors = 4;
5886
 
    filters = 0x1b4e4b1e;
5887
 
    load_raw = &CLASS canon_a5_load_raw;
5888
 
  } else if (!strcmp(model,"PowerShot Pro70")) {
5889
 
    height = 1024;
5890
 
    width  = 1552;
5891
 
    colors = 4;
5892
 
    filters = 0x1e4b4e1b;
5893
 
    load_raw = &CLASS canon_a5_load_raw;
5894
 
  } else if (!strcmp(model,"PowerShot A610")) {
5895
 
    height = 1960;
5896
 
    width  = 2616;
5897
 
    raw_height = 1968;
5898
 
    raw_width  = 2672;
5899
 
    top_margin  = 8;
5900
 
    left_margin = 12;
5901
 
    load_raw = &CLASS canon_a5_load_raw;
5902
 
  } else if (!strcmp(model,"PowerShot A620")) {
5903
 
    height = 2328;
5904
 
    width  = 3112;
5905
 
    raw_height = 2340;
5906
 
    raw_width  = 3152;
5907
 
    top_margin  = 12;
5908
 
    left_margin = 36;
5909
 
    load_raw = &CLASS canon_a5_load_raw;
5910
 
  } else if (!strcmp(model,"PowerShot S3 IS")) {
5911
 
    height = 2128;
5912
 
    width  = 2840;
5913
 
    raw_height = 2136;
5914
 
    raw_width  = 2888;
5915
 
    top_margin  = 8;
5916
 
    left_margin = 44;
5917
 
    load_raw = &CLASS canon_a5_load_raw;
5918
 
  } else if (!strcmp(model,"PowerShot Pro90 IS")) {
5919
 
    width  = 1896;
5920
 
    colors = 4;
5921
 
    filters = 0xb4b4b4b4;
5922
 
  } else if (is_canon && raw_width == 2144) {
5923
 
    height = 1550;
5924
 
    width  = 2088;
5925
 
    top_margin  = 8;
5926
 
    left_margin = 4;
5927
 
    if (!strcmp(model,"PowerShot G1")) {
5928
 
      colors = 4;
5929
 
      filters = 0xb4b4b4b4;
5930
 
    }
5931
 
  } else if (is_canon && raw_width == 2224) {
5932
 
    height = 1448;
5933
 
    width  = 2176;
5934
 
    top_margin  = 6;
5935
 
    left_margin = 48;
5936
 
  } else if (is_canon && raw_width == 2376) {
5937
 
    height = 1720;
5938
 
    width  = 2312;
5939
 
    top_margin  = 6;
5940
 
    left_margin = 12;
5941
 
  } else if (is_canon && raw_width == 2672) {
5942
 
    height = 1960;
5943
 
    width  = 2616;
5944
 
    top_margin  = 6;
5945
 
    left_margin = 12;
5946
 
  } else if (is_canon && raw_width == 3152) {
5947
 
    height = 2056;
5948
 
    width  = 3088;
5949
 
    top_margin  = 12;
5950
 
    left_margin = 64;
5951
 
    if (unique_id == 0x80000170)
5952
 
      adobe_coeff ("Canon","EOS 300D");
5953
 
    maximum = 0xfa0;
5954
 
  } else if (is_canon && raw_width == 3160) {
5955
 
    height = 2328;
5956
 
    width  = 3112;
5957
 
    top_margin  = 12;
5958
 
    left_margin = 44;
5959
 
  } else if (is_canon && raw_width == 3344) {
5960
 
    height = 2472;
5961
 
    width  = 3288;
5962
 
    top_margin  = 6;
5963
 
    left_margin = 4;
5964
 
  } else if (!strcmp(model,"EOS D2000C")) {
5965
 
    filters = 0x61616161;
5966
 
    black = curve[200];
5967
 
  } else if (is_canon && raw_width == 3516) {
5968
 
    top_margin  = 14;
5969
 
    left_margin = 42;
5970
 
    if (unique_id == 0x80000189)
5971
 
      adobe_coeff ("Canon","EOS 350D");
5972
 
    goto canon_cr2;
5973
 
  } else if (is_canon && raw_width == 3596) {
5974
 
    top_margin  = 12;
5975
 
    left_margin = 74;
5976
 
    goto canon_cr2;
5977
 
  } else if (is_canon && raw_width == 3948) {
5978
 
    top_margin  = 18;
5979
 
    left_margin = 42;
5980
 
    height -= 2;
5981
 
    if (unique_id == 0x80000236)
5982
 
      adobe_coeff ("Canon","EOS 400D");
5983
 
    goto canon_cr2;
5984
 
  } else if (is_canon && raw_width == 4476) {
5985
 
    top_margin  = 34;
5986
 
    left_margin = 90;
5987
 
    maximum = 0xe6c;
5988
 
    goto canon_cr2;
5989
 
  } else if (is_canon && raw_width == 5108) {
5990
 
    top_margin  = 13;
5991
 
    left_margin = 98;
5992
 
    maximum = 0xe80;
5993
 
canon_cr2:
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")) {
6000
 
    width -= 4;
6001
 
    pixel_aspect = 0.5;
6002
 
  } else if (!strncmp(model,"D40",3)) {
6003
 
    width--;
6004
 
  } else if (!strncmp(model,"D50",3) || !strncmp(model,"D70",3)) {
6005
 
    width--;
6006
 
    maximum = 0xf53;
6007
 
  } else if (!strcmp(model,"D80")) {
6008
 
    height -= 3;
6009
 
    width  -= 4;
6010
 
  } else if (!strcmp(model,"D100")) {
6011
 
    if (tiff_compress == 34713 && load_raw == &CLASS nikon_load_raw)
6012
 
      raw_width = (width += 3) + 3;
6013
 
    maximum = 0xf44;
6014
 
  } else if (!strcmp(model,"D200")) {
6015
 
    left_margin = 1;
6016
 
    width -= 4;
6017
 
    maximum = 0xfbc;
6018
 
    filters = 0x94949494;
6019
 
  } else if (!strncmp(model,"D2H",3)) {
6020
 
    left_margin = 6;
6021
 
    width -= 14;
6022
 
  } else if (!strcmp(model,"D2X")) {
6023
 
    width -= 8;
6024
 
    maximum = 0xf35;
6025
 
  } else if (fsize == 1581060) {
6026
 
    height = 963;
6027
 
    width = 1287;
6028
 
    raw_width = 1632;
6029
 
    load_raw = &CLASS nikon_e900_load_raw;
6030
 
    maximum = 0x3f4;
6031
 
    colors = 4;
6032
 
    filters = 0x1e1e1e1e;
6033
 
    simple_coeff(3);
6034
 
    pre_mul[0] = 1.2085;
6035
 
    pre_mul[1] = 1.0943;
6036
 
    pre_mul[3] = 1.1103;
6037
 
  } else if (fsize == 2465792) {
6038
 
    height = 1203;
6039
 
    width  = 1616;
6040
 
    raw_width = 2048;
6041
 
    load_raw = &CLASS nikon_e900_load_raw;
6042
 
    maximum = 0x3dd;
6043
 
    colors = 4;
6044
 
    filters = 0x4b4b4b4b;
6045
 
    adobe_coeff ("NIKON","E950");
6046
 
  } else if (fsize == 4771840) {
6047
 
    height = 1540;
6048
 
    width  = 2064;
6049
 
    colors = 4;
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;
6056
 
      simple_coeff(3);
6057
 
      pre_mul[0] = 1.196;
6058
 
      pre_mul[1] = 1.246;
6059
 
      pre_mul[2] = 1.018;
6060
 
    }
6061
 
  } else if (!strcmp(model,"E2100")) {
6062
 
    if (!timestamp && !nikon_e2100()) goto cp_e2500;
6063
 
    height = 1206;
6064
 
    width  = 1616;
6065
 
    load_raw = &CLASS nikon_e2100_load_raw;
6066
 
    pre_mul[0] = 1.945;
6067
 
    pre_mul[2] = 1.040;
6068
 
  } else if (!strcmp(model,"E2500")) {
6069
 
cp_e2500:
6070
 
    strcpy (model, "E2500");
6071
 
    height = 1204;
6072
 
    width  = 1616;
6073
 
    colors = 4;
6074
 
    filters = 0x4b4b4b4b;
6075
 
  } else if (fsize == 4775936) {
6076
 
    height = 1542;
6077
 
    width  = 2064;
6078
 
    load_raw = &CLASS nikon_e2100_load_raw;
6079
 
    pre_mul[0] = 1.818;
6080
 
    pre_mul[2] = 1.618;
6081
 
    if (!timestamp) nikon_3700();
6082
 
    if (model[0] == 'E' && atoi(model+1) < 3700)
6083
 
      filters = 0x49494949;
6084
 
    if (!strcmp(model,"Optio 33WR")) {
6085
 
      flip = 1;
6086
 
      filters = 0x16161616;
6087
 
      pre_mul[0] = 1.331;
6088
 
      pre_mul[2] = 1.820;
6089
 
    }
6090
 
  } else if (fsize == 5869568) {
6091
 
    height = 1710;
6092
 
    width  = 2288;
6093
 
    filters = 0x16161616;
6094
 
    if (!timestamp && minolta_z2()) {
6095
 
      strcpy (make, "Minolta");
6096
 
      strcpy (model,"DiMAGE Z2");
6097
 
    }
6098
 
    if (make[0] == 'M')
6099
 
      load_raw = &CLASS nikon_e2100_load_raw;
6100
 
  } else if (!strcmp(model,"E4500")) {
6101
 
    height = 1708;
6102
 
    width  = 2288;
6103
 
    colors = 4;
6104
 
    filters = 0xb4b4b4b4;
6105
 
  } else if (fsize == 7438336) {
6106
 
    height = 1924;
6107
 
    width  = 2576;
6108
 
    colors = 4;
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;
6116
 
    maximum = 0x3e00;
6117
 
  } else if (!strncmp(model,"FinePix",7)) {
6118
 
    if (!strcmp(model+7,"S2Pro")) {
6119
 
      strcpy (model+7," S2Pro");
6120
 
      height = 2144;
6121
 
      width  = 2880;
6122
 
      flip = 6;
6123
 
    } else
6124
 
      maximum = 0x3e00;
6125
 
    if (fuji_secondary && shot_select)
6126
 
      maximum = 0x2f00;
6127
 
    top_margin = (raw_height - height)/2;
6128
 
    left_margin = (raw_width - width )/2;
6129
 
    if (fuji_secondary)
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;
6135
 
    height = width - 1;
6136
 
    load_raw = &CLASS fuji_load_raw;
6137
 
    if (!(fuji_width & 1)) filters = 0x49494949;
6138
 
  } else if (!strcmp(model,"RD175")) {
6139
 
    height = 986;
6140
 
    width = 1534;
6141
 
    data_offset = 513;
6142
 
    filters = 0x61616161;
6143
 
    load_raw = &CLASS minolta_rd175_load_raw;
6144
 
  } else if (!strcmp(model,"KD-400Z")) {
6145
 
    height = 1712;
6146
 
    width  = 2312;
6147
 
    raw_width = 2336;
6148
 
    goto konica_400z;
6149
 
  } else if (!strcmp(model,"KD-510Z")) {
6150
 
    goto konica_510z;
6151
 
  } else if (!strcasecmp(make,"MINOLTA")) {
6152
 
    load_raw = &CLASS unpacked_load_raw;
6153
 
    maximum = 0xf7d;
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;
6165
 
      maximum = 0xffb;
6166
 
    } else if (!strncmp(model,"DiMAGE G",8)) {
6167
 
      if (model[8] == '4') {
6168
 
        height = 1716;
6169
 
        width  = 2304;
6170
 
      } else if (model[8] == '5') {
6171
 
konica_510z:
6172
 
        height = 1956;
6173
 
        width  = 2607;
6174
 
        raw_width = 2624;
6175
 
      } else if (model[8] == '6') {
6176
 
        height = 2136;
6177
 
        width  = 2848;
6178
 
      }
6179
 
      data_offset += 14;
6180
 
      filters = 0x61616161;
6181
 
konica_400z:
6182
 
      load_raw = &CLASS unpacked_load_raw;
6183
 
      maximum = 0x3df;
6184
 
      order = 0x4d4d;
6185
 
    }
6186
 
  } else if (!strcmp(model,"*ist DS")) {
6187
 
    height -= 2;
6188
 
  } else if (!strcmp(model,"Optio S")) {
6189
 
    if (fsize == 3178560) {
6190
 
      height = 1540;
6191
 
      width  = 2064;
6192
 
      load_raw = &CLASS eight_bit_load_raw;
6193
 
      cam_mul[0] *= 4;
6194
 
      cam_mul[2] *= 4;
6195
 
      pre_mul[0] = 1.391;
6196
 
      pre_mul[2] = 1.188;
6197
 
    } else {
6198
 
      height = 1544;
6199
 
      width  = 2068;
6200
 
      raw_width = 3136;
6201
 
      load_raw = &CLASS packed_12_load_raw;
6202
 
      maximum = 0xf7c;
6203
 
      pre_mul[0] = 1.137;
6204
 
      pre_mul[2] = 1.453;
6205
 
    }
6206
 
  } else if (fsize == 6114240) {
6207
 
    height = 1737;
6208
 
    width  = 2324;
6209
 
    raw_width = 3520;
6210
 
    load_raw = &CLASS packed_12_load_raw;
6211
 
    maximum = 0xf7a;
6212
 
    pre_mul[0] = 1.980;
6213
 
    pre_mul[2] = 1.570;
6214
 
  } else if (!strcmp(model,"STV680 VGA")) {
6215
 
    height = 484;
6216
 
    width  = 644;
6217
 
    load_raw = &CLASS eight_bit_load_raw;
6218
 
    flip = 2;
6219
 
    filters = 0x16161616;
6220
 
    black = 16;
6221
 
    pre_mul[0] = 1.097;
6222
 
    pre_mul[2] = 1.128;
6223
 
  } else if (!strcmp(model,"KAI-0340")) {
6224
 
    height = 477;
6225
 
    width  = 640;
6226
 
    order = 0x4949;
6227
 
    data_offset = 3840;
6228
 
    load_raw = &CLASS unpacked_load_raw;
6229
 
    pre_mul[0] = 1.561;
6230
 
    pre_mul[2] = 2.454;
6231
 
  } else if (!strcmp(model,"531C")) {
6232
 
    height = 1200;
6233
 
    width  = 1600;
6234
 
    load_raw = &CLASS unpacked_load_raw;
6235
 
    filters = 0x49494949;
6236
 
    pre_mul[1] = 1.218;
6237
 
  } else if (!strcmp(model,"F-145C")) {
6238
 
    height = 1040;
6239
 
    width  = 1392;
6240
 
    load_raw = &CLASS eight_bit_load_raw;
6241
 
  } else if (!strcmp(model,"F-201C")) {
6242
 
    height = 1200;
6243
 
    width  = 1600;
6244
 
    load_raw = &CLASS eight_bit_load_raw;
6245
 
  } else if (!strcmp(model,"F-510C")) {
6246
 
    height = 1958;
6247
 
    width  = 2588;
6248
 
    load_raw = fsize < 7500000 ?
6249
 
        &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
6250
 
    maximum = 0xfff0;
6251
 
  } else if (!strcmp(model,"F-810C")) {
6252
 
    height = 2469;
6253
 
    width  = 3272;
6254
 
    load_raw = &CLASS unpacked_load_raw;
6255
 
    maximum = 0xfff0;
6256
 
  } else if (!strcmp(model,"XCD-SX910CR")) {
6257
 
    height = 1024;
6258
 
    width  = 1375;
6259
 
    raw_width = 1376;
6260
 
    filters = 0x49494949;
6261
 
    maximum = 0x3ff;
6262
 
    load_raw = fsize < 2000000 ?
6263
 
        &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
6264
 
  } else if (!strcmp(model,"2010")) {
6265
 
    height = 1207;
6266
 
    width  = 1608;
6267
 
    order = 0x4949;
6268
 
    filters = 0x16161616;
6269
 
    data_offset = 3212;
6270
 
    maximum = 0x3ff;
6271
 
    load_raw = &CLASS unpacked_load_raw;
6272
 
  } else if (!strcmp(model,"A782")) {
6273
 
    height = 3000;
6274
 
    width  = 2208;
6275
 
    filters = 0x61616161;
6276
 
    load_raw = fsize < 10000000 ?
6277
 
        &CLASS eight_bit_load_raw : &CLASS unpacked_load_raw;
6278
 
    maximum = 0xffc0;
6279
 
  } else if (!strcmp(model,"3320AF")) {
6280
 
    height = 1536;
6281
 
    raw_width = width = 2048;
6282
 
    filters = 0x61616161;
6283
 
    load_raw = &CLASS unpacked_load_raw;
6284
 
    maximum = 0x3ff;
6285
 
    pre_mul[0] = 1.717;
6286
 
    pre_mul[2] = 1.138;
6287
 
    fseek (ifp, 0x300000, SEEK_SET);
6288
 
    if ((order = guess_byte_order(0x10000)) == 0x4d4d) {
6289
 
      height -= (top_margin = 16);
6290
 
      width -= (left_margin = 28);
6291
 
      maximum = 0xf5c0;
6292
 
      strcpy (make, "ISG");
6293
 
      model[0] = 0;
6294
 
    }
6295
 
  } else if (!strcmp(make,"Imacon")) {
6296
 
    sprintf (model, "Ixpress %d-Mp", height*width/1000000);
6297
 
    load_raw = &CLASS imacon_full_load_raw;
6298
 
    if (filters) {
6299
 
      if (left_margin & 1) filters = 0x61616161;
6300
 
      load_raw = &CLASS unpacked_load_raw;
6301
 
    }
6302
 
    maximum = 0xffff;
6303
 
  } else if (!strcmp(make,"Sinar")) {
6304
 
    if (!memcmp(head,"8BPS",4)) {
6305
 
      fseek (ifp, 14, SEEK_SET);
6306
 
      height = get4();
6307
 
      width  = get4();
6308
 
      filters = 0x61616161;
6309
 
      data_offset = 68;
6310
 
    }
6311
 
    if (!load_raw) load_raw = &CLASS unpacked_load_raw;
6312
 
    maximum = 0x3fff;
6313
 
  } else if (!strcmp(make,"Leaf")) {
6314
 
    maximum = 0x3fff;
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) {
6320
 
        filters = 1;
6321
 
        strcpy (cdesc, "RBTG");
6322
 
        strcpy (model, "CatchLight");
6323
 
        top_margin =  8; left_margin = 18; height = 2032; width = 2016;
6324
 
      } else {
6325
 
        strcpy (model, "DCB2");
6326
 
        top_margin = 10; left_margin = 16; height = 2028; width = 2022;
6327
 
      }
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;
6333
 
      } else {
6334
 
        left_margin = 6;  top_margin = 32;  width = 2048; height = 3072;
6335
 
        filters = 0x16161616;
6336
 
      }
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;
6348
 
    }
6349
 
  } else if (!strcmp(make,"LEICA") || !strcmp(make,"Panasonic")) {
6350
 
    maximum = 0xfff0;
6351
 
    load_raw = &CLASS unpacked_load_raw;
6352
 
    if (width == 2568) {
6353
 
      adobe_coeff ("Panasonic","DMC-LC1");
6354
 
    } else if (width == 3177) {
6355
 
      maximum = 0xf7fc;
6356
 
      width -= 10;
6357
 
      filters = 0x49494949;
6358
 
      adobe_coeff ("Panasonic","DMC-L1");
6359
 
      load_raw = &CLASS panasonic_load_raw;
6360
 
    } else if (width == 3304) {
6361
 
      maximum = 0xf94c;
6362
 
      width -= 16;
6363
 
      adobe_coeff ("Panasonic","DMC-FZ30");
6364
 
      load_raw = &CLASS panasonic_load_raw;
6365
 
    } else if (width == 3690) {
6366
 
      maximum = 0xf7f0;
6367
 
      height -= 3;
6368
 
      width = 3672;
6369
 
      left_margin = 3;
6370
 
      filters = 0x49494949;
6371
 
      adobe_coeff ("Panasonic","DMC-FZ50");
6372
 
      load_raw = &CLASS panasonic_load_raw;
6373
 
    } else if (width == 3770) {
6374
 
      height = 2760;
6375
 
      width  = 3672;
6376
 
      top_margin  = 15;
6377
 
      left_margin = 17;
6378
 
      adobe_coeff ("Panasonic","DMC-FZ50");
6379
 
      load_raw = &CLASS panasonic_load_raw;
6380
 
    } else if (width == 3880) {
6381
 
      maximum = 0xf7f0;
6382
 
      width -= 22;
6383
 
      left_margin = 6;
6384
 
      adobe_coeff ("Panasonic","DMC-LX1");
6385
 
      load_raw = &CLASS panasonic_load_raw;
6386
 
    } else if (width == 4290) {
6387
 
      height--;
6388
 
      width = 4248;
6389
 
      left_margin = 3;
6390
 
      filters = 0x49494949;
6391
 
      adobe_coeff ("Panasonic","DMC-LX2");
6392
 
    } else if (width == 4330) {
6393
 
      height = 2400;
6394
 
      width  = 4248;
6395
 
      top_margin  = 15;
6396
 
      left_margin = 17;
6397
 
      adobe_coeff ("Panasonic","DMC-LX2");
6398
 
    }
6399
 
  } else if (!strcmp(model,"E-1") ||
6400
 
             !strcmp(model,"E-400")) {
6401
 
    filters = 0x61616161;
6402
 
    maximum = 0xfff0;
6403
 
  } else if (!strcmp(model,"E-10") ||
6404
 
            !strncmp(model,"E-20",4)) {
6405
 
    maximum = 0xffc0;
6406
 
    black <<= 2;
6407
 
  } else if (!strcmp(model,"E-300") ||
6408
 
             !strcmp(model,"E-500")) {
6409
 
    width -= 20;
6410
 
    maximum = 0xfc30;
6411
 
    if (load_raw == &CLASS unpacked_load_raw) black = 0;
6412
 
  } else if (!strcmp(model,"E-330")) {
6413
 
    width -= 30;
6414
 
  } else if (!strcmp(model,"C770UZ")) {
6415
 
    height = 1718;
6416
 
    width  = 2304;
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")) {
6427
 
    height = 2047;
6428
 
    width  = 3072;
6429
 
    filters = 0x61616161;
6430
 
    data_offset = 0x1a00;
6431
 
    load_raw = &CLASS packed_12_load_raw;
6432
 
    maximum = 0xf1e;
6433
 
  } else if (!strcmp(model,"DSC-F828")) {
6434
 
    width = 3288;
6435
 
    left_margin = 5;
6436
 
    data_offset = 862144;
6437
 
    load_raw = &CLASS sony_load_raw;
6438
 
    filters = 0x9c9c9c9c;
6439
 
    colors = 4;
6440
 
    strcpy (cdesc, "RGBE");
6441
 
  } else if (!strcmp(model,"DSC-V3")) {
6442
 
    width = 3109;
6443
 
    left_margin = 59;
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");
6448
 
    width = 3925;
6449
 
    order = 0x4d4d;
6450
 
  } else if (!strcmp(model,"DSLR-A100")) {
6451
 
    height--;
6452
 
    load_raw = &CLASS sony_arw_load_raw;
6453
 
    maximum = 0xfeb;
6454
 
  } else if (!strncmp(model,"P850",4)) {
6455
 
    maximum = 0xf7c;
6456
 
  } else if (!strcasecmp(make,"KODAK")) {
6457
 
    if (filters == UINT_MAX) filters = 0x61616161;
6458
 
    if (!strncmp(model,"NC2000",6)) {
6459
 
      width -= 4;
6460
 
      left_margin = 2;
6461
 
    } else if (!strcmp(model,"EOSDCS3B")) {
6462
 
      width -= 4;
6463
 
      left_margin = 2;
6464
 
    } else if (!strcmp(model,"EOSDCS1")) {
6465
 
      width -= 4;
6466
 
      left_margin = 2;
6467
 
    } else if (!strcmp(model,"DCS420")) {
6468
 
      width -= 4;
6469
 
      left_margin = 2;
6470
 
    } else if (!strcmp(model,"DCS460")) {
6471
 
      width -= 4;
6472
 
      left_margin = 2;
6473
 
    } else if (!strcmp(model,"DCS460A")) {
6474
 
      width -= 4;
6475
 
      left_margin = 2;
6476
 
      colors = 1;
6477
 
      filters = 0;
6478
 
    } else if (!strcmp(model,"DCS660M")) {
6479
 
      black = 214;
6480
 
      colors = 1;
6481
 
      filters = 0;
6482
 
    } else if (!strcmp(model,"DCS760M")) {
6483
 
      colors = 1;
6484
 
      filters = 0;
6485
 
    }
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;
6491
 
    }
6492
 
    if (!strncmp(model,"DC2",3)) {
6493
 
      height = 242;
6494
 
      if (fsize < 100000) {
6495
 
        raw_width = 256; width = 249;
6496
 
      } else {
6497
 
        raw_width = 512; width = 501;
6498
 
      }
6499
 
      data_offset += raw_width + 1;
6500
 
      colors = 4;
6501
 
      filters = 0x8d8d8d8d;
6502
 
      simple_coeff(1);
6503
 
      pre_mul[1] = 1.179;
6504
 
      pre_mul[2] = 1.209;
6505
 
      pre_mul[3] = 1.036;
6506
 
      load_raw = &CLASS kodak_easy_load_raw;
6507
 
    } else if (!strcmp(model,"40")) {
6508
 
      strcpy (model, "DC40");
6509
 
      height = 512;
6510
 
      width  = 768;
6511
 
      data_offset = 1152;
6512
 
      load_raw = &CLASS kodak_radc_load_raw;
6513
 
    } else if (strstr(model,"DC50")) {
6514
 
      strcpy (model, "DC50");
6515
 
      height = 512;
6516
 
      width  = 768;
6517
 
      data_offset = 19712;
6518
 
      load_raw = &CLASS kodak_radc_load_raw;
6519
 
    } else if (strstr(model,"DC120")) {
6520
 
      strcpy (model, "DC120");
6521
 
      height = 976;
6522
 
      width  = 848;
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")) {
6527
 
      thumb_height = 128;
6528
 
      thumb_width  = 192;
6529
 
      thumb_offset = 6144;
6530
 
      thumb_misc   = 360;
6531
 
      write_thumb = &CLASS layer_thumb;
6532
 
      height = 1024;
6533
 
      width  = 1536;
6534
 
      data_offset = 79872;
6535
 
      load_raw = &CLASS eight_bit_load_raw;
6536
 
      black = 17;
6537
 
    }
6538
 
  } else if (!strcmp(model,"Fotoman Pixtura")) {
6539
 
    height = 512;
6540
 
    width  = 768;
6541
 
    data_offset = 3632;
6542
 
    load_raw = &CLASS kodak_radc_load_raw;
6543
 
    filters = 0x61616161;
6544
 
    simple_coeff(2);
6545
 
  } else if (!strcmp(make,"Rollei") && !load_raw) {
6546
 
    switch (raw_width) {
6547
 
      case 1316:
6548
 
        height = 1030;
6549
 
        width  = 1300;
6550
 
        top_margin  = 1;
6551
 
        left_margin = 6;
6552
 
        break;
6553
 
      case 2568:
6554
 
        height = 1960;
6555
 
        width  = 2560;
6556
 
        top_margin  = 2;
6557
 
        left_margin = 8;
6558
 
    }
6559
 
    filters = 0x16161616;
6560
 
    load_raw = &CLASS rollei_load_raw;
6561
 
    pre_mul[0] = 1.8;
6562
 
    pre_mul[2] = 1.3;
6563
 
  } else if (!strcmp(model,"PC-CAM 600")) {
6564
 
    height = 768;
6565
 
    data_offset = width = 1024;
6566
 
    filters = 0x49494949;
6567
 
    load_raw = &CLASS eight_bit_load_raw;
6568
 
    pre_mul[0] = 1.14;
6569
 
    pre_mul[2] = 2.73;
6570
 
  } else if (!strcmp(model,"QV-2000UX")) {
6571
 
    height = 1208;
6572
 
    width  = 1632;
6573
 
    data_offset = width * 2;
6574
 
    load_raw = &CLASS eight_bit_load_raw;
6575
 
  } else if (fsize == 3217760) {
6576
 
    height = 1546;
6577
 
    width  = 2070;
6578
 
    raw_width = 2080;
6579
 
    load_raw = &CLASS eight_bit_load_raw;
6580
 
  } else if (!strcmp(model,"QV-4000")) {
6581
 
    height = 1700;
6582
 
    width  = 2260;
6583
 
    load_raw = &CLASS unpacked_load_raw;
6584
 
    maximum = 0xffff;
6585
 
  } else if (!strcmp(model,"QV-5700")) {
6586
 
    height = 1924;
6587
 
    width  = 2576;
6588
 
    load_raw = &CLASS casio_qv5700_load_raw;
6589
 
  } else if (!strcmp(model,"QV-R51")) {
6590
 
    height = 1926;
6591
 
    width  = 2576;
6592
 
    raw_width = 3904;
6593
 
    load_raw = &CLASS packed_12_load_raw;
6594
 
    pre_mul[0] = 1.340;
6595
 
    pre_mul[2] = 1.672;
6596
 
  } else if (!strcmp(model,"EX-S100")) {
6597
 
    height = 1544;
6598
 
    width  = 2058;
6599
 
    raw_width = 3136;
6600
 
    load_raw = &CLASS packed_12_load_raw;
6601
 
    pre_mul[0] = 1.631;
6602
 
    pre_mul[2] = 1.106;
6603
 
  } else if (!strcmp(model,"EX-Z50")) {
6604
 
    height = 1931;
6605
 
    width  = 2570;
6606
 
    raw_width = 3904;
6607
 
    load_raw = &CLASS packed_12_load_raw;
6608
 
    pre_mul[0] = 2.529;
6609
 
    pre_mul[2] = 1.185;
6610
 
  } else if (!strcmp(model,"EX-Z55")) {
6611
 
    height = 1960;
6612
 
    width  = 2570;
6613
 
    raw_width = 3904;
6614
 
    load_raw = &CLASS packed_12_load_raw;
6615
 
    pre_mul[0] = 1.520;
6616
 
    pre_mul[2] = 1.316;
6617
 
  } else if (!strcmp(model,"EX-P505")) {
6618
 
    height = 1928;
6619
 
    width  = 2568;
6620
 
    raw_width = 3852;
6621
 
    load_raw = &CLASS packed_12_load_raw;
6622
 
    pre_mul[0] = 2.07;
6623
 
    pre_mul[2] = 1.88;
6624
 
  } else if (fsize == 9313536) {        /* EX-P600 or QV-R61 */
6625
 
    height = 2142;
6626
 
    width  = 2844;
6627
 
    raw_width = 4288;
6628
 
    load_raw = &CLASS packed_12_load_raw;
6629
 
    pre_mul[0] = 1.797;
6630
 
    pre_mul[2] = 1.219;
6631
 
  } else if (!strcmp(model,"EX-P700")) {
6632
 
    height = 2318;
6633
 
    width  = 3082;
6634
 
    raw_width = 4672;
6635
 
    load_raw = &CLASS packed_12_load_raw;
6636
 
    pre_mul[0] = 1.758;
6637
 
    pre_mul[2] = 1.504;
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;
6644
 
      flip = 2;
6645
 
    }
6646
 
  }
6647
 
  if (!model[0])
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;
6656
 
    }
6657
 
  }
6658
 
dng_skip:
6659
 
  if (!load_raw || !height) is_raw = 0;
6660
 
#ifdef NO_JPEG
6661
 
  if (load_raw == kodak_jpeg_load_raw) {
6662
 
    fprintf (stderr,_("%s: You must link dcraw with libjpeg!!\n"), ifname);
6663
 
    is_raw = 0;
6664
 
  }
6665
 
#endif
6666
 
  if (!cdesc[0])
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)
6673
 
        filters |= 2 << i;
6674
 
      if ((filters >> i & 15) == 6)
6675
 
        filters |= 8 << i;
6676
 
    }
6677
 
notraw:
6678
 
  if (flip == -1) flip = tiff_flip;
6679
 
  if (flip == -1) flip = 0;
6680
 
}
6681
 
 
6682
 
#ifndef NO_LCMS
6683
 
void CLASS apply_profile (char *input, char *output)
6684
 
{
6685
 
  char *prof;
6686
 
  cmsHPROFILE hInProfile=NULL, hOutProfile=NULL;
6687
 
  cmsHTRANSFORM hTransform;
6688
 
  FILE *fp;
6689
 
  unsigned size;
6690
 
 
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);
6700
 
    free (prof);
6701
 
  } else
6702
 
    fprintf (stderr,_("%s has no embedded profile.\n"), ifname);
6703
 
  if (!hInProfile) return;
6704
 
  if (!output)
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);
6712
 
    fclose (fp);
6713
 
    if (!(hOutProfile = cmsOpenProfileFromMem (oprof, size))) {
6714
 
      free (oprof);
6715
 
      oprof = NULL;
6716
 
    }
6717
 
  } else
6718
 
    fprintf (stderr,_("Cannot open file %s!\n"), output);
6719
 
  if (!hOutProfile) goto quit;
6720
 
  if (verbose)
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);
6728
 
quit:
6729
 
  cmsCloseProfile (hInProfile);
6730
 
}
6731
 
#endif
6732
 
 
6733
 
void CLASS convert_to_rgb()
6734
 
{
6735
 
  int mix_green, row, col, c, i, j, k;
6736
 
  ushort *img;
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 };
6764
 
  unsigned pbody[] =
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 };
6777
 
 
6778
 
  memcpy (out_cam, rgb_cam, sizeof out_cam);
6779
 
  raw_color |= colors == 1 || document_mode ||
6780
 
                output_color < 1 || output_color > 5;
6781
 
  if (!raw_color) {
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;
6791
 
    }
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)
6796
 
#ifdef SRGB_GAMMA
6797
 
      pcurve[3] = 0x2330000;
6798
 
#else
6799
 
      pcurve[3] = 0x1f00000;
6800
 
#endif
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;
6809
 
      }
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];
6818
 
  }
6819
 
  if (verbose)
6820
 
    fprintf (stderr, raw_color ? _("Building histograms...\n") :
6821
 
        _("Converting to %s colorspace...\n"), name[output_color-1]);
6822
 
 
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) {
6827
 
      if (!raw_color) {
6828
 
        out[0] = out[1] = out[2] = 0;
6829
 
        FORCC {
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];
6833
 
        }
6834
 
        FORC3 img[c] = CLIP((int) out[c]);
6835
 
      }
6836
 
      else if (document_mode)
6837
 
        img[0] = img[FC(row,col)];
6838
 
      else if (mix_green)
6839
 
        img[1] = (img[1] + img[3]) >> 1;
6840
 
      FORCC histogram[c][img[c] >> 3]++;
6841
 
    }
6842
 
  if (colors == 4 && (output_color || mix_green)) colors = 3;
6843
 
  if (document_mode && filters) colors = 1;
6844
 
}
6845
 
 
6846
 
void CLASS fuji_rotate()
6847
 
{
6848
 
  int i, wide, high, row, col;
6849
 
  double step;
6850
 
  float r, c, fr, fc;
6851
 
  unsigned ur, uc;
6852
 
  ushort (*img)[4], (*pix)[4];
6853
 
 
6854
 
  if (!fuji_width) return;
6855
 
  if (verbose)
6856
 
    fprintf (stderr,_("Rotating image 45 degrees...\n"));
6857
 
  fuji_width = (fuji_width - 1 + shrink) >> shrink;
6858
 
  step = sqrt(0.5);
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()");
6863
 
 
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;
6869
 
      fr = r - ur;
6870
 
      fc = c - uc;
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;
6876
 
    }
6877
 
  free (image);
6878
 
  width  = wide;
6879
 
  height = high;
6880
 
  image  = img;
6881
 
  fuji_width = 0;
6882
 
}
6883
 
 
6884
 
void CLASS stretch()
6885
 
{
6886
 
  int newdim, row, col, c;
6887
 
  double rc, frac;
6888
 
  ushort (*img)[4], *pix0, *pix1;
6889
 
 
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;
6902
 
    }
6903
 
    height = newdim;
6904
 
  } else {
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;
6914
 
    }
6915
 
    width = newdim;
6916
 
  }
6917
 
  free (image);
6918
 
  image = img;
6919
 
}
6920
 
 
6921
 
int CLASS flip_index (int row, int col)
6922
 
{
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;
6927
 
}
6928
 
 
6929
 
void CLASS gamma_lut (uchar lut[0x10000])
6930
 
{
6931
 
  int perc, c, val, total, i;
6932
 
  float white=0, r;
6933
 
 
6934
 
  perc = width * height * 0.01;         /* 99th percentile white point */
6935
 
  if (fuji_width) perc /= 2;
6936
 
  if (highlight) perc = 0;
6937
 
  FORCC {
6938
 
    for (val=0x2000, total=0; --val > 32; )
6939
 
      if ((total += histogram[c][val]) > perc) break;
6940
 
    if (white < val) white = val;
6941
 
  }
6942
 
  white *= 8 / bright;
6943
 
  for (i=0; i < 0x10000; i++) {
6944
 
    r = i / white;
6945
 
    val = 256 * ( !use_gamma ? r :
6946
 
#ifdef SRGB_GAMMA
6947
 
        r <= 0.00304 ? r*12.92 : pow(r,2.5/6)*1.055-0.055 );
6948
 
#else
6949
 
        r <= 0.018 ? r*4.5 : pow(r,0.45)*1.099-0.099 );
6950
 
#endif
6951
 
    if (val > 255) val = 255;
6952
 
    lut[i] = val;
6953
 
  }
6954
 
}
6955
 
 
6956
 
struct tiff_tag {
6957
 
  ushort tag, type;
6958
 
  int count;
6959
 
  union { short s0, s1; int i0; } val;
6960
 
};
6961
 
 
6962
 
struct tiff_hdr {
6963
 
  ushort order, magic;
6964
 
  int ifd;
6965
 
  ushort pad, ntag;
6966
 
  struct tiff_tag tag[15];
6967
 
  int nextifd;
6968
 
  ushort pad2, nexif;
6969
 
  struct tiff_tag exif[4];
6970
 
  short bps[4];
6971
 
  int rat[6];
6972
 
  char make[64], model[64], soft[32], date[20];
6973
 
};
6974
 
 
6975
 
void CLASS tiff_set (ushort *ntag,
6976
 
        ushort tag, ushort type, int count, int val)
6977
 
{
6978
 
  struct tiff_tag *tt;
6979
 
 
6980
 
  tt = (struct tiff_tag *)(ntag+1) + (*ntag)++;
6981
 
  tt->tag = tag;
6982
 
  tt->type = type;
6983
 
  tt->count = count;
6984
 
  if (type == 3 && count == 1)
6985
 
    tt->val.s0 = val;
6986
 
  else tt->val.i0 = val;
6987
 
}
6988
 
 
6989
 
#define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
6990
 
 
6991
 
void CLASS tiff_head (struct tiff_hdr *th, int full)
6992
 
{
6993
 
  int c, psize=0;
6994
 
  struct tm *t;
6995
 
 
6996
 
  memset (th, 0, sizeof *th);
6997
 
  th->order = htonl(0x4d4d4949) >> 16;
6998
 
  th->magic = 42;
6999
 
  th->ifd = 10;
7000
 
  if (full) {
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);
7004
 
    if (colors > 2)
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));
7009
 
  }
7010
 
  tiff_set (&th->ntag, 271, 2, 64, TOFF(th->make));
7011
 
  tiff_set (&th->ntag, 272, 2, 64, TOFF(th->model));
7012
 
  if (full) {
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);
7018
 
  } else
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 (&timestamp);
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);
7038
 
}
7039
 
 
7040
 
void CLASS jpeg_thumb (FILE *tfp)
7041
 
{
7042
 
  char *thumb;
7043
 
  ushort exif[5];
7044
 
  struct tiff_hdr th;
7045
 
 
7046
 
  thumb = (char *) malloc (thumb_length);
7047
 
  merror (thumb, "jpeg_thumb()");
7048
 
  fread (thumb, 1, thumb_length, ifp);
7049
 
  fputc (0xff, tfp);
7050
 
  fputc (0xd8, tfp);
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);
7055
 
    tiff_head (&th, 0);
7056
 
    fwrite (&th, 1, sizeof th, tfp);
7057
 
  }
7058
 
  fwrite (thumb+2, 1, thumb_length-2, tfp);
7059
 
  free (thumb);
7060
 
}
7061
 
 
7062
 
// void CLASS write_ppm_tiff (FILE *ofp)
7063
 
// {
7064
 
//   struct tiff_hdr th;
7065
 
//   uchar *ppm, lut[0x10000];
7066
 
//   ushort *ppm2;
7067
 
//   int c, row, col, soff, rstep, cstep;
7068
 
// 
7069
 
//   iheight = height;
7070
 
//   iwidth  = width;
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);
7078
 
//     if (oprof)
7079
 
//       fwrite (oprof, ntohl(oprof[0]), 1, ofp);
7080
 
//   } else if (colors > 3)
7081
 
//     fprintf (ofp,
7082
 
//       "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
7083
 
//      width, height, colors, (1 << output_bps)-1, cdesc);
7084
 
//   else
7085
 
//     fprintf (ofp, "P%d\n%d %d\n%d\n",
7086
 
//      colors/2+5, width, height, (1 << output_bps)-1);
7087
 
// 
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);
7100
 
//   }
7101
 
//   free (ppm);
7102
 
// }
7103
 
 
7104
 
// int CLASS PREVIOUSmain (int argc, char **argv)
7105
 
// {
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;
7113
 
// #ifndef NO_LCMS
7114
 
//   char *cam_profile = NULL, *out_profile = NULL;
7115
 
// #endif
7116
 
// 
7117
 
// #ifndef LOCALTIME
7118
 
//   putenv ("TZ=UTC");
7119
 
// #endif
7120
 
// #ifdef LOCALEDIR
7121
 
//   setlocale (LC_CTYPE, "");
7122
 
//   setlocale (LC_MESSAGES, "");
7123
 
//   bindtextdomain ("dcraw", LOCALEDIR);
7124
 
//   textdomain ("dcraw");
7125
 
// #endif
7126
 
// 
7127
 
//   if (argc == 1) {
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)"));
7146
 
// #ifndef NO_LCMS
7147
 
//     puts(_("-o <file> Apply output ICC profile from file"));
7148
 
//     puts(_("-p <file> Apply camera ICC profile from file or \"embed\""));
7149
 
// #endif
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"));
7160
 
//     puts("");
7161
 
//     return 1;
7162
 
//   }
7163
 
//   argv[argc] = "";
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);
7170
 
//        return 1;
7171
 
//      }
7172
 
//     switch (opt) {
7173
 
//       case 'B':  sigma_d     = atof(argv[arg++]);
7174
 
//               sigma_r     = atof(argv[arg++]);  break;
7175
 
//       case 'b':  bright      = atof(argv[arg++]);  break;
7176
 
//       case 'r':
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;
7183
 
//       case 'o':
7184
 
//      if (isdigit(argv[arg][0]) && !argv[arg][1])
7185
 
//        output_color = atoi(argv[arg++]);
7186
 
// #ifndef NO_LCMS
7187
 
//      else     out_profile = argv[arg++];
7188
 
//      break;
7189
 
//       case 'p':  cam_profile = argv[arg++];
7190
 
// #endif
7191
 
//      break;
7192
 
//       case 'K':  dark_frame  = argv[arg++];
7193
 
//      break;
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;
7203
 
//       case 'D':
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;
7209
 
//       default:
7210
 
//      fprintf (stderr,_("Unknown option \"-%c\".\n"), opt);
7211
 
//      return 1;
7212
 
//     }
7213
 
//   }
7214
 
//   if (arg == argc) {
7215
 
//     fprintf (stderr,_("No files to process.\n"));
7216
 
//     return 1;
7217
 
//   }
7218
 
//   if (write_to_stdout) {
7219
 
//     if (isatty(1)) {
7220
 
//       fprintf (stderr,_("Will not write an image to the terminal!\n"));
7221
 
//       return 1;
7222
 
//     }
7223
 
// #if defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__)
7224
 
//     if (setmode(1,O_BINARY) < 0) {
7225
 
//       perror("setmode()");
7226
 
//       return 1;
7227
 
//     }
7228
 
// #endif
7229
 
//   }
7230
 
//   for ( ; arg < argc; arg++) {
7231
 
//     status = 1;
7232
 
//     image = NULL;
7233
 
//     oprof = NULL;
7234
 
//     if (setjmp (failure)) {
7235
 
//       if (fileno(ifp) > 2) fclose(ifp);
7236
 
//       if (fileno(ofp) > 2) fclose(ofp);
7237
 
//       if (image) free (image);
7238
 
//       status = 1;
7239
 
//       continue;
7240
 
//     }
7241
 
//     ifname = argv[arg];
7242
 
//     if (!(ifp = fopen (ifname, "rb"))) {
7243
 
//       perror (ifname);
7244
 
//       continue;
7245
 
//     }
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;
7253
 
//     }
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);
7259
 
//       else {
7260
 
//      if (verbose)
7261
 
//        fprintf (stderr,_("%s time set to %d.\n"), ifname, (int) timestamp);
7262
 
//      ut.actime = ut.modtime = timestamp;
7263
 
//      utime (ifname, &ut);
7264
 
//       }
7265
 
//       goto next;
7266
 
//     }
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);
7271
 
//      goto next;
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;
7277
 
//      filters = 0;
7278
 
//       } else {
7279
 
//      fseek (ifp, thumb_offset, SEEK_SET);
7280
 
//      write_fun = write_thumb;
7281
 
//      goto thumbnail;
7282
 
//       }
7283
 
//     }
7284
 
//     if (load_raw == &CLASS kodak_ycbcr_load_raw) {
7285
 
//       height += height & 1;
7286
 
//       width  += width  & 1;
7287
 
//     }
7288
 
//     if (identify_only && verbose && make[0]) {
7289
 
//       printf (_("\nFilename: %s\n"), ifname);
7290
 
//       printf (_("Timestamp: %s"), ctime(&timestamp));
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');
7296
 
//       }
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) {
7319
 
//       if (verbose) {
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);
7325
 
//        } else {
7326
 
//          if (pixel_aspect < 1) iheight = iheight / pixel_aspect + 0.5;
7327
 
//          if (pixel_aspect > 1) iwidth  = iwidth  * pixel_aspect + 0.5;
7328
 
//        }
7329
 
//      }
7330
 
//      if (flip & 4)
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);
7335
 
//      if (filters) {
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)]);
7340
 
//      }
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]);
7346
 
//      }
7347
 
//      putchar ('\n');
7348
 
//       } else
7349
 
//      printf (_("%s is a %s %s image.\n"), ifname, make, model);
7350
 
// next:
7351
 
//       fclose(ifp);
7352
 
//       continue;
7353
 
//     }
7354
 
//     image = (ushort (*)[4])
7355
 
//      calloc (iheight*iwidth*sizeof *image + meta_length, 1);
7356
 
//     merror (image, "main()");
7357
 
//     meta_data = (char *) (image + iheight*iwidth);
7358
 
//     if (verbose)
7359
 
//       fprintf (stderr,
7360
 
//      _("Loading %s %s image from %s ...\n"), make, model, ifname);
7361
 
//     fseek (ifp, data_offset, SEEK_SET);
7362
 
//     (*load_raw)();
7363
 
//     bad_pixels();
7364
 
//     if (dark_frame) subtract (dark_frame);
7365
 
//     height = iheight;
7366
 
//     width  = iwidth;
7367
 
//     quality = 2 + !fuji_width;
7368
 
//     if (user_qual >= 0) quality = user_qual;
7369
 
//     if (user_black >= 0) black = user_black;
7370
 
// #ifdef COLORCHECK
7371
 
//     colorcheck();
7372
 
// #endif
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();
7383
 
//     }
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();
7387
 
// #ifndef NO_LCMS
7388
 
//     if (cam_profile) apply_profile (cam_profile, out_profile);
7389
 
// #endif
7390
 
//     convert_to_rgb();
7391
 
//     if (use_fuji_rotate) stretch();
7392
 
// thumbnail:
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";
7397
 
//     else
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"));
7403
 
//     else {
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");
7410
 
//       if (!ofp) {
7411
 
//      status = 1;
7412
 
//      perror(ofname);
7413
 
//      goto cleanup;
7414
 
//       }
7415
 
//     }
7416
 
//     if (verbose)
7417
 
//       fprintf (stderr,_("Writing data to %s ...\n"), ofname);
7418
 
//     (*write_fun)(ofp);
7419
 
//     fclose(ifp);
7420
 
//     if (ofp != stdout) fclose(ofp);
7421
 
// cleanup:
7422
 
//     if (oprof) free(oprof);
7423
 
//     free (ofname);
7424
 
//     free (image);
7425
 
//   }
7426
 
//   return status;
7427
 
// }
7428
 
 
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");
7447
 
//      }
7448
 
       }
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);
7452
 
  }
7453
 
}
7454
 
 
7455
 
pfs::Frame *readRAWfile (const char * ifname, dcraw_opts* opts) {
7456
 
  int status=0, /*user_flip=-1,*/ use_fuji_rotate=1;
7457
 
  int quality;
7458
 
 
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);
7472
 
 
7473
 
//we fix this value here, does it make sense to move it in the struct dcraw_opts?
7474
 
output_bps=16;
7475
 
 
7476
 
//
7477
 
    status = 1;
7478
 
    image = NULL;
7479
 
    oprof = NULL;
7480
 
    if (setjmp (failure)) {
7481
 
      if (fileno(ifp) > 2) fclose(ifp);
7482
 
      if (image) free (image);
7483
 
      status = 1;
7484
 
      return NULL;
7485
 
    }
7486
 
//     ifname = argv[arg];
7487
 
    if (!(ifp = fopen (ifname, "rb"))) {
7488
 
      perror (ifname);
7489
 
      return NULL;
7490
 
    }
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;
7497
 
      case  90:  flip = 6;
7498
 
    }
7499
 
//
7500
 
//     write_fun = &CLASS write_ppm_tiff;
7501
 
//
7502
 
    if (load_raw == &CLASS kodak_ycbcr_load_raw) {
7503
 
      height += height & 1;
7504
 
      width  += width  & 1;
7505
 
    }
7506
 
//
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);
7511
 
//
7512
 
    image = (ushort (*)[4])
7513
 
        calloc (iheight*iwidth*sizeof *image + meta_length, 1);
7514
 
    merror (image, "main()");
7515
 
    meta_data = (char *) (image + iheight*iwidth);
7516
 
//
7517
 
    fseek (ifp, data_offset, SEEK_SET);
7518
 
    (*load_raw)();
7519
 
    bad_pixels();
7520
 
//
7521
 
    height = iheight;
7522
 
    width  = iwidth;
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)
7527
 
        quality--;
7528
 
    fprintf(stderr,"dcraw: quality: %d\n", quality);
7529
 
//     if (user_black >= 0) black = user_black;
7530
 
//
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");
7537
 
      if (quality == 0) {
7538
 
        fprintf(stderr,"dcraw: bilinear, aka -q 0\n");
7539
 
        lin_interpolate();
7540
 
      } else if (quality == 1 || colors > 3) {
7541
 
        fprintf(stderr,"dcraw: VNG, aka -q 2\n");
7542
 
        vng_interpolate();
7543
 
      } else {
7544
 
        fprintf(stderr,"dcraw: AHD, aka -q 3\n");
7545
 
        ahd_interpolate();
7546
 
      }
7547
 
    }
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();
7551
 
//
7552
 
    convert_to_rgb();
7553
 
    if (use_fuji_rotate) stretch();
7554
 
//
7555
 
    pfs::DOMIO pfsio;
7556
 
  iheight = height;
7557
 
  iwidth  = width;
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);
7563
 
//
7564
 
    if (oprof) free(oprof);
7565
 
//     free (ofname);
7566
 
    free (image);
7567
 
    fclose(ifp);
7568
 
    return frame;
7569
 
}
7570
 
 
7571
 
}