Implement warning diagnostic flags for RGBGFX (#1738)

This commit is contained in:
Rangi
2025-07-10 09:58:40 -04:00
committed by GitHub
parent 276a200590
commit 3f4e8396aa
14 changed files with 210 additions and 15 deletions

View File

@@ -52,7 +52,7 @@ void Options::verbosePrint(uint8_t level, char const *fmt, ...) const {
}
// Short options
static char const *optstring = "-Aa:B:b:Cc:d:hi:L:l:mN:n:Oo:Pp:Qq:r:s:Tt:U:uVvXx:YZ";
static char const *optstring = "-Aa:B:b:Cc:d:hi:L:l:mN:n:Oo:Pp:Qq:r:s:Tt:U:uVvW:wXx:YZ";
// Equivalent long options
// Please keep in the same order as short opts.
@@ -90,6 +90,7 @@ static option const longopts[] = {
{"unique-tiles", no_argument, nullptr, 'u'},
{"version", no_argument, nullptr, 'V'},
{"verbose", no_argument, nullptr, 'v'},
{"warning", required_argument, nullptr, 'W'},
{"mirror-x", no_argument, nullptr, 'X'},
{"trim-end", required_argument, nullptr, 'x'},
{"mirror-y", no_argument, nullptr, 'Y'},
@@ -558,6 +559,12 @@ static char *parseArgv(int argc, char *argv[]) {
}
break;
// LCOV_EXCL_STOP
case 'W':
warnings.processWarningFlag(musl_optarg);
break;
case 'w':
warnings.state.warningsEnabled = false;
break;
case 'x':
options.trim = parseNumber(arg, "Number of tiles to trim", 0);
if (*arg != '\0') {

View File

@@ -615,6 +615,10 @@ static std::tuple<std::vector<size_t>, std::vector<Palette>>
sortGrayscale(palettes, png.getColors().raw());
} else if (auto [embPalSize, embPalRGB, embPalAlphaSize, embPalAlpha] = png.getEmbeddedPal();
embPalRGB != nullptr) {
warning(
WARNING_EMBEDDED,
"Sorting palette colors by PNG's embedded PLTE chunk without '-c/--colors embedded'"
);
sortIndexed(palettes, embPalSize, embPalRGB, embPalAlphaSize, embPalAlpha);
} else if (png.isSuitableForGrayscale()) {
sortGrayscale(palettes, png.getColors().raw());
@@ -870,32 +874,41 @@ static void outputUnoptimizedTileData(
uint16_t widthTiles = options.inputSlice.width ? options.inputSlice.width : png.getWidth() / 8;
uint16_t heightTiles =
options.inputSlice.height ? options.inputSlice.height : png.getHeight() / 8;
uint64_t remainingTiles = widthTiles * heightTiles;
if (remainingTiles <= options.trim) {
return;
}
remainingTiles -= options.trim;
uint64_t nbTiles = widthTiles * heightTiles;
uint64_t nbKeptTiles = nbTiles > options.trim ? nbTiles - options.trim : 0;
uint64_t tileIdx = 0;
for (auto [tile, attr] : zip(png.visitAsTiles(), attrmap)) {
// Do not emit fully-background tiles.
if (!attr.isBackgroundTile()) {
// If the tile is fully transparent, this defaults to palette 0.
Palette const &palette = palettes[attr.getPalID(mappings)];
bool empty = true;
for (uint32_t y = 0; y < 8; ++y) {
uint16_t bitplanes = TileData::rowBitplanes(tile, palette, y);
output->sputc(bitplanes & 0xFF);
if (options.bitDepth == 2) {
output->sputc(bitplanes >> 8);
if (bitplanes != 0) {
empty = false;
}
if (tileIdx < nbKeptTiles) {
output->sputc(bitplanes & 0xFF);
if (options.bitDepth == 2) {
output->sputc(bitplanes >> 8);
}
}
}
}
--remainingTiles;
if (remainingTiles == 0) {
break;
if (!empty && tileIdx >= nbKeptTiles) {
warning(
WARNING_TRIM_NONEMPTY,
"Trimming a nonempty tile (configure with '-x/--trim-end'"
);
break; // Don't repeat the warning for subsequent tiles
}
}
++tileIdx;
}
assume(remainingTiles == 0);
assume(nbKeptTiles <= tileIdx && tileIdx <= nbTiles);
}
static void outputUnoptimizedMaps(

View File

@@ -10,6 +10,21 @@
static uintmax_t nbErrors;
// clang-format off: nested initializers
Diagnostics<WarningLevel, WarningID> warnings = {
.metaWarnings = {
{"all", LEVEL_ALL },
{"everything", LEVEL_EVERYTHING},
},
.warningFlags = {
{"embedded", LEVEL_EVERYTHING},
{"trim-nonempty", LEVEL_ALL },
},
.paramWarnings = {},
.state = DiagnosticsState<WarningID>(),
};
// clang-format on
[[noreturn]]
void giveUp() {
fprintf(stderr, "Conversion aborted after %ju error%s\n", nbErrors, nbErrors == 1 ? "" : "s");
@@ -50,3 +65,33 @@ void fatal(char const *fmt, ...) {
giveUp();
}
void warning(WarningID id, char const *fmt, ...) {
char const *flag = warnings.warningFlags[id].name;
va_list ap;
switch (warnings.getWarningBehavior(id)) {
case WarningBehavior::DISABLED:
break;
case WarningBehavior::ENABLED:
fprintf(stderr, "warning: [-W%s]\n ", flag);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
putc('\n', stderr);
break;
case WarningBehavior::ERROR:
fprintf(stderr, "error: [-Werror=%s]\n ", flag);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
putc('\n', stderr);
if (nbErrors != std::numeric_limits<decltype(nbErrors)>::max()) {
nbErrors++;
}
break;
}
}