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

OpenTTD Source 12.0-beta1

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 "grf.hpp"
21
22 #include "../safeguards.h"
23
24 extern const byte _palmap_w2d[];
25
34 static bool WarnCorruptSprite(const SpriteFile &file, size_t file_pos, int line)
35 {
36 static byte warning_level = 0;
37 if (warning_level == 0) {
38 SetDParamStr(0, file.GetSimplifiedFilename());
39 ShowErrorMessage(STR_NEWGRF_ERROR_CORRUPT_SPRITE, INVALID_STRING_ID, WL_ERROR);
40 }
41 Debug(sprite, warning_level, "[{}] Loading corrupted sprite from {} at position {}", line, file.GetSimplifiedFilename(), file_pos);
42 warning_level = 6;
43 return false;
44 }
45
59 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)
60 {
61 std::unique_ptr<byte[]> dest_orig(new byte[num]);
62 byte *dest = dest_orig.get();
63 const int64 dest_size = num;
64
65 /* Read the file, which has some kind of compression */
66 while (num > 0) {
67 int8 code = file.ReadByte();
68
69 if (code >= 0) {
70 /* Plain bytes to read */
71 int size = (code == 0) ? 0x80 : code;
72 num -= size;
73 if (num < 0) return WarnCorruptSprite(file, file_pos, __LINE__);
74 for (; size > 0; size--) {
75 *dest = file.ReadByte();
76 dest++;
77 }
78 } else {
79 /* Copy bytes from earlier in the sprite */
80 const uint data_offset = ((code & 7) << 8) | file.ReadByte();
81 if (dest - data_offset < dest_orig.get()) return WarnCorruptSprite(file, file_pos, __LINE__);
82 int size = -(code >> 3);
83 num -= size;
84 if (num < 0) return WarnCorruptSprite(file, file_pos, __LINE__);
85 for (; size > 0; size--) {
86 *dest = *(dest - data_offset);
87 dest++;
88 }
89 }
90 }
91
92 if (num != 0) return WarnCorruptSprite(file, file_pos, __LINE__);
93
94 sprite->AllocateData(zoom_lvl, sprite->width * sprite->height);
95
96 /* Convert colour depth to pixel size. */
97 int bpp = 0;
98 if (colour_fmt & SCC_RGB) bpp += 3; // Has RGB data.
99 if (colour_fmt & SCC_ALPHA) bpp++; // Has alpha data.
100 if (colour_fmt & SCC_PAL) bpp++; // Has palette data.
101
102 /* When there are transparency pixels, this format has another trick.. decode it */
103 if (type & 0x08) {
104 for (int y = 0; y < sprite->height; y++) {
105 bool last_item = false;
106 /* Look up in the header-table where the real data is stored for this row */
107 int offset;
108 if (container_format >= 2 && dest_size > UINT16_MAX) {
109 offset = (dest_orig[y * 4 + 3] << 24) | (dest_orig[y * 4 + 2] << 16) | (dest_orig[y * 4 + 1] << 8) | dest_orig[y * 4];
110 } else {
111 offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
112 }
113
114 /* Go to that row */
115 dest = dest_orig.get() + offset;
116
117 do {
118 if (dest + (container_format >= 2 && sprite->width > 256 ? 4 : 2) > dest_orig.get() + dest_size) {
119 return WarnCorruptSprite(file, file_pos, __LINE__);
120 }
121
122 SpriteLoader::CommonPixel *data;
123 /* Read the header. */
124 int length, skip;
125 if (container_format >= 2 && sprite->width > 256) {
126 /* 0 .. 14 - length
127 * 15 - last_item
128 * 16 .. 31 - transparency bytes */
129 last_item = (dest[1] & 0x80) != 0;
130 length = ((dest[1] & 0x7F) << 8) | dest[0];
131 skip = (dest[3] << 8) | dest[2];
132 dest += 4;
133 } else {
134 /* 0 .. 6 - length
135 * 7 - last_item
136 * 8 .. 15 - transparency bytes */
137 last_item = ((*dest) & 0x80) != 0;
138 length = (*dest++) & 0x7F;
139 skip = *dest++;
140 }
141
142 data = &sprite->data[y * sprite->width + skip];
143
144 if (skip + length > sprite->width || dest + length * bpp > dest_orig.get() + dest_size) {
145 return WarnCorruptSprite(file, file_pos, __LINE__);
146 }
147
148 for (int x = 0; x < length; x++) {
149 if (colour_fmt & SCC_RGB) {
150 data->r = *dest++;
151 data->g = *dest++;
152 data->b = *dest++;
153 }
154 data->a = (colour_fmt & SCC_ALPHA) ? *dest++ : 0xFF;
155 if (colour_fmt & SCC_PAL) {
156 switch (sprite_type) {
157 case ST_NORMAL: data->m = file.NeedsPaletteRemap() ? _palmap_w2d[*dest] : *dest; break;
158 case ST_FONT: data->m = std::min<uint>(*dest, 2u); break;
159 default: data->m = *dest; break;
160 }
161 /* Magic blue. */
162 if (colour_fmt == SCC_PAL && *dest == 0) data->a = 0x00;
163 dest++;
164 }
165 data++;
166 }
167 } while (!last_item);
168 }
169 } else {
170 if (dest_size < sprite->width * sprite->height * bpp) {
171 return WarnCorruptSprite(file, file_pos, __LINE__);
172 }
173
174 if (dest_size > sprite->width * sprite->height * bpp) {
175 static byte warning_level = 0;
176 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);
177 warning_level = 6;
178 }
179
180 dest = dest_orig.get();
181
182 for (int i = 0; i < sprite->width * sprite->height; i++) {
183 byte *pixel = &dest[i * bpp];
184
185 if (colour_fmt & SCC_RGB) {
186 sprite->data[i].r = *pixel++;
187 sprite->data[i].g = *pixel++;
188 sprite->data[i].b = *pixel++;
189 }
190 sprite->data[i].a = (colour_fmt & SCC_ALPHA) ? *pixel++ : 0xFF;
191 if (colour_fmt & SCC_PAL) {
192 switch (sprite_type) {
193 case ST_NORMAL: sprite->data[i].m = file.NeedsPaletteRemap() ? _palmap_w2d[*pixel] : *pixel; break;
194 case ST_FONT: sprite->data[i].m = std::min<uint>(*pixel, 2u); break;
195 default: sprite->data[i].m = *pixel; break;
196 }
197 /* Magic blue. */
198 if (colour_fmt == SCC_PAL && *pixel == 0) sprite->data[i].a = 0x00;
199 pixel++;
200 }
201 }
202 }
203
204 return true;
205 }
206
207 uint8 LoadSpriteV1(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
208 {
209 /* Check the requested colour depth. */
210 if (load_32bpp) return 0;
211
212 /* Open the right file and go to the correct position */
213 file.SeekTo(file_pos, SEEK_SET);
214
215 /* Read the size and type */
216 int num = file.ReadWord();
217 byte type = file.ReadByte();
218
219 /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here */
220 if (type == 0xFF) return 0;
221
222 ZoomLevel zoom_lvl = (sprite_type != ST_MAPGEN) ? ZOOM_LVL_OUT_4X : ZOOM_LVL_NORMAL;
223
224 sprite[zoom_lvl].height = file.ReadByte();
225 sprite[zoom_lvl].width = file.ReadWord();
226 sprite[zoom_lvl].x_offs = file.ReadWord();
227 sprite[zoom_lvl].y_offs = file.ReadWord();
228 sprite[zoom_lvl].colours = SCC_PAL;
229
230 if (sprite[zoom_lvl].width > INT16_MAX) {
231 WarnCorruptSprite(file, file_pos, __LINE__);
232 return 0;
233 }
234
235 /* 0x02 indicates it is a compressed sprite, so we can't rely on 'num' to be valid.
236 * In case it is uncompressed, the size is 'num' - 8 (header-size). */
237 num = (type & 0x02) ? sprite[zoom_lvl].width * sprite[zoom_lvl].height : num - 8;
238
239 if (DecodeSingleSprite(&sprite[zoom_lvl], file, file_pos, sprite_type, num, type, zoom_lvl, SCC_PAL, 1)) return 1 << zoom_lvl;
240
241 return 0;
242 }
243
244 uint8 LoadSpriteV2(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
245 {
246 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};
247
248 /* Is the sprite not present/stripped in the GRF? */
249 if (file_pos == SIZE_MAX) return 0;
250
251 /* Open the right file and go to the correct position */
252 file.SeekTo(file_pos, SEEK_SET);
253
254 uint32 id = file.ReadDword();
255
256 uint8 loaded_sprites = 0;
257 do {
258 int64 num = file.ReadDword();
259 size_t start_pos = file.GetPos();
260 byte type = file.ReadByte();
261
262 /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here. */
263 if (type == 0xFF) return 0;
264
265 byte colour = type & SCC_MASK;
266 byte zoom = file.ReadByte();
267
268 bool is_wanted_colour_depth = (colour != 0 && (load_32bpp ? colour != SCC_PAL : colour == SCC_PAL));
269 bool is_wanted_zoom_lvl;
270
271 if (sprite_type != ST_MAPGEN) {
272 is_wanted_zoom_lvl = (zoom < lengthof(zoom_lvl_map) && zoom_lvl_map[zoom] >= _settings_client.gui.sprite_zoom_min);
273 } else {
274 is_wanted_zoom_lvl = (zoom == 0);
275 }
276
277 if (is_wanted_colour_depth && is_wanted_zoom_lvl) {
278 ZoomLevel zoom_lvl = (sprite_type != ST_MAPGEN) ? zoom_lvl_map[zoom] : ZOOM_LVL_NORMAL;
279
280 if (HasBit(loaded_sprites, zoom_lvl)) {
281 /* We already have this zoom level, skip sprite. */
282 Debug(sprite, 1, "Ignoring duplicate zoom level sprite {} from {}", id, file.GetSimplifiedFilename());
283 file.SkipBytes(num - 2);
284 continue;
285 }
286
287 sprite[zoom_lvl].height = file.ReadWord();
288 sprite[zoom_lvl].width = file.ReadWord();
289 sprite[zoom_lvl].x_offs = file.ReadWord();
290 sprite[zoom_lvl].y_offs = file.ReadWord();
291
292 if (sprite[zoom_lvl].width > INT16_MAX || sprite[zoom_lvl].height > INT16_MAX) {
293 WarnCorruptSprite(file, file_pos, __LINE__);
294 return 0;
295 }
296
297 /* Mask out colour information. */
298 type = type & ~SCC_MASK;
299
300 /* Convert colour depth to pixel size. */
301 int bpp = 0;
302 if (colour & SCC_RGB) bpp += 3; // Has RGB data.
303 if (colour & SCC_ALPHA) bpp++; // Has alpha data.
304 if (colour & SCC_PAL) bpp++; // Has palette data.
305
306 sprite[zoom_lvl].colours = (SpriteColourComponent)colour;
307
308 /* For chunked encoding we store the decompressed size in the file,
309 * otherwise we can calculate it from the image dimensions. */
310 uint decomp_size = (type & 0x08) ? file.ReadDword() : sprite[zoom_lvl].width * sprite[zoom_lvl].height * bpp;
311
312 bool valid = DecodeSingleSprite(&sprite[zoom_lvl], file, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2);
313 if (file.GetPos() != start_pos + num) {
314 WarnCorruptSprite(file, file_pos, __LINE__);
315 return 0;
316 }
317
318 if (valid) SetBit(loaded_sprites, zoom_lvl);
319 } else {
320 /* Not the wanted zoom level or colour depth, continue searching. */
321 file.SkipBytes(num - 2);
322 }
323
324 } while (file.ReadDword() == id);
325
326 return loaded_sprites;
327 }
328
329 uint8 SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
330 {
331 if (this->container_ver >= 2) {
332 return LoadSpriteV2(sprite, file, file_pos, sprite_type, load_32bpp);
333 } else {
334 return LoadSpriteV1(sprite, file, file_pos, sprite_type, load_32bpp);
335 }
336 }
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:25
ST_FONT
@ ST_FONT
A sprite used for fonts.
Definition: gfx_type.h:304
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:28
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:21
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
_palmap_w2d
const byte _palmap_w2d[]
Converting from the Windows palette to the DOS palette.
ST_NORMAL
@ ST_NORMAL
The most basic (normal) sprite.
Definition: gfx_type.h:302
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:383
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:59
SpriteLoaderGrf::LoadSprite
uint8 LoadSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
Definition: grf.cpp:329
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:27
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:301
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:34
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:29
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition: random_access_file.cpp:83
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:378
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:24
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:131
ZOOM_LVL_OUT_4X
@ ZOOM_LVL_OUT_4X
Zoomed 4 times out.
Definition: zoom_type.h:26
SpriteLoader::Sprite::height
uint16 height
Height of the sprite.
Definition: spriteloader.hpp:49
ST_MAPGEN
@ ST_MAPGEN
Special sprite for the map generator.
Definition: gfx_type.h:303
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:296
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:593
SpriteColourComponent
SpriteColourComponent
The different colour components a sprite can have.
Definition: spriteloader.hpp:22