Author: post
Date: 2010-11-13 23:32:53 +0100 (Sat, 13 Nov 2010)
New Revision: 290

Modified:
   RawSpeed/PefDecoder.cpp
   RawSpeed/PentaxDecompressor.cpp
   RawSpeed/PentaxDecompressor.h
Log:
Read Huffman table from Pentax makernotes, instead of using built-in table.

Modified: RawSpeed/PefDecoder.cpp
===================================================================
--- RawSpeed/PefDecoder.cpp     2010-11-13 22:25:07 UTC (rev 289)
+++ RawSpeed/PefDecoder.cpp     2010-11-13 22:32:53 UTC (rev 290)
@@ -26,6 +26,7 @@
 
 PefDecoder::PefDecoder(TiffIFD *rootIFD, FileMap* file) :
     RawDecoder(file), mRootIFD(rootIFD) {
+      decoderVersion = 0;
 }
 
 PefDecoder::~PefDecoder(void) {
@@ -63,7 +64,7 @@
   mRaw->createData();
   try {
     PentaxDecompressor l(mFile, mRaw);
-    l.decodePentax(offsets->getInt(), counts->getInt());
+    l.decodePentax(mRootIFD, offsets->getInt(), counts->getInt());
   } catch (IOException e) {
     errors.push_back(_strdup(e.what()));
     // Let's ignore it, it may have delivered somewhat useful data.

Modified: RawSpeed/PentaxDecompressor.cpp
===================================================================
--- RawSpeed/PentaxDecompressor.cpp     2010-11-13 22:25:07 UTC (rev 289)
+++ RawSpeed/PentaxDecompressor.cpp     2010-11-13 22:32:53 UTC (rev 290)
@@ -36,22 +36,69 @@
 }
 
 
-void PentaxDecompressor::decodePentax(uint32 offset, uint32 size) {
+void PentaxDecompressor::decodePentax(TiffIFD *root, uint32 offset, uint32 
size) {
   // Prepare huffmann table              0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 = 16 
entries
   static const uchar8 pentax_tree[] =  { 0, 2, 3, 1, 1, 1, 1, 1, 1, 2, 0, 0, 
0, 0, 0, 0,
                                          3, 4, 2, 5, 1, 6, 0, 7, 8, 9, 10, 11, 
12
                                        };
   //                                     0 1 2 3 4 5 6 7 8 9  0  1  2 = 13 
entries
   HuffmanTable *dctbl1 = &huff[0];
-  uint32 acc = 0;
-  for (uint32 i = 0; i < 16 ;i++) {
-    dctbl1->bits[i+1] = pentax_tree[i];
-    acc += dctbl1->bits[i+1];
-  }
-  dctbl1->bits[0] = 0;
 
-  for (uint32 i = 0 ; i < acc; i++) {
-    dctbl1->huffval[i] = pentax_tree[i+16];
+  /* Attempt to read huffman table, if found in makernote */
+  if (root->hasEntryRecursive((TiffTag)0x220)) {
+    TiffEntry *t = root->getEntryRecursive((TiffTag)0x220);
+    if (t->type == TIFF_UNDEFINED) {
+      const uchar8* data = t->getData();
+      uint32 depth = (data[1]+12)&0xf;
+      data +=14;
+      uint32 v0[16];
+      uint32 v1[16];
+      uint32 v2[16];
+      for (uint32 i = 0; i < depth; i++)
+         v0[i] = (uint32)(data[i*2])<<8 | (uint32)(data[i*2+1]);
+      data+=depth*2;
+
+      for (uint32 i = 0; i < depth; i++)
+        v1[i] = data[i];
+
+      /* Reset bits */
+      for (uint32 i = 0; i < 17; i++)
+        dctbl1->bits[i] = 0;
+
+      /* Calculate codes and store bitcounts */
+      for (uint32 c = 0; c < depth; c++)
+      {
+        v2[c] = v0[c]>>(12-v1[c]);
+        dctbl1->bits[v1[c]]++;
+      }
+      /* Find smallest */
+      for (uint32 i = 0; i < depth; i++)
+      {
+        uint32 sm_val = 0xfffffff;
+        uint32 sm_num = 0xff;
+        for (uint32 j = 0; j < depth; j++)
+        {
+          if(v2[j]<=sm_val)
+          {
+            sm_num = j;
+            sm_val = v2[j];
+          }
+        }
+        dctbl1->huffval[i] = sm_num;
+        v2[sm_num]=0xffffffff;
+      }
+    }
+  } else {
+    /* Initialize with legacy data */
+    uint32 acc = 0;
+    for (uint32 i = 0; i < 16 ;i++) {
+      dctbl1->bits[i+1] = pentax_tree[i];
+      acc += dctbl1->bits[i+1];
+    }
+    dctbl1->bits[0] = 0;
+    for (uint32 i = 0 ; i < acc; i++) {
+      dctbl1->huffval[i] = pentax_tree[i+16];
+    }
   }
   mUseBigtable = true;
   createHuffmanTable(dctbl1);
@@ -78,8 +125,8 @@
       pLeft2 += HuffDecodePentax();
       dest[x] =  pLeft1;
       dest[x+1] =  pLeft2;
-      _ASSERTE(pLeft1 >= 0 && pLeft1 <= (65536 >> 3));
-      _ASSERTE(pLeft2 >= 0 && pLeft2 <= (65536 >> 3));
+      _ASSERTE(pLeft1 >= 0 && pLeft1 <= (65536));
+      _ASSERTE(pLeft2 >= 0 && pLeft2 <= (65536));
     }
   }
 }

Modified: RawSpeed/PentaxDecompressor.h
===================================================================
--- RawSpeed/PentaxDecompressor.h       2010-11-13 22:25:07 UTC (rev 289)
+++ RawSpeed/PentaxDecompressor.h       2010-11-13 22:32:53 UTC (rev 290)
@@ -1,6 +1,8 @@
 #pragma once
 #include "LJpegDecompressor.h"
 #include "BitPumpMSB.h"
+#include "TiffIFD.h"
+
 /* 
     RawSpeed - RAW file decoder.
 
@@ -32,7 +34,7 @@
   PentaxDecompressor(FileMap* file, RawImage img);
   virtual ~PentaxDecompressor(void);
   int HuffDecodePentax();
-  void decodePentax( uint32 offset, uint32 size );
+  void decodePentax(TiffIFD *root, uint32 offset, uint32 size);
   BitPumpMSB *pentaxBits;
 };
 


_______________________________________________
Rawstudio-commit mailing list
[email protected]
http://rawstudio.org/cgi-bin/mailman/listinfo/rawstudio-commit

Reply via email to