1 namespace gfx::bitmaps;
5 #if !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
9 static int ReadData(GifFileType * gifFile, GifByteType * bytes, int size)
11 File f = gifFile->UserData;
12 return f.Read(bytes, 1, size);
15 /*static int WriteData(GifFileType * gifFile, GifByteType * bytes, int size)
17 File f = gifFile->UserData;
18 return f.Write(bytes, 1, size);
21 static const char * extensions[] = { "gif", null };
23 class GIFFormat : BitmapFormat
25 class_property(extensions) = extensions;
27 bool Load(Bitmap bitmap, File f)
32 GifFileType * gifFile = DGifOpen(f, ReadData, null);
34 GifFileType * gifFile = DGifOpen(f, ReadData);
39 if(bitmap.Allocate(null, gifFile->SWidth, gifFile->SHeight, 0, pixelFormat888, false))
42 ColorAlpha * picture = (ColorAlpha *)bitmap.picture;
44 struct SavedImage * image = &gifFile->SavedImages[0];
46 ColorMapObject * colorMap = gifFile->SColorMap;
48 if(!colorMap) colorMap = gifFile->Image.ColorMap;
50 for(c = 0; c<image->ExtensionBlockCount; c++)
52 ExtensionBlock * block = &image->ExtensionBlocks[c];
53 if(block->Function == 249)
56 transparent = (byte)block->Bytes[3];
61 int colorCount = colorMap->ColorCount;
62 if(!gifFile->Image.Interlace)
64 for(y = 0; y < gifFile->SHeight; y++)
67 for(x = 0; x < gifFile->SWidth; x++)
69 byte index = image->RasterBits[y*gifFile->SWidth+x];
70 if(index == transparent || index >= colorCount)
71 picture[y * bitmap.stride + x] = 0; //&= 0xFFFFFF;
73 picture[y * bitmap.stride + x] = ColorAlpha { 255, {
74 colorMap->Colors[index].Red,
75 colorMap->Colors[index].Green,
76 colorMap->Colors[index].Blue } };
82 int startline[4] = {0, 4, 2, 1};
83 int offset[4] = {8, 8, 4, 2};
86 for(group=0,i=0;group<4;group++)
89 for(y = startline[group]; y < gifFile->SHeight; y += offset[group], i++)
92 for(x = 0; x < gifFile->SWidth; x++)
94 byte index = image->RasterBits[i*gifFile->SWidth+x];
95 if(index == transparent)
96 picture[y * bitmap.stride + x] = 0;// &= 0xFFFFFF;
98 picture[y * bitmap.stride + x] = ColorAlpha { 255, {
99 colorMap->Colors[index].Red,
100 colorMap->Colors[index].Green,
101 colorMap->Colors[index].Blue } };
106 bitmap.transparent = (transparent != -1) ? true : false;
110 #if GIFLIB_MAJOR >= 5
111 DGifCloseFile(gifFile, null);
113 DGifCloseFile(gifFile);
122 bool Save(Bitmap bitmap, const char * filename, void * options)
128 ColorAlpha * LoadPalette(const char * fileName, const char * type)
130 ColorAlpha * result = null;
135 #endif // !defined(__EMSCRIPTEN__)