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