SFML community forums

Help => Graphics => Topic started by: Chrupiter on May 11, 2015, 06:25:02 pm

Title: Scale an image, what's wrong in this code?
Post by: Chrupiter on May 11, 2015, 06:25:02 pm
So I want to scale an image, but the problem is that only sprites have a function for that.
I need to use the function getPixel from the resulting image, but I don't know how to convert the scaled sprite to an image.

I'd try something like this:

Code: [Select]
sf::Texture texture;
texture.loadFromImage(image);
sf::Sprite sprite;
sprite.setTexture(texture, true);
sprite.scale(0.5,0.5);
//backward
texture=sprite.getTexture(); //this isn't accepted by the compiler
image=texture.copyToImage();
Title: Re: Scale an image, what's wrong in this code?
Post by: shadowmouse on May 11, 2015, 06:26:48 pm
Why do you need to get a pixel from a scaled image, rather than just scaling the coordinates of where you are getting it from?
Title: Re: Scale an image, what's wrong in this code?
Post by: Chrupiter on May 11, 2015, 06:59:50 pm
I'll explain my situation. I've set a view(28x28) on a window(200x200).
In an image(28x28) I draw with the mouse.
Then I convert the image into a matrix of int, with 1 and 0(black and white).
What I fear is that by converting coordinates like this:
Code: [Select]
matrix[(int)posX*0.8][(int)posY*0.8]
I might lose too much quality
Title: Re: Scale an image, what's wrong in this code?
Post by: AFS on May 12, 2015, 07:54:05 pm
I don't think I fully understand your problem, but you could try drawing your sprite in a RenderTexture and then obtain the image.


// Create the RenderTexture.
sf::RenderTexture renderTexture;
renderTexture.create(200, 200);
renderTexture.clear(255, 255, 255, 255);

// Draw an scaled sprite on the RenderTexture.
sf::Sprite sprite;
sprite.setTexture(someTexture);
sprite.scale(0.5, 0.5);
renderTexture.draw(sprite);

// Obtain the image from the renderTexture
renterTexture.display();
sf::Image myImage = renderTexture.getTexture().copyToImage();

// Then you may save the image on a file.
myImage.saveToFile("image.png");
 

Documentation about RenderTexture: http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderTexture.php