SFML community forums

Help => General => Topic started by: Jallen on June 29, 2010, 04:30:13 pm

Title: Image::LoadFromPixels() - Don't understand.
Post by: Jallen on June 29, 2010, 04:30:13 pm
Hello. I'm accessing pixel data directly but just doing image.SetPixel() is too slow. I read a while back that it's faster to simply work on my own array and then use Image::LoadFromPixels().

The problem with this is I can't seem to figure out how to get my pixel data from my image (Image::GetPixelsPtr() looks like what I want but ???), into a workable array, and then back into my image...

So how do I do this?
I'm using SFML 1.6

Thanks.
Title: Image::LoadFromPixels() - Don't understand.
Post by: Laurent on June 29, 2010, 05:55:30 pm
Code: [Select]
sf::Image image;
// load/fill image...

// image --> your array
const sf::Uint8* ptr = image.GetPixelsPtr();
std::vector<sf::Uint8> pixels(ptr, ptr + image.GetWidth() * image.GetHeight() * 4);

// your array --> image
image.LoadFromPixels(image.GetWidth(), image.GetHeight(), &pixels[0]);
Title: Image::LoadFromPixels() - Don't understand.
Post by: Jallen on June 29, 2010, 06:01:29 pm
That's precisely what I needed, thanks :D
Title: Image::LoadFromPixels() - Don't understand.
Post by: Dominator on June 29, 2010, 10:08:20 pm
Hi!

How can I achieve this in DotNet?

The Image constructor only accepts a 2-dimensional array of type Color, but the property "pixels" returns a byte array.

Shouldn't there be an easier way without having to convert the array?

Thanks!
Title: Image::LoadFromPixels() - Don't understand.
Post by: Laurent on June 29, 2010, 10:50:13 pm
I don't know .Net a lot, so this is probably not the best solution.

So you think that a 1D array would be better? Or both 1D and 2D?
Title: Image::LoadFromPixels() - Don't understand.
Post by: Dominator on June 30, 2010, 12:18:38 pm
I can't say it for everyone but I for myself would prefer the 1D byte array.
In my opinion it's much easier to work with when sending over the network (the Image object is not serializable).

But pixel manipulation could probably be easier with the 2D array, no idea, I didn't need it yet.

Maybe make both possible 1D and 2D?
I for myself would be happy if the constructor accepted a byte array. :)

I think it could be a good idea to wait for other people's opinions. ;)
Title: Image::LoadFromPixels() - Don't understand.
Post by: Laurent on July 01, 2010, 08:24:38 pm
Quote
I think it could be a good idea to wait for other people's opinions.

I decided to add this new constructor
Code: [Select]
Image(uint width, uint height, byte[] pixels)
So now everyone will be happy ;)

It is available in SFML 2.