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