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.
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.