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