]>
git.saurik.com Git - wxWidgets.git/blob - interface/image.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: documentation for wxImageHandler class
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 This is the base class for implementing image file loading/saving, and image
15 It is used within wxImage and is not normally seen by the application.
17 If you wish to extend the capabilities of wxImage, derive a class from
19 and add the handler using wxImage::AddHandler in your
20 application initialisation.
26 wxImage, wxInitAllImageHandlers
28 class wxImageHandler
: public wxObject
32 Default constructor. In your own default constructor, initialise the members
33 m_name, m_extension and m_type.
38 Destroys the wxImageHandler object.
43 Gets the file extension associated with this handler.
45 const wxString
GetExtension();
48 If the image file contains more than one image and the image handler is capable
49 of retrieving these individually, this function will return the number of
53 Opened input stream for reading image data. Currently, the stream must support
56 @returns Number of available images. For most image handlers, this is 1
57 (exceptions are TIFF and ICO formats).
59 int GetImageCount(wxInputStream
& stream
);
62 Gets the MIME type associated with this handler.
64 const wxString
GetMimeType();
67 Gets the name of this handler.
69 const wxString
GetName();
72 Gets the image type associated with this handler.
77 Loads a image from a stream, putting the resulting data into @e image. If the
79 more than one image and the image handler is capable of retrieving these
80 individually, @e index
81 indicates which image to read from the stream.
84 The image object which is to be affected by this operation.
87 Opened input stream for reading image data.
90 If set to @true, errors reported by the image handler will produce wxLogMessages.
93 The index of the image in the file (starting from zero).
95 @returns @true if the operation succeeded, @false otherwise.
97 @sa wxImage::LoadFile, wxImage::SaveFile, SaveFile()
99 bool LoadFile(wxImage
* image
, wxInputStream
& stream
,
100 bool verbose
=@
true, int index
=0);
103 Saves a image in the output stream.
106 The image object which is to be affected by this operation.
109 Opened output stream for writing the data.
111 @returns @true if the operation succeeded, @false otherwise.
113 @sa wxImage::LoadFile, wxImage::SaveFile, LoadFile()
115 bool SaveFile(wxImage
* image
, wxOutputStream
& stream
);
118 Sets the handler extension.
123 void SetExtension(const wxString
& extension
);
126 Sets the handler MIME type.
131 void SetMimeType(const wxString
& mimetype
);
134 Sets the handler name.
139 void SetName(const wxString
& name
);
147 This class encapsulates a platform-independent image. An image can be created
148 from data, or using wxBitmap::ConvertToImage. An image
149 can be loaded from a file in a variety of formats, and is extensible to new
151 via image format handlers. Functions are available to set and get image bits, so
152 it can be used for basic image manipulation.
154 A wxImage cannot (currently) be drawn directly to a wxDC. Instead,
155 a platform-specific wxBitmap object must be created from it using
156 the wxBitmap::wxBitmap(wxImage,int depth) constructor.
158 be drawn in a device context, using wxDC::DrawBitmap.
160 One colour value of the image may be used as a mask colour which will lead to
162 creation of a wxMask object associated to the bitmap object.
168 wxBitmap, wxInitAllImageHandlers
170 class wxImage
: public wxObject
175 Creates an image from XPM data.
178 Specifies the width of the image.
181 Specifies the height of the image.
184 Name of the file from which to load the image.
187 Opened input stream from which to load the image. Currently, the stream must
191 May be one of the following:
196 Load a Windows bitmap file.
201 Load a GIF bitmap file.
206 Load a JPEG bitmap file.
211 Load a PNG bitmap file.
216 Load a PCX bitmap file.
221 Load a PNM bitmap file.
226 Load a TIFF bitmap file.
231 Load a TGA bitmap file.
236 Load a XPM bitmap file.
241 Load a Windows icon file (ICO).
246 Load a Windows cursor file (CUR).
251 Load a Windows animated cursor file (ANI).
256 Will try to autodetect the format.
259 MIME type string (for example 'image/jpeg')
262 Index of the image to load in the case that the image file contains multiple
264 This is only used by GIF, ICO and TIFF handlers. The default value (-1) means
265 "choose the default image" and is interpreted as the first image (index=0) by
266 the GIF and TIFF handler and as the largest and most colourful one by the ICO
270 A pointer to XPM image data.
272 @remarks Depending on how wxWidgets has been configured, not all formats
278 wxImage(const wxImage
& image
);
279 wxImage(const wxBitmap
& bitmap
);
280 wxImage(int width
, int height
, bool clear
=@
true);
281 wxImage(int width
, int height
, unsigned char* data
,
282 bool static_data
= @
false);
283 wxImage(const wxString
& name
, long type
= wxBITMAP_TYPE_ANY
,
285 wxImage(const wxString
& name
, const wxString
& mimetype
,
287 wxImage(wxInputStream
& stream
, long type
= wxBITMAP_TYPE_ANY
,
289 wxImage(wxInputStream
& stream
, const wxString
& mimetype
,
291 wxImage(const char* const* xpmData
);
296 See @ref overview_refcountdestruct "reference-counted object destruction" for
303 returns @true if the current image handlers can read this file
305 static void AddHandler(wxImageHandler
* handler
);
306 See also
bool CanRead(const wxString
& filename
);
310 Blurs the image in both horizontal and vertical directions by the specified
312 @e blurRadius. This should not be used when using a single mask colour
315 @sa @ref horzblur() BlurHorizontal, @ref vertblur() BlurVertical
317 wxImage
Blur(int blurRadius
);
320 Blurs the image in the horizontal direction only. This should not be used
321 when using a single mask colour for transparency.
323 @sa Blur(), @ref vertblur() BlurVertical
325 wxImage
BlurHorizontal(int blurRadius
);
328 Blurs the image in the vertical direction only. This should not be used
329 when using a single mask colour for transparency.
331 @sa Blur(), @ref horzblur() BlurHorizontal
333 wxImage
BlurVertical(int blurRadius
);
336 Deletes all image handlers.
338 This function is called by wxWidgets on exit.
340 static void CleanUpHandlers();
343 Computes the histogram of the image. @e histogram is a reference to
344 wxImageHistogram object. wxImageHistogram is a specialization of
345 wxHashMap "template" and is defined as follows:
347 @returns Returns number of colours in the histogram.
349 unsigned long ComputeHistogram(wxImageHistogram
& histogram
);
352 If the image has alpha channel, this method converts it to mask. All pixels
353 with alpha value less than @e threshold are replaced with mask colour
354 and the alpha channel is removed. Mask colour is chosen automatically using
355 FindFirstUnusedColour().
357 If the image image doesn't have alpha channel,
358 ConvertAlphaToMask does nothing.
360 @returns @false if FindFirstUnusedColour returns @false, @true otherwise.
362 bool ConvertAlphaToMask(unsigned char threshold
= 128);
365 Deprecated, use equivalent @ref wxBitmap::ctor "wxBitmap constructor"
366 (which takes wxImage and depth as its arguments) instead.
368 wxBitmap
ConvertToBitmap();
371 Returns a greyscale version of the image. The returned image uses the luminance
372 component of the original to calculate the greyscale. Defaults to using
373 ITU-T BT.601 when converting to YUV, where every pixel equals
374 (R * @e lr) + (G * @e lg) + (B * @e lb).
376 wxImage
ConvertToGreyscale(double lr
= 0.299, double lg
= 0.587,
380 Returns monochromatic version of the image. The returned image has white
381 colour where the original has @e (r,g,b) colour and black colour
384 wxImage
ConvertToMono(unsigned char r
, unsigned char g
,
388 Returns an identical copy of the image.
393 Creates a fresh image. If @e clear is @true, the new image will be initialized
395 Otherwise, the image data will be uninitialized.
398 The width of the image in pixels.
401 The height of the image in pixels.
403 @returns @true if the call succeeded, @false otherwise.
405 bool Create(int width
, int height
, bool clear
=@
true);
408 Destroys the image data.
414 Pointers to variables to save the colour.
416 @param startR,startG,startB
417 Initial values of the colour. Returned colour
418 will have RGB values equal to or greater than these.
420 @returns Returns @false if there is no unused colour left, @true on success.
422 bool FindFirstUnusedColour(unsigned char * r
, unsigned char * g
,
424 unsigned char startR
= 1,
425 unsigned char startG
= 0,
426 unsigned char startB
= 0);
430 Finds the handler associated with the given MIME type.
436 The file extension, such as "bmp".
439 The image type, such as wxBITMAP_TYPE_BMP.
444 @returns A pointer to the handler if found, @NULL otherwise.
448 static wxImageHandler
* FindHandler(const wxString
& name
);
449 static wxImageHandler
* FindHandler(const wxString
& extension
,
451 static wxImageHandler
* FindHandler(long imageType
);
452 static wxImageHandler
* FindHandlerMime(const wxString
& mimetype
);
457 Returns pointer to the array storing the alpha values for this image. This
458 pointer is @NULL for the images without the alpha channel. If the image
459 does have it, this pointer may be used to directly manipulate the alpha values
460 which are stored as the @ref getdata() RGB ones.
462 unsigned char GetAlpha(int x
, int y
);
463 unsigned char * GetAlpha();
467 Returns the blue intensity at the given coordinate.
469 unsigned char GetBlue(int x
, int y
);
472 Returns the image data as an array. This is most often used when doing
473 direct image manipulation. The return value points to an array of
474 characters in RGBRGBRGB... format in the top-to-bottom, left-to-right
475 order, that is the first RGB triplet corresponds to the pixel first pixel of
476 the first row, the second one --- to the second pixel of the first row and so
477 on until the end of the first row, with second row following after it and so
480 You should not delete the returned pointer nor pass it to
483 unsigned char* GetData();
486 Returns the green intensity at the given coordinate.
488 unsigned char GetGreen(int x
, int y
);
491 Returns the static list of image format handlers.
495 static wxList
GetHandlers();
498 Gets the height of the image in pixels.
504 If the image file contains more than one image and the image handler is capable
505 of retrieving these individually, this function will return the number of
509 Name of the file to query.
512 Opened input stream with image data. Currently, the stream must support seeking.
515 May be one of the following:
520 Load a Windows bitmap file.
525 Load a GIF bitmap file.
530 Load a JPEG bitmap file.
535 Load a PNG bitmap file.
540 Load a PCX bitmap file.
545 Load a PNM bitmap file.
550 Load a TIFF bitmap file.
555 Load a XPM bitmap file.
560 Load a Windows icon file (ICO).
565 Load a Windows cursor file (CUR).
570 Load a Windows animated cursor file (ANI).
575 Will try to autodetect the format.
577 @returns Number of available images. For most image handlers, this is 1
578 (exceptions are TIFF and ICO formats).
580 static int GetImageCount(const wxString
& filename
,
581 long type
= wxBITMAP_TYPE_ANY
);
582 static int GetImageCount(wxInputStream
& stream
,
583 long type
= wxBITMAP_TYPE_ANY
);
587 Iterates all registered wxImageHandler objects, and returns a string containing
589 suitable for passing to file open/save dialog boxes.
591 @returns The format of the returned string is
592 "(*.ext1;*.ext2)|*.ext1;*.ext2".
596 static wxString
GetImageExtWildcard();
599 Gets the blue value of the mask colour.
601 unsigned char GetMaskBlue();
604 Gets the green value of the mask colour.
606 unsigned char GetMaskGreen();
609 Gets the red value of the mask colour.
611 unsigned char GetMaskRed();
614 Gets a user-defined option. The function is case-insensitive to @e name.
616 For example, when saving as a JPEG file, the option @b quality is
617 used, which is a number between 0 and 100 (0 is terrible, 100 is very good).
619 @sa SetOption(), GetOptionInt(), HasOption()
621 wxString
GetOption(const wxString
& name
);
624 Gets a user-defined option as an integer. The function is case-insensitive to
627 If the given option is not present, the function returns 0. Use
628 HasOption() is 0 is a possibly valid value
631 Options for wxPNGHandler
634 wxIMAGE_OPTION_PNG_FORMAT
637 Format for saving a PNG file.
639 wxIMAGE_OPTION_PNG_BITDEPTH
642 Bit depth for every channel (R/G/B/A).
644 Supported values for wxIMAGE_OPTION_PNG_FORMAT:
655 Stores grey image, converts from RGB.
660 Stores grey image, uses red value as grey.
663 @sa SetOption(), GetOption()
665 int GetOptionInt(const wxString
& name
);
668 Get the current mask colour or find a suitable unused colour that could be
669 used as a mask colour. Returns @true if the image currently has a mask.
671 bool GetOrFindMaskColour(unsigned char r
, unsigned char g
,
675 Returns the palette associated with the image. Currently the palette is only
676 used when converting to wxBitmap under Windows. Some of the wxImage handlers
677 have been modified to set the palette if one exists in the image file (usually
678 256 or less colour images in GIF or PNG format).
680 const wxPalette
GetPalette();
683 Returns the red intensity at the given coordinate.
685 unsigned char GetRed(int x
, int y
);
688 Returns a sub image of the current one as long as the rect belongs entirely to
691 wxImage
GetSubImage(const wxRect
& rect
);
694 Gets the width of the image in pixels.
701 Constructor for HSVValue, an object that contains values for hue, saturation
703 represent the value of a color. It is used by HSVtoRGB()
704 and RGBtoHSV(), which
705 converts between HSV color space and RGB color space.
707 HSVValue(double h
= 0.0, double s
= 0.0, double v
= 0.0);
710 Converts a color in HSV color space to RGB color space.
712 #define wxImage::RGBValue HSVtoRGB(const HSVValue & hsv) /* implementation is private */
715 Returns @true if this image has alpha channel, @false otherwise.
717 @sa GetAlpha(), SetAlpha()
722 Returns @true if there is a mask active, @false otherwise.
727 Returns @true if the given option is present. The function is case-insensitive
730 @sa SetOption(), GetOption(), GetOptionInt()
732 bool HasOption(const wxString
& name
);
735 Initializes the image alpha channel data. It is an error to call it
736 if the image already has alpha data. If it doesn't, alpha data will be
737 by default initialized to all pixels being fully opaque. But if the image has a
738 a mask colour, all mask pixels will be completely transparent.
743 Internal use only. Adds standard image format handlers. It only install BMP
744 for the time being, which is used by wxBitmap.
746 This function is called by wxWidgets on startup, and shouldn't be called by
749 @sa wxImageHandler, wxInitAllImageHandlers, wxQuantize
751 static void InitStandardHandlers();
754 Adds a handler at the start of the static list of format handlers.
757 A new image format handler object. There is usually only one instance
758 of a given handler class in an application session.
762 static void InsertHandler(wxImageHandler
* handler
);
765 Returns @true if image data is present.
767 #define bool IsOk() /* implementation is private */
770 Returns @true if the given pixel is transparent, i.e. either has the mask
771 colour if this image has a mask or if this image has alpha channel and alpha
772 value of this pixel is strictly less than @e threshold.
774 bool IsTransparent(int x
, int y
, unsigned char threshold
= 128);
778 Loads an image from an input stream.
781 Name of the file from which to load the image.
784 Opened input stream from which to load the image. Currently, the stream must
788 One of the following values:
793 Load a Windows image file.
798 Load a GIF image file.
803 Load a JPEG image file.
808 Load a PCX image file.
813 Load a PNG image file.
818 Load a PNM image file.
823 Load a TIFF image file.
828 Load a XPM image file.
833 Load a Windows icon file (ICO).
838 Load a Windows cursor file (CUR).
843 Load a Windows animated cursor file (ANI).
848 Will try to autodetect the format.
851 MIME type string (for example 'image/jpeg')
854 Index of the image to load in the case that the image file contains multiple
856 This is only used by GIF, ICO and TIFF handlers. The default value (-1) means
857 "choose the default image" and is interpreted as the first image (index=0) by
858 the GIF and TIFF handler and as the largest and most colourful one by the ICO
861 @returns @true if the operation succeeded, @false otherwise. If the
862 optional index parameter is out of range, @false is
863 returned and a call to wxLogError() takes place.
865 @remarks Depending on how wxWidgets has been configured, not all formats
870 bool LoadFile(const wxString
& name
,
871 long type
= wxBITMAP_TYPE_ANY
,
873 bool LoadFile(const wxString
& name
, const wxString
& mimetype
,
875 bool LoadFile(wxInputStream
& stream
, long type
,
877 bool LoadFile(wxInputStream
& stream
,
878 const wxString
& mimetype
,
883 Returns a mirrored copy of the image. The parameter @e horizontally
884 indicates the orientation.
886 wxImage
Mirror(bool horizontally
= @
true);
889 Copy the data of the given @e image to the specified position in this image.
891 void Paste(const wxImage
& image
, int x
, int y
);
894 Constructor for RGBValue, an object that contains values for red, green and
896 represent the value of a color. It is used by HSVtoRGB()
897 and RGBtoHSV(), which
898 converts between HSV color space and RGB color space.
900 RGBValue(unsigned char r
= 0, unsigned char g
= 0,
901 unsigned char b
= 0);
904 Converts a color in RGB color space to HSV color space.
906 #define wxImage::HSVValue RGBtoHSV(const RGBValue& rgb) /* implementation is private */
909 Finds the handler with the given name, and removes it. The handler
915 @returns @true if the handler was found and removed, @false otherwise.
919 static bool RemoveHandler(const wxString
& name
);
922 Replaces the colour specified by @e r1,g1,b1 by the colour @e r2,g2,b2.
924 void Replace(unsigned char r1
, unsigned char g1
,
925 unsigned char b1
, unsigned char r2
,
926 unsigned char g2
, unsigned char b2
);
929 Changes the size of the image in-place by scaling it: after a call to this
931 the image will have the given width and height.
933 For a description of the @e quality parameter, see the Scale() function.
935 Returns the (modified) image itself.
939 wxImage
Rescale(int width
, int height
,
940 int quality
= wxIMAGE_QUALITY_NORMAL
);
943 Changes the size of the image in-place without scaling it by adding either a
945 with the given colour or cropping as necessary. The image is pasted into a new
946 image with the given @e size and background colour at the position @e pos
947 relative to the upper left of the new image. If @e red = green = blue = -1
948 then use either the current mask colour if set or find, use, and set a
949 suitable mask colour for any newly exposed areas.
951 Returns the (modified) image itself.
955 wxImage
Resize(const wxSize
& size
, const wxPoint pos
,
956 int red
= -1, int green
= -1,
960 Rotates the image about the given point, by @e angle radians. Passing @true
961 to @e interpolating results in better image quality, but is slower. If the
962 image has a mask, then the mask colour is used for the uncovered pixels in the
963 rotated image background. Else, black (rgb 0, 0, 0) will be used.
965 Returns the rotated image, leaving this image intact.
967 wxImage
Rotate(double angle
, const wxPoint
& rotationCentre
,
968 bool interpolating
= @
true,
969 wxPoint
* offsetAfterRotation
= @NULL
);
972 Returns a copy of the image rotated 90 degrees in the direction
973 indicated by @e clockwise.
975 wxImage
Rotate90(bool clockwise
= @
true);
978 Rotates the hue of each pixel in the image by @e angle, which is a double in
979 the range of -1.0 to +1.0, where -1.0 corresponds to -360 degrees and +1.0
983 void RotateHue(double angle
);
987 Saves an image in the given stream.
990 Name of the file to save the image to.
993 Opened output stream to save the image to.
996 Currently these types can be used:
1001 Save a BMP image file.
1006 Save a JPEG image file.
1011 Save a PNG image file.
1016 Save a PCX image file (tries to save as 8-bit if possible, falls back to 24-bit
1022 Save a PNM image file (as raw RGB always).
1027 Save a TIFF image file.
1032 Save a XPM image file.
1037 Save a Windows icon file (ICO) (the size may be up to 255 wide by 127 high. A
1038 single image is saved in 8 colors at the size supplied).
1043 Save a Windows cursor file (CUR).
1048 @returns @true if the operation succeeded, @false otherwise.
1050 @remarks Depending on how wxWidgets has been configured, not all formats
1055 bool SaveFile(const wxString
& name
, int type
);
1056 bool SaveFile(const wxString
& name
, const wxString
& mimetype
);
1057 bool SaveFile(const wxString
& name
);
1058 bool SaveFile(wxOutputStream
& stream
, int type
);
1059 bool SaveFile(wxOutputStream
& stream
,
1060 const wxString
& mimetype
);
1064 Returns a scaled version of the image. This is also useful for
1065 scaling bitmaps in general as the only other way to scale bitmaps
1066 is to blit a wxMemoryDC into another wxMemoryDC.
1068 It should be noted that although using wxIMAGE_QUALITY_HIGH produces much nicer
1069 looking results it is a slower method. Downsampling will use the box averaging
1071 which seems to operate very fast. If you are upsampling larger images using
1072 this method you will most likely notice that it is a bit slower and in extreme
1074 it will be quite substantially slower as the bicubic algorithm has to process a
1078 It should also be noted that the high quality scaling may not work as expected
1079 when using a single mask colour for transparency, as the scaling will blur the
1080 image and will therefore remove the mask partially. Using the alpha channel
1086 Determines what method to use for resampling the image. Can be one of the
1089 wxIMAGE_QUALITY_NORMAL
1092 Uses the normal default scaling method of pixel replication
1094 wxIMAGE_QUALITY_HIGH
1097 Uses bicubic and box averaging resampling methods for upsampling and
1098 downsampling respectively
1102 wxImage
Scale(int width
, int height
,
1103 int quality
= wxIMAGE_QUALITY_NORMAL
);
1107 Sets the alpha value for the given pixel. This function should only be called
1108 if the image has alpha channel data, use HasAlpha() to
1111 void SetAlpha(unsigned char * alpha
= @NULL
,
1112 bool static_data
= @
false);
1113 void SetAlpha(int x
, int y
, unsigned char alpha
);
1117 Sets the image data without performing checks. The data given must have
1118 the size (width*height*3) or results will be unexpected. Don't use this
1119 method if you aren't sure you know what you are doing.
1121 The data must have been allocated with @c malloc(), @b NOT with
1124 After this call the pointer to the data is owned by the wxImage object,
1125 that will be responsible for deleting it.
1126 Do not pass to this function a pointer obtained through
1129 void SetData(unsigned char* data
);
1132 Specifies whether there is a mask or not. The area of the mask is determined by
1133 the current mask colour.
1135 void SetMask(bool hasMask
= @
true);
1138 Sets the mask colour for this image (and tells the image to use the mask).
1140 void SetMaskColour(unsigned char red
, unsigned char green
,
1141 unsigned char blue
);
1145 The mask image to extract mask shape from. Must have same dimensions as the
1149 RGB value of pixels in mask that will be used to create the mask.
1151 @returns Returns @false if mask does not have same dimensions as the image
1152 or if there is no unused colour left. Returns @true if
1153 the mask was successfully applied.
1155 bool SetMaskFromImage(const wxImage
& mask
, unsigned char mr
,
1161 Sets a user-defined option. The function is case-insensitive to @e name.
1163 For example, when saving as a JPEG file, the option @b quality is
1164 used, which is a number between 0 and 100 (0 is terrible, 100 is very good).
1166 @sa GetOption(), GetOptionInt(), HasOption()
1168 void SetOption(const wxString
& name
, const wxString
& value
);
1169 void SetOption(const wxString
& name
, int value
);
1173 Associates a palette with the image. The palette may be used when converting
1174 wxImage to wxBitmap (MSW only at present) or in file save operations (none as
1177 void SetPalette(const wxPalette
& palette
);
1180 Sets the colour of the pixels within the given rectangle. This routine performs
1181 bounds-checks for the coordinate so it can be considered a safe way to
1185 #define void SetRGB(wxRect & rect, unsigned char red,
1186 unsigned char green
,
1187 unsigned char blue
) /* implementation is private */
1190 Returns a resized version of this image without scaling it by adding either a
1192 with the given colour or cropping as necessary. The image is pasted into a new
1193 image with the given @e size and background colour at the position @e pos
1194 relative to the upper left of the new image. If @e red = green = blue = -1
1195 then the areas of the larger image not covered by this image are made
1196 transparent by filling them with the image mask colour (which will be allocated
1197 automatically if it isn't currently set). Otherwise, the areas will be filled
1198 with the colour with the specified RGB components.
1202 wxImage
Size(const wxSize
& size
, const wxPoint pos
, int red
= -1,
1203 int green
= -1, int blue
= -1);
1206 Assignment operator, using @ref overview_trefcount "reference counting".
1211 @returns Returns 'this' object.
1213 wxImage
operator =(const wxImage
& image
);
1217 // ============================================================================
1218 // Global functions/macros
1219 // ============================================================================
1222 Initializes all available image handlers. For a list of available handlers,
1225 @sa wxImage, wxImageHandler
1227 void wxInitAllImageHandlers();