libpgf  6.12.24
PGF - Progressive Graphics File
Encoder.h
Go to the documentation of this file.
1 /*
2  * The Progressive Graphics File; http://www.libpgf.org
3  *
4  * $Date: 2006-06-04 22:05:59 +0200 (So, 04 Jun 2006) $
5  * $Revision: 229 $
6  *
7  * This file Copyright (C) 2006 xeraina GmbH, Switzerland
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */
23 
28 
29 #ifndef PGF_ENCODER_H
30 #define PGF_ENCODER_H
31 
32 #include "PGFstream.h"
33 #include "BitStream.h"
34 #include "Subband.h"
35 #include "WaveletTransform.h"
36 
38 // Constants
39 #define BufferLen (BufferSize/WordWidth)
40 #define CodeBufferLen BufferSize
41 
42 class CEncoder {
51  class CMacroBlock {
52  public:
57  : m_header(0)
58  , m_encoder(encoder)
59  {
60  ASSERT(m_encoder);
61  Init(-1);
62  }
63 
67  void Init(int lastLevelIndex) { // initialize for reusage
68  m_valuePos = 0;
69  m_maxAbsValue = 0;
70  m_codePos = 0;
71  m_lastLevelIndex = lastLevelIndex;
72  }
73 
78  void BitplaneEncode();
79 
83  UINT32 m_valuePos;
84  UINT32 m_maxAbsValue;
85  UINT32 m_codePos;
87 
88  private:
89  UINT32 RLESigns(UINT32 codePos, UINT32* signBits, UINT32 signLen);
90  UINT32 DecomposeBitplane(UINT32 bufferSize, UINT32 planeMask, UINT32 codePos, UINT32* sigBits, UINT32* refBits, UINT32* signBits, UINT32& signLen, UINT32& codeLen);
91  UINT8 NumberOfBitplanes();
92  bool GetBitAtPos(UINT32 pos, UINT32 planeMask) const { return (abs(m_value[pos]) & planeMask) > 0; }
93 
94  CEncoder *m_encoder; // encoder instance
95  bool m_sigFlagVector[BufferSize+1]; // see paper from Malvar, Fast Progressive Wavelet Coder
96  };
97 
98 public:
108  CEncoder(CPGFStream* stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader& postHeader,
109  UINT64& userDataPos, bool useOMP) THROW_; // throws IOException
110 
113  ~CEncoder();
114 
117  void FavorSpeedOverSize() { m_favorSpeed = true; }
118 
122  void Flush() THROW_;
123 
128  void UpdatePostHeaderSize(PGFPreHeader preHeader) THROW_;
129 
135  UINT32 WriteLevelLength(UINT32*& levelLength) THROW_;
136 
141  UINT32 UpdateLevelLength() THROW_;
142 
153  void Partition(CSubband* band, int width, int height, int startPos, int pitch) THROW_;
154 
158  void SetEncodedLevel(int currentLevel) { ASSERT(currentLevel >= 0); m_currentBlock->m_lastLevelIndex = m_nLevels - currentLevel - 1; m_forceWriting = true; }
159 
165  void WriteValue(CSubband* band, int bandPos) THROW_;
166 
171 
175  INT64 ComputeBufferLength() const { return m_stream->GetPos() - m_bufferStartPos; }
176 
180  INT64 ComputeOffset() const { return m_stream->GetPos() - m_levelLengthPos; }
181 
185 
186 #ifdef __PGFROISUPPORT__
187  void EncodeTileBuffer() THROW_ { ASSERT(m_currentBlock && m_currentBlock->m_valuePos >= 0 && m_currentBlock->m_valuePos <= BufferSize); EncodeBuffer(ROIBlockHeader(m_currentBlock->m_valuePos, true)); }
191 
194  void SetROI() { m_roi = true; }
195 #endif
196 
197 #ifdef TRACE
198  void DumpBuffer() const;
199 #endif
200 
201 private:
202  void EncodeBuffer(ROIBlockHeader h) THROW_; // throws IOException
203  void WriteMacroBlock(CMacroBlock* block) THROW_; // throws IOException
204 
209 
214 
215  UINT32* m_levelLength;
217  UINT8 m_nLevels;
220 #ifdef __PGFROISUPPORT__
221  bool m_roi;
222 #endif
223 };
224 
225 #endif //PGF_ENCODER
UINT8 NumberOfBitplanes()
Definition: Encoder.cpp:736
int m_macroBlockLen
array length
Definition: Encoder.h:211
void SetBufferStartPos()
Save current stream position as beginning of current level.
Definition: Encoder.h:184
void WriteMacroBlock(CMacroBlock *block) THROW_
Definition: Encoder.cpp:395
UINT32 m_valuePos
current buffer position
Definition: Encoder.h:83
Abstract stream base class.
Definition: PGFstream.h:39
int m_lastLevelIndex
index of last encoded level: [0, nLevels); used because a level-end can occur before a buffer is full...
Definition: Encoder.h:86
void Flush() THROW_
Definition: Encoder.cpp:305
INT32 DataT
Definition: PGFtypes.h:219
~CEncoder()
Destructor.
Definition: Encoder.cpp:146
#define BufferSize
must be a multiple of WordWidth
Definition: PGFtypes.h:77
UINT32 m_maxAbsValue
maximum absolute coefficient in each buffer
Definition: Encoder.h:84
UINT32 m_codePos
current position in encoded bitstream
Definition: Encoder.h:85
UINT32 RLESigns(UINT32 codePos, UINT32 *signBits, UINT32 signLen)
Definition: Encoder.cpp:760
#define CodeBufferLen
number of words in code buffer (CodeBufferLen > BufferLen)
Definition: Encoder.h:40
UINT32 WriteLevelLength(UINT32 *&levelLength) THROW_
Definition: Encoder.cpp:172
PGF pre-header.
Definition: PGFtypes.h:114
PGF wavelet subband class.
Wavelet channel class.
Definition: Subband.h:42
bool m_sigFlagVector[BufferSize+1]
Definition: Encoder.h:95
UINT32 * m_levelLength
temporary saves the level index
Definition: Encoder.h:215
void EncodeBuffer(ROIBlockHeader h) THROW_
Definition: Encoder.cpp:336
Block header used with ROI coding scheme.
Definition: PGFtypes.h:151
PGF header.
Definition: PGFtypes.h:123
bool GetBitAtPos(UINT32 pos, UINT32 planeMask) const
Definition: Encoder.h:92
CMacroBlock(CEncoder *encoder)
Definition: Encoder.h:56
UINT32 DecomposeBitplane(UINT32 bufferSize, UINT32 planeMask, UINT32 codePos, UINT32 *sigBits, UINT32 *refBits, UINT32 *signBits, UINT32 &signLen, UINT32 &codeLen)
Definition: Encoder.cpp:620
INT64 ComputeBufferLength() const
Definition: Encoder.h:175
void Partition(CSubband *band, int width, int height, int startPos, int pitch) THROW_
Definition: Encoder.cpp:241
bool m_forceWriting
all macro blocks have to be written into the stream
Definition: Encoder.h:219
CMacroBlock * m_currentBlock
current macro block (used by main thread)
Definition: Encoder.h:213
UINT32 UpdateLevelLength() THROW_
Definition: Encoder.cpp:197
CEncoder * m_encoder
Definition: Encoder.h:94
Optional PGF post-header.
Definition: PGFtypes.h:141
CMacroBlock ** m_macroBlocks
array of macroblocks
Definition: Encoder.h:210
PGF wavelet transform class.
UINT64 m_startPosition
stream position of PGF start (PreHeader)
Definition: Encoder.h:206
UINT32 m_codeBuffer[CodeBufferLen]
output buffer for encoded bitstream
Definition: Encoder.h:81
A macro block is an encoding unit of fixed size (uncoded)
Definition: Encoder.h:51
virtual UINT64 GetPos() const =0
CPGFStream * m_stream
output PMF stream
Definition: Encoder.h:205
UINT64 m_bufferStartPos
stream position of encoded buffer
Definition: Encoder.h:208
UINT64 m_levelLengthPos
stream position of Metadata
Definition: Encoder.h:207
INT64 ComputeHeaderLength() const
Definition: Encoder.h:170
void WriteValue(CSubband *band, int bandPos) THROW_
Definition: Encoder.cpp:321
void FavorSpeedOverSize()
Encoder favors speed over compression size.
Definition: Encoder.h:117
PGF stream class.
bool m_favorSpeed
favor speed over size
Definition: Encoder.h:218
int m_currLevelIndex
counts where (=index) to save next value
Definition: Encoder.h:216
DataT m_value[BufferSize]
input buffer of values with index m_valuePos
Definition: Encoder.h:80
ROIBlockHeader m_header
block header
Definition: Encoder.h:82
int m_lastMacroBlock
array index of the last created macro block
Definition: Encoder.h:212
INT64 ComputeOffset() const
Definition: Encoder.h:180
UINT8 m_nLevels
number of levels
Definition: Encoder.h:217
CEncoder(CPGFStream *stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader &postHeader, UINT64 &userDataPos, bool useOMP) THROW_
Definition: Encoder.cpp:70
PGF encoder.
Definition: Encoder.h:46
void Init(int lastLevelIndex)
Definition: Encoder.h:67
void UpdatePostHeaderSize(PGFPreHeader preHeader) THROW_
Definition: Encoder.cpp:155
void SetEncodedLevel(int currentLevel)
Definition: Encoder.h:158