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