1 /////////////////////////////////////////////////////////////////////////////
 
   3 // Purpose:     SWIG definitions for wxImage and such
 
   7 // Created:     25-Sept-2000
 
   9 // Copyright:   (c) 2003 by Total Control Software
 
  10 // Licence:     wxWindows license
 
  11 /////////////////////////////////////////////////////////////////////////////
 
  16 //---------------------------------------------------------------------------
 
  19 #include "wx/wxPython/pyistream.h"
 
  22 //---------------------------------------------------------------------------
 
  25     wxIMAGE_ALPHA_TRANSPARENT,
 
  26     wxIMAGE_ALPHA_THRESHOLD,
 
  31 //---------------------------------------------------------------------------
 
  34 DocStr(wxImageHandler,
 
  35 "This is the base class for implementing image file loading/saving, and
 
  36 image creation from data. It is used within `wx.Image` and is not
 
  37 normally seen by the application.", "");
 
  38 class wxImageHandler : public wxObject {
 
  40     // wxImageHandler();    Abstract Base Class
 
  42     wxString GetExtension();
 
  44     wxString GetMimeType();
 
  46     //bool LoadFile(wxImage* image, wxInputStream& stream);
 
  47     //bool SaveFile(wxImage* image, wxOutputStream& stream);
 
  48     //virtual int GetImageCount( wxInputStream& stream );
 
  50     bool CanRead( const wxString& name );
 
  51     %Rename(CanReadStream, bool, CanRead( wxInputStream& stream ));
 
  53     void SetName(const wxString& name);
 
  54     void SetExtension(const wxString& extension);
 
  55     void SetType(long type);
 
  56     void SetMimeType(const wxString& mimetype);
 
  60 //---------------------------------------------------------------------------
 
  63 DocStr(wxPyImageHandler,
 
  64 "This is the base class for implementing image file loading/saving, and
 
  65 image creation from data, all written in Python.  To create a custom
 
  66 image handler derive a new class from wx.PyImageHandler and provide
 
  67 the following methods::
 
  69     def DoCanRead(self, stream) --> bool
 
  70         '''Check if this handler can read the image on the stream'''
 
  72     def LoadFile(self, image, stream, verbose, index) --> bool
 
  73         '''Load image data from the stream and load it into image.'''
 
  75     def SaveFile(self, image, stream, verbose) --> bool
 
  76         '''Save the iamge data in image to the stream using
 
  77            this handler's image file format.'''
 
  79     def GetImageCount(self, stream) --> int
 
  80         '''If this image format can hold more than one image,
 
  81            how many does the image on the stream have?'''
 
  83 To activate your handler create an instance of it and pass it to
 
  84 `wx.Image_AddHandler`.  Be sure to call `SetName`, `SetType`, and
 
  85 `SetExtension` from your constructor.
 
  88 class wxPyImageHandler: public wxImageHandler {
 
  90     %pythonAppend wxPyImageHandler() "self._SetSelf(self)"
 
  92     void _SetSelf(PyObject *self);
 
  96 //---------------------------------------------------------------------------
 
  99 class wxImageHistogram /* : public wxImageHistogramBase */
 
 104     DocStr(MakeKey, "Get the key in the histogram for the given RGB values", "");
 
 105     static unsigned long MakeKey(byte r,
 
 110         bool, FindFirstUnusedColour(byte *OUTPUT,
 
 115                                     byte startB = 0 ) const,
 
 116         "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
 
 117         "Find first colour that is not used in the image and has higher RGB
 
 118 values than startR, startG, startB.  Returns a tuple consisting of a
 
 119 success flag and rgb values.", "");
 
 123                "Returns the pixel count for the given key.  Use `MakeKey` to create a
 
 124 key value from a RGB tripple.", "");
 
 125         unsigned long GetCount(unsigned long key) {
 
 126             wxImageHistogramEntry e = (*self)[key];
 
 131                "Returns the pixel count for the given RGB values.", "");
 
 132         unsigned long GetCountRGB(byte r,
 
 135             unsigned long key = wxImageHistogram::MakeKey(r, g, b);
 
 136             wxImageHistogramEntry e = (*self)[key];
 
 140         DocStr(GetCountColour,
 
 141                "Returns the pixel count for the given `wx.Colour` value.", "");
 
 142         unsigned long GetCountColour(const wxColour& colour) {
 
 143             unsigned long key = wxImageHistogram::MakeKey(colour.Red(),
 
 146             wxImageHistogramEntry e = (*self)[key];
 
 154 //---------------------------------------------------------------------------
 
 157     typedef unsigned char* buffer;
 
 160 %typemap(in) (buffer data, int DATASIZE)
 
 161     { if (!PyArg_Parse($input, "t#", &$1, &$2)) SWIG_fail; }
 
 163 %typemap(in) (buffer alpha, int ALPHASIZE)
 
 164     { if (!PyArg_Parse($input, "t#", &$1, &$2)) SWIG_fail; }
 
 166 //---------------------------------------------------------------------------
 
 170 "A platform-independent image class.  An image can be created from
 
 171 data, or using `wx.Bitmap.ConvertToImage`, or loaded from a file in a
 
 172 variety of formats.  Functions are available to set and get image
 
 173 bits, so it can be used for basic image manipulation.
 
 175 A wx.Image cannot be drawn directly to a `wx.DC`.  Instead, a
 
 176 platform-specific `wx.Bitmap` object must be created from it using the
 
 177 `wx.BitmapFromImage` constructor. This bitmap can then be drawn in a
 
 178 device context, using `wx.DC.DrawBitmap`.
 
 180 One colour value of the image may be used as a mask colour which will
 
 181 lead to the automatic creation of a `wx.Mask` object associated to the
 
 184 wx.Image supports alpha channel data, that is in addition to a byte
 
 185 for the red, green and blue colour components for each pixel it also
 
 186 stores a byte representing the pixel opacity. An alpha value of 0
 
 187 corresponds to a transparent pixel (null opacity) while a value of 255
 
 188 means that the pixel is 100% opaque.
 
 190 Unlike RGB data, not all images have an alpha channel and before using
 
 191 `GetAlpha` you should check if this image contains an alpha channel
 
 192 with `HasAlpha`. Note that currently only images loaded from PNG files
 
 193 with transparency information will have an alpha channel.", "");
 
 197 // Pull the nested class out to the top level for SWIG's sake
 
 198 #define wxImage_RGBValue wxImage::RGBValue
 
 199 #define wxImage_HSVValue wxImage::HSVValue
 
 202 DocStr(wxImage_RGBValue,
 
 203 "An object that contains values for red, green and blue which represent
 
 204 the value of a color. It is used by `wx.Image.HSVtoRGB` and
 
 205 `wx.Image.RGBtoHSV`, which converts between HSV color space and RGB
 
 207 class wxImage_RGBValue
 
 211         wxImage_RGBValue(byte r=0, byte g=0, byte b=0),
 
 219 DocStr(wxImage_HSVValue,
 
 220 "An object that contains values for hue, saturation and value which
 
 221 represent the value of a color.  It is used by `wx.Image.HSVtoRGB` and
 
 222 `wx.Image.RGBtoHSV`, which +converts between HSV color space and RGB
 
 224 class wxImage_HSVValue
 
 228         wxImage_HSVValue(double h=0.0, double s=0.0, double v=0.0),
 
 236 class wxImage : public wxObject {
 
 238     %typemap(out) wxImage*;    // turn off this typemap
 
 241         wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
 
 242         "Loads an image from a file.",
 
 244     :param name:  Name of the file from which to load the image.
 
 246     :param type: May be one of the following:
 
 248         ====================    =======================================
 
 249         wx.BITMAP_TYPE_BMP      Load a Windows bitmap file.
 
 250         wx.BITMAP_TYPE_GIF      Load a GIF bitmap file.
 
 251         wx.BITMAP_TYPE_JPEG     Load a JPEG bitmap file.
 
 252         wx.BITMAP_TYPE_PNG      Load a PNG bitmap file.
 
 253         wx.BITMAP_TYPE_PCX      Load a PCX bitmap file.
 
 254         wx.BITMAP_TYPE_PNM      Load a PNM bitmap file.
 
 255         wx.BITMAP_TYPE_TIF      Load a TIFF bitmap file.
 
 256         wx.BITMAP_TYPE_XPM      Load a XPM bitmap file.
 
 257         wx.BITMAP_TYPE_ICO      Load a Windows icon file (ICO).
 
 258         wx.BITMAP_TYPE_CUR      Load a Windows cursor file (CUR).
 
 259         wx.BITMAP_TYPE_ANI      Load a Windows animated cursor file (ANI).
 
 260         wx.BITMAP_TYPE_ANY      Will try to autodetect the format. 
 
 261         ====================    =======================================
 
 263     :param index: Index of the image to load in the case that the
 
 264         image file contains multiple images. This is only used by GIF,
 
 265         ICO and TIFF handlers. The default value (-1) means to choose
 
 266         the default image and is interpreted as the first image (the
 
 267         one with index=0) by the GIF and TIFF handler and as the
 
 268         largest and most colourful one by the ICO handler.
 
 270 :see: `wx.ImageFromMime`, `wx.ImageFromStream`, `wx.ImageFromStreamMime`,
 
 271       `wx.EmptyImage`, `wx.ImageFromBitmap`, `wx.ImageFromBuffer`,
 
 272       `wx.ImageFromData`, `wx.ImageFromDataWithAlpha`
 
 277     // Alternate constructors
 
 279         wxImage(const wxString& name, const wxString& mimetype, int index = -1),
 
 280         "Loads an image from a file, using a MIME type string (such as
 
 281 'image/jpeg') to specify image type.", "
 
 287         wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1),
 
 288         "Loads an image from an input stream, or any readable Python file-like
 
 295         wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1 ),
 
 296         "Loads an image from an input stream, or any readable Python file-like
 
 297 object, specifying the image format with a MIME type string.", "
 
 300         ImageFromStreamMime);
 
 305             "Construct an empty image of a given size, optionally setting all
 
 309             wxImage(int width=0, int height=0, bool clear = true))
 
 311                 if (width > 0 && height > 0)
 
 312                     return new wxImage(width, height, clear);
 
 318        MustHaveApp(wxImage(const wxBitmap &bitmap));
 
 322             "Construct an Image from a `wx.Bitmap`.", "
 
 325             wxImage(const wxBitmap &bitmap))
 
 327                 return new wxImage(bitmap.ConvertToImage());
 
 332             "Construct an Image from a buffer of RGB bytes.  Accepts either a
 
 333 string or a buffer object holding the data and the length of the data
 
 334 must be width*height*3.", "
 
 337             wxImage(int width, int height, buffer data, int DATASIZE))
 
 339                 if (DATASIZE != width*height*3) {
 
 340                     wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
 
 344                 // Copy the source data so the wxImage can clean it up later
 
 345                 buffer copy = (buffer)malloc(DATASIZE);
 
 347                     wxPyBLOCK_THREADS(PyErr_NoMemory());
 
 350                 memcpy(copy, data, DATASIZE);
 
 351                 return new wxImage(width, height, copy, false);
 
 356             ImageFromDataWithAlpha,
 
 357             "Construct an Image from a buffer of RGB bytes with an Alpha channel.
 
 358 Accepts either a string or a buffer object holding the data and the
 
 359 length of the data must be width*height*3 bytes, and the length of the
 
 360 alpha data must be width*height bytes.", "
 
 363             wxImage(int width, int height, buffer data, int DATASIZE, buffer alpha, int ALPHASIZE))
 
 365                 if (DATASIZE != width*height*3) {
 
 366                     wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
 
 369                 if (ALPHASIZE != width*height) {
 
 370                     wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
 
 374                 // Copy the source data so the wxImage can clean it up later
 
 375                 buffer dcopy = (buffer)malloc(DATASIZE);
 
 377                     wxPyBLOCK_THREADS(PyErr_NoMemory());
 
 380                 memcpy(dcopy, data, DATASIZE);
 
 382                 buffer acopy = (buffer)malloc(ALPHASIZE);
 
 384                     wxPyBLOCK_THREADS(PyErr_NoMemory());
 
 387                 memcpy(acopy, alpha, ALPHASIZE);
 
 389                 return new wxImage(width, height, dcopy, acopy, false);
 
 392        // NOTE: We need the junk parameter so the compiler will be able
 
 393        // differentiate from the two functions above.  It isn't used for
 
 396             _ImageFromBuffer, ":see: `wx.ImageFromBuffer`", "",
 
 397             wxImage(int width, int height, buffer data, int DATASIZE, PyObject* junk))
 
 400                 if (DATASIZE != width*height*3) {
 
 401                     wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
 
 404                 return new wxImage(width, height, data, true);
 
 408             _ImageFromBufferWithAlpha, ":see: `wx.ImageFromBuffer`", "",
 
 409             wxImage(int width, int height, buffer data, int DATASIZE, buffer alpha, int ALPHASIZE, PyObject* junk))
 
 412                 if (DATASIZE != width*height*3) {
 
 413                     wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
 
 416                 if (ALPHASIZE != width*height) {
 
 417                     wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
 
 420                 return new wxImage(width, height, data, alpha, true);
 
 424     // TODO: wxImage( char** xpmData );
 
 426     // Turn the typemap back on again
 
 427     %typemap(out) wxImage* { $result = wxPyMake_wxObject($1, $owner); }
 
 431         void , Create( int width, int height, bool clear=true ),
 
 432         "Creates a fresh image.  If clear is ``True``, the new image will be
 
 433 initialized to black. Otherwise, the image data will be uninitialized.", "");
 
 437         "Destroys the image data.", "");
 
 441         wxImage , Scale( int width, int height ),
 
 442         "Returns a scaled version of the image. This is also useful for scaling
 
 443 bitmaps in general as the only other way to scale bitmaps is to blit a
 
 444 `wx.MemoryDC` into another `wx.MemoryDC`.", "
 
 449         wxImage , ShrinkBy( int xFactor , int yFactor ) const ,
 
 450         "Return a version of the image scaled smaller by the given factors.", "");
 
 453         wxImage& , Rescale(int width, int height),
 
 454         "Changes the size of the image in-place by scaling it: after a call to
 
 455 this function, the image will have the given width and height.
 
 457 Returns the (modified) image itself.", "
 
 462     // resizes the image in place
 
 464         wxImage& , Resize( const wxSize& size, const wxPoint& pos, 
 
 465                            int r = -1, int g = -1, int b = -1 ),
 
 466         "Changes the size of the image in-place without scaling it, by adding
 
 467 either a border with the given colour or cropping as necessary. The
 
 468 image is pasted into a new image with the given size and background
 
 469 colour at the position pos relative to the upper left of the new
 
 470 image. If red = green = blue = -1 then use either the current mask
 
 471 colour if set or find, use, and set a suitable mask colour for any
 
 474 Returns the (modified) image itself.", "
 
 480         void , SetRGB( int x, int y, byte r, byte g, byte b ),
 
 481         "Sets the pixel at the given coordinate. This routine performs
 
 482 bounds-checks for the coordinate so it can be considered a safe way to
 
 483 manipulate the data, but in some cases this might be too slow so that
 
 484 the data will have to be set directly. In that case you will have to
 
 485 get access to the image data using the `GetData` method.", "");
 
 489             void, SetRGB( const wxRect& rect,
 
 490                           byte r, byte g, byte b ),
 
 491             "Sets the colour of the pixels within the given rectangle. This routine
 
 492 performs bounds-checks for the rectangle so it can be considered a
 
 493 safe way to manipulate the data.", "",
 
 497         byte , GetRed( int x, int y ),
 
 498         "Returns the red intensity at the given coordinate.", "");
 
 501         byte , GetGreen( int x, int y ),
 
 502         "Returns the green intensity at the given coordinate.", "");
 
 505         byte , GetBlue( int x, int y ),
 
 506         "Returns the blue intensity at the given coordinate.", "");
 
 510         void , SetAlpha(int x, int y, byte alpha),
 
 511         "Sets the alpha value for the given pixel. This function should only be
 
 512 called if the image has alpha channel data, use `HasAlpha` to check
 
 516         byte , GetAlpha(int x, int y),
 
 517         "Returns the alpha value for the given pixel. This function may only be
 
 518 called for the images with alpha channel, use `HasAlpha` to check for
 
 521 The returned value is the *opacity* of the image, i.e. the value of 0
 
 522 corresponds to the fully transparent pixels while the value of 255 to
 
 523 the fully opaque pixels.", "");
 
 527         "Returns true if this image has alpha channel, false otherwise.", "
 
 529 :see: `GetAlpha`, `SetAlpha`");
 
 534         "Initializes the image alpha channel data. It is an error to call it if
 
 535 the image already has alpha data. If it doesn't, alpha data will be by
 
 536 default initialized to all pixels being fully opaque. But if the image
 
 537 has a a mask colour, all mask pixels will be completely transparent.", "");
 
 541         bool , IsTransparent(int x, int y,
 
 542                              byte threshold = wxIMAGE_ALPHA_THRESHOLD) const,
 
 543         "Returns ``True`` if this pixel is masked or has an alpha value less
 
 544 than the spcified threshold.", "");
 
 547     // find first colour that is not used in the image and has higher
 
 548     // RGB values than <startR,startG,startB>
 
 550         bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
 
 551                                      byte startR = 0, byte startG = 0, byte startB = 0 ) const,
 
 552         "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
 
 553         "Find first colour that is not used in the image and has higher RGB
 
 554 values than startR, startG, startB.  Returns a tuple consisting of a
 
 555 success flag and rgb values.", "");
 
 559         bool , ConvertAlphaToMask(byte threshold = wxIMAGE_ALPHA_THRESHOLD),
 
 560         "If the image has alpha channel, this method converts it to mask. All
 
 561 pixels with alpha value less than ``threshold`` are replaced with the
 
 562 mask colour and the alpha channel is removed. The mask colour is
 
 563 chosen automatically using `FindFirstUnusedColour`.
 
 565 If the image image doesn't have alpha channel, ConvertAlphaToMask does
 
 570         bool , ConvertColourToAlpha( byte r, byte g, byte b ),
 
 571         "This method converts an image where the original alpha information is
 
 572 only available as a shades of a colour (actually shades of grey)
 
 573 typically when you draw anti-aliased text into a bitmap. The DC
 
 574 drawing routines draw grey values on the black background although
 
 575 they actually mean to draw white with differnt alpha values.  This
 
 576 method reverses it, assuming a black (!) background and white text.
 
 577 The method will then fill up the whole image with the colour given.", "");
 
 582         bool , SetMaskFromImage(const wxImage & mask,
 
 583                                 byte mr, byte mg, byte mb),
 
 584         "Sets the image's mask so that the pixels that have RGB value of
 
 585 ``(mr,mg,mb)`` in ``mask`` will be masked in this image. This is done
 
 586 by first finding an unused colour in the image, setting this colour as
 
 587 the mask colour and then using this colour to draw all pixels in the
 
 588 image who corresponding pixel in mask has given RGB value.
 
 590 Returns ``False`` if ``mask`` does not have same dimensions as the
 
 591 image or if there is no unused colour left. Returns ``True`` if the
 
 592 mask was successfully applied.
 
 594 Note that this method involves computing the histogram, which is
 
 595 computationally intensive operation.", "");
 
 598 //      void DoFloodFill (wxCoord x, wxCoord y,
 
 599 //          const wxBrush & fillBrush,
 
 600 //          const wxColour& testColour,
 
 601 //          int style = wxFLOOD_SURFACE,
 
 602 //          int LogicalFunction = wxCOPY /* currently unused */ ) ;
 
 605         static bool , CanRead( const wxString& filename ),
 
 606         "Returns True if the image handlers can read this file.", "");
 
 609         static int , GetImageCount( const wxString& filename, long type = wxBITMAP_TYPE_ANY ),
 
 610         "If the image file contains more than one image and the image handler
 
 611 is capable of retrieving these individually, this function will return
 
 612 the number of available images.", "");
 
 616         bool , LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
 
 617         "Loads an image from a file. If no handler type is provided, the
 
 618 library will try to autodetect the format.", "");
 
 621         bool , LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ),
 
 622         "Loads an image from a file, specifying the image type with a MIME type
 
 628         bool , SaveFile( const wxString& name, int type ),
 
 629         "Saves an image in the named file.", "");
 
 633         bool , SaveFile( const wxString& name, const wxString& mimetype ),
 
 634         "Saves an image in the named file.", "",
 
 639         static bool , CanRead( wxInputStream& stream ),
 
 640         "Returns True if the image handlers can read an image file from the
 
 641 data currently on the input stream, or a readable Python file-like
 
 647         bool , LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
 
 648         "Loads an image from an input stream or a readable Python file-like
 
 649 object. If no handler type is provided, the library will try to
 
 650 autodetect the format.", "",
 
 655         bool , LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ),
 
 656         "Loads an image from an input stream or a readable Python file-like
 
 657 object, using a MIME type string to specify the image file format.", "",
 
 663         "Returns true if image data is present.", "");
 
 667         "Gets the width of the image in pixels.", "");
 
 671         "Gets the height of the image in pixels.", "");
 
 676                "Returns the size of the image in pixels.", "");
 
 678             wxSize size(self->GetWidth(), self->GetHeight());
 
 685         wxImage , GetSubImage(const wxRect& rect),
 
 686         "Returns a sub image of the current one as long as the rect belongs
 
 687 entirely to the image.", "");
 
 691         wxImage , Size( const wxSize& size, const wxPoint& pos, 
 
 692                         int r = -1, int g = -1, int b = -1 ) const,
 
 693         "Returns a resized version of this image without scaling it by adding
 
 694 either a border with the given colour or cropping as necessary. The
 
 695 image is pasted into a new image with the given size and background
 
 696 colour at the position ``pos`` relative to the upper left of the new
 
 697 image. If red = green = blue = -1 then use either the current mask
 
 698 colour if set or find, use, and set a suitable mask colour for any
 
 699 newly exposed areas.", "
 
 706         "Returns an identical copy of the image.", "");
 
 709         void , Paste( const wxImage &image, int x, int y ),
 
 710         "Pastes ``image`` into this instance and takes care of the mask colour
 
 711 and any out of bounds problems.", "");
 
 714     //unsigned char *GetData();
 
 715     //void SetData( unsigned char *data );
 
 719                "Returns a string containing a copy of the RGB bytes of the image.", "");
 
 722             buffer data = self->GetData();
 
 723             int len = self->GetWidth() * self->GetHeight() * 3;
 
 725             wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
 
 729                "Resets the Image's RGB data from a buffer of RGB bytes.  Accepts
 
 730 either a string or a buffer object holding the data and the length of
 
 731 the data must be width*height*3.", "");
 
 732         void SetData(buffer data, int DATASIZE)
 
 734             if (DATASIZE != self->GetWidth() * self->GetHeight() * 3) {
 
 735                 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
 
 738             buffer copy = (buffer)malloc(DATASIZE);
 
 740                 wxPyBLOCK_THREADS(PyErr_NoMemory());
 
 743             memcpy(copy, data, DATASIZE);
 
 744             self->SetData(copy, false);
 
 745             // wxImage takes ownership of copy...
 
 749         DocStr(GetDataBuffer,
 
 750                "Returns a writable Python buffer object that is pointing at the RGB
 
 751 image data buffer inside the wx.Image. You need to ensure that you do
 
 752 not use this buffer object after the image has been destroyed.", "");
 
 753         PyObject* GetDataBuffer()
 
 755             buffer data = self->GetData();
 
 756             int len = self->GetWidth() * self->GetHeight() * 3;
 
 758             wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
 
 762         DocStr(SetDataBuffer,
 
 763                "Sets the internal image data pointer to point at a Python buffer
 
 764 object.  This can save making an extra copy of the data but you must
 
 765 ensure that the buffer object lives longer than the wx.Image does.", "");
 
 766         void SetDataBuffer(buffer data, int DATASIZE)
 
 768             if (DATASIZE != self->GetWidth() * self->GetHeight() * 3) {
 
 769                 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
 
 772             self->SetData(data, true);
 
 778                "Returns a string containing a copy of the alpha bytes of the image.", "");
 
 779         PyObject* GetAlphaData() {
 
 780             buffer data = self->GetAlpha();
 
 784                 int len = self->GetWidth() * self->GetHeight();
 
 786                 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
 
 792                "Resets the Image's alpha data from a buffer of bytes.  Accepts either
 
 793 a string or a buffer object holding the data and the length of the
 
 794 data must be width*height.", ""); 
 
 795         void SetAlphaData(buffer alpha, int ALPHASIZE)
 
 797             if (ALPHASIZE != self->GetWidth() * self->GetHeight()) {
 
 798                 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
 
 801             buffer acopy = (buffer)malloc(ALPHASIZE);
 
 803                 wxPyBLOCK_THREADS(PyErr_NoMemory());
 
 806             memcpy(acopy, alpha, ALPHASIZE);
 
 807             self->SetAlpha(acopy, false);
 
 808             // wxImage takes ownership of acopy...
 
 813         DocStr(GetAlphaBuffer,
 
 814                "Returns a writable Python buffer object that is pointing at the Alpha
 
 815 data buffer inside the wx.Image. You need to ensure that you do not
 
 816 use this buffer object after the image has been destroyed.", "");
 
 817         PyObject* GetAlphaBuffer()
 
 819             buffer data = self->GetAlpha();
 
 820             int len = self->GetWidth() * self->GetHeight();
 
 822             wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
 
 827         DocStr(SetAlphaBuffer,
 
 828                "Sets the internal image alpha pointer to point at a Python buffer
 
 829 object.  This can save making an extra copy of the data but you must
 
 830 ensure that the buffer object lives as long as the wx.Image does.", "");
 
 831         void SetAlphaBuffer(buffer alpha, int ALPHASIZE)
 
 833             if (ALPHASIZE != self->GetWidth() * self->GetHeight()) {
 
 834                 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
 
 837             self->SetAlpha(alpha, true);
 
 844         void , SetMaskColour( byte r, byte g, byte b ),
 
 845         "Sets the mask colour for this image (and tells the image to use the
 
 850         /*bool*/ void , GetOrFindMaskColour( byte *OUTPUT,
 
 852                                              byte *OUTPUT ) const,
 
 853         "GetOrFindMaskColour() -> (r,g,b)",
 
 854         "Get the current mask colour or find a suitable colour.", "");
 
 859         "Gets the red component of the mask colour.", "");
 
 862         byte , GetMaskGreen(),
 
 863         "Gets the green component of the mask colour.", "");
 
 866         byte , GetMaskBlue(),
 
 867         "Gets the blue component of the mask colour.", "");
 
 870         void , SetMask( bool mask = true ),
 
 871         "Specifies whether there is a mask or not. The area of the mask is
 
 872 determined by the current mask colour.", "");
 
 876         "Returns ``True`` if there is a mask active, ``False`` otherwise.", "");
 
 880         wxImage , Rotate(double angle, const wxPoint & centre_of_rotation,
 
 881                          bool interpolating = true, wxPoint * offset_after_rotation = NULL) const ,
 
 882         "Rotates the image about the given point, by ``angle`` radians. Passing
 
 883 ``True`` to ``interpolating`` results in better image quality, but is
 
 884 slower. If the image has a mask, then the mask colour is used for the
 
 885 uncovered pixels in the rotated image background. Otherwise, black
 
 886 will be used as the fill colour.
 
 888 Returns the rotated image, leaving this image intact.", "");
 
 891         wxImage , Rotate90( bool clockwise = true ) ,
 
 892         "Returns a copy of the image rotated 90 degrees in the direction
 
 893 indicated by ``clockwise``.", "");
 
 896         wxImage , Mirror( bool horizontally = true ) ,
 
 897         "Returns a mirrored copy of the image. The parameter ``horizontally``
 
 898 indicates the orientation.", "");
 
 902         void , Replace( byte r1, byte g1, byte b1,
 
 903                         byte r2, byte g2, byte b2 ),
 
 904         "Replaces the colour specified by ``(r1,g1,b1)`` by the colour
 
 905 ``(r2,g2,b2)``.", "");
 
 908         wxImage , ConvertToGreyscale( double lr = 0.299,
 
 910                                       double lb = 0.114 ) const,
 
 911         "Convert to greyscale image. Uses the luminance component (Y) of the
 
 912 image.  The luma value (YUV) is calculated using (R * lr) + (G * lg) + (B * lb),
 
 913 defaults to ITU-T BT.601", "");
 
 917         wxImage , ConvertToMono( byte r, byte g, byte b ) const,
 
 918         "Returns monochromatic version of the image. The returned image has
 
 919 white colour where the original has ``(r,g,b)`` colour and black
 
 920 colour everywhere else.", "");
 
 924         void , SetOption(const wxString& name, const wxString& value),
 
 925         "Sets an image handler defined option.  For example, when saving as a
 
 926 JPEG file, the option ``wx.IMAGE_OPTION_QUALITY`` is used, which is a
 
 927 number between 0 and 100 (0 is terrible, 100 is very good).", "
 
 929     ================================= ===
 
 930     wx.IMAGE_OPTION_BMP_FORMAT
 
 931     wx.IMAGE_OPTION_CUR_HOTSPOT_X
 
 932     wx.IMAGE_OPTION_CUR_HOTSPOT_Y
 
 933     wx.IMAGE_OPTION_RESOLUTION
 
 934     wx.IMAGE_OPTION_RESOLUTIONX
 
 935     wx.IMAGE_OPTION_RESOLUTIONY
 
 936     wx.IMAGE_OPTION_RESOLUTIONUNIT
 
 937     wx.IMAGE_OPTION_QUALITY
 
 938     wx.IMAGE_OPTION_BITSPERSAMPLE
 
 939     wx.IMAGE_OPTION_SAMPLESPERPIXEL
 
 940     wx.IMAGE_OPTION_COMPRESSION
 
 941     wx.IMAGE_OPTION_IMAGEDESCRIPTOR
 
 942     wx.IMAGE_OPTION_PNG_FORMAT
 
 943     wx.IMAGE_OPTION_PNG_BITDEPTH
 
 944     ================================= ===
 
 946 :see: `HasOption`, `GetOption`, `GetOptionInt`, `SetOptionInt`");
 
 949         void,  SetOption(const wxString& name, int value),
 
 950         "Sets an image option as an integer.", "
 
 952 :see: `HasOption`, `GetOption`, `GetOptionInt`, `SetOption`",
 
 956         wxString , GetOption(const wxString& name) const,
 
 957         "Gets the value of an image handler option.", "
 
 959 :see: `HasOption`, `GetOptionInt`, `SetOption`, `SetOptionInt`");
 
 962         int , GetOptionInt(const wxString& name) const,
 
 963         "Gets the value of an image handler option as an integer.  If the given
 
 964 option is not present, the function returns 0.", "
 
 966 :see: `HasOption`, `GetOption`, `SetOptionInt`, `SetOption`");
 
 969         bool , HasOption(const wxString& name) const,
 
 970         "Returns true if the given option is present.", "
 
 972 :see: `GetOption`, `GetOptionInt`, `SetOption`, `SetOptionInt`");
 
 975     unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
 
 976     unsigned long ComputeHistogram( wxImageHistogram& h );
 
 978     static void AddHandler( wxImageHandler *handler );
 
 979     static void InsertHandler( wxImageHandler *handler );
 
 980     static bool RemoveHandler( const wxString& name );
 
 982         static PyObject* GetHandlers() {
 
 983             wxList& list = wxImage::GetHandlers();
 
 984             return wxPy_ConvertList(&list);
 
 989         static wxString , GetImageExtWildcard(),
 
 990         "Iterates all registered wxImageHandler objects, and returns a string
 
 991 containing file extension masks suitable for passing to file open/save
 
 996 MustHaveApp(ConvertToBitmap);
 
 997 MustHaveApp(ConvertToMonoBitmap);
 
1000         wxBitmap ConvertToBitmap(int depth=-1) {
 
1001             wxBitmap bitmap(*self, depth);
 
1005         wxBitmap ConvertToMonoBitmap( byte red,
 
1008             wxImage mono = self->ConvertToMono( red, green, blue );
 
1009             wxBitmap bitmap( mono, 1 );
 
1016         void , RotateHue(double angle),
 
1017         "Rotates the hue of each pixel of the image. Hue is a double in the
 
1018 range -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees", "");
 
1021         static wxImage_HSVValue , RGBtoHSV(wxImage_RGBValue rgb),
 
1022         "Converts a color in RGB color space to HSV color space.", "");
 
1025         static wxImage_RGBValue , HSVtoRGB(wxImage_HSVValue hsv),
 
1026         "Converts a color in HSV color space to RGB color space.", "");
 
1029     %pythoncode { def __nonzero__(self): return self.Ok() }
 
1034 def ImageFromBuffer(width, height, dataBuffer, alphaBuffer=None):
 
1036     Creates a `wx.Image` from the data in dataBuffer.  The dataBuffer
 
1037     parameter must be a Python object that implements the buffer
 
1038     interface, such as a string, array, etc.  The dataBuffer object is
 
1039     expected to contain a series of RGB bytes and be width*height*3
 
1040     bytes long.  A buffer object can optionally be supplied for the
 
1041     image's alpha channel data, and it is expected to be width*height
 
1044     A reference to the data and alpha buffer objects are kept with the
 
1045     wx.Image, so that they won't get deleted until after the wx.Image
 
1046     is deleted.  However please be aware that it is not guaranteed that
 
1047     an object won't move its memory buffer to a new location when it
 
1048     needs to resize its contents.  If that happens then the wx.Image
 
1049     will end up referring to an invalid memory location and could cause
 
1050     the application to crash.  Therefore care should be taken to not
 
1051     manipulate the objects used for the data and alpha buffers in a
 
1052     way that would cause them to change size.
 
1054     if alphaBuffer is not None:
 
1055         image = _ImageFromBufferWithAlpha(width, height, dataBuffer, alphaBuffer, None)
 
1057         image = _ImageFromBuffer(width, height, dataBuffer, None)
 
1058     image._buffer = dataBuffer
 
1059     image._alpha = alphaBuffer
 
1064 ///void wxInitAllImageHandlers();
 
1067     def InitAllImageHandlers():
 
1069         The former functionality of InitAllImageHanders is now done internal to
 
1070         the _core_ extension module and so this function has become a simple NOP.
 
1078 const wxImage    wxNullImage;
 
1081 //---------------------------------------------------------------------------
 
1083 MAKE_CONST_WXSTRING(IMAGE_OPTION_FILENAME);
 
1084 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
 
1085 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
 
1086 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
 
1087 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
 
1088 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONX);
 
1089 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONY);
 
1090 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
 
1091 MAKE_CONST_WXSTRING(IMAGE_OPTION_QUALITY);
 
1095     wxIMAGE_RESOLUTION_INCHES = 1,
 
1096     wxIMAGE_RESOLUTION_CM = 2
 
1100 MAKE_CONST_WXSTRING(IMAGE_OPTION_BITSPERSAMPLE);
 
1101 MAKE_CONST_WXSTRING(IMAGE_OPTION_SAMPLESPERPIXEL); 
 
1102 MAKE_CONST_WXSTRING(IMAGE_OPTION_COMPRESSION);
 
1103 MAKE_CONST_WXSTRING(IMAGE_OPTION_IMAGEDESCRIPTOR);
 
1105 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_FORMAT);
 
1106 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_BITDEPTH);
 
1110     wxPNG_TYPE_COLOUR = 0,
 
1111     wxPNG_TYPE_GREY = 2,
 
1112     wxPNG_TYPE_GREY_RED = 3
 
1117     wxBMP_24BPP        = 24, // default, do not need to set
 
1118     //wxBMP_16BPP      = 16, // wxQuantize can only do 236 colors?
 
1119     wxBMP_8BPP         =  8, // 8bpp, quantized colors
 
1120     wxBMP_8BPP_GREY    =  9, // 8bpp, rgb averaged to greys
 
1121     wxBMP_8BPP_GRAY    =  wxBMP_8BPP_GREY,
 
1122     wxBMP_8BPP_RED     = 10, // 8bpp, red used as greyscale
 
1123     wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
 
1124     wxBMP_4BPP         =  4, // 4bpp, quantized colors
 
1125     wxBMP_1BPP         =  1, // 1bpp, quantized "colors"
 
1126     wxBMP_1BPP_BW      =  2  // 1bpp, black & white from red
 
1130 DocStr(wxBMPHandler,
 
1131 "A `wx.ImageHandler` for \*.bmp bitmap files.", "");
 
1132 class wxBMPHandler : public wxImageHandler {
 
1137 DocStr(wxICOHandler,
 
1138 "A `wx.ImageHandler` for \*.ico icon files.", "");
 
1139 class wxICOHandler : public wxBMPHandler {
 
1144 DocStr(wxCURHandler,
 
1145 "A `wx.ImageHandler` for \*.cur cursor files.", "");
 
1146 class wxCURHandler : public wxICOHandler {
 
1151 DocStr(wxANIHandler,
 
1152 "A `wx.ImageHandler` for \*.ani animated cursor files.", "");
 
1153 class wxANIHandler : public wxCURHandler {
 
1159 //---------------------------------------------------------------------------
 
1161 DocStr(wxPNGHandler,
 
1162 "A `wx.ImageHandler` for PNG image files.", "");
 
1163 class wxPNGHandler : public wxImageHandler {
 
1169 DocStr(wxGIFHandler,
 
1170 "A `wx.ImageHandler` for GIF image files.", "");
 
1171 class wxGIFHandler : public wxImageHandler {
 
1177 DocStr(wxPCXHandler,
 
1178 "A `wx.ImageHandler` for PCX imager files.", "");
 
1179 class wxPCXHandler : public wxImageHandler {
 
1185 DocStr(wxJPEGHandler,
 
1186 "A `wx.ImageHandler` for JPEG/JPG image files.", "");
 
1187 class wxJPEGHandler : public wxImageHandler {
 
1193 DocStr(wxPNMHandler,
 
1194 "A `wx.ImageHandler` for PNM image files.", "");
 
1195 class wxPNMHandler : public wxImageHandler {
 
1200 DocStr(wxXPMHandler,
 
1201 "A `wx.ImageHandler` for XPM image.", "");
 
1202 class wxXPMHandler : public wxImageHandler {
 
1207 DocStr(wxTIFFHandler,
 
1208 "A `wx.ImageHandler` for TIFF image files.", "");
 
1209 class wxTIFFHandler : public wxImageHandler {
 
1216 DocStr(wxIFFHandler,
 
1217 "A `wx.ImageHandler` for IFF image files.", "");
 
1218 class wxIFFHandler : public wxImageHandler {
 
1224 //---------------------------------------------------------------------------
 
1227 #include <wx/quantize.h>
 
1231     wxQUANTIZE_INCLUDE_WINDOWS_COLOURS,
 
1232 //    wxQUANTIZE_RETURN_8BIT_DATA,
 
1233     wxQUANTIZE_FILL_DESTINATION_IMAGE
 
1238        "Performs quantization, or colour reduction, on a wxImage.", "");
 
1240 class wxQuantize /*: public wxObject */
 
1247             "Reduce the colours in the source image and put the result into the
 
1248 destination image, setting the palette in the destination if
 
1249 needed. Both images may be the same, to overwrite the source image.", "
 
1250 :todo: Create a version that returns the wx.Palette used.");
 
1252         static bool Quantize(const wxImage& src, wxImage& dest, int desiredNoColours = 236,
 
1253                              int flags = wxQUANTIZE_INCLUDE_WINDOWS_COLOURS|wxQUANTIZE_FILL_DESTINATION_IMAGE)
 
1255                 return wxQuantize::Quantize(src, dest, 
 
1258                                             NULL, // eightBitData
 
1265 //---------------------------------------------------------------------------