libpgf 7.26.45
PGF - Progressive Graphics File
Loading...
Searching...
No Matches
Decoder.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 "Decoder.h"
30#ifdef TRACE
31 #include <stdio.h>
32#endif
33
35// PGF: file structure
36//
37// PGFPreHeader PGFHeader [PGFPostHeader] LevelLengths Level_n-1 Level_n-2 ... Level_0
38// PGFPostHeader ::= [ColorTable] [UserData]
39// LevelLengths ::= uint32_t[nLevels]
40
42// Decoding scheme
43// input: binary file
44// output: wavelet coefficients stored in subbands
45//
46// file (for each buffer: packedLength (16 bit), packed bits)
47// |
48// m_codeBuffer (for each plane: RLcodeLength (16 bit), RLcoded sigBits + m_sign, refBits)
49// | | |
50// m_sign sigBits refBits [BufferLen, BufferLen, BufferLen]
51// | | |
52// m_value [BufferSize]
53// |
54// subband
55//
56
57namespace pgf {
58 // Constants
60 constexpr uint32_t MaxCodeLen = (1 << RLblockSizeLen) - 1;
61
75 PGFPostHeader& postHeader, uint32_t*& levelLength, uint64_t& userDataPos,
76 bool useOMP, uint32_t userDataPolicy)
77 : m_stream(stream)
78 , m_startPos(0)
84 , m_roi(false)
85#endif
86 {
87 ASSERT(m_stream);
88
89 int count, expected;
90
91 // store current stream position
93
94 // read magic and version
95 count = expected = MagicVersionSize;
96 m_stream->Read(&count, &preHeader);
97 if (count != expected) ReturnWithError(MissingData);
98
99 // read header size
100 if (preHeader.version & Version6) {
101 // 32 bit header size since version 6
102 count = expected = 4;
103 } else {
104 count = expected = 2;
105 }
106 m_stream->Read(&count, ((uint8_t*)&preHeader) + MagicVersionSize);
107 if (count != expected) ReturnWithError(MissingData);
108
109 // make sure the values are correct read
110 preHeader.hSize = __VAL(preHeader.hSize);
111
112 // check magic number
113 if (memcmp(preHeader.magic, PGFMagic, 3) != 0) {
114 // error condition: wrong Magic number
115 ReturnWithError(FormatCannotRead);
116 }
117
118 // read file header
119 count = expected = (preHeader.hSize < HeaderSize) ? preHeader.hSize : HeaderSize;
120 m_stream->Read(&count, &header);
121 if (count != expected) ReturnWithError(MissingData);
122
123 // make sure the values are correct read
124 header.height = __VAL(uint32_t(header.height));
125 header.width = __VAL(uint32_t(header.width));
126
127 // be ready to read all versions including version 0
128 if (preHeader.version > 0) {
129#ifndef __PGFROISUPPORT__
130 // check ROI usage
131 if (preHeader.version & PGFROI) ReturnWithError(FormatCannotRead);
132#endif
133
134 uint32_t size = preHeader.hSize;
135
136 if (size > HeaderSize) {
137 size -= HeaderSize;
138 count = 0;
139
140 // read post-header
141 if (header.mode == ImageModeIndexedColor) {
142 if (size < ColorTableSize) ReturnWithError(FormatCannotRead);
143 // read color table
144 count = expected = ColorTableSize;
145 m_stream->Read(&count, postHeader.clut);
146 if (count != expected) ReturnWithError(MissingData);
147 }
148
149 if (size > (uint32_t)count) {
150 size -= count;
151
152 // read/skip user data
153 UP::Policy policy = (userDataPolicy <= MaxUserDataSize) ? UP::CachePrefix : 0xFFFFFFFF - userDataPolicy;
154 userDataPos = m_stream->GetPos();
155 postHeader.userDataLen = size;
156
157 if (policy == UP::Skip) {
158 postHeader.cachedUserDataLen = 0;
159 postHeader.userData = nullptr;
160 Skip(size);
161 } else {
162 postHeader.cachedUserDataLen = (policy == UP::CachePrefix) ? __min(size, userDataPolicy) : size;
163
164 // create user data memory block
165 postHeader.userData = new(std::nothrow) uint8_t[postHeader.cachedUserDataLen];
166 if (!postHeader.userData) ReturnWithError(InsufficientMemory);
167
168 // read user data
169 count = expected = postHeader.cachedUserDataLen;
170 m_stream->Read(&count, postHeader.userData);
171 if (count != expected) ReturnWithError(MissingData);
172
173 // skip remaining user data
174 if (postHeader.cachedUserDataLen < size) Skip(size - postHeader.cachedUserDataLen);
175 }
176 }
177 }
178
179 // create levelLength
180 levelLength = new(std::nothrow) uint32_t[header.nLevels];
181 if (!levelLength) ReturnWithError(InsufficientMemory);
182
183 // read levelLength
184 count = expected = header.nLevels*WordBytes;
185 m_stream->Read(&count, levelLength);
186 if (count != expected) ReturnWithError(MissingData);
187
188#ifdef PGF_USE_BIG_ENDIAN
189 // make sure the values are correct read
190 for (int i = 0; i < header.nLevels; i++) {
191 levelLength[i] = __VAL(levelLength[i]);
192 }
193#endif
194
195 // compute the total size in bytes; keep attention: level length information is optional
196 for (int i = 0; i < header.nLevels; i++) {
197 m_streamSizeEstimation += levelLength[i];
198 }
199
200 }
201
202 // store current stream position
204
205 // set number of threads
206#ifdef LIBPGF_USE_OPENMP
207 m_macroBlockLen = omp_get_num_procs();
208#else
209 m_macroBlockLen = 1;
210#endif
211
212 if (useOMP && m_macroBlockLen > 1) {
213#ifdef LIBPGF_USE_OPENMP
214 omp_set_num_threads(m_macroBlockLen);
215#endif
216
217 // create macro block array
218 m_macroBlocks = new(std::nothrow) CMacroBlock*[m_macroBlockLen];
219 if (!m_macroBlocks) ReturnWithError(InsufficientMemory);
220 for (int i = 0; i < m_macroBlockLen; i++) m_macroBlocks[i] = new CMacroBlock();
222 } else {
223 m_macroBlocks = 0;
224 m_macroBlockLen = 1; // there is only one macro block
225 m_currentBlock = new(std::nothrow) CMacroBlock();
226 if (!m_currentBlock) ReturnWithError(InsufficientMemory);
227 }
228 }
229
231 // Destructor
233 if (m_macroBlocks) {
234 for (int i = 0; i < m_macroBlockLen; i++) delete m_macroBlocks[i];
235 delete[] m_macroBlocks;
236 } else {
237 delete m_currentBlock;
238 }
239 }
240
247 uint32_t CDecoder::ReadEncodedData(uint8_t* target, uint32_t len) const {
248 ASSERT(m_stream);
249
250 int count = len;
251 m_stream->Read(&count, target);
252
253 return count;
254 }
255
267 void CDecoder::Partition(CSubband* band, int quantParam, int width, int height, int startPos, int pitch) {
268 ASSERT(band);
269
270 const div_t ww = div(width, LinBlockSize);
271 const div_t hh = div(height, LinBlockSize);
272 const int ws = pitch - LinBlockSize;
273 const int wr = pitch - ww.rem;
274 int pos, base = startPos, base2;
275
276 // main height
277 for (int i = 0; i < hh.quot; i++) {
278 // main width
279 base2 = base;
280 for (int j = 0; j < ww.quot; j++) {
281 pos = base2;
282 for (int y = 0; y < LinBlockSize; y++) {
283 for (int x = 0; x < LinBlockSize; x++) {
284 DequantizeValue(band, pos, quantParam);
285 pos++;
286 }
287 pos += ws;
288 }
289 base2 += LinBlockSize;
290 }
291 // rest of width
292 pos = base2;
293 for (int y = 0; y < LinBlockSize; y++) {
294 for (int x = 0; x < ww.rem; x++) {
295 DequantizeValue(band, pos, quantParam);
296 pos++;
297 }
298 pos += wr;
299 base += pitch;
300 }
301 }
302 // main width
303 base2 = base;
304 for (int j = 0; j < ww.quot; j++) {
305 // rest of height
306 pos = base2;
307 for (int y = 0; y < hh.rem; y++) {
308 for (int x = 0; x < LinBlockSize; x++) {
309 DequantizeValue(band, pos, quantParam);
310 pos++;
311 }
312 pos += ws;
313 }
314 base2 += LinBlockSize;
315 }
316 // rest of height
317 pos = base2;
318 for (int y = 0; y < hh.rem; y++) {
319 // rest of width
320 for (int x = 0; x < ww.rem; x++) {
321 DequantizeValue(band, pos, quantParam);
322 pos++;
323 }
324 pos += wr;
325 }
326 }
327
329 // Decodes and dequantizes HL, and LH band of one level
330 // LH and HH are interleaved in the codestream and must be split
331 // Deccoding and dequantization of HL and LH Band (interleaved) using partitioning scheme
332 // partitions the plane in squares of side length InterBlockSize
333 // It might throw an IOException.
334 void CDecoder::DecodeInterleaved(CWaveletTransform* wtChannel, int level, int quantParam) {
335 CSubband* hlBand = wtChannel->GetSubband(level, Orient::HL);
336 CSubband* lhBand = wtChannel->GetSubband(level, Orient::LH);
337 const div_t lhH = div(lhBand->GetHeight(), InterBlockSize);
338 const div_t hlW = div(hlBand->GetWidth(), InterBlockSize);
339 const int hlws = hlBand->GetWidth() - InterBlockSize;
340 const int hlwr = hlBand->GetWidth() - hlW.rem;
341 const int lhws = lhBand->GetWidth() - InterBlockSize;
342 const int lhwr = lhBand->GetWidth() - hlW.rem;
343 int hlPos, lhPos;
344 int hlBase = 0, lhBase = 0, hlBase2, lhBase2;
345
346 ASSERT(lhBand->GetWidth() >= hlBand->GetWidth());
347 ASSERT(hlBand->GetHeight() >= lhBand->GetHeight());
348
349 if (!hlBand->AllocMemory()) ReturnWithError(InsufficientMemory);
350 if (!lhBand->AllocMemory()) ReturnWithError(InsufficientMemory);
351
352 // correct quantParam with normalization factor
353 quantParam -= level;
354 if (quantParam < 0) quantParam = 0;
355
356 // main height
357 for (int i = 0; i < lhH.quot; i++) {
358 // main width
359 hlBase2 = hlBase;
360 lhBase2 = lhBase;
361 for (int j = 0; j < hlW.quot; j++) {
362 hlPos = hlBase2;
363 lhPos = lhBase2;
364 for (int y = 0; y < InterBlockSize; y++) {
365 for (int x = 0; x < InterBlockSize; x++) {
366 DequantizeValue(hlBand, hlPos, quantParam);
367 DequantizeValue(lhBand, lhPos, quantParam);
368 hlPos++;
369 lhPos++;
370 }
371 hlPos += hlws;
372 lhPos += lhws;
373 }
374 hlBase2 += InterBlockSize;
375 lhBase2 += InterBlockSize;
376 }
377 // rest of width
378 hlPos = hlBase2;
379 lhPos = lhBase2;
380 for (int y = 0; y < InterBlockSize; y++) {
381 for (int x = 0; x < hlW.rem; x++) {
382 DequantizeValue(hlBand, hlPos, quantParam);
383 DequantizeValue(lhBand, lhPos, quantParam);
384 hlPos++;
385 lhPos++;
386 }
387 // width difference between HL and LH
388 if (lhBand->GetWidth() > hlBand->GetWidth()) {
389 DequantizeValue(lhBand, lhPos, quantParam);
390 }
391 hlPos += hlwr;
392 lhPos += lhwr;
393 hlBase += hlBand->GetWidth();
394 lhBase += lhBand->GetWidth();
395 }
396 }
397 // main width
398 hlBase2 = hlBase;
399 lhBase2 = lhBase;
400 for (int j = 0; j < hlW.quot; j++) {
401 // rest of height
402 hlPos = hlBase2;
403 lhPos = lhBase2;
404 for (int y = 0; y < lhH.rem; y++) {
405 for (int x = 0; x < InterBlockSize; x++) {
406 DequantizeValue(hlBand, hlPos, quantParam);
407 DequantizeValue(lhBand, lhPos, quantParam);
408 hlPos++;
409 lhPos++;
410 }
411 hlPos += hlws;
412 lhPos += lhws;
413 }
414 hlBase2 += InterBlockSize;
415 lhBase2 += InterBlockSize;
416 }
417 // rest of height
418 hlPos = hlBase2;
419 lhPos = lhBase2;
420 for (int y = 0; y < lhH.rem; y++) {
421 // rest of width
422 for (int x = 0; x < hlW.rem; x++) {
423 DequantizeValue(hlBand, hlPos, quantParam);
424 DequantizeValue(lhBand, lhPos, quantParam);
425 hlPos++;
426 lhPos++;
427 }
428 // width difference between HL and LH
429 if (lhBand->GetWidth() > hlBand->GetWidth()) {
430 DequantizeValue(lhBand, lhPos, quantParam);
431 }
432 hlPos += hlwr;
433 lhPos += lhwr;
434 hlBase += hlBand->GetWidth();
435 }
436 // height difference between HL and LH
437 if (hlBand->GetHeight() > lhBand->GetHeight()) {
438 // total width
439 hlPos = hlBase;
440 for (int j = 0; j < hlBand->GetWidth(); j++) {
441 DequantizeValue(hlBand, hlPos, quantParam);
442 hlPos++;
443 }
444 }
445 }
446
450 void CDecoder::Skip(uint64_t offset) {
451 m_stream->SetPos(FSFromCurrent, offset);
452 }
453
463 void CDecoder::DequantizeValue(CSubband* band, uint32_t bandPos, int quantParam) {
464 ASSERT(m_currentBlock);
465
467 // all data of current macro block has been read --> prepare next macro block
469 }
470
471 band->SetData(bandPos, m_currentBlock->m_value[m_currentBlock->m_valuePos] << quantParam);
473 }
474
476 // Gets next macro block
477 // It might throw an IOException.
479 // current block has been read --> prepare next current block
481
482 if (m_macroBlocksAvailable > 0) {
484 } else {
485 DecodeBuffer();
486 }
487 ASSERT(m_currentBlock);
488 }
489
491 // Reads next block(s) from stream and decodes them
492 // Decoding scheme: <wordLen>(16 bits) [ ROI ] data
493 // ROI ::= <bufferSize>(15 bits) <eofTile>(1 bit)
494 // It might throw an IOException.
496 ASSERT(m_macroBlocksAvailable <= 0);
497
498 // macro block management
499 if (m_macroBlockLen == 1) {
500 ASSERT(m_currentBlock);
504 } else {
506 for (int i = 0; i < m_macroBlockLen; i++) {
507 // read sequentially several blocks
508 try {
511 } catch (IOException& ex) {
512 if (ex.error == MissingData || ex.error == FormatCannotRead) {
513 break; // no further data available or the data isn't valid PGF data (might occur in streaming or PPPExt)
514 } else {
515 throw;
516 }
517 }
518 }
519#ifdef LIBPGF_USE_OPENMP
520 // decode in parallel
521#pragma omp parallel for default(shared) //no declared exceptions in next block
522#endif
523 for (int i = 0; i < m_macroBlocksAvailable; i++) {
525 }
526
527 // prepare current macro block
530 }
531 }
532
534 // Reads next block from stream and stores it in the given macro block
535 // It might throw an IOException.
537 ASSERT(block);
538
539 uint16_t wordLen;
541 int count, expected;
542
543#ifdef TRACE
544 //uint32_t filePos = (uint32_t)m_stream->GetPos();
545 //printf("DecodeBuffer: %d\n", filePos);
546#endif
547
548 // read wordLen
549 count = expected = sizeof(uint16_t);
550 m_stream->Read(&count, &wordLen);
551 if (count != expected) ReturnWithError(MissingData);
552 wordLen = __VAL(wordLen); // convert wordLen
553 if (wordLen > BufferSize) ReturnWithError(FormatCannotRead);
554
555#ifdef __PGFROISUPPORT__
556 // read ROIBlockHeader
557 if (m_roi) {
558 count = expected = sizeof(ROIBlockHeader);
559 m_stream->Read(&count, &h.val);
560 if (count != expected) ReturnWithError(MissingData);
561 h.val = __VAL(h.val); // convert ROIBlockHeader
562 }
563#endif
564 // save header
565 block->m_header = h;
566
567 // read data
568 count = expected = wordLen*WordBytes;
569 m_stream->Read(&count, block->m_codeBuffer);
570 if (count != expected) ReturnWithError(MissingData);
571
572#ifdef PGF_USE_BIG_ENDIAN
573 // convert data
574 count /= WordBytes;
575 for (int i = 0; i < count; i++) {
576 block->m_codeBuffer[i] = __VAL(block->m_codeBuffer[i]);
577 }
578#endif
579
580#ifdef __PGFROISUPPORT__
581 ASSERT(m_roi && h.rbh.bufferSize <= BufferSize || h.rbh.bufferSize == BufferSize);
582#else
583 ASSERT(h.rbh.bufferSize == BufferSize);
584#endif
585 }
586
587#ifdef __PGFROISUPPORT__
589 // Resets stream position to next tile.
590 // Used with ROI encoding scheme only.
591 // Reads several next blocks from stream but doesn't decode them into macro blocks
592 // Encoding scheme: <wordLen>(16 bits) ROI data
593 // ROI ::= <bufferSize>(15 bits) <eofTile>(1 bit)
594 // It might throw an IOException.
595 void CDecoder::SkipTileBuffer() {
596 ASSERT(m_roi);
597
598 // current macro block belongs to the last tile, so go to the next macro block
601
602 // check if pre-decoded data is available
603 while (m_macroBlocksAvailable > 0 && !m_macroBlocks[m_currentBlockIndex]->m_header.rbh.tileEnd) {
606 }
607 if (m_macroBlocksAvailable > 0) {
608 // set new current macro block
611 return;
612 }
613
614 ASSERT(m_macroBlocksAvailable <= 0);
616 uint16_t wordLen;
617 ROIBlockHeader h(0);
618 int count, expected;
619
620 // skips all blocks until tile end
621 do {
622 // read wordLen
623 count = expected = sizeof(wordLen);
624 m_stream->Read(&count, &wordLen);
625 if (count != expected) ReturnWithError(MissingData);
626 wordLen = __VAL(wordLen); // convert wordLen
627 if (wordLen > BufferSize) ReturnWithError(FormatCannotRead);
628
629 // read ROIBlockHeader
630 count = expected = sizeof(ROIBlockHeader);
631 m_stream->Read(&count, &h.val);
632 if (count != expected) ReturnWithError(MissingData);
633 h.val = __VAL(h.val); // convert ROIBlockHeader
634
635 // skip data
636 m_stream->SetPos(FSFromCurrent, wordLen*WordBytes);
637 } while (!h.rbh.tileEnd);
638 }
639#endif
640
642 // Decodes macro block into buffer of given size using bit plane coding.
643 // A buffer contains bufferLen uint32_t values, thus, bufferSize bits per bit plane.
644 // Following coding scheme is used:
645 // Buffer ::= <nPlanes>(5 bits) foreach(plane i): Plane[i]
646 // Plane[i] ::= [ Sig1 | Sig2 ] [DWORD alignment] refBits
647 // Sig1 ::= 1 <codeLen>(15 bits) codedSigAndSignBits
648 // Sig2 ::= 0 <sigLen>(15 bits) [Sign1 | Sign2 ] [DWORD alignment] sigBits
649 // Sign1 ::= 1 <codeLen>(15 bits) codedSignBits
650 // Sign2 ::= 0 <signLen>(15 bits) [DWORD alignment] signBits
652 uint32_t bufferSize = m_header.rbh.bufferSize; ASSERT(bufferSize <= BufferSize);
653
654 // clear significance vector
655 for (uint32_t k = 0; k < bufferSize; k++) {
656 m_sigFlagVector[k] = false;
657 }
658 m_sigFlagVector[bufferSize] = true; // sentinel
659
660 // clear output buffer
661 for (uint32_t k = 0; k < BufferSize; k++) {
662 m_value[k] = 0;
663 }
664
665 // read number of bit planes
666 // <nPlanes>
667 uint32_t nPlanes = GetValueBlock(m_codeBuffer, 0, MaxBitPlanesLog);
668 uint32_t codePos = MaxBitPlanesLog;
669
670 // loop through all bit planes
671 if (nPlanes == 0) nPlanes = MaxBitPlanes + 1;
672 ASSERT(0 < nPlanes && nPlanes <= MaxBitPlanes + 1);
673 DataT planeMask = 1 << (nPlanes - 1);
674
675 for (int plane = nPlanes - 1; plane >= 0; plane--) {
676 uint32_t sigLen = 0;
677
678 // read RL code
679 if (GetBit(m_codeBuffer, codePos)) {
680 // RL coding of sigBits is used
681 // <1><codeLen><codedSigAndSignBits>_<refBits>
682 codePos++;
683
684 // read codeLen
685 uint32_t codeLen = GetValueBlock(m_codeBuffer, codePos, RLblockSizeLen); ASSERT(codeLen <= MaxCodeLen);
686
687 // position of encoded sigBits and signBits
688 uint32_t sigPos = codePos + RLblockSizeLen; ASSERT(sigPos < CodeBufferBitLen);
689
690 // refinement bits
691 codePos = AlignWordPos(sigPos + codeLen); ASSERT(codePos < CodeBufferBitLen);
692
693 // run-length decode significant bits and signs from m_codeBuffer and
694 // read refinement bits from m_codeBuffer and compose bit plane
695 sigLen = ComposeBitplaneRLD(bufferSize, planeMask, sigPos, &m_codeBuffer[codePos >> WordWidthLog]);
696
697 } else {
698 // no RL coding is used for sigBits and signBits together
699 // <0><sigLen>
700 codePos++;
701
702 // read sigLen
703 sigLen = GetValueBlock(m_codeBuffer, codePos, RLblockSizeLen); ASSERT(sigLen <= MaxCodeLen);
704 codePos += RLblockSizeLen; ASSERT(codePos < CodeBufferBitLen);
705
706 // read RL code for signBits
707 if (GetBit(m_codeBuffer, codePos)) {
708 // RL coding is used just for signBits
709 // <1><codeLen><codedSignBits>_<sigBits>_<refBits>
710 codePos++;
711
712 // read codeLen
713 uint32_t codeLen = GetValueBlock(m_codeBuffer, codePos, RLblockSizeLen); ASSERT(codeLen <= MaxCodeLen);
714
715 // sign bits
716 uint32_t signPos = codePos + RLblockSizeLen; ASSERT(signPos < CodeBufferBitLen);
717
718 // significant bits
719 uint32_t sigPos = AlignWordPos(signPos + codeLen); ASSERT(sigPos < CodeBufferBitLen);
720
721 // refinement bits
722 codePos = AlignWordPos(sigPos + sigLen); ASSERT(codePos < CodeBufferBitLen);
723
724 // read significant and refinement bitset from m_codeBuffer
725 sigLen = ComposeBitplaneRLD(bufferSize, planeMask, &m_codeBuffer[sigPos >> WordWidthLog], &m_codeBuffer[codePos >> WordWidthLog], signPos);
726
727 } else {
728 // RL coding of signBits was not efficient and therefore not used
729 // <0><signLen>_<signBits>_<sigBits>_<refBits>
730 codePos++;
731
732 // read signLen
733 uint32_t signLen = GetValueBlock(m_codeBuffer, codePos, RLblockSizeLen); ASSERT(signLen <= MaxCodeLen);
734
735 // sign bits
736 uint32_t signPos = AlignWordPos(codePos + RLblockSizeLen); ASSERT(signPos < CodeBufferBitLen);
737
738 // significant bits
739 uint32_t sigPos = AlignWordPos(signPos + signLen); ASSERT(sigPos < CodeBufferBitLen);
740
741 // refinement bits
742 codePos = AlignWordPos(sigPos + sigLen); ASSERT(codePos < CodeBufferBitLen);
743
744 // read significant and refinement bitset from m_codeBuffer
745 sigLen = ComposeBitplane(bufferSize, planeMask, &m_codeBuffer[sigPos >> WordWidthLog], &m_codeBuffer[codePos >> WordWidthLog], &m_codeBuffer[signPos >> WordWidthLog]);
746 }
747 }
748
749 // start of next chunk
750 codePos = AlignWordPos(codePos + bufferSize - sigLen); ASSERT(codePos < CodeBufferBitLen);
751
752 // next plane
753 planeMask >>= 1;
754 }
755
756 m_valuePos = 0;
757 }
758
760 // Reconstructs bitplane from significant bitset and refinement bitset
761 // returns length [bits] of sigBits
762 // input: sigBits, refBits, signBits
763 // output: m_value
764 uint32_t CDecoder::CMacroBlock::ComposeBitplane(uint32_t bufferSize, DataT planeMask, uint32_t* sigBits, uint32_t* refBits, uint32_t* signBits) {
765 ASSERT(sigBits);
766 ASSERT(refBits);
767 ASSERT(signBits);
768
769 uint32_t valPos = 0, signPos = 0, refPos = 0, sigPos = 0;
770
771 while (valPos < bufferSize) {
772 // search next 1 in m_sigFlagVector using searching with sentinel
773 uint32_t sigEnd = valPos;
774 while (!m_sigFlagVector[sigEnd]) { sigEnd++; }
775 sigEnd -= valPos;
776 sigEnd += sigPos;
777
778 // search 1's in sigBits[sigPos..sigEnd)
779 // these 1's are significant bits
780 while (sigPos < sigEnd) {
781 // search 0's
782 uint32_t zerocnt = SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
783 sigPos += zerocnt;
784 valPos += zerocnt;
785 if (sigPos < sigEnd) {
786 // write bit to m_value
787 SetBitAtPos(valPos, planeMask);
788
789 // copy sign bit
790 SetSign(valPos, GetBit(signBits, signPos++));
791
792 // update significance flag vector
793 m_sigFlagVector[valPos++] = true;
794 sigPos++;
795 }
796 }
797 // refinement bit
798 if (valPos < bufferSize) {
799 // write one refinement bit
800 if (GetBit(refBits, refPos)) {
801 SetBitAtPos(valPos, planeMask);
802 }
803 refPos++;
804 valPos++;
805 }
806 }
807 ASSERT(sigPos <= bufferSize);
808 ASSERT(refPos <= bufferSize);
809 ASSERT(signPos <= bufferSize);
810 ASSERT(valPos == bufferSize);
811
812 return sigPos;
813 }
814
816 // Reconstructs bitplane from significant bitset and refinement bitset
817 // returns length [bits] of decoded significant bits
818 // input: RL encoded sigBits and signBits in m_codeBuffer, refBits
819 // output: m_value
820 // RLE:
821 // - Decode run of 2^k zeros by a single 0.
822 // - Decode run of count 0's followed by a 1 with codeword: 1<count>x
823 // - x is 0: if a positive sign has been stored, otherwise 1
824 // - Read each bit from m_codeBuffer[codePos] and increment codePos.
825 uint32_t CDecoder::CMacroBlock::ComposeBitplaneRLD(uint32_t bufferSize, DataT planeMask, uint32_t codePos, uint32_t* refBits) {
826 ASSERT(refBits);
827
828 uint32_t valPos = 0, refPos = 0;
829 uint32_t sigPos = 0, sigEnd;
830 uint32_t k = 3;
831 uint32_t runlen = 1 << k; // = 2^k
832 uint32_t count = 0, rest = 0;
833 bool set1 = false;
834
835 while (valPos < bufferSize) {
836 // search next 1 in m_sigFlagVector using searching with sentinel
837 sigEnd = valPos;
838 while (!m_sigFlagVector[sigEnd]) { sigEnd++; }
839 sigEnd -= valPos;
840 sigEnd += sigPos;
841
842 while (sigPos < sigEnd) {
843 if (rest || set1) {
844 // rest of last run
845 sigPos += rest;
846 valPos += rest;
847 rest = 0;
848 } else {
849 // decode significant bits
850 if (GetBit(m_codeBuffer, codePos++)) {
851 // extract counter and generate zero run of length count
852 if (k > 0) {
853 // extract counter
854 count = GetValueBlock(m_codeBuffer, codePos, k);
855 codePos += k;
856 if (count > 0) {
857 sigPos += count;
858 valPos += count;
859 }
860
861 // adapt k (half run-length interval)
862 k--;
863 runlen >>= 1;
864 }
865
866 set1 = true;
867
868 } else {
869 // generate zero run of length 2^k
870 sigPos += runlen;
871 valPos += runlen;
872
873 // adapt k (double run-length interval)
874 if (k < WordWidth) {
875 k++;
876 runlen <<= 1;
877 }
878 }
879 }
880
881 if (sigPos < sigEnd) {
882 if (set1) {
883 set1 = false;
884
885 // write 1 bit
886 SetBitAtPos(valPos, planeMask);
887
888 // set sign bit
889 SetSign(valPos, GetBit(m_codeBuffer, codePos++));
890
891 // update significance flag vector
892 m_sigFlagVector[valPos++] = true;
893 sigPos++;
894 }
895 } else {
896 rest = sigPos - sigEnd;
897 sigPos = sigEnd;
898 valPos -= rest;
899 }
900
901 }
902
903 // refinement bit
904 if (valPos < bufferSize) {
905 // write one refinement bit
906 if (GetBit(refBits, refPos)) {
907 SetBitAtPos(valPos, planeMask);
908 }
909 refPos++;
910 valPos++;
911 }
912 }
913 ASSERT(sigPos <= bufferSize);
914 ASSERT(refPos <= bufferSize);
915 ASSERT(valPos == bufferSize);
916
917 return sigPos;
918 }
919
921 // Reconstructs bitplane from significant bitset, refinement bitset, and RL encoded sign bits
922 // returns length [bits] of sigBits
923 // input: sigBits, refBits, RL encoded signBits
924 // output: m_value
925 // RLE:
926 // decode run of 2^k 1's by a single 1
927 // decode run of count 1's followed by a 0 with codeword: 0<count>
928 uint32_t CDecoder::CMacroBlock::ComposeBitplaneRLD(uint32_t bufferSize, DataT planeMask, uint32_t* sigBits, uint32_t* refBits, uint32_t signPos) {
929 ASSERT(sigBits);
930 ASSERT(refBits);
931
932 uint32_t valPos = 0, refPos = 0;
933 uint32_t sigPos = 0, sigEnd;
934 uint32_t zerocnt, count = 0;
935 uint32_t k = 0;
936 uint32_t runlen = 1 << k; // = 2^k
937 bool signBit = false;
938 bool zeroAfterRun = false;
939
940 while (valPos < bufferSize) {
941 // search next 1 in m_sigFlagVector using searching with sentinel
942 sigEnd = valPos;
943 while (!m_sigFlagVector[sigEnd]) { sigEnd++; }
944 sigEnd -= valPos;
945 sigEnd += sigPos;
946
947 // search 1's in sigBits[sigPos..sigEnd)
948 // these 1's are significant bits
949 while (sigPos < sigEnd) {
950 // search 0's
951 zerocnt = SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
952 sigPos += zerocnt;
953 valPos += zerocnt;
954 if (sigPos < sigEnd) {
955 // write bit to m_value
956 SetBitAtPos(valPos, planeMask);
957
958 // check sign bit
959 if (count == 0) {
960 // all 1's have been set
961 if (zeroAfterRun) {
962 // finish the run with a 0
963 signBit = false;
964 zeroAfterRun = false;
965 } else {
966 // decode next sign bit
967 if (GetBit(m_codeBuffer, signPos++)) {
968 // generate 1's run of length 2^k
969 count = runlen - 1;
970 signBit = true;
971
972 // adapt k (double run-length interval)
973 if (k < WordWidth) {
974 k++;
975 runlen <<= 1;
976 }
977 } else {
978 // extract counter and generate 1's run of length count
979 if (k > 0) {
980 // extract counter
981 count = GetValueBlock(m_codeBuffer, signPos, k);
982 signPos += k;
983
984 // adapt k (half run-length interval)
985 k--;
986 runlen >>= 1;
987 }
988 if (count > 0) {
989 count--;
990 signBit = true;
991 zeroAfterRun = true;
992 } else {
993 signBit = false;
994 }
995 }
996 }
997 } else {
998 ASSERT(count > 0);
999 ASSERT(signBit);
1000 count--;
1001 }
1002
1003 // copy sign bit
1004 SetSign(valPos, signBit);
1005
1006 // update significance flag vector
1007 m_sigFlagVector[valPos++] = true;
1008 sigPos++;
1009 }
1010 }
1011
1012 // refinement bit
1013 if (valPos < bufferSize) {
1014 // write one refinement bit
1015 if (GetBit(refBits, refPos)) {
1016 SetBitAtPos(valPos, planeMask);
1017 }
1018 refPos++;
1019 valPos++;
1020 }
1021 }
1022 ASSERT(sigPos <= bufferSize);
1023 ASSERT(refPos <= bufferSize);
1024 ASSERT(valPos == bufferSize);
1025
1026 return sigPos;
1027 }
1028
1030#ifdef TRACE
1031 void CDecoder::DumpBuffer() {
1032 //printf("\nDump\n");
1033 //for (int i=0; i < BufferSize; i++) {
1034 // printf("%d", m_value[i]);
1035 //}
1036 }
1037#endif //TRACE
1038}
PGF decoder class.
constexpr uint32_t WordWidth
WordBytes*8.
Definition PGFplatform.h:73
constexpr uint32_t WordBytes
sizeof(uint32_t)
Definition PGFplatform.h:76
constexpr uint32_t WordWidthLog
ld of WordWidth
Definition PGFplatform.h:74
constexpr uint8_t ImageModeIndexedColor
#define __PGFROISUPPORT__
Definition PGFplatform.h:60
#define __VAL(x)
#define __min(x, y)
A macro block is a decoding unit of fixed size (uncoded).
Definition Decoder.h:46
DataT m_value[BufferSize]
output buffer of values with index m_valuePos
Definition Decoder.h:72
uint32_t ComposeBitplane(uint32_t bufferSize, DataT planeMask, uint32_t *sigBits, uint32_t *refBits, uint32_t *signBits)
Definition Decoder.cpp:764
bool m_sigFlagVector[BufferSize+1]
Definition Decoder.h:83
uint32_t m_valuePos
current position in m_value
Definition Decoder.h:74
ROIBlockHeader m_header
block header
Definition Decoder.h:71
void SetBitAtPos(uint32_t pos, DataT planeMask)
Definition Decoder.h:80
CMacroBlock()
Constructor: Initializes new macro block.
Definition Decoder.h:50
void SetSign(uint32_t pos, bool sign)
Definition Decoder.h:81
uint32_t m_codeBuffer[CodeBufferLen]
input buffer for encoded bitstream
Definition Decoder.h:73
uint32_t ComposeBitplaneRLD(uint32_t bufferSize, DataT planeMask, uint32_t sigPos, uint32_t *refBits)
Definition Decoder.cpp:825
bool IsCompletelyRead() const
Definition Decoder.h:63
int m_macroBlocksAvailable
number of decoded macro blocks (including currently used macro block)
Definition Decoder.h:203
CMacroBlock ** m_macroBlocks
array of macroblocks
Definition Decoder.h:200
void Skip(uint64_t offset)
Definition Decoder.cpp:450
void Partition(CSubband *band, int quantParam, int width, int height, int startPos, int pitch)
Definition Decoder.cpp:267
void DecodeBuffer()
Definition Decoder.cpp:495
int m_currentBlockIndex
index of current macro block
Definition Decoder.h:201
int m_macroBlockLen
array length
Definition Decoder.h:202
uint32_t ReadEncodedData(uint8_t *target, uint32_t len) const
Definition Decoder.cpp:247
CPGFStream * m_stream
input PGF stream
Definition Decoder.h:195
void ReadMacroBlock(CMacroBlock *block)
throws IOException
Definition Decoder.cpp:536
void DequantizeValue(CSubband *band, uint32_t bandPos, int quantParam)
Definition Decoder.cpp:463
uint64_t m_streamSizeEstimation
estimation of stream size
Definition Decoder.h:197
void DecodeInterleaved(CWaveletTransform *wtChannel, int level, int quantParam)
Definition Decoder.cpp:334
uint32_t m_encodedHeaderLength
stream offset from startPos to the beginning of the data part (highest level)
Definition Decoder.h:198
uint64_t m_startPos
stream position at the beginning of the PGF pre-header
Definition Decoder.h:196
void GetNextMacroBlock()
Definition Decoder.cpp:478
CMacroBlock * m_currentBlock
current macro block (used by main thread)
Definition Decoder.h:204
CDecoder(CPGFStream *stream, PGFPreHeader &preHeader, PGFHeader &header, PGFPostHeader &postHeader, uint32_t *&levelLength, uint64_t &userDataPos, bool useOMP, uint32_t userDataPolicy)
Definition Decoder.cpp:74
~CDecoder()
Destructor.
Definition Decoder.cpp:232
Abstract stream base class.
Definition PGFstream.h:39
virtual void Read(int *count, void *buffer)=0
virtual uint64_t GetPos() const =0
virtual void SetPos(short posMode, int64_t posOff)=0
Wavelet channel class.
Definition Subband.h:42
bool AllocMemory()
Definition Subband.cpp:78
void SetData(uint32_t pos, DataT v)
Definition Subband.h:102
int GetHeight() const
Definition Subband.h:123
int GetWidth() const
Definition Subband.h:128
PGF wavelet transform.
CSubband * GetSubband(int level, Orient::Orientation orientation)
constexpr Orientation LH
Definition PGFtypes.h:106
constexpr Orientation HL
Definition PGFtypes.h:105
constexpr Policy Skip
Definition PGFtypes.h:112
constexpr Policy CachePrefix
Definition PGFtypes.h:112
uint32_t Policy
Definition PGFtypes.h:110
Definition PGFimage.h:33
constexpr int MaxBitPlanes
maximum number of bit planes of m_value: 32 minus sign bit
Definition PGFtypes.h:91
constexpr size_t HeaderSize
Definition PGFtypes.h:379
constexpr uint32_t CodeBufferBitLen
max number of bits in m_codeBuffer
Definition Decoder.cpp:59
constexpr int LinBlockSize
side length of a coefficient block in a HH or LL subband
Definition PGFtypes.h:88
constexpr size_t MagicVersionSize
Definition PGFtypes.h:377
constexpr size_t MaxUserDataSize
Definition PGFtypes.h:382
constexpr const char * PGFMagic
PGF identification.
Definition PGFtypes.h:61
bool GetBit(uint32_t *stream, uint32_t pos)
Definition BitStream.h:79
uint32_t GetValueBlock(uint32_t *stream, uint32_t pos, uint32_t k)
Definition BitStream.h:142
constexpr int InterBlockSize
side length of a coefficient block in a HL or LH subband
Definition PGFtypes.h:89
uint32_t SeekBitRange(uint32_t *stream, uint32_t pos, uint32_t len)
Definition BitStream.h:220
uint32_t AlignWordPos(uint32_t pos)
Definition BitStream.h:328
int32_t DataT
Definition PGFtypes.h:367
constexpr int RLblockSizeLen
block size length (< 16): ld(BufferSize) < RLblockSizeLen <= 2*ld(BufferSize)
Definition PGFtypes.h:87
constexpr uint32_t CodeBufferLen
number of words in code buffer (CodeBufferLen > BufferLen)
Definition PGFtypes.h:86
constexpr int Version6
hSize in PGFPreHeader uses 32 bits instead of 16 bits
Definition PGFtypes.h:72
constexpr int MaxBitPlanesLog
number of bits to code the maximum number of bit planes (in 32 or 16 bit mode)
Definition PGFtypes.h:95
constexpr uint32_t MaxCodeLen
max length of RL encoded block
Definition Decoder.cpp:60
constexpr size_t ColorTableSize
Definition PGFtypes.h:380
constexpr uint32_t BufferSize
must be a multiple of WordWidth, BufferSize <= UINT16_MAX
Definition PGFtypes.h:84
constexpr int PGFROI
supports Regions Of Interest
Definition PGFtypes.h:70
PGF exception.
Definition PGFtypes.h:243
OSError error
operating system error code
Definition PGFtypes.h:244
PGF header.
Definition PGFtypes.h:169
uint8_t nLevels
number of FWT transforms
Definition PGFtypes.h:172
uint8_t mode
image mode according to Adobe's image modes
Definition PGFtypes.h:176
uint32_t width
image width in pixels
Definition PGFtypes.h:170
uint32_t height
image height in pixels
Definition PGFtypes.h:171
char magic[3]
PGF identification = "PGF".
Definition PGFtypes.h:127
uint8_t version
PGF version.
Definition PGFtypes.h:128
Optional PGF post-header.
Definition PGFtypes.h:199
uint32_t userDataLen
user data size in bytes (not part of file header)
Definition PGFtypes.h:202
uint32_t cachedUserDataLen
cached user data size in bytes (not part of file header)
Definition PGFtypes.h:203
RGBQUAD clut[ColorTableLen]
color table for indexed color images (optional part of file header)
Definition PGFtypes.h:200
uint8_t * userData
user data of size userDataLen (optional part of file header)
Definition PGFtypes.h:201
PGF pre-header.
Definition PGFtypes.h:136
uint32_t hSize
total size of PGFHeader, [ColorTable], and [UserData] in bytes (since Version 6: 4 Bytes)
Definition PGFtypes.h:137
uint16_t bufferSize
number of uncoded uint32_t values in a block
Definition PGFtypes.h:219
uint16_t tileEnd
1: last part of a tile
Definition PGFtypes.h:220
Block header used with ROI coding scheme.
Definition PGFtypes.h:211
struct pgf::ROIBlockHeader::RBH rbh
ROI block header.