libpgf 6.14.12
PGF - Progressive Graphics File
Subband.cpp
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#include "Subband.h"
30#include "Encoder.h"
31#include "Decoder.h"
32
34// Default constructor
36: m_width(0)
37, m_height(0)
38, m_size(0)
39, m_level(0)
40, m_orientation(LL)
41, m_data(0)
42, m_dataPos(0)
44, m_nTiles(0)
45#endif
46{
47}
48
50// Destructor
52 FreeMemory();
53}
54
56// Initialize subband parameters
57void CSubband::Initialize(UINT32 width, UINT32 height, int level, Orientation orient) {
58 m_width = width;
59 m_height = height;
61 m_level = level;
62 m_orientation = orient;
63 m_data = 0;
64 m_dataPos = 0;
65#ifdef __PGFROISUPPORT__
66 m_ROI.left = 0;
67 m_ROI.top = 0;
68 m_ROI.right = m_width;
69 m_ROI.bottom = m_height;
70 m_nTiles = 0;
71#endif
72}
73
75// Allocate a memory buffer to store all wavelet coefficients of this subband.
76// @return True if the allocation works without any problems
78 UINT32 oldSize = m_size;
79
80#ifdef __PGFROISUPPORT__
81 m_size = BufferWidth()*m_ROI.Height();
82#endif
83 ASSERT(m_size > 0);
84
85 if (m_data) {
86 if (oldSize >= m_size) {
87 return true;
88 } else {
89 delete[] m_data;
90 m_data = new(std::nothrow) DataT[m_size];
91 return (m_data != 0);
92 }
93 } else {
94 m_data = new(std::nothrow) DataT[m_size];
95 return (m_data != 0);
96 }
97}
98
100// Delete the memory buffer of this subband.
102 if (m_data) {
103 delete[] m_data; m_data = 0;
104 }
105}
106
108// Perform subband quantization with given quantization parameter.
109// A scalar quantization (with dead-zone) is used. A large quantization value
110// results in strong quantization and therefore in big quality loss.
111// @param quantParam A quantization parameter (larger or equal to 0)
112void CSubband::Quantize(int quantParam) {
113 if (m_orientation == LL) {
114 quantParam -= (m_level + 1);
115 // uniform rounding quantization
116 if (quantParam > 0) {
117 quantParam--;
118 for (UINT32 i=0; i < m_size; i++) {
119 if (m_data[i] < 0) {
120 m_data[i] = -(((-m_data[i] >> quantParam) + 1) >> 1);
121 } else {
122 m_data[i] = ((m_data[i] >> quantParam) + 1) >> 1;
123 }
124 }
125 }
126 } else {
127 if (m_orientation == HH) {
128 quantParam -= (m_level - 1);
129 } else {
130 quantParam -= m_level;
131 }
132 // uniform deadzone quantization
133 if (quantParam > 0) {
134 int threshold = ((1 << quantParam) * 7)/5; // good value
135 quantParam--;
136 for (UINT32 i=0; i < m_size; i++) {
137 if (m_data[i] < -threshold) {
138 m_data[i] = -(((-m_data[i] >> quantParam) + 1) >> 1);
139 } else if (m_data[i] > threshold) {
140 m_data[i] = ((m_data[i] >> quantParam) + 1) >> 1;
141 } else {
142 m_data[i] = 0;
143 }
144 }
145 }
146 }
147}
148
154void CSubband::Dequantize(int quantParam) {
155 if (m_orientation == LL) {
156 quantParam -= m_level + 1;
157 } else if (m_orientation == HH) {
158 quantParam -= m_level - 1;
159 } else {
160 quantParam -= m_level;
161 }
162 if (quantParam > 0) {
163 for (UINT32 i=0; i < m_size; i++) {
164 m_data[i] <<= quantParam;
165 }
166 }
167}
168
177void CSubband::ExtractTile(CEncoder& encoder, bool tile /*= false*/, UINT32 tileX /*= 0*/, UINT32 tileY /*= 0*/) THROW_ {
178#ifdef __PGFROISUPPORT__
179 if (tile) {
180 // compute tile position and size
181 UINT32 xPos, yPos, w, h;
182 TilePosition(tileX, tileY, xPos, yPos, w, h);
183
184 // write values into buffer using partitiong scheme
185 encoder.Partition(this, w, h, xPos + yPos*m_width, m_width);
186 } else
187#endif
188 {
189 // write values into buffer using partitiong scheme
190 encoder.Partition(this, m_width, m_height, 0, m_width);
191 }
192}
193
202void CSubband::PlaceTile(CDecoder& decoder, int quantParam, bool tile /*= false*/, UINT32 tileX /*= 0*/, UINT32 tileY /*= 0*/) THROW_ {
203 // allocate memory
204 if (!AllocMemory()) ReturnWithError(InsufficientMemory);
205
206 // correct quantParam with normalization factor
207 if (m_orientation == LL) {
208 quantParam -= m_level + 1;
209 } else if (m_orientation == HH) {
210 quantParam -= m_level - 1;
211 } else {
212 quantParam -= m_level;
213 }
214 if (quantParam < 0) quantParam = 0;
215
216#ifdef __PGFROISUPPORT__
217 if (tile) {
218 UINT32 xPos, yPos, w, h;
219
220 // compute tile position and size
221 TilePosition(tileX, tileY, xPos, yPos, w, h);
222
223 ASSERT(xPos >= m_ROI.left && yPos >= m_ROI.top);
224 decoder.Partition(this, quantParam, w, h, (xPos - m_ROI.left) + (yPos - m_ROI.top)*BufferWidth(), BufferWidth());
225 } else
226#endif
227 {
228 // read values into buffer using partitiong scheme
229 decoder.Partition(this, quantParam, m_width, m_height, 0, m_width);
230 }
231}
232
233
234
235#ifdef __PGFROISUPPORT__
244void CSubband::TilePosition(UINT32 tileX, UINT32 tileY, UINT32& xPos, UINT32& yPos, UINT32& w, UINT32& h) const {
245 // example
246 // band = HH, w = 30, ldTiles = 2 -> 4 tiles in a row/column
247 // --> tile widths
248 // 8 7 8 7
249 //
250 // tile partitioning scheme
251 // 0 1 2 3
252 // 4 5 6 7
253 // 8 9 A B
254 // C D E F
255
256 UINT32 nTiles = m_nTiles;
257 ASSERT(tileX < nTiles); ASSERT(tileY < nTiles);
258 UINT32 m;
259 UINT32 left = 0, right = nTiles;
260 UINT32 top = 0, bottom = nTiles;
261
262 xPos = 0;
263 yPos = 0;
264 w = m_width;
265 h = m_height;
266
267 while (nTiles > 1) {
268 // compute xPos and w with binary search
269 m = left + ((right - left) >> 1);
270 if (tileX >= m) {
271 xPos += (w + 1) >> 1;
272 w >>= 1;
273 left = m;
274 } else {
275 w = (w + 1) >> 1;
276 right = m;
277 }
278 // compute yPos and h with binary search
279 m = top + ((bottom - top) >> 1);
280 if (tileY >= m) {
281 yPos += (h + 1) >> 1;
282 h >>= 1;
283 top = m;
284 } else {
285 h = (h + 1) >> 1;
286 bottom = m;
287 }
288 nTiles >>= 1;
289 }
290 ASSERT(xPos < m_width && (xPos + w <= m_width));
291 ASSERT(yPos < m_height && (yPos + h <= m_height));
292}
293
294#endif
PGF decoder class.
PGF encoder class.
#define __PGFROISUPPORT__
Definition: PGFplatform.h:60
Orientation
Definition: PGFtypes.h:92
@ LL
Definition: PGFtypes.h:92
@ HH
Definition: PGFtypes.h:92
INT32 DataT
Definition: PGFtypes.h:219
PGF wavelet subband class.
PGF decoder.
Definition: Decoder.h:46
PGF encoder.
Definition: Encoder.h:46
UINT32 m_size
size of data buffer m_data
Definition: Subband.h:166
UINT32 m_dataPos
current position in m_data
Definition: Subband.h:169
bool AllocMemory()
Definition: Subband.cpp:77
void Dequantize(int quantParam)
Definition: Subband.cpp:154
void Initialize(UINT32 width, UINT32 height, int level, Orientation orient)
Definition: Subband.cpp:57
void Quantize(int quantParam)
Definition: Subband.cpp:112
void FreeMemory()
Delete the memory buffer of this subband.
Definition: Subband.cpp:101
UINT32 m_height
height in pixels
Definition: Subband.h:165
Orientation m_orientation
0=LL, 1=HL, 2=LH, 3=HH L=lowpass filtered, H=highpass filterd
Definition: Subband.h:168
DataT * m_data
buffer
Definition: Subband.h:170
int m_level
recursion level
Definition: Subband.h:167
void PlaceTile(CDecoder &decoder, int quantParam, bool tile=false, UINT32 tileX=0, UINT32 tileY=0) THROW_
Definition: Subband.cpp:202
void ExtractTile(CEncoder &encoder, bool tile=false, UINT32 tileX=0, UINT32 tileY=0) THROW_
Definition: Subband.cpp:177
~CSubband()
Destructor.
Definition: Subband.cpp:51
UINT32 m_width
width in pixels
Definition: Subband.h:164
CSubband()
Standard constructor.
Definition: Subband.cpp:35