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"
22 #include "wx/hashmap.h"
25 # include "wx/stream.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 class WXDLLEXPORT wxImageHandler
;
35 class WXDLLEXPORT wxImage
;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 class WXDLLEXPORT wxImageHandler
: public wxObject
44 wxImageHandler() { m_name
= ""; m_extension
= ""; m_type
= 0; }
47 virtual bool LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
=TRUE
, int index
=-1 );
48 virtual bool SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
=TRUE
);
50 virtual int GetImageCount( wxInputStream
& stream
);
52 bool CanRead( wxInputStream
& stream
) { return DoCanRead(stream
); }
53 bool CanRead( const wxString
& name
);
54 #endif // wxUSE_STREAMS
56 void SetName(const wxString
& name
) { m_name
= name
; }
57 void SetExtension(const wxString
& ext
) { m_extension
= ext
; }
58 void SetType(long type
) { m_type
= type
; }
59 void SetMimeType(const wxString
& type
) { m_mime
= type
; }
60 wxString
GetName() const { return m_name
; }
61 wxString
GetExtension() const { return m_extension
; }
62 long GetType() const { return m_type
; }
63 wxString
GetMimeType() const { return m_mime
; }
67 virtual bool DoCanRead( wxInputStream
& stream
) = 0;
68 #endif // wxUSE_STREAMS
76 DECLARE_CLASS(wxImageHandler
)
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 class WXDLLEXPORT wxImageHistogramEntry
86 wxImageHistogramEntry() : index(0), value(0) {}
91 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry
,
92 wxIntegerHash
, wxIntegerEqual
,
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 class WXDLLEXPORT wxImage
: public wxObject
103 wxImage( int width
, int height
);
104 wxImage( int width
, int height
, unsigned char* data
, bool static_data
= FALSE
);
105 wxImage( const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
106 wxImage( const wxString
& name
, const wxString
& mimetype
, int index
= -1 );
109 wxImage( wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
110 wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
= -1 );
111 #endif // wxUSE_STREAMS
113 wxImage( const wxImage
& image
);
114 wxImage( const wxImage
* image
);
117 // convertion to/from wxBitmap (deprecated, use wxBitmap's methods instead):
118 wxImage( const wxBitmap
&bitmap
);
119 wxBitmap
ConvertToBitmap() const;
121 wxBitmap
ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const;
125 void Create( int width
, int height
);
126 void Create( int width
, int height
, unsigned char* data
, bool static_data
= FALSE
);
129 // creates an identical copy of the image (the = operator
130 // just raises the ref count)
131 wxImage
Copy() const;
133 // return the new image with size width*height
134 wxImage
GetSubImage( const wxRect
& ) const;
136 // pastes image into this instance and takes care of
137 // the mask colour and out of bounds problems
138 void Paste( const wxImage
&image
, int x
, int y
);
140 // return the new image with size width*height
141 wxImage
Scale( int width
, int height
) const;
143 // rescales the image in place
144 wxImage
& Rescale( int width
, int height
) { return *this = Scale(width
, height
); }
146 // Rotates the image about the given point, 'angle' radians.
147 // Returns the rotated image, leaving this image intact.
148 wxImage
Rotate(double angle
, const wxPoint
& centre_of_rotation
,
149 bool interpolating
= TRUE
, wxPoint
* offset_after_rotation
= (wxPoint
*) NULL
) const;
151 wxImage
Rotate90( bool clockwise
= TRUE
) const;
152 wxImage
Mirror( bool horizontally
= TRUE
) const;
154 // replace one colour with another
155 void Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
156 unsigned char r2
, unsigned char g2
, unsigned char b2
);
158 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
159 wxImage
ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const;
161 // these routines are slow but safe
162 void SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
);
163 unsigned char GetRed( int x
, int y
) const;
164 unsigned char GetGreen( int x
, int y
) const;
165 unsigned char GetBlue( int x
, int y
) const;
167 // find first colour that is not used in the image and has higher
168 // RGB values than <startR,startG,startB>
169 bool FindFirstUnusedColour( unsigned char *r
, unsigned char *g
, unsigned char *b
,
170 unsigned char startR
= 1, unsigned char startG
= 0,
171 unsigned char startB
= 0 );
172 // Set image's mask to the area of 'mask' that has <r,g,b> colour
173 bool SetMaskFromImage(const wxImage
& mask
,
174 unsigned char mr
, unsigned char mg
, unsigned char mb
);
176 static bool CanRead( const wxString
& name
);
177 static int GetImageCount( const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
);
178 virtual bool LoadFile( const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
179 virtual bool LoadFile( const wxString
& name
, const wxString
& mimetype
, int index
= -1 );
182 static bool CanRead( wxInputStream
& stream
);
183 static int GetImageCount( wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
);
184 virtual bool LoadFile( wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
185 virtual bool LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
= -1 );
188 virtual bool SaveFile( const wxString
& name
, int type
);
189 virtual bool SaveFile( const wxString
& name
, const wxString
& mimetype
);
192 virtual bool SaveFile( wxOutputStream
& stream
, int type
);
193 virtual bool SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
);
197 int GetWidth() const;
198 int GetHeight() const;
200 char unsigned *GetData() const;
201 void SetData( char unsigned *data
);
202 void SetData( char unsigned *data
, int new_width
, int new_height
);
205 void SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
);
206 unsigned char GetMaskRed() const;
207 unsigned char GetMaskGreen() const;
208 unsigned char GetMaskBlue() const;
209 void SetMask( bool mask
= TRUE
);
210 bool HasMask() const;
214 bool HasPalette() const;
215 const wxPalette
& GetPalette() const;
216 void SetPalette(const wxPalette
& palette
);
217 #endif // wxUSE_PALETTE
219 // Option functions (arbitrary name/value mapping)
220 void SetOption(const wxString
& name
, const wxString
& value
);
221 void SetOption(const wxString
& name
, int value
);
222 wxString
GetOption(const wxString
& name
) const;
223 int GetOptionInt(const wxString
& name
) const;
224 bool HasOption(const wxString
& name
) const;
226 unsigned long CountColours( unsigned long stopafter
= (unsigned long) -1 );
228 // Computes the histogram of the image and fills a hash table, indexed
229 // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry
230 // objects. Each of them contains an 'index' (useful to build a palette
231 // with the image colours) and a 'value', which is the number of pixels
232 // in the image with that colour.
233 // Returned value: # of entries in the histogram
234 unsigned long ComputeHistogram( wxImageHistogram
&h
);
236 wxImage
& operator = (const wxImage
& image
)
238 if ( (*this) != image
)
243 bool operator == (const wxImage
& image
)
244 { return m_refData
== image
.m_refData
; }
245 bool operator != (const wxImage
& image
)
246 { return m_refData
!= image
.m_refData
; }
248 static wxList
& GetHandlers() { return sm_handlers
; }
249 static void AddHandler( wxImageHandler
*handler
);
250 static void InsertHandler( wxImageHandler
*handler
);
251 static bool RemoveHandler( const wxString
& name
);
252 static wxImageHandler
*FindHandler( const wxString
& name
);
253 static wxImageHandler
*FindHandler( const wxString
& extension
, long imageType
);
254 static wxImageHandler
*FindHandler( long imageType
);
255 static wxImageHandler
*FindHandlerMime( const wxString
& mimetype
);
257 static void CleanUpHandlers();
258 static void InitStandardHandlers();
261 static wxList sm_handlers
;
264 friend class WXDLLEXPORT wxImageHandler
;
266 DECLARE_DYNAMIC_CLASS(wxImage
)
270 extern void WXDLLEXPORT
wxInitAllImageHandlers();
272 WXDLLEXPORT_DATA(extern wxImage
) wxNullImage
;
274 //-----------------------------------------------------------------------------
276 //-----------------------------------------------------------------------------
278 #include "wx/imagbmp.h"
279 #include "wx/imagpng.h"
280 #include "wx/imaggif.h"
281 #include "wx/imagpcx.h"
282 #include "wx/imagjpeg.h"
283 #include "wx/imagtiff.h"
284 #include "wx/imagpnm.h"
285 #include "wx/imagxpm.h"
286 #include "wx/imagiff.h"
288 #endif // wxUSE_IMAGE