OpenTTD Source: src/palette.cpp Source File

OpenTTD Source 14.0-beta3

palette.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include "stdafx.h"
11 #include "blitter/base.hpp"
12 #include "blitter/factory.hpp"
13 #include "fileio_func.h"
14 #include "gfx_type.h"
15 #include "landscape_type.h"
16 #include "palette_func.h"
17 #include "settings_type.h"
18 #include "thread.h"
19
20 #include "table/palettes.h"
21
22 #include "safeguards.h"
23
24 Palette _cur_palette;
25
26 byte _colour_gradient[COLOUR_END][8];
27
28 static std::recursive_mutex _palette_mutex;
29
40 const uint PALETTE_BITS = 6;
41 const uint PALETTE_SHIFT = 8 - PALETTE_BITS;
42 const uint PALETTE_BITS_MASK = ((1U << PALETTE_BITS) - 1) << PALETTE_SHIFT;
43 const uint PALETTE_BITS_OR = (1U << (PALETTE_SHIFT - 1));
44
45 /* Palette and reshade lookup table. */
46 using PaletteLookup = std::array<uint8_t, 1U << (PALETTE_BITS * 3)>;
47 static PaletteLookup _palette_lookup{};
48
59 inline uint CrunchColour(uint c)
60 {
61 return (c & PALETTE_BITS_MASK) | PALETTE_BITS_OR;
62 }
63
72 static uint CalculateColourDistance(const Colour &col1, int r2, int g2, int b2)
73 {
74 /* Euclidean colour distance for sRGB based on https://en.wikipedia.org/wiki/Color_difference#sRGB */
75 int r = (int)col1.r - (int)r2;
76 int g = (int)col1.g - (int)g2;
77 int b = (int)col1.b - (int)b2;
78
79 int avgr = (col1.r + r2) / 2;
80 return ((2 + (avgr / 256.0)) * r * r) + (4 * g * g) + ((2 + ((255 - avgr) / 256.0)) * b * b);
81 }
82
83 /* Palette indexes for conversion. See docs/palettes/palette_key.png */
84 const uint8_t PALETTE_INDEX_CC_START = 198;
85 const uint8_t PALETTE_INDEX_CC_END = PALETTE_INDEX_CC_START + 8;
86 const uint8_t PALETTE_INDEX_START = 1;
87 const uint8_t PALETTE_INDEX_END = 215;
88
96 static uint8_t FindNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
97 {
98 r = CrunchColour(r);
99 g = CrunchColour(g);
100 b = CrunchColour(b);
101
102 uint best_index = 0;
103 uint best_distance = UINT32_MAX;
104
105 for (uint i = PALETTE_INDEX_START; i < PALETTE_INDEX_CC_START; i++) {
106 if (uint distance = CalculateColourDistance(_palette.palette[i], r, g, b); distance < best_distance) {
107 best_index = i;
108 best_distance = distance;
109 }
110 }
111 /* There's a hole in the palette reserved for company colour remaps. */
112 for (uint i = PALETTE_INDEX_CC_END; i < PALETTE_INDEX_END; i++) {
113 if (uint distance = CalculateColourDistance(_palette.palette[i], r, g, b); distance < best_distance) {
114 best_index = i;
115 best_distance = distance;
116 }
117 }
118 return best_index;
119 }
120
129 uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
130 {
131 uint32_t key = (r >> PALETTE_SHIFT) | (g >> PALETTE_SHIFT) << PALETTE_BITS | (b >> PALETTE_SHIFT) << (PALETTE_BITS * 2);
132 if (_palette_lookup[key] == 0) _palette_lookup[key] = FindNearestColourIndex(r, g, b);
133 return _palette_lookup[key];
134 }
135
136 void DoPaletteAnimations();
137
138 void GfxInitPalettes()
139 {
140 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
141 memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
142 DoPaletteAnimations();
143 }
144
154 bool CopyPalette(Palette &local_palette, bool force_copy)
155 {
156 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
157
158 if (!force_copy && _cur_palette.count_dirty == 0) return false;
159
160 local_palette = _cur_palette;
161 _cur_palette.count_dirty = 0;
162
163 if (force_copy) {
164 local_palette.first_dirty = 0;
165 local_palette.count_dirty = 256;
166 }
167
168 return true;
169 }
170
171 #define EXTR(p, q) (((uint16_t)(palette_animation_counter * (p)) * (q)) >> 16)
172 #define EXTR2(p, q) (((uint16_t)(~palette_animation_counter * (p)) * (q)) >> 16)
173
174 void DoPaletteAnimations()
175 {
176 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
177
178 /* Animation counter for the palette animation. */
179 static int palette_animation_counter = 0;
180 palette_animation_counter += 8;
181
182 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
183 const Colour *s;
184 const ExtraPaletteValues *ev = &_extra_palette_values;
185 Colour old_val[PALETTE_ANIM_SIZE];
186 const uint old_tc = palette_animation_counter;
187 uint j;
188
189 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
190 palette_animation_counter = 0;
191 }
192
193 Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START]; // Points to where animations are taking place on the palette
194 /* Makes a copy of the current animation palette in old_val,
195 * so the work on the current palette could be compared, see if there has been any changes */
196 memcpy(old_val, palette_pos, sizeof(old_val));
197
198 /* Fizzy Drink bubbles animation */
199 s = ev->fizzy_drink;
200 j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
201 for (uint i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
202 *palette_pos++ = s[j];
203 j++;
204 if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
205 }
206
207 /* Oil refinery fire animation */
208 s = ev->oil_refinery;
209 j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
210 for (uint i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
211 *palette_pos++ = s[j];
212 j++;
213 if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
214 }
215
216 /* Radio tower blinking */
217 {
218 byte i = (palette_animation_counter >> 1) & 0x7F;
219 byte v;
220
221 if (i < 0x3f) {
222 v = 255;
223 } else if (i < 0x4A || i >= 0x75) {
224 v = 128;
225 } else {
226 v = 20;
227 }
228 palette_pos->r = v;
229 palette_pos->g = 0;
230 palette_pos->b = 0;
231 palette_pos++;
232
233 i ^= 0x40;
234 if (i < 0x3f) {
235 v = 255;
236 } else if (i < 0x4A || i >= 0x75) {
237 v = 128;
238 } else {
239 v = 20;
240 }
241 palette_pos->r = v;
242 palette_pos->g = 0;
243 palette_pos->b = 0;
244 palette_pos++;
245 }
246
247 /* Handle lighthouse and stadium animation */
248 s = ev->lighthouse;
249 j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
250 for (uint i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
251 *palette_pos++ = s[j];
252 j++;
253 if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
254 }
255
256 /* Dark blue water */
257 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
258 j = EXTR(320, EPV_CYCLES_DARK_WATER);
259 for (uint i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
260 *palette_pos++ = s[j];
261 j++;
262 if (j == EPV_CYCLES_DARK_WATER) j = 0;
263 }
264
265 /* Glittery water */
266 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
267 j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
268 for (uint i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
269 *palette_pos++ = s[j];
270 j += 3;
271 if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
272 }
273
274 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
275 palette_animation_counter = old_tc;
276 } else if (_cur_palette.count_dirty == 0 && memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0) {
277 /* Did we changed anything on the palette? Seems so. Mark it as dirty */
278 _cur_palette.first_dirty = PALETTE_ANIM_START;
279 _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
280 }
281 }
282
289 TextColour GetContrastColour(uint8_t background, uint8_t threshold)
290 {
291 Colour c = _cur_palette.palette[background];
292 /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
293 * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
294 uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
295 /* Compare with threshold brightness which defaults to 128 (50%) */
296 return sq1000_brightness < ((uint) threshold) * ((uint) threshold) * 1000 ? TC_WHITE : TC_BLACK;
297 }
factory.hpp
Palette::first_dirty
int first_dirty
The first dirty element.
Definition: gfx_type.h:325
CrunchColour
uint CrunchColour(uint c)
Reduce bits per channel to PALETTE_BITS, and place value in the middle of the reduced range.
Definition: palette.cpp:59
ExtraPaletteValues
Description of tables for the palette animation.
Definition: palettes.h:104
landscape_type.h
palettes.h
GameCreationSettings::landscape
byte landscape
the landscape we're currently in
Definition: settings_type.h:356
PALETTE_ANIM_SIZE
@ PALETTE_ANIM_SIZE
number of animated colours
Definition: gfx_type.h:287
Blitter::UsePaletteAnimation
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
lock
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:35
Blitter
How all blitters should look like.
Definition: base.hpp:29
ExtraPaletteValues::lighthouse
Colour lighthouse[EPV_CYCLES_LIGHTHOUSE]
lighthouse & stadium
Definition: palettes.h:107
_palette
static const Palette _palette
Colour palette (DOS)
Definition: palettes.h:15
CopyPalette
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition: palette.cpp:154
PALETTE_BITS
const uint PALETTE_BITS
PALETTE_BITS reduces the bits-per-channel of 32bpp graphics data to allow faster palette lookups from...
Definition: palette.cpp:40
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:253
fileio_func.h
PALETTE_INDEX_CC_END
const uint8_t PALETTE_INDEX_CC_END
Palette index of end of company colour remap area.
Definition: palette.cpp:85
EPV_CYCLES_FIZZY_DRINK
static const uint EPV_CYCLES_FIZZY_DRINK
length of the fizzy drinks animation
Definition: palettes.h:100
_palette_mutex
static std::recursive_mutex _palette_mutex
To coordinate access to _cur_palette.
Definition: palette.cpp:28
palette_func.h
_colour_gradient
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: palette.cpp:26
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:619
CalculateColourDistance
static uint CalculateColourDistance(const Colour &col1, int r2, int g2, int b2)
Calculate distance between two colours.
Definition: palette.cpp:72
_extra_palette_values
static const ExtraPaletteValues _extra_palette_values
Actual palette animation tables.
Definition: palettes.h:115
ExtraPaletteValues::oil_refinery
Colour oil_refinery[EPV_CYCLES_OIL_REFINERY]
oil refinery
Definition: palettes.h:108
ExtraPaletteValues::glitter_water_toyland
Colour glitter_water_toyland[EPV_CYCLES_GLITTER_WATER]
glittery water Toyland
Definition: palettes.h:111
Palette::palette
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition: gfx_type.h:324
PALETTE_INDEX_START
const uint8_t PALETTE_INDEX_START
Palette index of start of defined palette.
Definition: palette.cpp:86
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:55
PALETTE_INDEX_END
const uint8_t PALETTE_INDEX_END
Palette index of end of defined palette.
Definition: palette.cpp:87
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:138
EPV_CYCLES_OIL_REFINERY
static const uint EPV_CYCLES_OIL_REFINERY
length of the oil refinery's fire animation
Definition: palettes.h:99
safeguards.h
settings_type.h
stdafx.h
FindNearestColourIndex
static uint8_t FindNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Find nearest colour palette index for a 32bpp pixel.
Definition: palette.cpp:96
Palette::count_dirty
int count_dirty
The number of dirty elements.
Definition: gfx_type.h:326
PALETTE_ANIM_START
@ PALETTE_ANIM_START
Index in the _palettes array from which all animations are taking places (table/palettes....
Definition: gfx_type.h:288
Colour
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:159
GetNearestColourIndex
uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Get nearest colour palette index from an RGB colour.
Definition: palette.cpp:129
EPV_CYCLES_LIGHTHOUSE
static const uint EPV_CYCLES_LIGHTHOUSE
length of the lighthouse/stadium animation
Definition: palettes.h:98
base.hpp
ExtraPaletteValues::dark_water
Colour dark_water[EPV_CYCLES_DARK_WATER]
dark blue water
Definition: palettes.h:105
PALETTE_INDEX_CC_START
const uint8_t PALETTE_INDEX_CC_START
Palette index of start of company colour remap area.
Definition: palette.cpp:84
Blitter::PALETTE_ANIMATION_NONE
@ PALETTE_ANIMATION_NONE
No palette animation.
Definition: base.hpp:51
_cur_palette
Palette _cur_palette
Current palette.
Definition: palette.cpp:24
EPV_CYCLES_DARK_WATER
static const uint EPV_CYCLES_DARK_WATER
Description of the length of the palette cycle animations.
Definition: palettes.h:97
ExtraPaletteValues::dark_water_toyland
Colour dark_water_toyland[EPV_CYCLES_DARK_WATER]
dark blue water Toyland
Definition: palettes.h:106
ExtraPaletteValues::fizzy_drink
Colour fizzy_drink[EPV_CYCLES_FIZZY_DRINK]
fizzy drinks
Definition: palettes.h:109
gfx_type.h
Palette
Information about the currently used palette.
Definition: gfx_type.h:323
EPV_CYCLES_GLITTER_WATER
static const uint EPV_CYCLES_GLITTER_WATER
length of the glittery water animation
Definition: palettes.h:101
thread.h
GetContrastColour
TextColour GetContrastColour(uint8_t background, uint8_t threshold)
Determine a contrasty text colour for a coloured background.
Definition: palette.cpp:289
ExtraPaletteValues::glitter_water
Colour glitter_water[EPV_CYCLES_GLITTER_WATER]
glittery water
Definition: palettes.h:110