1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxImageHandler and wxImage
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
10 Possible values for the image resolution option.
12 @see wxImage::GetOptionInt().
14 enum wxImageResolution
16 /// Resolution not specified.
17 wxIMAGE_RESOLUTION_NONE
= 0,
19 /// Resolution specified in inches.
20 wxIMAGE_RESOLUTION_INCHES
= 1,
22 /// Resolution specified in centimetres.
23 wxIMAGE_RESOLUTION_CM
= 2
27 Possible values for PNG image type option.
29 @see wxImage::GetOptionInt().
33 wxPNG_TYPE_COLOUR
= 0, ///< Colour PNG image.
34 wxPNG_TYPE_GREY
= 2, ///< Greyscale PNG image converted from RGB.
35 wxPNG_TYPE_GREY_RED
= 3 ///< Greyscale PNG image using red as grey.
41 This is the base class for implementing image file loading/saving, and
42 image creation from data.
43 It is used within wxImage and is not normally seen by the application.
45 If you wish to extend the capabilities of wxImage, derive a class from
46 wxImageHandler and add the handler using wxImage::AddHandler in your
47 application initialization.
49 Note that all wxImageHandlers provided by wxWidgets are part of
50 the @ref page_libs_wxcore library.
51 For details about the default handlers, please see the section
52 @ref image_handlers in the wxImage class documentation.
55 @section imagehandler_note Note (Legal Issue)
57 This software is based in part on the work of the Independent JPEG Group.
58 (Applies when wxWidgets is linked with JPEG support.
59 wxJPEGHandler uses libjpeg created by IJG.)
68 @see wxImage, wxInitAllImageHandlers()
70 class wxImageHandler
: public wxObject
76 In your own default constructor, initialise the members
77 m_name, m_extension and m_type.
82 Destroys the wxImageHandler object.
84 virtual ~wxImageHandler();
87 Gets the preferred file extension associated with this handler.
89 @see GetAltExtensions()
91 const wxString
& GetExtension() const;
94 Returns the other file extensions associated with this handler.
96 The preferred extension for this handler is returned by GetExtension().
100 const wxArrayString
& GetAltExtensions() const;
103 If the image file contains more than one image and the image handler is capable
104 of retrieving these individually, this function will return the number of
108 Opened input stream for reading image data.
109 Currently, the stream must support seeking.
111 @return Number of available images. For most image handlers, this is 1
112 (exceptions are TIFF and ICO formats as well as animated GIFs
113 for which this function returns the number of frames in the
116 virtual int GetImageCount(wxInputStream
& stream
);
119 Gets the MIME type associated with this handler.
121 const wxString
& GetMimeType() const;
124 Gets the name of this handler.
126 const wxString
& GetName() const;
129 Gets the image type associated with this handler.
131 wxBitmapType
GetType() const;
134 Loads a image from a stream, putting the resulting data into @a image.
136 If the image file contains more than one image and the image handler is
137 capable of retrieving these individually, @a index indicates which image
138 to read from the stream.
141 The image object which is to be affected by this operation.
143 Opened input stream for reading image data.
145 If set to @true, errors reported by the image handler will produce
148 The index of the image in the file (starting from zero).
150 @return @true if the operation succeeded, @false otherwise.
152 @see wxImage::LoadFile, wxImage::SaveFile, SaveFile()
154 virtual bool LoadFile(wxImage
* image
, wxInputStream
& stream
,
155 bool verbose
= true, int index
= -1);
158 Saves a image in the output stream.
161 The image object which is to be affected by this operation.
163 Opened output stream for writing the data.
165 If set to @true, errors reported by the image handler will produce
168 @return @true if the operation succeeded, @false otherwise.
170 @see wxImage::LoadFile, wxImage::SaveFile, LoadFile()
172 virtual bool SaveFile(wxImage
* image
, wxOutputStream
& stream
,
173 bool verbose
= true);
176 Sets the preferred file extension associated with this handler.
179 File extension without leading dot.
181 @see SetAltExtensions()
183 void SetExtension(const wxString
& extension
);
186 Sets the alternative file extensions associated with this handler.
189 Array of file extensions.
195 void SetAltExtensions(const wxArrayString
& extensions
);
198 Sets the handler MIME type.
203 void SetMimeType(const wxString
& mimetype
);
206 Sets the handler name.
211 void SetName(const wxString
& name
);
216 Constant used to indicate the alpha value conventionally defined as
217 the complete transparency.
219 const unsigned char wxIMAGE_ALPHA_TRANSPARENT
= 0;
222 Constant used to indicate the alpha value conventionally defined as
223 the complete opacity.
225 const unsigned char wxIMAGE_ALPHA_OPAQUE
= 0xff;
230 This class encapsulates a platform-independent image.
232 An image can be created from data, or using wxBitmap::ConvertToImage.
233 An image can be loaded from a file in a variety of formats, and is extensible
234 to new formats via image format handlers. Functions are available to set and
235 get image bits, so it can be used for basic image manipulation.
237 A wxImage cannot (currently) be drawn directly to a wxDC.
238 Instead, a platform-specific wxBitmap object must be created from it using
239 the wxBitmap::wxBitmap(wxImage,int depth) constructor.
240 This bitmap can then be drawn in a device context, using wxDC::DrawBitmap.
242 One colour value of the image may be used as a mask colour which will lead to
243 the automatic creation of a wxMask object associated to the bitmap object.
246 @section image_alpha Alpha channel support
248 Starting from wxWidgets 2.5.0 wxImage supports alpha channel data, that is
249 in addition to a byte for the red, green and blue colour components for each
250 pixel it also stores a byte representing the pixel opacity.
252 An alpha value of 0 corresponds to a transparent pixel (null opacity) while
253 a value of 255 means that the pixel is 100% opaque.
254 The constants ::wxIMAGE_ALPHA_TRANSPARENT and ::wxIMAGE_ALPHA_OPAQUE can be
255 used to indicate those values in a more readable form.
257 Unlike RGB data, not all images have an alpha channel and before using
258 wxImage::GetAlpha you should check if this image contains an alpha channel
259 with wxImage::HasAlpha. Note that currently only the PNG format has full
260 alpha channel support so only the images loaded from PNG files can have
261 alpha and, if you initialize the image alpha channel yourself using
262 wxImage::SetAlpha, you should save it in PNG format to avoid losing it.
265 @section image_handlers Available image handlers
267 The following image handlers are available.
268 wxBMPHandler is always installed by default.
269 To use other image formats, install the appropriate handler with
270 wxImage::AddHandler or call ::wxInitAllImageHandlers().
272 - wxBMPHandler: For loading and saving, always installed.
273 - wxPNGHandler: For loading (including alpha support) and saving.
274 - wxJPEGHandler: For loading and saving.
275 - wxGIFHandler: Only for loading, due to legal issues.
276 - wxPCXHandler: For loading and saving (see below).
277 - wxPNMHandler: For loading and saving (see below).
278 - wxTIFFHandler: For loading and saving.
279 - wxTGAHandler: For loading only.
280 - wxIFFHandler: For loading only.
281 - wxXPMHandler: For loading and saving.
282 - wxICOHandler: For loading and saving.
283 - wxCURHandler: For loading and saving.
284 - wxANIHandler: For loading only.
286 When saving in PCX format, wxPCXHandler will count the number of different
287 colours in the image; if there are 256 or less colours, it will save as 8 bit,
288 else it will save as 24 bit.
290 Loading PNMs only works for ASCII or raw RGB images.
291 When saving in PNM format, wxPNMHandler will always save as raw RGB.
300 @see wxBitmap, wxInitAllImageHandlers(), wxPixelData
302 class wxImage
: public wxObject
306 A simple class which stores red, green and blue values as 8 bit unsigned integers
307 in the range of 0-255.
313 Constructor for RGBValue, an object that contains values for red, green
314 and blue which represent the value of a color.
316 It is used by wxImage::HSVtoRGB and wxImage::RGBtoHSV, which convert
317 between HSV color space and RGB color space.
319 RGBValue(unsigned char r
=0, unsigned char g
=0, unsigned char b
=0);
323 A simple class which stores hue, saturation and value as doubles in the range 0.0-1.0.
329 Constructor for HSVValue, an object that contains values for hue, saturation
330 and value which represent the value of a color.
332 It is used by wxImage::HSVtoRGB() and wxImage::RGBtoHSV(), which convert
333 between HSV color space and RGB color space.
335 HSVValue(double h
=0.0, double s
=0.0, double v
=0.0);
339 Creates an empty wxImage object without an alpha channel.
344 Creates an image with the given size and clears it if requested.
346 Does not create an alpha channel.
349 Specifies the width of the image.
351 Specifies the height of the image.
353 If @true, initialize the image to black.
355 wxImage(int width
, int height
, bool clear
= true);
360 wxImage(const wxSize
& sz
, bool clear
= true);
363 Creates an image from data in memory. If @a static_data is @false
364 then the wxImage will take ownership of the data and free it
365 afterwards. For this, it has to be allocated with @e malloc.
368 Specifies the width of the image.
370 Specifies the height of the image.
372 A pointer to RGB data
374 Indicates if the data should be free'd after use
377 wxImage(int width
, int height
, unsigned char* data
, bool static_data
= false);
382 wxImage(const wxSize
& sz
, unsigned char* data
, bool static_data
= false);
385 Creates an image from data in memory. If @a static_data is @false
386 then the wxImage will take ownership of the data and free it
387 afterwards. For this, it has to be allocated with @e malloc.
390 Specifies the width of the image.
392 Specifies the height of the image.
394 A pointer to RGB data
396 A pointer to alpha-channel data
398 Indicates if the data should be free'd after use
401 wxImage(int width
, int height
, unsigned char* data
, unsigned char* alpha
,
402 bool static_data
= false );
407 wxImage(const wxSize
& sz
, unsigned char* data
, unsigned char* data
, unsigned char* alpha
,
408 bool static_data
= false);
411 Creates an image from XPM data.
414 A pointer to XPM image data.
416 wxImage(const char* const* xpmData
);
419 Creates an image from a file.
422 Name of the file from which to load the image.
424 May be one of the following:
425 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
426 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
427 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
428 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
429 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
430 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
431 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
432 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
433 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
434 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
435 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
436 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
437 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
439 Index of the image to load in the case that the image file contains
440 multiple images. This is only used by GIF, ICO and TIFF handlers.
441 The default value (-1) means "choose the default image" and is
442 interpreted as the first image (index=0) by the GIF and TIFF handler
443 and as the largest and most colourful one by the ICO handler.
445 @remarks Depending on how wxWidgets has been configured and by which
446 handlers have been loaded, not all formats may be available.
447 Any handler other than BMP must be previously initialized with
448 wxImage::AddHandler or wxInitAllImageHandlers.
451 You can use GetOptionInt() to get the hotspot when loading cursor files:
453 int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
454 int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
459 wxImage(const wxString
& name
, wxBitmapType type
= wxBITMAP_TYPE_ANY
, int index
= -1);
462 Creates an image from a file using MIME-types to specify the type.
465 Name of the file from which to load the image.
467 MIME type string (for example 'image/jpeg')
469 See description in wxImage(const wxString&, wxBitmapType, int) overload.
471 wxImage(const wxString
& name
, const wxString
& mimetype
, int index
= -1);
474 Creates an image from a stream.
477 Opened input stream from which to load the image. Currently,
478 the stream must support seeking.
480 See description in wxImage(const wxString&, wxBitmapType, int) overload.
482 See description in wxImage(const wxString&, wxBitmapType, int) overload.
484 wxImage(wxInputStream
& stream
, wxBitmapType type
= wxBITMAP_TYPE_ANY
, int index
= -1);
487 Creates an image from a stream using MIME-types to specify the type.
490 Opened input stream from which to load the image. Currently,
491 the stream must support seeking.
493 MIME type string (for example 'image/jpeg')
495 See description in wxImage(const wxString&, wxBitmapType, int) overload.
497 wxImage(wxInputStream
& stream
, const wxString
& mimetype
, int index
= -1);
502 See @ref overview_refcount_destruct "reference-counted object destruction"
510 @name Image creation, initialization and deletion functions
515 Returns an identical copy of this image.
517 wxImage
Copy() const;
520 Creates a fresh image.
521 See wxImage::wxImage(int,int,bool) for more info.
523 @return @true if the call succeeded, @false otherwise.
525 bool Create(int width
, int height
, bool clear
= true);
530 bool Create( const wxSize
& sz
, bool clear
= true );
533 Creates a fresh image.
534 See wxImage::wxImage(int,int,unsigned char*,bool) for more info.
536 @return @true if the call succeeded, @false otherwise.
538 bool Create( int width
, int height
, unsigned char* data
, bool static_data
= false );
543 bool Create( const wxSize
& sz
, unsigned char* data
, bool static_data
= false );
546 Creates a fresh image.
547 See wxImage::wxImage(int,int,unsigned char*,unsigned char*,bool) for more info.
549 @return @true if the call succeeded, @false otherwise.
551 bool Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
= false );
556 bool Create( const wxSize
& sz
, unsigned char* data
, unsigned char* alpha
, bool static_data
= false );
559 Initialize the image data with zeroes (the default) or with the
560 byte value given as @a value.
564 void Clear(unsigned char value
= 0);
567 Destroys the image data.
572 Initializes the image alpha channel data.
574 It is an error to call it if the image already has alpha data.
575 If it doesn't, alpha data will be by default initialized to all pixels
576 being fully opaque. But if the image has a mask colour, all mask pixels
577 will be completely transparent.
585 @name Image manipulation functions
590 Blurs the image in both horizontal and vertical directions by the
591 specified pixel @a blurRadius. This should not be used when using
592 a single mask colour for transparency.
594 @see BlurHorizontal(), BlurVertical()
596 wxImage
Blur(int blurRadius
) const;
599 Blurs the image in the horizontal direction only. This should not be used
600 when using a single mask colour for transparency.
602 @see Blur(), BlurVertical()
604 wxImage
BlurHorizontal(int blurRadius
) const;
607 Blurs the image in the vertical direction only. This should not be used
608 when using a single mask colour for transparency.
610 @see Blur(), BlurHorizontal()
612 wxImage
BlurVertical(int blurRadius
) const;
615 Returns a mirrored copy of the image.
616 The parameter @a horizontally indicates the orientation.
618 wxImage
Mirror(bool horizontally
= true) const;
621 Copy the data of the given @a image to the specified position in this image.
623 void Paste(const wxImage
& image
, int x
, int y
);
626 Replaces the colour specified by @e r1,g1,b1 by the colour @e r2,g2,b2.
628 void Replace(unsigned char r1
, unsigned char g1
,
629 unsigned char b1
, unsigned char r2
,
630 unsigned char g2
, unsigned char b2
);
633 Changes the size of the image in-place by scaling it: after a call to this
634 function,the image will have the given width and height.
636 For a description of the @a quality parameter, see the Scale() function.
637 Returns the (modified) image itself.
641 wxImage
& Rescale(int width
, int height
,
642 int quality
= wxIMAGE_QUALITY_NORMAL
);
645 Changes the size of the image in-place without scaling it by adding either a
646 border with the given colour or cropping as necessary.
648 The image is pasted into a new image with the given @a size and background
649 colour at the position @a pos relative to the upper left of the new image.
651 If @a red = green = blue = -1 then use either the current mask colour
652 if set or find, use, and set a suitable mask colour for any newly exposed
655 @return The (modified) image itself.
659 wxImage
& Resize(const wxSize
& size
, const wxPoint
& pos
, int red
= -1,
660 int green
= -1, int blue
= -1);
663 Rotates the image about the given point, by @a angle radians.
665 Passing @true to @a interpolating results in better image quality, but is slower.
667 If the image has a mask, then the mask colour is used for the uncovered
668 pixels in the rotated image background. Else, black (rgb 0, 0, 0) will be used.
670 Returns the rotated image, leaving this image intact.
672 wxImage
Rotate(double angle
, const wxPoint
& rotationCentre
,
673 bool interpolating
= true,
674 wxPoint
* offsetAfterRotation
= NULL
) const;
677 Returns a copy of the image rotated 90 degrees in the direction
678 indicated by @a clockwise.
680 wxImage
Rotate90(bool clockwise
= true) const;
683 Rotates the hue of each pixel in the image by @e angle, which is a double in
684 the range of -1.0 to +1.0, where -1.0 corresponds to -360 degrees and +1.0
685 corresponds to +360 degrees.
687 void RotateHue(double angle
);
690 Returns a scaled version of the image.
692 This is also useful for scaling bitmaps in general as the only other way
693 to scale bitmaps is to blit a wxMemoryDC into another wxMemoryDC.
695 The parameter @a quality determines what method to use for resampling the image.
696 Can be one of the following:
697 - wxIMAGE_QUALITY_NORMAL: Uses the normal default scaling method of pixel
699 - wxIMAGE_QUALITY_HIGH: Uses bicubic and box averaging resampling methods
700 for upsampling and downsampling respectively
702 It should be noted that although using @c wxIMAGE_QUALITY_HIGH produces much nicer
703 looking results it is a slower method. Downsampling will use the box averaging
704 method which seems to operate very fast. If you are upsampling larger images using
705 this method you will most likely notice that it is a bit slower and in extreme
706 cases it will be quite substantially slower as the bicubic algorithm has to process a
709 It should also be noted that the high quality scaling may not work as expected
710 when using a single mask colour for transparency, as the scaling will blur the
711 image and will therefore remove the mask partially. Using the alpha channel
716 // get the bitmap from somewhere
719 // rescale it to have size of 32*32
720 if ( bmp.GetWidth() != 32 || bmp.GetHeight() != 32 )
722 wxImage image = bmp.ConvertToImage();
723 bmp = wxBitmap(image.Scale(32, 32));
725 // another possibility:
726 image.Rescale(32, 32);
733 wxImage
Scale(int width
, int height
,
734 int quality
= wxIMAGE_QUALITY_NORMAL
) const;
737 Returns a resized version of this image without scaling it by adding either a
738 border with the given colour or cropping as necessary.
740 The image is pasted into a new image with the given @a size and background
741 colour at the position @a pos relative to the upper left of the new image.
743 If @a red = green = blue = -1 then the areas of the larger image not covered
744 by this image are made transparent by filling them with the image mask colour
745 (which will be allocated automatically if it isn't currently set).
747 Otherwise, the areas will be filled with the colour with the specified RGB components.
751 wxImage
Size(const wxSize
& size
, const wxPoint
& pos
, int red
= -1,
752 int green
= -1, int blue
= -1) const;
758 @name Conversion functions
763 If the image has alpha channel, this method converts it to mask.
765 If the image has an alpha channel, all pixels with alpha value less
766 than @a threshold are replaced with the mask colour and the alpha
767 channel is removed. Otherwise nothing is done.
769 The mask colour is chosen automatically using
770 FindFirstUnusedColour() by this function, see the overload below if you
771 this is not appropriate.
773 @return @false if FindFirstUnusedColour returns @false, @true otherwise.
775 bool ConvertAlphaToMask(unsigned char threshold
= wxIMAGE_ALPHA_THRESHOLD
);
778 If the image has alpha channel, this method converts it to mask using
779 the specified colour as the mask colour.
781 If the image has an alpha channel, all pixels with alpha value less
782 than @a threshold are replaced with the mask colour and the alpha
783 channel is removed. Otherwise nothing is done.
788 The red component of the mask colour.
790 The green component of the mask colour.
792 The blue component of the mask colour.
794 Pixels with alpha channel values below the given threshold are
795 considered to be transparent, i.e. the corresponding mask pixels
796 are set. Pixels with the alpha values above the threshold are
797 considered to be opaque.
800 void ConvertAlphaToMask(unsigned char mr
, unsigned char mg
, unsigned char mb
,
801 unsigned char threshold
= wxIMAGE_ALPHA_THRESHOLD
);
804 Returns a greyscale version of the image.
806 The returned image uses the luminance component of the original to
807 calculate the greyscale. Defaults to using the standard ITU-T BT.601
808 when converting to YUV, where every pixel equals
809 (R * @a lr) + (G * @a lg) + (B * @a lb).
811 wxImage
ConvertToGreyscale(double lr
= 0.299, double lg
= 0.587, double lb
= 1.114) const;
814 Returns monochromatic version of the image.
816 The returned image has white colour where the original has @e (r,g,b)
817 colour and black colour everywhere else.
819 wxImage
ConvertToMono(unsigned char r
, unsigned char g
, unsigned char b
) const;
825 @name Miscellaneous functions
830 Computes the histogram of the image. @a histogram is a reference to
831 wxImageHistogram object. wxImageHistogram is a specialization of
832 wxHashMap "template" and is defined as follows:
835 class WXDLLEXPORT wxImageHistogramEntry
838 wxImageHistogramEntry() : index(0), value(0) {}
843 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
844 wxIntegerHash, wxIntegerEqual,
848 @return Returns number of colours in the histogram.
850 unsigned long ComputeHistogram(wxImageHistogram
& histogram
) const;
853 Finds the first colour that is never used in the image.
854 The search begins at given initial colour and continues by increasing
855 R, G and B components (in this order) by 1 until an unused colour is
856 found or the colour space exhausted.
858 The parameters @a r, @a g, @a b are pointers to variables to save the colour.
860 The parameters @a startR, @a startG, @a startB define the initial values
862 The returned colour will have RGB values equal to or greater than these.
864 @return Returns @false if there is no unused colour left, @true on success.
867 This method involves computing the histogram, which is a
868 computationally intensive operation.
870 bool FindFirstUnusedColour(unsigned char* r
, unsigned char* g
,
871 unsigned char* b
, unsigned char startR
= 1,
872 unsigned char startG
= 0,
873 unsigned char startB
= 0) const;
876 Assignment operator, using @ref overview_refcount "reference counting".
881 @return Returns 'this' object.
883 wxImage
& operator=(const wxImage
& image
);
894 Returns pointer to the array storing the alpha values for this image.
896 This pointer is @NULL for the images without the alpha channel. If the image
897 does have it, this pointer may be used to directly manipulate the alpha values
898 which are stored as the RGB ones.
900 unsigned char* GetAlpha() const;
903 Returns the image data as an array.
905 This is most often used when doing direct image manipulation.
906 The return value points to an array of characters in RGBRGBRGB... format
907 in the top-to-bottom, left-to-right order, that is the first RGB triplet
908 corresponds to the pixel first pixel of the first row, the second one ---
909 to the second pixel of the first row and so on until the end of the first
910 row, with second row following after it and so on.
912 You should not delete the returned pointer nor pass it to SetData().
914 unsigned char* GetData() const;
917 Return alpha value at given pixel location.
919 unsigned char GetAlpha(int x
, int y
) const;
922 Returns the red intensity at the given coordinate.
924 unsigned char GetRed(int x
, int y
) const;
927 Returns the green intensity at the given coordinate.
929 unsigned char GetGreen(int x
, int y
) const;
932 Returns the blue intensity at the given coordinate.
934 unsigned char GetBlue(int x
, int y
) const;
937 Gets the red value of the mask colour.
939 unsigned char GetMaskRed() const;
942 Gets the green value of the mask colour.
944 unsigned char GetMaskGreen() const;
947 Gets the blue value of the mask colour.
949 unsigned char GetMaskBlue() const;
952 Gets the width of the image in pixels.
954 @see GetHeight(), GetSize()
956 int GetWidth() const;
959 Gets the height of the image in pixels.
961 @see GetWidth(), GetSize()
963 int GetHeight() const;
966 Returns the size of the image in pixels.
970 @see GetHeight(), GetWidth()
972 wxSize
GetSize() const;
975 Gets a user-defined string-valued option.
977 Currently the only defined string option is
978 @li @c wxIMAGE_OPTION_FILENAME: The name of the file from which the image
982 The name of the option, case-insensitive.
984 The value of the option or an empty string if not found. Use
985 HasOption() if an empty string can be a valid option value.
987 @see SetOption(), GetOptionInt(), HasOption()
989 wxString
GetOption(const wxString
& name
) const;
992 Gets a user-defined integer-valued option.
994 The function is case-insensitive to @a name.
995 If the given option is not present, the function returns 0.
996 Use HasOption() is 0 is a possibly valid value for the option.
999 @li @c wxIMAGE_OPTION_MAX_WIDTH and @c wxIMAGE_OPTION_MAX_HEIGHT: If either
1000 of these options is specified, the loaded image will be scaled down
1001 (preserving its aspect ratio) so that its width is less than the
1002 max width given if it is not 0 @em and its height is less than the
1003 max height given if it is not 0. This is typically used for loading
1004 thumbnails and the advantage of using these options compared to
1005 calling Rescale() after loading is that some handlers (only JPEG
1006 one right now) support rescaling the image during loading which is
1007 vastly more efficient than loading the entire huge image and
1008 rescaling it later (if these options are not supported by the
1009 handler, this is still what happens however). These options must be
1010 set before calling LoadFile() to have any effect.
1012 @li @c wxIMAGE_OPTION_QUALITY: JPEG quality used when saving. This is an
1013 integer in 0..100 range with 0 meaning very poor and 100 excellent
1014 (but very badly compressed). This option is currently ignored for
1017 @li @c wxIMAGE_OPTION_RESOLUTIONUNIT: The value of this option determines
1018 whether the resolution of the image is specified in centimetres or
1019 inches, see wxImageResolution enum elements.
1021 @li @c wxIMAGE_OPTION_RESOLUTION, @c wxIMAGE_OPTION_RESOLUTIONX and
1022 @c wxIMAGE_OPTION_RESOLUTIONY: These options define the resolution of
1023 the image in the units corresponding to @c wxIMAGE_OPTION_RESOLUTIONUNIT
1024 options value. The first option can be set before saving the image
1025 to set both horizontal and vertical resolution to the same value.
1026 The X and Y options are set by the image handlers if they support
1027 the image resolution (currently BMP, JPEG and TIFF handlers do) and
1028 the image provides the resolution information and can be queried
1029 after loading the image.
1031 Options specific to wxPNGHandler:
1032 @li @c wxIMAGE_OPTION_PNG_FORMAT: Format for saving a PNG file, see
1033 wxImagePNGType for the supported values.
1034 @li @c wxIMAGE_OPTION_PNG_BITDEPTH: Bit depth for every channel (R/G/B/A).
1035 @li @c wxIMAGE_OPTION_PNG_FILTER: Filter for saving a PNG file, see libpng
1036 (http://www.libpng.org/pub/png/libpng-1.2.5-manual.html) for possible values
1037 (e.g. PNG_FILTER_NONE, PNG_FILTER_SUB, PNG_FILTER_UP, etc).
1038 @li @c wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL: Compression level (0..9) for
1039 saving a PNG file. An high value creates smaller-but-slower PNG file.
1040 Note that unlike other formats (e.g. JPEG) the PNG format is always
1041 lossless and thus this compression level doesn't tradeoff the image
1043 @li @c wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL: Compression memory usage
1044 level (1..9) for saving a PNG file. An high value means the saving
1045 process consumes more memory, but may create smaller PNG file.
1046 @li @c wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY: Possible values are 0 for
1047 default strategy, 1 for filter, and 2 for Huffman-only.
1048 You can use OptiPNG (http://optipng.sourceforge.net/) to get a suitable
1049 value for your application.
1050 @li @c wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE: Internal buffer size
1051 (in bytes) for saving a PNG file. Ideally this should be as big as
1052 the resulting PNG file. Use this option if your application produces
1053 images with small size variation.
1056 The name of the option, case-insensitive.
1058 The value of the option or 0 if not found.
1059 Use HasOption() if 0 can be a valid option value.
1061 @see SetOption(), GetOption()
1063 int GetOptionInt(const wxString
& name
) const;
1066 Get the current mask colour or find a suitable unused colour that could be
1067 used as a mask colour. Returns @true if the image currently has a mask.
1069 bool GetOrFindMaskColour(unsigned char* r
, unsigned char* g
,
1070 unsigned char* b
) const;
1073 Returns the palette associated with the image.
1074 Currently the palette is only used when converting to wxBitmap under Windows.
1076 Some of the wxImage handlers have been modified to set the palette if
1077 one exists in the image file (usually 256 or less colour images in
1080 const wxPalette
& GetPalette() const;
1083 Returns a sub image of the current one as long as the rect belongs entirely
1086 wxImage
GetSubImage(const wxRect
& rect
) const;
1089 Gets the type of image found by LoadFile() or specified with SaveFile().
1093 wxBitmapType
GetType() const;
1096 Returns @true if this image has alpha channel, @false otherwise.
1098 @see GetAlpha(), SetAlpha()
1100 bool HasAlpha() const;
1103 Returns @true if there is a mask active, @false otherwise.
1105 bool HasMask() const;
1108 Returns @true if the given option is present.
1109 The function is case-insensitive to @a name.
1111 The lists of the currently supported options are in GetOption() and
1112 GetOptionInt() function docs.
1114 @see SetOption(), GetOption(), GetOptionInt()
1116 bool HasOption(const wxString
& name
) const;
1119 Returns @true if image data is present.
1124 Returns @true if the given pixel is transparent, i.e. either has the mask
1125 colour if this image has a mask or if this image has alpha channel and alpha
1126 value of this pixel is strictly less than @a threshold.
1128 bool IsTransparent(int x
, int y
,
1129 unsigned char threshold
= wxIMAGE_ALPHA_THRESHOLD
) const;
1135 @name Loading and saving functions
1140 Loads an image from an input stream.
1143 Opened input stream from which to load the image.
1144 Currently, the stream must support seeking.
1146 May be one of the following:
1147 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
1148 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
1149 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
1150 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
1151 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
1152 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
1153 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
1154 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
1155 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
1156 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
1157 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
1158 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
1159 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
1161 Index of the image to load in the case that the image file contains
1162 multiple images. This is only used by GIF, ICO and TIFF handlers.
1163 The default value (-1) means "choose the default image" and is
1164 interpreted as the first image (index=0) by the GIF and TIFF handler
1165 and as the largest and most colourful one by the ICO handler.
1167 @return @true if the operation succeeded, @false otherwise.
1168 If the optional index parameter is out of range, @false is
1169 returned and a call to wxLogError() takes place.
1171 @remarks Depending on how wxWidgets has been configured, not all formats
1175 You can use GetOptionInt() to get the hotspot when loading cursor files:
1177 int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
1178 int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
1183 virtual bool LoadFile(wxInputStream
& stream
,
1184 wxBitmapType type
= wxBITMAP_TYPE_ANY
,
1188 Loads an image from a file.
1189 If no handler type is provided, the library will try to autodetect the format.
1192 Name of the file from which to load the image.
1194 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1196 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1198 virtual bool LoadFile(const wxString
& name
,
1199 wxBitmapType type
= wxBITMAP_TYPE_ANY
,
1203 Loads an image from a file.
1204 If no handler type is provided, the library will try to autodetect the format.
1207 Name of the file from which to load the image.
1209 MIME type string (for example 'image/jpeg')
1211 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1213 virtual bool LoadFile(const wxString
& name
, const wxString
& mimetype
,
1217 Loads an image from an input stream.
1220 Opened input stream from which to load the image.
1221 Currently, the stream must support seeking.
1223 MIME type string (for example 'image/jpeg')
1225 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1227 virtual bool LoadFile(wxInputStream
& stream
, const wxString
& mimetype
,
1231 Saves an image in the given stream.
1234 Opened output stream to save the image to.
1238 @return @true if the operation succeeded, @false otherwise.
1240 @remarks Depending on how wxWidgets has been configured, not all formats
1244 You can use SetOption() to set the hotspot when saving an image
1245 into a cursor file (default hotspot is in the centre of the image):
1247 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotspotX);
1248 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotspotY);
1253 virtual bool SaveFile(wxOutputStream
& stream
,
1254 const wxString
& mimetype
) const;
1257 Saves an image in the named file.
1260 Name of the file to save the image to.
1262 Currently these types can be used:
1263 @li wxBITMAP_TYPE_BMP: Save a BMP image file.
1264 @li wxBITMAP_TYPE_JPEG: Save a JPEG image file.
1265 @li wxBITMAP_TYPE_PNG: Save a PNG image file.
1266 @li wxBITMAP_TYPE_PCX: Save a PCX image file
1267 (tries to save as 8-bit if possible, falls back to 24-bit otherwise).
1268 @li wxBITMAP_TYPE_PNM: Save a PNM image file (as raw RGB always).
1269 @li wxBITMAP_TYPE_TIFF: Save a TIFF image file.
1270 @li wxBITMAP_TYPE_XPM: Save a XPM image file.
1271 @li wxBITMAP_TYPE_ICO: Save a Windows icon file (ICO).
1272 The size may be up to 255 wide by 127 high. A single image is saved
1273 in 8 colors at the size supplied.
1274 @li wxBITMAP_TYPE_CUR: Save a Windows cursor file (CUR).
1276 virtual bool SaveFile(const wxString
& name
, wxBitmapType type
) const;
1279 Saves an image in the named file.
1282 Name of the file to save the image to.
1286 virtual bool SaveFile(const wxString
& name
, const wxString
& mimetype
) const;
1289 Saves an image in the named file.
1291 File type is determined from the extension of the file name.
1292 Note that this function may fail if the extension is not recognized!
1293 You can use one of the forms above to save images to files with
1294 non-standard extensions.
1297 Name of the file to save the image to.
1299 virtual bool SaveFile(const wxString
& name
) const;
1302 Saves an image in the given stream.
1305 Opened output stream to save the image to.
1309 virtual bool SaveFile(wxOutputStream
& stream
, wxBitmapType type
) const;
1321 This function is similar to SetData() and has similar restrictions.
1323 The pointer passed to it may however be @NULL in which case the function
1324 will allocate the alpha array internally -- this is useful to add alpha
1325 channel data to an image which doesn't have any.
1327 If the pointer is not @NULL, it must have one byte for each image pixel
1328 and be allocated with malloc().
1329 wxImage takes ownership of the pointer and will free it unless @a static_data
1330 parameter is set to @true -- in this case the caller should do it.
1332 void SetAlpha(unsigned char* alpha
= NULL
,
1333 bool static_data
= false);
1336 Sets the alpha value for the given pixel.
1337 This function should only be called if the image has alpha channel data,
1338 use HasAlpha() to check for this.
1340 void SetAlpha(int x
, int y
, unsigned char alpha
);
1343 Sets the image data without performing checks.
1345 The data given must have the size (width*height*3) or results will be
1346 unexpected. Don't use this method if you aren't sure you know what you
1349 The data must have been allocated with @c malloc(), @b NOT with
1352 If @a static_data is @false, after this call the pointer to the data is
1353 owned by the wxImage object, that will be responsible for deleting it.
1354 Do not pass to this function a pointer obtained through GetData().
1356 void SetData(unsigned char* data
, bool static_data
= false);
1361 void SetData(unsigned char* data
, int new_width
, int new_height
,
1362 bool static_data
= false);
1365 Specifies whether there is a mask or not.
1367 The area of the mask is determined by the current mask colour.
1369 void SetMask(bool hasMask
= true);
1372 Sets the mask colour for this image (and tells the image to use the mask).
1374 void SetMaskColour(unsigned char red
, unsigned char green
,
1375 unsigned char blue
);
1378 Sets image's mask so that the pixels that have RGB value of mr,mg,mb in
1379 mask will be masked in the image.
1381 This is done by first finding an unused colour in the image, setting
1382 this colour as the mask colour and then using this colour to draw all
1383 pixels in the image who corresponding pixel in mask has given RGB value.
1385 The parameter @a mask is the mask image to extract mask shape from.
1386 It must have the same dimensions as the image.
1388 The parameters @a mr, @a mg, @a mb are the RGB values of the pixels in
1389 mask that will be used to create the mask.
1391 @return Returns @false if mask does not have same dimensions as the image
1392 or if there is no unused colour left. Returns @true if the mask
1393 was successfully applied.
1396 Note that this method involves computing the histogram, which is a
1397 computationally intensive operation.
1399 bool SetMaskFromImage(const wxImage
& mask
, unsigned char mr
,
1404 Sets a user-defined option. The function is case-insensitive to @a name.
1406 For example, when saving as a JPEG file, the option @b quality is
1407 used, which is a number between 0 and 100 (0 is terrible, 100 is very good).
1409 The lists of the currently supported options are in GetOption() and
1410 GetOptionInt() function docs.
1412 @see GetOption(), GetOptionInt(), HasOption()
1414 void SetOption(const wxString
& name
, const wxString
& value
);
1419 void SetOption(const wxString
& name
, int value
);
1422 Associates a palette with the image.
1424 The palette may be used when converting wxImage to wxBitmap (MSW only at present)
1425 or in file save operations (none as yet).
1427 void SetPalette(const wxPalette
& palette
);
1430 Sets the colour of the pixels within the given rectangle.
1432 This routine performs bounds-checks for the coordinate so it can be considered
1433 a safe way to manipulate the data.
1435 void SetRGB(const wxRect
& rect
,
1437 unsigned char green
,
1438 unsigned char blue
);
1441 Set the type of image returned by GetType().
1443 This method is mostly used internally by the library but can also be
1444 called from the user code if the image was created from data in the
1445 given bitmap format without using LoadFile() (which would set the type
1446 correctly automatically).
1448 Notice that the image must be created before this function is called.
1453 One of bitmap type constants, @c wxBITMAP_TYPE_INVALID is a valid
1454 value for it and can be used to reset the bitmap type to default
1455 but @c wxBITMAP_TYPE_MAX is not allowed here.
1457 void SetType(wxBitmapType type
);
1464 @name Handler management functions
1469 Register an image handler.
1470 See @ref image_handlers for a list of the available handlers.
1472 static void AddHandler(wxImageHandler
* handler
);
1475 Deletes all image handlers.
1476 This function is called by wxWidgets on exit.
1478 static void CleanUpHandlers();
1481 Finds the handler with the given name.
1486 @return A pointer to the handler if found, @NULL otherwise.
1490 static wxImageHandler
* FindHandler(const wxString
& name
);
1493 Finds the handler associated with the given extension and type.
1496 The file extension, such as "bmp".
1498 The image type; one of the ::wxBitmapType values.
1500 @return A pointer to the handler if found, @NULL otherwise.
1504 static wxImageHandler
* FindHandler(const wxString
& extension
,
1505 wxBitmapType imageType
);
1508 Finds the handler associated with the given image type.
1511 The image type; one of the ::wxBitmapType values.
1513 @return A pointer to the handler if found, @NULL otherwise.
1517 static wxImageHandler
* FindHandler(wxBitmapType imageType
);
1520 Finds the handler associated with the given MIME type.
1525 @return A pointer to the handler if found, @NULL otherwise.
1529 static wxImageHandler
* FindHandlerMime(const wxString
& mimetype
);
1532 Returns the static list of image format handlers.
1536 static wxList
& GetHandlers();
1539 Internal use only. Adds standard image format handlers.
1540 It only install wxBMPHandler for the time being, which is used by wxBitmap.
1542 This function is called by wxWidgets on startup, and shouldn't be called by
1545 @see wxImageHandler, wxInitAllImageHandlers(), wxQuantize
1547 static void InitStandardHandlers();
1550 Adds a handler at the start of the static list of format handlers.
1553 A new image format handler object. There is usually only one instance
1554 of a given handler class in an application session.
1558 static void InsertHandler(wxImageHandler
* handler
);
1561 Finds the handler with the given name, and removes it.
1562 The handler is not deleted.
1567 @return @true if the handler was found and removed, @false otherwise.
1571 static bool RemoveHandler(const wxString
& name
);
1577 Returns @true if the current image handlers can read this file
1579 static bool CanRead(const wxString
& filename
);
1583 If the image file contains more than one image and the image handler is
1584 capable of retrieving these individually, this function will return the
1585 number of available images.
1587 For the overload taking the parameter @a filename, that's the name
1588 of the file to query.
1589 For the overload taking the parameter @a stream, that's the ppened input
1590 stream with image data. Currently, the stream must support seeking.
1592 The parameter @a type may be one of the following values:
1593 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
1594 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
1595 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
1596 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
1597 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
1598 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
1599 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
1600 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
1601 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
1602 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
1603 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
1604 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
1605 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
1607 @return Number of available images. For most image handlers, this is 1
1608 (exceptions are TIFF and ICO formats as well as animated GIFs
1609 for which this function returns the number of frames in the
1612 static int GetImageCount(const wxString
& filename
,
1613 wxBitmapType type
= wxBITMAP_TYPE_ANY
);
1614 static int GetImageCount(wxInputStream
& stream
,
1615 wxBitmapType type
= wxBITMAP_TYPE_ANY
);
1619 Iterates all registered wxImageHandler objects, and returns a string containing
1620 file extension masks suitable for passing to file open/save dialog boxes.
1622 @return The format of the returned string is @c "(*.ext1;*.ext2)|*.ext1;*.ext2".
1623 It is usually a good idea to prepend a description before passing
1624 the result to the dialog.
1627 wxFileDialog FileDlg( this, "Choose Image", ::wxGetCwd(), "",
1628 _("Image Files ") + wxImage::GetImageExtWildcard(),
1634 static wxString
GetImageExtWildcard();
1637 Converts a color in RGB color space to HSV color space.
1639 static wxImage::HSVValue
RGBtoHSV(const wxImage::RGBValue
& rgb
);
1642 Converts a color in HSV color space to RGB color space.
1644 static wxImage::RGBValue
HSVtoRGB(const wxImage::HSVValue
& hsv
);
1648 An instance of an empty image without an alpha channel.
1650 wxImage wxNullImage
;
1653 // ============================================================================
1654 // Global functions/macros
1655 // ============================================================================
1657 /** @addtogroup group_funcmacro_appinitterm */
1661 Initializes all available image handlers. For a list of available handlers,
1663 If you don't need/want all image handlers loaded
1665 @see wxImage, wxImageHandler
1669 void wxInitAllImageHandlers();