sdk: const correctness
[sdk] / ecere / src / gfx / bitmaps / GIFFormat.ec
1 namespace gfx::bitmaps;
2
3 #include "gif_lib.h"
4
5 import "Display"
6
7 static int ReadData(GifFileType * gifFile, GifByteType * bytes, int size)
8 {
9    File f = gifFile->UserData;
10    return f.Read(bytes, 1, size);
11 }
12
13 /*static int WriteData(GifFileType * gifFile, GifByteType * bytes, int size)
14 {
15    File f = gifFile->UserData;
16    return f.Write(bytes, 1, size);
17 }
18 */
19 static const char * extensions[] = { "gif", null };
20
21 class GIFFormat : BitmapFormat
22 {
23    class_property(extensions) = extensions;
24
25    bool Load(Bitmap bitmap, File f)
26    {
27       bool result = false;
28
29 #if GIFLIB_MAJOR > 5
30       GifFileType * gifFile = DGifOpen(f, ReadData, null);
31 #else
32       GifFileType * gifFile = DGifOpen(f, ReadData);
33 #endif
34       if(gifFile)
35       {
36          DGifSlurp(gifFile);
37          if(bitmap.Allocate(null, gifFile->SWidth, gifFile->SHeight, 0, pixelFormat888, false))
38          {
39             uint y;
40             ColorAlpha * picture = (ColorAlpha *)bitmap.picture;
41             int transparent = -1;
42             struct SavedImage * image = &gifFile->SavedImages[0];
43             int c;
44             ColorMapObject * colorMap = gifFile->SColorMap;
45
46             if(!colorMap) colorMap = gifFile->Image.ColorMap;
47
48             for(c = 0; c<image->ExtensionBlockCount; c++)
49             {
50                ExtensionBlock * block = &image->ExtensionBlocks[c];
51                if(block->Function == 249)
52                {
53                   if(block->Bytes[0])
54                      transparent = (byte)block->Bytes[3];
55                }
56             }
57             if(colorMap)
58             {
59                int colorCount = colorMap->ColorCount;
60                if(!gifFile->Image.Interlace)
61                {
62                   for(y = 0; y < gifFile->SHeight; y++)
63                   {
64                      uint x;
65                      for(x = 0; x < gifFile->SWidth; x++)
66                      {
67                         byte index = image->RasterBits[y*gifFile->SWidth+x];
68                         if(index == transparent || index >= colorCount)
69                            picture[y * bitmap.stride + x] = 0; //&= 0xFFFFFF;
70                         else
71                            picture[y * bitmap.stride + x] = ColorAlpha { 255, {
72                               colorMap->Colors[index].Red,
73                               colorMap->Colors[index].Green,
74                               colorMap->Colors[index].Blue } };
75                      }
76                   }
77                }
78                else
79                {
80                   int startline[4] = {0, 4, 2, 1};
81                   int offset[4] = {8, 8, 4, 2};
82                   int group;
83                   uint i;
84                   for(group=0,i=0;group<4;group++)
85                   {
86                      uint y;
87                      for(y = startline[group]; y < gifFile->SHeight; y += offset[group], i++)
88                      {
89                         uint x;
90                         for(x = 0; x < gifFile->SWidth; x++)
91                         {
92                            byte index = image->RasterBits[i*gifFile->SWidth+x];
93                            if(index == transparent)
94                               picture[y * bitmap.stride + x] = 0;// &= 0xFFFFFF;
95                            else
96                               picture[y * bitmap.stride + x] = ColorAlpha { 255, {
97                                  colorMap->Colors[index].Red,
98                                  colorMap->Colors[index].Green,
99                                  colorMap->Colors[index].Blue } };
100                         }
101                      }
102                   }
103                }
104                bitmap.transparent = (transparent != -1) ? true : false;
105                result = true;
106             }
107          }
108          DGifCloseFile(gifFile);
109       }
110
111       if(!result)
112          bitmap.Free();
113       return result;
114    }
115
116    bool Save(Bitmap bitmap, const char * filename, void * options)
117    {
118       bool result = false;
119       return result;
120    }
121
122    ColorAlpha * LoadPalette(const char * fileName, const char * type)
123    {
124       ColorAlpha * result = null;
125       return result;
126    }
127 }