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