libpgf  6.12.24
PGF - Progressive Graphics File
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Decoder.h
Go to the documentation of this file.
00001 /*
00002  * The Progressive Graphics File; http://www.libpgf.org
00003  * 
00004  * $Date: 2006-06-04 22:05:59 +0200 (So, 04 Jun 2006) $
00005  * $Revision: 229 $
00006  * 
00007  * This file Copyright (C) 2006 xeraina GmbH, Switzerland
00008  * 
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
00011  * as published by the Free Software Foundation; either version 2.1
00012  * of the License, or (at your option) any later version.
00013  * 
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022  */
00023 
00028 
00029 #ifndef PGF_DECODER_H
00030 #define PGF_DECODER_H
00031 
00032 #include "PGFstream.h"
00033 #include "BitStream.h"
00034 #include "Subband.h"
00035 #include "WaveletTransform.h"
00036 
00038 // Constants
00039 #define BufferLen                       (BufferSize/WordWidth)  ///< number of words per buffer
00040 #define CodeBufferLen           BufferSize                              ///< number of words in code buffer (CodeBufferLen > BufferLen)
00041 
00046 class CDecoder {
00051         class CMacroBlock {
00052         public:
00056                 CMacroBlock(CDecoder *decoder)
00057                 : m_header(0)                                                           // makes sure that IsCompletelyRead() returns true for an empty macro block
00058                 , m_valuePos(0)
00059                 , m_decoder(decoder)
00060                 {
00061                         ASSERT(m_decoder);
00062                 }
00063 
00067                 bool IsCompletelyRead() const   { return m_valuePos >= m_header.rbh.bufferSize; }
00068 
00073                 void BitplaneDecode();
00074 
00075                 ROIBlockHeader m_header;                                        
00076                 DataT  m_value[BufferSize];                                     
00077                 UINT32 m_codeBuffer[CodeBufferLen];                     
00078                 UINT32 m_valuePos;                                                      
00079 
00080         private:
00081                 UINT32 ComposeBitplane(UINT32 bufferSize, DataT planeMask, UINT32* sigBits, UINT32* refBits, UINT32* signBits);
00082                 UINT32 ComposeBitplaneRLD(UINT32 bufferSize, DataT planeMask, UINT32 sigPos, UINT32* refBits);
00083                 UINT32 ComposeBitplaneRLD(UINT32 bufferSize, DataT planeMask, UINT32* sigBits, UINT32* refBits, UINT32 signPos);
00084                 void  SetBitAtPos(UINT32 pos, DataT planeMask)                  { (m_value[pos] >= 0) ? m_value[pos] |= planeMask : m_value[pos] -= planeMask; }
00085                 void  SetSign(UINT32 pos, bool sign)                                    { m_value[pos] = -m_value[pos]*sign + m_value[pos]*(!sign); }
00086 
00087                 CDecoder *m_decoder;                                            // outer class
00088                 bool m_sigFlagVector[BufferSize+1];                     // see paper from Malvar, Fast Progressive Wavelet Coder
00089         };
00090 
00091 public:
00103         CDecoder(CPGFStream* stream, PGFPreHeader& preHeader, PGFHeader& header, 
00104                      PGFPostHeader& postHeader, UINT32*& levelLength, UINT64& userDataPos, 
00105                          bool useOMP, bool skipUserData) THROW_; // throws IOException
00106 
00109         ~CDecoder();
00110 
00122         void Partition(CSubband* band, int quantParam, int width, int height, int startPos, int pitch) THROW_;
00123 
00131         void DecodeInterleaved(CWaveletTransform* wtChannel, int level, int quantParam) THROW_;
00132 
00136         UINT32 GetEncodedHeaderLength() const                   { return m_encodedHeaderLength; }
00137 
00140         void SetStreamPosToStart() THROW_                               { ASSERT(m_stream); m_stream->SetPos(FSFromStart, m_startPos); }
00141 
00144         void SetStreamPosToData() THROW_                                { ASSERT(m_stream); m_stream->SetPos(FSFromStart, m_startPos + m_encodedHeaderLength); }
00145 
00149         void Skip(UINT64 offset) THROW_;
00150 
00157         void DequantizeValue(CSubband* band, UINT32 bandPos, int quantParam) THROW_;
00158 
00165         UINT32 ReadEncodedData(UINT8* target, UINT32 len) const THROW_;
00166 
00170         void DecodeBuffer() THROW_;
00171 
00174         CPGFStream* GetStream()                                                 { return m_stream; }
00175 
00178         bool MacroBlocksAvailable() const                               { return m_macroBlocksAvailable > 1; }
00179 
00180 #ifdef __PGFROISUPPORT__
00181 
00182 
00183 
00184         void DecodeTileBuffer() THROW_;
00185 
00189         void SkipTileBuffer() THROW_;
00190 
00193         void SetROI()                                   { m_roi = true; }
00194 #endif
00195 
00196 #ifdef TRACE
00197         void DumpBuffer();
00198 #endif
00199 
00200 private:
00201         void ReadMacroBlock(CMacroBlock* block) THROW_; 
00202 
00203         CPGFStream *m_stream;                                           
00204         UINT64 m_startPos;                                                      
00205         UINT64 m_streamSizeEstimation;                          
00206         UINT32 m_encodedHeaderLength;                           
00207 
00208         CMacroBlock **m_macroBlocks;                            
00209         int m_currentBlockIndex;                                        
00210         int     m_macroBlockLen;                                                
00211         int     m_macroBlocksAvailable;                                 
00212         CMacroBlock *m_currentBlock;                            
00213 
00214 #ifdef __PGFROISUPPORT__
00215         bool   m_roi;                                                           
00216 #endif
00217 };
00218 
00219 #endif //PGF_DECODER_H