]>
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`, | |
111 | `wx.BitmapFromImage`, `wx.BitmapFromXPMData`, | |
112 | `wx.BitmapFromBits` | |
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 | ||
c5633576 | 216 | |
dce2bd22 RD |
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 | ||
310 | %pythoncode { def __nonzero__(self): return self.Ok() } | |
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 | } |
d14a1e28 RD |
316 | }; |
317 | ||
318 | ||
238ba802 RD |
319 | //--------------------------------------------------------------------------- |
320 | // Factory functions for creating wxBitmaps from Python buffer objects. They | |
321 | // use the Abstract Pixel API to be able to set RGB and A bytes directly into | |
322 | // the wxBitmap's pixel buffer. | |
323 | ||
1d1972fc RD |
324 | %{ |
325 | // See http://tinyurl.com/e5adr for what premultiplying alpha means. It | |
326 | // appears to me that the other platforms are already doing it, so I'll just | |
327 | // automatically do it for wxMSW here. | |
328 | #ifdef __WXMSW__ | |
329 | #define wxPy_premultiply(p, a) ((p) * (a) / 256) | |
c4caf81d | 330 | #define wxPy_unpremultiply(p, a) ((a) ? ((p) * 256 / (a)) : (p)) |
1d1972fc RD |
331 | #else |
332 | #define wxPy_premultiply(p, a) (p) | |
c4caf81d | 333 | #define wxPy_unpremultiply(p, a) (p) |
1d1972fc RD |
334 | #endif |
335 | %} | |
336 | ||
337 | ||
238ba802 RD |
338 | %newobject _BitmapFromBufferAlpha; |
339 | %newobject _BitmapFromBuffer; | |
340 | %inline %{ | |
341 | wxBitmap* _BitmapFromBufferAlpha(int width, int height, | |
342 | buffer data, int DATASIZE, | |
343 | buffer alpha, int ALPHASIZE) | |
344 | { | |
345 | if (DATASIZE != width*height*3) { | |
346 | wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); | |
347 | return NULL; | |
348 | } | |
349 | ||
350 | if (ALPHASIZE != width*height) { | |
351 | wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size."); | |
352 | return NULL; | |
353 | } | |
354 | ||
355 | wxBitmap* bmp = new wxBitmap(width, height, 32); | |
c4caf81d RD |
356 | wxAlphaPixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height)); |
357 | if (! pixData) { | |
238ba802 RD |
358 | // raise an exception... |
359 | wxPyErr_SetString(PyExc_RuntimeError, | |
360 | "Failed to gain raw access to bitmap data."); | |
361 | return NULL; | |
362 | } | |
363 | ||
c4caf81d RD |
364 | pixData.UseAlpha(); |
365 | wxAlphaPixelData::Iterator p(pixData); | |
238ba802 RD |
366 | for (int y=0; y<height; y++) { |
367 | wxAlphaPixelData::Iterator rowStart = p; | |
368 | for (int x=0; x<width; x++) { | |
1d1972fc RD |
369 | byte a = *(alpha++); |
370 | p.Red() = wxPy_premultiply(*(data++), a); | |
371 | p.Green() = wxPy_premultiply(*(data++), a); | |
372 | p.Blue() = wxPy_premultiply(*(data++), a); | |
373 | p.Alpha() = a; | |
238ba802 RD |
374 | ++p; |
375 | } | |
376 | p = rowStart; | |
c4caf81d | 377 | p.OffsetY(pixData, 1); |
238ba802 RD |
378 | } |
379 | return bmp; | |
380 | } | |
381 | ||
382 | wxBitmap* _BitmapFromBuffer(int width, int height, buffer data, int DATASIZE) | |
383 | { | |
384 | if (DATASIZE != width*height*3) { | |
385 | wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); | |
386 | return NULL; | |
387 | } | |
388 | ||
389 | wxBitmap* bmp = new wxBitmap(width, height, 24); | |
c4caf81d RD |
390 | wxNativePixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height)); |
391 | if (! pixData) { | |
238ba802 RD |
392 | // raise an exception... |
393 | wxPyErr_SetString(PyExc_RuntimeError, | |
394 | "Failed to gain raw access to bitmap data."); | |
395 | return NULL; | |
396 | } | |
397 | ||
c4caf81d | 398 | wxNativePixelData::Iterator p(pixData); |
238ba802 RD |
399 | for (int y=0; y<height; y++) { |
400 | wxNativePixelData::Iterator rowStart = p; | |
401 | for (int x=0; x<width; x++) { | |
402 | p.Red() = *(data++); | |
403 | p.Green() = *(data++); | |
404 | p.Blue() = *(data++); | |
405 | ++p; | |
406 | } | |
407 | p = rowStart; | |
c4caf81d | 408 | p.OffsetY(pixData, 1); |
238ba802 RD |
409 | } |
410 | return bmp; | |
411 | } | |
412 | %} | |
413 | ||
414 | ||
415 | %pythoncode { | |
416 | def BitmapFromBuffer(width, height, dataBuffer, alphaBuffer=None): | |
417 | """ | |
418 | Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer | |
419 | parameter must be a Python object that implements the buffer interface, or | |
420 | is convertable to a buffer object, such as a string, array, etc. The | |
421 | dataBuffer object is expected to contain a series of RGB bytes and be | |
422 | width*height*3 bytes long. A buffer object can optionally be supplied for | |
423 | the image's alpha channel data, and it is expected to be width*height | |
1d1972fc RD |
424 | bytes long. On Windows the RGB values are 'premultiplied' by the alpha |
425 | values. (The other platforms appear to already be premultiplying the | |
426 | alpha.) | |
238ba802 RD |
427 | |
428 | Unlike `wx.ImageFromBuffer` the bitmap created with this function does not | |
429 | share the memory buffer with the buffer object. This is because the | |
430 | native pixel buffer format varies on different platforms, and so instead | |
431 | an efficient as possible copy of the data is made from the buffer objects | |
432 | to the bitmap's native pixel buffer. For direct access to a bitmap's | |
433 | pixel buffer see `wx.NativePixelData` and `wx.AlphaPixelData`. | |
434 | ||
435 | :see: `wx.Bitmap`, `wx.BitmapFromBufferRGBA`, `wx.NativePixelData`, | |
436 | `wx.AlphaPixelData`, `wx.ImageFromBuffer` | |
437 | """ | |
438 | if not isinstance(dataBuffer, buffer): | |
439 | dataBuffer = buffer(dataBuffer) | |
440 | if alphaBuffer is not None and not isinstance(alphaBuffer, buffer): | |
441 | alphaBuffer = buffer(alphaBuffer) | |
442 | if alphaBuffer is not None: | |
443 | return _gdi_._BitmapFromBufferAlpha(width, height, dataBuffer, alphaBuffer) | |
444 | else: | |
445 | return _gdi_._BitmapFromBuffer(width, height, dataBuffer) | |
446 | } | |
447 | ||
448 | ||
449 | ||
450 | %newobject _BitmapFromBufferRGBA; | |
451 | %inline %{ | |
777dffec | 452 | wxBitmap* _BitmapFromBufferRGBA(int width, int height, buffer data, int DATASIZE) |
238ba802 RD |
453 | { |
454 | if (DATASIZE != width*height*4) { | |
455 | wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); | |
456 | return NULL; | |
457 | } | |
458 | ||
459 | wxBitmap* bmp = new wxBitmap(width, height, 32); | |
c4caf81d RD |
460 | wxAlphaPixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height)); |
461 | if (! pixData) { | |
238ba802 RD |
462 | // raise an exception... |
463 | wxPyErr_SetString(PyExc_RuntimeError, | |
464 | "Failed to gain raw access to bitmap data."); | |
465 | return NULL; | |
466 | } | |
777dffec | 467 | |
c4caf81d RD |
468 | pixData.UseAlpha(); |
469 | wxAlphaPixelData::Iterator p(pixData); | |
238ba802 RD |
470 | for (int y=0; y<height; y++) { |
471 | wxAlphaPixelData::Iterator rowStart = p; | |
472 | for (int x=0; x<width; x++) { | |
1d1972fc RD |
473 | byte a = data[3]; |
474 | p.Red() = wxPy_premultiply(*(data++), a); | |
475 | p.Green() = wxPy_premultiply(*(data++), a); | |
476 | p.Blue() = wxPy_premultiply(*(data++), a); | |
477 | p.Alpha() = a; data++; | |
238ba802 RD |
478 | ++p; |
479 | } | |
480 | p = rowStart; | |
c4caf81d | 481 | p.OffsetY(pixData, 1); |
238ba802 RD |
482 | } |
483 | return bmp; | |
484 | } | |
485 | %} | |
486 | ||
487 | %pythoncode { | |
488 | def BitmapFromBufferRGBA(width, height, dataBuffer): | |
489 | """ | |
490 | Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer | |
491 | parameter must be a Python object that implements the buffer interface, or | |
492 | is convertable to a buffer object, such as a string, array, etc. The | |
493 | dataBuffer object is expected to contain a series of RGBA bytes (red, | |
1d1972fc RD |
494 | green, blue and alpha) and be width*height*4 bytes long. On Windows the |
495 | RGB values are 'premultiplied' by the alpha values. (The other platforms | |
496 | appear to already be premultiplying the alpha.) | |
238ba802 RD |
497 | |
498 | Unlike `wx.ImageFromBuffer` the bitmap created with this function does not | |
499 | share the memory buffer with the buffer object. This is because the | |
500 | native pixel buffer format varies on different platforms, and so instead | |
501 | an efficient as possible copy of the data is made from the buffer object | |
502 | to the bitmap's native pixel buffer. For direct access to a bitmap's | |
503 | pixel buffer see `wx.NativePixelData` and `wx.AlphaPixelData`. | |
504 | ||
505 | :see: `wx.Bitmap`, `wx.BitmapFromBuffer`, `wx.NativePixelData`, | |
506 | `wx.AlphaPixelData`, `wx.ImageFromBuffer` | |
507 | """ | |
508 | if not isinstance(dataBuffer, buffer): | |
509 | dataBuffer = buffer(dataBuffer) | |
510 | return _gdi_._BitmapFromBufferRGBA(width, height, dataBuffer) | |
511 | } | |
512 | ||
513 | ||
777dffec RD |
514 | //--------------------------------------------------------------------------- |
515 | ||
516 | class wxPixelDataBase | |
517 | { | |
518 | public: | |
519 | // origin of the rectangular region we represent | |
520 | wxPoint GetOrigin() const { return m_ptOrigin; } | |
521 | ||
522 | // width and height of the region we represent | |
523 | int GetWidth() const { return m_width; } | |
524 | int GetHeight() const { return m_height; } | |
525 | ||
526 | wxSize GetSize() const { return wxSize(m_width, m_height); } | |
527 | ||
528 | // the distance between two rows | |
529 | int GetRowStride() const { return m_stride; } | |
530 | ||
531 | }; | |
532 | ||
533 | ||
534 | ||
535 | %define PIXELDATA(PixelData) | |
536 | %{ | |
537 | typedef PixelData##::Iterator PixelData##_Iterator; | |
538 | %} | |
539 | class PixelData##_Iterator; | |
540 | class PixelData : public wxPixelDataBase | |
541 | { | |
542 | public: | |
543 | %nokwargs PixelData; | |
544 | ||
545 | PixelData(wxBitmap& bmp); | |
546 | PixelData(wxBitmap& bmp, const wxRect& rect); | |
547 | PixelData(wxBitmap& bmp, const wxPoint& pt, const wxSize& sz); | |
548 | ||
549 | ~PixelData(); | |
550 | ||
551 | %extend { | |
552 | bool __nonzero__() { return self->operator bool(); } | |
553 | } | |
554 | ||
555 | PixelData##_Iterator GetPixels() const; | |
556 | void UseAlpha(); | |
557 | }; | |
558 | ||
559 | ||
c4caf81d | 560 | |
777dffec RD |
561 | class PixelData##_Iterator |
562 | { | |
563 | public: | |
564 | %nokwargs PixelData##_Iterator; | |
565 | ||
566 | PixelData##_Iterator(PixelData& data); | |
567 | PixelData##_Iterator(wxBitmap& bmp, PixelData& data); | |
568 | PixelData##_Iterator(); | |
569 | ||
570 | ~PixelData##_Iterator(); | |
571 | ||
572 | void Reset(const PixelData& data); | |
573 | bool IsOk() const; | |
574 | ||
575 | %extend { | |
576 | // PixelData##_Iterator& nextPixel() { return ++(*self); } | |
577 | void nextPixel() { ++(*self); } | |
578 | } | |
579 | ||
580 | void Offset(const PixelData& data, int x, int y); | |
581 | void OffsetX(const PixelData& data, int x); | |
582 | void OffsetY(const PixelData& data, int y); | |
583 | void MoveTo(const PixelData& data, int x, int y); | |
584 | ||
c4caf81d RD |
585 | // NOTE: For now I'm not wrapping the Red, Green, Blue and Alpha functions |
586 | // because I can't hide the premultiplying needed on wxMSW if only the | |
587 | // individual components are wrapped. Instead I've added the Set and Get | |
588 | // functions and put the puemultiplying in there. | |
589 | ||
590 | // %extend { | |
591 | // byte _get_Red() { return self->Red(); } | |
592 | // byte _get_Green() { return self->Green(); } | |
593 | // byte _get_Blue() { return self->Blue(); } | |
594 | ||
595 | // void _set_Red(byte val) { self->Red() = val; } | |
596 | // void _set_Green(byte val) { self->Green() = val; } | |
597 | // void _set_Blue(byte val) { self->Blue() = val; } | |
598 | // } | |
599 | ||
600 | // %pythoncode { | |
601 | // Red = property(_get_Red, _set_Red) | |
602 | // Green = property(_get_Green, _set_Green) | |
603 | // Blue = property(_get_Blue, _set_Blue) | |
604 | // } | |
777dffec RD |
605 | }; |
606 | %enddef | |
607 | ||
608 | ||
609 | PIXELDATA(wxNativePixelData) | |
610 | PIXELDATA(wxAlphaPixelData) | |
611 | ||
612 | ||
613 | // Add in a few things that are different between the wxNativePixelData and | |
614 | // wxAlphaPixelData iterators and so are not included in our macro... | |
615 | ||
616 | %extend wxNativePixelData_Iterator { | |
617 | void Set(byte red, byte green, byte blue) { | |
618 | self->Red() = red; | |
619 | self->Green() = green; | |
620 | self->Blue() = blue; | |
621 | } | |
622 | ||
623 | PyObject* Get() { | |
624 | PyObject* rv = PyTuple_New(3); | |
625 | PyTuple_SetItem(rv, 0, PyInt_FromLong(self->Red())); | |
626 | PyTuple_SetItem(rv, 1, PyInt_FromLong(self->Green())); | |
627 | PyTuple_SetItem(rv, 2, PyInt_FromLong(self->Blue())); | |
628 | return rv; | |
629 | } | |
630 | } | |
631 | ||
632 | %extend wxAlphaPixelData_Iterator { | |
c4caf81d RD |
633 | // byte _get_Alpha() { return self->Alpha(); } |
634 | // void _set_Alpha(byte val) { self->Alpha() = val; } | |
777dffec | 635 | |
c4caf81d RD |
636 | // %pythoncode { |
637 | // Alpha = property(_get_Alpha, _set_Alpha) | |
638 | // } | |
777dffec RD |
639 | |
640 | void Set(byte red, byte green, byte blue, byte alpha) { | |
c4caf81d RD |
641 | self->Red() = wxPy_premultiply(red, alpha); |
642 | self->Green() = wxPy_premultiply(green, alpha); | |
643 | self->Blue() = wxPy_premultiply(blue, alpha); | |
777dffec RD |
644 | self->Alpha() = alpha; |
645 | } | |
646 | ||
647 | PyObject* Get() { | |
648 | PyObject* rv = PyTuple_New(4); | |
c4caf81d RD |
649 | int red = self->Red(); |
650 | int green = self->Green(); | |
651 | int blue = self->Blue(); | |
652 | int alpha = self->Alpha(); | |
653 | ||
654 | PyTuple_SetItem(rv, 0, PyInt_FromLong( wxPy_unpremultiply(red, alpha) )); | |
655 | PyTuple_SetItem(rv, 1, PyInt_FromLong( wxPy_unpremultiply(green, alpha) )); | |
656 | PyTuple_SetItem(rv, 2, PyInt_FromLong( wxPy_unpremultiply(blue, alpha) )); | |
657 | PyTuple_SetItem(rv, 3, PyInt_FromLong( alpha )); | |
777dffec RD |
658 | return rv; |
659 | } | |
660 | } | |
661 | ||
d14a1e28 RD |
662 | //--------------------------------------------------------------------------- |
663 | ||
1e0c8722 | 664 | DocStr(wxMask, |
dce2bd22 RD |
665 | "This class encapsulates a monochrome mask bitmap, where the masked |
666 | area is black and the unmasked area is white. When associated with a | |
667 | bitmap and drawn in a device context, the unmasked area of the bitmap | |
668 | will be drawn, and the masked area will not be drawn. | |
669 | ||
670 | A mask may be associated with a `wx.Bitmap`. It is used in | |
671 | `wx.DC.DrawBitmap` or `wx.DC.Blit` when the source device context is a | |
672 | `wx.MemoryDC` with a `wx.Bitmap` selected into it that contains a | |
d07d2bc9 | 673 | mask.", ""); |
1e0c8722 | 674 | |
ab1f7d2a RD |
675 | MustHaveApp(wxMask); |
676 | ||
d14a1e28 RD |
677 | class wxMask : public wxObject { |
678 | public: | |
1e0c8722 | 679 | |
0482c494 | 680 | DocStr(wxMask, |
dce2bd22 RD |
681 | "Constructs a mask from a `wx.Bitmap` and a `wx.Colour` in that bitmap |
682 | that indicates the transparent portions of the mask. In other words, | |
683 | the pixels in ``bitmap`` that match ``colour`` will be the transparent | |
684 | portions of the mask. If no ``colour`` or an invalid ``colour`` is | |
685 | passed then BLACK is used. | |
686 | ||
d07d2bc9 | 687 | :see: `wx.Bitmap`, `wx.Colour`", ""); |
0482c494 RD |
688 | |
689 | %extend { | |
690 | wxMask(const wxBitmap& bitmap, const wxColour& colour = wxNullColour) { | |
691 | if ( !colour.Ok() ) | |
692 | return new wxMask(bitmap, *wxBLACK); | |
693 | else | |
694 | return new wxMask(bitmap, colour); | |
695 | } | |
696 | } | |
d14a1e28 | 697 | |
c5633576 | 698 | ~wxMask(); |
d14a1e28 RD |
699 | }; |
700 | ||
dce2bd22 | 701 | %pythoncode { MaskColour = wx._deprecated(Mask, "wx.MaskColour is deprecated, use `wx.Mask` instead.") } |
0482c494 | 702 | |
d14a1e28 RD |
703 | //--------------------------------------------------------------------------- |
704 | //--------------------------------------------------------------------------- | |
777dffec | 705 | %threadWrapperOn |