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