1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage class
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
14 #pragma interface "image.h"
18 #include "wx/object.h"
19 #include "wx/string.h"
20 #include "wx/gdicmn.h"
21 #include "wx/bitmap.h"
24 # include "wx/stream.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 class WXDLLEXPORT wxImageHandler
;
33 class WXDLLEXPORT wxPNGHandler
;
36 class WXDLLEXPORT wxJPEGHandler
;
39 class WXDLLEXPORT wxTIFFHandler
;
41 class WXDLLEXPORT wxBMPHandler
;
43 class WXDLLEXPORT wxGIFHandler
;
46 class WXDLLEXPORT wxPNMHandler
;
49 class WXDLLEXPORT wxPCXHandler
;
51 class WXDLLEXPORT wxImage
;
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 class WXDLLEXPORT wxImageHandler
: public wxObject
59 DECLARE_CLASS(wxImageHandler
)
62 wxImageHandler() { m_name
= ""; m_extension
= ""; m_type
= 0; }
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
);
68 virtual int GetImageCount( wxInputStream
& stream
);
70 bool CanRead( wxInputStream
& stream
) { return DoCanRead(stream
); }
71 bool CanRead( const wxString
& name
);
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
; }
84 virtual bool DoCanRead( wxInputStream
& stream
) = 0;
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
98 class WXDLLEXPORT wxPNGHandler
: public wxImageHandler
100 DECLARE_DYNAMIC_CLASS(wxPNGHandler
)
104 inline wxPNGHandler()
108 m_type
= wxBITMAP_TYPE_PNG
;
109 m_mime
= "image/png";
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
);
120 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
125 class WXDLLEXPORT wxJPEGHandler
: public wxImageHandler
127 DECLARE_DYNAMIC_CLASS(wxJPEGHandler
)
131 inline wxJPEGHandler()
133 m_name
= "JPEG file";
135 m_type
= wxBITMAP_TYPE_JPEG
;
136 m_mime
= "image/jpeg";
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
);
147 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
152 class WXDLLEXPORT wxTIFFHandler
: public wxImageHandler
154 DECLARE_DYNAMIC_CLASS(wxTIFFHandler
)
158 inline wxTIFFHandler()
160 m_name
= "TIFF file";
162 m_type
= wxBITMAP_TYPE_TIF
;
163 m_mime
= "image/tiff";
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
);
175 //-----------------------------------------------------------------------------
177 //-----------------------------------------------------------------------------
179 class WXDLLEXPORT wxBMPHandler
: public wxImageHandler
181 DECLARE_DYNAMIC_CLASS(wxBMPHandler
)
185 inline wxBMPHandler()
189 m_type
= wxBITMAP_TYPE_BMP
;
190 m_mime
= "image/bmp";
194 virtual bool LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
=TRUE
, int index
=0 );
195 virtual bool DoCanRead( wxInputStream
& stream
);
199 //-----------------------------------------------------------------------------
201 //-----------------------------------------------------------------------------
205 class WXDLLEXPORT wxGIFHandler
: public wxImageHandler
207 DECLARE_DYNAMIC_CLASS(wxGIFHandler
)
211 inline wxGIFHandler()
215 m_type
= wxBITMAP_TYPE_GIF
;
216 m_mime
= "image/gif";
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
);
227 //-----------------------------------------------------------------------------
229 //-----------------------------------------------------------------------------
232 class WXDLLEXPORT wxPNMHandler
: public wxImageHandler
234 DECLARE_DYNAMIC_CLASS(wxPNMHandler
)
238 inline wxPNMHandler()
242 m_type
= wxBITMAP_TYPE_PNM
;
243 m_mime
= "image/pnm";
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
);
254 //-----------------------------------------------------------------------------
256 //-----------------------------------------------------------------------------
259 class WXDLLEXPORT wxPCXHandler
: public wxImageHandler
261 DECLARE_DYNAMIC_CLASS(wxPCXHandler
)
265 inline wxPCXHandler()
269 m_type
= wxBITMAP_TYPE_PCX
;
270 m_mime
= "image/pcx";
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
281 //-----------------------------------------------------------------------------
283 //-----------------------------------------------------------------------------
285 class WXDLLEXPORT wxImage
: public wxObject
287 DECLARE_DYNAMIC_CLASS(wxImage
)
289 friend class WXDLLEXPORT wxImageHandler
;
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
);
300 wxImage( const wxImage
& image
);
301 wxImage( const wxImage
* image
);
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;
308 void Create( int width
, int height
);
311 // return the new image with size width*height
312 wxImage
GetSubImage( const wxRect
& ) const;
314 // return the new image with size width*height
315 wxImage
Scale( int width
, int height
) const;
317 // rescales the image in place
318 void Rescale( int width
, int height
) { *this = Scale(width
, height
); }
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
);
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
);
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
);
336 virtual bool SaveFile( const wxString
& name
, int type
);
337 virtual bool SaveFile( const wxString
& name
, const wxString
& mimetype
);
340 virtual bool SaveFile( wxOutputStream
& stream
, int type
);
341 virtual bool SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
);
345 int GetWidth() const;
346 int GetHeight() const;
348 char unsigned *GetData() const;
349 void SetData( char unsigned *data
);
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;
358 wxImage
& operator = (const wxImage
& image
)
360 if ( (*this) != image
)
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
; }
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
);
379 static void CleanUpHandlers();
380 static void InitStandardHandlers();
384 static wxList sm_handlers
;
388 extern void WXDLLEXPORT
wxInitAllImageHandlers();