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

PGF main class. More...

#include <PGFimage.h>

Public Member Functions

 CPGFImage ()
 Standard constructor.
virtual ~CPGFImage ()
 Destructor.
void Destroy ()
void Open (CPGFStream *stream)
bool IsOpen () const
 Returns true if the PGF has been opened for reading.
void Read (int level=0, CallbackPtr cb=nullptr, void *data=nullptr)
void Read (PGFRect &rect, int level=0, CallbackPtr cb=nullptr, void *data=nullptr)
void ReadPreview ()
void Reconstruct (int level=0)
void GetBitmap (int pitch, uint8_t *buff, uint8_t bpp, int channelMap[]=nullptr, CallbackPtr cb=nullptr, void *data=nullptr) const
void GetYUV (int pitch, DataT *buff, uint8_t bpp, int channelMap[]=nullptr, CallbackPtr cb=nullptr, void *data=nullptr) const
void ImportBitmap (int pitch, uint8_t *buff, uint8_t bpp, int channelMap[]=nullptr, CallbackPtr cb=nullptr, void *data=nullptr)
void ImportYUV (int pitch, DataT *buff, uint8_t bpp, int channelMap[]=nullptr, CallbackPtr cb=nullptr, void *data=nullptr)
void Write (CPGFStream *stream, uint32_t *nWrittenBytes=nullptr, CallbackPtr cb=nullptr, void *data=nullptr)
uint32_t WriteHeader (CPGFStream *stream)
uint32_t WriteImage (CPGFStream *stream, CallbackPtr cb=nullptr, void *data=nullptr)
uint32_t Write (int level, CallbackPtr cb=nullptr, void *data=nullptr)
void ConfigureEncoder (bool useOMP=true, bool favorSpeedOverSize=false)
void ConfigureDecoder (bool useOMP=true, UP::Policy policy=UP::CacheAll, uint32_t prefixSize=0)
void ResetStreamPos (bool startOfData)
void SetChannel (DataT *channel, int c=0)
void SetHeader (const PGFHeader &header, uint8_t flags=0, const uint8_t *userData=0, uint32_t userDataLength=0)
void SetMaxValue (uint32_t maxValue)
void SetProgressMode (ProgressMode pm)
void SetRefreshCallback (RefreshCB callback, void *arg)
void SetColorTable (uint32_t iFirstColor, uint32_t nColors, const RGBQUAD *prgbColors)
DataT * GetChannel (int c=0)
void GetColorTable (uint32_t iFirstColor, uint32_t nColors, RGBQUAD *prgbColors) const
const RGBQUAD * GetColorTable () const
const PGFHeader * GetHeader () const
uint32_t GetMaxValue () const
uint64_t GetUserDataPos () const
const uint8_t * GetUserData (uint32_t &cachedSize, uint32_t *pTotalSize=nullptr) const
uint32_t GetEncodedHeaderLength () const
uint32_t GetEncodedLevelLength (int level) const
uint32_t ReadEncodedHeader (uint8_t *target, uint32_t targetLen) const
uint32_t ReadEncodedData (int level, uint8_t *target, uint32_t targetLen) const
uint32_t ChannelWidth (int c=0) const
uint32_t ChannelHeight (int c=0) const
uint8_t ChannelDepth () const
uint32_t Width (int level=0) const
uint32_t Height (int level=0) const
uint8_t Level () const
uint8_t Levels () const
bool IsFullyRead () const
 Return true if all levels have been read.
uint8_t Quality () const
uint8_t Channels () const
uint8_t Mode () const
uint8_t BPP () const
bool ROIisSupported () const
PGFRect ComputeLevelROI () const
uint8_t UsedBitsPerChannel () const
uint8_t Version () const

Static Public Member Functions

static bool ImportIsSupported (uint8_t mode)
static uint32_t LevelSizeL (uint32_t size, int level)
static uint32_t LevelSizeH (uint32_t size, int level)
static uint8_t CodecMajorVersion (uint8_t version=PGFVersion)
 Return major version.
static uint8_t MaxChannelDepth (uint8_t version=PGFVersion)

Protected Attributes

CWaveletTransform * m_wtChannel [MaxChannels]
 wavelet transformed color channels
DataT * m_channel [MaxChannels]
 untransformed channels in YUV format
CDecoder * m_decoder
 PGF decoder.
CEncoder * m_encoder
 PGF encoder.
uint32_t * m_levelLength
 length of each level in bytes; first level starts immediately after this array
uint32_t m_width [MaxChannels]
 width of each channel at current level
uint32_t m_height [MaxChannels]
 height of each channel at current level
PGFPreHeader m_preHeader
 PGF pre-header.
PGFHeader m_header
 PGF file header.
PGFPostHeader m_postHeader
 PGF post-header.
uint64_t m_userDataPos
 stream position of user data
int m_currentLevel
 transform level of current image
uint32_t m_userDataPolicy
 user data (metadata) policy during open
uint8_t m_quant
 quantization parameter
bool m_downsample
 chrominance channels are downsampled
bool m_favorSpeedOverSize
 favor encoding speed over compression ratio
bool m_useOMPinEncoder
 use Open MP in encoder
bool m_useOMPinDecoder
 use Open MP in decoder
bool m_streamReinitialized
 stream has been reinitialized
PGFRect m_roi
 region of interest

Private Member Functions

void Init ()
void ComputeLevels ()
bool CompleteHeader ()
void RgbToYuv (int pitch, uint8_t *rgbBuff, uint8_t bpp, int channelMap[], CallbackPtr cb, void *data)
void Downsample (int nChannel)
uint32_t UpdatePostHeaderSize ()
void WriteLevel ()
PGFRect GetAlignedROI (int c=0) const
void SetROI (PGFRect rect)
uint8_t Clamp4 (DataT v) const
uint16_t Clamp6 (DataT v) const
uint8_t Clamp8 (DataT v) const
uint16_t Clamp16 (DataT v) const
uint32_t Clamp31 (DataT v) const

Private Attributes

RefreshCB m_cb
 pointer to refresh callback procedure
void * m_cbArg
 refresh callback argument
double m_percent
 progress [0..1]
ProgressMode m_progressMode
 progress mode used in Read and Write; Relative is default mode

Detailed Description

PGF main class.

PGF image class is the main class. You always need a PGF object for encoding or decoding image data. Decoding: Open() Read() GetBitmap() Encoding: SetHeader() ImportBitmap() Write()

Author
C. Stamm, R. Spuler

Definition at line 53 of file PGFimage.h.

Constructor & Destructor Documentation

◆ CPGFImage()

pgf::CPGFImage::CPGFImage ( )

Standard constructor.

Definition at line 65 of file PGFimage.cpp.

65 {
66 Init();
67 }

◆ ~CPGFImage()

pgf::CPGFImage::~CPGFImage ( )
virtual

Destructor.

Definition at line 121 of file PGFimage.cpp.

121 {
122 m_currentLevel = -100; // unusual value used as marker in Destroy()
123 Destroy();
124 }
int m_currentLevel
transform level of current image
Definition PGFimage.h:536

Member Function Documentation

◆ BPP()

uint8_t pgf::CPGFImage::BPP ( ) const
inline

Return the number of bits per pixel. Valid values can be 1, 8, 12, 16, 24, 32, 48, 64.

Returns
Number of bits per pixel.

Definition at line 465 of file PGFimage.h.

465{ return m_header.bpp; }
PGFHeader m_header
PGF file header.
Definition PGFimage.h:533
uint8_t bpp
bits per pixel
Definition PGFtypes.h:174

◆ ChannelDepth()

uint8_t pgf::CPGFImage::ChannelDepth ( ) const
inline

Return bits per channel of the image's encoder.

Returns
Bits per channel

Definition at line 410 of file PGFimage.h.

PGFPreHeader m_preHeader
PGF pre-header.
Definition PGFimage.h:532
static uint8_t MaxChannelDepth(uint8_t version=PGFVersion)
Definition PGFimage.h:522
uint8_t version
PGF version.
Definition PGFtypes.h:128

◆ ChannelHeight()

uint32_t pgf::CPGFImage::ChannelHeight ( int c = 0) const
inline

Return current image height of given channel in pixels. The returned height depends on the levels read so far and on ROI.

Parameters
cA channel index
Returns
Channel height in pixels

Definition at line 405 of file PGFimage.h.

405{ ASSERT(c >= 0 && c < MaxChannels); return m_height[c]; }
uint32_t m_height[MaxChannels]
height of each channel at current level
Definition PGFimage.h:531
constexpr int MaxChannels
maximum number of (color) channels
Definition PGFtypes.h:64

◆ Channels()

uint8_t pgf::CPGFImage::Channels ( ) const
inline

Return the number of image channels. An image of type RGB contains 3 image channels (B, G, R).

Returns
Number of image channels

Definition at line 452 of file PGFimage.h.

452{ return m_header.channels; }
uint8_t channels
number of channels
Definition PGFtypes.h:175

◆ ChannelWidth()

uint32_t pgf::CPGFImage::ChannelWidth ( int c = 0) const
inline

Return current image width of given channel in pixels. The returned width depends on the levels read so far and on ROI.

Parameters
cA channel index
Returns
Channel width in pixels

Definition at line 398 of file PGFimage.h.

398{ ASSERT(c >= 0 && c < MaxChannels); return m_width[c]; }
uint32_t m_width[MaxChannels]
width of each channel at current level
Definition PGFimage.h:530

◆ Clamp16()

uint16_t pgf::CPGFImage::Clamp16 ( DataT v) const
inlineprivate

Definition at line 577 of file PGFimage.h.

577 {
578 if (v & 0xFFFF0000) return (v < 0) ? (uint16_t)0 : (uint16_t)65535; else return (uint16_t)v;
579 }

◆ Clamp31()

uint32_t pgf::CPGFImage::Clamp31 ( DataT v) const
inlineprivate

Definition at line 580 of file PGFimage.h.

580 {
581 return (v < 0) ? 0 : (uint32_t)v;
582 }

◆ Clamp4()

uint8_t pgf::CPGFImage::Clamp4 ( DataT v) const
inlineprivate

Definition at line 567 of file PGFimage.h.

567 {
568 if (v & 0xFFFFFFF0) return (v < 0) ? (uint8_t)0 : (uint8_t)15; else return (uint8_t)v;
569 }

◆ Clamp6()

uint16_t pgf::CPGFImage::Clamp6 ( DataT v) const
inlineprivate

Definition at line 570 of file PGFimage.h.

570 {
571 if (v & 0xFFFFFFC0) return (v < 0) ? (uint16_t)0 : (uint16_t)63; else return (uint16_t)v;
572 }

◆ Clamp8()

uint8_t pgf::CPGFImage::Clamp8 ( DataT v) const
inlineprivate

Definition at line 573 of file PGFimage.h.

573 {
574 // needs only one test in the normal case
575 if (v & 0xFFFFFF00) return (v < 0) ? (uint8_t)0 : (uint8_t)255; else return (uint8_t)v;
576 }

◆ CodecMajorVersion()

uint8_t pgf::CPGFImage::CodecMajorVersion ( uint8_t version = PGFVersion)
static

Return major version.

Return codec major version.

Parameters
versionpgf pre-header version number
Returns
PGF major of given version

Definition at line 772 of file PGFimage.cpp.

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

◆ CompleteHeader()

bool pgf::CPGFImage::CompleteHeader ( )
private

Definition at line 222 of file PGFimage.cpp.

222 {
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 }
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 ImageModeLabColor
constexpr uint8_t ImageModeCMYK64
#define PGFWeek
Definition PGFtypes.h:45
#define PGFYear
Definition PGFtypes.h:44
#define PGFMajorNumber
Definition PGFtypes.h:43
uint8_t usedBitsPerChannel
number of used bits per channel in 16- and 32-bit per channel modes
Definition PGFtypes.h:177
uint8_t mode
image mode according to Adobe's image modes
Definition PGFtypes.h:176
PGFVersionNumber version
codec version number: (since Version 7)
Definition PGFtypes.h:178
PGFVersionNumber(uint8_t _major, uint8_t _year, uint8_t _week)
Definition PGFtypes.h:156

◆ ComputeLevelROI()

PGFRect pgf::CPGFImage::ComputeLevelROI ( ) const

Return ROI of channel 0 at current level in pixels. The returned rect is only valid after reading a ROI.

Returns
ROI in pixels

Definition at line 587 of file PGFimage.cpp.

587 {
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 }
PGFRect m_roi
region of interest
Definition PGFimage.h:545
static uint32_t LevelSizeL(uint32_t size, int level)
Definition PGFimage.h:503
uint32_t top
Definition PGFtypes.h:259
uint32_t bottom
Definition PGFtypes.h:259
uint32_t right
Definition PGFtypes.h:259
PGFRect()
Standard constructor.
Definition PGFtypes.h:262
uint32_t left
Definition PGFtypes.h:259

◆ ComputeLevels()

void pgf::CPGFImage::ComputeLevels ( )
private

Definition at line 858 of file PGFimage.cpp.

858 {
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 }
#define __min(x, y)
double m_percent
progress [0..1]
Definition PGFimage.h:551
constexpr int MaxLevel
maximum number of transform levels
Definition PGFtypes.h:62
constexpr uint32_t FilterSize
uint8_t nLevels
number of FWT transforms
Definition PGFtypes.h:172
uint32_t width
image width in pixels
Definition PGFtypes.h:170
uint32_t height
image height in pixels
Definition PGFtypes.h:171

◆ ConfigureDecoder()

void pgf::CPGFImage::ConfigureDecoder ( bool useOMP = true,
UP::Policy policy = UP::CacheAll,
uint32_t prefixSize = 0 )
inline

Configures the decoder.

Parameters
useOMPUse parallel threading with Open MP during decoding. Default value: true. Influences the decoding only if the codec has been compiled with OpenMP support.
policyThe file might contain user data (e.g. metadata). The policy defines the behaviour during Open(). UP::CacheAll: User data is read and stored completely in a new allocated memory block. It can be accessed by GetUserData(). UP::CachePrefix: Only prefixSize bytes at the beginning of the user data are stored in a new allocated memory block. It can be accessed by GetUserData(). UP::Skip: User data is skipped and nothing is cached.
prefixSizeIs only used in combination with UP::CachePrefix. It defines the number of bytes cached.

Definition at line 260 of file PGFimage.h.

260 {
261 ASSERT(prefixSize <= MaxUserDataSize);
262 m_useOMPinDecoder = useOMP;
263 m_userDataPolicy = (policy == UP::CachePrefix) ? prefixSize : 0xFFFFFFFF - policy;
264 }
uint32_t m_userDataPolicy
user data (metadata) policy during open
Definition PGFimage.h:537
bool m_useOMPinDecoder
use Open MP in decoder
Definition PGFimage.h:542
constexpr Policy CachePrefix
Definition PGFtypes.h:112
constexpr size_t MaxUserDataSize
Definition PGFtypes.h:382

◆ ConfigureEncoder()

void pgf::CPGFImage::ConfigureEncoder ( bool useOMP = true,
bool favorSpeedOverSize = false )
inline

Configures the encoder.

Parameters
useOMPUse parallel threading with Open MP during encoding. Default value: true. Influences the encoding only if the codec has been compiled with OpenMP support.
favorSpeedOverSizeFavors encoding speed over compression ratio. Default value: false

Definition at line 250 of file PGFimage.h.

250{ m_useOMPinEncoder = useOMP; m_favorSpeedOverSize = favorSpeedOverSize; }
bool m_favorSpeedOverSize
favor encoding speed over compression ratio
Definition PGFimage.h:540
bool m_useOMPinEncoder
use Open MP in encoder
Definition PGFimage.h:541

◆ Destroy()

void pgf::CPGFImage::Destroy ( )

Definition at line 128 of file PGFimage.cpp.

128 {
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 }
CWaveletTransform * m_wtChannel[MaxChannels]
wavelet transformed color channels
Definition PGFimage.h:525
uint32_t * m_levelLength
length of each level in bytes; first level starts immediately after this array
Definition PGFimage.h:529
PGFPostHeader m_postHeader
PGF post-header.
Definition PGFimage.h:534
CEncoder * m_encoder
PGF encoder.
Definition PGFimage.h:528
CDecoder * m_decoder
PGF decoder.
Definition PGFimage.h:527
uint8_t * userData
user data of size userDataLen (optional part of file header)
Definition PGFtypes.h:201

◆ Downsample()

void pgf::CPGFImage::Downsample ( int nChannel)
private

Definition at line 814 of file PGFimage.cpp.

814 {
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 }
DataT * m_channel[MaxChannels]
untransformed channels in YUV format
Definition PGFimage.h:526
int32_t DataT
Definition PGFtypes.h:367

◆ GetAlignedROI()

PGFRect pgf::CPGFImage::GetAlignedROI ( int c = 0) const
private

Returns aligned ROI in pixels of current level of channel c

Parameters
cA channel index

Definition at line 602 of file PGFimage.cpp.

602 {
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 }
bool ROIisSupported() const
Definition PGFimage.h:470

◆ GetBitmap()

void pgf::CPGFImage::GetBitmap ( int pitch,
uint8_t * buff,
uint8_t bpp,
int channelMap[] = nullptr,
CallbackPtr cb = nullptr,
void * data = nullptr ) const

Get image data in interleaved format: (ordering of RGB data is BGR[A]) Upsampling, YUV to RGB transform and interleaving are done here to reduce the number of passes over the data. The absolute value of pitch is the number of bytes of an image row of the given image buffer. If pitch is negative, then the image buffer must point to the last row of a bottom-up image (first byte on last row). if pitch is positive, then the image buffer must point to the first row of a top-down image (first byte). 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 provide a channelMap of size of expected channels (depending on image mode). For example, PGF provides a channel sequence BGR in RGB color mode. If your provided image buffer expects a channel sequence ARGB, then the channelMap looks like { 3, 2, 1, 0 }. It might throw an IOException.

Parameters
pitchThe number of bytes of a row of the image buffer.
buffAn image buffer.
bppThe number of bits per pixel used in image buffer.
channelMapA integer array containing the mapping of PGF channel ordering to expected channel ordering.
cbA pointer to a callback procedure. The procedure is called after each copied buffer row. If cb returns true, then it stops proceeding.
dataData Pointer to C++ class container to host callback procedure.

Definition at line 1793 of file PGFimage.cpp.

1793 {
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 }
constexpr uint8_t ImageModeHSBColor
constexpr uint8_t ImageModeHSLColor
#define __max(x, y)
uint8_t Clamp8(DataT v) const
Definition PGFimage.h:573
uint16_t Clamp16(DataT v) const
Definition PGFimage.h:577
uint8_t UsedBitsPerChannel() const
Definition PGFimage.cpp:760
uint16_t Clamp6(DataT v) const
Definition PGFimage.h:570
uint32_t Clamp31(DataT v) const
Definition PGFimage.h:580
PGFRect ComputeLevelROI() const
Definition PGFimage.cpp:587
bool m_downsample
chrominance channels are downsampled
Definition PGFimage.h:539
PGFRect GetAlignedROI(int c=0) const
Definition PGFimage.cpp:602
uint8_t Clamp4(DataT v) const
Definition PGFimage.h:567
constexpr DataT YUVoffset6
Definition PGFimage.cpp:38
constexpr DataT YUVoffset8
Definition PGFimage.cpp:39
constexpr DataT YUVoffset4
Definition PGFimage.cpp:37

◆ GetChannel()

DataT * pgf::CPGFImage::GetChannel ( int c = 0)
inline

Return an internal YUV image channel.

Parameters
cA channel index
Returns
An internal YUV image channel

Definition at line 321 of file PGFimage.h.

321{ ASSERT(c >= 0 && c < MaxChannels); return m_channel[c]; }

◆ GetColorTable() [1/2]

const RGBQUAD * pgf::CPGFImage::GetColorTable ( ) const
inline
Returns
Address of color table

Definition at line 334 of file PGFimage.h.

334{ return m_postHeader.clut; }
RGBQUAD clut[ColorTableLen]
color table for indexed color images (optional part of file header)
Definition PGFtypes.h:200

◆ GetColorTable() [2/2]

void pgf::CPGFImage::GetColorTable ( uint32_t iFirstColor,
uint32_t nColors,
RGBQUAD * prgbColors ) const

Retrieves red, green, blue (RGB) color values from a range of entries in the palette of the DIB section. It might throw an IOException.

Parameters
iFirstColorThe color table index of the first entry to retrieve.
nColorsThe number of color table entries to retrieve.
prgbColorsA pointer to the array of RGBQUAD structures to retrieve the color table entries.

Definition at line 1354 of file PGFimage.cpp.

1354 {
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 }
constexpr int ColorTableLen
size of color lookup table (clut)
Definition PGFtypes.h:66

◆ GetEncodedHeaderLength()

uint32_t pgf::CPGFImage::GetEncodedHeaderLength ( ) const

Return the length of all encoded headers in bytes. Precondition: The PGF image has been opened with a call of Open(...).

Returns
The length of all encoded headers in bytes

Definition at line 652 of file PGFimage.cpp.

652 {
653 ASSERT(m_decoder);
655 }
uint32_t GetEncodedHeaderLength() const
Definition Decoder.h:131

◆ GetEncodedLevelLength()

uint32_t pgf::CPGFImage::GetEncodedLevelLength ( int level) const
inline

Return the length of an encoded PGF level in bytes. Precondition: The PGF image has been opened with a call of Open(...).

Parameters
levelThe image level
Returns
The length of a PGF level in bytes

Definition at line 371 of file PGFimage.h.

371{ ASSERT(level >= 0 && level < m_header.nLevels); return m_levelLength[m_header.nLevels - level - 1]; }

◆ GetHeader()

const PGFHeader * pgf::CPGFImage::GetHeader ( ) const
inline

Return the PGF header structure.

Returns
A PGF header structure

Definition at line 339 of file PGFimage.h.

339{ return &m_header; }

◆ GetMaxValue()

uint32_t pgf::CPGFImage::GetMaxValue ( ) const
inline

Get maximum intensity value for image modes with more than eight bits per channel. Don't call this method before the PGF header has been read.

Returns
The maximum intensity value.

Definition at line 345 of file PGFimage.h.

345{ return (1 << m_header.usedBitsPerChannel) - 1; }

◆ GetUserData()

const uint8_t * pgf::CPGFImage::GetUserData ( uint32_t & cachedSize,
uint32_t * pTotalSize = nullptr ) const

Return user data and size of user data. Precondition: The PGF image has been opened with a call of Open(...).

Parameters
cachedSize[out] Size of returned user data in bytes.
pTotalSize[optional out] Pointer to return the size of user data stored in image header in bytes.
Returns
A pointer to user data or nullptr if there is no user data available.

Return user data and size of user data. Precondition: The PGF image has been opened with a call of Open(...). In an encoder scenario don't call this method before WriteHeader().

Parameters
cachedSize[out] Size of returned user data in bytes.
pTotalSize[optional out] Pointer to return the size of user data stored in image header in bytes.
Returns
A pointer to user data or nullptr if there is no user data available.

Definition at line 341 of file PGFimage.cpp.

341 {
342 cachedSize = m_postHeader.cachedUserDataLen;
343 if (pTotalSize) *pTotalSize = m_postHeader.userDataLen;
344 return m_postHeader.userData;
345 }
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

◆ GetUserDataPos()

uint64_t pgf::CPGFImage::GetUserDataPos ( ) const
inline

Return the stream position of the user data or 0. Precondition: The PGF image has been opened with a call of Open(...).

Definition at line 350 of file PGFimage.h.

350{ return m_userDataPos; }
uint64_t m_userDataPos
stream position of user data
Definition PGFimage.h:535

◆ GetYUV()

void pgf::CPGFImage::GetYUV ( int pitch,
DataT * buff,
uint8_t bpp,
int channelMap[] = nullptr,
CallbackPtr cb = nullptr,
void * data = nullptr ) const

Get YUV image data in interleaved format: (ordering is YUV[A]) The absolute value of pitch is the number of bytes of an image row of the given image buffer. If pitch is negative, then the image buffer must point to the last row of a bottom-up image (first byte on last row). if pitch is positive, then the image buffer must point to the first row of a top-down image (first byte). 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 provide a channelMap of size of expected channels (depending on image mode). For example, PGF provides a channel sequence BGR in RGB color mode. If your provided image buffer expects a channel sequence VUY, then the channelMap looks like { 2, 1, 0 }. It might throw an IOException.

Parameters
pitchThe number of bytes of a row of the image buffer.
buffAn image buffer.
bppThe number of bits per pixel used in image buffer.
channelMapA integer array containing the mapping of PGF channel ordering to expected channel ordering.
cbA pointer to a callback procedure. The procedure is called after each copied buffer row. If cb returns true, then it stops proceeding.
dataData Pointer to C++ class container to host callback procedure.

Get YUV image data in interleaved format: (ordering is YUV[A]) The absolute value of pitch is the number of bytes of an image row of the given image buffer. If pitch is negative, then the image buffer must point to the last row of a bottom-up image (first byte on last row). if pitch is positive, then the image buffer must point to the first row of a top-down image (first byte). 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 provide a channelMap of size of expected channels (depending on image mode). For example, PGF provides a channel sequence BGR in RGB color mode. If your provided image buffer expects a channel sequence VUY, then the channelMap looks like { 2, 1, 0 }. It might throw an IOException.

Parameters
pitchThe number of bytes of a row of the image buffer.
buffAn image buffer.
bppThe number of bits per pixel used in image buffer.
channelMapA integer array containing the mapping of PGF channel ordering to expected channel ordering.
cbA pointer to a callback procedure. The procedure is called after each copied buffer row. If cb returns true, then it stops proceeding.

Definition at line 2554 of file PGFimage.cpp.

2554 {
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 }
constexpr DataT YUVoffset16
Definition PGFimage.cpp:40
constexpr size_t DataTSize
Definition PGFtypes.h:381

◆ Height()

uint32_t pgf::CPGFImage::Height ( int level = 0) const
inline

Return image height of channel 0 at given level in pixels. The returned height is independent of any Read-operations and ROI.

Parameters
levelA level
Returns
Image level height in pixels

Definition at line 424 of file PGFimage.h.

424{ ASSERT(level >= 0); return LevelSizeL(m_header.height, level); }

◆ ImportBitmap()

void pgf::CPGFImage::ImportBitmap ( int pitch,
uint8_t * buff,
uint8_t bpp,
int channelMap[] = nullptr,
CallbackPtr cb = nullptr,
void * data = nullptr )

Import an image from a specified image buffer. This method is usually called before Write(...) and after SetHeader(...). The absolute value of pitch is the number of bytes of an image row. If pitch is negative, then buff points to the last row of a bottom-up image (first byte on last row). If pitch is positive, then buff points to the first row of a top-down image (first byte). 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 provide a channelMap of size of expected channels (depending on image mode). For example, PGF expects in RGB color mode a channel sequence BGR. If your provided image buffer contains a channel sequence ARGB, then the channelMap looks like { 3, 2, 1, 0 }. It might throw an IOException.

Parameters
pitchThe number of bytes of a row of the image buffer.
buffAn image buffer.
bppThe number of bits per pixel used in image buffer.
channelMapA integer array containing the mapping of input channel ordering to expected channel ordering.
cbA pointer to a callback procedure. The procedure is called after each imported buffer row. If cb returns true, then it stops proceeding.
dataData Pointer to C++ class container to host callback procedure.

Definition at line 796 of file PGFimage.cpp.

796 {
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 }
void RgbToYuv(int pitch, uint8_t *rgbBuff, uint8_t bpp, int channelMap[], CallbackPtr cb, void *data)
void Downsample(int nChannel)
Definition PGFimage.cpp:814

◆ ImportIsSupported()

bool pgf::CPGFImage::ImportIsSupported ( uint8_t mode)
static

Check for valid import image mode.

Parameters
modeImage mode
Returns
True if an image of given mode can be imported with ImportBitmap(...)

Definition at line 1309 of file PGFimage.cpp.

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

◆ ImportYUV()

void pgf::CPGFImage::ImportYUV ( int pitch,
DataT * buff,
uint8_t bpp,
int channelMap[] = nullptr,
CallbackPtr cb = nullptr,
void * data = nullptr )

Import a YUV image from a specified image buffer. The absolute value of pitch is the number of bytes of an image row. If pitch is negative, then buff points to the last row of a bottom-up image (first byte on last row). If pitch is positive, then buff points to the first row of a top-down image (first byte). 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 provide a channelMap of size of expected channels (depending on image mode). For example, PGF expects in RGB color mode a channel sequence BGR. If your provided image buffer contains a channel sequence VUY, then the channelMap looks like { 2, 1, 0 }. It might throw an IOException.

Parameters
pitchThe number of bytes of a row of the image buffer.
buffAn image buffer.
bppThe number of bits per pixel used in image buffer.
channelMapA integer array containing the mapping of input channel ordering to expected channel ordering.
cbA pointer to a callback procedure. The procedure is called after each imported buffer row. If cb returns true, then it stops proceeding.
dataData Pointer to C++ class container to host callback procedure.

Import a YUV image from a specified image buffer. The absolute value of pitch is the number of bytes of an image row. If pitch is negative, then buff points to the last row of a bottom-up image (first byte on last row). If pitch is positive, then buff points to the first row of a top-down image (first byte). 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 provide a channelMap of size of expected channels (depending on image mode). For example, PGF expects in RGB color mode a channel sequence BGR. If your provided image buffer contains a channel sequence VUY, then the channelMap looks like { 2, 1, 0 }. It might throw an IOException.

Parameters
pitchThe number of bytes of a row of the image buffer.
buffAn image buffer.
bppThe number of bits per pixel used in image buffer.
channelMapA integer array containing the mapping of input channel ordering to expected channel ordering.
cbA pointer to a callback procedure. The procedure is called after each imported buffer row. If cb returns true, then it stops proceeding.

Definition at line 2665 of file PGFimage.cpp.

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

◆ Init()

void pgf::CPGFImage::Init ( )
private

Definition at line 70 of file PGFimage.cpp.

70 {
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 }
RefreshCB m_cb
pointer to refresh callback procedure
Definition PGFimage.h:549
void * m_cbArg
refresh callback argument
Definition PGFimage.h:550
bool m_streamReinitialized
stream has been reinitialized
Definition PGFimage.h:544
ProgressMode m_progressMode
progress mode used in Read and Write; Relative is default mode
Definition PGFimage.h:552
uint8_t m_quant
quantization parameter
Definition PGFimage.h:538
constexpr Policy CacheAll
Definition PGFtypes.h:112
constexpr int PGFVersion
current standard version
Definition PGFtypes.h:76
constexpr const char * PGFMagic
PGF identification.
Definition PGFtypes.h:61
char magic[3]
PGF identification = "PGF".
Definition PGFtypes.h:127
uint32_t hSize
total size of PGFHeader, [ColorTable], and [UserData] in bytes (since Version 6: 4 Bytes)
Definition PGFtypes.h:137

◆ IsFullyRead()

bool pgf::CPGFImage::IsFullyRead ( ) const
inline

Return true if all levels have been read.

Definition at line 440 of file PGFimage.h.

440{ return m_currentLevel == 0; }

◆ IsOpen()

bool pgf::CPGFImage::IsOpen ( ) const
inline

Returns true if the PGF has been opened for reading.

Definition at line 77 of file PGFimage.h.

77{ return m_decoder != nullptr; }

◆ Level()

uint8_t pgf::CPGFImage::Level ( ) const
inline

Return current image level. Since Read(...) can be used to read each image level separately, it is helpful to know the current level. The current level immediately after Open(...) is Levels().

Returns
Current image level

Definition at line 431 of file PGFimage.h.

431{ return (uint8_t)m_currentLevel; }

◆ Levels()

uint8_t pgf::CPGFImage::Levels ( ) const
inline

Return the number of image levels.

Returns
Number of image levels

Definition at line 436 of file PGFimage.h.

436{ return m_header.nLevels; }

◆ LevelSizeH()

uint32_t pgf::CPGFImage::LevelSizeH ( uint32_t size,
int level )
inlinestatic

Compute and return image width/height of HH subband at given level.

Parameters
sizeOriginal image size (e.g. width or height at level 0)
levelAn image level
Returns
high pass size at given level in pixels

Definition at line 510 of file PGFimage.h.

510{ ASSERT(level >= 0); uint32_t d = 1 << (level - 1); return (size + d - 1) >> level; }

◆ LevelSizeL()

uint32_t pgf::CPGFImage::LevelSizeL ( uint32_t size,
int level )
inlinestatic

Compute and return image width/height of LL subband at given level.

Parameters
sizeOriginal image size (e.g. width or height at level 0)
levelAn image level
Returns
Image width/height at given level in pixels

Definition at line 503 of file PGFimage.h.

503{ ASSERT(level >= 0); uint32_t d = 1 << level; return (size + d - 1) >> level; }

◆ MaxChannelDepth()

uint8_t pgf::CPGFImage::MaxChannelDepth ( uint8_t version = PGFVersion)
inlinestatic

Return maximum channel depth.

Parameters
versionpgf pre-header version number
Returns
maximum channel depth in bit of given version (16 or 32 bit)

Definition at line 522 of file PGFimage.h.

522{ return (version & PGF32) ? 32 : 16; }
constexpr int PGF32
32 bit values are used -> allows at maximum 30 input bits, otherwise 16 bit values are used -> allows...
Definition PGFtypes.h:69

◆ Mode()

uint8_t pgf::CPGFImage::Mode ( ) const
inline

Return the image mode. An image mode is a predefined constant value (see also PGFtypes.h) compatible with Adobe Photoshop. It represents an image type and format.

Returns
Image mode

Definition at line 459 of file PGFimage.h.

459{ return m_header.mode; }

◆ Open()

void pgf::CPGFImage::Open ( CPGFStream * stream)

Open a PGF image at current stream position: read pre-header, header, and ckeck image type. Precondition: The stream has been opened for reading. It might throw an IOException.

Parameters
streamA PGF stream

Definition at line 145 of file PGFimage.cpp.

145 {
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 }
CDecoder(CPGFStream *stream, PGFPreHeader &preHeader, PGFHeader &header, PGFPostHeader &postHeader, uint32_t *&levelLength, uint64_t &userDataPos, bool useOMP, uint32_t userDataPolicy)
Definition Decoder.cpp:74
bool CompleteHeader()
Definition PGFimage.cpp:222
CWaveletTransform(uint32_t width, uint32_t height, int levels, DataT *data=nullptr)
constexpr int DownsampleThreshold
if quality is larger than this threshold than downsampling is used
Definition PGFtypes.h:65
uint8_t quality
quantization parameter: 0=lossless, 4=standard, 6=poor quality
Definition PGFtypes.h:173

◆ Quality()

uint8_t pgf::CPGFImage::Quality ( ) const
inline

Return the PGF quality. The quality is inbetween 0 and MaxQuality. PGF quality 0 means lossless quality.

Returns
PGF quality

Definition at line 446 of file PGFimage.h.

446{ return m_header.quality; }

◆ Read() [1/2]

void pgf::CPGFImage::Read ( int level = 0,
CallbackPtr cb = nullptr,
void * data = nullptr )

Read and decode some levels of a PGF image at current stream position. A PGF image is structered in levels, numbered between 0 and Levels() - 1. Each level can be seen as a single image, containing the same content as all other levels, but in a different size (width, height). The image size at level i is double the size (width, height) of the image at level i+1. The image at level 0 contains the original size. Precondition: The PGF image has been opened with a call of Open(...). It might throw an IOException.

Parameters
level[0, nLevels) The image level of the resulting image in the internal image buffer.
cbA pointer to a callback procedure. The procedure is called after reading a single level. If cb returns true, then it stops proceeding.
dataData Pointer to C++ class container to host callback procedure.

Definition at line 406 of file PGFimage.cpp.

406 {
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
440 wtChannel->GetSubband(m_currentLevel, Orient::LL)->PlaceTile(*m_decoder, m_quant);
441 }
443 // since version 5
444 wtChannel->GetSubband(m_currentLevel, Orient::HL)->PlaceTile(*m_decoder, m_quant);
445 wtChannel->GetSubband(m_currentLevel, Orient::LH)->PlaceTile(*m_decoder, m_quant);
446 } else {
447 // until version 4
449 }
450 wtChannel->GetSubband(m_currentLevel, Orient::HH)->PlaceTile(*m_decoder, m_quant);
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 }
void DecodeInterleaved(CWaveletTransform *wtChannel, int level, int quantParam)
Definition Decoder.cpp:334
void Read(int level=0, CallbackPtr cb=nullptr, void *data=nullptr)
Definition PGFimage.cpp:406
OSError InverseTransform(int level, uint32_t *width, uint32_t *height, DataT **data)
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

◆ Read() [2/2]

void pgf::CPGFImage::Read ( PGFRect & rect,
int level = 0,
CallbackPtr cb = nullptr,
void * data = nullptr )

Read a rectangular region of interest of a PGF image at current stream position. The origin of the coordinate axis is the top-left corner of the image. All coordinates are measured in pixels. It might throw an IOException.

Parameters
rect[inout] Rectangular region of interest (ROI) at level 0. The rect might be cropped.
level[0, nLevels) The image level of the resulting image in the internal image buffer.
cbA pointer to a callback procedure. The procedure is called after reading a single level. If cb returns true, then it stops proceeding.
dataData Pointer to C++ class container to host callback procedure.

Read and decode rectangular region of interest (ROI) of a PGF image at current stream position. The origin of the coordinate axis is the top-left corner of the image. All coordinates are measured in pixels. It might throw an IOException.

Parameters
rect[inout] Rectangular region of interest (ROI) at level 0. The rect might be cropped.
levelThe image level of the resulting image in the internal image buffer.
cbA pointer to a callback procedure. The procedure is called after reading a single level. If cb returns true, then it stops proceeding.
dataData Pointer to C++ class container to host callback procedure.

Definition at line 493 of file PGFimage.cpp.

493 {
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);
535 wtChannel->GetSubband(m_currentLevel, Orient::LL)->PlaceTile(*m_decoder, m_quant);
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 }
void SetStreamPosToData()
Resets stream position to beginning of data block.
Definition Decoder.h:139
void GetNextMacroBlock()
Definition Decoder.cpp:478
void SetROI(PGFRect rect)
Definition PGFimage.cpp:619

◆ ReadEncodedData()

uint32_t pgf::CPGFImage::ReadEncodedData ( int level,
uint8_t * target,
uint32_t targetLen ) const

Reads the data of an encoded PGF level and copies it to a target buffer without decoding. Precondition: The PGF image has been opened with a call of Open(...). It might throw an IOException.

Parameters
levelThe image level
targetThe target buffer
targetLenThe length of the target buffer in bytes
Returns
The number of bytes copied to the target buffer

Definition at line 711 of file PGFimage.cpp.

711 {
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 }
void Skip(uint64_t offset)
Definition Decoder.cpp:450
uint32_t ReadEncodedData(uint8_t *target, uint32_t len) const
Definition Decoder.cpp:247
uint32_t GetEncodedLevelLength(int level) const
Definition PGFimage.h:371

◆ ReadEncodedHeader()

uint32_t pgf::CPGFImage::ReadEncodedHeader ( uint8_t * target,
uint32_t targetLen ) const

Reads the encoded PGF header and copies it to a target buffer. Precondition: The PGF image has been opened with a call of Open(...). It might throw an IOException.

Parameters
targetThe target buffer
targetLenThe length of the target buffer in bytes
Returns
The number of bytes copied to the target buffer

Definition at line 664 of file PGFimage.cpp.

664 {
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 }
void SetStreamPosToStart()
Resets stream position to beginning of PGF pre-header.
Definition Decoder.h:135
uint32_t GetEncodedHeaderLength() const
Definition PGFimage.cpp:652

◆ ReadPreview()

void pgf::CPGFImage::ReadPreview ( )
inline

Read and decode smallest level of a PGF image at current stream position. For details, please refert to Read(...) Precondition: The PGF image has been opened with a call of Open(...). It might throw an IOException.

Definition at line 111 of file PGFimage.h.

111{ Read(Levels() - 1); }
uint8_t Levels() const
Definition PGFimage.h:436

◆ Reconstruct()

void pgf::CPGFImage::Reconstruct ( int level = 0)

After you've written a PGF image, you can call this method followed by GetBitmap/GetYUV to get a quick reconstruction (coded -> decoded image). It might throw an IOException.

Parameters
levelThe image level of the resulting image in the internal image buffer.

Definition at line 352 of file PGFimage.cpp.

352 {
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 }
DataT * GetBuffer()
Definition Subband.h:107
void Dequantize(int quantParam)
Definition Subband.cpp:155
CSubband * GetSubband(int level, Orient::Orientation orientation)

◆ ResetStreamPos()

void pgf::CPGFImage::ResetStreamPos ( bool startOfData)

Reset stream position to start of PGF pre-header or start of data. Must not be called before Open() or before Write(). Use this method after Read() if you want to read the same image several times, e.g. reading different ROIs.

Parameters
startOfDatatrue: you want to read the same image several times. false: resets stream position to the initial position

Definition at line 686 of file PGFimage.cpp.

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

◆ RgbToYuv()

void pgf::CPGFImage::RgbToYuv ( int pitch,
uint8_t * rgbBuff,
uint8_t bpp,
int channelMap[],
CallbackPtr cb,
void * data )
private

Definition at line 1393 of file PGFimage.cpp.

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

◆ ROIisSupported()

bool pgf::CPGFImage::ROIisSupported ( ) const
inline

Return true if the pgf image supports Region Of Interest (ROI).

Returns
true if the pgf image supports ROI.

Definition at line 470 of file PGFimage.h.

470{ return (m_preHeader.version & PGFROI) == PGFROI; }
constexpr int PGFROI
supports Regions Of Interest
Definition PGFtypes.h:70

◆ SetChannel()

void pgf::CPGFImage::SetChannel ( DataT * channel,
int c = 0 )
inline

Set internal PGF image buffer channel.

Parameters
channelA YUV data channel
cA channel index

Definition at line 276 of file PGFimage.h.

276{ ASSERT(c >= 0 && c < MaxChannels); m_channel[c] = channel; }

◆ SetColorTable()

void pgf::CPGFImage::SetColorTable ( uint32_t iFirstColor,
uint32_t nColors,
const RGBQUAD * prgbColors )

Sets the red, green, blue (RGB) color values for a range of entries in the palette (clut). It might throw an IOException.

Parameters
iFirstColorThe color table index of the first entry to set.
nColorsThe number of color table entries to set.
prgbColorsA pointer to the array of RGBQUAD structures to set the color table entries.

Definition at line 1368 of file PGFimage.cpp.

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

◆ SetHeader()

void pgf::CPGFImage::SetHeader ( const PGFHeader & header,
uint8_t flags = 0,
const uint8_t * userData = 0,
uint32_t userDataLength = 0 )

Set PGF header and user data. Precondition: The PGF image has been never opened with Open(...). It might throw an IOException.

Parameters
headerA valid and already filled in PGF header structure
flagsA combination of additional version flags. In case you use level-wise encoding then set flag = PGFROI.
userDataA user-defined memory block containing any kind of cached metadata.
userDataLengthThe size of user-defined memory block in bytes

Definition at line 898 of file PGFimage.cpp.

898 {
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 }
void ComputeLevels()
Definition PGFimage.cpp:858
constexpr size_t HeaderSize
Definition PGFtypes.h:379
constexpr int MaxQuality
maximum quality
Definition PGFtypes.h:96
constexpr size_t ColorTableSize
Definition PGFtypes.h:380

◆ SetMaxValue()

void pgf::CPGFImage::SetMaxValue ( uint32_t maxValue)

Set maximum intensity value for image modes with more than eight bits per channel. Call this method after SetHeader, but before ImportBitmap.

Parameters
maxValueThe maximum intensity value.

Definition at line 742 of file PGFimage.cpp.

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

◆ SetProgressMode()

void pgf::CPGFImage::SetProgressMode ( ProgressMode pm)
inline

Set progress mode used in Read and Write. Default mode is Relative. This method must be called before Open() or SetHeader(). Relative: 100% = level difference between current level and target level of Read/Write Absolute: 100% = number of levels

Definition at line 300 of file PGFimage.h.

300{ m_progressMode = pm; }

◆ SetRefreshCallback()

void pgf::CPGFImage::SetRefreshCallback ( RefreshCB callback,
void * arg )
inline

Set refresh callback procedure and its parameter. The refresh callback is called during Read(...) after each level read.

Parameters
callbackA refresh callback procedure
argA parameter of the refresh callback procedure

Definition at line 307 of file PGFimage.h.

307{ m_cb = callback; m_cbArg = arg; }

◆ SetROI()

void pgf::CPGFImage::SetROI ( PGFRect rect)
private

Compute ROIs for each channel and each level <= current level Called inside of Read(rect, ...).

Parameters
rectrectangular region of interest (ROI) at level 0

Definition at line 619 of file PGFimage.cpp.

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

◆ UpdatePostHeaderSize()

uint32_t pgf::CPGFImage::UpdatePostHeaderSize ( )
private

Definition at line 1128 of file PGFimage.cpp.

1128 {
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 }
uint32_t WriteLevelLength(uint32_t *&levelLength)
Definition Encoder.cpp:178
void UpdatePostHeaderSize(PGFPreHeader preHeader)
Definition Encoder.cpp:161
int64_t ComputeOffset() const
Definition Encoder.h:179

◆ UsedBitsPerChannel()

uint8_t pgf::CPGFImage::UsedBitsPerChannel ( ) const

Returns number of used bits per input/output image channel. Precondition: header must be initialized.

Returns
number of used bits per input/output image channel.

Definition at line 760 of file PGFimage.cpp.

760 {
761 const uint8_t bpc = m_header.bpp/m_header.channels;
762
763 if (bpc > 8) {
765 } else {
766 return bpc;
767 }
768 }

◆ Version()

uint8_t pgf::CPGFImage::Version ( ) const
inline

Returns the used codec major version of a pgf image

Returns
PGF codec major version of this image

Definition at line 488 of file PGFimage.h.

488{ uint8_t ver = CodecMajorVersion(m_preHeader.version); return (ver <= 7) ? ver : (uint8_t)m_header.version.major; }
static uint8_t CodecMajorVersion(uint8_t version=PGFVersion)
Return major version.
Definition PGFimage.cpp:772
uint16_t major
major version number
Definition PGFtypes.h:151

◆ Width()

uint32_t pgf::CPGFImage::Width ( int level = 0) const
inline

Return image width of channel 0 at given level in pixels. The returned width is independent of any Read-operations and ROI.

Parameters
levelA level
Returns
Image level width in pixels

Definition at line 417 of file PGFimage.h.

417{ ASSERT(level >= 0); return LevelSizeL(m_header.width, level); }

◆ Write() [1/2]

void pgf::CPGFImage::Write ( CPGFStream * stream,
uint32_t * nWrittenBytes = nullptr,
CallbackPtr cb = nullptr,
void * data = nullptr )

Encode and write an entire PGF image (header and image) at current stream position. A PGF image is structered in levels, numbered between 0 and Levels() - 1. Each level can be seen as a single image, containing the same content as all other levels, but in a different size (width, height). The image size at level i is double the size (width, height) of the image at level i+1. The image at level 0 contains the original size. Precondition: the PGF image contains a valid header (see also SetHeader(...)). It might throw an IOException.

Parameters
streamA PGF stream
nWrittenBytes[in-out] The number of bytes written into stream are added to the input value.
cbA pointer to a callback procedure. The procedure is called after writing a single level. If cb returns true, then it stops proceeding.
dataData Pointer to C++ class container to host callback procedure.

Definition at line 1225 of file PGFimage.cpp.

1225 {
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 }
uint32_t WriteImage(CPGFStream *stream, CallbackPtr cb=nullptr, void *data=nullptr)
uint32_t WriteHeader(CPGFStream *stream)
Definition PGFimage.cpp:983

◆ Write() [2/2]

uint32_t pgf::CPGFImage::Write ( int level,
CallbackPtr cb = nullptr,
void * data = nullptr )

Encode and write down to given level at current stream position. A PGF image is structered in levels, numbered between 0 and Levels() - 1. Each level can be seen as a single image, containing the same content as all other levels, but in a different size (width, height). The image size at level i is double the size (width, height) of the image at level i+1. The image at level 0 contains the original size. Preconditions: the PGF image contains a valid header (see also SetHeader(...)) and WriteHeader() has been called before. Levels() > 0. The ROI encoding scheme must be used (see also SetHeader(...)). It might throw an IOException.

Parameters
level[0, nLevels) The image level of the resulting image in the internal image buffer.
cbA pointer to a callback procedure. The procedure is called after writing a single level. If cb returns true, then it stops proceeding.
dataData Pointer to C++ class container to host callback procedure.
Returns
The number of bytes written into stream.

Definition at line 1254 of file PGFimage.cpp.

1254 {
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 }
uint32_t UpdateLevelLength()
Definition Encoder.cpp:203
int64_t ComputeBufferLength() const
Definition Encoder.h:174
uint32_t UpdatePostHeaderSize()

◆ WriteHeader()

uint32_t pgf::CPGFImage::WriteHeader ( CPGFStream * stream)

Create wavelet transform channels and encoder. Write header at current stream position. Call this method before your first call of Write(int level) or WriteImage(), but after SetHeader(). This method is called inside of Write(stream, ...). It might throw an IOException.

Parameters
streamA PGF stream
Returns
The number of bytes written into stream.

Create wavelet transform channels and encoder. Write header at current stream position. Performs forward FWT. Call this method before your first call of Write(int level) or WriteImage(), but after SetHeader(). This method is called inside of Write(stream, ...). It might throw an IOException.

Parameters
streamA PGF stream
Returns
The number of bytes written into stream.

Definition at line 983 of file PGFimage.cpp.

983 {
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 }
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
OSError ForwardTransform(int level, int quant)

◆ WriteImage()

uint32_t pgf::CPGFImage::WriteImage ( CPGFStream * stream,
CallbackPtr cb = nullptr,
void * data = nullptr )

Encode and write an image at current stream position. Call this method after WriteHeader(). In case you want to write uncached metadata, then do that after WriteHeader() and before WriteImage(). This method is called inside of Write(stream, ...). It might throw an IOException.

Parameters
streamA PGF stream
cbA pointer to a callback procedure. The procedure is called after writing a single level. If cb returns true, then it stops proceeding.
dataData Pointer to C++ class container to host callback procedure.
Returns
The number of bytes written into stream.

Definition at line 1154 of file PGFimage.cpp.

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

◆ WriteLevel()

void pgf::CPGFImage::WriteLevel ( )
private

Definition at line 1072 of file PGFimage.cpp.

1072 {
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 }
void SetEncodedLevel(int currentLevel)
Definition Encoder.h:157
void ExtractTile(CEncoder &encoder, bool tile=false, uint32_t tileX=0, uint32_t tileY=0)
Definition Subband.cpp:178

Member Data Documentation

◆ m_cb

RefreshCB pgf::CPGFImage::m_cb
private

pointer to refresh callback procedure

Definition at line 549 of file PGFimage.h.

◆ m_cbArg

void* pgf::CPGFImage::m_cbArg
private

refresh callback argument

Definition at line 550 of file PGFimage.h.

◆ m_channel

DataT* pgf::CPGFImage::m_channel[MaxChannels]
protected

untransformed channels in YUV format

Definition at line 526 of file PGFimage.h.

◆ m_currentLevel

int pgf::CPGFImage::m_currentLevel
protected

transform level of current image

Definition at line 536 of file PGFimage.h.

◆ m_decoder

CDecoder* pgf::CPGFImage::m_decoder
protected

PGF decoder.

Definition at line 527 of file PGFimage.h.

◆ m_downsample

bool pgf::CPGFImage::m_downsample
protected

chrominance channels are downsampled

Definition at line 539 of file PGFimage.h.

◆ m_encoder

CEncoder* pgf::CPGFImage::m_encoder
protected

PGF encoder.

Definition at line 528 of file PGFimage.h.

◆ m_favorSpeedOverSize

bool pgf::CPGFImage::m_favorSpeedOverSize
protected

favor encoding speed over compression ratio

Definition at line 540 of file PGFimage.h.

◆ m_header

PGFHeader pgf::CPGFImage::m_header
protected

PGF file header.

Definition at line 533 of file PGFimage.h.

◆ m_height

uint32_t pgf::CPGFImage::m_height[MaxChannels]
protected

height of each channel at current level

Definition at line 531 of file PGFimage.h.

◆ m_levelLength

uint32_t* pgf::CPGFImage::m_levelLength
protected

length of each level in bytes; first level starts immediately after this array

Definition at line 529 of file PGFimage.h.

◆ m_percent

double pgf::CPGFImage::m_percent
private

progress [0..1]

Definition at line 551 of file PGFimage.h.

◆ m_postHeader

PGFPostHeader pgf::CPGFImage::m_postHeader
protected

PGF post-header.

Definition at line 534 of file PGFimage.h.

◆ m_preHeader

PGFPreHeader pgf::CPGFImage::m_preHeader
protected

PGF pre-header.

Definition at line 532 of file PGFimage.h.

◆ m_progressMode

ProgressMode pgf::CPGFImage::m_progressMode
private

progress mode used in Read and Write; Relative is default mode

Definition at line 552 of file PGFimage.h.

◆ m_quant

uint8_t pgf::CPGFImage::m_quant
protected

quantization parameter

Definition at line 538 of file PGFimage.h.

◆ m_roi

PGFRect pgf::CPGFImage::m_roi
protected

region of interest

Definition at line 545 of file PGFimage.h.

◆ m_streamReinitialized

bool pgf::CPGFImage::m_streamReinitialized
protected

stream has been reinitialized

Definition at line 544 of file PGFimage.h.

◆ m_useOMPinDecoder

bool pgf::CPGFImage::m_useOMPinDecoder
protected

use Open MP in decoder

Definition at line 542 of file PGFimage.h.

◆ m_useOMPinEncoder

bool pgf::CPGFImage::m_useOMPinEncoder
protected

use Open MP in encoder

Definition at line 541 of file PGFimage.h.

◆ m_userDataPolicy

uint32_t pgf::CPGFImage::m_userDataPolicy
protected

user data (metadata) policy during open

Definition at line 537 of file PGFimage.h.

◆ m_userDataPos

uint64_t pgf::CPGFImage::m_userDataPos
protected

stream position of user data

Definition at line 535 of file PGFimage.h.

◆ m_width

uint32_t pgf::CPGFImage::m_width[MaxChannels]
protected

width of each channel at current level

Definition at line 530 of file PGFimage.h.

◆ m_wtChannel

CWaveletTransform* pgf::CPGFImage::m_wtChannel[MaxChannels]
protected

wavelet transformed color channels

Definition at line 525 of file PGFimage.h.


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