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