Skip to content

Commit 51a22b3

Browse files
committed
Fix getting sprite main colour for some sprite types
1 parent a0999b0 commit 51a22b3

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/spritecache.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "scope_info.h"
2424
#include "spritecache.h"
2525
#include "spritecache_internal.h"
26+
#include "blitter/32bpp_base.hpp"
2627

2728
#include "table/sprites.h"
2829
#include "table/strings.h"
@@ -1062,10 +1063,21 @@ uint32_t GetSpriteMainColour(SpriteID sprite_id, PaletteID palette_id)
10621063
uint32_t r = 0, g = 0, b = 0, cnt = 0;
10631064
SpriteLoader::CommonPixel *pixel = sprite->data;
10641065
for (uint x = sprite->width * sprite->height; x != 0; x--) {
1065-
if (pixel->a) {
1066-
if (remap && pixel->m) {
1067-
const Colour c = _cur_palette.palette[remap[pixel->m]];
1068-
if (c.a) {
1066+
if (pixel->a != 0) {
1067+
if (pixel->m != 0) {
1068+
uint8_t m = pixel->m;
1069+
if (remap != nullptr) m = remap[m];
1070+
1071+
/* Get brightest value */
1072+
uint8_t rgb_max = std::max({pixel->r, pixel->g, pixel->b});
1073+
1074+
/* Black pixel (8bpp or old 32bpp image), so use default value */
1075+
if (rgb_max == 0) rgb_max = Blitter_32bppBase::DEFAULT_BRIGHTNESS;
1076+
1077+
/* Convert the mapping channel to a RGB value */
1078+
const Colour c = Blitter_32bppBase::AdjustBrightness(_cur_palette.palette[m], rgb_max);
1079+
1080+
if (c.a != 0) {
10691081
r += c.r;
10701082
g += c.g;
10711083
b += c.b;
@@ -1093,8 +1105,8 @@ uint32_t GetSpriteMainColour(SpriteID sprite_id, PaletteID palette_id)
10931105
/* Return the average colour. */
10941106
uint32_t r = 0, g = 0, b = 0, cnt = 0;
10951107
for (uint x = sprite->width * sprite->height; x != 0; x--) {
1096-
if (pixel->a) {
1097-
const uint col_index = remap ? remap[pixel->m] : pixel->m;
1108+
if (pixel->a != 0) {
1109+
const uint col_index = remap != nullptr ? remap[pixel->m] : pixel->m;
10981110
const Colour c = _cur_palette.palette[col_index];
10991111
r += c.r;
11001112
g += c.g;
@@ -1106,21 +1118,14 @@ uint32_t GetSpriteMainColour(SpriteID sprite_id, PaletteID palette_id)
11061118
return cnt ? Colour(r / cnt, g / cnt, b / cnt).data : 0;
11071119
} else {
11081120
/* Return the most used indexed colour. */
1109-
int cnt[256];
1110-
memset(cnt, 0, sizeof(cnt));
1121+
std::array<uint, 256> counts{};
11111122
for (uint x = sprite->width * sprite->height; x != 0; x--) {
1112-
cnt[remap ? remap[pixel->m] : pixel->m]++;
1113-
pixel++;
1114-
}
1115-
int cnt_max = -1;
1116-
uint32_t rk = 0;
1117-
for (uint x = 1; x < lengthof(cnt); x++) {
1118-
if (cnt[x] > cnt_max) {
1119-
rk = x;
1120-
cnt_max = cnt[x];
1123+
if (pixel->a != 0) {
1124+
counts[remap != nullptr ? remap[pixel->m] : pixel->m]++;
11211125
}
1126+
pixel++;
11221127
}
1123-
return rk;
1128+
return std::max_element(counts.begin(), counts.end()) - counts.begin();
11241129
}
11251130
}
11261131

0 commit comments

Comments
 (0)