wximagethis 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 0 corresponds to a transparent pixel (null opacity) while a value of 255 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 call 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 members
wximage::wximage
wximage::wximagewximage() default constructor. wximage(const wximage& image) copy constructor, uses reference counting. wximage(const wxbitmap& bitmap) (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. wximage(int width, int height, bool clear=true) 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. wximage(int width, int height, unsigned char* data, bool static_data = false) 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(). wximage(const wxstring& name, long type = wxbitmap_type_any, int index = -1) wximage(const wxstring& name, const wxstring& mimetype, int index = -1) loads an image from a file. wximage(wxinputstream& stream, long type = wxbitmap_type_any, int index = -1) wximage(wxinputstream& stream, const wxstring& mimetype, int index = -1) loads an image from an input stream. wximage(const char* const* xpmdata) creates an image from xpm data. parameters width
height
name
stream
type
mimetype
index
xpmdata
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 note: constructors supported by wxpython are:
wxperl note: constructors supported by wxperl are:
wximage::~wximage~wximage() destructor. see reference-counted object destruction for more info.
wximage::addhandlerstatic void addhandler(wximagehandler* handler) adds a handler to the end of the static list of format handlers. handler
see also bool canread(const wxstring& filename) returns true if the current image handlers can read this file wxpython note: in wxpython this static method is named wximage_addhandler.
wximage::blurwximage blur(int blurradius) blurs the image in both horizontal and vertical directions by the specified pixel blurradius. this should not be used when using a single mask colour for transparency. see also
wximage::blurhorizontalwximage blurhorizontal(int blurradius) blurs the image in the horizontal direction only. this should not be used when using a single mask colour for transparency. see also
wximage::blurverticalwximage blurvertical(int blurradius) blurs the image in the vertical direction only. this should not be used when using a single mask colour for transparency. see also
wximage::cleanuphandlersstatic void cleanuphandlers() deletes all image handlers. this function is called by wxwidgets on exit.
wximage::computehistogramunsigned 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.
wximage::convertalphatomaskbool convertalphatomask(unsigned char threshold = 128) 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 false if findfirstunusedcolour returns false, true otherwise.
wximage::converttobitmapwxbitmap converttobitmap() const deprecated, use equivalent wxbitmap constructor (which takes wximage and depth as its arguments) instead.
wximage::converttogreyscalewximage converttogreyscale(double lr = 0.299, double lg = 0.587, double lb = 0.114) const returns a greyscale version of the image. the returned image uses the luminance component of the original to calculate the greyscale. defaults to using itu-t bt.601 when converting to yuv, where every pixel equals (r * lr) + (g * lg) + (b * lb).
wximage::converttomonowximage 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.
wximage::copywximage copy() const returns an identical copy of the image.
wximage::createbool create(int width, int height, bool clear=true) creates a fresh image. if clear is true, the new image will be initialized to black. otherwise, the image data will be uninitialized. parameters width
height
return value true if the call succeeded, false otherwise.
wximage::destroyvoid destroy() destroys the image data.
wximage::findfirstunusedcolourbool findfirstunusedcolour(unsigned char * r, unsigned char * g, unsigned char * b, unsigned char startr = 1, unsigned char startg = 0, unsigned char startb = 0) parameters r,g,b
startr,startg,startb
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.
wximage::findhandlerstatic wximagehandler* findhandler(const wxstring& name) finds the handler with the given name. static wximagehandler* findhandler(const wxstring& extension, long imagetype) finds the handler associated with the given extension and type. static wximagehandler* findhandler(long imagetype) finds the handler associated with the given image type. static wximagehandler* findhandlermime(const wxstring& mimetype) finds the handler associated with the given mime type. name
extension
imagetype
mimetype
return value a pointer to the handler if found, null otherwise. see also
wximage::getimageextwildcardstatic wxstring getimageextwildcard() 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", ::wxgetcwd(), "", _("image files ") + wximage::getimageextwildcard(), wxopen );see also
wximage::getalphaunsigned 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 0 corresponds to the transparent pixels while the value of 255 -- to the opaque ones. unsigned char * getalpha() 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.
wximage::getblueunsigned char getblue(int x, int y) const returns the blue intensity at the given coordinate.
wximage::getdataunsigned char* getdata() 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.
wximage::getgreenunsigned char getgreen(int x, int y) const returns the green intensity at the given coordinate.
wximage::getimagecountstatic int getimagecount(const wxstring& filename, long type = wxbitmap_type_any) static int getimagecount(wxinputstream& stream, long type = wxbitmap_type_any) 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. name
stream
type
return value number of available images. for most image handlers, this is 1 (exceptions are tiff and ico formats).
wximage::gethandlersstatic wxlist& gethandlers() returns the static list of image format handlers. see also
wximage::getheightint getheight() const gets the height of the image in pixels.
wximage::getmaskblueunsigned char getmaskblue() const gets the blue value of the mask colour.
wximage::getmaskgreenunsigned char getmaskgreen() const gets the green value of the mask colour.
wximage::getmaskredunsigned char getmaskred() const gets the red value of the mask colour.
wximage::getorfindmaskcolourbool 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.
wximage::getpaletteconst wxpalette& getpalette() const returns the palette associated with the image. currently the palette is only used when converting to wxbitmap under windows. some of the wximage handlers have been modified to set the palette if one exists in the image file (usually 256 or less colour images in gif or png format).
wximage::getredunsigned char getred(int x, int y) const returns the red intensity at the given coordinate.
wximage::getsubimagewximage getsubimage(const wxrect& rect) const returns a sub image of the current one as long as the rect belongs entirely to the image.
wximage::getwidthint getwidth() const gets the width of the image in pixels. see also
hsvvalue::hsvvaluehsvvalue(double h = 0.0, double s = 0.0, double v = 0.0) constructor for hsvvalue, an object that contains values for hue, saturation and value which represent the value of a color. it is used by wximage::hsvtorgb and wximage::rgbtohsv, which converts between hsv color space and rgb color space. wxpython note: use wximage_hsvvalue in wxpython
wximage::hsvtorgbwximage::rgbvalue hsvtorgb(const hsvvalue & hsv) converts a color in hsv color space to rgb color space.
wximage::hasalphabool hasalpha() const returns true if this image has alpha channel, false otherwise. see also
wximage::hasmaskbool hasmask() const returns true if there is a mask active, false otherwise.
wximage::getoptionwxstring 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
wximage::getoptionintint 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 0. use wximage::hasoption is 0 is a possibly valid value for the option. options for wxpnghandler
supported values for wximage_option_png_format:
see also wximage::setoption, wximage::getoption
wximage::hasoptionbool 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
wximage::initalphavoid initalpha() 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.
wximage::initstandardhandlersstatic void initstandardhandlers() 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
wximage::inserthandlerstatic void inserthandler(wximagehandler* handler) adds a handler at the start of the static list of format handlers. handler
see also
wximage::istransparentbool istransparent(int x, int y, unsigned char threshold = 128) const returns true if 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.
wximage::loadfilebool loadfile(const wxstring& name, long type = wxbitmap_type_any, int index = -1) bool loadfile(const wxstring& name, const wxstring& mimetype, int index = -1) loads an image from a file. if no handler type is provided, the library will try to autodetect the format. bool loadfile(wxinputstream& stream, long type, int index = -1) bool loadfile(wxinputstream& stream, const wxstring& mimetype, int index = -1) loads an image from an input stream. parameters name
stream
type
mimetype
index
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 note: in place of a single overloaded method name, wxpython implements the following methods:
wxperl note: methods supported by wxperl are:
wximage::isokbool isok() const returns true if image data is present.
rgbvalue::rgbvaluergbvalue(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0) constructor for rgbvalue, an object that contains values for red, green and blue which represent the value of a color. it is used by wximage::hsvtorgb and wximage::rgbtohsv, which converts between hsv color space and rgb color space. wxpython note: use wximage_rgbvalue in wxpython
wximage::rgbtohsvwximage::hsvvalue rgbtohsv(const rgbvalue& rgb) converts a color in rgb color space to hsv color space.
wximage::removehandlerstatic bool removehandler(const wxstring& name) finds the handler with the given name, and removes it. the handler is not deleted. name
return value true if the handler was found and removed, false otherwise. see also
wximage::mirrorwximage mirror(bool horizontally = true) const returns a mirrored copy of the image. the parameter horizontally indicates the orientation.
wximage::replacevoid replace(unsigned char r1, unsigned char g1, unsigned char b1, unsigned char r2, unsigned char g2, unsigned char b2) replaces the colour specified by r1,g1,b1 by the colour r2,g2,b2.
wximage::rescalewximage & rescale(int width, int height, int quality = wximage_quality_normal) 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. for a description of the quality parameter, see the scale function. returns the (modified) image itself. see also
wximage::resizewximage & resize(const wxsize& size, const wxpoint pos, int red = -1, int green = -1, int blue = -1) 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
wximage::rotatewximage rotate(double angle, const wxpoint& rotationcentre, bool interpolating = true, wxpoint* offsetafterrotation = null) 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::rotatehuevoid rotatehue(double angle) rotates the hue of each pixel in the image by angle, which is a double in the range of -1.0 to +1.0, where -1.0 corresponds to -360 degrees and +1.0 corresponds to +360 degrees.
wximage::rotate90wximage rotate90(bool clockwise = true) const returns a copy of the image rotated 90 degrees in the direction indicated by clockwise.
wximage::savefilebool 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 name
stream
type
mimetype
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 note: in place of a single overloaded method name, wxpython implements the following methods:
wxperl note: methods supported by wxperl are:
wximage::scalewximage scale(int width, int height, int quality = wximage_quality_normal) 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. quality
it should be noted that although using wximage_quality_high produces much nicer looking results it is a slower method. downsampling will use the box averaging method which seems to operate very fast. if you are upsampling larger images using this method you will most likely notice that it is a bit slower and in extreme cases it will be quite substantially slower as the bicubic algorithm has to process a lot of data. it should also be noted that the high quality scaling may not work as expected when using a single mask colour for transparency, as the scaling will blur the image and will therefore remove the mask partially. using the alpha channel will work. 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::sizewximage 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
wximage::setalphavoid setalpha(unsigned char *alpha = null,bool static_data = false) 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. void setalpha(int x, int y, unsigned char alpha) 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.
wximage::setdatavoid setdata(unsigned char*data) 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.
wximage::setmaskvoid setmask(bool hasmask = true) specifies whether there is a mask or not. the area of the mask is determined by the current mask colour.
wximage::setmaskcolourvoid setmaskcolour(unsigned char red, unsigned char green, unsigned char blue) sets the mask colour for this image (and tells the image to use the mask).
wximage::setmaskfromimagebool setmaskfromimage(const wximage& mask, unsigned char mr, unsigned char mg, unsigned char mb) parameters mask
mr,mg,mb
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.
wximage::setoptionvoid setoption(const wxstring& name, const wxstring& value) void setoption(const wxstring& name, int value) 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
wximage::setpalettevoid setpalette(const wxpalette& palette) 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).
wximage::setrgbvoid setrgb(int x, int y, unsigned char red, unsigned char green, unsigned char blue) 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.
wximage::setrgbvoid setrgb(wxrect & rect, unsigned char red, unsigned char green, unsigned char blue) 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.
wximage::operator =wximage& operator =(const wximage& image) assignment operator, using reference counting. parameters image
return value returns 'this' object.
|