OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

grf.cpp
Go to the documentation of this file.
1 /* $Id: grf.cpp 27004 2014-10-12 20:43:25Z peter1138 $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../gfx_func.h"
14 #include "../fileio_func.h"
15 #include "../debug.h"
16 #include "../strings_func.h"
17 #include "table/strings.h"
18 #include "../error.h"
19 #include "../core/math_func.hpp"
20 #include "../core/alloc_type.hpp"
21 #include "../core/bitmath_func.hpp"
22 #include "grf.hpp"
23
24 #include "../safeguards.h"
25
26 extern const byte _palmap_w2d[];
27
29 enum SpriteColourComponent {
30 SCC_RGB = 1 << 0,
31 SCC_ALPHA = 1 << 1,
32 SCC_PAL = 1 << 2,
33 SCC_MASK = SCC_RGB | SCC_ALPHA | SCC_PAL,
34 };
35 DECLARE_ENUM_AS_BIT_SET(SpriteColourComponent)
36
37
45 static bool WarnCorruptSprite(uint8 file_slot, size_t file_pos, int line)
46 {
47 static byte warning_level = 0;
48 if (warning_level == 0) {
49 SetDParamStr(0, FioGetFilename(file_slot));
50 ShowErrorMessage(STR_NEWGRF_ERROR_CORRUPT_SPRITE, INVALID_STRING_ID, WL_ERROR);
51 }
52 DEBUG(sprite, warning_level, "[%i] Loading corrupted sprite from %s at position %i", line, FioGetFilename(file_slot), (int)file_pos);
53 warning_level = 6;
54 return false;
55 }
56
70 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)
71 {
72 AutoFreePtr<byte> dest_orig(MallocT<byte>(num));
73 byte *dest = dest_orig;
74 const int64 dest_size = num;
75
76 /* Read the file, which has some kind of compression */
77 while (num > 0) {
78 int8 code = FioReadByte();
79
80 if (code >= 0) {
81 /* Plain bytes to read */
82 int size = (code == 0) ? 0x80 : code;
83 num -= size;
84 if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
85 for (; size > 0; size--) {
86 *dest = FioReadByte();
87 dest++;
88 }
89 } else {
90 /* Copy bytes from earlier in the sprite */
91 const uint data_offset = ((code & 7) << 8) | FioReadByte();
92 if (dest - data_offset < dest_orig) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
93 int size = -(code >> 3);
94 num -= size;
95 if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
96 for (; size > 0; size--) {
97 *dest = *(dest - data_offset);
98 dest++;
99 }
100 }
101 }
102
103 if (num != 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
104
105 sprite->AllocateData(zoom_lvl, sprite->width * sprite->height);
106
107 /* Convert colour depth to pixel size. */
108 int bpp = 0;
109 if (colour_fmt & SCC_RGB) bpp += 3; // Has RGB data.
110 if (colour_fmt & SCC_ALPHA) bpp++; // Has alpha data.
111 if (colour_fmt & SCC_PAL) bpp++; // Has palette data.
112
113 /* When there are transparency pixels, this format has another trick.. decode it */
114 if (type & 0x08) {
115 for (int y = 0; y < sprite->height; y++) {
116 bool last_item = false;
117 /* Look up in the header-table where the real data is stored for this row */
118 int offset;
119 if (container_format >= 2 && dest_size > UINT16_MAX) {
120 offset = (dest_orig[y * 4 + 3] << 24) | (dest_orig[y * 4 + 2] << 16) | (dest_orig[y * 4 + 1] << 8) | dest_orig[y * 4];
121 } else {
122 offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
123 }
124
125 /* Go to that row */
126 dest = dest_orig + offset;
127
128 do {
129 if (dest + (container_format >= 2 && sprite->width > 256 ? 4 : 2) > dest_orig + dest_size) {
130 return WarnCorruptSprite(file_slot, file_pos, __LINE__);
131 }
132
133 SpriteLoader::CommonPixel *data;
134 /* Read the header. */
135 int length, skip;
136 if (container_format >= 2 && sprite->width > 256) {
137 /* 0 .. 14 - length
138 * 15 - last_item
139 * 16 .. 31 - transparency bytes */
140 last_item = (dest[1] & 0x80) != 0;
141 length = ((dest[1] & 0x7F) << 8) | dest[0];
142 skip = (dest[3] << 8) | dest[2];
143 dest += 4;
144 } else {
145 /* 0 .. 6 - length
146 * 7 - last_item
147 * 8 .. 15 - transparency bytes */
148 last_item = ((*dest) & 0x80) != 0;
149 length = (*dest++) & 0x7F;
150 skip = *dest++;
151 }
152
153 data = &sprite->data[y * sprite->width + skip];
154
155 if (skip + length > sprite->width || dest + length * bpp > dest_orig + dest_size) {
156 return WarnCorruptSprite(file_slot, file_pos, __LINE__);
157 }
158
159 for (int x = 0; x < length; x++) {
160 if (colour_fmt & SCC_RGB) {
161 data->r = *dest++;
162 data->g = *dest++;
163 data->b = *dest++;
164 }
165 data->a = (colour_fmt & SCC_ALPHA) ? *dest++ : 0xFF;
166 if (colour_fmt & SCC_PAL) {
167 switch (sprite_type) {
168 case ST_NORMAL: data->m = _palette_remap_grf[file_slot] ? _palmap_w2d[*dest] : *dest; break;
169 case ST_FONT: data->m = min(*dest, 2u); break;
170 default: data->m = *dest; break;
171 }
172 /* Magic blue. */
173 if (colour_fmt == SCC_PAL && *dest == 0) data->a = 0x00;
174 dest++;
175 }
176 data++;
177 }
178 } while (!last_item);
179 }
180 } else {
181 if (dest_size < sprite->width * sprite->height * bpp) {
182 return WarnCorruptSprite(file_slot, file_pos, __LINE__);
183 }
184
185 if (dest_size > sprite->width * sprite->height * bpp) {
186 static byte warning_level = 0;
187 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);
188 warning_level = 6;
189 }
190
191 dest = dest_orig;
192
193 for (int i = 0; i < sprite->width * sprite->height; i++) {
194 byte *pixel = &dest[i * bpp];
195
196 if (colour_fmt & SCC_RGB) {
197 sprite->data[i].r = *pixel++;
198 sprite->data[i].g = *pixel++;
199 sprite->data[i].b = *pixel++;
200 }
201 sprite->data[i].a = (colour_fmt & SCC_ALPHA) ? *pixel++ : 0xFF;
202 if (colour_fmt & SCC_PAL) {
203 switch (sprite_type) {
204 case ST_NORMAL: sprite->data[i].m = _palette_remap_grf[file_slot] ? _palmap_w2d[*pixel] : *pixel; break;
205 case ST_FONT: sprite->data[i].m = min(*pixel, 2u); break;
206 default: sprite->data[i].m = *pixel; break;
207 }
208 /* Magic blue. */
209 if (colour_fmt == SCC_PAL && *pixel == 0) sprite->data[i].a = 0x00;
210 pixel++;
211 }
212 }
213 }
214
215 return true;
216 }
217
218 uint8 LoadSpriteV1(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
219 {
220 /* Check the requested colour depth. */
221 if (load_32bpp) return 0;
222
223 /* Open the right file and go to the correct position */
224 FioSeekToFile(file_slot, file_pos);
225
226 /* Read the size and type */
227 int num = FioReadWord();
228 byte type = FioReadByte();
229
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;
232
233 ZoomLevel zoom_lvl = (sprite_type != ST_MAPGEN) ? ZOOM_LVL_OUT_4X : ZOOM_LVL_NORMAL;
234
235 sprite[zoom_lvl].height = FioReadByte();
236 sprite[zoom_lvl].width = FioReadWord();
237 sprite[zoom_lvl].x_offs = FioReadWord();
238 sprite[zoom_lvl].y_offs = FioReadWord();
239
240 if (sprite[zoom_lvl].width > INT16_MAX) {
241 WarnCorruptSprite(file_slot, 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_slot, 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, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
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 FioSeekToFile(file_slot, file_pos);
263
264 uint32 id = FioReadDword();
265
266 uint8 loaded_sprites = 0;
267 do {
268 int64 num = FioReadDword();
269 size_t start_pos = FioGetPos();
270 byte type = FioReadByte();
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 = FioReadByte();
277
278 if (colour != 0 && (load_32bpp ? colour != SCC_PAL : colour == SCC_PAL) && (sprite_type != ST_MAPGEN ? zoom < lengthof(zoom_lvl_map) : zoom == 0)) {
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 /* For chunked encoding we store the decompressed size in the file,
308 * otherwise we can calculate it from the image dimensions. */
309 uint decomp_size = (type & 0x08) ? FioReadDword() : sprite[zoom_lvl].width * sprite[zoom_lvl].height * bpp;
310
311 bool valid = DecodeSingleSprite(&sprite[zoom_lvl], file_slot, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2);
312 if (FioGetPos() != start_pos + num) {
313 WarnCorruptSprite(file_slot, file_pos, __LINE__);
314 return 0;
315 }
316
317 if (valid) SetBit(loaded_sprites, zoom_lvl);
318 } else {
319 /* Not the wanted zoom level or colour depth, continue searching. */
320 FioSkipBytes(num - 2);
321 }
322
323 } while (FioReadDword() == id);
324
325 return loaded_sprites;
326 }
327
328 uint8 SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
329 {
330 if (this->container_ver >= 2) {
331 return LoadSpriteV2(sprite, file_slot, file_pos, sprite_type, load_32bpp);
332 } else {
333 return LoadSpriteV1(sprite, file_slot, file_pos, sprite_type, load_32bpp);
334 }
335 }