libpgf  6.12.24
PGF - Progressive Graphics File
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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_size(0)
37 , m_data(0)
38 #ifdef __PGFROISUPPORT__
39 , m_nTiles(0)
40 #endif
41 {
42 }
43 
45 // Destructor
47  FreeMemory();
48 }
49 
51 // Initialize subband parameters
52 void CSubband::Initialize(UINT32 width, UINT32 height, int level, Orientation orient) {
53  m_width = width;
54  m_height = height;
56  m_level = level;
57  m_orientation = orient;
58  m_data = 0;
59  m_dataPos = 0;
60 #ifdef __PGFROISUPPORT__
61  m_ROI.left = 0;
62  m_ROI.top = 0;
63  m_ROI.right = m_width;
64  m_ROI.bottom = m_height;
65  m_nTiles = 0;
66 #endif
67 }
68 
70 // Allocate a memory buffer to store all wavelet coefficients of this subband.
71 // @return True if the allocation works without any problems
73  UINT32 oldSize = m_size;
74 
75 #ifdef __PGFROISUPPORT__
76  m_size = BufferWidth()*m_ROI.Height();
77 #endif
78  ASSERT(m_size > 0);
79 
80  if (m_data) {
81  if (oldSize >= m_size) {
82  return true;
83  } else {
84  delete[] m_data;
85  m_data = new(std::nothrow) DataT[m_size];
86  return (m_data != 0);
87  }
88  } else {
89  m_data = new(std::nothrow) DataT[m_size];
90  return (m_data != 0);
91  }
92 }
93 
95 // Delete the memory buffer of this subband.
97  if (m_data) {
98  delete[] m_data; m_data = 0;
99  }
100 }
101 
103 // Perform subband quantization with given quantization parameter.
104 // A scalar quantization (with dead-zone) is used. A large quantization value
105 // results in strong quantization and therefore in big quality loss.
106 // @param quantParam A quantization parameter (larger or equal to 0)
107 void CSubband::Quantize(int quantParam) {
108  if (m_orientation == LL) {
109  quantParam -= (m_level + 1);
110  // uniform rounding quantization
111  if (quantParam > 0) {
112  quantParam--;
113  for (UINT32 i=0; i < m_size; i++) {
114  if (m_data[i] < 0) {
115  m_data[i] = -(((-m_data[i] >> quantParam) + 1) >> 1);
116  } else {
117  m_data[i] = ((m_data[i] >> quantParam) + 1) >> 1;
118  }
119  }
120  }
121  } else {
122  if (m_orientation == HH) {
123  quantParam -= (m_level - 1);
124  } else {
125  quantParam -= m_level;
126  }
127  // uniform deadzone quantization
128  if (quantParam > 0) {
129  int threshold = ((1 << quantParam) * 7)/5; // good value
130  quantParam--;
131  for (UINT32 i=0; i < m_size; i++) {
132  if (m_data[i] < -threshold) {
133  m_data[i] = -(((-m_data[i] >> quantParam) + 1) >> 1);
134  } else if (m_data[i] > threshold) {
135  m_data[i] = ((m_data[i] >> quantParam) + 1) >> 1;
136  } else {
137  m_data[i] = 0;
138  }
139  }
140  }
141  }
142 }
143 
149 void CSubband::Dequantize(int quantParam) {
150  if (m_orientation == LL) {
151  quantParam -= m_level + 1;
152  } else if (m_orientation == HH) {
153  quantParam -= m_level - 1;
154  } else {
155  quantParam -= m_level;
156  }
157  if (quantParam > 0) {
158  for (UINT32 i=0; i < m_size; i++) {
159  m_data[i] <<= quantParam;
160  }
161  }
162 }
163 
172 void CSubband::ExtractTile(CEncoder& encoder, bool tile /*= false*/, UINT32 tileX /*= 0*/, UINT32 tileY /*= 0*/) THROW_ {
173 #ifdef __PGFROISUPPORT__
174  if (tile) {
175  // compute tile position and size
176  UINT32 xPos, yPos, w, h;
177  TilePosition(tileX, tileY, xPos, yPos, w, h);
178 
179  // write values into buffer using partitiong scheme
180  encoder.Partition(this, w, h, xPos + yPos*m_width, m_width);
181  } else
182 #endif
183  {
184  // write values into buffer using partitiong scheme
185  encoder.Partition(this, m_width, m_height, 0, m_width);
186  }
187 }
188 
197 void CSubband::PlaceTile(CDecoder& decoder, int quantParam, bool tile /*= false*/, UINT32 tileX /*= 0*/, UINT32 tileY /*= 0*/) THROW_ {
198  // allocate memory
199  if (!AllocMemory()) ReturnWithError(InsufficientMemory);
200 
201  // correct quantParam with normalization factor
202  if (m_orientation == LL) {
203  quantParam -= m_level + 1;
204  } else if (m_orientation == HH) {
205  quantParam -= m_level - 1;
206  } else {
207  quantParam -= m_level;
208  }
209  if (quantParam < 0) quantParam = 0;
210 
211 #ifdef __PGFROISUPPORT__
212  if (tile) {
213  UINT32 xPos, yPos, w, h;
214 
215  // compute tile position and size
216  TilePosition(tileX, tileY, xPos, yPos, w, h);
217 
218  ASSERT(xPos >= m_ROI.left && yPos >= m_ROI.top);
219  decoder.Partition(this, quantParam, w, h, (xPos - m_ROI.left) + (yPos - m_ROI.top)*BufferWidth(), BufferWidth());
220  } else
221 #endif
222  {
223  // read values into buffer using partitiong scheme
224  decoder.Partition(this, quantParam, m_width, m_height, 0, m_width);
225  }
226 }
227 
228 
229 
230 #ifdef __PGFROISUPPORT__
231 
232 
233 
234 
235 
236 
237 
238 
239 void CSubband::TilePosition(UINT32 tileX, UINT32 tileY, UINT32& xPos, UINT32& yPos, UINT32& w, UINT32& h) const {
240  // example
241  // band = HH, w = 30, ldTiles = 2 -> 4 tiles in a row/column
242  // --> tile widths
243  // 8 7 8 7
244  //
245  // tile partitioning scheme
246  // 0 1 2 3
247  // 4 5 6 7
248  // 8 9 A B
249  // C D E F
250 
251  UINT32 nTiles = m_nTiles;
252  ASSERT(tileX < nTiles); ASSERT(tileY < nTiles);
253  UINT32 m;
254  UINT32 left = 0, right = nTiles;
255  UINT32 top = 0, bottom = nTiles;
256 
257  xPos = 0;
258  yPos = 0;
259  w = m_width;
260  h = m_height;
261 
262  while (nTiles > 1) {
263  // compute xPos and w with binary search
264  m = (left + right) >> 1;
265  if (tileX >= m) {
266  xPos += (w + 1) >> 1;
267  w >>= 1;
268  left = m;
269  } else {
270  w = (w + 1) >> 1;
271  right = m;
272  }
273  // compute yPos and h with binary search
274  m = (top + bottom) >> 1;
275  if (tileY >= m) {
276  yPos += (h + 1) >> 1;
277  h >>= 1;
278  top = m;
279  } else {
280  h = (h + 1) >> 1;
281  bottom = m;
282  }
283  nTiles >>= 1;
284  }
285  ASSERT(xPos < m_width && (xPos + w <= m_width));
286  ASSERT(yPos < m_height && (yPos + h <= m_height));
287 }
288 
289 #endif