wxBitmap from image array? 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
thedevilsjester
Knows some wx things
Knows some wx things
Posts: 39
Joined: Fri Jun 27, 2008 9:11 am

wxBitmap from image array?

Post by thedevilsjester »

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

Post by doublemax »

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:

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!
thedevilsjester
Knows some wx things
Knows some wx things
Posts: 39
Joined: Fri Jun 27, 2008 9:11 am

Post by thedevilsjester »

Thats really the official method?

I notice that wxImage has a command that will do exactly what I want, only it expects (w*h*3) when my array is (w*h*4), is there any reason it doesnt have a (w*h*4) option?

Anyway, thank you for the example, it will be useful.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

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.
Use the source, Luke!
Pr0pheT
In need of some credit
In need of some credit
Posts: 1
Joined: Sun Dec 19, 2010 7:47 pm

Post by Pr0pheT »

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
gtafan
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 201
Joined: Wed Mar 29, 2017 9:52 am

Re: wxBitmap from image array?

Post by gtafan »

Is there a way to get rgba values back from wxBitmap?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxBitmap from image array?

Post by doublemax »

gtafan wrote:Is there a way to get rgba values back from wxBitmap?
The easiest way is to convert it to a wxImage.

Otherwise, there is raw bitmap access:
http://docs.wxwidgets.org/trunk/classwx_pixel_data.html
Use the source, Luke!
gtafan
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 201
Joined: Wed Mar 29, 2017 9:52 am

Re: wxBitmap from image array?

Post by gtafan »

doublemax wrote:
gtafan wrote:Is there a way to get rgba values back from wxBitmap?
The easiest way is to convert it to a wxImage.

Otherwise, there is raw bitmap access:
http://docs.wxwidgets.org/trunk/classwx_pixel_data.html
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.
Here what I would call an easy solution:
char* grtRgba()
Where the char array contain the rgba values.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxBitmap from image array?

Post by doublemax »

Here what I would call an easy solution:
char* grtRgba()
Where the char array contain the rgba values.
It would be just a few lines of code to write this function yourself.
Use the source, Luke!
gtafan
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 201
Joined: Wed Mar 29, 2017 9:52 am

Re: wxBitmap from image array?

Post by gtafan »

doublemax wrote:
Here what I would call an easy solution:
char* grtRgba()
Where the char array contain the rgba values.
It would be just a few lines of code to write this function yourself.
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.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxBitmap from image array?

Post by doublemax »

Since there is a way to initialize a bitmap with an array of rgba values
Are you referring to this?
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!
gtafan
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 201
Joined: Wed Mar 29, 2017 9:52 am

Re: wxBitmap from image array?

Post by gtafan »

doublemax wrote:
Since there is a way to initialize a bitmap with an array of rgba values
Are you referring to this?
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.
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.
Post Reply