8 //#include <image_sif.h>
29 tImage *Image_SIF_Parse(void *Buffer, size_t Size)
38 struct sHeader *hdr = Buffer;
40 _SysDebug("Image_SIF_Parse: (Buffer=%p, Size=0x%x)", Buffer, Size);
42 // Get magic word and determine byte ordering
43 if(hdr->Magic == 0x51F0) // Little Endian
45 else if(hdr->Magic == 0xF051) // Big Endian
51 _SysDebug(" Image_SIF_Parse: bRevOrder = %i", bRevOrder);
54 comp = hdr->Flags & 7;
55 fmt = (hdr->Flags >> 3) & 7;
61 _SysDebug(" Image_SIF_Parse: Dimensions %ix%i", w, h);
66 case 0: // ARGB 32-bit Little Endian
70 case 1: // RGB 24-bit big endian
78 _SysDebug(" Image_SIF_Parse: sampleSize = %i, fmt = %i", sampleSize, fmt);
80 fileOfs = sizeof(struct sHeader);
83 ret = calloc(1, sizeof(tImage) + w * h * sampleSize);
90 // Uncompressed 32-bpp data
92 if( fileOfs + w*h*sampleSize > Size ) {
93 memcpy(ret->Data, Buffer+fileOfs, Size-fileOfs);
96 memcpy(ret->Data, Buffer+fileOfs, w*h*sampleSize);
101 // (1 Flag, 7-bit size, 32-bit value)
107 if( fileOfs + 1 > Size ) return ret;
108 len = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
112 if( fileOfs + len*sampleSize > Size ) {
113 memcpy(ret->Data + ofs*sampleSize, Buffer+fileOfs, Size-fileOfs);
117 memcpy(ret->Data + ofs*sampleSize, Buffer+fileOfs, len*sampleSize);
123 uint8_t tmp[sampleSize];
125 if( fileOfs + sampleSize > Size ) return ret;
127 for(i=0;i<sampleSize;i++)
128 tmp[i] = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
132 for(i=0;i<sampleSize;i++)
133 ret->Data[ofs++] = tmp[i];
140 // - Each channel is separately 1.7 RLE compressed
142 // Alpha, Red, Green, Blue
143 for( i = 0; i < sampleSize; i++ )
146 while( ofs < w*h*sampleSize )
149 if( fileOfs + 1 > Size ) return ret;
150 len = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
154 if( fileOfs + 1 > Size ) return ret;
155 val = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
157 ret->Data[ofs] = val;
159 ret->Data[ofs] |= val;
164 if( fileOfs + 1 > Size ) return ret;
165 val = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
168 ret->Data[ofs] = val; ofs += sampleSize;
173 ret->Data[ofs] |= val; ofs += sampleSize;
182 fprintf(stderr, "Warning: Unknown compression scheme %i for SIF\n", comp);