libpgf 7.26.45
PGF - Progressive Graphics File
Loading...
Searching...
No Matches
pgf::CWaveletTransform Class Reference

PGF wavelet transform. More...

#include <WaveletTransform.h>

Public Member Functions

 CWaveletTransform (uint32_t width, uint32_t height, int levels, DataT *data=nullptr)
 ~CWaveletTransform ()
 Destructor.
OSError ForwardTransform (int level, int quant)
OSError InverseTransform (int level, uint32_t *width, uint32_t *height, DataT **data)
CSubband * GetSubband (int level, Orient::Orientation orientation)

Private Member Functions

void Destroy ()
void InitSubbands (uint32_t width, uint32_t height, DataT *data)
void ForwardRow (DataT *buff, uint32_t width)
void InverseRow (DataT *buff, uint32_t width)
void InterleavedToSubbands (int destLevel, DataT *loRow, DataT *hiRow, uint32_t width)
void SubbandsToInterleaved (int srcLevel, DataT *loRow, DataT *hiRow, uint32_t width)

Private Attributes

int m_nLevels
 number of LL levels: one more than header.nLevels in PGFimage
CSubband(* m_subband )[NSubbands]
 quadtree of subbands: LL HL LH HH

Friends

class CSubband

Detailed Description

PGF wavelet transform.

PGF wavelet transform class.

Author
C. Stamm, R. Spuler

Definition at line 55 of file WaveletTransform.h.

Constructor & Destructor Documentation

◆ CWaveletTransform()

pgf::CWaveletTransform::CWaveletTransform ( uint32_t width,
uint32_t height,
int levels,
DataT * data = nullptr )

Constructor: Constructs a wavelet transform pyramid of given size and levels.

Parameters
widthThe width of the original image (at level 0) in pixels
heightThe height of the original image (at level 0) in pixels
levelsThe number of levels (>= 0)
dataInput data of subband LL at level 0

Definition at line 41 of file WaveletTransform.cpp.

42 : m_nLevels(levels + 1) // m_nLevels in CPGFImage determines the number of FWT steps; this.m_nLevels determines the number subband-planes
43 , m_subband(nullptr)
44#ifdef __PGFROISUPPORT__
45 , m_indices(nullptr)
46#endif
47 {
48 ASSERT(m_nLevels > 0 && m_nLevels <= MaxLevel + 1);
49 InitSubbands(width, height, data);
50 }
CSubband(* m_subband)[NSubbands]
quadtree of subbands: LL HL LH HH
void InitSubbands(uint32_t width, uint32_t height, DataT *data)
int m_nLevels
number of LL levels: one more than header.nLevels in PGFimage
constexpr int MaxLevel
maximum number of transform levels
Definition PGFtypes.h:62

◆ ~CWaveletTransform()

pgf::CWaveletTransform::~CWaveletTransform ( )
inline

Destructor.

Definition at line 69 of file WaveletTransform.h.

Member Function Documentation

◆ Destroy()

void pgf::CWaveletTransform::Destroy ( )
inlineprivate

Definition at line 125 of file WaveletTransform.h.

125 {
126 delete[] m_subband; m_subband = nullptr;
127#ifdef __PGFROISUPPORT__
128 delete[] m_indices; m_indices = nullptr;
129#endif
130 }

◆ ForwardRow()

void pgf::CWaveletTransform::ForwardRow ( DataT * buff,
uint32_t width )
private

Definition at line 181 of file WaveletTransform.cpp.

181 {
182 if (width >= FilterSize) {
183 uint32_t i = 3;
184
185 // left border handling
186 src[1] -= ((src[0] + src[2] + c1) >> 1); // high pass
187 src[0] += ((src[1] + c1) >> 1); // low pass
188
189 // middle part
190 for (; i < width-1; i += 2) {
191 src[i] -= ((src[i-1] + src[i+1] + c1) >> 1); // high pass
192 src[i-1] += ((src[i-2] + src[i] + c2) >> 2); // low pass
193 }
194
195 // right border handling
196 if (width & 1) {
197 src[i-1] += ((src[i-2] + c1) >> 1); // low pass
198 } else {
199 src[i] -= src[i-1]; // high pass
200 src[i-1] += ((src[i-2] + src[i] + c2) >> 2); // low pass
201 }
202 }
203 }
constexpr DataT c2
constexpr DataT c1
constexpr uint32_t FilterSize

◆ ForwardTransform()

OSError pgf::CWaveletTransform::ForwardTransform ( int level,
int quant )

Compute fast forward wavelet transform of LL subband at given level and stores result in all 4 subbands of level + 1.

Parameters
levelA wavelet transform pyramid level (>= 0 && < Levels())
quantA quantization value (linear scalar quantization)
Returns
error in case of a memory allocation problem

Definition at line 89 of file WaveletTransform.cpp.

89 {
90 ASSERT(level >= 0 && level < m_nLevels - 1);
91 const int destLevel = level + 1;
92 ASSERT(m_subband[destLevel]);
93 CSubband* srcBand = &m_subband[level][Orient::LL]; ASSERT(srcBand);
94 const uint32_t width = srcBand->GetWidth();
95 const uint32_t height = srcBand->GetHeight();
96 DataT* src = srcBand->GetBuffer(); ASSERT(src);
97 DataT* row0, * row1, * row2, * row3;
98
99 // Allocate memory for next transform level
100 for (int i = 0; i < NSubbands; i++) {
101 if (!m_subband[destLevel][i].AllocMemory()) return InsufficientMemory;
102 }
103
104 if (height >= FilterSize) { // changed from FilterSizeH to FilterSize
105 // top border handling
106 row0 = src; row1 = row0 + width; row2 = row1 + width;
107 ForwardRow(row0, width);
108 ForwardRow(row1, width);
109 ForwardRow(row2, width);
110 for (uint32_t k = 0; k < width; k++) {
111 row1[k] -= ((row0[k] + row2[k] + c1) >> 1); // high pass
112 row0[k] += ((row1[k] + c1) >> 1); // low pass
113 }
114 InterleavedToSubbands(destLevel, row0, row1, width);
115 row0 = row1; row1 = row2; row2 += width; row3 = row2 + width;
116
117 // middle part
118 for (uint32_t i = 3; i < height-1; i += 2) {
119 ForwardRow(row2, width);
120 ForwardRow(row3, width);
121 for (uint32_t k = 0; k < width; k++) {
122 row2[k] -= ((row1[k] + row3[k] + c1) >> 1); // high pass filter
123 row1[k] += ((row0[k] + row2[k] + c2) >> 2); // low pass filter
124 }
125 InterleavedToSubbands(destLevel, row1, row2, width);
126 row0 = row2; row1 = row3; row2 = row3 + width; row3 = row2 + width;
127 }
128
129 // bottom border handling
130 if (height & 1) {
131 for (uint32_t k = 0; k < width; k++) {
132 row1[k] += ((row0[k] + c1) >> 1); // low pass
133 }
134 InterleavedToSubbands(destLevel, row1, nullptr, width);
135 row0 = row1; row1 += width;
136 } else {
137 ForwardRow(row2, width);
138 for (uint32_t k = 0; k < width; k++) {
139 row2[k] -= row1[k]; // high pass
140 row1[k] += ((row0[k] + row2[k] + c2) >> 2); // low pass
141 }
142 InterleavedToSubbands(destLevel, row1, row2, width);
143 row0 = row1; row1 = row2; row2 += width;
144 }
145 } else {
146 // if height is too small
147 row0 = src; row1 = row0 + width;
148 // first part
149 for (uint32_t k = 0; k < height; k += 2) {
150 ForwardRow(row0, width);
151 ForwardRow(row1, width);
152 InterleavedToSubbands(destLevel, row0, row1, width);
153 row0 += width << 1; row1 += width << 1;
154 }
155 // bottom
156 if (height & 1) {
157 InterleavedToSubbands(destLevel, row0, nullptr, width);
158 }
159 }
160
161 if (quant > 0) {
162 // subband quantization (without LL)
163 for (int i = 1; i < NSubbands; i++) {
164 m_subband[destLevel][i].Quantize(quant);
165 }
166 // LL subband quantization
167 if (destLevel == m_nLevels - 1) {
168 m_subband[destLevel][Orient::LL].Quantize(quant);
169 }
170 }
171
172 // free source band
173 srcBand->FreeMemory();
174 return NoError;
175 }
void Quantize(int quantParam)
Definition Subband.cpp:113
void InterleavedToSubbands(int destLevel, DataT *loRow, DataT *hiRow, uint32_t width)
void ForwardRow(DataT *buff, uint32_t width)
constexpr Orientation LL
Definition PGFtypes.h:104
int32_t DataT
Definition PGFtypes.h:367
constexpr int NSubbands
number of subbands per level
Definition PGFtypes.h:63

◆ GetSubband()

CSubband * pgf::CWaveletTransform::GetSubband ( int level,
Orient::Orientation orientation )
inline

Get pointer to one of the 4 subband at a given level.

Parameters
levelA wavelet transform pyramid level (>= 0 && <= Levels())
orientationA quarter of the subband (LL, LH, HL, HH)

Definition at line 93 of file WaveletTransform.h.

93 {
94 ASSERT(level >= 0 && level < m_nLevels);
95 return &m_subband[level][orientation];
96 }

◆ InitSubbands()

void pgf::CWaveletTransform::InitSubbands ( uint32_t width,
uint32_t height,
DataT * data )
private

Definition at line 54 of file WaveletTransform.cpp.

54 {
55 if (m_subband) Destroy();
56
57 // create subbands
59
60 // init subbands
61 uint32_t loWidth = width;
62 uint32_t hiWidth = width;
63 uint32_t loHeight = height;
64 uint32_t hiHeight = height;
65
66 for (int level = 0; level < m_nLevels; level++) {
67 m_subband[level][Orient::LL].Initialize(loWidth, loHeight, level, Orient::LL); // LL
68 m_subband[level][Orient::HL].Initialize(hiWidth, loHeight, level, Orient::HL); // HL
69 m_subband[level][Orient::LH].Initialize(loWidth, hiHeight, level, Orient::LH); // LH
70 m_subband[level][Orient::HH].Initialize(hiWidth, hiHeight, level, Orient::HH); // HH
71 hiWidth = loWidth >> 1; hiHeight = loHeight >> 1;
72 loWidth = (loWidth + 1) >> 1; loHeight = (loHeight + 1) >> 1;
73 }
74 if (data) {
76 }
77 }
void Initialize(uint32_t width, uint32_t height, int level, Orient::Orientation orient)
Definition Subband.cpp:58
void SetBuffer(DataT *b)
Definition Subband.h:148
constexpr Orientation HH
Definition PGFtypes.h:107
constexpr Orientation LH
Definition PGFtypes.h:106
constexpr Orientation HL
Definition PGFtypes.h:105

◆ InterleavedToSubbands()

void pgf::CWaveletTransform::InterleavedToSubbands ( int destLevel,
DataT * loRow,
DataT * hiRow,
uint32_t width )
private

Definition at line 207 of file WaveletTransform.cpp.

207 {
208 const uint32_t wquot = width >> 1;
209 const bool wrem = (width & 1);
210 CSubband& ll = m_subband[destLevel][Orient::LL], & hl = m_subband[destLevel][Orient::HL];
211 CSubband& lh = m_subband[destLevel][Orient::LH], & hh = m_subband[destLevel][Orient::HH];
212
213 if (hiRow) {
214 for (uint32_t i = 0; i < wquot; i++) {
215 ll.WriteBuffer(*loRow++); // first access, than increment
216 hl.WriteBuffer(*loRow++);
217 lh.WriteBuffer(*hiRow++); // first access, than increment
218 hh.WriteBuffer(*hiRow++);
219 }
220 if (wrem) {
221 ll.WriteBuffer(*loRow);
222 lh.WriteBuffer(*hiRow);
223 }
224 } else {
225 for (uint32_t i = 0; i < wquot; i++) {
226 ll.WriteBuffer(*loRow++); // first access, than increment
227 hl.WriteBuffer(*loRow++);
228 }
229 if (wrem) ll.WriteBuffer(*loRow);
230 }
231 }

◆ InverseRow()

void pgf::CWaveletTransform::InverseRow ( DataT * buff,
uint32_t width )
private

Definition at line 420 of file WaveletTransform.cpp.

420 {
421 if (width >= FilterSize) {
422 uint32_t i = 2;
423
424 // left border handling
425 dest[0] -= ((dest[1] + c1) >> 1); // even
426
427 // middle part
428 for (; i < width - 1; i += 2) {
429 dest[i] -= ((dest[i-1] + dest[i+1] + c2) >> 2); // even
430 dest[i-1] += ((dest[i-2] + dest[i] + c1) >> 1); // odd
431 }
432
433 // right border handling
434 if (width & 1) {
435 dest[i] -= ((dest[i-1] + c1) >> 1); // even
436 dest[i-1] += ((dest[i-2] + dest[i] + c1) >> 1); // odd
437 } else {
438 dest[i-1] += dest[i-2]; // odd
439 }
440 }
441 }

◆ InverseTransform()

OSError pgf::CWaveletTransform::InverseTransform ( int level,
uint32_t * width,
uint32_t * height,
DataT ** data )

Compute fast inverse wavelet transform of all 4 subbands of given level and stores result in LL subband of level - 1.

Parameters
levelA wavelet transform pyramid level (> 0 && <= Levels())
widthA pointer to the returned width of subband LL (in pixels)
heightA pointer to the returned height of subband LL (in pixels)
dataA pointer to the returned array of image data
Returns
error in case of a memory allocation problem

Definition at line 246 of file WaveletTransform.cpp.

246 {
247 ASSERT(srcLevel > 0 && srcLevel < m_nLevels);
248 const int destLevel = srcLevel - 1;
249 ASSERT(m_subband[destLevel]);
250 CSubband* destBand = &m_subband[destLevel][Orient::LL];
251 uint32_t width, height;
252
253 // allocate memory for the results of the inverse transform
254 if (!destBand->AllocMemory()) return InsufficientMemory;
255 DataT* origin = destBand->GetBuffer(), * row0, * row1, * row2, * row3;
256
257#ifdef __PGFROISUPPORT__
258 PGFRect destROI = destBand->GetAlignedROI();
259 const uint32_t destWidth = destROI.Width(); // destination buffer width
260 const uint32_t destHeight = destROI.Height(); // destination buffer height
261 width = destWidth; // destination working width
262 height = destHeight; // destination working height
263
264 // update destination ROI
265 if (destROI.top & 1) {
266 destROI.top++;
267 origin += destWidth;
268 height--;
269 }
270 if (destROI.left & 1) {
271 destROI.left++;
272 origin++;
273 width--;
274 }
275
276 // init source buffer position
277 const uint32_t leftD = destROI.left >> 1;
278 const uint32_t left0 = m_subband[srcLevel][Orient::LL].GetAlignedROI().left;
279 const uint32_t left1 = m_subband[srcLevel][Orient::HL].GetAlignedROI().left;
280 const uint32_t topD = destROI.top >> 1;
281 const uint32_t top0 = m_subband[srcLevel][Orient::LL].GetAlignedROI().top;
282 const uint32_t top1 = m_subband[srcLevel][Orient::LH].GetAlignedROI().top;
283 ASSERT(m_subband[srcLevel][Orient::LH].GetAlignedROI().left == left0);
284 ASSERT(m_subband[srcLevel][Orient::HH].GetAlignedROI().left == left1);
285 ASSERT(m_subband[srcLevel][Orient::HL].GetAlignedROI().top == top0);
286 ASSERT(m_subband[srcLevel][Orient::HH].GetAlignedROI().top == top1);
287
288 uint32_t srcOffsetX[2] = { 0, 0 };
289 uint32_t srcOffsetY[2] = { 0, 0 };
290
291 if (leftD >= __max(left0, left1)) {
292 srcOffsetX[0] = leftD - left0;
293 srcOffsetX[1] = leftD - left1;
294 } else {
295 if (left0 <= left1) {
296 const uint32_t dx = (left1 - leftD) << 1;
297 destROI.left += dx;
298 origin += dx;
299 width -= dx;
300 srcOffsetX[0] = left1 - left0;
301 } else {
302 const uint32_t dx = (left0 - leftD) << 1;
303 destROI.left += dx;
304 origin += dx;
305 width -= dx;
306 srcOffsetX[1] = left0 - left1;
307 }
308 }
309 if (topD >= __max(top0, top1)) {
310 srcOffsetY[0] = topD - top0;
311 srcOffsetY[1] = topD - top1;
312 } else {
313 if (top0 <= top1) {
314 const uint32_t dy = (top1 - topD) << 1;
315 destROI.top += dy;
316 origin += dy*destWidth;
317 height -= dy;
318 srcOffsetY[0] = top1 - top0;
319 } else {
320 const uint32_t dy = (top0 - topD) << 1;
321 destROI.top += dy;
322 origin += dy*destWidth;
323 height -= dy;
324 srcOffsetY[1] = top0 - top1;
325 }
326 }
327
328 m_subband[srcLevel][pgf::Orient::LL].InitBuffPos(srcOffsetX[0], srcOffsetY[0]);
329 m_subband[srcLevel][pgf::Orient::HL].InitBuffPos(srcOffsetX[1], srcOffsetY[0]);
330 m_subband[srcLevel][pgf::Orient::LH].InitBuffPos(srcOffsetX[0], srcOffsetY[1]);
331 m_subband[srcLevel][pgf::Orient::HH].InitBuffPos(srcOffsetX[1], srcOffsetY[1]);
332
333#else
334 width = destBand->GetWidth();
335 height = destBand->GetHeight();
336 PGFRect destROI(0, 0, width, height);
337 const uint32_t destWidth = width; // destination buffer width
338 const uint32_t destHeight = height; // destination buffer height
339
340 // init source buffer position
341 for (int i = 0; i < NSubbands; i++) {
342 m_subband[srcLevel][i].InitBuffPos();
343 }
344#endif
345
346 if (destHeight >= FilterSize) { // changed from FilterSizeH to FilterSize
347 // top border handling
348 row0 = origin; row1 = row0 + destWidth;
349 SubbandsToInterleaved(srcLevel, row0, row1, width);
350 for (uint32_t k = 0; k < width; k++) {
351 row0[k] -= ((row1[k] + c1) >> 1); // even
352 }
353
354 // middle part
355 row2 = row1 + destWidth; row3 = row2 + destWidth;
356 for (uint32_t i = destROI.top + 2; i < destROI.bottom - 1; i += 2) {
357 SubbandsToInterleaved(srcLevel, row2, row3, width);
358 for (uint32_t k = 0; k < width; k++) {
359 row2[k] -= ((row1[k] + row3[k] + c2) >> 2); // even
360 row1[k] += ((row0[k] + row2[k] + c1) >> 1); // odd
361 }
362 InverseRow(row0, width);
363 InverseRow(row1, width);
364 row0 = row2; row1 = row3; row2 = row1 + destWidth; row3 = row2 + destWidth;
365 }
366
367 // bottom border handling
368 if (height & 1) {
369 SubbandsToInterleaved(srcLevel, row2, nullptr, width);
370 for (uint32_t k = 0; k < width; k++) {
371 row2[k] -= ((row1[k] + c1) >> 1); // even
372 row1[k] += ((row0[k] + row2[k] + c1) >> 1); // odd
373 }
374 InverseRow(row0, width);
375 InverseRow(row1, width);
376 InverseRow(row2, width);
377 row0 = row1; row1 = row2; row2 += destWidth;
378 } else {
379 for (uint32_t k = 0; k < width; k++) {
380 row1[k] += row0[k];
381 }
382 InverseRow(row0, width);
383 InverseRow(row1, width);
384 row0 = row1; row1 += destWidth;
385 }
386 } else {
387 // height is too small
388 row0 = origin; row1 = row0 + destWidth;
389 // first part
390 for (uint32_t k = 0; k < height; k += 2) {
391 SubbandsToInterleaved(srcLevel, row0, row1, width);
392 InverseRow(row0, width);
393 InverseRow(row1, width);
394 row0 += destWidth << 1; row1 += destWidth << 1;
395 }
396 // bottom
397 if (height & 1) {
398 SubbandsToInterleaved(srcLevel, row0, nullptr, width);
399 InverseRow(row0, width);
400 }
401 }
402
403 // free memory of the current srcLevel
404 for (int i = 0; i < NSubbands; i++) {
405 m_subband[srcLevel][i].FreeMemory();
406 }
407
408 // return info
409 *w = destWidth;
410 *h = destHeight;
411 *data = destBand->GetBuffer();
412 return NoError;
413 }
#define __max(x, y)
DataT * GetBuffer()
Definition Subband.h:107
void FreeMemory()
Delete the memory buffer of this subband.
Definition Subband.cpp:102
void InitBuffPos()
Definition Subband.h:162
void SubbandsToInterleaved(int srcLevel, DataT *loRow, DataT *hiRow, uint32_t width)
void InverseRow(DataT *buff, uint32_t width)

◆ SubbandsToInterleaved()

void pgf::CWaveletTransform::SubbandsToInterleaved ( int srcLevel,
DataT * loRow,
DataT * hiRow,
uint32_t width )
private

Definition at line 445 of file WaveletTransform.cpp.

445 {
446 const uint32_t wquot = width >> 1;
447 const bool wrem = (width & 1);
448 CSubband& ll = m_subband[srcLevel][Orient::LL], & hl = m_subband[srcLevel][Orient::HL];
449 CSubband& lh = m_subband[srcLevel][Orient::LH], & hh = m_subband[srcLevel][Orient::HH];
450
451 if (hiRow) {
452#ifdef __PGFROISUPPORT__
453 const bool storePos = wquot < ll.BufferWidth();
454 uint32_t llPos = 0, hlPos = 0, lhPos = 0, hhPos = 0;
455
456 if (storePos) {
457 // save current src buffer positions
458 llPos = ll.GetBuffPos();
459 hlPos = hl.GetBuffPos();
460 lhPos = lh.GetBuffPos();
461 hhPos = hh.GetBuffPos();
462 }
463#endif
464
465 for (uint32_t i = 0; i < wquot; i++) {
466 *loRow++ = ll.ReadBuffer();// first access, than increment
467 *loRow++ = hl.ReadBuffer();// first access, than increment
468 *hiRow++ = lh.ReadBuffer();// first access, than increment
469 *hiRow++ = hh.ReadBuffer();// first access, than increment
470 }
471
472 if (wrem) {
473 *loRow++ = ll.ReadBuffer();// first access, than increment
474 *hiRow++ = lh.ReadBuffer();// first access, than increment
475 }
476
477#ifdef __PGFROISUPPORT__
478 if (storePos) {
479 // increment src buffer positions
480 ll.IncBuffRow(llPos);
481 hl.IncBuffRow(hlPos);
482 lh.IncBuffRow(lhPos);
483 hh.IncBuffRow(hhPos);
484 }
485#endif
486
487 } else {
488#ifdef __PGFROISUPPORT__
489 const bool storePos = wquot < ll.BufferWidth();
490 uint32_t llPos = 0, hlPos = 0;
491
492 if (storePos) {
493 // save current src buffer positions
494 llPos = ll.GetBuffPos();
495 hlPos = hl.GetBuffPos();
496 }
497#endif
498
499 for (uint32_t i = 0; i < wquot; i++) {
500 *loRow++ = ll.ReadBuffer();// first access, than increment
501 *loRow++ = hl.ReadBuffer();// first access, than increment
502 }
503 if (wrem) *loRow++ = ll.ReadBuffer();
504
505#ifdef __PGFROISUPPORT__
506 if (storePos) {
507 // increment src buffer positions
508 ll.IncBuffRow(llPos);
509 hl.IncBuffRow(hlPos);
510 }
511#endif
512 }
513 }

◆ CSubband

friend class CSubband
friend

Definition at line 56 of file WaveletTransform.h.

Member Data Documentation

◆ m_nLevels

int pgf::CWaveletTransform::m_nLevels
private

number of LL levels: one more than header.nLevels in PGFimage

Definition at line 141 of file WaveletTransform.h.

◆ m_subband

CSubband(* pgf::CWaveletTransform::m_subband)[NSubbands]
private

quadtree of subbands: LL HL LH HH

Definition at line 142 of file WaveletTransform.h.


The documentation for this class was generated from the following files: