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++];
105 m_currentBlock =
new CMacroBlock(
this);
109 m_startPosition = m_stream->GetPos();
112 preHeader.hSize =
__VAL(preHeader.hSize);
114 m_stream->Write(&count, &preHeader);
120 m_stream->Write(&count, &
header);
126 m_stream->Write(&count, (
void *)
postHeader.clut);
129 userDataPos = m_stream->GetPos();
136 m_stream->SetPos(FSFromCurrent, count);
141 m_levelLengthPos = m_stream->GetPos();
147 delete m_currentBlock;
148 delete[] m_macroBlocks;
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);
198 UINT64 curPos = m_stream->GetPos();
201 m_stream->SetPos(FSFromStart, m_levelLengthPos);
204 #ifdef PGF_USE_BIG_ENDIAN
208 for (
int i=0; i < m_currLevelIndex; i++) {
209 levelLength =
__VAL(UINT32(m_levelLength[i]));
210 m_stream->Write(&count, &levelLength);
215 m_stream->Write(&count, m_levelLength);
216 #endif //PGF_USE_BIG_ENDIAN
219 m_stream->SetPos(FSFromCurrent, count);
223 UINT32 retValue = UINT32(curPos - m_stream->GetPos());
226 m_stream->SetPos(FSFromStart, curPos);
247 const int wr = pitch - ww.rem;
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);
306 if (m_currentBlock->m_valuePos > 0) {
308 memset(&(m_currentBlock->m_value[m_currentBlock->m_valuePos]), 0, (
BufferSize - m_currentBlock->m_valuePos)*
DataTSize);
312 m_forceWriting =
true;
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;
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);
395 void CEncoder::WriteMacroBlock(CMacroBlock* block)
THROW_ {
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;
468 void CEncoder::CMacroBlock::BitplaneEncode() {
470 UINT32 sigLen, codeLen = 0, wordPos, refLen, signLen;
483 for (UINT32
k=0;
k < bufferSize;
k++) {
484 m_sigFlagVector[
k] =
false;
486 m_sigFlagVector[bufferSize] =
true;
489 for (UINT32
k=0;
k < bufferSize;
k++) {
495 nPlanes = NumberOfBitplanes();
504 planeMask = 1 << (nPlanes - 1);
506 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
513 sigLen = DecomposeBitplane(bufferSize, planeMask, m_codePos +
RLblockSizeLen + 1, sigBits, refBits, signBits, signLen, codeLen);
518 SetBit(m_codeBuffer, m_codePos++);
536 ClearBit(m_codeBuffer, m_codePos++);
543 if (m_encoder->m_favorSpeed || signLen == 0) {
549 codeLen = RLESigns(m_codePos +
RLblockSizeLen + 1, signBits, signLen);
552 if (useRL && codeLen <=
MaxCodeLen && codeLen < signLen) {
556 SetBit(m_codeBuffer, m_codePos++);
568 ClearBit(m_codeBuffer, m_codePos++);
579 for (UINT32
k=0;
k < codeLen;
k++) {
580 m_codeBuffer[wordPos++] = signBits[
k];
589 for (UINT32
k=0;
k < refLen;
k++) {
590 m_codeBuffer[wordPos++] = sigBits[
k];
601 for (UINT32
k=0;
k < refLen;
k++) {
602 m_codeBuffer[wordPos++] = refBits[
k];
620 UINT32 CEncoder::CMacroBlock::DecomposeBitplane(UINT32 bufferSize, UINT32 planeMask, UINT32 codePos, UINT32* sigBits, UINT32* refBits, UINT32* signBits, UINT32& signLen, UINT32& codeLen) {
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;
736 UINT8 CEncoder::CMacroBlock::NumberOfBitplanes() {
740 if (m_maxAbsValue > 0) {
741 while (m_maxAbsValue > 0) {
742 m_maxAbsValue >>= 1; cnt++;
760 UINT32 CEncoder::CMacroBlock::RLESigns(UINT32 codePos, UINT32* signBits, UINT32 signLen) {
765 const UINT32 outStartPos = codePos;
767 UINT32 runlen = 1 <<
k;
771 while (signPos < signLen) {
773 count = SeekBit1Range(signBits, signPos,
__min(runlen, signLen - signPos));
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 {