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 // resizes the image in place
164 wxImage& Resize( const wxSize& size, const wxPoint& pos,
165 int r = -1, int g = -1, int b = -1 );
167 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
170 void, SetRGB( const wxRect& rect,
171 unsigned char r, unsigned char g, unsigned char b ));
173 unsigned char GetRed( int x, int y );
174 unsigned char GetGreen( int x, int y );
175 unsigned char GetBlue( int x, int y );
177 void SetAlpha(int x, int y, unsigned char alpha);
178 unsigned char GetAlpha(int x, int y);
183 "Initializes the image alpha channel data. It is an error to call it if
184 the image already has alpha data. If it doesn't, alpha data will be by
185 default initialized to all pixels being fully opaque. But if the image
186 has a a mask colour, all mask pixels will be completely transparent.", "");
189 // find first colour that is not used in the image and has higher
190 // RGB values than <startR,startG,startB>
192 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
193 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
194 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
195 "Find first colour that is not used in the image and has higher RGB
196 values than startR, startG, startB. Returns a tuple consisting of a
197 success flag and rgb values.", "");
201 bool , ConvertAlphaToMask(byte threshold = 128),
202 "If the image has alpha channel, this method converts it to mask. All pixels
203 with alpha value less than ``threshold`` are replaced with mask colour and the
204 alpha channel is removed. Mask colour is chosen automatically using
205 `FindFirstUnusedColour`.
207 If the image image doesn't have alpha channel, ConvertAlphaToMask does
212 bool , ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b ),
213 "This method converts an image where the original alpha information is
214 only available as a shades of a colour (actually shades of grey)
215 typically when you draw anti-aliased text into a bitmap. The DC
216 drawing routines draw grey values on the black background although
217 they actually mean to draw white with differnt alpha values. This
218 method reverses it, assuming a black (!) background and white text.
219 The method will then fill up the whole image with the colour given.", "");
223 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
224 bool SetMaskFromImage(const wxImage & mask,
225 byte mr, byte mg, byte mb);
227 // void DoFloodFill (wxCoord x, wxCoord y,
228 // const wxBrush & fillBrush,
229 // const wxColour& testColour,
230 // int style = wxFLOOD_SURFACE,
231 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
233 static bool CanRead( const wxString& name );
234 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
236 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
237 %Rename(LoadMimeFile, bool, LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ));
239 bool SaveFile( const wxString& name, int type );
240 %Rename(SaveMimeFile, bool, SaveFile( const wxString& name, const wxString& mimetype ));
242 %Rename(CanReadStream, static bool, CanRead( wxInputStream& stream ));
243 %Rename(LoadStream, bool, LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ));
244 %Rename(LoadMimeStream, bool, LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ));
252 wxSize size(self->GetWidth(), self->GetHeight());
257 wxImage GetSubImage(const wxRect& rect);
259 // Paste the image or part of this image into an image of the given size at the pos
260 // any newly exposed areas will be filled with the rgb colour
261 // by default if r = g = b = -1 then fill with this image's mask colour or find and
262 // set a suitable mask colour
263 wxImage Size( const wxSize& size, const wxPoint& pos,
264 int r = -1, int g = -1, int b = -1 ) const;
267 void Paste( const wxImage &image, int x, int y );
269 //unsigned char *GetData();
270 //void SetData( unsigned char *data );
273 PyObject* GetData() {
274 unsigned char* data = self->GetData();
275 int len = self->GetWidth() * self->GetHeight() * 3;
277 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
280 void SetData(PyObject* data) {
281 unsigned char* dataPtr;
283 if (! PyString_Check(data)) {
284 wxPyBLOCK_THREADS(PyErr_SetString(PyExc_TypeError,
285 "Expected string object"));
289 size_t len = self->GetWidth() * self->GetHeight() * 3;
290 dataPtr = (unsigned char*) malloc(len);
291 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
292 self->SetData(dataPtr);
293 // wxImage takes ownership of dataPtr...
298 PyObject* GetDataBuffer() {
299 unsigned char* data = self->GetData();
300 int len = self->GetWidth() * self->GetHeight() * 3;
302 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
305 void SetDataBuffer(PyObject* data) {
306 unsigned char* buffer;
309 wxPyBlock_t blocked = wxPyBeginBlockThreads();
310 if (!PyArg_Parse(data, "t#", &buffer, &size))
313 if (size != self->GetWidth() * self->GetHeight() * 3) {
314 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
317 self->SetData(buffer);
319 wxPyEndBlockThreads(blocked);
324 PyObject* GetAlphaData() {
325 unsigned char* data = self->GetAlpha();
329 int len = self->GetWidth() * self->GetHeight();
331 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
335 void SetAlphaData(PyObject* data) {
336 unsigned char* dataPtr;
338 if (! PyString_Check(data)) {
339 PyErr_SetString(PyExc_TypeError, "Expected string object");
343 size_t len = self->GetWidth() * self->GetHeight();
344 dataPtr = (unsigned char*) malloc(len);
345 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
346 self->SetAlpha(dataPtr);
347 // wxImage takes ownership of dataPtr...
352 PyObject* GetAlphaBuffer() {
353 unsigned char* data = self->GetAlpha();
354 int len = self->GetWidth() * self->GetHeight();
356 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
359 void SetAlphaBuffer(PyObject* data) {
360 unsigned char* buffer;
363 wxPyBlock_t blocked = wxPyBeginBlockThreads();
364 if (!PyArg_Parse(data, "t#", &buffer, &size))
367 if (size != self->GetWidth() * self->GetHeight()) {
368 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
371 self->SetAlpha(buffer);
373 wxPyEndBlockThreads(blocked);
377 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
380 /*bool*/ void , GetOrFindMaskColour( unsigned char *OUTPUT,
381 unsigned char *OUTPUT,
382 unsigned char *OUTPUT ) const,
383 "GetOrFindMaskColour() -> (r,g,b)",
384 "Get the current mask colour or find a suitable colour.", "");
387 unsigned char GetMaskRed();
388 unsigned char GetMaskGreen();
389 unsigned char GetMaskBlue();
390 void SetMask( bool mask = true );
393 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
394 bool interpolating = true, wxPoint * offset_after_rotation = NULL) const ;
395 wxImage Rotate90( bool clockwise = true ) ;
396 wxImage Mirror( bool horizontally = true ) ;
398 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
399 unsigned char r2, unsigned char g2, unsigned char b2 );
401 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
402 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
404 void SetOption(const wxString& name, const wxString& value);
405 %Rename(SetOptionInt, void, SetOption(const wxString& name, int value));
406 wxString GetOption(const wxString& name) const;
407 int GetOptionInt(const wxString& name) const;
408 bool HasOption(const wxString& name) const;
410 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
411 unsigned long ComputeHistogram( wxImageHistogram& h );
413 static void AddHandler( wxImageHandler *handler );
414 static void InsertHandler( wxImageHandler *handler );
415 static bool RemoveHandler( const wxString& name );
416 static wxString GetImageExtWildcard();
419 MustHaveApp(ConvertToBitmap);
420 MustHaveApp(ConvertToMonoBitmap);
423 wxBitmap ConvertToBitmap(int depth=-1) {
424 wxBitmap bitmap(*self, depth);
428 wxBitmap ConvertToMonoBitmap( unsigned char red,
430 unsigned char blue ) {
431 wxImage mono = self->ConvertToMono( red, green, blue );
432 wxBitmap bitmap( mono, 1 );
437 %pythoncode { def __nonzero__(self): return self.Ok() }
442 ///void wxInitAllImageHandlers();
445 def InitAllImageHandlers():
447 The former functionality of InitAllImageHanders is now done internal to
448 the _core_ extension module and so this function has become a simple NOP.
455 // See also wxPy_ReinitStockObjects in helpers.cpp
457 const wxImage wxNullImage;
460 //---------------------------------------------------------------------------
462 MAKE_CONST_WXSTRING(IMAGE_OPTION_FILENAME);
463 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
464 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
465 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
466 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
467 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONX);
468 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONY);
469 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
470 MAKE_CONST_WXSTRING(IMAGE_OPTION_QUALITY);
474 wxIMAGE_RESOLUTION_INCHES = 1,
475 wxIMAGE_RESOLUTION_CM = 2
479 MAKE_CONST_WXSTRING(IMAGE_OPTION_BITSPERSAMPLE);
480 MAKE_CONST_WXSTRING(IMAGE_OPTION_SAMPLESPERPIXEL);
481 MAKE_CONST_WXSTRING(IMAGE_OPTION_COMPRESSION);
482 MAKE_CONST_WXSTRING(IMAGE_OPTION_IMAGEDESCRIPTOR);
484 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_FORMAT);
485 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_BITDEPTH);
489 wxPNG_TYPE_COLOUR = 0,
491 wxPNG_TYPE_GREY_RED = 3
496 wxBMP_24BPP = 24, // default, do not need to set
497 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
498 wxBMP_8BPP = 8, // 8bpp, quantized colors
499 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
500 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
501 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
502 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
503 wxBMP_4BPP = 4, // 4bpp, quantized colors
504 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
505 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
509 class wxBMPHandler : public wxImageHandler {
514 class wxICOHandler : public wxBMPHandler {
519 class wxCURHandler : public wxICOHandler {
524 class wxANIHandler : public wxCURHandler {
530 //---------------------------------------------------------------------------
532 class wxPNGHandler : public wxImageHandler {
538 class wxGIFHandler : public wxImageHandler {
544 class wxPCXHandler : public wxImageHandler {
550 class wxJPEGHandler : public wxImageHandler {
556 class wxPNMHandler : public wxImageHandler {
561 class wxXPMHandler : public wxImageHandler {
566 class wxTIFFHandler : public wxImageHandler {
573 class wxIFFHandler : public wxImageHandler {
579 //---------------------------------------------------------------------------
582 #include <wx/quantize.h>
586 wxQUANTIZE_INCLUDE_WINDOWS_COLOURS,
587 // wxQUANTIZE_RETURN_8BIT_DATA,
588 wxQUANTIZE_FILL_DESTINATION_IMAGE
593 "Performs quantization, or colour reduction, on a wxImage.", "");
595 class wxQuantize /*: public wxObject */
602 "Reduce the colours in the source image and put the result into the
603 destination image, setting the palette in the destination if
604 needed. Both images may be the same, to overwrite the source image.", "
605 :todo: Create a version that returns the wx.Palette used.");
607 static bool Quantize(const wxImage& src, wxImage& dest, int desiredNoColours = 236,
608 int flags = wxQUANTIZE_INCLUDE_WINDOWS_COLOURS|wxQUANTIZE_FILL_DESTINATION_IMAGE)
610 return wxQuantize::Quantize(src, dest,
613 NULL, // eightBitData
620 //---------------------------------------------------------------------------