decompress.c

00001 /*
00002    Simple reference decompresser for Canon digital cameras.
00003    Outputs raw 16-bit CCD data, no header, native byte order.
00004 
00005    $Revision: 1.1 $
00006    $Date: 2005/06/27 14:07:24 $
00007 */
00008 
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 
00013 typedef unsigned char uchar;
00014 
00015 /* Global Variables */
00016 
00017 FILE *ifp;
00018 short order;
00019 int height, width, table, lowbits;
00020 char name[64];
00021 
00022 struct decode {
00023   struct decode *branch[2];
00024   int leaf;
00025 } first_decode[32], second_decode[512];
00026 
00027 /*
00028    Get a 2-byte integer, making no assumptions about CPU byte order.
00029    Nor should we assume that the compiler evaluates left-to-right.
00030  */
00031 short fget2 (FILE *f)
00032 {
00033   register uchar a, b;
00034 
00035   a = fgetc(f);
00036   b = fgetc(f);
00037   if (order == 0x4d4d)      /* "MM" means big-endian */
00038     return (a << 8) + b;
00039   else              /* "II" means little-endian */
00040     return a + (b << 8);
00041 }
00042 
00043 /*
00044    Same for a 4-byte integer.
00045  */
00046 int fget4 (FILE *f)
00047 {
00048   register uchar a, b, c, d;
00049 
00050   a = fgetc(f);
00051   b = fgetc(f);
00052   c = fgetc(f);
00053   d = fgetc(f);
00054   if (order == 0x4d4d)
00055     return (a << 24) + (b << 16) + (c << 8) + d;
00056   else
00057     return a + (b << 8) + (c << 16) + (d << 24);
00058 }
00059 
00060 /*
00061    Parse the CIFF structure
00062  */
00063 void parse (int offset, int length)
00064 {
00065   int tboff, nrecs, i, type, len, roff, aoff, save;
00066 
00067   fseek (ifp, offset+length-4, SEEK_SET);
00068   tboff = fget4(ifp) + offset;
00069   fseek (ifp, tboff, SEEK_SET);
00070   nrecs = fget2(ifp);
00071   for (i = 0; i < nrecs; i++) {
00072     type = fget2(ifp);
00073     len  = fget4(ifp);
00074     roff = fget4(ifp);
00075     aoff = offset + roff;
00076     save = ftell(ifp);
00077     if (type == 0x080a) {       /* Get the camera name */
00078       fseek (ifp, aoff, SEEK_SET);
00079       while (fgetc(ifp));
00080       fread (name, 64, 1, ifp);
00081     }
00082     if (type == 0x1031) {       /* Get the width and height */
00083       fseek (ifp, aoff+2, SEEK_SET);
00084       width  = fget2(ifp);
00085       height = fget2(ifp);
00086     }
00087     if (type == 0x1835) {       /* Get the decoder table */
00088       fseek (ifp, aoff, SEEK_SET);
00089       table = fget4(ifp);
00090     }
00091     if (type >> 8 == 0x28 || type >> 8 == 0x30) /* Get sub-tables */
00092       parse (aoff, len);
00093     fseek (ifp, save, SEEK_SET);
00094   }
00095 }
00096 
00097 /*
00098    Return 0 if the image starts with compressed data,
00099    1 if it starts with uncompressed low-order bits.
00100 
00101    In Canon compressed data, 0xff is always followed by 0x00.
00102  */
00103 int canon_has_lowbits()
00104 {
00105   uchar test[0x4000];
00106   int ret=1, i;
00107 
00108   fseek (ifp, 0, SEEK_SET);
00109   fread (test, 1, sizeof test, ifp);
00110   for (i=540; i < sizeof test - 1; i++)
00111     if (test[i] == 0xff) {
00112       if (test[i+1]) return 1;
00113       ret=0;
00114     }
00115   return ret;
00116 }
00117 
00118 /*
00119    Open a CRW file, identify which camera created it, and set
00120    global variables accordingly.  Returns nonzero if an error occurs.
00121  */
00122 int open_and_id(char *fname)
00123 {
00124   char head[8];
00125   int hlen;
00126 
00127   ifp = fopen(fname,"rb");
00128   if (!ifp) {
00129     perror(fname);
00130     return 1;
00131   }
00132   order = fget2(ifp);
00133   hlen  = fget4(ifp);
00134 
00135   fread (head, 1, 8, ifp);
00136   if (memcmp(head,"HEAPCCDR",8) || (order != 0x4949 && order != 0x4d4d)) {
00137     fprintf(stderr,"%s is not a Canon CRW file.\n",fname);
00138     return 1;
00139   }
00140 
00141   name[0] = 0;
00142   table = -1;
00143   fseek (ifp, 0, SEEK_END);
00144   parse (hlen, ftell(ifp) - hlen);
00145   lowbits = canon_has_lowbits();
00146 
00147   fprintf(stderr,"name = %s, width = %d, height = %d, table = %d, bpp = %d\n",
00148     name, width, height, table, 10+lowbits*2);
00149   if (table < 0) {
00150     fprintf(stderr,"Cannot decompress %s!!\n",fname);
00151     return 1;
00152   }
00153   return 0;
00154 }
00155 
00156 /*
00157    A rough description of Canon's compression algorithm:
00158 
00159 +  Each pixel outputs a 10-bit sample, from 0 to 1023.
00160 +  Split the data into blocks of 64 samples each.
00161 +  Subtract from each sample the value of the sample two positions
00162    to the left, which has the same color filter.  From the two
00163    leftmost samples in each row, subtract 512.
00164 +  For each nonzero sample, make a token consisting of two four-bit
00165    numbers.  The low nibble is the number of bits required to
00166    represent the sample, and the high nibble is the number of
00167    zero samples preceding this sample.
00168 +  Output this token as a variable-length bitstring using
00169    one of three tablesets.  Follow it with a fixed-length
00170    bitstring containing the sample.
00171 
00172    The "first_decode" table is used for the first sample in each
00173    block, and the "second_decode" table is used for the others.
00174  */
00175 
00176 /*
00177    Construct a decode tree according the specification in *source.
00178    The first 16 bytes specify how many codes should be 1-bit, 2-bit
00179    3-bit, etc.  Bytes after that are the leaf values.
00180 
00181    For example, if the source is
00182 
00183     { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
00184       0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff  },
00185 
00186    then the code is
00187 
00188     00      0x04
00189     010     0x03
00190     011     0x05
00191     100     0x06
00192     101     0x02
00193     1100        0x07
00194     1101        0x01
00195     11100       0x08
00196     11101       0x09
00197     11110       0x00
00198     111110      0x0a
00199     1111110     0x0b
00200     1111111     0xff
00201  */
00202 void make_decoder(struct decode *dest, const uchar *source, int level)
00203 {
00204   static struct decode *free;   /* Next unused node */
00205   static int leaf;      /* no. of leaves already added */
00206   int i, next;
00207 
00208   if (level==0) {
00209     free = dest;
00210     leaf = 0;
00211   }
00212   free++;
00213 /*
00214    At what level should the next leaf appear?
00215  */
00216   for (i=next=0; i <= leaf && next < 16; )
00217     i += source[next++];
00218 
00219   if (i > leaf)
00220     if (level < next) {     /* Are we there yet? */
00221       dest->branch[0] = free;
00222       make_decoder(free,source,level+1);
00223       dest->branch[1] = free;
00224       make_decoder(free,source,level+1);
00225     } else
00226       dest->leaf = source[16 + leaf++];
00227 }
00228 
00229 void init_tables(unsigned table)
00230 {
00231   static const uchar first_tree[3][29] = {
00232     { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
00233       0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff  },
00234 
00235     { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
00236       0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff  },
00237 
00238     { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
00239       0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff  },
00240   };
00241 
00242   static const uchar second_tree[3][180] = {
00243     { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
00244       0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
00245       0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
00246       0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
00247       0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
00248       0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
00249       0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
00250       0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
00251       0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
00252       0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
00253       0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
00254       0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
00255       0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
00256       0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
00257       0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff  },
00258 
00259     { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
00260       0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
00261       0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
00262       0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
00263       0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
00264       0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
00265       0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
00266       0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
00267       0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
00268       0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
00269       0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
00270       0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
00271       0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
00272       0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
00273       0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff  },
00274 
00275     { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
00276       0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
00277       0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
00278       0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
00279       0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
00280       0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
00281       0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
00282       0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
00283       0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
00284       0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
00285       0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
00286       0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
00287       0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
00288       0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
00289       0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff  }
00290   };
00291 
00292   if (table > 2) table = 2;
00293   memset( first_decode, 0, sizeof first_decode);
00294   memset(second_decode, 0, sizeof second_decode);
00295   make_decoder( first_decode,  first_tree[table], 0);
00296   make_decoder(second_decode, second_tree[table], 0);
00297 }
00298 
00299 #if 0
00300 writebits (int val, int nbits)
00301 {
00302   val <<= 32 - nbits;
00303   while (nbits--) {
00304     putchar(val & 0x80000000 ? '1':'0');
00305     val <<= 1;
00306   }
00307 }
00308 #endif
00309 
00310 /*
00311    getbits(-1) initializes the buffer
00312    getbits(n) where 0 <= n <= 25 returns an n-bit integer
00313 */
00314 unsigned long getbits(int nbits)
00315 {
00316   static unsigned long bitbuf=0, ret=0;
00317   static int vbits=0;
00318   unsigned char c;
00319 
00320   if (nbits == 0) return 0;
00321   if (nbits == -1)
00322     ret = bitbuf = vbits = 0;
00323   else {
00324     ret = bitbuf << (32 - vbits) >> (32 - nbits);
00325     vbits -= nbits;
00326   }
00327   while (vbits < 25) {
00328     c=fgetc(ifp);
00329     bitbuf = (bitbuf << 8) + c;
00330     if (c == 0xff) fgetc(ifp);  /* always extra 00 after ff */
00331     vbits += 8;
00332   }
00333   return ret;
00334 }
00335 
00336 int main(int argc, char **argv)
00337 {
00338   struct decode *decode, *dindex;
00339   int i, j, leaf, len, diff, diffbuf[64], r, save;
00340   int carry=0, column=0, base[2];
00341   unsigned short outbuf[64];
00342   uchar c;
00343 
00344   if (argc < 2) {
00345     fprintf(stderr,"Usage:  %s file.crw\n",argv[0]);
00346     exit(1);
00347   }
00348   if (open_and_id(argv[1]))
00349     exit(1);
00350 
00351   init_tables(table);
00352 
00353   fseek (ifp, 540 + lowbits*height*width/4, SEEK_SET);
00354   getbits(-1);          /* Prime the bit buffer */
00355 
00356   while (column < width * height) {
00357     memset(diffbuf,0,sizeof diffbuf);
00358     decode = first_decode;
00359     for (i=0; i < 64; i++ ) {
00360 
00361       for (dindex=decode; dindex->branch[0]; )
00362     dindex = dindex->branch[getbits(1)];
00363       leaf = dindex->leaf;
00364       decode = second_decode;
00365 
00366       if (leaf == 0 && i) break;
00367       if (leaf == 0xff) continue;
00368       i  += leaf >> 4;
00369       len = leaf & 15;
00370       if (len == 0) continue;
00371       diff = getbits(len);
00372       if ((diff & (1 << (len-1))) == 0)
00373     diff -= (1 << len) - 1;
00374       if (i < 64) diffbuf[i] = diff;
00375     }
00376     diffbuf[0] += carry;
00377     carry = diffbuf[0];
00378     for (i=0; i < 64; i++ ) {
00379       if (column++ % width == 0)
00380     base[0] = base[1] = 512;
00381       outbuf[i] = ( base[i & 1] += diffbuf[i] );
00382     }
00383     if (lowbits) {
00384       save = ftell(ifp);
00385       fseek (ifp, (column-64)/4 + 26, SEEK_SET);
00386       for (i=j=0; j < 64/4; j++ ) {
00387     c = fgetc(ifp);
00388     for (r = 0; r < 8; r += 2)
00389       outbuf[i++] = (outbuf[i] << 2) + ((c >> r) & 3);
00390       }
00391       fseek (ifp, save, SEEK_SET);
00392     }
00393     fwrite(outbuf,2,64,stdout);
00394   }
00395   return 0;
00396 }

Generated on Sat Jan 27 11:36:12 2007 for libopenraw by  doxygen 1.4.7