58 #define CodeBufferBitLen (CodeBufferLen*WordWidth)
59 #define MaxCodeLen ((1 << RLblockSizeLen) - 1)
74 , m_nLevels(header.nLevels)
76 , m_forceWriting(
false)
77 #ifdef __PGFROISUPPORT__
86 #ifdef LIBPGF_USE_OPENMP
87 m_macroBlockLen = omp_get_num_procs();
92 if (useOMP && m_macroBlockLen > 1) {
93 #ifdef LIBPGF_USE_OPENMP
94 omp_set_num_threads(m_macroBlockLen);
97 m_macroBlocks =
new(std::nothrow)
CMacroBlock*[m_macroBlockLen];
98 if (!m_macroBlocks) ReturnWithError(InsufficientMemory);
99 for (
int i=0; i < m_macroBlockLen; i++) m_macroBlocks[i] =
new CMacroBlock(
this);
100 m_lastMacroBlock = 0;
101 m_currentBlock = m_macroBlocks[m_lastMacroBlock++];
109 m_startPosition = m_stream->GetPos();
112 preHeader.hSize =
__VAL(preHeader.hSize);
114 m_stream->Write(&count, &preHeader);
117 header.height =
__VAL(header.height);
118 header.width =
__VAL(header.width);
120 m_stream->Write(&count, &header);
126 m_stream->Write(&count, (
void *)postHeader.clut);
129 userDataPos = m_stream->GetPos();
130 if (postHeader.userDataLen) {
131 if (postHeader.userData) {
133 count = postHeader.userDataLen;
134 m_stream->Write(&count, postHeader.userData);
136 m_stream->SetPos(FSFromCurrent, count);
141 m_levelLengthPos = m_stream->GetPos();
156 UINT64 curPos = m_stream->GetPos();
160 m_stream->SetPos(FSFromStart, m_startPosition);
161 preHeader.hSize =
__VAL(preHeader.hSize);
162 m_stream->Write(&count, &preHeader);
164 m_stream->SetPos(FSFromStart, curPos);
174 delete[] levelLength;
175 levelLength =
new(std::nothrow) UINT32[m_nLevels];
176 if (!levelLength) ReturnWithError(InsufficientMemory);
177 for (UINT8 l = 0; l < m_nLevels; l++) levelLength[l] = 0;
178 m_levelLength = levelLength;
181 m_levelLengthPos = m_stream->GetPos();
185 m_stream->Write(&count, m_levelLength);
204 #ifdef PGF_USE_BIG_ENDIAN
216 #endif //PGF_USE_BIG_ENDIAN
247 const int wr = pitch - ww.rem;
248 int pos, base = startPos, base2;
251 for (
int i=0; i < hh.quot; i++) {
254 for (
int j=0; j < ww.quot; j++) {
258 WriteValue(band, pos);
268 for (
int x=0; x < ww.rem; x++) {
269 WriteValue(band, pos);
278 for (
int j=0; j < ww.quot; j++) {
281 for (
int y=0; y < hh.rem; y++) {
283 WriteValue(band, pos);
292 for (
int y=0; y < hh.rem; y++) {
294 for (
int x=0; x < ww.rem; x++) {
295 WriteValue(band, pos);
322 if (m_currentBlock->m_valuePos ==
BufferSize) {
325 DataT val = m_currentBlock->m_value[m_currentBlock->m_valuePos++] = band->GetData(bandPos);
327 if (v > m_currentBlock->m_maxAbsValue) m_currentBlock->m_maxAbsValue = v;
337 ASSERT(m_currentBlock);
338 #ifdef __PGFROISUPPORT__
343 m_currentBlock->m_header = h;
346 if (m_macroBlockLen == 1) {
347 m_currentBlock->BitplaneEncode();
348 WriteMacroBlock(m_currentBlock);
351 int lastLevelIndex = m_currentBlock->m_lastLevelIndex;
353 if (m_forceWriting || m_lastMacroBlock == m_macroBlockLen) {
374 #pragma omp parallel for default(shared) //no declared exceptions in next block
375 for (
int i=0; i < m_lastMacroBlock; i++) {
376 m_macroBlocks[i]->BitplaneEncode();
378 for (
int i=0; i < m_lastMacroBlock; i++) {
379 WriteMacroBlock(m_macroBlocks[i]);
383 m_forceWriting =
false;
384 m_lastMacroBlock = 0;
387 m_currentBlock = m_macroBlocks[m_lastMacroBlock++];
388 m_currentBlock->Init(lastLevelIndex);
400 int count =
sizeof(UINT16);
407 #ifdef PGF_USE_BIG_ENDIAN
409 UINT16 wl =
__VAL(wordLen);
410 m_stream->Write(&count, &wl); ASSERT(count ==
sizeof(UINT16));
412 #ifdef __PGFROISUPPORT__
416 m_stream->Write(&count, &h.
val); ASSERT(count ==
sizeof(UINT16));
418 #endif // __PGFROISUPPORT__
421 for (
int i=0; i < wordLen; i++) {
422 block->m_codeBuffer[i] =
__VAL(block->m_codeBuffer[i]);
426 m_stream->Write(&count, &wordLen); ASSERT(count ==
sizeof(UINT16));
428 #ifdef __PGFROISUPPORT__
431 m_stream->Write(&count, &h.
val); ASSERT(count ==
sizeof(UINT16));
433 #endif // __PGFROISUPPORT__
434 #endif // PGF_USE_BIG_ENDIAN
438 m_stream->Write(&count, block->m_codeBuffer);
444 ASSERT(m_currLevelIndex < m_nLevels);
445 m_levelLength[m_currLevelIndex] += (UINT32)ComputeBufferLength();
446 m_currLevelIndex = block->m_lastLevelIndex + 1;
454 block->m_valuePos = 0;
455 block->m_maxAbsValue = 0;
470 UINT32 sigLen, codeLen = 0, wordPos, refLen, signLen;
483 for (UINT32 k=0; k < bufferSize; k++) {
489 for (UINT32 k=0; k < bufferSize; k++) {
504 planeMask = 1 << (nPlanes - 1);
506 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
552 if (useRL && codeLen <=
MaxCodeLen && codeLen < signLen) {
579 for (UINT32 k=0; k < codeLen; k++) {
589 for (UINT32 k=0; k < refLen; k++) {
601 for (UINT32 k=0; k < refLen; k++) {
627 UINT32 valuePos = 0, valueEnd;
634 const UINT32 outStartPos = codePos;
636 UINT32 runlen = 1 << k;
639 while (valuePos < bufferSize) {
642 while(!m_sigFlagVector[valueEnd]) { valueEnd++; }
646 while (valuePos < valueEnd) {
647 if (GetBitAtPos(valuePos, planeMask)) {
651 SetBit(m_codeBuffer, codePos++);
662 if (m_value[valuePos] < 0) {
663 SetBit(signBits, signLen++);
664 SetBit(m_codeBuffer, codePos++);
671 SetBit(sigBits, sigPos++);
674 m_sigFlagVector[valuePos] =
true;
681 if (count == runlen) {
700 if (valuePos < bufferSize) {
702 if (GetBitAtPos(valuePos++, planeMask)) {
713 SetBit(m_codeBuffer, codePos++);
719 SetBit(m_codeBuffer, codePos++);
723 ASSERT(sigPos <= bufferSize);
724 ASSERT(refPos <= bufferSize);
725 ASSERT(signLen <= bufferSize);
726 ASSERT(valuePos == bufferSize);
728 codeLen = codePos - outStartPos;
740 if (m_maxAbsValue > 0) {
741 while (m_maxAbsValue > 0) {
742 m_maxAbsValue >>= 1; cnt++;
765 const UINT32 outStartPos = codePos;
767 UINT32 runlen = 1 << k;
771 while (signPos < signLen) {
775 if (count == runlen) {
778 SetBit(m_codeBuffer, codePos++);
787 signPos += count + 1;
800 ASSERT(signPos == signLen || signPos == signLen + 1);
802 return codePos - outStartPos;
807 void CEncoder::DumpBuffer()
const {