From a82fd1752994b0f7f945db9df142f957291145f4 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Sat, 20 Sep 2025 21:06:51 -0400 Subject: [PATCH] Simplify RGBGFX code by using fewer templates --- src/gfx/pal_spec.cpp | 41 +++++++++++++---------------------------- src/gfx/reverse.cpp | 2 +- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/src/gfx/pal_spec.cpp b/src/gfx/pal_spec.cpp index 534ac57c..21b1246c 100644 --- a/src/gfx/pal_spec.cpp +++ b/src/gfx/pal_spec.cpp @@ -32,8 +32,7 @@ using namespace std::string_view_literals; static char const *hexDigits = "0123456789ABCDEFabcdef"; -template // Should be std::string or std::string_view -static void skipBlankSpace(Str const &str, size_t &pos) { +static void skipBlankSpace(std::string_view const &str, size_t &pos) { 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); } +static uint16_t toWord(uint8_t low, uint8_t high) { + return high << 8 | low; +} + void parseInlinePalSpec(char const * const rawArg) { // List of #rrggbb/#rgb colors (or #none); comma-separated. // Palettes are separated by colons. @@ -163,24 +166,6 @@ void parseInlinePalSpec(char const * const rawArg) { } } -template -static T readBE(U const *bytes) { - T val = 0; - for (size_t i = 0; i < sizeof(val); ++i) { - val = val << 8 | static_cast(bytes[i]); - } - return val; -} - -template -static T readLE(U const *bytes) { - T val = 0; - for (size_t i = 0; i < sizeof(val); ++i) { - val |= static_cast(bytes[i]) << (i * 8); - } - return val; -} - // Appends the first line read from `file` to the end of the provided `buffer`. // Returns true if a line was read. [[gnu::warn_unused_result]] @@ -423,7 +408,7 @@ static void parseACTFile(char const *filename, std::filebuf &file) { uint16_t nbColors = 256; if (len == 772) { - nbColors = readBE(&buf[768]); + nbColors = toWord(buf[769], buf[768]); if (nbColors > 256 || nbColors == 0) { error("Invalid number of colors in ACT file \"%s\" (%" PRIu16 ")", filename, nbColors); return; @@ -474,7 +459,7 @@ static void parseACOFile(char const *filename, std::filebuf &file) { error("Failed to read ACO file version"); return; } - if (readBE(buf) != 1) { + if (toWord(buf[1], buf[0]) != 1) { error("File \"%s\" is not a valid ACO v1 file", filename); return; } @@ -483,7 +468,7 @@ static void parseACOFile(char const *filename, std::filebuf &file) { error("Failed to read number of colors in palette file"); return; } - uint16_t nbColors = readBE(buf); + uint16_t nbColors = toWord(buf[1], buf[0]); if (uint16_t maxNbColors = options.maxNbColors(); nbColors > maxNbColors) { warnExtraColors("ACO", filename, nbColors, maxNbColors); @@ -503,7 +488,7 @@ static void parseACOFile(char const *filename, std::filebuf &file) { } std::optional &color = options.palSpec.back()[i % options.nbColorsPerPal]; - uint16_t colorType = readBE(buf); + uint16_t colorType = toWord(buf[1], buf[0]); switch (colorType) { case 0: // RGB // 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({ - Rgba::fromCGBColor(readLE(&buf[0])), - Rgba::fromCGBColor(readLE(&buf[2])), - Rgba::fromCGBColor(readLE(&buf[4])), - Rgba::fromCGBColor(readLE(&buf[6])), + Rgba::fromCGBColor(toWord(buf[0], buf[1])), + Rgba::fromCGBColor(toWord(buf[2], buf[3])), + Rgba::fromCGBColor(toWord(buf[4], buf[5])), + Rgba::fromCGBColor(toWord(buf[6], buf[7])), }); } } diff --git a/src/gfx/reverse.cpp b/src/gfx/reverse.cpp index fed8d8fd..21489eef 100644 --- a/src/gfx/reverse.cpp +++ b/src/gfx/reverse.cpp @@ -235,7 +235,7 @@ void reverse() { palette.begin() + options.nbColorsPerPal, [&buf, i = 0]() mutable { i += 2; - return Rgba::fromCGBColor(buf[i - 2] + (buf[i - 1] << 8)); + return Rgba::fromCGBColor(buf[i - 2] | buf[i - 1] << 8); // little-endian } ); }