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

OpenTTD Source 13.1

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 "table/strings.h"
16 #include "../error.h"
17 #include "../core/math_func.hpp"
18 #include "../core/alloc_type.hpp"
19 #include "../core/bitmath_func.hpp"
20 #include "../spritecache.h"
21 #include "grf.hpp"
22
23 #include "../safeguards.h"
24
25 extern const byte _palmap_w2d[];
26
35 static bool WarnCorruptSprite(const SpriteFile &file, size_t file_pos, int line)
36 {
37 static byte warning_level = 0;
38 if (warning_level == 0) {
39 SetDParamStr(0, file.GetSimplifiedFilename());
40 ShowErrorMessage(STR_NEWGRF_ERROR_CORRUPT_SPRITE, INVALID_STRING_ID, 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
60 bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, int64 num, byte type, ZoomLevel zoom_lvl, byte colour_fmt, byte 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<byte[]> dest_orig(new byte[num]);
72 byte *dest = dest_orig.get();
73 const int64 dest_size = num;
74
75 /* Read the file, which has some kind of compression */
76 while (num > 0) {
77 int8 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, sprite->width * sprite->height);
105
106 /* Convert colour depth to pixel size. */
107 int bpp = 0;
108 if (colour_fmt & SCC_RGB) bpp += 3; // Has RGB data.
109 if (colour_fmt & SCC_ALPHA) bpp++; // Has alpha data.
110 if (colour_fmt & SCC_PAL) 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
132 SpriteLoader::CommonPixel *data;
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 & SCC_RGB) {
160 data->r = *dest++;
161 data->g = *dest++;
162 data->b = *dest++;
163 }
164 data->a = (colour_fmt & SCC_ALPHA) ? *dest++ : 0xFF;
165 if (colour_fmt & SCC_PAL) {
166 switch (sprite_type) {
167 case ST_NORMAL: data->m = file.NeedsPaletteRemap() ? _palmap_w2d[*dest] : *dest; break;
168 case ST_FONT: data->m = std::min<uint>(*dest, 2u); break;
169 default: data->m = *dest; break;
170 }
171 /* Magic blue. */
172 if (colour_fmt == SCC_PAL && *dest == 0) data->a = 0x00;
173 dest++;
174 }
175 data++;
176 }
177 } while (!last_item);
178 }
179 } else {
180 if (dest_size < sprite->width * sprite->height * bpp) {
181 return WarnCorruptSprite(file, file_pos, __LINE__);
182 }
183
184 if (dest_size > sprite->width * sprite->height * bpp) {
185 static byte warning_level = 0;
186 Debug(sprite, warning_level, "Ignoring {} unused extra bytes from the sprite from {} at position {}", dest_size - sprite->width * sprite->height * bpp, file.GetSimplifiedFilename(), file_pos);
187 warning_level = 6;
188 }
189
190 dest = dest_orig.get();
191
192 for (int i = 0; i < sprite->width * sprite->height; i++) {
193 byte *pixel = &dest[i * bpp];
194
195 if (colour_fmt & SCC_RGB) {
196 sprite->data[i].r = *pixel++;
197 sprite->data[i].g = *pixel++;
198 sprite->data[i].b = *pixel++;
199 }
200 sprite->data[i].a = (colour_fmt & SCC_ALPHA) ? *pixel++ : 0xFF;
201 if (colour_fmt & SCC_PAL) {
202 switch (sprite_type) {
203 case ST_NORMAL: sprite->data[i].m = file.NeedsPaletteRemap() ? _palmap_w2d[*pixel] : *pixel; break;
204 case ST_FONT: sprite->data[i].m = std::min<uint>(*pixel, 2u); break;
205 default: sprite->data[i].m = *pixel; break;
206 }
207 /* Magic blue. */
208 if (colour_fmt == SCC_PAL && *pixel == 0) sprite->data[i].a = 0x00;
209 pixel++;
210 }
211 }
212 }
213
214 return true;
215 }
216
217 uint8 LoadSpriteV1(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
218 {
219 /* Check the requested colour depth. */
220 if (load_32bpp) return 0;
221
222 /* Open the right file and go to the correct position */
223 file.SeekTo(file_pos, SEEK_SET);
224
225 /* Read the size and type */
226 int num = file.ReadWord();
227 byte type = file.ReadByte();
228
229 /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here */
230 if (type == 0xFF) return 0;
231
232 ZoomLevel zoom_lvl = (sprite_type != ST_MAPGEN) ? ZOOM_LVL_OUT_4X : ZOOM_LVL_NORMAL;
233
234 sprite[zoom_lvl].height = file.ReadByte();
235 sprite[zoom_lvl].width = file.ReadWord();
236 sprite[zoom_lvl].x_offs = file.ReadWord();
237 sprite[zoom_lvl].y_offs = file.ReadWord();
238 sprite[zoom_lvl].colours = SCC_PAL;
239
240 if (sprite[zoom_lvl].width > INT16_MAX) {
241 WarnCorruptSprite(file, file_pos, __LINE__);
242 return 0;
243 }
244
245 /* 0x02 indicates it is a compressed sprite, so we can't rely on 'num' to be valid.
246 * In case it is uncompressed, the size is 'num' - 8 (header-size). */
247 num = (type & 0x02) ? sprite[zoom_lvl].width * sprite[zoom_lvl].height : num - 8;
248
249 if (DecodeSingleSprite(&sprite[zoom_lvl], file, file_pos, sprite_type, num, type, zoom_lvl, SCC_PAL, 1)) return 1 << zoom_lvl;
250
251 return 0;
252 }
253
254 uint8 LoadSpriteV2(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, byte control_flags)
255 {
256 static const ZoomLevel zoom_lvl_map[6] = {ZOOM_LVL_OUT_4X, ZOOM_LVL_NORMAL, ZOOM_LVL_OUT_2X, ZOOM_LVL_OUT_8X, ZOOM_LVL_OUT_16X, ZOOM_LVL_OUT_32X};
257
258 /* Is the sprite not present/stripped in the GRF? */
259 if (file_pos == SIZE_MAX) return 0;
260
261 /* Open the right file and go to the correct position */
262 file.SeekTo(file_pos, SEEK_SET);
263
264 uint32 id = file.ReadDword();
265
266 uint8 loaded_sprites = 0;
267 do {
268 int64 num = file.ReadDword();
269 size_t start_pos = file.GetPos();
270 byte type = file.ReadByte();
271
272 /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here. */
273 if (type == 0xFF) return 0;
274
275 byte colour = type & SCC_MASK;
276 byte zoom = file.ReadByte();
277
278 bool is_wanted_colour_depth = (colour != 0 && (load_32bpp ? colour != SCC_PAL : colour == SCC_PAL));
279 bool is_wanted_zoom_lvl;
280
281 if (sprite_type != ST_MAPGEN) {
282 if (zoom < lengthof(zoom_lvl_map)) {
283 is_wanted_zoom_lvl = true;
284 if (_settings_client.gui.sprite_zoom_min >= ZOOM_LVL_OUT_2X &&
285 HasBit(control_flags, load_32bpp ? SCCF_ALLOW_ZOOM_MIN_2X_32BPP : SCCF_ALLOW_ZOOM_MIN_2X_PAL) && zoom_lvl_map[zoom] < ZOOM_LVL_OUT_2X) {
286 is_wanted_zoom_lvl = false;
287 }
288 if (_settings_client.gui.sprite_zoom_min >= ZOOM_LVL_OUT_4X &&
289 HasBit(control_flags, load_32bpp ? SCCF_ALLOW_ZOOM_MIN_1X_32BPP : SCCF_ALLOW_ZOOM_MIN_1X_PAL) && zoom_lvl_map[zoom] < ZOOM_LVL_OUT_4X) {
290 is_wanted_zoom_lvl = false;
291 }
292 } else {
293 is_wanted_zoom_lvl = false;
294 }
295 } else {
296 is_wanted_zoom_lvl = (zoom == 0);
297 }
298
299 if (is_wanted_colour_depth && is_wanted_zoom_lvl) {
300 ZoomLevel zoom_lvl = (sprite_type != ST_MAPGEN) ? zoom_lvl_map[zoom] : ZOOM_LVL_NORMAL;
301
302 if (HasBit(loaded_sprites, zoom_lvl)) {
303 /* We already have this zoom level, skip sprite. */
304 Debug(sprite, 1, "Ignoring duplicate zoom level sprite {} from {}", id, file.GetSimplifiedFilename());
305 file.SkipBytes(num - 2);
306 continue;
307 }
308
309 sprite[zoom_lvl].height = file.ReadWord();
310 sprite[zoom_lvl].width = file.ReadWord();
311 sprite[zoom_lvl].x_offs = file.ReadWord();
312 sprite[zoom_lvl].y_offs = file.ReadWord();
313
314 if (sprite[zoom_lvl].width > INT16_MAX || sprite[zoom_lvl].height > INT16_MAX) {
315 WarnCorruptSprite(file, file_pos, __LINE__);
316 return 0;
317 }
318
319 /* Mask out colour information. */
320 type = type & ~SCC_MASK;
321
322 /* Convert colour depth to pixel size. */
323 int bpp = 0;
324 if (colour & SCC_RGB) bpp += 3; // Has RGB data.
325 if (colour & SCC_ALPHA) bpp++; // Has alpha data.
326 if (colour & SCC_PAL) bpp++; // Has palette data.
327
328 sprite[zoom_lvl].colours = (SpriteColourComponent)colour;
329
330 /* For chunked encoding we store the decompressed size in the file,
331 * otherwise we can calculate it from the image dimensions. */
332 uint decomp_size = (type & 0x08) ? file.ReadDword() : sprite[zoom_lvl].width * sprite[zoom_lvl].height * bpp;
333
334 bool valid = DecodeSingleSprite(&sprite[zoom_lvl], file, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2);
335 if (file.GetPos() != start_pos + num) {
336 WarnCorruptSprite(file, file_pos, __LINE__);
337 return 0;
338 }
339
340 if (valid) SetBit(loaded_sprites, zoom_lvl);
341 } else {
342 /* Not the wanted zoom level or colour depth, continue searching. */
343 file.SkipBytes(num - 2);
344 }
345
346 } while (file.ReadDword() == id);
347
348 return loaded_sprites;
349 }
350
351 uint8 SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, byte control_flags)
352 {
353 if (this->container_ver >= 2) {
354 return LoadSpriteV2(sprite, file, file_pos, sprite_type, load_32bpp, control_flags);
355 } else {
356 return LoadSpriteV1(sprite, file, file_pos, sprite_type, load_32bpp);
357 }
358 }
SpriteLoader::CommonPixel::m
uint8 m
Remap-channel.
Definition: spriteloader.hpp:39
SCC_PAL
@ SCC_PAL
Sprite has palette data.
Definition: spriteloader.hpp:25
SpriteFile::NeedsPaletteRemap
bool NeedsPaletteRemap() const
Whether a palette remap is needed when loading sprites from this file.
Definition: sprite_file_type.hpp:32
SpriteLoader::CommonPixel::r
uint8 r
Red-channel.
Definition: spriteloader.hpp:35
ZOOM_LVL_OUT_2X
@ ZOOM_LVL_OUT_2X
Zoomed 2 times out.
Definition: zoom_type.h:23
ST_FONT
@ ST_FONT
A sprite used for fonts.
Definition: gfx_type.h:311
SpriteLoader::Sprite::AllocateData
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
Definition: spriteloader.hpp:62
ZOOM_LVL_OUT_16X
@ ZOOM_LVL_OUT_16X
Zoomed 16 times out.
Definition: zoom_type.h:26
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
valid
uint8 valid
Bits indicating what variable is valid (for each bit, 0 is invalid, 1 is valid).
Definition: newgrf_station.cpp:248
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:19
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:53
_palmap_w2d
const byte _palmap_w2d[]
Converting from the Windows palette to the DOS palette.
SCCF_ALLOW_ZOOM_MIN_2X_32BPP
@ SCCF_ALLOW_ZOOM_MIN_2X_32BPP
Allow use of sprite min zoom setting at 2x in 32bpp mode.
Definition: spritecache.h:29
ST_NORMAL
@ ST_NORMAL
The most basic (normal) sprite.
Definition: gfx_type.h:309
grf.hpp
RandomAccessFile::ReadByte
byte ReadByte()
Read a byte from the file.
Definition: random_access_file.cpp:100
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:377
RandomAccessFile::SkipBytes
void SkipBytes(int n)
Skip n bytes ahead in the file.
Definition: random_access_file.cpp:148
SpriteLoader::Sprite::data
SpriteLoader::CommonPixel * data
The sprite itself.
Definition: spriteloader.hpp:55
DecodeSingleSprite
bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, int64 num, byte type, ZoomLevel zoom_lvl, byte colour_fmt, byte container_format)
Decode the image data of a single sprite.
Definition: grf.cpp:60
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition: spriteloader.hpp:34
ZOOM_LVL_OUT_8X
@ ZOOM_LVL_OUT_8X
Zoomed 8 times out.
Definition: zoom_type.h:25
SCCF_ALLOW_ZOOM_MIN_1X_PAL
@ SCCF_ALLOW_ZOOM_MIN_1X_PAL
Allow use of sprite min zoom setting at 1x in palette mode.
Definition: spritecache.h:26
SpriteLoader::Sprite::x_offs
int16 x_offs
The x-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:51
RandomAccessFile::ReadWord
uint16 ReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition: random_access_file.cpp:117
RandomAccessFile::ReadDword
uint32 ReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition: random_access_file.cpp:127
SpriteLoader::Sprite::colours
SpriteColourComponent colours
The colour components of the sprite with useful information.
Definition: spriteloader.hpp:54
RandomAccessFile::GetPos
size_t GetPos() const
Get position in the file.
Definition: random_access_file.cpp:73
SpriteFile
RandomAccessFile with some extra information specific for sprite files.
Definition: sprite_file_type.hpp:19
SCC_RGB
@ SCC_RGB
Sprite has RGB.
Definition: spriteloader.hpp:23
SpriteLoader::CommonPixel::b
uint8 b
Blue-channel.
Definition: spriteloader.hpp:37
SpriteLoader::Sprite::width
uint16 width
Width of the sprite.
Definition: spriteloader.hpp:50
SCC_MASK
@ SCC_MASK
Mask of valid colour bits.
Definition: spriteloader.hpp:26
SpriteType
SpriteType
Types of sprites that might be loaded.
Definition: gfx_type.h:308
SCC_ALPHA
@ SCC_ALPHA
Sprite has alpha.
Definition: spriteloader.hpp:24
WarnCorruptSprite
static bool WarnCorruptSprite(const SpriteFile &file, size_t file_pos, int line)
We found a corrupted sprite.
Definition: grf.cpp:35
WL_ERROR
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition: error.h:24
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition: spriteloader.hpp:48
RandomAccessFile::GetSimplifiedFilename
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
Definition: random_access_file.cpp:64
ZOOM_LVL_OUT_32X
@ ZOOM_LVL_OUT_32X
Zoomed 32 times out.
Definition: zoom_type.h:27
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition: random_access_file.cpp:83
SCCF_ALLOW_ZOOM_MIN_2X_PAL
@ SCCF_ALLOW_ZOOM_MIN_2X_PAL
Allow use of sprite min zoom setting at 2x in palette mode.
Definition: spritecache.h:28
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:386
SpriteLoader::CommonPixel::g
uint8 g
Green-channel.
Definition: spriteloader.hpp:36
SpriteLoader::CommonPixel::a
uint8 a
Alpha-channel.
Definition: spriteloader.hpp:38
ZOOM_LVL_NORMAL
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition: zoom_type.h:22
SpriteLoader::Sprite::y_offs
int16 y_offs
The y-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:52
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:136
ZOOM_LVL_OUT_4X
@ ZOOM_LVL_OUT_4X
Zoomed 4 times out.
Definition: zoom_type.h:24
SpriteLoader::Sprite::height
uint16 height
Height of the sprite.
Definition: spriteloader.hpp:49
SpriteLoaderGrf::LoadSprite
uint8 LoadSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, byte control_flags)
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
Definition: grf.cpp:351
ST_MAPGEN
@ ST_MAPGEN
Special sprite for the map generator.
Definition: gfx_type.h:310
SCCF_ALLOW_ZOOM_MIN_1X_32BPP
@ SCCF_ALLOW_ZOOM_MIN_1X_32BPP
Allow use of sprite min zoom setting at 1x in 32bpp mode.
Definition: spritecache.h:27
SetDParamStr
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:297
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:604
SpriteColourComponent
SpriteColourComponent
The different colour components a sprite can have.
Definition: spriteloader.hpp:22