]> git.saurik.com Git - wxWidgets.git/blob - wxPython/src/_image.i
better root dir for rc uploads
[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 %extend {
73 DocStr(GetCount,
74 "Returns the pixel count for the given key. Use `MakeKey` to create a
75 key value from a RGB tripple.", "");
76 unsigned long GetCount(unsigned long key) {
77 wxImageHistogramEntry e = (*self)[key];
78 return e.value;
79 }
80
81 DocStr(GetCountRGB,
82 "Returns the pixel count for the given RGB values.", "");
83 unsigned long GetCountRGB(unsigned char r,
84 unsigned char g,
85 unsigned char b) {
86 unsigned long key = wxImageHistogram::MakeKey(r, g, b);
87 wxImageHistogramEntry e = (*self)[key];
88 return e.value;
89 }
90
91 DocStr(GetCountColour,
92 "Returns the pixel count for the given `wx.Colour` value.", "");
93 unsigned long GetCountColour(const wxColour& colour) {
94 unsigned long key = wxImageHistogram::MakeKey(colour.Red(),
95 colour.Green(),
96 colour.Blue());
97 wxImageHistogramEntry e = (*self)[key];
98 return e.value;
99 }
100 }
101
102 };
103
104
105 //---------------------------------------------------------------------------
106
107
108 class wxImage : public wxObject {
109 public:
110 DocCtorStr(
111 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
112 "", "");
113
114 ~wxImage();
115
116 // Alternate constructors
117 DocCtorStrName(
118 wxImage(const wxString& name, const wxString& mimetype, int index = -1),
119 "", "",
120 ImageFromMime);
121
122 DocCtorStrName(
123 wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1),
124 "", "",
125 ImageFromStream);
126
127 DocCtorStrName(
128 wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1 ),
129 "", "",
130 ImageFromStreamMime);
131
132 %extend {
133 %rename(EmptyImage) wxImage(int width=0, int height=0, bool clear = true);
134 wxImage(int width=0, int height=0, bool clear = true)
135 {
136 if (width > 0 && height > 0)
137 return new wxImage(width, height, clear);
138 else
139 return new wxImage;
140 }
141
142 MustHaveApp(wxImage(const wxBitmap &bitmap));
143 %rename(ImageFromBitmap) wxImage(const wxBitmap &bitmap);
144 wxImage(const wxBitmap &bitmap)
145 {
146 return new wxImage(bitmap.ConvertToImage());
147 }
148
149 %rename(ImageFromData) wxImage(int width, int height, unsigned char* data);
150 wxImage(int width, int height, unsigned char* data)
151 {
152 // Copy the source data so the wxImage can clean it up later
153 unsigned char* copy = (unsigned char*)malloc(width*height*3);
154 if (copy == NULL) {
155 PyErr_NoMemory();
156 return NULL;
157 }
158 memcpy(copy, data, width*height*3);
159 return new wxImage(width, height, copy, false);
160 }
161
162
163 %rename(ImageFromDataWithAlpha) wxImage(int width, int height, unsigned char* data, unsigned char* alpha);
164 wxImage(int width, int height, unsigned char* data, unsigned char* alpha)
165 {
166 // Copy the source data so the wxImage can clean it up later
167 unsigned char* dcopy = (unsigned char*)malloc(width*height*3);
168 if (dcopy == NULL) {
169 PyErr_NoMemory();
170 return NULL;
171 }
172 memcpy(dcopy, data, width*height*3);
173 unsigned char* acopy = (unsigned char*)malloc(width*height);
174 if (acopy == NULL) {
175 PyErr_NoMemory();
176 return NULL;
177 }
178 memcpy(acopy, alpha, width*height);
179
180 return new wxImage(width, height, dcopy, acopy, false);
181 }
182 }
183
184 // TODO: wxImage( char** xpmData );
185
186
187 void Create( int width, int height );
188 void Destroy();
189
190 wxImage Scale( int width, int height );
191 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
192 wxImage& Rescale(int width, int height);
193
194 // resizes the image in place
195 wxImage& Resize( const wxSize& size, const wxPoint& pos,
196 int r = -1, int g = -1, int b = -1 );
197
198 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
199
200 %Rename(SetRGBRect,
201 void, SetRGB( const wxRect& rect,
202 unsigned char r, unsigned char g, unsigned char b ));
203
204 unsigned char GetRed( int x, int y );
205 unsigned char GetGreen( int x, int y );
206 unsigned char GetBlue( int x, int y );
207
208 void SetAlpha(int x, int y, unsigned char alpha);
209 unsigned char GetAlpha(int x, int y);
210 bool HasAlpha();
211
212 DocDeclStr(
213 void , InitAlpha(),
214 "Initializes the image alpha channel data. It is an error to call it if
215 the image already has alpha data. If it doesn't, alpha data will be by
216 default initialized to all pixels being fully opaque. But if the image
217 has a a mask colour, all mask pixels will be completely transparent.", "");
218
219
220 // find first colour that is not used in the image and has higher
221 // RGB values than <startR,startG,startB>
222 DocDeclAStr(
223 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
224 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
225 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
226 "Find first colour that is not used in the image and has higher RGB
227 values than startR, startG, startB. Returns a tuple consisting of a
228 success flag and rgb values.", "");
229
230
231 DocDeclStr(
232 bool , ConvertAlphaToMask(byte threshold = 128),
233 "If the image has alpha channel, this method converts it to mask. All pixels
234 with alpha value less than ``threshold`` are replaced with mask colour and the
235 alpha channel is removed. Mask colour is chosen automatically using
236 `FindFirstUnusedColour`.
237
238 If the image image doesn't have alpha channel, ConvertAlphaToMask does
239 nothing.", "");
240
241
242 DocDeclStr(
243 bool , ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b ),
244 "This method converts an image where the original alpha information is
245 only available as a shades of a colour (actually shades of grey)
246 typically when you draw anti-aliased text into a bitmap. The DC
247 drawing routines draw grey values on the black background although
248 they actually mean to draw white with differnt alpha values. This
249 method reverses it, assuming a black (!) background and white text.
250 The method will then fill up the whole image with the colour given.", "");
251
252
253
254 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
255 bool SetMaskFromImage(const wxImage & mask,
256 byte mr, byte mg, byte mb);
257
258 // void DoFloodFill (wxCoord x, wxCoord y,
259 // const wxBrush & fillBrush,
260 // const wxColour& testColour,
261 // int style = wxFLOOD_SURFACE,
262 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
263
264 static bool CanRead( const wxString& name );
265 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
266
267 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
268 %Rename(LoadMimeFile, bool, LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ));
269
270 bool SaveFile( const wxString& name, int type );
271 %Rename(SaveMimeFile, bool, SaveFile( const wxString& name, const wxString& mimetype ));
272
273 %Rename(CanReadStream, static bool, CanRead( wxInputStream& stream ));
274 %Rename(LoadStream, bool, LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ));
275 %Rename(LoadMimeStream, bool, LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ));
276
277 bool Ok();
278 int GetWidth();
279 int GetHeight();
280
281 %extend {
282 wxSize GetSize() {
283 wxSize size(self->GetWidth(), self->GetHeight());
284 return size;
285 }
286 }
287
288 wxImage GetSubImage(const wxRect& rect);
289
290 // Paste the image or part of this image into an image of the given size at the pos
291 // any newly exposed areas will be filled with the rgb colour
292 // by default if r = g = b = -1 then fill with this image's mask colour or find and
293 // set a suitable mask colour
294 wxImage Size( const wxSize& size, const wxPoint& pos,
295 int r = -1, int g = -1, int b = -1 ) const;
296
297 wxImage Copy();
298 void Paste( const wxImage &image, int x, int y );
299
300 //unsigned char *GetData();
301 //void SetData( unsigned char *data );
302
303 %extend {
304 PyObject* GetData() {
305 unsigned char* data = self->GetData();
306 int len = self->GetWidth() * self->GetHeight() * 3;
307 PyObject* rv;
308 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
309 return rv;
310 }
311 void SetData(PyObject* data) {
312 unsigned char* dataPtr;
313
314 if (! PyString_Check(data)) {
315 wxPyBLOCK_THREADS(PyErr_SetString(PyExc_TypeError,
316 "Expected string object"));
317 return /* NULL */ ;
318 }
319
320 size_t len = self->GetWidth() * self->GetHeight() * 3;
321 dataPtr = (unsigned char*) malloc(len);
322 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
323 self->SetData(dataPtr);
324 // wxImage takes ownership of dataPtr...
325 }
326
327
328
329 PyObject* GetDataBuffer() {
330 unsigned char* data = self->GetData();
331 int len = self->GetWidth() * self->GetHeight() * 3;
332 PyObject* rv;
333 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
334 return rv;
335 }
336 void SetDataBuffer(PyObject* data) {
337 unsigned char* buffer;
338 int size;
339
340 wxPyBlock_t blocked = wxPyBeginBlockThreads();
341 if (!PyArg_Parse(data, "t#", &buffer, &size))
342 goto done;
343
344 if (size != self->GetWidth() * self->GetHeight() * 3) {
345 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
346 goto done;
347 }
348 self->SetData(buffer);
349 done:
350 wxPyEndBlockThreads(blocked);
351 }
352
353
354
355 PyObject* GetAlphaData() {
356 unsigned char* data = self->GetAlpha();
357 if (! data) {
358 RETURN_NONE();
359 } else {
360 int len = self->GetWidth() * self->GetHeight();
361 PyObject* rv;
362 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
363 return rv;
364 }
365 }
366 void SetAlphaData(PyObject* data) {
367 unsigned char* dataPtr;
368
369 if (! PyString_Check(data)) {
370 PyErr_SetString(PyExc_TypeError, "Expected string object");
371 return /* NULL */ ;
372 }
373
374 size_t len = self->GetWidth() * self->GetHeight();
375 dataPtr = (unsigned char*) malloc(len);
376 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
377 self->SetAlpha(dataPtr);
378 // wxImage takes ownership of dataPtr...
379 }
380
381
382
383 PyObject* GetAlphaBuffer() {
384 unsigned char* data = self->GetAlpha();
385 int len = self->GetWidth() * self->GetHeight();
386 PyObject* rv;
387 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
388 return rv;
389 }
390 void SetAlphaBuffer(PyObject* data) {
391 unsigned char* buffer;
392 int size;
393
394 wxPyBlock_t blocked = wxPyBeginBlockThreads();
395 if (!PyArg_Parse(data, "t#", &buffer, &size))
396 goto done;
397
398 if (size != self->GetWidth() * self->GetHeight()) {
399 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
400 goto done;
401 }
402 self->SetAlpha(buffer);
403 done:
404 wxPyEndBlockThreads(blocked);
405 }
406 }
407
408 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
409
410 DocDeclAStr(
411 /*bool*/ void , GetOrFindMaskColour( unsigned char *OUTPUT,
412 unsigned char *OUTPUT,
413 unsigned char *OUTPUT ) const,
414 "GetOrFindMaskColour() -> (r,g,b)",
415 "Get the current mask colour or find a suitable colour.", "");
416
417
418 unsigned char GetMaskRed();
419 unsigned char GetMaskGreen();
420 unsigned char GetMaskBlue();
421 void SetMask( bool mask = true );
422 bool HasMask();
423
424 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
425 bool interpolating = true, wxPoint * offset_after_rotation = NULL) const ;
426 wxImage Rotate90( bool clockwise = true ) ;
427 wxImage Mirror( bool horizontally = true ) ;
428
429 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
430 unsigned char r2, unsigned char g2, unsigned char b2 );
431
432 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
433 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
434
435 void SetOption(const wxString& name, const wxString& value);
436 %Rename(SetOptionInt, void, SetOption(const wxString& name, int value));
437 wxString GetOption(const wxString& name) const;
438 int GetOptionInt(const wxString& name) const;
439 bool HasOption(const wxString& name) const;
440
441 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
442 unsigned long ComputeHistogram( wxImageHistogram& h );
443
444 static void AddHandler( wxImageHandler *handler );
445 static void InsertHandler( wxImageHandler *handler );
446 static bool RemoveHandler( const wxString& name );
447 static wxString GetImageExtWildcard();
448
449
450 MustHaveApp(ConvertToBitmap);
451 MustHaveApp(ConvertToMonoBitmap);
452
453 %extend {
454 wxBitmap ConvertToBitmap(int depth=-1) {
455 wxBitmap bitmap(*self, depth);
456 return bitmap;
457 }
458
459 wxBitmap ConvertToMonoBitmap( unsigned char red,
460 unsigned char green,
461 unsigned char blue ) {
462 wxImage mono = self->ConvertToMono( red, green, blue );
463 wxBitmap bitmap( mono, 1 );
464 return bitmap;
465 }
466 }
467
468 %pythoncode { def __nonzero__(self): return self.Ok() }
469 };
470
471
472
473 ///void wxInitAllImageHandlers();
474
475 %pythoncode {
476 def InitAllImageHandlers():
477 """
478 The former functionality of InitAllImageHanders is now done internal to
479 the _core_ extension module and so this function has become a simple NOP.
480 """
481 pass
482 }
483
484
485
486 // See also wxPy_ReinitStockObjects in helpers.cpp
487 %immutable;
488 const wxImage wxNullImage;
489 %mutable;
490
491 //---------------------------------------------------------------------------
492
493 MAKE_CONST_WXSTRING(IMAGE_OPTION_FILENAME);
494 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
495 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
496 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
497 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
498 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONX);
499 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONY);
500 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
501 MAKE_CONST_WXSTRING(IMAGE_OPTION_QUALITY);
502
503 enum
504 {
505 wxIMAGE_RESOLUTION_INCHES = 1,
506 wxIMAGE_RESOLUTION_CM = 2
507 };
508
509
510 MAKE_CONST_WXSTRING(IMAGE_OPTION_BITSPERSAMPLE);
511 MAKE_CONST_WXSTRING(IMAGE_OPTION_SAMPLESPERPIXEL);
512 MAKE_CONST_WXSTRING(IMAGE_OPTION_COMPRESSION);
513 MAKE_CONST_WXSTRING(IMAGE_OPTION_IMAGEDESCRIPTOR);
514
515 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_FORMAT);
516 MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_BITDEPTH);
517
518 enum
519 {
520 wxPNG_TYPE_COLOUR = 0,
521 wxPNG_TYPE_GREY = 2,
522 wxPNG_TYPE_GREY_RED = 3
523 };
524
525 enum
526 {
527 wxBMP_24BPP = 24, // default, do not need to set
528 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
529 wxBMP_8BPP = 8, // 8bpp, quantized colors
530 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
531 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
532 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
533 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
534 wxBMP_4BPP = 4, // 4bpp, quantized colors
535 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
536 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
537 };
538
539
540 class wxBMPHandler : public wxImageHandler {
541 public:
542 wxBMPHandler();
543 };
544
545 class wxICOHandler : public wxBMPHandler {
546 public:
547 wxICOHandler();
548 };
549
550 class wxCURHandler : public wxICOHandler {
551 public:
552 wxCURHandler();
553 };
554
555 class wxANIHandler : public wxCURHandler {
556 public:
557 wxANIHandler();
558 };
559
560
561 //---------------------------------------------------------------------------
562
563 class wxPNGHandler : public wxImageHandler {
564 public:
565 wxPNGHandler();
566 };
567
568
569 class wxGIFHandler : public wxImageHandler {
570 public:
571 wxGIFHandler();
572 };
573
574
575 class wxPCXHandler : public wxImageHandler {
576 public:
577 wxPCXHandler();
578 };
579
580
581 class wxJPEGHandler : public wxImageHandler {
582 public:
583 wxJPEGHandler();
584 };
585
586
587 class wxPNMHandler : public wxImageHandler {
588 public:
589 wxPNMHandler();
590 };
591
592 class wxXPMHandler : public wxImageHandler {
593 public:
594 wxXPMHandler();
595 };
596
597 class wxTIFFHandler : public wxImageHandler {
598 public:
599 wxTIFFHandler();
600 };
601
602
603 #if wxUSE_IFF
604 class wxIFFHandler : public wxImageHandler {
605 public:
606 wxIFFHandler();
607 };
608 #endif
609
610 //---------------------------------------------------------------------------
611
612 %{
613 #include <wx/quantize.h>
614 %}
615
616 enum {
617 wxQUANTIZE_INCLUDE_WINDOWS_COLOURS,
618 // wxQUANTIZE_RETURN_8BIT_DATA,
619 wxQUANTIZE_FILL_DESTINATION_IMAGE
620 };
621
622
623 DocStr(wxQuantize,
624 "Performs quantization, or colour reduction, on a wxImage.", "");
625
626 class wxQuantize /*: public wxObject */
627 {
628 public:
629
630 %extend {
631 DocStr(
632 Quantize,
633 "Reduce the colours in the source image and put the result into the
634 destination image, setting the palette in the destination if
635 needed. Both images may be the same, to overwrite the source image.", "
636 :todo: Create a version that returns the wx.Palette used.");
637
638 static bool Quantize(const wxImage& src, wxImage& dest, int desiredNoColours = 236,
639 int flags = wxQUANTIZE_INCLUDE_WINDOWS_COLOURS|wxQUANTIZE_FILL_DESTINATION_IMAGE)
640 {
641 return wxQuantize::Quantize(src, dest,
642 //NULL, // palette
643 desiredNoColours,
644 NULL, // eightBitData
645 flags);
646 }
647 }
648 };
649
650
651 //---------------------------------------------------------------------------