libpgf  6.11.42
PGF - Progressive Graphics File
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 
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.usedBitsPerChannel) - 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 CurrentChannelDepth(m_preHeader.version); }
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 
00462         BYTE Version() const                                                                                    { return CurrentVersion(m_preHeader.version); }
00463 
00464         //class methods
00465 
00470         static bool ImportIsSupported(BYTE mode);
00471 
00477         static UINT32 LevelWidth(UINT32 width, int level)                               { ASSERT(level >= 0); UINT32 w = (width >> level); return ((w << level) == width) ? w : w + 1; }
00478 
00484         static UINT32 LevelHeight(UINT32 height, int level)                             { ASSERT(level >= 0); UINT32 h = (height >> level); return ((h << level) == height) ? h : h + 1; }
00485 
00489         static BYTE CurrentVersion(BYTE version = PGFVersion);
00490 
00494         static BYTE CurrentChannelDepth(BYTE version = PGFVersion)              { return (version & PGF32) ? 32 : 16; }
00495 
00496 protected:
00497         CWaveletTransform* m_wtChannel[MaxChannels];    
00498         DataT* m_channel[MaxChannels];                                  
00499         CDecoder* m_decoder;                    
00500         CEncoder* m_encoder;                    
00501         UINT32* m_levelLength;                  
00502         UINT32 m_width[MaxChannels];    
00503         UINT32 m_height[MaxChannels];   
00504         PGFPreHeader m_preHeader;               
00505         PGFHeader m_header;                             
00506         PGFPostHeader m_postHeader;             
00507         int m_currentLevel;                             
00508         BYTE m_quant;                                   
00509         bool m_downsample;                              
00510         bool m_favorSpeedOverSize;              
00511         bool m_useOMPinEncoder;                 
00512         bool m_useOMPinDecoder;                 
00513 #ifdef __PGFROISUPPORT__
00514         bool m_levelwise;                               
00515         bool m_streamReinitialized;             
00516         PGFRect m_roi;                                  
00517 #endif
00518 
00519 private:        
00520         RefreshCB m_cb;                                 
00521         void *m_cbArg;                                  
00522 
00523         void ComputeLevels();
00524         void CompleteHeader();
00525         void RgbToYuv(int pitch, UINT8* rgbBuff, BYTE bpp, int channelMap[], CallbackPtr cb, void *data) THROW_;
00526         void Downsample(int nChannel);
00527         void WriteLevel() THROW_;
00528 
00529 #ifdef __PGFROISUPPORT__
00530         void SetROI(PGFRect rect);
00531 #endif
00532 
00533         UINT8 Clamp4(DataT v) const {
00534                 if (v & 0xFFFFFFF0) return (v < 0) ? (UINT8)0: (UINT8)15; else return (UINT8)v;
00535         }       
00536         UINT16 Clamp6(DataT v) const {
00537                 if (v & 0xFFFFFFC0) return (v < 0) ? (UINT16)0: (UINT16)63; else return (UINT16)v;
00538         }       
00539         UINT8 Clamp8(DataT v) const {
00540                 // needs only one test in the normal case
00541                 if (v & 0xFFFFFF00) return (v < 0) ? (UINT8)0 : (UINT8)255; else return (UINT8)v;
00542         }
00543         UINT16 Clamp16(DataT v) const {
00544                 if (v & 0xFFFF0000) return (v < 0) ? (UINT16)0: (UINT16)65535; else return (UINT16)v;
00545         }       
00546         UINT32 Clamp31(DataT v) const {
00547                 return (v < 0) ? 0 : (UINT32)v;
00548         }       
00549 };
00550 
00551 #endif //PGF_PGFIMAGE_H
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines