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_FILENAME wxString(_T("FileName"))
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 class WXDLLEXPORT wxImageHandler
;
40 class WXDLLEXPORT wxImage
;
41 class WXDLLEXPORT wxPalette
;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 class WXDLLEXPORT wxImageHandler
: public wxObject
51 : m_name(wxEmptyString
), m_extension(wxEmptyString
), m_mime(), m_type(0)
55 virtual bool LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
=true, int index
=-1 );
56 virtual bool SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
=true );
58 virtual int GetImageCount( wxInputStream
& stream
);
60 bool CanRead( wxInputStream
& stream
) { return CallDoCanRead(stream
); }
61 bool CanRead( const wxString
& name
);
62 #endif // wxUSE_STREAMS
64 void SetName(const wxString
& name
) { m_name
= name
; }
65 void SetExtension(const wxString
& ext
) { m_extension
= ext
; }
66 void SetType(long type
) { m_type
= type
; }
67 void SetMimeType(const wxString
& type
) { m_mime
= type
; }
68 wxString
GetName() const { return m_name
; }
69 wxString
GetExtension() const { return m_extension
; }
70 long GetType() const { return m_type
; }
71 wxString
GetMimeType() const { return m_mime
; }
75 virtual bool DoCanRead( wxInputStream
& stream
) = 0;
77 // save the stream position, call DoCanRead() and restore the position
78 bool CallDoCanRead(wxInputStream
& stream
);
79 #endif // wxUSE_STREAMS
87 DECLARE_CLASS(wxImageHandler
)
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 class WXDLLEXPORT wxImageHistogramEntry
97 wxImageHistogramEntry() { index
= value
= 0; }
102 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry
,
103 wxIntegerHash
, wxIntegerEqual
,
104 wxImageHistogramBase
);
106 class WXDLLEXPORT wxImageHistogram
: public wxImageHistogramBase
109 wxImageHistogram() : wxImageHistogramBase(256) { }
111 // get the key in the histogram for the given RGB values
112 static unsigned long MakeKey(unsigned char r
,
116 return (r
<< 16) | (g
<< 8) | b
;
119 // find first colour that is not used in the image and has higher
120 // RGB values than RGB(startR, startG, startB)
122 // returns true and puts this colour in r, g, b (each of which may be NULL)
123 // on success or returns false if there are no more free colours
124 bool FindFirstUnusedColour(unsigned char *r
,
127 unsigned char startR
= 1,
128 unsigned char startG
= 0,
129 unsigned char startB
= 0 ) const;
132 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
136 class WXDLLEXPORT wxImage
: public wxObject
140 wxImage( int width
, int height
, bool clear
= true );
141 wxImage( int width
, int height
, unsigned char* data
, bool static_data
= false );
142 wxImage( const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
143 wxImage( const wxString
& name
, const wxString
& mimetype
, int index
= -1 );
146 wxImage( wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
147 wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
= -1 );
148 #endif // wxUSE_STREAMS
150 wxImage( const wxImage
& image
);
151 wxImage( const wxImage
* image
);
153 bool Create( int width
, int height
, bool clear
= true );
154 bool Create( int width
, int height
, unsigned char* data
, bool static_data
= false );
157 // creates an identical copy of the image (the = operator
158 // just raises the ref count)
159 wxImage
Copy() const;
161 // return the new image with size width*height
162 wxImage
GetSubImage( const wxRect
& ) const;
164 // pastes image into this instance and takes care of
165 // the mask colour and out of bounds problems
166 void Paste( const wxImage
&image
, int x
, int y
);
168 // return the new image with size width*height
169 wxImage
Scale( int width
, int height
) const;
171 wxImage
ShrinkBy( int xFactor
, int yFactor
) const ;
173 // rescales the image in place
174 wxImage
& Rescale( int width
, int height
) { return *this = Scale(width
, height
); }
176 // Rotates the image about the given point, 'angle' radians.
177 // Returns the rotated image, leaving this image intact.
178 wxImage
Rotate(double angle
, const wxPoint
& centre_of_rotation
,
179 bool interpolating
= true, wxPoint
* offset_after_rotation
= (wxPoint
*) NULL
) const;
181 wxImage
Rotate90( bool clockwise
= true ) const;
182 wxImage
Mirror( bool horizontally
= true ) const;
184 // replace one colour with another
185 void Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
186 unsigned char r2
, unsigned char g2
, unsigned char b2
);
188 // convert to monochrome image (<r,g,b> will be replaced by white,
189 // everything else by black)
190 wxImage
ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const;
192 // these routines are slow but safe
193 void SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
);
194 unsigned char GetRed( int x
, int y
) const;
195 unsigned char GetGreen( int x
, int y
) const;
196 unsigned char GetBlue( int x
, int y
) const;
198 void SetAlpha(int x
, int y
, unsigned char alpha
);
199 unsigned char GetAlpha(int x
, int y
) const;
201 // find first colour that is not used in the image and has higher
202 // RGB values than <startR,startG,startB>
203 bool FindFirstUnusedColour( unsigned char *r
, unsigned char *g
, unsigned char *b
,
204 unsigned char startR
= 1, unsigned char startG
= 0,
205 unsigned char startB
= 0 ) const;
206 // Set image's mask to the area of 'mask' that has <r,g,b> colour
207 bool SetMaskFromImage(const wxImage
& mask
,
208 unsigned char mr
, unsigned char mg
, unsigned char mb
);
210 // converts image's alpha channel to mask, if it has any, does nothing
212 bool ConvertAlphaToMask(unsigned char threshold
= 128);
214 // This method converts an image where the original alpha
215 // information is only available as a shades of a colour
216 // (actually shades of grey) typically when you draw anti-
217 // aliased text into a bitmap. The DC drawinf routines
218 // draw grey values on the black background although they
219 // actually mean to draw white with differnt alpha values.
220 // This method reverses it, assuming a black (!) background
221 // and white text (actually only the red channel is read).
222 // The method will then fill up the whole image with the
224 bool ConvertColourToAlpha( unsigned char r
, unsigned char g
, unsigned char b
);
226 static bool CanRead( const wxString
& name
);
227 static int GetImageCount( const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
);
228 virtual bool LoadFile( const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
229 virtual bool LoadFile( const wxString
& name
, const wxString
& mimetype
, int index
= -1 );
232 static bool CanRead( wxInputStream
& stream
);
233 static int GetImageCount( wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
);
234 virtual bool LoadFile( wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
235 virtual bool LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
= -1 );
238 virtual bool SaveFile( const wxString
& name
) const;
239 virtual bool SaveFile( const wxString
& name
, int type
) const;
240 virtual bool SaveFile( const wxString
& name
, const wxString
& mimetype
) const;
243 virtual bool SaveFile( wxOutputStream
& stream
, int type
) const;
244 virtual bool SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const;
248 int GetWidth() const;
249 int GetHeight() const;
251 // these functions provide fastest access to wxImage data but should be
252 // used carefully as no checks are done
253 unsigned char *GetData() const;
254 void SetData( unsigned char *data
);
255 void SetData( unsigned char *data
, int new_width
, int new_height
);
257 unsigned char *GetAlpha() const; // may return NULL!
258 bool HasAlpha() const { return GetAlpha() != NULL
; }
259 void SetAlpha(unsigned char *alpha
= NULL
);
262 void SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
);
263 unsigned char GetMaskRed() const;
264 unsigned char GetMaskGreen() const;
265 unsigned char GetMaskBlue() const;
266 void SetMask( bool mask
= true );
267 bool HasMask() const;
271 bool HasPalette() const;
272 const wxPalette
& GetPalette() const;
273 void SetPalette(const wxPalette
& palette
);
274 #endif // wxUSE_PALETTE
276 // Option functions (arbitrary name/value mapping)
277 void SetOption(const wxString
& name
, const wxString
& value
);
278 void SetOption(const wxString
& name
, int value
);
279 wxString
GetOption(const wxString
& name
) const;
280 int GetOptionInt(const wxString
& name
) const;
281 bool HasOption(const wxString
& name
) const;
283 unsigned long CountColours( unsigned long stopafter
= (unsigned long) -1 ) const;
285 // Computes the histogram of the image and fills a hash table, indexed
286 // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry
287 // objects. Each of them contains an 'index' (useful to build a palette
288 // with the image colours) and a 'value', which is the number of pixels
289 // in the image with that colour.
290 // Returned value: # of entries in the histogram
291 unsigned long ComputeHistogram( wxImageHistogram
&h
) const;
293 wxImage
& operator = (const wxImage
& image
)
295 if ( (*this) != image
)
300 bool operator == (const wxImage
& image
) const
301 { return m_refData
== image
.m_refData
; }
302 bool operator != (const wxImage
& image
) const
303 { return m_refData
!= image
.m_refData
; }
305 static wxList
& GetHandlers() { return sm_handlers
; }
306 static void AddHandler( wxImageHandler
*handler
);
307 static void InsertHandler( wxImageHandler
*handler
);
308 static bool RemoveHandler( const wxString
& name
);
309 static wxImageHandler
*FindHandler( const wxString
& name
);
310 static wxImageHandler
*FindHandler( const wxString
& extension
, long imageType
);
311 static wxImageHandler
*FindHandler( long imageType
);
312 static wxImageHandler
*FindHandlerMime( const wxString
& mimetype
);
314 static wxString
GetImageExtWildcard();
316 static void CleanUpHandlers();
317 static void InitStandardHandlers();
320 static wxList sm_handlers
;
323 friend class WXDLLEXPORT wxImageHandler
;
325 DECLARE_DYNAMIC_CLASS(wxImage
)
329 extern void WXDLLEXPORT
wxInitAllImageHandlers();
331 WXDLLEXPORT_DATA(extern wxImage
) wxNullImage
;
333 //-----------------------------------------------------------------------------
335 //-----------------------------------------------------------------------------
337 #include "wx/imagbmp.h"
338 #include "wx/imagpng.h"
339 #include "wx/imaggif.h"
340 #include "wx/imagpcx.h"
341 #include "wx/imagjpeg.h"
342 #include "wx/imagtiff.h"
343 #include "wx/imagpnm.h"
344 #include "wx/imagxpm.h"
345 #include "wx/imagiff.h"
347 #endif // wxUSE_IMAGE