GifLoader done. SDL_Image dependency removed from Windows version. TBD on OSX and Linux
This commit is contained in:
61
gif.c
61
gif.c
@@ -13,6 +13,7 @@
|
||||
#define PLAINTEXT_EXTENSION 0x01
|
||||
|
||||
#define READ(dst, size) memcpy(dst, buffer, size); buffer += size
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned short width;
|
||||
@@ -277,7 +278,7 @@ static int read_sub_blocks( unsigned char* buffer, unsigned char **data )
|
||||
}
|
||||
|
||||
data_length += block_size;
|
||||
*data = realloc( *data, data_length );
|
||||
*data = (unsigned char*)realloc( *data, data_length );
|
||||
|
||||
// TODO this could be split across block size boundaries
|
||||
READ(*data + index, block_size);
|
||||
@@ -288,7 +289,7 @@ static int read_sub_blocks( unsigned char* buffer, unsigned char **data )
|
||||
return data_length;
|
||||
}
|
||||
|
||||
static int process_image_descriptor( unsigned char* buffer,
|
||||
unsigned char* process_image_descriptor( unsigned char* buffer,
|
||||
rgb *gct,
|
||||
int gct_size,
|
||||
int resolution_bits )
|
||||
@@ -299,7 +300,7 @@ static int process_image_descriptor( unsigned char* buffer,
|
||||
unsigned char *compressed_data = NULL;
|
||||
unsigned char lzw_code_size;
|
||||
int uncompressed_data_length = 0;
|
||||
//unsigned char *uncompressed_data = NULL;
|
||||
unsigned char *uncompressed_data = NULL;
|
||||
|
||||
// TODO there could actually be lots of these
|
||||
READ(&image_descriptor, 9);
|
||||
@@ -316,19 +317,17 @@ static int process_image_descriptor( unsigned char* buffer,
|
||||
height = image_descriptor.image_height;
|
||||
uncompressed_data_length = image_descriptor.image_width *
|
||||
image_descriptor.image_height;
|
||||
uncompressed_data = malloc( uncompressed_data_length );
|
||||
uncompressed_data = (unsigned char*)malloc( uncompressed_data_length );
|
||||
|
||||
uncompress( lzw_code_size, compressed_data, compressed_data_length,
|
||||
uncompressed_data );
|
||||
|
||||
done:
|
||||
if ( compressed_data )
|
||||
free( compressed_data );
|
||||
if ( compressed_data ) free( compressed_data );
|
||||
|
||||
//if ( uncompressed_data )
|
||||
// free( uncompressed_data );
|
||||
|
||||
return disposition;
|
||||
return uncompressed_data;
|
||||
}
|
||||
|
||||
static int process_extension( unsigned char* buffer )
|
||||
@@ -378,6 +377,30 @@ static int process_extension( unsigned char* buffer )
|
||||
* GIF-encoded file. This should point to the first byte in
|
||||
* the file when invoked.
|
||||
*/
|
||||
|
||||
unsigned char* LoadPalette(unsigned char *buffer) {
|
||||
unsigned char header[7];
|
||||
screen_descriptor_t screen_descriptor;
|
||||
int color_resolution_bits;
|
||||
|
||||
int global_color_table_size = 0; // number of entries in global_color_table
|
||||
rgb *global_color_table = NULL;
|
||||
|
||||
READ(header, 6);
|
||||
READ(&screen_descriptor, 7);
|
||||
|
||||
color_resolution_bits = ((screen_descriptor.fields & 0x70) >> 4) + 1;
|
||||
global_color_table = (rgb *)calloc(1, 768);
|
||||
|
||||
if (screen_descriptor.fields & 0x80) {
|
||||
global_color_table_size = 1 << (((screen_descriptor.fields & 0x07) + 1));
|
||||
|
||||
//global_color_table = (rgb *)malloc(3 * global_color_table_size);
|
||||
READ(global_color_table, 3 * global_color_table_size);
|
||||
}
|
||||
return (unsigned char*)global_color_table;
|
||||
}
|
||||
|
||||
static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short* w, unsigned short* h)
|
||||
{
|
||||
unsigned char header[ 7 ];
|
||||
@@ -400,7 +423,7 @@ static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short*
|
||||
fprintf( stderr,
|
||||
"Invalid GIF file (header is '%s', should be 'GIF89a')\n",
|
||||
header );
|
||||
return;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Followed by a logical screen descriptor
|
||||
@@ -433,18 +456,15 @@ static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short*
|
||||
switch ( block_type )
|
||||
{
|
||||
case IMAGE_DESCRIPTOR:
|
||||
if ( !process_image_descriptor( buffer,
|
||||
global_color_table,
|
||||
global_color_table_size,
|
||||
color_resolution_bits ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
return process_image_descriptor(buffer,
|
||||
global_color_table,
|
||||
global_color_table_size,
|
||||
color_resolution_bits);
|
||||
break;
|
||||
case EXTENSION_INTRODUCER:
|
||||
if ( !process_extension( buffer ) )
|
||||
{
|
||||
return;
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
case TRAILER:
|
||||
@@ -452,9 +472,10 @@ static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short*
|
||||
default:
|
||||
fprintf( stderr, "Bailing on unrecognized block type %.02x\n",
|
||||
block_type );
|
||||
return;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -462,7 +483,7 @@ unsigned char* LoadGif(unsigned char *buffer, unsigned short* w, unsigned short*
|
||||
return process_gif_stream(buffer, w, h);
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
/*int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE* gif_file;
|
||||
|
||||
@@ -483,4 +504,4 @@ int main( int argc, char *argv[] )
|
||||
process_gif_stream( gif_file );
|
||||
|
||||
fclose( gif_file );
|
||||
}
|
||||
}*/
|
||||
|
||||
Reference in New Issue
Block a user