Add wxImage::SetDataRGBA

This commit is contained in:
Maarten Bent
2025-05-10 17:18:30 +02:00
parent 2ec276affc
commit e040b0cd79
3 changed files with 50 additions and 0 deletions
+1
View File
@@ -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; }
+14
View File
@@ -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.
+35
View File
@@ -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
// ----------------------------------------------------------------------------