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

OpenTTD Source 1.11.0-RC1

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