summaryrefslogtreecommitdiff
path: root/src/modules/loaders/loader_gif.c
blob: b5bfe9c508971655c72294dafce70be59ff8edf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "loader_common.h"

#include <gif_lib.h>

int
load2(ImlibImage * im, int load_data)
{
   static const int    intoffset[] = { 0, 4, 2, 1 };
   static const int    intjump[] = { 8, 8, 4, 2 };
   int                 rc;
   DATA32             *ptr;
   GifFileType        *gif;
   GifRowType         *rows;
   GifRecordType       rec;
   ColorMapObject     *cmap;
   int                 i, j, done, bg, r, g, b, w = 0, h = 0;
   int                 transp;
   int                 fd;
   DATA32              colormap[256];

   fd = dup(fileno(im->fp));

#if GIFLIB_MAJOR >= 5
   gif = DGifOpenFileHandle(fd, NULL);
#else
   gif = DGifOpenFileHandle(fd);
#endif
   if (!gif)
      return LOAD_FAIL;

   rc = LOAD_FAIL;
   done = 0;
   rows = NULL;
   transp = -1;

   do
     {
        if (DGifGetRecordType(gif, &rec) == GIF_ERROR)
          {
             /* PrintGifError(); */
             rec = TERMINATE_RECORD_TYPE;
          }

        if ((rec == IMAGE_DESC_RECORD_TYPE) && (!done))
          {
             if (DGifGetImageDesc(gif) == GIF_ERROR)
               {
                  /* PrintGifError(); */
                  rec = TERMINATE_RECORD_TYPE;
                  break;
               }

             im->w = w = gif->Image.Width;
             im->h = h = gif->Image.Height;
             if (!IMAGE_DIMENSIONS_OK(w, h))
                goto quit;

             rows = calloc(h, sizeof(GifRowType));
             if (!rows)
                goto quit;

             for (i = 0; i < h; i++)
               {
                  rows[i] = calloc(w, sizeof(GifPixelType));
                  if (!rows[i])
                     goto quit;
               }

             if (gif->Image.Interlace)
               {
                  for (i = 0; i < 4; i++)
                    {
                       for (j = intoffset[i]; j < h; j += intjump[i])
                         {
                            DGifGetLine(gif, rows[j], w);
                         }
                    }
               }
             else
               {
                  for (i = 0; i < h; i++)
                    {
                       DGifGetLine(gif, rows[i], w);
                    }
               }
             done = 1;
          }
        else if (rec == EXTENSION_RECORD_TYPE)
          {
             int                 ext_code;
             GifByteType        *ext;

             ext = NULL;
             DGifGetExtension(gif, &ext_code, &ext);
             while (ext)
               {
                  if ((ext_code == 0xf9) && (ext[1] & 1) && (transp < 0))
                    {
                       transp = (int)ext[4];
                    }
                  ext = NULL;
                  DGifGetExtensionNext(gif, &ext);
               }
          }
     }
   while (rec != TERMINATE_RECORD_TYPE);

   UPDATE_FLAG(im->flags, F_HAS_ALPHA, transp >= 0);

   if (!rows)
      goto quit;

   if (!load_data)
     {
        rc = LOAD_SUCCESS;
        goto quit;
     }

   /* Load data */

   bg = gif->SBackGroundColor;
   cmap = (gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap);
   memset(colormap, 0, sizeof(colormap));
   if (cmap != NULL)
     {
        for (i = cmap->ColorCount > 256 ? 256 : cmap->ColorCount; i-- > 0;)
          {
             r = cmap->Colors[i].Red;
             g = cmap->Colors[i].Green;
             b = cmap->Colors[i].Blue;
             colormap[i] = PIXEL_ARGB(0xff, r, g, b);
          }
        /* if bg > cmap->ColorCount, it is transparent black already */
        if (transp >= 0 && transp < 256)
           colormap[transp] = bg >= 0 && bg < 256 ?
              colormap[bg] & 0x00ffffff : 0x00000000;
     }

   ptr = __imlib_AllocateData(im);
   if (!ptr)
      goto quit;

   for (i = 0; i < h; i++)
     {
        for (j = 0; j < w; j++)
          {
             *ptr++ = colormap[rows[i][j]];
          }

        if (im->lc && __imlib_LoadProgressRows(im, i, 1))
          {
             rc = LOAD_BREAK;
             goto quit;
          }
     }

   rc = LOAD_SUCCESS;

 quit:
   if (rows)
     {
        for (i = 0; i < h; i++)
           free(rows[i]);
        free(rows);
     }

#if GIFLIB_MAJOR > 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1)
   DGifCloseFile(gif, NULL);
#else
   DGifCloseFile(gif);
#endif

   if (rc <= 0)
      __imlib_FreeData(im);

   return rc;
}

void
formats(ImlibLoader * l)
{
   static const char  *const list_formats[] = { "gif" };
   __imlib_LoaderSetFormats(l, list_formats, ARRAY_SIZE(list_formats));
}