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
48 _SysDebug(" Image_SIF_Parse: Magic invalid (0x%x)", hdr->Magic);
52 _SysDebug(" Image_SIF_Parse: bRevOrder = %i", bRevOrder);
55 comp = hdr->Flags & 7;
56 fmt = (hdr->Flags >> 3) & 7;
62 _SysDebug(" Image_SIF_Parse: Dimensions %ix%i", w, h);
67 case 0: // ARGB 32-bit Little Endian
71 case 1: // RGB 24-bit big endian
79 _SysDebug(" Image_SIF_Parse: sampleSize = %i, fmt = %i", sampleSize, fmt);
81 fileOfs = sizeof(struct sHeader);
84 ret = calloc(1, sizeof(tImage) + w * h * sampleSize);
91 // Uncompressed 32-bpp data
93 if( fileOfs + w*h*sampleSize > Size ) {
94 memcpy(ret->Data, Buffer+fileOfs, Size-fileOfs);
97 memcpy(ret->Data, Buffer+fileOfs, w*h*sampleSize);
102 // (1 Flag, 7-bit size, 32-bit value)
108 if( fileOfs + 1 > Size ) return ret;
109 len = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
113 if( fileOfs + len*sampleSize > Size ) {
114 memcpy(ret->Data + ofs*sampleSize, Buffer+fileOfs, Size-fileOfs);
118 memcpy(ret->Data + ofs*sampleSize, Buffer+fileOfs, len*sampleSize);
124 uint8_t tmp[sampleSize];
126 if( fileOfs + sampleSize > Size ) return ret;
128 for(i=0;i<sampleSize;i++)
129 tmp[i] = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
133 for(i=0;i<sampleSize;i++)
134 ret->Data[ofs++] = tmp[i];
141 // - Each channel is separately 1.7 RLE compressed
143 // Alpha, Red, Green, Blue
144 for( i = 0; i < sampleSize; i++ )
147 while( ofs < w*h*sampleSize )
150 if( fileOfs + 1 > Size ) return ret;
151 len = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
155 if( fileOfs + 1 > Size ) return ret;
156 val = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
158 ret->Data[ofs] = val;
160 ret->Data[ofs] |= val;
165 if( fileOfs + 1 > Size ) return ret;
166 val = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
169 ret->Data[ofs] = val; ofs += sampleSize;
174 ret->Data[ofs] |= val; ofs += sampleSize;
183 fprintf(stderr, "Warning: Unknown compression scheme %i for SIF\n", comp);