1 namespace gfx::bitmaps;
7 static int ReadData(GifFileType * gifFile, GifByteType * bytes, int size)
9 File f = gifFile->UserData;
10 return f.Read(bytes, 1, size);
13 /*static int WriteData(GifFileType * gifFile, GifByteType * bytes, int size)
15 File f = gifFile->UserData;
16 return f.Write(bytes, 1, size);
19 static const char * extensions[] = { "gif", null };
21 class GIFFormat : BitmapFormat
23 class_property(extensions) = extensions;
25 bool Load(Bitmap bitmap, File f)
30 GifFileType * gifFile = DGifOpen(f, ReadData, null);
32 GifFileType * gifFile = DGifOpen(f, ReadData);
37 if(bitmap.Allocate(null, gifFile->SWidth, gifFile->SHeight, 0, pixelFormat888, false))
40 ColorAlpha * picture = (ColorAlpha *)bitmap.picture;
42 struct SavedImage * image = &gifFile->SavedImages[0];
44 ColorMapObject * colorMap = gifFile->SColorMap;
46 if(!colorMap) colorMap = gifFile->Image.ColorMap;
48 for(c = 0; c<image->ExtensionBlockCount; c++)
50 ExtensionBlock * block = &image->ExtensionBlocks[c];
51 if(block->Function == 249)
54 transparent = (byte)block->Bytes[3];
59 int colorCount = colorMap->ColorCount;
60 if(!gifFile->Image.Interlace)
62 for(y = 0; y < gifFile->SHeight; y++)
65 for(x = 0; x < gifFile->SWidth; x++)
67 byte index = image->RasterBits[y*gifFile->SWidth+x];
68 if(index == transparent || index >= colorCount)
69 picture[y * bitmap.stride + x] = 0; //&= 0xFFFFFF;
71 picture[y * bitmap.stride + x] = ColorAlpha { 255, {
72 colorMap->Colors[index].Red,
73 colorMap->Colors[index].Green,
74 colorMap->Colors[index].Blue } };
80 int startline[4] = {0, 4, 2, 1};
81 int offset[4] = {8, 8, 4, 2};
84 for(group=0,i=0;group<4;group++)
87 for(y = startline[group]; y < gifFile->SHeight; y += offset[group], i++)
90 for(x = 0; x < gifFile->SWidth; x++)
92 byte index = image->RasterBits[i*gifFile->SWidth+x];
93 if(index == transparent)
94 picture[y * bitmap.stride + x] = 0;// &= 0xFFFFFF;
96 picture[y * bitmap.stride + x] = ColorAlpha { 255, {
97 colorMap->Colors[index].Red,
98 colorMap->Colors[index].Green,
99 colorMap->Colors[index].Blue } };
104 bitmap.transparent = (transparent != -1) ? true : false;
108 DGifCloseFile(gifFile);
116 bool Save(Bitmap bitmap, const char * filename, void * options)
122 ColorAlpha * LoadPalette(const char * fileName, const char * type)
124 ColorAlpha * result = null;