]> git.saurik.com Git - wxWidgets.git/blame - wxPython/src/_image.i
added a test to check how exception handling works with Win32 SEH; corrected copyright
[wxWidgets.git] / wxPython / src / _image.i
CommitLineData
cf694132 1/////////////////////////////////////////////////////////////////////////////
d14a1e28
RD
2// Name: _image.i
3// Purpose: SWIG definitions for wxImage and such
cf694132
RD
4//
5// Author: Robin Dunn
6//
d14a1e28 7// Created: 25-Sept-2000
cf694132 8// RCS-ID: $Id$
d14a1e28 9// Copyright: (c) 2003 by Total Control Software
cf694132
RD
10// Licence: wxWindows license
11/////////////////////////////////////////////////////////////////////////////
12
d14a1e28 13// Not a %module
cf694132 14
d14a1e28
RD
15
16//---------------------------------------------------------------------------
cf694132
RD
17
18%{
d14a1e28 19#include "wx/wxPython/pyistream.h"
cf694132
RD
20%}
21
9cbf6f6e 22//---------------------------------------------------------------------------
d14a1e28 23%newgroup
9cbf6f6e 24
cf694132 25
9416aa89 26class wxImageHandler : public wxObject {
cf694132 27public:
1dec68aa 28 // wxImageHandler(); Abstract Base Class
cf694132
RD
29 wxString GetName();
30 wxString GetExtension();
31 long GetType();
32 wxString GetMimeType();
33
34 //bool LoadFile(wxImage* image, wxInputStream& stream);
35 //bool SaveFile(wxImage* image, wxOutputStream& stream);
b5a5d647
RD
36 //virtual int GetImageCount( wxInputStream& stream );
37 //bool CanRead( wxInputStream& stream );
38
39 bool CanRead( const wxString& name );
cf694132
RD
40
41 void SetName(const wxString& name);
42 void SetExtension(const wxString& extension);
43 void SetType(long type);
44 void SetMimeType(const wxString& mimetype);
45};
46
cf694132 47
d14a1e28 48//---------------------------------------------------------------------------
cf694132 49
d14a1e28
RD
50class wxImageHistogram /* : public wxImageHistogramBase */
51{
06c0fba4 52public:
d14a1e28 53 wxImageHistogram();
cf694132 54
d07d2bc9 55 DocStr(MakeKey, "Get the key in the histogram for the given RGB values", "");
d14a1e28
RD
56 static unsigned long MakeKey(unsigned char r,
57 unsigned char g,
58 unsigned char b);
cf694132 59
dd9f7fea
RD
60 DocDeclAStr(
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)",
d07d2bc9
RD
68 "Find first colour that is not used in the image and has higher RGB
69values than startR, startG, startB. Returns a tuple consisting of a
70success flag and rgb values.", "");
9b3d3bc4
RD
71};
72
73
cf694132
RD
74//---------------------------------------------------------------------------
75
d14a1e28 76
9416aa89 77class wxImage : public wxObject {
cf694132 78public:
1b8c7ba6
RD
79 DocCtorStr(
80 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
81 "", "");
82
cf694132
RD
83 ~wxImage();
84
d14a1e28 85 // Alternate constructors
1b8c7ba6
RD
86 DocCtorStrName(
87 wxImage(const wxString& name, const wxString& mimetype, int index = -1),
88 "", "",
89 ImageFromMime);
90
91 DocCtorStrName(
92 wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1),
93 "", "",
94 ImageFromStream);
95
96 DocCtorStrName(
97 wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1 ),
98 "", "",
99 ImageFromStreamMime);
100
d14a1e28 101 %extend {
1b8c7ba6
RD
102 %rename(EmptyImage) wxImage(int width=0, int height=0, bool clear = true);
103 wxImage(int width=0, int height=0, bool clear = true)
104 {
d14a1e28
RD
105 if (width > 0 && height > 0)
106 return new wxImage(width, height, clear);
107 else
108 return new wxImage;
be43ef04 109 }
ab1f7d2a
RD
110
111 MustHaveApp(wxImage(const wxBitmap &bitmap));
1b8c7ba6
RD
112 %rename(ImageFromBitmap) wxImage(const wxBitmap &bitmap);
113 wxImage(const wxBitmap &bitmap)
114 {
d14a1e28
RD
115 return new wxImage(bitmap.ConvertToImage());
116 }
be43ef04 117
1b8c7ba6
RD
118 %rename(ImageFromData) wxImage(int width, int height, unsigned char* data);
119 wxImage(int width, int height, unsigned char* data)
120 {
d14a1e28
RD
121 // Copy the source data so the wxImage can clean it up later
122 unsigned char* copy = (unsigned char*)malloc(width*height*3);
123 if (copy == NULL) {
124 PyErr_NoMemory();
125 return NULL;
126 }
127 memcpy(copy, data, width*height*3);
a72f4631 128 return new wxImage(width, height, copy, false);
d14a1e28 129 }
1b8c7ba6
RD
130
131
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)
134 {
c6d42899
RD
135 // Copy the source data so the wxImage can clean it up later
136 unsigned char* dcopy = (unsigned char*)malloc(width*height*3);
137 if (dcopy == NULL) {
138 PyErr_NoMemory();
139 return NULL;
140 }
141 memcpy(dcopy, data, width*height*3);
142 unsigned char* acopy = (unsigned char*)malloc(width*height);
143 if (acopy == NULL) {
144 PyErr_NoMemory();
145 return NULL;
146 }
147 memcpy(acopy, alpha, width*height);
148
149 return new wxImage(width, height, dcopy, acopy, false);
150 }
d14a1e28 151 }
be43ef04 152
cf694132
RD
153 void Create( int width, int height );
154 void Destroy();
6c0168c9 155
cf694132 156 wxImage Scale( int width, int height );
9cbf6f6e 157 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
f6bcfd97 158 wxImage& Rescale(int width, int height);
cf694132
RD
159
160 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
161 unsigned char GetRed( int x, int y );
162 unsigned char GetGreen( int x, int y );
163 unsigned char GetBlue( int x, int y );
164
9cbf6f6e
RD
165 void SetAlpha(int x, int y, unsigned char alpha);
166 unsigned char GetAlpha(int x, int y);
167 bool HasAlpha();
168
169 // find first colour that is not used in the image and has higher
68bc8549 170 // RGB values than <startR,startG,startB>
dd9f7fea
RD
171 DocDeclAStr(
172 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
173 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
174 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
d07d2bc9
RD
175 "Find first colour that is not used in the image and has higher RGB
176values than startR, startG, startB. Returns a tuple consisting of a
177success flag and rgb values.", "");
dd9f7fea 178
125c972b
RD
179
180 DocDeclStr(
181 bool , ConvertAlphaToMask(byte threshold = 128),
182 "If the image has alpha channel, this method converts it to mask. All pixels
183with alpha value less than ``threshold`` are replaced with mask colour and the
184alpha channel is removed. Mask colour is chosen automatically using
185`FindFirstUnusedColour`.
186
187If the image image doesn't have alpha channel, ConvertAlphaToMask does
188nothing.", "");
189
190
6f7ecb3b
RD
191 DocDeclStr(
192 bool , ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b ),
193 "This method converts an image where the original alpha information is
194only available as a shades of a colour (actually shades of grey)
195typically when you draw anti-aliased text into a bitmap. The DC
196drawing routines draw grey values on the black background although
197they actually mean to draw white with differnt alpha values. This
198method reverses it, assuming a black (!) background and white text.
199The method will then fill up the whole image with the colour given.", "");
200
201
125c972b 202
68bc8549
RD
203 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
204 bool SetMaskFromImage(const wxImage & mask,
205 byte mr, byte mg, byte mb);
206
db0ff83e
RD
207// void DoFloodFill (wxCoord x, wxCoord y,
208// const wxBrush & fillBrush,
209// const wxColour& testColour,
210// int style = wxFLOOD_SURFACE,
211// int LogicalFunction = wxCOPY /* currently unused */ ) ;
68bc8549 212
96bfd053 213 static bool CanRead( const wxString& name );
b5a5d647
RD
214 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
215
216 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
1b8c7ba6 217 %Rename(LoadMimeFile, bool, LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ));
cf694132
RD
218
219 bool SaveFile( const wxString& name, int type );
1b8c7ba6 220 %Rename(SaveMimeFile, bool, SaveFile( const wxString& name, const wxString& mimetype ));
cf694132 221
1b8c7ba6
RD
222 %Rename(CanReadStream, static bool, CanRead( wxInputStream& stream ));
223 %Rename(LoadStream, bool, LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ));
224 %Rename(LoadMimeStream, bool, LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ));
f74ff5ef 225
cf694132
RD
226 bool Ok();
227 int GetWidth();
228 int GetHeight();
229
ba938c3d
RD
230 %extend {
231 wxSize GetSize() {
232 wxSize size(self->GetWidth(), self->GetHeight());
233 return size;
234 }
235 }
236
9d8bd15f 237 wxImage GetSubImage(const wxRect& rect);
f6bcfd97
BP
238 wxImage Copy();
239 void Paste( const wxImage &image, int x, int y );
9d8bd15f 240
1dc2f865
RD
241 //unsigned char *GetData();
242 //void SetData( unsigned char *data );
243
d14a1e28 244 %extend {
9cbf6f6e 245 PyObject* GetData() {
d55d6b3f
RD
246 unsigned char* data = self->GetData();
247 int len = self->GetWidth() * self->GetHeight() * 3;
d14a1e28
RD
248 PyObject* rv;
249 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
250 return rv;
d55d6b3f 251 }
9cbf6f6e
RD
252 void SetData(PyObject* data) {
253 unsigned char* dataPtr;
d55d6b3f 254
9cbf6f6e 255 if (! PyString_Check(data)) {
debff7aa
RD
256 wxPyBLOCK_THREADS(PyErr_SetString(PyExc_TypeError,
257 "Expected string object"));
9cbf6f6e
RD
258 return /* NULL */ ;
259 }
260
261 size_t len = self->GetWidth() * self->GetHeight() * 3;
262 dataPtr = (unsigned char*) malloc(len);
d14a1e28 263 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
9cbf6f6e
RD
264 self->SetData(dataPtr);
265 // wxImage takes ownership of dataPtr...
266 }
267
268
269
270 PyObject* GetDataBuffer() {
1dc2f865
RD
271 unsigned char* data = self->GetData();
272 int len = self->GetWidth() * self->GetHeight() * 3;
d14a1e28
RD
273 PyObject* rv;
274 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
275 return rv;
1dc2f865 276 }
1e4a197e
RD
277 void SetDataBuffer(PyObject* data) {
278 unsigned char* buffer;
279 int size;
280
da32eb53 281 bool blocked = wxPyBeginBlockThreads();
d14a1e28
RD
282 if (!PyArg_Parse(data, "t#", &buffer, &size))
283 goto done;
1e4a197e
RD
284
285 if (size != self->GetWidth() * self->GetHeight() * 3) {
286 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
d14a1e28 287 goto done;
1e4a197e 288 }
1e4a197e 289 self->SetData(buffer);
d14a1e28 290 done:
da32eb53 291 wxPyEndBlockThreads(blocked);
1e4a197e
RD
292 }
293
9cbf6f6e
RD
294
295
296 PyObject* GetAlphaData() {
297 unsigned char* data = self->GetAlpha();
298 if (! data) {
299 RETURN_NONE();
300 } else {
301 int len = self->GetWidth() * self->GetHeight();
d14a1e28
RD
302 PyObject* rv;
303 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
304 return rv;
9cbf6f6e
RD
305 }
306 }
307 void SetAlphaData(PyObject* data) {
1dc2f865
RD
308 unsigned char* dataPtr;
309
310 if (! PyString_Check(data)) {
311 PyErr_SetString(PyExc_TypeError, "Expected string object");
312 return /* NULL */ ;
313 }
9d8bd15f 314
9cbf6f6e 315 size_t len = self->GetWidth() * self->GetHeight();
185d7c3e 316 dataPtr = (unsigned char*) malloc(len);
d14a1e28 317 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
9cbf6f6e 318 self->SetAlpha(dataPtr);
3b5ccda1 319 // wxImage takes ownership of dataPtr...
1dc2f865 320 }
9cbf6f6e
RD
321
322
323
324 PyObject* GetAlphaBuffer() {
325 unsigned char* data = self->GetAlpha();
326 int len = self->GetWidth() * self->GetHeight();
d14a1e28
RD
327 PyObject* rv;
328 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
329 return rv;
9cbf6f6e
RD
330 }
331 void SetAlphaBuffer(PyObject* data) {
332 unsigned char* buffer;
333 int size;
334
da32eb53 335 bool blocked = wxPyBeginBlockThreads();
d14a1e28
RD
336 if (!PyArg_Parse(data, "t#", &buffer, &size))
337 goto done;
9cbf6f6e
RD
338
339 if (size != self->GetWidth() * self->GetHeight()) {
340 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
d14a1e28 341 goto done;
9cbf6f6e
RD
342 }
343 self->SetAlpha(buffer);
d14a1e28 344 done:
da32eb53 345 wxPyEndBlockThreads(blocked);
9cbf6f6e 346 }
1dc2f865 347 }
cf694132
RD
348
349 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
350 unsigned char GetMaskRed();
351 unsigned char GetMaskGreen();
352 unsigned char GetMaskBlue();
a72f4631 353 void SetMask( bool mask = true );
cf694132
RD
354 bool HasMask();
355
f6bcfd97 356 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
a72f4631
RD
357 bool interpolating = true, wxPoint * offset_after_rotation = NULL) const ;
358 wxImage Rotate90( bool clockwise = true ) ;
359 wxImage Mirror( bool horizontally = true ) ;
f6bcfd97
BP
360
361 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
362 unsigned char r2, unsigned char g2, unsigned char b2 );
363
6c0168c9
RD
364 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
365 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
366
367 void SetOption(const wxString& name, const wxString& value);
1b8c7ba6 368 %Rename(SetOptionInt, void, SetOption(const wxString& name, int value));
6c0168c9
RD
369 wxString GetOption(const wxString& name) const;
370 int GetOptionInt(const wxString& name) const;
371 bool HasOption(const wxString& name) const;
372
f6bcfd97 373 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
9cbf6f6e 374 unsigned long ComputeHistogram( wxImageHistogram& h );
f6bcfd97 375
96bfd053
RD
376 static void AddHandler( wxImageHandler *handler );
377 static void InsertHandler( wxImageHandler *handler );
378 static bool RemoveHandler( const wxString& name );
3ef86e32 379 static wxString GetImageExtWildcard();
b96c7a38
RD
380
381
ab1f7d2a
RD
382MustHaveApp(ConvertToBitmap);
383MustHaveApp(ConvertToMonoBitmap);
be43ef04 384
d14a1e28 385 %extend {
12523ae4
RD
386 wxBitmap ConvertToBitmap(int depth=-1) {
387 wxBitmap bitmap(*self, depth);
b96c7a38
RD
388 return bitmap;
389 }
390
391 wxBitmap ConvertToMonoBitmap( unsigned char red,
392 unsigned char green,
393 unsigned char blue ) {
394 wxImage mono = self->ConvertToMono( red, green, blue );
395 wxBitmap bitmap( mono, 1 );
396 return bitmap;
397 }
398 }
1fded56b 399
d14a1e28 400 %pythoncode { def __nonzero__(self): return self.Ok() }
cf694132
RD
401};
402
926bb76c 403
f74ff5ef 404
a3150741
RD
405///void wxInitAllImageHandlers();
406
407%pythoncode {
408 def InitAllImageHandlers():
409 """
410 The former functionality of InitAllImageHanders is now done internal to
411 the _core_ extension module and so this function has become a simple NOP.
412 """
413 pass
414}
415
cf694132 416
f74ff5ef 417
d14a1e28
RD
418// See also wxPy_ReinitStockObjects in helpers.cpp
419%immutable;
420const wxImage wxNullImage;
421%mutable;
cf694132 422
d14a1e28 423//---------------------------------------------------------------------------
f74ff5ef 424
b5a5d647 425
d14a1e28
RD
426MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
427MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
428MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
429MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
be43ef04 430MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
f74ff5ef 431
d14a1e28
RD
432enum
433{
434 wxIMAGE_RESOLUTION_INCHES = 1,
435 wxIMAGE_RESOLUTION_CM = 2
436};
f74ff5ef
RD
437
438
d14a1e28
RD
439enum
440{
441 wxBMP_24BPP = 24, // default, do not need to set
442 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
443 wxBMP_8BPP = 8, // 8bpp, quantized colors
444 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
445 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
446 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
447 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
448 wxBMP_4BPP = 4, // 4bpp, quantized colors
449 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
450 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
451};
f74ff5ef
RD
452
453
d14a1e28
RD
454class wxBMPHandler : public wxImageHandler {
455public:
456 wxBMPHandler();
457};
cf694132 458
d14a1e28
RD
459class wxICOHandler : public wxBMPHandler {
460public:
461 wxICOHandler();
462};
f74ff5ef 463
d14a1e28
RD
464class wxCURHandler : public wxICOHandler {
465public:
466 wxCURHandler();
467};
f74ff5ef 468
d14a1e28
RD
469class wxANIHandler : public wxCURHandler {
470public:
471 wxANIHandler();
472};
06c0fba4 473
0a651eb8 474
d14a1e28 475//---------------------------------------------------------------------------
0a651eb8 476
d14a1e28
RD
477class wxPNGHandler : public wxImageHandler {
478public:
479 wxPNGHandler();
480};
0a651eb8 481
0a651eb8 482
d14a1e28
RD
483class wxGIFHandler : public wxImageHandler {
484public:
485 wxGIFHandler();
486};
0a651eb8
RD
487
488
d14a1e28
RD
489class wxPCXHandler : public wxImageHandler {
490public:
491 wxPCXHandler();
492};
926bb76c 493
926bb76c 494
d14a1e28
RD
495class wxJPEGHandler : public wxImageHandler {
496public:
497 wxJPEGHandler();
498};
926bb76c 499
926bb76c 500
d14a1e28
RD
501class wxPNMHandler : public wxImageHandler {
502public:
503 wxPNMHandler();
504};
505
506class wxXPMHandler : public wxImageHandler {
507public:
508 wxXPMHandler();
509};
510
511class wxTIFFHandler : public wxImageHandler {
512public:
513 wxTIFFHandler();
514};
515
516
517#if wxUSE_IFF
518class wxIFFHandler : public wxImageHandler {
519public:
520 wxIFFHandler();
521};
522#endif
926bb76c 523
be43ef04
RD
524//---------------------------------------------------------------------------
525
526%{
527#include <wx/quantize.h>
528%}
529
530enum {
531 wxQUANTIZE_INCLUDE_WINDOWS_COLOURS,
532// wxQUANTIZE_RETURN_8BIT_DATA,
533 wxQUANTIZE_FILL_DESTINATION_IMAGE
534};
535
536
537DocStr(wxQuantize,
538 "Performs quantization, or colour reduction, on a wxImage.", "");
539
540class wxQuantize /*: public wxObject */
541{
542public:
543
544 %extend {
545 DocStr(
546 Quantize,
547 "Reduce the colours in the source image and put the result into the
39101468 548destination image, setting the palette in the destination if
be43ef04
RD
549needed. Both images may be the same, to overwrite the source image.", "
550:todo: Create a version that returns the wx.Palette used.");
551
552 static bool Quantize(const wxImage& src, wxImage& dest, int desiredNoColours = 236,
553 int flags = wxQUANTIZE_INCLUDE_WINDOWS_COLOURS|wxQUANTIZE_FILL_DESTINATION_IMAGE)
554 {
555 return wxQuantize::Quantize(src, dest,
556 //NULL, // palette
557 desiredNoColours,
558 NULL, // eightBitData
559 flags);
560 }
561 }
562};
563
564
cf694132 565//---------------------------------------------------------------------------