8 //#include <image_sif.h>
9 #include <acess/sys.h> // _SysDebug
30 tImage *Image_SIF_Parse(void *Buffer, size_t Size)
39 struct sHeader *hdr = Buffer;
41 _SysDebug("Image_SIF_Parse: (Buffer=%p, Size=0x%x)", Buffer, Size);
43 // Get magic word and determine byte ordering
44 if(hdr->Magic == 0x51F0) // Little Endian
46 else if(hdr->Magic == 0xF051) // Big Endian
49 _SysDebug(" Image_SIF_Parse: Magic invalid (0x%x)", hdr->Magic);
53 _SysDebug(" Image_SIF_Parse: bRevOrder = %i", bRevOrder);
56 comp = hdr->Flags & 7;
57 fmt = (hdr->Flags >> 3) & 7;
63 _SysDebug(" Image_SIF_Parse: Dimensions %ix%i", w, h);
68 case 0: // ARGB 32-bit Little Endian
72 case 1: // RGB 24-bit big endian
80 _SysDebug(" Image_SIF_Parse: sampleSize = %i, fmt = %i", sampleSize, fmt);
82 fileOfs = sizeof(struct sHeader);
85 ret = calloc(1, sizeof(tImage) + w * h * sampleSize);
92 // Uncompressed 32-bpp data
94 if( fileOfs + w*h*sampleSize > Size ) {
95 memcpy(ret->Data, Buffer+fileOfs, Size-fileOfs);
98 memcpy(ret->Data, Buffer+fileOfs, w*h*sampleSize);
103 // (1 Flag, 7-bit size, 32-bit value)
109 if( fileOfs + 1 > Size ) return ret;
110 len = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
114 if( fileOfs + len*sampleSize > Size ) {
115 memcpy(ret->Data + ofs*sampleSize, Buffer+fileOfs, Size-fileOfs);
119 memcpy(ret->Data + ofs*sampleSize, Buffer+fileOfs, len*sampleSize);
125 uint8_t tmp[sampleSize];
127 if( fileOfs + sampleSize > Size ) return ret;
129 for(i=0;i<sampleSize;i++)
130 tmp[i] = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
134 for(i=0;i<sampleSize;i++)
135 ret->Data[ofs++] = tmp[i];
142 // - Each channel is separately 1.7 RLE compressed
144 // Alpha, Red, Green, Blue
145 for( i = 0; i < sampleSize; i++ )
148 while( ofs < w*h*sampleSize )
151 if( fileOfs + 1 > Size ) return ret;
152 len = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
156 if( fileOfs + 1 > Size ) return ret;
157 val = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
159 ret->Data[ofs] = val;
161 ret->Data[ofs] |= val;
166 if( fileOfs + 1 > Size ) return ret;
167 val = *(uint8_t*)Buffer+fileOfs; fileOfs += 1;
170 ret->Data[ofs] = val; ofs += sampleSize;
175 ret->Data[ofs] |= val; ofs += sampleSize;
184 fprintf(stderr, "Warning: Unknown compression scheme %i for SIF\n", comp);