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 // Constants for wxImage::Scale() for determining the level of quality
34 wxIMAGE_QUALITY_NORMAL = 0,
35 wxIMAGE_QUALITY_HIGH = 1
38 //---------------------------------------------------------------------------
41 DocStr(wxImageHandler,
42 "This is the base class for implementing image file loading/saving, and
43 image creation from data. It is used within `wx.Image` and is not
44 normally seen by the application.", "");
45 class wxImageHandler : public wxObject {
47 // wxImageHandler(); Abstract Base Class
49 wxString GetExtension();
51 wxString GetMimeType();
53 //bool LoadFile(wxImage* image, wxInputStream& stream);
54 //bool SaveFile(wxImage* image, wxOutputStream& stream);
55 //virtual int GetImageCount( wxInputStream& stream );
57 bool CanRead( const wxString& name );
58 %Rename(CanReadStream, bool, CanRead( wxInputStream& stream ));
60 void SetName(const wxString& name);
61 void SetExtension(const wxString& extension);
62 void SetType(long type);
63 void SetMimeType(const wxString& mimetype);
65 %property(Extension, GetExtension, SetExtension, doc="See `GetExtension` and `SetExtension`");
66 %property(MimeType, GetMimeType, SetMimeType, doc="See `GetMimeType` and `SetMimeType`");
67 %property(Name, GetName, SetName, doc="See `GetName` and `SetName`");
68 %property(Type, GetType, SetType, doc="See `GetType` and `SetType`");
72 //---------------------------------------------------------------------------
75 DocStr(wxPyImageHandler,
76 "This is the base class for implementing image file loading/saving, and
77 image creation from data, all written in Python. To create a custom
78 image handler derive a new class from wx.PyImageHandler and provide
79 the following methods::
81 def DoCanRead(self, stream) --> bool
82 '''Check if this handler can read the image on the stream'''
84 def LoadFile(self, image, stream, verbose, index) --> bool
85 '''Load image data from the stream and load it into image.'''
87 def SaveFile(self, image, stream, verbose) --> bool
88 '''Save the iamge data in image to the stream using
89 this handler's image file format.'''
91 def GetImageCount(self, stream) --> int
92 '''If this image format can hold more than one image,
93 how many does the image on the stream have?'''
95 To activate your handler create an instance of it and pass it to
96 `wx.Image_AddHandler`. Be sure to call `SetName`, `SetType`, and
97 `SetExtension` from your constructor.
100 class wxPyImageHandler: public wxImageHandler {
102 %pythonAppend wxPyImageHandler() "self._SetSelf(self)"
104 void _SetSelf(PyObject *self);
108 //---------------------------------------------------------------------------
111 class wxImageHistogram /* : public wxImageHistogramBase */
116 DocStr(MakeKey, "Get the key in the histogram for the given RGB values", "");
117 static unsigned long MakeKey(byte r,
122 bool, FindFirstUnusedColour(byte *OUTPUT,
127 byte startB = 0 ) const,
128 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
129 "Find first colour that is not used in the image and has higher RGB
130 values than startR, startG, startB. Returns a tuple consisting of a
131 success flag and rgb values.", "");
135 "Returns the pixel count for the given key. Use `MakeKey` to create a
136 key value from a RGB tripple.", "");
137 unsigned long GetCount(unsigned long key) {
138 wxImageHistogramEntry e = (*self)[key];
143 "Returns the pixel count for the given RGB values.", "");
144 unsigned long GetCountRGB(byte r,
147 unsigned long key = wxImageHistogram::MakeKey(r, g, b);
148 wxImageHistogramEntry e = (*self)[key];
152 DocStr(GetCountColour,
153 "Returns the pixel count for the given `wx.Colour` value.", "");
154 unsigned long GetCountColour(const wxColour& colour) {
155 unsigned long key = wxImageHistogram::MakeKey(colour.Red(),
158 wxImageHistogramEntry e = (*self)[key];
166 //---------------------------------------------------------------------------
169 "A platform-independent image class. An image can be created from
170 data, or using `wx.Bitmap.ConvertToImage`, or loaded from a file in a
171 variety of formats. Functions are available to set and get image
172 bits, so it can be used for basic image manipulation.
174 A wx.Image cannot be drawn directly to a `wx.DC`. Instead, a
175 platform-specific `wx.Bitmap` object must be created from it using the
176 `wx.BitmapFromImage` constructor. This bitmap can then be drawn in a
177 device context, using `wx.DC.DrawBitmap`.
179 One colour value of the image may be used as a mask colour which will
180 lead to the automatic creation of a `wx.Mask` object associated to the
183 wx.Image supports alpha channel data, that is in addition to a byte
184 for the red, green and blue colour components for each pixel it also
185 stores a byte representing the pixel opacity. An alpha value of 0
186 corresponds to a transparent pixel (null opacity) while a value of 255
187 means that the pixel is 100% opaque.
189 Unlike RGB data, not all images have an alpha channel and before using
190 `GetAlpha` you should check if this image contains an alpha channel
191 with `HasAlpha`. Note that currently only images loaded from PNG files
192 with transparency information will have an alpha channel.", "");
196 // Pull the nested class out to the top level for SWIG's sake
197 #define wxImage_RGBValue wxImage::RGBValue
198 #define wxImage_HSVValue wxImage::HSVValue
201 DocStr(wxImage_RGBValue,
202 "An object that contains values for red, green and blue which represent
203 the value of a color. It is used by `wx.Image.HSVtoRGB` and
204 `wx.Image.RGBtoHSV`, which converts between HSV color space and RGB
206 class wxImage_RGBValue
210 wxImage_RGBValue(byte r=0, byte g=0, byte b=0),
218 DocStr(wxImage_HSVValue,
219 "An object that contains values for hue, saturation and value which
220 represent the value of a color. It is used by `wx.Image.HSVtoRGB` and
221 `wx.Image.RGBtoHSV`, which +converts between HSV color space and RGB
223 class wxImage_HSVValue
227 wxImage_HSVValue(double h=0.0, double s=0.0, double v=0.0),
235 class wxImage : public wxObject {
237 %typemap(out) wxImage*; // turn off this typemap
240 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
241 "Loads an image from a file.",
243 :param name: Name of the file from which to load the image.
245 :param type: May be one of the following:
247 ==================== =======================================
248 wx.BITMAP_TYPE_BMP Load a Windows bitmap file.
249 wx.BITMAP_TYPE_GIF Load a GIF bitmap file.
250 wx.BITMAP_TYPE_JPEG Load a JPEG bitmap file.
251 wx.BITMAP_TYPE_PNG Load a PNG bitmap file.
252 wx.BITMAP_TYPE_PCX Load a PCX bitmap file.
253 wx.BITMAP_TYPE_PNM Load a PNM bitmap file.
254 wx.BITMAP_TYPE_TIF Load a TIFF bitmap file.
255 wx.BITMAP_TYPE_XPM Load a XPM bitmap file.
256 wx.BITMAP_TYPE_ICO Load a Windows icon file (ICO).
257 wx.BITMAP_TYPE_CUR Load a Windows cursor file (CUR).
258 wx.BITMAP_TYPE_ANI Load a Windows animated cursor file (ANI).
259 wx.BITMAP_TYPE_ANY Will try to autodetect the format.
260 ==================== =======================================
262 :param index: Index of the image to load in the case that the
263 image file contains multiple images. This is only used by GIF,
264 ICO and TIFF handlers. The default value (-1) means to choose
265 the default image and is interpreted as the first image (the
266 one with index=0) by the GIF and TIFF handler and as the
267 largest and most colourful one by the ICO handler.
269 :see: `wx.ImageFromMime`, `wx.ImageFromStream`, `wx.ImageFromStreamMime`,
270 `wx.EmptyImage`, `wx.ImageFromBitmap`, `wx.ImageFromBuffer`,
271 `wx.ImageFromData`, `wx.ImageFromDataWithAlpha`
276 // Alternate constructors
278 wxImage(const wxString& name, const wxString& mimetype, int index = -1),
279 "Loads an image from a file, using a MIME type string (such as
280 'image/jpeg') to specify image type.", "
286 wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1),
287 "Loads an image from an input stream, or any readable Python file-like
294 wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1 ),
295 "Loads an image from an input stream, or any readable Python file-like
296 object, specifying the image format with a MIME type string.", "
299 ImageFromStreamMime);
304 "Construct an empty image of a given size, optionally setting all
308 wxImage(int width=0, int height=0, bool clear = true))
310 if (width > 0 && height > 0)
311 return new wxImage(width, height, clear);
317 MustHaveApp(wxImage(const wxBitmap &bitmap));
321 "Construct an Image from a `wx.Bitmap`.", "
324 wxImage(const wxBitmap &bitmap))
326 return new wxImage(bitmap.ConvertToImage());
331 "Construct an Image from a buffer of RGB bytes. Accepts either a
332 string or a buffer object holding the data and the length of the data
333 must be width*height*3.", "
336 wxImage(int width, int height, buffer data, int DATASIZE))
338 if (DATASIZE != width*height*3) {
339 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
343 // Copy the source data so the wxImage can clean it up later
344 buffer copy = (buffer)malloc(DATASIZE);
346 wxPyBLOCK_THREADS(PyErr_NoMemory());
349 memcpy(copy, data, DATASIZE);
350 return new wxImage(width, height, copy, false);
355 ImageFromDataWithAlpha,
356 "Construct an Image from a buffer of RGB bytes with an Alpha channel.
357 Accepts either a string or a buffer object holding the data and the
358 length of the data must be width*height*3 bytes, and the length of the
359 alpha data must be width*height bytes.", "
362 wxImage(int width, int height, buffer data, int DATASIZE, buffer alpha, int ALPHASIZE))
364 if (DATASIZE != width*height*3) {
365 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
368 if (ALPHASIZE != width*height) {
369 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
373 // Copy the source data so the wxImage can clean it up later
374 buffer dcopy = (buffer)malloc(DATASIZE);
376 wxPyBLOCK_THREADS(PyErr_NoMemory());
379 memcpy(dcopy, data, DATASIZE);
381 buffer acopy = (buffer)malloc(ALPHASIZE);
383 wxPyBLOCK_THREADS(PyErr_NoMemory());
386 memcpy(acopy, alpha, ALPHASIZE);
388 return new wxImage(width, height, dcopy, acopy, false);
392 // TODO: wxImage( char** xpmData );
394 // Turn the typemap back on again
395 %typemap(out) wxImage* { $result = wxPyMake_wxObject($1, $owner); }
399 void , Create( int width, int height, bool clear=true ),
400 "Creates a fresh image. If clear is ``True``, the new image will be
401 initialized to black. Otherwise, the image data will be uninitialized.", "");
405 "Destroys the image data.", "");
409 wxImage , Scale( int width, int height, int quality = wxIMAGE_QUALITY_NORMAL ),
410 "Returns a scaled version of the image. This is also useful for scaling
411 bitmaps in general as the only other way to scale bitmaps is to blit a
412 `wx.MemoryDC` into another `wx.MemoryDC`. The ``quality`` parameter
413 specifies what method to use for resampling the image. It can be
414 either wx.IMAGE_QUALITY_NORMAL, which uses the normal default scaling
415 method of pixel replication, or wx.IMAGE_QUALITY_HIGH which uses
416 bicubic and box averaging resampling methods for upsampling and
417 downsampling respectively.", "
419 It should be noted that although using wx.IMAGE_QUALITY_HIGH produces
420 much nicer looking results it is a slower method. Downsampling will
421 use the box averaging method which seems to operate very fast. If you
422 are upsampling larger images using this method you will most likely
423 notice that it is a bit slower and in extreme cases it will be quite
424 substantially slower as the bicubic algorithm has to process a lot of
427 It should also be noted that the high quality scaling may not work as
428 expected when using a single mask colour for transparency, as the
429 scaling will blur the image and will therefore remove the mask
430 partially. Using the alpha channel will work.
435 wxImage ResampleBox(int width, int height) const;
436 wxImage ResampleBicubic(int width, int height) const;
439 wxImage , Blur(int radius),
440 "Blurs the image in both horizontal and vertical directions by the
441 specified pixel ``radius``. This should not be used when using a
442 single mask colour for transparency.", "");
445 wxImage , BlurHorizontal(int radius),
446 "Blurs the image in the horizontal direction only. This should not be
447 used when using a single mask colour for transparency.
451 wxImage , BlurVertical(int radius),
452 "Blurs the image in the vertical direction only. This should not be
453 used when using a single mask colour for transparency.", "");
458 wxImage , ShrinkBy( int xFactor , int yFactor ) const ,
459 "Return a version of the image scaled smaller by the given factors.", "");
462 wxImage& , Rescale(int width, int height, int quality = wxIMAGE_QUALITY_NORMAL),
463 "Changes the size of the image in-place by scaling it: after a call to
464 this function, the image will have the given width and height.
466 Returns the (modified) image itself.", "
471 // resizes the image in place
473 wxImage& , Resize( const wxSize& size, const wxPoint& pos,
474 int r = -1, int g = -1, int b = -1 ),
475 "Changes the size of the image in-place without scaling it, by adding
476 either a border with the given colour or cropping as necessary. The
477 image is pasted into a new image with the given size and background
478 colour at the position pos relative to the upper left of the new
479 image. If red = green = blue = -1 then use either the current mask
480 colour if set or find, use, and set a suitable mask colour for any
483 Returns the (modified) image itself.", "
489 void , SetRGB( int x, int y, byte r, byte g, byte b ),
490 "Sets the pixel at the given coordinate. This routine performs
491 bounds-checks for the coordinate so it can be considered a safe way to
492 manipulate the data, but in some cases this might be too slow so that
493 the data will have to be set directly. In that case you will have to
494 get access to the image data using the `GetData` method.", "");
498 void, SetRGB( const wxRect& rect,
499 byte r, byte g, byte b ),
500 "Sets the colour of the pixels within the given rectangle. This routine
501 performs bounds-checks for the rectangle so it can be considered a
502 safe way to manipulate the data.", "",
506 byte , GetRed( int x, int y ),
507 "Returns the red intensity at the given coordinate.", "");
510 byte , GetGreen( int x, int y ),
511 "Returns the green intensity at the given coordinate.", "");
514 byte , GetBlue( int x, int y ),
515 "Returns the blue intensity at the given coordinate.", "");
519 void , SetAlpha(int x, int y, byte alpha),
520 "Sets the alpha value for the given pixel. This function should only be
521 called if the image has alpha channel data, use `HasAlpha` to check
525 byte , GetAlpha(int x, int y),
526 "Returns the alpha value for the given pixel. This function may only be
527 called for the images with alpha channel, use `HasAlpha` to check for
530 The returned value is the *opacity* of the image, i.e. the value of 0
531 corresponds to the fully transparent pixels while the value of 255 to
532 the fully opaque pixels.", "");
536 "Returns true if this image has alpha channel, false otherwise.", "
538 :see: `GetAlpha`, `SetAlpha`");
543 "Initializes the image alpha channel data. It is an error to call it if
544 the image already has alpha data. If it doesn't, alpha data will be by
545 default initialized to all pixels being fully opaque. But if the image
546 has a a mask colour, all mask pixels will be completely transparent.", "");
550 bool , IsTransparent(int x, int y,
551 byte threshold = wxIMAGE_ALPHA_THRESHOLD) const,
552 "Returns ``True`` if this pixel is masked or has an alpha value less
553 than the spcified threshold.", "");
556 // find first colour that is not used in the image and has higher
557 // RGB values than <startR,startG,startB>
559 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
560 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
561 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
562 "Find first colour that is not used in the image and has higher RGB
563 values than startR, startG, startB. Returns a tuple consisting of a
564 success flag and rgb values.", "");
568 bool , ConvertAlphaToMask(byte threshold = wxIMAGE_ALPHA_THRESHOLD),
569 "If the image has alpha channel, this method converts it to mask. All
570 pixels with alpha value less than ``threshold`` are replaced with the
571 mask colour and the alpha channel is removed. The mask colour is
572 chosen automatically using `FindFirstUnusedColour`.
574 If the image image doesn't have alpha channel, ConvertAlphaToMask does
579 bool , ConvertColourToAlpha( byte r, byte g, byte b ),
580 "This method converts an image where the original alpha information is
581 only available as a shades of a colour (actually shades of grey)
582 typically when you draw anti-aliased text into a bitmap. The DC
583 drawing routines draw grey values on the black background although
584 they actually mean to draw white with differnt alpha values. This
585 method reverses it, assuming a black (!) background and white text.
586 The method will then fill up the whole image with the colour given.", "");
591 bool , SetMaskFromImage(const wxImage & mask,
592 byte mr, byte mg, byte mb),
593 "Sets the image's mask so that the pixels that have RGB value of
594 ``(mr,mg,mb)`` in ``mask`` will be masked in this image. This is done
595 by first finding an unused colour in the image, setting this colour as
596 the mask colour and then using this colour to draw all pixels in the
597 image who corresponding pixel in mask has given RGB value.
599 Returns ``False`` if ``mask`` does not have same dimensions as the
600 image or if there is no unused colour left. Returns ``True`` if the
601 mask was successfully applied.
603 Note that this method involves computing the histogram, which is
604 computationally intensive operation.", "");
607 // void DoFloodFill (wxCoord x, wxCoord y,
608 // const wxBrush & fillBrush,
609 // const wxColour& testColour,
610 // int style = wxFLOOD_SURFACE,
611 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
614 static bool , CanRead( const wxString& filename ),
615 "Returns True if the image handlers can read this file.", "");
618 static int , GetImageCount( const wxString& filename, long type = wxBITMAP_TYPE_ANY ),
619 "If the image file contains more than one image and the image handler
620 is capable of retrieving these individually, this function will return
621 the number of available images.", "");
625 bool , LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
626 "Loads an image from a file. If no handler type is provided, the
627 library will try to autodetect the format.", "");
630 bool , LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ),
631 "Loads an image from a file, specifying the image type with a MIME type
637 bool , SaveFile( const wxString& name, int type ),
638 "Saves an image in the named file.", "");
642 bool , SaveFile( const wxString& name, const wxString& mimetype ),
643 "Saves an image in the named file.", "",
648 static bool , CanRead( wxInputStream& stream ),
649 "Returns True if the image handlers can read an image file from the
650 data currently on the input stream, or a readable Python file-like
656 bool , LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
657 "Loads an image from an input stream or a readable Python file-like
658 object. If no handler type is provided, the library will try to
659 autodetect the format.", "",
664 bool , LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ),
665 "Loads an image from an input stream or a readable Python file-like
666 object, using a MIME type string to specify the image file format.", "",
672 "Returns true if image data is present.", "");
673 %pythoncode { Ok = IsOk }
677 "Gets the width of the image in pixels.", "");
681 "Gets the height of the image in pixels.", "");
686 "Returns the size of the image in pixels.", "");
688 wxSize size(self->GetWidth(), self->GetHeight());
695 wxImage , GetSubImage(const wxRect& rect),
696 "Returns a sub image of the current one as long as the rect belongs
697 entirely to the image.", "");
701 wxImage , Size( const wxSize& size, const wxPoint& pos,
702 int r = -1, int g = -1, int b = -1 ) const,
703 "Returns a resized version of this image without scaling it by adding
704 either a border with the given colour or cropping as necessary. The
705 image is pasted into a new image with the given size and background
706 colour at the position ``pos`` relative to the upper left of the new
707 image. If red = green = blue = -1 then use either the current mask
708 colour if set or find, use, and set a suitable mask colour for any
709 newly exposed areas.", "
716 "Returns an identical copy of the image.", "");
719 void , Paste( const wxImage &image, int x, int y ),
720 "Pastes ``image`` into this instance and takes care of the mask colour
721 and any out of bounds problems.", "");
724 //unsigned char *GetData();
725 //void SetData( unsigned char *data );
729 "Returns a string containing a copy of the RGB bytes of the image.", "");
732 buffer data = self->GetData();
733 int len = self->GetWidth() * self->GetHeight() * 3;
735 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
739 "Resets the Image's RGB data from a buffer of RGB bytes. Accepts
740 either a string or a buffer object holding the data and the length of
741 the data must be width*height*3.", "");
742 void SetData(buffer data, int DATASIZE)
744 if (DATASIZE != self->GetWidth() * self->GetHeight() * 3) {
745 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
748 buffer copy = (buffer)malloc(DATASIZE);
750 wxPyBLOCK_THREADS(PyErr_NoMemory());
753 memcpy(copy, data, DATASIZE);
754 self->SetData(copy, false);
755 // wxImage takes ownership of copy...
759 DocStr(GetDataBuffer,
760 "Returns a writable Python buffer object that is pointing at the RGB
761 image data buffer inside the wx.Image. You need to ensure that you do
762 not use this buffer object after the image has been destroyed.", "");
763 PyObject* GetDataBuffer()
765 buffer data = self->GetData();
766 int len = self->GetWidth() * self->GetHeight() * 3;
768 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
772 DocStr(SetDataBuffer,
773 "Sets the internal image data pointer to point at a Python buffer
774 object. This can save making an extra copy of the data but you must
775 ensure that the buffer object lives longer than the wx.Image does.", "");
776 void SetDataBuffer(buffer data, int DATASIZE)
778 if (DATASIZE != self->GetWidth() * self->GetHeight() * 3) {
779 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
782 self->SetData(data, true);
788 "Returns a string containing a copy of the alpha bytes of the image.", "");
789 PyObject* GetAlphaData() {
790 buffer data = self->GetAlpha();
794 int len = self->GetWidth() * self->GetHeight();
796 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
802 "Resets the Image's alpha data from a buffer of bytes. Accepts either
803 a string or a buffer object holding the data and the length of the
804 data must be width*height.", "");
805 void SetAlphaData(buffer alpha, int ALPHASIZE)
807 if (ALPHASIZE != self->GetWidth() * self->GetHeight()) {
808 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
811 buffer acopy = (buffer)malloc(ALPHASIZE);
813 wxPyBLOCK_THREADS(PyErr_NoMemory());
816 memcpy(acopy, alpha, ALPHASIZE);
817 self->SetAlpha(acopy, false);
818 // wxImage takes ownership of acopy...
823 DocStr(GetAlphaBuffer,
824 "Returns a writable Python buffer object that is pointing at the Alpha
825 data buffer inside the wx.Image. You need to ensure that you do not
826 use this buffer object after the image has been destroyed.", "");
827 PyObject* GetAlphaBuffer()
829 buffer data = self->GetAlpha();
830 int len = self->GetWidth() * self->GetHeight();
832 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
837 DocStr(SetAlphaBuffer,
838 "Sets the internal image alpha pointer to point at a Python buffer
839 object. This can save making an extra copy of the data but you must
840 ensure that the buffer object lives as long as the wx.Image does.", "");
841 void SetAlphaBuffer(buffer alpha, int ALPHASIZE)
843 if (ALPHASIZE != self->GetWidth() * self->GetHeight()) {
844 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
847 self->SetAlpha(alpha, true);
854 void , SetMaskColour( byte r, byte g, byte b ),
855 "Sets the mask colour for this image (and tells the image to use the
860 /*bool*/ void , GetOrFindMaskColour( byte *OUTPUT,
862 byte *OUTPUT ) const,
863 "GetOrFindMaskColour() -> (r,g,b)",
864 "Get the current mask colour or find a suitable colour.", "");
869 "Gets the red component of the mask colour.", "");
872 byte , GetMaskGreen(),
873 "Gets the green component of the mask colour.", "");
876 byte , GetMaskBlue(),
877 "Gets the blue component of the mask colour.", "");
880 void , SetMask( bool mask = true ),
881 "Specifies whether there is a mask or not. The area of the mask is
882 determined by the current mask colour.", "");
886 "Returns ``True`` if there is a mask active, ``False`` otherwise.", "");
890 wxImage , Rotate(double angle, const wxPoint & centre_of_rotation,
891 bool interpolating = true, wxPoint * offset_after_rotation = NULL) const ,
892 "Rotates the image about the given point, by ``angle`` radians. Passing
893 ``True`` to ``interpolating`` results in better image quality, but is
894 slower. If the image has a mask, then the mask colour is used for the
895 uncovered pixels in the rotated image background. Otherwise, black
896 will be used as the fill colour.
898 Returns the rotated image, leaving this image intact.", "");
901 wxImage , Rotate90( bool clockwise = true ) ,
902 "Returns a copy of the image rotated 90 degrees in the direction
903 indicated by ``clockwise``.", "");
906 wxImage , Mirror( bool horizontally = true ) ,
907 "Returns a mirrored copy of the image. The parameter ``horizontally``
908 indicates the orientation.", "");
912 void , Replace( byte r1, byte g1, byte b1,
913 byte r2, byte g2, byte b2 ),
914 "Replaces the colour specified by ``(r1,g1,b1)`` by the colour
915 ``(r2,g2,b2)``.", "");
918 wxImage , ConvertToGreyscale( double lr = 0.299,
920 double lb = 0.114 ) const,
921 "Convert to greyscale image. Uses the luminance component (Y) of the
922 image. The luma value (YUV) is calculated using (R * lr) + (G * lg) + (B * lb),
923 defaults to ITU-T BT.601", "");
927 wxImage , ConvertToMono( byte r, byte g, byte b ) const,
928 "Returns monochromatic version of the image. The returned image has
929 white colour where the original has ``(r,g,b)`` colour and black
930 colour everywhere else.", "");
934 void , SetOption(const wxString& name, const wxString& value),
935 "Sets an image handler defined option. For example, when saving as a
936 JPEG file, the option ``wx.IMAGE_OPTION_QUALITY`` is used, which is a
937 number between 0 and 100 (0 is terrible, 100 is very good).", "
939 ================================= ===
940 wx.IMAGE_OPTION_BMP_FORMAT
941 wx.IMAGE_OPTION_CUR_HOTSPOT_X
942 wx.IMAGE_OPTION_CUR_HOTSPOT_Y
943 wx.IMAGE_OPTION_RESOLUTION
944 wx.IMAGE_OPTION_RESOLUTIONX
945 wx.IMAGE_OPTION_RESOLUTIONY
946 wx.IMAGE_OPTION_RESOLUTIONUNIT
947 wx.IMAGE_OPTION_QUALITY
948 wx.IMAGE_OPTION_BITSPERSAMPLE
949 wx.IMAGE_OPTION_SAMPLESPERPIXEL
950 wx.IMAGE_OPTION_COMPRESSION
951 wx.IMAGE_OPTION_IMAGEDESCRIPTOR
952 wx.IMAGE_OPTION_PNG_FORMAT
953 wx.IMAGE_OPTION_PNG_BITDEPTH
954 ================================= ===
956 :see: `HasOption`, `GetOption`, `GetOptionInt`, `SetOptionInt`");
959 void, SetOption(const wxString& name, int value),
960 "Sets an image option as an integer.", "
962 :see: `HasOption`, `GetOption`, `GetOptionInt`, `SetOption`",
966 wxString , GetOption(const wxString& name) const,
967 "Gets the value of an image handler option.", "
969 :see: `HasOption`, `GetOptionInt`, `SetOption`, `SetOptionInt`");
972 int , GetOptionInt(const wxString& name) const,
973 "Gets the value of an image handler option as an integer. If the given
974 option is not present, the function returns 0.", "
976 :see: `HasOption`, `GetOption`, `SetOptionInt`, `SetOption`");
979 bool , HasOption(const wxString& name) const,
980 "Returns true if the given option is present.", "
982 :see: `GetOption`, `GetOptionInt`, `SetOption`, `SetOptionInt`");
985 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
986 unsigned long ComputeHistogram( wxImageHistogram& h );
988 static void AddHandler( wxImageHandler *handler );
989 static void InsertHandler( wxImageHandler *handler );
990 static bool RemoveHandler( const wxString& name );
992 static PyObject* GetHandlers() {
993 wxList& list = wxImage::GetHandlers();
994 return wxPy_ConvertList(&list);
999 static wxString , GetImageExtWildcard(),
1000 "Iterates all registered wxImageHandler objects, and returns a string
1001 containing file extension masks suitable for passing to file open/save
1002 dialog boxes.", "");
1006 MustHaveApp(ConvertToBitmap);
1007 MustHaveApp(ConvertToMonoBitmap);
1010 wxBitmap ConvertToBitmap(int depth=-1) {
1011 wxBitmap bitmap(*self, depth);
1015 wxBitmap ConvertToMonoBitmap( byte red,
1018 wxImage mono = self->ConvertToMono( red, green, blue );
1019 wxBitmap bitmap( mono, 1 );
1026 void , RotateHue(double angle),
1027 "Rotates the hue of each pixel of the image. Hue is a double in the
1028 range -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees", "");
1031 static wxImage_HSVValue , RGBtoHSV(wxImage_RGBValue rgb),
1032 "Converts a color in RGB color space to HSV color space.", "");
1035 static wxImage_RGBValue , HSVtoRGB(wxImage_HSVValue hsv),
1036 "Converts a color in HSV color space to RGB color space.", "");
1039 %pythoncode { def __nonzero__(self): return self.IsOk() }
1041 %property(AlphaBuffer, GetAlphaBuffer, SetAlphaBuffer, doc="See `GetAlphaBuffer` and `SetAlphaBuffer`");
1042 %property(AlphaData, GetAlphaData, SetAlphaData, doc="See `GetAlphaData` and `SetAlphaData`");
1043 %property(Data, GetData, SetData, doc="See `GetData` and `SetData`");
1044 %property(DataBuffer, GetDataBuffer, SetDataBuffer, doc="See `GetDataBuffer` and `SetDataBuffer`");
1045 %property(Height, GetHeight, doc="See `GetHeight`");
1046 %property(MaskBlue, GetMaskBlue, doc="See `GetMaskBlue`");
1047 %property(MaskGreen, GetMaskGreen, doc="See `GetMaskGreen`");
1048 %property(MaskRed, GetMaskRed, doc="See `GetMaskRed`");
1049 %property(Size, GetSize, doc="See `GetSize`");
1050 %property(Width, GetWidth, doc="See `GetWidth`");
1055 // Make an image from buffer objects. Not that this is here instead of in the
1056 // wxImage class (as a constructor) because there is already another one with
1057 // the exact same signature, so there woudl be ambiguities in the generated
1058 // C++. Doing it as an independent factory function like this accomplishes
1059 // the same thing however.
1060 %newobject _ImageFromBuffer;
1062 wxImage* _ImageFromBuffer(int width, int height,
1063 buffer data, int DATASIZE,
1064 buffer alpha=NULL, int ALPHASIZE=0)
1066 if (DATASIZE != width*height*3) {
1067 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
1070 if (alpha != NULL) {
1071 if (ALPHASIZE != width*height) {
1072 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
1075 return new wxImage(width, height, data, alpha, true);
1077 return new wxImage(width, height, data, true);
1082 def ImageFromBuffer(width, height, dataBuffer, alphaBuffer=None):
1084 Creates a `wx.Image` from the data in dataBuffer. The dataBuffer
1085 parameter must be a Python object that implements the buffer interface,
1086 such as a string, array, etc. The dataBuffer object is expected to
1087 contain a series of RGB bytes and be width*height*3 bytes long. A buffer
1088 object can optionally be supplied for the image's alpha channel data, and
1089 it is expected to be width*height bytes long.
1091 The wx.Image will be created with its data and alpha pointers initialized
1092 to the memory address pointed to by the buffer objects, thus saving the
1093 time needed to copy the image data from the buffer object to the wx.Image.
1094 While this has advantages, it also has the shoot-yourself-in-the-foot
1095 risks associated with sharing a C pointer between two objects.
1097 To help alleviate the risk a reference to the data and alpha buffer
1098 objects are kept with the wx.Image, so that they won't get deleted until
1099 after the wx.Image is deleted. However please be aware that it is not
1100 guaranteed that an object won't move its memory buffer to a new location
1101 when it needs to resize its contents. If that happens then the wx.Image
1102 will end up referring to an invalid memory location and could cause the
1103 application to crash. Therefore care should be taken to not manipulate
1104 the objects used for the data and alpha buffers in a way that would cause
1105 them to change size.
1107 image = _core_._ImageFromBuffer(width, height, dataBuffer, alphaBuffer)
1108 image._buffer = dataBuffer
1109 image._alpha = alphaBuffer
1114 ///void wxInitAllImageHandlers();
1117 def InitAllImageHandlers():
1119 The former functionality of InitAllImageHanders is now done internal to
1120 the _core_ extension module and so this function has become a simple NOP.
1128 const wxImage wxNullImage;
1131 //---------------------------------------------------------------------------
1133 MAKE_CONST_WXSTRING(IMAGE_OPTION_FILENAME);
1134 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
1135 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
1136 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
1137 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
1138 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONX);
1139 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONY);
1140 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
1141 MAKE_CONST_WXSTRING(IMAGE_OPTION_QUALITY);
1145 wxIMAGE_RESOLUTION_INCHES = 1,
1146 wxIMAGE_RESOLUTION_CM = 2
1150 MAKE_CONST_WXSTRING(IMAGE_OPTION_BITSPERSAMPLE);
1151 MAKE_CONST_WXSTRING(IMAGE_OPTION_SAMPLESPERPIXEL);
1152 MAKE_CONST_WXSTRING(IMAGE_OPTION_COMPRESSION);
1153 MAKE_CONST_WXSTRING(IMAGE_OPTION_IMAGEDESCRIPTOR);
1155 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_FORMAT);
1156 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_BITDEPTH);
1160 wxPNG_TYPE_COLOUR = 0,
1161 wxPNG_TYPE_GREY = 2,
1162 wxPNG_TYPE_GREY_RED = 3
1167 wxBMP_24BPP = 24, // default, do not need to set
1168 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
1169 wxBMP_8BPP = 8, // 8bpp, quantized colors
1170 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
1171 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
1172 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
1173 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
1174 wxBMP_4BPP = 4, // 4bpp, quantized colors
1175 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
1176 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
1180 DocStr(wxBMPHandler,
1181 "A `wx.ImageHandler` for \*.bmp bitmap files.", "");
1182 class wxBMPHandler : public wxImageHandler {
1187 DocStr(wxICOHandler,
1188 "A `wx.ImageHandler` for \*.ico icon files.", "");
1189 class wxICOHandler : public wxBMPHandler {
1194 DocStr(wxCURHandler,
1195 "A `wx.ImageHandler` for \*.cur cursor files.", "");
1196 class wxCURHandler : public wxICOHandler {
1201 DocStr(wxANIHandler,
1202 "A `wx.ImageHandler` for \*.ani animated cursor files.", "");
1203 class wxANIHandler : public wxCURHandler {
1209 //---------------------------------------------------------------------------
1211 DocStr(wxPNGHandler,
1212 "A `wx.ImageHandler` for PNG image files.", "");
1213 class wxPNGHandler : public wxImageHandler {
1219 DocStr(wxGIFHandler,
1220 "A `wx.ImageHandler` for GIF image files.", "");
1221 class wxGIFHandler : public wxImageHandler {
1227 DocStr(wxPCXHandler,
1228 "A `wx.ImageHandler` for PCX imager files.", "");
1229 class wxPCXHandler : public wxImageHandler {
1235 DocStr(wxJPEGHandler,
1236 "A `wx.ImageHandler` for JPEG/JPG image files.", "");
1237 class wxJPEGHandler : public wxImageHandler {
1243 DocStr(wxPNMHandler,
1244 "A `wx.ImageHandler` for PNM image files.", "");
1245 class wxPNMHandler : public wxImageHandler {
1250 DocStr(wxXPMHandler,
1251 "A `wx.ImageHandler` for XPM image.", "");
1252 class wxXPMHandler : public wxImageHandler {
1257 DocStr(wxTIFFHandler,
1258 "A `wx.ImageHandler` for TIFF image files.", "");
1259 class wxTIFFHandler : public wxImageHandler {
1266 DocStr(wxIFFHandler,
1267 "A `wx.ImageHandler` for IFF image files.", "");
1268 class wxIFFHandler : public wxImageHandler {
1274 //---------------------------------------------------------------------------
1277 #include <wx/quantize.h>
1281 wxQUANTIZE_INCLUDE_WINDOWS_COLOURS,
1282 // wxQUANTIZE_RETURN_8BIT_DATA,
1283 wxQUANTIZE_FILL_DESTINATION_IMAGE
1288 "Performs quantization, or colour reduction, on a wxImage.", "");
1290 class wxQuantize /*: public wxObject */
1297 "Reduce the colours in the source image and put the result into the
1298 destination image, setting the palette in the destination if
1299 needed. Both images may be the same, to overwrite the source image.", "
1300 :todo: Create a version that returns the wx.Palette used.");
1302 static bool Quantize(const wxImage& src, wxImage& dest, int desiredNoColours = 236,
1303 int flags = wxQUANTIZE_INCLUDE_WINDOWS_COLOURS|wxQUANTIZE_FILL_DESTINATION_IMAGE)
1305 return wxQuantize::Quantize(src, dest,
1308 NULL, // eightBitData
1315 //---------------------------------------------------------------------------