From c89a3adfe1ee2111cc02acb4ddb4699d1a1fa0c2 Mon Sep 17 00:00:00 2001 From: MegaMech <7255464+MegaMech@users.noreply.github.com> Date: Sun, 27 Apr 2025 18:09:03 -0600 Subject: [PATCH] Fix custom minimap rendering --- .../resource/importers/MinimapFactory.cpp | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/port/resource/importers/MinimapFactory.cpp b/src/port/resource/importers/MinimapFactory.cpp index abc786da0..011789f67 100644 --- a/src/port/resource/importers/MinimapFactory.cpp +++ b/src/port/resource/importers/MinimapFactory.cpp @@ -15,20 +15,38 @@ ResourceFactoryBinaryMinimapV0::ReadResource(std::shared_ptr file, auto texture = std::make_shared(initData); auto reader = std::get>(file->Reader); - texture->Texture.Size = file->Buffer->size(); - texture->Texture.Width = 0; - texture->Texture.Height = 0; - texture->Texture.Channels = 4; // Always force 4 channels (RGBA) - texture->Texture.Data = stbi_load_from_memory(reinterpret_cast(file->Buffer->data()), texture->Texture.Size, - &texture->Texture.Width, &texture->Texture.Height, nullptr, 4); + int width = 0, height = 0; + stbi_uc* data = stbi_load_from_memory(reinterpret_cast(file->Buffer->data()), file->Buffer->size(), &width, &height, nullptr, 1); - if (nullptr == texture->Texture.Data) { + if (data == nullptr) { SPDLOG_ERROR("MinimapFactory.cpp: Error loading minimap texture {}", stbi_failure_reason()); return nullptr; } + // Calculate new size for 4bpp (two pixels per byte) + size_t pixelCount = width * height; + size_t packedSize = (pixelCount + 1) / 2; // Round up if odd - // stbi_image_free(imageData); + uint8_t* packedData = new uint8_t[packedSize]; + + for (size_t i = 0; i < pixelCount; i += 2) { + uint8_t first = data[i] >> 4; // 8-bit -> 4-bit + uint8_t second = 0; + if (i + 1 < pixelCount) { + second = data[i + 1] >> 4; + } + packedData[i / 2] = (first << 4) | second; + } + + // Done with original data + stbi_image_free(data); + + // Store packed data + texture->Texture.Width = width; + texture->Texture.Height = height; + texture->Texture.Channels = 1; + texture->Texture.Size = packedSize; + texture->Texture.Data = packedData; return texture; }