OpenTTD Source 20251213-master-g1091fa6071
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../core/math_func.hpp"
12#include "../gfx_func.h"
13#include "../palette_func.h"
14#include "makeindexed.h"
15
16#include "../safeguards.h"
17
23{
24 const auto *pixel_end = sprite.data + sprite.width * sprite.height;
25 for (auto *pixel = sprite.data; pixel != pixel_end; ++pixel) {
26 if (pixel->m != 0) {
27 /* Pixel has 8bpp mask, test if should be reshaded. */
28 uint8_t brightness = std::max({pixel->r, pixel->g, pixel->b});
29 if (brightness == 0 || brightness == 128) continue;
30
31 /* Update RGB component with reshaded palette colour, and enabled reshade. */
32 Colour c = AdjustBrightness(_cur_palette.palette[pixel->m], brightness);
33
34 if (IsInsideMM(pixel->m, 0xC6, 0xCE)) {
35 /* Dumb but simple brightness conversion. */
36 pixel->m = GetNearestColourReshadeIndex((c.r + c.g + c.b) / 3);
37 } else {
38 pixel->m = GetNearestColourIndex(c.r, c.g, c.b);
39 }
40 } else if (pixel->a < 128) {
41 /* Transparent pixel. */
42 pixel->m = 0;
43 } else {
44 /* Find nearest match from palette. */
45 pixel->m = GetNearestColourIndex(pixel->r, pixel->g, pixel->b);
46 }
47 }
48}
49
50ZoomLevels SpriteLoaderMakeIndexed::LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool, SpriteCacheCtrlFlags control_flags, ZoomLevels &avail_8bpp, ZoomLevels &avail_32bpp)
51{
52 ZoomLevels avail = this->baseloader.LoadSprite(sprite, file, file_pos, sprite_type, true, control_flags, avail_8bpp, avail_32bpp);
53
54 for (ZoomLevel zoom : avail) {
55 Convert32bppTo8bpp(sprite[zoom]);
56 }
57
58 return avail;
59}
Enum-as-bit-set wrapper.
Map zoom level to data.
RandomAccessFile with some extra information specific for sprite files.
ZoomLevels LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, SpriteCacheCtrlFlags control_flags, ZoomLevels &avail_8bpp, ZoomLevels &avail_32bpp) override
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
virtual ZoomLevels LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, SpriteCacheCtrlFlags control_flags, ZoomLevels &avail_8bpp, ZoomLevels &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:357
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:374
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:20