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

OpenTTD Source 15.3

grf.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 "../gfx_func.h"
12#include "../debug.h"
13#include "../settings_type.h"
14#include "../strings_func.h"
15#include "../error.h"
16#include "../spritecache.h"
17#include "grf.hpp"
18
19#include "table/strings.h"
20
21#include "../safeguards.h"
22
23extern const uint8_t _palmap_w2d[];
24
33 static bool WarnCorruptSprite(const SpriteFile &file, size_t file_pos, int line)
34{
35 static uint8_t warning_level = 0;
36 if (warning_level == 0) {
37 ShowErrorMessage(GetEncodedString(STR_NEWGRF_ERROR_CORRUPT_SPRITE, file.GetSimplifiedFilename()), {}, WL_ERROR);
38 }
39 Debug(sprite, warning_level, "[{}] Loading corrupted sprite from {} at position {}", line, file.GetSimplifiedFilename(), file_pos);
40 warning_level = 6;
41 return false;
42}
43
57 bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, int64_t num, uint8_t type, ZoomLevel zoom_lvl, SpriteComponents colour_fmt, uint8_t container_format)
58{
59 /*
60 * Original sprite height was max 255 pixels, with 4x extra zoom => 1020 pixels.
61 * Original maximum width for sprites was 640 pixels, with 4x extra zoom => 2560 pixels.
62 * Now up to 5 bytes per pixel => 1020 * 2560 * 5 => ~ 12.5 MiB.
63 *
64 * So, any sprite data more than 64 MiB is way larger that we would even expect; prevent allocating more memory!
65 */
66 if (num < 0 || num > 64 * 1024 * 1024) return WarnCorruptSprite(file, file_pos, __LINE__);
67
68 std::unique_ptr<uint8_t[]> dest_orig = std::make_unique<uint8_t[]>(num);
69 uint8_t *dest = dest_orig.get();
70 const int64_t dest_size = num;
71
72 /* Read the file, which has some kind of compression */
73 while (num > 0) {
74 int8_t code = file.ReadByte();
75
76 if (code >= 0) {
77 /* Plain bytes to read */
78 int size = (code == 0) ? 0x80 : code;
79 num -= size;
80 if (num < 0) return WarnCorruptSprite(file, file_pos, __LINE__);
81 for (; size > 0; size--) {
82 *dest = file.ReadByte();
83 dest++;
84 }
85 } else {
86 /* Copy bytes from earlier in the sprite */
87 const uint data_offset = ((code & 7) << 8) | file.ReadByte();
88 if (dest - data_offset < dest_orig.get()) return WarnCorruptSprite(file, file_pos, __LINE__);
89 int size = -(code >> 3);
90 num -= size;
91 if (num < 0) return WarnCorruptSprite(file, file_pos, __LINE__);
92 for (; size > 0; size--) {
93 *dest = *(dest - data_offset);
94 dest++;
95 }
96 }
97 }
98
99 if (num != 0) return WarnCorruptSprite(file, file_pos, __LINE__);
100
101 sprite->AllocateData(zoom_lvl, static_cast<size_t>(sprite->width) * sprite->height);
102
103 /* Convert colour depth to pixel size. */
104 int bpp = 0;
105 if (colour_fmt.Test(SpriteComponent::RGB)) bpp += 3; // Has RGB data.
106 if (colour_fmt.Test(SpriteComponent::Alpha)) bpp++; // Has alpha data.
107 if (colour_fmt.Test(SpriteComponent::Palette)) bpp++; // Has palette data.
108
109 /* When there are transparency pixels, this format has another trick.. decode it */
110 if (type & 0x08) {
111 for (int y = 0; y < sprite->height; y++) {
112 bool last_item = false;
113 /* Look up in the header-table where the real data is stored for this row */
114 int offset;
115 if (container_format >= 2 && dest_size > UINT16_MAX) {
116 offset = (dest_orig[y * 4 + 3] << 24) | (dest_orig[y * 4 + 2] << 16) | (dest_orig[y * 4 + 1] << 8) | dest_orig[y * 4];
117 } else {
118 offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
119 }
120
121 /* Go to that row */
122 dest = dest_orig.get() + offset;
123
124 do {
125 if (dest + (container_format >= 2 && sprite->width > 256 ? 4 : 2) > dest_orig.get() + dest_size) {
126 return WarnCorruptSprite(file, file_pos, __LINE__);
127 }
128
129 SpriteLoader::CommonPixel *data;
130 /* Read the header. */
131 int length, skip;
132 if (container_format >= 2 && sprite->width > 256) {
133 /* 0 .. 14 - length
134 * 15 - last_item
135 * 16 .. 31 - transparency bytes */
136 last_item = (dest[1] & 0x80) != 0;
137 length = ((dest[1] & 0x7F) << 8) | dest[0];
138 skip = (dest[3] << 8) | dest[2];
139 dest += 4;
140 } else {
141 /* 0 .. 6 - length
142 * 7 - last_item
143 * 8 .. 15 - transparency bytes */
144 last_item = ((*dest) & 0x80) != 0;
145 length = (*dest++) & 0x7F;
146 skip = *dest++;
147 }
148
149 data = &sprite->data[y * sprite->width + skip];
150
151 if (skip + length > sprite->width || dest + length * bpp > dest_orig.get() + dest_size) {
152 return WarnCorruptSprite(file, file_pos, __LINE__);
153 }
154
155 for (int x = 0; x < length; x++) {
156 if (colour_fmt.Test(SpriteComponent::RGB)) {
157 data->r = *dest++;
158 data->g = *dest++;
159 data->b = *dest++;
160 }
161 data->a = colour_fmt.Test(SpriteComponent::Alpha) ? *dest++ : 0xFF;
162 if (colour_fmt.Test(SpriteComponent::Palette)) {
163 switch (sprite_type) {
164 case SpriteType::Normal: data->m = file.NeedsPaletteRemap() ? _palmap_w2d[*dest] : *dest; break;
165 case SpriteType::Font: data->m = std::min<uint8_t>(*dest, 2u); break;
166 default: data->m = *dest; break;
167 }
168 /* Magic blue. */
169 if (colour_fmt == SpriteComponent::Palette && *dest == 0) data->a = 0x00;
170 dest++;
171 }
172 data++;
173 }
174 } while (!last_item);
175 }
176 } else {
177 int64_t sprite_size = static_cast<int64_t>(sprite->width) * sprite->height * bpp;
178 if (dest_size < sprite_size) {
179 return WarnCorruptSprite(file, file_pos, __LINE__);
180 }
181
182 if (dest_size > sprite_size) {
183 static uint8_t warning_level = 0;
184 Debug(sprite, warning_level, "Ignoring {} unused extra bytes from the sprite from {} at position {}", dest_size - sprite_size, file.GetSimplifiedFilename(), file_pos);
185 warning_level = 6;
186 }
187
188 dest = dest_orig.get();
189
190 for (int i = 0; i < sprite->width * sprite->height; i++) {
191 uint8_t *pixel = &dest[i * bpp];
192
193 if (colour_fmt.Test(SpriteComponent::RGB)) {
194 sprite->data[i].r = *pixel++;
195 sprite->data[i].g = *pixel++;
196 sprite->data[i].b = *pixel++;
197 }
198 sprite->data[i].a = colour_fmt.Test(SpriteComponent::Alpha) ? *pixel++ : 0xFF;
199 if (colour_fmt.Test(SpriteComponent::Palette)) {
200 switch (sprite_type) {
201 case SpriteType::Normal: sprite->data[i].m = file.NeedsPaletteRemap() ? _palmap_w2d[*pixel] : *pixel; break;
202 case SpriteType::Font: sprite->data[i].m = std::min<uint8_t>(*pixel, 2u); break;
203 default: sprite->data[i].m = *pixel; break;
204 }
205 /* Magic blue. */
206 if (colour_fmt == SpriteComponent::Palette && *pixel == 0) sprite->data[i].a = 0x00;
207 pixel++;
208 }
209 }
210 }
211
212 return true;
213}
214
215static ZoomLevels LoadSpriteV1(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, ZoomLevels &avail_8bpp)
216{
217 /* Check the requested colour depth. */
218 if (load_32bpp) return {};
219
220 /* Open the right file and go to the correct position */
221 file.SeekTo(file_pos, SEEK_SET);
222
223 /* Read the size and type */
224 int num = file.ReadWord();
225 uint8_t type = file.ReadByte();
226
227 /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here */
228 if (type == 0xFF) return {};
229
230 ZoomLevel zoom_lvl = (sprite_type != SpriteType::MapGen) ? ZoomLevel::Normal : ZoomLevel::Min;
231 auto &dest_sprite = sprite[zoom_lvl];
232
233 dest_sprite.height = file.ReadByte();
234 dest_sprite.width = file.ReadWord();
235 dest_sprite.x_offs = file.ReadWord();
236 dest_sprite.y_offs = file.ReadWord();
237 dest_sprite.colours = SpriteComponent::Palette;
238
239 if (dest_sprite.width > INT16_MAX) {
240 WarnCorruptSprite(file, file_pos, __LINE__);
241 return {};
242 }
243
244 /* 0x02 indicates it is a compressed sprite, so we can't rely on 'num' to be valid.
245 * In case it is uncompressed, the size is 'num' - 8 (header-size). */
246 num = (type & 0x02) ? dest_sprite.width * dest_sprite.height : num - 8;
247
248 if (DecodeSingleSprite(&dest_sprite, file, file_pos, sprite_type, num, type, zoom_lvl, SpriteComponent::Palette, 1)) {
249 avail_8bpp.Set(zoom_lvl);
250 return avail_8bpp;
251 }
252
253 return {};
254}
255
256static ZoomLevels LoadSpriteV2(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, SpriteCacheCtrlFlags control_flags, ZoomLevels &avail_8bpp, ZoomLevels &avail_32bpp)
257{
258 static const ZoomLevel zoom_lvl_map[6] = {ZoomLevel::Normal, ZoomLevel::In4x, ZoomLevel::In2x, ZoomLevel::Out2x, ZoomLevel::Out4x, ZoomLevel::Out8x};
259
260 /* Is the sprite not present/stripped in the GRF? */
261 if (file_pos == SIZE_MAX) return {};
262
263 /* Open the right file and go to the correct position */
264 file.SeekTo(file_pos, SEEK_SET);
265
266 uint32_t id = file.ReadDword();
267
268 ZoomLevels loaded_sprites;
269 do {
270 int64_t num = file.ReadDword();
271 size_t start_pos = file.GetPos();
272 uint8_t type = file.ReadByte();
273
274 /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here. */
275 if (type == 0xFF) return {};
276
277 SpriteComponents colour{type};
278 /* Mask out colour component information from type. */
279 type &= ~SpriteComponents::MASK;
280
281 uint8_t zoom = file.ReadByte();
282
283 bool is_wanted_colour_depth = (colour.Any() && (load_32bpp ? colour != SpriteComponent::Palette : colour == SpriteComponent::Palette));
284 bool is_wanted_zoom_lvl;
285
286 if (sprite_type != SpriteType::MapGen) {
287 if (zoom < lengthof(zoom_lvl_map)) {
288 ZoomLevel zoom_lvl = zoom_lvl_map[zoom];
289 if (colour == SpriteComponent::Palette) avail_8bpp.Set(zoom_lvl);
290 if (colour != SpriteComponent::Palette) avail_32bpp.Set(zoom_lvl);
291
292 is_wanted_zoom_lvl = true;
293 ZoomLevel zoom_min = sprite_type == SpriteType::Font ? ZoomLevel::Min : _settings_client.gui.sprite_zoom_min;
294 if (zoom_min >= ZoomLevel::In2x &&
295 control_flags.Test(load_32bpp ? SpriteCacheCtrlFlag::AllowZoomMin2x32bpp : SpriteCacheCtrlFlag::AllowZoomMin2xPal) && zoom_lvl < ZoomLevel::In2x) {
296 is_wanted_zoom_lvl = false;
297 }
298 if (zoom_min >= ZoomLevel::Normal &&
299 control_flags.Test(load_32bpp ? SpriteCacheCtrlFlag::AllowZoomMin1x32bpp : SpriteCacheCtrlFlag::AllowZoomMin1xPal) && zoom_lvl < ZoomLevel::Normal) {
300 is_wanted_zoom_lvl = false;
301 }
302 } else {
303 is_wanted_zoom_lvl = false;
304 }
305 } else {
306 is_wanted_zoom_lvl = (zoom == 0);
307 }
308
309 if (is_wanted_colour_depth && is_wanted_zoom_lvl) {
310 ZoomLevel zoom_lvl = (sprite_type != SpriteType::MapGen) ? zoom_lvl_map[zoom] : ZoomLevel::Min;
311
312 if (loaded_sprites.Test(zoom_lvl)) {
313 /* We already have this zoom level, skip sprite. */
314 Debug(sprite, 1, "Ignoring duplicate zoom level sprite {} from {}", id, file.GetSimplifiedFilename());
315 file.SkipBytes(num - 2);
316 continue;
317 }
318
319 auto &dest_sprite = sprite[zoom_lvl];
320 dest_sprite.height = file.ReadWord();
321 dest_sprite.width = file.ReadWord();
322 dest_sprite.x_offs = file.ReadWord();
323 dest_sprite.y_offs = file.ReadWord();
324
325 if (dest_sprite.width > INT16_MAX || dest_sprite.height > INT16_MAX) {
326 WarnCorruptSprite(file, file_pos, __LINE__);
327 return {};
328 }
329
330 /* Convert colour components to pixel size. */
331 int bpp = 0;
332 if (colour.Test(SpriteComponent::RGB)) bpp += 3;
333 if (colour.Test(SpriteComponent::Alpha)) bpp++;
334 if (colour.Test(SpriteComponent::Palette)) bpp++;
335
336 dest_sprite.colours = colour;
337
338 /* For chunked encoding we store the decompressed size in the file,
339 * otherwise we can calculate it from the image dimensions. */
340 uint decomp_size = (type & 0x08) ? file.ReadDword() : dest_sprite.width * dest_sprite.height * bpp;
341
342 bool valid = DecodeSingleSprite(&dest_sprite, file, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2);
343 if (file.GetPos() != start_pos + num) {
344 WarnCorruptSprite(file, file_pos, __LINE__);
345 return {};
346 }
347
348 if (valid) loaded_sprites.Set(zoom_lvl);
349 } else {
350 /* Not the wanted zoom level or colour depth, continue searching. */
351 file.SkipBytes(num - 2);
352 }
353
354 } while (file.ReadDword() == id);
355
356 return loaded_sprites;
357}
358
359 ZoomLevels SpriteLoaderGrf::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)
360{
361 if (this->container_ver >= 2) {
362 return LoadSpriteV2(sprite, file, file_pos, sprite_type, load_32bpp, control_flags, avail_8bpp, avail_32bpp);
363 } else {
364 return LoadSpriteV1(sprite, file, file_pos, sprite_type, load_32bpp, avail_8bpp);
365 }
366}
BlitterMode::Normal
@ Normal
Perform the simple blitting.
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
EnumBitSet< SpriteComponent, uint8_t, SpriteComponent::End >
RandomAccessFile::GetPos
size_t GetPos() const
Get position in the file.
Definition random_access_file.cpp:71
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition random_access_file.cpp:90
RandomAccessFile::ReadByte
uint8_t ReadByte()
Read a byte from the file.
Definition random_access_file.cpp:107
RandomAccessFile::GetSimplifiedFilename
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
Definition random_access_file.cpp:62
RandomAccessFile::ReadDword
uint32_t ReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition random_access_file.cpp:134
RandomAccessFile::SkipBytes
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
Definition random_access_file.cpp:163
RandomAccessFile::ReadWord
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition random_access_file.cpp:124
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
SpriteFile::NeedsPaletteRemap
bool NeedsPaletteRemap() const
Whether a palette remap is needed when loading sprites from this file.
Definition sprite_file_type.hpp:32
SpriteLoaderGrf::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 grf.cpp:359
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
WL_ERROR
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition error.h:26
ShowErrorMessage
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
Display an error message in a window.
Definition error_gui.cpp:296
SpriteType
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:357
SpriteType::Font
@ Font
A sprite used for fonts.
SpriteType::MapGen
@ MapGen
Special sprite for the map generator.
SpriteType::Normal
@ Normal
The most basic (normal) sprite.
WarnCorruptSprite
static bool WarnCorruptSprite(const SpriteFile &file, size_t file_pos, int line)
We found a corrupted sprite.
Definition grf.cpp:33
DecodeSingleSprite
bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, int64_t num, uint8_t type, ZoomLevel zoom_lvl, SpriteComponents colour_fmt, uint8_t container_format)
Decode the image data of a single sprite.
Definition grf.cpp:57
grf.hpp
Base for reading sprites from (New)GRFs.
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
SpriteCacheCtrlFlag
SpriteCacheCtrlFlag
Definition spritecache_type.h:24
SpriteCacheCtrlFlag::AllowZoomMin1x32bpp
@ AllowZoomMin1x32bpp
Allow use of sprite min zoom setting at 1x in 32bpp mode.
SpriteCacheCtrlFlag::AllowZoomMin2x32bpp
@ AllowZoomMin2x32bpp
Allow use of sprite min zoom setting at 2x in 32bpp mode.
SpriteCacheCtrlFlag::AllowZoomMin1xPal
@ AllowZoomMin1xPal
Allow use of sprite min zoom setting at 1x in palette mode.
SpriteCacheCtrlFlag::AllowZoomMin2xPal
@ AllowZoomMin2xPal
Allow use of sprite min zoom setting at 2x in palette mode.
SpriteComponent::Palette
@ Palette
Sprite has palette data.
SpriteComponent::Alpha
@ Alpha
Sprite has alpha.
SpriteComponent::RGB
@ RGB
Sprite has RGB.
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
GetEncodedString
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition settings_type.h:650
GUISettings::sprite_zoom_min
ZoomLevel sprite_zoom_min
maximum zoom level at which higher-resolution alternative sprites will be used (if available) instead...
Definition settings_type.h:193
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition spriteloader.hpp:48
SpriteLoader::CommonPixel::m
uint8_t m
Remap-channel.
Definition spriteloader.hpp:53
SpriteLoader::CommonPixel::b
uint8_t b
Blue-channel.
Definition spriteloader.hpp:51
SpriteLoader::CommonPixel::r
uint8_t r
Red-channel.
Definition spriteloader.hpp:49
SpriteLoader::CommonPixel::g
uint8_t g
Green-channel.
Definition spriteloader.hpp:50
SpriteLoader::CommonPixel::a
uint8_t a
Alpha-channel.
Definition spriteloader.hpp:52
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition spriteloader.hpp:62
SpriteLoader::Sprite::AllocateData
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
Definition spriteloader.hpp:75
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
ZoomLevel::In2x
@ In2x
Zoomed 2 times in.
ZoomLevel::Min
@ Min
Minimum zoom level.
ZoomLevel::Out2x
@ Out2x
Zoomed 2 times out.
ZoomLevel::Normal
@ Normal
The normal zoom level.
ZoomLevel::In4x
@ In4x
Zoomed 4 times in.
ZoomLevel::Out8x
@ Out8x
Zoomed 8 times out.
ZoomLevel::Out4x
@ Out4x
Zoomed 4 times out.