Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Resize texture and get pixel  (Read 5084 times)

0 Members and 1 Guest are viewing this topic.

lefsler

  • Newbie
  • *
  • Posts: 11
    • View Profile
Resize texture and get pixel
« 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Resize texture and get pixel
« Reply #1 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lefsler

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Resize texture and get pixel
« Reply #2 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).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Resize texture and get pixel
« Reply #3 on: November 25, 2014, 05:51:43 pm »
The thing is sf::Texture "lives" on the GPU, 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Resize texture and get pixel
« Reply #4 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.

lefsler

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: AW: Resize texture and get pixel
« Reply #5 on: November 25, 2014, 06:51:01 pm »
The thing is sf::Texture "lives" on the GPU, 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?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Resize texture and get pixel
« Reply #6 on: November 25, 2014, 06:54:08 pm »
There is only one method in the sf::Texture class which returns an sf::Image, easy to find. :p

lefsler

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Resize texture and get pixel
« Reply #7 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("");

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Resize texture and get pixel
« Reply #8 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lefsler

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Resize texture and get pixel
« Reply #9 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Resize texture and get pixel
« Reply #10 on: November 25, 2014, 10:45:55 pm »
Depends from where you got it and even more on with OS you are.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/