OpenTTD Source 20250205-master-gfd85ab1e2c
makeindexed.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 "../core/bitmath_func.hpp"
12#include "../core/math_func.hpp"
13#include "../gfx_func.h"
14#include "../palette_func.h"
15#include "makeindexed.h"
16
17#include "../safeguards.h"
18
24{
25 const auto *pixel_end = sprite.data + sprite.width * sprite.height;
26 for (auto *pixel = sprite.data; pixel != pixel_end; ++pixel) {
27 if (pixel->m != 0) {
28 /* Pixel has 8bpp mask, test if should be reshaded. */
29 uint8_t brightness = std::max({pixel->r, pixel->g, pixel->b});
30 if (brightness == 0 || brightness == 128) continue;
31
32 /* Update RGB component with reshaded palette colour, and enabled reshade. */
33 Colour c = AdjustBrightness(_cur_palette.palette[pixel->m], brightness);
34
35 if (IsInsideMM(pixel->m, 0xC6, 0xCE)) {
36 /* Dumb but simple brightness conversion. */
37 pixel->m = GetNearestColourReshadeIndex((c.r + c.g + c.b) / 3);
38 } else {
39 pixel->m = GetNearestColourIndex(c.r, c.g, c.b);
40 }
41 } else if (pixel->a < 128) {
42 /* Transparent pixel. */
43 pixel->m = 0;
44 } else {
45 /* Find nearest match from palette. */
46 pixel->m = GetNearestColourIndex(pixel->r, pixel->g, pixel->b);
47 }
48 }
49}
50
51uint8_t SpriteLoaderMakeIndexed::LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool, uint8_t control_flags, uint8_t &avail_8bpp, uint8_t &avail_32bpp)
52{
53 uint8_t avail = this->baseloader.LoadSprite(sprite, file, file_pos, sprite_type, true, control_flags, avail_8bpp, avail_32bpp);
54
55 for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
56 if (HasBit(avail, zoom)) Convert32bppTo8bpp(sprite[zoom]);
57 }
58
59 return avail;
60}
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
RandomAccessFile with some extra information specific for sprite files.
uint8_t LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, uint8_t control_flags, uint8_t &avail_8bpp, uint8_t &avail_32bpp) override
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
std::array< Sprite, ZOOM_LVL_END > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
virtual uint8_t LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, uint8_t control_flags, uint8_t &avail_8bpp, uint8_t &avail_32bpp)=0
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
Palette _cur_palette
Current palette.
Definition palette.cpp:24
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:344
static void Convert32bppTo8bpp(SpriteLoader::Sprite &sprite)
Convert in place a 32bpp sprite to 8bpp.
Base for converting sprites from another source from 32bpp RGBA to indexed 8bpp.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
uint8_t GetNearestColourReshadeIndex(uint8_t b)
Get nearest colour palette index from a brightness level.
Definition palette.cpp:164
uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Get nearest colour palette index from an RGB colour.
Definition palette.cpp:151
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:361
Structure for passing information from the sprite loader to the blitter.
uint16_t width
Width of the sprite.
SpriteLoader::CommonPixel * data
The sprite itself.
uint16_t height
Height of the sprite.
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:16
@ ZOOM_LVL_BEGIN
Begin for iteration.
Definition zoom_type.h:18
@ ZOOM_LVL_END
End for iteration.
Definition zoom_type.h:25