Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

dcraw.c

Go to the documentation of this file.
00001 /*
00002    dcraw.c -- Dave Coffin's raw photo decoder
00003    Copyright 1997-2004 by Dave Coffin, dcoffin a cybercom o net
00004 
00005    This is a portable ANSI C program to convert raw image files from
00006    any digital camera into PPM format.  TIFF and CIFF parsing are
00007    based upon public specifications, but no such documentation is
00008    available for the raw sensor data, so writing this program has
00009    been an immense effort.
00010 
00011    This code is freely licensed for all uses, commercial and
00012    otherwise.  Comments, questions, and encouragement are welcome.
00013 
00014    $Revision: 1.251 $
00015    $Date: 2004/10/29 01:52:38 $
00016  */
00017 
00018 #define _GNU_SOURCE
00019 #include <ctype.h>
00020 #include <errno.h>
00021 #include <fcntl.h>
00022 #include <float.h>
00023 #include <limits.h>
00024 #include <math.h>
00025 #include <setjmp.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <time.h>
00030 /*
00031    By defining NO_JPEG, you lose only the ability to
00032    decode compressed .KDC files from the Kodak DC120.
00033  */
00034 #ifndef NO_JPEG
00035 #include <jpeglib.h>
00036 #endif
00037 
00038 #ifdef WIN32
00039 #include <winsock2.h>
00040 #pragma comment(lib, "ws2_32.lib")
00041 #define strcasecmp stricmp
00042 typedef __int64 INT64;
00043 #else
00044 #include <unistd.h>
00045 #include <netinet/in.h>
00046 typedef long long INT64;
00047 #endif
00048 
00049 #ifdef LJPEG_DECODE
00050 #error Please compile dcraw.c by itself.
00051 #error Do not link it with ljpeg_decode.
00052 #endif
00053 
00054 #ifndef LONG_BIT
00055 #define LONG_BIT (8 * sizeof (long))
00056 #endif
00057 
00058 #define ushort UshORt
00059 typedef unsigned char uchar;
00060 typedef unsigned short ushort;
00061 
00062 #define ABS_MAX ((ushort) -1)
00063 #define RGB_MAX ((ushort) -1)
00064 
00065 /*
00066    All global variables are defined here, and all functions that
00067    access them are prefixed with "CLASS".  Note that a thread-safe
00068    C++ class cannot have non-const static local variables.
00069  */
00070 
00071 
00072 
00073 // Cinelerra
00074 char dcraw_info[1024];
00075 float **dcraw_data;
00076 int dcraw_alpha;
00077 
00078 
00079 FILE *ifp;
00080 short order;
00081 char *ifname, make[64], model[64], model2[64];
00082 time_t timestamp;
00083 int data_offset, curve_offset, curve_length;
00084 int tiff_data_compression, kodak_data_compression;
00085 int raw_height, raw_width, top_margin, left_margin;
00086 int height, width, colors, black, rgb_max;
00087 int iheight, iwidth, shrink;
00088 int is_canon, is_cmy, is_foveon, use_coeff, trim, flip, xmag, ymag;
00089 int zero_after_ff;
00090 unsigned filters;
00091 ushort (*image)[4], white[8][8];
00092 void (*load_raw)();
00093 float gamma_val=0.6, bright=1.0, red_scale=1.0, blue_scale=1.0;
00094 int four_color_rgb=0, document_mode=0, quick_interpolate=0;
00095 int verbose=0, use_auto_wb=0, use_camera_wb=0, use_secondary=0;
00096 float camera_red, camera_blue;
00097 float pre_mul[4], coeff[3][4];
00098 float k1=1.5, k2=0.5, juice=0.0;
00099 int histogram[0x2000];
00100 void write_ppm(FILE *);
00101 
00102 
00103 void (*write_fun)(FILE *) = write_ppm;
00104 
00105 
00106 jmp_buf failure;
00107 
00108 float green_scale=1.0;
00109 float saturation = 1.0;
00110 float contrast = 1.0;
00111 int autoexposure=0, use_pivot=0, use_neutral_wb=0;
00112 int alternate_scale = 0;
00113 int center_weight = 0;
00114 int use_camera_black = 1;
00115 int user_black = -1;
00116 float pivot_value = 0.75, exposure_compensation=0.0;
00117 unsigned pivot_point[4], pivot_base[4];
00118 float white_point_fraction = 0.99;
00119 
00120 struct decode {
00121   struct decode *branch[2];
00122   int leaf;
00123 } first_decode[2048], *second_decode, *free_decode;
00124 
00125 #define CLASS
00126 
00127 /*
00128    In order to inline this calculation, I make the risky
00129    assumption that all filter patterns can be described
00130    by a repeating pattern of eight rows and two columns
00131 
00132    Return values are either 0/1/2/3 = G/M/C/Y or 0/1/2/3 = R/G1/B/G2
00133  */
00134 #define FC(row,col) \
00135         (filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
00136 
00137 #define BAYER(row,col) \
00138         image[((row) >> shrink)*iwidth + ((col) >> shrink)][FC(row,col)]
00139 
00140 /*
00141    PowerShot 600 uses 0xe1e4e1e4:
00142 
00143           0 1 2 3 4 5
00144         0 G M G M G M
00145         1 C Y C Y C Y
00146         2 M G M G M G
00147         3 C Y C Y C Y
00148 
00149    PowerShot A5 uses 0x1e4e1e4e:
00150 
00151           0 1 2 3 4 5
00152         0 C Y C Y C Y
00153         1 G M G M G M
00154         2 C Y C Y C Y
00155         3 M G M G M G
00156 
00157    PowerShot A50 uses 0x1b4e4b1e:
00158 
00159           0 1 2 3 4 5
00160         0 C Y C Y C Y
00161         1 M G M G M G
00162         2 Y C Y C Y C
00163         3 G M G M G M
00164         4 C Y C Y C Y
00165         5 G M G M G M
00166         6 Y C Y C Y C
00167         7 M G M G M G
00168 
00169    PowerShot Pro70 uses 0x1e4b4e1b:
00170 
00171           0 1 2 3 4 5
00172         0 Y C Y C Y C
00173         1 M G M G M G
00174         2 C Y C Y C Y
00175         3 G M G M G M
00176         4 Y C Y C Y C
00177         5 G M G M G M
00178         6 C Y C Y C Y
00179         7 M G M G M G
00180 
00181    PowerShots Pro90 and G1 use 0xb4b4b4b4:
00182 
00183           0 1 2 3 4 5
00184         0 G M G M G M
00185         1 Y C Y C Y C
00186 
00187    All RGB cameras use one of these Bayer grids:
00188 
00189         0x16161616:     0x61616161:     0x49494949:     0x94949494:
00190 
00191           0 1 2 3 4 5     0 1 2 3 4 5     0 1 2 3 4 5     0 1 2 3 4 5
00192         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
00193         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
00194         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
00195         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
00196 
00197  */
00198 
00199 #ifndef __GLIBC__
00200 char *memmem (char *haystack, size_t haystacklen,
00201               char *needle, size_t needlelen)
00202 {
00203   char *c;
00204   for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
00205     if (!memcmp (c, needle, needlelen))
00206       return c;
00207   return NULL;
00208 }
00209 #endif
00210 
00211 void CLASS merror (void *ptr, char *where)
00212 {
00213   if (ptr) return;
00214   fprintf (stderr, "%s: Out of memory in %s\n", ifname, where);
00215   longjmp (failure, 1);
00216 }
00217 
00218 /*
00219    Get a 2-byte integer, making no assumptions about CPU byte order.
00220    Nor should we assume that the compiler evaluates left-to-right.
00221  */
00222 ushort CLASS fget2 (FILE *f)
00223 {
00224   uchar a, b;
00225 
00226   a = fgetc(f);
00227   b = fgetc(f);
00228   if (order == 0x4949)          /* "II" means little-endian */
00229     return a + (b << 8);
00230   else                          /* "MM" means big-endian */
00231     return (a << 8) + b;
00232 }
00233 
00234 /*
00235    Same for a 4-byte integer.
00236  */
00237 int CLASS fget4 (FILE *f)
00238 {
00239   uchar a, b, c, d;
00240 
00241   a = fgetc(f);
00242   b = fgetc(f);
00243   c = fgetc(f);
00244   d = fgetc(f);
00245   if (order == 0x4949)
00246     return a + (b << 8) + (c << 16) + (d << 24);
00247   else
00248     return (a << 24) + (b << 16) + (c << 8) + d;
00249 }
00250 
00251 void CLASS canon_600_load_raw()
00252 {
00253   uchar  data[1120], *dp;
00254   ushort pixel[896], *pix;
00255   int irow, orow, col;
00256 
00257   for (irow=orow=0; irow < height; irow++)
00258   {
00259     fread (data, 1120, 1, ifp);
00260     for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8)
00261     {
00262       pix[0] = (dp[0] << 2) + (dp[1] >> 6    );
00263       pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
00264       pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
00265       pix[3] = (dp[4] << 2) + (dp[1]      & 3);
00266       pix[4] = (dp[5] << 2) + (dp[9]      & 3);
00267       pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
00268       pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
00269       pix[7] = (dp[8] << 2) + (dp[9] >> 6    );
00270     }
00271     for (col=0; col < width; col++)
00272       BAYER(orow,col) = pixel[col] << 4;
00273     for (col=width; col < 896; col++)
00274       black += pixel[col];
00275     if ((orow+=2) > height)
00276       orow = 1;
00277   }
00278   black = ((INT64) black << 4) / ((896 - width) * height);
00279 }
00280 
00281 void CLASS canon_a5_load_raw()
00282 {
00283   uchar  data[1940], *dp;
00284   ushort pixel[1552], *pix;
00285   int row, col;
00286 
00287   for (row=0; row < height; row++) {
00288     fread (data, raw_width * 10 / 8, 1, ifp);
00289     for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=10, pix+=8)
00290     {
00291       pix[0] = (dp[1] << 2) + (dp[0] >> 6);
00292       pix[1] = (dp[0] << 4) + (dp[3] >> 4);
00293       pix[2] = (dp[3] << 6) + (dp[2] >> 2);
00294       pix[3] = (dp[2] << 8) + (dp[5]     );
00295       pix[4] = (dp[4] << 2) + (dp[7] >> 6);
00296       pix[5] = (dp[7] << 4) + (dp[6] >> 4);
00297       pix[6] = (dp[6] << 6) + (dp[9] >> 2);
00298       pix[7] = (dp[9] << 8) + (dp[8]     );
00299     }
00300     for (col=0; col < width; col++)
00301       BAYER(row,col) = (pixel[col] & 0x3ff) << 4;
00302     for (col=width; col < raw_width; col++)
00303       black += pixel[col] & 0x3ff;
00304   }
00305   if (raw_width > width)
00306     black = ((INT64) black << 4) / ((raw_width - width) * height);
00307 }
00308 
00309 /*
00310    getbits(-1) initializes the buffer
00311    getbits(n) where 0 <= n <= 25 returns an n-bit integer
00312  */
00313 unsigned CLASS getbits (int nbits)
00314 {
00315   static unsigned long bitbuf=0;
00316   static int vbits=0;
00317   unsigned c, ret;
00318 
00319   if (nbits == 0) return 0;
00320   if (nbits == -1)
00321     ret = bitbuf = vbits = 0;
00322   else {
00323     ret = bitbuf << (LONG_BIT - vbits) >> (LONG_BIT - nbits);
00324     vbits -= nbits;
00325   }
00326   while (vbits < LONG_BIT - 7) {
00327     c = fgetc(ifp);
00328     bitbuf = (bitbuf << 8) + c;
00329     if (c == 0xff && zero_after_ff)
00330       fgetc(ifp);
00331     vbits += 8;
00332   }
00333   return ret;
00334 }
00335 
00336 void CLASS init_decoder ()
00337 {
00338   memset (first_decode, 0, sizeof first_decode);
00339   free_decode = first_decode;
00340 }
00341 
00342 /*
00343    Construct a decode tree according the specification in *source.
00344    The first 16 bytes specify how many codes should be 1-bit, 2-bit
00345    3-bit, etc.  Bytes after that are the leaf values.
00346 
00347    For example, if the source is
00348 
00349     { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
00350       0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff  },
00351 
00352    then the code is
00353 
00354         00              0x04
00355         010             0x03
00356         011             0x05
00357         100             0x06
00358         101             0x02
00359         1100            0x07
00360         1101            0x01
00361         11100           0x08
00362         11101           0x09
00363         11110           0x00
00364         111110          0x0a
00365         1111110         0x0b
00366         1111111         0xff
00367  */
00368 uchar * CLASS make_decoder (const uchar *source, int level)
00369 {
00370   struct decode *cur;
00371   static int leaf;
00372   int i, next;
00373 
00374   if (level==0) leaf=0;
00375   cur = free_decode++;
00376   if (free_decode > first_decode+2048) {
00377     fprintf (stderr, "%s: decoder table overflow\n", ifname);
00378     longjmp (failure, 2);
00379   }
00380   for (i=next=0; i <= leaf && next < 16; )
00381     i += source[next++];
00382   if (i > leaf) {
00383     if (level < next) {
00384       cur->branch[0] = free_decode;
00385       make_decoder (source, level+1);
00386       cur->branch[1] = free_decode;
00387       make_decoder (source, level+1);
00388     } else
00389       cur->leaf = source[16 + leaf++];
00390   }
00391   return (uchar *) source + 16 + leaf;
00392 }
00393 
00394 void CLASS crw_init_tables (unsigned table)
00395 {
00396   static const uchar first_tree[3][29] = {
00397     { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
00398       0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff  },
00399 
00400     { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
00401       0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff  },
00402 
00403     { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
00404       0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff  },
00405   };
00406 
00407   static const uchar second_tree[3][180] = {
00408     { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
00409       0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
00410       0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
00411       0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
00412       0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
00413       0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
00414       0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
00415       0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
00416       0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
00417       0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
00418       0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
00419       0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
00420       0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
00421       0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
00422       0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff  },
00423 
00424     { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
00425       0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
00426       0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
00427       0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
00428       0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
00429       0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
00430       0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
00431       0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
00432       0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
00433       0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
00434       0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
00435       0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
00436       0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
00437       0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
00438       0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff  },
00439 
00440     { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
00441       0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
00442       0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
00443       0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
00444       0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
00445       0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
00446       0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
00447       0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
00448       0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
00449       0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
00450       0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
00451       0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
00452       0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
00453       0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
00454       0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff  }
00455   };
00456 
00457   if (table > 2) table = 2;
00458   init_decoder();
00459   make_decoder ( first_tree[table], 0);
00460   second_decode = free_decode;
00461   make_decoder (second_tree[table], 0);
00462 }
00463 
00464 /*
00465    Return 0 if the image starts with compressed data,
00466    1 if it starts with uncompressed low-order bits.
00467 
00468    In Canon compressed data, 0xff is always followed by 0x00.
00469  */
00470 int CLASS canon_has_lowbits()
00471 {
00472   uchar test[0x4000];
00473   int ret=1, i;
00474 
00475   fseek (ifp, 0, SEEK_SET);
00476   fread (test, 1, sizeof test, ifp);
00477   for (i=540; i < sizeof test - 1; i++)
00478     if (test[i] == 0xff) {
00479       if (test[i+1]) return 1;
00480       ret=0;
00481     }
00482   return ret;
00483 }
00484 
00485 void CLASS canon_compressed_load_raw()
00486 {
00487   ushort *pixel, *prow;
00488   int lowbits, shift, i, row, r, col, save, val;
00489   unsigned irow, icol;
00490   struct decode *decode, *dindex;
00491   int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
00492   uchar c;
00493   INT64 bblack=0;
00494 
00495   pixel = calloc (raw_width*8, sizeof *pixel);
00496   merror (pixel, "canon_compressed_load_raw()");
00497   lowbits = canon_has_lowbits();
00498   shift = 4 - lowbits*2;
00499   fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
00500   zero_after_ff = 1;
00501   getbits(-1);
00502 
00503   for (row = 0; row < raw_height; row += 8) {
00504     for (block=0; block < raw_width >> 3; block++) {
00505       memset (diffbuf, 0, sizeof diffbuf);
00506       decode = first_decode;
00507       for (i=0; i < 64; i++ ) {
00508         for (dindex=decode; dindex->branch[0]; )
00509           dindex = dindex->branch[getbits(1)];
00510         leaf = dindex->leaf;
00511         decode = second_decode;
00512         if (leaf == 0 && i) break;
00513         if (leaf == 0xff) continue;
00514         i  += leaf >> 4;
00515         len = leaf & 15;
00516         if (len == 0) continue;
00517         diff = getbits(len);
00518         if ((diff & (1 << (len-1))) == 0)
00519           diff -= (1 << len) - 1;
00520         if (i < 64) diffbuf[i] = diff;
00521       }
00522       diffbuf[0] += carry;
00523       carry = diffbuf[0];
00524       for (i=0; i < 64; i++ ) {
00525         if (pnum++ % raw_width == 0)
00526           base[0] = base[1] = 512;
00527         pixel[(block << 6) + i] = ( base[i & 1] += diffbuf[i] );
00528       }
00529     }
00530     if (lowbits) {
00531       save = ftell(ifp);                        /* Don't lose our place */
00532       fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
00533       for (prow=pixel, i=0; i < raw_width*2; i++) {
00534         c = fgetc(ifp);
00535         for (r=0; r < 8; r+=2, prow++) {
00536           val = (*prow << 2) + ((c >> r) & 3);
00537           if (raw_width == 2672 && val < 512) val += 2;
00538           *prow = val;
00539         }
00540       }
00541       fseek (ifp, save, SEEK_SET);
00542     }
00543     for (r=0; r < 8; r++) {
00544       irow = row - top_margin + r;
00545       if (irow >= height) continue;
00546       for (col = 0; col < raw_width; col++) {
00547         icol = col - left_margin;
00548         if (icol < width)
00549           BAYER(irow,icol) = pixel[r*raw_width+col] << shift;
00550         else
00551           bblack += pixel[r*raw_width+col];
00552       }
00553     }
00554   }
00555   free(pixel);
00556   if (raw_width > width)
00557     black = (bblack << shift) / ((raw_width - width) * height);
00558 }
00559 
00560 void CLASS kodak_curve (ushort *curve)
00561 {
00562   int i, entries, tag, type, len, val;
00563 
00564   for (i=0; i < 0x1000; i++)
00565     curve[i] = i;
00566   if (strcasecmp(make,"KODAK")) return;
00567   if (!curve_offset) {
00568     fseek (ifp, 12, SEEK_SET);
00569     entries = fget2(ifp);
00570     while (entries--) {
00571       tag  = fget2(ifp);
00572       type = fget2(ifp);
00573       len  = fget4(ifp);
00574       val  = fget4(ifp);
00575       if (tag == 0x90d) {
00576         curve_offset = val;
00577         curve_length = len;
00578       }
00579     }
00580   }
00581   if (curve_offset) {
00582     fseek (ifp, curve_offset, SEEK_SET);
00583     for (i=0; i < curve_length; i++)
00584       curve[i] = fget2(ifp);
00585     for ( ; i < 0x1000; i++)
00586       curve[i] = curve[i-1];
00587     rgb_max = curve[i-1] << 2;
00588   }
00589   fseek (ifp, data_offset, SEEK_SET);
00590 }
00591 
00592 /*
00593    Not a full implementation of Lossless JPEG,
00594    just enough to decode Canon and Kodak images.
00595  */
00596 void CLASS lossless_jpeg_load_raw()
00597 {
00598   int tag, len, jhigh=0, jwide=0, jrow, jcol, jidx, diff, i, row, col;
00599   uchar data[256], *dp;
00600   int vpred[2] = { 0x800, 0x800 }, hpred[2];
00601   struct decode *dstart[2], *dindex;
00602   ushort curve[0x10000];
00603   INT64 bblack=0;
00604   int min=INT_MAX;
00605 
00606   kodak_curve(curve);
00607   order = 0x4d4d;
00608   if (fget2(ifp) != 0xffd8) return;
00609 
00610   do {
00611     tag = fget2(ifp);
00612     len = fget2(ifp) - 2;
00613     if (tag <= 0xff00 || len > 255) return;
00614     fread (data, 1, len, ifp);
00615     switch (tag) {
00616       case 0xffc3:
00617         jhigh = (data[1] << 8) + data[2];
00618         jwide =((data[3] << 8) + data[4])*2;
00619         break;
00620       case 0xffc4:
00621         init_decoder();
00622         dstart[0] = dstart[1] = free_decode;
00623         for (dp = data; dp < data+len && *dp < 2; ) {
00624           dstart[*dp] = free_decode;
00625           dp = make_decoder (++dp, 0);
00626         }
00627     }
00628   } while (tag != 0xffda);
00629 
00630   zero_after_ff = 1;
00631   getbits(-1);
00632   for (jrow=0; jrow < jhigh; jrow++)
00633   {
00634  
00635  
00636  
00637  
00638     for (jcol=0; jcol < jwide; jcol++)
00639     {
00640       for (dindex = dstart[jcol & 1]; dindex->branch[0]; )
00641         dindex = dindex->branch[getbits(1)];
00642       len = dindex->leaf;
00643       diff = getbits(len);
00644       if ((diff & (1 << (len-1))) == 0)
00645         diff -= (1 << len) - 1;
00646       if (jcol < 2) {
00647         vpred[jcol] += diff;
00648         hpred[jcol] = vpred[jcol];
00649       } else
00650         hpred[jcol & 1] += diff;
00651       diff = hpred[jcol & 1];
00652       if (diff < 0) diff = 0;
00653       jidx = jrow*jwide + jcol;
00654       if (raw_width == 5108) {
00655         i = jidx / (1680*jhigh);
00656         if (i < 2) {
00657           row = jidx / 1680 % jhigh;
00658           col = jidx % 1680 + i*1680;
00659         } else {
00660           jidx -= 2*1680*jhigh;
00661           row = jidx / 1748;
00662           col = jidx % 1748 + 2*1680;
00663         }
00664       } else {
00665         row = jidx / raw_width;
00666         col = jidx % raw_width;
00667       }
00668       if ((unsigned) (row-top_margin) >= height)
00669         continue;
00670       if ((unsigned) (col-left_margin) < width) {
00671         BAYER(row-top_margin,col-left_margin) = curve[diff] << 2;
00672         if (min > curve[diff])
00673             min = curve[diff];
00674       } else
00675         bblack += curve[diff];
00676     }
00677   }
00678 
00679 
00680 
00681 
00682 
00683 
00684 
00685 
00686 
00687 
00688 
00689   if (raw_width > width)
00690     black = (bblack << 2) / ((raw_width - width) * height);
00691   if (!strcasecmp(make,"KODAK"))
00692     black = min << 2;
00693 }
00694 
00695 void CLASS nikon_compressed_load_raw()
00696 {
00697   static const uchar nikon_tree[] = {
00698     0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0,
00699     5,4,3,6,2,7,1,0,8,9,11,10,12
00700   };
00701   int vpred[4], hpred[2], csize, row, col, i, len, diff;
00702   ushort *curve;
00703   struct decode *dindex;
00704 
00705   init_decoder();
00706   make_decoder (nikon_tree, 0);
00707 
00708   fseek (ifp, curve_offset, SEEK_SET);
00709   for (i=0; i < 4; i++)
00710     vpred[i] = fget2(ifp);
00711   csize = fget2(ifp);
00712   curve = calloc (csize, sizeof *curve);
00713   merror (curve, "nikon_compressed_load_raw()");
00714   for (i=0; i < csize; i++)
00715     curve[i] = fget2(ifp);
00716 
00717   fseek (ifp, data_offset, SEEK_SET);
00718   getbits(-1);
00719 
00720   for (row=0; row < height; row++)
00721     for (col=0; col < raw_width; col++)
00722     {
00723       for (dindex=first_decode; dindex->branch[0]; )
00724         dindex = dindex->branch[getbits(1)];
00725       len = dindex->leaf;
00726       diff = getbits(len);
00727       if ((diff & (1 << (len-1))) == 0)
00728         diff -= (1 << len) - 1;
00729       if (col < 2) {
00730         i = 2*(row & 1) + (col & 1);
00731         vpred[i] += diff;
00732         hpred[col] = vpred[i];
00733       } else
00734         hpred[col & 1] += diff;
00735       if ((unsigned) (col-left_margin) >= width) continue;
00736       diff = hpred[col & 1];
00737       if (diff < 0) diff = 0;
00738       if (diff >= csize) diff = csize-1;
00739       BAYER(row,col-left_margin) = curve[diff] << 2;
00740     }
00741   free(curve);
00742 }
00743 
00744 void CLASS nikon_load_raw()
00745 {
00746   int irow, row, col, i;
00747 
00748   getbits(-1);
00749   for (irow=0; irow < height; irow++) {
00750     row = irow;
00751     if (model[0] == 'E') {
00752       row = irow * 2 % height + irow / (height/2);
00753       if (row == 1 && atoi(model+1) < 5000) {
00754         fseek (ifp, 0, SEEK_END);
00755         fseek (ifp, ftell(ifp)/2, SEEK_SET);
00756         getbits(-1);
00757       }
00758     }
00759     for (col=0; col < raw_width; col++) {
00760       i = getbits(12);
00761       if ((unsigned) (col-left_margin) < width)
00762         BAYER(row,col-left_margin) = i << 2;
00763       if (tiff_data_compression == 34713 && (col % 10) == 9)
00764         getbits(8);
00765     }
00766   }
00767 }
00768 
00769 /*
00770    Figure out if a NEF file is compressed.  These fancy heuristics
00771    are only needed for the D100, thanks to a bug in some cameras
00772    that tags all images as "compressed".
00773  */
00774 int CLASS nikon_is_compressed()
00775 {
00776   uchar test[256];
00777   int i;
00778 
00779   if (tiff_data_compression != 34713)
00780     return 0;
00781   if (strcmp(model,"D100"))
00782     return 1;
00783   fseek (ifp, data_offset, SEEK_SET);
00784   fread (test, 1, 256, ifp);
00785   for (i=15; i < 256; i+=16)
00786     if (test[i]) return 1;
00787   return 0;
00788 }
00789 
00790 /*
00791    Returns 1 for a Coolpix 990, 0 for a Coolpix 995.
00792  */
00793 int CLASS nikon_e990()
00794 {
00795   int i, histo[256];
00796   const uchar often[] = { 0x00, 0x55, 0xaa, 0xff };
00797 
00798   memset (histo, 0, sizeof histo);
00799   fseek (ifp, 2064*1540*3/4, SEEK_SET);
00800   for (i=0; i < 2000; i++)
00801     histo[fgetc(ifp)]++;
00802   for (i=0; i < 4; i++)
00803     if (histo[often[i]] > 400)
00804       return 1;
00805   return 0;
00806 }
00807 
00808 /*
00809    Returns 1 for a Coolpix 2100, 0 for anything else.
00810  */
00811 int CLASS nikon_e2100()
00812 {
00813   uchar t[12];
00814   int i;
00815 
00816   fseek (ifp, 0, SEEK_SET);
00817   for (i=0; i < 1024; i++) {
00818     fread (t, 1, 12, ifp);
00819     if (((t[2] & t[4] & t[7] & t[9]) >> 4
00820         & t[1] & t[6] & t[8] & t[11] & 3) != 3)
00821       return 0;
00822   }
00823   return 1;
00824 }
00825 
00826 /*
00827    Separates a Minolta DiMAGE Z2 from a Nikon E4300.
00828  */
00829 int CLASS minolta_z2()
00830 {
00831   int i;
00832   char tail[424];
00833 
00834   fseek (ifp, -sizeof tail, SEEK_END);
00835   fread (tail, 1, sizeof tail, ifp);
00836   for (i=0; i < sizeof tail; i++)
00837     if (tail[i]) return 1;
00838   return 0;
00839 }
00840 
00841 void CLASS nikon_e2100_load_raw()
00842 {
00843   uchar   data[3432], *dp;
00844   ushort pixel[2288], *pix;
00845   int row, col;
00846 
00847   for (row=0; row <= height; row+=2) {
00848     if (row == height) {
00849       fseek (ifp, width==1616 ? 8792:424, SEEK_CUR);
00850       row = 1;
00851     }
00852     fread (data, 1, width*3/2, ifp);
00853     for (dp=data, pix=pixel; pix < pixel+width; dp+=12, pix+=8) {
00854       pix[0] = (dp[2] >> 4) + (dp[ 3] << 4);
00855       pix[1] = (dp[2] << 8) +  dp[ 1];
00856       pix[2] = (dp[7] >> 4) + (dp[ 0] << 4);
00857       pix[3] = (dp[7] << 8) +  dp[ 6];
00858       pix[4] = (dp[4] >> 4) + (dp[ 5] << 4);
00859       pix[5] = (dp[4] << 8) +  dp[11];
00860       pix[6] = (dp[9] >> 4) + (dp[10] << 4);
00861       pix[7] = (dp[9] << 8) +  dp[ 8];
00862     }
00863     for (col=0; col < width; col++)
00864       BAYER(row,col) = (pixel[col] & 0xfff) << 2;
00865   }
00866 }
00867 
00868 void CLASS nikon_e950_load_raw()
00869 {
00870   int irow, row, col;
00871 
00872   getbits(-1);
00873   for (irow=0; irow < height; irow++) {
00874     row = irow * 2 % height;
00875     for (col=0; col < width; col++)
00876       BAYER(row,col) = getbits(10) << 4;
00877     for (col=28; col--; )
00878       getbits(8);
00879   }
00880 }
00881 
00882 /*
00883    The Fuji Super CCD is just a Bayer grid rotated 45 degrees.
00884  */
00885 void CLASS fuji_s2_load_raw()
00886 {
00887   ushort pixel[2944];
00888   int row, col, r, c;
00889 
00890   fseek (ifp, (2944*24+32)*2, SEEK_CUR);
00891   for (row=0; row < 2144; row++) {
00892     fread (pixel, 2, 2944, ifp);
00893     for (col=0; col < 2880; col++) {
00894       r = row + ((col+1) >> 1);
00895       c = 2143 - row + (col >> 1);
00896       BAYER(r,c) = ntohs(pixel[col]) << 2;
00897     }
00898   }
00899 }
00900 
00901 void CLASS fuji_common_load_raw (int ncol, int icol, int nrow)
00902 {
00903   ushort pixel[2048];
00904   int row, col, r, c;
00905 
00906   for (row=0; row < nrow; row++) {
00907     fread (pixel, 2, ncol, ifp);
00908     if (ntohs(0xaa55) == 0xaa55)        /* data is little-endian */
00909       swab (pixel, pixel, ncol*2);
00910     for (col=0; col <= icol; col++) {
00911       r = icol - col + (row >> 1);
00912       c = col + ((row+1) >> 1);
00913       BAYER(r,c) = pixel[col] << 2;
00914     }
00915   }
00916 }
00917 
00918 void CLASS fuji_s5000_load_raw()
00919 {
00920   fseek (ifp, (1472*4+24)*2, SEEK_CUR);
00921   fuji_common_load_raw (1472, 1423, 2152);
00922 }
00923 
00924 void CLASS fuji_s7000_load_raw()
00925 {
00926   fuji_common_load_raw (2048, 2047, 3080);
00927 }
00928 
00929 /*
00930    The Fuji Super CCD SR has two photodiodes for each pixel.
00931    The secondary has about 1/16 the sensitivity of the primary,
00932    but this ratio may vary.
00933  */
00934 void CLASS fuji_f700_load_raw()
00935 {
00936   ushort pixel[2944];
00937   int row, col, r, c, val;
00938 
00939   for (row=0; row < 2168; row++) {
00940     fread (pixel, 2, 2944, ifp);
00941     if (ntohs(0xaa55) == 0xaa55)        /* data is little-endian */
00942       swab (pixel, pixel, 2944*2);
00943     for (col=0; col < 1440; col++) {
00944       r = 1439 - col + (row >> 1);
00945       c = col + ((row+1) >> 1);
00946       val = pixel[col+16 + use_secondary*1472];
00947       BAYER(r,c) = val;
00948     }
00949   }
00950 }
00951 
00952 void CLASS rollei_load_raw()
00953 {
00954   uchar pixel[10];
00955   unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
00956 
00957   isix = raw_width * raw_height * 5 / 8;
00958   while (fread (pixel, 1, 10, ifp) == 10) {
00959     for (i=0; i < 10; i+=2) {
00960       todo[i]   = iten++;
00961       todo[i+1] = pixel[i] << 8 | pixel[i+1];
00962       buffer    = pixel[i] >> 2 | buffer << 6;
00963     }
00964     for (   ; i < 16; i+=2) {
00965       todo[i]   = isix++;
00966       todo[i+1] = buffer >> (14-i)*5;
00967     }
00968     for (i=0; i < 16; i+=2) {
00969       row = todo[i] / raw_width - top_margin;
00970       col = todo[i] % raw_width - left_margin;
00971       if (row < height && col < width)
00972         BAYER(row,col) = (todo[i+1] & 0x3ff) << 4;
00973     }
00974   }
00975 }
00976 
00977 void CLASS phase_one_load_raw()
00978 {
00979   int row, col, a, b;
00980   ushort pixel[4134], akey, bkey;
00981 
00982   fseek (ifp, 8, SEEK_CUR);
00983   fseek (ifp, fget4(ifp) + 296, SEEK_CUR);
00984   akey = fget2(ifp);
00985   bkey = fget2(ifp);
00986   fseek (ifp, data_offset + 12 + top_margin*raw_width*2, SEEK_SET);
00987   for (row=0; row < height; row++) {
00988     fread (pixel, 2, raw_width, ifp);
00989     for (col=0; col < raw_width; col+=2) {
00990       a = ntohs(pixel[col+0]) ^ akey;
00991       b = ntohs(pixel[col+1]) ^ bkey;
00992       pixel[col+0] = (b & 0xaaaa) | (a & 0x5555);
00993       pixel[col+1] = (a & 0xaaaa) | (b & 0x5555);
00994     }
00995     for (col=0; col < width; col++)
00996       BAYER(row,col) = pixel[col+left_margin];
00997   }
00998 }
00999 
01000 void CLASS ixpress_load_raw()
01001 {
01002   ushort pixel[4090];
01003   int row, col;
01004 
01005   fseek (ifp, 304 + 6*2*4090, SEEK_SET);
01006   for (row=height; --row >= 0; ) {
01007     fread (pixel, 2, 4090, ifp);
01008     if (ntohs(0xaa55) == 0xaa55)        /* data is little-endian */
01009       swab (pixel, pixel, 4090*2);
01010     for (col=0; col < width; col++)
01011       BAYER(row,col) = pixel[width-1-col];
01012   }
01013 }
01014 
01015 /* For this function only, raw_width is in bytes, not pixels! */
01016 void CLASS packed_12_load_raw()
01017 {
01018   int row, col;
01019 
01020   getbits(-1);
01021   for (row=0; row < height; row++) {
01022     for (col=0; col < width; col++)
01023       BAYER(row,col) = getbits(12) << 2;
01024     for (col = width*3/2; col < raw_width; col++)
01025       getbits(8);
01026   }
01027 }
01028 
01029 void CLASS unpacked_load_raw (int order, int rsh)
01030 {
01031   ushort *pixel;
01032   int row, col;
01033 
01034   pixel = calloc (raw_width, sizeof *pixel);
01035   merror (pixel, "unpacked_load_raw()");
01036   for (row=0; row < height; row++) {
01037     fread (pixel, 2, raw_width, ifp);
01038     if (order != ntohs(0x55aa))
01039       swab (pixel, pixel, width*2);
01040     for (col=0; col < width; col++)
01041       BAYER(row,col) = pixel[col] << 8 >> (8+rsh);
01042   }
01043   free(pixel);
01044 }
01045 
01046 void CLASS be_16_load_raw()             /* "be" = "big-endian" */
01047 {
01048   unpacked_load_raw (0x55aa, 0);
01049 }
01050 
01051 void CLASS be_high_12_load_raw()
01052 {
01053   unpacked_load_raw (0x55aa, 2);
01054 }
01055 
01056 void CLASS be_low_12_load_raw()
01057 {
01058   unpacked_load_raw (0x55aa,-2);
01059 }
01060 
01061 void CLASS be_low_10_load_raw()
01062 {
01063   unpacked_load_raw (0x55aa,-4);
01064 }
01065 
01066 void CLASS le_high_12_load_raw()        /* "le" = "little-endian" */
01067 {
01068   unpacked_load_raw (0xaa55, 2);
01069 }
01070 
01071 void CLASS olympus_cseries_load_raw()
01072 {
01073   int irow, row, col;
01074 
01075   for (irow=0; irow < height; irow++) {
01076     row = irow * 2 % height + irow / (height/2);
01077     if (row < 2) {
01078       fseek (ifp, data_offset - row*(-width*height*3/4 & -2048), SEEK_SET);
01079       getbits(-1);
01080     }
01081     for (col=0; col < width; col++)
01082       BAYER(row,col) = getbits(12) << 2;
01083   }
01084 }
01085 
01086 void CLASS eight_bit_load_raw()
01087 {
01088   uchar *pixel;
01089   int row, col;
01090 
01091   pixel = calloc (raw_width, sizeof *pixel);
01092   merror (pixel, "eight_bit_load_raw()");
01093   for (row=0; row < height; row++) {
01094     fread (pixel, 1, raw_width, ifp);
01095     for (col=0; col < width; col++)
01096       BAYER(row,col) = pixel[col] << 6;
01097   }
01098   free (pixel);
01099 }
01100 
01101 void CLASS casio_qv5700_load_raw()
01102 {
01103   uchar  data[3232],  *dp;
01104   ushort pixel[2576], *pix;
01105   int row, col;
01106 
01107   for (row=0; row < height; row++) {
01108     fread (data, 1, 3232, ifp);
01109     for (dp=data, pix=pixel; dp < data+3220; dp+=5, pix+=4) {
01110       pix[0] = (dp[0] << 2) + (dp[1] >> 6);
01111       pix[1] = (dp[1] << 4) + (dp[2] >> 4);
01112       pix[2] = (dp[2] << 6) + (dp[3] >> 2);
01113       pix[3] = (dp[3] << 8) + (dp[4]     );
01114     }
01115     for (col=0; col < width; col++)
01116       BAYER(row,col) = (pixel[col] & 0x3ff) << 4;
01117   }
01118 }
01119 
01120 void CLASS nucore_load_raw()
01121 {
01122   uchar *data, *dp;
01123   int irow, row, col;
01124 
01125   data = calloc (width, 2);
01126   merror (data, "nucore_load_raw()");
01127   for (irow=0; irow < height; irow++) {
01128     fread (data, 2, width, ifp);
01129     if (model[0] == 'B' && width == 2598)
01130       row = height - 1 - irow/2 - height/2 * (irow & 1);
01131     else
01132       row = irow;
01133     for (dp=data, col=0; col < width; col++, dp+=2)
01134       BAYER(row,col) = (dp[0] << 2) + (dp[1] << 10);
01135   }
01136   free(data);
01137 }
01138 
01139 const int * CLASS make_decoder_int (const int *source, int level)
01140 {
01141   struct decode *cur;
01142 
01143   cur = free_decode++;
01144   if (level < source[0]) {
01145     cur->branch[0] = free_decode;
01146     source = make_decoder_int (source, level+1);
01147     cur->branch[1] = free_decode;
01148     source = make_decoder_int (source, level+1);
01149   } else {
01150     cur->leaf = source[1];
01151     source += 2;
01152   }
01153   return source;
01154 }
01155 
01156 int CLASS radc_token (int tree)
01157 {
01158   int t;
01159   static struct decode *dstart[18], *dindex;
01160   static const int *s, source[] = {
01161     1,1, 2,3, 3,4, 4,2, 5,7, 6,5, 7,6, 7,8,
01162     1,0, 2,1, 3,3, 4,4, 5,2, 6,7, 7,6, 8,5, 8,8,
01163     2,1, 2,3, 3,0, 3,2, 3,4, 4,6, 5,5, 6,7, 6,8,
01164     2,0, 2,1, 2,3, 3,2, 4,4, 5,6, 6,7, 7,5, 7,8,
01165     2,1, 2,4, 3,0, 3,2, 3,3, 4,7, 5,5, 6,6, 6,8,
01166     2,3, 3,1, 3,2, 3,4, 3,5, 3,6, 4,7, 5,0, 5,8,
01167     2,3, 2,6, 3,0, 3,1, 4,4, 4,5, 4,7, 5,2, 5,8,
01168     2,4, 2,7, 3,3, 3,6, 4,1, 4,2, 4,5, 5,0, 5,8,
01169     2,6, 3,1, 3,3, 3,5, 3,7, 3,8, 4,0, 5,2, 5,4,
01170     2,0, 2,1, 3,2, 3,3, 4,4, 4,5, 5,6, 5,7, 4,8,
01171     1,0, 2,2, 2,-2,
01172     1,-3, 1,3,
01173     2,-17, 2,-5, 2,5, 2,17,
01174     2,-7, 2,2, 2,9, 2,18,
01175     2,-18, 2,-9, 2,-2, 2,7,
01176     2,-28, 2,28, 3,-49, 3,-9, 3,9, 4,49, 5,-79, 5,79,
01177     2,-1, 2,13, 2,26, 3,39, 4,-16, 5,55, 6,-37, 6,76,
01178     2,-26, 2,-13, 2,1, 3,-39, 4,16, 5,-55, 6,-76, 6,37
01179   };
01180 
01181   if (free_decode == first_decode)
01182     for (s=source, t=0; t < 18; t++) {
01183       dstart[t] = free_decode;
01184       s = make_decoder_int (s, 0);
01185     }
01186   if (tree == 18) {
01187     if (model[2] == '4')
01188       return (getbits(5) << 3) + 4;     /* DC40 */
01189     else
01190       return (getbits(6) << 2) + 2;     /* DC50 */
01191   }
01192   for (dindex = dstart[tree]; dindex->branch[0]; )
01193     dindex = dindex->branch[getbits(1)];
01194   return dindex->leaf;
01195 }
01196 
01197 #define FORYX for (y=1; y < 3; y++) for (x=col+1; x >= col; x--)
01198 
01199 #define PREDICTOR (c ? (buf[c][y-1][x] + buf[c][y][x+1]) / 2 \
01200 : (buf[c][y-1][x+1] + 2*buf[c][y-1][x] + buf[c][y][x+1]) / 4)
01201 
01202 void CLASS kodak_radc_load_raw()
01203 {
01204   int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
01205   short last[3] = { 16,16,16 }, mul[3], buf[3][3][386];
01206 
01207   init_decoder();
01208   getbits(-1);
01209   for (i=0; i < sizeof(buf)/sizeof(short); i++)
01210     buf[0][0][i] = 2048;
01211   for (row=0; row < height; row+=4) {
01212     for (i=0; i < 3; i++)
01213       mul[i] = getbits(6);
01214     for (c=0; c < 3; c++) {
01215       val = ((0x1000000/last[c] + 0x7ff) >> 12) * mul[c];
01216       s = val > 65564 ? 10:12;
01217       x = ~(-1 << (s-1));
01218       val <<= 12-s;
01219       for (i=0; i < sizeof(buf[0])/sizeof(short); i++)
01220         buf[c][0][i] = (buf[c][0][i] * val + x) >> s;
01221       last[c] = mul[c];
01222       for (r=0; r <= !c; r++) {
01223         buf[c][1][width/2] = buf[c][2][width/2] = mul[c] << 7;
01224         for (tree=1, col=width/2; col > 0; ) {
01225           if ((tree = radc_token(tree))) {
01226             col -= 2;
01227             if (tree == 8)
01228               FORYX buf[c][y][x] = radc_token(tree+10) * mul[c];
01229             else
01230               FORYX buf[c][y][x] = radc_token(tree+10) * 16 + PREDICTOR;
01231           } else
01232             do {
01233               nreps = (col > 2) ? radc_token(9) + 1 : 1;
01234               for (rep=0; rep < 8 && rep < nreps && col > 0; rep++) {
01235                 col -= 2;
01236                 FORYX buf[c][y][x] = PREDICTOR;
01237                 if (rep & 1) {
01238                   step = radc_token(10) << 4;
01239                   FORYX buf[c][y][x] += step;
01240                 }
01241               }
01242             } while (nreps == 9);
01243         }
01244         for (y=0; y < 2; y++)
01245           for (x=0; x < width/2; x++) {
01246             val = (buf[c][y+1][x] << 4) / mul[c];
01247             if (val < 0) val = 0;
01248             if (c)
01249               BAYER(row+y*2+c-1,x*2+2-c) = val;
01250             else
01251               BAYER(row+r*2+y,x*2+y) = val;
01252           }
01253         memcpy (buf[c][0]+!c, buf[c][2], sizeof buf[c][0]-2*!c);
01254       }
01255     }
01256     for (y=row; y < row+4; y++)
01257       for (x=0; x < width; x++)
01258         if ((x+y) & 1) {
01259           val = (BAYER(y,x)-2048)*2 + (BAYER(y,x-1)+BAYER(y,x+1))/2;
01260           if (val < 0) val = 0;
01261           BAYER(y,x) = val;
01262         }
01263   }
01264 }
01265 
01266 #undef FORYX
01267 #undef PREDICTOR
01268 
01269 #ifdef NO_JPEG
01270 void CLASS kodak_jpeg_load_raw() {}
01271 #else
01272 
01273 METHODDEF(boolean)
01274 fill_input_buffer (j_decompress_ptr cinfo)
01275 {
01276   static char jpeg_buffer[4096];
01277   size_t nbytes;
01278 
01279   nbytes = fread (jpeg_buffer, 1, 4096, ifp);
01280   swab (jpeg_buffer, jpeg_buffer, nbytes);
01281   cinfo->src->next_input_byte = jpeg_buffer;
01282   cinfo->src->bytes_in_buffer = nbytes;
01283   return TRUE;
01284 }
01285 
01286 void CLASS kodak_jpeg_load_raw()
01287 {
01288   struct jpeg_decompress_struct cinfo;
01289   struct jpeg_error_mgr jerr;
01290   JSAMPARRAY buf;
01291   JSAMPLE (*pixel)[3];
01292   int row, col;
01293 
01294   cinfo.err = jpeg_std_error (&jerr);
01295   jpeg_create_decompress (&cinfo);
01296   jpeg_stdio_src (&cinfo, ifp);
01297   cinfo.src->fill_input_buffer = fill_input_buffer;
01298   jpeg_read_header (&cinfo, TRUE);
01299   jpeg_start_decompress (&cinfo);
01300   if ((cinfo.output_width      != width  ) ||
01301       (cinfo.output_height*2   != height ) ||
01302       (cinfo.output_components != 3      )) {
01303     fprintf (stderr, "%s: incorrect JPEG dimensions\n", ifname);
01304     jpeg_destroy_decompress (&cinfo);
01305     longjmp (failure, 3);
01306   }
01307   buf = (*cinfo.mem->alloc_sarray)
01308                 ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*3, 1);
01309 
01310   while (cinfo.output_scanline < cinfo.output_height) {
01311     row = cinfo.output_scanline * 2;
01312     jpeg_read_scanlines (&cinfo, buf, 1);
01313     pixel = (void *) buf[0];
01314     for (col=0; col < width; col+=2) {
01315       BAYER(row+0,col+0) = pixel[col+0][1] << 6;
01316       BAYER(row+1,col+1) = pixel[col+1][1] << 6;
01317       BAYER(row+0,col+1) = (pixel[col][0] + pixel[col+1][0]) << 5;
01318       BAYER(ro