From 8cc7be498627cf9f79bd7ef1509b28fdb5fd4a10 Mon Sep 17 00:00:00 2001 From: Kp Date: Thu, 16 Jul 2020 02:31:04 +0000 Subject: [PATCH] Use std::bitset to track used colors This reduces memory usage over using std::array for the same number of bits. --- common/2d/bitmap.cpp | 4 ++-- common/2d/bitmap.h | 3 ++- similar/2d/font.cpp | 7 ++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/common/2d/bitmap.cpp b/common/2d/bitmap.cpp index 6ed8b2db7..ca8658252 100644 --- a/common/2d/bitmap.cpp +++ b/common/2d/bitmap.cpp @@ -137,7 +137,7 @@ void gr_init_sub_bitmap (grs_bitmap &bm, grs_bitmap &bmParent, uint16_t x, uint1 bm.bm_data = &bmParent.bm_data[static_cast((y*bmParent.bm_rowsize)+x)]; } -void decode_data(ubyte *data, uint_fast32_t num_pixels, std::array &colormap, std::array &used) +void decode_data(uint8_t *data, uint_fast32_t num_pixels, std::array &colormap, std::bitset<256> &used) { const auto a = [&](uint8_t mapped) { return used[mapped] = true, colormap[mapped]; @@ -169,7 +169,7 @@ void gr_remap_bitmap_good(grs_bitmap &bmp, palette_array_t &palette, uint_fast32 if (transparent_color < colormap.size()) colormap[transparent_color] = TRANSPARENCY_COLOR; - std::array freq{}; + std::bitset<256> freq{}; if (bmp.bm_w == bmp.bm_rowsize) decode_data(bmp.get_bitmap_data(), bmp.bm_w * bmp.bm_h, colormap, freq ); else { diff --git a/common/2d/bitmap.h b/common/2d/bitmap.h index bccc782af..bf94e7ada 100644 --- a/common/2d/bitmap.h +++ b/common/2d/bitmap.h @@ -11,10 +11,11 @@ #ifdef __cplusplus #include "palette.h" #include +#include namespace dcx { void build_colormap_good(const palette_array_t &palette, std::array &colormap); -void decode_data(ubyte *data, uint_fast32_t num_pixels, std::array &colormap, std::array &used); +void decode_data(uint8_t *data, uint_fast32_t num_pixels, std::array &colormap, std::bitset<256> &used); } #endif diff --git a/similar/2d/font.cpp b/similar/2d/font.cpp index 6e8c355ef..83b077069 100644 --- a/similar/2d/font.cpp +++ b/similar/2d/font.cpp @@ -1013,10 +1013,11 @@ static std::unique_ptr gr_internal_init_font(const char *fontname) std::array colormap; /* `freq` exists so that decode_data can write to it, but it is * otherwise unused. `decode_data` is not guaranteed to write - * to every element, so `freq` must not be read without first - * adding an initialization step. + * to every element, but the bitset constructor will initialize + * the storage, so reading unwritten fields will always return + * false. */ - std::array freq; + std::bitset<256> freq; PHYSFS_read(fontfile,&palette[0],sizeof(palette[0]),palette.size()); //read the palette