mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Mention palette filenames in rgbgfx -c error messages
This commit is contained in:
@@ -71,6 +71,8 @@ struct Options {
|
|||||||
mutable bool hasTransparentPixels = false;
|
mutable bool hasTransparentPixels = false;
|
||||||
uint8_t maxOpaqueColors() const { return nbColorsPerPal - hasTransparentPixels; }
|
uint8_t maxOpaqueColors() const { return nbColorsPerPal - hasTransparentPixels; }
|
||||||
|
|
||||||
|
uint16_t maxNbColors() const { return nbColorsPerPal * nbPalettes; }
|
||||||
|
|
||||||
uint8_t dmgColors[4] = {};
|
uint8_t dmgColors[4] = {};
|
||||||
uint8_t dmgValue(uint8_t i) const {
|
uint8_t dmgValue(uint8_t i) const {
|
||||||
assume(i < 4);
|
assume(i < 4);
|
||||||
|
|||||||
@@ -216,14 +216,27 @@ static bool readLine(std::filebuf &file, std::string &buffer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define requireLine(kind, file, buffer) \
|
#define requireLine(kind, filename, file, buffer) \
|
||||||
do { \
|
do { \
|
||||||
if (!readLine(file, buffer)) { \
|
if (!readLine(file, buffer)) { \
|
||||||
error(kind " palette file is shorter than expected"); \
|
error(kind " palette file \"%s\" is shorter than expected", filename); \
|
||||||
return; \
|
return; \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
static void warnExtraColors(
|
||||||
|
char const *kind, char const *filename, uint16_t nbColors, uint16_t maxNbColors
|
||||||
|
) {
|
||||||
|
warnx(
|
||||||
|
"%s file \"%s\" contains %" PRIu16 " colors, but there can only be %" PRIu16
|
||||||
|
"; ignoring extra",
|
||||||
|
kind,
|
||||||
|
filename,
|
||||||
|
nbColors,
|
||||||
|
maxNbColors
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Parses the initial part of a string_view, advancing the "read index" as it does
|
// Parses the initial part of a string_view, advancing the "read index" as it does
|
||||||
template<typename U> // Should be uint*_t
|
template<typename U> // Should be uint*_t
|
||||||
static std::optional<U> parseDec(std::string const &str, size_t &n) {
|
static std::optional<U> parseDec(std::string const &str, size_t &n) {
|
||||||
@@ -266,24 +279,24 @@ static std::optional<Rgba> parseColor(std::string const &str, size_t &n, uint16_
|
|||||||
return std::optional<Rgba>{Rgba(*r, *g, *b, 0xFF)};
|
return std::optional<Rgba>{Rgba(*r, *g, *b, 0xFF)};
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parsePSPFile(char const *, std::filebuf &file) {
|
static void parsePSPFile(char const *filename, std::filebuf &file) {
|
||||||
// https://www.selapa.net/swatches/colors/fileformats.php#psp_pal
|
// https://www.selapa.net/swatches/colors/fileformats.php#psp_pal
|
||||||
|
|
||||||
std::string line;
|
std::string line;
|
||||||
if (!readLine(file, line) || line != "JASC-PAL") {
|
if (!readLine(file, line) || line != "JASC-PAL") {
|
||||||
error("Palette file does not appear to be a PSP palette file");
|
error("File \"%s\" is not a valid PSP palette file", filename);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
line.clear();
|
line.clear();
|
||||||
requireLine("PSP", file, line);
|
requireLine("PSP", filename, file, line);
|
||||||
if (line != "0100") {
|
if (line != "0100") {
|
||||||
error("Unsupported PSP palette file version \"%s\"", line.c_str());
|
error("Unsupported PSP palette file version \"%s\"", line.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
line.clear();
|
line.clear();
|
||||||
requireLine("PSP", file, line);
|
requireLine("PSP", filename, file, line);
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
std::optional<uint16_t> nbColors = parseDec<uint16_t>(line, n);
|
std::optional<uint16_t> nbColors = parseDec<uint16_t>(line, n);
|
||||||
if (!nbColors || n != line.length()) {
|
if (!nbColors || n != line.length()) {
|
||||||
@@ -291,22 +304,16 @@ static void parsePSPFile(char const *, std::filebuf &file) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uint16_t nbPalColors = options.nbColorsPerPal * options.nbPalettes;
|
if (uint16_t maxNbColors = options.maxNbColors(); *nbColors > maxNbColors) {
|
||||||
*nbColors > nbPalColors) {
|
warnExtraColors("PSP", filename, *nbColors, maxNbColors);
|
||||||
warnx(
|
nbColors = maxNbColors;
|
||||||
"PSP file contains %" PRIu16 " colors, but there can only be %" PRIu16
|
|
||||||
"; ignoring extra",
|
|
||||||
*nbColors,
|
|
||||||
nbPalColors
|
|
||||||
);
|
|
||||||
nbColors = nbPalColors;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
options.palSpec.clear();
|
options.palSpec.clear();
|
||||||
|
|
||||||
for (uint16_t i = 0; i < *nbColors; ++i) {
|
for (uint16_t i = 0; i < *nbColors; ++i) {
|
||||||
line.clear();
|
line.clear();
|
||||||
requireLine("PSP", file, line);
|
requireLine("PSP", filename, file, line);
|
||||||
|
|
||||||
n = 0;
|
n = 0;
|
||||||
std::optional<Rgba> color = parseColor(line, n, i + 1);
|
std::optional<Rgba> color = parseColor(line, n, i + 1);
|
||||||
@@ -329,17 +336,17 @@ static void parsePSPFile(char const *, std::filebuf &file) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parseGPLFile(char const *, std::filebuf &file) {
|
static void parseGPLFile(char const *filename, std::filebuf &file) {
|
||||||
// https://gitlab.gnome.org/GNOME/gimp/-/blob/gimp-2-10/app/core/gimppalette-load.c#L39
|
// https://gitlab.gnome.org/GNOME/gimp/-/blob/gimp-2-10/app/core/gimppalette-load.c#L39
|
||||||
|
|
||||||
std::string line;
|
std::string line;
|
||||||
if (!readLine(file, line) || !line.starts_with("GIMP Palette")) {
|
if (!readLine(file, line) || !line.starts_with("GIMP Palette")) {
|
||||||
error("Palette file does not appear to be a GPL palette file");
|
error("File \"%s\" is not a valid GPL palette file", filename);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t nbColors = 0;
|
uint16_t nbColors = 0;
|
||||||
uint16_t const maxNbColors = options.nbColorsPerPal * options.nbPalettes;
|
uint16_t const maxNbColors = options.maxNbColors();
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
line.clear();
|
line.clear();
|
||||||
@@ -375,20 +382,15 @@ static void parseGPLFile(char const *, std::filebuf &file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (nbColors > maxNbColors) {
|
if (nbColors > maxNbColors) {
|
||||||
warnx(
|
warnExtraColors("GPL", filename, nbColors, maxNbColors);
|
||||||
"GPL file contains %" PRIu16 " colors, but there can only be %" PRIu16
|
|
||||||
"; ignoring extra",
|
|
||||||
nbColors,
|
|
||||||
maxNbColors
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parseHEXFile(char const *, std::filebuf &file) {
|
static void parseHEXFile(char const *filename, std::filebuf &file) {
|
||||||
// https://lospec.com/palette-list/tag/gbc
|
// https://lospec.com/palette-list/tag/gbc
|
||||||
|
|
||||||
uint16_t nbColors = 0;
|
uint16_t nbColors = 0;
|
||||||
uint16_t const maxNbColors = options.nbColorsPerPal * options.nbPalettes;
|
uint16_t const maxNbColors = options.maxNbColors();
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
std::string line;
|
std::string line;
|
||||||
@@ -422,16 +424,11 @@ static void parseHEXFile(char const *, std::filebuf &file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (nbColors > maxNbColors) {
|
if (nbColors > maxNbColors) {
|
||||||
warnx(
|
warnExtraColors("HEX", filename, nbColors, maxNbColors);
|
||||||
"HEX file contains %" PRIu16 " colors, but there can only be %" PRIu16
|
|
||||||
"; ignoring extra",
|
|
||||||
nbColors,
|
|
||||||
maxNbColors
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parseACTFile(char const *, std::filebuf &file) {
|
static void parseACTFile(char const *filename, std::filebuf &file) {
|
||||||
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577411_pgfId-1070626
|
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577411_pgfId-1070626
|
||||||
|
|
||||||
std::array<char, 772> buf{};
|
std::array<char, 772> buf{};
|
||||||
@@ -441,23 +438,21 @@ static void parseACTFile(char const *, std::filebuf &file) {
|
|||||||
if (len == 772) {
|
if (len == 772) {
|
||||||
nbColors = readBE<uint16_t>(&buf[768]);
|
nbColors = readBE<uint16_t>(&buf[768]);
|
||||||
if (nbColors > 256 || nbColors == 0) {
|
if (nbColors > 256 || nbColors == 0) {
|
||||||
error("Invalid number of colors in ACT file (%" PRIu16 ")", nbColors);
|
error("Invalid number of colors in ACT file \"%s\" (%" PRIu16 ")", filename, nbColors);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (len != 768) {
|
} else if (len != 768) {
|
||||||
error("Invalid file size for ACT file (expected 768 or 772 bytes, got %zu", len);
|
error(
|
||||||
|
"Invalid file size for ACT file \"%s\" (expected 768 or 772 bytes, got %zu)",
|
||||||
|
filename,
|
||||||
|
len
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uint16_t nbPalColors = options.nbColorsPerPal * options.nbPalettes;
|
if (uint16_t maxNbColors = options.maxNbColors(); nbColors > maxNbColors) {
|
||||||
nbColors > nbPalColors) {
|
warnExtraColors("ACT", filename, nbColors, maxNbColors);
|
||||||
warnx(
|
nbColors = maxNbColors;
|
||||||
"ACT file contains %" PRIu16 " colors, but there can only be %" PRIu16
|
|
||||||
"; ignoring extra",
|
|
||||||
nbColors,
|
|
||||||
nbPalColors
|
|
||||||
);
|
|
||||||
nbColors = nbPalColors;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
options.palSpec.clear();
|
options.palSpec.clear();
|
||||||
@@ -483,7 +478,7 @@ static void parseACTFile(char const *, std::filebuf &file) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parseACOFile(char const *, std::filebuf &file) {
|
static void parseACOFile(char const *filename, std::filebuf &file) {
|
||||||
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577411_pgfId-1055819
|
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577411_pgfId-1055819
|
||||||
|
|
||||||
char buf[10];
|
char buf[10];
|
||||||
@@ -493,7 +488,7 @@ static void parseACOFile(char const *, std::filebuf &file) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (readBE<uint16_t>(buf) != 1) {
|
if (readBE<uint16_t>(buf) != 1) {
|
||||||
error("Palette file does not appear to be an ACO v1 file");
|
error("File \"%s\" is not a valid ACO v1 file", filename);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -503,15 +498,9 @@ static void parseACOFile(char const *, std::filebuf &file) {
|
|||||||
}
|
}
|
||||||
uint16_t nbColors = readBE<uint16_t>(buf);
|
uint16_t nbColors = readBE<uint16_t>(buf);
|
||||||
|
|
||||||
if (uint16_t nbPalColors = options.nbColorsPerPal * options.nbPalettes;
|
if (uint16_t maxNbColors = options.maxNbColors(); nbColors > maxNbColors) {
|
||||||
nbColors > nbPalColors) {
|
warnExtraColors("ACO", filename, nbColors, maxNbColors);
|
||||||
warnx(
|
nbColors = maxNbColors;
|
||||||
"ACO file contains %" PRIu16 " colors, but there can only be %" PRIu16
|
|
||||||
"; ignoring extra",
|
|
||||||
nbColors,
|
|
||||||
nbPalColors
|
|
||||||
);
|
|
||||||
nbColors = nbPalColors;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
options.palSpec.clear();
|
options.palSpec.clear();
|
||||||
@@ -552,7 +541,7 @@ static void parseACOFile(char const *, std::filebuf &file) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parseGBCFile(char const *, std::filebuf &file) {
|
static void parseGBCFile(char const *filename, std::filebuf &file) {
|
||||||
// This only needs to be able to read back files generated by `rgbgfx -p`
|
// This only needs to be able to read back files generated by `rgbgfx -p`
|
||||||
options.palSpec.clear();
|
options.palSpec.clear();
|
||||||
|
|
||||||
@@ -562,7 +551,8 @@ static void parseGBCFile(char const *, std::filebuf &file) {
|
|||||||
break;
|
break;
|
||||||
} else if (len != sizeof(buf)) {
|
} else if (len != sizeof(buf)) {
|
||||||
error(
|
error(
|
||||||
"GBC palette dump contains %zu 8-byte palette%s, plus %zu byte%s",
|
"GBC palette file \"%s\" contains %zu 8-byte palette%s, plus %zu byte%s",
|
||||||
|
filename,
|
||||||
options.palSpec.size(),
|
options.palSpec.size(),
|
||||||
options.palSpec.size() == 1 ? "" : "s",
|
options.palSpec.size() == 1 ? "" : "s",
|
||||||
len,
|
len,
|
||||||
@@ -629,11 +619,8 @@ static void parsePNGFile(char const *filename, std::filebuf &file) {
|
|||||||
// More palettes than the maximum are a warning, not an error
|
// More palettes than the maximum are a warning, not an error
|
||||||
uint32_t nbPals = png.height / swatchSize;
|
uint32_t nbPals = png.height / swatchSize;
|
||||||
if (nbPals > options.nbPalettes) {
|
if (nbPals > options.nbPalettes) {
|
||||||
warnx(
|
warnExtraColors(
|
||||||
"PNG palette file contains %" PRIu32 " palette rows, but there can only be %" PRIu16
|
"PNG palette", filename, nbPals * options.nbColorsPerPal, options.maxNbColors()
|
||||||
"; ignoring extra",
|
|
||||||
nbPals,
|
|
||||||
options.nbPalettes
|
|
||||||
);
|
);
|
||||||
nbPals = options.nbPalettes;
|
nbPals = options.nbPalettes;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ Png::Png(char const *filename, std::streambuf &file) {
|
|||||||
if (input.file.sgetn(reinterpret_cast<char *>(pngHeader.data()), pngHeader.size())
|
if (input.file.sgetn(reinterpret_cast<char *>(pngHeader.data()), pngHeader.size())
|
||||||
!= static_cast<std::streamsize>(pngHeader.size()) // Not enough bytes?
|
!= static_cast<std::streamsize>(pngHeader.size()) // Not enough bytes?
|
||||||
|| png_sig_cmp(pngHeader.data(), 0, pngHeader.size()) != 0) {
|
|| png_sig_cmp(pngHeader.data(), 0, pngHeader.size()) != 0) {
|
||||||
fatal("PNG file (\"%s\") is not a valid PNG image!", input.filename); // LCOV_EXCL_LINE
|
fatal("File \"%s\" is not a valid PNG image", input.filename); // LCOV_EXCL_LINE
|
||||||
}
|
}
|
||||||
|
|
||||||
options.verbosePrint(Options::VERB_INTERM, "PNG header signature is OK\n");
|
options.verbosePrint(Options::VERB_INTERM, "PNG header signature is OK\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user