1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage class
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma interface "image.h"
18 #include "wx/object.h"
19 #include "wx/string.h"
20 #include "wx/gdicmn.h"
21 #include "wx/hashmap.h"
24 # include "wx/stream.h"
29 // on some systems (Unixware 7.x) index is defined as a macro in the headers
30 // which breaks the compilation below
33 #define wxIMAGE_OPTION_QUALITY wxString(_T("quality"))
34 #define wxIMAGE_OPTION_FILENAME wxString(_T("FileName"))
36 #define wxIMAGE_OPTION_RESOLUTION wxString(_T("Resolution"))
37 #define wxIMAGE_OPTION_RESOLUTIONX wxString(_T("ResolutionX"))
38 #define wxIMAGE_OPTION_RESOLUTIONY wxString(_T("ResolutionY"))
40 #define wxIMAGE_OPTION_RESOLUTIONUNIT wxString(_T("ResolutionUnit"))
42 // constants used with wxIMAGE_OPTION_RESOLUTIONUNIT
45 wxIMAGE_RESOLUTION_INCHES
= 1,
46 wxIMAGE_RESOLUTION_CM
= 2
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 class WXDLLEXPORT wxImageHandler
;
54 class WXDLLEXPORT wxImage
;
55 class WXDLLEXPORT wxPalette
;
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 class WXDLLEXPORT wxImageHandler
: public wxObject
65 : m_name(wxEmptyString
), m_extension(wxEmptyString
), m_mime(), m_type(0)
69 virtual bool LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
=true, int index
=-1 );
70 virtual bool SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
=true );
72 virtual int GetImageCount( wxInputStream
& stream
);
74 bool CanRead( wxInputStream
& stream
) { return CallDoCanRead(stream
); }
75 bool CanRead( const wxString
& name
);
76 #endif // wxUSE_STREAMS
78 void SetName(const wxString
& name
) { m_name
= name
; }
79 void SetExtension(const wxString
& ext
) { m_extension
= ext
; }
80 void SetType(long type
) { m_type
= type
; }
81 void SetMimeType(const wxString
& type
) { m_mime
= type
; }
82 wxString
GetName() const { return m_name
; }
83 wxString
GetExtension() const { return m_extension
; }
84 long GetType() const { return m_type
; }
85 wxString
GetMimeType() const { return m_mime
; }
89 virtual bool DoCanRead( wxInputStream
& stream
) = 0;
91 // save the stream position, call DoCanRead() and restore the position
92 bool CallDoCanRead(wxInputStream
& stream
);
93 #endif // wxUSE_STREAMS
101 DECLARE_CLASS(wxImageHandler
)
104 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
108 class WXDLLEXPORT wxImageHistogramEntry
111 wxImageHistogramEntry() { index
= value
= 0; }
116 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry
,
117 wxIntegerHash
, wxIntegerEqual
,
118 wxImageHistogramBase
);
120 class WXDLLEXPORT wxImageHistogram
: public wxImageHistogramBase
123 wxImageHistogram() : wxImageHistogramBase(256) { }
125 // get the key in the histogram for the given RGB values
126 static unsigned long MakeKey(unsigned char r
,
130 return (r
<< 16) | (g
<< 8) | b
;
133 // find first colour that is not used in the image and has higher
134 // RGB values than RGB(startR, startG, startB)
136 // returns true and puts this colour in r, g, b (each of which may be NULL)
137 // on success or returns false if there are no more free colours
138 bool FindFirstUnusedColour(unsigned char *r
,
141 unsigned char startR
= 1,
142 unsigned char startG
= 0,
143 unsigned char startB
= 0 ) const;
146 //-----------------------------------------------------------------------------
148 //-----------------------------------------------------------------------------
150 class WXDLLEXPORT wxImage
: public wxObject
154 wxImage( int width
, int height
, bool clear
= true );
155 wxImage( int width
, int height
, unsigned char* data
, bool static_data
= false );
156 wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
= false );
157 wxImage( const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
158 wxImage( const wxString
& name
, const wxString
& mimetype
, int index
= -1 );
159 wxImage( const char** xpmData
);
160 wxImage( char** xpmData
);
163 wxImage( wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
164 wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
= -1 );
165 #endif // wxUSE_STREAMS
167 wxImage( const wxImage
& image
);
168 wxImage( const wxImage
* image
);
170 bool Create( int width
, int height
, bool clear
= true );
171 bool Create( int width
, int height
, unsigned char* data
, bool static_data
= false );
172 bool Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
= false );
173 bool Create( const char** xpmData
);
176 // creates an identical copy of the image (the = operator
177 // just raises the ref count)
178 wxImage
Copy() const;
180 // return the new image with size width*height
181 wxImage
GetSubImage( const wxRect
& ) const;
183 // pastes image into this instance and takes care of
184 // the mask colour and out of bounds problems
185 void Paste( const wxImage
&image
, int x
, int y
);
187 // return the new image with size width*height
188 wxImage
Scale( int width
, int height
) const;
190 wxImage
ShrinkBy( int xFactor
, int yFactor
) const ;
192 // rescales the image in place
193 wxImage
& Rescale( int width
, int height
) { return *this = Scale(width
, height
); }
195 // Rotates the image about the given point, 'angle' radians.
196 // Returns the rotated image, leaving this image intact.
197 wxImage
Rotate(double angle
, const wxPoint
& centre_of_rotation
,
198 bool interpolating
= true, wxPoint
* offset_after_rotation
= (wxPoint
*) NULL
) const;
200 wxImage
Rotate90( bool clockwise
= true ) const;
201 wxImage
Mirror( bool horizontally
= true ) const;
203 // replace one colour with another
204 void Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
205 unsigned char r2
, unsigned char g2
, unsigned char b2
);
207 // convert to monochrome image (<r,g,b> will be replaced by white,
208 // everything else by black)
209 wxImage
ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const;
211 // these routines are slow but safe
212 void SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
);
213 unsigned char GetRed( int x
, int y
) const;
214 unsigned char GetGreen( int x
, int y
) const;
215 unsigned char GetBlue( int x
, int y
) const;
217 void SetAlpha(int x
, int y
, unsigned char alpha
);
218 unsigned char GetAlpha(int x
, int y
) const;
220 // find first colour that is not used in the image and has higher
221 // RGB values than <startR,startG,startB>
222 bool FindFirstUnusedColour( unsigned char *r
, unsigned char *g
, unsigned char *b
,
223 unsigned char startR
= 1, unsigned char startG
= 0,
224 unsigned char startB
= 0 ) const;
225 // Set image's mask to the area of 'mask' that has <r,g,b> colour
226 bool SetMaskFromImage(const wxImage
& mask
,
227 unsigned char mr
, unsigned char mg
, unsigned char mb
);
229 // converts image's alpha channel to mask, if it has any, does nothing
231 bool ConvertAlphaToMask(unsigned char threshold
= 128);
233 // This method converts an image where the original alpha
234 // information is only available as a shades of a colour
235 // (actually shades of grey) typically when you draw anti-
236 // aliased text into a bitmap. The DC drawinf routines
237 // draw grey values on the black background although they
238 // actually mean to draw white with differnt alpha values.
239 // This method reverses it, assuming a black (!) background
240 // and white text (actually only the red channel is read).
241 // The method will then fill up the whole image with the
243 bool ConvertColourToAlpha( unsigned char r
, unsigned char g
, unsigned char b
);
245 static bool CanRead( const wxString
& name
);
246 static int GetImageCount( const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
);
247 virtual bool LoadFile( const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
248 virtual bool LoadFile( const wxString
& name
, const wxString
& mimetype
, int index
= -1 );
251 static bool CanRead( wxInputStream
& stream
);
252 static int GetImageCount( wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
);
253 virtual bool LoadFile( wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
254 virtual bool LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
= -1 );
257 virtual bool SaveFile( const wxString
& name
) const;
258 virtual bool SaveFile( const wxString
& name
, int type
) const;
259 virtual bool SaveFile( const wxString
& name
, const wxString
& mimetype
) const;
262 virtual bool SaveFile( wxOutputStream
& stream
, int type
) const;
263 virtual bool SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const;
267 int GetWidth() const;
268 int GetHeight() const;
270 // these functions provide fastest access to wxImage data but should be
271 // used carefully as no checks are done
272 unsigned char *GetData() const;
273 void SetData( unsigned char *data
);
274 void SetData( unsigned char *data
, int new_width
, int new_height
);
276 unsigned char *GetAlpha() const; // may return NULL!
277 bool HasAlpha() const { return GetAlpha() != NULL
; }
278 void SetAlpha(unsigned char *alpha
= NULL
);
282 void SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
);
283 unsigned char GetMaskRed() const;
284 unsigned char GetMaskGreen() const;
285 unsigned char GetMaskBlue() const;
286 void SetMask( bool mask
= true );
287 bool HasMask() const;
291 bool HasPalette() const;
292 const wxPalette
& GetPalette() const;
293 void SetPalette(const wxPalette
& palette
);
294 #endif // wxUSE_PALETTE
296 // Option functions (arbitrary name/value mapping)
297 void SetOption(const wxString
& name
, const wxString
& value
);
298 void SetOption(const wxString
& name
, int value
);
299 wxString
GetOption(const wxString
& name
) const;
300 int GetOptionInt(const wxString
& name
) const;
301 bool HasOption(const wxString
& name
) const;
303 unsigned long CountColours( unsigned long stopafter
= (unsigned long) -1 ) const;
305 // Computes the histogram of the image and fills a hash table, indexed
306 // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry
307 // objects. Each of them contains an 'index' (useful to build a palette
308 // with the image colours) and a 'value', which is the number of pixels
309 // in the image with that colour.
310 // Returned value: # of entries in the histogram
311 unsigned long ComputeHistogram( wxImageHistogram
&h
) const;
313 wxImage
& operator = (const wxImage
& image
)
315 if ( (*this) != image
)
320 bool operator == (const wxImage
& image
) const
321 { return m_refData
== image
.m_refData
; }
322 bool operator != (const wxImage
& image
) const
323 { return m_refData
!= image
.m_refData
; }
325 static wxList
& GetHandlers() { return sm_handlers
; }
326 static void AddHandler( wxImageHandler
*handler
);
327 static void InsertHandler( wxImageHandler
*handler
);
328 static bool RemoveHandler( const wxString
& name
);
329 static wxImageHandler
*FindHandler( const wxString
& name
);
330 static wxImageHandler
*FindHandler( const wxString
& extension
, long imageType
);
331 static wxImageHandler
*FindHandler( long imageType
);
332 static wxImageHandler
*FindHandlerMime( const wxString
& mimetype
);
334 static wxString
GetImageExtWildcard();
336 static void CleanUpHandlers();
337 static void InitStandardHandlers();
340 static wxList sm_handlers
;
343 friend class WXDLLEXPORT wxImageHandler
;
345 DECLARE_DYNAMIC_CLASS(wxImage
)
349 extern void WXDLLEXPORT
wxInitAllImageHandlers();
351 extern WXDLLEXPORT_DATA(wxImage
) wxNullImage
;
353 //-----------------------------------------------------------------------------
355 //-----------------------------------------------------------------------------
357 #include "wx/imagbmp.h"
358 #include "wx/imagpng.h"
359 #include "wx/imaggif.h"
360 #include "wx/imagpcx.h"
361 #include "wx/imagjpeg.h"
362 #include "wx/imagtiff.h"
363 #include "wx/imagpnm.h"
364 #include "wx/imagxpm.h"
365 #include "wx/imagiff.h"
367 #endif // wxUSE_IMAGE