libpgf  6.11.42
PGF - Progressive Graphics File
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 
00045 class CEncoder {
00050         class CMacroBlock {
00051         public:
00055                 CMacroBlock(CEncoder *encoder)
00056                 : m_header(0)
00057                 , m_encoder(encoder)
00058                 {
00059                         ASSERT(m_encoder);
00060                         Init(-1);
00061                 }
00062 
00066                 void Init(int lastLevelIndex) {                         // initialize for reusage
00067                         m_valuePos = 0;
00068                         m_maxAbsValue = 0;
00069                         m_codePos = 0;
00070                         m_lastLevelIndex = lastLevelIndex;
00071                 }
00072 
00077                 void BitplaneEncode();
00078 
00079                 DataT   m_value[BufferSize];                            
00080                 UINT32  m_codeBuffer[BufferSize];                       
00081 
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, UINT32*& levelLength, bool useOMP = true) THROW_; // throws IOException
00109 
00112         ~CEncoder();
00113 
00116         void FavorSpeedOverSize() { m_favorSpeed = true; }
00117 
00121         void Flush() THROW_;
00122 
00127         UINT32 WriteLevelLength() THROW_;
00128 
00139         void Partition(CSubband* band, int width, int height, int startPos, int pitch) THROW_;
00140 
00144         void SetEncodedLevel(int currentLevel) { ASSERT(currentLevel >= 0); m_currentBlock->m_lastLevelIndex = m_nLevels - currentLevel - 1; m_forceWriting = true; }
00145 
00151         void WriteValue(CSubband* band, int bandPos) THROW_;
00152 
00156         UINT32 ComputeHeaderLength() const { return UINT32(m_bufferStartPos - m_startPosition); }
00157 
00161         UINT32 ComputeBufferLength() const { return UINT32(m_stream->GetPos() - m_bufferStartPos); }
00162 
00165         void SetBufferStartPos() { m_bufferStartPos = m_stream->GetPos(); }
00166 
00167 #ifdef __PGFROISUPPORT__
00168 
00169 
00170 
00171         void EncodeTileBuffer() THROW_  { ASSERT(m_currentBlock && m_currentBlock->m_valuePos >= 0 && m_currentBlock->m_valuePos <= BufferSize); EncodeBuffer(ROIBlockHeader(m_currentBlock->m_valuePos, true)); }
00172 
00175         void SetROI()                                   { m_roi = true; }
00176 #endif
00177 
00178 #ifdef TRACE
00179         void DumpBuffer() const;
00180 #endif
00181 
00182 private:
00183         void EncodeBuffer(ROIBlockHeader h) THROW_; // throws IOException
00184         void WriteMacroBlock(CMacroBlock* block) THROW_; // throws IOException
00185 
00186         CPGFStream *m_stream;
00187         UINT64  m_startPosition;                                        
00188         UINT64  m_levelLengthPos;                                       
00189         UINT64  m_bufferStartPos;                                       
00190 
00191         CMacroBlock **m_macroBlocks;                            
00192         int             m_macroBlockLen;                                        
00193         int             m_lastMacroBlock;                                       
00194         CMacroBlock *m_currentBlock;                            
00195 
00196         UINT32* m_levelLength;                                          
00197         int     m_currLevelIndex;                                       
00198         UINT8   m_nLevels;                                                      
00199         bool    m_favorSpeed;                                           
00200         bool    m_forceWriting;                                         
00201 #ifdef __PGFROISUPPORT__
00202         bool    m_roi;                                                          
00203 #endif
00204 };
00205 
00206 #endif //PGF_ENCODER
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines