This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/launch/imagelib/img_wad.c

454 lines
12 KiB
C
Raw Normal View History

2008-08-06 22:00:00 +02:00
//=======================================================================
// Copyright XashXT Group 2007 <20>
// img_mip.c - hl1 q1 image mips
//=======================================================================
2008-10-22 22:00:00 +02:00
#include "imagelib.h"
2008-11-25 22:00:00 +01:00
#include "qfiles_ref.h"
2008-08-06 22:00:00 +02:00
/*
============
Image_LoadPAL
============
*/
bool Image_LoadPAL( const char *name, const byte *buffer, size_t filesize )
{
2008-10-19 22:00:00 +02:00
int rendermode = LUMP_NORMAL;
2008-08-06 22:00:00 +02:00
if( filesize != 768 )
{
2008-08-08 22:00:00 +02:00
MsgDev( D_ERROR, "Image_LoadPAL: (%s) have invalid size (%d should be %d)\n", name, filesize, 768 );
2008-08-06 22:00:00 +02:00
return false;
}
2008-10-19 22:00:00 +02:00
if( name[0] == '#' )
{
// using palette name as rendermode
if( com.stristr( name, "normal" ))
rendermode = LUMP_NORMAL;
else if( com.stristr( name, "transparent" ))
rendermode = LUMP_TRANSPARENT;
else if( com.stristr( name, "decal" ))
rendermode = LUMP_DECAL;
else if( com.stristr( name, "qfont" ))
rendermode = LUMP_QFONT;
2009-08-25 22:00:00 +02:00
else if( com.stristr( name, "quake" ))
buffer = NULL; // force to get Q1 palette
2008-10-19 22:00:00 +02:00
}
// NOTE: image.d_currentpal not cleared with Image_Reset()
// and stay valid any time before new call of Image_SetPalette
Image_GetPaletteLMP( buffer, rendermode );
2008-08-07 22:00:00 +02:00
Image_CopyPalette32bit();
2008-10-19 22:00:00 +02:00
image.rgba = NULL; // only palette, not real image
image.size = 1024; // expanded palette
2009-07-22 22:00:00 +02:00
image.num_mips = image.depth = 0;
2008-10-19 22:00:00 +02:00
image.width = image.height = 0;
2008-08-06 22:00:00 +02:00
return true;
}
2008-10-19 22:00:00 +02:00
/*
============
Image_LoadMDL
============
*/
bool Image_LoadMDL( const char *name, const byte *buffer, size_t filesize )
{
byte *fin;
size_t pixels;
2008-10-20 22:00:00 +02:00
dstudiotexture_t *pin;
2009-08-25 22:00:00 +02:00
int i, flags;
2008-10-19 22:00:00 +02:00
2008-10-20 22:00:00 +02:00
pin = (dstudiotexture_t *)buffer;
2008-10-19 22:00:00 +02:00
flags = LittleLong( pin->flags );
// Valve never used endian functions for studiomodels...
image.width = LittleLong( pin->width );
image.height = LittleLong( pin->height );
pixels = image.width * image.height;
2009-08-25 22:00:00 +02:00
fin = (byte *)pin->index; // setup buffer
2008-10-19 22:00:00 +02:00
2009-08-25 22:00:00 +02:00
if( image.hint != IL_HINT_Q1 && !( flags & STUDIO_NF_QUAKESKIN ))
2008-10-19 22:00:00 +02:00
{
2009-08-25 22:00:00 +02:00
if( flags & STUDIO_NF_TRANSPARENT )
{
2009-09-04 22:00:00 +02:00
Image_GetPaletteLMP( fin + pixels, LUMP_TRANSPARENT );
2009-08-25 22:00:00 +02:00
image.flags |= IMAGE_HAS_ALPHA;
}
2009-09-04 22:00:00 +02:00
else Image_GetPaletteLMP( fin + pixels, LUMP_NORMAL );
2008-10-19 22:00:00 +02:00
}
2009-08-25 22:00:00 +02:00
else if( image.hint != IL_HINT_HL && flags & STUDIO_NF_QUAKESKIN )
2008-10-19 22:00:00 +02:00
{
2009-08-25 22:00:00 +02:00
// alias models setup
Image_GetPaletteQ1();
// check for luma pixels
for( i = 0; i < pixels; i++ )
{
if( fin[i] > 224 )
{
Msg( "%s has luma-pixels\n", name );
image.flags |= IMAGE_HAS_LUMA_Q1;
break;
}
}
2008-10-19 22:00:00 +02:00
}
2009-08-25 22:00:00 +02:00
else
{
if( image.hint == IL_HINT_NO )
MsgDev( D_ERROR, "Image_LoadMDL: lump (%s) is corrupted\n", name );
return false; // unknown or unsupported mode rejected
}
2008-10-19 22:00:00 +02:00
if(!Image_LumpValidSize( name )) return false;
2009-07-22 22:00:00 +02:00
image.depth = 1;
2008-10-19 22:00:00 +02:00
image.type = PF_INDEXED_32; // 32-bit palete
return FS_AddMipmapToPack( fin, image.width, image.height );
}
/*
============
Image_LoadSPR
============
*/
bool Image_LoadSPR( const char *name, const byte *buffer, size_t filesize )
{
2008-10-25 22:00:00 +02:00
dspriteframe_t *pin; // indetical for q1\hl sprites
2008-11-03 22:00:00 +01:00
2008-10-19 22:00:00 +02:00
if( image.hint == IL_HINT_HL )
{
if( !image.d_currentpal )
{
MsgDev( D_ERROR, "Image_LoadSPR: (%s) palette not installed\n", name );
return false;
}
}
else if( image.hint == IL_HINT_Q1 )
{
Image_GetPaletteQ1();
}
else return false; // unknown mode rejected
2008-10-25 22:00:00 +02:00
pin = (dspriteframe_t *)buffer;
2008-10-19 22:00:00 +02:00
image.width = LittleLong( pin->width );
image.height = LittleLong( pin->height );
2008-10-20 22:00:00 +02:00
if( filesize < image.width * image.height )
{
MsgDev( D_ERROR, "Image_LoadSPR: file (%s) have invalid size\n", name );
return false;
}
2008-10-19 22:00:00 +02:00
// sorry, can't validate palette rendermode
if(!Image_LumpValidSize( name )) return false;
2009-07-22 22:00:00 +02:00
image.depth = 1;
2008-10-19 22:00:00 +02:00
image.type = PF_INDEXED_32; // 32-bit palete
2008-10-20 22:00:00 +02:00
// detect alpha-channel by palette type
if( image.d_rendermode == LUMP_DECAL || image.d_rendermode == LUMP_TRANSPARENT )
2008-11-03 22:00:00 +01:00
image.flags |= IMAGE_HAS_ALPHA;
2008-10-20 22:00:00 +02:00
2008-10-19 22:00:00 +02:00
return FS_AddMipmapToPack( (byte *)(pin + 1), image.width, image.height );
}
2008-08-06 22:00:00 +02:00
/*
==============
Image_LoadWAL
==============
*/
bool Image_LoadWAL( const char *name, const byte *buffer, size_t filesize )
{
wal_t wal;
int pixels, ofs[4], mipsize;
int i, flags, value, contents; // wal additional parms
2008-11-13 22:00:00 +01:00
const byte *fin;
2008-08-06 22:00:00 +02:00
2009-08-02 22:00:00 +02:00
if( filesize < (int)sizeof( wal ))
2008-08-06 22:00:00 +02:00
{
MsgDev( D_ERROR, "Image_LoadWAL: file (%s) have invalid size\n", name );
return false;
}
2009-08-02 22:00:00 +02:00
Mem_Copy( &wal, buffer, sizeof( wal ));
2008-08-06 22:00:00 +02:00
flags = LittleLong(wal.flags);
value = LittleLong(wal.value);
contents = LittleLong(wal.contents);
2008-10-19 22:00:00 +02:00
image.width = LittleLong(wal.width);
image.height = LittleLong(wal.height);
2008-08-06 22:00:00 +02:00
for(i = 0; i < 4; i++) ofs[i] = LittleLong(wal.offsets[i]);
2008-10-12 22:00:00 +02:00
if(!Image_LumpValidSize( name )) return false;
2008-08-06 22:00:00 +02:00
2008-10-19 22:00:00 +02:00
pixels = image.width * image.height;
2008-08-06 22:00:00 +02:00
mipsize = (int)sizeof(wal) + ofs[0] + pixels;
if( pixels > 256 && filesize < mipsize )
{
// NOTE: wal's with dimensions < 32 have invalid sizes.
MsgDev( D_ERROR, "Image_LoadWAL: file (%s) have invalid size\n", name );
return false;
}
2009-07-22 22:00:00 +02:00
image.depth = 1;
2008-10-19 22:00:00 +02:00
image.type = PF_INDEXED_32; // 32-bit palete
2008-11-13 22:00:00 +01:00
fin = buffer + ofs[0];
// check for luma pixels
for( i = 0; i < image.width * image.height; i++ )
{
if( fin[i] > 208 && fin[i] < 240 )
{
image.flags |= IMAGE_HAS_LUMA_Q2;
break;
}
}
2008-08-06 22:00:00 +02:00
Image_GetPaletteQ2(); // hardcoded
2008-11-13 22:00:00 +01:00
return FS_AddMipmapToPack( fin, image.width, image.height );
2008-08-06 22:00:00 +02:00
}
/*
============
Image_LoadFLT
============
*/
bool Image_LoadFLT( const char *name, const byte *buffer, size_t filesize )
{
flat_t flat;
vfile_t *f;
2008-08-07 22:00:00 +02:00
bool result = false;
2008-10-19 22:00:00 +02:00
int trans_pixels = 0;
2008-08-06 22:00:00 +02:00
word column_loop, row_loop;
int i, column_offset, pointer_position, first_pos;
byte *Data, post, topdelta, length;
2008-08-08 22:00:00 +02:00
// wadsupport disabled, so nothing to load
if( Sys.app_name == HOST_NORMAL && !fs_wadsupport->integer )
return false;
2009-08-02 22:00:00 +02:00
if(filesize < (int)sizeof( flat ))
2008-08-06 22:00:00 +02:00
{
MsgDev( D_ERROR, "Image_LoadFLAT: file (%s) have invalid size\n", name );
return false;
}
// stupid copypaste from DevIL, but it works
f = VFS_Create( buffer, filesize );
first_pos = VFS_Tell( f );
VFS_Read(f, &flat, sizeof(flat));
2008-10-19 22:00:00 +02:00
image.width = LittleShort( flat.width );
image.height = LittleShort( flat.height );
2008-08-06 22:00:00 +02:00
flat.desc[0] = LittleShort( flat.desc[0] );
flat.desc[1] = LittleShort( flat.desc[1] );
2008-08-08 22:00:00 +02:00
if(!Image_LumpValidSize( name )) return false;
2008-10-19 22:00:00 +02:00
Data = (byte *)Mem_Alloc( Sys.imagepool, image.width * image.height );
2008-10-29 22:00:00 +01:00
Mem_Set( Data, 247, image.width * image.height ); // set default transparency
2009-07-22 22:00:00 +02:00
image.depth = 1;
2008-08-06 22:00:00 +02:00
2008-10-19 22:00:00 +02:00
for( column_loop = 0; column_loop < image.width; column_loop++ )
2008-08-06 22:00:00 +02:00
{
VFS_Read(f, &column_offset, sizeof(int));
pointer_position = VFS_Tell( f );
VFS_Seek( f, first_pos + column_offset, SEEK_SET );
while( 1 )
{
2008-08-08 22:00:00 +02:00
if(VFS_Read(f, &topdelta, 1) != 1) goto img_trunc;
if( topdelta == 255 ) break;
if(VFS_Read(f, &length, 1) != 1) goto img_trunc;
if(VFS_Read(f, &post, 1) != 1) goto img_trunc;
2008-08-06 22:00:00 +02:00
2008-10-19 22:00:00 +02:00
for( row_loop = 0; row_loop < length; row_loop++ )
2008-08-06 22:00:00 +02:00
{
2008-08-08 22:00:00 +02:00
if(VFS_Read(f, &post, 1) != 1) goto img_trunc;
2008-10-19 22:00:00 +02:00
if(row_loop + topdelta < image.height)
Data[(row_loop + topdelta) * image.width + column_loop] = post;
2008-08-06 22:00:00 +02:00
}
VFS_Read( f, &post, 1 );
}
2008-10-19 22:00:00 +02:00
VFS_Seek( f, pointer_position, SEEK_SET );
2008-08-06 22:00:00 +02:00
}
VFS_Close( f );
2008-08-08 22:00:00 +02:00
// swap colors in image, and check for transparency
2008-10-19 22:00:00 +02:00
for( i = 0; i < image.width * image.height; i++ )
2008-08-06 22:00:00 +02:00
{
if( Data[i] == 247 )
{
2008-08-08 22:00:00 +02:00
Data[i] = 255;
2008-10-19 22:00:00 +02:00
trans_pixels++;
2008-08-06 22:00:00 +02:00
}
2008-08-08 22:00:00 +02:00
else if( Data[i] == 255 ) Data[i] = 247;
2008-08-06 22:00:00 +02:00
}
2008-08-08 22:00:00 +02:00
// yes it's really transparent texture
2008-11-03 22:00:00 +01:00
// otherwise transparency it's a product of lazy designers (or painters ?)
if( trans_pixels > TRANS_THRESHOLD ) image.flags |= IMAGE_HAS_ALPHA;
2008-08-08 22:00:00 +02:00
2008-10-19 22:00:00 +02:00
image.type = PF_INDEXED_32; // 32-bit palete
2008-08-06 22:00:00 +02:00
Image_GetPaletteD1();
2008-10-19 22:00:00 +02:00
result = FS_AddMipmapToPack( Data, image.width, image.height );
2008-08-08 22:00:00 +02:00
if( Data ) Mem_Free( Data );
2008-08-07 22:00:00 +02:00
return result;
2008-08-08 22:00:00 +02:00
img_trunc:
VFS_Close( f );
Mem_Free( Data );
MsgDev( D_NOTE, "Image_LoadFLAT: probably it's not a .flat image)\n" );
return false;
2008-08-06 22:00:00 +02:00
}
/*
============
Image_LoadLMP
============
*/
bool Image_LoadLMP( const char *name, const byte *buffer, size_t filesize )
{
lmp_t lmp;
byte *fin, *pal;
2008-10-25 22:00:00 +02:00
int rendermode;
2008-08-06 22:00:00 +02:00
int pixels;
2008-08-08 22:00:00 +02:00
// wadsupport disabled, so nothing to load
if( Sys.app_name == HOST_NORMAL && !fs_wadsupport->integer )
return false;
2008-10-25 22:00:00 +02:00
if( filesize < sizeof( lmp ))
2008-08-06 22:00:00 +02:00
{
MsgDev( D_ERROR, "Image_LoadLMP: file (%s) have invalid size\n", name );
return false;
}
2008-10-25 22:00:00 +02:00
// greatest hack from id software
if( image.hint != IL_HINT_HL && com.stristr( name, "conchars" ))
{
image.width = image.height = 128;
2008-11-03 22:00:00 +01:00
image.flags |= IMAGE_HAS_ALPHA;
2008-10-25 22:00:00 +02:00
rendermode = LUMP_QFONT;
filesize += sizeof(lmp);
fin = (byte *)buffer;
}
else
{
fin = (byte *)buffer;
2009-08-02 22:00:00 +02:00
Mem_Copy( &lmp, fin, sizeof( lmp ));
2008-10-25 22:00:00 +02:00
image.width = LittleLong( lmp.width );
image.height = LittleLong( lmp.height );
rendermode = LUMP_NORMAL;
fin += sizeof(lmp);
}
2008-10-19 22:00:00 +02:00
pixels = image.width * image.height;
2008-08-06 22:00:00 +02:00
2008-10-25 22:00:00 +02:00
if( filesize < sizeof( lmp ) + pixels )
2008-08-06 22:00:00 +02:00
{
MsgDev( D_ERROR, "Image_LoadLMP: file (%s) have invalid size %d\n", name, filesize );
return false;
}
2008-10-19 22:00:00 +02:00
if(!Image_ValidSize( name )) return false;
2009-07-22 22:00:00 +02:00
image.depth = 1;
2008-08-06 22:00:00 +02:00
2008-10-19 22:00:00 +02:00
if( image.hint != IL_HINT_Q1 && filesize > (int)sizeof(lmp) + pixels )
2008-08-06 22:00:00 +02:00
{
2008-10-19 22:00:00 +02:00
int numcolors;
2008-08-06 22:00:00 +02:00
pal = fin + pixels;
numcolors = BuffLittleShort( pal );
if( numcolors != 256 ) pal = NULL; // corrupted lump ?
2008-10-19 22:00:00 +02:00
else pal += sizeof( short );
2008-08-06 22:00:00 +02:00
}
2008-10-19 22:00:00 +02:00
else if( image.hint != IL_HINT_HL ) pal = NULL;
else return false; // unknown mode rejected
2008-11-03 22:00:00 +01:00
if( fin[0] == 255 ) image.flags |= IMAGE_HAS_ALPHA;
2008-08-06 22:00:00 +02:00
2008-10-25 22:00:00 +02:00
Image_GetPaletteLMP( pal, rendermode );
2008-10-19 22:00:00 +02:00
image.type = PF_INDEXED_32; // 32-bit palete
return FS_AddMipmapToPack( fin, image.width, image.height );
2008-08-06 22:00:00 +02:00
}
/*
=============
Image_LoadMIP
=============
*/
bool Image_LoadMIP( const char *name, const byte *buffer, size_t filesize )
{
mip_t mip;
byte *fin, *pal;
int ofs[4], rendermode;
int i, pixels, numcolors;
2008-08-08 22:00:00 +02:00
// wadsupport disabled, so nothing to load
if( Sys.app_name == HOST_NORMAL && !fs_wadsupport->integer )
return false;
2008-08-06 22:00:00 +02:00
if( filesize < (int)sizeof(mip))
{
MsgDev( D_ERROR, "Image_LoadMIP: file (%s) have invalid size\n", name );
return false;
}
2009-08-02 22:00:00 +02:00
Mem_Copy( &mip, buffer, sizeof( mip ));
image.width = LittleLong( mip.width );
image.height = LittleLong( mip.height );
2008-11-03 22:00:00 +01:00
if(!Image_ValidSize( name )) return false;
2009-08-02 22:00:00 +02:00
for( i = 0; i < 4; i++ ) ofs[i] = LittleLong( mip.offsets[i] );
2008-10-19 22:00:00 +02:00
pixels = image.width * image.height;
2009-07-22 22:00:00 +02:00
image.depth = 1;
2008-08-06 22:00:00 +02:00
2008-10-27 22:00:00 +01:00
if( image.hint != IL_HINT_Q1 && filesize >= (int)sizeof(mip) + ((pixels * 85)>>6) + sizeof(short) + 768)
2008-08-06 22:00:00 +02:00
{
// half-life 1.0.0.1 mip version with palette
fin = (byte *)buffer + mip.offsets[0];
2008-10-19 22:00:00 +02:00
pal = (byte *)buffer + mip.offsets[0] + (((image.width * image.height) * 85)>>6);
2008-08-06 22:00:00 +02:00
numcolors = BuffLittleShort( pal );
2008-10-27 22:00:00 +01:00
if( numcolors != 256 ) pal = NULL; // corrupted mip ?
2008-08-06 22:00:00 +02:00
else pal += sizeof(short); // skip colorsize
// detect rendermode
2008-10-27 22:00:00 +01:00
if( com.strchr( name, '{' ))
2008-08-06 22:00:00 +02:00
{
2008-10-27 22:00:00 +01:00
rendermode = LUMP_TRANSPARENT;
2008-08-06 22:00:00 +02:00
// qlumpy used this color for transparent textures, otherwise it's decals
2008-10-27 22:00:00 +01:00
if( pal[255*3+0] == 0 && pal[255*3+1] == 0 && pal[255*3+2] == 255 );
2008-12-06 22:00:00 +01:00
else if(!( image.cmd_flags & IL_KEEP_8BIT ))
2008-11-27 22:00:00 +01:00
{
2008-12-06 22:00:00 +01:00
// apply decal palette immediately
2008-11-27 22:00:00 +01:00
image.flags |= IMAGE_COLORINDEX;
rendermode = LUMP_DECAL;
}
2008-11-03 22:00:00 +01:00
image.flags |= IMAGE_HAS_ALPHA;
2008-08-06 22:00:00 +02:00
}
else rendermode = LUMP_NORMAL;
}
2008-10-19 22:00:00 +02:00
else if( image.hint != IL_HINT_HL && filesize >= (int)sizeof(mip) + ((pixels * 85)>>6))
2008-08-06 22:00:00 +02:00
{
// quake1 1.01 mip version without palette
fin = (byte *)buffer + mip.offsets[0];
pal = NULL; // clear palette
rendermode = LUMP_NORMAL;
2008-11-13 22:00:00 +01:00
// check for luma pixels
for( i = 0; i < image.width * image.height; i++ )
{
if( fin[i] > 224 )
{
image.flags |= IMAGE_HAS_LUMA_Q1;
break;
}
}
2008-08-06 22:00:00 +02:00
}
else
{
2008-10-19 22:00:00 +02:00
if( image.hint == IL_HINT_NO )
MsgDev( D_ERROR, "Image_LoadMIP: lump (%s) is corrupted\n", name );
return false; // unknown or unsupported mode rejected
2008-08-06 22:00:00 +02:00
}
Image_GetPaletteLMP( pal, rendermode );
2008-10-19 22:00:00 +02:00
image.type = PF_INDEXED_32; // 32-bit palete
return FS_AddMipmapToPack( fin, image.width, image.height );
2008-08-06 22:00:00 +02:00
}