added alpha channel support to wxImage
[wxWidgets.git] / include / wx / image.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: image.h
3 // Purpose: wxImage class
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_IMAGE_H_
11 #define _WX_IMAGE_H_
12
13 #if defined(__GNUG__) && !defined(__APPLE__)
14 #pragma interface "image.h"
15 #endif
16
17 #include "wx/setup.h"
18 #include "wx/object.h"
19 #include "wx/string.h"
20 #include "wx/gdicmn.h"
21 #if WXWIN_COMPATIBILITY_2_2
22 # include "wx/bitmap.h"
23 #endif
24 #include "wx/hashmap.h"
25
26 #if wxUSE_STREAMS
27 # include "wx/stream.h"
28 #endif
29
30 #if wxUSE_IMAGE
31
32 #define wxIMAGE_OPTION_FILENAME wxString(_T("FileName"))
33
34 //-----------------------------------------------------------------------------
35 // classes
36 //-----------------------------------------------------------------------------
37
38 class WXDLLEXPORT wxImageHandler;
39 class WXDLLEXPORT wxImage;
40 class WXDLLEXPORT wxPalette;
41
42 //-----------------------------------------------------------------------------
43 // wxImageHandler
44 //-----------------------------------------------------------------------------
45
46 class WXDLLEXPORT wxImageHandler: public wxObject
47 {
48 public:
49 wxImageHandler()
50 : m_name(wxT("")), m_extension(wxT("")), m_mime(), m_type(0)
51 { }
52
53 #if wxUSE_STREAMS
54 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=-1 );
55 virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
56
57 virtual int GetImageCount( wxInputStream& stream );
58
59 bool CanRead( wxInputStream& stream ) { return CallDoCanRead(stream); }
60 bool CanRead( const wxString& name );
61 #endif // wxUSE_STREAMS
62
63 void SetName(const wxString& name) { m_name = name; }
64 void SetExtension(const wxString& ext) { m_extension = ext; }
65 void SetType(long type) { m_type = type; }
66 void SetMimeType(const wxString& type) { m_mime = type; }
67 wxString GetName() const { return m_name; }
68 wxString GetExtension() const { return m_extension; }
69 long GetType() const { return m_type; }
70 wxString GetMimeType() const { return m_mime; }
71
72 protected:
73 #if wxUSE_STREAMS
74 virtual bool DoCanRead( wxInputStream& stream ) = 0;
75
76 // save the stream position, call DoCanRead() and restore the position
77 bool CallDoCanRead(wxInputStream& stream);
78 #endif // wxUSE_STREAMS
79
80 wxString m_name;
81 wxString m_extension;
82 wxString m_mime;
83 long m_type;
84
85 private:
86 DECLARE_CLASS(wxImageHandler)
87 };
88
89 //-----------------------------------------------------------------------------
90 // wxImageHistogram
91 //-----------------------------------------------------------------------------
92
93 class WXDLLEXPORT wxImageHistogramEntry
94 {
95 public:
96 wxImageHistogramEntry() : index(0), value(0) {}
97 unsigned long index;
98 unsigned long value;
99 };
100
101 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
102 wxIntegerHash, wxIntegerEqual,
103 wxImageHistogramBase);
104
105 class wxImageHistogram : public wxImageHistogramBase
106 {
107 public:
108 wxImageHistogram() : wxImageHistogramBase(256) { }
109
110 // get the key in the histogram for the given RGB values
111 static unsigned long MakeKey(unsigned char r,
112 unsigned char g,
113 unsigned char b)
114 {
115 return (r << 16) | (g << 8) | b;
116 }
117
118 // find first colour that is not used in the image and has higher
119 // RGB values than RGB(startR, startG, startB)
120 //
121 // returns true and puts this colour in r, g, b (each of which may be NULL)
122 // on success or returns false if there are no more free colours
123 bool FindFirstUnusedColour(unsigned char *r,
124 unsigned char *g,
125 unsigned char *b,
126 unsigned char startR = 1,
127 unsigned char startG = 0,
128 unsigned char startB = 0 ) const;
129 };
130
131 //-----------------------------------------------------------------------------
132 // wxImage
133 //-----------------------------------------------------------------------------
134
135 class WXDLLEXPORT wxImage: public wxObject
136 {
137 public:
138 wxImage();
139 wxImage( int width, int height );
140 wxImage( int width, int height, unsigned char* data, bool static_data = FALSE );
141 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
142 wxImage( const wxString& name, const wxString& mimetype, int index = -1 );
143
144 #if wxUSE_STREAMS
145 wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
146 wxImage( wxInputStream& stream, const wxString& mimetype, int index = -1 );
147 #endif // wxUSE_STREAMS
148
149 wxImage( const wxImage& image );
150 wxImage( const wxImage* image );
151
152 #if WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI
153 // conversion to/from wxBitmap (deprecated, use wxBitmap's methods instead):
154 wxDEPRECATED( wxImage(const wxBitmap &bitmap) );
155 wxDEPRECATED( wxBitmap ConvertToBitmap() const );
156 #ifdef __WXGTK__
157 wxBitmap ConvertToMonoBitmap( unsigned char red, unsigned char green, unsigned char blue ) const;
158 #endif
159 #endif
160
161 void Create( int width, int height );
162 void Create( int width, int height, unsigned char* data, bool static_data = FALSE );
163 void Destroy();
164
165 // creates an identical copy of the image (the = operator
166 // just raises the ref count)
167 wxImage Copy() const;
168
169 // return the new image with size width*height
170 wxImage GetSubImage( const wxRect& ) const;
171
172 // pastes image into this instance and takes care of
173 // the mask colour and out of bounds problems
174 void Paste( const wxImage &image, int x, int y );
175
176 // return the new image with size width*height
177 wxImage Scale( int width, int height ) const;
178
179 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
180
181 // rescales the image in place
182 wxImage& Rescale( int width, int height ) { return *this = Scale(width, height); }
183
184 // Rotates the image about the given point, 'angle' radians.
185 // Returns the rotated image, leaving this image intact.
186 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
187 bool interpolating = TRUE, wxPoint * offset_after_rotation = (wxPoint*) NULL) const;
188
189 wxImage Rotate90( bool clockwise = TRUE ) const;
190 wxImage Mirror( bool horizontally = TRUE ) const;
191
192 // replace one colour with another
193 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
194 unsigned char r2, unsigned char g2, unsigned char b2 );
195
196 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
197 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
198
199 // these routines are slow but safe
200 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
201 unsigned char GetRed( int x, int y ) const;
202 unsigned char GetGreen( int x, int y ) const;
203 unsigned char GetBlue( int x, int y ) const;
204
205 void SetAlpha(int x, int y, unsigned char alpha);
206 unsigned char GetAlpha(int x, int y);
207
208 // find first colour that is not used in the image and has higher
209 // RGB values than <startR,startG,startB>
210 bool FindFirstUnusedColour( unsigned char *r, unsigned char *g, unsigned char *b,
211 unsigned char startR = 1, unsigned char startG = 0,
212 unsigned char startB = 0 ) const;
213 // Set image's mask to the area of 'mask' that has <r,g,b> colour
214 bool SetMaskFromImage(const wxImage & mask,
215 unsigned char mr, unsigned char mg, unsigned char mb);
216
217 static bool CanRead( const wxString& name );
218 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
219 virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
220 virtual bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
221
222 #if wxUSE_STREAMS
223 static bool CanRead( wxInputStream& stream );
224 static int GetImageCount( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
225 virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
226 virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
227 #endif
228
229 virtual bool SaveFile( const wxString& name ) const;
230 virtual bool SaveFile( const wxString& name, int type ) const;
231 virtual bool SaveFile( const wxString& name, const wxString& mimetype ) const;
232
233 #if wxUSE_STREAMS
234 virtual bool SaveFile( wxOutputStream& stream, int type ) const;
235 virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype ) const;
236 #endif
237
238 bool Ok() const;
239 int GetWidth() const;
240 int GetHeight() const;
241
242 // these functions provide fastest access to wxImage data but should be
243 // used carefully as no checks are done
244 unsigned char *GetData() const;
245 void SetData( unsigned char *data );
246 void SetData( unsigned char *data, int new_width, int new_height );
247
248 unsigned char *GetAlpha() const; // may return NULL!
249 bool HasAlpha() const { return GetAlpha() != NULL; }
250 void SetAlpha(unsigned char *alpha = NULL);
251
252 // Mask functions
253 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
254 unsigned char GetMaskRed() const;
255 unsigned char GetMaskGreen() const;
256 unsigned char GetMaskBlue() const;
257 void SetMask( bool mask = TRUE );
258 bool HasMask() const;
259
260 #if wxUSE_PALETTE
261 // Palette functions
262 bool HasPalette() const;
263 const wxPalette& GetPalette() const;
264 void SetPalette(const wxPalette& palette);
265 #endif // wxUSE_PALETTE
266
267 // Option functions (arbitrary name/value mapping)
268 void SetOption(const wxString& name, const wxString& value);
269 void SetOption(const wxString& name, int value);
270 wxString GetOption(const wxString& name) const;
271 int GetOptionInt(const wxString& name) const;
272 bool HasOption(const wxString& name) const;
273
274 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ) const;
275
276 // Computes the histogram of the image and fills a hash table, indexed
277 // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry
278 // objects. Each of them contains an 'index' (useful to build a palette
279 // with the image colours) and a 'value', which is the number of pixels
280 // in the image with that colour.
281 // Returned value: # of entries in the histogram
282 unsigned long ComputeHistogram( wxImageHistogram &h ) const;
283
284 wxImage& operator = (const wxImage& image)
285 {
286 if ( (*this) != image )
287 Ref(image);
288 return *this;
289 }
290
291 bool operator == (const wxImage& image) const
292 { return m_refData == image.m_refData; }
293 bool operator != (const wxImage& image) const
294 { return m_refData != image.m_refData; }
295
296 static wxList& GetHandlers() { return sm_handlers; }
297 static void AddHandler( wxImageHandler *handler );
298 static void InsertHandler( wxImageHandler *handler );
299 static bool RemoveHandler( const wxString& name );
300 static wxImageHandler *FindHandler( const wxString& name );
301 static wxImageHandler *FindHandler( const wxString& extension, long imageType );
302 static wxImageHandler *FindHandler( long imageType );
303 static wxImageHandler *FindHandlerMime( const wxString& mimetype );
304
305 static void CleanUpHandlers();
306 static void InitStandardHandlers();
307
308 protected:
309 static wxList sm_handlers;
310
311 private:
312 friend class WXDLLEXPORT wxImageHandler;
313
314 DECLARE_DYNAMIC_CLASS(wxImage)
315 };
316
317
318 extern void WXDLLEXPORT wxInitAllImageHandlers();
319
320 WXDLLEXPORT_DATA(extern wxImage) wxNullImage;
321
322 //-----------------------------------------------------------------------------
323 // wxImage handlers
324 //-----------------------------------------------------------------------------
325
326 #include "wx/imagbmp.h"
327 #include "wx/imagpng.h"
328 #include "wx/imaggif.h"
329 #include "wx/imagpcx.h"
330 #include "wx/imagjpeg.h"
331 #include "wx/imagtiff.h"
332 #include "wx/imagpnm.h"
333 #include "wx/imagxpm.h"
334 #include "wx/imagiff.h"
335
336 #endif // wxUSE_IMAGE
337
338 #endif
339 // _WX_IMAGE_H_
340