SFML community forums

Help => Graphics => Topic started by: lefsler on November 25, 2014, 05:31:43 pm

Title: Resize texture and get pixel
Post by: lefsler on November 25, 2014, 05:31:43 pm
Hi,

I need to resize a image and get each pixel (after the image is resized)..
I do the resize using

Code: [Select]
Texture tmpTexture;
tmpTexture.loadFromFile(tmp.str());
               
Sprite tmpSprite(tmpTexture);
tmpSprite.setScale(width/tmpSprite.getLocalBounds().width, height/tmpSprite.getLocalBounds().height);

But after that is possible to get the pixel after the resize operation is performed?

Regards
Title: AW: Resize texture and get pixel
Post by: eXpl0it3r on November 25, 2014, 05:37:39 pm
There are multiple ways to go about this. What exactly are you going to do with the pixel information?
Title: Re: Resize texture and get pixel
Post by: lefsler on November 25, 2014, 05:40:28 pm
i will generate some data about the R, G, B to do another stuff. In the end i am using the SFML library to do another thing, in the end i will just need to output 3 values to a text file, with (R, G, B).
Title: AW: Resize texture and get pixel
Post by: eXpl0it3r on November 25, 2014, 05:51:43 pm
The thing is sf::Texture  "lives" on the GPU (https://github.com/SFML/SFML/wiki/FAQ#graphics-image-texture), thus the best way would be to write a shader program.
Otherwise you'll have to render the scaled sprite to a render texture and convert the extracted texture to an sf::Image to get to the pixel info. This can be slow.
Title: Re: Resize texture and get pixel
Post by: Jesper Juhl on November 25, 2014, 06:02:54 pm
If the idea is just to scale an image and then write it to a file in a new format, then I think I'd just use the convert tool from imagemagic and maybe a little python script for post-processing the text form.
Title: Re: AW: Resize texture and get pixel
Post by: lefsler on November 25, 2014, 06:51:01 pm
The thing is sf::Texture  "lives" on the GPU (https://github.com/SFML/SFML/wiki/FAQ#graphics-image-texture), thus the best way would be to write a shader program.
Otherwise you'll have to render the scaled sprite to a render texture and convert the extracted texture to an sf::Image to get to the pixel info. This can be slow.

How do i convert the RenderTexture to image or Texture to Image.

What i did was:

Code: [Select]
Texture tmpTexture;
tmpTexture.loadFromFile(tmp.str());
               
Sprite tmpSprite(tmpTexture);
tmpSprite.setScale(width/tmpSprite.getLocalBounds().width, height/tmpSprite.getLocalBounds().height);
               
sf::RenderTexture renderTexture;
renderTexture.create(width, height);
renderTexture.draw(tmpSprite);
               
Texture t=renderTexture.getTexture();
Image imageOut.... How do i convert?
Title: Re: Resize texture and get pixel
Post by: G. on November 25, 2014, 06:54:08 pm
There is only one method in the sf::Texture class (http://sfml-dev.org/documentation/2.1/classsf_1_1Texture.php) which returns an sf::Image, easy to find. :p
Title: Re: Resize texture and get pixel
Post by: lefsler on November 25, 2014, 06:58:44 pm
lol, thanks.

After i did that i try to save the image on a file (just to check the size) and i get.

JPEG parameter struct mismatch: library thinks size is 584, caller expects 568

What could be the problem?

The code is

Code: [Select]
Texture tmpTexture;
tmpTexture.loadFromFile(tmp.str());
Sprite tmpSprite(tmpTexture);
tmpSprite.setScale(width/tmpSprite.getLocalBounds().width, height/tmpSprite.getLocalBounds().height);
sf::RenderTexture renderTexture;
renderTexture.create(width, height);
renderTexture.draw(tmpSprite);
Texture t=renderTexture.getTexture();
Image imageOut =t.copyToImage();

tmp.str("");
tmp << "1" << entry->d_name;
imageOut.saveToFile(tmp.str());
cout << imageOut.getSize().x << endl;
cout << imageOut.getSize().y << endl;
tmp.str("");
Title: Re: Resize texture and get pixel
Post by: eXpl0it3r on November 25, 2014, 08:46:22 pm
Why does everybody always forget to call either clear() or display() when it comes to the render texture? ;D
For ever render target it's always clear, draw, display.

Googling the error message for like 1min told me, that your system most likely has a different version of libjpeg installed than the version SFML was built with. Are you on Linux?
Make sure the versions are the same.
Title: Re: Resize texture and get pixel
Post by: lefsler on November 25, 2014, 09:09:58 pm
Ohh, so to render the texture i need to add the

clear.
...draw here
display.

What version does SFML use of the libjpg?
Title: Re: Resize texture and get pixel
Post by: eXpl0it3r on November 25, 2014, 10:45:55 pm
Depends from where you got it and even more on with OS you are.