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 {
80 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
85 // Alternate constructors
87 wxImage(const wxString& name, const wxString& mimetype, int index = -1),
92 wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1),
97 wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1 ),
102 %rename(EmptyImage) wxImage(int width=0, int height=0, bool clear = true);
103 wxImage(int width=0, int height=0, bool clear = true)
105 if (width > 0 && height > 0)
106 return new wxImage(width, height, clear);
111 MustHaveApp(wxImage(const wxBitmap &bitmap));
112 %rename(ImageFromBitmap) wxImage(const wxBitmap &bitmap);
113 wxImage(const wxBitmap &bitmap)
115 return new wxImage(bitmap.ConvertToImage());
118 %rename(ImageFromData) wxImage(int width, int height, unsigned char* data);
119 wxImage(int width, int height, unsigned char* data)
121 // Copy the source data so the wxImage can clean it up later
122 unsigned char* copy = (unsigned char*)malloc(width*height*3);
127 memcpy(copy, data, width*height*3);
128 return new wxImage(width, height, copy, false);
132 %rename(ImageFromDataWithAlpha) wxImage(int width, int height, unsigned char* data, unsigned char* alpha);
133 wxImage(int width, int height, unsigned char* data, unsigned char* alpha)
135 // Copy the source data so the wxImage can clean it up later
136 unsigned char* dcopy = (unsigned char*)malloc(width*height*3);
141 memcpy(dcopy, data, width*height*3);
142 unsigned char* acopy = (unsigned char*)malloc(width*height);
147 memcpy(acopy, alpha, width*height);
149 return new wxImage(width, height, dcopy, acopy, false);
153 // TODO: wxImage( char** xpmData );
156 void Create( int width, int height );
159 wxImage Scale( int width, int height );
160 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
161 wxImage& Rescale(int width, int height);
163 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
164 unsigned char GetRed( int x, int y );
165 unsigned char GetGreen( int x, int y );
166 unsigned char GetBlue( int x, int y );
168 void SetAlpha(int x, int y, unsigned char alpha);
169 unsigned char GetAlpha(int x, int y);
174 "Initializes the image alpha channel data. It is an error to call it if
175 the image already has alpha data. If it doesn't, alpha data will be by
176 default initialized to all pixels being fully opaque. But if the image
177 has a a mask colour, all mask pixels will be completely transparent.", "");
180 // find first colour that is not used in the image and has higher
181 // RGB values than <startR,startG,startB>
183 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
184 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
185 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
186 "Find first colour that is not used in the image and has higher RGB
187 values than startR, startG, startB. Returns a tuple consisting of a
188 success flag and rgb values.", "");
192 bool , ConvertAlphaToMask(byte threshold = 128),
193 "If the image has alpha channel, this method converts it to mask. All pixels
194 with alpha value less than ``threshold`` are replaced with mask colour and the
195 alpha channel is removed. Mask colour is chosen automatically using
196 `FindFirstUnusedColour`.
198 If the image image doesn't have alpha channel, ConvertAlphaToMask does
203 bool , ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b ),
204 "This method converts an image where the original alpha information is
205 only available as a shades of a colour (actually shades of grey)
206 typically when you draw anti-aliased text into a bitmap. The DC
207 drawing routines draw grey values on the black background although
208 they actually mean to draw white with differnt alpha values. This
209 method reverses it, assuming a black (!) background and white text.
210 The method will then fill up the whole image with the colour given.", "");
214 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
215 bool SetMaskFromImage(const wxImage & mask,
216 byte mr, byte mg, byte mb);
218 // void DoFloodFill (wxCoord x, wxCoord y,
219 // const wxBrush & fillBrush,
220 // const wxColour& testColour,
221 // int style = wxFLOOD_SURFACE,
222 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
224 static bool CanRead( const wxString& name );
225 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
227 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
228 %Rename(LoadMimeFile, bool, LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ));
230 bool SaveFile( const wxString& name, int type );
231 %Rename(SaveMimeFile, bool, SaveFile( const wxString& name, const wxString& mimetype ));
233 %Rename(CanReadStream, static bool, CanRead( wxInputStream& stream ));
234 %Rename(LoadStream, bool, LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ));
235 %Rename(LoadMimeStream, bool, LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ));
243 wxSize size(self->GetWidth(), self->GetHeight());
248 wxImage GetSubImage(const wxRect& rect);
250 void Paste( const wxImage &image, int x, int y );
252 //unsigned char *GetData();
253 //void SetData( unsigned char *data );
256 PyObject* GetData() {
257 unsigned char* data = self->GetData();
258 int len = self->GetWidth() * self->GetHeight() * 3;
260 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
263 void SetData(PyObject* data) {
264 unsigned char* dataPtr;
266 if (! PyString_Check(data)) {
267 wxPyBLOCK_THREADS(PyErr_SetString(PyExc_TypeError,
268 "Expected string object"));
272 size_t len = self->GetWidth() * self->GetHeight() * 3;
273 dataPtr = (unsigned char*) malloc(len);
274 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
275 self->SetData(dataPtr);
276 // wxImage takes ownership of dataPtr...
281 PyObject* GetDataBuffer() {
282 unsigned char* data = self->GetData();
283 int len = self->GetWidth() * self->GetHeight() * 3;
285 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
288 void SetDataBuffer(PyObject* data) {
289 unsigned char* buffer;
292 bool blocked = wxPyBeginBlockThreads();
293 if (!PyArg_Parse(data, "t#", &buffer, &size))
296 if (size != self->GetWidth() * self->GetHeight() * 3) {
297 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
300 self->SetData(buffer);
302 wxPyEndBlockThreads(blocked);
307 PyObject* GetAlphaData() {
308 unsigned char* data = self->GetAlpha();
312 int len = self->GetWidth() * self->GetHeight();
314 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
318 void SetAlphaData(PyObject* data) {
319 unsigned char* dataPtr;
321 if (! PyString_Check(data)) {
322 PyErr_SetString(PyExc_TypeError, "Expected string object");
326 size_t len = self->GetWidth() * self->GetHeight();
327 dataPtr = (unsigned char*) malloc(len);
328 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
329 self->SetAlpha(dataPtr);
330 // wxImage takes ownership of dataPtr...
335 PyObject* GetAlphaBuffer() {
336 unsigned char* data = self->GetAlpha();
337 int len = self->GetWidth() * self->GetHeight();
339 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
342 void SetAlphaBuffer(PyObject* data) {
343 unsigned char* buffer;
346 bool blocked = wxPyBeginBlockThreads();
347 if (!PyArg_Parse(data, "t#", &buffer, &size))
350 if (size != self->GetWidth() * self->GetHeight()) {
351 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
354 self->SetAlpha(buffer);
356 wxPyEndBlockThreads(blocked);
360 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
361 unsigned char GetMaskRed();
362 unsigned char GetMaskGreen();
363 unsigned char GetMaskBlue();
364 void SetMask( bool mask = true );
367 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
368 bool interpolating = true, wxPoint * offset_after_rotation = NULL) const ;
369 wxImage Rotate90( bool clockwise = true ) ;
370 wxImage Mirror( bool horizontally = true ) ;
372 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
373 unsigned char r2, unsigned char g2, unsigned char b2 );
375 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
376 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
378 void SetOption(const wxString& name, const wxString& value);
379 %Rename(SetOptionInt, void, SetOption(const wxString& name, int value));
380 wxString GetOption(const wxString& name) const;
381 int GetOptionInt(const wxString& name) const;
382 bool HasOption(const wxString& name) const;
384 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
385 unsigned long ComputeHistogram( wxImageHistogram& h );
387 static void AddHandler( wxImageHandler *handler );
388 static void InsertHandler( wxImageHandler *handler );
389 static bool RemoveHandler( const wxString& name );
390 static wxString GetImageExtWildcard();
393 MustHaveApp(ConvertToBitmap);
394 MustHaveApp(ConvertToMonoBitmap);
397 wxBitmap ConvertToBitmap(int depth=-1) {
398 wxBitmap bitmap(*self, depth);
402 wxBitmap ConvertToMonoBitmap( unsigned char red,
404 unsigned char blue ) {
405 wxImage mono = self->ConvertToMono( red, green, blue );
406 wxBitmap bitmap( mono, 1 );
411 %pythoncode { def __nonzero__(self): return self.Ok() }
416 ///void wxInitAllImageHandlers();
419 def InitAllImageHandlers():
421 The former functionality of InitAllImageHanders is now done internal to
422 the _core_ extension module and so this function has become a simple NOP.
429 // See also wxPy_ReinitStockObjects in helpers.cpp
431 const wxImage wxNullImage;
434 //---------------------------------------------------------------------------
436 MAKE_CONST_WXSTRING(IMAGE_OPTION_FILENAME);
437 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
438 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
439 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
440 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
441 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONX);
442 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONY);
443 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
444 MAKE_CONST_WXSTRING(IMAGE_OPTION_QUALITY);
448 wxIMAGE_RESOLUTION_INCHES = 1,
449 wxIMAGE_RESOLUTION_CM = 2
453 MAKE_CONST_WXSTRING(IMAGE_OPTION_BITSPERSAMPLE);
454 MAKE_CONST_WXSTRING(IMAGE_OPTION_SAMPLESPERPIXEL);
455 MAKE_CONST_WXSTRING(IMAGE_OPTION_COMPRESSION);
456 MAKE_CONST_WXSTRING(IMAGE_OPTION_IMAGEDESCRIPTOR);
458 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_FORMAT);
459 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_BITDEPTH);
463 wxPNG_TYPE_COLOUR = 0,
465 wxPNG_TYPE_GREY_RED = 3
470 wxBMP_24BPP = 24, // default, do not need to set
471 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
472 wxBMP_8BPP = 8, // 8bpp, quantized colors
473 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
474 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
475 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
476 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
477 wxBMP_4BPP = 4, // 4bpp, quantized colors
478 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
479 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
483 class wxBMPHandler : public wxImageHandler {
488 class wxICOHandler : public wxBMPHandler {
493 class wxCURHandler : public wxICOHandler {
498 class wxANIHandler : public wxCURHandler {
504 //---------------------------------------------------------------------------
506 class wxPNGHandler : public wxImageHandler {
512 class wxGIFHandler : public wxImageHandler {
518 class wxPCXHandler : public wxImageHandler {
524 class wxJPEGHandler : public wxImageHandler {
530 class wxPNMHandler : public wxImageHandler {
535 class wxXPMHandler : public wxImageHandler {
540 class wxTIFFHandler : public wxImageHandler {
547 class wxIFFHandler : public wxImageHandler {
553 //---------------------------------------------------------------------------
556 #include <wx/quantize.h>
560 wxQUANTIZE_INCLUDE_WINDOWS_COLOURS,
561 // wxQUANTIZE_RETURN_8BIT_DATA,
562 wxQUANTIZE_FILL_DESTINATION_IMAGE
567 "Performs quantization, or colour reduction, on a wxImage.", "");
569 class wxQuantize /*: public wxObject */
576 "Reduce the colours in the source image and put the result into the
577 destination image, setting the palette in the destination if
578 needed. Both images may be the same, to overwrite the source image.", "
579 :todo: Create a version that returns the wx.Palette used.");
581 static bool Quantize(const wxImage& src, wxImage& dest, int desiredNoColours = 236,
582 int flags = wxQUANTIZE_INCLUDE_WINDOWS_COLOURS|wxQUANTIZE_FILL_DESTINATION_IMAGE)
584 return wxQuantize::Quantize(src, dest,
587 NULL, // eightBitData
594 //---------------------------------------------------------------------------