7#include "png++/png.hpp"
21 const std::vector greyscale_pal = {
22 Rgba32{255, 255, 255, 255},
23 Rgba32{238, 238, 238, 255},
24 Rgba32{222, 222, 222, 255},
25 Rgba32{205, 205, 205, 255},
26 Rgba32{189, 189, 189, 255},
27 Rgba32{172, 172, 172, 255},
28 Rgba32{156, 156, 156, 255},
29 Rgba32{139, 139, 139, 255},
30 Rgba32{115, 115, 115, 255},
40 png::palette png_pal{0};
41 png::image<png::index_pixel> out{image.
width(), image.
height()};
44 if (exists(path) && !is_regular_file(path)) {
45 return FormattableError{fmt::format(
"{}: exists but is not a file", path.filename().c_str())};
49 std::vector<Rgba32> palette_to_use;
51 palette_to_use = image.
palette().value();
55 palette_to_use = greyscale_pal;
59 for (
const auto &color : palette_to_use) {
60 png_pal.emplace_back(color.red(), color.green(), color.blue());
62 out.set_palette(png_pal);
65 for (
unsigned int pixel_index = 0; pixel_index < image.
size(); pixel_index++) {
66 const auto row = pixel_index / image.
width();
67 const auto col = pixel_index % image.
width();
68 out[row][col] = image.
at(pixel_index).index();
75 catch (
const std::exception &e) {
76 return FormattableError{fmt::format(
"{}: save failed: {}", path.filename().c_str(), e.what())};
A result type that maintains a chainable sequence of errors for debugging and error reporting.
A template for two-dimensional images with arbitrarily typed pixel values.
std::size_t width() const
Gets the width of this image in pixels.
const std::optional< std::vector< Rgba32 > > & palette() const
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.
std::size_t size() const
Gets the size of this image in pixels.
virtual ChainableResult< void > save_to_file(const Image< IndexPixel > &image, const std::filesystem::path &path, TilesPalMode mode) const
Represents a 32-bit RGBA color.