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 » Wed Jul 02, 2008 8:38 pm

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: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Wed Jul 02, 2008 8:59 pm

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 » Wed Jul 02, 2008 9:33 pm

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: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Wed Jul 02, 2008 9:40 pm

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 » Mon Dec 20, 2010 7:36 am

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
Earned some good credits
Earned some good credits
Posts: 143
Joined: Wed Mar 29, 2017 9:52 am

Re: wxBitmap from image array?

Post by gtafan » Sat Feb 24, 2018 1:18 pm

Is there a way to get rgba values back from wxBitmap?

User avatar
doublemax
Moderator
Moderator
Posts: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxBitmap from image array?

Post by doublemax » Sat Feb 24, 2018 2:39 pm

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
Earned some good credits
Earned some good credits
Posts: 143
Joined: Wed Mar 29, 2017 9:52 am

Re: wxBitmap from image array?

Post by gtafan » Sun Feb 25, 2018 3:49 pm

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: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxBitmap from image array?

Post by doublemax » Sun Feb 25, 2018 4:26 pm

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
Earned some good credits
Earned some good credits
Posts: 143
Joined: Wed Mar 29, 2017 9:52 am

Re: wxBitmap from image array?

Post by gtafan » Mon Feb 26, 2018 12:49 pm

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: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxBitmap from image array?

Post by doublemax » Mon Feb 26, 2018 5:38 pm

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
Earned some good credits
Earned some good credits
Posts: 143
Joined: Wed Mar 29, 2017 9:52 am

Re: wxBitmap from image array?

Post by gtafan » Tue Feb 27, 2018 11:05 am

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