libpgf  6.12.24
PGF - Progressive Graphics File
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Encoder.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_ENCODER_H
00030 #define PGF_ENCODER_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 CEncoder {
00051         class CMacroBlock {
00052         public:
00056                 CMacroBlock(CEncoder *encoder)
00057                 : m_header(0)
00058                 , m_encoder(encoder)
00059                 {
00060                         ASSERT(m_encoder);
00061                         Init(-1);
00062                 }
00063 
00067                 void Init(int lastLevelIndex) {                         // initialize for reusage
00068                         m_valuePos = 0;
00069                         m_maxAbsValue = 0;
00070                         m_codePos = 0;
00071                         m_lastLevelIndex = lastLevelIndex;
00072                 }
00073 
00078                 void BitplaneEncode();
00079 
00080                 DataT   m_value[BufferSize];                            
00081                 UINT32  m_codeBuffer[CodeBufferLen];            
00082                 ROIBlockHeader m_header;                                        
00083                 UINT32  m_valuePos;                                                     
00084                 UINT32  m_maxAbsValue;                                          
00085                 UINT32  m_codePos;                                                      
00086                 int             m_lastLevelIndex;                                       
00087 
00088         private:
00089                 UINT32 RLESigns(UINT32 codePos, UINT32* signBits, UINT32 signLen);
00090                 UINT32 DecomposeBitplane(UINT32 bufferSize, UINT32 planeMask, UINT32 codePos, UINT32* sigBits, UINT32* refBits, UINT32* signBits, UINT32& signLen, UINT32& codeLen);
00091                 UINT8  NumberOfBitplanes();
00092                 bool   GetBitAtPos(UINT32 pos, UINT32 planeMask) const  { return (abs(m_value[pos]) & planeMask) > 0; } 
00093 
00094                 CEncoder *m_encoder;                                            // encoder instance
00095                 bool    m_sigFlagVector[BufferSize+1];          // see paper from Malvar, Fast Progressive Wavelet Coder
00096         };
00097 
00098 public:
00108         CEncoder(CPGFStream* stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader& postHeader, 
00109                 UINT64& userDataPos, bool useOMP) THROW_; // throws IOException
00110 
00113         ~CEncoder();
00114 
00117         void FavorSpeedOverSize() { m_favorSpeed = true; }
00118 
00122         void Flush() THROW_;
00123 
00128         void UpdatePostHeaderSize(PGFPreHeader preHeader) THROW_;
00129 
00135         UINT32 WriteLevelLength(UINT32*& levelLength) THROW_;
00136 
00141         UINT32 UpdateLevelLength() THROW_;
00142 
00153         void Partition(CSubband* band, int width, int height, int startPos, int pitch) THROW_;
00154 
00158         void SetEncodedLevel(int currentLevel) { ASSERT(currentLevel >= 0); m_currentBlock->m_lastLevelIndex = m_nLevels - currentLevel - 1; m_forceWriting = true; }
00159 
00165         void WriteValue(CSubband* band, int bandPos) THROW_;
00166 
00170         INT64 ComputeHeaderLength() const { return m_levelLengthPos - m_startPosition; }
00171 
00175         INT64 ComputeBufferLength() const { return m_stream->GetPos() - m_bufferStartPos; }
00176 
00180         INT64 ComputeOffset() const { return m_stream->GetPos() - m_levelLengthPos; }
00181 
00184         void SetBufferStartPos() { m_bufferStartPos = m_stream->GetPos(); }
00185 
00186 #ifdef __PGFROISUPPORT__
00187 
00188 
00189 
00190         void EncodeTileBuffer() THROW_  { ASSERT(m_currentBlock && m_currentBlock->m_valuePos >= 0 && m_currentBlock->m_valuePos <= BufferSize); EncodeBuffer(ROIBlockHeader(m_currentBlock->m_valuePos, true)); }
00191 
00194         void SetROI()                                   { m_roi = true; }
00195 #endif
00196 
00197 #ifdef TRACE
00198         void DumpBuffer() const;
00199 #endif
00200 
00201 private:
00202         void EncodeBuffer(ROIBlockHeader h) THROW_; // throws IOException
00203         void WriteMacroBlock(CMacroBlock* block) THROW_; // throws IOException
00204 
00205         CPGFStream *m_stream;                                           
00206         UINT64  m_startPosition;                                        
00207         UINT64  m_levelLengthPos;                                       
00208         UINT64  m_bufferStartPos;                                       
00209 
00210         CMacroBlock **m_macroBlocks;                            
00211         int             m_macroBlockLen;                                        
00212         int             m_lastMacroBlock;                                       
00213         CMacroBlock *m_currentBlock;                            
00214 
00215         UINT32* m_levelLength;                                          
00216         int     m_currLevelIndex;                                       
00217         UINT8   m_nLevels;                                                      
00218         bool    m_favorSpeed;                                           
00219         bool    m_forceWriting;                                         
00220 #ifdef __PGFROISUPPORT__
00221         bool    m_roi;                                                          
00222 #endif
00223 };
00224 
00225 #endif //PGF_ENCODER