From 93480c026eaf52130e336cfb42c0484c8007d1ed Mon Sep 17 00:00:00 2001 From: John Hodge Date: Mon, 22 Jul 2013 20:30:23 +0800 Subject: [PATCH] Usermode/libimage_sif - Added endian-flipping support --- Usermode/Libraries/libimage_sif.so_src/main.c | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/Usermode/Libraries/libimage_sif.so_src/main.c b/Usermode/Libraries/libimage_sif.so_src/main.c index a5158562..61136a1c 100644 --- a/Usermode/Libraries/libimage_sif.so_src/main.c +++ b/Usermode/Libraries/libimage_sif.so_src/main.c @@ -27,6 +27,29 @@ int SoMain(void) return 0; } +static uint32_t flipendian32(uint32_t val) +{ + return ((val >> 24) & 0xFF) << 0 + | ((val >> 16) & 0xFF) << 8 + | ((val >> 8) & 0xFF) << 16 + | ((val >> 0) & 0xFF) << 24; +} + +static void flip_buffer_endian(void *Data, size_t SampleSize, size_t Pixels) +{ + if( SampleSize == 3 ) + return ; + + if( SampleSize == 4 ) + { + uint32_t *data32 = Data; + while( Pixels -- ) { + *data32 = flipendian32(*data32); + data32 ++; + } + } +} + /** */ tImage *Image_SIF_Parse(void *Buffer, size_t Size) @@ -94,14 +117,15 @@ tImage *Image_SIF_Parse(void *Buffer, size_t Size) switch(comp) { // Uncompressed 32-bpp data - case 0: - if( fileOfs + w*h*sampleSize > Size ) { - memcpy(ret->Data, Buffer+fileOfs, Size-fileOfs); - } - else { - memcpy(ret->Data, Buffer+fileOfs, w*h*sampleSize); - } + case 0: { + size_t idatsize = w*h*sampleSize; + if( idatsize > Size - fileOfs ) + idatsize = Size - fileOfs; + memcpy(ret->Data, Buffer+fileOfs, idatsize); + if(bRevOrder) + flip_buffer_endian(ret->Data, sampleSize, w*h); return ret; + } // 1.7.n*8 RLE // (1 Flag, 7-bit size, 32-bit value) @@ -138,6 +162,8 @@ tImage *Image_SIF_Parse(void *Buffer, size_t Size) fileOfs += sampleSize; } } + if(bRevOrder) + flip_buffer_endian(ret->Data, sampleSize, w*h); _SysDebug("Image_SIF_Parse: Complete at %i bytes", fileOfs); return ret; -- 2.20.1