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