Move File-opening of PNG image into Png constructor (#2161)

Also rename `File::c_str` to `display_name` for clarity
This commit is contained in:
Rangi authored and GitHub committed 2026-09-27 13:19:05 -04:00
1 parent ac7d7edf4d
commit c0a16e4bad
6 files changed
+37 -22

No files matched your search

+1 -1
View File
@@ -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<std::filebuf>(_file) ? path.c_str()
: std::get<std::streambuf *>(_file) == std::cin.rdbuf() ? "<stdin>"
: "<stdout>";
+5
View File
@@ -5,6 +5,7 @@
#include <stdint.h>
#include <streambuf>
#include <string>
#include <vector>
#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
+1 -10
View File
@@ -5,7 +5,6 @@
#include "gfx/main.hpp"
#include <inttypes.h>
#include <ios>
#include <optional>
#include <png.h>
#include <stdarg.h>
@@ -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.
+13
View File
@@ -15,6 +15,7 @@
#include <vector>
#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);
+11 -7
View File
@@ -491,7 +491,11 @@ static void outputPalettes(std::vector<Palette> 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<AttrmapEntry> 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
}
+6 -4
View File
@@ -31,7 +31,7 @@
static std::vector<uint8_t> 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<uint8_t> 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<char *>(pngFile.c_str(options.input)),
const_cast<char *>(pngFile.display_name(options.input)),
pngError,
pngWarning
);