1 /* $Id: grf.cpp 26132 2013-11-26 22:03:56Z rubidium $ */
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/>.
10 /** @file grf.cpp Reading graphics data from (New)GRF files. */
12 #include "../stdafx.h"
13 #include "../gfx_func.h"
14 #include "../fileio_func.h"
16 #include "../strings_func.h"
17 #include "table/strings.h"
19 #include "../core/math_func.hpp"
20 #include "../core/alloc_type.hpp"
21 #include "../core/bitmath_func.hpp"
24 #include "../safeguards.h"
26 extern const byte _palmap_w2d
[];
28 /** The different colour components a sprite can have. */
29 enum SpriteColourComponent
{
30 SCC_RGB
= 1 << 0, ///< Sprite has RGB.
31 SCC_ALPHA
= 1 << 1, ///< Sprite has alpha.
32 SCC_PAL
= 1 << 2, ///< Sprite has palette data.
33 SCC_MASK
= SCC_RGB
| SCC_ALPHA
| SCC_PAL
, ///< Mask of valid colour bits.
35 DECLARE_ENUM_AS_BIT_SET(SpriteColourComponent
)
38 * We found a corrupted sprite. This means that the sprite itself
39 * contains invalid data or is too small for the given dimensions.
40 * @param file_slot the file the errored sprite is in
41 * @param file_pos the location in the file of the errored sprite
42 * @param line the line where the error occurs.
43 * @return always false (to tell loading the sprite failed)
45 static bool WarnCorruptSprite(uint8 file_slot
, size_t file_pos
, int line
)
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
);
52 DEBUG(sprite
, warning_level
, "[%i] Loading corrupted sprite from %s at position %i", line
, FioGetFilename(file_slot
), (int)file_pos
);
58 * Decode the image data of a single sprite.
59 * @param[in,out] sprite Filled with the sprite image data.
60 * @param file_slot File slot.
61 * @param file_pos File position.
62 * @param sprite_type Type of the sprite we're decoding.
63 * @param num Size of the decompressed sprite.
64 * @param type Type of the encoded sprite.
65 * @param zoom_lvl Requested zoom level.
66 * @param colour_fmt Colour format of the sprite.
67 * @param container_format Container format of the GRF this sprite is in.
68 * @return True if the sprite was successfully loaded.
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
)
72 AutoFreePtr
<byte
> dest_orig(MallocT
<byte
>(num
));
73 byte
*dest
= dest_orig
;
74 const int64 dest_size
= num
;
76 /* Read the file, which has some kind of compression */
78 int8 code
= FioReadByte();
81 /* Plain bytes to read */
82 int size
= (code
== 0) ? 0x80 : code
;
84 if (num
< 0) return WarnCorruptSprite(file_slot
, file_pos
, __LINE__
);
85 for (; size
> 0; size
--) {
86 *dest
= FioReadByte();
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);
95 if (num
< 0) return WarnCorruptSprite(file_slot
, file_pos
, __LINE__
);
96 for (; size
> 0; size
--) {
97 *dest
= *(dest
- data_offset
);
103 if (num
!= 0) return WarnCorruptSprite(file_slot
, file_pos
, __LINE__
);
105 sprite
->AllocateData(zoom_lvl
, sprite
->width
* sprite
->height
);
107 /* Convert colour depth to pixel size. */
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.
113 /* When there are transparency pixels, this format has another trick.. decode it */
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 */
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];
122 offset
= (dest_orig
[y
* 2 + 1] << 8) | dest_orig
[y
* 2];
126 dest
= dest_orig
+ offset
;
129 if (dest
+ (container_format
>= 2 && sprite
->width
> 256 ? 4 : 2) > dest_orig
+ dest_size
) {
130 return WarnCorruptSprite(file_slot
, file_pos
, __LINE__
);
133 SpriteLoader::CommonPixel
*data
;
134 /* Read the header. */
136 if (container_format
>= 2 && sprite
->width
> 256) {
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];
147 * 8 .. 15 - transparency bytes */
148 last_item
= ((*dest
) & 0x80) != 0;
149 length
= (*dest
++) & 0x7F;
153 data
= &sprite
->data
[y
* sprite
->width
+ skip
];
155 if (skip
+ length
> sprite
->width
|| dest
+ length
* bpp
> dest_orig
+ dest_size
) {
156 return WarnCorruptSprite(file_slot
, file_pos
, __LINE__
);
159 for (int x
= 0; x
< length
; x
++) {
160 if (colour_fmt
& SCC_RGB
) {
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;
173 if (colour_fmt
== SCC_PAL
&& *dest
== 0) data
->a
= 0x00;
178 } while (!last_item
);
181 if (dest_size
< sprite
->width
* sprite
->height
* bpp
) {
182 return WarnCorruptSprite(file_slot
, file_pos
, __LINE__
);
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
);
193 for (int i
= 0; i
< sprite
->width
* sprite
->height
; i
++) {
194 byte
*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 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;
209 if (colour_fmt
== SCC_PAL
&& *pixel
== 0) sprite
->data
[i
].a
= 0x00;
218 uint8
LoadSpriteV1(SpriteLoader::Sprite
*sprite
, uint8 file_slot
, 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 FioSeekToFile(file_slot
, file_pos
);
226 /* Read the size and type */
227 int num
= FioReadWord();
228 byte type
= FioReadByte();
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
!= ST_MAPGEN
) ? ZOOM_LVL_OUT_4X
: ZOOM_LVL_NORMAL
;
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();
240 if (sprite
[zoom_lvl
].width
> INT16_MAX
) {
241 WarnCorruptSprite(file_slot
, file_pos
, __LINE__
);
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;
249 if (DecodeSingleSprite(&sprite
[zoom_lvl
], file_slot
, file_pos
, sprite_type
, num
, type
, zoom_lvl
, SCC_PAL
, 1)) return 1 << zoom_lvl
;
254 uint8
LoadSpriteV2(SpriteLoader::Sprite
*sprite
, uint8 file_slot
, size_t file_pos
, SpriteType sprite_type
, bool load_32bpp
)
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
};
258 /* Is the sprite not present/stripped in the GRF? */
259 if (file_pos
== SIZE_MAX
) return 0;
261 /* Open the right file and go to the correct position */
262 FioSeekToFile(file_slot
, file_pos
);
264 uint32 id
= FioReadDword();
266 uint8 loaded_sprites
= 0;
268 int64 num
= FioReadDword();
269 size_t start_pos
= FioGetPos();
270 byte type
= FioReadByte();
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;
275 byte colour
= type
& SCC_MASK
;
276 byte zoom
= FioReadByte();
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
;
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);
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();
293 if (sprite
[zoom_lvl
].width
> INT16_MAX
|| sprite
[zoom_lvl
].height
> INT16_MAX
) {
294 WarnCorruptSprite(file_slot
, file_pos
, __LINE__
);
298 /* Mask out colour information. */
299 type
= type
& ~SCC_MASK
;
301 /* Convert colour depth to pixel size. */
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.
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
;
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__
);
317 if (valid
) SetBit(loaded_sprites
, zoom_lvl
);
319 /* Not the wanted zoom level or colour depth, continue searching. */
320 FioSkipBytes(num
- 2);
323 } while (FioReadDword() == id
);
325 return loaded_sprites
;
328 uint8
SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite
*sprite
, uint8 file_slot
, size_t file_pos
, SpriteType sprite_type
, bool load_32bpp
)
330 if (this->container_ver
>= 2) {
331 return LoadSpriteV2(sprite
, file_slot
, file_pos
, sprite_type
, load_32bpp
);
333 return LoadSpriteV1(sprite
, file_slot
, file_pos
, sprite_type
, load_32bpp
);