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
;
32 class WXDLLEXPORT wxImage
;
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 class WXDLLEXPORT wxImageHandler
: public wxObject
40 DECLARE_CLASS(wxImageHandler
)
43 wxImageHandler() { m_name
= ""; m_extension
= ""; m_type
= 0; }
46 virtual bool LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
=TRUE
, int index
=0 );
47 virtual bool SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
=TRUE
);
49 virtual int GetImageCount( wxInputStream
& stream
);
51 bool CanRead( wxInputStream
& stream
) { return DoCanRead(stream
); }
52 bool CanRead( const wxString
& name
);
53 #endif // wxUSE_STREAMS
55 void SetName(const wxString
& name
) { m_name
= name
; }
56 void SetExtension(const wxString
& ext
) { m_extension
= ext
; }
57 void SetType(long type
) { m_type
= type
; }
58 void SetMimeType(const wxString
& type
) { m_mime
= type
; }
59 wxString
GetName() const { return m_name
; }
60 wxString
GetExtension() const { return m_extension
; }
61 long GetType() const { return m_type
; }
62 wxString
GetMimeType() const { return m_mime
; }
66 virtual bool DoCanRead( wxInputStream
& stream
) = 0;
67 #endif // wxUSE_STREAMS
75 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
80 class WXDLLEXPORT wxPNGHandler
: public wxImageHandler
82 DECLARE_DYNAMIC_CLASS(wxPNGHandler
)
90 m_type
= wxBITMAP_TYPE_PNG
;
95 virtual bool LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
=TRUE
, int index
=0 );
96 virtual bool SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
=TRUE
);
97 virtual bool DoCanRead( wxInputStream
& stream
);
102 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
107 class WXDLLEXPORT wxJPEGHandler
: public wxImageHandler
109 DECLARE_DYNAMIC_CLASS(wxJPEGHandler
)
113 inline wxJPEGHandler()
115 m_name
= "JPEG file";
117 m_type
= wxBITMAP_TYPE_JPEG
;
118 m_mime
= "image/jpeg";
122 virtual bool LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
=TRUE
, int index
=0 );
123 virtual bool SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
=TRUE
);
124 virtual bool DoCanRead( wxInputStream
& stream
);
129 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
134 class WXDLLEXPORT wxTIFFHandler
: public wxImageHandler
136 DECLARE_DYNAMIC_CLASS(wxTIFFHandler
)
140 inline wxTIFFHandler()
142 m_name
= "TIFF file";
144 m_type
= wxBITMAP_TYPE_TIF
;
145 m_mime
= "image/tiff";
149 virtual bool LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
=TRUE
, int index
=0 );
150 virtual bool SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
=TRUE
);
151 virtual bool DoCanRead( wxInputStream
& stream
);
152 virtual int GetImageCount( wxInputStream
& stream
);
157 //-----------------------------------------------------------------------------
159 //-----------------------------------------------------------------------------
161 class WXDLLEXPORT wxBMPHandler
: public wxImageHandler
163 DECLARE_DYNAMIC_CLASS(wxBMPHandler
)
167 inline wxBMPHandler()
171 m_type
= wxBITMAP_TYPE_BMP
;
172 m_mime
= "image/bmp";
176 virtual bool LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
=TRUE
, int index
=0 );
177 virtual bool DoCanRead( wxInputStream
& stream
);
183 //-----------------------------------------------------------------------------
186 class WXDLLEXPORT wxPNMHandler
: public wxImageHandler
188 DECLARE_DYNAMIC_CLASS(wxPNMHandler
)
192 inline wxPNMHandler()
196 m_type
= wxBITMAP_TYPE_PNM
;
197 m_mime
= "image/pnm";
201 virtual bool LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
=TRUE
, int index
=0 );
202 virtual bool SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
=TRUE
);
203 virtual bool DoCanRead( wxInputStream
& stream
);
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
213 class WXDLLEXPORT wxPCXHandler
: public wxImageHandler
215 DECLARE_DYNAMIC_CLASS(wxPCXHandler
)
219 inline wxPCXHandler()
223 m_type
= wxBITMAP_TYPE_PCX
;
224 m_mime
= "image/pcx";
228 virtual bool LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
=TRUE
, int index
=0 );
229 virtual bool SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
=TRUE
);
230 virtual bool DoCanRead( wxInputStream
& stream
);
231 #endif // wxUSE_STREAMS
235 //-----------------------------------------------------------------------------
237 //-----------------------------------------------------------------------------
241 class WXDLLEXPORT wxHNode
249 class WXDLLEXPORT wxImage
: public wxObject
251 DECLARE_DYNAMIC_CLASS(wxImage
)
253 friend class WXDLLEXPORT wxImageHandler
;
258 wxImage( int width
, int height
);
259 wxImage( const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
);
260 wxImage( wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
);
261 wxImage( const wxString
& name
, const wxString
& mimetype
);
262 wxImage( wxInputStream
& stream
, const wxString
& mimetype
);
264 wxImage( const wxImage
& image
);
265 wxImage( const wxImage
* image
);
267 // these functions get implemented in /src/(platform)/bitmap.cpp
268 wxImage( const wxBitmap
&bitmap
);
269 operator wxBitmap() const { return ConvertToBitmap(); }
270 wxBitmap
ConvertToBitmap() const;
272 void Create( int width
, int height
);
275 // return the new image with size width*height
276 wxImage
GetSubImage( const wxRect
& ) const;
278 // return the new image with size width*height
279 wxImage
Scale( int width
, int height
) const;
281 // rescales the image in place
282 void Rescale( int width
, int height
) { *this = Scale(width
, height
); }
284 // these routines are slow but safe
285 void SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
);
286 unsigned char GetRed( int x
, int y
);
287 unsigned char GetGreen( int x
, int y
);
288 unsigned char GetBlue( int x
, int y
);
290 static bool CanRead( const wxString
& name
);
291 virtual bool LoadFile( const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
);
292 virtual bool LoadFile( const wxString
& name
, const wxString
& mimetype
);
295 static bool CanRead( wxInputStream
& stream
);
296 virtual bool LoadFile( wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
);
297 virtual bool LoadFile( wxInputStream
& stream
, const wxString
& mimetype
);
300 virtual bool SaveFile( const wxString
& name
, int type
);
301 virtual bool SaveFile( const wxString
& name
, const wxString
& mimetype
);
304 virtual bool SaveFile( wxOutputStream
& stream
, int type
);
305 virtual bool SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
);
309 int GetWidth() const;
310 int GetHeight() const;
312 char unsigned *GetData() const;
313 void SetData( char unsigned *data
);
315 void SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
);
316 unsigned char GetMaskRed() const;
317 unsigned char GetMaskGreen() const;
318 unsigned char GetMaskBlue() const;
319 void SetMask( bool mask
= TRUE
);
320 bool HasMask() const;
322 wxImage
& operator = (const wxImage
& image
)
324 if ( (*this) != image
)
329 bool operator == (const wxImage
& image
)
330 { return m_refData
== image
.m_refData
; }
331 bool operator != (const wxImage
& image
)
332 { return m_refData
!= image
.m_refData
; }
334 static wxList
& GetHandlers() { return sm_handlers
; }
335 static void AddHandler( wxImageHandler
*handler
);
336 static void InsertHandler( wxImageHandler
*handler
);
337 static bool RemoveHandler( const wxString
& name
);
338 static wxImageHandler
*FindHandler( const wxString
& name
);
339 static wxImageHandler
*FindHandler( const wxString
& extension
, long imageType
);
340 static wxImageHandler
*FindHandler( long imageType
);
341 static wxImageHandler
*FindHandlerMime( const wxString
& mimetype
);
343 static void CleanUpHandlers();
344 static void InitStandardHandlers();
347 unsigned long CountColours( unsigned long stopafter
= -1 );
348 unsigned long ComputeHistogram( wxHashTable
&h
);
353 static wxList sm_handlers
;
358 extern void WXDLLEXPORT
wxInitAllImageHandlers();
361 //-----------------------------------------------------------------------------
363 //-----------------------------------------------------------------------------
365 #include "wx/imagbmp.h"
366 #include "wx/imagpng.h"
367 #include "wx/imaggif.h"
368 #include "wx/imagpcx.h"
369 #include "wx/imagjpeg.h"
370 #include "wx/imagtiff.h"
371 #include "wx/imagpnm.h"