00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00039 #define BufferLen (BufferSize/WordWidth)
00040 #define CodeBufferLen BufferSize
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) {
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;
00099 bool m_sigFlagVector[BufferSize+1];
00100 };
00101
00102 public:
00112 CEncoder(CPGFStream* stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader& postHeader,
00113 UINT64& userDataPos, bool useOMP) THROW_;
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__
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_;
00207 void WriteMacroBlock(CMacroBlock* block) THROW_;
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