58 #define CodeBufferBitLen (BufferSize*WordWidth) // max number of bits in m_codeBuffer
59 #define MaxCodeLen ((1 << RLblockSizeLen) - 1) // max length of RL encoded block
70 , m_nLevels(header.nLevels)
72 , m_forceWriting(
false)
73 #ifdef __PGFROISUPPORT__
82 #ifdef LIBPGF_USE_OPENMP
83 m_macroBlockLen = omp_get_num_procs();
88 if (useOMP && m_macroBlockLen > 1) {
89 #ifdef LIBPGF_USE_OPENMP
90 omp_set_num_threads(m_macroBlockLen);
94 for (
int i=0; i < m_macroBlockLen; i++) m_macroBlocks[i] =
new CMacroBlock(
this);
96 m_currentBlock = m_macroBlocks[m_lastMacroBlock++];
104 m_startPosition = m_stream->GetPos();
107 preHeader.hSize =
__VAL(preHeader.hSize);
109 m_stream->Write(&count, &preHeader);
112 header.height =
__VAL(header.height);
113 header.width =
__VAL(header.width);
115 m_stream->Write(&count, &header);
121 m_stream->Write(&count, (
void *)postHeader.clut);
123 if (postHeader.userData && postHeader.userDataLen) {
125 count = postHeader.userDataLen;
126 m_stream->Write(&count, postHeader.userData);
130 delete[] levelLength;
131 levelLength =
new UINT32[m_nLevels];
132 for (UINT8 l = 0; l < m_nLevels; l++) levelLength[l] = 0;
133 m_levelLength = levelLength;
136 m_levelLengthPos = m_stream->GetPos();
138 m_stream->Write(&count, m_levelLength);
167 const int wr = pitch - ww.rem;
168 int pos, base = startPos, base2;
171 for (
int i=0; i < hh.quot; i++) {
174 for (
int j=0; j < ww.quot; j++) {
178 WriteValue(band, pos);
188 for (
int x=0; x < ww.rem; x++) {
189 WriteValue(band, pos);
198 for (
int j=0; j < ww.quot; j++) {
201 for (
int y=0; y < hh.rem; y++) {
203 WriteValue(band, pos);
212 for (
int y=0; y < hh.rem; y++) {
214 for (
int x=0; x < ww.rem; x++) {
215 WriteValue(band, pos);
247 #ifdef PGF_USE_BIG_ENDIAN
259 #endif //PGF_USE_BIG_ENDIAN
273 if (m_currentBlock->m_valuePos ==
BufferSize) {
276 DataT val = m_currentBlock->m_value[m_currentBlock->m_valuePos++] = band->GetData(bandPos);
278 if (v > m_currentBlock->m_maxAbsValue) m_currentBlock->m_maxAbsValue = v;
288 ASSERT(m_currentBlock);
289 #ifdef __PGFROISUPPORT__
294 m_currentBlock->m_header = h;
297 if (m_macroBlockLen == 1) {
298 m_currentBlock->BitplaneEncode();
299 WriteMacroBlock(m_currentBlock);
302 int lastLevelIndex = m_currentBlock->m_lastLevelIndex;
304 if (m_forceWriting || m_lastMacroBlock == m_macroBlockLen) {
325 #pragma omp parallel for default(shared) //no declared exceptions in next block
326 for (
int i=0; i < m_lastMacroBlock; i++) {
327 m_macroBlocks[i]->BitplaneEncode();
329 for (
int i=0; i < m_lastMacroBlock; i++) {
330 WriteMacroBlock(m_macroBlocks[i]);
334 m_forceWriting =
false;
335 m_lastMacroBlock = 0;
338 m_currentBlock = m_macroBlocks[m_lastMacroBlock++];
339 m_currentBlock->Init(lastLevelIndex);
351 int count =
sizeof(UINT16);
358 #ifdef PGF_USE_BIG_ENDIAN
360 UINT16 wl =
__VAL(wordLen);
361 m_stream->Write(&count, &wl); ASSERT(count ==
sizeof(UINT16));
363 #ifdef __PGFROISUPPORT__
367 m_stream->Write(&count, &h.
val); ASSERT(count ==
sizeof(UINT16));
369 #endif // __PGFROISUPPORT__
372 for (
int i=0; i < wordLen; i++) {
373 block->m_codeBuffer[i] =
__VAL(block->m_codeBuffer[i]);
377 m_stream->Write(&count, &wordLen); ASSERT(count ==
sizeof(UINT16));
379 #ifdef __PGFROISUPPORT__
382 m_stream->Write(&count, &h.
val); ASSERT(count ==
sizeof(UINT16));
384 #endif // __PGFROISUPPORT__
385 #endif // PGF_USE_BIG_ENDIAN
389 m_stream->Write(&count, block->m_codeBuffer);
395 m_levelLength[m_currLevelIndex] += ComputeBufferLength();
396 m_currLevelIndex = block->m_lastLevelIndex + 1;
404 block->m_valuePos = 0;
405 block->m_maxAbsValue = 0;
420 UINT32 sigLen, codeLen = 0, wordPos, refLen, signLen;
433 for (UINT32 k=0; k < bufferSize; k++) {
439 for (UINT32 k=0; k < bufferSize; k++) {
454 planeMask = 1 << (nPlanes - 1);
456 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
502 if (useRL && codeLen <=
MaxCodeLen && codeLen < signLen) {
513 ASSERT(0 <= wordPos && wordPos < bufferSize);
526 ASSERT(0 <= wordPos && wordPos < bufferSize);
529 for (UINT32 k=0; k < codeLen; k++) {
536 ASSERT(0 <= wordPos && wordPos < bufferSize);
539 for (UINT32 k=0; k < refLen; k++) {
548 ASSERT(0 <= wordPos && wordPos < bufferSize);
551 for (UINT32 k=0; k < refLen; k++) {
577 UINT32 valuePos = 0, valueEnd;
584 const UINT32 outStartPos = codePos;
586 UINT32 runlen = 1 << k;
589 while (valuePos < bufferSize) {
592 while(!m_sigFlagVector[valueEnd]) { valueEnd++; }
596 while (valuePos < valueEnd) {
597 if (GetBitAtPos(valuePos, planeMask)) {
601 SetBit(m_codeBuffer, codePos++);
612 if (m_value[valuePos] < 0) {
613 SetBit(signBits, signLen++);
614 SetBit(m_codeBuffer, codePos++);
621 SetBit(sigBits, sigPos++);
624 m_sigFlagVector[valuePos] =
true;
631 if (count == runlen) {
650 if (valuePos < bufferSize) {
652 if (GetBitAtPos(valuePos++, planeMask)) {
663 SetBit(m_codeBuffer, codePos++);
669 SetBit(m_codeBuffer, codePos++);
673 ASSERT(sigPos <= bufferSize);
674 ASSERT(refPos <= bufferSize);
675 ASSERT(signLen <= bufferSize);
676 ASSERT(valuePos == bufferSize);
678 codeLen = codePos - outStartPos;
690 if (m_maxAbsValue > 0) {
691 while (m_maxAbsValue > 0) {
692 m_maxAbsValue >>= 1; cnt++;
715 const UINT32 outStartPos = codePos;
717 UINT32 runlen = 1 << k;
721 while (signPos < signLen) {
725 if (count == runlen) {
728 SetBit(m_codeBuffer, codePos++);
737 signPos += count + 1;
750 ASSERT(signPos == signLen || signPos == signLen + 1);
752 return codePos - outStartPos;
757 void CEncoder::DumpBuffer()
const {