diff --git a/engine/common/imagelib/img_bmp.c b/engine/common/imagelib/img_bmp.c index 90b3f65b..b9af050b 100644 --- a/engine/common/imagelib/img_bmp.c +++ b/engine/common/imagelib/img_bmp.c @@ -16,6 +16,24 @@ GNU General Public License for more details. #include "imagelib.h" #include "xash3d_mathlib.h" #include "img_bmp.h" +#include "swaplib.h" + +le_struct_begin( bmp_swap ) + le_struct_field( bmp_t, fileSize ) + le_struct_field( bmp_t, reserved0 ) + le_struct_field( bmp_t, bitmapDataOffset ) + le_struct_field( bmp_t, bitmapHeaderSize ) + le_struct_field( bmp_t, width ) + le_struct_field( bmp_t, height ) + le_struct_field( bmp_t, planes ) + le_struct_field( bmp_t, bitsPerPixel ) + le_struct_field( bmp_t, compression ) + le_struct_field( bmp_t, bitmapDataSize ) + le_struct_field( bmp_t, hRes ) + le_struct_field( bmp_t, vRes ) + le_struct_field( bmp_t, colors ) + le_struct_field( bmp_t, importantColors ) +le_struct_end(); /* ============= @@ -41,6 +59,7 @@ qboolean Image_LoadBMP( const char *name, const byte *buffer, fs_offset_t filesi buf_p = (byte *)buffer; memcpy( &bhdr, buf_p, sizeof( bmp_t )); + le_struct_swap( bmp_swap, &bhdr ); buf_p += BI_FILE_HEADER_SIZE + bhdr.bitmapHeaderSize; // bogus file header check @@ -282,7 +301,8 @@ qboolean Image_LoadBMP( const char *name, const byte *buffer, fs_offset_t filesi } break; case 16: - shortPixel = *(word *)buf_p, buf_p += 2; + shortPixel = buf_p[0] | (buf_p[1] << 8); + buf_p += 2; *pixbuf++ = blue = (shortPixel & ( 31 << 10 )) >> 7; *pixbuf++ = green = (shortPixel & ( 31 << 5 )) >> 2; *pixbuf++ = red = (shortPixel & ( 31 )) << 3; @@ -396,7 +416,9 @@ qboolean Image_SaveBMP( const char *name, rgbdata_t *pix ) hdr.colors = ( pixel_size == 1 ) ? 256 : 0; hdr.importantColors = 0; + le_struct_swap( bmp_swap, &hdr ); FS_Write( pfile, &hdr, sizeof( bmp_t )); + le_struct_swap( bmp_swap, &hdr ); pbBmpBits = Mem_Malloc( host.imagepool, cbBmpBits ); diff --git a/engine/common/imagelib/img_dds.c b/engine/common/imagelib/img_dds.c index 9678c348..3d4ba7ae 100644 --- a/engine/common/imagelib/img_dds.c +++ b/engine/common/imagelib/img_dds.c @@ -16,6 +16,49 @@ GNU General Public License for more details. #include "imagelib.h" #include "xash3d_mathlib.h" #include "img_dds.h" +#include "swaplib.h" + +le_struct_begin( dds_pixf_swap ) + le_struct_field( dds_pixf_t, dwSize ) + le_struct_field( dds_pixf_t, dwFlags ) + le_struct_field( dds_pixf_t, dwFourCC ) + le_struct_field( dds_pixf_t, dwRGBBitCount ) + le_struct_field( dds_pixf_t, dwRBitMask ) + le_struct_field( dds_pixf_t, dwGBitMask ) + le_struct_field( dds_pixf_t, dwBBitMask ) + le_struct_field( dds_pixf_t, dwABitMask ) +le_struct_end(); + +le_struct_begin( dds_caps_swap ) + le_struct_field( dds_caps_t, dwCaps1 ) + le_struct_field( dds_caps_t, dwCaps2 ) + le_struct_field( dds_caps_t, dwCaps3 ) + le_struct_field( dds_caps_t, dwCaps4 ) +le_struct_end(); + +le_struct_begin( dds_swap ) + le_struct_field( dds_t, dwIdent ) + le_struct_field( dds_t, dwSize ) + le_struct_field( dds_t, dwFlags ) + le_struct_field( dds_t, dwHeight ) + le_struct_field( dds_t, dwWidth ) + le_struct_field( dds_t, dwLinearSize ) + le_struct_field( dds_t, dwDepth ) + le_struct_field( dds_t, dwMipMapCount ) + le_struct_field( dds_t, dwAlphaBitDepth ) + le_struct_array( dds_t, dwReserved1, 10 ) + le_struct_child( dds_t, dsPixelFormat, dds_pixf_swap ) + le_struct_child( dds_t, dsCaps, dds_caps_swap ) + le_struct_field( dds_t, dwTextureStage ) +le_struct_end(); + +le_struct_begin( dds_dxt10_swap ) + le_struct_field( dds_header_dxt10_t, dxgiFormat ) + le_struct_field( dds_header_dxt10_t, resourceDimension ) + le_struct_field( dds_header_dxt10_t, miscFlag ) + le_struct_field( dds_header_dxt10_t, arraySize ) + le_struct_field( dds_header_dxt10_t, miscFlags2 ) +le_struct_end(); static qboolean Image_CheckDXT3Alpha( dds_t *hdr, byte *fin ) { @@ -295,6 +338,7 @@ qboolean Image_LoadDDS( const char *name, const byte *buffer, fs_offset_t filesi return false; memcpy( &header, buffer, sizeof( header )); + le_struct_swap( dds_swap, &header ); if( header.dwIdent != DDSHEADER ) return false; // it's not a dds file, just skip it @@ -315,6 +359,7 @@ qboolean Image_LoadDDS( const char *name, const byte *buffer, fs_offset_t filesi if( header.dsPixelFormat.dwFourCC == TYPE_DX10 ) { memcpy( &header2, buffer + sizeof( header ), sizeof( header2 )); + le_struct_swap( dds_dxt10_swap, &header2 ); headersOffset += sizeof( header2 ); } diff --git a/engine/common/imagelib/img_ktx2.c b/engine/common/imagelib/img_ktx2.c index 998d82b2..b5d0c075 100644 --- a/engine/common/imagelib/img_ktx2.c +++ b/engine/common/imagelib/img_ktx2.c @@ -16,6 +16,34 @@ GNU General Public License for more details. #include "imagelib.h" #include "xash3d_mathlib.h" #include "img_ktx2.h" +#include "swaplib.h" + +le_struct_begin( ktx2_header_swap ) + le_struct_field( ktx2_header_t, vkFormat ) + le_struct_field( ktx2_header_t, typeSize ) + le_struct_field( ktx2_header_t, pixelWidth ) + le_struct_field( ktx2_header_t, pixelHeight ) + le_struct_field( ktx2_header_t, pixelDepth ) + le_struct_field( ktx2_header_t, layerCount ) + le_struct_field( ktx2_header_t, faceCount ) + le_struct_field( ktx2_header_t, levelCount ) + le_struct_field( ktx2_header_t, supercompressionScheme ) +le_struct_end(); + +le_struct_begin( ktx2_index_swap ) + le_struct_field( ktx2_index_t, dfdByteOffset ) + le_struct_field( ktx2_index_t, dfdByteLength ) + le_struct_field( ktx2_index_t, kvdByteOffset ) + le_struct_field( ktx2_index_t, kvdByteLength ) + le_struct_field( ktx2_index_t, sgdByteOffset ) + le_struct_field( ktx2_index_t, sgdByteLength ) +le_struct_end(); + +le_struct_begin( ktx2_level_swap ) + le_struct_field( ktx2_level_t, byteOffset ) + le_struct_field( ktx2_level_t, byteLength ) + le_struct_field( ktx2_level_t, uncompressedByteLength ) +le_struct_end(); static void Image_KTX2Format( uint32_t ktx2_format ) { @@ -125,6 +153,7 @@ static qboolean Image_KTX2Parse( const ktx2_header_t *header, const byte *buffer } memcpy( &index, buffer + KTX2_IDENTIFIER_SIZE + sizeof( ktx2_header_t ), sizeof( index )); + le_struct_swap( ktx2_index_swap, &index ); for( mip = 0; mip < header->levelCount; ++mip ) { @@ -134,6 +163,7 @@ static qboolean Image_KTX2Parse( const ktx2_header_t *header, const byte *buffer ktx2_level_t level; memcpy( &level, levels_begin + mip * sizeof( level ), sizeof( level )); + le_struct_swap( ktx2_level_swap, &level ); if( mip_size != level.byteLength ) { @@ -159,6 +189,7 @@ static qboolean Image_KTX2Parse( const ktx2_header_t *header, const byte *buffer { ktx2_level_t level; memcpy( &level, levels_begin + mip * sizeof( level ), sizeof( level )); + le_struct_swap( ktx2_level_swap, &level ); memcpy( image.rgba + cursor, buffer + level.byteOffset, level.byteLength ); cursor += level.byteLength; } @@ -180,6 +211,7 @@ qboolean Image_LoadKTX2( const char *name, const byte *buffer, fs_offset_t files } memcpy( &header, buffer + KTX2_IDENTIFIER_SIZE, sizeof( header )); + le_struct_swap( ktx2_header_swap, &header ); image.width = header.pixelWidth; image.height = header.pixelHeight; diff --git a/engine/common/imagelib/img_tga.c b/engine/common/imagelib/img_tga.c index fb622e95..3d266be6 100644 --- a/engine/common/imagelib/img_tga.c +++ b/engine/common/imagelib/img_tga.c @@ -16,6 +16,16 @@ GNU General Public License for more details. #include "imagelib.h" #include "xash3d_mathlib.h" #include "img_tga.h" +#include "swaplib.h" + +le_struct_begin( tga_swap ) + le_struct_field( tga_t, colormap_index ) + le_struct_field( tga_t, colormap_length ) + le_struct_field( tga_t, x_origin ) + le_struct_field( tga_t, y_origin ) + le_struct_field( tga_t, width ) + le_struct_field( tga_t, height ) +le_struct_end(); /* ============= @@ -44,10 +54,10 @@ qboolean Image_LoadTGA( const char *name, const byte *buffer, fs_offset_t filesi targa_header.colormap_index = buf_p[0] + buf_p[1] * 256; buf_p += 2; targa_header.colormap_length = buf_p[0] + buf_p[1] * 256; buf_p += 2; targa_header.colormap_size = *buf_p; buf_p += 1; - targa_header.x_origin = *(short *)buf_p; buf_p += 2; - targa_header.y_origin = *(short *)buf_p; buf_p += 2; - targa_header.width = image.width = *(short *)buf_p; buf_p += 2; - targa_header.height = image.height = *(short *)buf_p; buf_p += 2; + targa_header.x_origin = buf_p[0] + buf_p[1] * 256; buf_p += 2; + targa_header.y_origin = buf_p[0] + buf_p[1] * 256; buf_p += 2; + targa_header.width = image.width = buf_p[0] + buf_p[1] * 256; buf_p += 2; + targa_header.height = image.height = buf_p[0] + buf_p[1] * 256; buf_p += 2; targa_header.pixel_size = *buf_p++; targa_header.attributes = *buf_p++; if( targa_header.id_length != 0 ) buf_p += targa_header.id_length; // skip TARGA image comment @@ -284,6 +294,7 @@ qboolean Image_SaveTGA( const char *name, rgbdata_t *pix ) out = buffer; + le_struct_swap( tga_swap, &targa_header ); memcpy( out, &targa_header, sizeof( tga_t ) ); out += sizeof( tga_t ); diff --git a/engine/common/imagelib/img_wad.c b/engine/common/imagelib/img_wad.c index fa045f53..eb12bfc1 100644 --- a/engine/common/imagelib/img_wad.c +++ b/engine/common/imagelib/img_wad.c @@ -19,6 +19,43 @@ GNU General Public License for more details. #include "studio.h" #include "sprite.h" #include "qfont.h" +#include "swaplib.h" + +le_struct_begin( charinfo_swap ) + le_struct_field( charinfo, startoffset ) + le_struct_field( charinfo, charwidth ) +le_struct_end(); + +le_struct_begin( qfont_swap ) + le_struct_field( qfont_t, width ) + le_struct_field( qfont_t, height ) + le_struct_field( qfont_t, rowcount ) + le_struct_field( qfont_t, rowheight ) + le_struct_array_child( qfont_t, fontinfo, charinfo_swap, NUM_GLYPHS ) +le_struct_end(); + +le_struct_begin( lmp_swap ) + le_struct_field( lmp_t, width ) + le_struct_field( lmp_t, height ) +le_struct_end(); + +le_struct_begin( mip_swap ) + le_struct_field( mip_t, width ) + le_struct_field( mip_t, height ) + le_struct_array( mip_t, offsets, 4 ) +le_struct_end(); + +le_struct_begin( dwadinfo_swap ) + le_struct_field( dwadinfo_t, ident ) + le_struct_field( dwadinfo_t, numlumps ) + le_struct_field( dwadinfo_t, infotableofs ) +le_struct_end(); + +le_struct_begin( dlumpinfo_swap ) + le_struct_field( dlumpinfo_t, filepos ) + le_struct_field( dlumpinfo_t, disksize ) + le_struct_field( dlumpinfo_t, size ) +le_struct_end(); /* ============ @@ -98,6 +135,7 @@ qboolean Image_LoadFNT( const char *name, const byte *buffer, fs_offset_t filesi return false; memcpy( &font, buffer, sizeof( font )); + le_struct_swap( qfont_swap, &font ); // last sixty four bytes - what the hell ???? size = sizeof( qfont_t ) - 4 + ( font.height * font.width * QCHAR_WIDTH ) + sizeof( short ) + 768 + 64; @@ -120,7 +158,8 @@ qboolean Image_LoadFNT( const char *name, const byte *buffer, fs_offset_t filesi fin = buffer + sizeof( font ) - 4; pal = fin + (image.width * image.height); - numcolors = *(short *)pal, pal += sizeof( short ); + numcolors = pal[0] | (pal[1] << 8); + pal += sizeof( short ); if( numcolors == 768 || numcolors == 256 ) { @@ -305,6 +344,7 @@ qboolean Image_LoadLMP( const char *name, const byte *buffer, fs_offset_t filesi { fin = (byte *)buffer; memcpy( &lmp, fin, sizeof( lmp )); + le_struct_swap( lmp_swap, &lmp ); image.width = lmp.width; image.height = lmp.height; rendermode = LUMP_NORMAL; @@ -337,7 +377,7 @@ qboolean Image_LoadLMP( const char *name, const byte *buffer, fs_offset_t filesi } } pal = fin + pixels; - numcolors = *(short *)pal; + numcolors = pal[0] | (pal[1] << 8); if( numcolors != 256 ) pal = NULL; // corrupted lump ? else pal += sizeof( short ); } @@ -397,6 +437,7 @@ qboolean Image_LoadMIP( const char *name, const byte *buffer, fs_offset_t filesi return false; memcpy( &mip, buffer, sizeof( mip )); + le_struct_swap( mip_swap, &mip ); image.width = mip.width; image.height = mip.height; @@ -411,7 +452,7 @@ qboolean Image_LoadMIP( const char *name, const byte *buffer, fs_offset_t filesi // half-life 1.0.0.1 mip version with palette fin = (byte *)buffer + mip.offsets[0]; pal = (byte *)buffer + mip.offsets[0] + (((image.width * image.height) * 85)>>6); - numcolors = *(short *)pal; + numcolors = pal[0] | (pal[1] << 8); if( numcolors != 256 ) pal = NULL; // corrupted mip ? else pal += sizeof( short ); // skip colorsize @@ -596,24 +637,23 @@ Image_LoadWAD */ qboolean Image_LoadWAD( const char *name, const byte *buffer, fs_offset_t filesize ) { - const dwadinfo_t *header; - const dlumpinfo_t *lumps; + dwadinfo_t whdr; const unsigned char *mipdata; int i, j; if( !buffer || filesize < sizeof( dwadinfo_t )) return false; - header = (const dwadinfo_t *)buffer; - if( header->numlumps <= 0 || header->infotableofs <= 0 || header->infotableofs >= (int)filesize ) + memcpy( &whdr, buffer, sizeof( whdr )); + le_struct_swap( dwadinfo_swap, &whdr ); + if( whdr.numlumps <= 0 || whdr.infotableofs <= 0 || whdr.infotableofs >= (int)filesize ) return false; - lumps = (const dlumpinfo_t *)((const unsigned char *)buffer + header->infotableofs ); - - for( i = 0; i < header->numlumps; ++i ) + for( i = 0; i < whdr.numlumps; ++i ) { const unsigned char *pixels, *palette, *use_palette; unsigned char grad_palette[256 * 3]; + dlumpinfo_t lump; int mip_size; mip_t mip; uint32_t width, height, offset0; @@ -623,16 +663,20 @@ qboolean Image_LoadWAD( const char *name, const byte *buffer, fs_offset_t filesi float t; byte idx; - if( lumps[i].type != TYP_MIPTEX && lumps[i].type != TYP_PALETTE ) + memcpy( &lump, buffer + whdr.infotableofs + i * sizeof( dlumpinfo_t ), sizeof( lump )); + le_struct_swap( dlumpinfo_swap, &lump ); + + if( lump.type != TYP_MIPTEX && lump.type != TYP_PALETTE ) continue; // get lump data and validate - mipdata = (const unsigned char *)buffer + lumps[i].filepos; - mip_size = lumps[i].disksize; - if( lumps[i].filepos < 0 || lumps[i].filepos + mip_size > (int)filesize ) + mipdata = (const unsigned char *)buffer + lump.filepos; + mip_size = lump.disksize; + if( lump.filepos < 0 || lump.filepos + mip_size > (int)filesize ) continue; memcpy( &mip, mipdata, sizeof( mip )); + le_struct_swap( mip_swap, &mip ); width = mip.width; height = mip.height; @@ -652,7 +696,7 @@ qboolean Image_LoadWAD( const char *name, const byte *buffer, fs_offset_t filesi use_palette = palette; // handle gradient palette - if( lumps[i].type == TYP_PALETTE ) + if( lump.type == TYP_PALETTE ) { // gradient palette const unsigned char *frontColorPtr = palette + 255 * 3; @@ -755,12 +799,16 @@ qboolean Image_SaveWAD( const char *name, rgbdata_t *pix ) header.ident = IDWAD3HEADER; header.numlumps = 1; + le_struct_swap( dwadinfo_swap, &header ); FS_Write( f, &header, sizeof( header )); + le_struct_swap( mip_swap, &miptex ); FS_Write( f, &miptex, sizeof( mip_t )); + le_struct_swap( mip_swap, &miptex ); FS_Write( f, pix->buffer, m0size ); FS_Write( f, mip1_data, m1size ); FS_Write( f, mip2_data, m2size ); FS_Write( f, mip3_data, m3size ); + palette_size = LittleShort( palette_size ); FS_Write( f, &palette_size, sizeof( short )); if( lump_type == TYP_PALETTE ) @@ -793,10 +841,11 @@ qboolean Image_SaveWAD( const char *name, rgbdata_t *pix ) lump.type = (char)lump_type; lump.attribs = 0; Q_strncpy( lump.name, "tempdecal", sizeof( lump.name )); + le_struct_swap( dlumpinfo_swap, &lump ); FS_Write( f, &lump, sizeof( lump )); FS_Seek( f, offsetof( dwadinfo_t, infotableofs ), SEEK_SET ); - infotableofs32 = (int)infotableofs; + infotableofs32 = LittleLong((int)infotableofs ); FS_Write( f, &infotableofs32, sizeof( int )); FS_Close( f );