libpgf 7.26.45
PGF - Progressive Graphics File
Loading...
Searching...
No Matches
PGFimage.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 "PGFimage.h"
30#include "Decoder.h"
31#include "Encoder.h"
32#include "BitStream.h"
33#include <cmath>
34#include <cstring>
35
36namespace pgf {
37 constexpr DataT YUVoffset4 = 8; // 2^3
38 constexpr DataT YUVoffset6 = 32; // 2^5
39 constexpr DataT YUVoffset8 = 128; // 2^7
40 constexpr DataT YUVoffset16 = 32768; // 2^15
41 //constexpr DataT YUVoffset31 = 1073741824; // 2^30
42
44 // global methods and variables
45 #ifdef NEXCEPTIONS
46 OSError _PGF_Error_;
47
48 OSError GetLastPGFError() {
49 OSError tmp = _PGF_Error_;
50 _PGF_Error_ = NoError;
51 return tmp;
52 }
53 #endif
54
55 #ifdef _DEBUG
56 // allows RGB and RGBA image visualization inside Visual Studio Debugger
57 struct DebugBGRImage {
58 int width, height, pitch;
59 uint8_t *data;
60 } roiimage;
61 #endif
62
64 // Standard constructor
66 Init();
67 }
68
71 // init pointers
72 m_decoder = nullptr;
73 m_encoder = nullptr;
74 m_levelLength = nullptr;
75
76 // init members
77 #ifdef __PGFROISUPPORT__
79 #endif
81 m_quant = 0;
82 m_userDataPos = 0;
83 m_downsample = false;
85 m_useOMPinEncoder = true;
86 m_useOMPinDecoder = true;
87 m_cb = nullptr;
88 m_cbArg = nullptr;
90 m_percent = 0;
92
93 // init preHeader
94 memcpy(m_preHeader.magic, PGFMagic, 3);
97
98 // init postHeader
99 m_postHeader.userData = nullptr;
102
103 // init header
104 m_header.channels = 0;
105
106 // init channels
107 for (int i = 0; i < MaxChannels; i++) {
108 m_channel[i] = nullptr;
109 m_wtChannel[i] = nullptr;
110 }
111
112 // set image width and height
113 for (int i = 0; i < MaxChannels; i++) {
114 m_width[0] = 0;
115 m_height[0] = 0;
116 }
117 }
118
120 // Destructor: Destroy internal data structures.
122 m_currentLevel = -100; // unusual value used as marker in Destroy()
123 Destroy();
124 }
125
127 // Destroy internal data structures. Object state after this is the same as after CPGFImage().
129 for (int i = 0; i < MaxChannels; i++) {
130 delete m_wtChannel[i]; // also deletes m_channel
131 }
132 delete[] m_postHeader.userData;
133 delete[] m_levelLength;
134 delete m_decoder;
135 delete m_encoder;
136
137 if (m_currentLevel != -100) Init();
138 }
139
141 // Open a PGF image at current stream position: read pre-header, header, levelLength, and ckeck image type.
142 // Precondition: The stream has been opened for reading.
143 // It might throw an IOException.
144 // @param stream A PGF stream
146 ASSERT(stream);
147
148 // create decoder and read PGFPreHeader PGFHeader PGFPostHeader LevelLengths
151
152 if (m_header.nLevels > MaxLevel) ReturnWithError(FormatCannotRead);
153
154 // set current level
156
157 // set image width and height
160
161 // complete header
162 if (!CompleteHeader()) ReturnWithError(FormatCannotRead);
163
164 // interpret quant parameter
173 m_downsample = true;
175 } else {
176 m_downsample = false;
178 }
179
180 // set channel dimensions (chrominance is subsampled by factor 2)
181 if (m_downsample) {
182 for (int i=1; i < m_header.channels; i++) {
183 m_width[i] = (m_width[0] + 1) >> 1;
184 m_height[i] = (m_height[0] + 1) >> 1;
185 }
186 } else {
187 for (int i=1; i < m_header.channels; i++) {
188 m_width[i] = m_width[0];
189 m_height[i] = m_height[0];
190 }
191 }
192
193 if (m_header.nLevels > 0) {
194 // init wavelet subbands
195 for (int i=0; i < m_header.channels; i++) {
197 }
198
199 // used in Read when Absolute
200 m_percent = pow(0.25, m_header.nLevels);
201
202 } else {
203 // very small image: we don't use DWT and encoding
204
205 // read channels
206 for (int c=0; c < m_header.channels; c++) {
207 const uint32_t size = m_width[c]*m_height[c];
208 m_channel[c] = new(std::nothrow) DataT[size];
209 if (!m_channel[c]) ReturnWithError(InsufficientMemory);
210
211 // read channel data from stream
212 for (uint32_t i=0; i < size; i++) {
213 int count = DataTSize;
214 stream->Read(&count, &m_channel[c][i]);
215 if (count != DataTSize) ReturnWithError(MissingData);
216 }
217 }
218 }
219 }
220
223 // set current codec version
225
227 // undefined mode
228 switch(m_header.bpp) {
229 case 1: m_header.mode = ImageModeBitmap; break;
230 case 8: m_header.mode = ImageModeGrayScale; break;
231 case 12: m_header.mode = ImageModeRGB12; break;
232 case 16: m_header.mode = ImageModeRGB16; break;
233 case 24: m_header.mode = ImageModeRGBColor; break;
234 case 32: m_header.mode = ImageModeRGBA; break;
235 case 48: m_header.mode = ImageModeRGB48; break;
236 default: m_header.mode = ImageModeRGBColor; break;
237 }
238 }
239 if (!m_header.bpp) {
240 // undefined bpp
241 switch(m_header.mode) {
242 case ImageModeBitmap:
243 m_header.bpp = 1;
244 break;
247 m_header.bpp = 8;
248 break;
249 case ImageModeRGB12:
250 m_header.bpp = 12;
251 break;
252 case ImageModeRGB16:
253 case ImageModeGray16:
254 m_header.bpp = 16;
255 break;
258 m_header.bpp = 24;
259 break;
260 case ImageModeRGBA:
262 case ImageModeGray32:
263 m_header.bpp = 32;
264 break;
265 case ImageModeRGB48:
266 case ImageModeLab48:
267 m_header.bpp = 48;
268 break;
269 case ImageModeCMYK64:
270 m_header.bpp = 64;
271 break;
272 default:
273 ASSERT(false);
274 m_header.bpp = 24;
275 }
276 }
277 if (m_header.mode == ImageModeRGBColor && m_header.bpp == 32) {
278 // change mode
280 }
281 if (m_header.mode == ImageModeBitmap && m_header.bpp != 1) return false;
282 if (m_header.mode == ImageModeIndexedColor && m_header.bpp != 8) return false;
283 if (m_header.mode == ImageModeGrayScale && m_header.bpp != 8) return false;
284 if (m_header.mode == ImageModeGray16 && m_header.bpp != 16) return false;
285 if (m_header.mode == ImageModeGray32 && m_header.bpp != 32) return false;
286 if (m_header.mode == ImageModeRGBColor && m_header.bpp != 24) return false;
287 if (m_header.mode == ImageModeRGBA && m_header.bpp != 32) return false;
288 if (m_header.mode == ImageModeRGB12 && m_header.bpp != 12) return false;
289 if (m_header.mode == ImageModeRGB16 && m_header.bpp != 16) return false;
290 if (m_header.mode == ImageModeRGB48 && m_header.bpp != 48) return false;
291 if (m_header.mode == ImageModeLabColor && m_header.bpp != 24) return false;
292 if (m_header.mode == ImageModeLab48 && m_header.bpp != 48) return false;
293 if (m_header.mode == ImageModeCMYKColor && m_header.bpp != 32) return false;
294 if (m_header.mode == ImageModeCMYK64 && m_header.bpp != 64) return false;
295
296 // set number of channels
297 if (!m_header.channels) {
298 switch(m_header.mode) {
299 case ImageModeBitmap:
302 case ImageModeGray16:
303 case ImageModeGray32:
304 m_header.channels = 1;
305 break;
307 case ImageModeRGB12:
308 case ImageModeRGB16:
309 case ImageModeRGB48:
311 case ImageModeLab48:
312 m_header.channels = 3;
313 break;
314 case ImageModeRGBA:
316 case ImageModeCMYK64:
317 m_header.channels = 4;
318 break;
319 default:
320 return false;
321 }
322 }
323
324 // store used bits per channel
325 uint8_t bpc = m_header.bpp/m_header.channels;
326 if (bpc > 31) bpc = 31;
329 }
330
331 return true;
332 }
333
341 const uint8_t* CPGFImage::GetUserData(uint32_t& cachedSize, uint32_t* pTotalSize /*= nullptr*/) const {
342 cachedSize = m_postHeader.cachedUserDataLen;
343 if (pTotalSize) *pTotalSize = m_postHeader.userDataLen;
344 return m_postHeader.userData;
345 }
346
352 void CPGFImage::Reconstruct(int level /*= 0*/) {
353 if (m_header.nLevels == 0) {
354 // image didn't use wavelet transform
355 if (level == 0) {
356 for (int i=0; i < m_header.channels; i++) {
357 ASSERT(m_wtChannel[i]);
359 }
360 }
361 } else {
362 int currentLevel = m_header.nLevels;
363
364 #ifdef __PGFROISUPPORT__
365 if (ROIisSupported()) {
366 // enable ROI reading
368 }
369 #endif
370
371 while (currentLevel > level) {
372 for (int i=0; i < m_header.channels; i++) {
373 ASSERT(m_wtChannel[i]);
374 // dequantize subbands
375 if (currentLevel == m_header.nLevels) {
376 // last level also has LL band
378 }
382
383 // inverse transform from m_wtChannel to m_channel
384 OSError err = m_wtChannel[i]->InverseTransform(currentLevel, &m_width[i], &m_height[i], &m_channel[i]);
385 if (err != NoError) ReturnWithError(err);
386 ASSERT(m_channel[i]);
387 }
388
389 currentLevel--;
390 }
391 }
392 }
393
395 // Read and decode some levels of a PGF image at current stream position.
396 // A PGF image is structered in levels, numbered between 0 and Levels() - 1.
397 // Each level can be seen as a single image, containing the same content
398 // as all other levels, but in a different size (width, height).
399 // The image size at level i is double the size (width, height) of the image at level i+1.
400 // The image at level 0 contains the original size.
401 // Precondition: The PGF image has been opened with a call of Open(...).
402 // It might throw an IOException.
403 // @param level The image level of the resulting image in the internal image buffer.
404 // @param cb A pointer to a callback procedure. The procedure is called after reading a single level. If cb returns true, then it stops proceeding.
405 // @param data Data Pointer to C++ class container to host callback procedure.
406 void CPGFImage::Read(int level /*= 0*/, CallbackPtr cb /*= nullptr*/, void *data /*=nullptr*/) {
407 ASSERT((level >= 0 && level < m_header.nLevels) || m_header.nLevels == 0); // m_header.nLevels == 0: image didn't use wavelet transform
408 ASSERT(m_decoder);
409
410 #ifdef __PGFROISUPPORT__
411 if (ROIisSupported() && m_header.nLevels > 0) {
412 // new encoding scheme supporting ROI
413 PGFRect rect(0, 0, m_header.width, m_header.height);
414 Read(rect, level, cb, data);
415 return;
416 }
417 #endif
418
419 if (m_header.nLevels == 0) {
420 if (level == 0) {
421 // the data has already been read during open
422 // now update progress
423 if (cb) {
424 if ((*cb)(1.0, true, data)) ReturnWithError(EscapePressed);
425 }
426 }
427 } else {
428 const int levelDiff = m_currentLevel - level;
429 double percent = (m_progressMode == ProgressMode::Relative) ? pow(0.25, levelDiff) : m_percent;
430
431 // encoding scheme without ROI
432 while (m_currentLevel > level) {
433 for (int i=0; i < m_header.channels; i++) {
434 CWaveletTransform* wtChannel = m_wtChannel[i];
435 ASSERT(wtChannel);
436
437 // decode file and write stream to m_wtChannel
439 // last level also has LL band
441 }
443 // since version 5
446 } else {
447 // until version 4
449 }
451 }
452
453 volatile OSError error = NoError; // volatile prevents optimizations
454 #ifdef LIBPGF_USE_OPENMP
455 #pragma omp parallel for default(shared)
456 #endif
457 for (int i=0; i < m_header.channels; i++) {
458 // inverse transform from m_wtChannel to m_channel
459 if (error == NoError) {
460 OSError err = m_wtChannel[i]->InverseTransform(m_currentLevel, &m_width[i], &m_height[i], &m_channel[i]);
461 if (err != NoError) error = err;
462 }
463 ASSERT(m_channel[i]);
464 }
465 if (error != NoError) ReturnWithError(error);
466
467 // set new level: must be done before refresh callback
469
470 // now we have to refresh the display
471 if (m_cb) m_cb(m_cbArg);
472
473 // now update progress
474 if (cb) {
475 percent *= 4;
477 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
478 }
479 }
480 }
481 }
482
483 #ifdef __PGFROISUPPORT__
493 void CPGFImage::Read(PGFRect& rect, int level /*= 0*/, CallbackPtr cb /*= nullptr*/, void *data /*=nullptr*/) {
494 ASSERT((level >= 0 && level < m_header.nLevels) || m_header.nLevels == 0); // m_header.nLevels == 0: image didn't use wavelet transform
495 ASSERT(m_decoder);
496
497 if (m_header.nLevels == 0 || !ROIisSupported()) {
498 rect.left = rect.top = 0;
499 rect.right = m_header.width; rect.bottom = m_header.height;
500 Read(level, cb, data);
501 } else {
502 ASSERT(ROIisSupported());
503 // new encoding scheme supporting ROI
504 ASSERT(rect.left < m_header.width && rect.top < m_header.height);
505
506 // check rectangle
507 if (rect.right == 0 || rect.right > m_header.width) rect.right = m_header.width;
508 if (rect.bottom == 0 || rect.bottom > m_header.height) rect.bottom = m_header.height;
509
510 const int levelDiff = m_currentLevel - level;
511 double percent = (m_progressMode == pgf::ProgressMode::Relative) ? pow(0.25, levelDiff) : m_percent;
512
513 // check level difference
514 if (levelDiff <= 0) {
515 // it is a new read call, probably with a new ROI
518 }
519
520 // enable ROI decoding and reading
521 SetROI(rect);
522
523 while (m_currentLevel > level) {
524 for (int i=0; i < m_header.channels; i++) {
525 CWaveletTransform* wtChannel = m_wtChannel[i];
526 ASSERT(wtChannel);
527
528 // get number of tiles and tile indices
529 const uint32_t nTiles = wtChannel->GetNofTiles(m_currentLevel); // independent of ROI
530
531 // decode file and write stream to m_wtChannel
532 if (m_currentLevel == m_header.nLevels) { // last level also has LL band
533 ASSERT(nTiles == 1);
536 }
537 for (uint32_t tileY=0; tileY < nTiles; tileY++) {
538 for (uint32_t tileX=0; tileX < nTiles; tileX++) {
539 // check relevance of tile
540 if (wtChannel->TileIsRelevant(m_currentLevel, tileX, tileY)) {
542 wtChannel->GetSubband(m_currentLevel, Orient::HL)->PlaceTile(*m_decoder, m_quant, true, tileX, tileY);
543 wtChannel->GetSubband(m_currentLevel, Orient::LH)->PlaceTile(*m_decoder, m_quant, true, tileX, tileY);
544 wtChannel->GetSubband(m_currentLevel, Orient::HH)->PlaceTile(*m_decoder, m_quant, true, tileX, tileY);
545 } else {
546 // skip tile
547 m_decoder->SkipTileBuffer();
548 }
549 }
550 }
551 }
552
553 volatile OSError error = NoError; // volatile prevents optimizations
554 #ifdef LIBPGF_USE_OPENMP
555 #pragma omp parallel for default(shared)
556 #endif
557 for (int i=0; i < m_header.channels; i++) {
558 // inverse transform from m_wtChannel to m_channel
559 if (error == NoError) {
560 OSError err = m_wtChannel[i]->InverseTransform(m_currentLevel, &m_width[i], &m_height[i], &m_channel[i]);
561 if (err != NoError) error = err;
562 }
563 ASSERT(m_channel[i]);
564 }
565 if (error != NoError) ReturnWithError(error);
566
567 // set new level: must be done before refresh callback
569
570 // now we have to refresh the display
571 if (m_cb) m_cb(m_cbArg);
572
573 // now update progress
574 if (cb) {
575 percent *= 4;
577 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
578 }
579 }
580 }
581 }
582
588 if (m_currentLevel == 0) {
589 return m_roi;
590 } else {
591 const uint32_t rLeft = LevelSizeL(m_roi.left, m_currentLevel);
592 const uint32_t rRight = LevelSizeL(m_roi.right, m_currentLevel);
593 const uint32_t rTop = LevelSizeL(m_roi.top, m_currentLevel);
594 const uint32_t rBottom = LevelSizeL(m_roi.bottom, m_currentLevel);
595 return PGFRect(rLeft, rTop, rRight - rLeft, rBottom - rTop);
596 }
597 }
598
602 PGFRect CPGFImage::GetAlignedROI(int c /*= 0*/) const {
603 PGFRect roi(0, 0, m_width[c], m_height[c]);
604
605 if (ROIisSupported()) {
606 ASSERT(m_wtChannel[c]);
607
608 roi = m_wtChannel[c]->GetAlignedROI(m_currentLevel);
609 }
610 ASSERT(roi.Width() == m_width[c]);
611 ASSERT(roi.Height() == m_height[c]);
612 return roi;
613 }
614
620 ASSERT(m_decoder);
621 ASSERT(ROIisSupported());
622 ASSERT(m_wtChannel[0]);
623
624 // store ROI for a later call of GetBitmap
625 m_roi = rect;
626
627 // enable ROI decoding
628 m_decoder->SetROI();
629
630 // prepare wavelet channels for using ROI
631 m_wtChannel[0]->SetROI(rect);
632
633 if (m_downsample && m_header.channels > 1) {
634 // all further channels are downsampled, therefore downsample ROI
635 rect.left >>= 1;
636 rect.top >>= 1;
637 rect.right = (rect.right + 1) >> 1;
638 rect.bottom = (rect.bottom + 1) >> 1;
639 }
640 for (int i=1; i < m_header.channels; i++) {
641 ASSERT(m_wtChannel[i]);
642 m_wtChannel[i]->SetROI(rect);
643 }
644 }
645
646 #endif // __PGFROISUPPORT__
647
653 ASSERT(m_decoder);
655 }
656
664 uint32_t CPGFImage::ReadEncodedHeader(uint8_t* target, uint32_t targetLen) const {
665 ASSERT(target);
666 ASSERT(targetLen > 0);
667 ASSERT(m_decoder);
668
669 // reset stream position
671
672 // compute number of bytes to read
673 uint32_t len = __min(targetLen, GetEncodedHeaderLength());
674
675 // read data
676 len = m_decoder->ReadEncodedData(target, len);
677 ASSERT(len >= 0 && len <= targetLen);
678
679 return len;
680 }
681
686 void CPGFImage::ResetStreamPos(bool startOfData) {
687 m_currentLevel = 0;
688 if (startOfData) {
689 ASSERT(m_decoder);
691 } else {
692 if (m_decoder) {
694 } else if (m_encoder) {
696 } else {
697 ASSERT(false);
698 }
699 }
700 }
701
711 uint32_t CPGFImage::ReadEncodedData(int level, uint8_t* target, uint32_t targetLen) const {
712 ASSERT(level >= 0 && level < m_header.nLevels);
713 ASSERT(target);
714 ASSERT(targetLen > 0);
715 ASSERT(m_decoder);
716
717 // reset stream position
719
720 // position stream
721 uint64_t offset = 0;
722
723 for (int i=m_header.nLevels - 1; i > level; i--) {
724 offset += m_levelLength[m_header.nLevels - 1 - i];
725 }
726 m_decoder->Skip(offset);
727
728 // compute number of bytes to read
729 uint32_t len = __min(targetLen, GetEncodedLevelLength(level));
730
731 // read data
732 len = m_decoder->ReadEncodedData(target, len);
733 ASSERT(len >= 0 && len <= targetLen);
734
735 return len;
736 }
737
742 void CPGFImage::SetMaxValue(uint32_t maxValue) {
743 const uint8_t bpc = m_header.bpp/m_header.channels;
744 uint8_t pot = 0;
745
746 while(maxValue > 0) {
747 pot++;
748 maxValue >>= 1;
749 }
750 // store bits per channel
751 if (pot > bpc) pot = bpc;
752 if (pot > 31) pot = 31;
754 }
755
761 const uint8_t bpc = m_header.bpp/m_header.channels;
762
763 if (bpc > 8) {
765 } else {
766 return bpc;
767 }
768 }
769
772 uint8_t CPGFImage::CodecMajorVersion(uint8_t version) {
773 if (version & Version7) return 7;
774 if (version & Version6) return 6;
775 if (version & Version5) return 5;
776 if (version & Version2) return 2;
777 return 1;
778 }
779
781 // Import an image from a specified image buffer.
782 // This method is usually called before Write(...) and after SetHeader(...).
783 // It might throw an IOException.
784 // The absolute value of pitch is the number of bytes of an image row.
785 // If pitch is negative, then buff points to the last row of a bottom-up image (first byte on last row).
786 // If pitch is positive, then buff points to the first row of a top-down image (first byte).
787 // The sequence of input channels in the input image buffer does not need to be the same as expected from PGF. In case of different sequences you have to
788 // provide a channelMap of size of expected channels (depending on image mode). For example, PGF expects in RGB color mode a channel sequence BGR.
789 // If your provided image buffer contains a channel sequence ARGB, then the channelMap looks like { 3, 2, 1 }.
790 // @param pitch The number of bytes of a row of the image buffer.
791 // @param buff An image buffer.
792 // @param bpp The number of bits per pixel used in image buffer.
793 // @param channelMap A integer array containing the mapping of input channel ordering to expected channel ordering.
794 // @param cb A pointer to a callback procedure. The procedure is called after each imported buffer row. If cb returns true, then it stops proceeding.
795 // @param data Data Pointer to C++ class container to host callback procedure.
796 void CPGFImage::ImportBitmap(int pitch, uint8_t *buff, uint8_t bpp, int channelMap[] /*= nullptr */, CallbackPtr cb /*= nullptr*/, void *data /*=nullptr*/) {
797 ASSERT(buff);
798 ASSERT(m_channel[0]);
799
800 // color transform
801 RgbToYuv(pitch, buff, bpp, channelMap, cb, data);
802
803 if (m_downsample) {
804 // Subsampling of the chrominance and alpha channels
805 for (int i=1; i < m_header.channels; i++) {
806 Downsample(i);
807 }
808 }
809 }
810
812 // Bilinerar Subsampling of channel ch by a factor 2
813 // Called before Write()
815 ASSERT(ch > 0);
816
817 const int w = m_width[0];
818 const int w2 = w/2;
819 const int h2 = m_height[0]/2;
820 const int oddW = w%2; // don't use bool -> problems with MaxSpeed optimization
821 const int oddH = m_height[0]%2; // "
822 int loPos = 0;
823 int hiPos = w;
824 int sampledPos = 0;
825 DataT* buff = m_channel[ch]; ASSERT(buff);
826
827 for (int i=0; i < h2; i++) {
828 for (int j=0; j < w2; j++) {
829 // compute average of pixel block
830 buff[sampledPos] = (buff[loPos] + buff[loPos + 1] + buff[hiPos] + buff[hiPos + 1]) >> 2;
831 loPos += 2; hiPos += 2;
832 sampledPos++;
833 }
834 if (oddW) {
835 buff[sampledPos] = (buff[loPos] + buff[hiPos]) >> 1;
836 loPos++; hiPos++;
837 sampledPos++;
838 }
839 loPos += w; hiPos += w;
840 }
841 if (oddH) {
842 for (int j=0; j < w2; j++) {
843 buff[sampledPos] = (buff[loPos] + buff[loPos+1]) >> 1;
844 loPos += 2; hiPos += 2;
845 sampledPos++;
846 }
847 if (oddW) {
848 buff[sampledPos] = buff[loPos];
849 }
850 }
851
852 // downsampled image has half width and half height
853 m_width[ch] = (m_width[ch] + 1)/2;
854 m_height[ch] = (m_height[ch] + 1)/2;
855 }
856
859 const int maxThumbnailWidth = 20*FilterSize;
860 const int m = __min(m_header.width, m_header.height);
861 int s = m;
862
864 m_header.nLevels = 1;
865 // compute a good value depending on the size of the image
866 while (s > maxThumbnailWidth) {
868 s >>= 1;
869 }
870 }
871
872 int levels = m_header.nLevels; // we need a signed value during level reduction
873
874 // reduce number of levels if the image size is smaller than FilterSize*(2^levels)
875 s = FilterSize*(1 << levels); // must be at least the double filter size because of subsampling
876 while (m < s) {
877 levels--;
878 s >>= 1;
879 }
880 if (levels > MaxLevel) m_header.nLevels = MaxLevel;
881 else if (levels < 0) m_header.nLevels = 0;
882 else m_header.nLevels = (uint8_t)levels;
883
884 // used in Write when Absolute
885 m_percent = pow(0.25, m_header.nLevels);
886
887 ASSERT(0 <= m_header.nLevels && m_header.nLevels <= MaxLevel);
888 }
889
898 void CPGFImage::SetHeader(const PGFHeader& header, uint8_t flags /*=0*/, const uint8_t* userData /*= 0*/, uint32_t userDataLength /*= 0*/) {
899 ASSERT(!m_decoder); // current image must be closed
900 ASSERT(header.quality <= MaxQuality);
901 ASSERT(userDataLength <= MaxUserDataSize);
902
903 // init state
904 #ifdef __PGFROISUPPORT__
905 m_streamReinitialized = false;
906 #endif
907
908 // init preHeader
909 memcpy(m_preHeader.magic, PGFMagic, 3);
912
913 // copy header
914 memcpy(&m_header, &header, HeaderSize);
915
916 // check quality
918
919 // complete header
921
922 // check and set number of levels
924
925 // check for downsample
933 m_downsample = true;
935 } else {
936 m_downsample = false;
938 }
939
940 // update header size and copy user data
942 // update header size
944 }
945 if (userDataLength && userData) {
946 if (userDataLength > MaxUserDataSize) userDataLength = MaxUserDataSize;
947 m_postHeader.userData = new(std::nothrow) uint8_t[userDataLength];
948 if (!m_postHeader.userData) ReturnWithError(InsufficientMemory);
950 memcpy(m_postHeader.userData, userData, userDataLength);
951 // update header size
952 m_preHeader.hSize += userDataLength;
953 }
954
955 // allocate channels
956 for (int i=0; i < m_header.channels; i++) {
957 // set current width and height
960
961 // allocate channels
962 ASSERT(!m_channel[i]);
963 m_channel[i] = new(std::nothrow) DataT[m_header.width*m_header.height];
964 if (!m_channel[i]) {
965 if (i) i--;
966 while(i) {
967 delete[] m_channel[i]; m_channel[i] = 0;
968 i--;
969 }
970 ReturnWithError(InsufficientMemory);
971 }
972 }
973 }
974
984 ASSERT(m_header.nLevels <= MaxLevel);
985 ASSERT(m_header.quality <= MaxQuality); // quality is already initialized
986
987 if (m_header.nLevels > 0) {
988 volatile OSError error = NoError; // volatile prevents optimizations
989 // create new wt channels
990 #ifdef LIBPGF_USE_OPENMP
991 #pragma omp parallel for default(shared)
992 #endif
993 for (int i=0; i < m_header.channels; i++) {
994 DataT *temp = nullptr;
995 if (error == NoError) {
996 if (m_wtChannel[i]) {
997 ASSERT(m_channel[i]);
998 // copy m_channel to temp
999 int size = m_height[i]*m_width[i];
1000 temp = new(std::nothrow) DataT[size];
1001 if (temp) {
1002 memcpy(temp, m_channel[i], size*DataTSize);
1003 delete m_wtChannel[i]; // also deletes m_channel
1004 m_channel[i] = nullptr;
1005 } else {
1006 error = InsufficientMemory;
1007 }
1008 }
1009 if (error == NoError) {
1010 if (temp) {
1011 ASSERT(!m_channel[i]);
1012 m_channel[i] = temp;
1013 }
1015 if (m_wtChannel[i]) {
1016 #ifdef __PGFROISUPPORT__
1017 m_wtChannel[i]->SetROI(PGFRect(0, 0, m_width[i], m_height[i]));
1018 #endif
1019
1020 // wavelet subband decomposition
1021 for (int l=0; error == NoError && l < m_header.nLevels; l++) {
1022 OSError err = m_wtChannel[i]->ForwardTransform(l, m_quant);
1023 if (err != NoError) error = err;
1024 }
1025 } else {
1026 delete[] m_channel[i];
1027 error = InsufficientMemory;
1028 }
1029 }
1030 }
1031 }
1032 if (error != NoError) {
1033 // free already allocated memory
1034 for (int i=0; i < m_header.channels; i++) {
1035 delete m_wtChannel[i];
1036 }
1037 ReturnWithError(error);
1038 }
1039
1041
1042 // create encoder, write headers and user data, but not level-length area
1045
1046 #ifdef __PGFROISUPPORT__
1047 if (ROIisSupported()) {
1048 // new encoding scheme supporting ROI
1049 m_encoder->SetROI();
1050 }
1051 #endif
1052
1053 } else {
1054 // very small image: we don't use DWT and encoding
1055
1056 // create encoder, write headers and user data, but not level-length area
1058 }
1059
1060 int64_t nBytes = m_encoder->ComputeHeaderLength();
1061 return (nBytes > 0) ? (uint32_t)nBytes : 0;
1062 }
1063
1065 // Encode and write next level of a PGF image at current stream position.
1066 // A PGF image is structered in levels, numbered between 0 and Levels() - 1.
1067 // Each level can be seen as a single image, containing the same content
1068 // as all other levels, but in a different size (width, height).
1069 // The image size at level i is double the size (width, height) of the image at level i+1.
1070 // The image at level 0 contains the original size.
1071 // It might throw an IOException.
1073 ASSERT(m_encoder);
1074 ASSERT(m_currentLevel > 0);
1075 ASSERT(m_header.nLevels > 0);
1076
1077 #ifdef __PGFROISUPPORT__
1078 if (ROIisSupported()) {
1079 const int lastChannel = m_header.channels - 1;
1080
1081 for (int i=0; i < m_header.channels; i++) {
1082 // get number of tiles and tile indices
1083 const uint32_t nTiles = m_wtChannel[i]->GetNofTiles(m_currentLevel);
1084 const uint32_t lastTile = nTiles - 1;
1085
1087 // last level also has LL band
1088 ASSERT(nTiles == 1);
1090 m_encoder->EncodeTileBuffer(); // encode macro block with tile-end = true
1091 }
1092 for (uint32_t tileY=0; tileY < nTiles; tileY++) {
1093 for (uint32_t tileX=0; tileX < nTiles; tileX++) {
1094 // extract tile to macro block and encode already filled macro blocks with tile-end = false
1098 if (i == lastChannel && tileY == lastTile && tileX == lastTile) {
1099 // all necessary data are buffered. next call of EncodeTileBuffer will write the last piece of data of the current level.
1101 }
1102 m_encoder->EncodeTileBuffer(); // encode last macro block with tile-end = true
1103 }
1104 }
1105 }
1106 } else
1107 #endif
1108 {
1109 for (int i=0; i < m_header.channels; i++) {
1110 ASSERT(m_wtChannel[i]);
1112 // last level also has LL band
1114 }
1115 //encoder.EncodeInterleaved(m_wtChannel[i], m_currentLevel, m_quant); // until version 4
1119 }
1120
1121 // all necessary data are buffered. next call of EncodeBuffer will write the last piece of data of the current level.
1123 }
1124 }
1125
1127 // Return written levelLength bytes
1129 ASSERT(m_encoder);
1130
1131 int64_t offset = m_encoder->ComputeOffset(); ASSERT(offset >= 0);
1132
1133 if (offset > 0) {
1134 // update post-header size and rewrite pre-header
1135 m_preHeader.hSize += (uint32_t)offset;
1137 }
1138
1139 // write dummy levelLength into stream
1141 }
1142
1154 uint32_t CPGFImage::WriteImage(CPGFStream* stream, CallbackPtr cb /*= nullptr*/, void *data /*= nullptr*/) {
1155 ASSERT(stream);
1156 ASSERT(m_preHeader.hSize);
1157
1158 int levels = m_header.nLevels;
1159 double percent = pow(0.25, levels);
1160
1161 // update post-header size, rewrite pre-header, and write dummy levelLength
1162 uint32_t nWrittenBytes = UpdatePostHeaderSize();
1163
1164 if (levels == 0) {
1165 // for very small images: write channels uncoded
1166 for (int c=0; c < m_header.channels; c++) {
1167 const uint32_t size = m_width[c]*m_height[c];
1168
1169 // write channel data into stream
1170 for (uint32_t i=0; i < size; i++) {
1171 int count = DataTSize;
1172 stream->Write(&count, &m_channel[c][i]);
1173 }
1174 }
1175
1176 // now update progress
1177 if (cb) {
1178 if ((*cb)(1, true, data)) ReturnWithError(EscapePressed);
1179 }
1180
1181 } else {
1182 // encode quantized wavelet coefficients and write to PGF file
1183 // encode subbands, higher levels first
1184 // color channels are interleaved
1185
1186 // encode all levels
1187 for (m_currentLevel = levels; m_currentLevel > 0; ) {
1188 WriteLevel(); // decrements m_currentLevel
1189
1190 // now update progress
1191 if (cb) {
1192 percent *= 4;
1193 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1194 }
1195 }
1196
1197 // flush encoder and write level lengths
1198 m_encoder->Flush();
1199 }
1200
1201 // update level lengths
1202 nWrittenBytes += m_encoder->UpdateLevelLength(); // return written image bytes
1203
1204 // delete encoder
1205 delete m_encoder; m_encoder = nullptr;
1206
1207 ASSERT(!m_encoder);
1208
1209 return nWrittenBytes;
1210 }
1211
1225 void CPGFImage::Write(CPGFStream* stream, uint32_t* nWrittenBytes /*= nullptr*/, CallbackPtr cb /*= nullptr*/, void *data /*=nullptr*/) {
1226 ASSERT(stream);
1227 ASSERT(m_preHeader.hSize);
1228
1229 // create wavelet transform channels and encoder
1230 uint32_t nBytes = WriteHeader(stream);
1231
1232 // write image
1233 nBytes += WriteImage(stream, cb, data);
1234
1235 // return written bytes
1236 if (nWrittenBytes) *nWrittenBytes += nBytes;
1237 }
1238
1239 #ifdef __PGFROISUPPORT__
1241 // Encode and write down to given level at current stream position.
1242 // A PGF image is structered in levels, numbered between 0 and Levels() - 1.
1243 // Each level can be seen as a single image, containing the same content
1244 // as all other levels, but in a different size (width, height).
1245 // The image size at level i is double the size (width, height) of the image at level i+1.
1246 // The image at level 0 contains the original size.
1247 // Precondition: the PGF image contains a valid header (see also SetHeader(...)) and WriteHeader() has been called before.
1248 // The ROI encoding scheme is used.
1249 // It might throw an IOException.
1250 // @param level The image level of the resulting image in the internal image buffer.
1251 // @param cb A pointer to a callback procedure. The procedure is called after writing a single level. If cb returns true, then it stops proceeding.
1252 // @param data Data Pointer to C++ class container to host callback procedure.
1253 // @return The number of bytes written into stream.
1254 uint32_t CPGFImage::Write(int level, CallbackPtr cb /*= nullptr*/, void *data /*=nullptr*/) {
1255 ASSERT(m_header.nLevels > 0);
1256 ASSERT(0 <= level && level < m_header.nLevels);
1257 ASSERT(m_encoder);
1258 ASSERT(ROIisSupported());
1259
1260 const int levelDiff = m_currentLevel - level;
1261 double percent = (m_progressMode == pgf::ProgressMode::Relative) ? pow(0.25, levelDiff) : m_percent;
1262 uint32_t nWrittenBytes = 0;
1263
1265 // update post-header size, rewrite pre-header, and write dummy levelLength
1266 nWrittenBytes = UpdatePostHeaderSize();
1267 } else {
1268 // prepare for next level: save current file position, because the stream might have been reinitialized
1270 m_streamReinitialized = true;
1271 }
1272 }
1273
1274 // encoding scheme with ROI
1275 while (m_currentLevel > level) {
1276 WriteLevel(); // decrements m_currentLevel
1277
1278 if (m_levelLength) {
1279 nWrittenBytes += m_levelLength[m_header.nLevels - m_currentLevel - 1];
1280 }
1281
1282 // now update progress
1283 if (cb) {
1284 percent *= 4;
1286 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1287 }
1288 }
1289
1290 // automatically closing
1291 if (m_currentLevel == 0) {
1292 if (!m_streamReinitialized) {
1293 // don't write level lengths, if the stream position changed inbetween two Write operations
1295 }
1296 // delete encoder
1297 delete m_encoder; m_encoder = nullptr;
1298 }
1299
1300 return nWrittenBytes;
1301 }
1302 #endif // __PGFROISUPPORT__
1303
1304
1306 // Check for valid import image mode.
1307 // @param mode Image mode
1308 // @return True if an image of given mode can be imported with ImportBitmap(...)
1309 bool CPGFImage::ImportIsSupported(uint8_t mode) {
1310 size_t size = DataTSize;
1311
1312 if (size >= 2) {
1313 switch(mode) {
1314 case ImageModeBitmap:
1316 case ImageModeGrayScale:
1317 case ImageModeRGBColor:
1318 case ImageModeCMYKColor:
1319 case ImageModeHSLColor:
1320 case ImageModeHSBColor:
1321 //case ImageModeDuotone:
1322 case ImageModeLabColor:
1323 case ImageModeRGB12:
1324 case ImageModeRGB16:
1325 case ImageModeRGBA:
1326 return true;
1327 }
1328 }
1329 if (size >= 3) {
1330 switch(mode) {
1331 case ImageModeGray16:
1332 case ImageModeRGB48:
1333 case ImageModeLab48:
1334 case ImageModeCMYK64:
1335 //case ImageModeDuotone16:
1336 return true;
1337 }
1338 }
1339 if (size >=4) {
1340 switch(mode) {
1341 case ImageModeGray32:
1342 return true;
1343 }
1344 }
1345 return false;
1346 }
1347
1354 void CPGFImage::GetColorTable(uint32_t iFirstColor, uint32_t nColors, RGBQUAD* prgbColors) const {
1355 if (iFirstColor + nColors > ColorTableLen) ReturnWithError(ColorTableError);
1356
1357 for (uint32_t i=iFirstColor, j=0; j < nColors; i++, j++) {
1358 prgbColors[j] = m_postHeader.clut[i];
1359 }
1360 }
1361
1368 void CPGFImage::SetColorTable(uint32_t iFirstColor, uint32_t nColors, const RGBQUAD* prgbColors) {
1369 if (iFirstColor + nColors > ColorTableLen) ReturnWithError(ColorTableError);
1370
1371 for (uint32_t i=iFirstColor, j=0; j < nColors; i++, j++) {
1372 m_postHeader.clut[i] = prgbColors[j];
1373 }
1374 }
1375
1377 // Buffer transform from interleaved to channel seperated format
1378 // the absolute value of pitch is the number of bytes of an image row
1379 // if pitch is negative, then buff points to the last row of a bottom-up image (first byte on last row)
1380 // if pitch is positive, then buff points to the first row of a top-down image (first byte)
1381 // bpp is the number of bits per pixel used in image buffer buff
1382 //
1383 // RGB is transformed into YUV format (ordering of buffer data is BGR[A])
1384 // Y = (R + 2*G + B)/4 -128
1385 // U = R - G
1386 // V = B - G
1387 //
1388 // Since PGF Codec version 2.0 images are stored in top-down direction
1389 //
1390 // The sequence of input channels in the input image buffer does not need to be the same as expected from PGF. In case of different sequences you have to
1391 // provide a channelMap of size of expected channels (depending on image mode). For example, PGF expects in RGB color mode a channel sequence BGR.
1392 // If your provided image buffer contains a channel sequence ARGB, then the channelMap looks like { 3, 2, 1 }.
1393 void CPGFImage::RgbToYuv(int pitch, uint8_t* buff, uint8_t bpp, int channelMap[], CallbackPtr cb, void *data /*=nullptr*/) {
1394 ASSERT(buff);
1395 uint32_t yPos = 0, cnt = 0;
1396 double percent = 0;
1397 const double dP = 1.0/m_header.height;
1398 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(sizeof(defMap)/sizeof(defMap[0]) == MaxChannels);
1399
1400 if (channelMap == nullptr) channelMap = defMap;
1401
1402 switch(m_header.mode) {
1403 case ImageModeBitmap:
1404 {
1405 ASSERT(m_header.channels == 1);
1406 ASSERT(m_header.bpp == 1);
1407 ASSERT(bpp == 1);
1408
1409 const uint32_t w = m_header.width;
1410 const uint32_t w2 = (m_header.width + 7)/8;
1411 DataT* y = m_channel[0]; ASSERT(y);
1412
1413 // new unpacked version since version 7
1414 for (uint32_t h = 0; h < m_header.height; h++) {
1415 if (cb) {
1416 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1417 percent += dP;
1418 }
1419 cnt = 0;
1420 for (uint32_t j = 0; j < w2; j++) {
1421 uint8_t byte = buff[j];
1422 for (int k = 0; k < 8; k++) {
1423 uint8_t bit = (byte & 0x80) >> 7;
1424 if (cnt < w) y[yPos++] = bit;
1425 byte <<= 1;
1426 cnt++;
1427 }
1428 }
1429 buff += pitch;
1430 }
1431 /* old version: packed values: 8 pixels in 1 byte
1432 for (uint32_t h = 0; h < m_header.height; h++) {
1433 if (cb) {
1434 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1435 percent += dP;
1436 }
1437
1438 for (uint32_t j = 0; j < w2; j++) {
1439 y[yPos++] = buff[j] - YUVoffset8;
1440 }
1441 // version 5 and 6
1442 // for (uint32_t j = w2; j < w; j++) {
1443 // y[yPos++] = YUVoffset8;
1444 //}
1445 buff += pitch;
1446 }
1447 */
1448 }
1449 break;
1451 case ImageModeGrayScale:
1452 case ImageModeHSLColor:
1453 case ImageModeHSBColor:
1454 case ImageModeLabColor:
1455 {
1456 ASSERT(m_header.channels >= 1);
1457 ASSERT(m_header.bpp == m_header.channels*8);
1458 ASSERT(bpp%8 == 0);
1459 const int channels = bpp/8; ASSERT(channels >= m_header.channels);
1460
1461 for (uint32_t h=0; h < m_header.height; h++) {
1462 if (cb) {
1463 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1464 percent += dP;
1465 }
1466
1467 cnt = 0;
1468 for (uint32_t w=0; w < m_header.width; w++) {
1469 for (int c=0; c < m_header.channels; c++) {
1470 m_channel[c][yPos] = buff[cnt + channelMap[c]] - YUVoffset8;
1471 }
1472 cnt += channels;
1473 yPos++;
1474 }
1475 buff += pitch;
1476 }
1477 }
1478 break;
1479 case ImageModeGray16:
1480 case ImageModeLab48:
1481 {
1482 ASSERT(m_header.channels >= 1);
1483 ASSERT(m_header.bpp == m_header.channels*16);
1484 ASSERT(bpp%16 == 0);
1485
1486 uint16_t *buff16 = (uint16_t *)buff;
1487 const int pitch16 = pitch/2;
1488 const int channels = bpp/16; ASSERT(channels >= m_header.channels);
1489 const int shift = 16 - UsedBitsPerChannel(); ASSERT(shift >= 0);
1490 const DataT yuvOffset16 = 1 << (UsedBitsPerChannel() - 1);
1491
1492 for (uint32_t h=0; h < m_header.height; h++) {
1493 if (cb) {
1494 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1495 percent += dP;
1496 }
1497
1498 cnt = 0;
1499 for (uint32_t w=0; w < m_header.width; w++) {
1500 for (int c=0; c < m_header.channels; c++) {
1501 m_channel[c][yPos] = (buff16[cnt + channelMap[c]] >> shift) - yuvOffset16;
1502 }
1503 cnt += channels;
1504 yPos++;
1505 }
1506 buff16 += pitch16;
1507 }
1508 }
1509 break;
1510 case ImageModeRGBColor:
1511 {
1512 ASSERT(m_header.channels == 3);
1513 ASSERT(m_header.bpp == m_header.channels*8);
1514 ASSERT(bpp%8 == 0);
1515
1516 DataT* y = m_channel[0]; ASSERT(y);
1517 DataT* u = m_channel[1]; ASSERT(u);
1518 DataT* v = m_channel[2]; ASSERT(v);
1519 const int channels = bpp/8; ASSERT(channels >= m_header.channels);
1520 uint8_t b, g, r;
1521
1522 for (uint32_t h=0; h < m_header.height; h++) {
1523 if (cb) {
1524 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1525 percent += dP;
1526 }
1527
1528 cnt = 0;
1529 for (uint32_t w=0; w < m_header.width; w++) {
1530 b = buff[cnt + channelMap[0]];
1531 g = buff[cnt + channelMap[1]];
1532 r = buff[cnt + channelMap[2]];
1533 // Yuv
1534 y[yPos] = ((b + (g << 1) + r) >> 2) - YUVoffset8;
1535 u[yPos] = r - g;
1536 v[yPos] = b - g;
1537 yPos++;
1538 cnt += channels;
1539 }
1540 buff += pitch;
1541 }
1542 }
1543 break;
1544 case ImageModeRGB48:
1545 {
1546 ASSERT(m_header.channels == 3);
1547 ASSERT(m_header.bpp == m_header.channels*16);
1548 ASSERT(bpp%16 == 0);
1549
1550 uint16_t *buff16 = (uint16_t *)buff;
1551 const int pitch16 = pitch/2;
1552 const int channels = bpp/16; ASSERT(channels >= m_header.channels);
1553 const int shift = 16 - UsedBitsPerChannel(); ASSERT(shift >= 0);
1554 const DataT yuvOffset16 = 1 << (UsedBitsPerChannel() - 1);
1555
1556 DataT* y = m_channel[0]; ASSERT(y);
1557 DataT* u = m_channel[1]; ASSERT(u);
1558 DataT* v = m_channel[2]; ASSERT(v);
1559 uint16_t b, g, r;
1560
1561 for (uint32_t h=0; h < m_header.height; h++) {
1562 if (cb) {
1563 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1564 percent += dP;
1565 }
1566
1567 cnt = 0;
1568 for (uint32_t w=0; w < m_header.width; w++) {
1569 b = buff16[cnt + channelMap[0]] >> shift;
1570 g = buff16[cnt + channelMap[1]] >> shift;
1571 r = buff16[cnt + channelMap[2]] >> shift;
1572 // Yuv
1573 y[yPos] = ((b + (g << 1) + r) >> 2) - yuvOffset16;
1574 u[yPos] = r - g;
1575 v[yPos] = b - g;
1576 yPos++;
1577 cnt += channels;
1578 }
1579 buff16 += pitch16;
1580 }
1581 }
1582 break;
1583 case ImageModeRGBA:
1584 case ImageModeCMYKColor:
1585 {
1586 ASSERT(m_header.channels == 4);
1587 ASSERT(m_header.bpp == m_header.channels*8);
1588 ASSERT(bpp%8 == 0);
1589 const int channels = bpp/8; ASSERT(channels >= m_header.channels);
1590
1591 DataT* y = m_channel[0]; ASSERT(y);
1592 DataT* u = m_channel[1]; ASSERT(u);
1593 DataT* v = m_channel[2]; ASSERT(v);
1594 DataT* a = m_channel[3]; ASSERT(a);
1595 uint8_t b, g, r;
1596
1597 for (uint32_t h=0; h < m_header.height; h++) {
1598 if (cb) {
1599 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1600 percent += dP;
1601 }
1602
1603 cnt = 0;
1604 for (uint32_t w=0; w < m_header.width; w++) {
1605 b = buff[cnt + channelMap[0]];
1606 g = buff[cnt + channelMap[1]];
1607 r = buff[cnt + channelMap[2]];
1608 // Yuv
1609 y[yPos] = ((b + (g << 1) + r) >> 2) - YUVoffset8;
1610 u[yPos] = r - g;
1611 v[yPos] = b - g;
1612 a[yPos++] = buff[cnt + channelMap[3]] - YUVoffset8;
1613 cnt += channels;
1614 }
1615 buff += pitch;
1616 }
1617 }
1618 break;
1619 case ImageModeCMYK64:
1620 {
1621 ASSERT(m_header.channels == 4);
1622 ASSERT(m_header.bpp == m_header.channels*16);
1623 ASSERT(bpp%16 == 0);
1624
1625 uint16_t *buff16 = (uint16_t *)buff;
1626 const int pitch16 = pitch/2;
1627 const int channels = bpp/16; ASSERT(channels >= m_header.channels);
1628 const int shift = 16 - UsedBitsPerChannel(); ASSERT(shift >= 0);
1629 const DataT yuvOffset16 = 1 << (UsedBitsPerChannel() - 1);
1630
1631 DataT* y = m_channel[0]; ASSERT(y);
1632 DataT* u = m_channel[1]; ASSERT(u);
1633 DataT* v = m_channel[2]; ASSERT(v);
1634 DataT* a = m_channel[3]; ASSERT(a);
1635 uint16_t b, g, r;
1636
1637 for (uint32_t h=0; h < m_header.height; h++) {
1638 if (cb) {
1639 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1640 percent += dP;
1641 }
1642
1643 cnt = 0;
1644 for (uint32_t w=0; w < m_header.width; w++) {
1645 b = buff16[cnt + channelMap[0]] >> shift;
1646 g = buff16[cnt + channelMap[1]] >> shift;
1647 r = buff16[cnt + channelMap[2]] >> shift;
1648 // Yuv
1649 y[yPos] = ((b + (g << 1) + r) >> 2) - yuvOffset16;
1650 u[yPos] = r - g;
1651 v[yPos] = b - g;
1652 a[yPos++] = (buff16[cnt + channelMap[3]] >> shift) - yuvOffset16;
1653 cnt += channels;
1654 }
1655 buff16 += pitch16;
1656 }
1657 }
1658 break;
1659 #ifdef __PGF32SUPPORT__
1660 case ImageModeGray32:
1661 {
1662 ASSERT(m_header.channels == 1);
1663 ASSERT(m_header.bpp == 32);
1664 ASSERT(bpp == 32);
1665 ASSERT(DataTSize == sizeof(uint32_t));
1666
1667 DataT* y = m_channel[0]; ASSERT(y);
1668
1669 uint32_t *buff32 = (uint32_t *)buff;
1670 const int pitch32 = pitch/4;
1671 const int shift = 31 - UsedBitsPerChannel(); ASSERT(shift >= 0);
1672 const DataT yuvOffset31 = 1 << (UsedBitsPerChannel() - 1);
1673
1674 for (uint32_t h=0; h < m_header.height; h++) {
1675 if (cb) {
1676 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1677 percent += dP;
1678 }
1679
1680 for (uint32_t w=0; w < m_header.width; w++) {
1681 y[yPos++] = (buff32[w] >> shift) - yuvOffset31;
1682 }
1683 buff32 += pitch32;
1684 }
1685 }
1686 break;
1687 #endif
1688 case ImageModeRGB12:
1689 {
1690 ASSERT(m_header.channels == 3);
1691 ASSERT(m_header.bpp == m_header.channels*4);
1692 ASSERT(bpp == m_header.channels*4);
1693
1694 DataT* y = m_channel[0]; ASSERT(y);
1695 DataT* u = m_channel[1]; ASSERT(u);
1696 DataT* v = m_channel[2]; ASSERT(v);
1697
1698 uint8_t rgb = 0, b, g, r;
1699
1700 for (uint32_t h=0; h < m_header.height; h++) {
1701 if (cb) {
1702 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1703 percent += dP;
1704 }
1705
1706 cnt = 0;
1707 for (uint32_t w=0; w < m_header.width; w++) {
1708 if (w%2 == 0) {
1709 // even pixel position
1710 rgb = buff[cnt];
1711 b = rgb & 0x0F;
1712 g = (rgb & 0xF0) >> 4;
1713 cnt++;
1714 rgb = buff[cnt];
1715 r = rgb & 0x0F;
1716 } else {
1717 // odd pixel position
1718 b = (rgb & 0xF0) >> 4;
1719 cnt++;
1720 rgb = buff[cnt];
1721 g = rgb & 0x0F;
1722 r = (rgb & 0xF0) >> 4;
1723 cnt++;
1724 }
1725
1726 // Yuv
1727 y[yPos] = ((b + (g << 1) + r) >> 2) - YUVoffset4;
1728 u[yPos] = r - g;
1729 v[yPos] = b - g;
1730 yPos++;
1731 }
1732 buff += pitch;
1733 }
1734 }
1735 break;
1736 case ImageModeRGB16:
1737 {
1738 ASSERT(m_header.channels == 3);
1739 ASSERT(m_header.bpp == 16);
1740 ASSERT(bpp == 16);
1741
1742 DataT* y = m_channel[0]; ASSERT(y);
1743 DataT* u = m_channel[1]; ASSERT(u);
1744 DataT* v = m_channel[2]; ASSERT(v);
1745
1746 uint16_t *buff16 = (uint16_t *)buff;
1747 uint16_t rgb, b, g, r;
1748 const int pitch16 = pitch/2;
1749
1750 for (uint32_t h=0; h < m_header.height; h++) {
1751 if (cb) {
1752 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1753 percent += dP;
1754 }
1755 for (uint32_t w=0; w < m_header.width; w++) {
1756 rgb = buff16[w];
1757 r = (rgb & 0xF800) >> 10; // highest 5 bits
1758 g = (rgb & 0x07E0) >> 5; // middle 6 bits
1759 b = (rgb & 0x001F) << 1; // lowest 5 bits
1760 // Yuv
1761 y[yPos] = ((b + (g << 1) + r) >> 2) - YUVoffset6;
1762 u[yPos] = r - g;
1763 v[yPos] = b - g;
1764 yPos++;
1765 }
1766
1767 buff16 += pitch16;
1768 }
1769 }
1770 break;
1771 default:
1772 ASSERT(false);
1773 }
1774 }
1775
1777 // Get image data in interleaved format: (ordering of RGB data is BGR[A])
1778 // Upsampling, YUV to RGB transform and interleaving are done here to reduce the number
1779 // of passes over the data.
1780 // The absolute value of pitch is the number of bytes of an image row of the given image buffer.
1781 // If pitch is negative, then the image buffer must point to the last row of a bottom-up image (first byte on last row).
1782 // if pitch is positive, then the image buffer must point to the first row of a top-down image (first byte).
1783 // The sequence of output channels in the output image buffer does not need to be the same as provided by PGF. In case of different sequences you have to
1784 // provide a channelMap of size of expected channels (depending on image mode). For example, PGF provides a channel sequence BGR in RGB color mode.
1785 // If your provided image buffer expects a channel sequence ARGB, then the channelMap looks like { 3, 2, 1 }.
1786 // It might throw an IOException.
1787 // @param pitch The number of bytes of a row of the image buffer.
1788 // @param buff An image buffer.
1789 // @param bpp The number of bits per pixel used in image buffer.
1790 // @param channelMap A integer array containing the mapping of PGF channel ordering to expected channel ordering.
1791 // @param cb A pointer to a callback procedure. The procedure is called after each copied buffer row. If cb returns true, then it stops proceeding.
1792 // @param data Data Pointer to C++ class container to host callback procedure.
1793 void CPGFImage::GetBitmap(int pitch, uint8_t* buff, uint8_t bpp, int channelMap[] /*= nullptr */, CallbackPtr cb /*= nullptr*/, void *data /*=nullptr*/) const {
1794 ASSERT(buff);
1795 uint32_t w = m_width[0]; // width of decoded image
1796 uint32_t h = m_height[0]; // height of decoded image
1797 uint32_t yw = w; // y-channel width
1798 uint32_t uw = m_width[1]; // u-channel width
1799 uint32_t roiOffsetX = 0;
1800 uint32_t roiOffsetY = 0;
1801 uint32_t yOffset = 0;
1802 uint32_t uOffset = 0;
1803
1804 #ifdef __PGFROISUPPORT__
1805 const PGFRect& roi = GetAlignedROI(); // in pixels, roi is usually larger than levelRoi
1806 ASSERT(w == roi.Width() && h == roi.Height());
1807 const PGFRect levelRoi = ComputeLevelROI();
1808 ASSERT(roi.left <= levelRoi.left && levelRoi.right <= roi.right);
1809 ASSERT(roi.top <= levelRoi.top && levelRoi.bottom <= roi.bottom);
1810
1811 if (ROIisSupported() && (levelRoi.Width() < w || levelRoi.Height() < h)) {
1812 // ROI is used
1813 w = levelRoi.Width();
1814 h = levelRoi.Height();
1815 roiOffsetX = levelRoi.left - roi.left;
1816 roiOffsetY = levelRoi.top - roi.top;
1817 yOffset = roiOffsetX + roiOffsetY*yw;
1818
1819 if (m_downsample) {
1820 const PGFRect& downsampledRoi = GetAlignedROI(1);
1821 uOffset = levelRoi.left/2 - downsampledRoi.left + (levelRoi.top/2 - downsampledRoi.top)*m_width[1];
1822 } else {
1823 uOffset = yOffset;
1824 }
1825 }
1826 #endif
1827
1828 const double dP = 1.0/h;
1829 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(sizeof(defMap)/sizeof(defMap[0]) == MaxChannels);
1830 if (channelMap == nullptr) channelMap = defMap;
1831 DataT uAvg, vAvg;
1832 double percent = 0;
1833 uint32_t i, j;
1834
1835 switch(m_header.mode) {
1836 case ImageModeBitmap:
1837 {
1838 ASSERT(m_header.channels == 1);
1839 ASSERT(m_header.bpp == 1);
1840 ASSERT(bpp == 1);
1841
1842 const uint32_t w2 = (w + 7)/8;
1843 DataT* y = m_channel[0]; ASSERT(y);
1844
1846 // new unpacked version has a little better compression ratio
1847 // since version 7
1848 for (i = 0; i < h; i++) {
1849 uint32_t cnt = 0;
1850 for (j = 0; j < w2; j++) {
1851 uint8_t byte = 0;
1852 for (int k = 0; k < 8; k++) {
1853 byte <<= 1;
1854 uint8_t bit = 0;
1855 if (cnt < w) {
1856 bit = y[yOffset + cnt] & 1;
1857 }
1858 byte |= bit;
1859 cnt++;
1860 }
1861 buff[j] = byte;
1862 }
1863 yOffset += yw;
1864 buff += pitch;
1865
1866 if (cb) {
1867 percent += dP;
1868 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1869 }
1870 }
1871 } else {
1872 // old versions
1873 // packed pixels: 8 pixel in 1 byte of channel[0]
1874 if (!(m_preHeader.version & Version5)) yw = w2; // not version 5 or 6
1875 yOffset = roiOffsetX/8 + roiOffsetY*yw; // 1 byte in y contains 8 pixel values
1876 for (i = 0; i < h; i++) {
1877 for (j = 0; j < w2; j++) {
1878 buff[j] = Clamp8(y[yOffset + j] + YUVoffset8);
1879 }
1880 yOffset += yw;
1881 buff += pitch;
1882
1883 if (cb) {
1884 percent += dP;
1885 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1886 }
1887 }
1888 }
1889 break;
1890 }
1892 case ImageModeGrayScale:
1893 case ImageModeHSLColor:
1894 case ImageModeHSBColor:
1895 {
1896 ASSERT(m_header.channels >= 1);
1897 ASSERT(m_header.bpp == m_header.channels*8);
1898 ASSERT(bpp%8 == 0);
1899
1900 uint32_t cnt, channels = bpp/8; ASSERT(channels >= m_header.channels);
1901
1902 for (i=0; i < h; i++) {
1903 uint32_t yPos = yOffset;
1904 cnt = 0;
1905 for (j=0; j < w; j++) {
1906 for (uint32_t c=0; c < m_header.channels; c++) {
1907 buff[cnt + channelMap[c]] = Clamp8(m_channel[c][yPos] + YUVoffset8);
1908 }
1909 cnt += channels;
1910 yPos++;
1911 }
1912 yOffset += yw;
1913 buff += pitch;
1914
1915 if (cb) {
1916 percent += dP;
1917 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1918 }
1919 }
1920 break;
1921 }
1922 case ImageModeGray16:
1923 {
1924 ASSERT(m_header.channels >= 1);
1925 ASSERT(m_header.bpp == m_header.channels*16);
1926
1927 const DataT yuvOffset16 = 1 << (UsedBitsPerChannel() - 1);
1928 uint32_t cnt, channels;
1929
1930 if (bpp%16 == 0) {
1931 const int shift = 16 - UsedBitsPerChannel(); ASSERT(shift >= 0);
1932 uint16_t *buff16 = (uint16_t *)buff;
1933 int pitch16 = pitch/2;
1934 channels = bpp/16; ASSERT(channels >= m_header.channels);
1935
1936 for (i=0; i < h; i++) {
1937 uint32_t yPos = yOffset;
1938 cnt = 0;
1939 for (j=0; j < w; j++) {
1940 for (uint32_t c=0; c < m_header.channels; c++) {
1941 buff16[cnt + channelMap[c]] = Clamp16((m_channel[c][yPos] + yuvOffset16) << shift);
1942 }
1943 cnt += channels;
1944 yPos++;
1945 }
1946 yOffset += yw;
1947 buff16 += pitch16;
1948
1949 if (cb) {
1950 percent += dP;
1951 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1952 }
1953 }
1954 } else {
1955 ASSERT(bpp%8 == 0);
1956 const int shift = __max(0, UsedBitsPerChannel() - 8);
1957 channels = bpp/8; ASSERT(channels >= m_header.channels);
1958
1959 for (i=0; i < h; i++) {
1960 uint32_t yPos = yOffset;
1961 cnt = 0;
1962 for (j=0; j < w; j++) {
1963 for (uint32_t c=0; c < m_header.channels; c++) {
1964 buff[cnt + channelMap[c]] = Clamp8((m_channel[c][yPos] + yuvOffset16) >> shift);
1965 }
1966 cnt += channels;
1967 yPos++;
1968 }
1969 yOffset += yw;
1970 buff += pitch;
1971
1972 if (cb) {
1973 percent += dP;
1974 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
1975 }
1976 }
1977 }
1978 break;
1979 }
1980 case ImageModeRGBColor:
1981 {
1982 ASSERT(m_header.channels == 3);
1983 ASSERT(m_header.bpp == m_header.channels*8);
1984 ASSERT(bpp%8 == 0);
1985 ASSERT(bpp >= m_header.bpp);
1986
1987 DataT* y = m_channel[0]; ASSERT(y);
1988 DataT* u = m_channel[1]; ASSERT(u);
1989 DataT* v = m_channel[2]; ASSERT(v);
1990 uint8_t *buffg = &buff[channelMap[1]],
1991 *buffr = &buff[channelMap[2]],
1992 *buffb = &buff[channelMap[0]];
1993 uint8_t g;
1994 uint32_t cnt, channels = bpp/8;
1995
1996 if (m_downsample) {
1997 for (i=0; i < h; i++) {
1998 uint32_t uPos = uOffset;
1999 uint32_t yPos = yOffset;
2000 cnt = 0;
2001 for (j=0; j < w; j++) {
2002 // u and v are downsampled
2003 uAvg = u[uPos];
2004 vAvg = v[uPos];
2005 // Yuv
2006 buffg[cnt] = g = Clamp8(y[yPos] + YUVoffset8 - ((uAvg + vAvg ) >> 2)); // must be logical shift operator
2007 buffr[cnt] = Clamp8(uAvg + g);
2008 buffb[cnt] = Clamp8(vAvg + g);
2009 cnt += channels;
2010 if (j & 1) uPos++;
2011 yPos++;
2012 }
2013 if (i & 1) uOffset += uw;
2014 yOffset += yw;
2015 buffb += pitch;
2016 buffg += pitch;
2017 buffr += pitch;
2018
2019 if (cb) {
2020 percent += dP;
2021 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2022 }
2023 }
2024
2025 } else {
2026 for (i=0; i < h; i++) {
2027 cnt = 0;
2028 uint32_t yPos = yOffset;
2029 for (j = 0; j < w; j++) {
2030 uAvg = u[yPos];
2031 vAvg = v[yPos];
2032 // Yuv
2033 buffg[cnt] = g = Clamp8(y[yPos] + YUVoffset8 - ((uAvg + vAvg ) >> 2)); // must be logical shift operator
2034 buffr[cnt] = Clamp8(uAvg + g);
2035 buffb[cnt] = Clamp8(vAvg + g);
2036 cnt += channels;
2037 yPos++;
2038 }
2039 yOffset += yw;
2040 buffb += pitch;
2041 buffg += pitch;
2042 buffr += pitch;
2043
2044 if (cb) {
2045 percent += dP;
2046 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2047 }
2048 }
2049 }
2050 break;
2051 }
2052 case ImageModeRGB48:
2053 {
2054 ASSERT(m_header.channels == 3);
2055 ASSERT(m_header.bpp == 48);
2056
2057 const DataT yuvOffset16 = 1 << (UsedBitsPerChannel() - 1);
2058
2059 DataT* y = m_channel[0]; ASSERT(y);
2060 DataT* u = m_channel[1]; ASSERT(u);
2061 DataT* v = m_channel[2]; ASSERT(v);
2062 uint32_t cnt, channels;
2063 DataT g;
2064
2065 if (bpp >= 48 && bpp%16 == 0) {
2066 const int shift = 16 - UsedBitsPerChannel(); ASSERT(shift >= 0);
2067 uint16_t *buff16 = (uint16_t *)buff;
2068 int pitch16 = pitch/2;
2069 channels = bpp/16; ASSERT(channels >= m_header.channels);
2070
2071 for (i=0; i < h; i++) {
2072 uint32_t uPos = uOffset;
2073 uint32_t yPos = yOffset;
2074 cnt = 0;
2075 for (j=0; j < w; j++) {
2076 uAvg = u[uPos];
2077 vAvg = v[uPos];
2078 // Yuv
2079 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2); // must be logical shift operator
2080 buff16[cnt + channelMap[1]] = Clamp16(g << shift);
2081 buff16[cnt + channelMap[2]] = Clamp16((uAvg + g) << shift);
2082 buff16[cnt + channelMap[0]] = Clamp16((vAvg + g) << shift);
2083 cnt += channels;
2084 if (!m_downsample || (j & 1)) uPos++;
2085 yPos++;
2086 }
2087 if (!m_downsample || (i & 1)) uOffset += uw;
2088 yOffset += yw;
2089 buff16 += pitch16;
2090
2091 if (cb) {
2092 percent += dP;
2093 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2094 }
2095 }
2096 } else {
2097 ASSERT(bpp%8 == 0);
2098 const int shift = __max(0, UsedBitsPerChannel() - 8);
2099 channels = bpp/8; ASSERT(channels >= m_header.channels);
2100
2101 for (i=0; i < h; i++) {
2102 uint32_t uPos = uOffset;
2103 uint32_t yPos = yOffset;
2104 cnt = 0;
2105 for (j=0; j < w; j++) {
2106 uAvg = u[uPos];
2107 vAvg = v[uPos];
2108 // Yuv
2109 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2); // must be logical shift operator
2110 buff[cnt + channelMap[1]] = Clamp8(g >> shift);
2111 buff[cnt + channelMap[2]] = Clamp8((uAvg + g) >> shift);
2112 buff[cnt + channelMap[0]] = Clamp8((vAvg + g) >> shift);
2113 cnt += channels;
2114 if (!m_downsample || (j & 1)) uPos++;
2115 yPos++;
2116 }
2117 if (!m_downsample || (i & 1)) uOffset += uw;
2118 yOffset += yw;
2119 buff += pitch;
2120
2121 if (cb) {
2122 percent += dP;
2123 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2124 }
2125 }
2126 }
2127 break;
2128 }
2129 case ImageModeLabColor:
2130 {
2131 ASSERT(m_header.channels == 3);
2132 ASSERT(m_header.bpp == m_header.channels*8);
2133 ASSERT(bpp%8 == 0);
2134
2135 DataT* l = m_channel[0]; ASSERT(l);
2136 DataT* a = m_channel[1]; ASSERT(a);
2137 DataT* b = m_channel[2]; ASSERT(b);
2138 uint32_t cnt, channels = bpp/8; ASSERT(channels >= m_header.channels);
2139
2140 for (i=0; i < h; i++) {
2141 uint32_t uPos = uOffset;
2142 uint32_t yPos = yOffset;
2143 cnt = 0;
2144 for (j=0; j < w; j++) {
2145 uAvg = a[uPos];
2146 vAvg = b[uPos];
2147 buff[cnt + channelMap[0]] = Clamp8(l[yPos] + YUVoffset8);
2148 buff[cnt + channelMap[1]] = Clamp8(uAvg + YUVoffset8);
2149 buff[cnt + channelMap[2]] = Clamp8(vAvg + YUVoffset8);
2150 cnt += channels;
2151 if (!m_downsample || (j & 1)) uPos++;
2152 yPos++;
2153 }
2154 if (!m_downsample || (i & 1)) uOffset += uw;
2155 yOffset += yw;
2156 buff += pitch;
2157
2158 if (cb) {
2159 percent += dP;
2160 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2161 }
2162 }
2163 break;
2164 }
2165 case ImageModeLab48:
2166 {
2167 ASSERT(m_header.channels == 3);
2168 ASSERT(m_header.bpp == m_header.channels*16);
2169
2170 const DataT yuvOffset16 = 1 << (UsedBitsPerChannel() - 1);
2171
2172 DataT* l = m_channel[0]; ASSERT(l);
2173 DataT* a = m_channel[1]; ASSERT(a);
2174 DataT* b = m_channel[2]; ASSERT(b);
2175 uint32_t cnt, channels;
2176
2177 if (bpp%16 == 0) {
2178 const int shift = 16 - UsedBitsPerChannel(); ASSERT(shift >= 0);
2179 uint16_t *buff16 = (uint16_t *)buff;
2180 int pitch16 = pitch/2;
2181 channels = bpp/16; ASSERT(channels >= m_header.channels);
2182
2183 for (i=0; i < h; i++) {
2184 uint32_t uPos = uOffset;
2185 uint32_t yPos = yOffset;
2186 cnt = 0;
2187 for (j=0; j < w; j++) {
2188 uAvg = a[uPos];
2189 vAvg = b[uPos];
2190 buff16[cnt + channelMap[0]] = Clamp16((l[yPos] + yuvOffset16) << shift);
2191 buff16[cnt + channelMap[1]] = Clamp16((uAvg + yuvOffset16) << shift);
2192 buff16[cnt + channelMap[2]] = Clamp16((vAvg + yuvOffset16) << shift);
2193 cnt += channels;
2194 if (!m_downsample || (j & 1)) uPos++;
2195 yPos++;
2196 }
2197 if (!m_downsample || (i & 1)) uOffset += uw;
2198 yOffset += yw;
2199 buff16 += pitch16;
2200
2201 if (cb) {
2202 percent += dP;
2203 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2204 }
2205 }
2206 } else {
2207 ASSERT(bpp%8 == 0);
2208 const int shift = __max(0, UsedBitsPerChannel() - 8);
2209 channels = bpp/8; ASSERT(channels >= m_header.channels);
2210
2211 for (i=0; i < h; i++) {
2212 uint32_t uPos = uOffset;
2213 uint32_t yPos = yOffset;
2214 cnt = 0;
2215 for (j=0; j < w; j++) {
2216 uAvg = a[uPos];
2217 vAvg = b[uPos];
2218 buff[cnt + channelMap[0]] = Clamp8((l[yPos] + yuvOffset16) >> shift);
2219 buff[cnt + channelMap[1]] = Clamp8((uAvg + yuvOffset16) >> shift);
2220 buff[cnt + channelMap[2]] = Clamp8((vAvg + yuvOffset16) >> shift);
2221 cnt += channels;
2222 if (!m_downsample || (j & 1)) uPos++;
2223 yPos++;
2224 }
2225 if (!m_downsample || (i & 1)) uOffset += uw;
2226 yOffset += yw;
2227 buff += pitch;
2228
2229 if (cb) {
2230 percent += dP;
2231 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2232 }
2233 }
2234 }
2235 break;
2236 }
2237 case ImageModeRGBA:
2238 case ImageModeCMYKColor:
2239 {
2240 ASSERT(m_header.channels == 4);
2241 ASSERT(m_header.bpp == m_header.channels*8);
2242 ASSERT(bpp%8 == 0);
2243
2244 DataT* y = m_channel[0]; ASSERT(y);
2245 DataT* u = m_channel[1]; ASSERT(u);
2246 DataT* v = m_channel[2]; ASSERT(v);
2247 DataT* a = m_channel[3]; ASSERT(a);
2248 uint8_t g, aAvg;
2249 uint32_t cnt, channels = bpp/8; ASSERT(channels >= m_header.channels);
2250
2251 for (i=0; i < h; i++) {
2252 uint32_t uPos = uOffset;
2253 uint32_t yPos = yOffset;
2254 cnt = 0;
2255 for (j=0; j < w; j++) {
2256 uAvg = u[uPos];
2257 vAvg = v[uPos];
2258 aAvg = Clamp8(a[uPos] + YUVoffset8);
2259 // Yuv
2260 buff[cnt + channelMap[1]] = g = Clamp8(y[yPos] + YUVoffset8 - ((uAvg + vAvg ) >> 2)); // must be logical shift operator
2261 buff[cnt + channelMap[2]] = Clamp8(uAvg + g);
2262 buff[cnt + channelMap[0]] = Clamp8(vAvg + g);
2263 buff[cnt + channelMap[3]] = aAvg;
2264 cnt += channels;
2265 if (!m_downsample || (j & 1)) uPos++;
2266 yPos++;
2267 }
2268 if (!m_downsample || (i & 1)) uOffset += uw;
2269 yOffset += yw;
2270 buff += pitch;
2271
2272 if (cb) {
2273 percent += dP;
2274 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2275 }
2276 }
2277 break;
2278 }
2279 case ImageModeCMYK64:
2280 {
2281 ASSERT(m_header.channels == 4);
2282 ASSERT(m_header.bpp == 64);
2283
2284 const DataT yuvOffset16 = 1 << (UsedBitsPerChannel() - 1);
2285
2286 DataT* y = m_channel[0]; ASSERT(y);
2287 DataT* u = m_channel[1]; ASSERT(u);
2288 DataT* v = m_channel[2]; ASSERT(v);
2289 DataT* a = m_channel[3]; ASSERT(a);
2290 DataT g, aAvg;
2291 uint32_t cnt, channels;
2292
2293 if (bpp%16 == 0) {
2294 const int shift = 16 - UsedBitsPerChannel(); ASSERT(shift >= 0);
2295 uint16_t *buff16 = (uint16_t *)buff;
2296 int pitch16 = pitch/2;
2297 channels = bpp/16; ASSERT(channels >= m_header.channels);
2298
2299 for (i=0; i < h; i++) {
2300 uint32_t uPos = uOffset;
2301 uint32_t yPos = yOffset;
2302 cnt = 0;
2303 for (j=0; j < w; j++) {
2304 uAvg = u[uPos];
2305 vAvg = v[uPos];
2306 aAvg = a[uPos] + yuvOffset16;
2307 // Yuv
2308 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2); // must be logical shift operator
2309 buff16[cnt + channelMap[1]] = Clamp16(g << shift);
2310 buff16[cnt + channelMap[2]] = Clamp16((uAvg + g) << shift);
2311 buff16[cnt + channelMap[0]] = Clamp16((vAvg + g) << shift);
2312 buff16[cnt + channelMap[3]] = Clamp16(aAvg << shift);
2313 cnt += channels;
2314 if (!m_downsample || (j & 1)) uPos++;
2315 yPos++;
2316 }
2317 if (!m_downsample || (i & 1)) uOffset += uw;
2318 yOffset += yw;
2319 buff16 += pitch16;
2320
2321 if (cb) {
2322 percent += dP;
2323 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2324 }
2325 }
2326 } else {
2327 ASSERT(bpp%8 == 0);
2328 const int shift = __max(0, UsedBitsPerChannel() - 8);
2329 channels = bpp/8; ASSERT(channels >= m_header.channels);
2330
2331 for (i=0; i < h; i++) {
2332 uint32_t uPos = uOffset;
2333 uint32_t yPos = yOffset;
2334 cnt = 0;
2335 for (j=0; j < w; j++) {
2336 uAvg = u[uPos];
2337 vAvg = v[uPos];
2338 aAvg = a[uPos] + yuvOffset16;
2339 // Yuv
2340 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2); // must be logical shift operator
2341 buff[cnt + channelMap[1]] = Clamp8(g >> shift);
2342 buff[cnt + channelMap[2]] = Clamp8((uAvg + g) >> shift);
2343 buff[cnt + channelMap[0]] = Clamp8((vAvg + g) >> shift);
2344 buff[cnt + channelMap[3]] = Clamp8(aAvg >> shift);
2345 cnt += channels;
2346 if (!m_downsample || (j & 1)) uPos++;
2347 yPos++;
2348 }
2349 if (!m_downsample || (i & 1)) uOffset += uw;
2350 yOffset += yw;
2351 buff += pitch;
2352
2353 if (cb) {
2354 percent += dP;
2355 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2356 }
2357 }
2358 }
2359 break;
2360 }
2361 #ifdef __PGF32SUPPORT__
2362 case ImageModeGray32:
2363 {
2364 ASSERT(m_header.channels == 1);
2365 ASSERT(m_header.bpp == 32);
2366
2367 const int yuvOffset31 = 1 << (UsedBitsPerChannel() - 1);
2368 DataT* y = m_channel[0]; ASSERT(y);
2369
2370 if (bpp == 32) {
2371 const int shift = 31 - UsedBitsPerChannel(); ASSERT(shift >= 0);
2372 uint32_t *buff32 = (uint32_t *)buff;
2373 int pitch32 = pitch/4;
2374
2375 for (i=0; i < h; i++) {
2376 uint32_t yPos = yOffset;
2377 for (j = 0; j < w; j++) {
2378 buff32[j] = Clamp31((y[yPos++] + yuvOffset31) << shift);
2379 }
2380 yOffset += yw;
2381 buff32 += pitch32;
2382
2383 if (cb) {
2384 percent += dP;
2385 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2386 }
2387 }
2388 } else if (bpp == 16) {
2389 const int usedBits = UsedBitsPerChannel();
2390 uint16_t *buff16 = (uint16_t *)buff;
2391 int pitch16 = pitch/2;
2392
2393 if (usedBits < 16) {
2394 const int shift = 16 - usedBits;
2395 for (i=0; i < h; i++) {
2396 uint32_t yPos = yOffset;
2397 for (j = 0; j < w; j++) {
2398 buff16[j] = Clamp16((y[yPos++] + yuvOffset31) << shift);
2399 }
2400 yOffset += yw;
2401 buff16 += pitch16;
2402
2403 if (cb) {
2404 percent += dP;
2405 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2406 }
2407 }
2408 } else {
2409 const int shift = __max(0, usedBits - 16);
2410 for (i=0; i < h; i++) {
2411 uint32_t yPos = yOffset;
2412 for (j = 0; j < w; j++) {
2413 buff16[j] = Clamp16((y[yPos++] + yuvOffset31) >> shift);
2414 }
2415 yOffset += yw;
2416 buff16 += pitch16;
2417
2418 if (cb) {
2419 percent += dP;
2420 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2421 }
2422 }
2423 }
2424 } else {
2425 ASSERT(bpp == 8);
2426 const int shift = __max(0, UsedBitsPerChannel() - 8);
2427
2428 for (i=0; i < h; i++) {
2429 uint32_t yPos = yOffset;
2430 for (j = 0; j < w; j++) {
2431 buff[j] = Clamp8((y[yPos++] + yuvOffset31) >> shift);
2432 }
2433 yOffset += yw;
2434 buff += pitch;
2435
2436 if (cb) {
2437 percent += dP;
2438 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2439 }
2440 }
2441 }
2442 break;
2443 }
2444 #endif
2445 case ImageModeRGB12:
2446 {
2447 ASSERT(m_header.channels == 3);
2448 ASSERT(m_header.bpp == m_header.channels*4);
2449 ASSERT(bpp == m_header.channels*4);
2450 ASSERT(!m_downsample);
2451
2452 DataT* y = m_channel[0]; ASSERT(y);
2453 DataT* u = m_channel[1]; ASSERT(u);
2454 DataT* v = m_channel[2]; ASSERT(v);
2455 uint16_t yval;
2456 uint32_t cnt;
2457
2458 for (i=0; i < h; i++) {
2459 uint32_t yPos = yOffset;
2460 cnt = 0;
2461 for (j=0; j < w; j++) {
2462 // Yuv
2463 uAvg = u[yPos];
2464 vAvg = v[yPos];
2465 yval = Clamp4(y[yPos] + YUVoffset4 - ((uAvg + vAvg ) >> 2)); // must be logical shift operator
2466 if (j%2 == 0) {
2467 buff[cnt] = uint8_t(Clamp4(vAvg + yval) | (yval << 4));
2468 cnt++;
2469 buff[cnt] = Clamp4(uAvg + yval);
2470 } else {
2471 buff[cnt] |= Clamp4(vAvg + yval) << 4;
2472 cnt++;
2473 buff[cnt] = uint8_t(yval | (Clamp4(uAvg + yval) << 4));
2474 cnt++;
2475 }
2476 yPos++;
2477 }
2478 yOffset += yw;
2479 buff += pitch;
2480
2481 if (cb) {
2482 percent += dP;
2483 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2484 }
2485 }
2486 break;
2487 }
2488 case ImageModeRGB16:
2489 {
2490 ASSERT(m_header.channels == 3);
2491 ASSERT(m_header.bpp == 16);
2492 ASSERT(bpp == 16);
2493 ASSERT(!m_downsample);
2494
2495 DataT* y = m_channel[0]; ASSERT(y);
2496 DataT* u = m_channel[1]; ASSERT(u);
2497 DataT* v = m_channel[2]; ASSERT(v);
2498 uint16_t yval;
2499 uint16_t *buff16 = (uint16_t *)buff;
2500 int pitch16 = pitch/2;
2501
2502 for (i=0; i < h; i++) {
2503 uint32_t yPos = yOffset;
2504 for (j = 0; j < w; j++) {
2505 // Yuv
2506 uAvg = u[yPos];
2507 vAvg = v[yPos];
2508 yval = Clamp6(y[yPos++] + YUVoffset6 - ((uAvg + vAvg ) >> 2)); // must be logical shift operator
2509 buff16[j] = (yval << 5) | ((Clamp6(uAvg + yval) >> 1) << 11) | (Clamp6(vAvg + yval) >> 1);
2510 }
2511 yOffset += yw;
2512 buff16 += pitch16;
2513
2514 if (cb) {
2515 percent += dP;
2516 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2517 }
2518 }
2519 break;
2520 }
2521 default:
2522 ASSERT(false);
2523 }
2524
2525 #ifdef _DEBUG
2526 // display ROI (RGB) in debugger
2527 roiimage.width = w;
2528 roiimage.height = h;
2529 if (pitch > 0) {
2530 roiimage.pitch = pitch;
2531 roiimage.data = buff;
2532 } else {
2533 roiimage.pitch = -pitch;
2534 roiimage.data = buff + (h - 1)*pitch;
2535 }
2536 #endif
2537
2538 }
2539
2554 void CPGFImage::GetYUV(int pitch, DataT* buff, uint8_t bpp, int channelMap[] /*= nullptr*/, CallbackPtr cb /*= nullptr*/, void *data /*=nullptr*/) const {
2555 ASSERT(buff);
2556 const uint32_t w = m_width[0];
2557 const uint32_t h = m_height[0];
2558 const bool wOdd = (1 == w%2);
2559 const int dataBits = DataTSize*8; ASSERT(dataBits == 16 || dataBits == 32);
2560 const int pitch2 = pitch/DataTSize;
2561 const int yuvOffset = (dataBits == 16) ? YUVoffset8 : YUVoffset16;
2562 const double dP = 1.0/h;
2563
2564 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(sizeof(defMap)/sizeof(defMap[0]) == MaxChannels);
2565 if (channelMap == nullptr) channelMap = defMap;
2566 int sampledPos = 0, yPos = 0;
2567 DataT uAvg, vAvg;
2568 double percent = 0;
2569 uint32_t i, j;
2570
2571 if (m_header.channels == 3) {
2572 ASSERT(bpp%dataBits == 0);
2573
2574 DataT* y = m_channel[0]; ASSERT(y);
2575 DataT* u = m_channel[1]; ASSERT(u);
2576 DataT* v = m_channel[2]; ASSERT(v);
2577 int cnt, channels = bpp/dataBits; ASSERT(channels >= m_header.channels);
2578
2579 for (i=0; i < h; i++) {
2580 if (i%2) sampledPos -= (w + 1)/2;
2581 cnt = 0;
2582 for (j=0; j < w; j++) {
2583 if (m_downsample) {
2584 // image was downsampled
2585 uAvg = u[sampledPos];
2586 vAvg = v[sampledPos];
2587 } else {
2588 uAvg = u[yPos];
2589 vAvg = v[yPos];
2590 }
2591 buff[cnt + channelMap[0]] = y[yPos];
2592 buff[cnt + channelMap[1]] = uAvg;
2593 buff[cnt + channelMap[2]] = vAvg;
2594 yPos++;
2595 cnt += channels;
2596 if (j%2) sampledPos++;
2597 }
2598 buff += pitch2;
2599 if (wOdd) sampledPos++;
2600
2601 if (cb) {
2602 percent += dP;
2603 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2604 }
2605 }
2606 } else if (m_header.channels == 4) {
2607 ASSERT(m_header.bpp == m_header.channels*8);
2608 ASSERT(bpp%dataBits == 0);
2609
2610 DataT* y = m_channel[0]; ASSERT(y);
2611 DataT* u = m_channel[1]; ASSERT(u);
2612 DataT* v = m_channel[2]; ASSERT(v);
2613 DataT* a = m_channel[3]; ASSERT(a);
2614 uint8_t aAvg;
2615 int cnt, channels = bpp/dataBits; ASSERT(channels >= m_header.channels);
2616
2617 for (i=0; i < h; i++) {
2618 if (i%2) sampledPos -= (w + 1)/2;
2619 cnt = 0;
2620 for (j=0; j < w; j++) {
2621 if (m_downsample) {
2622 // image was downsampled
2623 uAvg = u[sampledPos];
2624 vAvg = v[sampledPos];
2625 aAvg = Clamp8(a[sampledPos] + yuvOffset);
2626 } else {
2627 uAvg = u[yPos];
2628 vAvg = v[yPos];
2629 aAvg = Clamp8(a[yPos] + yuvOffset);
2630 }
2631 // Yuv
2632 buff[cnt + channelMap[0]] = y[yPos];
2633 buff[cnt + channelMap[1]] = uAvg;
2634 buff[cnt + channelMap[2]] = vAvg;
2635 buff[cnt + channelMap[3]] = aAvg;
2636 yPos++;
2637 cnt += channels;
2638 if (j%2) sampledPos++;
2639 }
2640 buff += pitch2;
2641 if (wOdd) sampledPos++;
2642
2643 if (cb) {
2644 percent += dP;
2645 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2646 }
2647 }
2648 }
2649 }
2650
2665 void CPGFImage::ImportYUV(int pitch, DataT *buff, uint8_t bpp, int channelMap[] /*= nullptr*/, CallbackPtr cb /*= nullptr*/, void *data /*=nullptr*/) {
2666 ASSERT(buff);
2667 const double dP = 1.0/m_header.height;
2668 const int dataBits = DataTSize*8; ASSERT(dataBits == 16 || dataBits == 32);
2669 const int pitch2 = pitch/DataTSize;
2670 const int yuvOffset = (dataBits == 16) ? YUVoffset8 : YUVoffset16;
2671
2672 int yPos = 0, cnt = 0;
2673 double percent = 0;
2674 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(sizeof(defMap)/sizeof(defMap[0]) == MaxChannels);
2675
2676 if (channelMap == nullptr) channelMap = defMap;
2677
2678 if (m_header.channels == 3) {
2679 ASSERT(bpp%dataBits == 0);
2680
2681 DataT* y = m_channel[0]; ASSERT(y);
2682 DataT* u = m_channel[1]; ASSERT(u);
2683 DataT* v = m_channel[2]; ASSERT(v);
2684 const int channels = bpp/dataBits; ASSERT(channels >= m_header.channels);
2685
2686 for (uint32_t h=0; h < m_header.height; h++) {
2687 if (cb) {
2688 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2689 percent += dP;
2690 }
2691
2692 cnt = 0;
2693 for (uint32_t w=0; w < m_header.width; w++) {
2694 y[yPos] = buff[cnt + channelMap[0]];
2695 u[yPos] = buff[cnt + channelMap[1]];
2696 v[yPos] = buff[cnt + channelMap[2]];
2697 yPos++;
2698 cnt += channels;
2699 }
2700 buff += pitch2;
2701 }
2702 } else if (m_header.channels == 4) {
2703 ASSERT(bpp%dataBits == 0);
2704
2705 DataT* y = m_channel[0]; ASSERT(y);
2706 DataT* u = m_channel[1]; ASSERT(u);
2707 DataT* v = m_channel[2]; ASSERT(v);
2708 DataT* a = m_channel[3]; ASSERT(a);
2709 const int channels = bpp/dataBits; ASSERT(channels >= m_header.channels);
2710
2711 for (uint32_t h=0; h < m_header.height; h++) {
2712 if (cb) {
2713 if ((*cb)(percent, true, data)) ReturnWithError(EscapePressed);
2714 percent += dP;
2715 }
2716
2717 cnt = 0;
2718 for (uint32_t w=0; w < m_header.width; w++) {
2719 y[yPos] = buff[cnt + channelMap[0]];
2720 u[yPos] = buff[cnt + channelMap[1]];
2721 v[yPos] = buff[cnt + channelMap[2]];
2722 a[yPos] = buff[cnt + channelMap[3]] - yuvOffset;
2723 yPos++;
2724 cnt += channels;
2725 }
2726 buff += pitch2;
2727 }
2728 }
2729
2730 if (m_downsample) {
2731 // Subsampling of the chrominance and alpha channels
2732 for (int i=1; i < m_header.channels; i++) {
2733 Downsample(i);
2734 }
2735 }
2736 }
2737}
PGF decoder class.
PGF encoder class.
PGF image class.
constexpr uint8_t ImageModeCMYKColor
constexpr uint8_t ImageModeRGB48
constexpr uint8_t ImageModeUnknown
constexpr uint8_t ImageModeBitmap
constexpr uint8_t ImageModeGray16
constexpr uint8_t ImageModeLab48
constexpr uint8_t ImageModeRGB12
constexpr uint8_t ImageModeRGBA
constexpr uint8_t ImageModeRGB16
constexpr uint8_t ImageModeGrayScale
constexpr uint8_t ImageModeRGBColor
constexpr uint8_t ImageModeGray32
constexpr uint8_t ImageModeIndexedColor
constexpr uint8_t ImageModeHSBColor
constexpr uint8_t ImageModeLabColor
#define __min(x, y)
constexpr uint8_t ImageModeHSLColor
constexpr uint8_t ImageModeCMYK64
#define __max(x, y)
#define PGFWeek
Definition PGFtypes.h:45
#define PGFYear
Definition PGFtypes.h:44
#define PGFMajorNumber
Definition PGFtypes.h:43
void Skip(uint64_t offset)
Definition Decoder.cpp:450
uint32_t ReadEncodedData(uint8_t *target, uint32_t len) const
Definition Decoder.cpp:247
void SetStreamPosToData()
Resets stream position to beginning of data block.
Definition Decoder.h:139
void DecodeInterleaved(CWaveletTransform *wtChannel, int level, int quantParam)
Definition Decoder.cpp:334
void GetNextMacroBlock()
Definition Decoder.cpp:478
CDecoder(CPGFStream *stream, PGFPreHeader &preHeader, PGFHeader &header, PGFPostHeader &postHeader, uint32_t *&levelLength, uint64_t &userDataPos, bool useOMP, uint32_t userDataPolicy)
Definition Decoder.cpp:74
uint32_t GetEncodedHeaderLength() const
Definition Decoder.h:131
void SetStreamPosToStart()
Resets stream position to beginning of PGF pre-header.
Definition Decoder.h:135
CEncoder(CPGFStream *stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader &postHeader, uint64_t &userDataPos, bool useOMP)
Definition Encoder.cpp:71
void FavorSpeedOverSize()
Encoder favors speed over compression size.
Definition Encoder.h:116
int64_t ComputeHeaderLength() const
Definition Encoder.h:169
uint32_t WriteLevelLength(uint32_t *&levelLength)
Definition Encoder.cpp:178
void SetEncodedLevel(int currentLevel)
Definition Encoder.h:157
void SetStreamPosToStart()
Resets stream position to beginning of PGF pre-header.
Definition Encoder.h:183
uint32_t UpdateLevelLength()
Definition Encoder.cpp:203
void UpdatePostHeaderSize(PGFPreHeader preHeader)
Definition Encoder.cpp:161
int64_t ComputeBufferLength() const
Definition Encoder.h:174
int64_t ComputeOffset() const
Definition Encoder.h:179
void ComputeLevels()
Definition PGFimage.cpp:858
uint32_t WriteImage(CPGFStream *stream, CallbackPtr cb=nullptr, void *data=nullptr)
void SetHeader(const PGFHeader &header, uint8_t flags=0, const uint8_t *userData=0, uint32_t userDataLength=0)
Definition PGFimage.cpp:898
CWaveletTransform * m_wtChannel[MaxChannels]
wavelet transformed color channels
Definition PGFimage.h:525
uint8_t Clamp8(DataT v) const
Definition PGFimage.h:573
void ImportBitmap(int pitch, uint8_t *buff, uint8_t bpp, int channelMap[]=nullptr, CallbackPtr cb=nullptr, void *data=nullptr)
Definition PGFimage.cpp:796
void Write(CPGFStream *stream, uint32_t *nWrittenBytes=nullptr, CallbackPtr cb=nullptr, void *data=nullptr)
CPGFImage()
Standard constructor.
Definition PGFimage.cpp:65
PGFRect m_roi
region of interest
Definition PGFimage.h:545
PGFPreHeader m_preHeader
PGF pre-header.
Definition PGFimage.h:532
uint32_t * m_levelLength
length of each level in bytes; first level starts immediately after this array
Definition PGFimage.h:529
uint16_t Clamp16(DataT v) const
Definition PGFimage.h:577
bool ROIisSupported() const
Definition PGFimage.h:470
void ImportYUV(int pitch, DataT *buff, uint8_t bpp, int channelMap[]=nullptr, CallbackPtr cb=nullptr, void *data=nullptr)
void Reconstruct(int level=0)
Definition PGFimage.cpp:352
const RGBQUAD * GetColorTable() const
Definition PGFimage.h:334
void Open(CPGFStream *stream)
Definition PGFimage.cpp:145
static uint8_t CodecMajorVersion(uint8_t version=PGFVersion)
Return major version.
Definition PGFimage.cpp:772
void ResetStreamPos(bool startOfData)
Definition PGFimage.cpp:686
uint32_t GetEncodedLevelLength(int level) const
Definition PGFimage.h:371
static bool ImportIsSupported(uint8_t mode)
PGFHeader m_header
PGF file header.
Definition PGFimage.h:533
uint8_t UsedBitsPerChannel() const
Definition PGFimage.cpp:760
uint32_t GetEncodedHeaderLength() const
Definition PGFimage.cpp:652
uint32_t ReadEncodedData(int level, uint8_t *target, uint32_t targetLen) const
Definition PGFimage.cpp:711
void SetMaxValue(uint32_t maxValue)
Definition PGFimage.cpp:742
void SetColorTable(uint32_t iFirstColor, uint32_t nColors, const RGBQUAD *prgbColors)
uint16_t Clamp6(DataT v) const
Definition PGFimage.h:570
void RgbToYuv(int pitch, uint8_t *rgbBuff, uint8_t bpp, int channelMap[], CallbackPtr cb, void *data)
uint32_t m_height[MaxChannels]
height of each channel at current level
Definition PGFimage.h:531
uint32_t Clamp31(DataT v) const
Definition PGFimage.h:580
uint32_t m_userDataPolicy
user data (metadata) policy during open
Definition PGFimage.h:537
double m_percent
progress [0..1]
Definition PGFimage.h:551
RefreshCB m_cb
pointer to refresh callback procedure
Definition PGFimage.h:549
uint64_t m_userDataPos
stream position of user data
Definition PGFimage.h:535
uint32_t UpdatePostHeaderSize()
void GetBitmap(int pitch, uint8_t *buff, uint8_t bpp, int channelMap[]=nullptr, CallbackPtr cb=nullptr, void *data=nullptr) const
virtual ~CPGFImage()
Destructor.
Definition PGFimage.cpp:121
bool CompleteHeader()
Definition PGFimage.cpp:222
uint32_t m_width[MaxChannels]
width of each channel at current level
Definition PGFimage.h:530
uint32_t ReadEncodedHeader(uint8_t *target, uint32_t targetLen) const
Definition PGFimage.cpp:664
void * m_cbArg
refresh callback argument
Definition PGFimage.h:550
bool m_streamReinitialized
stream has been reinitialized
Definition PGFimage.h:544
PGFPostHeader m_postHeader
PGF post-header.
Definition PGFimage.h:534
void GetYUV(int pitch, DataT *buff, uint8_t bpp, int channelMap[]=nullptr, CallbackPtr cb=nullptr, void *data=nullptr) const
PGFRect ComputeLevelROI() const
Definition PGFimage.cpp:587
void Read(int level=0, CallbackPtr cb=nullptr, void *data=nullptr)
Definition PGFimage.cpp:406
bool m_downsample
chrominance channels are downsampled
Definition PGFimage.h:539
static uint32_t LevelSizeL(uint32_t size, int level)
Definition PGFimage.h:503
CEncoder * m_encoder
PGF encoder.
Definition PGFimage.h:528
bool m_favorSpeedOverSize
favor encoding speed over compression ratio
Definition PGFimage.h:540
CDecoder * m_decoder
PGF decoder.
Definition PGFimage.h:527
ProgressMode m_progressMode
progress mode used in Read and Write; Relative is default mode
Definition PGFimage.h:552
bool m_useOMPinEncoder
use Open MP in encoder
Definition PGFimage.h:541
const uint8_t * GetUserData(uint32_t &cachedSize, uint32_t *pTotalSize=nullptr) const
Definition PGFimage.cpp:341
void SetROI(PGFRect rect)
Definition PGFimage.cpp:619
uint8_t m_quant
quantization parameter
Definition PGFimage.h:538
void Downsample(int nChannel)
Definition PGFimage.cpp:814
bool m_useOMPinDecoder
use Open MP in decoder
Definition PGFimage.h:542
uint32_t WriteHeader(CPGFStream *stream)
Definition PGFimage.cpp:983
PGFRect GetAlignedROI(int c=0) const
Definition PGFimage.cpp:602
int m_currentLevel
transform level of current image
Definition PGFimage.h:536
DataT * m_channel[MaxChannels]
untransformed channels in YUV format
Definition PGFimage.h:526
uint8_t Clamp4(DataT v) const
Definition PGFimage.h:567
Abstract stream base class.
Definition PGFstream.h:39
virtual void Write(int *count, void *buffer)=0
virtual void Read(int *count, void *buffer)=0
void ExtractTile(CEncoder &encoder, bool tile=false, uint32_t tileX=0, uint32_t tileY=0)
Definition Subband.cpp:178
DataT * GetBuffer()
Definition Subband.h:107
void Dequantize(int quantParam)
Definition Subband.cpp:155
void PlaceTile(CDecoder &decoder, int quantParam, bool tile=false, uint32_t tileX=0, uint32_t tileY=0)
Definition Subband.cpp:204
PGF wavelet transform.
CWaveletTransform(uint32_t width, uint32_t height, int levels, DataT *data=nullptr)
OSError ForwardTransform(int level, int quant)
OSError InverseTransform(int level, uint32_t *width, uint32_t *height, DataT **data)
CSubband * GetSubband(int level, Orient::Orientation orientation)
constexpr Orientation LL
Definition PGFtypes.h:104
constexpr Orientation HH
Definition PGFtypes.h:107
constexpr Orientation LH
Definition PGFtypes.h:106
constexpr Orientation HL
Definition PGFtypes.h:105
constexpr Policy CacheAll
Definition PGFtypes.h:112
Definition PGFimage.h:33
constexpr int PGFVersion
current standard version
Definition PGFtypes.h:76
constexpr DataT YUVoffset6
Definition PGFimage.cpp:38
constexpr size_t HeaderSize
Definition PGFtypes.h:379
constexpr DataT YUVoffset16
Definition PGFimage.cpp:40
constexpr size_t MaxUserDataSize
Definition PGFtypes.h:382
constexpr int MaxChannels
maximum number of (color) channels
Definition PGFtypes.h:64
constexpr const char * PGFMagic
PGF identification.
Definition PGFtypes.h:61
constexpr int MaxLevel
maximum number of transform levels
Definition PGFtypes.h:62
constexpr int MaxQuality
maximum quality
Definition PGFtypes.h:96
constexpr int ColorTableLen
size of color lookup table (clut)
Definition PGFtypes.h:66
constexpr uint32_t FilterSize
constexpr DataT YUVoffset8
Definition PGFimage.cpp:39
int32_t DataT
Definition PGFtypes.h:367
constexpr DataT YUVoffset4
Definition PGFimage.cpp:37
constexpr int Version5
new coding scheme since major version 5
Definition PGFtypes.h:71
constexpr int Version7
Codec major and minor version number stored in PGFHeader.
Definition PGFtypes.h:73
constexpr size_t DataTSize
Definition PGFtypes.h:381
constexpr int Version6
hSize in PGFPreHeader uses 32 bits instead of 16 bits
Definition PGFtypes.h:72
constexpr int Version2
data structure PGFHeader of major version 2
Definition PGFtypes.h:68
constexpr int DownsampleThreshold
if quality is larger than this threshold than downsampling is used
Definition PGFtypes.h:65
constexpr size_t ColorTableSize
Definition PGFtypes.h:380
PGF header.
Definition PGFtypes.h:169
uint8_t usedBitsPerChannel
number of used bits per channel in 16- and 32-bit per channel modes
Definition PGFtypes.h:177
uint8_t bpp
bits per pixel
Definition PGFtypes.h:174
uint8_t channels
number of channels
Definition PGFtypes.h:175
uint8_t nLevels
number of FWT transforms
Definition PGFtypes.h:172
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
uint8_t quality
quantization parameter: 0=lossless, 4=standard, 6=poor quality
Definition PGFtypes.h:173
uint32_t height
image height in pixels
Definition PGFtypes.h:171
PGFVersionNumber version
codec version number: (since Version 7)
Definition PGFtypes.h:178
char magic[3]
PGF identification = "PGF".
Definition PGFtypes.h:127
uint8_t version
PGF version.
Definition PGFtypes.h:128
uint32_t userDataLen
user data size in bytes (not part of file header)
Definition PGFtypes.h:202
uint32_t cachedUserDataLen
cached user data size in bytes (not part of file header)
Definition PGFtypes.h:203
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
uint32_t hSize
total size of PGFHeader, [ColorTable], and [UserData] in bytes (since Version 6: 4 Bytes)
Definition PGFtypes.h:137
Rectangle.
Definition PGFtypes.h:258
uint32_t top
Definition PGFtypes.h:259
uint32_t Height() const
Definition PGFtypes.h:307
uint32_t bottom
Definition PGFtypes.h:259
uint32_t right
Definition PGFtypes.h:259
uint32_t Width() const
Definition PGFtypes.h:304
PGFRect()
Standard constructor.
Definition PGFtypes.h:262
uint32_t left
Definition PGFtypes.h:259
PGFVersionNumber(uint8_t _major, uint8_t _year, uint8_t _week)
Definition PGFtypes.h:156