1
General / 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:
Please, write me back if the code is correct and advise, please, a better way to implement my idea.
Thanks a lot.
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();
}
//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.