File: simage_gif.c

package info (click to toggle)
simage 1.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,908 kB
  • sloc: ansic: 14,231; sh: 12,587; cpp: 930; perl: 854; makefile: 617; lisp: 25
file content (378 lines) | stat: -rw-r--r-- 9,776 bytes parent folder | download | duplicates (2)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*!
  GIF loader, using giflib or libungif.
  Based, in part, on source code found in libungif, gif2rgb.c
*/

#include <config.h>
#ifdef HAVE_GIFLIB

#include <simage_gif.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <gif_lib.h>

enum { 
  ERR_NO_ERROR, 
  ERR_OPEN, 
  ERR_READ, 
  ERR_WRITE, 
  ERR_MEM
};

static int giferror = ERR_NO_ERROR;

int
simage_gif_error(char * buffer, int buflen)
{
  switch (giferror) {
  case ERR_OPEN:
    strncpy(buffer, "GIF loader: Error opening file", buflen);
    break;
  case ERR_READ:
    strncpy(buffer, "GIF loader: Error reading file", buflen);
    break;
  case ERR_WRITE:
    strncpy(buffer, "GIF loader: Error writing file", buflen);
    break;
  case ERR_MEM:
    strncpy(buffer, "GIF loader: Out of memory error", buflen);
    break;
  }
  return giferror;
}

int 
simage_gif_identify(const char *filename,
                    const unsigned char *header,
                    int headerlen)
{
  return (headerlen >= 3) 
    && header[0] == 'G'
    && header[1] == 'I' 
    && header[2] == 'F';
}

static void
decode_row(GifFileType * giffile, 
           unsigned char * buffer, 
           unsigned char * rowdata,
           int x, int y, int len, 
           int transparent)
{
  GifColorType * cmentry;
  ColorMapObject * colormap;
  int colormapsize;
  unsigned char col;
  unsigned char * ptr;

  y = giffile->SHeight - (y+1);
  ptr = buffer + (giffile->SWidth * y + x) * 4;

  colormap = (giffile->Image.ColorMap
              ? giffile->Image.ColorMap
              : giffile->SColorMap);
  colormapsize = colormap ? colormap->ColorCount : 255;
  
  while (len--) {
    col = *rowdata++;
    if (col >= colormapsize) col = 0; /* just in case */
    cmentry = colormap ? &colormap->Colors[col] : NULL;
    if (cmentry) {
      *ptr++ = cmentry->Red;
      *ptr++ = cmentry->Green;
      *ptr++ = cmentry->Blue;
    }
    else {
      *ptr++ = col;
      *ptr++ = col;
      *ptr++ = col;
    }
    *ptr++ = (col == transparent ? 0x00 : 0xff);
  }
}

unsigned char *
simage_gif_load(const char *filename,
                int *width_ret,
                int *height_ret,
                int *numComponents_ret)
{
  int i, j, n, row, col, width, height, extcode;
  unsigned char * rowdata;
  unsigned char * buffer, * ptr;
  unsigned char bg;
  int transparent;
  GifRecordType recordtype;
  GifByteType * extension;
  GifFileType * giffile;
  GifColorType * bgcol;
  
  /* The way an interlaced image should be read - offsets and jumps */
  int interlacedoffset[] = { 0, 4, 2, 1 }; 
  int interlacedjumps[] = { 8, 8, 4, 2 };    
  
  giffile = DGifOpenFileName(filename);
  if (!giffile) {
    giferror = ERR_OPEN;
    return NULL;
  }
  
  transparent = -1; /* no transparent color by default */
  
  n = giffile->SHeight * giffile->SWidth;
  buffer = (unsigned char*) malloc(n * 4);
  if (!buffer) {
    giferror = ERR_MEM;
    return NULL;
  }
  rowdata = (unsigned char*) malloc(giffile->SWidth);
  if (!rowdata) {
    giferror = ERR_MEM;
    free(buffer);
    return NULL;
  }
  
  bg = giffile->SBackGroundColor;
  if (giffile->SColorMap && bg < giffile->SColorMap->ColorCount) {
    bgcol = &giffile->SColorMap->Colors[bg];
  }
  else bgcol = NULL;  
  ptr = buffer;
  for (i = 0; i < n; i++) {
    if (bgcol) {
      *ptr++ = bgcol->Red;
      *ptr++ = bgcol->Green;
      *ptr++ = bgcol->Blue;
      *ptr++ = 0xff;
    }
    else {
      *ptr++ = 0x00;
      *ptr++ = 0x00;
      *ptr++ = 0x00;
      *ptr++ = 0xff;
    }
  }
 
  /* Scan the content of the GIF file and load the image(s) in: */
  do {
    if (DGifGetRecordType(giffile, &recordtype) == GIF_ERROR) {
      giferror = ERR_READ;
      free(buffer);
      free(rowdata);
      return NULL;
    }
    switch (recordtype) {
    case IMAGE_DESC_RECORD_TYPE:
      if (DGifGetImageDesc(giffile) == GIF_ERROR) {
        giferror = ERR_READ;
        free(buffer);
        free(rowdata);
        return NULL;
      }
      row = giffile->Image.Top; /* subimage position in composite image */
      col = giffile->Image.Left;
      width = giffile->Image.Width;
      height = giffile->Image.Height;
      if (giffile->Image.Left + giffile->Image.Width > giffile->SWidth ||
          giffile->Image.Top + giffile->Image.Height > giffile->SHeight) {
        /* image is not confined to screen dimension */
        giferror = ERR_READ;
        free(buffer);
        free(rowdata);
        return NULL;
      }
      if (giffile->Image.Interlace) {
        /* Need to perform 4 passes on the images: */
        for (i = 0; i < 4; i++) {
          for (j = row + interlacedoffset[i]; j < row + height;
               j += interlacedjumps[i]) {
            if (DGifGetLine(giffile, rowdata, width) == GIF_ERROR) {
              giferror = ERR_READ;
              free(buffer);
              free(rowdata);
              return NULL;
            }
            else decode_row(giffile, buffer, rowdata, col, j, width, transparent);
          }
        }
      }
      else {
        for (i = 0; i < height; i++, row++) {
          if (DGifGetLine(giffile, rowdata, width) == GIF_ERROR) {
            giferror = ERR_READ;
            free(buffer);
            free(rowdata);
            return NULL;
          }
          else decode_row(giffile, buffer, rowdata, col, row, width, transparent);
        }
      }
      break;
    case EXTENSION_RECORD_TYPE:
      /* Skip any extension blocks in file: */
      if (DGifGetExtension(giffile, &extcode, &extension) == GIF_ERROR) {
        giferror = ERR_READ;
        free(buffer);
        free(rowdata);
        return NULL;
      }
      /* transparent test from the gimp gif-plugin. Open Source rulez! */
      else if (extcode == 0xf9) { 
        if (extension[0] >= 4 && extension[1] & 0x1) transparent = extension[4];
        else transparent = -1;
      }
      while (extension != NULL) {
        if (DGifGetExtensionNext(giffile, &extension) == GIF_ERROR) {
          giferror = ERR_READ;
          free(buffer);
          free(rowdata);
          return NULL;
        }
      }
      break;
    case TERMINATE_RECORD_TYPE:
      break;
    default:		    /* Should be trapped by DGifGetRecordType. */
      break;
    }
  }
  while (recordtype != TERMINATE_RECORD_TYPE);

  free(rowdata);
  *width_ret = giffile->SWidth;
  *height_ret = giffile->SHeight;
  *numComponents_ret = 4;
  DGifCloseFile(giffile);
  return buffer;
}

int 
simage_gif_save(const char * filename,
                const unsigned char * bytes,
                int width,
                int height,
                int numcomponents)
{
  const unsigned char * bytes_ptr = bytes;
  int i, colormapsize = 256;
  unsigned long bufsize = width * height;
  ColorMapObject * cmapobj;
  GifByteType * outbuf = NULL, * outbuf_ptr = NULL;
  GifByteType * rgbbuf = NULL, * rgbbuf_ptr = NULL;
  GifFileType * giffile = NULL;

  /* allocate memory for the channels of the rgb buffer */
  if (!(rgbbuf = (GifByteType*)malloc(bufsize*3))) {
    giferror = ERR_MEM;
    return 0;
  }

  rgbbuf_ptr = rgbbuf;

  /* FIXME: ignoring alpha channel. should find a way to set the
     background value appropriately. 20060108 tamer. */
  switch (numcomponents) {
    case 3:
    case 4:
      /* split up the rgb values into their respective channels */
      for (i=0; i < bufsize; i++) {
        rgbbuf_ptr[0]         = *bytes_ptr++; /* red */
        rgbbuf_ptr[bufsize]   = *bytes_ptr++; /* green */
        rgbbuf_ptr[bufsize*2] = *bytes_ptr++; /* blue */
        rgbbuf_ptr += 1;
        if (numcomponents == 4) { bytes_ptr++; }
      }
      break;

    case 1:
    case 2:
      /* split up the grayscale values into their respective rgb channels */
      for (i=0; i < bufsize; i++) {
        rgbbuf_ptr[0] = rgbbuf_ptr[bufsize] = rgbbuf_ptr[bufsize*2] = *bytes_ptr++;
        rgbbuf_ptr += 1;
        if (numcomponents == 2) { bytes_ptr++; }
      }
      break;

    default:
      giferror = ERR_WRITE;
      free(rgbbuf);
      return 0;
  }

  if (!(outbuf = (GifByteType*)malloc(bufsize))) {
    giferror = ERR_MEM;
    free(rgbbuf);
    return 0;
  }

  if (!(cmapobj = MakeMapObject(colormapsize, NULL))) {
    giferror = ERR_MEM;
    free(rgbbuf);
    free(outbuf);
    return 0;
  }

  if (QuantizeBuffer(width, height, &colormapsize,
                     rgbbuf, &rgbbuf[bufsize], &rgbbuf[bufsize*2],
                     outbuf, cmapobj->Colors) == GIF_ERROR) {
    giferror = ERR_MEM;
    free(rgbbuf);
    free(outbuf);
    FreeMapObject(cmapobj);
    return 0;
  }      
    
  /* open gif file and overwrite any existing file */
  if (!(giffile = EGifOpenFileName(filename, FALSE))) {
    giferror = ERR_OPEN;
    free(rgbbuf);
    free(outbuf);
    FreeMapObject(cmapobj);
    return 0;
  }

  if (EGifPutScreenDesc(giffile, width, height, 8,
                        0, cmapobj) == GIF_ERROR ||
      EGifPutImageDesc(giffile, 0, 0, width, height, 
                       FALSE, NULL) == GIF_ERROR) { 
    giferror = ERR_WRITE;
    free(rgbbuf);
    free(outbuf);
    EGifCloseFile(giffile);
    FreeMapObject(cmapobj);
    return 0;
  }

  outbuf_ptr = outbuf + bufsize;
  for (i = height; i > 0; i--) {
    outbuf_ptr -= width;
    if (EGifPutLine(giffile, outbuf_ptr, width) == GIF_ERROR) { 
      giferror = ERR_WRITE;
      free(rgbbuf);
      free(outbuf);
      EGifCloseFile(giffile);
      FreeMapObject(cmapobj);
      return 0;
    }
  }

  if (EGifPutComment(giffile, "Image saved using simage.") == GIF_ERROR ||
      EGifCloseFile(giffile) == GIF_ERROR) { 
    giferror = ERR_WRITE;
    free(rgbbuf);
    free(outbuf);
    EGifCloseFile(giffile);
    FreeMapObject(cmapobj);
    return 0;
  }

  free(rgbbuf);
  free(outbuf);
  FreeMapObject(cmapobj);

  return 1;
}

#endif /* HAVE_GIFLIB */