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 #ifdef LIBPGF_USE_OPENMP
92 m_macroBlockLen = omp_get_num_procs();
97 if (useOMP && m_macroBlockLen > 1) {
98 #ifdef LIBPGF_USE_OPENMP
99 omp_set_num_threads(m_macroBlockLen);
103 m_macroBlocks =
new(std::nothrow)
CMacroBlock*[m_macroBlockLen];
104 if (!m_macroBlocks) ReturnWithError(InsufficientMemory);
105 for (
int i=0; i < m_macroBlockLen; i++) m_macroBlocks[i] =
new CMacroBlock(
this);
106 m_currentBlock = m_macroBlocks[m_currentBlockIndex];
114 m_startPos = m_stream->GetPos();
118 m_stream->Read(&count, &preHeader);
119 if (count != expected) ReturnWithError(MissingData);
124 count = expected = 4;
126 count = expected = 2;
129 if (count != expected) ReturnWithError(MissingData);
132 preHeader.hSize =
__VAL(preHeader.hSize);
135 if (memcmp(preHeader.magic,
PGFMagic, 3) != 0) {
137 ReturnWithError(FormatCannotRead);
142 m_stream->Read(&count, &header);
143 if (count != expected) ReturnWithError(MissingData);
146 header.height =
__VAL(UINT32(header.height));
147 header.width =
__VAL(UINT32(header.width));
150 if (preHeader.version > 0) {
151 #ifndef __PGFROISUPPORT__
153 if (preHeader.version &
PGFROI) ReturnWithError(FormatCannotRead);
164 m_stream->Read(&count, postHeader.clut);
165 if (count != expected) ReturnWithError(MissingData);
170 userDataPos = m_stream->GetPos();
171 postHeader.userDataLen = size;
176 postHeader.userData =
new(std::nothrow) UINT8[postHeader.userDataLen];
177 if (!postHeader.userData) ReturnWithError(InsufficientMemory);
180 count = expected = postHeader.userDataLen;
181 m_stream->Read(&count, postHeader.userData);
182 if (count != expected) ReturnWithError(MissingData);
188 levelLength =
new(std::nothrow) UINT32[header.nLevels];
189 if (!levelLength) ReturnWithError(InsufficientMemory);
192 count = expected = header.nLevels*
WordBytes;
193 m_stream->Read(&count, levelLength);
194 if (count != expected) ReturnWithError(MissingData);
196 #ifdef PGF_USE_BIG_ENDIAN
198 for (
int i=0; i < header.nLevels; i++) {
199 levelLength[i] =
__VAL(levelLength[i]);
204 for (
int i=0; i < header.nLevels; i++) {
205 m_streamSizeEstimation += levelLength[i];
211 m_encodedHeaderLength = UINT32(m_stream->GetPos() - m_startPos);
235 m_stream->Read(&count, target);
257 const int wr = pitch - ww.rem;
258 int pos, base = startPos, base2;
261 for (
int i=0; i < hh.quot; i++) {
264 for (
int j=0; j < ww.quot; j++) {
268 DequantizeValue(band, pos, quantParam);
278 for (
int x=0; x < ww.rem; x++) {
279 DequantizeValue(band, pos, quantParam);
288 for (
int j=0; j < ww.quot; j++) {
291 for (
int y=0; y < hh.rem; y++) {
293 DequantizeValue(band, pos, quantParam);
302 for (
int y=0; y < hh.rem; y++) {
304 for (
int x=0; x < ww.rem; x++) {
305 DequantizeValue(band, pos, quantParam);
319 CSubband* hlBand = wtChannel->GetSubband(level,
HL);
320 CSubband* lhBand = wtChannel->GetSubband(level,
LH);
324 const int hlwr = hlBand->
GetWidth() - hlW.rem;
326 const int lhwr = lhBand->
GetWidth() - hlW.rem;
328 int hlBase = 0, lhBase = 0, hlBase2, lhBase2;
333 if (!hlBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
334 if (!lhBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
338 if (quantParam < 0) quantParam = 0;
341 for (
int i=0; i < lhH.quot; i++) {
345 for (
int j=0; j < hlW.quot; j++) {
350 DequantizeValue(hlBand, hlPos, quantParam);
351 DequantizeValue(lhBand, lhPos, quantParam);
365 for (
int x=0; x < hlW.rem; x++) {
366 DequantizeValue(hlBand, hlPos, quantParam);
367 DequantizeValue(lhBand, lhPos, quantParam);
373 DequantizeValue(lhBand, lhPos, quantParam);
384 for (
int j=0; j < hlW.quot; j++) {
388 for (
int y=0; y < lhH.rem; y++) {
390 DequantizeValue(hlBand, hlPos, quantParam);
391 DequantizeValue(lhBand, lhPos, quantParam);
404 for (
int y=0; y < lhH.rem; y++) {
406 for (
int x=0; x < hlW.rem; x++) {
407 DequantizeValue(hlBand, hlPos, quantParam);
408 DequantizeValue(lhBand, lhPos, quantParam);
414 DequantizeValue(lhBand, lhPos, quantParam);
424 for (
int j=0; j < hlBand->
GetWidth(); j++) {
425 DequantizeValue(hlBand, hlPos, quantParam);
435 m_stream->SetPos(FSFromCurrent, offset);
448 ASSERT(m_currentBlock);
450 if (m_currentBlock->IsCompletelyRead()) {
455 band->SetData(bandPos, m_currentBlock->m_value[m_currentBlock->m_valuePos] << quantParam);
456 m_currentBlock->m_valuePos++;
462 void CDecoder::DecodeTileBuffer() THROW_ {
496 if (ex.
error == MissingData) {
503 #ifdef LIBPGF_USE_OPENMP
505 #pragma omp parallel for default(shared) //no declared exceptions in next block
533 count = expected =
sizeof(UINT16);
534 m_stream->Read(&count, &wordLen);
535 if (count != expected) ReturnWithError(MissingData);
536 wordLen =
__VAL(wordLen);
538 ReturnWithError(FormatCannotRead);
540 #ifdef __PGFROISUPPORT__
543 m_stream->Read(&count, &h.
val);
544 if (count != expected) ReturnWithError(MissingData);
555 m_stream->Read(&count, block->m_codeBuffer);
556 if (count != expected) ReturnWithError(MissingData);
558 #ifdef PGF_USE_BIG_ENDIAN
561 for (
int i=0; i < count; i++) {
562 block->m_codeBuffer[i] =
__VAL(block->m_codeBuffer[i]);
566 #ifdef __PGFROISUPPORT__
578 void CDecoder::SkipTileBuffer() THROW_ {
592 count = expected =
sizeof(wordLen);
594 if (count != expected) ReturnWithError(MissingData);
595 wordLen =
__VAL(wordLen);
598 #ifdef __PGFROISUPPORT__
623 UINT32 codePos = 0, codeLen, sigLen, sigPos, signLen, signPos;
627 for (UINT32 k=0; k < bufferSize; k++) {
645 planeMask = 1 << (nPlanes - 1);
647 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
739 UINT32 valPos = 0, signPos = 0, refPos = 0;
740 UINT32 sigPos = 0, sigEnd;
743 while (valPos < bufferSize) {
746 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
752 while (sigPos < sigEnd) {
754 zerocnt =
SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
757 if (sigPos < sigEnd) {
759 SetBitAtPos(valPos, planeMask);
762 SetSign(valPos,
GetBit(signBits, signPos++));
765 m_sigFlagVector[valPos++] =
true;
770 if (valPos < bufferSize) {
772 if (
GetBit(refBits, refPos)) {
773 SetBitAtPos(valPos, planeMask);
779 ASSERT(sigPos <= bufferSize);
780 ASSERT(refPos <= bufferSize);
781 ASSERT(signPos <= bufferSize);
782 ASSERT(valPos == bufferSize);
800 UINT32 valPos = 0, refPos = 0;
801 UINT32 sigPos = 0, sigEnd;
803 UINT32 runlen = 1 << k;
804 UINT32 count = 0, rest = 0;
807 while (valPos < bufferSize) {
810 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
814 while (sigPos < sigEnd) {
822 if (
GetBit(m_codeBuffer, codePos++)) {
853 if (sigPos < sigEnd) {
858 SetBitAtPos(valPos, planeMask);
861 SetSign(valPos,
GetBit(m_codeBuffer, codePos++));
864 m_sigFlagVector[valPos++] =
true;
868 rest = sigPos - sigEnd;
876 if (valPos < bufferSize) {
878 if (
GetBit(refBits, refPos)) {
879 SetBitAtPos(valPos, planeMask);
885 ASSERT(sigPos <= bufferSize);
886 ASSERT(refPos <= bufferSize);
887 ASSERT(valPos == bufferSize);
904 UINT32 valPos = 0, refPos = 0;
905 UINT32 sigPos = 0, sigEnd;
906 UINT32 zerocnt, count = 0;
908 UINT32 runlen = 1 << k;
909 bool signBit =
false;
910 bool zeroAfterRun =
false;
912 while (valPos < bufferSize) {
915 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
921 while (sigPos < sigEnd) {
923 zerocnt =
SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
926 if (sigPos < sigEnd) {
928 SetBitAtPos(valPos, planeMask);
936 zeroAfterRun =
false;
939 if (
GetBit(m_codeBuffer, signPos++)) {
976 SetSign(valPos, signBit);
979 m_sigFlagVector[valPos++] =
true;
985 if (valPos < bufferSize) {
987 if (
GetBit(refBits, refPos)) {
988 SetBitAtPos(valPos, planeMask);
994 ASSERT(sigPos <= bufferSize);
995 ASSERT(refPos <= bufferSize);
996 ASSERT(valPos == bufferSize);
1003 void CDecoder::DumpBuffer() {