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.", "");
138 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
139 bool SetMaskFromImage(const wxImage & mask,
140 byte mr, byte mg, byte mb);
142 // void DoFloodFill (wxCoord x, wxCoord y,
143 // const wxBrush & fillBrush,
144 // const wxColour& testColour,
145 // int style = wxFLOOD_SURFACE,
146 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
148 static bool CanRead( const wxString& name );
149 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
151 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
152 %name(LoadMimeFile)bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
154 bool SaveFile( const wxString& name, int type );
155 %name(SaveMimeFile)bool SaveFile( const wxString& name, const wxString& mimetype );
157 %name(CanReadStream) static bool CanRead( wxInputStream& stream );
158 %name(LoadStream) bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
159 %name(LoadMimeStream) bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
167 wxSize size(self->GetWidth(), self->GetHeight());
172 wxImage GetSubImage(const wxRect& rect);
174 void Paste( const wxImage &image, int x, int y );
176 //unsigned char *GetData();
177 //void SetData( unsigned char *data );
180 PyObject* GetData() {
181 unsigned char* data = self->GetData();
182 int len = self->GetWidth() * self->GetHeight() * 3;
184 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
187 void SetData(PyObject* data) {
188 unsigned char* dataPtr;
190 if (! PyString_Check(data)) {
191 PyErr_SetString(PyExc_TypeError, "Expected string object");
195 size_t len = self->GetWidth() * self->GetHeight() * 3;
196 dataPtr = (unsigned char*) malloc(len);
197 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
198 self->SetData(dataPtr);
199 // wxImage takes ownership of dataPtr...
204 PyObject* GetDataBuffer() {
205 unsigned char* data = self->GetData();
206 int len = self->GetWidth() * self->GetHeight() * 3;
208 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
211 void SetDataBuffer(PyObject* data) {
212 unsigned char* buffer;
215 bool blocked = wxPyBeginBlockThreads();
216 if (!PyArg_Parse(data, "t#", &buffer, &size))
219 if (size != self->GetWidth() * self->GetHeight() * 3) {
220 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
223 self->SetData(buffer);
225 wxPyEndBlockThreads(blocked);
230 PyObject* GetAlphaData() {
231 unsigned char* data = self->GetAlpha();
235 int len = self->GetWidth() * self->GetHeight();
237 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
241 void SetAlphaData(PyObject* data) {
242 unsigned char* dataPtr;
244 if (! PyString_Check(data)) {
245 PyErr_SetString(PyExc_TypeError, "Expected string object");
249 size_t len = self->GetWidth() * self->GetHeight();
250 dataPtr = (unsigned char*) malloc(len);
251 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
252 self->SetAlpha(dataPtr);
253 // wxImage takes ownership of dataPtr...
258 PyObject* GetAlphaBuffer() {
259 unsigned char* data = self->GetAlpha();
260 int len = self->GetWidth() * self->GetHeight();
262 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
265 void SetAlphaBuffer(PyObject* data) {
266 unsigned char* buffer;
269 bool blocked = wxPyBeginBlockThreads();
270 if (!PyArg_Parse(data, "t#", &buffer, &size))
273 if (size != self->GetWidth() * self->GetHeight()) {
274 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
277 self->SetAlpha(buffer);
279 wxPyEndBlockThreads(blocked);
283 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
284 unsigned char GetMaskRed();
285 unsigned char GetMaskGreen();
286 unsigned char GetMaskBlue();
287 void SetMask( bool mask = True );
290 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
291 bool interpolating = True, wxPoint * offset_after_rotation = NULL) const ;
292 wxImage Rotate90( bool clockwise = True ) ;
293 wxImage Mirror( bool horizontally = True ) ;
295 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
296 unsigned char r2, unsigned char g2, unsigned char b2 );
298 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
299 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
301 void SetOption(const wxString& name, const wxString& value);
302 %name(SetOptionInt)void SetOption(const wxString& name, int value);
303 wxString GetOption(const wxString& name) const;
304 int GetOptionInt(const wxString& name) const;
305 bool HasOption(const wxString& name) const;
307 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
308 unsigned long ComputeHistogram( wxImageHistogram& h );
310 static void AddHandler( wxImageHandler *handler );
311 static void InsertHandler( wxImageHandler *handler );
312 static bool RemoveHandler( const wxString& name );
313 static wxString GetImageExtWildcard();
316 MustHaveApp(ConvertToBitmap);
317 MustHaveApp(ConvertToMonoBitmap);
320 wxBitmap ConvertToBitmap() {
321 wxBitmap bitmap(*self);
325 wxBitmap ConvertToMonoBitmap( unsigned char red,
327 unsigned char blue ) {
328 wxImage mono = self->ConvertToMono( red, green, blue );
329 wxBitmap bitmap( mono, 1 );
334 %pythoncode { def __nonzero__(self): return self.Ok() }
339 ///void wxInitAllImageHandlers();
342 def InitAllImageHandlers():
344 The former functionality of InitAllImageHanders is now done internal to
345 the _core_ extension module and so this function has become a simple NOP.
352 // See also wxPy_ReinitStockObjects in helpers.cpp
354 const wxImage wxNullImage;
357 //---------------------------------------------------------------------------
360 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
361 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
362 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
363 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
364 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
368 wxIMAGE_RESOLUTION_INCHES = 1,
369 wxIMAGE_RESOLUTION_CM = 2
375 wxBMP_24BPP = 24, // default, do not need to set
376 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
377 wxBMP_8BPP = 8, // 8bpp, quantized colors
378 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
379 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
380 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
381 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
382 wxBMP_4BPP = 4, // 4bpp, quantized colors
383 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
384 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
388 class wxBMPHandler : public wxImageHandler {
393 class wxICOHandler : public wxBMPHandler {
398 class wxCURHandler : public wxICOHandler {
403 class wxANIHandler : public wxCURHandler {
409 //---------------------------------------------------------------------------
411 class wxPNGHandler : public wxImageHandler {
417 class wxGIFHandler : public wxImageHandler {
423 class wxPCXHandler : public wxImageHandler {
429 class wxJPEGHandler : public wxImageHandler {
435 class wxPNMHandler : public wxImageHandler {
440 class wxXPMHandler : public wxImageHandler {
445 class wxTIFFHandler : public wxImageHandler {
452 class wxIFFHandler : public wxImageHandler {
458 //---------------------------------------------------------------------------