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 values than\n"
69 "startR, startG, startB. Returns a tuple consisting of a success flag and rgb\n"
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 );
91 %nokwargs wxImage(int width=0, int height=0, bool clear = True);
92 %nokwargs wxImage(const wxSize& size, bool clear = True);
93 %name(EmptyImage) wxImage(int width=0, int height=0, bool clear = True) {
94 if (width > 0 && height > 0)
95 return new wxImage(width, height, clear);
99 %name(EmptyImage) wxImage(const wxSize& size, bool clear = True) {
100 return new wxImage(size.x, size.y, clear);
104 %name(ImageFromBitmap) wxImage(const wxBitmap &bitmap) {
105 return new wxImage(bitmap.ConvertToImage());
108 %name(ImageFromData) wxImage(int width, int height, unsigned char* data) {
109 // Copy the source data so the wxImage can clean it up later
110 unsigned char* copy = (unsigned char*)malloc(width*height*3);
115 memcpy(copy, data, width*height*3);
116 return new wxImage(width, height, copy, False);
120 void Create( int width, int height );
123 wxImage Scale( int width, int height );
124 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
125 wxImage& Rescale(int width, int height);
127 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
128 unsigned char GetRed( int x, int y );
129 unsigned char GetGreen( int x, int y );
130 unsigned char GetBlue( int x, int y );
132 void SetAlpha(int x, int y, unsigned char alpha);
133 unsigned char GetAlpha(int x, int y);
136 // find first colour that is not used in the image and has higher
137 // RGB values than <startR,startG,startB>
139 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
140 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
141 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
142 "Find first colour that is not used in the image and has higher RGB values than\n"
143 "startR, startG, startB. Returns a tuple consisting of a success flag and rgb\n"
147 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
148 bool SetMaskFromImage(const wxImage & mask,
149 byte mr, byte mg, byte mb);
151 // void DoFloodFill (wxCoord x, wxCoord y,
152 // const wxBrush & fillBrush,
153 // const wxColour& testColour,
154 // int style = wxFLOOD_SURFACE,
155 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
157 static bool CanRead( const wxString& name );
158 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
160 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
161 %name(LoadMimeFile)bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
163 bool SaveFile( const wxString& name, int type );
164 %name(SaveMimeFile)bool SaveFile( const wxString& name, const wxString& mimetype );
166 %name(CanReadStream) static bool CanRead( wxInputStream& stream );
167 %name(LoadStream) bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
168 %name(LoadMimeStream) bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
176 wxSize size(self->GetWidth(), self->GetHeight());
181 wxImage GetSubImage(const wxRect& rect);
183 void Paste( const wxImage &image, int x, int y );
185 //unsigned char *GetData();
186 //void SetData( unsigned char *data );
189 PyObject* GetData() {
190 unsigned char* data = self->GetData();
191 int len = self->GetWidth() * self->GetHeight() * 3;
193 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
196 void SetData(PyObject* data) {
197 unsigned char* dataPtr;
199 if (! PyString_Check(data)) {
200 PyErr_SetString(PyExc_TypeError, "Expected string object");
204 size_t len = self->GetWidth() * self->GetHeight() * 3;
205 dataPtr = (unsigned char*) malloc(len);
206 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
207 self->SetData(dataPtr);
208 // wxImage takes ownership of dataPtr...
213 PyObject* GetDataBuffer() {
214 unsigned char* data = self->GetData();
215 int len = self->GetWidth() * self->GetHeight() * 3;
217 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
220 void SetDataBuffer(PyObject* data) {
221 unsigned char* buffer;
224 bool blocked = wxPyBeginBlockThreads();
225 if (!PyArg_Parse(data, "t#", &buffer, &size))
228 if (size != self->GetWidth() * self->GetHeight() * 3) {
229 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
232 self->SetData(buffer);
234 wxPyEndBlockThreads(blocked);
239 PyObject* GetAlphaData() {
240 unsigned char* data = self->GetAlpha();
244 int len = self->GetWidth() * self->GetHeight();
246 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
250 void SetAlphaData(PyObject* data) {
251 unsigned char* dataPtr;
253 if (! PyString_Check(data)) {
254 PyErr_SetString(PyExc_TypeError, "Expected string object");
258 size_t len = self->GetWidth() * self->GetHeight();
259 dataPtr = (unsigned char*) malloc(len);
260 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
261 self->SetAlpha(dataPtr);
262 // wxImage takes ownership of dataPtr...
267 PyObject* GetAlphaBuffer() {
268 unsigned char* data = self->GetAlpha();
269 int len = self->GetWidth() * self->GetHeight();
271 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
274 void SetAlphaBuffer(PyObject* data) {
275 unsigned char* buffer;
278 bool blocked = wxPyBeginBlockThreads();
279 if (!PyArg_Parse(data, "t#", &buffer, &size))
282 if (size != self->GetWidth() * self->GetHeight()) {
283 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
286 self->SetAlpha(buffer);
288 wxPyEndBlockThreads(blocked);
292 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
293 unsigned char GetMaskRed();
294 unsigned char GetMaskGreen();
295 unsigned char GetMaskBlue();
296 void SetMask( bool mask = True );
299 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
300 bool interpolating = True, wxPoint * offset_after_rotation = NULL) const ;
301 wxImage Rotate90( bool clockwise = True ) ;
302 wxImage Mirror( bool horizontally = True ) ;
304 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
305 unsigned char r2, unsigned char g2, unsigned char b2 );
307 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
308 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
310 void SetOption(const wxString& name, const wxString& value);
311 %name(SetOptionInt)void SetOption(const wxString& name, int value);
312 wxString GetOption(const wxString& name) const;
313 int GetOptionInt(const wxString& name) const;
314 bool HasOption(const wxString& name) const;
316 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
317 unsigned long ComputeHistogram( wxImageHistogram& h );
319 static void AddHandler( wxImageHandler *handler );
320 static void InsertHandler( wxImageHandler *handler );
321 static bool RemoveHandler( const wxString& name );
322 static wxString GetImageExtWildcard();
326 wxBitmap ConvertToBitmap() {
327 wxBitmap bitmap(*self);
331 wxBitmap ConvertToMonoBitmap( unsigned char red,
333 unsigned char blue ) {
334 wxImage mono = self->ConvertToMono( red, green, blue );
335 wxBitmap bitmap( mono, 1 );
340 %pythoncode { def __nonzero__(self): return self.Ok() }
345 void wxInitAllImageHandlers();
348 // See also wxPy_ReinitStockObjects in helpers.cpp
350 const wxImage wxNullImage;
353 //---------------------------------------------------------------------------
356 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
357 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
358 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
359 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
360 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
364 wxIMAGE_RESOLUTION_INCHES = 1,
365 wxIMAGE_RESOLUTION_CM = 2
371 wxBMP_24BPP = 24, // default, do not need to set
372 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
373 wxBMP_8BPP = 8, // 8bpp, quantized colors
374 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
375 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
376 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
377 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
378 wxBMP_4BPP = 4, // 4bpp, quantized colors
379 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
380 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
384 class wxBMPHandler : public wxImageHandler {
389 class wxICOHandler : public wxBMPHandler {
394 class wxCURHandler : public wxICOHandler {
399 class wxANIHandler : public wxCURHandler {
405 //---------------------------------------------------------------------------
407 class wxPNGHandler : public wxImageHandler {
413 class wxGIFHandler : public wxImageHandler {
419 class wxPCXHandler : public wxImageHandler {
425 class wxJPEGHandler : public wxImageHandler {
431 class wxPNMHandler : public wxImageHandler {
436 class wxXPMHandler : public wxImageHandler {
441 class wxTIFFHandler : public wxImageHandler {
448 class wxIFFHandler : public wxImageHandler {
454 //---------------------------------------------------------------------------