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