Porytiles
Loading...
Searching...
No Matches
png_rgba_image_saver.cpp
Go to the documentation of this file.
2
3#include <filesystem>
4#include <format>
5
6#include "png++/png.hpp"
7
11
12namespace porytiles {
13
14ChainableResult<void>
15PngRgbaImageSaver::save_to_file(const Image<Rgba32> &image, const std::filesystem::path &path) const
16{
17 const auto width = static_cast<png::uint_32>(image.width());
18 const auto height = static_cast<png::uint_32>(image.height());
19
20 png::image<png::rgba_pixel> png{width, height};
21
22 for (std::size_t row = 0; row < image.height(); ++row) {
23 for (std::size_t col = 0; col < image.width(); ++col) {
24 const auto pixel = image.at(row, col);
25 png[row][col] = png::rgba_pixel{pixel.red(), pixel.green(), pixel.blue(), pixel.alpha()};
26 }
27 }
28
29 try {
30 png.write(path.string());
31 }
32 catch (const std::exception &e) {
33 return FormattableError{std::format("{}: save failed: {}", path.filename().c_str(), e.what())};
34 }
35
36 return {};
37}
38
39} // namespace porytiles
General-purpose error implementation with formatted message support.
Definition error.hpp:63
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:23
std::size_t width() const
Definition image.hpp:114
std::size_t height() const
Definition image.hpp:119
PixelType at(std::size_t i) const
Fetches the pixel value at a given one-dimensional pixel index.
Definition image.hpp:56
virtual ChainableResult< void > save_to_file(const Image< Rgba32 > &image, const std::filesystem::path &path) const