added wxImage::ConvertAlphaToMask
[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(NO_GCC_PRAGMA)
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 #include "wx/hashmap.h"
22
23 #if wxUSE_STREAMS
24 # include "wx/stream.h"
25 #endif
26
27 #if wxUSE_IMAGE
28
29 // on some systems (Unixware 7.x) index is defined as a macro in the headers
30 // which breaks the compilation below
31 #undef index
32
33 #define wxIMAGE_OPTION_FILENAME wxString(_T("FileName"))
34
35 //-----------------------------------------------------------------------------
36 // classes
37 //-----------------------------------------------------------------------------
38
39 class WXDLLEXPORT wxImageHandler;
40 class WXDLLEXPORT wxImage;
41 class WXDLLEXPORT wxPalette;
42
43 //-----------------------------------------------------------------------------
44 // wxImageHandler
45 //-----------------------------------------------------------------------------
46
47 class WXDLLEXPORT wxImageHandler: public wxObject
48 {
49 public:
50 wxImageHandler()
51 : m_name(wxEmptyString), m_extension(wxEmptyString), m_mime(), m_type(0)
52 { }
53
54 #if wxUSE_STREAMS
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 );
57
58 virtual int GetImageCount( wxInputStream& stream );
59
60 bool CanRead( wxInputStream& stream ) { return CallDoCanRead(stream); }
61 bool CanRead( const wxString& name );
62 #endif // wxUSE_STREAMS
63
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; }
72
73 protected:
74 #if wxUSE_STREAMS
75 virtual bool DoCanRead( wxInputStream& stream ) = 0;
76
77 // save the stream position, call DoCanRead() and restore the position
78 bool CallDoCanRead(wxInputStream& stream);
79 #endif // wxUSE_STREAMS
80
81 wxString m_name;
82 wxString m_extension;
83 wxString m_mime;
84 long m_type;
85
86 private:
87 DECLARE_CLASS(wxImageHandler)
88 };
89
90 //-----------------------------------------------------------------------------
91 // wxImageHistogram
92 //-----------------------------------------------------------------------------
93
94 class WXDLLEXPORT wxImageHistogramEntry
95 {
96 public:
97 wxImageHistogramEntry() { index = value = 0; }
98 unsigned long index;
99 unsigned long value;
100 };
101
102 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
103 wxIntegerHash, wxIntegerEqual,
104 wxImageHistogramBase);
105
106 class WXDLLEXPORT wxImageHistogram : public wxImageHistogramBase
107 {
108 public:
109 wxImageHistogram() : wxImageHistogramBase(256) { }
110
111 // get the key in the histogram for the given RGB values
112 static unsigned long MakeKey(unsigned char r,
113 unsigned char g,
114 unsigned char b)
115 {
116 return (r << 16) | (g << 8) | b;
117 }
118
119 // find first colour that is not used in the image and has higher
120 // RGB values than RGB(startR, startG, startB)
121 //
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,
125 unsigned char *g,
126 unsigned char *b,
127 unsigned char startR = 1,
128 unsigned char startG = 0,
129 unsigned char startB = 0 ) const;
130 };
131
132 //-----------------------------------------------------------------------------
133 // wxImage
134 //-----------------------------------------------------------------------------
135
136 class WXDLLEXPORT wxImage: public wxObject
137 {
138 public:
139 wxImage();
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 );
144
145 #if wxUSE_STREAMS
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
149
150 wxImage( const wxImage& image );
151 wxImage( const wxImage* image );
152
153 bool Create( int width, int height, bool clear = true );
154 bool Create( int width, int height, unsigned char* data, bool static_data = FALSE );
155 void Destroy();
156
157 // creates an identical copy of the image (the = operator
158 // just raises the ref count)
159 wxImage Copy() const;
160
161 // return the new image with size width*height
162 wxImage GetSubImage( const wxRect& ) const;
163
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 );
167
168 // return the new image with size width*height
169 wxImage Scale( int width, int height ) const;
170
171 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
172
173 // rescales the image in place
174 wxImage& Rescale( int width, int height ) { return *this = Scale(width, height); }
175
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;
180
181 wxImage Rotate90( bool clockwise = TRUE ) const;
182 wxImage Mirror( bool horizontally = TRUE ) const;
183
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 );
187
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;
191
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;
197
198 void SetAlpha(int x, int y, unsigned char alpha);
199 unsigned char GetAlpha(int x, int y) const;
200
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);
209
210 // converts image's alpha channel to mask, if it has any, does nothing
211 // otherwise:
212 bool ConvertAlphaToMask(unsigned threshold = 128);
213
214 static bool CanRead( const wxString& name );
215 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
216 virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
217 virtual bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
218
219 #if wxUSE_STREAMS
220 static bool CanRead( wxInputStream& stream );
221 static int GetImageCount( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
222 virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
223 virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
224 #endif
225
226 virtual bool SaveFile( const wxString& name ) const;
227 virtual bool SaveFile( const wxString& name, int type ) const;
228 virtual bool SaveFile( const wxString& name, const wxString& mimetype ) const;
229
230 #if wxUSE_STREAMS
231 virtual bool SaveFile( wxOutputStream& stream, int type ) const;
232 virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype ) const;
233 #endif
234
235 bool Ok() const;
236 int GetWidth() const;
237 int GetHeight() const;
238
239 // these functions provide fastest access to wxImage data but should be
240 // used carefully as no checks are done
241 unsigned char *GetData() const;
242 void SetData( unsigned char *data );
243 void SetData( unsigned char *data, int new_width, int new_height );
244
245 unsigned char *GetAlpha() const; // may return NULL!
246 bool HasAlpha() const { return GetAlpha() != NULL; }
247 void SetAlpha(unsigned char *alpha = NULL);
248
249 // Mask functions
250 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
251 unsigned char GetMaskRed() const;
252 unsigned char GetMaskGreen() const;
253 unsigned char GetMaskBlue() const;
254 void SetMask( bool mask = TRUE );
255 bool HasMask() const;
256
257 #if wxUSE_PALETTE
258 // Palette functions
259 bool HasPalette() const;
260 const wxPalette& GetPalette() const;
261 void SetPalette(const wxPalette& palette);
262 #endif // wxUSE_PALETTE
263
264 // Option functions (arbitrary name/value mapping)
265 void SetOption(const wxString& name, const wxString& value);
266 void SetOption(const wxString& name, int value);
267 wxString GetOption(const wxString& name) const;
268 int GetOptionInt(const wxString& name) const;
269 bool HasOption(const wxString& name) const;
270
271 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ) const;
272
273 // Computes the histogram of the image and fills a hash table, indexed
274 // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry
275 // objects. Each of them contains an 'index' (useful to build a palette
276 // with the image colours) and a 'value', which is the number of pixels
277 // in the image with that colour.
278 // Returned value: # of entries in the histogram
279 unsigned long ComputeHistogram( wxImageHistogram &h ) const;
280
281 wxImage& operator = (const wxImage& image)
282 {
283 if ( (*this) != image )
284 Ref(image);
285 return *this;
286 }
287
288 bool operator == (const wxImage& image) const
289 { return m_refData == image.m_refData; }
290 bool operator != (const wxImage& image) const
291 { return m_refData != image.m_refData; }
292
293 static wxList& GetHandlers() { return sm_handlers; }
294 static void AddHandler( wxImageHandler *handler );
295 static void InsertHandler( wxImageHandler *handler );
296 static bool RemoveHandler( const wxString& name );
297 static wxImageHandler *FindHandler( const wxString& name );
298 static wxImageHandler *FindHandler( const wxString& extension, long imageType );
299 static wxImageHandler *FindHandler( long imageType );
300 static wxImageHandler *FindHandlerMime( const wxString& mimetype );
301
302 static wxString GetImageExtWildcard();
303
304 static void CleanUpHandlers();
305 static void InitStandardHandlers();
306
307 protected:
308 static wxList sm_handlers;
309
310 private:
311 friend class WXDLLEXPORT wxImageHandler;
312
313 DECLARE_DYNAMIC_CLASS(wxImage)
314 };
315
316
317 extern void WXDLLEXPORT wxInitAllImageHandlers();
318
319 WXDLLEXPORT_DATA(extern wxImage) wxNullImage;
320
321 //-----------------------------------------------------------------------------
322 // wxImage handlers
323 //-----------------------------------------------------------------------------
324
325 #include "wx/imagbmp.h"
326 #include "wx/imagpng.h"
327 #include "wx/imaggif.h"
328 #include "wx/imagpcx.h"
329 #include "wx/imagjpeg.h"
330 #include "wx/imagtiff.h"
331 #include "wx/imagpnm.h"
332 #include "wx/imagxpm.h"
333 #include "wx/imagiff.h"
334
335 #endif // wxUSE_IMAGE
336
337 #endif
338 // _WX_IMAGE_H_
339