Page 1 of 1

copying unsigned char array?

Posted: Sat Mar 17, 2007 9:00 am
by jazz
Hey all. I'm trying to play around with wxImage alpha channel data and I seem to be a little stumped with how to handle unsigned char* 's properly.

Basically, wxImage::GetAlpha() returns a pointer to an unsigned char* array so I can directly manipulate the alpha channel. Before doing this though, I'd like to copy the array so I have an unmodified version that I can put back when I'm done.

So my question is, how can I copy an unsigned char* array? memcpy()? (which doesn't work). I'd also like to know how to get the length because strnlen() doesn't work either.

Anyone?

Posted: Sat Mar 17, 2007 9:38 am
by Martin
Just guessing here, but the length could be

wxImage::GetHeight() * wxImage::GetWidth()

I would try something like this:

Code: Select all

  unsigned char* alphaCopy = new unsigned char[length];
  memcpy(alphaCopy, wxImage::GetAlpha(), length);
How does your memcpy code look like?

Posted: Sat Mar 17, 2007 10:38 am
by jazz
Martin wrote:Just guessing here, but the length could be

wxImage::GetHeight() * wxImage::GetWidth()

I would try something like this:

Code: Select all

  unsigned char* alphaCopy = new unsigned char[length];
  memcpy(alphaCopy, wxImage::GetAlpha(), length);
How does your memcpy code look like?
Geez i can be such a n00b sometimes. I swear i tried that, except you just showed me that my syntax was incorrect. I was doing

Code: Select all

new unsigned char( length )
instead of

Code: Select all

new unsigned char[length]
which seems to cause a segfault.

Thanks heaps.