libpgf 7.26.45
PGF - Progressive Graphics File
Loading...
Searching...
No Matches
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
33namespace pgf {
35 // Default constructor
37 : m_width(0)
38 , m_height(0)
39 , m_size(0)
40 , m_level(0)
42 , m_data(0)
43 , m_dataPos(0)
45 , m_nTiles(0)
46#endif
47 {
48 }
49
51 // Destructor
55
57 // Initialize subband parameters
58 void CSubband::Initialize(uint32_t width, uint32_t height, int level, Orient::Orientation orient) {
59 m_width = width;
60 m_height = height;
62 m_level = level;
63 m_orientation = orient;
64 m_data = 0;
65 m_dataPos = 0;
66#ifdef __PGFROISUPPORT__
67 m_ROI.left = 0;
68 m_ROI.top = 0;
69 m_ROI.right = m_width;
70 m_ROI.bottom = m_height;
71 m_nTiles = 0;
72#endif
73 }
74
76 // Allocate a memory buffer to store all wavelet coefficients of this subband.
77 // @return True if the allocation works without any problems
79 uint32_t oldSize = m_size;
80
81#ifdef __PGFROISUPPORT__
82 m_size = BufferWidth()*m_ROI.Height();
83#endif
84 ASSERT(m_size > 0);
85
86 if (m_data) {
87 if (oldSize >= m_size) {
88 return true;
89 } else {
90 delete[] m_data;
91 m_data = new(std::nothrow) DataT[m_size];
92 return (m_data != 0);
93 }
94 } else {
95 m_data = new(std::nothrow) DataT[m_size];
96 return (m_data != 0);
97 }
98 }
99
101 // Delete the memory buffer of this subband.
103 if (m_data) {
104 delete[] m_data; m_data = 0;
105 }
106 }
107
109 // Perform subband quantization with given quantization parameter.
110 // A scalar quantization (with dead-zone) is used. A large quantization value
111 // results in strong quantization and therefore in big quality loss.
112 // @param quantParam A quantization parameter (larger or equal to 0)
113 void CSubband::Quantize(int quantParam) {
114 if (m_orientation == Orient::LL) {
115 quantParam -= (m_level + 1);
116 // uniform rounding quantization
117 if (quantParam > 0) {
118 quantParam--;
119 for (uint32_t i = 0; i < m_size; i++) {
120 if (m_data[i] < 0) {
121 m_data[i] = -(((-m_data[i] >> quantParam) + 1) >> 1);
122 } else {
123 m_data[i] = ((m_data[i] >> quantParam) + 1) >> 1;
124 }
125 }
126 }
127 } else {
128 if (m_orientation == Orient::HH) {
129 quantParam -= (m_level - 1);
130 } else {
131 quantParam -= m_level;
132 }
133 // uniform deadzone quantization
134 if (quantParam > 0) {
135 int threshold = ((1 << quantParam) * 7)/5; // good value
136 quantParam--;
137 for (uint32_t i = 0; i < m_size; i++) {
138 if (m_data[i] < -threshold) {
139 m_data[i] = -(((-m_data[i] >> quantParam) + 1) >> 1);
140 } else if (m_data[i] > threshold) {
141 m_data[i] = ((m_data[i] >> quantParam) + 1) >> 1;
142 } else {
143 m_data[i] = 0;
144 }
145 }
146 }
147 }
148 }
149
155 void CSubband::Dequantize(int quantParam) {
156 if (m_orientation == Orient::LL) {
157 quantParam -= m_level + 1;
158 } else if (m_orientation == Orient::HH) {
159 quantParam -= m_level - 1;
160 } else {
161 quantParam -= m_level;
162 }
163 if (quantParam > 0) {
164 for (uint32_t i = 0; i < m_size; i++) {
165 m_data[i] <<= quantParam;
166 }
167 }
168 }
169
178 void CSubband::ExtractTile(CEncoder& encoder, bool tile /*= false*/, uint32_t tileX /*= 0*/, uint32_t tileY /*= 0*/) {
179#ifdef __PGFROISUPPORT__
180 if (tile) {
181 // compute tile position and size
182 uint32_t xPos, yPos, w, h;
183 TilePosition(tileX, tileY, xPos, yPos, w, h);
184
185 // write values into buffer using partitiong scheme
186 encoder.Partition(this, w, h, xPos + yPos*m_width, m_width);
187 } else
188#endif
189 {
190 tileX; tileY; tile; // prevents from unreferenced formal parameter warning
191 // write values into buffer using partitiong scheme
192 encoder.Partition(this, m_width, m_height, 0, m_width);
193 }
194 }
195
204 void CSubband::PlaceTile(CDecoder& decoder, int quantParam, bool tile /*= false*/, uint32_t tileX /*= 0*/, uint32_t tileY /*= 0*/) {
205 // allocate memory
206 if (!AllocMemory()) ReturnWithError(InsufficientMemory);
207
208 // correct quantParam with normalization factor
209 if (m_orientation == Orient::LL) {
210 quantParam -= m_level + 1;
211 } else if (m_orientation == Orient::HH) {
212 quantParam -= m_level - 1;
213 } else {
214 quantParam -= m_level;
215 }
216 if (quantParam < 0) quantParam = 0;
217
218#ifdef __PGFROISUPPORT__
219 if (tile) {
220 uint32_t xPos, yPos, w, h;
221
222 // compute tile position and size
223 TilePosition(tileX, tileY, xPos, yPos, w, h);
224
225 ASSERT(xPos >= m_ROI.left && yPos >= m_ROI.top);
226 decoder.Partition(this, quantParam, w, h, (xPos - m_ROI.left) + (yPos - m_ROI.top)*BufferWidth(), BufferWidth());
227 } else
228#endif
229 {
230 tileX; tileY; tile; // prevents from unreferenced formal parameter warning
231 // read values into buffer using partitiong scheme
232 decoder.Partition(this, quantParam, m_width, m_height, 0, m_width);
233 }
234 }
235
236
237
238#ifdef __PGFROISUPPORT__
241 void CSubband::SetAlignedROI(const PGFRect& roi) {
242 ASSERT(roi.left <= m_width);
243 ASSERT(roi.top <= m_height);
244
245 m_ROI = roi;
246 if (m_ROI.right > m_width) m_ROI.right = m_width;
247 if (m_ROI.bottom > m_height) m_ROI.bottom = m_height;
248 }
249
258 void CSubband::TilePosition(uint32_t tileX, uint32_t tileY, uint32_t& xPos, uint32_t& yPos, uint32_t& w, uint32_t& h) const {
259 ASSERT(tileX < m_nTiles); ASSERT(tileY < m_nTiles);
260 // example
261 // band = HH, w = 30, ldTiles = 2 -> 4 tiles in a row/column
262 // --> tile widths
263 // 8 7 8 7
264 //
265 // tile partitioning scheme
266 // 0 1 2 3
267 // 4 5 6 7
268 // 8 9 A B
269 // C D E F
270
271 uint32_t nTiles = m_nTiles;
272 uint32_t m;
273 uint32_t left = 0, right = nTiles;
274 uint32_t top = 0, bottom = nTiles;
275
276 xPos = 0;
277 yPos = 0;
278 w = m_width;
279 h = m_height;
280
281 while (nTiles > 1) {
282 // compute xPos and w with binary search
283 m = left + ((right - left) >> 1);
284 if (tileX >= m) {
285 xPos += (w + 1) >> 1;
286 w >>= 1;
287 left = m;
288 } else {
289 w = (w + 1) >> 1;
290 right = m;
291 }
292 // compute yPos and h with binary search
293 m = top + ((bottom - top) >> 1);
294 if (tileY >= m) {
295 yPos += (h + 1) >> 1;
296 h >>= 1;
297 top = m;
298 } else {
299 h = (h + 1) >> 1;
300 bottom = m;
301 }
302 nTiles >>= 1;
303 }
304 ASSERT(xPos < m_width && (xPos + w <= m_width));
305 ASSERT(yPos < m_height && (yPos + h <= m_height));
306 }
307
310 void CSubband::TileIndex(bool topLeft, uint32_t xPos, uint32_t yPos, uint32_t& tileX, uint32_t& tileY, uint32_t& x, uint32_t& y) const {
311 uint32_t m;
312 uint32_t left = 0, right = m_width;
313 uint32_t top = 0, bottom = m_height;
314 uint32_t nTiles = m_nTiles;
315
316 if (xPos > m_width) xPos = m_width;
317 if (yPos > m_height) yPos = m_height;
318
319 if (topLeft) {
320 // compute tileX with binary search
321 tileX = 0;
322 while (nTiles > 1) {
323 nTiles >>= 1;
324 m = left + ((right - left + 1) >> 1);
325 if (xPos < m) {
326 // exclusive m
327 right = m;
328 } else {
329 tileX += nTiles;
330 left = m;
331 }
332 }
333 x = left;
334 ASSERT(tileX >= 0 && tileX < m_nTiles);
335
336 // compute tileY with binary search
337 nTiles = m_nTiles;
338 tileY = 0;
339 while (nTiles > 1) {
340 nTiles >>= 1;
341 m = top + ((bottom - top + 1) >> 1);
342 if (yPos < m) {
343 // exclusive m
344 bottom = m;
345 } else {
346 tileY += nTiles;
347 top = m;
348 }
349 }
350 y = top;
351 ASSERT(tileY >= 0 && tileY < m_nTiles);
352
353 } else {
354 // compute tileX with binary search
355 tileX = 1;
356 while (nTiles > 1) {
357 nTiles >>= 1;
358 m = left + ((right - left + 1) >> 1);
359 if (xPos <= m) {
360 // inclusive m
361 right = m;
362 } else {
363 tileX += nTiles;
364 left = m;
365 }
366 }
367 x = right;
368 ASSERT(tileX > 0 && tileX <= m_nTiles);
369
370 // compute tileY with binary search
371 nTiles = m_nTiles;
372 tileY = 1;
373 while (nTiles > 1) {
374 nTiles >>= 1;
375 m = top + ((bottom - top + 1) >> 1);
376 if (yPos <= m) {
377 // inclusive m
378 bottom = m;
379 } else {
380 tileY += nTiles;
381 top = m;
382 }
383 }
384 y = bottom;
385 ASSERT(tileY > 0 && tileY <= m_nTiles);
386 }
387 }
388
389#endif
390}
PGF decoder class.
PGF encoder class.
#define __PGFROISUPPORT__
Definition PGFplatform.h:60
PGF wavelet subband class.
PGF decoder.
Definition Decoder.h:41
void Partition(CSubband *band, int quantParam, int width, int height, int startPos, int pitch)
Definition Decoder.cpp:267
PGF encoder.
Definition Encoder.h:41
void Partition(CSubband *band, int width, int height, int startPos, int pitch)
Definition Encoder.cpp:247
void ExtractTile(CEncoder &encoder, bool tile=false, uint32_t tileX=0, uint32_t tileY=0)
Definition Subband.cpp:178
bool AllocMemory()
Definition Subband.cpp:78
int m_level
recursion level
Definition Subband.h:169
uint32_t m_height
height in pixels
Definition Subband.h:167
uint32_t m_dataPos
current position in m_data
Definition Subband.h:171
void Dequantize(int quantParam)
Definition Subband.cpp:155
void FreeMemory()
Delete the memory buffer of this subband.
Definition Subband.cpp:102
uint32_t m_size
size of data buffer m_data
Definition Subband.h:168
void Initialize(uint32_t width, uint32_t height, int level, Orient::Orientation orient)
Definition Subband.cpp:58
CSubband()
Standard constructor.
Definition Subband.cpp:36
Orient::Orientation m_orientation
0=LL, 1=HL, 2=LH, 3=HH L=lowpass filtered, H=highpass filterd
Definition Subband.h:170
DataT * m_data
buffer
Definition Subband.h:172
uint32_t m_width
width in pixels
Definition Subband.h:166
void PlaceTile(CDecoder &decoder, int quantParam, bool tile=false, uint32_t tileX=0, uint32_t tileY=0)
Definition Subband.cpp:204
void Quantize(int quantParam)
Definition Subband.cpp:113
~CSubband()
Destructor.
Definition Subband.cpp:52
constexpr Orientation LL
Definition PGFtypes.h:104
constexpr Orientation HH
Definition PGFtypes.h:107
uint8_t Orientation
Definition PGFtypes.h:102
Definition PGFimage.h:33
int32_t DataT
Definition PGFtypes.h:367
Rectangle.
Definition PGFtypes.h:258
uint32_t top
Definition PGFtypes.h:259
uint32_t right
Definition PGFtypes.h:259
uint32_t left
Definition PGFtypes.h:259