libpgf 6.11.32
PGF - Progressive Graphics File
|
00001 /* 00002 * The Progressive Graphics File; http://www.libpgf.org 00003 * 00004 * $Date: 2007-02-03 13:04:21 +0100 (Sa, 03 Feb 2007) $ 00005 * $Revision: 280 $ 00006 * 00007 * This file Copyright (C) 2006 xeraina GmbH, Switzerland 00008 * 00009 * This program is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE 00011 * as published by the Free Software Foundation; either version 2.1 00012 * of the License, or (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 */ 00023 00028 00029 #ifndef PGF_PGFIMAGE_H 00030 #define PGF_PGFIMAGE_H 00031 00032 #include "PGFstream.h" 00033 00034 class CDecoder; 00035 class CEncoder; 00036 class CWaveletTransform; 00037 00051 class CPGFImage { 00052 public: 00053 00056 CPGFImage(); 00057 00060 virtual ~CPGFImage(); 00061 00065 virtual void Close(); 00066 00070 virtual void Destroy(); 00071 00077 void Open(CPGFStream* stream) THROW_; 00078 00081 bool IsOpen() const { return m_decoder != NULL; } 00082 00095 void Read(int level = 0, CallbackPtr cb = NULL, void *data = NULL) THROW_; 00096 00097 #ifdef __PGFROISUPPORT__ 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 void Read(PGFRect& rect, int level = 0, CallbackPtr cb = NULL, void *data = NULL) THROW_; 00108 #endif 00109 00115 void ReadPreview() THROW_ { Read(Levels() - 1); } 00116 00122 void Reconstruct(int level = 0) THROW_; 00123 00141 void GetBitmap(int pitch, UINT8* buff, BYTE bpp, int channelMap[] = NULL, CallbackPtr cb = NULL, void *data = NULL) const THROW_; // throws IOException 00142 00158 void GetYUV(int pitch, DataT* buff, BYTE bpp, int channelMap[] = NULL, CallbackPtr cb = NULL, void *data = NULL) const THROW_; // throws IOException 00159 00176 void ImportBitmap(int pitch, UINT8 *buff, BYTE bpp, int channelMap[] = NULL, CallbackPtr cb = NULL, void *data = NULL) THROW_; 00177 00193 void ImportYUV(int pitch, DataT *buff, BYTE bpp, int channelMap[] = NULL, CallbackPtr cb = NULL, void *data = NULL) THROW_; 00194 00210 void Write(CPGFStream* stream, UINT32* nWrittenBytes = NULL, CallbackPtr cb = NULL, void *data = NULL) THROW_; 00211 00219 UINT32 WriteHeader(CPGFStream* stream) THROW_; 00220 00221 #ifdef __PGFROISUPPORT__ 00222 00223 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237 UINT32 Write(int level, CallbackPtr cb = NULL, void *data = NULL) THROW_; 00238 #endif 00239 00244 void ConfigureEncoder(bool useOMP = true, bool favorSpeedOverSize = false) { m_useOMPinEncoder = useOMP; m_favorSpeedOverSize = favorSpeedOverSize; } 00245 00249 void ConfigureDecoder(bool useOMP = true) { m_useOMPinDecoder = useOMP; } 00250 00254 void SetBackground(const RGBTRIPLE* bg); 00255 00261 void SetBackground(BYTE red, BYTE green, BYTE blue) { /*m_backgroundSet = true;*/ m_header.background.rgbtRed = red; m_header.background.rgbtGreen = green; m_header.background.rgbtBlue = blue; } 00262 00267 void SetChannel(DataT* channel, int c = 0) { ASSERT(c >= 0 && c < MaxChannels); m_channel[c] = channel; } 00268 00277 void SetHeader(const PGFHeader& header, BYTE flags = 0, UINT8* userData = 0, UINT32 userDataLength = 0) THROW_; // throws IOException 00278 00283 void SetMaxValue(UINT32 maxValue); 00284 00289 BYTE UsedBitsPerChannel() const; 00290 00296 void SetRefreshCallback(RefreshCB callback, void* arg) { m_cb = callback; m_cbArg = arg; } 00297 00304 void SetColorTable(UINT32 iFirstColor, UINT32 nColors, const RGBQUAD* prgbColors) THROW_; 00305 00309 RGBTRIPLE Background() const { return m_header.background; } 00310 00315 DataT* GetChannel(int c = 0) { ASSERT(c >= 0 && c < MaxChannels); return m_channel[c]; } 00316 00323 void GetColorTable(UINT32 iFirstColor, UINT32 nColors, RGBQUAD* prgbColors) const THROW_; 00324 00326 // Returns address of internal color table 00328 const RGBQUAD* GetColorTable() const { return m_postHeader.clut; } 00329 00333 const PGFHeader* GetHeader() const { return &m_header; } 00334 00339 UINT32 GetMaxValue() const { return (1 << m_header.background.rgbtBlue) - 1; } 00340 00345 const UINT8* GetUserData(UINT32& size) const; 00346 00351 UINT32 GetEncodedHeaderLength() const; 00352 00358 UINT32 GetEncodedLevelLength(int level) const { ASSERT(level >= 0 && level < m_header.nLevels); return m_levelLength[m_header.nLevels - level - 1]; } 00359 00362 void ResetStreamPos() THROW_; 00363 00371 UINT32 ReadEncodedHeader(UINT8* target, UINT32 targetLen) const THROW_; 00372 00382 UINT32 ReadEncodedData(int level, UINT8* target, UINT32 targetLen) const THROW_; 00383 00389 UINT32 ChannelWidth(int c = 0) const { ASSERT(c >= 0 && c < MaxChannels); return m_width[c]; } 00390 00396 UINT32 ChannelHeight(int c = 0) const { ASSERT(c >= 0 && c < MaxChannels); return m_height[c]; } 00397 00401 BYTE ChannelDepth() const { return (m_preHeader.version & PGF32) ? 32 : 16; } 00402 00408 UINT32 Width(int level = 0) const { ASSERT(level >= 0); return LevelWidth(m_header.width, level); } 00409 00415 UINT32 Height(int level = 0) const { ASSERT(level >= 0); return LevelHeight(m_header.height, level); } 00416 00422 BYTE Level() const { return (BYTE)m_currentLevel; } 00423 00427 BYTE Levels() const { return m_header.nLevels; } 00428 00433 BYTE Quality() const { return m_header.quality; } 00434 00439 BYTE Channels() const { return m_header.channels; } 00440 00446 BYTE Mode() const { return m_header.mode; } 00447 00452 BYTE BPP() const { return m_header.bpp; } 00453 00457 bool ROIisSupported() const { return (m_preHeader.version & PGFROI) == PGFROI; } 00458 00461 BYTE Version() const; 00462 00463 //class methods 00464 00469 static bool ImportIsSupported(BYTE mode); 00470 00476 static UINT32 LevelWidth(UINT32 width, int level) { ASSERT(level >= 0); UINT32 w = (width >> level); return ((w << level) == width) ? w : w + 1; } 00477 00483 static UINT32 LevelHeight(UINT32 height, int level) { ASSERT(level >= 0); UINT32 h = (height >> level); return ((h << level) == height) ? h : h + 1; } 00484 00485 protected: 00486 CWaveletTransform* m_wtChannel[MaxChannels]; // wavelet transformed color channels 00487 DataT* m_channel[MaxChannels]; // untransformed channels in YUV format 00488 CDecoder* m_decoder; // PGF decoder 00489 CEncoder* m_encoder; // PGF encoder 00490 UINT32* m_levelLength; // length of each level in bytes; first level starts immediately after this array 00491 UINT32 m_width[MaxChannels]; // width of each channel at current level 00492 UINT32 m_height[MaxChannels]; // height of each channel at current level 00493 PGFPreHeader m_preHeader; // PGF pre header 00494 PGFHeader m_header; // PGF file header 00495 PGFPostHeader m_postHeader; // PGF post header 00496 int m_currentLevel; // transform level of current image 00497 BYTE m_quant; // quantization parameter 00498 bool m_downsample; // chrominance channels are downsampled 00499 bool m_favorSpeedOverSize; // favor encoding speed over compression ratio 00500 bool m_useOMPinEncoder; // use Open MP in encoder 00501 bool m_useOMPinDecoder; // use Open MP in decoder 00502 #ifdef __PGFROISUPPORT__ 00503 bool m_levelwise; // write level-wise (only used with WriteNextLevel) 00504 bool m_streamReinitialized; // stream has been reinitialized 00505 PGFRect m_roi; // region of interest 00506 #endif 00507 00508 private: 00509 RefreshCB m_cb; // pointer to refresh callback procedure 00510 void *m_cbArg; // refresh callback argument 00511 00512 void ComputeLevels(); 00513 void CompleteHeader(); 00514 void RgbToYuv(int pitch, UINT8* rgbBuff, BYTE bpp, int channelMap[], CallbackPtr cb, void *data) THROW_; 00515 void Downsample(int nChannel); 00516 void WriteLevel() THROW_; 00517 00518 #ifdef __PGFROISUPPORT__ 00519 void SetROI(PGFRect rect); 00520 #endif 00521 00522 UINT8 Clamp(DataT v) const { 00523 // needs only one test in the normal case 00524 if (v & 0xFFFFFF00) return (v < 0) ? (UINT8)0 : (UINT8)255; else return (UINT8)v; 00525 } 00526 UINT8 Clamp4(DataT v) const { 00527 if (v & 0xFFFFFFF0) return (v < 0) ? (UINT8)0: (UINT8)15; else return (UINT8)v; 00528 } 00529 UINT16 Clamp6(DataT v) const { 00530 if (v & 0xFFFFFFC0) return (v < 0) ? (UINT16)0: (UINT16)63; else return (UINT16)v; 00531 } 00532 UINT16 Clamp16(DataT v) const { 00533 if (v & 0xFFFF0000) return (v < 0) ? (UINT16)0: (UINT16)65535; else return (UINT16)v; 00534 } 00535 UINT32 Clamp31(DataT v) const { 00536 if (v < 0) return 0; else return (UINT32)v; 00537 } 00538 }; 00539 00540 #endif //PGF_PGFIMAGE_H