If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
struct RGBA{unsigned char* R,G,B,A};
//the Color Buffer
unsigned char* picData = new unsigned char[sizeof(RGBA) * width * height];
//now, how can I load the picData?
wxImage pic;
pic.loadfile(....); //??????????????????????????????????
//What I'm doing now is copy each R, G, B, to the pic.data..........but ,Is it too time-consuming?
Unfortunately, no. For "historic" reasons (when memory was sparse and precious), wxImage internally uses 24 Bit RGB pixels, with separate 8 bit alpha channel. So you'll have to copy the data.
doublemax wrote:Unfortunately, no. For "historic" reasons (when memory was sparse and precious), wxImage internally uses 24 Bit RGB pixels, with separate 8 bit alpha channel. So you'll have to copy the data.
thanks doublemax
may be, this one is only used.
opencv Built-in conversion, but it can't draw with in wxFrame. Do you have any suggestions?
#include <wx/rawbmp.h>
typedef wxAlphaPixelData PixelData;
// when creating the bitmap, use an explicit depth of 32!
bool CopyRGBAintoBitmap( wxBitmap &bitmap, unsigned char *rgba )
{
if( !bitmap.Ok() )
return false;
PixelData bmdata( bitmap );
if(bmdata == NULL)
return false;
PixelData::Iterator dst(bmdata);
// the size of the RGBA buffer must match the dimensions of the bitmap
const int w = bitmap.GetWidth();
const int h = bitmap.GetHeight();
for( int y = 0; y < h; y++)
{
dst.MoveTo(bmdata, 0, y);
for(int x = 0; x < w; x++)
{
// wxBitmap contains rgb values pre-multiplied with alpha
unsigned char a = rgba[3];
// you could use "/256" here to speed up the code,
// but at the price of being not 100% accurate
dst.Red() = rgba[0] * a / 255;
dst.Green() = rgba[1] * a / 255;
dst.Blue() = rgba[2] * a / 255;
dst.Alpha() = a;
dst++;
rgba += 4;
}
}
return true;
}
#include <wx/rawbmp.h>
typedef wxAlphaPixelData PixelData;
// when creating the bitmap, use an explicit depth of 32!
bool CopyRGBAintoBitmap( wxBitmap &bitmap, unsigned char *rgba )
{
if( !bitmap.Ok() )
return false;
PixelData bmdata( bitmap );
if(bmdata == NULL)
return false;
PixelData::Iterator dst(bmdata);
// the size of the RGBA buffer must match the dimensions of the bitmap
const int w = bitmap.GetWidth();
const int h = bitmap.GetHeight();
for( int y = 0; y < h; y++)
{
dst.MoveTo(bmdata, 0, y);
for(int x = 0; x < w; x++)
{
// wxBitmap contains rgb values pre-multiplied with alpha
unsigned char a = rgba[3];
// you could use "/256" here to speed up the code,
// but at the price of being not 100% accurate
dst.Red() = rgba[0] * a / 255;
dst.Green() = rgba[1] * a / 255;
dst.Blue() = rgba[2] * a / 255;
dst.Alpha() = a;
dst++;
rgba += 4;
}
}
return true;
}