1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxImageHandler and wxImage
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 This is the base class for implementing image file loading/saving, and
13 image creation from data.
14 It is used within wxImage and is not normally seen by the application.
16 If you wish to extend the capabilities of wxImage, derive a class from
17 wxImageHandler and add the handler using wxImage::AddHandler in your
18 application initialisation.
20 Note that all wxImageHandlers provided by wxWidgets are part of
21 the @ref page_libs_wxcore library.
22 For details about the default handlers, please see the section
23 @ref image_handlers in the wxImage class documentation.
26 @section imagehandler_note Note (Legal Issue)
28 This software is based in part on the work of the Independent JPEG Group.
29 (Applies when wxWidgets is linked with JPEG support.
30 wxJPEGHandler uses libjpeg created by IJG.)
39 @see wxImage, wxInitAllImageHandlers()
41 class wxImageHandler
: public wxObject
47 In your own default constructor, initialise the members
48 m_name, m_extension and m_type.
53 Destroys the wxImageHandler object.
55 virtual ~wxImageHandler();
58 Gets the file extension associated with this handler.
60 const wxString
& GetExtension() const;
63 If the image file contains more than one image and the image handler is capable
64 of retrieving these individually, this function will return the number of
68 Opened input stream for reading image data.
69 Currently, the stream must support seeking.
71 @return Number of available images. For most image handlers, this is 1
72 (exceptions are TIFF and ICO formats).
74 virtual int GetImageCount(wxInputStream
& stream
);
77 Gets the MIME type associated with this handler.
79 const wxString
& GetMimeType() const;
82 Gets the name of this handler.
84 const wxString
& GetName() const;
87 Gets the image type associated with this handler.
89 wxBitmapType
GetType() const;
92 Loads a image from a stream, putting the resulting data into @a image.
94 If the image file contains more than one image and the image handler is
95 capable of retrieving these individually, @a index indicates which image
96 to read from the stream.
99 The image object which is to be affected by this operation.
101 Opened input stream for reading image data.
103 If set to @true, errors reported by the image handler will produce
106 The index of the image in the file (starting from zero).
108 @return @true if the operation succeeded, @false otherwise.
110 @see wxImage::LoadFile, wxImage::SaveFile, SaveFile()
112 virtual bool LoadFile(wxImage
* image
, wxInputStream
& stream
,
113 bool verbose
= true, int index
= -1);
116 Saves a image in the output stream.
119 The image object which is to be affected by this operation.
121 Opened output stream for writing the data.
123 If set to @true, errors reported by the image handler will produce
126 @return @true if the operation succeeded, @false otherwise.
128 @see wxImage::LoadFile, wxImage::SaveFile, LoadFile()
130 virtual bool SaveFile(wxImage
* image
, wxOutputStream
& stream
,
131 bool verbose
= true);
134 Sets the handler extension.
139 void SetExtension(const wxString
& extension
);
142 Sets the handler MIME type.
147 void SetMimeType(const wxString
& mimetype
);
150 Sets the handler name.
155 void SetName(const wxString
& name
);
160 Constant used to indicate the alpha value conventionally defined as
161 the complete transparency.
163 const unsigned char wxIMAGE_ALPHA_TRANSPARENT
= 0;
166 Constant used to indicate the alpha value conventionally defined as
167 the complete opacity.
169 const unsigned char wxIMAGE_ALPHA_OPAQUE
= 0xff;
174 This class encapsulates a platform-independent image.
176 An image can be created from data, or using wxBitmap::ConvertToImage.
177 An image can be loaded from a file in a variety of formats, and is extensible
178 to new formats via image format handlers. Functions are available to set and
179 get image bits, so it can be used for basic image manipulation.
181 A wxImage cannot (currently) be drawn directly to a wxDC.
182 Instead, a platform-specific wxBitmap object must be created from it using
183 the wxBitmap::wxBitmap(wxImage,int depth) constructor.
184 This bitmap can then be drawn in a device context, using wxDC::DrawBitmap.
186 One colour value of the image may be used as a mask colour which will lead to
187 the automatic creation of a wxMask object associated to the bitmap object.
190 @section image_alpha Alpha channel support
192 Starting from wxWidgets 2.5.0 wxImage supports alpha channel data, that is
193 in addition to a byte for the red, green and blue colour components for each
194 pixel it also stores a byte representing the pixel opacity.
196 An alpha value of 0 corresponds to a transparent pixel (null opacity) while
197 a value of 255 means that the pixel is 100% opaque.
198 The constants ::wxIMAGE_ALPHA_TRANSPARENT and ::wxIMAGE_ALPHA_OPAQUE can be
199 used to indicate those values in a more readable form.
201 Unlike RGB data, not all images have an alpha channel and before using
202 wxImage::GetAlpha you should check if this image contains an alpha channel
203 with wxImage::HasAlpha. Note that currently only the PNG format has full
204 alpha channel support so only the images loaded from PNG files can have
205 alpha and, if you initialize the image alpha channel yourself using
206 wxImage::SetAlpha, you should save it in PNG format to avoid losing it.
209 @section image_handlers Available image handlers
211 The following image handlers are available.
212 wxBMPHandler is always installed by default.
213 To use other image formats, install the appropriate handler with
214 wxImage::AddHandler or call ::wxInitAllImageHandlers().
216 - wxBMPHandler: For loading and saving, always installed.
217 - wxPNGHandler: For loading (including alpha support) and saving.
218 - wxJPEGHandler: For loading and saving.
219 - wxGIFHandler: Only for loading, due to legal issues.
220 - wxPCXHandler: For loading and saving (see below).
221 - wxPNMHandler: For loading and saving (see below).
222 - wxTIFFHandler: For loading and saving.
223 - wxTGAHandler: For loading only.
224 - wxIFFHandler: For loading only.
225 - wxXPMHandler: For loading and saving.
226 - wxICOHandler: For loading and saving.
227 - wxCURHandler: For loading and saving.
228 - wxANIHandler: For loading only.
230 When saving in PCX format, wxPCXHandler will count the number of different
231 colours in the image; if there are 256 or less colours, it will save as 8 bit,
232 else it will save as 24 bit.
234 Loading PNMs only works for ASCII or raw RGB images.
235 When saving in PNM format, wxPNMHandler will always save as raw RGB.
244 @see wxBitmap, wxInitAllImageHandlers(), wxPixelData
246 class wxImage
: public wxObject
253 Constructor for RGBValue, an object that contains values for red, green
254 and blue which represent the value of a color.
256 It is used by wxImage::HSVtoRGB and wxImage::RGBtoHSV, which converts
257 between HSV color space and RGB color space.
259 RGBValue(unsigned char r
=0, unsigned char g
=0, unsigned char b
=0);
266 Constructor for HSVValue, an object that contains values for hue, saturation
267 and value which represent the value of a color.
269 It is used by wxImage::HSVtoRGB() and wxImage::RGBtoHSV(), which converts
270 between HSV color space and RGB color space.
272 HSVValue(double h
=0.0, double s
=0.0, double v
=0.0);
276 Creates an empty wxImage object without an alpha channel.
281 Creates an image with the given size and clears it if requested.
283 Does not create an alpha channel.
286 Specifies the width of the image.
288 Specifies the height of the image.
290 If @true, initialize the image to black.
292 wxImage(int width
, int height
, bool clear
= true);
295 Creates an image from data in memory. If @a static_data is @false
296 then the wxImage will take ownership of the data and free it
297 afterwards. For this, it has to be allocated with @e malloc.
300 Specifies the width of the image.
302 Specifies the height of the image.
304 A pointer to RGB data
306 Indicates if the data should be free'd after use
309 wxImage(int width
, int height
, unsigned char* data
, bool static_data
= false);
312 Creates an image from data in memory. If @a static_data is @false
313 then the wxImage will take ownership of the data and free it
314 afterwards. For this, it has to be allocated with @e malloc.
317 Specifies the width of the image.
319 Specifies the height of the image.
321 A pointer to RGB data
323 A pointer to alpha-channel data
325 Indicates if the data should be free'd after use
328 wxImage(int width
, int height
, unsigned char* data
, unsigned char* alpha
,
329 bool static_data
= false );
332 Creates an image from XPM data.
335 A pointer to XPM image data.
337 wxImage(const char* const* xpmData
);
340 Creates an image from a file.
343 Name of the file from which to load the image.
345 May be one of the following:
346 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
347 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
348 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
349 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
350 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
351 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
352 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
353 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
354 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
355 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
356 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
357 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
358 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
360 Index of the image to load in the case that the image file contains
361 multiple images. This is only used by GIF, ICO and TIFF handlers.
362 The default value (-1) means "choose the default image" and is
363 interpreted as the first image (index=0) by the GIF and TIFF handler
364 and as the largest and most colourful one by the ICO handler.
366 @remarks Depending on how wxWidgets has been configured and by which
367 handlers have been loaded, not all formats may be available.
368 Any handler other than BMP must be previously initialized with
369 wxImage::AddHandler or wxInitAllImageHandlers.
372 You can use GetOptionInt() to get the hotspot when loading cursor files:
374 int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
375 int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
380 wxImage(const wxString
& name
, wxBitmapType type
= wxBITMAP_TYPE_ANY
, int index
= -1);
383 Creates an image from a file using MIME-types to specify the type.
386 Name of the file from which to load the image.
388 MIME type string (for example 'image/jpeg')
390 See description in wxImage(const wxString&, wxBitmapType, int) overload.
392 wxImage(const wxString
& name
, const wxString
& mimetype
, int index
= -1);
395 Creates an image from a stream.
398 Opened input stream from which to load the image. Currently,
399 the stream must support seeking.
401 See description in wxImage(const wxString&, wxBitmapType, int) overload.
403 See description in wxImage(const wxString&, wxBitmapType, int) overload.
405 wxImage(wxInputStream
& stream
, wxBitmapType type
= wxBITMAP_TYPE_ANY
, int index
= -1);
408 Creates an image from a stream using MIME-types to specify the type.
411 Opened input stream from which to load the image. Currently,
412 the stream must support seeking.
414 MIME type string (for example 'image/jpeg')
416 See description in wxImage(const wxString&, wxBitmapType, int) overload.
418 wxImage(wxInputStream
& stream
, const wxString
& mimetype
, int index
= -1);
424 See @ref overview_refcount_destruct "reference-counted object destruction"
430 Register an image handler.
432 static void AddHandler(wxImageHandler
* handler
);
435 Blurs the image in both horizontal and vertical directions by the
436 specified pixel @a blurRadius. This should not be used when using
437 a single mask colour for transparency.
439 @see BlurHorizontal(), BlurVertical()
441 wxImage
Blur(int blurRadius
) const;
444 Blurs the image in the horizontal direction only. This should not be used
445 when using a single mask colour for transparency.
447 @see Blur(), BlurVertical()
449 wxImage
BlurHorizontal(int blurRadius
) const;
452 Blurs the image in the vertical direction only. This should not be used
453 when using a single mask colour for transparency.
455 @see Blur(), BlurHorizontal()
457 wxImage
BlurVertical(int blurRadius
) const;
460 Returns @true if the current image handlers can read this file
462 static bool CanRead(const wxString
& filename
);
465 Deletes all image handlers.
466 This function is called by wxWidgets on exit.
468 static void CleanUpHandlers();
471 Computes the histogram of the image. @a histogram is a reference to
472 wxImageHistogram object. wxImageHistogram is a specialization of
473 wxHashMap "template" and is defined as follows:
476 class WXDLLEXPORT wxImageHistogramEntry
479 wxImageHistogramEntry() : index(0), value(0) {}
484 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
485 wxIntegerHash, wxIntegerEqual,
489 @return Returns number of colours in the histogram.
491 unsigned long ComputeHistogram(wxImageHistogram
& histogram
) const;
494 If the image has alpha channel, this method converts it to mask.
496 All pixels with alpha value less than @a threshold are replaced with mask
497 colour and the alpha channel is removed. Mask colour is chosen automatically
498 using FindFirstUnusedColour().
500 If the image image doesn't have alpha channel, ConvertAlphaToMask() does nothing.
502 @return @false if FindFirstUnusedColour returns @false, @true otherwise.
504 bool ConvertAlphaToMask(unsigned char threshold
= wxIMAGE_ALPHA_THRESHOLD
);
508 Use the equivalent @ref wxBitmap::wxBitmap "wxBitmap constructor"
509 (which takes wxImage and depth as its arguments) instead.
511 wxBitmap
ConvertToBitmap() const;
514 Returns a greyscale version of the image.
516 The returned image uses the luminance component of the original to
517 calculate the greyscale. Defaults to using the standard ITU-T BT.601
518 when converting to YUV, where every pixel equals
519 (R * @a lr) + (G * @a lg) + (B * @a lb).
521 wxImage
ConvertToGreyscale(double lr
= 0.299, double lg
= 0.587, double lb
= 1.114) const;
524 Returns monochromatic version of the image.
526 The returned image has white colour where the original has @e (r,g,b)
527 colour and black colour everywhere else.
529 wxImage
ConvertToMono(unsigned char r
, unsigned char g
, unsigned char b
) const;
532 Returns an identical copy of the image.
534 wxImage
Copy() const;
537 Creates a fresh image.
539 If @a clear is @true, the new image will be initialized to black.
540 Otherwise, the image data will be uninitialized.
543 The width of the image in pixels.
545 The height of the image in pixels.
547 If @true, initialize the image data with zeros.
549 @return @true if the call succeeded, @false otherwise.
551 bool Create(int width
, int height
, bool clear
= true);
554 Destroys the image data.
559 Finds the first colour that is never used in the image.
560 The search begins at given initial colour and continues by increasing
561 R, G and B components (in this order) by 1 until an unused colour is
562 found or the colour space exhausted.
564 The parameters @a r, @a g, @a b are pointers to variables to save the colour.
566 The parameters @a startR, @a startG, @a startB define the initial values
568 The returned colour will have RGB values equal to or greater than these.
570 @return Returns @false if there is no unused colour left, @true on success.
573 This method involves computing the histogram, which is a
574 computationally intensive operation.
576 bool FindFirstUnusedColour(unsigned char* r
, unsigned char* g
,
577 unsigned char* b
, unsigned char startR
= 1,
578 unsigned char startG
= 0,
579 unsigned char startB
= 0) const;
582 Finds the handler with the given name.
587 @return A pointer to the handler if found, @NULL otherwise.
591 static wxImageHandler
* FindHandler(const wxString
& name
);
594 Finds the handler associated with the given extension and type.
597 The file extension, such as "bmp".
599 The image type; one of the ::wxBitmapType values.
601 @return A pointer to the handler if found, @NULL otherwise.
605 static wxImageHandler
* FindHandler(const wxString
& extension
,
606 wxBitmapType imageType
);
609 Finds the handler associated with the given image type.
612 The image type; one of the ::wxBitmapType values.
614 @return A pointer to the handler if found, @NULL otherwise.
618 static wxImageHandler
* FindHandler(wxBitmapType imageType
);
621 Finds the handler associated with the given MIME type.
626 @return A pointer to the handler if found, @NULL otherwise.
630 static wxImageHandler
* FindHandlerMime(const wxString
& mimetype
);
633 Return alpha value at given pixel location.
635 unsigned char GetAlpha(int x
, int y
) const;
638 Returns pointer to the array storing the alpha values for this image.
640 This pointer is @NULL for the images without the alpha channel. If the image
641 does have it, this pointer may be used to directly manipulate the alpha values
642 which are stored as the RGB ones.
644 unsigned char* GetAlpha() const;
647 Returns the blue intensity at the given coordinate.
649 unsigned char GetBlue(int x
, int y
) const;
652 Returns the image data as an array.
654 This is most often used when doing direct image manipulation.
655 The return value points to an array of characters in RGBRGBRGB... format
656 in the top-to-bottom, left-to-right order, that is the first RGB triplet
657 corresponds to the pixel first pixel of the first row, the second one ---
658 to the second pixel of the first row and so on until the end of the first
659 row, with second row following after it and so on.
661 You should not delete the returned pointer nor pass it to SetData().
663 unsigned char* GetData() const;
666 Returns the green intensity at the given coordinate.
668 unsigned char GetGreen(int x
, int y
) const;
671 Returns the static list of image format handlers.
675 static wxList
GetHandlers();
678 Gets the height of the image in pixels.
680 int GetHeight() const;
684 If the image file contains more than one image and the image handler is
685 capable of retrieving these individually, this function will return the
686 number of available images.
688 For the overload taking the parameter @a filename, that's the name
689 of the file to query.
690 For the overload taking the parameter @a stream, that's the ppened input
691 stream with image data. Currently, the stream must support seeking.
693 The parameter @a type may be one of the following values:
694 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
695 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
696 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
697 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
698 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
699 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
700 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
701 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
702 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
703 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
704 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
705 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
706 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
708 @return Number of available images. For most image handlers, this is 1
709 (exceptions are TIFF and ICO formats).
711 static int GetImageCount(const wxString
& filename
,
712 wxBitmapType type
= wxBITMAP_TYPE_ANY
);
713 static int GetImageCount(wxInputStream
& stream
,
714 wxBitmapType type
= wxBITMAP_TYPE_ANY
);
718 Iterates all registered wxImageHandler objects, and returns a string containing
719 file extension masks suitable for passing to file open/save dialog boxes.
721 @return The format of the returned string is @c "(*.ext1;*.ext2)|*.ext1;*.ext2".
722 It is usually a good idea to prepend a description before passing
723 the result to the dialog.
726 wxFileDialog FileDlg( this, "Choose Image", ::wxGetCwd(), "",
727 _("Image Files ") + wxImage::GetImageExtWildcard(),
733 static wxString
GetImageExtWildcard();
736 Gets the blue value of the mask colour.
738 unsigned char GetMaskBlue() const;
741 Gets the green value of the mask colour.
743 unsigned char GetMaskGreen() const;
746 Gets the red value of the mask colour.
748 unsigned char GetMaskRed() const;
751 Gets a user-defined option. The function is case-insensitive to @a name.
753 For example, when saving as a JPEG file, the option @b quality is
754 used, which is a number between 0 and 100 (0 is terrible, 100 is very good).
756 @see SetOption(), GetOptionInt(), HasOption()
758 wxString
GetOption(const wxString
& name
) const;
761 Gets a user-defined option as an integer.
762 The function is case-insensitive to @a name.
763 If the given option is not present, the function returns 0.
764 Use HasOption() is 0 is a possibly valid value for the option.
766 Options common to all formats:
767 @li wxIMAGE_OPTION_MAX_WIDTH and wxIMAGE_OPTION_MAX_HEIGHT: If either
768 of these options is specified, the loaded image will be scaled down
769 (preserving its aspect ratio) so that its width is less than the
770 max width given if it is not 0 @em and its height is less than the
771 max height given if it is not 0. This is typically used for loading
772 thumbnails and the advantage of using these options compared to
773 calling Rescale() after loading is that some handlers (only JPEG
774 one right now) support rescaling the image during loading which is
775 vastly more efficient than loading the entire huge image and
776 rescaling it later (if these options are not supported by the
777 handler, this is still what happens however). These options must be
778 set before calling LoadFile() to have any effect.
780 Options for wxPNGHandler:
781 @li wxIMAGE_OPTION_PNG_FORMAT: Format for saving a PNG file.
782 @li wxIMAGE_OPTION_PNG_BITDEPTH: Bit depth for every channel (R/G/B/A).
784 Supported values for wxIMAGE_OPTION_PNG_FORMAT:
785 @li wxPNG_TYPE_COLOUR: Stores RGB image.
786 @li wxPNG_TYPE_GREY: Stores grey image, converts from RGB.
787 @li wxPNG_TYPE_GREY_RED: Stores grey image, uses red value as grey.
789 @see SetOption(), GetOption()
791 int GetOptionInt(const wxString
& name
) const;
794 Get the current mask colour or find a suitable unused colour that could be
795 used as a mask colour. Returns @true if the image currently has a mask.
797 bool GetOrFindMaskColour(unsigned char* r
, unsigned char* g
,
798 unsigned char* b
) const;
801 Returns the palette associated with the image.
802 Currently the palette is only used when converting to wxBitmap under Windows.
804 Some of the wxImage handlers have been modified to set the palette if
805 one exists in the image file (usually 256 or less colour images in
808 const wxPalette
& GetPalette() const;
811 Returns the red intensity at the given coordinate.
813 unsigned char GetRed(int x
, int y
) const;
816 Returns a sub image of the current one as long as the rect belongs entirely
819 wxImage
GetSubImage(const wxRect
& rect
) const;
822 Gets the type of image found by LoadFile() or specified with SaveFile().
826 wxBitmapType
GetType() const;
829 Gets the width of the image in pixels.
833 int GetWidth() const;
836 Converts a color in HSV color space to RGB color space.
838 static wxImage::RGBValue
HSVtoRGB(const wxImage::HSVValue
& hsv
);
841 Returns @true if this image has alpha channel, @false otherwise.
843 @see GetAlpha(), SetAlpha()
845 bool HasAlpha() const;
848 Returns @true if there is a mask active, @false otherwise.
850 bool HasMask() const;
853 Returns @true if the given option is present.
854 The function is case-insensitive to @a name.
856 @see SetOption(), GetOption(), GetOptionInt()
858 bool HasOption(const wxString
& name
) const;
861 Initializes the image alpha channel data.
863 It is an error to call it if the image already has alpha data.
864 If it doesn't, alpha data will be by default initialized to all pixels
865 being fully opaque. But if the image has a mask colour, all mask pixels
866 will be completely transparent.
871 Internal use only. Adds standard image format handlers.
872 It only install wxBMPHandler for the time being, which is used by wxBitmap.
874 This function is called by wxWidgets on startup, and shouldn't be called by
877 @see wxImageHandler, wxInitAllImageHandlers(), wxQuantize
879 static void InitStandardHandlers();
882 Adds a handler at the start of the static list of format handlers.
885 A new image format handler object. There is usually only one instance
886 of a given handler class in an application session.
890 static void InsertHandler(wxImageHandler
* handler
);
893 Returns @true if image data is present.
898 Returns @true if the given pixel is transparent, i.e. either has the mask
899 colour if this image has a mask or if this image has alpha channel and alpha
900 value of this pixel is strictly less than @a threshold.
902 bool IsTransparent(int x
, int y
,
903 unsigned char threshold
= wxIMAGE_ALPHA_THRESHOLD
) const;
906 Loads an image from an input stream.
909 Opened input stream from which to load the image.
910 Currently, the stream must support seeking.
912 May be one of the following:
913 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
914 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
915 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
916 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
917 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
918 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
919 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
920 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
921 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
922 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
923 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
924 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
925 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
927 Index of the image to load in the case that the image file contains
928 multiple images. This is only used by GIF, ICO and TIFF handlers.
929 The default value (-1) means "choose the default image" and is
930 interpreted as the first image (index=0) by the GIF and TIFF handler
931 and as the largest and most colourful one by the ICO handler.
933 @return @true if the operation succeeded, @false otherwise.
934 If the optional index parameter is out of range, @false is
935 returned and a call to wxLogError() takes place.
937 @remarks Depending on how wxWidgets has been configured, not all formats
941 You can use GetOptionInt() to get the hotspot when loading cursor files:
943 int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
944 int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
949 virtual bool LoadFile(wxInputStream
& stream
,
950 wxBitmapType type
= wxBITMAP_TYPE_ANY
,
954 Loads an image from a file.
955 If no handler type is provided, the library will try to autodetect the format.
958 Name of the file from which to load the image.
960 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
962 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
964 virtual bool LoadFile(const wxString
& name
,
965 wxBitmapType type
= wxBITMAP_TYPE_ANY
,
969 Loads an image from a file.
970 If no handler type is provided, the library will try to autodetect the format.
973 Name of the file from which to load the image.
975 MIME type string (for example 'image/jpeg')
977 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
979 virtual bool LoadFile(const wxString
& name
, const wxString
& mimetype
,
984 Loads an image from an input stream.
987 Opened input stream from which to load the image.
988 Currently, the stream must support seeking.
990 MIME type string (for example 'image/jpeg')
992 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
994 virtual bool LoadFile(wxInputStream
& stream
, const wxString
& mimetype
,
998 Returns a mirrored copy of the image.
999 The parameter @a horizontally indicates the orientation.
1001 wxImage
Mirror(bool horizontally
= true) const;
1004 Copy the data of the given @a image to the specified position in this image.
1006 void Paste(const wxImage
& image
, int x
, int y
);
1009 Converts a color in RGB color space to HSV color space.
1011 static wxImage::HSVValue
RGBtoHSV(const wxImage::RGBValue
& rgb
);
1014 Finds the handler with the given name, and removes it.
1015 The handler is not deleted.
1020 @return @true if the handler was found and removed, @false otherwise.
1024 static bool RemoveHandler(const wxString
& name
);
1027 Replaces the colour specified by @e r1,g1,b1 by the colour @e r2,g2,b2.
1029 void Replace(unsigned char r1
, unsigned char g1
,
1030 unsigned char b1
, unsigned char r2
,
1031 unsigned char g2
, unsigned char b2
);
1034 Changes the size of the image in-place by scaling it: after a call to this
1035 function,the image will have the given width and height.
1037 For a description of the @a quality parameter, see the Scale() function.
1038 Returns the (modified) image itself.
1042 wxImage
& Rescale(int width
, int height
,
1043 int quality
= wxIMAGE_QUALITY_NORMAL
);
1046 Changes the size of the image in-place without scaling it by adding either a
1047 border with the given colour or cropping as necessary.
1049 The image is pasted into a new image with the given @a size and background
1050 colour at the position @a pos relative to the upper left of the new image.
1052 If @a red = green = blue = -1 then use either the current mask colour
1053 if set or find, use, and set a suitable mask colour for any newly exposed
1056 @return The (modified) image itself.
1060 wxImage
& Resize(const wxSize
& size
, const wxPoint
& pos
, int red
= -1,
1061 int green
= -1, int blue
= -1);
1064 Rotates the image about the given point, by @a angle radians.
1066 Passing @true to @a interpolating results in better image quality, but is slower.
1068 If the image has a mask, then the mask colour is used for the uncovered
1069 pixels in the rotated image background. Else, black (rgb 0, 0, 0) will be used.
1071 Returns the rotated image, leaving this image intact.
1073 wxImage
Rotate(double angle
, const wxPoint
& rotationCentre
,
1074 bool interpolating
= true,
1075 wxPoint
* offsetAfterRotation
= NULL
) const;
1078 Returns a copy of the image rotated 90 degrees in the direction
1079 indicated by @a clockwise.
1081 wxImage
Rotate90(bool clockwise
= true) const;
1084 Rotates the hue of each pixel in the image by @e angle, which is a double in
1085 the range of -1.0 to +1.0, where -1.0 corresponds to -360 degrees and +1.0
1086 corresponds to +360 degrees.
1088 void RotateHue(double angle
);
1091 Saves an image in the given stream.
1094 Opened output stream to save the image to.
1098 @return @true if the operation succeeded, @false otherwise.
1100 @remarks Depending on how wxWidgets has been configured, not all formats
1104 You can use SetOption() to set the hotspot when saving an image
1105 into a cursor file (default hotspot is in the centre of the image):
1107 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotspotX);
1108 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotspotY);
1113 virtual bool SaveFile(wxOutputStream
& stream
,
1114 const wxString
& mimetype
) const;
1117 Saves an image in the named file.
1120 Name of the file to save the image to.
1122 Currently these types can be used:
1123 @li wxBITMAP_TYPE_BMP: Save a BMP image file.
1124 @li wxBITMAP_TYPE_JPEG: Save a JPEG image file.
1125 @li wxBITMAP_TYPE_PNG: Save a PNG image file.
1126 @li wxBITMAP_TYPE_PCX: Save a PCX image file
1127 (tries to save as 8-bit if possible, falls back to 24-bit otherwise).
1128 @li wxBITMAP_TYPE_PNM: Save a PNM image file (as raw RGB always).
1129 @li wxBITMAP_TYPE_TIFF: Save a TIFF image file.
1130 @li wxBITMAP_TYPE_XPM: Save a XPM image file.
1131 @li wxBITMAP_TYPE_ICO: Save a Windows icon file (ICO).
1132 The size may be up to 255 wide by 127 high. A single image is saved
1133 in 8 colors at the size supplied.
1134 @li wxBITMAP_TYPE_CUR: Save a Windows cursor file (CUR).
1136 virtual bool SaveFile(const wxString
& name
, wxBitmapType type
) const;
1139 Saves an image in the named file.
1142 Name of the file to save the image to.
1146 virtual bool SaveFile(const wxString
& name
, const wxString
& mimetype
) const;
1149 Saves an image in the named file.
1151 File type is determined from the extension of the file name.
1152 Note that this function may fail if the extension is not recognized!
1153 You can use one of the forms above to save images to files with
1154 non-standard extensions.
1157 Name of the file to save the image to.
1159 virtual bool SaveFile(const wxString
& name
) const;
1162 Saves an image in the given stream.
1165 Opened output stream to save the image to.
1169 virtual bool SaveFile(wxOutputStream
& stream
, wxBitmapType type
) const;
1172 Returns a scaled version of the image.
1174 This is also useful for scaling bitmaps in general as the only other way
1175 to scale bitmaps is to blit a wxMemoryDC into another wxMemoryDC.
1177 The parameter @a quality determines what method to use for resampling the image.
1178 Can be one of the following:
1179 - wxIMAGE_QUALITY_NORMAL: Uses the normal default scaling method of pixel
1181 - wxIMAGE_QUALITY_HIGH: Uses bicubic and box averaging resampling methods
1182 for upsampling and downsampling respectively
1184 It should be noted that although using @c wxIMAGE_QUALITY_HIGH produces much nicer
1185 looking results it is a slower method. Downsampling will use the box averaging
1186 method which seems to operate very fast. If you are upsampling larger images using
1187 this method you will most likely notice that it is a bit slower and in extreme
1188 cases it will be quite substantially slower as the bicubic algorithm has to process a
1191 It should also be noted that the high quality scaling may not work as expected
1192 when using a single mask colour for transparency, as the scaling will blur the
1193 image and will therefore remove the mask partially. Using the alpha channel
1198 // get the bitmap from somewhere
1201 // rescale it to have size of 32*32
1202 if ( bmp.GetWidth() != 32 || bmp.GetHeight() != 32 )
1204 wxImage image = bmp.ConvertToImage();
1205 bmp = wxBitmap(image.Scale(32, 32));
1207 // another possibility:
1208 image.Rescale(32, 32);
1215 wxImage
Scale(int width
, int height
,
1216 int quality
= wxIMAGE_QUALITY_NORMAL
) const;
1219 This function is similar to SetData() and has similar restrictions.
1221 The pointer passed to it may however be @NULL in which case the function
1222 will allocate the alpha array internally -- this is useful to add alpha
1223 channel data to an image which doesn't have any.
1225 If the pointer is not @NULL, it must have one byte for each image pixel
1226 and be allocated with malloc().
1227 wxImage takes ownership of the pointer and will free it unless @a static_data
1228 parameter is set to @true -- in this case the caller should do it.
1230 void SetAlpha(unsigned char* alpha
= NULL
,
1231 bool static_data
= false);
1234 Sets the alpha value for the given pixel.
1235 This function should only be called if the image has alpha channel data,
1236 use HasAlpha() to check for this.
1238 void SetAlpha(int x
, int y
, unsigned char alpha
);
1242 Sets the image data without performing checks.
1244 The data given must have the size (width*height*3) or results will be
1245 unexpected. Don't use this method if you aren't sure you know what you
1248 The data must have been allocated with @c malloc(), @b NOT with
1251 After this call the pointer to the data is owned by the wxImage object,
1252 that will be responsible for deleting it.
1253 Do not pass to this function a pointer obtained through GetData().
1255 void SetData(unsigned char* data
, bool static_data
= false);
1256 void SetData(unsigned char* data
, int new_width
, int new_height
,
1257 bool static_data
= false);
1261 Specifies whether there is a mask or not.
1263 The area of the mask is determined by the current mask colour.
1265 void SetMask(bool hasMask
= true);
1268 Sets the mask colour for this image (and tells the image to use the mask).
1270 void SetMaskColour(unsigned char red
, unsigned char green
,
1271 unsigned char blue
);
1274 Sets image's mask so that the pixels that have RGB value of mr,mg,mb in
1275 mask will be masked in the image.
1277 This is done by first finding an unused colour in the image, setting
1278 this colour as the mask colour and then using this colour to draw all
1279 pixels in the image who corresponding pixel in mask has given RGB value.
1281 The parameter @a mask is the mask image to extract mask shape from.
1282 It must have the same dimensions as the image.
1284 The parameters @a mr, @a mg, @a mb are the RGB values of the pixels in
1285 mask that will be used to create the mask.
1287 @return Returns @false if mask does not have same dimensions as the image
1288 or if there is no unused colour left. Returns @true if the mask
1289 was successfully applied.
1292 Note that this method involves computing the histogram, which is a
1293 computationally intensive operation.
1295 bool SetMaskFromImage(const wxImage
& mask
, unsigned char mr
,
1301 Sets a user-defined option. The function is case-insensitive to @a name.
1303 For example, when saving as a JPEG file, the option @b quality is
1304 used, which is a number between 0 and 100 (0 is terrible, 100 is very good).
1306 @see GetOption(), GetOptionInt(), HasOption()
1308 void SetOption(const wxString
& name
, const wxString
& value
);
1309 void SetOption(const wxString
& name
, int value
);
1313 Associates a palette with the image.
1315 The palette may be used when converting wxImage to wxBitmap (MSW only at present)
1316 or in file save operations (none as yet).
1318 void SetPalette(const wxPalette
& palette
);
1321 Sets the colour of the pixels within the given rectangle.
1323 This routine performs bounds-checks for the coordinate so it can be considered
1324 a safe way to manipulate the data.
1326 void SetRGB(const wxRect
& rect
,
1328 unsigned char green
,
1329 unsigned char blue
);
1332 Set the type of image returned by GetType().
1334 This method is mostly used internally by the library but can also be
1335 called from the user code if the image was created from data in the
1336 given bitmap format without using LoadFile() (which would set the type
1337 correctly automatically).
1339 Notice that the image must be created before this function is called.
1344 One of bitmap type constants, @c wxBITMAP_TYPE_INVALID is a valid
1345 value for it and can be used to reset the bitmap type to default
1346 but @c wxBITMAP_TYPE_MAX is not allowed here.
1348 void SetType(wxBitmapType type
);
1351 Returns a resized version of this image without scaling it by adding either a
1352 border with the given colour or cropping as necessary.
1354 The image is pasted into a new image with the given @a size and background
1355 colour at the position @a pos relative to the upper left of the new image.
1357 If @a red = green = blue = -1 then the areas of the larger image not covered
1358 by this image are made transparent by filling them with the image mask colour
1359 (which will be allocated automatically if it isn't currently set).
1361 Otherwise, the areas will be filled with the colour with the specified RGB components.
1365 wxImage
Size(const wxSize
& size
, const wxPoint
& pos
, int red
= -1,
1366 int green
= -1, int blue
= -1) const;
1369 Assignment operator, using @ref overview_refcount "reference counting".
1374 @return Returns 'this' object.
1376 wxImage
& operator=(const wxImage
& image
);
1379 // ============================================================================
1380 // Global functions/macros
1381 // ============================================================================
1383 /** @ingroup group_funcmacro_appinitterm */
1387 Initializes all available image handlers. For a list of available handlers,
1390 @see wxImage, wxImageHandler
1394 void wxInitAllImageHandlers();