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