23
23
#include " scope_info.h"
24
24
#include " spritecache.h"
25
25
#include " spritecache_internal.h"
26
+ #include " blitter/32bpp_base.hpp"
26
27
27
28
#include " table/sprites.h"
28
29
#include " table/strings.h"
@@ -1062,10 +1063,21 @@ uint32_t GetSpriteMainColour(SpriteID sprite_id, PaletteID palette_id)
1062
1063
uint32_t r = 0 , g = 0 , b = 0 , cnt = 0 ;
1063
1064
SpriteLoader::CommonPixel *pixel = sprite->data ;
1064
1065
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 ) {
1069
1081
r += c.r ;
1070
1082
g += c.g ;
1071
1083
b += c.b ;
@@ -1093,8 +1105,8 @@ uint32_t GetSpriteMainColour(SpriteID sprite_id, PaletteID palette_id)
1093
1105
/* Return the average colour. */
1094
1106
uint32_t r = 0 , g = 0 , b = 0 , cnt = 0 ;
1095
1107
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 ;
1098
1110
const Colour c = _cur_palette.palette [col_index];
1099
1111
r += c.r ;
1100
1112
g += c.g ;
@@ -1106,21 +1118,14 @@ uint32_t GetSpriteMainColour(SpriteID sprite_id, PaletteID palette_id)
1106
1118
return cnt ? Colour (r / cnt, g / cnt, b / cnt).data : 0 ;
1107
1119
} else {
1108
1120
/* Return the most used indexed colour. */
1109
- int cnt[256 ];
1110
- memset (cnt, 0 , sizeof (cnt));
1121
+ std::array<uint, 256 > counts{};
1111
1122
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 ]++;
1121
1125
}
1126
+ pixel++;
1122
1127
}
1123
- return rk ;
1128
+ return std::max_element (counts. begin (), counts. end ()) - counts. begin () ;
1124
1129
}
1125
1130
}
1126
1131
0 commit comments