libpgf  6.11.42
PGF - Progressive Graphics File
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
41 
42 
43 
44 
45 class CEncoder {
50  class CMacroBlock {
51  public:
56  : m_header(0)
57  , m_encoder(encoder)
58  {
59  ASSERT(m_encoder);
60  Init(-1);
61  }
62 
66  void Init(int lastLevelIndex) { // initialize for reusage
67  m_valuePos = 0;
68  m_maxAbsValue = 0;
69  m_codePos = 0;
70  m_lastLevelIndex = lastLevelIndex;
71  }
72 
77  void BitplaneEncode();
78 
81 
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, UINT32*& levelLength, bool useOMP = true) THROW_; // throws IOException
109 
112  ~CEncoder();
113 
116  void FavorSpeedOverSize() { m_favorSpeed = true; }
117 
121  void Flush() THROW_;
122 
127  UINT32 WriteLevelLength() THROW_;
128 
139  void Partition(CSubband* band, int width, int height, int startPos, int pitch) THROW_;
140 
144  void SetEncodedLevel(int currentLevel) { ASSERT(currentLevel >= 0); m_currentBlock->m_lastLevelIndex = m_nLevels - currentLevel - 1; m_forceWriting = true; }
145 
151  void WriteValue(CSubband* band, int bandPos) THROW_;
152 
156  UINT32 ComputeHeaderLength() const { return UINT32(m_bufferStartPos - m_startPosition); }
157 
161  UINT32 ComputeBufferLength() const { return UINT32(m_stream->GetPos() - m_bufferStartPos); }
162 
166 
167 #ifdef __PGFROISUPPORT__
168 
169 
170 
171  void EncodeTileBuffer() THROW_ { ASSERT(m_currentBlock && m_currentBlock->m_valuePos >= 0 && m_currentBlock->m_valuePos <= BufferSize); EncodeBuffer(ROIBlockHeader(m_currentBlock->m_valuePos, true)); }
172 
175  void SetROI() { m_roi = true; }
176 #endif
177 
178 #ifdef TRACE
179  void DumpBuffer() const;
180 #endif
181 
182 private:
183  void EncodeBuffer(ROIBlockHeader h) THROW_; // throws IOException
184  void WriteMacroBlock(CMacroBlock* block) THROW_; // throws IOException
185 
190 
195 
196  UINT32* m_levelLength;
198  UINT8 m_nLevels;
201 #ifdef __PGFROISUPPORT__
202  bool m_roi;
203 #endif
204 };
205 
206 #endif //PGF_ENCODER