From db6578d57c52fdd1c6274f5d3406a7b05d3f4178 Mon Sep 17 00:00:00 2001 From: Martin Michelsen Date: Thu, 16 Nov 2023 21:01:06 -0800 Subject: [PATCH] move color encoding functions to GVMEncoder header --- src/GVMEncoder.cc | 16 ---------------- src/GVMEncoder.hh | 37 +++++++++++++++++++++++++++++++++++++ src/ProxyCommands.cc | 9 +++------ 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/GVMEncoder.cc b/src/GVMEncoder.cc index 0012413f..396afe2d 100644 --- a/src/GVMEncoder.cc +++ b/src/GVMEncoder.cc @@ -8,22 +8,6 @@ using namespace std; -static uint16_t encode_rgb565(uint8_t r, uint8_t g, uint8_t b) { - return ((r << 8) & 0xF800) | ((g << 3) & 0x07E0) | ((b >> 3) & 0x001F); -} - -static uint16_t encode_rgb5a3(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { - if ((a & 0xE0) == 0xE0) { - return 0x8000 | ((r << 7) & 0x7C00) | ((g << 2) & 0x03E0) | ((b >> 3) & 0x001F); - } else { - return ((a << 7) & 0x7000) | ((r << 4) & 0x0F00) | (g & 0x00F0) | ((b >> 4) & 0x000F); - } -} - -static uint32_t encode_argb8888(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { - return (a << 24) | (r << 16) | (g << 8) | b; -} - struct GVMFileEntry { be_uint16_t file_num; pstring name; diff --git a/src/GVMEncoder.hh b/src/GVMEncoder.hh index 207a9e8e..4a0e3eb5 100644 --- a/src/GVMEncoder.hh +++ b/src/GVMEncoder.hh @@ -20,3 +20,40 @@ enum class GVRDataFormat : uint8_t { }; std::string encode_gvm(const Image& img, GVRDataFormat data_format); + +constexpr uint16_t encode_rgb565(uint8_t r, uint8_t g, uint8_t b) { + return ((r << 8) & 0xF800) | ((g << 3) & 0x07E0) | ((b >> 3) & 0x001F); +} + +constexpr uint16_t encode_rgb5a3(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { + if ((a & 0xE0) == 0xE0) { + return 0x8000 | ((r << 7) & 0x7C00) | ((g << 2) & 0x03E0) | ((b >> 3) & 0x001F); + } else { + return ((a << 7) & 0x7000) | ((r << 4) & 0x0F00) | (g & 0x00F0) | ((b >> 4) & 0x000F); + } +} + +constexpr uint32_t encode_argb8888(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { + return (a << 24) | (r << 16) | (g << 8) | b; +} + +constexpr uint16_t encode_xrgb8888_to_xrgb1555(uint32_t xrgb8888) { + // In: rrrrrrrrggggggggbbbbbbbbaaaaaaaa + // Out: -rrrrrgggggbbbbb + return ((xrgb8888 >> 9) & 0x7C00) | ((xrgb8888 >> 6) & 0x03E0) | ((xrgb8888 >> 3) & 0x001F); +} + +constexpr uint32_t encode_rgbx8888_to_xrgb1555(uint16_t rgbx8888) { + // In: rrrrrrrrggggggggbbbbbbbbxxxxxxxx + // Out: -rrrrrgggggbbbbb + return ((rgbx8888 >> 17) & 0x7C00) | ((rgbx8888 >> 14) & 0x03E0) | ((rgbx8888 >> 11) & 0x001F); +} + +constexpr uint32_t decode_xrgb1555_to_rgba8888(uint16_t xrgb1555) { + // In: -rrrrrgggggbbbbb + // Out: rrrrrrrrggggggggbbbbbbbbaaaaaaaa (a is always FF) + return ((xrgb1555 << 17) & 0xF8000000) | ((xrgb1555 << 12) & 0x07000000) | + ((xrgb1555 << 14) & 0x00F80000) | ((xrgb1555 << 9) & 0x00070000) | + ((xrgb1555 << 11) & 0x0000F800) | ((xrgb1555 << 6) & 0x00000700) | + 0x000000FF; +} diff --git a/src/ProxyCommands.cc b/src/ProxyCommands.cc index 16583719..90a6430a 100644 --- a/src/ProxyCommands.cc +++ b/src/ProxyCommands.cc @@ -28,6 +28,7 @@ #include "ChatCommands.hh" #include "Compression.hh" +#include "GVMEncoder.hh" #include "Loggers.hh" #include "PSOProtocol.hh" #include "ReceiveCommands.hh" @@ -36,10 +37,6 @@ using namespace std; -static constexpr uint16_t encode_xrgb1555(uint32_t xrgb8888) { - return ((xrgb8888 >> 9) & 0x7C00) | ((xrgb8888 >> 6) & 0x03E0) | ((xrgb8888 >> 3) & 0x001F); -} - struct HandlerResult { enum class Type { FORWARD = 0, @@ -1053,7 +1050,7 @@ static HandlerResult C_GXB_61(shared_ptr ses, uint16 modified = true; } if (!ses->challenge_rank_title_override.empty()) { - pd.records.challenge.title_color = encode_xrgb1555(ses->challenge_rank_color_override); + pd.records.challenge.title_color = encode_xrgb8888_to_xrgb1555(ses->challenge_rank_color_override); pd.records.challenge.rank_title.encode(ses->challenge_rank_title_override, ses->language()); } @@ -1090,7 +1087,7 @@ static HandlerResult C_GXB_61(shared_ptr ses, uint16 modified = true; } if (!ses->challenge_rank_title_override.empty()) { - pd->records.challenge.stats.title_color = encode_xrgb1555(ses->challenge_rank_color_override); + pd->records.challenge.stats.title_color = encode_xrgb8888_to_xrgb1555(ses->challenge_rank_color_override); pd->records.challenge.rank_title.encode(ses->challenge_rank_title_override, ses->language()); } }