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