This class encapsulates a platform-independent image. An image can be created from data, or using wxBitmap::ConvertToImage. An image can be loaded from a file in a variety of formats, and is extensible to new formats via image format handlers. Functions are available to set and get image bits, so it can be used for basic image manipulation.
A wxImage cannot (currently) be drawn directly to a wxDC. Instead, a platform-specific wxBitmap object must be created from it using the wxBitmap::wxBitmap(wxImage,int depth) constructor. This bitmap can then be drawn in a device context, using wxDC::DrawBitmap.
One colour value of the image may be used as a mask colour which will lead to the automatic creation of a wxMask object associated to the bitmap object.
Alpha channel support
Starting from wxWidgets 2.5.0 wxImage supports alpha channel data, that is in
addition to a byte for the red, green and blue colour components for each pixel
it also stores a byte representing the pixel opacity. An alpha value of
corresponds to a transparent pixel (null opacity) while a value of
means that the pixel is 100% opaque.
Unlike RGB data, not all images have an alpha channel and before using GetAlpha you should check if this image contains an alpha channel with HasAlpha. Note that currently only images loaded from PNG files with transparency information will have an alpha channel but alpha support will be added to the other formats as well (as well as support for saving images with alpha channel which also isn't implemented).
Available image handlers
The following image handlers are available. wxBMPHandler is always installed by default. To use other image formats, install the appropriate handler with wxImage::AddHandler or wxInitAllImageHandlers.
When saving in PCX format, wxPCXHandler will count the number of different colours in the image; if there are 256 or less colours, it will save as 8 bit, else it will save as 24 bit.
Loading PNMs only works for ASCII or raw RGB images. When saving in PNM format, wxPNMHandler will always save as raw RGB.
Derived from
Include files
<wx/image.h>
See also
wxBitmap, wxInitAllImageHandlers
Default constructor.
Copy constructor.
(Deprecated form, use wxBitmap::ConvertToImage instead.) Constructs an image from a platform-dependent bitmap. This preserves mask information so that bitmaps and images can be converted back and forth without loss in that respect.
Creates an image with the given width and height. If clear is true, the new image will be initialized to black. Otherwise, the image data will be uninitialized.
Creates an image from given data with the given width and height. If static_data is true, then wxImage will not delete the actual image data in its destructor, otherwise it will free it by calling free().
Loads an image from a file.
Loads an image from an input stream.
Creates an image from XPM data.
Parameters
Remarks
Depending on how wxWidgets has been configured, not all formats may be available.
Note: any handler other than BMP must be previously initialized with wxImage::AddHandler or wxInitAllImageHandlers.
Note: you can use GetOptionInt to get the hotspot for loaded cursor file:
int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
See also
wxPython での注意点: Constructors supported by wxPython are:
2cm
wxImage(name, flag) | Loads an image from a file |
wxNullImage() | Create a null image (has no size or image data) |
wxEmptyImage(width, height) | Creates an empty image of the given size |
wxImageFromMime(name, mimetype | Creates an image from the given file of the given mimetype |
wxImageFromBitmap(bitmap) | Creates an image from a platform-dependent bitmap |
wxPerl での注意点: Constructors supported by wxPerl are:
Destructor.
Adds a handler to the end of the static list of format handlers.
See also
returns true if the current image handlers can read this file
wxPython での注意点: In wxPython this static method is named wxImage_AddHandler.
Deletes all image handlers.
This function is called by wxWidgets on exit.
unsigned long ComputeHistogram(wxImageHistogram& histogram) const
Computes the histogram of the image. histogram is a reference to wxImageHistogram object. wxImageHistogram is a specialization of wxHashMap "template" and is defined as follows:
class WXDLLEXPORT wxImageHistogramEntry { public: wxImageHistogramEntry() : index(0), value(0) {} unsigned long index; unsigned long value; }; WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry, wxIntegerHash, wxIntegerEqual, wxImageHistogram);
Return value
Returns number of colours in the histogram.
If the image has alpha channel, this method converts it to mask. All pixels with alpha value less than threshold are replaced with mask colour and the alpha channel is removed. Mask colour is chosen automatically using FindFirstUnusedColour.
If the image image doesn't have alpha channel, ConvertAlphaToMask does nothing.
Return value
FALSEif FindFirstUnusedColour returns FALSE, TRUEotherwise.
wxBitmap ConvertToBitmap(void) const
Deprecated, use equivalent wxBitmap constructor (which takes wxImage and depth as its arguments) instead.
wxImage ConvertToMono(unsigned char r, unsigned char g, unsigned char b) const
Returns monochromatic version of the image. The returned image has white colour where the original has (r,g,b) colour and black colour everywhere else.
Returns an identical copy of the image.
Creates a fresh image. If clear is true, the new image will be initialized to black. Otherwise, the image data will be uninitialized.
Parameters
Return value
true if the call succeeded, false otherwise.
Destroys the image data.
Parameters
Finds the first colour that is never used in the image. The search begins at given initial colour and continues by increasing R, G and B components (in this order) by 1 until an unused colour is found or the colour space exhausted.
Return value
Returns false if there is no unused colour left, true on success.
Notes
Note that this method involves computing the histogram, which is computationally intensive operation.
Finds the handler with the given name.
Finds the handler associated with the given extension and type.
Finds the handler associated with the given image type.
Finds the handler associated with the given MIME type.
Return value
A pointer to the handler if found, NULL otherwise.
See also
Iterates all registered wxImageHandler objects, and returns a string containing file extension masks suitable for passing to file open/save dialog boxes.
Return value
The format of the returned string is "(*.ext1;*.ext2)|*.ext1;*.ext2".
It is usually a good idea to prepend a description before passing the result to the dialog.
Example:
wxFileDialog FileDlg( this, "Choose Image", ::wxGetWorkingDirectory(), "", _("Image Files ") + wxImage::GetImageExtWildcard(), wxOPEN );
See also
unsigned char GetAlpha(int x, int y) const
Returns the alpha value for the given pixel. This function may only be called for the images with alpha channel, use HasAlpha to check for this.
The returned value is the opacity of the image, i.e. the value of
corresponds to the transparent pixels while the value of
- to the opaque
ones.
unsigned char * GetAlpha(void) const
Returns pointer to the array storing the alpha values for this image. This pointer is NULL for the images without the alpha channel. If the image does have it, this pointer may be used to directly manipulate the alpha values which are stored as the RGB ones.
unsigned char GetBlue(int x, int y) const
Returns the blue intensity at the given coordinate.
unsigned char* GetData(void) const
Returns the image data as an array. This is most often used when doing
direct image manipulation. The return value points to an array of
characters in RGBRGBRGB format in the top-to-bottom, left-to-right
order, that is the first RGB triplet corresponds to the pixel first pixel of
the first row, the second one -- to the second pixel of the first row and so
on until the end of the first row, with second row following after it and so
on.
You should not delete the returned pointer nor pass it to wxImage::SetData.
unsigned char GetGreen(int x, int y) const
Returns the green intensity at the given coordinate.
If the image file contains more than one image and the image handler is capable of retrieving these individually, this function will return the number of available images.
Return value
Number of available images. For most image handlers, this is 1 (exceptions are TIFF and ICO formats).
Returns the static list of image format handlers.
See also
Gets the height of the image in pixels.
unsigned char GetMaskBlue(void) const
Gets the blue value of the mask colour.
unsigned char GetMaskGreen(void) const
Gets the green value of the mask colour.
unsigned char GetMaskRed(void) const
Gets the red value of the mask colour.
bool GetOrFindMaskColour(unsigned char *r, unsigned char *g, unsigned char *b) const
Get the current mask colour or find a suitable unused colour that could be used as a mask colour. Returns true if the image currently has a mask.
const wxPalette& GetPalette(void) const
Returns the palette associated with the image. Currently the palette is only used when converting to wxBitmap under Windows.
Eventually wxImage handlers will set the palette if one exists in the image file.
unsigned char GetRed(int x, int y) const
Returns the red intensity at the given coordinate.
wxImage GetSubImage(const wxRect& rect) const
Returns a sub image of the current one as long as the rect belongs entirely to the image.
Gets the width of the image in pixels.
See also
Returns true if this image has alpha channel, false otherwise.
See also
Returns true if there is a mask active, false otherwise.
wxString GetOption(const wxString& name) const
Gets a user-defined option. The function is case-insensitive to name.
For example, when saving as a JPEG file, the option quality is used, which is a number between 0 and 100 (0 is terrible, 100 is very good).
See also
wxImage::SetOption, wxImage::GetOptionInt, wxImage::HasOption
int GetOptionInt(const wxString& name) const
Gets a user-defined option as an integer. The function is case-insensitive to name.
If the given option is not present, the function returns . Use
wxImage::HasOption is
is a possibly valid value
for the option.
Options for wxPNGHandler
wxIMAGE_OPTION_PNG_FORMAT | Format for saving a PNG file. |
wxIMAGE_OPTION_PNG_BITDEPTH | Bit depth for every channel (R/G/B/A). |
Supported values for wxIMAGE_OPTION_PNG_FORMAT:
wxPNG_TYPE_COLOUR | Stores RGB image. |
wxPNG_TYPE_GREY | Stores grey image, converts from RGB. |
wxPNG_TYPE_GREY_RED | Stores grey image, uses red value as grey. |
See also
wxImage::SetOption, wxImage::GetOption
bool HasOption(const wxString& name) const
Returns true if the given option is present. The function is case-insensitive to name.
See also
wxImage::SetOption, wxImage::GetOption, wxImage::GetOptionInt
Initializes the image alpha channel data. It is an error to call it if the image already has alpha data. If it doesn't, alpha data will be by default initialized to all pixels being fully opaque. But if the image has a a mask colour, all mask pixels will be completely transparent.
Internal use only. Adds standard image format handlers. It only install BMP for the time being, which is used by wxBitmap.
This function is called by wxWidgets on startup, and shouldn't be called by the user.
See also
wxImageHandler, wxInitAllImageHandlers
Adds a handler at the start of the static list of format handlers.
See also
bool IsTransparent(int x, int y, unsigned char threshold = ) const
Returns TRUEif the given pixel is transparent, i.e. either has the mask colour if this image has a mask or if this image has alpha channel and alpha value of this pixel is strictly less than threshold.
Loads an image from a file. If no handler type is provided, the library will try to autodetect the format.
Loads an image from an input stream.
Parameters
Remarks
Depending on how wxWidgets has been configured, not all formats may be available.
Note: you can use GetOptionInt to get the hotspot for loaded cursor file:
int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
Return value
true if the operation succeeded, false otherwise. If the optional index parameter is out of range, false is returned and a call to wxLogError() takes place.
See also
wxPython での注意点: In place of a single overloaded method name, wxPython implements the following methods:
2cm
LoadFile(filename, type) | Loads an image of the given type from a file |
LoadMimeFile(filename, mimetype) | Loads an image of the given mimetype from a file |
wxPerl での注意点: Methods supported by wxPerl are:
Returns true if image data is present.
Finds the handler with the given name, and removes it. The handler is not deleted.
Return value
true if the handler was found and removed, false otherwise.
See also
wxImage Mirror(bool horizontally = true) const
Returns a mirrored copy of the image. The parameter horizontally indicates the orientation.
Replaces the colour specified by r1,g1,b1 by the colour r2,g2,b2.
Changes the size of the image in-place by scaling it: after a call to this function, the image will have the given width and height.
Returns the (modified) image itself.
See also
Changes the size of the image in-place without scaling it by adding either a border with the given colour or cropping as necessary. The image is pasted into a new image with the given size and background colour at the position pos relative to the upper left of the new image. If red = green = blue = -1 then use either the current mask colour if set or find, use, and set a suitable mask colour for any newly exposed areas.
Returns the (modified) image itself.
See also
Rotates the image about the given point, by angle radians. Passing true to interpolating results in better image quality, but is slower. If the image has a mask, then the mask colour is used for the uncovered pixels in the rotated image background. Else, black (rgb 0, 0, 0) will be used.
Returns the rotated image, leaving this image intact.
wxImage Rotate90(bool clockwise = true) const
Returns a copy of the image rotated 90 degrees in the direction indicated by clockwise.
bool SaveFile(const wxString& name, int type) const
bool SaveFile(const wxString& name, const wxString& mimetype) const
Saves an image in the named file.
bool SaveFile(const wxString& name) const
Saves an image in the named file. File type is determined from the extension of the file name. Note that this function may fail if the extension is not recognized! You can use one of the forms above to save images to files with non-standard extensions.
bool SaveFile(wxOutputStream& stream, int type) const
bool SaveFile(wxOutputStream& stream, const wxString& mimetype) const
Saves an image in the given stream.
Parameters
wxBITMAP_TYPE_BMP | Save a BMP image file. |
wxBITMAP_TYPE_JPEG | Save a JPEG image file. |
wxBITMAP_TYPE_PNG | Save a PNG image file. |
wxBITMAP_TYPE_PCX | Save a PCX image file (tries to save as 8-bit if possible, falls back to 24-bit otherwise). |
wxBITMAP_TYPE_PNM | Save a PNM image file (as raw RGB always). |
wxBITMAP_TYPE_TIFF | Save a TIFF image file. |
wxBITMAP_TYPE_XPM | Save a XPM image file. |
wxBITMAP_TYPE_ICO | Save a Windows icon file (ICO) (the size may be up to 255 wide by 127 high. A single image is saved in 8 colors at the size supplied). |
wxBITMAP_TYPE_CUR | Save a Windows cursor file (CUR). |
Return value
true if the operation succeeded, false otherwise.
Remarks
Depending on how wxWidgets has been configured, not all formats may be available.
Note: you can use GetOptionInt to set the hotspot before saving an image into a cursor file (default hotspot is in the centre of the image):
image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotspotX); image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotspotY);
See also
wxPython での注意点: In place of a single overloaded method name, wxPython implements the following methods:
2cm
SaveFile(filename, type) | Saves the image using the given type to the named file |
SaveMimeFile(filename, mimetype) | Saves the image using the given mimetype to the named file |
wxPerl での注意点: Methods supported by wxPerl are:
wxImage Scale(int width, int height) const
Returns a scaled version of the image. This is also useful for scaling bitmaps in general as the only other way to scale bitmaps is to blit a wxMemoryDC into another wxMemoryDC.
It may be mentioned that the GTK port uses this function internally to scale bitmaps when using mapping modes in wxDC.
Example:
// get the bitmap from somewhere wxBitmap bmp = ...; // rescale it to have size of 32*32 if ( bmp.GetWidth() != 32 || bmp.GetHeight() != 32 ) { wxImage image = bmp.ConvertToImage(); bmp = wxBitmap(image.Scale(32, 32)); // another possibility: image.Rescale(32, 32); bmp = image; }
See also
wxImage Size(const wxSize& size, const wxPoint& pos, int red = -1, int green = -1, int blue = -1) const
Returns a resized version of this image without scaling it by adding either a border with the given colour or cropping as necessary. The image is pasted into a new image with the given size and background colour at the position pos relative to the upper left of the new image. If red = green = blue = -1 then use either the current mask colour if set or find, use, and set a suitable mask colour for any newly exposed areas.
See also
This function is similar to SetData and has similar restrictions. The pointer passed to it may however be NULL in which case the function will allocate the alpha array internally - this is useful to add alpha channel data to an image which doesn't have any. If the pointer is not NULL, it must have one byte for each image pixel and be allocated with malloc(). wxImage takes ownership of the pointer and will free it unless static_data parameter is set.to TRUE- in this case the caller should do it.
Sets the alpha value for the given pixel. This function should only be called if the image has alpha channel data, use HasAlpha to check for this.
Sets the image data without performing checks. The data given must have the size (width*height*3) or results will be unexpected. Don't use this method if you aren't sure you know what you are doing.
The data must have been allocated with malloc(), NOT with operator new.
After this call the pointer to the data is owned by the wxImage object, that will be responsible for deleting it. Do not pass to this function a pointer obtained through wxImage::GetData.
Specifies whether there is a mask or not. The area of the mask is determined by the current mask colour.
Sets the mask colour for this image (and tells the image to use the mask).
Parameters
Sets image's mask so that the pixels that have RGB value of mr,mg,mb in mask will be masked in the image. This is done by first finding an unused colour in the image, setting this colour as the mask colour and then using this colour to draw all pixels in the image who corresponding pixel in mask has given RGB value.
Return value
Returns false if mask does not have same dimensions as the image or if there is no unused colour left. Returns true if the mask was successfully applied.
Notes
Note that this method involves computing the histogram, which is computationally intensive operation.
Sets a user-defined option. The function is case-insensitive to name.
For example, when saving as a JPEG file, the option quality is used, which is a number between 0 and 100 (0 is terrible, 100 is very good).
See also
wxImage::GetOption, wxImage::GetOptionInt, wxImage::HasOption
Associates a palette with the image. The palette may be used when converting wxImage to wxBitmap (MSW only at present) or in file save operations (none as yet).
Sets the pixel at the given coordinate. This routine performs bounds-checks for the coordinate so it can be considered a safe way to manipulate the data, but in some cases this might be too slow so that the data will have to be set directly. In that case you will have to get access to the image data using the GetData method.
Sets the colour of the pixels within the given rectangle. This routine performs bounds-checks for the coordinate so it can be considered a safe way to manipulate the data.
Assignment operator. This operator does not copy any data, but instead passes a pointer to the data in image and increments a reference counter. It is a fast operation.
Parameters
Return value
Returns 'this' object.
bool operator (const wxImage& image) const
Equality operator. This operator tests whether the internal data pointers are equal (a fast test).
Parameters
Return value
Returns true if the images were effectively equal, false otherwise.
bool operator (const wxImage& image) const
Inequality operator. This operator tests whether the internal data pointers are unequal (a fast test).
Parameters
Return value
Returns true if the images were unequal, false otherwise.
ymasuda 平成17年11月19日