]> git.saurik.com Git - wxWidgets.git/blob - include/wx/image.h
Performance optimizations
[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 #ifdef __GNUG__
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/bitmap.h"
22
23 #if wxUSE_STREAMS
24 # include "wx/stream.h"
25 #endif
26
27 //-----------------------------------------------------------------------------
28 // classes
29 //-----------------------------------------------------------------------------
30
31 class WXDLLEXPORT wxImageHandler;
32 #if wxUSE_LIBPNG
33 class WXDLLEXPORT wxPNGHandler;
34 #endif
35 #if wxUSE_LIBJPEG
36 class WXDLLEXPORT wxJPEGHandler;
37 #endif
38 #if wxUSE_LIBTIFF
39 class WXDLLEXPORT wxTIFFHandler;
40 #endif
41 class WXDLLEXPORT wxBMPHandler;
42 #if wxUSE_GIF
43 class WXDLLEXPORT wxGIFHandler;
44 #endif
45 #if wxUSE_PNM
46 class WXDLLEXPORT wxPNMHandler;
47 #endif
48 #if wxUSE_PCX
49 class WXDLLEXPORT wxPCXHandler;
50 #endif
51 class WXDLLEXPORT wxImage;
52
53 //-----------------------------------------------------------------------------
54 // wxImageHandler
55 //-----------------------------------------------------------------------------
56
57 class WXDLLEXPORT wxImageHandler: public wxObject
58 {
59 DECLARE_CLASS(wxImageHandler)
60
61 public:
62 wxImageHandler() { m_name = ""; m_extension = ""; m_type = 0; }
63
64 #if wxUSE_STREAMS
65 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
66 virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
67
68 virtual int GetImageCount( wxInputStream& stream );
69
70 bool CanRead( wxInputStream& stream ) { return DoCanRead(stream); }
71 bool CanRead( const wxString& name );
72 #endif
73
74 void SetName(const wxString& name) { m_name = name; }
75 void SetExtension(const wxString& ext) { m_extension = ext; }
76 void SetType(long type) { m_type = type; }
77 void SetMimeType(const wxString& type) { m_mime = type; }
78 wxString GetName() const { return m_name; }
79 wxString GetExtension() const { return m_extension; }
80 long GetType() const { return m_type; }
81 wxString GetMimeType() const { return m_mime; }
82
83 protected:
84 virtual bool DoCanRead( wxInputStream& stream ) = 0;
85
86 wxString m_name;
87 wxString m_extension;
88 wxString m_mime;
89 long m_type;
90
91 };
92
93 //-----------------------------------------------------------------------------
94 // wxPNGHandler
95 //-----------------------------------------------------------------------------
96
97 #if wxUSE_LIBPNG
98 class WXDLLEXPORT wxPNGHandler: public wxImageHandler
99 {
100 DECLARE_DYNAMIC_CLASS(wxPNGHandler)
101
102 public:
103
104 inline wxPNGHandler()
105 {
106 m_name = "PNG file";
107 m_extension = "png";
108 m_type = wxBITMAP_TYPE_PNG;
109 m_mime = "image/png";
110 };
111
112 #if wxUSE_STREAMS
113 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
114 virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
115 virtual bool DoCanRead( wxInputStream& stream );
116 #endif
117 };
118 #endif
119
120 //-----------------------------------------------------------------------------
121 // wxJPEGHandler
122 //-----------------------------------------------------------------------------
123
124 #if wxUSE_LIBJPEG
125 class WXDLLEXPORT wxJPEGHandler: public wxImageHandler
126 {
127 DECLARE_DYNAMIC_CLASS(wxJPEGHandler)
128
129 public:
130
131 inline wxJPEGHandler()
132 {
133 m_name = "JPEG file";
134 m_extension = "jpg";
135 m_type = wxBITMAP_TYPE_JPEG;
136 m_mime = "image/jpeg";
137 };
138
139 #if wxUSE_STREAMS
140 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
141 virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
142 virtual bool DoCanRead( wxInputStream& stream );
143 #endif
144 };
145 #endif
146
147 //-----------------------------------------------------------------------------
148 // wxTIFFHandler
149 //-----------------------------------------------------------------------------
150
151 #if wxUSE_LIBTIFF
152 class WXDLLEXPORT wxTIFFHandler: public wxImageHandler
153 {
154 DECLARE_DYNAMIC_CLASS(wxTIFFHandler)
155
156 public:
157
158 inline wxTIFFHandler()
159 {
160 m_name = "TIFF file";
161 m_extension = "tif";
162 m_type = wxBITMAP_TYPE_TIF;
163 m_mime = "image/tiff";
164 };
165
166 #if wxUSE_STREAMS
167 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
168 virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
169 virtual bool DoCanRead( wxInputStream& stream );
170 virtual int GetImageCount( wxInputStream& stream );
171 #endif
172 };
173 #endif
174
175 //-----------------------------------------------------------------------------
176 // wxBMPHandler
177 //-----------------------------------------------------------------------------
178
179 class WXDLLEXPORT wxBMPHandler: public wxImageHandler
180 {
181 DECLARE_DYNAMIC_CLASS(wxBMPHandler)
182
183 public:
184
185 inline wxBMPHandler()
186 {
187 m_name = "BMP file";
188 m_extension = "bmp";
189 m_type = wxBITMAP_TYPE_BMP;
190 m_mime = "image/bmp";
191 };
192
193 #if wxUSE_STREAMS
194 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
195 virtual bool DoCanRead( wxInputStream& stream );
196 #endif
197 };
198
199 //-----------------------------------------------------------------------------
200 // wxGIFHandler
201 //-----------------------------------------------------------------------------
202
203 #if wxUSE_GIF
204
205 class WXDLLEXPORT wxGIFHandler : public wxImageHandler
206 {
207 DECLARE_DYNAMIC_CLASS(wxGIFHandler)
208
209 public:
210
211 inline wxGIFHandler()
212 {
213 m_name = "GIF file";
214 m_extension = "gif";
215 m_type = wxBITMAP_TYPE_GIF;
216 m_mime = "image/gif";
217 };
218
219 #if wxUSE_STREAMS
220 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
221 virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
222 virtual bool DoCanRead( wxInputStream& stream );
223 #endif
224 };
225 #endif
226
227 //-----------------------------------------------------------------------------
228 // wxPNMHandler
229 //-----------------------------------------------------------------------------
230
231 #if wxUSE_PNM
232 class WXDLLEXPORT wxPNMHandler : public wxImageHandler
233 {
234 DECLARE_DYNAMIC_CLASS(wxPNMHandler)
235
236 public:
237
238 inline wxPNMHandler()
239 {
240 m_name = "PNM file";
241 m_extension = "pnm";
242 m_type = wxBITMAP_TYPE_PNM;
243 m_mime = "image/pnm";
244 };
245
246 #if wxUSE_STREAMS
247 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
248 virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
249 virtual bool DoCanRead( wxInputStream& stream );
250 #endif
251 };
252 #endif
253
254 //-----------------------------------------------------------------------------
255 // wxPCXHandler
256 //-----------------------------------------------------------------------------
257
258 #if wxUSE_PCX
259 class WXDLLEXPORT wxPCXHandler : public wxImageHandler
260 {
261 DECLARE_DYNAMIC_CLASS(wxPCXHandler)
262
263 public:
264
265 inline wxPCXHandler()
266 {
267 m_name = "PCX file";
268 m_extension = "pcx";
269 m_type = wxBITMAP_TYPE_PCX;
270 m_mime = "image/pcx";
271 };
272
273 #if wxUSE_STREAMS
274 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
275 virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
276 virtual bool DoCanRead( wxInputStream& stream );
277 #endif // wxUSE_STREAMS
278 };
279 #endif // wxUSE_PCX
280
281 //-----------------------------------------------------------------------------
282 // wxImage
283 //-----------------------------------------------------------------------------
284
285 class WXDLLEXPORT wxImage: public wxObject
286 {
287 DECLARE_DYNAMIC_CLASS(wxImage)
288
289 friend class WXDLLEXPORT wxImageHandler;
290
291 public:
292
293 wxImage();
294 wxImage( int width, int height );
295 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY );
296 wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
297 wxImage( const wxString& name, const wxString& mimetype );
298 wxImage( wxInputStream& stream, const wxString& mimetype );
299
300 wxImage( const wxImage& image );
301 wxImage( const wxImage* image );
302
303 // these functions get implemented in /src/(platform)/bitmap.cpp
304 wxImage( const wxBitmap &bitmap );
305 operator wxBitmap() const { return ConvertToBitmap(); }
306 wxBitmap ConvertToBitmap() const;
307
308 void Create( int width, int height );
309 void Destroy();
310
311 // return the new image with size width*height
312 wxImage GetSubImage( const wxRect& ) const;
313
314 // return the new image with size width*height
315 wxImage Scale( int width, int height ) const;
316
317 // rescales the image in place
318 void Rescale( int width, int height ) { *this = Scale(width, height); }
319
320 // these routines are slow but safe
321 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
322 unsigned char GetRed( int x, int y );
323 unsigned char GetGreen( int x, int y );
324 unsigned char GetBlue( int x, int y );
325
326 static bool CanRead( const wxString& name );
327 virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY );
328 virtual bool LoadFile( const wxString& name, const wxString& mimetype );
329
330 #if wxUSE_STREAMS
331 static bool CanRead( wxInputStream& stream );
332 virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
333 virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype );
334 #endif
335
336 virtual bool SaveFile( const wxString& name, int type );
337 virtual bool SaveFile( const wxString& name, const wxString& mimetype );
338
339 #if wxUSE_STREAMS
340 virtual bool SaveFile( wxOutputStream& stream, int type );
341 virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype );
342 #endif
343
344 bool Ok() const;
345 int GetWidth() const;
346 int GetHeight() const;
347
348 char unsigned *GetData() const;
349 void SetData( char unsigned *data );
350
351 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
352 unsigned char GetMaskRed() const;
353 unsigned char GetMaskGreen() const;
354 unsigned char GetMaskBlue() const;
355 void SetMask( bool mask = TRUE );
356 bool HasMask() const;
357
358 wxImage& operator = (const wxImage& image)
359 {
360 if ( (*this) != image )
361 Ref(image);
362 return *this;
363 }
364
365 bool operator == (const wxImage& image)
366 { return m_refData == image.m_refData; }
367 bool operator != (const wxImage& image)
368 { return m_refData != image.m_refData; }
369
370 static wxList& GetHandlers() { return sm_handlers; }
371 static void AddHandler( wxImageHandler *handler );
372 static void InsertHandler( wxImageHandler *handler );
373 static bool RemoveHandler( const wxString& name );
374 static wxImageHandler *FindHandler( const wxString& name );
375 static wxImageHandler *FindHandler( const wxString& extension, long imageType );
376 static wxImageHandler *FindHandler( long imageType );
377 static wxImageHandler *FindHandlerMime( const wxString& mimetype );
378
379 static void CleanUpHandlers();
380 static void InitStandardHandlers();
381
382 protected:
383
384 static wxList sm_handlers;
385
386 };
387
388 extern void WXDLLEXPORT wxInitAllImageHandlers();
389
390 #endif
391 // _WX_IMAGE_H_
392