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);
89 for( ofs = 0; ofs < w*h*sampleSize; ofs ++ )
94 // Uncompressed 32-bpp data
96 if( fileOfs + w*h*sampleSize > Size ) {
97 memcpy(ret->Data, Buffer+fileOfs, Size-fileOfs);
100 memcpy(ret->Data, Buffer+fileOfs, w*h*sampleSize);
105 // (1 Flag, 7-bit size, 32-bit value)
108 while( ofs < w*h*sampleSize )
111 if( fileOfs + 1 > Size )
113 len = ((uint8_t*)Buffer)[fileOfs++];
119 if( fileOfs + sampleSize > Size )
121 memcpy(ret->Data+ofs, Buffer+fileOfs, sampleSize);
123 fileOfs += sampleSize;
128 if( fileOfs + sampleSize > Size )
133 memcpy(ret->Data+ofs, Buffer+fileOfs, sampleSize);
136 fileOfs += sampleSize;
139 _SysDebug("Image_SIF_Parse: Complete at %i bytes", fileOfs);
143 // - Each channel is separately 1.7 RLE compressed
145 // Alpha, Red, Green, Blue
146 for( i = 0; i < sampleSize; i++ )
149 while( ofs < w*h*sampleSize )
152 if( fileOfs + 1 > Size ) return ret;
153 len = ((uint8_t*)Buffer)[fileOfs++];
157 if( fileOfs + 1 > Size ) return ret;
158 val = ((uint8_t*)Buffer)[fileOfs++];
159 ret->Data[ofs] = val;
164 if( fileOfs + 1 > Size ) return ret;
165 val = ((uint8_t*)Buffer)[fileOfs++];
167 ret->Data[ofs] = val;
176 fprintf(stderr, "Warning: Unknown compression scheme %i for SIF\n", comp);