X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Flibimage_sif.so_src%2Fmain.c;h=180668a56fe34b52632507164f4aeb2e435d53ee;hb=8695e2b9f65ab4411677d4eff57d0d80f6c26f9a;hp=455d3bced3d3f3f1585d6c1e0996668e1c5759ed;hpb=7edeed66f89e03c3de5fb7d3b41421f23c1bc4f7;p=tpg%2Facess2.git diff --git a/Usermode/Libraries/libimage_sif.so_src/main.c b/Usermode/Libraries/libimage_sif.so_src/main.c index 455d3bce..180668a5 100644 --- a/Usermode/Libraries/libimage_sif.so_src/main.c +++ b/Usermode/Libraries/libimage_sif.so_src/main.c @@ -6,6 +6,18 @@ #include #include //#include +#include // _SysDebug + +// === STRUCTURES === +struct sHeader +{ + uint16_t Magic; + uint16_t Flags; + uint16_t Width; + uint16_t Height; +}; + +// === CONSTANTS === // === CODE === int SoMain(void) @@ -17,57 +29,66 @@ int SoMain(void) */ tImage *Image_SIF_Parse(void *Buffer, size_t Size) { - uint16_t magic; - uint16_t flags; uint16_t w, h; int ofs, i; - tImage_SIF *ret; + tImage *ret; int bRevOrder; int fileOfs = 0; int comp, fmt; - int sampleSize = 4; + int sampleSize; + struct sHeader *hdr = Buffer; + + _SysDebug("Image_SIF_Parse: (Buffer=%p, Size=0x%x)", Buffer, Size); // Get magic word and determine byte ordering - magic = *(uint16_t*)Buffer+fileOfs; fileOfs += 2; - if(magic == 0x51F0) + if(hdr->Magic == 0x51F0) // Little Endian bRevOrder = 0; - else if(magic == 0xF051) + else if(hdr->Magic == 0xF051) // Big Endian bRevOrder = 1; else { + _SysDebug(" Image_SIF_Parse: Magic invalid (0x%x)", hdr->Magic); return NULL; } + _SysDebug(" Image_SIF_Parse: bRevOrder = %i", bRevOrder); + // Read flags - flags = *(uint16_t*)Buffer+fileOfs; fileOfs += 2; - comp = flags & 7; - fmt = (flags >> 3) & 7; + comp = hdr->Flags & 7; + fmt = (hdr->Flags >> 3) & 7; // Read dimensions - w = *(uint16_t*)Buffer+fileOfs; fileOfs += 2; - h = *(uint16_t*)Buffer+fileOfs; fileOfs += 2; + w = hdr->Width; + h = hdr->Height; - - // Allocate space - ret = calloc(1, sizeof(tImage) + w * h * sampleSize); - ret->Width = w; - ret->Height = h; + _SysDebug(" Image_SIF_Parse: Dimensions %ix%i", w, h); // Get image format switch(fmt) { - case 0: // ARGB - ret->Format = IMGFMT_ARGB; + case 0: // ARGB 32-bit Little Endian + fmt = IMGFMT_BGRA; sampleSize = 4; break; - case 1: // RGB - ret->Format = IMGFMT_RGB; + case 1: // RGB 24-bit big endian + fmt = IMGFMT_RGB; sampleSize = 3; break; default: - free(ret); return NULL; } + _SysDebug(" Image_SIF_Parse: sampleSize = %i, fmt = %i", sampleSize, fmt); + + fileOfs = sizeof(struct sHeader); + + // Allocate space + ret = calloc(1, sizeof(tImage) + w * h * sampleSize); + ret->Width = w; + ret->Height = h; + ret->Format = fmt; + for( ofs = 0; ofs < w*h*sampleSize; ofs ++ ) + ret->Data[ofs] = 255; + switch(comp) { // Uncompressed 32-bpp data @@ -84,40 +105,42 @@ tImage *Image_SIF_Parse(void *Buffer, size_t Size) // (1 Flag, 7-bit size, 32-bit value) case 1: ofs = 0; - while( ofs < w*h ) + while( ofs < w*h*sampleSize ) { uint8_t len; - if( fileOfs + 1 > Size ) return ret; - len = *(uint8_t*)Buffer+fileOfs; fileOfs += 1; + if( fileOfs + 1 > Size ) + return ret; + len = ((uint8_t*)Buffer)[fileOfs++]; + // Verbatim if(len & 0x80) { len &= 0x7F; - if( fileOfs + len*sampleSize > Size ) { - memcpy(ret->Data + ofs*sampleSize, Buffer+fileOfs, Size-fileOfs); - return ret; - } - else { - memcpy(ret->Data + ofs*sampleSize, Buffer+fileOfs, len*sampleSize); + while( len -- ) + { + if( fileOfs + sampleSize > Size ) + return ret; + memcpy(ret->Data+ofs, Buffer+fileOfs, sampleSize); + ofs += sampleSize; + fileOfs += sampleSize; } - ofs += len; } + // RLE else { - uint8_t tmp[sampleSize]; - - if( fileOfs + sampleSize > Size ) return ret; - - for(i=0;i Size ) + return ret; - i = 0; - while(len--) { - for(i=0;iData[ofs++] = tmp[i]; + while( len -- ) + { + memcpy(ret->Data+ofs, Buffer+fileOfs, sampleSize); + ofs += sampleSize; } + fileOfs += sampleSize; } } + _SysDebug("Image_SIF_Parse: Complete at %i bytes", fileOfs); return ret; // Channel 1.7.8 RLE + // - Each channel is separately 1.7 RLE compressed case 3: // Alpha, Red, Green, Blue for( i = 0; i < sampleSize; i++ ) @@ -127,31 +150,22 @@ tImage *Image_SIF_Parse(void *Buffer, size_t Size) { uint8_t len, val; if( fileOfs + 1 > Size ) return ret; - len = *(uint8_t*)Buffer+fileOfs; fileOfs += 1; + len = ((uint8_t*)Buffer)[fileOfs++]; if(len & 0x80) { len &= 0x7F; while(len--) { if( fileOfs + 1 > Size ) return ret; - val = *(uint8_t*)Buffer+fileOfs; fileOfs += 1; - if(i == 0) - ret->Data[ofs] = val; - else - ret->Data[ofs] |= val; + val = ((uint8_t*)Buffer)[fileOfs++]; + ret->Data[ofs] = val; ofs += sampleSize; } } else { if( fileOfs + 1 > Size ) return ret; - val = *(uint8_t*)Buffer+fileOfs; fileOfs += 1; - if(i == 0) { - while(len--) { - ret->Data[ofs] = val; ofs += sampleSize; - } - } - else { - while(len--) { - ret->Data[ofs] |= val; ofs += sampleSize; - } + val = ((uint8_t*)Buffer)[fileOfs++]; + while(len--) { + ret->Data[ofs] = val; + ofs += sampleSize; } } }