]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/src/_image.i
cleanup - heavy reformatting
[wxWidgets.git] / wxPython / src / _image.i
... / ...
CommitLineData
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
24enum {
25 wxIMAGE_ALPHA_TRANSPARENT,
26 wxIMAGE_ALPHA_THRESHOLD,
27 wxIMAGE_ALPHA_OPAQUE
28};
29
30
31//---------------------------------------------------------------------------
32%newgroup
33
34DocStr(wxImageHandler,
35"This is the base class for implementing image file loading/saving, and
36image creation from data. It is used within `wx.Image` and is not
37normally seen by the application.", "");
38class wxImageHandler : public wxObject {
39public:
40 // wxImageHandler(); Abstract Base Class
41 wxString GetName();
42 wxString GetExtension();
43 long GetType();
44 wxString GetMimeType();
45
46 //bool LoadFile(wxImage* image, wxInputStream& stream);
47 //bool SaveFile(wxImage* image, wxOutputStream& stream);
48 //virtual int GetImageCount( wxInputStream& stream );
49 //bool CanRead( wxInputStream& stream );
50
51 bool CanRead( const wxString& name );
52
53 void SetName(const wxString& name);
54 void SetExtension(const wxString& extension);
55 void SetType(long type);
56 void SetMimeType(const wxString& mimetype);
57};
58
59
60//---------------------------------------------------------------------------
61
62
63DocStr(wxPyImageHandler,
64"This is the base class for implementing image file loading/saving, and
65image creation from data, all written in Python. To create a custom
66image handler derive a new class from wx.PyImageHandler and provide
67the following methods::
68
69 def DoCanRead(self, stream) --> bool
70 '''Check if this handler can read the image on the stream'''
71
72 def LoadFile(self, image, stream, verbose, index) --> bool
73 '''Load image data from the stream and load it into image.'''
74
75 def SaveFile(self, image, stream, verbose) --> bool
76 '''Save the iamge data in image to the stream using
77 this handler's image file format.'''
78
79 def GetImageCount(self, stream) --> int
80 '''If this image format can hold more than one image,
81 how many does the image on the stream have?'''
82
83To activate your handler create an instance of it and pass it to
84`wx.Image_AddHandler`. Be sure to call `SetName`, `SetType`, and
85`SetExtension` from your constructor.
86", "");
87
88class wxPyImageHandler: public wxImageHandler {
89public:
90 %pythonAppend wxPyImageHandler() "self._SetSelf(self)"
91 wxPyImageHandler();
92 void _SetSelf(PyObject *self);
93};
94
95
96//---------------------------------------------------------------------------
97
98
99class wxImageHistogram /* : public wxImageHistogramBase */
100{
101public:
102 wxImageHistogram();
103
104 DocStr(MakeKey, "Get the key in the histogram for the given RGB values", "");
105 static unsigned long MakeKey(byte r,
106 byte g,
107 byte b);
108
109 DocDeclAStr(
110 bool, FindFirstUnusedColour(byte *OUTPUT,
111 byte *OUTPUT,
112 byte *OUTPUT,
113 byte startR = 1,
114 byte startG = 0,
115 byte startB = 0 ) const,
116 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
117 "Find first colour that is not used in the image and has higher RGB
118values than startR, startG, startB. Returns a tuple consisting of a
119success flag and rgb values.", "");
120
121 %extend {
122 DocStr(GetCount,
123 "Returns the pixel count for the given key. Use `MakeKey` to create a
124key value from a RGB tripple.", "");
125 unsigned long GetCount(unsigned long key) {
126 wxImageHistogramEntry e = (*self)[key];
127 return e.value;
128 }
129
130 DocStr(GetCountRGB,
131 "Returns the pixel count for the given RGB values.", "");
132 unsigned long GetCountRGB(byte r,
133 byte g,
134 byte b) {
135 unsigned long key = wxImageHistogram::MakeKey(r, g, b);
136 wxImageHistogramEntry e = (*self)[key];
137 return e.value;
138 }
139
140 DocStr(GetCountColour,
141 "Returns the pixel count for the given `wx.Colour` value.", "");
142 unsigned long GetCountColour(const wxColour& colour) {
143 unsigned long key = wxImageHistogram::MakeKey(colour.Red(),
144 colour.Green(),
145 colour.Blue());
146 wxImageHistogramEntry e = (*self)[key];
147 return e.value;
148 }
149 }
150
151};
152
153
154//---------------------------------------------------------------------------
155
156%{
157 typedef unsigned char* buffer;
158%}
159
160%typemap(in) (buffer data, int DATASIZE)
161 { if (!PyArg_Parse($input, "t#", &$1, &$2)) SWIG_fail; }
162
163%typemap(in) (buffer alpha, int ALPHASIZE)
164 { if (!PyArg_Parse($input, "t#", &$1, &$2)) SWIG_fail; }
165
166//---------------------------------------------------------------------------
167
168
169DocStr(wxImage,
170"A platform-independent image class. An image can be created from
171data, or using `wx.Bitmap.ConvertToImage`, or loaded from a file in a
172variety of formats. Functions are available to set and get image
173bits, so it can be used for basic image manipulation.
174
175A wx.Image cannot be drawn directly to a `wx.DC`. Instead, a
176platform-specific `wx.Bitmap` object must be created from it using the
177`wx.BitmapFromImage` constructor. This bitmap can then be drawn in a
178device context, using `wx.DC.DrawBitmap`.
179
180One colour value of the image may be used as a mask colour which will
181lead to the automatic creation of a `wx.Mask` object associated to the
182bitmap object.
183
184wx.Image supports alpha channel data, that is in addition to a byte
185for the red, green and blue colour components for each pixel it also
186stores a byte representing the pixel opacity. An alpha value of 0
187corresponds to a transparent pixel (null opacity) while a value of 255
188means that the pixel is 100% opaque.
189
190Unlike RGB data, not all images have an alpha channel and before using
191`GetAlpha` you should check if this image contains an alpha channel
192with `HasAlpha`. Note that currently only images loaded from PNG files
193with transparency information will have an alpha channel.", "");
194
195
196%{
197// Pull the nested class out to the top level for SWIG's sake
198#define wxImage_RGBValue wxImage::RGBValue
199#define wxImage_HSVValue wxImage::HSVValue
200%}
201
202DocStr(wxImage_RGBValue,
203"An object that contains values for red, green and blue which represent
204the value of a color. It is used by `wx.Image.HSVtoRGB` and
205`wx.Image.RGBtoHSV`, which converts between HSV color space and RGB
206color space.", "");
207class wxImage_RGBValue
208{
209public:
210 DocCtorStr(
211 wxImage_RGBValue(byte r=0, byte g=0, byte b=0),
212 "Constructor.", "");
213 byte red;
214 byte green;
215 byte blue;
216};
217
218
219DocStr(wxImage_HSVValue,
220"An object that contains values for hue, saturation and value which
221represent the value of a color. It is used by `wx.Image.HSVtoRGB` and
222`wx.Image.RGBtoHSV`, which +converts between HSV color space and RGB
223color space.", "");
224class wxImage_HSVValue
225{
226public:
227 DocCtorStr(
228 wxImage_HSVValue(double h=0.0, double s=0.0, double v=0.0),
229 "Constructor.", "");
230 double hue;
231 double saturation;
232 double value;
233};
234
235
236class wxImage : public wxObject {
237public:
238 %typemap(out) wxImage*; // turn off this typemap
239
240 DocCtorStr(
241 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
242 "Loads an image from a file.",
243 "
244 :param name: Name of the file from which to load the image.
245
246 :param type: May be one of the following:
247
248 ==================== =======================================
249 wx.BITMAP_TYPE_BMP Load a Windows bitmap file.
250 wx.BITMAP_TYPE_GIF Load a GIF bitmap file.
251 wx.BITMAP_TYPE_JPEG Load a JPEG bitmap file.
252 wx.BITMAP_TYPE_PNG Load a PNG bitmap file.
253 wx.BITMAP_TYPE_PCX Load a PCX bitmap file.
254 wx.BITMAP_TYPE_PNM Load a PNM bitmap file.
255 wx.BITMAP_TYPE_TIF Load a TIFF bitmap file.
256 wx.BITMAP_TYPE_XPM Load a XPM bitmap file.
257 wx.BITMAP_TYPE_ICO Load a Windows icon file (ICO).
258 wx.BITMAP_TYPE_CUR Load a Windows cursor file (CUR).
259 wx.BITMAP_TYPE_ANI Load a Windows animated cursor file (ANI).
260 wx.BITMAP_TYPE_ANY Will try to autodetect the format.
261 ==================== =======================================
262
263 :param index: Index of the image to load in the case that the
264 image file contains multiple images. This is only used by GIF,
265 ICO and TIFF handlers. The default value (-1) means to choose
266 the default image and is interpreted as the first image (the
267 one with index=0) by the GIF and TIFF handler and as the
268 largest and most colourful one by the ICO handler.
269
270:see: `wx.ImageFromMime`, `wx.ImageFromStream`, `wx.ImageFromStreamMime`,
271 `wx.EmptyImage`, `wx.ImageFromBitmap`, `wx.ImageFromData`,
272 `wx.ImageFromDataWithAlpha`
273");
274
275 ~wxImage();
276
277 // Alternate constructors
278 DocCtorStrName(
279 wxImage(const wxString& name, const wxString& mimetype, int index = -1),
280 "Loads an image from a file, using a MIME type string (such as
281'image/jpeg') to specify image type.", "
282
283:see: `wx.Image`",
284 ImageFromMime);
285
286 DocCtorStrName(
287 wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1),
288 "Loads an image from an input stream, or any readable Python file-like
289object.", "
290
291:see: `wx.Image`",
292 ImageFromStream);
293
294 DocCtorStrName(
295 wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1 ),
296 "Loads an image from an input stream, or any readable Python file-like
297object, specifying the image format with a MIME type string.", "
298
299:see: `wx.Image`",
300 ImageFromStreamMime);
301
302 %extend {
303 %RenameDocCtor(
304 EmptyImage,
305 "Construct an empty image of a given size, optionally setting all
306pixels to black.", "
307
308:see: `wx.Image`",
309 wxImage(int width=0, int height=0, bool clear = true))
310 {
311 if (width > 0 && height > 0)
312 return new wxImage(width, height, clear);
313 else
314 return new wxImage;
315 }
316
317
318 MustHaveApp(wxImage(const wxBitmap &bitmap));
319
320 %RenameDocCtor(
321 ImageFromBitmap,
322 "Construct an Image from a `wx.Bitmap`.", "
323
324:see: `wx.Image`",
325 wxImage(const wxBitmap &bitmap))
326 {
327 return new wxImage(bitmap.ConvertToImage());
328 }
329
330 %RenameDocCtor(
331 ImageFromData,
332 "Construct an Image from a buffer of RGB bytes. Accepts either a
333string or a buffer object holding the data and the length of the data
334must be width*height*3.", "
335
336:see: `wx.Image`",
337 wxImage(int width, int height, buffer data, int DATASIZE))
338 {
339 if (DATASIZE != width*height*3) {
340 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
341 return NULL;
342 }
343
344 // Copy the source data so the wxImage can clean it up later
345 buffer copy = (buffer)malloc(DATASIZE);
346 if (copy == NULL) {
347 wxPyBLOCK_THREADS(PyErr_NoMemory());
348 return NULL;
349 }
350 memcpy(copy, data, DATASIZE);
351 return new wxImage(width, height, copy, false);
352 }
353
354
355 %RenameDocCtor(
356 ImageFromDataWithAlpha,
357 "Construct an Image from a buffer of RGB bytes with an Alpha channel.
358Accepts either a string or a buffer object holding the data and the
359length of the data must be width*height*3 bytes, and the length of the
360alpha data must be width*height bytes.", "
361
362:see: `wx.Image`",
363 wxImage(int width, int height, buffer data, int DATASIZE, buffer alpha, int ALPHASIZE))
364 {
365 if (DATASIZE != width*height*3) {
366 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
367 return NULL;
368 }
369 if (ALPHASIZE != width*height) {
370 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
371 return NULL;
372 }
373
374 // Copy the source data so the wxImage can clean it up later
375 buffer dcopy = (buffer)malloc(DATASIZE);
376 if (dcopy == NULL) {
377 wxPyBLOCK_THREADS(PyErr_NoMemory());
378 return NULL;
379 }
380 memcpy(dcopy, data, DATASIZE);
381
382 buffer acopy = (buffer)malloc(ALPHASIZE);
383 if (acopy == NULL) {
384 wxPyBLOCK_THREADS(PyErr_NoMemory());
385 return NULL;
386 }
387 memcpy(acopy, alpha, ALPHASIZE);
388
389 return new wxImage(width, height, dcopy, acopy, false);
390 }
391 }
392
393 // TODO: wxImage( char** xpmData );
394
395 // Turn it back on again
396 %typemap(out) wxImage* { $result = wxPyMake_wxObject($1, $owner); }
397
398
399 DocDeclStr(
400 void , Create( int width, int height, bool clear=true ),
401 "Creates a fresh image. If clear is ``True``, the new image will be
402initialized to black. Otherwise, the image data will be uninitialized.", "");
403
404 DocDeclStr(
405 void , Destroy(),
406 "Destroys the image data.", "");
407
408
409 DocDeclStr(
410 wxImage , Scale( int width, int height ),
411 "Returns a scaled version of the image. This is also useful for scaling
412bitmaps in general as the only other way to scale bitmaps is to blit a
413`wx.MemoryDC` into another `wx.MemoryDC`.", "
414
415:see: `Rescale`");
416
417 DocDeclStr(
418 wxImage , ShrinkBy( int xFactor , int yFactor ) const ,
419 "Return a version of the image scaled smaller by the given factors.", "");
420
421 DocDeclStr(
422 wxImage& , Rescale(int width, int height),
423 "Changes the size of the image in-place by scaling it: after a call to
424this function, the image will have the given width and height.
425
426Returns the (modified) image itself.", "
427
428:see: `Scale`");
429
430
431 // resizes the image in place
432 DocDeclStr(
433 wxImage& , Resize( const wxSize& size, const wxPoint& pos,
434 int r = -1, int g = -1, int b = -1 ),
435 "Changes the size of the image in-place without scaling it, by adding
436either a border with the given colour or cropping as necessary. The
437image is pasted into a new image with the given size and background
438colour at the position pos relative to the upper left of the new
439image. If red = green = blue = -1 then use either the current mask
440colour if set or find, use, and set a suitable mask colour for any
441newly exposed areas.
442
443Returns the (modified) image itself.", "
444
445:see: `Size`");
446
447
448 DocDeclStr(
449 void , SetRGB( int x, int y, byte r, byte g, byte b ),
450 "Sets the pixel at the given coordinate. This routine performs
451bounds-checks for the coordinate so it can be considered a safe way to
452manipulate the data, but in some cases this might be too slow so that
453the data will have to be set directly. In that case you will have to
454get access to the image data using the `GetData` method.", "");
455
456
457 DocDeclStrName(
458 void, SetRGB( const wxRect& rect,
459 byte r, byte g, byte b ),
460 "Sets the colour of the pixels within the given rectangle. This routine
461performs bounds-checks for the rectangle so it can be considered a
462safe way to manipulate the data.", "",
463 SetRGBRect);
464
465 DocDeclStr(
466 byte , GetRed( int x, int y ),
467 "Returns the red intensity at the given coordinate.", "");
468
469 DocDeclStr(
470 byte , GetGreen( int x, int y ),
471 "Returns the green intensity at the given coordinate.", "");
472
473 DocDeclStr(
474 byte , GetBlue( int x, int y ),
475 "Returns the blue intensity at the given coordinate.", "");
476
477
478 DocDeclStr(
479 void , SetAlpha(int x, int y, byte alpha),
480 "Sets the alpha value for the given pixel. This function should only be
481called if the image has alpha channel data, use `HasAlpha` to check
482for this.", "");
483
484 DocDeclStr(
485 byte , GetAlpha(int x, int y),
486 "Returns the alpha value for the given pixel. This function may only be
487called for the images with alpha channel, use `HasAlpha` to check for
488this.
489
490The returned value is the *opacity* of the image, i.e. the value of 0
491corresponds to the fully transparent pixels while the value of 255 to
492the fully opaque pixels.", "");
493
494 DocDeclStr(
495 bool , HasAlpha(),
496 "Returns true if this image has alpha channel, false otherwise.", "
497
498:see: `GetAlpha`, `SetAlpha`");
499
500
501 DocDeclStr(
502 void , InitAlpha(),
503 "Initializes the image alpha channel data. It is an error to call it if
504the image already has alpha data. If it doesn't, alpha data will be by
505default initialized to all pixels being fully opaque. But if the image
506has a a mask colour, all mask pixels will be completely transparent.", "");
507
508
509 DocDeclStr(
510 bool , IsTransparent(int x, int y,
511 byte threshold = wxIMAGE_ALPHA_THRESHOLD) const,
512 "Returns ``True`` if this pixel is masked or has an alpha value less
513than the spcified threshold.", "");
514
515
516 // find first colour that is not used in the image and has higher
517 // RGB values than <startR,startG,startB>
518 DocDeclAStr(
519 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
520 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
521 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
522 "Find first colour that is not used in the image and has higher RGB
523values than startR, startG, startB. Returns a tuple consisting of a
524success flag and rgb values.", "");
525
526
527 DocDeclStr(
528 bool , ConvertAlphaToMask(byte threshold = wxIMAGE_ALPHA_THRESHOLD),
529 "If the image has alpha channel, this method converts it to mask. All
530pixels with alpha value less than ``threshold`` are replaced with the
531mask colour and the alpha channel is removed. The mask colour is
532chosen automatically using `FindFirstUnusedColour`.
533
534If the image image doesn't have alpha channel, ConvertAlphaToMask does
535nothing.", "");
536
537
538 DocDeclStr(
539 bool , ConvertColourToAlpha( byte r, byte g, byte b ),
540 "This method converts an image where the original alpha information is
541only available as a shades of a colour (actually shades of grey)
542typically when you draw anti-aliased text into a bitmap. The DC
543drawing routines draw grey values on the black background although
544they actually mean to draw white with differnt alpha values. This
545method reverses it, assuming a black (!) background and white text.
546The method will then fill up the whole image with the colour given.", "");
547
548
549
550 DocDeclStr(
551 bool , SetMaskFromImage(const wxImage & mask,
552 byte mr, byte mg, byte mb),
553 "Sets the image's mask so that the pixels that have RGB value of
554``(mr,mg,mb)`` in ``mask`` will be masked in this image. This is done
555by first finding an unused colour in the image, setting this colour as
556the mask colour and then using this colour to draw all pixels in the
557image who corresponding pixel in mask has given RGB value.
558
559Returns ``False`` if ``mask`` does not have same dimensions as the
560image or if there is no unused colour left. Returns ``True`` if the
561mask was successfully applied.
562
563Note that this method involves computing the histogram, which is
564computationally intensive operation.", "");
565
566
567// void DoFloodFill (wxCoord x, wxCoord y,
568// const wxBrush & fillBrush,
569// const wxColour& testColour,
570// int style = wxFLOOD_SURFACE,
571// int LogicalFunction = wxCOPY /* currently unused */ ) ;
572
573 DocDeclStr(
574 static bool , CanRead( const wxString& filename ),
575 "Returns True if the image handlers can read this file.", "");
576
577 DocDeclStr(
578 static int , GetImageCount( const wxString& filename, long type = wxBITMAP_TYPE_ANY ),
579 "If the image file contains more than one image and the image handler
580is capable of retrieving these individually, this function will return
581the number of available images.", "");
582
583
584 DocDeclStr(
585 bool , LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
586 "Loads an image from a file. If no handler type is provided, the
587library will try to autodetect the format.", "");
588
589 DocDeclStrName(
590 bool , LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ),
591 "Loads an image from a file, specifying the image type with a MIME type
592string.", "",
593 LoadMimeFile);
594
595
596 DocDeclStr(
597 bool , SaveFile( const wxString& name, int type ),
598 "Saves an image in the named file.", "");
599
600
601 DocDeclStrName(
602 bool , SaveFile( const wxString& name, const wxString& mimetype ),
603 "Saves an image in the named file.", "",
604 SaveMimeFile);
605
606
607 DocDeclStrName(
608 static bool , CanRead( wxInputStream& stream ),
609 "Returns True if the image handlers can read an image file from the
610data currently on the input stream, or a readable Python file-like
611object.", "",
612 CanReadStream);
613
614
615 DocDeclStrName(
616 bool , LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ),
617 "Loads an image from an input stream or a readable Python file-like
618object. If no handler type is provided, the library will try to
619autodetect the format.", "",
620 LoadStream);
621
622
623 DocDeclStrName(
624 bool , LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ),
625 "Loads an image from an input stream or a readable Python file-like
626object, using a MIME type string to specify the image file format.", "",
627 LoadMimeStream);
628
629
630 DocDeclStr(
631 bool , Ok(),
632 "Returns true if image data is present.", "");
633
634 DocDeclStr(
635 int , GetWidth(),
636 "Gets the width of the image in pixels.", "");
637
638 DocDeclStr(
639 int , GetHeight(),
640 "Gets the height of the image in pixels.", "");
641
642
643 %extend {
644 DocStr(GetSize,
645 "Returns the size of the image in pixels.", "");
646 wxSize GetSize() {
647 wxSize size(self->GetWidth(), self->GetHeight());
648 return size;
649 }
650 }
651
652
653 DocDeclStr(
654 wxImage , GetSubImage(const wxRect& rect),
655 "Returns a sub image of the current one as long as the rect belongs
656entirely to the image.", "");
657
658
659 DocDeclStr(
660 wxImage , Size( const wxSize& size, const wxPoint& pos,
661 int r = -1, int g = -1, int b = -1 ) const,
662 "Returns a resized version of this image without scaling it by adding
663either a border with the given colour or cropping as necessary. The
664image is pasted into a new image with the given size and background
665colour at the position ``pos`` relative to the upper left of the new
666image. If red = green = blue = -1 then use either the current mask
667colour if set or find, use, and set a suitable mask colour for any
668newly exposed areas.", "
669
670:see: `Resize`");
671
672
673 DocDeclStr(
674 wxImage , Copy(),
675 "Returns an identical copy of the image.", "");
676
677 DocDeclStr(
678 void , Paste( const wxImage &image, int x, int y ),
679 "Pastes ``image`` into this instance and takes care of the mask colour
680and any out of bounds problems.", "");
681
682
683 //unsigned char *GetData();
684 //void SetData( unsigned char *data );
685
686 %extend {
687 DocStr(GetData,
688 "Returns a string containing a copy of the RGB bytes of the image.", "");
689 PyObject* GetData()
690 {
691 buffer data = self->GetData();
692 int len = self->GetWidth() * self->GetHeight() * 3;
693 PyObject* rv;
694 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
695 return rv;
696 }
697 DocStr(SetData,
698 "Resets the Image's RGB data from a buffer of RGB bytes. Accepts
699either a string or a buffer object holding the data and the length of
700the data must be width*height*3.", "");
701 void SetData(buffer data, int DATASIZE)
702 {
703 if (DATASIZE != self->GetWidth() * self->GetHeight() * 3) {
704 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
705 return;
706 }
707 buffer copy = (buffer)malloc(DATASIZE);
708 if (copy == NULL) {
709 wxPyBLOCK_THREADS(PyErr_NoMemory());
710 return;
711 }
712 memcpy(copy, data, DATASIZE);
713 self->SetData(copy, false);
714 // wxImage takes ownership of copy...
715 }
716
717
718 DocStr(GetDataBuffer,
719 "Returns a writable Python buffer object that is pointing at the RGB
720image data buffer inside the wx.Image. You need to ensure that you do
721not use this buffer object after the image has been destroyed.", "");
722 PyObject* GetDataBuffer()
723 {
724 buffer data = self->GetData();
725 int len = self->GetWidth() * self->GetHeight() * 3;
726 PyObject* rv;
727 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
728 return rv;
729 }
730
731 DocStr(SetDataBuffer,
732 "Sets the internal image data pointer to point at a Python buffer
733object. This can save making an extra copy of the data but you must
734ensure that the buffer object lives longer than the wx.Image does.", "");
735 void SetDataBuffer(buffer data, int DATASIZE)
736 {
737 if (DATASIZE != self->GetWidth() * self->GetHeight() * 3) {
738 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
739 return;
740 }
741 self->SetData(data, true);
742 }
743
744
745
746 DocStr(GetAlphaData,
747 "Returns a string containing a copy of the alpha bytes of the image.", "");
748 PyObject* GetAlphaData() {
749 buffer data = self->GetAlpha();
750 if (! data) {
751 RETURN_NONE();
752 } else {
753 int len = self->GetWidth() * self->GetHeight();
754 PyObject* rv;
755 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
756 return rv;
757 }
758 }
759
760 DocStr(SetAlphaData,
761 "Resets the Image's alpha data from a buffer of bytes. Accepts either
762a string or a buffer object holding the data and the length of the
763data must be width*height.", "");
764 void SetAlphaData(buffer alpha, int ALPHASIZE)
765 {
766 if (ALPHASIZE != self->GetWidth() * self->GetHeight()) {
767 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
768 return;
769 }
770 buffer acopy = (buffer)malloc(ALPHASIZE);
771 if (acopy == NULL) {
772 wxPyBLOCK_THREADS(PyErr_NoMemory());
773 return;
774 }
775 memcpy(acopy, alpha, ALPHASIZE);
776 self->SetAlpha(acopy, false);
777 // wxImage takes ownership of acopy...
778 }
779
780
781
782 DocStr(GetDataBuffer,
783 "Returns a writable Python buffer object that is pointing at the Alpha
784data buffer inside the wx.Image. You need to ensure that you do not
785use this buffer object after the image has been destroyed.", "");
786 PyObject* GetAlphaBuffer()
787 {
788 buffer data = self->GetAlpha();
789 int len = self->GetWidth() * self->GetHeight();
790 PyObject* rv;
791 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
792 return rv;
793 }
794
795
796 DocStr(SetDataBuffer,
797 "Sets the internal image alpha pointer to point at a Python buffer
798object. This can save making an extra copy of the data but you must
799ensure that the buffer object lives as long as the wx.Image does.", "");
800 void SetAlphaBuffer(buffer alpha, int ALPHASIZE)
801 {
802 if (ALPHASIZE != self->GetWidth() * self->GetHeight()) {
803 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
804 return;
805 }
806 self->SetAlpha(alpha, true);
807 }
808 }
809
810
811
812 DocDeclStr(
813 void , SetMaskColour( byte r, byte g, byte b ),
814 "Sets the mask colour for this image (and tells the image to use the
815mask).", "");
816
817
818 DocDeclAStr(
819 /*bool*/ void , GetOrFindMaskColour( byte *OUTPUT,
820 byte *OUTPUT,
821 byte *OUTPUT ) const,
822 "GetOrFindMaskColour() -> (r,g,b)",
823 "Get the current mask colour or find a suitable colour.", "");
824
825
826 DocDeclStr(
827 byte , GetMaskRed(),
828 "Gets the red component of the mask colour.", "");
829
830 DocDeclStr(
831 byte , GetMaskGreen(),
832 "Gets the green component of the mask colour.", "");
833
834 DocDeclStr(
835 byte , GetMaskBlue(),
836 "Gets the blue component of the mask colour.", "");
837
838 DocDeclStr(
839 void , SetMask( bool mask = true ),
840 "Specifies whether there is a mask or not. The area of the mask is
841determined by the current mask colour.", "");
842
843 DocDeclStr(
844 bool , HasMask(),
845 "Returns ``True`` if there is a mask active, ``False`` otherwise.", "");
846
847
848 DocDeclStr(
849 wxImage , Rotate(double angle, const wxPoint & centre_of_rotation,
850 bool interpolating = true, wxPoint * offset_after_rotation = NULL) const ,
851 "Rotates the image about the given point, by ``angle`` radians. Passing
852``True`` to ``interpolating`` results in better image quality, but is
853slower. If the image has a mask, then the mask colour is used for the
854uncovered pixels in the rotated image background. Otherwise, black
855will be used as the fill colour.
856
857Returns the rotated image, leaving this image intact.", "");
858
859 DocDeclStr(
860 wxImage , Rotate90( bool clockwise = true ) ,
861 "Returns a copy of the image rotated 90 degrees in the direction
862indicated by ``clockwise``.", "");
863
864 DocDeclStr(
865 wxImage , Mirror( bool horizontally = true ) ,
866 "Returns a mirrored copy of the image. The parameter ``horizontally``
867indicates the orientation.", "");
868
869
870 DocDeclStr(
871 void , Replace( byte r1, byte g1, byte b1,
872 byte r2, byte g2, byte b2 ),
873 "Replaces the colour specified by ``(r1,g1,b1)`` by the colour
874``(r2,g2,b2)``.", "");
875
876 DocDeclStr(
877 wxImage , ConvertToGreyscale( double lr = 0.299,
878 double lg = 0.587,
879 double lb = 0.114 ) const,
880 "Convert to greyscale image. Uses the luminance component (Y) of the
881image. The luma value (YUV) is calculated using (R * lr) + (G * lg) + (B * lb),
882defaults to ITU-T BT.601", "");
883
884
885 DocDeclStr(
886 wxImage , ConvertToMono( byte r, byte g, byte b ) const,
887 "Returns monochromatic version of the image. The returned image has
888white colour where the original has ``(r,g,b)`` colour and black
889colour everywhere else.", "");
890
891
892 DocDeclStr(
893 void , SetOption(const wxString& name, const wxString& value),
894 "Sets an image handler defined option. For example, when saving as a
895JPEG file, the option ``wx.IMAGE_OPTION_QUALITY`` is used, which is a
896number between 0 and 100 (0 is terrible, 100 is very good).", "
897
898 ================================= ===
899 wx.IMAGE_OPTION_BMP_FORMAT
900 wx.IMAGE_OPTION_CUR_HOTSPOT_X
901 wx.IMAGE_OPTION_CUR_HOTSPOT_Y
902 wx.IMAGE_OPTION_RESOLUTION
903 wx.IMAGE_OPTION_RESOLUTIONX
904 wx.IMAGE_OPTION_RESOLUTIONY
905 wx.IMAGE_OPTION_RESOLUTIONUNIT
906 wx.IMAGE_OPTION_QUALITY
907 wx.IMAGE_OPTION_BITSPERSAMPLE
908 wx.IMAGE_OPTION_SAMPLESPERPIXEL
909 wx.IMAGE_OPTION_COMPRESSION
910 wx.IMAGE_OPTION_IMAGEDESCRIPTOR
911 wx.IMAGE_OPTION_PNG_FORMAT
912 wx.IMAGE_OPTION_PNG_BITDEPTH
913 ================================= ===
914
915:see: `HasOption`, `GetOption`, `GetOptionInt`, `SetOptionInt`");
916
917 DocDeclStrName(
918 void, SetOption(const wxString& name, int value),
919 "Sets an image option as an integer.", "
920
921:see: `HasOption`, `GetOption`, `GetOptionInt`, `SetOption`",
922 SetOptionInt);
923
924 DocDeclStr(
925 wxString , GetOption(const wxString& name) const,
926 "Gets the value of an image handler option.", "
927
928:see: `HasOption`, `GetOptionInt`, `SetOption`, `SetOptionInt`");
929
930 DocDeclStr(
931 int , GetOptionInt(const wxString& name) const,
932 "Gets the value of an image handler option as an integer. If the given
933option is not present, the function returns 0.", "
934
935:see: `HasOption`, `GetOption`, `SetOptionInt`, `SetOption`");
936
937 DocDeclStr(
938 bool , HasOption(const wxString& name) const,
939 "Returns true if the given option is present.", "
940
941:see: `GetOption`, `GetOptionInt`, `SetOption`, `SetOptionInt`");
942
943
944 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
945 unsigned long ComputeHistogram( wxImageHistogram& h );
946
947 static void AddHandler( wxImageHandler *handler );
948 static void InsertHandler( wxImageHandler *handler );
949 static bool RemoveHandler( const wxString& name );
950
951 DocDeclStr(
952 static wxString , GetImageExtWildcard(),
953 "Iterates all registered wxImageHandler objects, and returns a string
954containing file extension masks suitable for passing to file open/save
955dialog boxes.", "");
956
957
958
959MustHaveApp(ConvertToBitmap);
960MustHaveApp(ConvertToMonoBitmap);
961
962 %extend {
963 wxBitmap ConvertToBitmap(int depth=-1) {
964 wxBitmap bitmap(*self, depth);
965 return bitmap;
966 }
967
968 wxBitmap ConvertToMonoBitmap( byte red,
969 byte green,
970 byte blue ) {
971 wxImage mono = self->ConvertToMono( red, green, blue );
972 wxBitmap bitmap( mono, 1 );
973 return bitmap;
974 }
975 }
976
977
978 DocDeclStr(
979 void , RotateHue(double angle),
980 "Rotates the hue of each pixel of the image. Hue is a double in the
981range -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees", "");
982
983 DocDeclStr(
984 static wxImage_HSVValue , RGBtoHSV(wxImage_RGBValue rgb),
985 "Converts a color in RGB color space to HSV color space.", "");
986
987 DocDeclStr(
988 static wxImage_RGBValue , HSVtoRGB(wxImage_HSVValue hsv),
989 "Converts a color in HSV color space to RGB color space.", "");
990
991
992 %pythoncode { def __nonzero__(self): return self.Ok() }
993};
994
995
996
997///void wxInitAllImageHandlers();
998
999%pythoncode {
1000 def InitAllImageHandlers():
1001 """
1002 The former functionality of InitAllImageHanders is now done internal to
1003 the _core_ extension module and so this function has become a simple NOP.
1004 """
1005 pass
1006}
1007
1008
1009
1010// See also wxPy_ReinitStockObjects in helpers.cpp
1011%immutable;
1012const wxImage wxNullImage;
1013%mutable;
1014
1015//---------------------------------------------------------------------------
1016
1017MAKE_CONST_WXSTRING(IMAGE_OPTION_FILENAME);
1018MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
1019MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
1020MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
1021MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
1022MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONX);
1023MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONY);
1024MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
1025MAKE_CONST_WXSTRING(IMAGE_OPTION_QUALITY);
1026
1027enum
1028{
1029 wxIMAGE_RESOLUTION_INCHES = 1,
1030 wxIMAGE_RESOLUTION_CM = 2
1031};
1032
1033
1034MAKE_CONST_WXSTRING(IMAGE_OPTION_BITSPERSAMPLE);
1035MAKE_CONST_WXSTRING(IMAGE_OPTION_SAMPLESPERPIXEL);
1036MAKE_CONST_WXSTRING(IMAGE_OPTION_COMPRESSION);
1037MAKE_CONST_WXSTRING(IMAGE_OPTION_IMAGEDESCRIPTOR);
1038
1039MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_FORMAT);
1040MAKE_CONST_WXSTRING(IMAGE_OPTION_PNG_BITDEPTH);
1041
1042enum
1043{
1044 wxPNG_TYPE_COLOUR = 0,
1045 wxPNG_TYPE_GREY = 2,
1046 wxPNG_TYPE_GREY_RED = 3
1047};
1048
1049enum
1050{
1051 wxBMP_24BPP = 24, // default, do not need to set
1052 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
1053 wxBMP_8BPP = 8, // 8bpp, quantized colors
1054 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
1055 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
1056 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
1057 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
1058 wxBMP_4BPP = 4, // 4bpp, quantized colors
1059 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
1060 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
1061};
1062
1063
1064DocStr(wxBMPHandler,
1065"A `wx.ImageHandler` for \*.bmp bitmap files.", "");
1066class wxBMPHandler : public wxImageHandler {
1067public:
1068 wxBMPHandler();
1069};
1070
1071DocStr(wxICOHandler,
1072"A `wx.ImageHandler` for \*.ico icon files.", "");
1073class wxICOHandler : public wxBMPHandler {
1074public:
1075 wxICOHandler();
1076};
1077
1078DocStr(wxCURHandler,
1079"A `wx.ImageHandler` for \*.cur cursor files.", "");
1080class wxCURHandler : public wxICOHandler {
1081public:
1082 wxCURHandler();
1083};
1084
1085DocStr(wxANIHandler,
1086"A `wx.ImageHandler` for \*.ani animated cursor files.", "");
1087class wxANIHandler : public wxCURHandler {
1088public:
1089 wxANIHandler();
1090};
1091
1092
1093//---------------------------------------------------------------------------
1094
1095DocStr(wxPNGHandler,
1096"A `wx.ImageHandler` for PNG image files.", "");
1097class wxPNGHandler : public wxImageHandler {
1098public:
1099 wxPNGHandler();
1100};
1101
1102
1103DocStr(wxGIFHandler,
1104"A `wx.ImageHandler` for GIF image files.", "");
1105class wxGIFHandler : public wxImageHandler {
1106public:
1107 wxGIFHandler();
1108};
1109
1110
1111DocStr(wxPCXHandler,
1112"A `wx.ImageHandler` for PCX imager files.", "");
1113class wxPCXHandler : public wxImageHandler {
1114public:
1115 wxPCXHandler();
1116};
1117
1118
1119DocStr(wxJPEGHandler,
1120"A `wx.ImageHandler` for JPEG/JPG image files.", "");
1121class wxJPEGHandler : public wxImageHandler {
1122public:
1123 wxJPEGHandler();
1124};
1125
1126
1127DocStr(wxPNMHandler,
1128"A `wx.ImageHandler` for PNM image files.", "");
1129class wxPNMHandler : public wxImageHandler {
1130public:
1131 wxPNMHandler();
1132};
1133
1134DocStr(wxXPMHandler,
1135"A `wx.ImageHandler` for XPM image.", "");
1136class wxXPMHandler : public wxImageHandler {
1137public:
1138 wxXPMHandler();
1139};
1140
1141DocStr(wxTIFFHandler,
1142"A `wx.ImageHandler` for TIFF image files.", "");
1143class wxTIFFHandler : public wxImageHandler {
1144public:
1145 wxTIFFHandler();
1146};
1147
1148
1149#if wxUSE_IFF
1150DocStr(wxIFFHandler,
1151"A `wx.ImageHandler` for IFF image files.", "");
1152class wxIFFHandler : public wxImageHandler {
1153public:
1154 wxIFFHandler();
1155};
1156#endif
1157
1158//---------------------------------------------------------------------------
1159
1160%{
1161#include <wx/quantize.h>
1162%}
1163
1164enum {
1165 wxQUANTIZE_INCLUDE_WINDOWS_COLOURS,
1166// wxQUANTIZE_RETURN_8BIT_DATA,
1167 wxQUANTIZE_FILL_DESTINATION_IMAGE
1168};
1169
1170
1171DocStr(wxQuantize,
1172 "Performs quantization, or colour reduction, on a wxImage.", "");
1173
1174class wxQuantize /*: public wxObject */
1175{
1176public:
1177
1178 %extend {
1179 DocStr(
1180 Quantize,
1181 "Reduce the colours in the source image and put the result into the
1182destination image, setting the palette in the destination if
1183needed. Both images may be the same, to overwrite the source image.", "
1184:todo: Create a version that returns the wx.Palette used.");
1185
1186 static bool Quantize(const wxImage& src, wxImage& dest, int desiredNoColours = 236,
1187 int flags = wxQUANTIZE_INCLUDE_WINDOWS_COLOURS|wxQUANTIZE_FILL_DESTINATION_IMAGE)
1188 {
1189 return wxQuantize::Quantize(src, dest,
1190 //NULL, // palette
1191 desiredNoColours,
1192 NULL, // eightBitData
1193 flags);
1194 }
1195 }
1196};
1197
1198
1199//---------------------------------------------------------------------------