From c0a16e4bad7fe529c45d9f617b685215897be625 Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Sun, 27 Sep 2026 13:19:05 -0400 Subject: [PATCH] Move `File`-opening of PNG image into `Png` constructor (#2161) Also rename `File::c_str` to `display_name` for clarity --- include/file.hpp | 2 +- include/gfx/png.hpp | 5 +++++ src/gfx/main.cpp | 11 +---------- src/gfx/png.cpp | 13 +++++++++++++ src/gfx/process.cpp | 18 +++++++++++------- src/gfx/reverse.cpp | 10 ++++++---- 6 files changed, 37 insertions(+), 22 deletions(-) diff --git a/include/file.hpp b/include/file.hpp index 6c9dd583..d781464d 100644 --- a/include/file.hpp +++ b/include/file.hpp @@ -47,7 +47,7 @@ public: } std::streambuf *operator->() { return &**this; } - char const *c_str(std::string const &path) const { + char const *display_name(std::string const &path) const { return std::holds_alternative(_file) ? path.c_str() : std::get(_file) == std::cin.rdbuf() ? "" : ""; diff --git a/include/gfx/png.hpp b/include/gfx/png.hpp index 87f15db6..74268514 100644 --- a/include/gfx/png.hpp +++ b/include/gfx/png.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include "gfx/rgba.hpp" @@ -17,7 +18,11 @@ struct Png { bool isIndexed = false; Png() {} + Png(std::string const &path); Png(char const *filename, std::streambuf &file); + +private: + void initialize(char const *filename, std::streambuf &file); }; #endif // RGBDS_GFX_PNG_HPP diff --git a/src/gfx/main.cpp b/src/gfx/main.cpp index 38dfc388..1406632c 100644 --- a/src/gfx/main.cpp +++ b/src/gfx/main.cpp @@ -5,7 +5,6 @@ #include "gfx/main.hpp" #include -#include #include #include #include @@ -19,7 +18,6 @@ #include "cli.hpp" #include "diagnostics.hpp" -#include "file.hpp" #include "helpers.hpp" #include "style.hpp" // style_Parse #include "usage.hpp" @@ -681,14 +679,7 @@ int main(int argc, char *argv[]) { bool useInputImage = !options.input.empty() && !localOptions.reverse; Png png; if (useInputImage) { - if (File image; - image.open(options.input, std::ios_base::in | std::ios_base::binary) == nullptr) { - fatal( - "Failed to open PNG image (\"%s\"): %s", image.c_str(options.input), strerror(errno) - ); - } else { - png = Png(image.c_str(options.input), *image); - } + png = Png(options.input); } // Execute deferred pal spec parsing, now that all other params are known. diff --git a/src/gfx/png.cpp b/src/gfx/png.cpp index 739a74d8..31ac5495 100644 --- a/src/gfx/png.cpp +++ b/src/gfx/png.cpp @@ -15,6 +15,7 @@ #include #include "diagnostics.hpp" +#include "file.hpp" #include "helpers.hpp" #include "verbosity.hpp" @@ -61,7 +62,19 @@ static void readData(png_structp png, png_bytep data, size_t length) { } } +Png::Png(std::string const &path) { + if (File image; image.open(path, std::ios_base::in | std::ios_base::binary) == nullptr) { + fatal("Failed to open PNG image (\"%s\"): %s", image.display_name(path), strerror(errno)); + } else { + initialize(image.display_name(path), *image); + } +} + Png::Png(char const *filename, std::streambuf &file) { + initialize(filename, file); +} + +void Png::initialize(char const *filename, std::streambuf &file) { Input input(filename, file); verbosePrint(VERB_NOTICE, "Reading PNG file \"%s\"\n", input.filename); diff --git a/src/gfx/process.cpp b/src/gfx/process.cpp index 5cee407a..54154ac2 100644 --- a/src/gfx/process.cpp +++ b/src/gfx/process.cpp @@ -491,7 +491,11 @@ static void outputPalettes(std::vector const &palettes) { File output; if (!output.open(options.palettes, std::ios_base::out | std::ios_base::binary)) { // LCOV_EXCL_START - fatal("Failed to create \"%s\": %s", output.c_str(options.palettes), strerror(errno)); + fatal( + "Failed to create \"%s\": %s", + output.display_name(options.palettes), + strerror(errno) + ); // LCOV_EXCL_STOP } @@ -646,7 +650,7 @@ static void outputUnoptimizedTileData( File output; if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) { // LCOV_EXCL_START - fatal("Failed to create \"%s\": %s", output.c_str(options.output), strerror(errno)); + fatal("Failed to create \"%s\": %s", output.display_name(options.output), strerror(errno)); // LCOV_EXCL_STOP } @@ -699,7 +703,7 @@ static void outputUnoptimizedMaps( file.emplace(); if (!file->open(path, std::ios_base::out | std::ios_base::binary)) { // LCOV_EXCL_START - fatal("Failed to create \"%s\": %s", file->c_str(path), strerror(errno)); + fatal("Failed to create \"%s\": %s", file->display_name(path), strerror(errno)); // LCOV_EXCL_STOP } } @@ -873,7 +877,7 @@ static void outputTileData(UniqueTiles const &tiles) { File output; if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) { // LCOV_EXCL_START - fatal("Failed to create \"%s\": %s", output.c_str(options.output), strerror(errno)); + fatal("Failed to create \"%s\": %s", output.display_name(options.output), strerror(errno)); // LCOV_EXCL_STOP } @@ -913,7 +917,7 @@ static void outputTilemap(std::vector const &attrmap) { File output; if (!output.open(options.tilemap, std::ios_base::out | std::ios_base::binary)) { // LCOV_EXCL_START - fatal("Failed to create \"%s\": %s", output.c_str(options.tilemap), strerror(errno)); + fatal("Failed to create \"%s\": %s", output.display_name(options.tilemap), strerror(errno)); // LCOV_EXCL_STOP } @@ -929,7 +933,7 @@ static void File output; if (!output.open(options.attrmap, std::ios_base::out | std::ios_base::binary)) { // LCOV_EXCL_START - fatal("Failed to create \"%s\": %s", output.c_str(options.attrmap), strerror(errno)); + fatal("Failed to create \"%s\": %s", output.display_name(options.attrmap), strerror(errno)); // LCOV_EXCL_STOP } @@ -950,7 +954,7 @@ static void File output; if (!output.open(options.palmap, std::ios_base::out | std::ios_base::binary)) { // LCOV_EXCL_START - fatal("Failed to create \"%s\": %s", output.c_str(options.palmap), strerror(errno)); + fatal("Failed to create \"%s\": %s", output.display_name(options.palmap), strerror(errno)); // LCOV_EXCL_STOP } diff --git a/src/gfx/reverse.cpp b/src/gfx/reverse.cpp index da7dd3a7..362d2db7 100644 --- a/src/gfx/reverse.cpp +++ b/src/gfx/reverse.cpp @@ -31,7 +31,7 @@ static std::vector readInto(std::string const &path) { File file; if (!file.open(path, std::ios::in | std::ios::binary)) { - fatal("Failed to open \"%s\": %s", file.c_str(path), strerror(errno)); + fatal("Failed to open \"%s\": %s", file.display_name(path), strerror(errno)); } std::vector data(128 * 16); // Begin with some room pre-allocated @@ -207,7 +207,9 @@ void reverse() { if (!options.palettes.empty()) { File file; if (!file.open(options.palettes, std::ios::in | std::ios::binary)) { - fatal("Failed to open \"%s\": %s", file.c_str(options.palettes), strerror(errno)); + fatal( + "Failed to open \"%s\": %s", file.display_name(options.palettes), strerror(errno) + ); } palettes.clear(); @@ -478,12 +480,12 @@ void reverse() { File pngFile; if (!pngFile.open(options.input, std::ios::out | std::ios::binary)) { // LCOV_EXCL_START - fatal("Failed to create \"%s\": %s", pngFile.c_str(options.input), strerror(errno)); + fatal("Failed to create \"%s\": %s", pngFile.display_name(options.input), strerror(errno)); // LCOV_EXCL_STOP } png_structp png = png_create_write_struct( PNG_LIBPNG_VER_STRING, - const_cast(pngFile.c_str(options.input)), + const_cast(pngFile.display_name(options.input)), pngError, pngWarning );