]>
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(NO_GCC_PRAGMA) | |
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/hashmap.h" | |
22 | ||
23 | #if wxUSE_STREAMS | |
24 | # include "wx/stream.h" | |
25 | #endif | |
26 | ||
27 | #if wxUSE_IMAGE | |
28 | ||
29 | // on some systems (Unixware 7.x) index is defined as a macro in the headers | |
30 | // which breaks the compilation below | |
31 | #undef index | |
32 | ||
33 | #define wxIMAGE_OPTION_QUALITY wxString(_T("quality")) | |
34 | #define wxIMAGE_OPTION_FILENAME wxString(_T("FileName")) | |
35 | ||
36 | #define wxIMAGE_OPTION_RESOLUTION wxString(_T("Resolution")) | |
37 | #define wxIMAGE_OPTION_RESOLUTIONX wxString(_T("ResolutionX")) | |
38 | #define wxIMAGE_OPTION_RESOLUTIONY wxString(_T("ResolutionY")) | |
39 | ||
40 | #define wxIMAGE_OPTION_RESOLUTIONUNIT wxString(_T("ResolutionUnit")) | |
41 | ||
42 | // constants used with wxIMAGE_OPTION_RESOLUTIONUNIT | |
43 | enum | |
44 | { | |
45 | wxIMAGE_RESOLUTION_INCHES = 1, | |
46 | wxIMAGE_RESOLUTION_CM = 2 | |
47 | }; | |
48 | ||
49 | //----------------------------------------------------------------------------- | |
50 | // classes | |
51 | //----------------------------------------------------------------------------- | |
52 | ||
53 | class WXDLLEXPORT wxImageHandler; | |
54 | class WXDLLEXPORT wxImage; | |
55 | class WXDLLEXPORT wxPalette; | |
56 | ||
57 | //----------------------------------------------------------------------------- | |
58 | // wxImageHandler | |
59 | //----------------------------------------------------------------------------- | |
60 | ||
61 | class WXDLLEXPORT wxImageHandler: public wxObject | |
62 | { | |
63 | public: | |
64 | wxImageHandler() | |
65 | : m_name(wxEmptyString), m_extension(wxEmptyString), m_mime(), m_type(0) | |
66 | { } | |
67 | ||
68 | #if wxUSE_STREAMS | |
69 | virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=true, int index=-1 ); | |
70 | virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=true ); | |
71 | ||
72 | virtual int GetImageCount( wxInputStream& stream ); | |
73 | ||
74 | bool CanRead( wxInputStream& stream ) { return CallDoCanRead(stream); } | |
75 | bool CanRead( const wxString& name ); | |
76 | #endif // wxUSE_STREAMS | |
77 | ||
78 | void SetName(const wxString& name) { m_name = name; } | |
79 | void SetExtension(const wxString& ext) { m_extension = ext; } | |
80 | void SetType(long type) { m_type = type; } | |
81 | void SetMimeType(const wxString& type) { m_mime = type; } | |
82 | wxString GetName() const { return m_name; } | |
83 | wxString GetExtension() const { return m_extension; } | |
84 | long GetType() const { return m_type; } | |
85 | wxString GetMimeType() const { return m_mime; } | |
86 | ||
87 | protected: | |
88 | #if wxUSE_STREAMS | |
89 | virtual bool DoCanRead( wxInputStream& stream ) = 0; | |
90 | ||
91 | // save the stream position, call DoCanRead() and restore the position | |
92 | bool CallDoCanRead(wxInputStream& stream); | |
93 | #endif // wxUSE_STREAMS | |
94 | ||
95 | wxString m_name; | |
96 | wxString m_extension; | |
97 | wxString m_mime; | |
98 | long m_type; | |
99 | ||
100 | private: | |
101 | DECLARE_CLASS(wxImageHandler) | |
102 | }; | |
103 | ||
104 | //----------------------------------------------------------------------------- | |
105 | // wxImageHistogram | |
106 | //----------------------------------------------------------------------------- | |
107 | ||
108 | class WXDLLEXPORT wxImageHistogramEntry | |
109 | { | |
110 | public: | |
111 | wxImageHistogramEntry() { index = value = 0; } | |
112 | unsigned long index; | |
113 | unsigned long value; | |
114 | }; | |
115 | ||
116 | WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry, | |
117 | wxIntegerHash, wxIntegerEqual, | |
118 | wxImageHistogramBase); | |
119 | ||
120 | class WXDLLEXPORT wxImageHistogram : public wxImageHistogramBase | |
121 | { | |
122 | public: | |
123 | wxImageHistogram() : wxImageHistogramBase(256) { } | |
124 | ||
125 | // get the key in the histogram for the given RGB values | |
126 | static unsigned long MakeKey(unsigned char r, | |
127 | unsigned char g, | |
128 | unsigned char b) | |
129 | { | |
130 | return (r << 16) | (g << 8) | b; | |
131 | } | |
132 | ||
133 | // find first colour that is not used in the image and has higher | |
134 | // RGB values than RGB(startR, startG, startB) | |
135 | // | |
136 | // returns true and puts this colour in r, g, b (each of which may be NULL) | |
137 | // on success or returns false if there are no more free colours | |
138 | bool FindFirstUnusedColour(unsigned char *r, | |
139 | unsigned char *g, | |
140 | unsigned char *b, | |
141 | unsigned char startR = 1, | |
142 | unsigned char startG = 0, | |
143 | unsigned char startB = 0 ) const; | |
144 | }; | |
145 | ||
146 | //----------------------------------------------------------------------------- | |
147 | // wxImage | |
148 | //----------------------------------------------------------------------------- | |
149 | ||
150 | class WXDLLEXPORT wxImage: public wxObject | |
151 | { | |
152 | public: | |
153 | wxImage(){} | |
154 | wxImage( int width, int height, bool clear = true ); | |
155 | wxImage( int width, int height, unsigned char* data, bool static_data = false ); | |
156 | wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false ); | |
157 | wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ); | |
158 | wxImage( const wxString& name, const wxString& mimetype, int index = -1 ); | |
159 | wxImage( const char** xpmData ); | |
160 | wxImage( char** xpmData ); | |
161 | ||
162 | #if wxUSE_STREAMS | |
163 | wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ); | |
164 | wxImage( wxInputStream& stream, const wxString& mimetype, int index = -1 ); | |
165 | #endif // wxUSE_STREAMS | |
166 | ||
167 | wxImage( const wxImage& image ); | |
168 | wxImage( const wxImage* image ); | |
169 | ||
170 | bool Create( int width, int height, bool clear = true ); | |
171 | bool Create( int width, int height, unsigned char* data, bool static_data = false ); | |
172 | bool Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false ); | |
173 | bool Create( const char** xpmData ); | |
174 | void Destroy(); | |
175 | ||
176 | // creates an identical copy of the image (the = operator | |
177 | // just raises the ref count) | |
178 | wxImage Copy() const; | |
179 | ||
180 | // return the new image with size width*height | |
181 | wxImage GetSubImage( const wxRect& rect) const; | |
182 | ||
183 | // Paste the image or part of this image into an image of the given size at the pos | |
184 | // any newly exposed areas will be filled with the rgb colour | |
185 | // by default if r = g = b = -1 then fill with this image's mask colour or find and | |
186 | // set a suitable mask colour | |
187 | wxImage Size( const wxSize& size, const wxPoint& pos, | |
188 | int r = -1, int g = -1, int b = -1 ) const; | |
189 | ||
190 | // pastes image into this instance and takes care of | |
191 | // the mask colour and out of bounds problems | |
192 | void Paste( const wxImage &image, int x, int y ); | |
193 | ||
194 | // return the new image with size width*height | |
195 | wxImage Scale( int width, int height ) const; | |
196 | ||
197 | wxImage ShrinkBy( int xFactor , int yFactor ) const ; | |
198 | ||
199 | // rescales the image in place | |
200 | wxImage& Rescale( int width, int height ) { return *this = Scale(width, height); } | |
201 | ||
202 | // resizes the image in place | |
203 | wxImage& Resize( const wxSize& size, const wxPoint& pos, | |
204 | int r = -1, int g = -1, int b = -1 ) { return *this = Size(size, pos, r, g, b); } | |
205 | ||
206 | // Rotates the image about the given point, 'angle' radians. | |
207 | // Returns the rotated image, leaving this image intact. | |
208 | wxImage Rotate(double angle, const wxPoint & centre_of_rotation, | |
209 | bool interpolating = true, wxPoint * offset_after_rotation = (wxPoint*) NULL) const; | |
210 | ||
211 | wxImage Rotate90( bool clockwise = true ) const; | |
212 | wxImage Mirror( bool horizontally = true ) const; | |
213 | ||
214 | // replace one colour with another | |
215 | void Replace( unsigned char r1, unsigned char g1, unsigned char b1, | |
216 | unsigned char r2, unsigned char g2, unsigned char b2 ); | |
217 | ||
218 | // convert to monochrome image (<r,g,b> will be replaced by white, | |
219 | // everything else by black) | |
220 | wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const; | |
221 | ||
222 | // these routines are slow but safe | |
223 | void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ); | |
224 | void SetRGB( const wxRect& rect, unsigned char r, unsigned char g, unsigned char b ); | |
225 | unsigned char GetRed( int x, int y ) const; | |
226 | unsigned char GetGreen( int x, int y ) const; | |
227 | unsigned char GetBlue( int x, int y ) const; | |
228 | ||
229 | void SetAlpha(int x, int y, unsigned char alpha); | |
230 | unsigned char GetAlpha(int x, int y) const; | |
231 | ||
232 | // find first colour that is not used in the image and has higher | |
233 | // RGB values than <startR,startG,startB> | |
234 | bool FindFirstUnusedColour( unsigned char *r, unsigned char *g, unsigned char *b, | |
235 | unsigned char startR = 1, unsigned char startG = 0, | |
236 | unsigned char startB = 0 ) const; | |
237 | // Set image's mask to the area of 'mask' that has <r,g,b> colour | |
238 | bool SetMaskFromImage(const wxImage & mask, | |
239 | unsigned char mr, unsigned char mg, unsigned char mb); | |
240 | ||
241 | // converts image's alpha channel to mask, if it has any, does nothing | |
242 | // otherwise: | |
243 | bool ConvertAlphaToMask(unsigned char threshold = 128); | |
244 | ||
245 | // This method converts an image where the original alpha | |
246 | // information is only available as a shades of a colour | |
247 | // (actually shades of grey) typically when you draw anti- | |
248 | // aliased text into a bitmap. The DC drawinf routines | |
249 | // draw grey values on the black background although they | |
250 | // actually mean to draw white with differnt alpha values. | |
251 | // This method reverses it, assuming a black (!) background | |
252 | // and white text (actually only the red channel is read). | |
253 | // The method will then fill up the whole image with the | |
254 | // colour given. | |
255 | bool ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b ); | |
256 | ||
257 | static bool CanRead( const wxString& name ); | |
258 | static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY ); | |
259 | virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ); | |
260 | virtual bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ); | |
261 | ||
262 | #if wxUSE_STREAMS | |
263 | static bool CanRead( wxInputStream& stream ); | |
264 | static int GetImageCount( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY ); | |
265 | virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ); | |
266 | virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ); | |
267 | #endif | |
268 | ||
269 | virtual bool SaveFile( const wxString& name ) const; | |
270 | virtual bool SaveFile( const wxString& name, int type ) const; | |
271 | virtual bool SaveFile( const wxString& name, const wxString& mimetype ) const; | |
272 | ||
273 | #if wxUSE_STREAMS | |
274 | virtual bool SaveFile( wxOutputStream& stream, int type ) const; | |
275 | virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype ) const; | |
276 | #endif | |
277 | ||
278 | bool Ok() const; | |
279 | int GetWidth() const; | |
280 | int GetHeight() const; | |
281 | ||
282 | // these functions provide fastest access to wxImage data but should be | |
283 | // used carefully as no checks are done | |
284 | unsigned char *GetData() const; | |
285 | void SetData( unsigned char *data ); | |
286 | void SetData( unsigned char *data, int new_width, int new_height ); | |
287 | ||
288 | unsigned char *GetAlpha() const; // may return NULL! | |
289 | bool HasAlpha() const { return GetAlpha() != NULL; } | |
290 | void SetAlpha(unsigned char *alpha = NULL); | |
291 | void InitAlpha(); | |
292 | ||
293 | // Mask functions | |
294 | void SetMaskColour( unsigned char r, unsigned char g, unsigned char b ); | |
295 | // Get the current mask colour or find a suitable colour | |
296 | // returns true if using current mask colour | |
297 | bool GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const; | |
298 | unsigned char GetMaskRed() const; | |
299 | unsigned char GetMaskGreen() const; | |
300 | unsigned char GetMaskBlue() const; | |
301 | void SetMask( bool mask = true ); | |
302 | bool HasMask() const; | |
303 | ||
304 | #if wxUSE_PALETTE | |
305 | // Palette functions | |
306 | bool HasPalette() const; | |
307 | const wxPalette& GetPalette() const; | |
308 | void SetPalette(const wxPalette& palette); | |
309 | #endif // wxUSE_PALETTE | |
310 | ||
311 | // Option functions (arbitrary name/value mapping) | |
312 | void SetOption(const wxString& name, const wxString& value); | |
313 | void SetOption(const wxString& name, int value); | |
314 | wxString GetOption(const wxString& name) const; | |
315 | int GetOptionInt(const wxString& name) const; | |
316 | bool HasOption(const wxString& name) const; | |
317 | ||
318 | unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ) const; | |
319 | ||
320 | // Computes the histogram of the image and fills a hash table, indexed | |
321 | // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry | |
322 | // objects. Each of them contains an 'index' (useful to build a palette | |
323 | // with the image colours) and a 'value', which is the number of pixels | |
324 | // in the image with that colour. | |
325 | // Returned value: # of entries in the histogram | |
326 | unsigned long ComputeHistogram( wxImageHistogram &h ) const; | |
327 | ||
328 | wxImage& operator = (const wxImage& image) | |
329 | { | |
330 | if ( (*this) != image ) | |
331 | Ref(image); | |
332 | return *this; | |
333 | } | |
334 | ||
335 | bool operator == (const wxImage& image) const | |
336 | { return m_refData == image.m_refData; } | |
337 | bool operator != (const wxImage& image) const | |
338 | { return m_refData != image.m_refData; } | |
339 | ||
340 | static wxList& GetHandlers() { return sm_handlers; } | |
341 | static void AddHandler( wxImageHandler *handler ); | |
342 | static void InsertHandler( wxImageHandler *handler ); | |
343 | static bool RemoveHandler( const wxString& name ); | |
344 | static wxImageHandler *FindHandler( const wxString& name ); | |
345 | static wxImageHandler *FindHandler( const wxString& extension, long imageType ); | |
346 | static wxImageHandler *FindHandler( long imageType ); | |
347 | static wxImageHandler *FindHandlerMime( const wxString& mimetype ); | |
348 | ||
349 | static wxString GetImageExtWildcard(); | |
350 | ||
351 | static void CleanUpHandlers(); | |
352 | static void InitStandardHandlers(); | |
353 | ||
354 | protected: | |
355 | static wxList sm_handlers; | |
356 | ||
357 | private: | |
358 | friend class WXDLLEXPORT wxImageHandler; | |
359 | ||
360 | DECLARE_DYNAMIC_CLASS(wxImage) | |
361 | }; | |
362 | ||
363 | ||
364 | extern void WXDLLEXPORT wxInitAllImageHandlers(); | |
365 | ||
366 | extern WXDLLEXPORT_DATA(wxImage) wxNullImage; | |
367 | ||
368 | //----------------------------------------------------------------------------- | |
369 | // wxImage handlers | |
370 | //----------------------------------------------------------------------------- | |
371 | ||
372 | #include "wx/imagbmp.h" | |
373 | #include "wx/imagpng.h" | |
374 | #include "wx/imaggif.h" | |
375 | #include "wx/imagpcx.h" | |
376 | #include "wx/imagjpeg.h" | |
377 | #include "wx/imagtiff.h" | |
378 | #include "wx/imagpnm.h" | |
379 | #include "wx/imagxpm.h" | |
380 | #include "wx/imagiff.h" | |
381 | ||
382 | #endif // wxUSE_IMAGE | |
383 | ||
384 | #endif | |
385 | // _WX_IMAGE_H_ | |
386 |