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 //---------------------------------------------------------------------------
77 class wxImage : public wxObject {
79 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
82 // Alternate constructors
83 %name(ImageFromMime) wxImage(const wxString& name, const wxString& mimetype, int index = -1);
84 %name(ImageFromStream) wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1);
85 %name(ImageFromStreamMime) wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1 );
87 %name(EmptyImage) wxImage(int width=0, int height=0, bool clear = true) {
88 if (width > 0 && height > 0)
89 return new wxImage(width, height, clear);
94 MustHaveApp(wxImage(const wxBitmap &bitmap));
95 %name(ImageFromBitmap) wxImage(const wxBitmap &bitmap) {
96 return new wxImage(bitmap.ConvertToImage());
99 %name(ImageFromData) wxImage(int width, int height, unsigned char* data) {
100 // Copy the source data so the wxImage can clean it up later
101 unsigned char* copy = (unsigned char*)malloc(width*height*3);
106 memcpy(copy, data, width*height*3);
107 return new wxImage(width, height, copy, false);
111 void Create( int width, int height );
114 wxImage Scale( int width, int height );
115 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
116 wxImage& Rescale(int width, int height);
118 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
119 unsigned char GetRed( int x, int y );
120 unsigned char GetGreen( int x, int y );
121 unsigned char GetBlue( int x, int y );
123 void SetAlpha(int x, int y, unsigned char alpha);
124 unsigned char GetAlpha(int x, int y);
127 // find first colour that is not used in the image and has higher
128 // RGB values than <startR,startG,startB>
130 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
131 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
132 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
133 "Find first colour that is not used in the image and has higher RGB
134 values than startR, startG, startB. Returns a tuple consisting of a
135 success flag and rgb values.", "");
139 bool , ConvertAlphaToMask(byte threshold = 128),
140 "If the image has alpha channel, this method converts it to mask. All pixels
141 with alpha value less than ``threshold`` are replaced with mask colour and the
142 alpha channel is removed. Mask colour is chosen automatically using
143 `FindFirstUnusedColour`.
145 If the image image doesn't have alpha channel, ConvertAlphaToMask does
150 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
151 bool SetMaskFromImage(const wxImage & mask,
152 byte mr, byte mg, byte mb);
154 // void DoFloodFill (wxCoord x, wxCoord y,
155 // const wxBrush & fillBrush,
156 // const wxColour& testColour,
157 // int style = wxFLOOD_SURFACE,
158 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
160 static bool CanRead( const wxString& name );
161 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
163 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
164 %name(LoadMimeFile)bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
166 bool SaveFile( const wxString& name, int type );
167 %name(SaveMimeFile)bool SaveFile( const wxString& name, const wxString& mimetype );
169 %name(CanReadStream) static bool CanRead( wxInputStream& stream );
170 %name(LoadStream) bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
171 %name(LoadMimeStream) bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
179 wxSize size(self->GetWidth(), self->GetHeight());
184 wxImage GetSubImage(const wxRect& rect);
186 void Paste( const wxImage &image, int x, int y );
188 //unsigned char *GetData();
189 //void SetData( unsigned char *data );
192 PyObject* GetData() {
193 unsigned char* data = self->GetData();
194 int len = self->GetWidth() * self->GetHeight() * 3;
196 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
199 void SetData(PyObject* data) {
200 unsigned char* dataPtr;
202 if (! PyString_Check(data)) {
203 wxPyBLOCK_THREADS(PyErr_SetString(PyExc_TypeError,
204 "Expected string object"));
208 size_t len = self->GetWidth() * self->GetHeight() * 3;
209 dataPtr = (unsigned char*) malloc(len);
210 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
211 self->SetData(dataPtr);
212 // wxImage takes ownership of dataPtr...
217 PyObject* GetDataBuffer() {
218 unsigned char* data = self->GetData();
219 int len = self->GetWidth() * self->GetHeight() * 3;
221 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
224 void SetDataBuffer(PyObject* data) {
225 unsigned char* buffer;
228 bool blocked = wxPyBeginBlockThreads();
229 if (!PyArg_Parse(data, "t#", &buffer, &size))
232 if (size != self->GetWidth() * self->GetHeight() * 3) {
233 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
236 self->SetData(buffer);
238 wxPyEndBlockThreads(blocked);
243 PyObject* GetAlphaData() {
244 unsigned char* data = self->GetAlpha();
248 int len = self->GetWidth() * self->GetHeight();
250 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
254 void SetAlphaData(PyObject* data) {
255 unsigned char* dataPtr;
257 if (! PyString_Check(data)) {
258 PyErr_SetString(PyExc_TypeError, "Expected string object");
262 size_t len = self->GetWidth() * self->GetHeight();
263 dataPtr = (unsigned char*) malloc(len);
264 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
265 self->SetAlpha(dataPtr);
266 // wxImage takes ownership of dataPtr...
271 PyObject* GetAlphaBuffer() {
272 unsigned char* data = self->GetAlpha();
273 int len = self->GetWidth() * self->GetHeight();
275 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
278 void SetAlphaBuffer(PyObject* data) {
279 unsigned char* buffer;
282 bool blocked = wxPyBeginBlockThreads();
283 if (!PyArg_Parse(data, "t#", &buffer, &size))
286 if (size != self->GetWidth() * self->GetHeight()) {
287 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
290 self->SetAlpha(buffer);
292 wxPyEndBlockThreads(blocked);
296 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
297 unsigned char GetMaskRed();
298 unsigned char GetMaskGreen();
299 unsigned char GetMaskBlue();
300 void SetMask( bool mask = true );
303 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
304 bool interpolating = true, wxPoint * offset_after_rotation = NULL) const ;
305 wxImage Rotate90( bool clockwise = true ) ;
306 wxImage Mirror( bool horizontally = true ) ;
308 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
309 unsigned char r2, unsigned char g2, unsigned char b2 );
311 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
312 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
314 void SetOption(const wxString& name, const wxString& value);
315 %name(SetOptionInt)void SetOption(const wxString& name, int value);
316 wxString GetOption(const wxString& name) const;
317 int GetOptionInt(const wxString& name) const;
318 bool HasOption(const wxString& name) const;
320 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
321 unsigned long ComputeHistogram( wxImageHistogram& h );
323 static void AddHandler( wxImageHandler *handler );
324 static void InsertHandler( wxImageHandler *handler );
325 static bool RemoveHandler( const wxString& name );
326 static wxString GetImageExtWildcard();
329 MustHaveApp(ConvertToBitmap);
330 MustHaveApp(ConvertToMonoBitmap);
333 wxBitmap ConvertToBitmap(int depth=-1) {
334 wxBitmap bitmap(*self, depth);
338 wxBitmap ConvertToMonoBitmap( unsigned char red,
340 unsigned char blue ) {
341 wxImage mono = self->ConvertToMono( red, green, blue );
342 wxBitmap bitmap( mono, 1 );
347 %pythoncode { def __nonzero__(self): return self.Ok() }
352 ///void wxInitAllImageHandlers();
355 def InitAllImageHandlers():
357 The former functionality of InitAllImageHanders is now done internal to
358 the _core_ extension module and so this function has become a simple NOP.
365 // See also wxPy_ReinitStockObjects in helpers.cpp
367 const wxImage wxNullImage;
370 //---------------------------------------------------------------------------
373 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
374 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
375 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
376 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
377 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
381 wxIMAGE_RESOLUTION_INCHES = 1,
382 wxIMAGE_RESOLUTION_CM = 2
388 wxBMP_24BPP = 24, // default, do not need to set
389 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
390 wxBMP_8BPP = 8, // 8bpp, quantized colors
391 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
392 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
393 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
394 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
395 wxBMP_4BPP = 4, // 4bpp, quantized colors
396 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
397 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
401 class wxBMPHandler : public wxImageHandler {
406 class wxICOHandler : public wxBMPHandler {
411 class wxCURHandler : public wxICOHandler {
416 class wxANIHandler : public wxCURHandler {
422 //---------------------------------------------------------------------------
424 class wxPNGHandler : public wxImageHandler {
430 class wxGIFHandler : public wxImageHandler {
436 class wxPCXHandler : public wxImageHandler {
442 class wxJPEGHandler : public wxImageHandler {
448 class wxPNMHandler : public wxImageHandler {
453 class wxXPMHandler : public wxImageHandler {
458 class wxTIFFHandler : public wxImageHandler {
465 class wxIFFHandler : public wxImageHandler {
471 //---------------------------------------------------------------------------
474 #include <wx/quantize.h>
478 wxQUANTIZE_INCLUDE_WINDOWS_COLOURS,
479 // wxQUANTIZE_RETURN_8BIT_DATA,
480 wxQUANTIZE_FILL_DESTINATION_IMAGE
485 "Performs quantization, or colour reduction, on a wxImage.", "");
487 class wxQuantize /*: public wxObject */
494 "Reduce the colours in the source image and put the result into the
495 destination image, setting the palette in the destination if
496 needed. Both images may be the same, to overwrite the source image.", "
497 :todo: Create a version that returns the wx.Palette used.");
499 static bool Quantize(const wxImage& src, wxImage& dest, int desiredNoColours = 236,
500 int flags = wxQUANTIZE_INCLUDE_WINDOWS_COLOURS|wxQUANTIZE_FILL_DESTINATION_IMAGE)
502 return wxQuantize::Quantize(src, dest,
505 NULL, // eightBitData
512 //---------------------------------------------------------------------------