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