libpgf  6.12.24
PGF - Progressive Graphics File
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
PGFimage.h
Go to the documentation of this 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 
00035 // types
00036 enum ProgressMode { PM_Relative, PM_Absolute };
00037 
00039 // prototypes
00040 class CDecoder;
00041 class CEncoder;
00042 class CWaveletTransform;
00043 
00057 class CPGFImage {
00058 public:
00059         
00062         CPGFImage();
00063 
00066         virtual ~CPGFImage();
00067 
00071         virtual void Close();
00072 
00076         virtual void Destroy();
00077 
00083         void Open(CPGFStream* stream) THROW_;
00084 
00087         bool IsOpen() const     { return m_decoder != NULL; }
00088 
00101         void Read(int level = 0, CallbackPtr cb = NULL, void *data = NULL) THROW_;
00102 
00103 #ifdef __PGFROISUPPORT__
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113         void Read(PGFRect& rect, int level = 0, CallbackPtr cb = NULL, void *data = NULL) THROW_;
00114 #endif
00115 
00121         void ReadPreview() THROW_                                                                               { Read(Levels() - 1); }
00122 
00128         void Reconstruct(int level = 0) THROW_;
00129 
00147         void GetBitmap(int pitch, UINT8* buff, BYTE bpp, int channelMap[] = NULL, CallbackPtr cb = NULL, void *data = NULL) const THROW_; // throws IOException
00148 
00164         void GetYUV(int pitch, DataT* buff, BYTE bpp, int channelMap[] = NULL, CallbackPtr cb = NULL, void *data = NULL) const THROW_; // throws IOException
00165 
00182         void ImportBitmap(int pitch, UINT8 *buff, BYTE bpp, int channelMap[] = NULL, CallbackPtr cb = NULL, void *data = NULL) THROW_;
00183 
00199         void ImportYUV(int pitch, DataT *buff, BYTE bpp, int channelMap[] = NULL, CallbackPtr cb = NULL, void *data = NULL) THROW_;
00200 
00214         void Write(CPGFStream* stream, UINT32* nWrittenBytes = NULL, CallbackPtr cb = NULL, void *data = NULL) THROW_;
00215 
00223         UINT32 WriteHeader(CPGFStream* stream) THROW_;
00224 
00235         UINT32 WriteImage(CPGFStream* stream, CallbackPtr cb = NULL, void *data = NULL) THROW_;
00236 
00237 #ifdef __PGFROISUPPORT__
00238 
00239 
00240 
00241 
00242 
00243 
00244 
00245 
00246 
00247 
00248 
00249 
00250 
00251 
00252 
00253         UINT32 Write(int level, CallbackPtr cb = NULL, void *data = NULL) THROW_;
00254 #endif
00255 
00260         void ConfigureEncoder(bool useOMP = true, bool favorSpeedOverSize = false) { m_useOMPinEncoder = useOMP; m_favorSpeedOverSize = favorSpeedOverSize; }
00261 
00266         void ConfigureDecoder(bool useOMP = true, bool skipUserData = false) { m_useOMPinDecoder = useOMP; m_skipUserData = skipUserData; }
00267 
00270         void ResetStreamPos() THROW_;
00271 
00276         void SetChannel(DataT* channel, int c = 0)                                              { ASSERT(c >= 0 && c < MaxChannels); m_channel[c] = channel; }
00277 
00286         void SetHeader(const PGFHeader& header, BYTE flags = 0, UINT8* userData = 0, UINT32 userDataLength = 0) THROW_; // throws IOException
00287 
00292         void SetMaxValue(UINT32 maxValue);
00293 
00300         void SetProgressMode(ProgressMode pm)                                                   { m_progressMode = pm; }
00301 
00307         void SetRefreshCallback(RefreshCB callback, void* arg)                  { m_cb = callback; m_cbArg = arg; }
00308 
00315         void SetColorTable(UINT32 iFirstColor, UINT32 nColors, const RGBQUAD* prgbColors) THROW_;
00316 
00321         DataT* GetChannel(int c = 0)                                                                    { ASSERT(c >= 0 && c < MaxChannels); return m_channel[c]; }
00322 
00329         void GetColorTable(UINT32 iFirstColor, UINT32 nColors, RGBQUAD* prgbColors) const THROW_;
00330 
00332         // Returns address of internal color table
00334         const RGBQUAD* GetColorTable() const                                                    { return m_postHeader.clut; }
00335 
00339         const PGFHeader* GetHeader() const                                                              { return &m_header; }
00340 
00345         UINT32 GetMaxValue() const                                                                              { return (1 << m_header.usedBitsPerChannel) - 1; }
00346 
00350         UINT64 GetUserDataPos() const                                                                   { return m_userDataPos; }                                                                       
00351 
00357         const UINT8* GetUserData(UINT32& size) const;
00358 
00363         UINT32 GetEncodedHeaderLength() const;
00364 
00370         UINT32 GetEncodedLevelLength(int level) const                                   { ASSERT(level >= 0 && level < m_header.nLevels); return m_levelLength[m_header.nLevels - level - 1]; }
00371 
00379         UINT32 ReadEncodedHeader(UINT8* target, UINT32 targetLen) const THROW_;
00380 
00390         UINT32 ReadEncodedData(int level, UINT8* target, UINT32 targetLen) const THROW_;
00391 
00397         UINT32 ChannelWidth(int c = 0) const                                                    { ASSERT(c >= 0 && c < MaxChannels); return m_width[c]; }
00398 
00404         UINT32 ChannelHeight(int c = 0) const                                                   { ASSERT(c >= 0 && c < MaxChannels); return m_height[c]; }
00405 
00409         BYTE ChannelDepth() const                                                                               { return CurrentChannelDepth(m_preHeader.version); }
00410 
00416         UINT32 Width(int level = 0) const                                                               { ASSERT(level >= 0); return LevelWidth(m_header.width, level); }
00417 
00423         UINT32 Height(int level = 0) const                                                              { ASSERT(level >= 0); return LevelHeight(m_header.height, level); }
00424 
00430         BYTE Level() const                                                                                              { return (BYTE)m_currentLevel; }
00431 
00435         BYTE Levels() const                                                                                             { return m_header.nLevels; }
00436 
00441         BYTE Quality() const                                                                                    { return m_header.quality; }
00442 
00447         BYTE Channels() const                                                                                   { return m_header.channels; }
00448         
00454         BYTE Mode() const                                                                                               { return m_header.mode; }
00455 
00460         BYTE BPP() const                                                                                                { return m_header.bpp; }
00461 
00465         bool ROIisSupported() const                                                                             { return (m_preHeader.version & PGFROI) == PGFROI; }
00466 
00471         BYTE UsedBitsPerChannel() const;
00472 
00476         BYTE Version() const                                                                                    { return CurrentVersion(m_preHeader.version); }
00477 
00478         //class methods
00479 
00484         static bool ImportIsSupported(BYTE mode);
00485 
00491         static UINT32 LevelWidth(UINT32 width, int level)                               { ASSERT(level >= 0); UINT32 w = (width >> level); return ((w << level) == width) ? w : w + 1; }
00492 
00498         static UINT32 LevelHeight(UINT32 height, int level)                             { ASSERT(level >= 0); UINT32 h = (height >> level); return ((h << level) == height) ? h : h + 1; }
00499 
00503         static BYTE CurrentVersion(BYTE version = PGFVersion);
00504 
00508         static BYTE CurrentChannelDepth(BYTE version = PGFVersion)              { return (version & PGF32) ? 32 : 16; }
00509 
00510 protected:
00511         CWaveletTransform* m_wtChannel[MaxChannels];    
00512         DataT* m_channel[MaxChannels];                                  
00513         CDecoder* m_decoder;                    
00514         CEncoder* m_encoder;                    
00515         UINT32* m_levelLength;                  
00516         UINT32 m_width[MaxChannels];    
00517         UINT32 m_height[MaxChannels];   
00518         PGFPreHeader m_preHeader;               
00519         PGFHeader m_header;                             
00520         PGFPostHeader m_postHeader;             
00521         UINT64 m_userDataPos;                   
00522         int m_currentLevel;                             
00523         BYTE m_quant;                                   
00524         bool m_downsample;                              
00525         bool m_favorSpeedOverSize;              
00526         bool m_useOMPinEncoder;                 
00527         bool m_useOMPinDecoder;                 
00528         bool m_skipUserData;                    
00529 #ifdef __PGFROISUPPORT__
00530         bool m_streamReinitialized;             
00531         PGFRect m_roi;                                  
00532 #endif
00533 
00534 private:        
00535         RefreshCB m_cb;                                 
00536         void *m_cbArg;                                  
00537         double m_percent;                               
00538         ProgressMode m_progressMode;    
00539 
00540         void ComputeLevels();
00541         void CompleteHeader();
00542         void RgbToYuv(int pitch, UINT8* rgbBuff, BYTE bpp, int channelMap[], CallbackPtr cb, void *data) THROW_;
00543         void Downsample(int nChannel);
00544         UINT32 UpdatePostHeaderSize() THROW_;
00545         void WriteLevel() THROW_;
00546 
00547 #ifdef __PGFROISUPPORT__
00548         void SetROI(PGFRect rect);
00549 #endif
00550 
00551         UINT8 Clamp4(DataT v) const {
00552                 if (v & 0xFFFFFFF0) return (v < 0) ? (UINT8)0: (UINT8)15; else return (UINT8)v;
00553         }       
00554         UINT16 Clamp6(DataT v) const {
00555                 if (v & 0xFFFFFFC0) return (v < 0) ? (UINT16)0: (UINT16)63; else return (UINT16)v;
00556         }       
00557         UINT8 Clamp8(DataT v) const {
00558                 // needs only one test in the normal case
00559                 if (v & 0xFFFFFF00) return (v < 0) ? (UINT8)0 : (UINT8)255; else return (UINT8)v;
00560         }
00561         UINT16 Clamp16(DataT v) const {
00562                 if (v & 0xFFFF0000) return (v < 0) ? (UINT16)0: (UINT16)65535; else return (UINT16)v;
00563         }       
00564         UINT32 Clamp31(DataT v) const {
00565                 return (v < 0) ? 0 : (UINT32)v;
00566         }       
00567 };
00568 
00569 #endif //PGF_PGFIMAGE_H