Codechange: Return pair from instead of optional out parameter. (#13166)
[openttd-github.git] / src / spriteloader / grf.cpp
blob62580ad6a8361e9f2f177ee9c1c9ba9bc7f88503
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 "../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"
23 #include "../safeguards.h"
25 extern const uint8_t _palmap_w2d[];
27 /**
28 * We found a corrupted sprite. This means that the sprite itself
29 * contains invalid data or is too small for the given dimensions.
30 * @param file_slot the file the errored sprite is in
31 * @param file_pos the location in the file of the errored sprite
32 * @param line the line where the error occurs.
33 * @return always false (to tell loading the sprite failed)
35 static bool WarnCorruptSprite(const SpriteFile &file, size_t file_pos, int line)
37 static uint8_t 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);
42 Debug(sprite, warning_level, "[{}] Loading corrupted sprite from {} at position {}", line, file.GetSimplifiedFilename(), file_pos);
43 warning_level = 6;
44 return false;
47 /**
48 * Decode the image data of a single sprite.
49 * @param[in,out] sprite Filled with the sprite image data.
50 * @param file The file with the sprite data.
51 * @param file_pos File position.
52 * @param sprite_type Type of the sprite we're decoding.
53 * @param num Size of the decompressed sprite.
54 * @param type Type of the encoded sprite.
55 * @param zoom_lvl Requested zoom level.
56 * @param colour_fmt Colour format of the sprite.
57 * @param container_format Container format of the GRF this sprite is in.
58 * @return True if the sprite was successfully loaded.
60 bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, int64_t num, uint8_t type, ZoomLevel zoom_lvl, uint8_t colour_fmt, uint8_t container_format)
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.
67 * So, any sprite data more than 64 MiB is way larger that we would even expect; prevent allocating more memory!
69 if (num < 0 || num > 64 * 1024 * 1024) return WarnCorruptSprite(file, file_pos, __LINE__);
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;
75 /* Read the file, which has some kind of compression */
76 while (num > 0) {
77 int8_t code = file.ReadByte();
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++;
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++;
102 if (num != 0) return WarnCorruptSprite(file, file_pos, __LINE__);
104 sprite->AllocateData(zoom_lvl, static_cast<size_t>(sprite->width) * sprite->height);
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.
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];
124 /* Go to that row */
125 dest = dest_orig.get() + offset;
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__);
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++;
152 data = &sprite->data[y * sprite->width + skip];
154 if (skip + length > sprite->width || dest + length * bpp > dest_orig.get() + dest_size) {
155 return WarnCorruptSprite(file, file_pos, __LINE__);
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++;
164 data->a = (colour_fmt & SCC_ALPHA) ? *dest++ : 0xFF;
165 if (colour_fmt & SCC_PAL) {
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;
171 /* Magic blue. */
172 if (colour_fmt == SCC_PAL && *dest == 0) data->a = 0x00;
173 dest++;
175 data++;
177 } while (!last_item);
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__);
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;
191 dest = dest_orig.get();
193 for (int i = 0; i < sprite->width * sprite->height; i++) {
194 uint8_t *pixel = &dest[i * bpp];
196 if (colour_fmt & SCC_RGB) {
197 sprite->data[i].r = *pixel++;
198 sprite->data[i].g = *pixel++;
199 sprite->data[i].b = *pixel++;
201 sprite->data[i].a = (colour_fmt & SCC_ALPHA) ? *pixel++ : 0xFF;
202 if (colour_fmt & SCC_PAL) {
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;
208 /* Magic blue. */
209 if (colour_fmt == SCC_PAL && *pixel == 0) sprite->data[i].a = 0x00;
210 pixel++;
215 return true;
218 uint8_t LoadSpriteV1(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
220 /* Check the requested colour depth. */
221 if (load_32bpp) return 0;
223 /* Open the right file and go to the correct position */
224 file.SeekTo(file_pos, SEEK_SET);
226 /* Read the size and type */
227 int num = file.ReadWord();
228 uint8_t type = file.ReadByte();
230 /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here */
231 if (type == 0xFF) return 0;
233 ZoomLevel zoom_lvl = (sprite_type != SpriteType::MapGen) ? ZOOM_LVL_NORMAL : ZOOM_LVL_MIN;
235 sprite[zoom_lvl].height = file.ReadByte();
236 sprite[zoom_lvl].width = file.ReadWord();
237 sprite[zoom_lvl].x_offs = file.ReadWord();
238 sprite[zoom_lvl].y_offs = file.ReadWord();
239 sprite[zoom_lvl].colours = SCC_PAL;
241 if (sprite[zoom_lvl].width > INT16_MAX) {
242 WarnCorruptSprite(file, file_pos, __LINE__);
243 return 0;
246 /* 0x02 indicates it is a compressed sprite, so we can't rely on 'num' to be valid.
247 * In case it is uncompressed, the size is 'num' - 8 (header-size). */
248 num = (type & 0x02) ? sprite[zoom_lvl].width * sprite[zoom_lvl].height : num - 8;
250 if (DecodeSingleSprite(&sprite[zoom_lvl], file, file_pos, sprite_type, num, type, zoom_lvl, SCC_PAL, 1)) return 1 << zoom_lvl;
252 return 0;
255 uint8_t LoadSpriteV2(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, uint8_t control_flags)
257 static const ZoomLevel zoom_lvl_map[6] = {ZOOM_LVL_NORMAL, ZOOM_LVL_IN_4X, ZOOM_LVL_IN_2X, ZOOM_LVL_OUT_2X, ZOOM_LVL_OUT_4X, ZOOM_LVL_OUT_8X};
259 /* Is the sprite not present/stripped in the GRF? */
260 if (file_pos == SIZE_MAX) return 0;
262 /* Open the right file and go to the correct position */
263 file.SeekTo(file_pos, SEEK_SET);
265 uint32_t id = file.ReadDword();
267 uint8_t loaded_sprites = 0;
268 do {
269 int64_t num = file.ReadDword();
270 size_t start_pos = file.GetPos();
271 uint8_t type = file.ReadByte();
273 /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here. */
274 if (type == 0xFF) return 0;
276 uint8_t colour = type & SCC_MASK;
277 uint8_t zoom = file.ReadByte();
279 bool is_wanted_colour_depth = (colour != 0 && (load_32bpp ? colour != SCC_PAL : colour == SCC_PAL));
280 bool is_wanted_zoom_lvl;
282 if (sprite_type != SpriteType::MapGen) {
283 if (zoom < lengthof(zoom_lvl_map)) {
284 is_wanted_zoom_lvl = true;
285 ZoomLevel zoom_min = sprite_type == SpriteType::Font ? ZOOM_LVL_MIN : _settings_client.gui.sprite_zoom_min;
286 if (zoom_min >= ZOOM_LVL_IN_2X &&
287 HasBit(control_flags, load_32bpp ? SCCF_ALLOW_ZOOM_MIN_2X_32BPP : SCCF_ALLOW_ZOOM_MIN_2X_PAL) && zoom_lvl_map[zoom] < ZOOM_LVL_IN_2X) {
288 is_wanted_zoom_lvl = false;
290 if (zoom_min >= ZOOM_LVL_NORMAL &&
291 HasBit(control_flags, load_32bpp ? SCCF_ALLOW_ZOOM_MIN_1X_32BPP : SCCF_ALLOW_ZOOM_MIN_1X_PAL) && zoom_lvl_map[zoom] < ZOOM_LVL_NORMAL) {
292 is_wanted_zoom_lvl = false;
294 } else {
295 is_wanted_zoom_lvl = false;
297 } else {
298 is_wanted_zoom_lvl = (zoom == 0);
301 if (is_wanted_colour_depth && is_wanted_zoom_lvl) {
302 ZoomLevel zoom_lvl = (sprite_type != SpriteType::MapGen) ? zoom_lvl_map[zoom] : ZOOM_LVL_MIN;
304 if (HasBit(loaded_sprites, zoom_lvl)) {
305 /* We already have this zoom level, skip sprite. */
306 Debug(sprite, 1, "Ignoring duplicate zoom level sprite {} from {}", id, file.GetSimplifiedFilename());
307 file.SkipBytes(num - 2);
308 continue;
311 sprite[zoom_lvl].height = file.ReadWord();
312 sprite[zoom_lvl].width = file.ReadWord();
313 sprite[zoom_lvl].x_offs = file.ReadWord();
314 sprite[zoom_lvl].y_offs = file.ReadWord();
316 if (sprite[zoom_lvl].width > INT16_MAX || sprite[zoom_lvl].height > INT16_MAX) {
317 WarnCorruptSprite(file, file_pos, __LINE__);
318 return 0;
321 /* Mask out colour information. */
322 type = type & ~SCC_MASK;
324 /* Convert colour depth to pixel size. */
325 int bpp = 0;
326 if (colour & SCC_RGB) bpp += 3; // Has RGB data.
327 if (colour & SCC_ALPHA) bpp++; // Has alpha data.
328 if (colour & SCC_PAL) bpp++; // Has palette data.
330 sprite[zoom_lvl].colours = (SpriteColourComponent)colour;
332 /* For chunked encoding we store the decompressed size in the file,
333 * otherwise we can calculate it from the image dimensions. */
334 uint decomp_size = (type & 0x08) ? file.ReadDword() : sprite[zoom_lvl].width * sprite[zoom_lvl].height * bpp;
336 bool valid = DecodeSingleSprite(&sprite[zoom_lvl], file, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2);
337 if (file.GetPos() != start_pos + num) {
338 WarnCorruptSprite(file, file_pos, __LINE__);
339 return 0;
342 if (valid) SetBit(loaded_sprites, zoom_lvl);
343 } else {
344 /* Not the wanted zoom level or colour depth, continue searching. */
345 file.SkipBytes(num - 2);
348 } while (file.ReadDword() == id);
350 return loaded_sprites;
353 uint8_t SpriteLoaderGrf::LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, uint8_t control_flags)
355 if (this->container_ver >= 2) {
356 return LoadSpriteV2(sprite, file, file_pos, sprite_type, load_32bpp, control_flags);
357 } else {
358 return LoadSpriteV1(sprite, file, file_pos, sprite_type, load_32bpp);