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

Author Topic: Scale an image, what's wrong in this code?  (Read 1594 times)

0 Members and 1 Guest are viewing this topic.

Chrupiter

  • Newbie
  • *
  • Posts: 8
    • View Profile
Scale an image, what's wrong in this code?
« 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();

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Scale an image, what's wrong in this code?
« Reply #1 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?

Chrupiter

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Scale an image, what's wrong in this code?
« Reply #2 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

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: Scale an image, what's wrong in this code?
« Reply #3 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

 

anything