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 //---------------------------------------------------------------------------
26 class wxImageHandler : public wxObject {
28 // wxImageHandler(); Abstract Base Class
30 wxString GetExtension();
32 wxString GetMimeType();
34 //bool LoadFile(wxImage* image, wxInputStream& stream);
35 //bool SaveFile(wxImage* image, wxOutputStream& stream);
36 //virtual int GetImageCount( wxInputStream& stream );
37 //bool CanRead( wxInputStream& stream );
39 bool CanRead( const wxString& name );
41 void SetName(const wxString& name);
42 void SetExtension(const wxString& extension);
43 void SetType(long type);
44 void SetMimeType(const wxString& mimetype);
48 //---------------------------------------------------------------------------
50 class wxImageHistogram /* : public wxImageHistogramBase */
55 DocStr(MakeKey, "Get the key in the histogram for the given RGB values", "");
56 static unsigned long MakeKey(unsigned char r,
61 bool, FindFirstUnusedColour(unsigned char *OUTPUT,
62 unsigned char *OUTPUT,
63 unsigned char *OUTPUT,
64 unsigned char startR = 1,
65 unsigned char startG = 0,
66 unsigned char startB = 0 ) const,
67 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
68 "Find first colour that is not used in the image and has higher RGB
69 values than startR, startG, startB. Returns a tuple consisting of a
70 success flag and rgb values.", "");
74 "Returns the pixel count for the given key. Use `MakeKey` to create a
75 key value from a RGB tripple.", "");
76 unsigned long GetCount(unsigned long key) {
77 wxImageHistogramEntry e = (*self)[key];
82 "Returns the pixel count for the given RGB values.", "");
83 unsigned long GetCountRGB(unsigned char r,
86 unsigned long key = wxImageHistogram::MakeKey(r, g, b);
87 wxImageHistogramEntry e = (*self)[key];
91 DocStr(GetCountColour,
92 "Returns the pixel count for the given `wx.Colour` value.", "");
93 unsigned long GetCountColour(const wxColour& colour) {
94 unsigned long key = wxImageHistogram::MakeKey(colour.Red(),
97 wxImageHistogramEntry e = (*self)[key];
105 //---------------------------------------------------------------------------
108 class wxImage : public wxObject {
111 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
116 // Alternate constructors
118 wxImage(const wxString& name, const wxString& mimetype, int index = -1),
123 wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1),
128 wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1 ),
130 ImageFromStreamMime);
133 %rename(EmptyImage) wxImage(int width=0, int height=0, bool clear = true);
134 wxImage(int width=0, int height=0, bool clear = true)
136 if (width > 0 && height > 0)
137 return new wxImage(width, height, clear);
142 MustHaveApp(wxImage(const wxBitmap &bitmap));
143 %rename(ImageFromBitmap) wxImage(const wxBitmap &bitmap);
144 wxImage(const wxBitmap &bitmap)
146 return new wxImage(bitmap.ConvertToImage());
149 %rename(ImageFromData) wxImage(int width, int height, unsigned char* data);
150 wxImage(int width, int height, unsigned char* data)
152 // Copy the source data so the wxImage can clean it up later
153 unsigned char* copy = (unsigned char*)malloc(width*height*3);
158 memcpy(copy, data, width*height*3);
159 return new wxImage(width, height, copy, false);
163 %rename(ImageFromDataWithAlpha) wxImage(int width, int height, unsigned char* data, unsigned char* alpha);
164 wxImage(int width, int height, unsigned char* data, unsigned char* alpha)
166 // Copy the source data so the wxImage can clean it up later
167 unsigned char* dcopy = (unsigned char*)malloc(width*height*3);
172 memcpy(dcopy, data, width*height*3);
173 unsigned char* acopy = (unsigned char*)malloc(width*height);
178 memcpy(acopy, alpha, width*height);
180 return new wxImage(width, height, dcopy, acopy, false);
184 // TODO: wxImage( char** xpmData );
187 void Create( int width, int height );
190 wxImage Scale( int width, int height );
191 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
192 wxImage& Rescale(int width, int height);
194 // resizes the image in place
195 wxImage& Resize( const wxSize& size, const wxPoint& pos,
196 int r = -1, int g = -1, int b = -1 );
198 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
201 void, SetRGB( const wxRect& rect,
202 unsigned char r, unsigned char g, unsigned char b ));
204 unsigned char GetRed( int x, int y );
205 unsigned char GetGreen( int x, int y );
206 unsigned char GetBlue( int x, int y );
208 void SetAlpha(int x, int y, unsigned char alpha);
209 unsigned char GetAlpha(int x, int y);
214 "Initializes the image alpha channel data. It is an error to call it if
215 the image already has alpha data. If it doesn't, alpha data will be by
216 default initialized to all pixels being fully opaque. But if the image
217 has a a mask colour, all mask pixels will be completely transparent.", "");
220 // find first colour that is not used in the image and has higher
221 // RGB values than <startR,startG,startB>
223 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
224 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
225 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
226 "Find first colour that is not used in the image and has higher RGB
227 values than startR, startG, startB. Returns a tuple consisting of a
228 success flag and rgb values.", "");
232 bool , ConvertAlphaToMask(byte threshold = 128),
233 "If the image has alpha channel, this method converts it to mask. All pixels
234 with alpha value less than ``threshold`` are replaced with mask colour and the
235 alpha channel is removed. Mask colour is chosen automatically using
236 `FindFirstUnusedColour`.
238 If the image image doesn't have alpha channel, ConvertAlphaToMask does
243 bool , ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b ),
244 "This method converts an image where the original alpha information is
245 only available as a shades of a colour (actually shades of grey)
246 typically when you draw anti-aliased text into a bitmap. The DC
247 drawing routines draw grey values on the black background although
248 they actually mean to draw white with differnt alpha values. This
249 method reverses it, assuming a black (!) background and white text.
250 The method will then fill up the whole image with the colour given.", "");
254 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
255 bool SetMaskFromImage(const wxImage & mask,
256 byte mr, byte mg, byte mb);
258 // void DoFloodFill (wxCoord x, wxCoord y,
259 // const wxBrush & fillBrush,
260 // const wxColour& testColour,
261 // int style = wxFLOOD_SURFACE,
262 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
264 static bool CanRead( const wxString& name );
265 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
267 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
268 %Rename(LoadMimeFile, bool, LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ));
270 bool SaveFile( const wxString& name, int type );
271 %Rename(SaveMimeFile, bool, SaveFile( const wxString& name, const wxString& mimetype ));
273 %Rename(CanReadStream, static bool, CanRead( wxInputStream& stream ));
274 %Rename(LoadStream, bool, LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ));
275 %Rename(LoadMimeStream, bool, LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ));
283 wxSize size(self->GetWidth(), self->GetHeight());
288 wxImage GetSubImage(const wxRect& rect);
290 // Paste the image or part of this image into an image of the given size at the pos
291 // any newly exposed areas will be filled with the rgb colour
292 // by default if r = g = b = -1 then fill with this image's mask colour or find and
293 // set a suitable mask colour
294 wxImage Size( const wxSize& size, const wxPoint& pos,
295 int r = -1, int g = -1, int b = -1 ) const;
298 void Paste( const wxImage &image, int x, int y );
300 //unsigned char *GetData();
301 //void SetData( unsigned char *data );
304 PyObject* GetData() {
305 unsigned char* data = self->GetData();
306 int len = self->GetWidth() * self->GetHeight() * 3;
308 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
311 void SetData(PyObject* data) {
312 unsigned char* dataPtr;
314 if (! PyString_Check(data)) {
315 wxPyBLOCK_THREADS(PyErr_SetString(PyExc_TypeError,
316 "Expected string object"));
320 size_t len = self->GetWidth() * self->GetHeight() * 3;
321 dataPtr = (unsigned char*) malloc(len);
322 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
323 self->SetData(dataPtr);
324 // wxImage takes ownership of dataPtr...
329 PyObject* GetDataBuffer() {
330 unsigned char* data = self->GetData();
331 int len = self->GetWidth() * self->GetHeight() * 3;
333 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
336 void SetDataBuffer(PyObject* data) {
337 unsigned char* buffer;
340 wxPyBlock_t blocked = wxPyBeginBlockThreads();
341 if (!PyArg_Parse(data, "t#", &buffer, &size))
344 if (size != self->GetWidth() * self->GetHeight() * 3) {
345 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
348 self->SetData(buffer);
350 wxPyEndBlockThreads(blocked);
355 PyObject* GetAlphaData() {
356 unsigned char* data = self->GetAlpha();
360 int len = self->GetWidth() * self->GetHeight();
362 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
366 void SetAlphaData(PyObject* data) {
367 unsigned char* dataPtr;
369 if (! PyString_Check(data)) {
370 PyErr_SetString(PyExc_TypeError, "Expected string object");
374 size_t len = self->GetWidth() * self->GetHeight();
375 dataPtr = (unsigned char*) malloc(len);
376 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
377 self->SetAlpha(dataPtr);
378 // wxImage takes ownership of dataPtr...
383 PyObject* GetAlphaBuffer() {
384 unsigned char* data = self->GetAlpha();
385 int len = self->GetWidth() * self->GetHeight();
387 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
390 void SetAlphaBuffer(PyObject* data) {
391 unsigned char* buffer;
394 wxPyBlock_t blocked = wxPyBeginBlockThreads();
395 if (!PyArg_Parse(data, "t#", &buffer, &size))
398 if (size != self->GetWidth() * self->GetHeight()) {
399 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
402 self->SetAlpha(buffer);
404 wxPyEndBlockThreads(blocked);
408 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
411 /*bool*/ void , GetOrFindMaskColour( unsigned char *OUTPUT,
412 unsigned char *OUTPUT,
413 unsigned char *OUTPUT ) const,
414 "GetOrFindMaskColour() -> (r,g,b)",
415 "Get the current mask colour or find a suitable colour.", "");
418 unsigned char GetMaskRed();
419 unsigned char GetMaskGreen();
420 unsigned char GetMaskBlue();
421 void SetMask( bool mask = true );
424 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
425 bool interpolating = true, wxPoint * offset_after_rotation = NULL) const ;
426 wxImage Rotate90( bool clockwise = true ) ;
427 wxImage Mirror( bool horizontally = true ) ;
429 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
430 unsigned char r2, unsigned char g2, unsigned char b2 );
432 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
433 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
435 void SetOption(const wxString& name, const wxString& value);
436 %Rename(SetOptionInt, void, SetOption(const wxString& name, int value));
437 wxString GetOption(const wxString& name) const;
438 int GetOptionInt(const wxString& name) const;
439 bool HasOption(const wxString& name) const;
441 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
442 unsigned long ComputeHistogram( wxImageHistogram& h );
444 static void AddHandler( wxImageHandler *handler );
445 static void InsertHandler( wxImageHandler *handler );
446 static bool RemoveHandler( const wxString& name );
447 static wxString GetImageExtWildcard();
450 MustHaveApp(ConvertToBitmap);
451 MustHaveApp(ConvertToMonoBitmap);
454 wxBitmap ConvertToBitmap(int depth=-1) {
455 wxBitmap bitmap(*self, depth);
459 wxBitmap ConvertToMonoBitmap( unsigned char red,
461 unsigned char blue ) {
462 wxImage mono = self->ConvertToMono( red, green, blue );
463 wxBitmap bitmap( mono, 1 );
468 %pythoncode { def __nonzero__(self): return self.Ok() }
473 ///void wxInitAllImageHandlers();
476 def InitAllImageHandlers():
478 The former functionality of InitAllImageHanders is now done internal to
479 the _core_ extension module and so this function has become a simple NOP.
486 // See also wxPy_ReinitStockObjects in helpers.cpp
488 const wxImage wxNullImage;
491 //---------------------------------------------------------------------------
493 MAKE_CONST_WXSTRING(IMAGE_OPTION_FILENAME);
494 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
495 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
496 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
497 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
498 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONX);
499 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONY);
500 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
501 MAKE_CONST_WXSTRING(IMAGE_OPTION_QUALITY);
505 wxIMAGE_RESOLUTION_INCHES = 1,
506 wxIMAGE_RESOLUTION_CM = 2
510 MAKE_CONST_WXSTRING(IMAGE_OPTION_BITSPERSAMPLE);
511 MAKE_CONST_WXSTRING(IMAGE_OPTION_SAMPLESPERPIXEL);
512 MAKE_CONST_WXSTRING(IMAGE_OPTION_COMPRESSION);
513 MAKE_CONST_WXSTRING(IMAGE_OPTION_IMAGEDESCRIPTOR);
515 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_FORMAT);
516 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_BITDEPTH);
520 wxPNG_TYPE_COLOUR = 0,
522 wxPNG_TYPE_GREY_RED = 3
527 wxBMP_24BPP = 24, // default, do not need to set
528 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
529 wxBMP_8BPP = 8, // 8bpp, quantized colors
530 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
531 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
532 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
533 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
534 wxBMP_4BPP = 4, // 4bpp, quantized colors
535 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
536 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
540 class wxBMPHandler : public wxImageHandler {
545 class wxICOHandler : public wxBMPHandler {
550 class wxCURHandler : public wxICOHandler {
555 class wxANIHandler : public wxCURHandler {
561 //---------------------------------------------------------------------------
563 class wxPNGHandler : public wxImageHandler {
569 class wxGIFHandler : public wxImageHandler {
575 class wxPCXHandler : public wxImageHandler {
581 class wxJPEGHandler : public wxImageHandler {
587 class wxPNMHandler : public wxImageHandler {
592 class wxXPMHandler : public wxImageHandler {
597 class wxTIFFHandler : public wxImageHandler {
604 class wxIFFHandler : public wxImageHandler {
610 //---------------------------------------------------------------------------
613 #include <wx/quantize.h>
617 wxQUANTIZE_INCLUDE_WINDOWS_COLOURS,
618 // wxQUANTIZE_RETURN_8BIT_DATA,
619 wxQUANTIZE_FILL_DESTINATION_IMAGE
624 "Performs quantization, or colour reduction, on a wxImage.", "");
626 class wxQuantize /*: public wxObject */
633 "Reduce the colours in the source image and put the result into the
634 destination image, setting the palette in the destination if
635 needed. Both images may be the same, to overwrite the source image.", "
636 :todo: Create a version that returns the wx.Palette used.");
638 static bool Quantize(const wxImage& src, wxImage& dest, int desiredNoColours = 236,
639 int flags = wxQUANTIZE_INCLUDE_WINDOWS_COLOURS|wxQUANTIZE_FILL_DESTINATION_IMAGE)
641 return wxQuantize::Quantize(src, dest,
644 NULL, // eightBitData
651 //---------------------------------------------------------------------------