libpgf  6.14.12
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 #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 #pragma warning( suppress : 4351 )
00058                 : m_value()
00059                 , m_codeBuffer()
00060                 , m_header(0)
00061                 , m_encoder(encoder)
00062                 , m_sigFlagVector()
00063                 {
00064                         ASSERT(m_encoder);
00065                         Init(-1);
00066                 }
00067 
00071                 void Init(int lastLevelIndex) {                         // initialize for reusage
00072                         m_valuePos = 0;
00073                         m_maxAbsValue = 0;
00074                         m_codePos = 0;
00075                         m_lastLevelIndex = lastLevelIndex;
00076                 }
00077 
00082                 void BitplaneEncode();
00083 
00084                 DataT   m_value[BufferSize];                            
00085                 UINT32  m_codeBuffer[CodeBufferLen];            
00086                 ROIBlockHeader m_header;                                        
00087                 UINT32  m_valuePos;                                                     
00088                 UINT32  m_maxAbsValue;                                          
00089                 UINT32  m_codePos;                                                      
00090                 int             m_lastLevelIndex;                                       
00091 
00092         private:
00093                 UINT32 RLESigns(UINT32 codePos, UINT32* signBits, UINT32 signLen);
00094                 UINT32 DecomposeBitplane(UINT32 bufferSize, UINT32 planeMask, UINT32 codePos, UINT32* sigBits, UINT32* refBits, UINT32* signBits, UINT32& signLen, UINT32& codeLen);
00095                 UINT8  NumberOfBitplanes();
00096                 bool   GetBitAtPos(UINT32 pos, UINT32 planeMask) const  { return (abs(m_value[pos]) & planeMask) > 0; } 
00097 
00098                 CEncoder *m_encoder;                                            // encoder instance
00099                 bool    m_sigFlagVector[BufferSize+1];          // see paper from Malvar, Fast Progressive Wavelet Coder
00100         };
00101 
00102 public:
00112         CEncoder(CPGFStream* stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader& postHeader, 
00113                 UINT64& userDataPos, bool useOMP) THROW_; // throws IOException
00114 
00117         ~CEncoder();
00118 
00121         void FavorSpeedOverSize() { m_favorSpeed = true; }
00122 
00126         void Flush() THROW_;
00127 
00132         void UpdatePostHeaderSize(PGFPreHeader preHeader) THROW_;
00133 
00139         UINT32 WriteLevelLength(UINT32*& levelLength) THROW_;
00140 
00145         UINT32 UpdateLevelLength() THROW_;
00146 
00157         void Partition(CSubband* band, int width, int height, int startPos, int pitch) THROW_;
00158 
00162         void SetEncodedLevel(int currentLevel) { ASSERT(currentLevel >= 0); m_currentBlock->m_lastLevelIndex = m_nLevels - currentLevel - 1; m_forceWriting = true; }
00163 
00169         void WriteValue(CSubband* band, int bandPos) THROW_;
00170 
00174         INT64 ComputeHeaderLength() const { return m_levelLengthPos - m_startPosition; }
00175 
00179         INT64 ComputeBufferLength() const { return m_stream->GetPos() - m_bufferStartPos; }
00180 
00184         INT64 ComputeOffset() const { return m_stream->GetPos() - m_levelLengthPos; }
00185 
00188         void SetBufferStartPos() { m_bufferStartPos = m_stream->GetPos(); }
00189 
00190 #ifdef __PGFROISUPPORT__
00191 
00192 
00193 
00194         void EncodeTileBuffer() THROW_  { ASSERT(m_currentBlock && m_currentBlock->m_valuePos >= 0 && m_currentBlock->m_valuePos <= BufferSize); EncodeBuffer(ROIBlockHeader(m_currentBlock->m_valuePos, true)); }
00195 
00198         void SetROI()                                   { m_roi = true; }
00199 #endif
00200 
00201 #ifdef TRACE
00202         void DumpBuffer() const;
00203 #endif
00204 
00205 private:
00206         void EncodeBuffer(ROIBlockHeader h) THROW_; // throws IOException
00207         void WriteMacroBlock(CMacroBlock* block) THROW_; // throws IOException
00208 
00209         CPGFStream *m_stream;                                           
00210         UINT64  m_startPosition;                                        
00211         UINT64  m_levelLengthPos;                                       
00212         UINT64  m_bufferStartPos;                                       
00213 
00214         CMacroBlock **m_macroBlocks;                            
00215         int             m_macroBlockLen;                                        
00216         int             m_lastMacroBlock;                                       
00217         CMacroBlock *m_currentBlock;                            
00218 
00219         UINT32* m_levelLength;                                          
00220         int     m_currLevelIndex;                                       
00221         UINT8   m_nLevels;                                                      
00222         bool    m_favorSpeed;                                           
00223         bool    m_forceWriting;                                         
00224 #ifdef __PGFROISUPPORT__
00225         bool    m_roi;                                                          
00226 #endif
00227 };
00228 
00229 #endif //PGF_ENCODER
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines