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