Fix two RGBGFX bugs (#1671)

* Fix two RGBGFX bugs

* Fix clang-format idempotence

* Update src/gfx/rgba.cpp

Co-authored-by: Eldred Habert <me@eldred.fr>

---------

Co-authored-by: Eldred Habert <me@eldred.fr>
This commit is contained in:
Rangi
2025-04-24 09:39:14 -04:00
committed by GitHub
parent a72843748f
commit 2a5b9b5f98
19 changed files with 14 additions and 10 deletions

View File

@@ -231,8 +231,7 @@ static std::optional<U> parseDec(std::string const &str, size_t &n) {
return std::optional<U>{value}; return std::optional<U>{value};
} }
static std::optional<Rgba> static std::optional<Rgba> parseColor(std::string const &str, size_t &n, uint16_t i) {
parseColor(std::string const &str, size_t &n, uint16_t i) {
std::optional<uint8_t> r = parseDec<uint8_t>(str, n); std::optional<uint8_t> r = parseDec<uint8_t>(str, n);
if (!r) { if (!r) {
error("Failed to parse color #%d (\"%s\"): invalid red component", i + 1, str.c_str()); error("Failed to parse color #%d (\"%s\"): invalid red component", i + 1, str.c_str());
@@ -602,10 +601,9 @@ void parseExternalPalSpec(char const *arg) {
std::tuple{"GBC", &parseGBCFile, std::ios::binary}, std::tuple{"GBC", &parseGBCFile, std::ios::binary},
}; };
auto iter = auto iter = std::find_if(RANGE(parsers), [&arg, &ptr](auto const &parser) {
std::find_if(RANGE(parsers), [&arg, &ptr](auto const &parser) { return strncasecmp(arg, std::get<0>(parser), ptr - arg) == 0;
return strncasecmp(arg, std::get<0>(parser), ptr - arg) == 0; });
});
if (iter == parsers.end()) { if (iter == parsers.end()) {
error( error(
"Unknown external palette format \"%.*s\"", "Unknown external palette format \"%.*s\"",

View File

@@ -604,9 +604,9 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
// Convert the palette spec to actual palettes // Convert the palette spec to actual palettes
std::vector<Palette> palettes(options.palSpec.size()); std::vector<Palette> palettes(options.palSpec.size());
for (auto [spec, pal] : zip(options.palSpec, palettes)) { for (auto [spec, pal] : zip(options.palSpec, palettes)) {
for (size_t i = 0; i < options.nbColorsPerPal && (!spec[i] || spec[i]->isOpaque()); ++i) { for (size_t i = 0; i < options.nbColorsPerPal; ++i) {
// If the spec has a gap, there's no need to copy anything. // If the spec has a gap, there's no need to copy anything.
if (spec[i]) { if (spec[i].has_value() && !spec[i]->isTransparent()) {
pal[i] = spec[i]->cgbColor(); pal[i] = spec[i]->cgbColor();
} }
} }

View File

@@ -58,6 +58,7 @@ uint16_t Rgba::cgbColor() const {
uint8_t Rgba::grayIndex() const { uint8_t Rgba::grayIndex() const {
assume(isGray()); assume(isGray());
// Convert from [0; 256[ to [0; maxOpaqueColors[ // Convert from 0..<256 to hasTransparentPixels..<nbColorsPerPal
return static_cast<uint16_t>(255 - red) * options.maxOpaqueColors() / 256; // Note that `maxOpaqueColors()` already takes `hasTransparentPixels` into account
return (255 - red) * options.maxOpaqueColors() / 256 + options.hasTransparentPixels;
} }

View File

@@ -0,0 +1,4 @@
error: Failed to fit tile colors [$1527, $15cc, $1ab4] in specified palettes
note: The following palette was specified:
[$1ab4, $15cc]
Conversion aborted after 1 error

View File

@@ -0,0 +1 @@
-c embedded

BIN
test/gfx/alpha_embedded.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 279 B

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 B

BIN
test/gfx/alpha_rgb.out.2bpp Normal file

Binary file not shown.

BIN
test/gfx/alpha_rgb.out.pal Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
test/gfx/alpha_rgb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 B

BIN
test/gfx/font_nums.out.2bpp Normal file

Binary file not shown.

BIN
test/gfx/font_nums.out.pal Normal file

Binary file not shown.

BIN
test/gfx/font_nums.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 B