the wxImage, How to load the RGBA Topic is solved

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.
Post Reply
lfjking
Earned some good credits
Earned some good credits
Posts: 102
Joined: Mon Nov 14, 2016 1:35 pm

the wxImage, How to load the RGBA

Post by lfjking »

The questions as this:

Code: Select all

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?

//Is there a quicker way?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: the wxImage, How to load the RGBA

Post by doublemax »

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.
Use the source, Luke!
lfjking
Earned some good credits
Earned some good credits
Posts: 102
Joined: Mon Nov 14, 2016 1:35 pm

Re: the wxImage, How to load the RGBA

Post by lfjking »

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?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: the wxImage, How to load the RGBA

Post by doublemax »

If you want to display RGBA data with wxWidgets, you'll always have to copy the data at least once.

As you need a wxBitmap to draw onto a wxDC, the fastest way is to use raw bitmap access to write directly into the bitmap:

Code: Select all

#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;
}
Use the source, Luke!
lfjking
Earned some good credits
Earned some good credits
Posts: 102
Joined: Mon Nov 14, 2016 1:35 pm

Re: the wxImage, How to load the RGBA

Post by lfjking »

doublemax wrote:If you want to display RGBA data with wxWidgets, you'll always have to copy the data at least once.

As you need a wxBitmap to draw onto a wxDC, the fastest way is to use raw bitmap access to write directly into the bitmap:

Code: Select all

#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;
}

This method is faster than mine!
thanks doublemax :D
Post Reply