]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-gif.c
Bumped version number to 0.3.
[~andy/gtk] / gdk-pixbuf / io-gif.c
1 /* GdkPixbuf library - GIF image loader
2  *
3  * Copyright (C) 1999 Mark Crichton
4  * Copyright (C) 1999 The Free Software Foundation
5  *
6  * Authors: Mark Crichton <crichton@gimp.org>
7  *          Federico Mena-Quintero <federico@gimp.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <config.h>
26 #include <stdio.h>
27 #include <glib.h>
28 #include <gif_lib.h>
29 #include "gdk-pixbuf.h"
30
31 \f
32
33 /* Destroy notification function for the libart pixbuf */
34 static void
35 free_buffer (gpointer user_data, gpointer data)
36 {
37         free (data);
38 }
39
40 /* Shared library entry point */
41 GdkPixbuf *
42 image_load (FILE *f)
43 {
44         gint fn, is_trans = FALSE;
45         gint done = 0;
46         gint t_color = -1;
47         gint w, h, i, j;
48         guchar *pixels, *tmpptr;
49         GifFileType *gif;
50         GifRowType *rows;
51         GifRecordType rec;
52         ColorMapObject *cmap;
53         int intoffset[] = { 0, 4, 2, 1 };
54         int intjump[] = { 8, 8, 4, 2 };
55
56         fn = fileno (f);
57         gif = DGifOpenFileHandle (fn);
58
59         if (!gif) {
60                 g_warning ("DGifOpenFilehandle FAILED");
61                 PrintGifError ();
62                 return NULL;
63         }
64
65         /* Now we do the ungodly mess of loading a GIF image I used to remember
66          * when I liked this file format...  of course, I still coded in
67          * assembler then.  This comes from gdk_imlib, with some cleanups.
68          */
69
70         do {
71                 if (DGifGetRecordType (gif, &rec) == GIF_ERROR) {
72                         PrintGifError ();
73                         DGifCloseFile (gif);
74                         return NULL;
75                 }
76
77                 if (rec == IMAGE_DESC_RECORD_TYPE && !done) {
78                         if (DGifGetImageDesc (gif) == GIF_ERROR) {
79                                 PrintGifError ();
80                                 DGifCloseFile (gif);
81                                 return NULL;
82                         }
83
84                         /* Note the careful use of malloc/calloc vs. g_malloc;
85                          * we want to fail gracefully if we run out of memory.
86                          */
87
88                         w = gif->Image.Width;
89                         h = gif->Image.Height;
90                         rows = calloc (sizeof (GifRowType *), h);
91                         if (!rows) {
92                                 DGifCloseFile (gif);
93                                 return NULL;
94                         }
95
96                         for (i = 0; i < h; i++) {
97                                 rows[i] = calloc (sizeof (GifPixelType), w);
98                                 if (!rows[i]) {
99                                         DGifCloseFile (gif);
100                                         for (j = 0; j < h; j++)
101                                                 if (rows[j])
102                                                         free (rows[j]);
103
104                                         free (rows);
105                                         return NULL;
106                                 }
107                         }
108
109                         if (gif->Image.Interlace)
110                                 for (i = 0; i < 4; i++) {
111                                         for (j = intoffset[i]; j < h; j += intjump[i])
112                                                 DGifGetLine (gif, rows[j], w);
113                                 }
114                         else
115                                 for (i = 0; i < h; i++)
116                                         DGifGetLine (gif, rows[i], w);
117
118                         done = 1;
119                 } else if (rec == EXTENSION_RECORD_TYPE) {
120                         gint ext_code;
121                         GifByteType *ext;
122
123                         DGifGetExtension (gif, &ext_code, &ext);
124                         while (ext) {
125                                 if ((ext_code == 0xf9) && (ext[1] & 1) && (t_color < 0)) {
126                                         is_trans = TRUE;
127                                         t_color = (gint) ext[4];
128                                 }
129
130                                 ext = NULL;
131                                 DGifGetExtensionNext (gif, &ext);
132                         }
133                 }
134         } while (rec != TERMINATE_RECORD_TYPE);
135
136         /* Ok, we're loaded, now to convert from indexed -> RGB with alpha if necessary */
137
138         if (is_trans)
139                 pixels = malloc (h * w * 4);
140         else
141                 pixels = malloc (h * w * 3);
142
143         tmpptr = pixels;
144
145         if (!pixels) {
146                 DGifCloseFile (gif);
147                 for (i = 0; i < h; i++)
148                         if (rows[i])
149                                 free (rows[i]);
150
151                 free (rows);
152                 return NULL;
153         }
154
155         cmap = (gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap);
156
157         /* Unindex the data, and pack it in RGB(A) order */
158
159         for (i = 0; i < h; i++) {
160                 for (j = 0; j < w; j++) {
161                         tmpptr[0] = cmap->Colors[rows[i][j]].Red;
162                         tmpptr[1] = cmap->Colors[rows[i][j]].Green;
163                         tmpptr[2] = cmap->Colors[rows[i][j]].Blue;
164
165                         if (is_trans) {
166                                 if (rows[i][j] == t_color)
167                                         tmpptr[3] = 0;
168                                 else
169                                         tmpptr[3] = 0xFF;
170
171                                 tmpptr += 4;
172                         } else
173                                 tmpptr += 3;
174                 }
175
176                 free (rows[i]);
177         }
178
179         free (rows);
180         DGifCloseFile (gif);
181
182         return gdk_pixbuf_new_from_data (pixels, ART_PIX_RGB, is_trans,
183                                          w, h, is_trans ? (w * 4) : (w * 3),
184                                          free_buffer, NULL);
185 }