Add: INR currency (#8136)
[openttd-github.git] / src / spriteloader / grf.cpp
blob058193c34c975f3127b1b17672d51fc5a4e1067a
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 */
8 /** @file grf.cpp Reading graphics data from (New)GRF files. */
10 #include "../stdafx.h"
11 #include "../gfx_func.h"
12 #include "../fileio_func.h"
13 #include "../debug.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"
22 #include "../safeguards.h"
24 extern const byte _palmap_w2d[];
26 /** The different colour components a sprite can have. */
27 enum SpriteColourComponent {
28 SCC_RGB = 1 << 0, ///< Sprite has RGB.
29 SCC_ALPHA = 1 << 1, ///< Sprite has alpha.
30 SCC_PAL = 1 << 2, ///< Sprite has palette data.
31 SCC_MASK = SCC_RGB | SCC_ALPHA | SCC_PAL, ///< Mask of valid colour bits.
33 DECLARE_ENUM_AS_BIT_SET(SpriteColourComponent)
35 /**
36 * We found a corrupted sprite. This means that the sprite itself
37 * contains invalid data or is too small for the given dimensions.
38 * @param file_slot the file the errored sprite is in
39 * @param file_pos the location in the file of the errored sprite
40 * @param line the line where the error occurs.
41 * @return always false (to tell loading the sprite failed)
43 static bool WarnCorruptSprite(uint8 file_slot, size_t file_pos, int line)
45 static byte warning_level = 0;
46 if (warning_level == 0) {
47 SetDParamStr(0, FioGetFilename(file_slot));
48 ShowErrorMessage(STR_NEWGRF_ERROR_CORRUPT_SPRITE, INVALID_STRING_ID, WL_ERROR);
50 DEBUG(sprite, warning_level, "[%i] Loading corrupted sprite from %s at position %i", line, FioGetFilename(file_slot), (int)file_pos);
51 warning_level = 6;
52 return false;
55 /**
56 * Decode the image data of a single sprite.
57 * @param[in,out] sprite Filled with the sprite image data.
58 * @param file_slot File slot.
59 * @param file_pos File position.
60 * @param sprite_type Type of the sprite we're decoding.
61 * @param num Size of the decompressed sprite.
62 * @param type Type of the encoded sprite.
63 * @param zoom_lvl Requested zoom level.
64 * @param colour_fmt Colour format of the sprite.
65 * @param container_format Container format of the GRF this sprite is in.
66 * @return True if the sprite was successfully loaded.
68 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)
70 std::unique_ptr<byte[]> dest_orig(new byte[num]);
71 byte *dest = dest_orig.get();
72 const int64 dest_size = num;
74 /* Read the file, which has some kind of compression */
75 while (num > 0) {
76 int8 code = FioReadByte();
78 if (code >= 0) {
79 /* Plain bytes to read */
80 int size = (code == 0) ? 0x80 : code;
81 num -= size;
82 if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
83 for (; size > 0; size--) {
84 *dest = FioReadByte();
85 dest++;
87 } else {
88 /* Copy bytes from earlier in the sprite */
89 const uint data_offset = ((code & 7) << 8) | FioReadByte();
90 if (dest - data_offset < dest_orig.get()) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
91 int size = -(code >> 3);
92 num -= size;
93 if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
94 for (; size > 0; size--) {
95 *dest = *(dest - data_offset);
96 dest++;
101 if (num != 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
103 sprite->AllocateData(zoom_lvl, sprite->width * sprite->height);
105 /* Convert colour depth to pixel size. */
106 int bpp = 0;
107 if (colour_fmt & SCC_RGB) bpp += 3; // Has RGB data.
108 if (colour_fmt & SCC_ALPHA) bpp++; // Has alpha data.
109 if (colour_fmt & SCC_PAL) bpp++; // Has palette data.
111 /* When there are transparency pixels, this format has another trick.. decode it */
112 if (type & 0x08) {
113 for (int y = 0; y < sprite->height; y++) {
114 bool last_item = false;
115 /* Look up in the header-table where the real data is stored for this row */
116 int offset;
117 if (container_format >= 2 && dest_size > UINT16_MAX) {
118 offset = (dest_orig[y * 4 + 3] << 24) | (dest_orig[y * 4 + 2] << 16) | (dest_orig[y * 4 + 1] << 8) | dest_orig[y * 4];
119 } else {
120 offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
123 /* Go to that row */
124 dest = dest_orig.get() + offset;
126 do {
127 if (dest + (container_format >= 2 && sprite->width > 256 ? 4 : 2) > dest_orig.get() + dest_size) {
128 return WarnCorruptSprite(file_slot, file_pos, __LINE__);
131 SpriteLoader::CommonPixel *data;
132 /* Read the header. */
133 int length, skip;
134 if (container_format >= 2 && sprite->width > 256) {
135 /* 0 .. 14 - length
136 * 15 - last_item
137 * 16 .. 31 - transparency bytes */
138 last_item = (dest[1] & 0x80) != 0;
139 length = ((dest[1] & 0x7F) << 8) | dest[0];
140 skip = (dest[3] << 8) | dest[2];
141 dest += 4;
142 } else {
143 /* 0 .. 6 - length
144 * 7 - last_item
145 * 8 .. 15 - transparency bytes */
146 last_item = ((*dest) & 0x80) != 0;
147 length = (*dest++) & 0x7F;
148 skip = *dest++;
151 data = &sprite->data[y * sprite->width + skip];
153 if (skip + length > sprite->width || dest + length * bpp > dest_orig.get() + dest_size) {
154 return WarnCorruptSprite(file_slot, file_pos, __LINE__);
157 for (int x = 0; x < length; x++) {
158 if (colour_fmt & SCC_RGB) {
159 data->r = *dest++;
160 data->g = *dest++;
161 data->b = *dest++;
163 data->a = (colour_fmt & SCC_ALPHA) ? *dest++ : 0xFF;
164 if (colour_fmt & SCC_PAL) {
165 switch (sprite_type) {
166 case ST_NORMAL: data->m = _palette_remap_grf[file_slot] ? _palmap_w2d[*dest] : *dest; break;
167 case ST_FONT: data->m = min(*dest, 2u); break;
168 default: data->m = *dest; break;
170 /* Magic blue. */
171 if (colour_fmt == SCC_PAL && *dest == 0) data->a = 0x00;
172 dest++;
174 data++;
176 } while (!last_item);
178 } else {
179 if (dest_size < sprite->width * sprite->height * bpp) {
180 return WarnCorruptSprite(file_slot, file_pos, __LINE__);
183 if (dest_size > sprite->width * sprite->height * bpp) {
184 static byte warning_level = 0;
185 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);
186 warning_level = 6;
189 dest = dest_orig.get();
191 for (int i = 0; i < sprite->width * sprite->height; i++) {
192 byte *pixel = &dest[i * bpp];
194 if (colour_fmt & SCC_RGB) {
195 sprite->data[i].r = *pixel++;
196 sprite->data[i].g = *pixel++;
197 sprite->data[i].b = *pixel++;
199 sprite->data[i].a = (colour_fmt & SCC_ALPHA) ? *pixel++ : 0xFF;
200 if (colour_fmt & SCC_PAL) {
201 switch (sprite_type) {
202 case ST_NORMAL: sprite->data[i].m = _palette_remap_grf[file_slot] ? _palmap_w2d[*pixel] : *pixel; break;
203 case ST_FONT: sprite->data[i].m = min(*pixel, 2u); break;
204 default: sprite->data[i].m = *pixel; break;
206 /* Magic blue. */
207 if (colour_fmt == SCC_PAL && *pixel == 0) sprite->data[i].a = 0x00;
208 pixel++;
213 return true;
216 uint8 LoadSpriteV1(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
218 /* Check the requested colour depth. */
219 if (load_32bpp) return 0;
221 /* Open the right file and go to the correct position */
222 FioSeekToFile(file_slot, file_pos);
224 /* Read the size and type */
225 int num = FioReadWord();
226 byte type = FioReadByte();
228 /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here */
229 if (type == 0xFF) return 0;
231 ZoomLevel zoom_lvl = (sprite_type != ST_MAPGEN) ? ZOOM_LVL_OUT_4X : ZOOM_LVL_NORMAL;
233 sprite[zoom_lvl].height = FioReadByte();
234 sprite[zoom_lvl].width = FioReadWord();
235 sprite[zoom_lvl].x_offs = FioReadWord();
236 sprite[zoom_lvl].y_offs = FioReadWord();
238 if (sprite[zoom_lvl].width > INT16_MAX) {
239 WarnCorruptSprite(file_slot, file_pos, __LINE__);
240 return 0;
243 /* 0x02 indicates it is a compressed sprite, so we can't rely on 'num' to be valid.
244 * In case it is uncompressed, the size is 'num' - 8 (header-size). */
245 num = (type & 0x02) ? sprite[zoom_lvl].width * sprite[zoom_lvl].height : num - 8;
247 if (DecodeSingleSprite(&sprite[zoom_lvl], file_slot, file_pos, sprite_type, num, type, zoom_lvl, SCC_PAL, 1)) return 1 << zoom_lvl;
249 return 0;
252 uint8 LoadSpriteV2(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
254 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};
256 /* Is the sprite not present/stripped in the GRF? */
257 if (file_pos == SIZE_MAX) return 0;
259 /* Open the right file and go to the correct position */
260 FioSeekToFile(file_slot, file_pos);
262 uint32 id = FioReadDword();
264 uint8 loaded_sprites = 0;
265 do {
266 int64 num = FioReadDword();
267 size_t start_pos = FioGetPos();
268 byte type = FioReadByte();
270 /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here. */
271 if (type == 0xFF) return 0;
273 byte colour = type & SCC_MASK;
274 byte zoom = FioReadByte();
276 if (colour != 0 && (load_32bpp ? colour != SCC_PAL : colour == SCC_PAL) && (sprite_type != ST_MAPGEN ? zoom < lengthof(zoom_lvl_map) : zoom == 0)) {
277 ZoomLevel zoom_lvl = (sprite_type != ST_MAPGEN) ? zoom_lvl_map[zoom] : ZOOM_LVL_NORMAL;
279 if (HasBit(loaded_sprites, zoom_lvl)) {
280 /* We already have this zoom level, skip sprite. */
281 DEBUG(sprite, 1, "Ignoring duplicate zoom level sprite %u from %s", id, FioGetFilename(file_slot));
282 FioSkipBytes(num - 2);
283 continue;
286 sprite[zoom_lvl].height = FioReadWord();
287 sprite[zoom_lvl].width = FioReadWord();
288 sprite[zoom_lvl].x_offs = FioReadWord();
289 sprite[zoom_lvl].y_offs = FioReadWord();
291 if (sprite[zoom_lvl].width > INT16_MAX || sprite[zoom_lvl].height > INT16_MAX) {
292 WarnCorruptSprite(file_slot, file_pos, __LINE__);
293 return 0;
296 /* Mask out colour information. */
297 type = type & ~SCC_MASK;
299 /* Convert colour depth to pixel size. */
300 int bpp = 0;
301 if (colour & SCC_RGB) bpp += 3; // Has RGB data.
302 if (colour & SCC_ALPHA) bpp++; // Has alpha data.
303 if (colour & SCC_PAL) bpp++; // Has palette data.
305 /* For chunked encoding we store the decompressed size in the file,
306 * otherwise we can calculate it from the image dimensions. */
307 uint decomp_size = (type & 0x08) ? FioReadDword() : sprite[zoom_lvl].width * sprite[zoom_lvl].height * bpp;
309 bool valid = DecodeSingleSprite(&sprite[zoom_lvl], file_slot, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2);
310 if (FioGetPos() != start_pos + num) {
311 WarnCorruptSprite(file_slot, file_pos, __LINE__);
312 return 0;
315 if (valid) SetBit(loaded_sprites, zoom_lvl);
316 } else {
317 /* Not the wanted zoom level or colour depth, continue searching. */
318 FioSkipBytes(num - 2);
321 } while (FioReadDword() == id);
323 return loaded_sprites;
326 uint8 SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
328 if (this->container_ver >= 2) {
329 return LoadSpriteV2(sprite, file_slot, file_pos, sprite_type, load_32bpp);
330 } else {
331 return LoadSpriteV1(sprite, file_slot, file_pos, sprite_type, load_32bpp);