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