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 //---------------------------------------------------------------------------
35 class wxImageHandler : public wxObject {
37 // wxImageHandler(); Abstract Base Class
39 wxString GetExtension();
41 wxString GetMimeType();
43 //bool LoadFile(wxImage* image, wxInputStream& stream);
44 //bool SaveFile(wxImage* image, wxOutputStream& stream);
45 //virtual int GetImageCount( wxInputStream& stream );
46 //bool CanRead( wxInputStream& stream );
48 bool CanRead( const wxString& name );
50 void SetName(const wxString& name);
51 void SetExtension(const wxString& extension);
52 void SetType(long type);
53 void SetMimeType(const wxString& mimetype);
57 //---------------------------------------------------------------------------
59 class wxImageHistogram /* : public wxImageHistogramBase */
64 DocStr(MakeKey, "Get the key in the histogram for the given RGB values", "");
65 static unsigned long MakeKey(unsigned char r,
70 bool, FindFirstUnusedColour(unsigned char *OUTPUT,
71 unsigned char *OUTPUT,
72 unsigned char *OUTPUT,
73 unsigned char startR = 1,
74 unsigned char startG = 0,
75 unsigned char startB = 0 ) const,
76 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
77 "Find first colour that is not used in the image and has higher RGB
78 values than startR, startG, startB. Returns a tuple consisting of a
79 success flag and rgb values.", "");
83 "Returns the pixel count for the given key. Use `MakeKey` to create a
84 key value from a RGB tripple.", "");
85 unsigned long GetCount(unsigned long key) {
86 wxImageHistogramEntry e = (*self)[key];
91 "Returns the pixel count for the given RGB values.", "");
92 unsigned long GetCountRGB(unsigned char r,
95 unsigned long key = wxImageHistogram::MakeKey(r, g, b);
96 wxImageHistogramEntry e = (*self)[key];
100 DocStr(GetCountColour,
101 "Returns the pixel count for the given `wx.Colour` value.", "");
102 unsigned long GetCountColour(const wxColour& colour) {
103 unsigned long key = wxImageHistogram::MakeKey(colour.Red(),
106 wxImageHistogramEntry e = (*self)[key];
114 //---------------------------------------------------------------------------
117 typedef unsigned char* buffer;
120 %typemap(in) (buffer data, int DATASIZE)
121 { if (!PyArg_Parse($input, "t#", &$1, &$2)) SWIG_fail; }
123 %typemap(in) (buffer alpha, int ALPHASIZE)
124 { if (!PyArg_Parse($input, "t#", &$1, &$2)) SWIG_fail; }
126 //---------------------------------------------------------------------------
129 class wxImage : public wxObject {
131 %typemap(out) wxImage*; // turn off this typemap
134 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
139 // Alternate constructors
141 wxImage(const wxString& name, const wxString& mimetype, int index = -1),
146 wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1),
151 wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1 ),
153 ImageFromStreamMime);
158 "Construct an empty image of a given size, optionally setting all
159 pixels to black.", "",
160 wxImage(int width=0, int height=0, bool clear = true))
162 if (width > 0 && height > 0)
163 return new wxImage(width, height, clear);
169 MustHaveApp(wxImage(const wxBitmap &bitmap));
173 "Construct an Image from a `wx.Bitmap`.", "",
174 wxImage(const wxBitmap &bitmap))
176 return new wxImage(bitmap.ConvertToImage());
181 "Construct an Image from a buffer of RGB bytes. Accepts either a
182 string or a buffer object holding the data and the length of the data
183 must be width*height*3.", "",
184 wxImage(int width, int height, buffer data, int DATASIZE))
186 if (DATASIZE != width*height*3) {
187 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
191 // Copy the source data so the wxImage can clean it up later
192 buffer copy = (buffer)malloc(DATASIZE);
194 wxPyBLOCK_THREADS(PyErr_NoMemory());
197 memcpy(copy, data, DATASIZE);
198 return new wxImage(width, height, copy, false);
203 ImageFromDataWithAlpha,
204 "Construct an Image from a buffer of RGB bytes with an Alpha channel.
205 Accepts either a string or a buffer object holding the data and the
206 length of the data must be width*height*3.", "",
207 wxImage(int width, int height, buffer data, int DATASIZE, buffer alpha, int ALPHASIZE))
209 if (DATASIZE != width*height*3) {
210 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
213 if (ALPHASIZE != width*height) {
214 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
218 // Copy the source data so the wxImage can clean it up later
219 buffer dcopy = (buffer)malloc(DATASIZE);
221 wxPyBLOCK_THREADS(PyErr_NoMemory());
224 memcpy(dcopy, data, DATASIZE);
226 buffer acopy = (buffer)malloc(ALPHASIZE);
228 wxPyBLOCK_THREADS(PyErr_NoMemory());
231 memcpy(acopy, alpha, ALPHASIZE);
233 return new wxImage(width, height, dcopy, acopy, false);
237 // TODO: wxImage( char** xpmData );
240 // Turn it back on again
241 %typemap(out) wxImage* { $result = wxPyMake_wxObject($1, $owner); }
244 void Create( int width, int height );
247 wxImage Scale( int width, int height );
248 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
249 wxImage& Rescale(int width, int height);
251 // resizes the image in place
252 wxImage& Resize( const wxSize& size, const wxPoint& pos,
253 int r = -1, int g = -1, int b = -1 );
255 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
258 void, SetRGB( const wxRect& rect,
259 unsigned char r, unsigned char g, unsigned char b ));
261 unsigned char GetRed( int x, int y );
262 unsigned char GetGreen( int x, int y );
263 unsigned char GetBlue( int x, int y );
265 void SetAlpha(int x, int y, unsigned char alpha);
266 unsigned char GetAlpha(int x, int y);
271 "Initializes the image alpha channel data. It is an error to call it if
272 the image already has alpha data. If it doesn't, alpha data will be by
273 default initialized to all pixels being fully opaque. But if the image
274 has a a mask colour, all mask pixels will be completely transparent.", "");
278 bool , IsTransparent(int x, int y,
279 unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD) const,
280 "Returns True if this pixel is masked or has an alpha value less than
281 the spcified threshold.", "");
284 // find first colour that is not used in the image and has higher
285 // RGB values than <startR,startG,startB>
287 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
288 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
289 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
290 "Find first colour that is not used in the image and has higher RGB
291 values than startR, startG, startB. Returns a tuple consisting of a
292 success flag and rgb values.", "");
296 bool , ConvertAlphaToMask(byte threshold = wxIMAGE_ALPHA_THRESHOLD),
297 "If the image has alpha channel, this method converts it to mask. All pixels
298 with alpha value less than ``threshold`` are replaced with mask colour and the
299 alpha channel is removed. Mask colour is chosen automatically using
300 `FindFirstUnusedColour`.
302 If the image image doesn't have alpha channel, ConvertAlphaToMask does
307 bool , ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b ),
308 "This method converts an image where the original alpha information is
309 only available as a shades of a colour (actually shades of grey)
310 typically when you draw anti-aliased text into a bitmap. The DC
311 drawing routines draw grey values on the black background although
312 they actually mean to draw white with differnt alpha values. This
313 method reverses it, assuming a black (!) background and white text.
314 The method will then fill up the whole image with the colour given.", "");
318 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
319 bool SetMaskFromImage(const wxImage & mask,
320 byte mr, byte mg, byte mb);
322 // void DoFloodFill (wxCoord x, wxCoord y,
323 // const wxBrush & fillBrush,
324 // const wxColour& testColour,
325 // int style = wxFLOOD_SURFACE,
326 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
328 static bool CanRead( const wxString& name );
329 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
331 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
332 %Rename(LoadMimeFile, bool, LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ));
334 bool SaveFile( const wxString& name, int type );
335 %Rename(SaveMimeFile, bool, SaveFile( const wxString& name, const wxString& mimetype ));
337 %Rename(CanReadStream, static bool, CanRead( wxInputStream& stream ));
338 %Rename(LoadStream, bool, LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ));
339 %Rename(LoadMimeStream, bool, LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ));
347 wxSize size(self->GetWidth(), self->GetHeight());
352 wxImage GetSubImage(const wxRect& rect);
354 // Paste the image or part of this image into an image of the given size at the pos
355 // any newly exposed areas will be filled with the rgb colour
356 // by default if r = g = b = -1 then fill with this image's mask colour or find and
357 // set a suitable mask colour
358 wxImage Size( const wxSize& size, const wxPoint& pos,
359 int r = -1, int g = -1, int b = -1 ) const;
362 void Paste( const wxImage &image, int x, int y );
364 //unsigned char *GetData();
365 //void SetData( unsigned char *data );
369 "Returns a string containing a copy of the RGB bytes of the image.", "");
372 buffer data = self->GetData();
373 int len = self->GetWidth() * self->GetHeight() * 3;
375 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
379 "Resets the Image's RGB data from a buffer of RGB bytes. Accepts
380 either a string or a buffer object holding the data and the length of
381 the data must be width*height*3.", "");
382 void SetData(buffer data, int DATASIZE)
384 if (DATASIZE != self->GetWidth() * self->GetHeight() * 3) {
385 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
388 buffer copy = (buffer)malloc(DATASIZE);
390 wxPyBLOCK_THREADS(PyErr_NoMemory());
393 memcpy(copy, data, DATASIZE);
394 self->SetData(copy, false);
395 // wxImage takes ownership of copy...
399 DocStr(GetDataBuffer,
400 "Returns a writable Python buffer object that is pointing at the RGB
401 image data buffer inside the wx.Image.", "");
402 PyObject* GetDataBuffer()
404 buffer data = self->GetData();
405 int len = self->GetWidth() * self->GetHeight() * 3;
407 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
411 DocStr(SetDataBuffer,
412 "Sets the internal image data pointer to point at a Python buffer
413 object. This can save a copy of the data but you must ensure that the
414 buffer object lives longer than the wx.Image does.", "");
415 void SetDataBuffer(buffer data, int DATASIZE)
417 if (DATASIZE != self->GetWidth() * self->GetHeight() * 3) {
418 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
421 self->SetData(data, true);
427 "Returns a string containing a copy of the alpha bytes of the image.", "");
428 PyObject* GetAlphaData() {
429 buffer data = self->GetAlpha();
433 int len = self->GetWidth() * self->GetHeight();
435 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
441 "Resets the Image's alpha data from a buffer of bytes. Accepts either
442 a string or a buffer object holding the data and the length of the
443 data must be width*height.", "");
444 void SetAlphaData(buffer alpha, int ALPHASIZE)
446 if (ALPHASIZE != self->GetWidth() * self->GetHeight()) {
447 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
450 buffer acopy = (buffer)malloc(ALPHASIZE);
452 wxPyBLOCK_THREADS(PyErr_NoMemory());
455 memcpy(acopy, alpha, ALPHASIZE);
456 self->SetAlpha(acopy, false);
457 // wxImage takes ownership of acopy...
462 DocStr(GetDataBuffer,
463 "Returns a writable Python buffer object that is pointing at the Alpha
464 data buffer inside the wx.Image.", "");
465 PyObject* GetAlphaBuffer()
467 buffer data = self->GetAlpha();
468 int len = self->GetWidth() * self->GetHeight();
470 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
475 DocStr(SetDataBuffer,
476 "Sets the internal image alpha pointer to point at a Python buffer
477 object. This can save a copy of the data but you must ensure that the
478 buffer object lives longer than the wx.Image does.", "");
479 void SetAlphaBuffer(buffer alpha, int ALPHASIZE)
481 if (ALPHASIZE != self->GetWidth() * self->GetHeight()) {
482 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
485 self->SetAlpha(alpha, true);
489 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
492 /*bool*/ void , GetOrFindMaskColour( unsigned char *OUTPUT,
493 unsigned char *OUTPUT,
494 unsigned char *OUTPUT ) const,
495 "GetOrFindMaskColour() -> (r,g,b)",
496 "Get the current mask colour or find a suitable colour.", "");
499 unsigned char GetMaskRed();
500 unsigned char GetMaskGreen();
501 unsigned char GetMaskBlue();
502 void SetMask( bool mask = true );
505 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
506 bool interpolating = true, wxPoint * offset_after_rotation = NULL) const ;
507 wxImage Rotate90( bool clockwise = true ) ;
508 wxImage Mirror( bool horizontally = true ) ;
510 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
511 unsigned char r2, unsigned char g2, unsigned char b2 );
513 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
514 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
516 void SetOption(const wxString& name, const wxString& value);
517 %Rename(SetOptionInt, void, SetOption(const wxString& name, int value));
518 wxString GetOption(const wxString& name) const;
519 int GetOptionInt(const wxString& name) const;
520 bool HasOption(const wxString& name) const;
522 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
523 unsigned long ComputeHistogram( wxImageHistogram& h );
525 static void AddHandler( wxImageHandler *handler );
526 static void InsertHandler( wxImageHandler *handler );
527 static bool RemoveHandler( const wxString& name );
528 static wxString GetImageExtWildcard();
531 MustHaveApp(ConvertToBitmap);
532 MustHaveApp(ConvertToMonoBitmap);
535 wxBitmap ConvertToBitmap(int depth=-1) {
536 wxBitmap bitmap(*self, depth);
540 wxBitmap ConvertToMonoBitmap( unsigned char red,
542 unsigned char blue ) {
543 wxImage mono = self->ConvertToMono( red, green, blue );
544 wxBitmap bitmap( mono, 1 );
549 %pythoncode { def __nonzero__(self): return self.Ok() }
554 ///void wxInitAllImageHandlers();
557 def InitAllImageHandlers():
559 The former functionality of InitAllImageHanders is now done internal to
560 the _core_ extension module and so this function has become a simple NOP.
567 // See also wxPy_ReinitStockObjects in helpers.cpp
569 const wxImage wxNullImage;
572 //---------------------------------------------------------------------------
574 MAKE_CONST_WXSTRING(IMAGE_OPTION_FILENAME);
575 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
576 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
577 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
578 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
579 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONX);
580 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONY);
581 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
582 MAKE_CONST_WXSTRING(IMAGE_OPTION_QUALITY);
586 wxIMAGE_RESOLUTION_INCHES = 1,
587 wxIMAGE_RESOLUTION_CM = 2
591 MAKE_CONST_WXSTRING(IMAGE_OPTION_BITSPERSAMPLE);
592 MAKE_CONST_WXSTRING(IMAGE_OPTION_SAMPLESPERPIXEL);
593 MAKE_CONST_WXSTRING(IMAGE_OPTION_COMPRESSION);
594 MAKE_CONST_WXSTRING(IMAGE_OPTION_IMAGEDESCRIPTOR);
596 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_FORMAT);
597 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_BITDEPTH);
601 wxPNG_TYPE_COLOUR = 0,
603 wxPNG_TYPE_GREY_RED = 3
608 wxBMP_24BPP = 24, // default, do not need to set
609 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
610 wxBMP_8BPP = 8, // 8bpp, quantized colors
611 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
612 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
613 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
614 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
615 wxBMP_4BPP = 4, // 4bpp, quantized colors
616 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
617 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
621 class wxBMPHandler : public wxImageHandler {
626 class wxICOHandler : public wxBMPHandler {
631 class wxCURHandler : public wxICOHandler {
636 class wxANIHandler : public wxCURHandler {
642 //---------------------------------------------------------------------------
644 class wxPNGHandler : public wxImageHandler {
650 class wxGIFHandler : public wxImageHandler {
656 class wxPCXHandler : public wxImageHandler {
662 class wxJPEGHandler : public wxImageHandler {
668 class wxPNMHandler : public wxImageHandler {
673 class wxXPMHandler : public wxImageHandler {
678 class wxTIFFHandler : public wxImageHandler {
685 class wxIFFHandler : public wxImageHandler {
691 //---------------------------------------------------------------------------
694 #include <wx/quantize.h>
698 wxQUANTIZE_INCLUDE_WINDOWS_COLOURS,
699 // wxQUANTIZE_RETURN_8BIT_DATA,
700 wxQUANTIZE_FILL_DESTINATION_IMAGE
705 "Performs quantization, or colour reduction, on a wxImage.", "");
707 class wxQuantize /*: public wxObject */
714 "Reduce the colours in the source image and put the result into the
715 destination image, setting the palette in the destination if
716 needed. Both images may be the same, to overwrite the source image.", "
717 :todo: Create a version that returns the wx.Palette used.");
719 static bool Quantize(const wxImage& src, wxImage& dest, int desiredNoColours = 236,
720 int flags = wxQUANTIZE_INCLUDE_WINDOWS_COLOURS|wxQUANTIZE_FILL_DESTINATION_IMAGE)
722 return wxQuantize::Quantize(src, dest,
725 NULL, // eightBitData
732 //---------------------------------------------------------------------------