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>
20 // Turn off the aquisition of the Global Interpreter Lock for this file
23 //---------------------------------------------------------------------------
28 static char** ConvertListOfStrings(PyObject* listOfStrings) {
32 if (!PyList_Check(listOfStrings)) {
33 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
36 count = PyList_Size(listOfStrings);
37 cArray = new char*[count];
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));
48 //---------------------------------------------------------------------------
50 // TODO: When the API stabalizes and is available on other platforms, add
51 // wrappers for the new wxBitmap, wxRawBitmap, wxDIB stuff...
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
59 to be used as a drawing surface.", "
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.
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?
70 :todo: Find a way to do very efficient PIL Image <--> wx.Bitmap
73 :see: `wx.EmptyBitmap`, `wx.BitmapFromIcon`, `wx.BitmapFromImage`,
74 `wx.BitmapFromXPMData`, `wx.BitmapFromBits`, `wx.BitmapFromBuffer`,
75 `wx.BitmapFromBufferRGBA`, `wx.Image`
79 MustHaveApp(wxBitmap);
81 class wxBitmap : public wxGDIObject
85 wxBitmap(const wxString& name, wxBitmapType type=wxBITMAP_TYPE_ANY),
86 "Loads a bitmap from a file.",
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
102 * wx.BITMAP_TYPE_JPEG
105 * wx.BITMAP_TYPE_PICT
106 * wx.BITMAP_TYPE_ICON
110 :see: Alternate constructors `wx.EmptyBitmap`, `wx.BitmapFromIcon`,
111 `wx.BitmapFromImage`, `wx.BitmapFromXPMData`,
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
121 for monochrome and -1 for the current display depth.", "",
125 wxBitmap(const wxIcon& icon),
126 "Create a new bitmap from a `wx.Icon` object.", "",
130 wxBitmap(const wxImage& image, int depth=-1),
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
135 that a colour reduction may have to take place.", "",
142 "Construct a Bitmap from a list of strings formatted as XPM data.", "",
143 wxBitmap(PyObject* listOfStrings))
145 char** cArray = NULL;
148 cArray = ConvertListOfStrings(listOfStrings);
151 bmp = new wxBitmap(cArray);
159 "Creates a bitmap from an array of bits. You should only use this
160 function for monochrome bitmaps (depth 1) in portable programs: in
161 this case the bits parameter should contain an XBM image. For other
162 bit depths, the behaviour is platform dependent.", "",
163 wxBitmap(PyObject* bits, int width, int height, int depth=1 ))
167 PyString_AsStringAndSize(bits, &buf, &length);
168 return new wxBitmap(buf, width, height, depth);
173 // wxGDIImage methods
177 void SetHandle(long handle) { self->SetHandle((WXHANDLE)handle); }
185 "Gets the width of the bitmap in pixels.", "");
190 "Gets the height of the bitmap in pixels.", "");
195 "Gets the colour depth of the bitmap. A value of 1 indicates a
196 monochrome bitmap.", "");
201 DocStr(GetSize, "Get the size of the bitmap.", "");
203 wxSize size(self->GetWidth(), self->GetHeight());
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
213 be converted back and forth without loss in that respect.", "");
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.
222 :see: `SetMask`, `wx.Mask`
225 // MSW only? wxBitmap GetMaskBitmap() const;
229 virtual void , SetMask(wxMask* mask),
230 "Sets the mask for this bitmap.
232 :see: `GetMask`, `wx.Mask`
234 %cleardisown(wxMask*);
237 DocStr(SetMaskColour,
238 "Create a Mask based on a specified colour in the Bitmap.", "");
239 void SetMaskColour(const wxColour& colour) {
240 wxMask *mask = new wxMask(*self, colour);
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
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
257 the ``type`` parameter.", "");
261 virtual bool , LoadFile(const wxString &name, wxBitmapType type),
262 "Loads a bitmap from a file. See `__init__` for a description of the
263 ``type`` parameter.", "");
267 virtual wxPalette *GetPalette() const;
269 virtual void SetPalette(const wxPalette& palette);
273 virtual bool CopyFromIcon(const wxIcon& icon);
276 virtual void , SetHeight(int height),
277 "Set the height property (does not affect the existing bitmap data).", "");
281 virtual void , SetWidth(int width),
282 "Set the width property (does not affect the existing bitmap data).", "");
286 virtual void , SetDepth(int depth),
287 "Set the depth property (does not affect the existing bitmap data).", "");
291 DocStr(SetSize, "Set the bitmap size (does not affect the existing bitmap data).", "");
292 void SetSize(const wxSize& size) {
293 self->SetWidth(size.x);
294 self->SetHeight(size.y);
299 bool CopyFromCursor(const wxCursor& cursor);
301 // WXWIN_COMPATIBILITY_2_4
304 void SetQuality(int q);
305 %pythoncode { GetQuality = wx._deprecated(GetQuality) }
306 %pythoncode { SetQuality = wx._deprecated(SetQuality) }
310 %pythoncode { def __nonzero__(self): return self.Ok() }
313 bool __eq__(const wxBitmap* other) { return other ? (*self == *other) : false; }
314 bool __ne__(const wxBitmap* other) { return other ? (*self != *other) : true; }
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.
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.
329 #define wxPy_premultiply(p, a) ((p) * (a) / 256)
330 #define wxPy_unpremultiply(p, a) ((a) ? ((p) * 256 / (a)) : (p))
332 #define wxPy_premultiply(p, a) (p)
333 #define wxPy_unpremultiply(p, a) (p)
338 %newobject _BitmapFromBufferAlpha;
339 %newobject _BitmapFromBuffer;
341 wxBitmap* _BitmapFromBufferAlpha(int width, int height,
342 buffer data, int DATASIZE,
343 buffer alpha, int ALPHASIZE)
345 if (DATASIZE != width*height*3) {
346 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
350 if (ALPHASIZE != width*height) {
351 wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
355 wxBitmap* bmp = new wxBitmap(width, height, 32);
356 wxAlphaPixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height));
358 // raise an exception...
359 wxPyErr_SetString(PyExc_RuntimeError,
360 "Failed to gain raw access to bitmap data.");
365 wxAlphaPixelData::Iterator p(pixData);
366 for (int y=0; y<height; y++) {
367 wxAlphaPixelData::Iterator rowStart = p;
368 for (int x=0; x<width; x++) {
370 p.Red() = wxPy_premultiply(*(data++), a);
371 p.Green() = wxPy_premultiply(*(data++), a);
372 p.Blue() = wxPy_premultiply(*(data++), a);
377 p.OffsetY(pixData, 1);
382 wxBitmap* _BitmapFromBuffer(int width, int height, buffer data, int DATASIZE)
384 if (DATASIZE != width*height*3) {
385 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
389 wxBitmap* bmp = new wxBitmap(width, height, 24);
390 wxNativePixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height));
392 // raise an exception...
393 wxPyErr_SetString(PyExc_RuntimeError,
394 "Failed to gain raw access to bitmap data.");
398 wxNativePixelData::Iterator p(pixData);
399 for (int y=0; y<height; y++) {
400 wxNativePixelData::Iterator rowStart = p;
401 for (int x=0; x<width; x++) {
403 p.Green() = *(data++);
404 p.Blue() = *(data++);
408 p.OffsetY(pixData, 1);
416 def BitmapFromBuffer(width, height, dataBuffer, alphaBuffer=None):
418 Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer
419 parameter must be a Python object that implements the buffer
420 interface, such as a string, array, etc. The dataBuffer object is
421 expected to contain a series of RGB bytes and be width*height*3
422 bytes long. A buffer object can optionally be supplied for the
423 image's alpha channel data, and it is expected to be width*height
424 bytes long. On Windows the RGB values are 'premultiplied' by the
425 alpha values. (The other platforms do the multiplication
428 Unlike `wx.ImageFromBuffer` the bitmap created with this function
429 does not share the memory buffer with the buffer object. This is
430 because the native pixel buffer format varies on different
431 platforms, and so instead an efficient as possible copy of the
432 data is made from the buffer objects to the bitmap's native pixel
433 buffer. For direct access to a bitmap's pixel buffer see
434 `wx.NativePixelData` and `wx.AlphaPixelData`.
436 :see: `wx.Bitmap`, `wx.BitmapFromBufferRGBA`, `wx.NativePixelData`,
437 `wx.AlphaPixelData`, `wx.ImageFromBuffer`
439 if alphaBuffer is not None:
440 return _gdi_._BitmapFromBufferAlpha(width, height, dataBuffer, alphaBuffer)
442 return _gdi_._BitmapFromBuffer(width, height, dataBuffer)
447 %newobject _BitmapFromBufferRGBA;
449 wxBitmap* _BitmapFromBufferRGBA(int width, int height, buffer data, int DATASIZE)
451 if (DATASIZE != width*height*4) {
452 wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
456 wxBitmap* bmp = new wxBitmap(width, height, 32);
457 wxAlphaPixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height));
459 // raise an exception...
460 wxPyErr_SetString(PyExc_RuntimeError,
461 "Failed to gain raw access to bitmap data.");
466 wxAlphaPixelData::Iterator p(pixData);
467 for (int y=0; y<height; y++) {
468 wxAlphaPixelData::Iterator rowStart = p;
469 for (int x=0; x<width; x++) {
471 p.Red() = wxPy_premultiply(*(data++), a);
472 p.Green() = wxPy_premultiply(*(data++), a);
473 p.Blue() = wxPy_premultiply(*(data++), a);
474 p.Alpha() = a; data++;
478 p.OffsetY(pixData, 1);
485 def BitmapFromBufferRGBA(width, height, dataBuffer):
487 Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer
488 parameter must be a Python object that implements the buffer
489 interface, such as a string, array, etc. The dataBuffer object is
490 expected to contain a series of RGBA bytes (red, green, blue and
491 alpha) and be width*height*4 bytes long. On Windows the RGB
492 values are 'premultiplied' by the alpha values. (The other
493 platforms do the multiplication themselves.)
495 Unlike `wx.ImageFromBuffer` the bitmap created with this function
496 does not share the memory buffer with the buffer object. This is
497 because the native pixel buffer format varies on different
498 platforms, and so instead an efficient as possible copy of the
499 data is made from the buffer object to the bitmap's native pixel
500 buffer. For direct access to a bitmap's pixel buffer see
501 `wx.NativePixelData` and `wx.AlphaPixelData`.
503 :see: `wx.Bitmap`, `wx.BitmapFromBuffer`, `wx.NativePixelData`,
504 `wx.AlphaPixelData`, `wx.ImageFromBuffer`
506 return _gdi_._BitmapFromBufferRGBA(width, height, dataBuffer)
510 //---------------------------------------------------------------------------
512 class wxPixelDataBase
515 // origin of the rectangular region we represent
516 wxPoint GetOrigin() const { return m_ptOrigin; }
518 // width and height of the region we represent
519 int GetWidth() const { return m_width; }
520 int GetHeight() const { return m_height; }
522 wxSize GetSize() const { return wxSize(m_width, m_height); }
524 // the distance between two rows
525 int GetRowStride() const { return m_stride; }
530 // Both wxNativePixelData and wxAlphaPixelData have the same interface, so
531 // make a macro to declare them both.
533 %define PIXELDATA(PixelData)
535 typedef PixelData##::Iterator PixelData##_Accessor;
537 class PixelData##_Accessor;
538 class PixelData : public wxPixelDataBase
543 PixelData(wxBitmap& bmp);
544 PixelData(wxBitmap& bmp, const wxRect& rect);
545 PixelData(wxBitmap& bmp, const wxPoint& pt, const wxSize& sz);
549 PixelData##_Accessor GetPixels() const;
553 bool __nonzero__() { return self->operator bool(); }
558 """Create and return an iterator object for this pixel data object."""
559 return self.PixelIterator(self)
561 class PixelIterator(object):
563 Sequential iterator which returns pixel accessor objects starting at the
564 the top-left corner, and going row-by-row until the bottom-right
568 class PixelAccessor(object):
570 This class is what is returned by the iterator and allows the pixel
573 def __init__(self, data, pixels, x, y):
578 def Set(self, *args, **kw):
579 self.pixels.MoveTo(self.data, self.x, self.y)
580 return self.pixels.Set(*args, **kw)
582 self.pixels.MoveTo(self.data, self.x, self.y)
583 return self.pixels.Get()
585 def __init__(self, pixelData):
587 self.w = pixelData.GetWidth()
588 self.h = pixelData.GetHeight()
589 self.data = pixelData
590 self.pixels = pixelData.GetPixels()
598 p = self.PixelAccessor(self.data, self.pixels, self.x, self.y)
609 class PixelData##_Accessor
612 %nokwargs PixelData##_Accessor;
614 PixelData##_Accessor(PixelData& data);
615 PixelData##_Accessor(wxBitmap& bmp, PixelData& data);
616 PixelData##_Accessor();
618 ~PixelData##_Accessor();
620 void Reset(const PixelData& data);
624 // PixelData##_Accessor& nextPixel() { return ++(*self); }
625 void nextPixel() { ++(*self); }
628 void Offset(const PixelData& data, int x, int y);
629 void OffsetX(const PixelData& data, int x);
630 void OffsetY(const PixelData& data, int y);
631 void MoveTo(const PixelData& data, int x, int y);
633 // NOTE: For now I'm not wrapping the Red, Green, Blue and Alpha functions
634 // because I can't hide the premultiplying needed on wxMSW if only the
635 // individual components are wrapped. Instead I've added the Set and Get
636 // functions and put the premultiplying in there.
639 // byte _get_Red() { return self->Red(); }
640 // byte _get_Green() { return self->Green(); }
641 // byte _get_Blue() { return self->Blue(); }
643 // void _set_Red(byte val) { self->Red() = val; }
644 // void _set_Green(byte val) { self->Green() = val; }
645 // void _set_Blue(byte val) { self->Blue() = val; }
649 // Red = property(_get_Red, _set_Red)
650 // Green = property(_get_Green, _set_Green)
651 // Blue = property(_get_Blue, _set_Blue)
657 %pythonAppend wxAlphaPixelData::wxAlphaPixelData "self.UseAlpha()"
660 PIXELDATA(wxNativePixelData)
661 PIXELDATA(wxAlphaPixelData)
664 // Add in a few things that are different between the wxNativePixelData and
665 // wxAlphaPixelData and the iterator classes and so are not included in our
668 %extend wxNativePixelData_Accessor {
669 void Set(byte red, byte green, byte blue) {
671 self->Green() = green;
676 PyObject* rv = PyTuple_New(3);
677 PyTuple_SetItem(rv, 0, PyInt_FromLong(self->Red()));
678 PyTuple_SetItem(rv, 1, PyInt_FromLong(self->Green()));
679 PyTuple_SetItem(rv, 2, PyInt_FromLong(self->Blue()));
684 %extend wxAlphaPixelData_Accessor {
685 // byte _get_Alpha() { return self->Alpha(); }
686 // void _set_Alpha(byte val) { self->Alpha() = val; }
689 // Alpha = property(_get_Alpha, _set_Alpha)
692 void Set(byte red, byte green, byte blue, byte alpha) {
693 self->Red() = wxPy_premultiply(red, alpha);
694 self->Green() = wxPy_premultiply(green, alpha);
695 self->Blue() = wxPy_premultiply(blue, alpha);
696 self->Alpha() = alpha;
700 PyObject* rv = PyTuple_New(4);
701 int red = self->Red();
702 int green = self->Green();
703 int blue = self->Blue();
704 int alpha = self->Alpha();
706 PyTuple_SetItem(rv, 0, PyInt_FromLong( wxPy_unpremultiply(red, alpha) ));
707 PyTuple_SetItem(rv, 1, PyInt_FromLong( wxPy_unpremultiply(green, alpha) ));
708 PyTuple_SetItem(rv, 2, PyInt_FromLong( wxPy_unpremultiply(blue, alpha) ));
709 PyTuple_SetItem(rv, 3, PyInt_FromLong( alpha ));
715 //---------------------------------------------------------------------------
718 "This class encapsulates a monochrome mask bitmap, where the masked
719 area is black and the unmasked area is white. When associated with a
720 bitmap and drawn in a device context, the unmasked area of the bitmap
721 will be drawn, and the masked area will not be drawn.
723 A mask may be associated with a `wx.Bitmap`. It is used in
724 `wx.DC.DrawBitmap` or `wx.DC.Blit` when the source device context is a
725 `wx.MemoryDC` with a `wx.Bitmap` selected into it that contains a
730 class wxMask : public wxObject {
734 "Constructs a mask from a `wx.Bitmap` and a `wx.Colour` in that bitmap
735 that indicates the transparent portions of the mask. In other words,
736 the pixels in ``bitmap`` that match ``colour`` will be the transparent
737 portions of the mask. If no ``colour`` or an invalid ``colour`` is
738 passed then BLACK is used.
740 :see: `wx.Bitmap`, `wx.Colour`", "");
743 wxMask(const wxBitmap& bitmap, const wxColour& colour = wxNullColour) {
745 return new wxMask(bitmap, *wxBLACK);
747 return new wxMask(bitmap, colour);
754 %pythoncode { MaskColour = wx._deprecated(Mask, "wx.MaskColour is deprecated, use `wx.Mask` instead.") }
756 //---------------------------------------------------------------------------
757 //---------------------------------------------------------------------------