diff --git a/include/wx/image.h b/include/wx/image.h index 80afae6ac3..bed5d53aba 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -506,6 +506,7 @@ public: unsigned char *GetData() const; void SetData( unsigned char *data, bool static_data=false ); void SetData( unsigned char *data, int new_width, int new_height, bool static_data=false ); + void SetDataRGBA(unsigned char* data); unsigned char *GetAlpha() const; // may return nullptr! bool HasAlpha() const { return GetAlpha() != nullptr; } diff --git a/interface/wx/image.h b/interface/wx/image.h index e1edb6debe..4997b623be 100644 --- a/interface/wx/image.h +++ b/interface/wx/image.h @@ -1771,6 +1771,20 @@ public: void SetData(unsigned char* data, int new_width, int new_height, bool static_data = false); + /** + Sets the image data without performing checks. + + The data given must have the size (width*height*4) or results will be + unexpected. Don't use this method if you aren't sure you know what you + are doing. + + Due to the internal data representation, wxImage will always create a + copy of the data. + + @since 3.3.0 + */ + void SetDataRGBA(unsigned char* data); + /** Sets the default value for the flags used for loading image files. diff --git a/src/common/image.cpp b/src/common/image.cpp index f65eb85a08..2ec2095942 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -2137,6 +2137,41 @@ void wxImage::SetData( unsigned char *data, int new_width, int new_height, bool m_refData = newRefData; } +void wxImage::SetDataRGBA(unsigned char* data) +{ + wxCHECK_RET(IsOk(), wxT("invalid image")); + + wxImageRefData* newRefData = new wxImageRefData(); + + newRefData->m_width = M_IMGDATA->m_width; + newRefData->m_height = M_IMGDATA->m_height; + + size_t pixel_count = (size_t)newRefData->m_width * (size_t)newRefData->m_height; + newRefData->m_data = (unsigned char*)malloc(3 * pixel_count); + newRefData->m_alpha = (unsigned char*)malloc(pixel_count); + + size_t rgba_index = 0, rgb_index = 0, alpha_index = 0; + for (size_t pixel_counter = 0; pixel_counter < pixel_count; pixel_counter++) + { + newRefData->m_data[rgb_index++] = data[rgba_index++]; // R + newRefData->m_data[rgb_index++] = data[rgba_index++]; // G + newRefData->m_data[rgb_index++] = data[rgba_index++]; // B + newRefData->m_alpha[alpha_index++] = data[rgba_index++]; // A + } + + newRefData->m_ok = true; + newRefData->m_maskRed = M_IMGDATA->m_maskRed; + newRefData->m_maskGreen = M_IMGDATA->m_maskGreen; + newRefData->m_maskBlue = M_IMGDATA->m_maskBlue; + newRefData->m_hasMask = M_IMGDATA->m_hasMask; + newRefData->m_static = false; + newRefData->m_staticAlpha = false; + + UnRef(); + + m_refData = newRefData; +} + // ---------------------------------------------------------------------------- // alpha channel support // ----------------------------------------------------------------------------