7#include "png++/png.hpp"
24 if (exists(path) && !is_regular_file(path)) {
25 return FormattableError{std::format(
"{}: exists but is not a file", path.filename().c_str())};
29 std::vector<Rgba32> palette_to_use;
31 palette_to_use = image.
palette().value();
35 palette_to_use = greyscale_pal;
39 png::palette png_pal{0};
40 for (
const auto &color : palette_to_use) {
41 png_pal.emplace_back(color.red(), color.green(), color.blue());
47 auto write_image = [&]<
typename PixelType>(png::image<PixelType> &img) {
48 img.set_palette(png_pal);
49 for (std::size_t pixel_index = 0; pixel_index < image.
size(); pixel_index++) {
50 const auto row = pixel_index / image.
width();
51 const auto col = pixel_index % image.
width();
52 if constexpr (std::is_same_v<PixelType, png::index_pixel_4>) {
54 img[row][col] = image.
at(pixel_index).color_index();
58 img[row][col] = image.
at(pixel_index).index();
67 if (palette_to_use.size() <= 16) {
68 png::image<png::index_pixel_4> out{image.
width(), image.
height()};
72 png::image<png::index_pixel> out{image.
width(), image.
height()};
76 catch (
const std::exception &e) {
77 return FormattableError{std::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
const std::optional< std::vector< Rgba32 > > & palette() const
std::size_t height() const
PixelType at(std::size_t i) const
Fetches the pixel value at a given one-dimensional pixel index.
virtual ChainableResult< void > save_to_file(const Image< IndexPixel > &image, const std::filesystem::path &path, TilesPalMode mode) const
TilesPalMode
Controls how tiles.png is rendered.
@ true_color
Render tiles with true colors from the palette.
std::vector< Rgba32 > standard_greyscale_pal()
Returns the standard 16-color greyscale palette used for indexed tile output.