58 #define CodeBufferBitLen (BufferSize*WordWidth) // max number of bits in m_codeBuffer
59 #define MaxCodeLen ((1 << RLblockSizeLen) - 1) // max length of RL encoded block
68 , m_streamSizeEstimation(0)
69 , m_encodedHeaderLength(0)
70 , m_currentBlockIndex(0)
71 , m_macroBlocksAvailable(0)
72 #ifdef __PGFROISUPPORT__
81 #ifdef LIBPGF_USE_OPENMP
82 m_macroBlockLen = omp_get_num_procs();
87 if (useOMP && m_macroBlockLen > 1) {
88 #ifdef LIBPGF_USE_OPENMP
89 omp_set_num_threads(m_macroBlockLen);
94 for (
int i=0; i < m_macroBlockLen; i++) m_macroBlocks[i] =
new CMacroBlock(
this);
95 m_currentBlock = m_macroBlocks[m_currentBlockIndex];
103 m_startPos = m_stream->GetPos();
107 m_stream->Read(&count, &preHeader);
108 if (count != expected) ReturnWithError(MissingData);
113 count = expected = 4;
115 count = expected = 2;
118 if (count != expected) ReturnWithError(MissingData);
121 preHeader.hSize =
__VAL(preHeader.hSize);
124 if (memcmp(preHeader.magic,
Magic, 3) != 0) {
126 ReturnWithError(FormatCannotRead);
131 m_stream->Read(&count, &header);
132 if (count != expected) ReturnWithError(MissingData);
135 header.height =
__VAL(UINT32(header.height));
136 header.width =
__VAL(UINT32(header.width));
139 if (preHeader.version > 0) {
140 #ifndef __PGFROISUPPORT__
142 if (preHeader.version &
PGFROI) ReturnWithError(FormatCannotRead);
153 m_stream->Read(&count, postHeader.clut);
154 if (count != expected) ReturnWithError(MissingData);
160 postHeader.userDataLen = size;
161 postHeader.userData =
new(std::nothrow) UINT8[postHeader.userDataLen];
162 if (!postHeader.userData) ReturnWithError(InsufficientMemory);
165 count = expected = postHeader.userDataLen;
166 m_stream->Read(&count, postHeader.userData);
167 if (count != expected) ReturnWithError(MissingData);
172 levelLength =
new UINT32[header.nLevels];
175 count = expected = header.nLevels*
WordBytes;
176 m_stream->Read(&count, levelLength);
177 if (count != expected) ReturnWithError(MissingData);
179 #ifdef PGF_USE_BIG_ENDIAN
181 for (
int i=0; i < header.nLevels; i++) {
182 levelLength[i] =
__VAL(levelLength[i]);
187 for (
int i=0; i < header.nLevels; i++) {
188 m_streamSizeEstimation += levelLength[i];
194 m_encodedHeaderLength = UINT32(m_stream->GetPos() - m_startPos);
218 m_stream->Read(&count, target);
240 const int wr = pitch - ww.rem;
241 int pos, base = startPos, base2;
244 for (
int i=0; i < hh.quot; i++) {
247 for (
int j=0; j < ww.quot; j++) {
251 DequantizeValue(band, pos, quantParam);
261 for (
int x=0; x < ww.rem; x++) {
262 DequantizeValue(band, pos, quantParam);
271 for (
int j=0; j < ww.quot; j++) {
274 for (
int y=0; y < hh.rem; y++) {
276 DequantizeValue(band, pos, quantParam);
285 for (
int y=0; y < hh.rem; y++) {
287 for (
int x=0; x < ww.rem; x++) {
288 DequantizeValue(band, pos, quantParam);
302 CSubband* hlBand = wtChannel->GetSubband(level,
HL);
303 CSubband* lhBand = wtChannel->GetSubband(level,
LH);
307 const int hlwr = hlBand->
GetWidth() - hlW.rem;
309 const int lhwr = lhBand->
GetWidth() - hlW.rem;
311 int hlBase = 0, lhBase = 0, hlBase2, lhBase2;
316 if (!hlBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
317 if (!lhBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
321 if (quantParam < 0) quantParam = 0;
324 for (
int i=0; i < lhH.quot; i++) {
328 for (
int j=0; j < hlW.quot; j++) {
333 DequantizeValue(hlBand, hlPos, quantParam);
334 DequantizeValue(lhBand, lhPos, quantParam);
348 for (
int x=0; x < hlW.rem; x++) {
349 DequantizeValue(hlBand, hlPos, quantParam);
350 DequantizeValue(lhBand, lhPos, quantParam);
356 DequantizeValue(lhBand, lhPos, quantParam);
367 for (
int j=0; j < hlW.quot; j++) {
371 for (
int y=0; y < lhH.rem; y++) {
373 DequantizeValue(hlBand, hlPos, quantParam);
374 DequantizeValue(lhBand, lhPos, quantParam);
387 for (
int y=0; y < lhH.rem; y++) {
389 for (
int x=0; x < hlW.rem; x++) {
390 DequantizeValue(hlBand, hlPos, quantParam);
391 DequantizeValue(lhBand, lhPos, quantParam);
397 DequantizeValue(lhBand, lhPos, quantParam);
407 for (
int j=0; j < hlBand->
GetWidth(); j++) {
408 DequantizeValue(hlBand, hlPos, quantParam);
418 m_stream->SetPos(FSFromCurrent, offset);
431 ASSERT(m_currentBlock);
433 if (m_currentBlock->IsCompletelyRead()) {
438 band->SetData(bandPos, m_currentBlock->m_value[m_currentBlock->m_valuePos] << quantParam);
439 m_currentBlock->m_valuePos++;
445 void CDecoder::DecodeTileBuffer() THROW_ {
479 if (ex.
error == MissingData) {
488 #pragma omp parallel for default(shared) //no declared exceptions in next block
515 count = expected =
sizeof(UINT16);
516 m_stream->Read(&count, &wordLen);
517 if (count != expected) ReturnWithError(MissingData);
518 wordLen =
__VAL(wordLen);
520 ReturnWithError(FormatCannotRead);
522 #ifdef __PGFROISUPPORT__
525 m_stream->Read(&count, &h.
val);
526 if (count != expected) ReturnWithError(MissingData);
537 m_stream->Read(&count, block->m_codeBuffer);
538 if (count != expected) ReturnWithError(MissingData);
540 #ifdef PGF_USE_BIG_ENDIAN
543 for (
int i=0; i < count; i++) {
544 block->m_codeBuffer[i] =
__VAL(block->m_codeBuffer[i]);
548 #ifdef __PGFROISUPPORT__
560 void CDecoder::SkipTileBuffer() THROW_ {
574 count = expected =
sizeof(wordLen);
576 if (count != expected) ReturnWithError(MissingData);
577 wordLen =
__VAL(wordLen);
580 #ifdef __PGFROISUPPORT__
605 UINT32 codePos = 0, codeLen, sigLen, sigPos, signLen, signPos;
609 for (UINT32 k=0; k < bufferSize; k++) {
627 planeMask = 1 << (nPlanes - 1);
629 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
721 UINT32 valPos = 0, signPos = 0, refPos = 0;
722 UINT32 sigPos = 0, sigEnd;
725 while (valPos < bufferSize) {
728 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
734 while (sigPos < sigEnd) {
736 zerocnt =
SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
739 if (sigPos < sigEnd) {
741 SetBitAtPos(valPos, planeMask);
744 SetSign(valPos,
GetBit(signBits, signPos++));
747 m_sigFlagVector[valPos++] =
true;
752 if (valPos < bufferSize) {
754 if (
GetBit(refBits, refPos)) {
755 SetBitAtPos(valPos, planeMask);
761 ASSERT(sigPos <= bufferSize);
762 ASSERT(refPos <= bufferSize);
763 ASSERT(signPos <= bufferSize);
764 ASSERT(valPos == bufferSize);
782 UINT32 valPos = 0, refPos = 0;
783 UINT32 sigPos = 0, sigEnd;
785 UINT32 runlen = 1 << k;
786 UINT32 count = 0, rest = 0;
789 while (valPos < bufferSize) {
792 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
796 while (sigPos < sigEnd) {
804 if (
GetBit(m_codeBuffer, codePos++)) {
835 if (sigPos < sigEnd) {
840 SetBitAtPos(valPos, planeMask);
843 SetSign(valPos,
GetBit(m_codeBuffer, codePos++));
846 m_sigFlagVector[valPos++] =
true;
850 rest = sigPos - sigEnd;
858 if (valPos < bufferSize) {
860 if (
GetBit(refBits, refPos)) {
861 SetBitAtPos(valPos, planeMask);
867 ASSERT(sigPos <= bufferSize);
868 ASSERT(refPos <= bufferSize);
869 ASSERT(valPos == bufferSize);
886 UINT32 valPos = 0, refPos = 0;
887 UINT32 sigPos = 0, sigEnd;
888 UINT32 zerocnt, count = 0;
890 UINT32 runlen = 1 << k;
891 bool signBit =
false;
892 bool zeroAfterRun =
false;
894 while (valPos < bufferSize) {
897 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
903 while (sigPos < sigEnd) {
905 zerocnt =
SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
908 if (sigPos < sigEnd) {
910 SetBitAtPos(valPos, planeMask);
918 zeroAfterRun =
false;
921 if (
GetBit(m_codeBuffer, signPos++)) {
958 SetSign(valPos, signBit);
961 m_sigFlagVector[valPos++] =
true;
967 if (valPos < bufferSize) {
969 if (
GetBit(refBits, refPos)) {
970 SetBitAtPos(valPos, planeMask);
976 ASSERT(sigPos <= bufferSize);
977 ASSERT(refPos <= bufferSize);
978 ASSERT(valPos == bufferSize);
985 void CDecoder::DumpBuffer() {