OpenTTD Source: src/spriteloader/makeindexed.cpp Source File

OpenTTD Source 15.0-RC2

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
22 static void Convert32bppTo8bpp(SpriteLoader::Sprite &sprite)
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
50 ZoomLevels 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}
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
SpriteCollMap
Map zoom level to data.
Definition spriteloader.hpp:34
SpriteFile
RandomAccessFile with some extra information specific for sprite files.
Definition sprite_file_type.hpp:19
SpriteLoaderMakeIndexed::LoadSprite
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.
Definition makeindexed.cpp:50
SpriteLoader::LoadSprite
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.
_cur_palette
Palette _cur_palette
Current palette.
Definition palette.cpp:24
SpriteType
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:357
Convert32bppTo8bpp
static void Convert32bppTo8bpp(SpriteLoader::Sprite &sprite)
Convert in place a 32bpp sprite to 8bpp.
Definition makeindexed.cpp:22
makeindexed.h
Base for converting sprites from another source from 32bpp RGBA to indexed 8bpp.
IsInsideMM
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition math_func.hpp:267
GetNearestColourReshadeIndex
uint8_t GetNearestColourReshadeIndex(uint8_t b)
Get nearest colour palette index from a brightness level.
Definition palette.cpp:164
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:151
Palette::palette
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:374
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition spriteloader.hpp:62
SpriteLoader::Sprite::width
uint16_t width
Width of the sprite.
Definition spriteloader.hpp:64
SpriteLoader::Sprite::data
SpriteLoader::CommonPixel * data
The sprite itself.
Definition spriteloader.hpp:68
SpriteLoader::Sprite::height
uint16_t height
Height of the sprite.
Definition spriteloader.hpp:63
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20