2007-06-12 12:27:40 +00:00
/* $Id$ */
2008-05-06 15:11:33 +00:00
/** @file grf.cpp Reading graphics data from (New)GRF files. */
2007-06-12 12:27:40 +00:00
2007-06-11 11:50:49 +00:00
# include "../stdafx.h"
2007-12-23 10:56:02 +00:00
# include "../gfx_func.h"
2008-08-31 10:50:05 +00:00
# include "../fileio_func.h"
2007-06-11 11:50:49 +00:00
# include "../debug.h"
2007-12-25 09:48:53 +00:00
# include "../core/alloc_func.hpp"
2008-11-23 13:42:05 +00:00
# include "../strings_func.h"
# include "table/strings.h"
# include "../gui.h"
2007-06-11 11:50:49 +00:00
# include "grf.hpp"
2008-11-23 13:42:05 +00:00
/**
* We found a corrupted sprite . This means that the sprite itself
* contains invalid data or is too small for the given dimensions .
* @ param file_slot the file the errored sprite is in
* @ param file_pos the location in the file of the errored sprite
* @ param line the line where the error occurs .
* @ return always false ( to tell loading the sprite failed )
*/
static bool WarnCorruptSprite ( uint8 file_slot , size_t file_pos , int line )
{
static byte warning_level = 0 ;
if ( warning_level = = 0 ) {
SetDParamStr ( 0 , FioGetFilename ( file_slot ) ) ;
ShowErrorMessage ( INVALID_STRING_ID , STR_NEWGRF_ERROR_CORRUPT_SPRITE , 0 , 0 ) ;
}
DEBUG ( sprite , warning_level , " [%i] Loading corrupted sprite from %s at position %i " , line , FioGetFilename ( file_slot ) , ( int ) file_pos ) ;
warning_level = 6 ;
return false ;
}
2008-09-02 15:20:38 +00:00
bool SpriteLoaderGrf : : LoadSprite ( SpriteLoader : : Sprite * sprite , uint8 file_slot , size_t file_pos , SpriteType sprite_type )
2007-06-11 11:50:49 +00:00
{
/* Open the right file and go to the correct position */
2007-09-13 18:22:34 +00:00
FioSeekToFile ( file_slot , file_pos ) ;
2007-06-11 11:50:49 +00:00
/* Read the size and type */
int num = FioReadWord ( ) ;
byte type = FioReadByte ( ) ;
2009-02-09 02:57:15 +00:00
/* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here */
2007-06-11 11:50:49 +00:00
if ( type = = 0xFF ) return false ;
sprite - > height = FioReadByte ( ) ;
sprite - > width = FioReadWord ( ) ;
sprite - > x_offs = FioReadWord ( ) ;
sprite - > y_offs = FioReadWord ( ) ;
/* 0x02 indicates it is a compressed sprite, so we can't rely on 'num' to be valid.
* In case it is uncompressed , the size is ' num ' - 8 ( header - size ) . */
num = ( type & 0x02 ) ? sprite - > width * sprite - > height : num - 8 ;
2008-08-30 09:43:07 +00:00
byte * dest_orig = AllocaM ( byte , num ) ;
2007-06-11 11:50:49 +00:00
byte * dest = dest_orig ;
2008-11-23 13:42:05 +00:00
const int dest_size = num ;
2007-06-11 11:50:49 +00:00
/* Read the file, which has some kind of compression */
while ( num > 0 ) {
int8 code = FioReadByte ( ) ;
if ( code > = 0 ) {
/* Plain bytes to read */
int size = ( code = = 0 ) ? 0x80 : code ;
num - = size ;
2008-11-23 13:42:05 +00:00
if ( num < 0 ) return WarnCorruptSprite ( file_slot , file_pos , __LINE__ ) ;
2007-06-11 11:50:49 +00:00
for ( ; size > 0 ; size - - ) {
* dest = FioReadByte ( ) ;
dest + + ;
}
} else {
/* Copy bytes from earlier in the sprite */
const uint data_offset = ( ( code & 7 ) < < 8 ) | FioReadByte ( ) ;
2008-11-23 13:42:05 +00:00
if ( dest - data_offset < dest_orig ) return WarnCorruptSprite ( file_slot , file_pos , __LINE__ ) ;
2007-06-11 11:50:49 +00:00
int size = - ( code > > 3 ) ;
num - = size ;
2008-11-23 13:42:05 +00:00
if ( num < 0 ) return WarnCorruptSprite ( file_slot , file_pos , __LINE__ ) ;
2007-06-11 11:50:49 +00:00
for ( ; size > 0 ; size - - ) {
* dest = * ( dest - data_offset ) ;
dest + + ;
}
}
}
2008-11-23 13:42:05 +00:00
if ( num ! = 0 ) return WarnCorruptSprite ( file_slot , file_pos , __LINE__ ) ;
2007-06-11 11:50:49 +00:00
2009-02-23 10:50:25 +00:00
sprite - > AllocateData ( sprite - > width * sprite - > height ) ;
2007-06-11 11:50:49 +00:00
/* When there are transparency pixels, this format has an other trick.. decode it */
if ( type & 0x08 ) {
for ( int y = 0 ; y < sprite - > height ; y + + ) {
bool last_item = false ;
/* Look up in the header-table where the real data is stored for this row */
int offset = ( dest_orig [ y * 2 + 1 ] < < 8 ) | dest_orig [ y * 2 ] ;
2008-11-23 13:42:05 +00:00
2007-06-11 11:50:49 +00:00
/* Go to that row */
2008-11-23 13:42:05 +00:00
dest = dest_orig + offset ;
2007-06-11 11:50:49 +00:00
do {
2008-11-23 13:42:05 +00:00
if ( dest + 2 > dest_orig + dest_size ) {
free ( sprite - > data ) ;
return WarnCorruptSprite ( file_slot , file_pos , __LINE__ ) ;
}
2007-06-11 11:50:49 +00:00
SpriteLoader : : CommonPixel * data ;
/* Read the header:
* 0 . . 14 - length
* 15 - last_item
* 16 . . 31 - transparency bytes */
last_item = ( ( * dest ) & 0x80 ) ! = 0 ;
int length = ( * dest + + ) & 0x7F ;
int skip = * dest + + ;
data = & sprite - > data [ y * sprite - > width + skip ] ;
2008-11-23 13:42:05 +00:00
if ( skip + length > sprite - > width | | dest + length > dest_orig + dest_size ) {
free ( sprite - > data ) ;
return WarnCorruptSprite ( file_slot , file_pos , __LINE__ ) ;
}
2007-06-11 11:50:49 +00:00
for ( int x = 0 ; x < length ; x + + ) {
2008-09-02 18:45:15 +00:00
data - > m = ( ( sprite_type = = ST_NORMAL & & _palette_remap_grf [ file_slot ] ) ? _palette_remap [ * dest ] : * dest ) ;
2007-06-11 11:50:49 +00:00
dest + + ;
data + + ;
}
} while ( ! last_item ) ;
}
} else {
2008-11-23 13:42:05 +00:00
if ( dest_size < sprite - > width * sprite - > height ) {
free ( sprite - > data ) ;
return WarnCorruptSprite ( file_slot , file_pos , __LINE__ ) ;
}
if ( dest_size > sprite - > width * sprite - > height ) {
static byte warning_level = 0 ;
DEBUG ( sprite , warning_level , " Ignoring %i unused extra bytes from the sprite from %s at position %i " , dest_size - sprite - > width * sprite - > height , FioGetFilename ( file_slot ) , ( int ) file_pos ) ;
warning_level = 6 ;
}
2007-06-11 11:50:49 +00:00
dest = dest_orig ;
2008-09-02 18:45:15 +00:00
for ( int i = 0 ; i < sprite - > width * sprite - > height ; i + + ) {
sprite - > data [ i ] . m = ( ( sprite_type = = ST_NORMAL & & _palette_remap_grf [ file_slot ] ) ? _palette_remap [ dest [ i ] ] : dest [ i ] ) ;
}
2007-06-11 11:50:49 +00:00
}
2007-06-13 10:31:40 +00:00
/* Make sure to mark all transparent pixels transparent on the alpha channel too */
for ( int i = 0 ; i < sprite - > width * sprite - > height ; i + + )
if ( sprite - > data [ i ] . m ! = 0 ) sprite - > data [ i ] . a = 0xFF ;
2007-06-11 11:50:49 +00:00
return true ;
}