Simplify RGBGFX code by using fewer templates

This commit is contained in:
Rangi42
2025-09-20 21:06:51 -04:00
parent e7f5ab3f55
commit a82fd17529
2 changed files with 14 additions and 29 deletions

View File

@@ -32,8 +32,7 @@ using namespace std::string_view_literals;
static char const *hexDigits = "0123456789ABCDEFabcdef"; static char const *hexDigits = "0123456789ABCDEFabcdef";
template<typename Str> // Should be std::string or std::string_view static void skipBlankSpace(std::string_view const &str, size_t &pos) {
static void skipBlankSpace(Str const &str, size_t &pos) {
pos = std::min(str.find_first_not_of(" \t"sv, pos), str.length()); pos = std::min(str.find_first_not_of(" \t"sv, pos), str.length());
} }
@@ -45,6 +44,10 @@ static uint8_t singleToHex(char c) {
return toHex(c, c); return toHex(c, c);
} }
static uint16_t toWord(uint8_t low, uint8_t high) {
return high << 8 | low;
}
void parseInlinePalSpec(char const * const rawArg) { void parseInlinePalSpec(char const * const rawArg) {
// List of #rrggbb/#rgb colors (or #none); comma-separated. // List of #rrggbb/#rgb colors (or #none); comma-separated.
// Palettes are separated by colons. // Palettes are separated by colons.
@@ -163,24 +166,6 @@ void parseInlinePalSpec(char const * const rawArg) {
} }
} }
template<typename T, typename U>
static T readBE(U const *bytes) {
T val = 0;
for (size_t i = 0; i < sizeof(val); ++i) {
val = val << 8 | static_cast<uint8_t>(bytes[i]);
}
return val;
}
template<typename T, typename U>
static T readLE(U const *bytes) {
T val = 0;
for (size_t i = 0; i < sizeof(val); ++i) {
val |= static_cast<uint8_t>(bytes[i]) << (i * 8);
}
return val;
}
// Appends the first line read from `file` to the end of the provided `buffer`. // Appends the first line read from `file` to the end of the provided `buffer`.
// Returns true if a line was read. // Returns true if a line was read.
[[gnu::warn_unused_result]] [[gnu::warn_unused_result]]
@@ -423,7 +408,7 @@ static void parseACTFile(char const *filename, std::filebuf &file) {
uint16_t nbColors = 256; uint16_t nbColors = 256;
if (len == 772) { if (len == 772) {
nbColors = readBE<uint16_t>(&buf[768]); nbColors = toWord(buf[769], buf[768]);
if (nbColors > 256 || nbColors == 0) { if (nbColors > 256 || nbColors == 0) {
error("Invalid number of colors in ACT file \"%s\" (%" PRIu16 ")", filename, nbColors); error("Invalid number of colors in ACT file \"%s\" (%" PRIu16 ")", filename, nbColors);
return; return;
@@ -474,7 +459,7 @@ static void parseACOFile(char const *filename, std::filebuf &file) {
error("Failed to read ACO file version"); error("Failed to read ACO file version");
return; return;
} }
if (readBE<uint16_t>(buf) != 1) { if (toWord(buf[1], buf[0]) != 1) {
error("File \"%s\" is not a valid ACO v1 file", filename); error("File \"%s\" is not a valid ACO v1 file", filename);
return; return;
} }
@@ -483,7 +468,7 @@ static void parseACOFile(char const *filename, std::filebuf &file) {
error("Failed to read number of colors in palette file"); error("Failed to read number of colors in palette file");
return; return;
} }
uint16_t nbColors = readBE<uint16_t>(buf); uint16_t nbColors = toWord(buf[1], buf[0]);
if (uint16_t maxNbColors = options.maxNbColors(); nbColors > maxNbColors) { if (uint16_t maxNbColors = options.maxNbColors(); nbColors > maxNbColors) {
warnExtraColors("ACO", filename, nbColors, maxNbColors); warnExtraColors("ACO", filename, nbColors, maxNbColors);
@@ -503,7 +488,7 @@ static void parseACOFile(char const *filename, std::filebuf &file) {
} }
std::optional<Rgba> &color = options.palSpec.back()[i % options.nbColorsPerPal]; std::optional<Rgba> &color = options.palSpec.back()[i % options.nbColorsPerPal];
uint16_t colorType = readBE<uint16_t>(buf); uint16_t colorType = toWord(buf[1], buf[0]);
switch (colorType) { switch (colorType) {
case 0: // RGB case 0: // RGB
// Only keep the MSB of the (big-endian) 16-bit values. // Only keep the MSB of the (big-endian) 16-bit values.
@@ -549,10 +534,10 @@ static void parseGBCFile(char const *filename, std::filebuf &file) {
} }
options.palSpec.push_back({ options.palSpec.push_back({
Rgba::fromCGBColor(readLE<uint16_t>(&buf[0])), Rgba::fromCGBColor(toWord(buf[0], buf[1])),
Rgba::fromCGBColor(readLE<uint16_t>(&buf[2])), Rgba::fromCGBColor(toWord(buf[2], buf[3])),
Rgba::fromCGBColor(readLE<uint16_t>(&buf[4])), Rgba::fromCGBColor(toWord(buf[4], buf[5])),
Rgba::fromCGBColor(readLE<uint16_t>(&buf[6])), Rgba::fromCGBColor(toWord(buf[6], buf[7])),
}); });
} }
} }

View File

@@ -235,7 +235,7 @@ void reverse() {
palette.begin() + options.nbColorsPerPal, palette.begin() + options.nbColorsPerPal,
[&buf, i = 0]() mutable { [&buf, i = 0]() mutable {
i += 2; i += 2;
return Rgba::fromCGBColor(buf[i - 2] + (buf[i - 1] << 8)); return Rgba::fromCGBColor(buf[i - 2] | buf[i - 1] << 8); // little-endian
} }
); );
} }