]> git.saurik.com Git - wxWidgets.git/blob - include/wx/image.h
8718380516efeaf0f426c0d7729556ee59a928b2
[wxWidgets.git] / include / wx / image.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/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 #include "wx/defs.h"
14
15 #if wxUSE_IMAGE
16
17 #include "wx/object.h"
18 #include "wx/string.h"
19 #include "wx/gdicmn.h"
20 #include "wx/hashmap.h"
21
22 #if wxUSE_STREAMS
23 # include "wx/stream.h"
24 #endif
25
26 // on some systems (Unixware 7.x) index is defined as a macro in the headers
27 // which breaks the compilation below
28 #undef index
29
30 #define wxIMAGE_OPTION_QUALITY wxString(_T("quality"))
31 #define wxIMAGE_OPTION_FILENAME wxString(_T("FileName"))
32
33 #define wxIMAGE_OPTION_RESOLUTION wxString(_T("Resolution"))
34 #define wxIMAGE_OPTION_RESOLUTIONX wxString(_T("ResolutionX"))
35 #define wxIMAGE_OPTION_RESOLUTIONY wxString(_T("ResolutionY"))
36
37 #define wxIMAGE_OPTION_RESOLUTIONUNIT wxString(_T("ResolutionUnit"))
38
39 // constants used with wxIMAGE_OPTION_RESOLUTIONUNIT
40 enum
41 {
42 wxIMAGE_RESOLUTION_INCHES = 1,
43 wxIMAGE_RESOLUTION_CM = 2
44 };
45
46 // Constants for wxImage::Scale() for determining the level of quality
47 enum
48 {
49 wxIMAGE_QUALITY_NORMAL = 0,
50 wxIMAGE_QUALITY_HIGH = 1
51 };
52
53 // alpha channel values: fully transparent, default threshold separating
54 // transparent pixels from opaque for a few functions dealing with alpha and
55 // fully opaque
56 const unsigned char wxIMAGE_ALPHA_TRANSPARENT = 0;
57 const unsigned char wxIMAGE_ALPHA_THRESHOLD = 0x80;
58 const unsigned char wxIMAGE_ALPHA_OPAQUE = 0xff;
59
60 //-----------------------------------------------------------------------------
61 // classes
62 //-----------------------------------------------------------------------------
63
64 class WXDLLEXPORT wxImageHandler;
65 class WXDLLEXPORT wxImage;
66 class WXDLLEXPORT wxPalette;
67
68 //-----------------------------------------------------------------------------
69 // wxVariant support
70 //-----------------------------------------------------------------------------
71
72 #if wxUSE_VARIANT
73 #include "wx/variant.h"
74 DECLARE_VARIANT_OBJECT_EXPORTED(wxImage,WXDLLEXPORT)
75 #endif
76
77 //-----------------------------------------------------------------------------
78 // wxImageHandler
79 //-----------------------------------------------------------------------------
80
81 class WXDLLEXPORT wxImageHandler: public wxObject
82 {
83 public:
84 wxImageHandler()
85 : m_name(wxEmptyString), m_extension(wxEmptyString), m_mime(), m_type(0)
86 { }
87
88 #if wxUSE_STREAMS
89 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=true, int index=-1 );
90 virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=true );
91
92 virtual int GetImageCount( wxInputStream& stream );
93
94 bool CanRead( wxInputStream& stream ) { return CallDoCanRead(stream); }
95 bool CanRead( const wxString& name );
96 #endif // wxUSE_STREAMS
97
98 void SetName(const wxString& name) { m_name = name; }
99 void SetExtension(const wxString& ext) { m_extension = ext; }
100 void SetType(long type) { m_type = type; }
101 void SetMimeType(const wxString& type) { m_mime = type; }
102 wxString GetName() const { return m_name; }
103 wxString GetExtension() const { return m_extension; }
104 long GetType() const { return m_type; }
105 wxString GetMimeType() const { return m_mime; }
106
107 protected:
108 #if wxUSE_STREAMS
109 virtual bool DoCanRead( wxInputStream& stream ) = 0;
110
111 // save the stream position, call DoCanRead() and restore the position
112 bool CallDoCanRead(wxInputStream& stream);
113 #endif // wxUSE_STREAMS
114
115 wxString m_name;
116 wxString m_extension;
117 wxString m_mime;
118 long m_type;
119
120 private:
121 DECLARE_CLASS(wxImageHandler)
122 };
123
124 //-----------------------------------------------------------------------------
125 // wxImageHistogram
126 //-----------------------------------------------------------------------------
127
128 class WXDLLEXPORT wxImageHistogramEntry
129 {
130 public:
131 wxImageHistogramEntry() { index = value = 0; }
132 unsigned long index;
133 unsigned long value;
134 };
135
136 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
137 wxIntegerHash, wxIntegerEqual,
138 wxImageHistogramBase);
139
140 class WXDLLEXPORT wxImageHistogram : public wxImageHistogramBase
141 {
142 public:
143 wxImageHistogram() : wxImageHistogramBase(256) { }
144
145 // get the key in the histogram for the given RGB values
146 static unsigned long MakeKey(unsigned char r,
147 unsigned char g,
148 unsigned char b)
149 {
150 return (r << 16) | (g << 8) | b;
151 }
152
153 // find first colour that is not used in the image and has higher
154 // RGB values than RGB(startR, startG, startB)
155 //
156 // returns true and puts this colour in r, g, b (each of which may be NULL)
157 // on success or returns false if there are no more free colours
158 bool FindFirstUnusedColour(unsigned char *r,
159 unsigned char *g,
160 unsigned char *b,
161 unsigned char startR = 1,
162 unsigned char startG = 0,
163 unsigned char startB = 0 ) const;
164 };
165
166 //-----------------------------------------------------------------------------
167 // wxImage
168 //-----------------------------------------------------------------------------
169
170 class WXDLLEXPORT wxImage: public wxObject
171 {
172 public:
173 // red, green and blue are 8 bit unsigned integers in the range of 0..255
174 // We use the identifier RGBValue instead of RGB, since RGB is #defined
175 class RGBValue
176 {
177 public:
178 RGBValue(unsigned char r=0, unsigned char g=0, unsigned char b=0)
179 : red(r), green(g), blue(b) {}
180 unsigned char red;
181 unsigned char green;
182 unsigned char blue;
183 };
184
185 // hue, saturation and value are doubles in the range 0.0..1.0
186 class HSVValue
187 {
188 public:
189 HSVValue(double h=0.0, double s=0.0, double v=0.0)
190 : hue(h), saturation(s), value(v) {}
191 double hue;
192 double saturation;
193 double value;
194 };
195
196 wxImage(){}
197 wxImage( int width, int height, bool clear = true );
198 wxImage( int width, int height, unsigned char* data, bool static_data = false );
199 wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
200 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
201 wxImage( const wxString& name, const wxString& mimetype, int index = -1 );
202 wxImage( const char** xpmData );
203 wxImage( char** xpmData );
204
205 #if wxUSE_STREAMS
206 wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
207 wxImage( wxInputStream& stream, const wxString& mimetype, int index = -1 );
208 #endif // wxUSE_STREAMS
209
210 bool Create( int width, int height, bool clear = true );
211 bool Create( int width, int height, unsigned char* data, bool static_data = false );
212 bool Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
213 bool Create( const char** xpmData );
214 void Destroy();
215
216 // creates an identical copy of the image (the = operator
217 // just raises the ref count)
218 wxImage Copy() const;
219
220 // return the new image with size width*height
221 wxImage GetSubImage( const wxRect& rect) const;
222
223 // Paste the image or part of this image into an image of the given size at the pos
224 // any newly exposed areas will be filled with the rgb colour
225 // by default if r = g = b = -1 then fill with this image's mask colour or find and
226 // set a suitable mask colour
227 wxImage Size( const wxSize& size, const wxPoint& pos,
228 int r = -1, int g = -1, int b = -1 ) const;
229
230 // pastes image into this instance and takes care of
231 // the mask colour and out of bounds problems
232 void Paste( const wxImage &image, int x, int y );
233
234 // return the new image with size width*height
235 wxImage Scale( int width, int height, int quality = wxIMAGE_QUALITY_NORMAL ) const;
236
237 // box averager and bicubic filters for up/down sampling
238 wxImage ResampleBox(int width, int height) const;
239 wxImage ResampleBicubic(int width, int height) const;
240
241 // blur the image according to the specified pixel radius
242 wxImage Blur(int radius);
243 wxImage BlurHorizontal(int radius);
244 wxImage BlurVertical(int radius);
245
246 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
247
248 // rescales the image in place
249 wxImage& Rescale( int width, int height, int quality = wxIMAGE_QUALITY_NORMAL ) { return *this = Scale(width, height, quality); }
250
251 // resizes the image in place
252 wxImage& Resize( const wxSize& size, const wxPoint& pos,
253 int r = -1, int g = -1, int b = -1 ) { return *this = Size(size, pos, r, g, b); }
254
255 // Rotates the image about the given point, 'angle' radians.
256 // Returns the rotated image, leaving this image intact.
257 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
258 bool interpolating = true, wxPoint * offset_after_rotation = (wxPoint*) NULL) const;
259
260 wxImage Rotate90( bool clockwise = true ) const;
261 wxImage Mirror( bool horizontally = true ) const;
262
263 // replace one colour with another
264 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
265 unsigned char r2, unsigned char g2, unsigned char b2 );
266
267 // Convert to greyscale image. Uses the luminance component (Y) of the image.
268 // The luma value (YUV) is calculated using (R * lr) + (G * lg) + (B * lb), defaults to ITU-T BT.601
269 wxImage ConvertToGreyscale( double lr = 0.299, double lg = 0.587, double lb = 0.114 ) const;
270
271 // convert to monochrome image (<r,g,b> will be replaced by white,
272 // everything else by black)
273 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
274
275 // these routines are slow but safe
276 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
277 void SetRGB( const wxRect& rect, unsigned char r, unsigned char g, unsigned char b );
278 unsigned char GetRed( int x, int y ) const;
279 unsigned char GetGreen( int x, int y ) const;
280 unsigned char GetBlue( int x, int y ) const;
281
282 void SetAlpha(int x, int y, unsigned char alpha);
283 unsigned char GetAlpha(int x, int y) const;
284
285 // find first colour that is not used in the image and has higher
286 // RGB values than <startR,startG,startB>
287 bool FindFirstUnusedColour( unsigned char *r, unsigned char *g, unsigned char *b,
288 unsigned char startR = 1, unsigned char startG = 0,
289 unsigned char startB = 0 ) const;
290 // Set image's mask to the area of 'mask' that has <r,g,b> colour
291 bool SetMaskFromImage(const wxImage & mask,
292 unsigned char mr, unsigned char mg, unsigned char mb);
293
294 // converts image's alpha channel to mask, if it has any, does nothing
295 // otherwise:
296 bool ConvertAlphaToMask(unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD);
297
298 // This method converts an image where the original alpha
299 // information is only available as a shades of a colour
300 // (actually shades of grey) typically when you draw anti-
301 // aliased text into a bitmap. The DC drawinf routines
302 // draw grey values on the black background although they
303 // actually mean to draw white with differnt alpha values.
304 // This method reverses it, assuming a black (!) background
305 // and white text (actually only the red channel is read).
306 // The method will then fill up the whole image with the
307 // colour given.
308 bool ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b );
309
310 static bool CanRead( const wxString& name );
311 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
312 virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
313 virtual bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
314
315 #if wxUSE_STREAMS
316 static bool CanRead( wxInputStream& stream );
317 static int GetImageCount( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
318 virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
319 virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
320 #endif
321
322 virtual bool SaveFile( const wxString& name ) const;
323 virtual bool SaveFile( const wxString& name, int type ) const;
324 virtual bool SaveFile( const wxString& name, const wxString& mimetype ) const;
325
326 #if wxUSE_STREAMS
327 virtual bool SaveFile( wxOutputStream& stream, int type ) const;
328 virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype ) const;
329 #endif
330
331 bool Ok() const;
332 int GetWidth() const;
333 int GetHeight() const;
334
335 // these functions provide fastest access to wxImage data but should be
336 // used carefully as no checks are done
337 unsigned char *GetData() const;
338 void SetData( unsigned char *data, bool static_data=false );
339 void SetData( unsigned char *data, int new_width, int new_height, bool static_data=false );
340
341 unsigned char *GetAlpha() const; // may return NULL!
342 bool HasAlpha() const { return GetAlpha() != NULL; }
343 void SetAlpha(unsigned char *alpha = NULL, bool static_data=false);
344 void InitAlpha();
345
346 // return true if this pixel is masked or has alpha less than specified
347 // threshold
348 bool IsTransparent(int x, int y,
349 unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD) const;
350
351 // Mask functions
352 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
353 // Get the current mask colour or find a suitable colour
354 // returns true if using current mask colour
355 bool GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const;
356 unsigned char GetMaskRed() const;
357 unsigned char GetMaskGreen() const;
358 unsigned char GetMaskBlue() const;
359 void SetMask( bool mask = true );
360 bool HasMask() const;
361
362 #if wxUSE_PALETTE
363 // Palette functions
364 bool HasPalette() const;
365 const wxPalette& GetPalette() const;
366 void SetPalette(const wxPalette& palette);
367 #endif // wxUSE_PALETTE
368
369 // Option functions (arbitrary name/value mapping)
370 void SetOption(const wxString& name, const wxString& value);
371 void SetOption(const wxString& name, int value);
372 wxString GetOption(const wxString& name) const;
373 int GetOptionInt(const wxString& name) const;
374 bool HasOption(const wxString& name) const;
375
376 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ) const;
377
378 // Computes the histogram of the image and fills a hash table, indexed
379 // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry
380 // objects. Each of them contains an 'index' (useful to build a palette
381 // with the image colours) and a 'value', which is the number of pixels
382 // in the image with that colour.
383 // Returned value: # of entries in the histogram
384 unsigned long ComputeHistogram( wxImageHistogram &h ) const;
385
386 // Rotates the hue of each pixel of the image. angle is a double in the range
387 // -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
388 void RotateHue(double angle);
389
390 bool operator == (const wxImage& image) const
391 { return m_refData == image.m_refData; }
392 bool operator != (const wxImage& image) const
393 { return m_refData != image.m_refData; }
394
395 static wxList& GetHandlers() { return sm_handlers; }
396 static void AddHandler( wxImageHandler *handler );
397 static void InsertHandler( wxImageHandler *handler );
398 static bool RemoveHandler( const wxString& name );
399 static wxImageHandler *FindHandler( const wxString& name );
400 static wxImageHandler *FindHandler( const wxString& extension, long imageType );
401 static wxImageHandler *FindHandler( long imageType );
402 static wxImageHandler *FindHandlerMime( const wxString& mimetype );
403
404 static wxString GetImageExtWildcard();
405
406 static void CleanUpHandlers();
407 static void InitStandardHandlers();
408
409 static HSVValue RGBtoHSV(const RGBValue& rgb);
410 static RGBValue HSVtoRGB(const HSVValue& hsv);
411
412
413 protected:
414 static wxList sm_handlers;
415
416 // return the index of the point with the given coordinates or -1 if the
417 // image is invalid of the coordinates are out of range
418 //
419 // note that index must be multiplied by 3 when using it with RGB array
420 long XYToIndex(int x, int y) const;
421
422 virtual wxObjectRefData* CreateRefData() const;
423 virtual wxObjectRefData* CloneRefData(const wxObjectRefData* data) const;
424
425 private:
426 friend class WXDLLEXPORT wxImageHandler;
427
428 DECLARE_DYNAMIC_CLASS(wxImage)
429 };
430
431
432 extern void WXDLLEXPORT wxInitAllImageHandlers();
433
434 extern WXDLLEXPORT_DATA(wxImage) wxNullImage;
435
436 //-----------------------------------------------------------------------------
437 // wxImage handlers
438 //-----------------------------------------------------------------------------
439
440 #include "wx/imagbmp.h"
441 #include "wx/imagpng.h"
442 #include "wx/imaggif.h"
443 #include "wx/imagpcx.h"
444 #include "wx/imagjpeg.h"
445 #include "wx/imagtiff.h"
446 #include "wx/imagpnm.h"
447 #include "wx/imagxpm.h"
448 #include "wx/imagiff.h"
449
450 #endif // wxUSE_IMAGE
451
452 #endif
453 // _WX_IMAGE_H_