SFML community forums

Help => Graphics => Topic started by: Artfloriani on June 13, 2013, 03:54:26 am

Title: How to work with std::vector when using pixel array.
Post by: Artfloriani on June 13, 2013, 03:54:26 am
Hello,

I'm currently using a pixel array on my project. I want to use std::vector but werdly I can't.

For now I'm using:
sf::Uint8* pixels =  new sf::Uint8[sizeX * sizeY * 4];

sf::Image image;
image.create(sizeX, sizeY, pixels);
 

But if I try to change it to:
std::vector<sf::Uint8> pixels; //or even with sf::Uint8*
sf::Image image;
image.create(sizeX, sizeY, pixels);

My IDE gives me an error (before compiling) on image.create saying "no instance of overloaded function "sf::Image::create matches the argument list".

How do I properly create an image with a std::vector of pixels?

Thanks
Title: Re: How to work with std::vector when using pixel array.
Post by: eigenbom on June 13, 2013, 05:58:23 am
The function you are calling:

Code: [Select]
void Image::create (unsigned int width, unsigned int height, const Uint8 *pixels);
only accepts a pointer to the data, which is why your code doesn't compile. If you want to use std::vector, you'll either have to:
(a) copy all the data into an array, or
(b) try the following code

Code: [Select]
std::vector<sf::Uint8> pixels;
sf::Image image;
image.create(sizeX, sizeY, pixels.data());

Option (b) will only work with the very latest compilers, so I'd recommend option (a). i.e.,

Code: [Select]
std::vector<sf::Uint8> pixels;
// fill pixels...

sf::Uint8* dat = new sf::Uint8[pixels.size()];
std::copy(pixels.begin(), pixels.end(), dat); // copy them somehow..
sf::Image image;
image.create(sizeX, sizeY, dat);
delete[]dat;
Title: Re: How to work with std::vector when using pixel array.
Post by: MadMartin on June 13, 2013, 08:33:30 am
Try
std::vector<sf::Uint8> pixels; //or even with sf::Uint8*
sf::Image image;
image.create(sizeX, sizeY, &pixels[0]);
 
Title: Re: How to work with std::vector when using pixel array.
Post by: Laurent on June 13, 2013, 08:37:32 am
Quote
Option (b) will only work with the very latest compilers, so I'd recommend option (a). i.e.,
Wow, this is so wrong and unnecessary (see the other answer(s)) :P
Title: Re: How to work with std::vector when using pixel array.
Post by: Artfloriani on June 13, 2013, 02:21:51 pm
Thank you all  :)
Title: Re: How to work with std::vector when using pixel array.
Post by: eigenbom on June 14, 2013, 01:06:53 am
Quote
Option (b) will only work with the very latest compilers, so I'd recommend option (a). i.e.,
Wow, this is so wrong and unnecessary (see the other answer(s)) :P

I disagree, you shouldn't be afraid to copy your data between structures. If the OP had a std::list for example, this method is the only option, so it's good to know. :)
Title: Re: How to work with std::vector when using pixel array.
Post by: Laurent on June 14, 2013, 08:09:58 am
Quote
I disagree, you shouldn't be afraid to copy your data between structures. If the OP had a std::list for example, this method is the only option, so it's good to know.
But it's not a std::list. So it is wrong and misleading to give such an answer (and to add "most compilers don't support it" ???).
Title: Re: How to work with std::vector when using pixel array.
Post by: Nexus on June 14, 2013, 08:54:20 am
Also, it's completely unnecessary to work with new[] and delete[], because std::vector would do the job much better.
Title: Re: How to work with std::vector when using pixel array.
Post by: OniLinkPlus on June 14, 2013, 12:11:54 pm
Option (b) will only work with the very latest compilers, so I'd recommend option (a). i.e.,
That's just flat-out wrong. Based on vector's definition in the standard, it's safe and has always been safe (in any sane implementation of std::vector) to use &pixels[0] in place of data().
Title: Re: How to work with std::vector when using pixel array.
Post by: eigenbom on June 16, 2013, 05:00:49 am
Quote
I disagree, you shouldn't be afraid to copy your data between structures. If the OP had a std::list for example, this method is the only option, so it's good to know.
But it's not a std::list. So it is wrong and misleading to give such an answer (and to add "most compilers don't support it" ???).

It's just a more general answer, and it works. That doesn't fall into my definition of wrong.

As for support, it was a guess. I should have said more clearly that data() is a c++11 feature.

Knowledge that vector guarantees that things are contiguous requires a higher-level understanding, especially if you are learning about STL and the way it generically handles containers.