58 #define CodeBufferBitLen (CodeBufferLen*WordWidth) 59 #define MaxCodeLen ((1 << RLblockSizeLen) - 1) 74 PGFPostHeader& postHeader, UINT32*& levelLength, UINT64& userDataPos,
75 bool useOMP, UINT32 userDataPolicy)
78 , m_streamSizeEstimation(0)
79 , m_encodedHeaderLength(0)
80 , m_currentBlockIndex(0)
81 , m_macroBlocksAvailable(0)
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);
127 if (preHeader.version > 0) {
128 #ifndef __PGFROISUPPORT__ 130 if (preHeader.version &
PGFROI) ReturnWithError(FormatCannotRead);
133 UINT32 size = preHeader.hSize;
144 m_stream->Read(&count, postHeader.
clut);
145 if (count != expected) ReturnWithError(MissingData);
148 if (size > (UINT32)count) {
153 userDataPos = m_stream->GetPos();
165 if (!postHeader.
userData) ReturnWithError(InsufficientMemory);
169 m_stream->Read(&count, postHeader.
userData);
170 if (count != expected) ReturnWithError(MissingData);
179 levelLength =
new(std::nothrow) UINT32[header.
nLevels];
180 if (!levelLength) ReturnWithError(InsufficientMemory);
184 m_stream->Read(&count, levelLength);
185 if (count != expected) ReturnWithError(MissingData);
187 #ifdef PGF_USE_BIG_ENDIAN 189 for (
int i=0; i < header.
nLevels; i++) {
190 levelLength[i] =
__VAL(levelLength[i]);
195 for (
int i=0; i < header.
nLevels; i++) {
196 m_streamSizeEstimation += levelLength[i];
202 m_encodedHeaderLength = UINT32(m_stream->GetPos() - m_startPos);
205 #ifdef LIBPGF_USE_OPENMP 206 m_macroBlockLen = omp_get_num_procs();
211 if (useOMP && m_macroBlockLen > 1) {
212 #ifdef LIBPGF_USE_OPENMP 213 omp_set_num_threads(m_macroBlockLen);
217 m_macroBlocks =
new(std::nothrow) CMacroBlock*[m_macroBlockLen];
218 if (!m_macroBlocks) ReturnWithError(InsufficientMemory);
219 for (
int i = 0; i < m_macroBlockLen; i++) m_macroBlocks[i] =
new CMacroBlock();
220 m_currentBlock = m_macroBlocks[m_currentBlockIndex];
224 m_currentBlock =
new(std::nothrow) CMacroBlock();
225 if (!m_currentBlock) ReturnWithError(InsufficientMemory);
272 const int wr = pitch - ww.rem;
273 int pos, base = startPos, base2;
276 for (
int i=0; i < hh.quot; i++) {
279 for (
int j=0; j < ww.quot; j++) {
293 for (
int x=0; x < ww.rem; x++) {
303 for (
int j=0; j < ww.quot; j++) {
306 for (
int y=0; y < hh.rem; y++) {
317 for (
int y=0; y < hh.rem; y++) {
319 for (
int x=0; x < ww.rem; x++) {
339 const int hlwr = hlBand->
GetWidth() - hlW.rem;
341 const int lhwr = lhBand->
GetWidth() - hlW.rem;
343 int hlBase = 0, lhBase = 0, hlBase2, lhBase2;
348 if (!hlBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
349 if (!lhBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
353 if (quantParam < 0) quantParam = 0;
356 for (
int i=0; i < lhH.quot; i++) {
360 for (
int j=0; j < hlW.quot; j++) {
380 for (
int x=0; x < hlW.rem; x++) {
399 for (
int j=0; j < hlW.quot; j++) {
403 for (
int y=0; y < lhH.rem; y++) {
419 for (
int y=0; y < lhH.rem; y++) {
421 for (
int x=0; x < hlW.rem; x++) {
439 for (
int j=0; j < hlBand->
GetWidth(); j++) {
511 if (ex.
error == MissingData || ex.
error == FormatCannotRead) {
518 #ifdef LIBPGF_USE_OPENMP 520 #pragma omp parallel for default(shared) //no declared exceptions in next block 548 count = expected =
sizeof(UINT16);
550 if (count != expected) ReturnWithError(MissingData);
551 wordLen =
__VAL(wordLen);
552 if (wordLen >
BufferSize) ReturnWithError(FormatCannotRead);
554 #ifdef __PGFROISUPPORT__ 559 if (count != expected) ReturnWithError(MissingData);
569 if (count != expected) ReturnWithError(MissingData);
571 #ifdef PGF_USE_BIG_ENDIAN 574 for (
int i=0; i < count; i++) {
579 #ifdef __PGFROISUPPORT__ 586 #ifdef __PGFROISUPPORT__ 594 void CDecoder::SkipTileBuffer() {
622 count = expected =
sizeof(wordLen);
624 if (count != expected) ReturnWithError(MissingData);
625 wordLen =
__VAL(wordLen);
626 if (wordLen >
BufferSize) ReturnWithError(FormatCannotRead);
631 if (count != expected) ReturnWithError(MissingData);
632 h.val =
__VAL(h.val);
636 }
while (!h.rbh.tileEnd);
654 for (UINT32 k=0; k < bufferSize; k++) {
672 DataT planeMask = 1 << (nPlanes - 1);
674 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
768 UINT32 valPos = 0, signPos = 0, refPos = 0, sigPos = 0;
770 while (valPos < bufferSize) {
772 UINT32 sigEnd = valPos;
773 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
779 while (sigPos < sigEnd) {
781 UINT32 zerocnt =
SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
784 if (sigPos < sigEnd) {
786 SetBitAtPos(valPos, planeMask);
789 SetSign(valPos,
GetBit(signBits, signPos++));
792 m_sigFlagVector[valPos++] =
true;
797 if (valPos < bufferSize) {
799 if (
GetBit(refBits, refPos)) {
800 SetBitAtPos(valPos, planeMask);
806 ASSERT(sigPos <= bufferSize);
807 ASSERT(refPos <= bufferSize);
808 ASSERT(signPos <= bufferSize);
809 ASSERT(valPos == bufferSize);
827 UINT32 valPos = 0, refPos = 0;
828 UINT32 sigPos = 0, sigEnd;
830 UINT32 runlen = 1 << k;
831 UINT32 count = 0, rest = 0;
834 while (valPos < bufferSize) {
837 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
841 while (sigPos < sigEnd) {
849 if (
GetBit(m_codeBuffer, codePos++)) {
880 if (sigPos < sigEnd) {
885 SetBitAtPos(valPos, planeMask);
888 SetSign(valPos,
GetBit(m_codeBuffer, codePos++));
891 m_sigFlagVector[valPos++] =
true;
895 rest = sigPos - sigEnd;
903 if (valPos < bufferSize) {
905 if (
GetBit(refBits, refPos)) {
906 SetBitAtPos(valPos, planeMask);
912 ASSERT(sigPos <= bufferSize);
913 ASSERT(refPos <= bufferSize);
914 ASSERT(valPos == bufferSize);
931 UINT32 valPos = 0, refPos = 0;
932 UINT32 sigPos = 0, sigEnd;
933 UINT32 zerocnt, count = 0;
935 UINT32 runlen = 1 << k;
936 bool signBit =
false;
937 bool zeroAfterRun =
false;
939 while (valPos < bufferSize) {
942 while(!m_sigFlagVector[sigEnd]) { sigEnd++; }
948 while (sigPos < sigEnd) {
950 zerocnt =
SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
953 if (sigPos < sigEnd) {
955 SetBitAtPos(valPos, planeMask);
963 zeroAfterRun =
false;
966 if (
GetBit(m_codeBuffer, signPos++)) {
1003 SetSign(valPos, signBit);
1006 m_sigFlagVector[valPos++] =
true;
1012 if (valPos < bufferSize) {
1014 if (
GetBit(refBits, refPos)) {
1015 SetBitAtPos(valPos, planeMask);
1021 ASSERT(sigPos <= bufferSize);
1022 ASSERT(refPos <= bufferSize);
1023 ASSERT(valPos == bufferSize);
1030 void CDecoder::DumpBuffer() {
bool GetBit(UINT32 *stream, UINT32 pos)
virtual void Read(int *count, void *buffer)=0
UINT8 version
PGF version.
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.
#define LinBlockSize
side length of a coefficient block in a HH or LL subband
UINT32 m_valuePos
current position in m_value
void DecodeInterleaved(CWaveletTransform *wtChannel, int level, int quantParam)
#define BufferSize
must be a multiple of WordWidth, BufferSize <= UINT16_MAX
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
void Partition(CSubband *band, int quantParam, int width, int height, int startPos, int pitch)
DataT m_value[BufferSize]
output buffer of values with index m_valuePos
#define PGFMagic
PGF identification.
#define Version6
hSize in PGFPreHeader uses 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
UINT32 ReadEncodedData(UINT8 *target, UINT32 len) const
OSError error
operating system error code
void ReadMacroBlock(CMacroBlock *block)
throws IOException
CDecoder(CPGFStream *stream, PGFPreHeader &preHeader, PGFHeader &header, PGFPostHeader &postHeader, UINT32 *&levelLength, UINT64 &userDataPos, bool useOMP, UINT32 userDataPolicy)
UINT32 ComposeBitplaneRLD(UINT32 bufferSize, DataT planeMask, UINT32 sigPos, UINT32 *refBits)
void DequantizeValue(CSubband *band, UINT32 bandPos, int quantParam)
ROIBlockHeader m_header
block header
void SetData(UINT32 pos, DataT v)
#define CodeBufferBitLen
max number of bits in m_codeBuffer
bool m_sigFlagVector[BufferSize+1]
UINT32 m_codeBuffer[CodeBufferLen]
input buffer for encoded bitstream