1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: SWIG interface for wxBitmap and wxMask
7 // Created: 7-July-1997
9 // Copyright: (c) 2003 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
16 #include <wx/rawbmp.h>
19 //---------------------------------------------------------------------------
24 static char** ConvertListOfStrings(PyObject* listOfStrings) {
28 if (!PyList_Check(listOfStrings)) {
29 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
32 count = PyList_Size(listOfStrings);
33 cArray = new char*[count];
35 for(int x=0; x<count; x++) {
36 // TODO: Need some validation and error checking here
37 cArray[x] = PyString_AsString(PyList_GET_ITEM(listOfStrings, x));
44 //---------------------------------------------------------------------------
46 // TODO: When the API stabalizes and is available on other platforms, add
47 // wrappers for the new wxBitmap, wxRawBitmap, wxDIB stuff...
50 "The wx.Bitmap class encapsulates the concept of a platform-dependent
51 bitmap. It can be either monochrome or colour, and either loaded from
52 a file or created dynamically. A bitmap can be selected into a memory
53 device context (instance of `wx.MemoryDC`). This enables the bitmap to
54 be copied to a window or memory device context using `wx.DC.Blit`, or
55 to be used as a drawing surface.", "
57 The BMP and XMP image file formats are supported on all platforms by
58 wx.Bitmap. Other formats are automatically loaded by `wx.Image` and
59 converted to a wx.Bitmap, so any image file format supported by
60 `wx.Image` can be used.
62 :todo: Add wrappers and support for raw bitmap data access. Can this
63 be be put into Python without losing the speed benefits of the
64 teplates and iterators in rawbmp.h?
66 :todo: Find a way to do very efficient PIL Image <--> wx.Bitmap
69 :see: `wx.EmptyBitmap`, `wx.BitmapFromIcon`, `wx.BitmapFromImage`,
70 `wx.BitmapFromXPMData`, `wx.BitmapFromBits`, `wx.BitmapFromBuffer`,
71 `wx.BitmapFromBufferRGBA`, `wx.Image`
75 MustHaveApp(wxBitmap);
77 class wxBitmap : public wxGDIObject
81 wxBitmap(const wxString& name, wxBitmapType type=wxBITMAP_TYPE_ANY),
82 "Loads a bitmap from a file.",
84 :param name: Name of the file to load the bitmap from.
85 :param type: The type of image to expect. Can be one of the following
86 constants (assuming that the neccessary `wx.Image` handlers are
101 * wx.BITMAP_TYPE_PICT
102 * wx.BITMAP_TYPE_ICON
106 :see: Alternate constructors `wx.EmptyBitmap`, `wx.BitmapFromIcon`,
107 `wx.BitmapFromImage`, `wx.BitmapFromXPMData`,
114 wxBitmap(int width, int height, int depth=-1),
115 "Creates a new bitmap of the given size. A depth of -1 indicates the
116 depth of the current screen or visual. Some platforms only support 1
117 for monochrome and -1 for the current display depth.", "",
121 wxBitmap(const wxIcon& icon),
122 "Create a new bitmap from a `wx.Icon` object.", "",
126 wxBitmap(const wxImage& image, int depth=-1),
127 "Creates bitmap object from a `wx.Image`. This has to be done to
128 actually display a `wx.Image` as you cannot draw an image directly on
129 a window. The resulting bitmap will use the provided colour depth (or
130 that of the current screen colour depth if depth is -1) which entails
131 that a colour reduction may have to take place.", "",
138 "Construct a Bitmap from a list of strings formatted as XPM data.", "",
139 wxBitmap(PyObject* listOfStrings))
141 char** cArray = NULL;
144 cArray = ConvertListOfStrings(listOfStrings);
147 bmp = new wxBitmap(cArray);
155 "Creates a bitmap from an array of bits. You should only use this
156 function for monochrome bitmaps (depth 1) in portable programs: in
157 this case the bits parameter should contain an XBM image. For other
158 bit depths, the behaviour is platform dependent.", "",
159 wxBitmap(PyObject* bits, int width, int height, int depth=1 ))
163 PyString_AsStringAndSize(bits, &buf, &length);
164 return new wxBitmap(buf, width, height, depth);
169 // wxGDIImage methods
173 void SetHandle(long handle) { self->SetHandle((WXHANDLE)handle); }
181 "Gets the width of the bitmap in pixels.", "");
186 "Gets the height of the bitmap in pixels.", "");
191 "Gets the colour depth of the bitmap. A value of 1 indicates a
192 monochrome bitmap.", "");
197 DocStr(GetSize, "Get the size of the bitmap.", "");
199 wxSize size(self->GetWidth(), self->GetHeight());
206 virtual wxImage , ConvertToImage() const,
207 "Creates a platform-independent image from a platform-dependent
208 bitmap. This preserves mask information so that bitmaps and images can
209 be converted back and forth without loss in that respect.", "");
214 virtual wxMask* , GetMask() const,
215 "Gets the associated mask (if any) which may have been loaded from a
216 file or explpicitly set for the bitmap.
218 :see: `SetMask`, `wx.Mask`
221 // MSW only? wxBitmap GetMaskBitmap() const;
225 virtual void , SetMask(wxMask* mask),
226 "Sets the mask for this bitmap.
228 :see: `GetMask`, `wx.Mask`
230 %cleardisown(wxMask*);
233 DocStr(SetMaskColour,
234 "Create a Mask based on a specified colour in the Bitmap.", "");
235 void SetMaskColour(const wxColour& colour) {
236 wxMask *mask = new wxMask(*self, colour);
243 virtual wxBitmap , GetSubBitmap(const wxRect& rect) const,
244 "Returns a sub-bitmap of the current one as long as the rect belongs
245 entirely to the bitmap. This function preserves bit depth and mask
250 virtual bool , SaveFile(const wxString &name, wxBitmapType type,
251 wxPalette *palette = NULL),
252 "Saves a bitmap in the named file. See `__init__` for a description of
253 the ``type`` parameter.", "");
257 virtual bool , LoadFile(const wxString &name, wxBitmapType type),
258 "Loads a bitmap from a file. See `__init__` for a description of the
259 ``type`` parameter.", "");
263 virtual wxPalette *GetPalette() const;
265 virtual void SetPalette(const wxPalette& palette);
269 virtual bool CopyFromIcon(const wxIcon& icon);
272 virtual void , SetHeight(int height),
273 "Set the height property (does not affect the existing bitmap data).", "");
277 virtual void , SetWidth(int width),
278 "Set the width property (does not affect the existing bitmap data).", "");
282 virtual void , SetDepth(int depth),
283 "Set the depth property (does not affect the existing bitmap data).", "");
287 DocStr(SetSize, "Set the bitmap size (does not affect the existing bitmap data).", "");
288 void SetSize(const wxSize& size) {
289 self->SetWidth(size.x);
290 self->SetHeight(size.y);
295 bool CopyFromCursor(const wxCursor& cursor);
297 // WXWIN_COMPATIBILITY_2_4
300 void SetQuality(int q);
301 %pythoncode { GetQuality = wx._deprecated(GetQuality) }
302 %pythoncode { SetQuality = wx._deprecated(SetQuality) }
306 %pythoncode { def __nonzero__(self): return self.Ok() }
309 bool __eq__(const wxBitmap* other) { return other ? (*self == *other) : false; }
310 bool __ne__(const wxBitmap* other) { return other ? (*self != *other) : true; }
315 //---------------------------------------------------------------------------
316 // Factory functions for creating wxBitmaps from Python buffer objects. They
317 // use the Abstract Pixel API to be able to set RGB and A bytes directly into
318 // the wxBitmap's pixel buffer.
321 // See http://tinyurl.com/e5adr for what premultiplying alpha means. It
322 // appears to me that the other platforms are already doing it, so I'll just
323 // automatically do it for wxMSW here.
325 #define wxPy_premultiply(p, a) ((p) * (a) / 256)
327 #define wxPy_premultiply(p, a) (p)
332 %newobject _BitmapFromBufferAlpha;
333 %newobject _BitmapFromBuffer;
335 wxBitmap* _BitmapFromBufferAlpha(int width, int height,
336 buffer data, int DATASIZE,
337 buffer alpha, int ALPHASIZE)
339 if (DATASIZE != width*height*3) {
340 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
344 if (ALPHASIZE != width*height) {
345 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
349 wxBitmap* bmp = new wxBitmap(width, height, 32);
350 wxAlphaPixelData pixels(*bmp, wxPoint(0,0), wxSize(width,height));
352 // raise an exception...
353 wxPyErr_SetString(PyExc_RuntimeError,
354 "Failed to gain raw access to bitmap data.");
359 wxAlphaPixelData::Iterator p(pixels);
360 for (int y=0; y<height; y++) {
361 wxAlphaPixelData::Iterator rowStart = p;
362 for (int x=0; x<width; x++) {
364 p.Red() = wxPy_premultiply(*(data++), a);
365 p.Green() = wxPy_premultiply(*(data++), a);
366 p.Blue() = wxPy_premultiply(*(data++), a);
371 p.OffsetY(pixels, 1);
376 wxBitmap* _BitmapFromBuffer(int width, int height, buffer data, int DATASIZE)
378 if (DATASIZE != width*height*3) {
379 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
383 wxBitmap* bmp = new wxBitmap(width, height, 24);
384 wxNativePixelData pixels(*bmp, wxPoint(0,0), wxSize(width,height));
386 // raise an exception...
387 wxPyErr_SetString(PyExc_RuntimeError,
388 "Failed to gain raw access to bitmap data.");
392 wxNativePixelData::Iterator p(pixels);
393 for (int y=0; y<height; y++) {
394 wxNativePixelData::Iterator rowStart = p;
395 for (int x=0; x<width; x++) {
397 p.Green() = *(data++);
398 p.Blue() = *(data++);
402 p.OffsetY(pixels, 1);
410 def BitmapFromBuffer(width, height, dataBuffer, alphaBuffer=None):
412 Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer
413 parameter must be a Python object that implements the buffer interface, or
414 is convertable to a buffer object, such as a string, array, etc. The
415 dataBuffer object is expected to contain a series of RGB bytes and be
416 width*height*3 bytes long. A buffer object can optionally be supplied for
417 the image's alpha channel data, and it is expected to be width*height
418 bytes long. On Windows the RGB values are 'premultiplied' by the alpha
419 values. (The other platforms appear to already be premultiplying the
422 Unlike `wx.ImageFromBuffer` the bitmap created with this function does not
423 share the memory buffer with the buffer object. This is because the
424 native pixel buffer format varies on different platforms, and so instead
425 an efficient as possible copy of the data is made from the buffer objects
426 to the bitmap's native pixel buffer. For direct access to a bitmap's
427 pixel buffer see `wx.NativePixelData` and `wx.AlphaPixelData`.
429 :see: `wx.Bitmap`, `wx.BitmapFromBufferRGBA`, `wx.NativePixelData`,
430 `wx.AlphaPixelData`, `wx.ImageFromBuffer`
432 if not isinstance(dataBuffer, buffer):
433 dataBuffer = buffer(dataBuffer)
434 if alphaBuffer is not None and not isinstance(alphaBuffer, buffer):
435 alphaBuffer = buffer(alphaBuffer)
436 if alphaBuffer is not None:
437 return _gdi_._BitmapFromBufferAlpha(width, height, dataBuffer, alphaBuffer)
439 return _gdi_._BitmapFromBuffer(width, height, dataBuffer)
444 %newobject _BitmapFromBufferRGBA;
446 wxBitmap* _BitmapFromBufferRGBA(int width, int height, buffer data, int DATASIZE)
448 if (DATASIZE != width*height*4) {
449 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
453 wxBitmap* bmp = new wxBitmap(width, height, 32);
454 wxAlphaPixelData pixels(*bmp, wxPoint(0,0), wxSize(width,height));
456 // raise an exception...
457 wxPyErr_SetString(PyExc_RuntimeError,
458 "Failed to gain raw access to bitmap data.");
463 wxAlphaPixelData::Iterator p(pixels);
464 for (int y=0; y<height; y++) {
465 wxAlphaPixelData::Iterator rowStart = p;
466 for (int x=0; x<width; x++) {
468 p.Red() = wxPy_premultiply(*(data++), a);
469 p.Green() = wxPy_premultiply(*(data++), a);
470 p.Blue() = wxPy_premultiply(*(data++), a);
471 p.Alpha() = a; data++;
475 p.OffsetY(pixels, 1);
482 def BitmapFromBufferRGBA(width, height, dataBuffer):
484 Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer
485 parameter must be a Python object that implements the buffer interface, or
486 is convertable to a buffer object, such as a string, array, etc. The
487 dataBuffer object is expected to contain a series of RGBA bytes (red,
488 green, blue and alpha) and be width*height*4 bytes long. On Windows the
489 RGB values are 'premultiplied' by the alpha values. (The other platforms
490 appear to already be premultiplying the alpha.)
492 Unlike `wx.ImageFromBuffer` the bitmap created with this function does not
493 share the memory buffer with the buffer object. This is because the
494 native pixel buffer format varies on different platforms, and so instead
495 an efficient as possible copy of the data is made from the buffer object
496 to the bitmap's native pixel buffer. For direct access to a bitmap's
497 pixel buffer see `wx.NativePixelData` and `wx.AlphaPixelData`.
499 :see: `wx.Bitmap`, `wx.BitmapFromBuffer`, `wx.NativePixelData`,
500 `wx.AlphaPixelData`, `wx.ImageFromBuffer`
502 if not isinstance(dataBuffer, buffer):
503 dataBuffer = buffer(dataBuffer)
504 return _gdi_._BitmapFromBufferRGBA(width, height, dataBuffer)
508 //---------------------------------------------------------------------------
511 "This class encapsulates a monochrome mask bitmap, where the masked
512 area is black and the unmasked area is white. When associated with a
513 bitmap and drawn in a device context, the unmasked area of the bitmap
514 will be drawn, and the masked area will not be drawn.
516 A mask may be associated with a `wx.Bitmap`. It is used in
517 `wx.DC.DrawBitmap` or `wx.DC.Blit` when the source device context is a
518 `wx.MemoryDC` with a `wx.Bitmap` selected into it that contains a
523 class wxMask : public wxObject {
527 "Constructs a mask from a `wx.Bitmap` and a `wx.Colour` in that bitmap
528 that indicates the transparent portions of the mask. In other words,
529 the pixels in ``bitmap`` that match ``colour`` will be the transparent
530 portions of the mask. If no ``colour`` or an invalid ``colour`` is
531 passed then BLACK is used.
533 :see: `wx.Bitmap`, `wx.Colour`", "");
536 wxMask(const wxBitmap& bitmap, const wxColour& colour = wxNullColour) {
538 return new wxMask(bitmap, *wxBLACK);
540 return new wxMask(bitmap, colour);
547 %pythoncode { MaskColour = wx._deprecated(Mask, "wx.MaskColour is deprecated, use `wx.Mask` instead.") }
549 //---------------------------------------------------------------------------
550 //---------------------------------------------------------------------------