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

Author Topic: The shortest possible way to resize my image?  (Read 2239 times)

0 Members and 1 Guest are viewing this topic.

frezario

  • Newbie
  • *
  • Posts: 1
    • View Profile
The shortest possible way to resize my image?
« on: July 10, 2020, 07:04:09 pm »
I found out that SFML doesnt offers embedded resize functions for images. Its so sad, because my goal is to write the function, which resizes images to '100x100' format and writes the rgb values of each pixel into txt file eventually. I started with this one code snippet:
void translate(const string& photo_file, const string& to_) {
        //Loading resourses    
        sf::Texture img;
        sf::Sprite sprite;
        sf::Image image;
        img.loadFromFile(photo_file);
        sprite.setTexture(img);
        ofstream output(to_, ios_base::app);
        assert(output.is_open());


        //Preparing the image to appropriate size
        sprite.setScale(100, 100);
        img = *sprite.getTexture();
        image = img.copyToImage();


        //Sending pixels to txt file
        //Red
        for (size_t x{ 0 }; x != 100; ++x) {
                for (size_t y{ 0 }; y != 100; ++y) {
                        auto color = image.getPixel(x, y);
                        output << color.r << " ";
                }
        }

        output << '\n';
        //Grean
        for (size_t x{ 0 }; x != 100; ++x) {
                for (size_t y{ 0 }; y != 100; ++y) {
                        auto color = image.getPixel(x, y);
                        output << color.g << " ";
                }
        }

        output << '\n';
        //Blue
        for (size_t x{ 0 }; x != 100; ++x) {
                for (size_t y{ 0 }; y != 100; ++y) {
                        auto color = image.getPixel(x, y);
                        output << color.b << " ";
                }
        }

        output << '\n';

        output.close();
}
 

Please, write me back if the code is correct and advise, please, a better way to implement my idea.
Thanks a lot.

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: The shortest possible way to resize my image?
« Reply #1 on: July 12, 2020, 11:00:32 pm »
You can skip or duplicate pixels to resize images but there are simpler or better ways.

A simpler way would be to create a render texture, draw the sprite onto it while using its scale to adjust size, and then transferring the render texture's texture to an image.

"Better" ways would involve manually calculations which take into account "sub-pixel" positions, whether that be - for example - by multiple samples or by first converting the pixels to rectangles.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: The shortest possible way to resize my image?
« Reply #2 on: July 15, 2020, 07:31:00 pm »
SFML doesn't provide resizing, because there is no single algorithm for all purposes. There are in fact loads of scaling algorithms, all with different trade-offs. If you need one of them, you might want to look for a specific library.

As Hapaxia said, if you don't care about image quality/information loss, simply render a sprite to a render-texture and then save its contents. Don't forget to call display().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: The shortest possible way to resize my image?
« Reply #3 on: July 16, 2020, 06:36:08 am »
I found out that SFML doesnt offers embedded resize functions for images. Its so sad, because my goal is to write the function, which resizes images to '100x100' format and writes the rgb values of each pixel into txt file eventually. I started with this one code snippet:
Please, write me back if the code is correct and advise, please, a better way to implement my idea.
Thanks a lot.

Like this?

void resizeImage(const sf::Image& originalImage, sf::Image& resizedImage) {
        const sf::Vector2u originalImageSize{ originalImage.getSize() };
        const sf::Vector2u resizedImageSize{ resizedImage.getSize() };
        for (unsigned int y{ 0u }; y < resizedImageSize.y; ++y) {
                for (unsigned int x{ 0u }; x < resizedImageSize.x; ++x) {
                        unsigned int origX{ static_cast<unsigned int>(static_cast<double>(x) / resizedImageSize.x * originalImageSize.x) };
                        unsigned int origY{ static_cast<unsigned int>(static_cast<double>(y) / resizedImageSize.y * originalImageSize.y) };
                        resizedImage.setPixel(x, y, originalImage.getPixel(origX, origY));
                }
        }
}
 

I created a program to take a folder of pictures and resize them to 480x320 and put a "stamp" on them all. I used that function and used Filesystem to set up a directory to read? That function works great.
« Last Edit: July 16, 2020, 06:37:47 am by SFMLNewGuy »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
Re: The shortest possible way to resize my image?
« Reply #4 on: July 23, 2020, 08:05:29 am »
You can save yourself a lot of time by just using a tool like XnView of IrfanView which both have batch processing capabilities for images. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Paul

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: The shortest possible way to resize my image?
« Reply #5 on: July 26, 2020, 09:40:02 pm »
If you want make thumbnails on the fly then you can't use an external program. It's best to use a special library to manipulate bitmaps, converting sf::Image to another format and back should not be complicated.