58 #define CodeBufferBitLen (CodeBufferLen*WordWidth)
59 #define MaxCodeLen ((1 << RLblockSizeLen) - 1)
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];
110 m_currentBlock =
new CMacroBlock(
this);
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,
Magic, 3) != 0) {
137 ReturnWithError(FormatCannotRead);
142 m_stream->Read(&count, &
header);
143 if (count != expected) ReturnWithError(MissingData);
150 if (preHeader.version > 0) {
151 #ifndef __PGFROISUPPORT__
153 if (preHeader.version &
PGFROI) ReturnWithError(FormatCannotRead);
165 if (count != expected) ReturnWithError(MissingData);
170 userDataPos = m_stream->GetPos();
177 if (!
postHeader.userData) ReturnWithError(InsufficientMemory);
182 if (count != expected) ReturnWithError(MissingData);
188 levelLength =
new(std::nothrow) UINT32[
header.nLevels];
189 if (!levelLength) ReturnWithError(InsufficientMemory);
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;
261 for (
int i=0; i < hh.quot; i++) {
264 for (
int j=0; j < ww.quot; j++) {
278 for (
int x=0; x < ww.rem; x++) {
288 for (
int j=0; j < ww.quot; j++) {
291 for (
int y=0; y < hh.rem; y++) {
302 for (
int y=0; y < hh.rem; y++) {
304 for (
int x=0; x < ww.rem; x++) {
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;
330 ASSERT(lhBand->GetWidth() >= hlBand->GetWidth());
331 ASSERT(hlBand->GetHeight() >= lhBand->GetHeight());
333 if (!hlBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
334 if (!lhBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
341 for (
int i=0; i < lhH.quot; i++) {
345 for (
int j=0; j < hlW.quot; j++) {
365 for (
int x=0; x < hlW.rem; x++) {
372 if (lhBand->GetWidth() > hlBand->GetWidth()) {
377 hlBase += hlBand->GetWidth();
378 lhBase += lhBand->GetWidth();
384 for (
int j=0; j < hlW.quot; j++) {
388 for (
int y=0; y < lhH.rem; y++) {
404 for (
int y=0; y < lhH.rem; y++) {
406 for (
int x=0; x < hlW.rem; x++) {
413 if (lhBand->GetWidth() > hlBand->GetWidth()) {
418 hlBase += hlBand->GetWidth();
421 if (hlBand->GetHeight() > lhBand->GetHeight()) {
424 for (
int j=0; j < hlBand->GetWidth(); j++) {
435 m_stream->SetPos(FSFromCurrent,
offset);
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) {
505 #pragma omp parallel for default(shared) //no declared exceptions in next block
532 count = expected =
sizeof(UINT16);
533 m_stream->Read(&count, &wordLen);
534 if (count != expected) ReturnWithError(MissingData);
535 wordLen =
__VAL(wordLen);
537 ReturnWithError(FormatCannotRead);
539 #ifdef __PGFROISUPPORT__
542 m_stream->Read(&count, &h.
val);
543 if (count != expected) ReturnWithError(MissingData);
554 m_stream->Read(&count, block->m_codeBuffer);
555 if (count != expected) ReturnWithError(MissingData);
557 #ifdef PGF_USE_BIG_ENDIAN
560 for (
int i=0; i <
count; i++) {
561 block->m_codeBuffer[i] =
__VAL(block->m_codeBuffer[i]);
565 #ifdef __PGFROISUPPORT__
577 void CDecoder::SkipTileBuffer()
THROW_ {
591 count = expected =
sizeof(wordLen);
593 if (count != expected) ReturnWithError(MissingData);
594 wordLen =
__VAL(wordLen);
597 #ifdef __PGFROISUPPORT__
618 void CDecoder::CMacroBlock::BitplaneDecode() {
622 UINT32 codePos = 0, codeLen, sigLen, sigPos, signLen, signPos;
626 for (UINT32
k=0;
k < bufferSize;
k++) {
627 m_sigFlagVector[
k] =
false;
629 m_sigFlagVector[bufferSize] =
true;
644 planeMask = 1 << (nPlanes - 1);
646 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
648 if (
GetBit(m_codeBuffer, codePos)) {
664 sigLen = ComposeBitplaneRLD(bufferSize, planeMask, sigPos, &m_codeBuffer[codePos >>
WordWidthLog]);
676 if (
GetBit(m_codeBuffer, codePos)) {
694 sigLen = ComposeBitplaneRLD(bufferSize, planeMask, &m_codeBuffer[sigPos >>
WordWidthLog], &m_codeBuffer[codePos >>
WordWidthLog], signPos);
733 UINT32 CDecoder::CMacroBlock::ComposeBitplane(UINT32 bufferSize,
DataT planeMask, UINT32* sigBits, UINT32* refBits, UINT32* signBits) {
738 UINT32 valPos = 0, signPos = 0, refPos = 0;
739 UINT32 sigPos = 0, sigEnd;
742 while (valPos < bufferSize) {
745 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
751 while (sigPos < sigEnd) {
753 zerocnt = SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
756 if (sigPos < sigEnd) {
758 SetBitAtPos(valPos, planeMask);
761 SetSign(valPos,
GetBit(signBits, signPos++));
764 m_sigFlagVector[valPos++] =
true;
769 if (valPos < bufferSize) {
771 if (
GetBit(refBits, refPos)) {
772 SetBitAtPos(valPos, planeMask);
778 ASSERT(sigPos <= bufferSize);
779 ASSERT(refPos <= bufferSize);
780 ASSERT(signPos <= bufferSize);
781 ASSERT(valPos == bufferSize);
796 UINT32 CDecoder::CMacroBlock::ComposeBitplaneRLD(UINT32 bufferSize,
DataT planeMask, UINT32 codePos, UINT32* refBits) {
799 UINT32 valPos = 0, refPos = 0;
800 UINT32 sigPos = 0, sigEnd;
802 UINT32 runlen = 1 <<
k;
803 UINT32 count = 0, rest = 0;
806 while (valPos < bufferSize) {
809 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
813 while (sigPos < sigEnd) {
821 if (
GetBit(m_codeBuffer, codePos++)) {
825 count = GetValueBlock(m_codeBuffer, codePos, k);
852 if (sigPos < sigEnd) {
857 SetBitAtPos(valPos, planeMask);
860 SetSign(valPos,
GetBit(m_codeBuffer, codePos++));
863 m_sigFlagVector[valPos++] =
true;
867 rest = sigPos - sigEnd;
875 if (valPos < bufferSize) {
877 if (
GetBit(refBits, refPos)) {
878 SetBitAtPos(valPos, planeMask);
884 ASSERT(sigPos <= bufferSize);
885 ASSERT(refPos <= bufferSize);
886 ASSERT(valPos == bufferSize);
899 UINT32 CDecoder::CMacroBlock::ComposeBitplaneRLD(UINT32 bufferSize,
DataT planeMask, UINT32* sigBits, UINT32* refBits, UINT32 signPos) {
903 UINT32 valPos = 0, refPos = 0;
904 UINT32 sigPos = 0, sigEnd;
905 UINT32 zerocnt, count = 0;
907 UINT32 runlen = 1 <<
k;
908 bool signBit =
false;
909 bool zeroAfterRun =
false;
911 while (valPos < bufferSize) {
914 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
920 while (sigPos < sigEnd) {
922 zerocnt = SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
925 if (sigPos < sigEnd) {
927 SetBitAtPos(valPos, planeMask);
935 zeroAfterRun =
false;
938 if (
GetBit(m_codeBuffer, signPos++)) {
952 count = GetValueBlock(m_codeBuffer, signPos, k);
975 SetSign(valPos, signBit);
978 m_sigFlagVector[valPos++] =
true;
984 if (valPos < bufferSize) {
986 if (
GetBit(refBits, refPos)) {
987 SetBitAtPos(valPos, planeMask);
993 ASSERT(sigPos <= bufferSize);
994 ASSERT(refPos <= bufferSize);
995 ASSERT(valPos == bufferSize);
1002 void CDecoder::DumpBuffer() {