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 );
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>
129 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
130 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
131 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
132 "Find first colour that is not used in the image and has higher RGB values than\n"
133 "startR, startG, startB. Returns a tuple consisting of a success flag and rgb\n"
137 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
138 bool SetMaskFromImage(const wxImage & mask,
139 byte mr, byte mg, byte mb);
141 // void DoFloodFill (wxCoord x, wxCoord y,
142 // const wxBrush & fillBrush,
143 // const wxColour& testColour,
144 // int style = wxFLOOD_SURFACE,
145 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
147 static bool CanRead( const wxString& name );
148 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
150 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
151 %name(LoadMimeFile)bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
153 bool SaveFile( const wxString& name, int type );
154 %name(SaveMimeFile)bool SaveFile( const wxString& name, const wxString& mimetype );
156 %name(CanReadStream) static bool CanRead( wxInputStream& stream );
157 %name(LoadStream) bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
158 %name(LoadMimeStream) bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
164 wxImage GetSubImage(const wxRect& rect);
166 void Paste( const wxImage &image, int x, int y );
168 //unsigned char *GetData();
169 //void SetData( unsigned char *data );
172 PyObject* GetData() {
173 unsigned char* data = self->GetData();
174 int len = self->GetWidth() * self->GetHeight() * 3;
176 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
179 void SetData(PyObject* data) {
180 unsigned char* dataPtr;
182 if (! PyString_Check(data)) {
183 PyErr_SetString(PyExc_TypeError, "Expected string object");
187 size_t len = self->GetWidth() * self->GetHeight() * 3;
188 dataPtr = (unsigned char*) malloc(len);
189 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
190 self->SetData(dataPtr);
191 // wxImage takes ownership of dataPtr...
196 PyObject* GetDataBuffer() {
197 unsigned char* data = self->GetData();
198 int len = self->GetWidth() * self->GetHeight() * 3;
200 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
203 void SetDataBuffer(PyObject* data) {
204 unsigned char* buffer;
207 bool blocked = wxPyBeginBlockThreads();
208 if (!PyArg_Parse(data, "t#", &buffer, &size))
211 if (size != self->GetWidth() * self->GetHeight() * 3) {
212 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
215 self->SetData(buffer);
217 wxPyEndBlockThreads(blocked);
222 PyObject* GetAlphaData() {
223 unsigned char* data = self->GetAlpha();
227 int len = self->GetWidth() * self->GetHeight();
229 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
233 void SetAlphaData(PyObject* data) {
234 unsigned char* dataPtr;
236 if (! PyString_Check(data)) {
237 PyErr_SetString(PyExc_TypeError, "Expected string object");
241 size_t len = self->GetWidth() * self->GetHeight();
242 dataPtr = (unsigned char*) malloc(len);
243 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
244 self->SetAlpha(dataPtr);
245 // wxImage takes ownership of dataPtr...
250 PyObject* GetAlphaBuffer() {
251 unsigned char* data = self->GetAlpha();
252 int len = self->GetWidth() * self->GetHeight();
254 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
257 void SetAlphaBuffer(PyObject* data) {
258 unsigned char* buffer;
261 bool blocked = wxPyBeginBlockThreads();
262 if (!PyArg_Parse(data, "t#", &buffer, &size))
265 if (size != self->GetWidth() * self->GetHeight()) {
266 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
269 self->SetAlpha(buffer);
271 wxPyEndBlockThreads(blocked);
275 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
276 unsigned char GetMaskRed();
277 unsigned char GetMaskGreen();
278 unsigned char GetMaskBlue();
279 void SetMask( bool mask = True );
282 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
283 bool interpolating = True, wxPoint * offset_after_rotation = NULL) const ;
284 wxImage Rotate90( bool clockwise = True ) ;
285 wxImage Mirror( bool horizontally = True ) ;
287 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
288 unsigned char r2, unsigned char g2, unsigned char b2 );
290 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
291 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
293 void SetOption(const wxString& name, const wxString& value);
294 %name(SetOptionInt)void SetOption(const wxString& name, int value);
295 wxString GetOption(const wxString& name) const;
296 int GetOptionInt(const wxString& name) const;
297 bool HasOption(const wxString& name) const;
299 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
300 unsigned long ComputeHistogram( wxImageHistogram& h );
302 static void AddHandler( wxImageHandler *handler );
303 static void InsertHandler( wxImageHandler *handler );
304 static bool RemoveHandler( const wxString& name );
305 static wxString GetImageExtWildcard();
309 wxBitmap ConvertToBitmap() {
310 wxBitmap bitmap(*self);
314 wxBitmap ConvertToMonoBitmap( unsigned char red,
316 unsigned char blue ) {
317 wxImage mono = self->ConvertToMono( red, green, blue );
318 wxBitmap bitmap( mono, 1 );
323 %pythoncode { def __nonzero__(self): return self.Ok() }
328 void wxInitAllImageHandlers();
331 // See also wxPy_ReinitStockObjects in helpers.cpp
333 const wxImage wxNullImage;
336 //---------------------------------------------------------------------------
339 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
340 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
341 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
342 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
343 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
347 wxIMAGE_RESOLUTION_INCHES = 1,
348 wxIMAGE_RESOLUTION_CM = 2
354 wxBMP_24BPP = 24, // default, do not need to set
355 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
356 wxBMP_8BPP = 8, // 8bpp, quantized colors
357 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
358 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
359 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
360 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
361 wxBMP_4BPP = 4, // 4bpp, quantized colors
362 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
363 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
367 class wxBMPHandler : public wxImageHandler {
372 class wxICOHandler : public wxBMPHandler {
377 class wxCURHandler : public wxICOHandler {
382 class wxANIHandler : public wxCURHandler {
388 //---------------------------------------------------------------------------
390 class wxPNGHandler : public wxImageHandler {
396 class wxGIFHandler : public wxImageHandler {
402 class wxPCXHandler : public wxImageHandler {
408 class wxJPEGHandler : public wxImageHandler {
414 class wxPNMHandler : public wxImageHandler {
419 class wxXPMHandler : public wxImageHandler {
424 class wxTIFFHandler : public wxImageHandler {
431 class wxIFFHandler : public wxImageHandler {
437 //---------------------------------------------------------------------------