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 );
49 //bool CanRead( wxInputStream& stream );
51 bool CanRead( const wxString& name );
53 void SetName(const wxString& name);
54 void SetExtension(const wxString& extension);
55 void SetType(long type);
56 void SetMimeType(const wxString& mimetype);
60 //---------------------------------------------------------------------------
62 class wxImageHistogram /* : public wxImageHistogramBase */
67 DocStr(MakeKey, "Get the key in the histogram for the given RGB values", "");
68 static unsigned long MakeKey(unsigned char r,
73 bool, FindFirstUnusedColour(unsigned char *OUTPUT,
74 unsigned char *OUTPUT,
75 unsigned char *OUTPUT,
76 unsigned char startR = 1,
77 unsigned char startG = 0,
78 unsigned char startB = 0 ) const,
79 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
80 "Find first colour that is not used in the image and has higher RGB
81 values than startR, startG, startB. Returns a tuple consisting of a
82 success flag and rgb values.", "");
86 "Returns the pixel count for the given key. Use `MakeKey` to create a
87 key value from a RGB tripple.", "");
88 unsigned long GetCount(unsigned long key) {
89 wxImageHistogramEntry e = (*self)[key];
94 "Returns the pixel count for the given RGB values.", "");
95 unsigned long GetCountRGB(unsigned char r,
98 unsigned long key = wxImageHistogram::MakeKey(r, g, b);
99 wxImageHistogramEntry e = (*self)[key];
103 DocStr(GetCountColour,
104 "Returns the pixel count for the given `wx.Colour` value.", "");
105 unsigned long GetCountColour(const wxColour& colour) {
106 unsigned long key = wxImageHistogram::MakeKey(colour.Red(),
109 wxImageHistogramEntry e = (*self)[key];
117 //---------------------------------------------------------------------------
120 typedef unsigned char* buffer;
123 %typemap(in) (buffer data, int DATASIZE)
124 { if (!PyArg_Parse($input, "t#", &$1, &$2)) SWIG_fail; }
126 %typemap(in) (buffer alpha, int ALPHASIZE)
127 { if (!PyArg_Parse($input, "t#", &$1, &$2)) SWIG_fail; }
129 //---------------------------------------------------------------------------
132 class wxImage : public wxObject {
134 %typemap(out) wxImage*; // turn off this typemap
137 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
142 // Alternate constructors
144 wxImage(const wxString& name, const wxString& mimetype, int index = -1),
149 wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1),
154 wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1 ),
156 ImageFromStreamMime);
161 "Construct an empty image of a given size, optionally setting all
162 pixels to black.", "",
163 wxImage(int width=0, int height=0, bool clear = true))
165 if (width > 0 && height > 0)
166 return new wxImage(width, height, clear);
172 MustHaveApp(wxImage(const wxBitmap &bitmap));
176 "Construct an Image from a `wx.Bitmap`.", "",
177 wxImage(const wxBitmap &bitmap))
179 return new wxImage(bitmap.ConvertToImage());
184 "Construct an Image from a buffer of RGB bytes. Accepts either a
185 string or a buffer object holding the data and the length of the data
186 must be width*height*3.", "",
187 wxImage(int width, int height, buffer data, int DATASIZE))
189 if (DATASIZE != width*height*3) {
190 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
194 // Copy the source data so the wxImage can clean it up later
195 buffer copy = (buffer)malloc(DATASIZE);
197 wxPyBLOCK_THREADS(PyErr_NoMemory());
200 memcpy(copy, data, DATASIZE);
201 return new wxImage(width, height, copy, false);
206 ImageFromDataWithAlpha,
207 "Construct an Image from a buffer of RGB bytes with an Alpha channel.
208 Accepts either a string or a buffer object holding the data and the
209 length of the data must be width*height*3.", "",
210 wxImage(int width, int height, buffer data, int DATASIZE, buffer alpha, int ALPHASIZE))
212 if (DATASIZE != width*height*3) {
213 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
216 if (ALPHASIZE != width*height) {
217 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
221 // Copy the source data so the wxImage can clean it up later
222 buffer dcopy = (buffer)malloc(DATASIZE);
224 wxPyBLOCK_THREADS(PyErr_NoMemory());
227 memcpy(dcopy, data, DATASIZE);
229 buffer acopy = (buffer)malloc(ALPHASIZE);
231 wxPyBLOCK_THREADS(PyErr_NoMemory());
234 memcpy(acopy, alpha, ALPHASIZE);
236 return new wxImage(width, height, dcopy, acopy, false);
240 // TODO: wxImage( char** xpmData );
243 // Turn it back on again
244 %typemap(out) wxImage* { $result = wxPyMake_wxObject($1, $owner); }
247 void Create( int width, int height );
250 wxImage Scale( int width, int height );
251 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
252 wxImage& Rescale(int width, int height);
254 // resizes the image in place
255 wxImage& Resize( const wxSize& size, const wxPoint& pos,
256 int r = -1, int g = -1, int b = -1 );
258 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
261 void, SetRGB( const wxRect& rect,
262 unsigned char r, unsigned char g, unsigned char b ));
264 unsigned char GetRed( int x, int y );
265 unsigned char GetGreen( int x, int y );
266 unsigned char GetBlue( int x, int y );
268 void SetAlpha(int x, int y, unsigned char alpha);
269 unsigned char GetAlpha(int x, int y);
274 "Initializes the image alpha channel data. It is an error to call it if
275 the image already has alpha data. If it doesn't, alpha data will be by
276 default initialized to all pixels being fully opaque. But if the image
277 has a a mask colour, all mask pixels will be completely transparent.", "");
281 bool , IsTransparent(int x, int y,
282 unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD) const,
283 "Returns True if this pixel is masked or has an alpha value less than
284 the spcified threshold.", "");
287 // find first colour that is not used in the image and has higher
288 // RGB values than <startR,startG,startB>
290 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
291 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
292 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
293 "Find first colour that is not used in the image and has higher RGB
294 values than startR, startG, startB. Returns a tuple consisting of a
295 success flag and rgb values.", "");
299 bool , ConvertAlphaToMask(byte threshold = wxIMAGE_ALPHA_THRESHOLD),
300 "If the image has alpha channel, this method converts it to mask. All pixels
301 with alpha value less than ``threshold`` are replaced with mask colour and the
302 alpha channel is removed. Mask colour is chosen automatically using
303 `FindFirstUnusedColour`.
305 If the image image doesn't have alpha channel, ConvertAlphaToMask does
310 bool , ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b ),
311 "This method converts an image where the original alpha information is
312 only available as a shades of a colour (actually shades of grey)
313 typically when you draw anti-aliased text into a bitmap. The DC
314 drawing routines draw grey values on the black background although
315 they actually mean to draw white with differnt alpha values. This
316 method reverses it, assuming a black (!) background and white text.
317 The method will then fill up the whole image with the colour given.", "");
321 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
322 bool SetMaskFromImage(const wxImage & mask,
323 byte mr, byte mg, byte mb);
325 // void DoFloodFill (wxCoord x, wxCoord y,
326 // const wxBrush & fillBrush,
327 // const wxColour& testColour,
328 // int style = wxFLOOD_SURFACE,
329 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
331 static bool CanRead( const wxString& name );
332 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
334 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
335 %Rename(LoadMimeFile, bool, LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ));
337 bool SaveFile( const wxString& name, int type );
338 %Rename(SaveMimeFile, bool, SaveFile( const wxString& name, const wxString& mimetype ));
340 %Rename(CanReadStream, static bool, CanRead( wxInputStream& stream ));
341 %Rename(LoadStream, bool, LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ));
342 %Rename(LoadMimeStream, bool, LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ));
350 wxSize size(self->GetWidth(), self->GetHeight());
355 wxImage GetSubImage(const wxRect& rect);
357 // Paste the image or part of this image into an image of the given size at the pos
358 // any newly exposed areas will be filled with the rgb colour
359 // by default if r = g = b = -1 then fill with this image's mask colour or find and
360 // set a suitable mask colour
361 wxImage Size( const wxSize& size, const wxPoint& pos,
362 int r = -1, int g = -1, int b = -1 ) const;
365 void Paste( const wxImage &image, int x, int y );
367 //unsigned char *GetData();
368 //void SetData( unsigned char *data );
372 "Returns a string containing a copy of the RGB bytes of the image.", "");
375 buffer data = self->GetData();
376 int len = self->GetWidth() * self->GetHeight() * 3;
378 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
382 "Resets the Image's RGB data from a buffer of RGB bytes. Accepts
383 either a string or a buffer object holding the data and the length of
384 the data must be width*height*3.", "");
385 void SetData(buffer data, int DATASIZE)
387 if (DATASIZE != self->GetWidth() * self->GetHeight() * 3) {
388 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
391 buffer copy = (buffer)malloc(DATASIZE);
393 wxPyBLOCK_THREADS(PyErr_NoMemory());
396 memcpy(copy, data, DATASIZE);
397 self->SetData(copy, false);
398 // wxImage takes ownership of copy...
402 DocStr(GetDataBuffer,
403 "Returns a writable Python buffer object that is pointing at the RGB
404 image data buffer inside the wx.Image.", "");
405 PyObject* GetDataBuffer()
407 buffer data = self->GetData();
408 int len = self->GetWidth() * self->GetHeight() * 3;
410 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
414 DocStr(SetDataBuffer,
415 "Sets the internal image data pointer to point at a Python buffer
416 object. This can save a copy of the data but you must ensure that the
417 buffer object lives longer than the wx.Image does.", "");
418 void SetDataBuffer(buffer data, int DATASIZE)
420 if (DATASIZE != self->GetWidth() * self->GetHeight() * 3) {
421 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
424 self->SetData(data, true);
430 "Returns a string containing a copy of the alpha bytes of the image.", "");
431 PyObject* GetAlphaData() {
432 buffer data = self->GetAlpha();
436 int len = self->GetWidth() * self->GetHeight();
438 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
444 "Resets the Image's alpha data from a buffer of bytes. Accepts either
445 a string or a buffer object holding the data and the length of the
446 data must be width*height.", "");
447 void SetAlphaData(buffer alpha, int ALPHASIZE)
449 if (ALPHASIZE != self->GetWidth() * self->GetHeight()) {
450 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
453 buffer acopy = (buffer)malloc(ALPHASIZE);
455 wxPyBLOCK_THREADS(PyErr_NoMemory());
458 memcpy(acopy, alpha, ALPHASIZE);
459 self->SetAlpha(acopy, false);
460 // wxImage takes ownership of acopy...
465 DocStr(GetDataBuffer,
466 "Returns a writable Python buffer object that is pointing at the Alpha
467 data buffer inside the wx.Image.", "");
468 PyObject* GetAlphaBuffer()
470 buffer data = self->GetAlpha();
471 int len = self->GetWidth() * self->GetHeight();
473 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
478 DocStr(SetDataBuffer,
479 "Sets the internal image alpha pointer to point at a Python buffer
480 object. This can save a copy of the data but you must ensure that the
481 buffer object lives longer than the wx.Image does.", "");
482 void SetAlphaBuffer(buffer alpha, int ALPHASIZE)
484 if (ALPHASIZE != self->GetWidth() * self->GetHeight()) {
485 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
488 self->SetAlpha(alpha, true);
492 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
495 /*bool*/ void , GetOrFindMaskColour( unsigned char *OUTPUT,
496 unsigned char *OUTPUT,
497 unsigned char *OUTPUT ) const,
498 "GetOrFindMaskColour() -> (r,g,b)",
499 "Get the current mask colour or find a suitable colour.", "");
502 unsigned char GetMaskRed();
503 unsigned char GetMaskGreen();
504 unsigned char GetMaskBlue();
505 void SetMask( bool mask = true );
508 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
509 bool interpolating = true, wxPoint * offset_after_rotation = NULL) const ;
510 wxImage Rotate90( bool clockwise = true ) ;
511 wxImage Mirror( bool horizontally = true ) ;
513 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
514 unsigned char r2, unsigned char g2, unsigned char b2 );
516 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
517 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
519 void SetOption(const wxString& name, const wxString& value);
520 %Rename(SetOptionInt, void, SetOption(const wxString& name, int value));
521 wxString GetOption(const wxString& name) const;
522 int GetOptionInt(const wxString& name) const;
523 bool HasOption(const wxString& name) const;
525 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
526 unsigned long ComputeHistogram( wxImageHistogram& h );
528 static void AddHandler( wxImageHandler *handler );
529 static void InsertHandler( wxImageHandler *handler );
530 static bool RemoveHandler( const wxString& name );
531 static wxString GetImageExtWildcard();
534 MustHaveApp(ConvertToBitmap);
535 MustHaveApp(ConvertToMonoBitmap);
538 wxBitmap ConvertToBitmap(int depth=-1) {
539 wxBitmap bitmap(*self, depth);
543 wxBitmap ConvertToMonoBitmap( unsigned char red,
545 unsigned char blue ) {
546 wxImage mono = self->ConvertToMono( red, green, blue );
547 wxBitmap bitmap( mono, 1 );
552 %pythoncode { def __nonzero__(self): return self.Ok() }
557 ///void wxInitAllImageHandlers();
560 def InitAllImageHandlers():
562 The former functionality of InitAllImageHanders is now done internal to
563 the _core_ extension module and so this function has become a simple NOP.
570 // See also wxPy_ReinitStockObjects in helpers.cpp
572 const wxImage wxNullImage;
575 //---------------------------------------------------------------------------
577 MAKE_CONST_WXSTRING(IMAGE_OPTION_FILENAME);
578 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
579 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
580 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
581 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
582 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONX);
583 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONY);
584 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
585 MAKE_CONST_WXSTRING(IMAGE_OPTION_QUALITY);
589 wxIMAGE_RESOLUTION_INCHES = 1,
590 wxIMAGE_RESOLUTION_CM = 2
594 MAKE_CONST_WXSTRING(IMAGE_OPTION_BITSPERSAMPLE);
595 MAKE_CONST_WXSTRING(IMAGE_OPTION_SAMPLESPERPIXEL);
596 MAKE_CONST_WXSTRING(IMAGE_OPTION_COMPRESSION);
597 MAKE_CONST_WXSTRING(IMAGE_OPTION_IMAGEDESCRIPTOR);
599 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_FORMAT);
600 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_BITDEPTH);
604 wxPNG_TYPE_COLOUR = 0,
606 wxPNG_TYPE_GREY_RED = 3
611 wxBMP_24BPP = 24, // default, do not need to set
612 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
613 wxBMP_8BPP = 8, // 8bpp, quantized colors
614 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
615 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
616 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
617 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
618 wxBMP_4BPP = 4, // 4bpp, quantized colors
619 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
620 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
625 "A `wx.ImageHandler` for \*.bmp bitmap files.", "");
626 class wxBMPHandler : public wxImageHandler {
632 "A `wx.ImageHandler` for \*.ico icon files.", "");
633 class wxICOHandler : public wxBMPHandler {
639 "A `wx.ImageHandler` for \*.cur cursor files.", "");
640 class wxCURHandler : public wxICOHandler {
646 "A `wx.ImageHandler` for \*.ani animated cursor files.", "");
647 class wxANIHandler : public wxCURHandler {
653 //---------------------------------------------------------------------------
656 "A `wx.ImageHandler` for PNG image files.", "");
657 class wxPNGHandler : public wxImageHandler {
664 "A `wx.ImageHandler` for GIF image files.", "");
665 class wxGIFHandler : public wxImageHandler {
672 "A `wx.ImageHandler` for PCX imager files.", "");
673 class wxPCXHandler : public wxImageHandler {
679 DocStr(wxJPEGHandler,
680 "A `wx.ImageHandler` for JPEG/JPG image files.", "");
681 class wxJPEGHandler : public wxImageHandler {
688 "A `wx.ImageHandler` for PNM image files.", "");
689 class wxPNMHandler : public wxImageHandler {
695 "A `wx.ImageHandler` for XPM image.", "");
696 class wxXPMHandler : public wxImageHandler {
701 DocStr(wxTIFFHandler,
702 "A `wx.ImageHandler` for TIFF image files.", "");
703 class wxTIFFHandler : public wxImageHandler {
711 "A `wx.ImageHandler` for IFF image files.", "");
712 class wxIFFHandler : public wxImageHandler {
718 //---------------------------------------------------------------------------
721 #include <wx/quantize.h>
725 wxQUANTIZE_INCLUDE_WINDOWS_COLOURS,
726 // wxQUANTIZE_RETURN_8BIT_DATA,
727 wxQUANTIZE_FILL_DESTINATION_IMAGE
732 "Performs quantization, or colour reduction, on a wxImage.", "");
734 class wxQuantize /*: public wxObject */
741 "Reduce the colours in the source image and put the result into the
742 destination image, setting the palette in the destination if
743 needed. Both images may be the same, to overwrite the source image.", "
744 :todo: Create a version that returns the wx.Palette used.");
746 static bool Quantize(const wxImage& src, wxImage& dest, int desiredNoColours = 236,
747 int flags = wxQUANTIZE_INCLUDE_WINDOWS_COLOURS|wxQUANTIZE_FILL_DESTINATION_IMAGE)
749 return wxQuantize::Quantize(src, dest,
752 NULL, // eightBitData
759 //---------------------------------------------------------------------------