displaying an image with wxBitmap

341 views
Skip to first unread message

Johan "MrDutchy" van den Broek

unread,
Apr 11, 2005, 8:19:00 AM4/11/05
to wx-u...@lists.wxwidgets.org

i would like to display a texture i have created in memory using
wxBitmap (not read from disk).

From the docs i understood i need to do the following:
char rgb array -> wxImage -> wxBitmap -> Display

My source data is a size*size float based rgb array (named: colarr). So
first i convert it to an unsigned char based rgb array (named: d).
Then i create a wxImage and use SetData to make it point to the char array.

The problem is that this crashes: wxbmp = wxBitmap(wximg, -1); (access
violation)

wximg.Ok() returns true so it should be possible to convert it to a
wxBitmap and display it.

Any help is greatly appreciated :)

// -------- code ----------


void texture::tctextowximage()
{
d= (unsigned char *) malloc (size*size*channels);
// doesn't use new :)

int c=0;

for (unsigned int x=0; x<size; x++)
{
for (unsigned int y=0; y<size; y++)
{
// get float triplet (r,g,b)
unsigned char r=(unsigned char) (colarr [c++]*255);
unsigned char g=(unsigned char) (colarr [c++]*255);
unsigned char b=(unsigned char) (colarr [c++]*255);

unsigned char a;

// write float triplet (r,g,b)
*d++=r;
*d++=g;
*d++=b;

if (channels==TC_RGBA) // atm this is not the case
{
a=(unsigned char) (colarr [c++]*255);
*d++=a;
}

}
}
// request a size*size wximg
wximg=wxImage(size,size);

// make it point to the rgb data adressed by d
wximg.SetData(d);
}

void texture::wximagetowxbitmap()
{
initted=true;
wxbmp = wxBitmap(wximg, -1); // <<<--- this crashes
}

// -------------------

Gunnar Roth

unread,
Apr 11, 2005, 9:14:50 AM4/11/05
to wx-u...@lists.wxwidgets.org
Johan "MrDutchy" van den Broek schrieb:

> void texture::wximagetowxbitmap()
> {
> initted=true;
> wxbmp = wxBitmap(wximg, -1); // <<<--- this crashes
> }

So compile wxWidgets and your application in debug mode
and start debuggging. I personally have my glass bowl not at hand to
see whats happening there.
Come on, you have all the source...

regards,
gunnar


Gunnar Roth

unread,
Apr 11, 2005, 9:16:41 AM4/11/05
to wx-u...@lists.wxwidgets.org
I forgot to mention, you did not even specify,
which version and platform of wxWidgets.

regards,
gunnar


David Jones

unread,
Apr 11, 2005, 9:32:23 AM4/11/05
to wx-u...@lists.wxwidgets.org

On Apr 11, 2005, at 13:19, Johan "MrDutchy" van den Broek wrote:

>
> i would like to display a texture i have created in memory using
> wxBitmap (not read from disk).
>
> From the docs i understood i need to do the following:
> char rgb array -> wxImage -> wxBitmap -> Display
>
> My source data is a size*size float based rgb array (named: colarr).
> So first i convert it to an unsigned char based rgb array (named: d).
> Then i create a wxImage and use SetData to make it point to the char
> array.
>
> The problem is that this crashes: wxbmp = wxBitmap(wximg, -1); (access
> violation)
>
> wximg.Ok() returns true so it should be possible to convert it to a
> wxBitmap and display it.
>
> Any help is greatly appreciated :)

Don't increment d to the end of your malloc'ed array and then try and
use the same d to tell wxImage where your bytes are.

You need something like

p = d

Just after the malloc, and

wximg.SetData(p);

instead of wximg.SetData(d);

This is not really a wxWidgets problem.

Cheers,
drj


Johan "MrDutchy" van den Broek

unread,
Apr 11, 2005, 10:01:50 AM4/11/05
to wx-u...@lists.wxwidgets.org

> Don't increment d to the end of your malloc'ed array and then try and
> use the same d to tell wxImage where your bytes are.

That was indeed the cause of the violation, thank you for pointing that out.

> This is not really a wxWidgets problem.

I realize this now, i just overlooked my bug several times and
afterwards i assumed i'm just using the wxBitmap constructer in a wrong way.

Johan "MrDutchy" van den Broek

unread,
Apr 11, 2005, 10:08:45 AM4/11/05
to wx-u...@lists.wxwidgets.org

> So compile wxWidgets and your application in debug mode

It is true that I should have checked things myself a bit longer before
I asked here.

> and start debuggging. I personally have my glass bowl not at hand to
> see whats happening there.

Well the cause was exactly what David Jones posted and well it seems
very obvious to me now. A glass bowl is certainly not necessary to find it.

> Come on, you have all the source...

I'm just getting started. Please be more gentle ;)

About your plattform/wxw version note: I forgot to mention it, in future

questions i will be more precise.

Reply all
Reply to author
Forward
0 new messages