//-------------------------------------------------------------------------- // Name: src/bitmap_ex.h // Purpose: Helper functions and etc. for copying bitmap data to/from // buffer objects. This file is included in etg/bitmap.py and // used in the wxBitmap wrapper. // // Author: Robin Dunn // // Created: 27-Apr-2012 // Copyright: (c) 2012-2017 by Total Control Software // Licence: wxWindows license //-------------------------------------------------------------------------- #include // TODO: Switch these APIs to use the new wxPyBuffer class void wxPyCopyBitmapFromBuffer(wxBitmap* bmp, buffer data, Py_ssize_t DATASIZE, wxBitmapBufferFormat format, int stride) { int height = bmp->GetHeight(); int width = bmp->GetWidth(); switch (format) { // A simple sequence of RGB bytes case wxBitmapBufferFormat_RGB: { if (DATASIZE < width * height * 3) { wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); return; } wxNativePixelData pixData(*bmp, wxPoint(0,0), wxSize(width, height)); if (! pixData) { wxPyErr_SetString(PyExc_RuntimeError, "Failed to gain raw access to bitmap data."); return; } wxNativePixelData::Iterator p(pixData); for (int y=0; y> 24) & 0xFF : 255; pix.Red() = (value >> 16) & 0xFF; pix.Green() = (value >> 8) & 0xFF; pix.Blue() = (value >> 0) & 0xFF; ++pix; ++bufptr; } rowStart += stride; } break; } } } // Some helper macros used below to help declutter the code #define MAKE_PIXDATA(type) \ type pixData(*bmp, wxPoint(0,0), wxSize(width, height)); \ if (! pixData) { \ wxPyErr_SetString(PyExc_RuntimeError, "Failed to gain raw access to bitmap data."); \ return; \ } \ type::Iterator p(pixData); \ type::Iterator rowStart #define CHECK_BUFFERSIZE(size_needed) \ if (DATASIZE < size_needed) { \ wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); \ return; \ } void wxPyCopyBitmapToBuffer(wxBitmap* bmp, buffer data, Py_ssize_t DATASIZE, wxBitmapBufferFormat format, int stride) { int height = bmp->GetHeight(); int width = bmp->GetWidth(); int depth = bmp->GetDepth(); // images loaded from a file may not have set the depth, at least on Mac... if (depth == -1) { if (bmp->HasAlpha()) depth = 32; else depth = 24; } switch (format) { // A simple sequence of RGB bytes case wxBitmapBufferFormat_RGB: { CHECK_BUFFERSIZE(width * height * 3); if (depth == 24) { MAKE_PIXDATA(wxNativePixelData); for (int y=0; y