libpgf 7.26.45
PGF - Progressive Graphics File
Loading...
Searching...
No Matches
pgf::CEncoder Class Reference

PGF encoder. More...

#include <Encoder.h>

Classes

class  CMacroBlock
 A macro block is an encoding unit of fixed size (uncoded). More...

Public Member Functions

 CEncoder (CPGFStream *stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader &postHeader, uint64_t &userDataPos, bool useOMP)
 ~CEncoder ()
 Destructor.
void FavorSpeedOverSize ()
 Encoder favors speed over compression size.
void Flush ()
void UpdatePostHeaderSize (PGFPreHeader preHeader)
uint32_t WriteLevelLength (uint32_t *&levelLength)
uint32_t UpdateLevelLength ()
void Partition (CSubband *band, int width, int height, int startPos, int pitch)
void SetEncodedLevel (int currentLevel)
void WriteValue (CSubband *band, int bandPos)
int64_t ComputeHeaderLength () const
int64_t ComputeBufferLength () const
int64_t ComputeOffset () const
void SetStreamPosToStart ()
 Resets stream position to beginning of PGF pre-header.
void SetBufferStartPos ()
 Save current stream position as beginning of current level.

Private Member Functions

void EncodeBuffer (ROIBlockHeader h)
void WriteMacroBlock (CMacroBlock *block)

Private Attributes

CPGFStream * m_stream
 output PMF stream
uint64_t m_startPosition
 stream position of PGF start (PreHeader)
uint64_t m_levelLengthPos
 stream position of Metadata
uint64_t m_bufferStartPos
 stream position of encoded buffer
CMacroBlock ** m_macroBlocks
 array of macroblocks
int m_macroBlockLen
 array length
int m_lastMacroBlock
 array index of the last created macro block
CMacroBlock * m_currentBlock
 current macro block (used by main thread)
uint32_t * m_levelLength
 temporary saves the level index
int m_currLevelIndex
 counts where (=index) to save next value
uint8_t m_nLevels
 number of levels
bool m_favorSpeed
 favor speed over size
bool m_forceWriting
 all macro blocks have to be written into the stream

Detailed Description

PGF encoder.

PGF encoder class.

Author
C. Stamm

Definition at line 41 of file Encoder.h.

Constructor & Destructor Documentation

◆ CEncoder()

pgf::CEncoder::CEncoder ( CPGFStream * stream,
PGFPreHeader preHeader,
PGFHeader header,
const PGFPostHeader & postHeader,
uint64_t & userDataPos,
bool useOMP )

Write pre-header, header, post-Header, and levelLength. It might throw an IOException.

Parameters
streamA PGF stream
preHeaderA already filled in PGF pre-header
headerAn already filled in PGF header
postHeader[in] An already filled in PGF post-header (containing color table, user data, ...)
userDataPos[out] File position of user data
useOMPIf true, then the encoder will use multi-threading based on openMP

Write pre-header, header, postHeader, and levelLength. It might throw an IOException.

Parameters
streamA PGF stream
preHeaderA already filled in PGF pre-header
headerAn already filled in PGF header
postHeader[in] An already filled in PGF post-header (containing color table, user data, ...)
userDataPos[out] File position of user data
useOMPIf true, then the encoder will use multi-threading based on openMP

Definition at line 71 of file Encoder.cpp.

72 : m_stream(stream)
75 , m_nLevels(header.nLevels)
76 , m_favorSpeed(false)
77 , m_forceWriting(false)
78#ifdef __PGFROISUPPORT__
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 }
constexpr uint8_t ImageModeIndexedColor
#define __VAL(x)
A macro block is an encoding unit of fixed size (uncoded).
Definition Encoder.h:46
CMacroBlock(CEncoder *encoder)
Definition Encoder.h:51
CPGFStream * m_stream
output PMF stream
Definition Encoder.h:208
uint32_t * m_levelLength
temporary saves the level index
Definition Encoder.h:218
uint8_t m_nLevels
number of levels
Definition Encoder.h:220
int m_lastMacroBlock
array index of the last created macro block
Definition Encoder.h:215
int m_macroBlockLen
array length
Definition Encoder.h:214
uint64_t m_bufferStartPos
stream position of encoded buffer
Definition Encoder.h:211
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
virtual void Write(int *count, void *buffer)=0
virtual uint64_t GetPos() const =0
virtual void SetPos(short posMode, int64_t posOff)=0
constexpr size_t HeaderSize
Definition PGFtypes.h:379
constexpr size_t PreHeaderSize
Definition PGFtypes.h:378
constexpr size_t ColorTableSize
Definition PGFtypes.h:380

◆ ~CEncoder()

pgf::CEncoder::~CEncoder ( )

Destructor.

Definition at line 148 of file Encoder.cpp.

148 {
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 }

Member Function Documentation

◆ ComputeBufferLength()

int64_t pgf::CEncoder::ComputeBufferLength ( ) const
inline

Compute stream length of encoded buffer.

Returns
encoded buffer length

Definition at line 174 of file Encoder.h.

174{ return m_stream->GetPos() - m_bufferStartPos; }

◆ ComputeHeaderLength()

int64_t pgf::CEncoder::ComputeHeaderLength ( ) const
inline

Compute stream length of header.

Returns
header length

Definition at line 169 of file Encoder.h.

◆ ComputeOffset()

int64_t pgf::CEncoder::ComputeOffset ( ) const
inline

Compute file offset between real and expected levelLength position.

Returns
file offset

Definition at line 179 of file Encoder.h.

179{ return m_stream->GetPos() - m_levelLengthPos; }

◆ EncodeBuffer()

void pgf::CEncoder::EncodeBuffer ( ROIBlockHeader h)
private

Definition at line 342 of file Encoder.cpp.

342 {
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 }
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
void WriteMacroBlock(CMacroBlock *block)
Definition Encoder.cpp:407
constexpr uint32_t BufferSize
must be a multiple of WordWidth, BufferSize <= UINT16_MAX
Definition PGFtypes.h:84

◆ FavorSpeedOverSize()

void pgf::CEncoder::FavorSpeedOverSize ( )
inline

Encoder favors speed over compression size.

Definition at line 116 of file Encoder.h.

116{ m_favorSpeed = true; }

◆ Flush()

void pgf::CEncoder::Flush ( )

Pad buffer with zeros and encode buffer. It might throw an IOException.

Definition at line 311 of file Encoder.cpp.

311 {
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 }
uint32_t m_valuePos
current buffer position
Definition Encoder.h:82
DataT m_value[BufferSize]
input buffer of values with index m_valuePos
Definition Encoder.h:79
void EncodeBuffer(ROIBlockHeader h)
Definition Encoder.cpp:342
constexpr size_t DataTSize
Definition PGFtypes.h:381
ROIBlockHeader(uint16_t v)
Definition PGFtypes.h:227

◆ Partition()

void pgf::CEncoder::Partition ( CSubband * band,
int width,
int height,
int startPos,
int pitch )

Partitions a rectangular region of a given subband. Partitioning scheme: The plane is partitioned in squares of side length LinBlockSize. Write wavelet coefficients from subband into the input buffer of a macro block. It might throw an IOException.

Parameters
bandA subband
widthThe width of the rectangle
heightThe height of the rectangle
startPosThe absolute subband position of the top left corner of the rectangular region
pitchThe number of bytes in row of the subband

Definition at line 247 of file Encoder.cpp.

247 {
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 }
void WriteValue(CSubband *band, int bandPos)
Definition Encoder.cpp:327
constexpr int LinBlockSize
side length of a coefficient block in a HH or LL subband
Definition PGFtypes.h:88

◆ SetBufferStartPos()

void pgf::CEncoder::SetBufferStartPos ( )
inline

Save current stream position as beginning of current level.

Definition at line 187 of file Encoder.h.

◆ SetEncodedLevel()

void pgf::CEncoder::SetEncodedLevel ( int currentLevel)
inline

Informs the encoder about the encoded level.

Parameters
currentLevelencoded level [0, nLevels)

Definition at line 157 of file Encoder.h.

157{ ASSERT(currentLevel >= 0); m_currentBlock->m_lastLevelIndex = m_nLevels - currentLevel - 1; m_forceWriting = true; }

◆ SetStreamPosToStart()

void pgf::CEncoder::SetStreamPosToStart ( )
inline

Resets stream position to beginning of PGF pre-header.

Definition at line 183 of file Encoder.h.

183{ ASSERT(m_stream); m_stream->SetPos(FSFromStart, m_startPosition); }

◆ UpdateLevelLength()

uint32_t pgf::CEncoder::UpdateLevelLength ( )

Write new levelLength into stream. It might throw an IOException.

Returns
Written image bytes.

Definition at line 203 of file Encoder.cpp.

203 {
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 }
constexpr uint32_t WordBytes
sizeof(uint32_t)
Definition PGFplatform.h:76

◆ UpdatePostHeaderSize()

void pgf::CEncoder::UpdatePostHeaderSize ( PGFPreHeader preHeader)

Increase post-header size and write new size into stream.

Parameters
preHeaderAn already filled in PGF pre-header It might throw an IOException.

Definition at line 161 of file Encoder.cpp.

161 {
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 }
void SetStreamPosToStart()
Resets stream position to beginning of PGF pre-header.
Definition Encoder.h:183

◆ WriteLevelLength()

uint32_t pgf::CEncoder::WriteLevelLength ( uint32_t *& levelLength)

Create level length data structure and write a place holder into stream. It might throw an IOException.

Parameters
levelLengthA reference to an integer array, large enough to save the relative file positions of all PGF levels
Returns
number of bytes written into stream

Definition at line 178 of file Encoder.cpp.

178 {
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 }
void SetBufferStartPos()
Save current stream position as beginning of current level.
Definition Encoder.h:187

◆ WriteMacroBlock()

void pgf::CEncoder::WriteMacroBlock ( CMacroBlock * block)
private

Definition at line 407 of file Encoder.cpp.

407 {
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);
461 m_currLevelIndex = block->m_lastLevelIndex + 1;
462
463 }
464
465 // prepare for next buffer
467
468 // reset values
469 block->m_valuePos = 0;
470 block->m_maxAbsValue = 0;
471 }
int64_t ComputeBufferLength() const
Definition Encoder.h:174
constexpr uint32_t CodeBufferLen
number of words in code buffer (CodeBufferLen > BufferLen)
Definition PGFtypes.h:86
uint32_t NumberOfWords(uint32_t pos)
Definition BitStream.h:337

◆ WriteValue()

void pgf::CEncoder::WriteValue ( CSubband * band,
int bandPos )

Write a single value into subband at given position. It might throw an IOException.

Parameters
bandA subband
bandPosA valid position in subband band

Definition at line 327 of file Encoder.cpp.

327 {
330 }
331 DataT val = m_currentBlock->m_value[m_currentBlock->m_valuePos++] = band->GetData(bandPos);
332 uint32_t v = abs(val);
334 }
uint32_t m_maxAbsValue
maximum absolute coefficient in each buffer
Definition Encoder.h:83
int32_t DataT
Definition PGFtypes.h:367

Member Data Documentation

◆ m_bufferStartPos

uint64_t pgf::CEncoder::m_bufferStartPos
private

stream position of encoded buffer

Definition at line 211 of file Encoder.h.

◆ m_currentBlock

CMacroBlock* pgf::CEncoder::m_currentBlock
private

current macro block (used by main thread)

Definition at line 216 of file Encoder.h.

◆ m_currLevelIndex

int pgf::CEncoder::m_currLevelIndex
private

counts where (=index) to save next value

Definition at line 219 of file Encoder.h.

◆ m_favorSpeed

bool pgf::CEncoder::m_favorSpeed
private

favor speed over size

Definition at line 221 of file Encoder.h.

◆ m_forceWriting

bool pgf::CEncoder::m_forceWriting
private

all macro blocks have to be written into the stream

Definition at line 222 of file Encoder.h.

◆ m_lastMacroBlock

int pgf::CEncoder::m_lastMacroBlock
private

array index of the last created macro block

Definition at line 215 of file Encoder.h.

◆ m_levelLength

uint32_t* pgf::CEncoder::m_levelLength
private

temporary saves the level index

Definition at line 218 of file Encoder.h.

◆ m_levelLengthPos

uint64_t pgf::CEncoder::m_levelLengthPos
private

stream position of Metadata

Definition at line 210 of file Encoder.h.

◆ m_macroBlockLen

int pgf::CEncoder::m_macroBlockLen
private

array length

Definition at line 214 of file Encoder.h.

◆ m_macroBlocks

CMacroBlock** pgf::CEncoder::m_macroBlocks
private

array of macroblocks

Definition at line 213 of file Encoder.h.

◆ m_nLevels

uint8_t pgf::CEncoder::m_nLevels
private

number of levels

Definition at line 220 of file Encoder.h.

◆ m_startPosition

uint64_t pgf::CEncoder::m_startPosition
private

stream position of PGF start (PreHeader)

Definition at line 209 of file Encoder.h.

◆ m_stream

CPGFStream* pgf::CEncoder::m_stream
private

output PMF stream

Definition at line 208 of file Encoder.h.


The documentation for this class was generated from the following files: