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).
114 virtual int GetImageCount(wxInputStream
& stream
);
117 Gets the MIME type associated with this handler.
119 const wxString
& GetMimeType() const;
122 Gets the name of this handler.
124 const wxString
& GetName() const;
127 Gets the image type associated with this handler.
129 wxBitmapType
GetType() const;
132 Loads a image from a stream, putting the resulting data into @a image.
134 If the image file contains more than one image and the image handler is
135 capable of retrieving these individually, @a index indicates which image
136 to read from the stream.
139 The image object which is to be affected by this operation.
141 Opened input stream for reading image data.
143 If set to @true, errors reported by the image handler will produce
146 The index of the image in the file (starting from zero).
148 @return @true if the operation succeeded, @false otherwise.
150 @see wxImage::LoadFile, wxImage::SaveFile, SaveFile()
152 virtual bool LoadFile(wxImage
* image
, wxInputStream
& stream
,
153 bool verbose
= true, int index
= -1);
156 Saves a image in the output stream.
159 The image object which is to be affected by this operation.
161 Opened output stream for writing the data.
163 If set to @true, errors reported by the image handler will produce
166 @return @true if the operation succeeded, @false otherwise.
168 @see wxImage::LoadFile, wxImage::SaveFile, LoadFile()
170 virtual bool SaveFile(wxImage
* image
, wxOutputStream
& stream
,
171 bool verbose
= true);
174 Sets the preferred file extension associated with this handler.
177 File extension without leading dot.
179 @see SetAltExtensions()
181 void SetExtension(const wxString
& extension
);
184 Sets the alternative file extensions associated with this handler.
187 Array of file extensions.
193 void SetAltExtensions(const wxArrayString
& extensions
);
196 Sets the handler MIME type.
201 void SetMimeType(const wxString
& mimetype
);
204 Sets the handler name.
209 void SetName(const wxString
& name
);
214 Constant used to indicate the alpha value conventionally defined as
215 the complete transparency.
217 const unsigned char wxIMAGE_ALPHA_TRANSPARENT
= 0;
220 Constant used to indicate the alpha value conventionally defined as
221 the complete opacity.
223 const unsigned char wxIMAGE_ALPHA_OPAQUE
= 0xff;
228 This class encapsulates a platform-independent image.
230 An image can be created from data, or using wxBitmap::ConvertToImage.
231 An image can be loaded from a file in a variety of formats, and is extensible
232 to new formats via image format handlers. Functions are available to set and
233 get image bits, so it can be used for basic image manipulation.
235 A wxImage cannot (currently) be drawn directly to a wxDC.
236 Instead, a platform-specific wxBitmap object must be created from it using
237 the wxBitmap::wxBitmap(wxImage,int depth) constructor.
238 This bitmap can then be drawn in a device context, using wxDC::DrawBitmap.
240 One colour value of the image may be used as a mask colour which will lead to
241 the automatic creation of a wxMask object associated to the bitmap object.
244 @section image_alpha Alpha channel support
246 Starting from wxWidgets 2.5.0 wxImage supports alpha channel data, that is
247 in addition to a byte for the red, green and blue colour components for each
248 pixel it also stores a byte representing the pixel opacity.
250 An alpha value of 0 corresponds to a transparent pixel (null opacity) while
251 a value of 255 means that the pixel is 100% opaque.
252 The constants ::wxIMAGE_ALPHA_TRANSPARENT and ::wxIMAGE_ALPHA_OPAQUE can be
253 used to indicate those values in a more readable form.
255 Unlike RGB data, not all images have an alpha channel and before using
256 wxImage::GetAlpha you should check if this image contains an alpha channel
257 with wxImage::HasAlpha. Note that currently only the PNG format has full
258 alpha channel support so only the images loaded from PNG files can have
259 alpha and, if you initialize the image alpha channel yourself using
260 wxImage::SetAlpha, you should save it in PNG format to avoid losing it.
263 @section image_handlers Available image handlers
265 The following image handlers are available.
266 wxBMPHandler is always installed by default.
267 To use other image formats, install the appropriate handler with
268 wxImage::AddHandler or call ::wxInitAllImageHandlers().
270 - wxBMPHandler: For loading and saving, always installed.
271 - wxPNGHandler: For loading (including alpha support) and saving.
272 - wxJPEGHandler: For loading and saving.
273 - wxGIFHandler: Only for loading, due to legal issues.
274 - wxPCXHandler: For loading and saving (see below).
275 - wxPNMHandler: For loading and saving (see below).
276 - wxTIFFHandler: For loading and saving.
277 - wxTGAHandler: For loading only.
278 - wxIFFHandler: For loading only.
279 - wxXPMHandler: For loading and saving.
280 - wxICOHandler: For loading and saving.
281 - wxCURHandler: For loading and saving.
282 - wxANIHandler: For loading only.
284 When saving in PCX format, wxPCXHandler will count the number of different
285 colours in the image; if there are 256 or less colours, it will save as 8 bit,
286 else it will save as 24 bit.
288 Loading PNMs only works for ASCII or raw RGB images.
289 When saving in PNM format, wxPNMHandler will always save as raw RGB.
298 @see wxBitmap, wxInitAllImageHandlers(), wxPixelData
300 class wxImage
: public wxObject
307 Constructor for RGBValue, an object that contains values for red, green
308 and blue which represent the value of a color.
310 It is used by wxImage::HSVtoRGB and wxImage::RGBtoHSV, which converts
311 between HSV color space and RGB color space.
313 RGBValue(unsigned char r
=0, unsigned char g
=0, unsigned char b
=0);
320 Constructor for HSVValue, an object that contains values for hue, saturation
321 and value which represent the value of a color.
323 It is used by wxImage::HSVtoRGB() and wxImage::RGBtoHSV(), which converts
324 between HSV color space and RGB color space.
326 HSVValue(double h
=0.0, double s
=0.0, double v
=0.0);
330 Creates an empty wxImage object without an alpha channel.
335 Creates an image with the given size and clears it if requested.
337 Does not create an alpha channel.
340 Specifies the width of the image.
342 Specifies the height of the image.
344 If @true, initialize the image to black.
346 wxImage(int width
, int height
, bool clear
= true);
349 Creates an image from data in memory. If @a static_data is @false
350 then the wxImage will take ownership of the data and free it
351 afterwards. For this, it has to be allocated with @e malloc.
354 Specifies the width of the image.
356 Specifies the height of the image.
358 A pointer to RGB data
360 Indicates if the data should be free'd after use
363 wxImage(int width
, int height
, unsigned char* data
, bool static_data
= false);
366 Creates an image from data in memory. If @a static_data is @false
367 then the wxImage will take ownership of the data and free it
368 afterwards. For this, it has to be allocated with @e malloc.
371 Specifies the width of the image.
373 Specifies the height of the image.
375 A pointer to RGB data
377 A pointer to alpha-channel data
379 Indicates if the data should be free'd after use
382 wxImage(int width
, int height
, unsigned char* data
, unsigned char* alpha
,
383 bool static_data
= false );
386 Creates an image from XPM data.
389 A pointer to XPM image data.
391 wxImage(const char* const* xpmData
);
394 Creates an image from a file.
397 Name of the file from which to load the image.
399 May be one of the following:
400 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
401 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
402 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
403 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
404 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
405 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
406 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
407 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
408 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
409 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
410 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
411 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
412 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
414 Index of the image to load in the case that the image file contains
415 multiple images. This is only used by GIF, ICO and TIFF handlers.
416 The default value (-1) means "choose the default image" and is
417 interpreted as the first image (index=0) by the GIF and TIFF handler
418 and as the largest and most colourful one by the ICO handler.
420 @remarks Depending on how wxWidgets has been configured and by which
421 handlers have been loaded, not all formats may be available.
422 Any handler other than BMP must be previously initialized with
423 wxImage::AddHandler or wxInitAllImageHandlers.
426 You can use GetOptionInt() to get the hotspot when loading cursor files:
428 int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
429 int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
434 wxImage(const wxString
& name
, wxBitmapType type
= wxBITMAP_TYPE_ANY
, int index
= -1);
437 Creates an image from a file using MIME-types to specify the type.
440 Name of the file from which to load the image.
442 MIME type string (for example 'image/jpeg')
444 See description in wxImage(const wxString&, wxBitmapType, int) overload.
446 wxImage(const wxString
& name
, const wxString
& mimetype
, int index
= -1);
449 Creates an image from a stream.
452 Opened input stream from which to load the image. Currently,
453 the stream must support seeking.
455 See description in wxImage(const wxString&, wxBitmapType, int) overload.
457 See description in wxImage(const wxString&, wxBitmapType, int) overload.
459 wxImage(wxInputStream
& stream
, wxBitmapType type
= wxBITMAP_TYPE_ANY
, int index
= -1);
462 Creates an image from a stream using MIME-types to specify the type.
465 Opened input stream from which to load the image. Currently,
466 the stream must support seeking.
468 MIME type string (for example 'image/jpeg')
470 See description in wxImage(const wxString&, wxBitmapType, int) overload.
472 wxImage(wxInputStream
& stream
, const wxString
& mimetype
, int index
= -1);
478 See @ref overview_refcount_destruct "reference-counted object destruction"
484 Register an image handler.
485 See @ref image_handlers for a list of the available handlers.
487 static void AddHandler(wxImageHandler
* handler
);
490 Blurs the image in both horizontal and vertical directions by the
491 specified pixel @a blurRadius. This should not be used when using
492 a single mask colour for transparency.
494 @see BlurHorizontal(), BlurVertical()
496 wxImage
Blur(int blurRadius
) const;
499 Blurs the image in the horizontal direction only. This should not be used
500 when using a single mask colour for transparency.
502 @see Blur(), BlurVertical()
504 wxImage
BlurHorizontal(int blurRadius
) const;
507 Blurs the image in the vertical direction only. This should not be used
508 when using a single mask colour for transparency.
510 @see Blur(), BlurHorizontal()
512 wxImage
BlurVertical(int blurRadius
) const;
515 Returns @true if the current image handlers can read this file
517 static bool CanRead(const wxString
& filename
);
520 Deletes all image handlers.
521 This function is called by wxWidgets on exit.
523 static void CleanUpHandlers();
526 Computes the histogram of the image. @a histogram is a reference to
527 wxImageHistogram object. wxImageHistogram is a specialization of
528 wxHashMap "template" and is defined as follows:
531 class WXDLLEXPORT wxImageHistogramEntry
534 wxImageHistogramEntry() : index(0), value(0) {}
539 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
540 wxIntegerHash, wxIntegerEqual,
544 @return Returns number of colours in the histogram.
546 unsigned long ComputeHistogram(wxImageHistogram
& histogram
) const;
550 If the image has alpha channel, this method converts it to mask.
552 If the image has an alpha channel, all pixels with alpha value less
553 than @a threshold are replaced with the mask colour and the alpha
554 channel is removed. Otherwise nothing is done.
556 The mask colour is chosen automatically using
557 FindFirstUnusedColour() by this function, see the overload below if you
558 this is not appropriate.
560 @return @false if FindFirstUnusedColour returns @false, @true otherwise.
562 bool ConvertAlphaToMask(unsigned char threshold
= wxIMAGE_ALPHA_THRESHOLD
);
565 If the image has alpha channel, this method converts it to mask using
566 the specified colour as the mask colour.
568 If the image has an alpha channel, all pixels with alpha value less
569 than @a threshold are replaced with the mask colour and the alpha
570 channel is removed. Otherwise nothing is done.
575 The red component of the mask colour.
577 The green component of the mask colour.
579 The blue component of the mask colour.
581 Pixels with alpha channel values below the given threshold are
582 considered to be transparent, i.e. the corresponding mask pixels
583 are set. Pixels with the alpha values above the threshold are
584 considered to be opaque.
587 void ConvertAlphaToMask(unsigned char mr
, unsigned char mg
, unsigned char mb
,
588 unsigned char threshold
= wxIMAGE_ALPHA_THRESHOLD
);
592 Returns a greyscale version of the image.
594 The returned image uses the luminance component of the original to
595 calculate the greyscale. Defaults to using the standard ITU-T BT.601
596 when converting to YUV, where every pixel equals
597 (R * @a lr) + (G * @a lg) + (B * @a lb).
599 wxImage
ConvertToGreyscale(double lr
= 0.299, double lg
= 0.587, double lb
= 1.114) const;
602 Returns monochromatic version of the image.
604 The returned image has white colour where the original has @e (r,g,b)
605 colour and black colour everywhere else.
607 wxImage
ConvertToMono(unsigned char r
, unsigned char g
, unsigned char b
) const;
610 Returns an identical copy of the image.
612 wxImage
Copy() const;
615 Creates a fresh image.
617 If @a clear is @true, the new image will be initialized to black.
618 Otherwise, the image data will be uninitialized.
621 The width of the image in pixels.
623 The height of the image in pixels.
625 If @true, initialize the image data with zeroes.
627 @return @true if the call succeeded, @false otherwise.
629 bool Create(int width
, int height
, bool clear
= true);
632 Initialize the image data with zeroes (the default) or with the
633 byte value given as @a value.
637 void Clear(unsigned char value
= 0);
640 Destroys the image data.
645 Finds the first colour that is never used in the image.
646 The search begins at given initial colour and continues by increasing
647 R, G and B components (in this order) by 1 until an unused colour is
648 found or the colour space exhausted.
650 The parameters @a r, @a g, @a b are pointers to variables to save the colour.
652 The parameters @a startR, @a startG, @a startB define the initial values
654 The returned colour will have RGB values equal to or greater than these.
656 @return Returns @false if there is no unused colour left, @true on success.
659 This method involves computing the histogram, which is a
660 computationally intensive operation.
662 bool FindFirstUnusedColour(unsigned char* r
, unsigned char* g
,
663 unsigned char* b
, unsigned char startR
= 1,
664 unsigned char startG
= 0,
665 unsigned char startB
= 0) const;
668 Finds the handler with the given name.
673 @return A pointer to the handler if found, @NULL otherwise.
677 static wxImageHandler
* FindHandler(const wxString
& name
);
680 Finds the handler associated with the given extension and type.
683 The file extension, such as "bmp".
685 The image type; one of the ::wxBitmapType values.
687 @return A pointer to the handler if found, @NULL otherwise.
691 static wxImageHandler
* FindHandler(const wxString
& extension
,
692 wxBitmapType imageType
);
695 Finds the handler associated with the given image type.
698 The image type; one of the ::wxBitmapType values.
700 @return A pointer to the handler if found, @NULL otherwise.
704 static wxImageHandler
* FindHandler(wxBitmapType imageType
);
707 Finds the handler associated with the given MIME type.
712 @return A pointer to the handler if found, @NULL otherwise.
716 static wxImageHandler
* FindHandlerMime(const wxString
& mimetype
);
719 Return alpha value at given pixel location.
721 unsigned char GetAlpha(int x
, int y
) const;
724 Returns pointer to the array storing the alpha values for this image.
726 This pointer is @NULL for the images without the alpha channel. If the image
727 does have it, this pointer may be used to directly manipulate the alpha values
728 which are stored as the RGB ones.
730 unsigned char* GetAlpha() const;
733 Returns the blue intensity at the given coordinate.
735 unsigned char GetBlue(int x
, int y
) const;
738 Returns the image data as an array.
740 This is most often used when doing direct image manipulation.
741 The return value points to an array of characters in RGBRGBRGB... format
742 in the top-to-bottom, left-to-right order, that is the first RGB triplet
743 corresponds to the pixel first pixel of the first row, the second one ---
744 to the second pixel of the first row and so on until the end of the first
745 row, with second row following after it and so on.
747 You should not delete the returned pointer nor pass it to SetData().
749 unsigned char* GetData() const;
752 Returns the green intensity at the given coordinate.
754 unsigned char GetGreen(int x
, int y
) const;
757 Returns the static list of image format handlers.
761 static wxList
& GetHandlers();
764 Gets the height of the image in pixels.
766 @see GetWidth(), GetSize()
768 int GetHeight() const;
772 If the image file contains more than one image and the image handler is
773 capable of retrieving these individually, this function will return the
774 number of available images.
776 For the overload taking the parameter @a filename, that's the name
777 of the file to query.
778 For the overload taking the parameter @a stream, that's the ppened input
779 stream with image data. Currently, the stream must support seeking.
781 The parameter @a type may be one of the following values:
782 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
783 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
784 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
785 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
786 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
787 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
788 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
789 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
790 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
791 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
792 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
793 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
794 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
796 @return Number of available images. For most image handlers, this is 1
797 (exceptions are TIFF and ICO formats).
799 static int GetImageCount(const wxString
& filename
,
800 wxBitmapType type
= wxBITMAP_TYPE_ANY
);
801 static int GetImageCount(wxInputStream
& stream
,
802 wxBitmapType type
= wxBITMAP_TYPE_ANY
);
806 Iterates all registered wxImageHandler objects, and returns a string containing
807 file extension masks suitable for passing to file open/save dialog boxes.
809 @return The format of the returned string is @c "(*.ext1;*.ext2)|*.ext1;*.ext2".
810 It is usually a good idea to prepend a description before passing
811 the result to the dialog.
814 wxFileDialog FileDlg( this, "Choose Image", ::wxGetCwd(), "",
815 _("Image Files ") + wxImage::GetImageExtWildcard(),
821 static wxString
GetImageExtWildcard();
824 Gets the blue value of the mask colour.
826 unsigned char GetMaskBlue() const;
829 Gets the green value of the mask colour.
831 unsigned char GetMaskGreen() const;
834 Gets the red value of the mask colour.
836 unsigned char GetMaskRed() const;
839 Gets a user-defined string-valued option.
841 Currently the only defined string option is
842 @li @c wxIMAGE_OPTION_FILENAME: The name of the file from which the image
846 The name of the option, case-insensitive.
848 The value of the option or an empty string if not found. Use
849 HasOption() if an empty string can be a valid option value.
851 @see SetOption(), GetOptionInt(), HasOption()
853 wxString
GetOption(const wxString
& name
) const;
856 Gets a user-defined integer-valued option.
858 The function is case-insensitive to @a name.
859 If the given option is not present, the function returns 0.
860 Use HasOption() is 0 is a possibly valid value for the option.
863 @li @c wxIMAGE_OPTION_MAX_WIDTH and @c wxIMAGE_OPTION_MAX_HEIGHT: If either
864 of these options is specified, the loaded image will be scaled down
865 (preserving its aspect ratio) so that its width is less than the
866 max width given if it is not 0 @em and its height is less than the
867 max height given if it is not 0. This is typically used for loading
868 thumbnails and the advantage of using these options compared to
869 calling Rescale() after loading is that some handlers (only JPEG
870 one right now) support rescaling the image during loading which is
871 vastly more efficient than loading the entire huge image and
872 rescaling it later (if these options are not supported by the
873 handler, this is still what happens however). These options must be
874 set before calling LoadFile() to have any effect.
876 @li @c wxIMAGE_OPTION_QUALITY: JPEG quality used when saving. This is an
877 integer in 0..100 range with 0 meaning very poor and 100 excellent
878 (but very badly compressed). This option is currently ignored for
881 @li @c wxIMAGE_OPTION_RESOLUTIONUNIT: The value of this option determines
882 whether the resolution of the image is specified in centimetres or
883 inches, see wxImageResolution enum elements.
885 @li @c wxIMAGE_OPTION_RESOLUTION, @c wxIMAGE_OPTION_RESOLUTIONX and
886 @c wxIMAGE_OPTION_RESOLUTIONY: These options define the resolution of
887 the image in the units corresponding to @c wxIMAGE_OPTION_RESOLUTIONUNIT
888 options value. The first option can be set before saving the image
889 to set both horizontal and vertical resolution to the same value.
890 The X and Y options are set by the image handlers if they support
891 the image resolution (currently BMP, JPEG and TIFF handlers do) and
892 the image provides the resolution information and can be queried
893 after loading the image.
895 Options specific to wxPNGHandler:
896 @li @c wxIMAGE_OPTION_PNG_FORMAT: Format for saving a PNG file, see
897 wxImagePNGType for the supported values.
898 @li @c wxIMAGE_OPTION_PNG_BITDEPTH: Bit depth for every channel (R/G/B/A).
899 @li @c wxIMAGE_OPTION_PNG_FILTER: Filter for saving a PNG file, see libpng
900 (http://www.libpng.org/pub/png/libpng-1.2.5-manual.html) for possible values
901 (e.g. PNG_FILTER_NONE, PNG_FILTER_SUB, PNG_FILTER_UP, etc).
902 @li @c wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL: Compression level (0..9) for
903 saving a PNG file. An high value creates smaller-but-slower PNG file.
904 Note that unlike other formats (e.g. JPEG) the PNG format is always
905 lossless and thus this compression level doesn't tradeoff the image
907 @li @c wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL: Compression memory usage
908 level (1..9) for saving a PNG file. An high value means the saving
909 process consumes more memory, but may create smaller PNG file.
910 @li @c wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY: Possible values are 0 for
911 default strategy, 1 for filter, and 2 for Huffman-only.
912 You can use OptiPNG (http://optipng.sourceforge.net/) to get a suitable
913 value for your application.
914 @li @c wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE: Internal buffer size
915 (in bytes) for saving a PNG file. Ideally this should be as big as
916 the resulting PNG file. Use this option if your application produces
917 images with small size variation.
920 The name of the option, case-insensitive.
922 The value of the option or 0 if not found.
923 Use HasOption() if 0 can be a valid option value.
925 @see SetOption(), GetOption()
927 int GetOptionInt(const wxString
& name
) const;
930 Get the current mask colour or find a suitable unused colour that could be
931 used as a mask colour. Returns @true if the image currently has a mask.
933 bool GetOrFindMaskColour(unsigned char* r
, unsigned char* g
,
934 unsigned char* b
) const;
937 Returns the palette associated with the image.
938 Currently the palette is only used when converting to wxBitmap under Windows.
940 Some of the wxImage handlers have been modified to set the palette if
941 one exists in the image file (usually 256 or less colour images in
944 const wxPalette
& GetPalette() const;
947 Returns the red intensity at the given coordinate.
949 unsigned char GetRed(int x
, int y
) const;
952 Returns a sub image of the current one as long as the rect belongs entirely
955 wxImage
GetSubImage(const wxRect
& rect
) const;
958 Returns the size of the image in pixels.
962 @see GetHeight(), GetWidth()
964 wxSize
GetSize() const;
967 Gets the type of image found by LoadFile() or specified with SaveFile().
971 wxBitmapType
GetType() const;
974 Gets the width of the image in pixels.
976 @see GetHeight(), GetSize()
978 int GetWidth() const;
981 Converts a color in HSV color space to RGB color space.
983 static wxImage::RGBValue
HSVtoRGB(const wxImage::HSVValue
& hsv
);
986 Returns @true if this image has alpha channel, @false otherwise.
988 @see GetAlpha(), SetAlpha()
990 bool HasAlpha() const;
993 Returns @true if there is a mask active, @false otherwise.
995 bool HasMask() const;
998 Returns @true if the given option is present.
999 The function is case-insensitive to @a name.
1001 The lists of the currently supported options are in GetOption() and
1002 GetOptionInt() function docs.
1004 @see SetOption(), GetOption(), GetOptionInt()
1006 bool HasOption(const wxString
& name
) const;
1009 Initializes the image alpha channel data.
1011 It is an error to call it if the image already has alpha data.
1012 If it doesn't, alpha data will be by default initialized to all pixels
1013 being fully opaque. But if the image has a mask colour, all mask pixels
1014 will be completely transparent.
1019 Internal use only. Adds standard image format handlers.
1020 It only install wxBMPHandler for the time being, which is used by wxBitmap.
1022 This function is called by wxWidgets on startup, and shouldn't be called by
1025 @see wxImageHandler, wxInitAllImageHandlers(), wxQuantize
1027 static void InitStandardHandlers();
1030 Adds a handler at the start of the static list of format handlers.
1033 A new image format handler object. There is usually only one instance
1034 of a given handler class in an application session.
1038 static void InsertHandler(wxImageHandler
* handler
);
1041 Returns @true if image data is present.
1046 Returns @true if the given pixel is transparent, i.e. either has the mask
1047 colour if this image has a mask or if this image has alpha channel and alpha
1048 value of this pixel is strictly less than @a threshold.
1050 bool IsTransparent(int x
, int y
,
1051 unsigned char threshold
= wxIMAGE_ALPHA_THRESHOLD
) const;
1054 Loads an image from an input stream.
1057 Opened input stream from which to load the image.
1058 Currently, the stream must support seeking.
1060 May be one of the following:
1061 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
1062 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
1063 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
1064 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
1065 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
1066 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
1067 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
1068 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
1069 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
1070 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
1071 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
1072 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
1073 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
1075 Index of the image to load in the case that the image file contains
1076 multiple images. This is only used by GIF, ICO and TIFF handlers.
1077 The default value (-1) means "choose the default image" and is
1078 interpreted as the first image (index=0) by the GIF and TIFF handler
1079 and as the largest and most colourful one by the ICO handler.
1081 @return @true if the operation succeeded, @false otherwise.
1082 If the optional index parameter is out of range, @false is
1083 returned and a call to wxLogError() takes place.
1085 @remarks Depending on how wxWidgets has been configured, not all formats
1089 You can use GetOptionInt() to get the hotspot when loading cursor files:
1091 int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
1092 int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
1097 virtual bool LoadFile(wxInputStream
& stream
,
1098 wxBitmapType type
= wxBITMAP_TYPE_ANY
,
1102 Loads an image from a file.
1103 If no handler type is provided, the library will try to autodetect the format.
1106 Name of the file from which to load the image.
1108 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1110 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1112 virtual bool LoadFile(const wxString
& name
,
1113 wxBitmapType type
= wxBITMAP_TYPE_ANY
,
1117 Loads an image from a file.
1118 If no handler type is provided, the library will try to autodetect the format.
1121 Name of the file from which to load the image.
1123 MIME type string (for example 'image/jpeg')
1125 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1127 virtual bool LoadFile(const wxString
& name
, const wxString
& mimetype
,
1132 Loads an image from an input stream.
1135 Opened input stream from which to load the image.
1136 Currently, the stream must support seeking.
1138 MIME type string (for example 'image/jpeg')
1140 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1142 virtual bool LoadFile(wxInputStream
& stream
, const wxString
& mimetype
,
1146 Returns a mirrored copy of the image.
1147 The parameter @a horizontally indicates the orientation.
1149 wxImage
Mirror(bool horizontally
= true) const;
1152 Copy the data of the given @a image to the specified position in this image.
1154 void Paste(const wxImage
& image
, int x
, int y
);
1157 Converts a color in RGB color space to HSV color space.
1159 static wxImage::HSVValue
RGBtoHSV(const wxImage::RGBValue
& rgb
);
1162 Finds the handler with the given name, and removes it.
1163 The handler is not deleted.
1168 @return @true if the handler was found and removed, @false otherwise.
1172 static bool RemoveHandler(const wxString
& name
);
1175 Replaces the colour specified by @e r1,g1,b1 by the colour @e r2,g2,b2.
1177 void Replace(unsigned char r1
, unsigned char g1
,
1178 unsigned char b1
, unsigned char r2
,
1179 unsigned char g2
, unsigned char b2
);
1182 Changes the size of the image in-place by scaling it: after a call to this
1183 function,the image will have the given width and height.
1185 For a description of the @a quality parameter, see the Scale() function.
1186 Returns the (modified) image itself.
1190 wxImage
& Rescale(int width
, int height
,
1191 int quality
= wxIMAGE_QUALITY_NORMAL
);
1194 Changes the size of the image in-place without scaling it by adding either a
1195 border with the given colour or cropping as necessary.
1197 The image is pasted into a new image with the given @a size and background
1198 colour at the position @a pos relative to the upper left of the new image.
1200 If @a red = green = blue = -1 then use either the current mask colour
1201 if set or find, use, and set a suitable mask colour for any newly exposed
1204 @return The (modified) image itself.
1208 wxImage
& Resize(const wxSize
& size
, const wxPoint
& pos
, int red
= -1,
1209 int green
= -1, int blue
= -1);
1212 Rotates the image about the given point, by @a angle radians.
1214 Passing @true to @a interpolating results in better image quality, but is slower.
1216 If the image has a mask, then the mask colour is used for the uncovered
1217 pixels in the rotated image background. Else, black (rgb 0, 0, 0) will be used.
1219 Returns the rotated image, leaving this image intact.
1221 wxImage
Rotate(double angle
, const wxPoint
& rotationCentre
,
1222 bool interpolating
= true,
1223 wxPoint
* offsetAfterRotation
= NULL
) const;
1226 Returns a copy of the image rotated 90 degrees in the direction
1227 indicated by @a clockwise.
1229 wxImage
Rotate90(bool clockwise
= true) const;
1232 Rotates the hue of each pixel in the image by @e angle, which is a double in
1233 the range of -1.0 to +1.0, where -1.0 corresponds to -360 degrees and +1.0
1234 corresponds to +360 degrees.
1236 void RotateHue(double angle
);
1239 Saves an image in the given stream.
1242 Opened output stream to save the image to.
1246 @return @true if the operation succeeded, @false otherwise.
1248 @remarks Depending on how wxWidgets has been configured, not all formats
1252 You can use SetOption() to set the hotspot when saving an image
1253 into a cursor file (default hotspot is in the centre of the image):
1255 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotspotX);
1256 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotspotY);
1261 virtual bool SaveFile(wxOutputStream
& stream
,
1262 const wxString
& mimetype
) const;
1265 Saves an image in the named file.
1268 Name of the file to save the image to.
1270 Currently these types can be used:
1271 @li wxBITMAP_TYPE_BMP: Save a BMP image file.
1272 @li wxBITMAP_TYPE_JPEG: Save a JPEG image file.
1273 @li wxBITMAP_TYPE_PNG: Save a PNG image file.
1274 @li wxBITMAP_TYPE_PCX: Save a PCX image file
1275 (tries to save as 8-bit if possible, falls back to 24-bit otherwise).
1276 @li wxBITMAP_TYPE_PNM: Save a PNM image file (as raw RGB always).
1277 @li wxBITMAP_TYPE_TIFF: Save a TIFF image file.
1278 @li wxBITMAP_TYPE_XPM: Save a XPM image file.
1279 @li wxBITMAP_TYPE_ICO: Save a Windows icon file (ICO).
1280 The size may be up to 255 wide by 127 high. A single image is saved
1281 in 8 colors at the size supplied.
1282 @li wxBITMAP_TYPE_CUR: Save a Windows cursor file (CUR).
1284 virtual bool SaveFile(const wxString
& name
, wxBitmapType type
) const;
1287 Saves an image in the named file.
1290 Name of the file to save the image to.
1294 virtual bool SaveFile(const wxString
& name
, const wxString
& mimetype
) const;
1297 Saves an image in the named file.
1299 File type is determined from the extension of the file name.
1300 Note that this function may fail if the extension is not recognized!
1301 You can use one of the forms above to save images to files with
1302 non-standard extensions.
1305 Name of the file to save the image to.
1307 virtual bool SaveFile(const wxString
& name
) const;
1310 Saves an image in the given stream.
1313 Opened output stream to save the image to.
1317 virtual bool SaveFile(wxOutputStream
& stream
, wxBitmapType type
) const;
1320 Returns a scaled version of the image.
1322 This is also useful for scaling bitmaps in general as the only other way
1323 to scale bitmaps is to blit a wxMemoryDC into another wxMemoryDC.
1325 The parameter @a quality determines what method to use for resampling the image.
1326 Can be one of the following:
1327 - wxIMAGE_QUALITY_NORMAL: Uses the normal default scaling method of pixel
1329 - wxIMAGE_QUALITY_HIGH: Uses bicubic and box averaging resampling methods
1330 for upsampling and downsampling respectively
1332 It should be noted that although using @c wxIMAGE_QUALITY_HIGH produces much nicer
1333 looking results it is a slower method. Downsampling will use the box averaging
1334 method which seems to operate very fast. If you are upsampling larger images using
1335 this method you will most likely notice that it is a bit slower and in extreme
1336 cases it will be quite substantially slower as the bicubic algorithm has to process a
1339 It should also be noted that the high quality scaling may not work as expected
1340 when using a single mask colour for transparency, as the scaling will blur the
1341 image and will therefore remove the mask partially. Using the alpha channel
1346 // get the bitmap from somewhere
1349 // rescale it to have size of 32*32
1350 if ( bmp.GetWidth() != 32 || bmp.GetHeight() != 32 )
1352 wxImage image = bmp.ConvertToImage();
1353 bmp = wxBitmap(image.Scale(32, 32));
1355 // another possibility:
1356 image.Rescale(32, 32);
1363 wxImage
Scale(int width
, int height
,
1364 int quality
= wxIMAGE_QUALITY_NORMAL
) const;
1367 This function is similar to SetData() and has similar restrictions.
1369 The pointer passed to it may however be @NULL in which case the function
1370 will allocate the alpha array internally -- this is useful to add alpha
1371 channel data to an image which doesn't have any.
1373 If the pointer is not @NULL, it must have one byte for each image pixel
1374 and be allocated with malloc().
1375 wxImage takes ownership of the pointer and will free it unless @a static_data
1376 parameter is set to @true -- in this case the caller should do it.
1378 void SetAlpha(unsigned char* alpha
= NULL
,
1379 bool static_data
= false);
1382 Sets the alpha value for the given pixel.
1383 This function should only be called if the image has alpha channel data,
1384 use HasAlpha() to check for this.
1386 void SetAlpha(int x
, int y
, unsigned char alpha
);
1390 Sets the image data without performing checks.
1392 The data given must have the size (width*height*3) or results will be
1393 unexpected. Don't use this method if you aren't sure you know what you
1396 The data must have been allocated with @c malloc(), @b NOT with
1399 After this call the pointer to the data is owned by the wxImage object,
1400 that will be responsible for deleting it.
1401 Do not pass to this function a pointer obtained through GetData().
1403 void SetData(unsigned char* data
, bool static_data
= false);
1404 void SetData(unsigned char* data
, int new_width
, int new_height
,
1405 bool static_data
= false);
1409 Specifies whether there is a mask or not.
1411 The area of the mask is determined by the current mask colour.
1413 void SetMask(bool hasMask
= true);
1416 Sets the mask colour for this image (and tells the image to use the mask).
1418 void SetMaskColour(unsigned char red
, unsigned char green
,
1419 unsigned char blue
);
1422 Sets image's mask so that the pixels that have RGB value of mr,mg,mb in
1423 mask will be masked in the image.
1425 This is done by first finding an unused colour in the image, setting
1426 this colour as the mask colour and then using this colour to draw all
1427 pixels in the image who corresponding pixel in mask has given RGB value.
1429 The parameter @a mask is the mask image to extract mask shape from.
1430 It must have the same dimensions as the image.
1432 The parameters @a mr, @a mg, @a mb are the RGB values of the pixels in
1433 mask that will be used to create the mask.
1435 @return Returns @false if mask does not have same dimensions as the image
1436 or if there is no unused colour left. Returns @true if the mask
1437 was successfully applied.
1440 Note that this method involves computing the histogram, which is a
1441 computationally intensive operation.
1443 bool SetMaskFromImage(const wxImage
& mask
, unsigned char mr
,
1449 Sets a user-defined option. The function is case-insensitive to @a name.
1451 For example, when saving as a JPEG file, the option @b quality is
1452 used, which is a number between 0 and 100 (0 is terrible, 100 is very good).
1454 The lists of the currently supported options are in GetOption() and
1455 GetOptionInt() function docs.
1457 @see GetOption(), GetOptionInt(), HasOption()
1459 void SetOption(const wxString
& name
, const wxString
& value
);
1460 void SetOption(const wxString
& name
, int value
);
1464 Associates a palette with the image.
1466 The palette may be used when converting wxImage to wxBitmap (MSW only at present)
1467 or in file save operations (none as yet).
1469 void SetPalette(const wxPalette
& palette
);
1472 Sets the colour of the pixels within the given rectangle.
1474 This routine performs bounds-checks for the coordinate so it can be considered
1475 a safe way to manipulate the data.
1477 void SetRGB(const wxRect
& rect
,
1479 unsigned char green
,
1480 unsigned char blue
);
1483 Set the type of image returned by GetType().
1485 This method is mostly used internally by the library but can also be
1486 called from the user code if the image was created from data in the
1487 given bitmap format without using LoadFile() (which would set the type
1488 correctly automatically).
1490 Notice that the image must be created before this function is called.
1495 One of bitmap type constants, @c wxBITMAP_TYPE_INVALID is a valid
1496 value for it and can be used to reset the bitmap type to default
1497 but @c wxBITMAP_TYPE_MAX is not allowed here.
1499 void SetType(wxBitmapType type
);
1502 Returns a resized version of this image without scaling it by adding either a
1503 border with the given colour or cropping as necessary.
1505 The image is pasted into a new image with the given @a size and background
1506 colour at the position @a pos relative to the upper left of the new image.
1508 If @a red = green = blue = -1 then the areas of the larger image not covered
1509 by this image are made transparent by filling them with the image mask colour
1510 (which will be allocated automatically if it isn't currently set).
1512 Otherwise, the areas will be filled with the colour with the specified RGB components.
1516 wxImage
Size(const wxSize
& size
, const wxPoint
& pos
, int red
= -1,
1517 int green
= -1, int blue
= -1) const;
1520 Assignment operator, using @ref overview_refcount "reference counting".
1525 @return Returns 'this' object.
1527 wxImage
& operator=(const wxImage
& image
);
1531 An instance of an empty image without an alpha channel.
1533 wxImage wxNullImage
;
1536 // ============================================================================
1537 // Global functions/macros
1538 // ============================================================================
1540 /** @addtogroup group_funcmacro_appinitterm */
1544 Initializes all available image handlers. For a list of available handlers,
1546 If you don't need/want all image handlers loaded
1548 @see wxImage, wxImageHandler
1552 void wxInitAllImageHandlers();