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