]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: _bitmap.i | |
3 | // Purpose: SWIG interface for wxBitmap and wxMask | |
4 | // | |
5 | // Author: Robin Dunn | |
6 | // | |
7 | // Created: 7-July-1997 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 2003 by Total Control Software | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | // Not a %module | |
14 | ||
238ba802 RD |
15 | %{ |
16 | #include <wx/rawbmp.h> | |
17 | %} | |
d14a1e28 | 18 | |
777dffec | 19 | |
0f82eb02 RD |
20 | // Turn off the aquisition of the Global Interpreter Lock for the classes and |
21 | // functions in this file | |
777dffec RD |
22 | %threadWrapperOff |
23 | ||
d14a1e28 RD |
24 | //--------------------------------------------------------------------------- |
25 | ||
26 | %{ | |
27 | #include <wx/image.h> | |
28 | ||
29 | static char** ConvertListOfStrings(PyObject* listOfStrings) { | |
30 | char** cArray = NULL; | |
31 | int count; | |
32 | ||
33 | if (!PyList_Check(listOfStrings)) { | |
34 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
35 | return NULL; | |
36 | } | |
37 | count = PyList_Size(listOfStrings); | |
38 | cArray = new char*[count]; | |
39 | ||
40 | for(int x=0; x<count; x++) { | |
41 | // TODO: Need some validation and error checking here | |
42 | cArray[x] = PyString_AsString(PyList_GET_ITEM(listOfStrings, x)); | |
43 | } | |
44 | return cArray; | |
45 | } | |
46 | ||
47 | %} | |
48 | ||
49 | //--------------------------------------------------------------------------- | |
50 | ||
51 | // TODO: When the API stabalizes and is available on other platforms, add | |
52 | // wrappers for the new wxBitmap, wxRawBitmap, wxDIB stuff... | |
53 | ||
dce2bd22 RD |
54 | DocStr(wxBitmap, |
55 | "The wx.Bitmap class encapsulates the concept of a platform-dependent | |
56 | bitmap. It can be either monochrome or colour, and either loaded from | |
57 | a file or created dynamically. A bitmap can be selected into a memory | |
58 | device context (instance of `wx.MemoryDC`). This enables the bitmap to | |
59 | be copied to a window or memory device context using `wx.DC.Blit`, or | |
d07d2bc9 | 60 | to be used as a drawing surface.", " |
dce2bd22 RD |
61 | |
62 | The BMP and XMP image file formats are supported on all platforms by | |
63 | wx.Bitmap. Other formats are automatically loaded by `wx.Image` and | |
64 | converted to a wx.Bitmap, so any image file format supported by | |
65 | `wx.Image` can be used. | |
66 | ||
67 | :todo: Add wrappers and support for raw bitmap data access. Can this | |
68 | be be put into Python without losing the speed benefits of the | |
69 | teplates and iterators in rawbmp.h? | |
70 | ||
71 | :todo: Find a way to do very efficient PIL Image <--> wx.Bitmap | |
72 | converstions. | |
238ba802 RD |
73 | |
74 | :see: `wx.EmptyBitmap`, `wx.BitmapFromIcon`, `wx.BitmapFromImage`, | |
75 | `wx.BitmapFromXPMData`, `wx.BitmapFromBits`, `wx.BitmapFromBuffer`, | |
76 | `wx.BitmapFromBufferRGBA`, `wx.Image` | |
dce2bd22 RD |
77 | "); |
78 | ||
d14a1e28 | 79 | |
ab1f7d2a RD |
80 | MustHaveApp(wxBitmap); |
81 | ||
d14a1e28 RD |
82 | class wxBitmap : public wxGDIObject |
83 | { | |
84 | public: | |
1e0c8722 RD |
85 | DocCtorStr( |
86 | wxBitmap(const wxString& name, wxBitmapType type=wxBITMAP_TYPE_ANY), | |
d07d2bc9 RD |
87 | "Loads a bitmap from a file.", |
88 | " | |
89 | :param name: Name of the file to load the bitmap from. | |
90 | :param type: The type of image to expect. Can be one of the following | |
91 | constants (assuming that the neccessary `wx.Image` handlers are | |
92 | loaded): | |
dce2bd22 RD |
93 | |
94 | * wx.BITMAP_TYPE_ANY | |
95 | * wx.BITMAP_TYPE_BMP | |
96 | * wx.BITMAP_TYPE_ICO | |
97 | * wx.BITMAP_TYPE_CUR | |
98 | * wx.BITMAP_TYPE_XBM | |
99 | * wx.BITMAP_TYPE_XPM | |
100 | * wx.BITMAP_TYPE_TIF | |
101 | * wx.BITMAP_TYPE_GIF | |
102 | * wx.BITMAP_TYPE_PNG | |
103 | * wx.BITMAP_TYPE_JPEG | |
104 | * wx.BITMAP_TYPE_PNM | |
105 | * wx.BITMAP_TYPE_PCX | |
106 | * wx.BITMAP_TYPE_PICT | |
107 | * wx.BITMAP_TYPE_ICON | |
108 | * wx.BITMAP_TYPE_ANI | |
109 | * wx.BITMAP_TYPE_IFF | |
110 | ||
111 | :see: Alternate constructors `wx.EmptyBitmap`, `wx.BitmapFromIcon`, | |
8423bd78 RD |
112 | `wx.BitmapFromImage`, `wx.BitmapFromXPMData`, `wx.BitmapFromBits`, |
113 | `wx.BitmapFromBuffer`, `wx.BitmapFromBufferRGBA`, | |
dce2bd22 | 114 | "); |
1e0c8722 | 115 | |
d14a1e28 RD |
116 | ~wxBitmap(); |
117 | ||
dce2bd22 RD |
118 | DocCtorStrName( |
119 | wxBitmap(int width, int height, int depth=-1), | |
120 | "Creates a new bitmap of the given size. A depth of -1 indicates the | |
121 | depth of the current screen or visual. Some platforms only support 1 | |
9c383901 | 122 | for monochrome and -1 for the current display depth.", "", |
dce2bd22 | 123 | EmptyBitmap); |
1e0c8722 RD |
124 | |
125 | DocCtorStrName( | |
126 | wxBitmap(const wxIcon& icon), | |
d07d2bc9 | 127 | "Create a new bitmap from a `wx.Icon` object.", "", |
1e0c8722 RD |
128 | BitmapFromIcon); |
129 | ||
130 | DocCtorStrName( | |
131 | wxBitmap(const wxImage& image, int depth=-1), | |
dce2bd22 RD |
132 | "Creates bitmap object from a `wx.Image`. This has to be done to |
133 | actually display a `wx.Image` as you cannot draw an image directly on | |
134 | a window. The resulting bitmap will use the provided colour depth (or | |
135 | that of the current screen colour depth if depth is -1) which entails | |
d07d2bc9 | 136 | that a colour reduction may have to take place.", "", |
1e0c8722 RD |
137 | BitmapFromImage); |
138 | ||
139 | ||
d14a1e28 | 140 | %extend { |
238ba802 RD |
141 | %RenameDocCtor( |
142 | BitmapFromXPMData, | |
143 | "Construct a Bitmap from a list of strings formatted as XPM data.", "", | |
144 | wxBitmap(PyObject* listOfStrings)) | |
145 | { | |
146 | char** cArray = NULL; | |
147 | wxBitmap* bmp; | |
148 | ||
149 | cArray = ConvertListOfStrings(listOfStrings); | |
150 | if (! cArray) | |
151 | return NULL; | |
152 | bmp = new wxBitmap(cArray); | |
153 | delete [] cArray; | |
154 | return bmp; | |
155 | } | |
156 | ||
157 | ||
158 | %RenameDocCtor( | |
159 | BitmapFromBits, | |
160 | "Creates a bitmap from an array of bits. You should only use this | |
dce2bd22 RD |
161 | function for monochrome bitmaps (depth 1) in portable programs: in |
162 | this case the bits parameter should contain an XBM image. For other | |
238ba802 RD |
163 | bit depths, the behaviour is platform dependent.", "", |
164 | wxBitmap(PyObject* bits, int width, int height, int depth=1 )) | |
165 | { | |
166 | char* buf; | |
167 | Py_ssize_t length; | |
168 | PyString_AsStringAndSize(bits, &buf, &length); | |
169 | return new wxBitmap(buf, width, height, depth); | |
170 | } | |
d14a1e28 RD |
171 | } |
172 | ||
173 | ||
d14a1e28 RD |
174 | // wxGDIImage methods |
175 | #ifdef __WXMSW__ | |
176 | long GetHandle(); | |
a0c956e8 RD |
177 | %extend { |
178 | void SetHandle(long handle) { self->SetHandle((WXHANDLE)handle); } | |
179 | } | |
d14a1e28 RD |
180 | #endif |
181 | ||
6c2dd16f RD |
182 | bool IsOk(); |
183 | %pythoncode { Ok = IsOk } | |
184 | ||
dce2bd22 RD |
185 | DocDeclStr( |
186 | int , GetWidth(), | |
d07d2bc9 | 187 | "Gets the width of the bitmap in pixels.", ""); |
dce2bd22 | 188 | |
1e0c8722 | 189 | |
dce2bd22 RD |
190 | DocDeclStr( |
191 | int , GetHeight(), | |
d07d2bc9 | 192 | "Gets the height of the bitmap in pixels.", ""); |
dce2bd22 | 193 | |
1e0c8722 | 194 | |
dce2bd22 RD |
195 | DocDeclStr( |
196 | int , GetDepth(), | |
197 | "Gets the colour depth of the bitmap. A value of 1 indicates a | |
d07d2bc9 | 198 | monochrome bitmap.", ""); |
dce2bd22 | 199 | |
d14a1e28 | 200 | |
ba938c3d RD |
201 | |
202 | %extend { | |
d07d2bc9 | 203 | DocStr(GetSize, "Get the size of the bitmap.", ""); |
ba938c3d RD |
204 | wxSize GetSize() { |
205 | wxSize size(self->GetWidth(), self->GetHeight()); | |
206 | return size; | |
207 | } | |
208 | } | |
209 | ||
210 | ||
dce2bd22 RD |
211 | DocDeclStr( |
212 | virtual wxImage , ConvertToImage() const, | |
213 | "Creates a platform-independent image from a platform-dependent | |
214 | bitmap. This preserves mask information so that bitmaps and images can | |
d07d2bc9 | 215 | be converted back and forth without loss in that respect.", ""); |
dce2bd22 RD |
216 | |
217 | ||
218 | DocDeclStr( | |
219 | virtual wxMask* , GetMask() const, | |
220 | "Gets the associated mask (if any) which may have been loaded from a | |
221 | file or explpicitly set for the bitmap. | |
222 | ||
223 | :see: `SetMask`, `wx.Mask` | |
d07d2bc9 | 224 | ", ""); |
c1acc129 RD |
225 | |
226 | // MSW only? wxBitmap GetMaskBitmap() const; | |
dce2bd22 | 227 | |
c5633576 | 228 | %disownarg(wxMask*); |
dce2bd22 RD |
229 | DocDeclStr( |
230 | virtual void , SetMask(wxMask* mask), | |
231 | "Sets the mask for this bitmap. | |
232 | ||
233 | :see: `GetMask`, `wx.Mask` | |
d07d2bc9 | 234 | ", ""); |
c5633576 | 235 | %cleardisown(wxMask*); |
1e0c8722 | 236 | |
d14a1e28 | 237 | %extend { |
1e0c8722 | 238 | DocStr(SetMaskColour, |
d07d2bc9 | 239 | "Create a Mask based on a specified colour in the Bitmap.", ""); |
d14a1e28 RD |
240 | void SetMaskColour(const wxColour& colour) { |
241 | wxMask *mask = new wxMask(*self, colour); | |
242 | self->SetMask(mask); | |
243 | } | |
244 | } | |
d14a1e28 | 245 | |
d14a1e28 | 246 | |
dce2bd22 RD |
247 | DocDeclStr( |
248 | virtual wxBitmap , GetSubBitmap(const wxRect& rect) const, | |
249 | "Returns a sub-bitmap of the current one as long as the rect belongs | |
250 | entirely to the bitmap. This function preserves bit depth and mask | |
d07d2bc9 | 251 | information.", ""); |
dce2bd22 RD |
252 | |
253 | ||
254 | DocDeclStr( | |
255 | virtual bool , SaveFile(const wxString &name, wxBitmapType type, | |
256 | wxPalette *palette = NULL), | |
257 | "Saves a bitmap in the named file. See `__init__` for a description of | |
d07d2bc9 | 258 | the ``type`` parameter.", ""); |
dce2bd22 | 259 | |
1e0c8722 | 260 | |
dce2bd22 RD |
261 | DocDeclStr( |
262 | virtual bool , LoadFile(const wxString &name, wxBitmapType type), | |
263 | "Loads a bitmap from a file. See `__init__` for a description of the | |
d07d2bc9 | 264 | ``type`` parameter.", ""); |
dce2bd22 | 265 | |
d14a1e28 RD |
266 | |
267 | ||
d14a1e28 | 268 | virtual wxPalette *GetPalette() const; |
7aada1e0 | 269 | #ifdef __WXMSW__ |
d14a1e28 RD |
270 | virtual void SetPalette(const wxPalette& palette); |
271 | #endif | |
272 | ||
d14a1e28 | 273 | |
1e0c8722 RD |
274 | virtual bool CopyFromIcon(const wxIcon& icon); |
275 | ||
dce2bd22 RD |
276 | DocDeclStr( |
277 | virtual void , SetHeight(int height), | |
d07d2bc9 | 278 | "Set the height property (does not affect the existing bitmap data).", ""); |
dce2bd22 RD |
279 | |
280 | ||
281 | DocDeclStr( | |
282 | virtual void , SetWidth(int width), | |
d07d2bc9 | 283 | "Set the width property (does not affect the existing bitmap data).", ""); |
1e0c8722 | 284 | |
1e0c8722 | 285 | |
dce2bd22 RD |
286 | DocDeclStr( |
287 | virtual void , SetDepth(int depth), | |
d07d2bc9 | 288 | "Set the depth property (does not affect the existing bitmap data).", ""); |
dce2bd22 | 289 | |
d14a1e28 | 290 | |
ba938c3d | 291 | %extend { |
d07d2bc9 | 292 | DocStr(SetSize, "Set the bitmap size (does not affect the existing bitmap data).", ""); |
ba938c3d RD |
293 | void SetSize(const wxSize& size) { |
294 | self->SetWidth(size.x); | |
295 | self->SetHeight(size.y); | |
296 | } | |
297 | } | |
1e0c8722 | 298 | |
d14a1e28 RD |
299 | #ifdef __WXMSW__ |
300 | bool CopyFromCursor(const wxCursor& cursor); | |
02b800ce | 301 | |
2f91e3df KO |
302 | // WXWIN_COMPATIBILITY_2_4 |
303 | #if 0 | |
d14a1e28 RD |
304 | int GetQuality(); |
305 | void SetQuality(int q); | |
02b800ce RD |
306 | %pythoncode { GetQuality = wx._deprecated(GetQuality) } |
307 | %pythoncode { SetQuality = wx._deprecated(SetQuality) } | |
2f91e3df | 308 | #endif |
d14a1e28 RD |
309 | #endif |
310 | ||
6c2dd16f | 311 | %pythoncode { def __nonzero__(self): return self.IsOk() } |
b403cd65 RD |
312 | |
313 | %extend { | |
a72f4631 RD |
314 | bool __eq__(const wxBitmap* other) { return other ? (*self == *other) : false; } |
315 | bool __ne__(const wxBitmap* other) { return other ? (*self != *other) : true; } | |
b403cd65 | 316 | } |
0eae5d09 RD |
317 | |
318 | %property(Depth, GetDepth, SetDepth, doc="See `GetDepth` and `SetDepth`"); | |
319 | %property(Height, GetHeight, SetHeight, doc="See `GetHeight` and `SetHeight`"); | |
320 | %property(Mask, GetMask, SetMask, doc="See `GetMask` and `SetMask`"); | |
321 | %property(Palette, GetPalette, doc="See `GetPalette`"); | |
322 | %property(Size, GetSize, SetSize, doc="See `GetSize` and `SetSize`"); | |
323 | %property(SubBitmap, GetSubBitmap, doc="See `GetSubBitmap`"); | |
324 | %property(Width, GetWidth, SetWidth, doc="See `GetWidth` and `SetWidth`"); | |
325 | ||
d14a1e28 RD |
326 | }; |
327 | ||
328 | ||
238ba802 RD |
329 | //--------------------------------------------------------------------------- |
330 | // Factory functions for creating wxBitmaps from Python buffer objects. They | |
331 | // use the Abstract Pixel API to be able to set RGB and A bytes directly into | |
332 | // the wxBitmap's pixel buffer. | |
333 | ||
1d1972fc RD |
334 | %{ |
335 | // See http://tinyurl.com/e5adr for what premultiplying alpha means. It | |
336 | // appears to me that the other platforms are already doing it, so I'll just | |
337 | // automatically do it for wxMSW here. | |
338 | #ifdef __WXMSW__ | |
9bcdb631 RD |
339 | #define wxPy_premultiply(p, a) ((p) * (a) / 0xff) |
340 | #define wxPy_unpremultiply(p, a) ((a) ? ((p) * 0xff / (a)) : (p)) | |
1d1972fc RD |
341 | #else |
342 | #define wxPy_premultiply(p, a) (p) | |
c4caf81d | 343 | #define wxPy_unpremultiply(p, a) (p) |
1d1972fc RD |
344 | #endif |
345 | %} | |
346 | ||
347 | ||
238ba802 RD |
348 | %newobject _BitmapFromBufferAlpha; |
349 | %newobject _BitmapFromBuffer; | |
350 | %inline %{ | |
351 | wxBitmap* _BitmapFromBufferAlpha(int width, int height, | |
352 | buffer data, int DATASIZE, | |
353 | buffer alpha, int ALPHASIZE) | |
354 | { | |
355 | if (DATASIZE != width*height*3) { | |
356 | wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); | |
357 | return NULL; | |
358 | } | |
359 | ||
360 | if (ALPHASIZE != width*height) { | |
361 | wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size."); | |
362 | return NULL; | |
363 | } | |
364 | ||
365 | wxBitmap* bmp = new wxBitmap(width, height, 32); | |
c4caf81d RD |
366 | wxAlphaPixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height)); |
367 | if (! pixData) { | |
238ba802 RD |
368 | // raise an exception... |
369 | wxPyErr_SetString(PyExc_RuntimeError, | |
370 | "Failed to gain raw access to bitmap data."); | |
371 | return NULL; | |
372 | } | |
373 | ||
c4caf81d RD |
374 | pixData.UseAlpha(); |
375 | wxAlphaPixelData::Iterator p(pixData); | |
238ba802 RD |
376 | for (int y=0; y<height; y++) { |
377 | wxAlphaPixelData::Iterator rowStart = p; | |
378 | for (int x=0; x<width; x++) { | |
1d1972fc RD |
379 | byte a = *(alpha++); |
380 | p.Red() = wxPy_premultiply(*(data++), a); | |
381 | p.Green() = wxPy_premultiply(*(data++), a); | |
382 | p.Blue() = wxPy_premultiply(*(data++), a); | |
383 | p.Alpha() = a; | |
238ba802 RD |
384 | ++p; |
385 | } | |
386 | p = rowStart; | |
c4caf81d | 387 | p.OffsetY(pixData, 1); |
238ba802 RD |
388 | } |
389 | return bmp; | |
390 | } | |
391 | ||
392 | wxBitmap* _BitmapFromBuffer(int width, int height, buffer data, int DATASIZE) | |
393 | { | |
394 | if (DATASIZE != width*height*3) { | |
395 | wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); | |
396 | return NULL; | |
397 | } | |
398 | ||
399 | wxBitmap* bmp = new wxBitmap(width, height, 24); | |
c4caf81d RD |
400 | wxNativePixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height)); |
401 | if (! pixData) { | |
238ba802 RD |
402 | // raise an exception... |
403 | wxPyErr_SetString(PyExc_RuntimeError, | |
404 | "Failed to gain raw access to bitmap data."); | |
405 | return NULL; | |
406 | } | |
407 | ||
c4caf81d | 408 | wxNativePixelData::Iterator p(pixData); |
238ba802 RD |
409 | for (int y=0; y<height; y++) { |
410 | wxNativePixelData::Iterator rowStart = p; | |
411 | for (int x=0; x<width; x++) { | |
412 | p.Red() = *(data++); | |
413 | p.Green() = *(data++); | |
414 | p.Blue() = *(data++); | |
415 | ++p; | |
416 | } | |
417 | p = rowStart; | |
c4caf81d | 418 | p.OffsetY(pixData, 1); |
238ba802 RD |
419 | } |
420 | return bmp; | |
421 | } | |
422 | %} | |
423 | ||
424 | ||
425 | %pythoncode { | |
426 | def BitmapFromBuffer(width, height, dataBuffer, alphaBuffer=None): | |
427 | """ | |
428 | Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer | |
bf64693b RD |
429 | parameter must be a Python object that implements the buffer |
430 | interface, such as a string, array, etc. The dataBuffer object is | |
431 | expected to contain a series of RGB bytes and be width*height*3 | |
432 | bytes long. A buffer object can optionally be supplied for the | |
433 | image's alpha channel data, and it is expected to be width*height | |
434 | bytes long. On Windows the RGB values are 'premultiplied' by the | |
435 | alpha values. (The other platforms do the multiplication | |
436 | themselves.) | |
437 | ||
438 | Unlike `wx.ImageFromBuffer` the bitmap created with this function | |
439 | does not share the memory buffer with the buffer object. This is | |
440 | because the native pixel buffer format varies on different | |
441 | platforms, and so instead an efficient as possible copy of the | |
442 | data is made from the buffer objects to the bitmap's native pixel | |
443 | buffer. For direct access to a bitmap's pixel buffer see | |
444 | `wx.NativePixelData` and `wx.AlphaPixelData`. | |
238ba802 RD |
445 | |
446 | :see: `wx.Bitmap`, `wx.BitmapFromBufferRGBA`, `wx.NativePixelData`, | |
447 | `wx.AlphaPixelData`, `wx.ImageFromBuffer` | |
448 | """ | |
238ba802 RD |
449 | if alphaBuffer is not None: |
450 | return _gdi_._BitmapFromBufferAlpha(width, height, dataBuffer, alphaBuffer) | |
451 | else: | |
452 | return _gdi_._BitmapFromBuffer(width, height, dataBuffer) | |
453 | } | |
454 | ||
455 | ||
456 | ||
457 | %newobject _BitmapFromBufferRGBA; | |
458 | %inline %{ | |
777dffec | 459 | wxBitmap* _BitmapFromBufferRGBA(int width, int height, buffer data, int DATASIZE) |
238ba802 RD |
460 | { |
461 | if (DATASIZE != width*height*4) { | |
462 | wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); | |
463 | return NULL; | |
464 | } | |
465 | ||
466 | wxBitmap* bmp = new wxBitmap(width, height, 32); | |
c4caf81d RD |
467 | wxAlphaPixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height)); |
468 | if (! pixData) { | |
238ba802 RD |
469 | // raise an exception... |
470 | wxPyErr_SetString(PyExc_RuntimeError, | |
471 | "Failed to gain raw access to bitmap data."); | |
472 | return NULL; | |
473 | } | |
777dffec | 474 | |
c4caf81d RD |
475 | pixData.UseAlpha(); |
476 | wxAlphaPixelData::Iterator p(pixData); | |
238ba802 RD |
477 | for (int y=0; y<height; y++) { |
478 | wxAlphaPixelData::Iterator rowStart = p; | |
479 | for (int x=0; x<width; x++) { | |
1d1972fc RD |
480 | byte a = data[3]; |
481 | p.Red() = wxPy_premultiply(*(data++), a); | |
482 | p.Green() = wxPy_premultiply(*(data++), a); | |
483 | p.Blue() = wxPy_premultiply(*(data++), a); | |
484 | p.Alpha() = a; data++; | |
238ba802 RD |
485 | ++p; |
486 | } | |
487 | p = rowStart; | |
c4caf81d | 488 | p.OffsetY(pixData, 1); |
238ba802 RD |
489 | } |
490 | return bmp; | |
491 | } | |
492 | %} | |
493 | ||
494 | %pythoncode { | |
495 | def BitmapFromBufferRGBA(width, height, dataBuffer): | |
496 | """ | |
497 | Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer | |
bf64693b RD |
498 | parameter must be a Python object that implements the buffer |
499 | interface, such as a string, array, etc. The dataBuffer object is | |
500 | expected to contain a series of RGBA bytes (red, green, blue and | |
501 | alpha) and be width*height*4 bytes long. On Windows the RGB | |
502 | values are 'premultiplied' by the alpha values. (The other | |
503 | platforms do the multiplication themselves.) | |
504 | ||
505 | Unlike `wx.ImageFromBuffer` the bitmap created with this function | |
506 | does not share the memory buffer with the buffer object. This is | |
507 | because the native pixel buffer format varies on different | |
508 | platforms, and so instead an efficient as possible copy of the | |
509 | data is made from the buffer object to the bitmap's native pixel | |
510 | buffer. For direct access to a bitmap's pixel buffer see | |
511 | `wx.NativePixelData` and `wx.AlphaPixelData`. | |
238ba802 RD |
512 | |
513 | :see: `wx.Bitmap`, `wx.BitmapFromBuffer`, `wx.NativePixelData`, | |
514 | `wx.AlphaPixelData`, `wx.ImageFromBuffer` | |
515 | """ | |
238ba802 RD |
516 | return _gdi_._BitmapFromBufferRGBA(width, height, dataBuffer) |
517 | } | |
518 | ||
519 | ||
777dffec RD |
520 | //--------------------------------------------------------------------------- |
521 | ||
522 | class wxPixelDataBase | |
523 | { | |
524 | public: | |
525 | // origin of the rectangular region we represent | |
526 | wxPoint GetOrigin() const { return m_ptOrigin; } | |
527 | ||
528 | // width and height of the region we represent | |
529 | int GetWidth() const { return m_width; } | |
530 | int GetHeight() const { return m_height; } | |
531 | ||
532 | wxSize GetSize() const { return wxSize(m_width, m_height); } | |
533 | ||
534 | // the distance between two rows | |
535 | int GetRowStride() const { return m_stride; } | |
536 | ||
7012bb9f RD |
537 | %property(Height, GetHeight, doc="See `GetHeight`"); |
538 | %property(Origin, GetOrigin, doc="See `GetOrigin`"); | |
539 | %property(RowStride, GetRowStride, doc="See `GetRowStride`"); | |
540 | %property(Size, GetSize, doc="See `GetSize`"); | |
541 | %property(Width, GetWidth, doc="See `GetWidth`"); | |
777dffec RD |
542 | }; |
543 | ||
544 | ||
97c7d01d RD |
545 | // Both wxNativePixelData and wxAlphaPixelData have the same interface, so |
546 | // make a macro to declare them both. | |
777dffec RD |
547 | |
548 | %define PIXELDATA(PixelData) | |
549 | %{ | |
97c7d01d | 550 | typedef PixelData##::Iterator PixelData##_Accessor; |
777dffec | 551 | %} |
97c7d01d | 552 | class PixelData##_Accessor; |
777dffec RD |
553 | class PixelData : public wxPixelDataBase |
554 | { | |
555 | public: | |
556 | %nokwargs PixelData; | |
557 | ||
558 | PixelData(wxBitmap& bmp); | |
559 | PixelData(wxBitmap& bmp, const wxRect& rect); | |
560 | PixelData(wxBitmap& bmp, const wxPoint& pt, const wxSize& sz); | |
561 | ||
562 | ~PixelData(); | |
563 | ||
97c7d01d RD |
564 | PixelData##_Accessor GetPixels() const; |
565 | void UseAlpha(); | |
566 | ||
777dffec RD |
567 | %extend { |
568 | bool __nonzero__() { return self->operator bool(); } | |
569 | } | |
570 | ||
97c7d01d RD |
571 | %pythoncode { |
572 | def __iter__(self): | |
97c7d01d | 573 | """ |
9bcdb631 RD |
574 | Create and return an iterator object for this pixel data |
575 | object. (It's really a generator but I won't tell if you | |
576 | don't tell.) | |
97c7d01d | 577 | """ |
9bcdb631 RD |
578 | width = self.GetWidth() |
579 | height = self.GetHeight() | |
580 | pixels = self.GetPixels() | |
581 | ||
582 | # This class is a facade over the pixels object (using the one | |
583 | # in the enclosing scope) that only allows Get() and Set() to | |
584 | # be called. | |
585 | class PixelFacade(object): | |
97c7d01d | 586 | def Get(self): |
9bcdb631 RD |
587 | return pixels.Get() |
588 | def Set(self, *args, **kw): | |
589 | return pixels.Set(*args, **kw) | |
590 | def __str__(self): | |
591 | return str(self.Get()) | |
592 | def __repr__(self): | |
593 | return 'pixel(%d,%d): %s' % (x,y,self.Get()) | |
594 | X = property(lambda self: x) | |
595 | Y = property(lambda self: y) | |
596 | ||
597 | pf = PixelFacade() | |
598 | for y in xrange(height): | |
599 | for x in xrange(width): | |
600 | # We always generate the same pf instance, but it | |
601 | # accesses the pixels object which we use to iterate | |
602 | # over the pixel buffer. | |
603 | yield pf | |
604 | pixels.nextPixel() | |
605 | pixels.MoveTo(self, 0, y) | |
97c7d01d | 606 | } |
0eae5d09 RD |
607 | |
608 | %property(Pixels, GetPixels, doc="See `GetPixels`"); | |
777dffec RD |
609 | }; |
610 | ||
611 | ||
c4caf81d | 612 | |
97c7d01d | 613 | class PixelData##_Accessor |
777dffec RD |
614 | { |
615 | public: | |
97c7d01d | 616 | %nokwargs PixelData##_Accessor; |
777dffec | 617 | |
97c7d01d RD |
618 | PixelData##_Accessor(PixelData& data); |
619 | PixelData##_Accessor(wxBitmap& bmp, PixelData& data); | |
620 | PixelData##_Accessor(); | |
777dffec | 621 | |
97c7d01d | 622 | ~PixelData##_Accessor(); |
777dffec RD |
623 | |
624 | void Reset(const PixelData& data); | |
625 | bool IsOk() const; | |
626 | ||
627 | %extend { | |
97c7d01d | 628 | // PixelData##_Accessor& nextPixel() { return ++(*self); } |
777dffec RD |
629 | void nextPixel() { ++(*self); } |
630 | } | |
631 | ||
632 | void Offset(const PixelData& data, int x, int y); | |
633 | void OffsetX(const PixelData& data, int x); | |
634 | void OffsetY(const PixelData& data, int y); | |
635 | void MoveTo(const PixelData& data, int x, int y); | |
636 | ||
9bcdb631 RD |
637 | // NOTE: For now I'm not wrapping the Red, Green, Blue and Alpha |
638 | // functions because I can't hide the premultiplying needed on wxMSW | |
639 | // if only the individual components are wrapped, plus it would mean 3 | |
640 | // or 4 trips per pixel from Python to C++ instead of just one. | |
641 | // Instead I've added the Set and Get functions and put the | |
642 | // premultiplying in there. | |
c4caf81d RD |
643 | |
644 | // %extend { | |
645 | // byte _get_Red() { return self->Red(); } | |
646 | // byte _get_Green() { return self->Green(); } | |
647 | // byte _get_Blue() { return self->Blue(); } | |
648 | ||
649 | // void _set_Red(byte val) { self->Red() = val; } | |
650 | // void _set_Green(byte val) { self->Green() = val; } | |
651 | // void _set_Blue(byte val) { self->Blue() = val; } | |
652 | // } | |
653 | ||
654 | // %pythoncode { | |
655 | // Red = property(_get_Red, _set_Red) | |
656 | // Green = property(_get_Green, _set_Green) | |
657 | // Blue = property(_get_Blue, _set_Blue) | |
658 | // } | |
777dffec RD |
659 | }; |
660 | %enddef | |
661 | ||
662 | ||
97c7d01d RD |
663 | %pythonAppend wxAlphaPixelData::wxAlphaPixelData "self.UseAlpha()" |
664 | ||
665 | // Make the classes | |
777dffec RD |
666 | PIXELDATA(wxNativePixelData) |
667 | PIXELDATA(wxAlphaPixelData) | |
668 | ||
669 | ||
670 | // Add in a few things that are different between the wxNativePixelData and | |
97c7d01d RD |
671 | // wxAlphaPixelData and the iterator classes and so are not included in our |
672 | // macro... | |
777dffec | 673 | |
97c7d01d | 674 | %extend wxNativePixelData_Accessor { |
777dffec RD |
675 | void Set(byte red, byte green, byte blue) { |
676 | self->Red() = red; | |
677 | self->Green() = green; | |
678 | self->Blue() = blue; | |
679 | } | |
680 | ||
681 | PyObject* Get() { | |
682 | PyObject* rv = PyTuple_New(3); | |
683 | PyTuple_SetItem(rv, 0, PyInt_FromLong(self->Red())); | |
684 | PyTuple_SetItem(rv, 1, PyInt_FromLong(self->Green())); | |
685 | PyTuple_SetItem(rv, 2, PyInt_FromLong(self->Blue())); | |
686 | return rv; | |
687 | } | |
688 | } | |
689 | ||
97c7d01d | 690 | %extend wxAlphaPixelData_Accessor { |
c4caf81d RD |
691 | // byte _get_Alpha() { return self->Alpha(); } |
692 | // void _set_Alpha(byte val) { self->Alpha() = val; } | |
777dffec | 693 | |
c4caf81d RD |
694 | // %pythoncode { |
695 | // Alpha = property(_get_Alpha, _set_Alpha) | |
696 | // } | |
777dffec RD |
697 | |
698 | void Set(byte red, byte green, byte blue, byte alpha) { | |
c4caf81d RD |
699 | self->Red() = wxPy_premultiply(red, alpha); |
700 | self->Green() = wxPy_premultiply(green, alpha); | |
701 | self->Blue() = wxPy_premultiply(blue, alpha); | |
777dffec RD |
702 | self->Alpha() = alpha; |
703 | } | |
704 | ||
705 | PyObject* Get() { | |
706 | PyObject* rv = PyTuple_New(4); | |
c4caf81d RD |
707 | int red = self->Red(); |
708 | int green = self->Green(); | |
709 | int blue = self->Blue(); | |
710 | int alpha = self->Alpha(); | |
711 | ||
712 | PyTuple_SetItem(rv, 0, PyInt_FromLong( wxPy_unpremultiply(red, alpha) )); | |
713 | PyTuple_SetItem(rv, 1, PyInt_FromLong( wxPy_unpremultiply(green, alpha) )); | |
714 | PyTuple_SetItem(rv, 2, PyInt_FromLong( wxPy_unpremultiply(blue, alpha) )); | |
715 | PyTuple_SetItem(rv, 3, PyInt_FromLong( alpha )); | |
777dffec RD |
716 | return rv; |
717 | } | |
718 | } | |
719 | ||
97c7d01d | 720 | |
d14a1e28 RD |
721 | //--------------------------------------------------------------------------- |
722 | ||
1e0c8722 | 723 | DocStr(wxMask, |
dce2bd22 RD |
724 | "This class encapsulates a monochrome mask bitmap, where the masked |
725 | area is black and the unmasked area is white. When associated with a | |
726 | bitmap and drawn in a device context, the unmasked area of the bitmap | |
727 | will be drawn, and the masked area will not be drawn. | |
728 | ||
729 | A mask may be associated with a `wx.Bitmap`. It is used in | |
730 | `wx.DC.DrawBitmap` or `wx.DC.Blit` when the source device context is a | |
731 | `wx.MemoryDC` with a `wx.Bitmap` selected into it that contains a | |
d07d2bc9 | 732 | mask.", ""); |
1e0c8722 | 733 | |
ab1f7d2a RD |
734 | MustHaveApp(wxMask); |
735 | ||
d14a1e28 RD |
736 | class wxMask : public wxObject { |
737 | public: | |
1e0c8722 | 738 | |
0482c494 | 739 | DocStr(wxMask, |
dce2bd22 RD |
740 | "Constructs a mask from a `wx.Bitmap` and a `wx.Colour` in that bitmap |
741 | that indicates the transparent portions of the mask. In other words, | |
742 | the pixels in ``bitmap`` that match ``colour`` will be the transparent | |
743 | portions of the mask. If no ``colour`` or an invalid ``colour`` is | |
744 | passed then BLACK is used. | |
745 | ||
d07d2bc9 | 746 | :see: `wx.Bitmap`, `wx.Colour`", ""); |
0482c494 RD |
747 | |
748 | %extend { | |
749 | wxMask(const wxBitmap& bitmap, const wxColour& colour = wxNullColour) { | |
6c2dd16f | 750 | if ( !colour.IsOk() ) |
0482c494 RD |
751 | return new wxMask(bitmap, *wxBLACK); |
752 | else | |
753 | return new wxMask(bitmap, colour); | |
754 | } | |
755 | } | |
d14a1e28 | 756 | |
c5633576 | 757 | ~wxMask(); |
d14a1e28 RD |
758 | }; |
759 | ||
dce2bd22 | 760 | %pythoncode { MaskColour = wx._deprecated(Mask, "wx.MaskColour is deprecated, use `wx.Mask` instead.") } |
0482c494 | 761 | |
d14a1e28 RD |
762 | //--------------------------------------------------------------------------- |
763 | //--------------------------------------------------------------------------- | |
0f82eb02 RD |
764 | |
765 | // Turn GIL acquisition back on. | |
777dffec | 766 | %threadWrapperOn |