Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members

image.h

00001 
00002 // Name:        image.h
00003 // Purpose:     wxImage class
00004 // Author:      Robert Roebling
00005 // RCS-ID:      $Id: image.h,v 1.108 2005/07/28 22:50:19 VZ Exp $
00006 // Copyright:   (c) Robert Roebling
00007 // Licence:     wxWindows licence
00009 
00010 #ifndef _WX_IMAGE_H_
00011 #define _WX_IMAGE_H_
00012 
00013 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
00014 #pragma interface "image.h"
00015 #endif
00016 
00017 #include "wx/defs.h"
00018 #include "wx/object.h"
00019 #include "wx/string.h"
00020 #include "wx/gdicmn.h"
00021 #include "wx/hashmap.h"
00022 
00023 #if wxUSE_STREAMS
00024 #  include "wx/stream.h"
00025 #endif
00026 
00027 #if wxUSE_IMAGE
00028 
00029 // on some systems (Unixware 7.x) index is defined as a macro in the headers
00030 // which breaks the compilation below
00031 #undef index
00032 
00033 #define wxIMAGE_OPTION_QUALITY  wxString(_T("quality"))
00034 #define wxIMAGE_OPTION_FILENAME wxString(_T("FileName"))
00035 
00036 #define wxIMAGE_OPTION_RESOLUTION            wxString(_T("Resolution"))
00037 #define wxIMAGE_OPTION_RESOLUTIONX           wxString(_T("ResolutionX"))
00038 #define wxIMAGE_OPTION_RESOLUTIONY           wxString(_T("ResolutionY"))
00039 
00040 #define wxIMAGE_OPTION_RESOLUTIONUNIT        wxString(_T("ResolutionUnit"))
00041 
00042 // constants used with wxIMAGE_OPTION_RESOLUTIONUNIT
00043 enum
00044 {
00045     wxIMAGE_RESOLUTION_INCHES = 1,
00046     wxIMAGE_RESOLUTION_CM = 2
00047 };
00048 
00049 // alpha channel values: fully transparent, default threshold separating
00050 // transparent pixels from opaque for a few functions dealing with alpha and
00051 // fully opaque
00052 const unsigned char wxIMAGE_ALPHA_TRANSPARENT = 0;
00053 const unsigned char wxIMAGE_ALPHA_THRESHOLD = 0x80;
00054 const unsigned char wxIMAGE_ALPHA_OPAQUE = 0xff;
00055 
00056 //-----------------------------------------------------------------------------
00057 // classes
00058 //-----------------------------------------------------------------------------
00059 
00060 class WXDLLEXPORT wxImageHandler;
00061 class WXDLLEXPORT wxImage;
00062 class WXDLLEXPORT wxPalette;
00063 
00064 //-----------------------------------------------------------------------------
00065 // wxImageHandler
00066 //-----------------------------------------------------------------------------
00067 
00068 class WXDLLEXPORT wxImageHandler: public wxObject
00069 {
00070 public:
00071     wxImageHandler()
00072         : m_name(wxEmptyString), m_extension(wxEmptyString), m_mime(), m_type(0)
00073         { }
00074 
00075 #if wxUSE_STREAMS
00076     virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=true, int index=-1 );
00077     virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=true );
00078 
00079     virtual int GetImageCount( wxInputStream& stream );
00080 
00081     bool CanRead( wxInputStream& stream ) { return CallDoCanRead(stream); }
00082     bool CanRead( const wxString& name );
00083 #endif // wxUSE_STREAMS
00084 
00085     void SetName(const wxString& name) { m_name = name; }
00086     void SetExtension(const wxString& ext) { m_extension = ext; }
00087     void SetType(long type) { m_type = type; }
00088     void SetMimeType(const wxString& type) { m_mime = type; }
00089     wxString GetName() const { return m_name; }
00090     wxString GetExtension() const { return m_extension; }
00091     long GetType() const { return m_type; }
00092     wxString GetMimeType() const { return m_mime; }
00093 
00094 protected:
00095 #if wxUSE_STREAMS
00096     virtual bool DoCanRead( wxInputStream& stream ) = 0;
00097 
00098     // save the stream position, call DoCanRead() and restore the position
00099     bool CallDoCanRead(wxInputStream& stream);
00100 #endif // wxUSE_STREAMS
00101 
00102     wxString  m_name;
00103     wxString  m_extension;
00104     wxString  m_mime;
00105     long      m_type;
00106 
00107 private:
00108     DECLARE_CLASS(wxImageHandler)
00109 };
00110 
00111 //-----------------------------------------------------------------------------
00112 // wxImageHistogram
00113 //-----------------------------------------------------------------------------
00114 
00115 class WXDLLEXPORT wxImageHistogramEntry
00116 {
00117 public:
00118     wxImageHistogramEntry() { index = value = 0; }
00119     unsigned long index;
00120     unsigned long value;
00121 };
00122 
00123 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
00124                              wxIntegerHash, wxIntegerEqual,
00125                              wxImageHistogramBase);
00126 
00127 class WXDLLEXPORT wxImageHistogram : public wxImageHistogramBase
00128 {
00129 public:
00130     wxImageHistogram() : wxImageHistogramBase(256) { }
00131 
00132     // get the key in the histogram for the given RGB values
00133     static unsigned long MakeKey(unsigned char r,
00134                                  unsigned char g,
00135                                  unsigned char b)
00136     {
00137         return (r << 16) | (g << 8) | b;
00138     }
00139 
00140     // find first colour that is not used in the image and has higher
00141     // RGB values than RGB(startR, startG, startB)
00142     //
00143     // returns true and puts this colour in r, g, b (each of which may be NULL)
00144     // on success or returns false if there are no more free colours
00145     bool FindFirstUnusedColour(unsigned char *r,
00146                                unsigned char *g,
00147                                unsigned char *b,
00148                                unsigned char startR = 1,
00149                                unsigned char startG = 0,
00150                                unsigned char startB = 0 ) const;
00151 };
00152 
00153 //-----------------------------------------------------------------------------
00154 // wxImage
00155 //-----------------------------------------------------------------------------
00156 
00157 class WXDLLEXPORT wxImage: public wxObject
00158 {
00159 public:
00160 #if wxABI_VERSION >= 20602
00161     // red, green and blue are 8 bit unsigned integers in the range of 0..255
00162     // We use the identifier RGBValue instead of RGB, since RGB is #defined
00163     class RGBValue
00164     {
00165     public:
00166       RGBValue(unsigned char r=0, unsigned char g=0, unsigned char b=0)
00167         : red(r), green(g), blue(b) {}
00168         unsigned char red;  
00169         unsigned char green;
00170         unsigned char blue;
00171     };
00172 
00173     // hue, saturation and value are doubles in the range 0.0..1.0
00174     class HSVValue
00175     {
00176     public:
00177         HSVValue(double h=0.0, double s=0.0, double v=0.0)
00178             : hue(h), saturation(s), value(v) {}
00179         double hue;  
00180         double saturation;
00181         double value;
00182     };
00183 #endif // wxABI_VERSION >= 2.6.2
00184 
00185     wxImage(){}
00186     wxImage( int width, int height, bool clear = true );
00187     wxImage( int width, int height, unsigned char* data, bool static_data = false );
00188     wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
00189     wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
00190     wxImage( const wxString& name, const wxString& mimetype, int index = -1 );
00191     wxImage( const char** xpmData );
00192     wxImage( char** xpmData );
00193 
00194 #if wxUSE_STREAMS
00195     wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
00196     wxImage( wxInputStream& stream, const wxString& mimetype, int index = -1 );
00197 #endif // wxUSE_STREAMS
00198 
00199     wxImage( const wxImage& image );
00200     wxImage( const wxImage* image );
00201 
00202     bool Create( int width, int height, bool clear = true );
00203     bool Create( int width, int height, unsigned char* data, bool static_data = false );
00204     bool Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
00205     bool Create( const char** xpmData );
00206     void Destroy();
00207 
00208     // creates an identical copy of the image (the = operator
00209     // just raises the ref count)
00210     wxImage Copy() const;
00211 
00212     // return the new image with size width*height
00213     wxImage GetSubImage( const wxRect& rect) const;
00214 
00215     // Paste the image or part of this image into an image of the given size at the pos
00216     //  any newly exposed areas will be filled with the rgb colour
00217     //  by default if r = g = b = -1 then fill with this image's mask colour or find and
00218     //  set a suitable mask colour
00219     wxImage Size( const wxSize& size, const wxPoint& pos,
00220                   int r = -1, int g = -1, int b = -1 ) const;
00221 
00222     // pastes image into this instance and takes care of
00223     // the mask colour and out of bounds problems
00224     void Paste( const wxImage &image, int x, int y );
00225 
00226     // return the new image with size width*height
00227     wxImage Scale( int width, int height ) const;
00228 
00229     wxImage ShrinkBy( int xFactor , int yFactor ) const ;
00230 
00231     // rescales the image in place
00232     wxImage& Rescale( int width, int height ) { return *this = Scale(width, height); }
00233 
00234     // resizes the image in place
00235     wxImage& Resize( const wxSize& size, const wxPoint& pos,
00236                      int r = -1, int g = -1, int b = -1 ) { return *this = Size(size, pos, r, g, b); }
00237 
00238     // Rotates the image about the given point, 'angle' radians.
00239     // Returns the rotated image, leaving this image intact.
00240     wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
00241                    bool interpolating = true, wxPoint * offset_after_rotation = (wxPoint*) NULL) const;
00242 
00243     wxImage Rotate90( bool clockwise = true ) const;
00244     wxImage Mirror( bool horizontally = true ) const;
00245 
00246     // replace one colour with another
00247     void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
00248                   unsigned char r2, unsigned char g2, unsigned char b2 );
00249 
00250     // convert to monochrome image (<r,g,b> will be replaced by white,
00251     // everything else by black)
00252     wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
00253 
00254     // these routines are slow but safe
00255     void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
00256     void SetRGB( const wxRect& rect, unsigned char r, unsigned char g, unsigned char b );
00257     unsigned char GetRed( int x, int y ) const;
00258     unsigned char GetGreen( int x, int y ) const;
00259     unsigned char GetBlue( int x, int y ) const;
00260 
00261     void SetAlpha(int x, int y, unsigned char alpha);
00262     unsigned char GetAlpha(int x, int y) const;
00263 
00264     // find first colour that is not used in the image and has higher
00265     // RGB values than <startR,startG,startB>
00266     bool FindFirstUnusedColour( unsigned char *r, unsigned char *g, unsigned char *b,
00267                                 unsigned char startR = 1, unsigned char startG = 0,
00268                                 unsigned char startB = 0 ) const;
00269     // Set image's mask to the area of 'mask' that has <r,g,b> colour
00270     bool SetMaskFromImage(const wxImage & mask,
00271                           unsigned char mr, unsigned char mg, unsigned char mb);
00272 
00273     // converts image's alpha channel to mask, if it has any, does nothing
00274     // otherwise:
00275     bool ConvertAlphaToMask(unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD);
00276 
00277     // This method converts an image where the original alpha
00278     // information is only available as a shades of a colour
00279     // (actually shades of grey) typically when you draw anti-
00280     // aliased text into a bitmap. The DC drawinf routines
00281     // draw grey values on the black background although they
00282     // actually mean to draw white with differnt alpha values.
00283     // This method reverses it, assuming a black (!) background
00284     // and white text (actually only the red channel is read).
00285     // The method will then fill up the whole image with the
00286     // colour given.
00287     bool ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b );
00288 
00289     static bool CanRead( const wxString& name );
00290     static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
00291     virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
00292     virtual bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
00293 
00294 #if wxUSE_STREAMS
00295     static bool CanRead( wxInputStream& stream );
00296     static int GetImageCount( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
00297     virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
00298     virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
00299 #endif
00300 
00301     virtual bool SaveFile( const wxString& name ) const;
00302     virtual bool SaveFile( const wxString& name, int type ) const;
00303     virtual bool SaveFile( const wxString& name, const wxString& mimetype ) const;
00304 
00305 #if wxUSE_STREAMS
00306     virtual bool SaveFile( wxOutputStream& stream, int type ) const;
00307     virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype ) const;
00308 #endif
00309 
00310     bool Ok() const;
00311     int GetWidth() const;
00312     int GetHeight() const;
00313 
00314     // these functions provide fastest access to wxImage data but should be
00315     // used carefully as no checks are done
00316     unsigned char *GetData() const;
00317     void SetData( unsigned char *data, bool static_data=false );
00318     void SetData( unsigned char *data, int new_width, int new_height, bool static_data=false );
00319 
00320     unsigned char *GetAlpha() const;    // may return NULL!
00321     bool HasAlpha() const { return GetAlpha() != NULL; }
00322     void SetAlpha(unsigned char *alpha = NULL, bool static_data=false);
00323     void InitAlpha();
00324 
00325     // return true if this pixel is masked or has alpha less than specified
00326     // threshold
00327     bool IsTransparent(int x, int y,
00328                        unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD) const;
00329 
00330     // Mask functions
00331     void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
00332     // Get the current mask colour or find a suitable colour
00333     // returns true if using current mask colour
00334     bool GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const;
00335     unsigned char GetMaskRed() const;
00336     unsigned char GetMaskGreen() const;
00337     unsigned char GetMaskBlue() const;
00338     void SetMask( bool mask = true );
00339     bool HasMask() const;
00340 
00341 #if wxUSE_PALETTE
00342     // Palette functions
00343     bool HasPalette() const;
00344     const wxPalette& GetPalette() const;
00345     void SetPalette(const wxPalette& palette);
00346 #endif // wxUSE_PALETTE
00347 
00348     // Option functions (arbitrary name/value mapping)
00349     void SetOption(const wxString& name, const wxString& value);
00350     void SetOption(const wxString& name, int value);
00351     wxString GetOption(const wxString& name) const;
00352     int GetOptionInt(const wxString& name) const;
00353     bool HasOption(const wxString& name) const;
00354 
00355     unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ) const;
00356 
00357     // Computes the histogram of the image and fills a hash table, indexed
00358     // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry
00359     // objects. Each of them contains an 'index' (useful to build a palette
00360     // with the image colours) and a 'value', which is the number of pixels
00361     // in the image with that colour.
00362     // Returned value: # of entries in the histogram
00363     unsigned long ComputeHistogram( wxImageHistogram &h ) const;
00364 
00365 #if wxABI_VERSION >= 20602
00366     // Rotates the hue of each pixel of the image. angle is a double in the range
00367     // -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
00368     void RotateHue(double angle);
00369 #endif // wxABI_VERSION >= 2.6.2
00370 
00371     wxImage& operator = (const wxImage& image)
00372     {
00373         if ( (*this) != image )
00374             Ref(image);
00375         return *this;
00376     }
00377 
00378     bool operator == (const wxImage& image) const
00379         { return m_refData == image.m_refData; }
00380     bool operator != (const wxImage& image) const
00381         { return m_refData != image.m_refData; }
00382 
00383     static wxList& GetHandlers() { return sm_handlers; }
00384     static void AddHandler( wxImageHandler *handler );
00385     static void InsertHandler( wxImageHandler *handler );
00386     static bool RemoveHandler( const wxString& name );
00387     static wxImageHandler *FindHandler( const wxString& name );
00388     static wxImageHandler *FindHandler( const wxString& extension, long imageType );
00389     static wxImageHandler *FindHandler( long imageType );
00390     static wxImageHandler *FindHandlerMime( const wxString& mimetype );
00391 
00392     static wxString GetImageExtWildcard();
00393 
00394     static void CleanUpHandlers();
00395     static void InitStandardHandlers();
00396 
00397 #if wxABI_VERSION >= 20602
00398     static HSVValue RGBtoHSV(const RGBValue& rgb);
00399     static RGBValue HSVtoRGB(const HSVValue& hsv);
00400 #endif // wxABI_VERSION >= 2.6.2
00401 
00402 
00403 protected:
00404     static wxList   sm_handlers;
00405 
00406     // return the index of the point with the given coordinates or -1 if the
00407     // image is invalid of the coordinates are out of range
00408     //
00409     // note that index must be multiplied by 3 when using it with RGB array
00410     long XYToIndex(int x, int y) const;
00411 
00412 private:
00413     friend class WXDLLEXPORT wxImageHandler;
00414 
00415     DECLARE_DYNAMIC_CLASS(wxImage)
00416 };
00417 
00418 
00419 extern void WXDLLEXPORT wxInitAllImageHandlers();
00420 
00421 extern WXDLLEXPORT_DATA(wxImage)    wxNullImage;
00422 
00423 //-----------------------------------------------------------------------------
00424 // wxImage handlers
00425 //-----------------------------------------------------------------------------
00426 
00427 #include "wx/imagbmp.h"
00428 #include "wx/imagpng.h"
00429 #include "wx/imaggif.h"
00430 #include "wx/imagpcx.h"
00431 #include "wx/imagjpeg.h"
00432 #include "wx/imagtiff.h"
00433 #include "wx/imagpnm.h"
00434 #include "wx/imagxpm.h"
00435 #include "wx/imagiff.h"
00436 
00437 #endif // wxUSE_IMAGE
00438 
00439 #endif
00440   // _WX_IMAGE_H_
00441 

Generated on Tue Feb 28 16:44:01 2006 for xCHM by  doxygen 1.4.4