libpgf 7.26.45
PGF - Progressive Graphics File
Loading...
Searching...
No Matches
Encoder.cpp
Go to the documentation of this file.
1/*
2 * The Progressive Graphics File; http://www.libpgf.org
3 *
4 * $Date: 2007-02-03 13:04:21 +0100 (Sa, 03 Feb 2007) $
5 * $Revision: 280 $
6 *
7 * This file Copyright (C) 2006 xeraina GmbH, Switzerland
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
28
29#include "Encoder.h"
30#ifdef TRACE
31 #include <stdio.h>
32#endif
33
35// PGF: file structure
36//
37// PGFPreHeader PGFHeader [PGFPostHeader] LevelLengths Level_n-1 Level_n-2 ... Level_0
38// PGFPostHeader ::= [ColorTable] [UserData]
39// LevelLengths ::= uint32_t[nLevels]
40
42// Encoding scheme
43// input: wavelet coefficients stored in subbands
44// output: binary file
45//
46// subband
47// |
48// m_value [BufferSize]
49// | | |
50// m_sign sigBits refBits [BufferSize, BufferLen, BufferLen]
51// | | |
52// m_codeBuffer (for each plane: RLcodeLength (16 bit), RLcoded sigBits + m_sign, refBits)
53// |
54// file (for each buffer: packedLength (16 bit), packed bits)
55//
56
57namespace pgf {
58 // Constants
59 constexpr uint32_t CodeBufferBitLen = CodeBufferLen*WordWidth;
60 constexpr uint32_t MaxCodeLen = (1 << RLblockSizeLen) - 1;
61
71 CEncoder::CEncoder(CPGFStream* stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader& postHeader, uint64_t& userDataPos, bool useOMP)
72 : m_stream(stream)
75 , m_nLevels(header.nLevels)
76 , m_favorSpeed(false)
77 , m_forceWriting(false)
79 , m_roi(false)
80#endif
81 {
82 ASSERT(m_stream);
83
84 int count;
86 m_levelLength = nullptr;
87
88 // set number of threads
89#ifdef LIBPGF_USE_OPENMP
90 m_macroBlockLen = omp_get_num_procs();
91#else
93#endif
94
95 if (useOMP && m_macroBlockLen > 1) {
96#ifdef LIBPGF_USE_OPENMP
97 omp_set_num_threads(m_macroBlockLen);
98#endif
99 // create macro block array
100 m_macroBlocks = new(std::nothrow) CMacroBlock*[m_macroBlockLen];
101 if (!m_macroBlocks) ReturnWithError(InsufficientMemory);
102 for (int i = 0; i < m_macroBlockLen; i++) m_macroBlocks[i] = new CMacroBlock(this);
104 } else {
105 m_macroBlocks = 0;
106 m_macroBlockLen = 1;
107 m_currentBlock = new CMacroBlock(this);
108 }
109
110 // save file position
112
113 // write preHeader
114 preHeader.hSize = __VAL(preHeader.hSize);
115 count = PreHeaderSize;
116 m_stream->Write(&count, &preHeader);
117
118 // write file header
119 header.height = __VAL(header.height);
120 header.width = __VAL(header.width);
121 count = HeaderSize;
122 m_stream->Write(&count, &header);
123
124 // write postHeader
125 if (header.mode == ImageModeIndexedColor) {
126 // write color table
127 count = ColorTableSize;
128 m_stream->Write(&count, (void*)postHeader.clut);
129 }
130 // save user data file position
131 userDataPos = m_stream->GetPos();
132 if (postHeader.userDataLen) {
133 if (postHeader.userData) {
134 // write user data
135 count = postHeader.userDataLen;
136 m_stream->Write(&count, postHeader.userData);
137 } else {
138 m_stream->SetPos(SEEK_CUR, count);
139 }
140 }
141
142 // save level length file position
144 }
145
147 // Destructor
149 if (m_macroBlocks) {
150 for (int i = 0; i < m_macroBlockLen; i++) delete m_macroBlocks[i];
151 delete[] m_macroBlocks;
152 } else {
153 delete m_currentBlock;
154 }
155 }
156
162 uint64_t curPos = m_stream->GetPos(); // end of user data
163 int count = PreHeaderSize;
164
165 // write preHeader
167 preHeader.hSize = __VAL(preHeader.hSize);
168 m_stream->Write(&count, &preHeader);
169
170 m_stream->SetPos(SEEK_SET, curPos);
171 }
172
178 uint32_t CEncoder::WriteLevelLength(uint32_t*& levelLength) {
179 // renew levelLength
180 delete[] levelLength;
181 levelLength = new(std::nothrow) uint32_t[m_nLevels];
182 if (!levelLength) ReturnWithError(InsufficientMemory);
183 for (uint8_t l = 0; l < m_nLevels; l++) levelLength[l] = 0;
184 m_levelLength = levelLength;
185
186 // save level length file position
188
189 // write dummy levelLength
190 int count = m_nLevels*WordBytes;
191 m_stream->Write(&count, m_levelLength);
192
193 // save current file position
195
196 return count;
197 }
198
204 uint64_t curPos = m_stream->GetPos(); // end of image
205
206 // set file pos to levelLength
207 m_stream->SetPos(SEEK_SET, m_levelLengthPos);
208
209 if (m_levelLength) {
210#ifdef PGF_USE_BIG_ENDIAN
211 uint32_t levelLength;
212 int count = WordBytes;
213
214 for (int i = 0; i < m_currLevelIndex; i++) {
215 levelLength = __VAL(uint32_t(m_levelLength[i]));
216 m_stream->Write(&count, &levelLength);
217 }
218#else
219 int count = m_currLevelIndex*WordBytes;
220
221 m_stream->Write(&count, m_levelLength);
222#endif //PGF_USE_BIG_ENDIAN
223 } else {
224 int count = m_currLevelIndex*WordBytes;
225 m_stream->SetPos(SEEK_CUR, count);
226 }
227
228 // begin of image
229 uint32_t retValue = uint32_t(curPos - m_stream->GetPos());
230
231 // restore file position
232 m_stream->SetPos(SEEK_SET, curPos);
233
234 return retValue;
235 }
236
247 void CEncoder::Partition(CSubband* band, int width, int height, int startPos, int pitch) {
248 ASSERT(band);
249
250 const div_t hh = div(height, LinBlockSize);
251 const div_t ww = div(width, LinBlockSize);
252 const int ws = pitch - LinBlockSize;
253 const int wr = pitch - ww.rem;
254 int pos, base = startPos, base2;
255
256 // main height
257 for (int i = 0; i < hh.quot; i++) {
258 // main width
259 base2 = base;
260 for (int j = 0; j < ww.quot; j++) {
261 pos = base2;
262 for (int y = 0; y < LinBlockSize; y++) {
263 for (int x = 0; x < LinBlockSize; x++) {
264 WriteValue(band, pos);
265 pos++;
266 }
267 pos += ws;
268 }
269 base2 += LinBlockSize;
270 }
271 // rest of width
272 pos = base2;
273 for (int y = 0; y < LinBlockSize; y++) {
274 for (int x = 0; x < ww.rem; x++) {
275 WriteValue(band, pos);
276 pos++;
277 }
278 pos += wr;
279 base += pitch;
280 }
281 }
282 // main width
283 base2 = base;
284 for (int j = 0; j < ww.quot; j++) {
285 // rest of height
286 pos = base2;
287 for (int y = 0; y < hh.rem; y++) {
288 for (int x = 0; x < LinBlockSize; x++) {
289 WriteValue(band, pos);
290 pos++;
291 }
292 pos += ws;
293 }
294 base2 += LinBlockSize;
295 }
296 // rest of height
297 pos = base2;
298 for (int y = 0; y < hh.rem; y++) {
299 // rest of width
300 for (int x = 0; x < ww.rem; x++) {
301 WriteValue(band, pos);
302 pos++;
303 }
304 pos += wr;
305 }
306 }
307
312 if (m_currentBlock->m_valuePos > 0) {
313 // pad buffer with zeros
316
317 // encode buffer
318 m_forceWriting = true; // makes sure that the following EncodeBuffer is really written into the stream
320 }
321 }
322
324 // Stores band value from given position bandPos into buffer m_value at position m_valuePos
325 // If buffer is full encode it to file
326 // It might throw an IOException.
327 void CEncoder::WriteValue(CSubband* band, int bandPos) {
330 }
331 DataT val = m_currentBlock->m_value[m_currentBlock->m_valuePos++] = band->GetData(bandPos);
332 uint32_t v = abs(val);
334 }
335
337 // Encode buffer and write data into stream.
338 // h contains buffer size and flag indicating end of tile.
339 // Encoding scheme: <wordLen>(16 bits) [ ROI ] data
340 // ROI ::= <bufferSize>(15 bits) <eofTile>(1 bit)
341 // It might throw an IOException.
343 ASSERT(m_currentBlock);
344#ifdef __PGFROISUPPORT__
345 ASSERT(m_roi && h.rbh.bufferSize <= BufferSize || h.rbh.bufferSize == BufferSize);
346#else
347 ASSERT(h.rbh.bufferSize == BufferSize);
348#endif
350
351 // macro block management
352 if (m_macroBlockLen == 1) {
355 } else {
356 // save last level index
357 int lastLevelIndex = m_currentBlock->m_lastLevelIndex;
358
360 // encode macro blocks
361 /*
362 volatile OSError error = NoError;
363 #ifdef LIBPGF_USE_OPENMP
364 #pragma omp parallel for ordered default(shared)
365 #endif
366 for (int i=0; i < m_lastMacroBlock; i++) {
367 if (error == NoError) {
368 m_macroBlocks[i]->BitplaneEncode();
369 #ifdef LIBPGF_USE_OPENMP
370 #pragma omp ordered
371 #endif
372 {
373 try {
374 WriteMacroBlock(m_macroBlocks[i]);
375 } catch (IOException& e) {
376 error = e.error;
377 }
378 delete m_macroBlocks[i]; m_macroBlocks[i] = 0;
379 }
380 }
381 }
382 if (error != NoError) ReturnWithError(error);
383 */
384#ifdef LIBPGF_USE_OPENMP
385#pragma omp parallel for default(shared) //no declared exceptions in next block
386#endif
387 for (int i = 0; i < m_lastMacroBlock; i++) {
389 }
390 for (int i = 0; i < m_lastMacroBlock; i++) {
392 }
393
394 // prepare for next round
395 m_forceWriting = false;
397 }
398 // re-initialize macro block
400 m_currentBlock->Init(lastLevelIndex);
401 }
402 }
403
405 // Write encoded macro block into stream.
406 // It might throw an IOException.
408 ASSERT(block);
409#ifdef __PGFROISUPPORT__
410 ROIBlockHeader h = block->m_header;
411#endif
412 uint16_t wordLen = uint16_t(NumberOfWords(block->m_codePos)); ASSERT(wordLen <= CodeBufferLen);
413 int count = sizeof(uint16_t);
414
415#ifdef TRACE
416 //uint32_t filePos = (uint32_t)m_stream->GetPos();
417 //printf("EncodeBuffer: %d\n", filePos);
418#endif
419
420#ifdef PGF_USE_BIG_ENDIAN
421 // write wordLen
422 uint16_t wl = __VAL(wordLen);
423 m_stream->Write(&count, &wl); ASSERT(count == sizeof(uint16_t));
424
425#ifdef __PGFROISUPPORT__
426 // write ROIBlockHeader
427 if (m_roi) {
428 count = sizeof(ROIBlockHeader);
429 h.val = __VAL(h.val);
430 m_stream->Write(&count, &h.val); ASSERT(count == sizeof(ROIBlockHeader));
431 }
432#endif // __PGFROISUPPORT__
433
434 // convert data
435 for (int i = 0; i < wordLen; i++) {
436 block->m_codeBuffer[i] = __VAL(block->m_codeBuffer[i]);
437 }
438#else
439 // write wordLen
440 m_stream->Write(&count, &wordLen); ASSERT(count == sizeof(uint16_t));
441
442#ifdef __PGFROISUPPORT__
443 // write ROIBlockHeader
444 if (m_roi) {
445 count = sizeof(ROIBlockHeader);
446 m_stream->Write(&count, &h.val); ASSERT(count == sizeof(ROIBlockHeader));
447 }
448#endif // __PGFROISUPPORT__
449#endif // PGF_USE_BIG_ENDIAN
450
451 // write encoded data into stream
452 count = wordLen*WordBytes;
453 m_stream->Write(&count, block->m_codeBuffer);
454
455 // store levelLength
456 if (m_levelLength) {
457 // store level length
458 // EncodeBuffer has been called after m_lastLevelIndex has been updated
459 ASSERT(m_currLevelIndex < m_nLevels);
462
463 }
464
465 // prepare for next buffer
467
468 // reset values
469 block->m_valuePos = 0;
470 block->m_maxAbsValue = 0;
471 }
472
474 // Encode buffer of given size using bit plane coding.
475 // A buffer contains bufferLen uint32_t values, thus, bufferSize bits per bit plane.
476 // Following coding scheme is used:
477 // Buffer ::= <nPlanes>(5 bits) foreach(plane i): Plane[i]
478 // Plane[i] ::= [ Sig1 | Sig2 ] [DWORD alignment] refBits
479 // Sig1 ::= 1 <codeLen>(15 bits) codedSigAndSignBits
480 // Sig2 ::= 0 <sigLen>(15 bits) [Sign1 | Sign2 ] [DWORD alignment] sigBits
481 // Sign1 ::= 1 <codeLen>(15 bits) codedSignBits
482 // Sign2 ::= 0 <signLen>(15 bits) [DWORD alignment] signBits
484 uint8_t nPlanes;
485 uint32_t sigLen, codeLen = 0, wordPos, refLen, signLen;
486 uint32_t sigBits[BufferLen] = { 0 };
487 uint32_t refBits[BufferLen] = { 0 };
488 uint32_t signBits[BufferLen] = { 0 };
489 uint32_t planeMask;
490 uint32_t bufferSize = m_header.rbh.bufferSize; ASSERT(bufferSize <= BufferSize);
491 bool useRL;
492
493#ifdef TRACE
494 //printf("which thread: %d\n", omp_get_thread_num());
495#endif
496
497 // clear significance vector
498 for (uint32_t k = 0; k < bufferSize; k++) {
499 m_sigFlagVector[k] = false;
500 }
501 m_sigFlagVector[bufferSize] = true; // sentinel
502
503 // clear output buffer
504 for (uint32_t k = 0; k < bufferSize; k++) {
505 m_codeBuffer[k] = 0;
506 }
507 m_codePos = 0;
508
509 // compute number of bit planes and split buffer into separate bit planes
510 nPlanes = NumberOfBitplanes();
511
512 // write number of bit planes to m_codeBuffer
513 // <nPlanes>
516
517 // loop through all bit planes
518 if (nPlanes == 0) nPlanes = MaxBitPlanes + 1;
519 planeMask = 1 << (nPlanes - 1);
520
521 for (int plane = nPlanes - 1; plane >= 0; plane--) {
522 // clear significant bitset
523 for (uint32_t k = 0; k < BufferLen; k++) {
524 sigBits[k] = 0;
525 }
526
527 // split bitplane in significant bitset and refinement bitset
528 sigLen = DecomposeBitplane(bufferSize, planeMask, m_codePos + RLblockSizeLen + 1, sigBits, refBits, signBits, signLen, codeLen);
529
530 if (sigLen > 0 && codeLen <= MaxCodeLen && codeLen < AlignWordPos(sigLen) + AlignWordPos(signLen) + 2*RLblockSizeLen) {
531 // set RL code bit
532 // <1><codeLen>
534
535 // write length codeLen to m_codeBuffer
537 m_codePos += RLblockSizeLen + codeLen;
538 } else {
539#ifdef TRACE
540 //printf("new\n");
541 //for (uint32_t i=0; i < bufferSize; i++) {
542 // printf("%s", (GetBit(sigBits, i)) ? "1" : "_");
543 // if (i%120 == 119) printf("\n");
544 //}
545 //printf("\n");
546#endif // TRACE
547
548 // run-length coding wasn't efficient enough
549 // we don't use RL coding for sigBits
550 // <0><sigLen>
552
553 // write length sigLen to m_codeBuffer
554 ASSERT(sigLen <= MaxCodeLen);
557
558 if (m_encoder->m_favorSpeed || signLen == 0) {
559 useRL = false;
560 } else {
561 // overwrite m_codeBuffer
562 useRL = true;
563 // run-length encode m_sign and append them to the m_codeBuffer
564 codeLen = RLESigns(m_codePos + RLblockSizeLen + 1, signBits, signLen);
565 }
566
567 if (useRL && codeLen <= MaxCodeLen && codeLen < signLen) {
568 // RL encoding of m_sign was efficient
569 // <1><codeLen><codedSignBits>_
570 // write RL code bit
572
573 // write codeLen to m_codeBuffer
575
576 // compute position of sigBits
577 wordPos = NumberOfWords(m_codePos + RLblockSizeLen + codeLen);
578 ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
579 } else {
580 // RL encoding of signBits wasn't efficient
581 // <0><signLen>_<signBits>_
582 // clear RL code bit
584
585 // write signLen to m_codeBuffer
586 ASSERT(signLen <= MaxCodeLen);
588
589 // write signBits to m_codeBuffer
591 ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
592 codeLen = NumberOfWords(signLen);
593
594 for (uint32_t k = 0; k < codeLen; k++) {
595 m_codeBuffer[wordPos++] = signBits[k];
596 }
597 }
598
599 // write sigBits
600 // <sigBits>_
601 ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
602 refLen = NumberOfWords(sigLen);
603
604 for (uint32_t k = 0; k < refLen; k++) {
605 m_codeBuffer[wordPos++] = sigBits[k];
606 }
607 m_codePos = wordPos << WordWidthLog;
608 }
609
610 // append refinement bitset (aligned to word boundary)
611 // _<refBits>
612 wordPos = NumberOfWords(m_codePos);
613 ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
614 refLen = NumberOfWords(bufferSize - sigLen);
615
616 for (uint32_t k = 0; k < refLen; k++) {
617 m_codeBuffer[wordPos++] = refBits[k];
618 }
619 m_codePos = wordPos << WordWidthLog;
620 planeMask >>= 1;
621 }
622 ASSERT(0 <= m_codePos && m_codePos <= CodeBufferBitLen);
623 }
624
626 // Split bitplane of length bufferSize into significant and refinement bitset
627 // returns length [bits] of significant bits
628 // input: bufferSize, planeMask, codePos
629 // output: sigBits, refBits, signBits, signLen [bits], codeLen [bits]
630 // RLE
631 // - Encode run of 2^k zeros by a single 0.
632 // - Encode run of count 0's followed by a 1 with codeword: 1<count>x
633 // - x is 0: if a positive sign is stored, otherwise 1
634 // - Store each bit in m_codeBuffer[codePos] and increment codePos.
635 uint32_t CEncoder::CMacroBlock::DecomposeBitplane(uint32_t bufferSize, uint32_t planeMask, uint32_t codePos, uint32_t* sigBits, uint32_t* refBits, uint32_t* signBits, uint32_t& signLen, uint32_t& codeLen) {
636 ASSERT(sigBits);
637 ASSERT(refBits);
638 ASSERT(signBits);
639 ASSERT(codePos < CodeBufferBitLen);
640
641 uint32_t sigPos = 0;
642 uint32_t valuePos = 0, valueEnd;
643 uint32_t refPos = 0;
644
645 // set output value
646 signLen = 0;
647
648 // prepare RLE of Sigs and Signs
649 const uint32_t outStartPos = codePos;
650 uint32_t k = 3;
651 uint32_t runlen = 1 << k; // = 2^k
652 uint32_t count = 0;
653
654 while (valuePos < bufferSize) {
655 // search next 1 in m_sigFlagVector using searching with sentinel
656 valueEnd = valuePos;
657 while (!m_sigFlagVector[valueEnd]) { valueEnd++; }
658
659 // search 1's in m_value[plane][valuePos..valueEnd)
660 // these 1's are significant bits
661 while (valuePos < valueEnd) {
662 if (GetBitAtPos(valuePos, planeMask)) {
663 // RLE encoding
664 // encode run of count 0's followed by a 1
665 // with codeword: 1<count>(signBits[signPos])
666 SetBit(m_codeBuffer, codePos++);
667 if (k > 0) {
668 SetValueBlock(m_codeBuffer, codePos, count, k);
669 codePos += k;
670
671 // adapt k (half the zero run-length)
672 k--;
673 runlen >>= 1;
674 }
675
676 // copy and write sign bit
677 if (m_value[valuePos] < 0) {
678 SetBit(signBits, signLen++);
679 SetBit(m_codeBuffer, codePos++);
680 } else {
681 ClearBit(signBits, signLen++);
682 ClearBit(m_codeBuffer, codePos++);
683 }
684
685 // write a 1 to sigBits
686 SetBit(sigBits, sigPos++);
687
688 // update m_sigFlagVector
689 m_sigFlagVector[valuePos] = true;
690
691 // prepare for next run
692 count = 0;
693 } else {
694 // RLE encoding
695 count++;
696 if (count == runlen) {
697 // encode run of 2^k zeros by a single 0
698 ClearBit(m_codeBuffer, codePos++);
699 // adapt k (double the zero run-length)
700 if (k < WordWidth) {
701 k++;
702 runlen <<= 1;
703 }
704
705 // prepare for next run
706 count = 0;
707 }
708
709 // write 0 to sigBits
710 sigPos++;
711 }
712 valuePos++;
713 }
714 // refinement bit
715 if (valuePos < bufferSize) {
716 // write one refinement bit
717 if (GetBitAtPos(valuePos++, planeMask)) {
718 SetBit(refBits, refPos);
719 } else {
720 ClearBit(refBits, refPos);
721 }
722 refPos++;
723 }
724 }
725 // RLE encoding of the rest of the plane
726 // encode run of count 0's followed by a 1
727 // with codeword: 1<count>(signBits[signPos])
728 SetBit(m_codeBuffer, codePos++);
729 if (k > 0) {
730 SetValueBlock(m_codeBuffer, codePos, count, k);
731 codePos += k;
732 }
733 // write dmmy sign bit
734 SetBit(m_codeBuffer, codePos++);
735
736 // write word filler zeros
737
738 ASSERT(sigPos <= bufferSize);
739 ASSERT(refPos <= bufferSize);
740 ASSERT(signLen <= bufferSize);
741 ASSERT(valuePos == bufferSize);
742 ASSERT(codePos >= outStartPos && codePos < CodeBufferBitLen);
743 codeLen = codePos - outStartPos;
744
745 return sigPos;
746 }
747
748
750 // Compute number of bit planes needed
752 uint8_t cnt = 0;
753
754 // determine number of bitplanes for max value
755 if (m_maxAbsValue > 0) {
756 while (m_maxAbsValue > 0) {
757 m_maxAbsValue >>= 1; cnt++;
758 }
759 if (cnt == MaxBitPlanes + 1) cnt = 0;
760 // end cs
761 ASSERT(cnt <= MaxBitPlanes);
762 ASSERT((cnt >> MaxBitPlanesLog) == 0);
763 return cnt;
764 } else {
765 return 1;
766 }
767 }
768
770 // Adaptive Run-Length encoder for long sequences of ones.
771 // Returns length of output in bits.
772 // - Encode run of 2^k ones by a single 1.
773 // - Encode run of count 1's followed by a 0 with codeword: 0<count>.
774 // - Store each bit in m_codeBuffer[codePos] and increment codePos.
775 uint32_t CEncoder::CMacroBlock::RLESigns(uint32_t codePos, uint32_t* signBits, uint32_t signLen) {
776 ASSERT(signBits);
777 ASSERT(0 <= codePos && codePos < CodeBufferBitLen);
778 ASSERT(0 < signLen && signLen <= BufferSize);
779
780 const uint32_t outStartPos = codePos;
781 uint32_t k = 0;
782 uint32_t runlen = 1 << k; // = 2^k
783 uint32_t count = 0;
784 uint32_t signPos = 0;
785
786 while (signPos < signLen) {
787 // search next 0 in signBits starting at position signPos
788 count = SeekBit1Range(signBits, signPos, __min(runlen, signLen - signPos));
789 // count 1's found
790 if (count == runlen) {
791 // encode run of 2^k ones by a single 1
792 signPos += count;
793 SetBit(m_codeBuffer, codePos++);
794 // adapt k (double the 1's run-length)
795 if (k < WordWidth) {
796 k++;
797 runlen <<= 1;
798 }
799 } else {
800 // encode run of count 1's followed by a 0
801 // with codeword: 0(count)
802 signPos += count + 1;
803 ClearBit(m_codeBuffer, codePos++);
804 if (k > 0) {
805 SetValueBlock(m_codeBuffer, codePos, count, k);
806 codePos += k;
807 }
808 // adapt k (half the 1's run-length)
809 if (k > 0) {
810 k--;
811 runlen >>= 1;
812 }
813 }
814 }
815 ASSERT(signPos == signLen || signPos == signLen + 1);
816 ASSERT(codePos >= outStartPos && codePos < CodeBufferBitLen);
817 return codePos - outStartPos;
818 }
819
821#ifdef TRACE
822 void CEncoder::DumpBuffer() const {
823 //printf("\nDump\n");
824 //for (uint32_t i=0; i < BufferSize; i++) {
825 // printf("%d", m_value[i]);
826 //}
827 //printf("\n");
828 }
829#endif //TRACE
830
831}
PGF encoder class.
constexpr uint32_t WordWidth
WordBytes*8.
Definition PGFplatform.h:73
constexpr uint32_t WordBytes
sizeof(uint32_t)
Definition PGFplatform.h:76
constexpr uint32_t WordWidthLog
ld of WordWidth
Definition PGFplatform.h:74
constexpr uint8_t ImageModeIndexedColor
#define __PGFROISUPPORT__
Definition PGFplatform.h:60
#define __VAL(x)
#define __min(x, y)
A macro block is an encoding unit of fixed size (uncoded).
Definition Encoder.h:46
uint32_t m_valuePos
current buffer position
Definition Encoder.h:82
uint32_t RLESigns(uint32_t codePos, uint32_t *signBits, uint32_t signLen)
Definition Encoder.cpp:775
uint32_t DecomposeBitplane(uint32_t bufferSize, uint32_t planeMask, uint32_t codePos, uint32_t *sigBits, uint32_t *refBits, uint32_t *signBits, uint32_t &signLen, uint32_t &codeLen)
Definition Encoder.cpp:635
uint32_t m_codePos
current position in encoded bitstream
Definition Encoder.h:84
uint32_t m_maxAbsValue
maximum absolute coefficient in each buffer
Definition Encoder.h:83
bool GetBitAtPos(uint32_t pos, uint32_t planeMask) const
Definition Encoder.h:91
bool m_sigFlagVector[BufferSize+1]
Definition Encoder.h:94
void Init(int lastLevelIndex)
Definition Encoder.h:66
int m_lastLevelIndex
index of last encoded level: [0, nLevels); used because a level-end can occur before a buffer is full
Definition Encoder.h:85
ROIBlockHeader m_header
block header
Definition Encoder.h:81
DataT m_value[BufferSize]
input buffer of values with index m_valuePos
Definition Encoder.h:79
CMacroBlock(CEncoder *encoder)
Definition Encoder.h:51
uint32_t m_codeBuffer[CodeBufferLen]
output buffer for encoded bitstream
Definition Encoder.h:80
CEncoder(CPGFStream *stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader &postHeader, uint64_t &userDataPos, bool useOMP)
Definition Encoder.cpp:71
CPGFStream * m_stream
output PMF stream
Definition Encoder.h:208
void EncodeBuffer(ROIBlockHeader h)
Definition Encoder.cpp:342
~CEncoder()
Destructor.
Definition Encoder.cpp:148
uint32_t WriteLevelLength(uint32_t *&levelLength)
Definition Encoder.cpp:178
uint32_t * m_levelLength
temporary saves the level index
Definition Encoder.h:218
void SetStreamPosToStart()
Resets stream position to beginning of PGF pre-header.
Definition Encoder.h:183
uint8_t m_nLevels
number of levels
Definition Encoder.h:220
uint32_t UpdateLevelLength()
Definition Encoder.cpp:203
void WriteMacroBlock(CMacroBlock *block)
Definition Encoder.cpp:407
int m_lastMacroBlock
array index of the last created macro block
Definition Encoder.h:215
void WriteValue(CSubband *band, int bandPos)
Definition Encoder.cpp:327
void Partition(CSubband *band, int width, int height, int startPos, int pitch)
Definition Encoder.cpp:247
int m_macroBlockLen
array length
Definition Encoder.h:214
uint64_t m_bufferStartPos
stream position of encoded buffer
Definition Encoder.h:211
void UpdatePostHeaderSize(PGFPreHeader preHeader)
Definition Encoder.cpp:161
int64_t ComputeBufferLength() const
Definition Encoder.h:174
uint64_t m_startPosition
stream position of PGF start (PreHeader)
Definition Encoder.h:209
bool m_forceWriting
all macro blocks have to be written into the stream
Definition Encoder.h:222
uint64_t m_levelLengthPos
stream position of Metadata
Definition Encoder.h:210
CMacroBlock * m_currentBlock
current macro block (used by main thread)
Definition Encoder.h:216
CMacroBlock ** m_macroBlocks
array of macroblocks
Definition Encoder.h:213
int m_currLevelIndex
counts where (=index) to save next value
Definition Encoder.h:219
bool m_favorSpeed
favor speed over size
Definition Encoder.h:221
void SetBufferStartPos()
Save current stream position as beginning of current level.
Definition Encoder.h:187
Abstract stream base class.
Definition PGFstream.h:39
virtual void Write(int *count, void *buffer)=0
virtual uint64_t GetPos() const =0
virtual void SetPos(short posMode, int64_t posOff)=0
Wavelet channel class.
Definition Subband.h:42
DataT GetData(uint32_t pos) const
Definition Subband.h:113
Definition PGFimage.h:33
void ClearBit(uint32_t *stream, uint32_t pos)
Definition BitStream.h:70
constexpr int MaxBitPlanes
maximum number of bit planes of m_value: 32 minus sign bit
Definition PGFtypes.h:91
constexpr size_t HeaderSize
Definition PGFtypes.h:379
constexpr uint32_t CodeBufferBitLen
max number of bits in m_codeBuffer
Definition Decoder.cpp:59
constexpr int LinBlockSize
side length of a coefficient block in a HH or LL subband
Definition PGFtypes.h:88
void SetBit(uint32_t *stream, uint32_t pos)
Definition BitStream.h:62
constexpr size_t PreHeaderSize
Definition PGFtypes.h:378
void SetValueBlock(uint32_t *stream, uint32_t pos, uint32_t val, uint32_t k)
Definition BitStream.h:116
uint32_t AlignWordPos(uint32_t pos)
Definition BitStream.h:328
int32_t DataT
Definition PGFtypes.h:367
constexpr int RLblockSizeLen
block size length (< 16): ld(BufferSize) < RLblockSizeLen <= 2*ld(BufferSize)
Definition PGFtypes.h:87
constexpr uint32_t CodeBufferLen
number of words in code buffer (CodeBufferLen > BufferLen)
Definition PGFtypes.h:86
constexpr size_t DataTSize
Definition PGFtypes.h:381
constexpr int MaxBitPlanesLog
number of bits to code the maximum number of bit planes (in 32 or 16 bit mode)
Definition PGFtypes.h:95
constexpr uint32_t MaxCodeLen
max length of RL encoded block
Definition Decoder.cpp:60
uint32_t NumberOfWords(uint32_t pos)
Definition BitStream.h:337
constexpr uint32_t BufferLen
number of words per buffer
Definition PGFtypes.h:85
constexpr size_t ColorTableSize
Definition PGFtypes.h:380
constexpr uint32_t BufferSize
must be a multiple of WordWidth, BufferSize <= UINT16_MAX
Definition PGFtypes.h:84
uint32_t SeekBit1Range(uint32_t *stream, uint32_t pos, uint32_t len)
Definition BitStream.h:249
PGF header.
Definition PGFtypes.h:169
uint8_t mode
image mode according to Adobe's image modes
Definition PGFtypes.h:176
uint32_t width
image width in pixels
Definition PGFtypes.h:170
uint32_t height
image height in pixels
Definition PGFtypes.h:171
Optional PGF post-header.
Definition PGFtypes.h:199
uint32_t userDataLen
user data size in bytes (not part of file header)
Definition PGFtypes.h:202
RGBQUAD clut[ColorTableLen]
color table for indexed color images (optional part of file header)
Definition PGFtypes.h:200
uint8_t * userData
user data of size userDataLen (optional part of file header)
Definition PGFtypes.h:201
PGF pre-header.
Definition PGFtypes.h:136
uint32_t hSize
total size of PGFHeader, [ColorTable], and [UserData] in bytes (since Version 6: 4 Bytes)
Definition PGFtypes.h:137
uint16_t bufferSize
number of uncoded uint32_t values in a block
Definition PGFtypes.h:219
Block header used with ROI coding scheme.
Definition PGFtypes.h:211
struct pgf::ROIBlockHeader::RBH rbh
ROI block header.
ROIBlockHeader(uint16_t v)
Definition PGFtypes.h:227