16using cimg_library::CImg;
17using cimg_library::CImgException;
22 const auto width =
static_cast<int>(image.
width());
23 const auto height =
static_cast<int>(image.
height());
24 constexpr auto spectrum = 4;
27 CImg<std::uint8_t> cimg_png(width, height, 1, spectrum);
29 for (std::size_t row = 0; row < image.
height(); ++row) {
30 for (std::size_t col = 0; col < image.
width(); ++col) {
31 const auto pixel = image.
at(row, col);
33 cimg_png(col, row, 0, 0) = pixel.red();
34 cimg_png(col, row, 0, 1) = pixel.green();
35 cimg_png(col, row, 0, 2) = pixel.blue();
36 cimg_png(col, row, 0, 3) = pixel.alpha();
40 const auto path_c_str = path.c_str();
42 std::ignore = cimg_png.save_png(path_c_str);
44 catch (
const std::exception &e) {
45 return FormattableError{fmt::format(
"{}: save failed: {}", path.filename().c_str(), e.what())};
A template for two-dimensional images with arbitrarily typed pixel values.
std::size_t width() const
Gets the width of this image in pixels.
PixelType at(std::size_t i) const
Fetches the pixel value at a given one-dimensional pixel index.
std::size_t height() const
Gets the height of this image in pixels.
virtual ChainableResult< void > save_to_file(const Image< Rgba32 > &image, const std::filesystem::path &path) const