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];
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);
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);
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++) {
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;
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++) {
365 for (
int x=0; x < hlW.rem; x++) {
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++) {
424 for (
int j=0; j < hlBand->
GetWidth(); j++) {
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);
534 if (count != expected) ReturnWithError(MissingData);
535 wordLen =
__VAL(wordLen);
537 ReturnWithError(FormatCannotRead);
539 #ifdef __PGFROISUPPORT__ 543 if (count != expected) ReturnWithError(MissingData);
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__ 619 UINT32 bufferSize = m_header.rbh.bufferSize; ASSERT(bufferSize <=
BufferSize);
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);
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);
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++)) {
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);
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++)) {
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() {
bool GetBit(UINT32 *stream, UINT32 pos)
virtual void Read(int *count, void *buffer)=0
UINT32 ComposeBitplane(UINT32 bufferSize, DataT planeMask, UINT32 *sigBits, UINT32 *refBits, UINT32 *signBits)
#define MaxCodeLen
max length of RL encoded block
UINT32 AlignWordPos(UINT32 pos)
CMacroBlock ** m_macroBlocks
array of macroblocks
Abstract stream base class.
void DecodeInterleaved(CWaveletTransform *wtChannel, int level, int quantParam) THROW_
#define LinBlockSize
side length of a coefficient block in a HH or LL subband
UINT32 ReadEncodedData(UINT8 *target, UINT32 len) const THROW_
UINT32 m_valuePos
current position in m_value
void ReadMacroBlock(CMacroBlock *block) THROW_
throws IOException
#define BufferSize
must be a multiple of WordWidth
void Partition(CSubband *band, int quantParam, int width, int height, int startPos, int pitch) THROW_
virtual void SetPos(short posMode, INT64 posOff)=0
#define RLblockSizeLen
block size length (< 16): ld(BufferSize) < RLblockSizeLen <= 2*ld(BufferSize)
int m_macroBlockLen
array length
DataT m_value[BufferSize]
output buffer of values with index m_valuePos
void DequantizeValue(CSubband *band, UINT32 bandPos, int quantParam) THROW_
#define Version6
new HeaderSize: 32 bits instead of 16 bits
int m_currentBlockIndex
index of current macro block
UINT32 SeekBitRange(UINT32 *stream, UINT32 pos, UINT32 len)
#define MaxBitPlanesLog
number of bits to code the maximum number of bit planes (in 32 or 16 bit mode)
CMacroBlock * m_currentBlock
current macro block (used by main thread)
bool IsCompletelyRead() const
A macro block is a decoding unit of fixed size (uncoded)
CPGFStream * m_stream
input PGF stream
int m_macroBlocksAvailable
number of decoded macro blocks (including currently used macro block)
#define PGFROI
supports Regions Of Interest
UINT32 GetValueBlock(UINT32 *stream, UINT32 pos, UINT32 k)
#define MaxBitPlanes
maximum number of bit planes of m_value: 32 minus sign bit
#define InterBlockSize
side length of a coefficient block in a HL or LH subband
OSError error
operating system error code
void DecodeBuffer() THROW_
UINT32 ComposeBitplaneRLD(UINT32 bufferSize, DataT planeMask, UINT32 sigPos, UINT32 *refBits)
CDecoder(CPGFStream *stream, PGFPreHeader &preHeader, PGFHeader &header, PGFPostHeader &postHeader, UINT32 *&levelLength, UINT64 &userDataPos, bool useOMP, bool skipUserData) THROW_
void Skip(UINT64 offset) THROW_
#define Magic
PGF identification.
#define CodeBufferBitLen
max number of bits in m_codeBuffer