58 #define CodeBufferBitLen (CodeBufferLen*WordWidth)
59 #define MaxCodeLen ((1 << RLblockSizeLen) - 1)
74 PGFPostHeader& postHeader, UINT32*& levelLength, UINT64& userDataPos,
75 bool useOMP,
bool skipUserData) THROW_
78 , m_streamSizeEstimation(0)
79 , m_encodedHeaderLength(0)
80 , m_currentBlockIndex(0)
81 , m_macroBlocksAvailable(0)
82 #ifdef __PGFROISUPPORT__
91 m_startPos = m_stream->GetPos();
95 m_stream->Read(&count, &preHeader);
96 if (count != expected) ReturnWithError(MissingData);
101 count = expected = 4;
103 count = expected = 2;
106 if (count != expected) ReturnWithError(MissingData);
109 preHeader.hSize =
__VAL(preHeader.hSize);
112 if (memcmp(preHeader.magic,
PGFMagic, 3) != 0) {
114 ReturnWithError(FormatCannotRead);
119 m_stream->Read(&count, &header);
120 if (count != expected) ReturnWithError(MissingData);
123 header.height =
__VAL(UINT32(header.height));
124 header.width =
__VAL(UINT32(header.width));
127 if (preHeader.version > 0) {
128 #ifndef __PGFROISUPPORT__
130 if (preHeader.version &
PGFROI) ReturnWithError(FormatCannotRead);
141 m_stream->Read(&count, postHeader.clut);
142 if (count != expected) ReturnWithError(MissingData);
147 userDataPos = m_stream->GetPos();
148 postHeader.userDataLen = size;
153 postHeader.userData =
new(std::nothrow) UINT8[postHeader.userDataLen];
154 if (!postHeader.userData) ReturnWithError(InsufficientMemory);
157 count = expected = postHeader.userDataLen;
158 m_stream->Read(&count, postHeader.userData);
159 if (count != expected) ReturnWithError(MissingData);
165 levelLength =
new(std::nothrow) UINT32[header.nLevels];
166 if (!levelLength) ReturnWithError(InsufficientMemory);
169 count = expected = header.nLevels*
WordBytes;
170 m_stream->Read(&count, levelLength);
171 if (count != expected) ReturnWithError(MissingData);
173 #ifdef PGF_USE_BIG_ENDIAN
175 for (
int i=0; i < header.nLevels; i++) {
176 levelLength[i] =
__VAL(levelLength[i]);
181 for (
int i=0; i < header.nLevels; i++) {
182 m_streamSizeEstimation += levelLength[i];
188 m_encodedHeaderLength = UINT32(m_stream->GetPos() - m_startPos);
191 #ifdef LIBPGF_USE_OPENMP
192 m_macroBlockLen = omp_get_num_procs();
197 if (useOMP && m_macroBlockLen > 1) {
198 #ifdef LIBPGF_USE_OPENMP
199 omp_set_num_threads(m_macroBlockLen);
203 m_macroBlocks =
new(std::nothrow) CMacroBlock*[m_macroBlockLen];
204 if (!m_macroBlocks) ReturnWithError(InsufficientMemory);
205 for (
int i = 0; i < m_macroBlockLen; i++) m_macroBlocks[i] =
new CMacroBlock();
206 m_currentBlock = m_macroBlocks[m_currentBlockIndex];
210 m_currentBlock =
new(std::nothrow) CMacroBlock();
211 if (!m_currentBlock) ReturnWithError(InsufficientMemory);
236 m_stream->Read(&count, target);
258 const int wr = pitch - ww.rem;
259 int pos, base = startPos, base2;
262 for (
int i=0; i < hh.quot; i++) {
265 for (
int j=0; j < ww.quot; j++) {
269 DequantizeValue(band, pos, quantParam);
279 for (
int x=0; x < ww.rem; x++) {
280 DequantizeValue(band, pos, quantParam);
289 for (
int j=0; j < ww.quot; j++) {
292 for (
int y=0; y < hh.rem; y++) {
294 DequantizeValue(band, pos, quantParam);
303 for (
int y=0; y < hh.rem; y++) {
305 for (
int x=0; x < ww.rem; x++) {
306 DequantizeValue(band, pos, quantParam);
320 CSubband* hlBand = wtChannel->GetSubband(level,
HL);
321 CSubband* lhBand = wtChannel->GetSubband(level,
LH);
325 const int hlwr = hlBand->
GetWidth() - hlW.rem;
327 const int lhwr = lhBand->
GetWidth() - hlW.rem;
329 int hlBase = 0, lhBase = 0, hlBase2, lhBase2;
334 if (!hlBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
335 if (!lhBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
339 if (quantParam < 0) quantParam = 0;
342 for (
int i=0; i < lhH.quot; i++) {
346 for (
int j=0; j < hlW.quot; j++) {
351 DequantizeValue(hlBand, hlPos, quantParam);
352 DequantizeValue(lhBand, lhPos, quantParam);
366 for (
int x=0; x < hlW.rem; x++) {
367 DequantizeValue(hlBand, hlPos, quantParam);
368 DequantizeValue(lhBand, lhPos, quantParam);
374 DequantizeValue(lhBand, lhPos, quantParam);
385 for (
int j=0; j < hlW.quot; j++) {
389 for (
int y=0; y < lhH.rem; y++) {
391 DequantizeValue(hlBand, hlPos, quantParam);
392 DequantizeValue(lhBand, lhPos, quantParam);
405 for (
int y=0; y < lhH.rem; y++) {
407 for (
int x=0; x < hlW.rem; x++) {
408 DequantizeValue(hlBand, hlPos, quantParam);
409 DequantizeValue(lhBand, lhPos, quantParam);
415 DequantizeValue(lhBand, lhPos, quantParam);
425 for (
int j=0; j < hlBand->
GetWidth(); j++) {
426 DequantizeValue(hlBand, hlPos, quantParam);
436 m_stream->SetPos(FSFromCurrent, offset);
449 ASSERT(m_currentBlock);
451 if (m_currentBlock->IsCompletelyRead()) {
456 band->SetData(bandPos, m_currentBlock->m_value[m_currentBlock->m_valuePos] << quantParam);
457 m_currentBlock->m_valuePos++;
463 void CDecoder::DecodeTileBuffer() THROW_ {
497 if (ex.
error == MissingData) {
504 #ifdef LIBPGF_USE_OPENMP
506 #pragma omp parallel for default(shared) //no declared exceptions in next block
534 count = expected =
sizeof(UINT16);
535 m_stream->Read(&count, &wordLen);
536 if (count != expected) ReturnWithError(MissingData);
537 wordLen =
__VAL(wordLen);
539 ReturnWithError(FormatCannotRead);
541 #ifdef __PGFROISUPPORT__
544 m_stream->Read(&count, &h.
val);
545 if (count != expected) ReturnWithError(MissingData);
556 m_stream->Read(&count, block->m_codeBuffer);
557 if (count != expected) ReturnWithError(MissingData);
559 #ifdef PGF_USE_BIG_ENDIAN
562 for (
int i=0; i < count; i++) {
563 block->m_codeBuffer[i] =
__VAL(block->m_codeBuffer[i]);
567 #ifdef __PGFROISUPPORT__
579 void CDecoder::SkipTileBuffer() THROW_ {
593 count = expected =
sizeof(wordLen);
595 if (count != expected) ReturnWithError(MissingData);
596 wordLen =
__VAL(wordLen);
599 #ifdef __PGFROISUPPORT__
624 UINT32 codePos = 0, codeLen, sigLen, sigPos, signLen, signPos;
628 for (UINT32 k=0; k < bufferSize; k++) {
646 planeMask = 1 << (nPlanes - 1);
648 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
740 UINT32 valPos = 0, signPos = 0, refPos = 0;
741 UINT32 sigPos = 0, sigEnd;
744 while (valPos < bufferSize) {
747 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
753 while (sigPos < sigEnd) {
755 zerocnt =
SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
758 if (sigPos < sigEnd) {
760 SetBitAtPos(valPos, planeMask);
763 SetSign(valPos,
GetBit(signBits, signPos++));
766 m_sigFlagVector[valPos++] =
true;
771 if (valPos < bufferSize) {
773 if (
GetBit(refBits, refPos)) {
774 SetBitAtPos(valPos, planeMask);
780 ASSERT(sigPos <= bufferSize);
781 ASSERT(refPos <= bufferSize);
782 ASSERT(signPos <= bufferSize);
783 ASSERT(valPos == bufferSize);
801 UINT32 valPos = 0, refPos = 0;
802 UINT32 sigPos = 0, sigEnd;
804 UINT32 runlen = 1 << k;
805 UINT32 count = 0, rest = 0;
808 while (valPos < bufferSize) {
811 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
815 while (sigPos < sigEnd) {
823 if (
GetBit(m_codeBuffer, codePos++)) {
854 if (sigPos < sigEnd) {
859 SetBitAtPos(valPos, planeMask);
862 SetSign(valPos,
GetBit(m_codeBuffer, codePos++));
865 m_sigFlagVector[valPos++] =
true;
869 rest = sigPos - sigEnd;
877 if (valPos < bufferSize) {
879 if (
GetBit(refBits, refPos)) {
880 SetBitAtPos(valPos, planeMask);
886 ASSERT(sigPos <= bufferSize);
887 ASSERT(refPos <= bufferSize);
888 ASSERT(valPos == bufferSize);
905 UINT32 valPos = 0, refPos = 0;
906 UINT32 sigPos = 0, sigEnd;
907 UINT32 zerocnt, count = 0;
909 UINT32 runlen = 1 << k;
910 bool signBit =
false;
911 bool zeroAfterRun =
false;
913 while (valPos < bufferSize) {
916 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
922 while (sigPos < sigEnd) {
924 zerocnt =
SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
927 if (sigPos < sigEnd) {
929 SetBitAtPos(valPos, planeMask);
937 zeroAfterRun =
false;
940 if (
GetBit(m_codeBuffer, signPos++)) {
977 SetSign(valPos, signBit);
980 m_sigFlagVector[valPos++] =
true;
986 if (valPos < bufferSize) {
988 if (
GetBit(refBits, refPos)) {
989 SetBitAtPos(valPos, planeMask);
995 ASSERT(sigPos <= bufferSize);
996 ASSERT(refPos <= bufferSize);
997 ASSERT(valPos == bufferSize);
1004 void CDecoder::DumpBuffer() {