wxBitmap from image array? Topic is solved
-
- Knows some wx things
- Posts: 39
- Joined: Fri Jun 27, 2008 9:11 am
wxBitmap from image array?
I am coming from another GUI Toolkit and am having a couple troubles adapting some of my code. In this instance I need to take an array of RGB data and draw with it to an off-screen DC.
From what I can tell I have to use wxMemoryDC and wxBitmap for this purpose but I am having trouble finding a method to dump my image array onto a wxBitmap.
How is this best accomplished? Am I using the wrong widget?
From what I can tell I have to use wxMemoryDC and wxBitmap for this purpose but I am having trouble finding a method to dump my image array onto a wxBitmap.
How is this best accomplished? Am I using the wrong widget?
there is no easy way for that in wxwidgets. The best way is to use raw bitmap access to transfer your rgba data to the bitmap pixel by pixel. (It's not properly documented, check the "image" sample and /wx/rawbmp.h for more info)
I blindly stripped together some code from one of my projects. Not sure if it compiles, but you should get the idea:
I blindly stripped together some code from one of my projects. Not sure if it compiles, but you should get the idea:
Code: Select all
#include <wx/rawbmp.h>
typedef wxAlphaPixelData PixelData;
wxBitmap *RGBAtoBitmap(unsigned char *rgba, int w, int h)
{
wxBitmap *bitmap=new wxBitmap(w, h, 32);
if(!bitmap->Ok()) {
delete bitmap;
return NULL;
}
PixelData bmdata(*bitmap);
if(bmdata==NULL) {
wxLogDebug(wxT("getBitmap() failed"));
delete bitmap;
return NULL;
}
bmdata.UseAlpha();
PixelData::Iterator dst(bmdata);
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];
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 bitmap;
}
Use the source, Luke!
-
- Knows some wx things
- Posts: 39
- Joined: Fri Jun 27, 2008 9:11 am
for historic reasons wxImage internally uses 24bit RGB pixels with additional alpha plane. You can only directly create a wxImage from your data if it's in exactly that format. However, you can't blit a wxImage directly into a DC, it would have been copied into a wxBitmap each time anyway. That would be less code to type, but would have bad runtime performance.
So this method is still the fastest and easiest AFAIK.
So this method is still the fastest and easiest AFAIK.
Use the source, Luke!
while this is 2 years old, I just used it and it works, and I have to say a GREAT thank you for pointing this out
I was trying using direct access to wxImage and then creating a wxBitmap. It was miserably crashing from memory access.
I also tried to use direct access to wxBufferedDC but it also failed from memory errors.
Whatever the case, thanks very much mate
I was trying using direct access to wxImage and then creating a wxBitmap. It was miserably crashing from memory access.
I also tried to use direct access to wxBufferedDC but it also failed from memory errors.
Whatever the case, thanks very much mate
Re: wxBitmap from image array?
Is there a way to get rgba values back from wxBitmap?
Re: wxBitmap from image array?
The easiest way is to convert it to a wxImage.gtafan wrote:Is there a way to get rgba values back from wxBitmap?
Otherwise, there is raw bitmap access:
http://docs.wxwidgets.org/trunk/classwx_pixel_data.html
Use the source, Luke!
Re: wxBitmap from image array?
Not shure what you mean with wxImage solution, but if it's what I think can't call it easy. I can only get rgb values from wxImage, the coresponding alpha values can be get in a separate array and I have to merge the rgb and alpha manualy. The fact rgb values stored just in char array maces it even more complikated.doublemax wrote:The easiest way is to convert it to a wxImage.gtafan wrote:Is there a way to get rgba values back from wxBitmap?
Otherwise, there is raw bitmap access:
http://docs.wxwidgets.org/trunk/classwx_pixel_data.html
Here what I would call an easy solution:
char* grtRgba()
Where the char array contain the rgba values.
Re: wxBitmap from image array?
It would be just a few lines of code to write this function yourself.Here what I would call an easy solution:
char* grtRgba()
Where the char array contain the rgba values.
Use the source, Luke!
Re: wxBitmap from image array?
Not really few lines, but that´s not the point, a single line is still easier then few ones. Since there is a way to initialize a bitmap with an array of rgba values, see no reason why there is no way to get that array back the same simple way.doublemax wrote:It would be just a few lines of code to write this function yourself.Here what I would call an easy solution:
char* grtRgba()
Where the char array contain the rgba values.
Re: wxBitmap from image array?
Are you referring to this?Since there is a way to initialize a bitmap with an array of rgba values
http://docs.wxwidgets.org/trunk/classwx ... c1de516f21
That hardly counts. It's totally platform specific and e.g. under Windows would expect data in BGRA order and with color values premultiplied with alpha. (Just a guess, i've actually never tried this method).
There is no method in wxWidgets that "officially" works with packed RGBA data.
Use the source, Luke!
Re: wxBitmap from image array?
BGRA is even better for me since I am worcking with textures. However this is an interesting fact, since it´s not mentioned in the documentation.doublemax wrote:Are you referring to this?Since there is a way to initialize a bitmap with an array of rgba values
http://docs.wxwidgets.org/trunk/classwx ... c1de516f21
That hardly counts. It's totally platform specific and e.g. under Windows would expect data in BGRA order and with color values premultiplied with alpha. (Just a guess, i've actually never tried this method).
There is no method in wxWidgets that "officially" works with packed RGBA data.