]>
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 | ||
181 | bool Ok(); | |
182 | ||
dce2bd22 RD |
183 | DocDeclStr( |
184 | int , GetWidth(), | |
d07d2bc9 | 185 | "Gets the width of the bitmap in pixels.", ""); |
dce2bd22 | 186 | |
1e0c8722 | 187 | |
dce2bd22 RD |
188 | DocDeclStr( |
189 | int , GetHeight(), | |
d07d2bc9 | 190 | "Gets the height of the bitmap in pixels.", ""); |
dce2bd22 | 191 | |
1e0c8722 | 192 | |
dce2bd22 RD |
193 | DocDeclStr( |
194 | int , GetDepth(), | |
195 | "Gets the colour depth of the bitmap. A value of 1 indicates a | |
d07d2bc9 | 196 | monochrome bitmap.", ""); |
dce2bd22 | 197 | |
d14a1e28 | 198 | |
ba938c3d RD |
199 | |
200 | %extend { | |
d07d2bc9 | 201 | DocStr(GetSize, "Get the size of the bitmap.", ""); |
ba938c3d RD |
202 | wxSize GetSize() { |
203 | wxSize size(self->GetWidth(), self->GetHeight()); | |
204 | return size; | |
205 | } | |
206 | } | |
207 | ||
208 | ||
dce2bd22 RD |
209 | DocDeclStr( |
210 | virtual wxImage , ConvertToImage() const, | |
211 | "Creates a platform-independent image from a platform-dependent | |
212 | bitmap. This preserves mask information so that bitmaps and images can | |
d07d2bc9 | 213 | be converted back and forth without loss in that respect.", ""); |
dce2bd22 RD |
214 | |
215 | ||
216 | DocDeclStr( | |
217 | virtual wxMask* , GetMask() const, | |
218 | "Gets the associated mask (if any) which may have been loaded from a | |
219 | file or explpicitly set for the bitmap. | |
220 | ||
221 | :see: `SetMask`, `wx.Mask` | |
d07d2bc9 | 222 | ", ""); |
c1acc129 RD |
223 | |
224 | // MSW only? wxBitmap GetMaskBitmap() const; | |
dce2bd22 | 225 | |
c5633576 | 226 | %disownarg(wxMask*); |
dce2bd22 RD |
227 | DocDeclStr( |
228 | virtual void , SetMask(wxMask* mask), | |
229 | "Sets the mask for this bitmap. | |
230 | ||
231 | :see: `GetMask`, `wx.Mask` | |
d07d2bc9 | 232 | ", ""); |
c5633576 | 233 | %cleardisown(wxMask*); |
1e0c8722 | 234 | |
d14a1e28 | 235 | %extend { |
1e0c8722 | 236 | DocStr(SetMaskColour, |
d07d2bc9 | 237 | "Create a Mask based on a specified colour in the Bitmap.", ""); |
d14a1e28 RD |
238 | void SetMaskColour(const wxColour& colour) { |
239 | wxMask *mask = new wxMask(*self, colour); | |
240 | self->SetMask(mask); | |
241 | } | |
242 | } | |
d14a1e28 | 243 | |
d14a1e28 | 244 | |
dce2bd22 RD |
245 | DocDeclStr( |
246 | virtual wxBitmap , GetSubBitmap(const wxRect& rect) const, | |
247 | "Returns a sub-bitmap of the current one as long as the rect belongs | |
248 | entirely to the bitmap. This function preserves bit depth and mask | |
d07d2bc9 | 249 | information.", ""); |
dce2bd22 RD |
250 | |
251 | ||
252 | DocDeclStr( | |
253 | virtual bool , SaveFile(const wxString &name, wxBitmapType type, | |
254 | wxPalette *palette = NULL), | |
255 | "Saves a bitmap in the named file. See `__init__` for a description of | |
d07d2bc9 | 256 | the ``type`` parameter.", ""); |
dce2bd22 | 257 | |
1e0c8722 | 258 | |
dce2bd22 RD |
259 | DocDeclStr( |
260 | virtual bool , LoadFile(const wxString &name, wxBitmapType type), | |
261 | "Loads a bitmap from a file. See `__init__` for a description of the | |
d07d2bc9 | 262 | ``type`` parameter.", ""); |
dce2bd22 | 263 | |
d14a1e28 RD |
264 | |
265 | ||
d14a1e28 | 266 | virtual wxPalette *GetPalette() const; |
7aada1e0 | 267 | #ifdef __WXMSW__ |
d14a1e28 RD |
268 | virtual void SetPalette(const wxPalette& palette); |
269 | #endif | |
270 | ||
d14a1e28 | 271 | |
1e0c8722 RD |
272 | virtual bool CopyFromIcon(const wxIcon& icon); |
273 | ||
dce2bd22 RD |
274 | DocDeclStr( |
275 | virtual void , SetHeight(int height), | |
d07d2bc9 | 276 | "Set the height property (does not affect the existing bitmap data).", ""); |
dce2bd22 RD |
277 | |
278 | ||
279 | DocDeclStr( | |
280 | virtual void , SetWidth(int width), | |
d07d2bc9 | 281 | "Set the width property (does not affect the existing bitmap data).", ""); |
1e0c8722 | 282 | |
1e0c8722 | 283 | |
dce2bd22 RD |
284 | DocDeclStr( |
285 | virtual void , SetDepth(int depth), | |
d07d2bc9 | 286 | "Set the depth property (does not affect the existing bitmap data).", ""); |
dce2bd22 | 287 | |
d14a1e28 | 288 | |
ba938c3d | 289 | %extend { |
d07d2bc9 | 290 | DocStr(SetSize, "Set the bitmap size (does not affect the existing bitmap data).", ""); |
ba938c3d RD |
291 | void SetSize(const wxSize& size) { |
292 | self->SetWidth(size.x); | |
293 | self->SetHeight(size.y); | |
294 | } | |
295 | } | |
1e0c8722 | 296 | |
d14a1e28 RD |
297 | #ifdef __WXMSW__ |
298 | bool CopyFromCursor(const wxCursor& cursor); | |
02b800ce | 299 | |
2f91e3df KO |
300 | // WXWIN_COMPATIBILITY_2_4 |
301 | #if 0 | |
d14a1e28 RD |
302 | int GetQuality(); |
303 | void SetQuality(int q); | |
02b800ce RD |
304 | %pythoncode { GetQuality = wx._deprecated(GetQuality) } |
305 | %pythoncode { SetQuality = wx._deprecated(SetQuality) } | |
2f91e3df | 306 | #endif |
d14a1e28 RD |
307 | #endif |
308 | ||
309 | %pythoncode { def __nonzero__(self): return self.Ok() } | |
b403cd65 RD |
310 | |
311 | %extend { | |
a72f4631 RD |
312 | bool __eq__(const wxBitmap* other) { return other ? (*self == *other) : false; } |
313 | bool __ne__(const wxBitmap* other) { return other ? (*self != *other) : true; } | |
b403cd65 | 314 | } |
d14a1e28 RD |
315 | }; |
316 | ||
317 | ||
238ba802 RD |
318 | //--------------------------------------------------------------------------- |
319 | // Factory functions for creating wxBitmaps from Python buffer objects. They | |
320 | // use the Abstract Pixel API to be able to set RGB and A bytes directly into | |
321 | // the wxBitmap's pixel buffer. | |
322 | ||
1d1972fc RD |
323 | %{ |
324 | // See http://tinyurl.com/e5adr for what premultiplying alpha means. It | |
325 | // appears to me that the other platforms are already doing it, so I'll just | |
326 | // automatically do it for wxMSW here. | |
327 | #ifdef __WXMSW__ | |
328 | #define wxPy_premultiply(p, a) ((p) * (a) / 256) | |
c4caf81d | 329 | #define wxPy_unpremultiply(p, a) ((a) ? ((p) * 256 / (a)) : (p)) |
1d1972fc RD |
330 | #else |
331 | #define wxPy_premultiply(p, a) (p) | |
c4caf81d | 332 | #define wxPy_unpremultiply(p, a) (p) |
1d1972fc RD |
333 | #endif |
334 | %} | |
335 | ||
336 | ||
238ba802 RD |
337 | %newobject _BitmapFromBufferAlpha; |
338 | %newobject _BitmapFromBuffer; | |
339 | %inline %{ | |
340 | wxBitmap* _BitmapFromBufferAlpha(int width, int height, | |
341 | buffer data, int DATASIZE, | |
342 | buffer alpha, int ALPHASIZE) | |
343 | { | |
344 | if (DATASIZE != width*height*3) { | |
345 | wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); | |
346 | return NULL; | |
347 | } | |
348 | ||
349 | if (ALPHASIZE != width*height) { | |
350 | wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size."); | |
351 | return NULL; | |
352 | } | |
353 | ||
354 | wxBitmap* bmp = new wxBitmap(width, height, 32); | |
c4caf81d RD |
355 | wxAlphaPixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height)); |
356 | if (! pixData) { | |
238ba802 RD |
357 | // raise an exception... |
358 | wxPyErr_SetString(PyExc_RuntimeError, | |
359 | "Failed to gain raw access to bitmap data."); | |
360 | return NULL; | |
361 | } | |
362 | ||
c4caf81d RD |
363 | pixData.UseAlpha(); |
364 | wxAlphaPixelData::Iterator p(pixData); | |
238ba802 RD |
365 | for (int y=0; y<height; y++) { |
366 | wxAlphaPixelData::Iterator rowStart = p; | |
367 | for (int x=0; x<width; x++) { | |
1d1972fc RD |
368 | byte a = *(alpha++); |
369 | p.Red() = wxPy_premultiply(*(data++), a); | |
370 | p.Green() = wxPy_premultiply(*(data++), a); | |
371 | p.Blue() = wxPy_premultiply(*(data++), a); | |
372 | p.Alpha() = a; | |
238ba802 RD |
373 | ++p; |
374 | } | |
375 | p = rowStart; | |
c4caf81d | 376 | p.OffsetY(pixData, 1); |
238ba802 RD |
377 | } |
378 | return bmp; | |
379 | } | |
380 | ||
381 | wxBitmap* _BitmapFromBuffer(int width, int height, buffer data, int DATASIZE) | |
382 | { | |
383 | if (DATASIZE != width*height*3) { | |
384 | wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); | |
385 | return NULL; | |
386 | } | |
387 | ||
388 | wxBitmap* bmp = new wxBitmap(width, height, 24); | |
c4caf81d RD |
389 | wxNativePixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height)); |
390 | if (! pixData) { | |
238ba802 RD |
391 | // raise an exception... |
392 | wxPyErr_SetString(PyExc_RuntimeError, | |
393 | "Failed to gain raw access to bitmap data."); | |
394 | return NULL; | |
395 | } | |
396 | ||
c4caf81d | 397 | wxNativePixelData::Iterator p(pixData); |
238ba802 RD |
398 | for (int y=0; y<height; y++) { |
399 | wxNativePixelData::Iterator rowStart = p; | |
400 | for (int x=0; x<width; x++) { | |
401 | p.Red() = *(data++); | |
402 | p.Green() = *(data++); | |
403 | p.Blue() = *(data++); | |
404 | ++p; | |
405 | } | |
406 | p = rowStart; | |
c4caf81d | 407 | p.OffsetY(pixData, 1); |
238ba802 RD |
408 | } |
409 | return bmp; | |
410 | } | |
411 | %} | |
412 | ||
413 | ||
414 | %pythoncode { | |
415 | def BitmapFromBuffer(width, height, dataBuffer, alphaBuffer=None): | |
416 | """ | |
417 | Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer | |
bf64693b RD |
418 | parameter must be a Python object that implements the buffer |
419 | interface, such as a string, array, etc. The dataBuffer object is | |
420 | expected to contain a series of RGB bytes and be width*height*3 | |
421 | bytes long. A buffer object can optionally be supplied for the | |
422 | image's alpha channel data, and it is expected to be width*height | |
423 | bytes long. On Windows the RGB values are 'premultiplied' by the | |
424 | alpha values. (The other platforms do the multiplication | |
425 | themselves.) | |
426 | ||
427 | Unlike `wx.ImageFromBuffer` the bitmap created with this function | |
428 | does not share the memory buffer with the buffer object. This is | |
429 | because the native pixel buffer format varies on different | |
430 | platforms, and so instead an efficient as possible copy of the | |
431 | data is made from the buffer objects to the bitmap's native pixel | |
432 | buffer. For direct access to a bitmap's pixel buffer see | |
433 | `wx.NativePixelData` and `wx.AlphaPixelData`. | |
238ba802 RD |
434 | |
435 | :see: `wx.Bitmap`, `wx.BitmapFromBufferRGBA`, `wx.NativePixelData`, | |
436 | `wx.AlphaPixelData`, `wx.ImageFromBuffer` | |
437 | """ | |
238ba802 RD |
438 | if alphaBuffer is not None: |
439 | return _gdi_._BitmapFromBufferAlpha(width, height, dataBuffer, alphaBuffer) | |
440 | else: | |
441 | return _gdi_._BitmapFromBuffer(width, height, dataBuffer) | |
442 | } | |
443 | ||
444 | ||
445 | ||
446 | %newobject _BitmapFromBufferRGBA; | |
447 | %inline %{ | |
777dffec | 448 | wxBitmap* _BitmapFromBufferRGBA(int width, int height, buffer data, int DATASIZE) |
238ba802 RD |
449 | { |
450 | if (DATASIZE != width*height*4) { | |
451 | wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); | |
452 | return NULL; | |
453 | } | |
454 | ||
455 | wxBitmap* bmp = new wxBitmap(width, height, 32); | |
c4caf81d RD |
456 | wxAlphaPixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height)); |
457 | if (! pixData) { | |
238ba802 RD |
458 | // raise an exception... |
459 | wxPyErr_SetString(PyExc_RuntimeError, | |
460 | "Failed to gain raw access to bitmap data."); | |
461 | return NULL; | |
462 | } | |
777dffec | 463 | |
c4caf81d RD |
464 | pixData.UseAlpha(); |
465 | wxAlphaPixelData::Iterator p(pixData); | |
238ba802 RD |
466 | for (int y=0; y<height; y++) { |
467 | wxAlphaPixelData::Iterator rowStart = p; | |
468 | for (int x=0; x<width; x++) { | |
1d1972fc RD |
469 | byte a = data[3]; |
470 | p.Red() = wxPy_premultiply(*(data++), a); | |
471 | p.Green() = wxPy_premultiply(*(data++), a); | |
472 | p.Blue() = wxPy_premultiply(*(data++), a); | |
473 | p.Alpha() = a; data++; | |
238ba802 RD |
474 | ++p; |
475 | } | |
476 | p = rowStart; | |
c4caf81d | 477 | p.OffsetY(pixData, 1); |
238ba802 RD |
478 | } |
479 | return bmp; | |
480 | } | |
481 | %} | |
482 | ||
483 | %pythoncode { | |
484 | def BitmapFromBufferRGBA(width, height, dataBuffer): | |
485 | """ | |
486 | Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer | |
bf64693b RD |
487 | parameter must be a Python object that implements the buffer |
488 | interface, such as a string, array, etc. The dataBuffer object is | |
489 | expected to contain a series of RGBA bytes (red, green, blue and | |
490 | alpha) and be width*height*4 bytes long. On Windows the RGB | |
491 | values are 'premultiplied' by the alpha values. (The other | |
492 | platforms do the multiplication themselves.) | |
493 | ||
494 | Unlike `wx.ImageFromBuffer` the bitmap created with this function | |
495 | does not share the memory buffer with the buffer object. This is | |
496 | because the native pixel buffer format varies on different | |
497 | platforms, and so instead an efficient as possible copy of the | |
498 | data is made from the buffer object to the bitmap's native pixel | |
499 | buffer. For direct access to a bitmap's pixel buffer see | |
500 | `wx.NativePixelData` and `wx.AlphaPixelData`. | |
238ba802 RD |
501 | |
502 | :see: `wx.Bitmap`, `wx.BitmapFromBuffer`, `wx.NativePixelData`, | |
503 | `wx.AlphaPixelData`, `wx.ImageFromBuffer` | |
504 | """ | |
238ba802 RD |
505 | return _gdi_._BitmapFromBufferRGBA(width, height, dataBuffer) |
506 | } | |
507 | ||
508 | ||
777dffec RD |
509 | //--------------------------------------------------------------------------- |
510 | ||
511 | class wxPixelDataBase | |
512 | { | |
513 | public: | |
514 | // origin of the rectangular region we represent | |
515 | wxPoint GetOrigin() const { return m_ptOrigin; } | |
516 | ||
517 | // width and height of the region we represent | |
518 | int GetWidth() const { return m_width; } | |
519 | int GetHeight() const { return m_height; } | |
520 | ||
521 | wxSize GetSize() const { return wxSize(m_width, m_height); } | |
522 | ||
523 | // the distance between two rows | |
524 | int GetRowStride() const { return m_stride; } | |
525 | ||
526 | }; | |
527 | ||
528 | ||
97c7d01d RD |
529 | // Both wxNativePixelData and wxAlphaPixelData have the same interface, so |
530 | // make a macro to declare them both. | |
777dffec RD |
531 | |
532 | %define PIXELDATA(PixelData) | |
533 | %{ | |
97c7d01d | 534 | typedef PixelData##::Iterator PixelData##_Accessor; |
777dffec | 535 | %} |
97c7d01d | 536 | class PixelData##_Accessor; |
777dffec RD |
537 | class PixelData : public wxPixelDataBase |
538 | { | |
539 | public: | |
540 | %nokwargs PixelData; | |
541 | ||
542 | PixelData(wxBitmap& bmp); | |
543 | PixelData(wxBitmap& bmp, const wxRect& rect); | |
544 | PixelData(wxBitmap& bmp, const wxPoint& pt, const wxSize& sz); | |
545 | ||
546 | ~PixelData(); | |
547 | ||
97c7d01d RD |
548 | PixelData##_Accessor GetPixels() const; |
549 | void UseAlpha(); | |
550 | ||
777dffec RD |
551 | %extend { |
552 | bool __nonzero__() { return self->operator bool(); } | |
553 | } | |
554 | ||
97c7d01d RD |
555 | %pythoncode { |
556 | def __iter__(self): | |
557 | """Create and return an iterator object for this pixel data object.""" | |
558 | return self.PixelIterator(self) | |
559 | ||
560 | class PixelIterator(object): | |
561 | """ | |
562 | Sequential iterator which returns pixel accessor objects starting at the | |
563 | the top-left corner, and going row-by-row until the bottom-right | |
564 | corner is reached. | |
565 | """ | |
566 | ||
567 | class PixelAccessor(object): | |
568 | """ | |
569 | This class is what is returned by the iterator and allows the pixel | |
570 | to be Get or Set. | |
571 | """ | |
572 | def __init__(self, data, pixels, x, y): | |
573 | self.data = data | |
574 | self.pixels = pixels | |
575 | self.x = x | |
576 | self.y = y | |
577 | def Set(self, *args, **kw): | |
578 | self.pixels.MoveTo(self.data, self.x, self.y) | |
579 | return self.pixels.Set(*args, **kw) | |
580 | def Get(self): | |
581 | self.pixels.MoveTo(self.data, self.x, self.y) | |
582 | return self.pixels.Get() | |
583 | ||
584 | def __init__(self, pixelData): | |
585 | self.x = self.y = 0 | |
586 | self.w = pixelData.GetWidth() | |
587 | self.h = pixelData.GetHeight() | |
588 | self.data = pixelData | |
589 | self.pixels = pixelData.GetPixels() | |
590 | ||
591 | def __iter__(self): | |
592 | return self | |
593 | ||
594 | def next(self): | |
595 | if self.y >= self.h: | |
596 | raise StopIteration | |
597 | p = self.PixelAccessor(self.data, self.pixels, self.x, self.y) | |
598 | self.x += 1 | |
599 | if self.x >= self.w: | |
600 | self.x = 0 | |
601 | self.y += 1 | |
602 | return p | |
603 | } | |
777dffec RD |
604 | }; |
605 | ||
606 | ||
c4caf81d | 607 | |
97c7d01d | 608 | class PixelData##_Accessor |
777dffec RD |
609 | { |
610 | public: | |
97c7d01d | 611 | %nokwargs PixelData##_Accessor; |
777dffec | 612 | |
97c7d01d RD |
613 | PixelData##_Accessor(PixelData& data); |
614 | PixelData##_Accessor(wxBitmap& bmp, PixelData& data); | |
615 | PixelData##_Accessor(); | |
777dffec | 616 | |
97c7d01d | 617 | ~PixelData##_Accessor(); |
777dffec RD |
618 | |
619 | void Reset(const PixelData& data); | |
620 | bool IsOk() const; | |
621 | ||
622 | %extend { | |
97c7d01d | 623 | // PixelData##_Accessor& nextPixel() { return ++(*self); } |
777dffec RD |
624 | void nextPixel() { ++(*self); } |
625 | } | |
626 | ||
627 | void Offset(const PixelData& data, int x, int y); | |
628 | void OffsetX(const PixelData& data, int x); | |
629 | void OffsetY(const PixelData& data, int y); | |
630 | void MoveTo(const PixelData& data, int x, int y); | |
631 | ||
c4caf81d RD |
632 | // NOTE: For now I'm not wrapping the Red, Green, Blue and Alpha functions |
633 | // because I can't hide the premultiplying needed on wxMSW if only the | |
634 | // individual components are wrapped. Instead I've added the Set and Get | |
bf64693b | 635 | // functions and put the premultiplying in there. |
c4caf81d RD |
636 | |
637 | // %extend { | |
638 | // byte _get_Red() { return self->Red(); } | |
639 | // byte _get_Green() { return self->Green(); } | |
640 | // byte _get_Blue() { return self->Blue(); } | |
641 | ||
642 | // void _set_Red(byte val) { self->Red() = val; } | |
643 | // void _set_Green(byte val) { self->Green() = val; } | |
644 | // void _set_Blue(byte val) { self->Blue() = val; } | |
645 | // } | |
646 | ||
647 | // %pythoncode { | |
648 | // Red = property(_get_Red, _set_Red) | |
649 | // Green = property(_get_Green, _set_Green) | |
650 | // Blue = property(_get_Blue, _set_Blue) | |
651 | // } | |
777dffec RD |
652 | }; |
653 | %enddef | |
654 | ||
655 | ||
97c7d01d RD |
656 | %pythonAppend wxAlphaPixelData::wxAlphaPixelData "self.UseAlpha()" |
657 | ||
658 | // Make the classes | |
777dffec RD |
659 | PIXELDATA(wxNativePixelData) |
660 | PIXELDATA(wxAlphaPixelData) | |
661 | ||
662 | ||
663 | // Add in a few things that are different between the wxNativePixelData and | |
97c7d01d RD |
664 | // wxAlphaPixelData and the iterator classes and so are not included in our |
665 | // macro... | |
777dffec | 666 | |
97c7d01d | 667 | %extend wxNativePixelData_Accessor { |
777dffec RD |
668 | void Set(byte red, byte green, byte blue) { |
669 | self->Red() = red; | |
670 | self->Green() = green; | |
671 | self->Blue() = blue; | |
672 | } | |
673 | ||
674 | PyObject* Get() { | |
675 | PyObject* rv = PyTuple_New(3); | |
676 | PyTuple_SetItem(rv, 0, PyInt_FromLong(self->Red())); | |
677 | PyTuple_SetItem(rv, 1, PyInt_FromLong(self->Green())); | |
678 | PyTuple_SetItem(rv, 2, PyInt_FromLong(self->Blue())); | |
679 | return rv; | |
680 | } | |
681 | } | |
682 | ||
97c7d01d | 683 | %extend wxAlphaPixelData_Accessor { |
c4caf81d RD |
684 | // byte _get_Alpha() { return self->Alpha(); } |
685 | // void _set_Alpha(byte val) { self->Alpha() = val; } | |
777dffec | 686 | |
c4caf81d RD |
687 | // %pythoncode { |
688 | // Alpha = property(_get_Alpha, _set_Alpha) | |
689 | // } | |
777dffec RD |
690 | |
691 | void Set(byte red, byte green, byte blue, byte alpha) { | |
c4caf81d RD |
692 | self->Red() = wxPy_premultiply(red, alpha); |
693 | self->Green() = wxPy_premultiply(green, alpha); | |
694 | self->Blue() = wxPy_premultiply(blue, alpha); | |
777dffec RD |
695 | self->Alpha() = alpha; |
696 | } | |
697 | ||
698 | PyObject* Get() { | |
699 | PyObject* rv = PyTuple_New(4); | |
c4caf81d RD |
700 | int red = self->Red(); |
701 | int green = self->Green(); | |
702 | int blue = self->Blue(); | |
703 | int alpha = self->Alpha(); | |
704 | ||
705 | PyTuple_SetItem(rv, 0, PyInt_FromLong( wxPy_unpremultiply(red, alpha) )); | |
706 | PyTuple_SetItem(rv, 1, PyInt_FromLong( wxPy_unpremultiply(green, alpha) )); | |
707 | PyTuple_SetItem(rv, 2, PyInt_FromLong( wxPy_unpremultiply(blue, alpha) )); | |
708 | PyTuple_SetItem(rv, 3, PyInt_FromLong( alpha )); | |
777dffec RD |
709 | return rv; |
710 | } | |
711 | } | |
712 | ||
97c7d01d | 713 | |
d14a1e28 RD |
714 | //--------------------------------------------------------------------------- |
715 | ||
1e0c8722 | 716 | DocStr(wxMask, |
dce2bd22 RD |
717 | "This class encapsulates a monochrome mask bitmap, where the masked |
718 | area is black and the unmasked area is white. When associated with a | |
719 | bitmap and drawn in a device context, the unmasked area of the bitmap | |
720 | will be drawn, and the masked area will not be drawn. | |
721 | ||
722 | A mask may be associated with a `wx.Bitmap`. It is used in | |
723 | `wx.DC.DrawBitmap` or `wx.DC.Blit` when the source device context is a | |
724 | `wx.MemoryDC` with a `wx.Bitmap` selected into it that contains a | |
d07d2bc9 | 725 | mask.", ""); |
1e0c8722 | 726 | |
ab1f7d2a RD |
727 | MustHaveApp(wxMask); |
728 | ||
d14a1e28 RD |
729 | class wxMask : public wxObject { |
730 | public: | |
1e0c8722 | 731 | |
0482c494 | 732 | DocStr(wxMask, |
dce2bd22 RD |
733 | "Constructs a mask from a `wx.Bitmap` and a `wx.Colour` in that bitmap |
734 | that indicates the transparent portions of the mask. In other words, | |
735 | the pixels in ``bitmap`` that match ``colour`` will be the transparent | |
736 | portions of the mask. If no ``colour`` or an invalid ``colour`` is | |
737 | passed then BLACK is used. | |
738 | ||
d07d2bc9 | 739 | :see: `wx.Bitmap`, `wx.Colour`", ""); |
0482c494 RD |
740 | |
741 | %extend { | |
742 | wxMask(const wxBitmap& bitmap, const wxColour& colour = wxNullColour) { | |
743 | if ( !colour.Ok() ) | |
744 | return new wxMask(bitmap, *wxBLACK); | |
745 | else | |
746 | return new wxMask(bitmap, colour); | |
747 | } | |
748 | } | |
d14a1e28 | 749 | |
c5633576 | 750 | ~wxMask(); |
d14a1e28 RD |
751 | }; |
752 | ||
dce2bd22 | 753 | %pythoncode { MaskColour = wx._deprecated(Mask, "wx.MaskColour is deprecated, use `wx.Mask` instead.") } |
0482c494 | 754 | |
d14a1e28 RD |
755 | //--------------------------------------------------------------------------- |
756 | //--------------------------------------------------------------------------- | |
777dffec | 757 | %threadWrapperOn |