Scribus
Open source desktop publishing at your fingertips
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:
56  CMacroBlock(CEncoder *encoder)
57 #if defined(WIN32) || defined(WINCE) || defined(WIN64)
58 #pragma warning( suppress : 4351 )
59 #endif
60  : m_value()
61  , m_codeBuffer()
62  , m_header(0)
63  , m_encoder(encoder)
64  , m_sigFlagVector()
65  {
66  ASSERT(m_encoder);
67  Init(-1);
68  }
69 
73  void Init(int lastLevelIndex) { // initialize for reusage
74  m_valuePos = 0;
75  m_maxAbsValue = 0;
76  m_codePos = 0;
77  m_lastLevelIndex = lastLevelIndex;
78  }
79 
84  void BitplaneEncode();
85 
86  DataT m_value[BufferSize];
87  UINT32 m_codeBuffer[CodeBufferLen];
88  ROIBlockHeader m_header;
89  UINT32 m_valuePos;
90  UINT32 m_maxAbsValue;
91  UINT32 m_codePos;
92  int m_lastLevelIndex;
93 
94  private:
95  UINT32 RLESigns(UINT32 codePos, UINT32* signBits, UINT32 signLen);
96  UINT32 DecomposeBitplane(UINT32 bufferSize, UINT32 planeMask, UINT32 codePos, UINT32* sigBits, UINT32* refBits, UINT32* signBits, UINT32& signLen, UINT32& codeLen);
97  UINT8 NumberOfBitplanes();
98  bool GetBitAtPos(UINT32 pos, UINT32 planeMask) const { return (abs(m_value[pos]) & planeMask) > 0; }
99 
100  CEncoder *m_encoder; // encoder instance
101  bool m_sigFlagVector[BufferSize+1]; // see paper from Malvar, Fast Progressive Wavelet Coder
102  };
103 
104 public:
114  CEncoder(CPGFStream* stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader& postHeader,
115  UINT64& userDataPos, bool useOMP) THROW_; // throws IOException
116 
119  ~CEncoder();
120 
123  void FavorSpeedOverSize() { m_favorSpeed = true; }
124 
128  void Flush() THROW_;
129 
134  void UpdatePostHeaderSize(PGFPreHeader preHeader) THROW_;
135 
141  UINT32 WriteLevelLength(UINT32*& levelLength) THROW_;
142 
147  UINT32 UpdateLevelLength() THROW_;
148 
159  void Partition(CSubband* band, int width, int height, int startPos, int pitch) THROW_;
160 
164  void SetEncodedLevel(int currentLevel) { ASSERT(currentLevel >= 0); m_currentBlock->m_lastLevelIndex = m_nLevels - currentLevel - 1; m_forceWriting = true; }
165 
171  void WriteValue(CSubband* band, int bandPos) THROW_;
172 
176  INT64 ComputeHeaderLength() const { return m_levelLengthPos - m_startPosition; }
177 
181  INT64 ComputeBufferLength() const { return m_stream->GetPos() - m_bufferStartPos; }
182 
186  INT64 ComputeOffset() const { return m_stream->GetPos() - m_levelLengthPos; }
187 
190  void SetBufferStartPos() { m_bufferStartPos = m_stream->GetPos(); }
191 
192 #ifdef __PGFROISUPPORT__
193  void EncodeTileBuffer() THROW_ { ASSERT(m_currentBlock && m_currentBlock->m_valuePos >= 0 && m_currentBlock->m_valuePos <= BufferSize); EncodeBuffer(ROIBlockHeader(m_currentBlock->m_valuePos, true)); }
197 
200  void SetROI() { m_roi = true; }
201 #endif
202 
203 #ifdef TRACE
204  void DumpBuffer() const;
205 #endif
206 
207 private:
208  void EncodeBuffer(ROIBlockHeader h) THROW_; // throws IOException
209  void WriteMacroBlock(CMacroBlock* block) THROW_; // throws IOException
210 
211  CPGFStream *m_stream;
212  UINT64 m_startPosition;
213  UINT64 m_levelLengthPos;
214  UINT64 m_bufferStartPos;
215 
216  CMacroBlock **m_macroBlocks;
217  int m_macroBlockLen;
218  int m_lastMacroBlock;
219  CMacroBlock *m_currentBlock;
220 
221  UINT32* m_levelLength;
222  int m_currLevelIndex;
223  UINT8 m_nLevels;
224  bool m_favorSpeed;
225  bool m_forceWriting;
226 #ifdef __PGFROISUPPORT__
227  bool m_roi;
228 #endif
229 };
230 
231 #endif //PGF_ENCODER
void FavorSpeedOverSize()
Encoder favors speed over compression size.
Definition: Encoder.h:123
PGF stream class.
void SetROI()
Enables region of interest (ROI) status.
Definition: Encoder.h:200
~CEncoder()
Destructor.
Definition: Encoder.cpp:147
#define BufferSize
must be a multiple of WordWidth
Definition: PGFtypes.h:77
#define CodeBufferLen
number of words in code buffer (CodeBufferLen > BufferLen)
Definition: Encoder.h:40
Abstract stream base class.
Definition: PGFstream.h:39
CEncoder(CPGFStream *stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader &postHeader, UINT64 &userDataPos, bool useOMP) THROW_
Definition: Encoder.cpp:70
PGF wavelet transform class.
PGF encoder.
Definition: Encoder.h:46
INT64 ComputeBufferLength() const
Definition: Encoder.h:181
PGF header.
Definition: PGFtypes.h:123
INT64 ComputeHeaderLength() const
Definition: Encoder.h:176
Wavelet channel class.
Definition: Subband.h:42
void SetEncodedLevel(int currentLevel)
Definition: Encoder.h:164
void WriteValue(CSubband *band, int bandPos) THROW_
Definition: Encoder.cpp:326
void SetBufferStartPos()
Save current stream position as beginning of current level.
Definition: Encoder.h:190
virtual UINT64 GetPos() const =0
Block header used with ROI coding scheme.
Definition: PGFtypes.h:151
PGF wavelet subband class.
UINT32 UpdateLevelLength() THROW_
Definition: Encoder.cpp:202
INT64 ComputeOffset() const
Definition: Encoder.h:186
void Partition(CSubband *band, int width, int height, int startPos, int pitch) THROW_
Definition: Encoder.cpp:246
PGF bit-stream operations.
void Flush() THROW_
Definition: Encoder.cpp:310
PGF pre-header.
Definition: PGFtypes.h:114
Optional PGF post-header.
Definition: PGFtypes.h:141
void UpdatePostHeaderSize(PGFPreHeader preHeader) THROW_
Definition: Encoder.cpp:160
UINT32 WriteLevelLength(UINT32 *&levelLength) THROW_
Definition: Encoder.cpp:177
void EncodeTileBuffer() THROW_
Definition: Encoder.h:196