75 PGFPostHeader& postHeader, uint32_t*& levelLength, uint64_t& userDataPos,
76 bool useOMP, uint32_t userDataPolicy)
97 if (count != expected) ReturnWithError(MissingData);
102 count = expected = 4;
104 count = expected = 2;
107 if (count != expected) ReturnWithError(MissingData);
115 ReturnWithError(FormatCannotRead);
121 if (count != expected) ReturnWithError(MissingData);
129#ifndef __PGFROISUPPORT__
131 if (preHeader.
version &
PGFROI) ReturnWithError(FormatCannotRead);
134 uint32_t size = preHeader.
hSize;
146 if (count != expected) ReturnWithError(MissingData);
149 if (size > (uint32_t)count) {
166 if (!postHeader.
userData) ReturnWithError(InsufficientMemory);
171 if (count != expected) ReturnWithError(MissingData);
180 levelLength =
new(std::nothrow) uint32_t[header.
nLevels];
181 if (!levelLength) ReturnWithError(InsufficientMemory);
186 if (count != expected) ReturnWithError(MissingData);
188#ifdef PGF_USE_BIG_ENDIAN
190 for (
int i = 0; i < header.
nLevels; i++) {
191 levelLength[i] =
__VAL(levelLength[i]);
196 for (
int i = 0; i < header.
nLevels; i++) {
206#ifdef LIBPGF_USE_OPENMP
213#ifdef LIBPGF_USE_OPENMP
273 const int wr = pitch - ww.rem;
274 int pos, base = startPos, base2;
277 for (
int i = 0; i < hh.quot; i++) {
280 for (
int j = 0; j < ww.quot; j++) {
294 for (
int x = 0; x < ww.rem; x++) {
304 for (
int j = 0; j < ww.quot; j++) {
307 for (
int y = 0; y < hh.rem; y++) {
318 for (
int y = 0; y < hh.rem; y++) {
320 for (
int x = 0; x < ww.rem; x++) {
340 const int hlwr = hlBand->
GetWidth() - hlW.rem;
342 const int lhwr = lhBand->
GetWidth() - hlW.rem;
344 int hlBase = 0, lhBase = 0, hlBase2, lhBase2;
349 if (!hlBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
350 if (!lhBand->
AllocMemory()) ReturnWithError(InsufficientMemory);
354 if (quantParam < 0) quantParam = 0;
357 for (
int i = 0; i < lhH.quot; i++) {
361 for (
int j = 0; j < hlW.quot; j++) {
381 for (
int x = 0; x < hlW.rem; x++) {
400 for (
int j = 0; j < hlW.quot; j++) {
404 for (
int y = 0; y < lhH.rem; y++) {
420 for (
int y = 0; y < lhH.rem; y++) {
422 for (
int x = 0; x < hlW.rem; x++) {
440 for (
int j = 0; j < hlBand->
GetWidth(); j++) {
512 if (ex.
error == MissingData || ex.
error == FormatCannotRead) {
519#ifdef LIBPGF_USE_OPENMP
521#pragma omp parallel for default(shared)
549 count = expected =
sizeof(uint16_t);
551 if (count != expected) ReturnWithError(MissingData);
552 wordLen =
__VAL(wordLen);
553 if (wordLen >
BufferSize) ReturnWithError(FormatCannotRead);
555#ifdef __PGFROISUPPORT__
560 if (count != expected) ReturnWithError(MissingData);
570 if (count != expected) ReturnWithError(MissingData);
572#ifdef PGF_USE_BIG_ENDIAN
575 for (
int i = 0; i < count; i++) {
580#ifdef __PGFROISUPPORT__
587#ifdef __PGFROISUPPORT__
595 void CDecoder::SkipTileBuffer() {
623 count = expected =
sizeof(wordLen);
625 if (count != expected) ReturnWithError(MissingData);
626 wordLen =
__VAL(wordLen);
627 if (wordLen >
BufferSize) ReturnWithError(FormatCannotRead);
630 count = expected =
sizeof(ROIBlockHeader);
632 if (count != expected) ReturnWithError(MissingData);
633 h.val =
__VAL(h.val);
637 }
while (!h.rbh.tileEnd);
655 for (uint32_t k = 0; k < bufferSize; k++) {
673 DataT planeMask = 1 << (nPlanes - 1);
675 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
769 uint32_t valPos = 0, signPos = 0, refPos = 0, sigPos = 0;
771 while (valPos < bufferSize) {
773 uint32_t sigEnd = valPos;
780 while (sigPos < sigEnd) {
782 uint32_t zerocnt =
SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
785 if (sigPos < sigEnd) {
798 if (valPos < bufferSize) {
800 if (
GetBit(refBits, refPos)) {
807 ASSERT(sigPos <= bufferSize);
808 ASSERT(refPos <= bufferSize);
809 ASSERT(signPos <= bufferSize);
810 ASSERT(valPos == bufferSize);
828 uint32_t valPos = 0, refPos = 0;
829 uint32_t sigPos = 0, sigEnd;
831 uint32_t runlen = 1 << k;
832 uint32_t count = 0, rest = 0;
835 while (valPos < bufferSize) {
842 while (sigPos < sigEnd) {
881 if (sigPos < sigEnd) {
896 rest = sigPos - sigEnd;
904 if (valPos < bufferSize) {
906 if (
GetBit(refBits, refPos)) {
913 ASSERT(sigPos <= bufferSize);
914 ASSERT(refPos <= bufferSize);
915 ASSERT(valPos == bufferSize);
932 uint32_t valPos = 0, refPos = 0;
933 uint32_t sigPos = 0, sigEnd;
934 uint32_t zerocnt, count = 0;
936 uint32_t runlen = 1 << k;
937 bool signBit =
false;
938 bool zeroAfterRun =
false;
940 while (valPos < bufferSize) {
949 while (sigPos < sigEnd) {
951 zerocnt =
SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
954 if (sigPos < sigEnd) {
964 zeroAfterRun =
false;
1013 if (valPos < bufferSize) {
1015 if (
GetBit(refBits, refPos)) {
1022 ASSERT(sigPos <= bufferSize);
1023 ASSERT(refPos <= bufferSize);
1024 ASSERT(valPos == bufferSize);
1031 void CDecoder::DumpBuffer() {
A macro block is a decoding unit of fixed size (uncoded).
DataT m_value[BufferSize]
output buffer of values with index m_valuePos
uint32_t ComposeBitplane(uint32_t bufferSize, DataT planeMask, uint32_t *sigBits, uint32_t *refBits, uint32_t *signBits)
bool m_sigFlagVector[BufferSize+1]
uint32_t m_valuePos
current position in m_value
ROIBlockHeader m_header
block header
void SetBitAtPos(uint32_t pos, DataT planeMask)
CMacroBlock()
Constructor: Initializes new macro block.
void SetSign(uint32_t pos, bool sign)
uint32_t m_codeBuffer[CodeBufferLen]
input buffer for encoded bitstream
uint32_t ComposeBitplaneRLD(uint32_t bufferSize, DataT planeMask, uint32_t sigPos, uint32_t *refBits)
bool IsCompletelyRead() const
int m_macroBlocksAvailable
number of decoded macro blocks (including currently used macro block)
CMacroBlock ** m_macroBlocks
array of macroblocks
void Skip(uint64_t offset)
void Partition(CSubband *band, int quantParam, int width, int height, int startPos, int pitch)
int m_currentBlockIndex
index of current macro block
int m_macroBlockLen
array length
uint32_t ReadEncodedData(uint8_t *target, uint32_t len) const
CPGFStream * m_stream
input PGF stream
void ReadMacroBlock(CMacroBlock *block)
throws IOException
void DequantizeValue(CSubband *band, uint32_t bandPos, int quantParam)
uint64_t m_streamSizeEstimation
estimation of stream size
void DecodeInterleaved(CWaveletTransform *wtChannel, int level, int quantParam)
uint32_t m_encodedHeaderLength
stream offset from startPos to the beginning of the data part (highest level)
uint64_t m_startPos
stream position at the beginning of the PGF pre-header
CMacroBlock * m_currentBlock
current macro block (used by main thread)
CDecoder(CPGFStream *stream, PGFPreHeader &preHeader, PGFHeader &header, PGFPostHeader &postHeader, uint32_t *&levelLength, uint64_t &userDataPos, bool useOMP, uint32_t userDataPolicy)
Abstract stream base class.
virtual void Read(int *count, void *buffer)=0
virtual uint64_t GetPos() const =0
virtual void SetPos(short posMode, int64_t posOff)=0
void SetData(uint32_t pos, DataT v)
constexpr Policy CachePrefix
constexpr int MaxBitPlanes
maximum number of bit planes of m_value: 32 minus sign bit
constexpr size_t HeaderSize
constexpr uint32_t CodeBufferBitLen
max number of bits in m_codeBuffer
constexpr int LinBlockSize
side length of a coefficient block in a HH or LL subband
constexpr size_t MagicVersionSize
constexpr size_t MaxUserDataSize
constexpr const char * PGFMagic
PGF identification.
bool GetBit(uint32_t *stream, uint32_t pos)
uint32_t GetValueBlock(uint32_t *stream, uint32_t pos, uint32_t k)
constexpr int InterBlockSize
side length of a coefficient block in a HL or LH subband
uint32_t SeekBitRange(uint32_t *stream, uint32_t pos, uint32_t len)
uint32_t AlignWordPos(uint32_t pos)
constexpr int RLblockSizeLen
block size length (< 16): ld(BufferSize) < RLblockSizeLen <= 2*ld(BufferSize)
constexpr uint32_t CodeBufferLen
number of words in code buffer (CodeBufferLen > BufferLen)
constexpr int Version6
hSize in PGFPreHeader uses 32 bits instead of 16 bits
constexpr int MaxBitPlanesLog
number of bits to code the maximum number of bit planes (in 32 or 16 bit mode)
constexpr uint32_t MaxCodeLen
max length of RL encoded block
constexpr size_t ColorTableSize
constexpr uint32_t BufferSize
must be a multiple of WordWidth, BufferSize <= UINT16_MAX
constexpr int PGFROI
supports Regions Of Interest
OSError error
operating system error code
char magic[3]
PGF identification = "PGF".
uint8_t version
PGF version.