Fix Endian dependent code when compiled without SDL or SDL2

Signed-off-by: Thomas Bernard <miniupnp@free.fr>
This commit is contained in:
Thomas Bernard 2018-06-28 14:28:53 +02:00
parent d7abcb36c9
commit c53b86d6c9
3 changed files with 141 additions and 138 deletions

View File

@ -61,6 +61,16 @@
#endif
#endif
#if !defined(WIN32) && !defined(USE_SDL) && !defined(USE_SDL2)
#if defined(__macosx__)
#include <machine/endian.h>
#elif defined(__FreeBSD__)
#include <sys/endian.h>
#else
#include <endian.h>
#endif
#endif
#include "errors.h"
#include "global.h"
#include "loadsave.h"
@ -1798,10 +1808,17 @@ void Load_IFF(T_IO_Context * context)
PCHG_palettes = prev_pal;
lineBitMask = (dword *)PCHGData;
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
#if defined(SDL_BYTEORDER) && (SDL_BYTEORDER != SDL_BIG_ENDIAN)
for (i = 0 ; i < ((LineCount + 31) >> 5); i++)
lineBitMask[i] = SDL_Swap32(lineBitMask[i]);
#endif
#elif defined(BYTE_ORDER) && (BYTE_ORDER != BIG_ENDIAN)
for (i = 0 ; i < ((LineCount + 31) >> 5); i++)
lineBitMask[i] = be32toh(lineBitMask[i]);
#elif defined(WIN32)
// assume WIN32 is little endian
for (i = 0 ; i < ((LineCount + 31) >> 5); i++)
lineBitMask[i] = __builtin_bswap32(lineBitMask[i]);
#endif
data = (const byte *)PCHGData + ((LineCount + 31) >> 5) * 4;
for (y_pos = 0 ; y_pos < LineCount; y_pos++)
{
@ -3068,21 +3085,25 @@ static void Load_BMP_Pixels(T_IO_Context * context, FILE * file, unsigned int co
break;
case 32:
{
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
#if defined(SDL_BYTEORDER) && (SDL_BYTEORDER != SDL_LIL_ENDIAN)
dword pixel = SDL_Swap32(((dword *)buffer)[x_pos]);
#else
#elif defined(BYTEORDER)
dword pixel = le32toh(((dword *)buffer)[x_pos]);
#else // default to little endian
dword pixel = ((dword *)buffer)[x_pos];
#endif
#endif
Set_pixel_24b(context, x_pos,target_y,Bitmap_mask(pixel,mask[0]),Bitmap_mask(pixel,mask[1]),Bitmap_mask(pixel,mask[2]));
}
break;
case 16:
{
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
#if defined(SDL_BYTEORDER) && (SDL_BYTEORDER != SDL_LIL_ENDIAN)
word pixel = SDL_Swap16(((word *)buffer)[x_pos]);
#else
#elif defined(BYTEORDER)
word pixel = le16toh(((word *)buffer)[x_pos]);
#else // default to little endian
word pixel = ((word *)buffer)[x_pos];
#endif
#endif
Set_pixel_24b(context, x_pos,target_y,Bitmap_mask(pixel,mask[0]),Bitmap_mask(pixel,mask[1]),Bitmap_mask(pixel,mask[2]));
}
break;

View File

@ -59,6 +59,16 @@
#include <sys/param.h>
#endif
#if !defined(WIN32)
#if defined(__macosx__)
#include <machine/endian.h>
#elif defined(__FreeBSD__)
#include <sys/endian.h>
#else
#include <endian.h>
#endif
#endif
#include "const.h"
#include "struct.h"
#include "global.h"
@ -557,7 +567,7 @@ int Init_program(int argc,char * argv[])
// iconv is used to convert filenames
cd = iconv_open(TOCODE, FROMCODE); // From UTF8 to ANSI
cd_inv = iconv_open(FROMCODE, TOCODE); // From ANSI to UTF8
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#if (defined(SDL_BYTEORDER) && (SDL_BYTEORDER == SDL_BIG_ENDIAN)) || (defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN))
cd_utf16 = iconv_open("UTF-16BE", FROMCODE); // From UTF8 to UTF16
cd_utf16_inv = iconv_open(FROMCODE, "UTF-16BE"); // From UTF16 to UTF8
#else

View File

@ -1463,10 +1463,9 @@ void PI1_16p_to_8b(byte * src,byte * dest)
//// DECODAGE de la PALETTE ////
void PI1_decode_palette(byte * src,byte * palette)
static void PI1_decode_palette(const byte * src, T_Components * palette)
{
int i; // Numéro de la couleur traitée
int ip; // index dans la palette
word w; // Word contenant le code
// Schéma d'un word =
@ -1475,36 +1474,22 @@ void PI1_decode_palette(byte * src,byte * palette)
// VVVV RRRR | 0000 BBBB
// 0321 0321 | 0321
ip=0;
for (i=0;i<16;i++)
{
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
w=(((word)src[(i*2)+1]<<8) | (src[(i*2)+0]));
// Traitement des couleurs rouge, verte et bleue:
palette[ip++]=(((w & 0x0007) << 1) | ((w & 0x0008) >> 3)) << 4;
palette[ip++]=(((w & 0x7000) >> 11) | ((w & 0x8000) >> 15)) << 4;
palette[ip++]=(((w & 0x0700) >> 7) | ((w & 0x0800) >> 11)) << 4;
#else
w=(((word)src[(i*2+1)])|(((word)src[(i*2)])<<8));
palette[ip++] = (((w & 0x0700)>>7) | ((w & 0x0800) >> 7))<<4 ;
palette[ip++]=(((w & 0x0070)>>3) | ((w & 0x0080) >> 3))<<4 ;
palette[ip++] = (((w & 0x0007)<<1) | ((w & 0x0008)))<<4 ;
#endif
w = (word)src[0] << 8 | (word)src[1];
src += 2;
palette[i].R = (((w & 0x0700)>>7) | ((w & 0x0800) >> 11)) * 0x11 ;
palette[i].G = (((w & 0x0070)>>3) | ((w & 0x0080) >> 7)) * 0x11 ;
palette[i].B = (((w & 0x0007)<<1) | ((w & 0x0008) >> 3)) * 0x11 ;
}
}
//// CODAGE de la PALETTE ////
void PI1_code_palette(byte * palette,byte * dest)
void PI1_code_palette(const T_Components * palette, byte * dest)
{
int i; // Numéro de la couleur traitée
int ip; // index dans la palette
word w; // Word contenant le code
// Schéma d'un word =
@ -1513,27 +1498,14 @@ void PI1_code_palette(byte * palette,byte * dest)
// VVVV RRRR | 0000 BBBB
// 0321 0321 | 0321
ip=0;
for (i=0;i<16;i++)
{
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
w = ((word)(palette[i].R & 0xe0) << 3) | ((word)(palette[i].R & 0x10) << 7);
w |= ((word)(palette[i].G & 0xe0) >> 1) | ((word)(palette[i].G & 0x10) << 3);
w |= ((word)(palette[i].B & 0xe0) >> 5) | ((word)(palette[i].B & 0x10) >> 1);
// Traitement des couleurs rouge, verte et bleue:
w =(((word)(palette[ip]>>2) & 0x38) >> 3) | (((word)(palette[ip]>>2) & 0x04) << 1); ip++;
w|=(((word)(palette[ip]>>2) & 0x38) << 9) | (((word)(palette[ip]>>2) & 0x04) << 13); ip++;
w|=(((word)(palette[ip]>>2) & 0x38) << 5) | (((word)(palette[ip]>>2) & 0x04) << 9); ip++;
dest[(i*2)+0]=w & 0x00FF;
dest[(i*2)+1]=(w>>8);
#else
w=(((word)(palette[ip]<<3))&0x0700);ip++;
w|=(((word)(palette[ip]>>1))&0x0070);ip++;
w|=(((word)(palette[ip]>>5))&0x0007);ip++;
dest[(i*2)+1]=w & 0x00FF;
dest[(i*2)+0]=(w>>8);
#endif
*dest++ = (w >> 8);
*dest++ = (w & 0xff);
}
}
@ -1654,7 +1626,7 @@ void Load_PI1(T_IO_Context * context)
// Initialisation de la palette
if (Config.Clear_palette)
memset(context->Palette,0,sizeof(T_Palette));
PI1_decode_palette(buffer+2,(byte *)context->Palette);
PI1_decode_palette(buffer+2, context->Palette);
// Chargement/décompression de l'image
ptr=buffer+34;
@ -1706,7 +1678,7 @@ void Save_PI1(T_IO_Context * context)
buffer[0]=0x00;
buffer[1]=0x00;
// Codage de la palette
PI1_code_palette((byte *)context->Palette,buffer+2);
PI1_code_palette(context->Palette, buffer+2);
// Codage de l'image
ptr=buffer+34;
for (y_pos=0;y_pos<200;y_pos++)
@ -1967,7 +1939,7 @@ void Load_PC1(T_IO_Context * context)
// Initialisation de la palette
if (Config.Clear_palette)
memset(context->Palette,0,sizeof(T_Palette));
PI1_decode_palette(buffercomp+2,(byte *)context->Palette);
PI1_decode_palette(buffercomp+2, context->Palette);
// Décompression du buffer
PC1_uncompress_packbits(buffercomp+34,bufferdecomp);
@ -2033,7 +2005,7 @@ void Save_PC1(T_IO_Context * context)
buffercomp[0]=0x80;
buffercomp[1]=0x00;
// Codage de la palette
PI1_code_palette((byte *)context->Palette,buffercomp+2);
PI1_code_palette(context->Palette, buffercomp+2);
// Codage de l'image
ptr=bufferdecomp;
for (y_pos=0;y_pos<200;y_pos++)
@ -2137,7 +2109,7 @@ void Load_NEO(T_IO_Context * context)
if (Config.Clear_palette)
memset(context->Palette,0,sizeof(T_Palette));
// on saute la résolution et le flag, chacun 2 bits
PI1_decode_palette(buffer+4,(byte *)context->Palette);
PI1_decode_palette(buffer+4, context->Palette);
// Chargement/décompression de l'image
ptr=buffer+128;
@ -2188,7 +2160,7 @@ void Save_NEO(T_IO_Context * context)
buffer[2]=0x00;
buffer[3]=0x00;
// Codage de la palette
PI1_code_palette((byte *)context->Palette,buffer+4);
PI1_code_palette(context->Palette, buffer+4);
// Codage de l'image
ptr=buffer+128;
for (y_pos=0;y_pos<200;y_pos++)