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 // get the key in the histogram for the given RGB values
56 static unsigned long MakeKey(unsigned char r,
60 // find first colour that is not used in the image and has higher
61 // RGB values than RGB(startR, startG, startB)
63 // returns true and puts this colour in r, g, b (each of which may be NULL)
64 // on success or returns false if there are no more free colours
65 bool FindFirstUnusedColour(unsigned char *OUTPUT,
66 unsigned char *OUTPUT,
67 unsigned char *OUTPUT,
68 unsigned char startR = 1,
69 unsigned char startG = 0,
70 unsigned char startB = 0 ) const;
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 %name(ImageFromBitmap) wxImage(const wxBitmap &bitmap) {
95 return new wxImage(bitmap.ConvertToImage());
98 %name(ImageFromData) wxImage(int width, int height, unsigned char* data) {
99 // Copy the source data so the wxImage can clean it up later
100 unsigned char* copy = (unsigned char*)malloc(width*height*3);
105 memcpy(copy, data, width*height*3);
106 return new wxImage(width, height, copy, FALSE);
110 void Create( int width, int height );
113 wxImage Scale( int width, int height );
114 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
115 wxImage& Rescale(int width, int height);
117 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
118 unsigned char GetRed( int x, int y );
119 unsigned char GetGreen( int x, int y );
120 unsigned char GetBlue( int x, int y );
122 void SetAlpha(int x, int y, unsigned char alpha);
123 unsigned char GetAlpha(int x, int y);
126 // find first colour that is not used in the image and has higher
127 // RGB values than <startR,startG,startB>
128 bool FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
129 byte startR = 0, byte startG = 0, byte startB = 0 ) const;
131 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
132 bool SetMaskFromImage(const wxImage & mask,
133 byte mr, byte mg, byte mb);
135 // void DoFloodFill (wxCoord x, wxCoord y,
136 // const wxBrush & fillBrush,
137 // const wxColour& testColour,
138 // int style = wxFLOOD_SURFACE,
139 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
141 static bool CanRead( const wxString& name );
142 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
144 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
145 %name(LoadMimeFile)bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
147 bool SaveFile( const wxString& name, int type );
148 %name(SaveMimeFile)bool SaveFile( const wxString& name, const wxString& mimetype );
150 %name(CanReadStream) static bool CanRead( wxInputStream& stream );
151 %name(LoadStream) bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
152 %name(LoadMimeStream) bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
158 wxImage GetSubImage(const wxRect& rect);
160 void Paste( const wxImage &image, int x, int y );
162 //unsigned char *GetData();
163 //void SetData( unsigned char *data );
166 PyObject* GetData() {
167 unsigned char* data = self->GetData();
168 int len = self->GetWidth() * self->GetHeight() * 3;
170 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
173 void SetData(PyObject* data) {
174 unsigned char* dataPtr;
176 if (! PyString_Check(data)) {
177 PyErr_SetString(PyExc_TypeError, "Expected string object");
181 size_t len = self->GetWidth() * self->GetHeight() * 3;
182 dataPtr = (unsigned char*) malloc(len);
183 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
184 self->SetData(dataPtr);
185 // wxImage takes ownership of dataPtr...
190 PyObject* GetDataBuffer() {
191 unsigned char* data = self->GetData();
192 int len = self->GetWidth() * self->GetHeight() * 3;
194 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
197 void SetDataBuffer(PyObject* data) {
198 unsigned char* buffer;
201 wxPyBeginBlockThreads();
202 if (!PyArg_Parse(data, "t#", &buffer, &size))
205 if (size != self->GetWidth() * self->GetHeight() * 3) {
206 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
209 self->SetData(buffer);
211 wxPyEndBlockThreads();
216 PyObject* GetAlphaData() {
217 unsigned char* data = self->GetAlpha();
221 int len = self->GetWidth() * self->GetHeight();
223 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
227 void SetAlphaData(PyObject* data) {
228 unsigned char* dataPtr;
230 if (! PyString_Check(data)) {
231 PyErr_SetString(PyExc_TypeError, "Expected string object");
235 size_t len = self->GetWidth() * self->GetHeight();
236 dataPtr = (unsigned char*) malloc(len);
237 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
238 self->SetAlpha(dataPtr);
239 // wxImage takes ownership of dataPtr...
244 PyObject* GetAlphaBuffer() {
245 unsigned char* data = self->GetAlpha();
246 int len = self->GetWidth() * self->GetHeight();
248 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
251 void SetAlphaBuffer(PyObject* data) {
252 unsigned char* buffer;
255 wxPyBeginBlockThreads();
256 if (!PyArg_Parse(data, "t#", &buffer, &size))
259 if (size != self->GetWidth() * self->GetHeight()) {
260 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
263 self->SetAlpha(buffer);
265 wxPyEndBlockThreads();
269 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
270 unsigned char GetMaskRed();
271 unsigned char GetMaskGreen();
272 unsigned char GetMaskBlue();
273 void SetMask( bool mask = TRUE );
276 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
277 bool interpolating = TRUE, wxPoint * offset_after_rotation = NULL) const ;
278 wxImage Rotate90( bool clockwise = TRUE ) ;
279 wxImage Mirror( bool horizontally = TRUE ) ;
281 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
282 unsigned char r2, unsigned char g2, unsigned char b2 );
284 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
285 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
287 void SetOption(const wxString& name, const wxString& value);
288 %name(SetOptionInt)void SetOption(const wxString& name, int value);
289 wxString GetOption(const wxString& name) const;
290 int GetOptionInt(const wxString& name) const;
291 bool HasOption(const wxString& name) const;
293 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
294 unsigned long ComputeHistogram( wxImageHistogram& h );
296 static void AddHandler( wxImageHandler *handler );
297 static void InsertHandler( wxImageHandler *handler );
298 static bool RemoveHandler( const wxString& name );
299 static wxString GetImageExtWildcard();
303 wxBitmap ConvertToBitmap() {
304 wxBitmap bitmap(*self);
308 wxBitmap ConvertToMonoBitmap( unsigned char red,
310 unsigned char blue ) {
311 wxImage mono = self->ConvertToMono( red, green, blue );
312 wxBitmap bitmap( mono, 1 );
317 %pythoncode { def __nonzero__(self): return self.Ok() }
322 void wxInitAllImageHandlers();
325 // See also wxPy_ReinitStockObjects in helpers.cpp
327 const wxImage wxNullImage;
330 //---------------------------------------------------------------------------
333 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
334 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
335 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
336 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
337 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
341 wxIMAGE_RESOLUTION_INCHES = 1,
342 wxIMAGE_RESOLUTION_CM = 2
348 wxBMP_24BPP = 24, // default, do not need to set
349 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
350 wxBMP_8BPP = 8, // 8bpp, quantized colors
351 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
352 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
353 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
354 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
355 wxBMP_4BPP = 4, // 4bpp, quantized colors
356 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
357 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
361 class wxBMPHandler : public wxImageHandler {
366 class wxICOHandler : public wxBMPHandler {
371 class wxCURHandler : public wxICOHandler {
376 class wxANIHandler : public wxCURHandler {
382 //---------------------------------------------------------------------------
384 class wxPNGHandler : public wxImageHandler {
390 class wxGIFHandler : public wxImageHandler {
396 class wxPCXHandler : public wxImageHandler {
402 class wxJPEGHandler : public wxImageHandler {
408 class wxPNMHandler : public wxImageHandler {
413 class wxXPMHandler : public wxImageHandler {
418 class wxTIFFHandler : public wxImageHandler {
425 class wxIFFHandler : public wxImageHandler {
431 //---------------------------------------------------------------------------