1 /* GdkPixbuf library - GIF image loader
3 * Copyright (C) 1999 Mark Crichton
4 * Copyright (C) 1999 The Free Software Foundation
6 * Authors: Mark Crichton <crichton@gimp.org>
7 * Federico Mena-Quintero <federico@gimp.org>
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.
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.
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.
29 #include "gdk-pixbuf.h"
33 /* Destroy notification function for the libart pixbuf */
35 free_buffer (gpointer user_data, gpointer data)
40 /* Shared library entry point */
44 gint fn, is_trans = FALSE;
48 guchar *pixels, *tmpptr;
53 int intoffset[] = { 0, 4, 2, 1 };
54 int intjump[] = { 8, 8, 4, 2 };
57 gif = DGifOpenFileHandle (fn);
60 g_warning ("DGifOpenFilehandle FAILED");
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.
71 if (DGifGetRecordType (gif, &rec) == GIF_ERROR) {
77 if (rec == IMAGE_DESC_RECORD_TYPE && !done) {
78 if (DGifGetImageDesc (gif) == GIF_ERROR) {
84 /* Note the careful use of malloc/calloc vs. g_malloc;
85 * we want to fail gracefully if we run out of memory.
89 h = gif->Image.Height;
90 rows = calloc (sizeof (GifRowType *), h);
96 for (i = 0; i < h; i++) {
97 rows[i] = calloc (sizeof (GifPixelType), w);
100 for (j = 0; j < h; j++)
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);
115 for (i = 0; i < h; i++)
116 DGifGetLine (gif, rows[i], w);
119 } else if (rec == EXTENSION_RECORD_TYPE) {
123 DGifGetExtension (gif, &ext_code, &ext);
125 if ((ext_code == 0xf9) && (ext[1] & 1) && (t_color < 0)) {
127 t_color = (gint) ext[4];
131 DGifGetExtensionNext (gif, &ext);
134 } while (rec != TERMINATE_RECORD_TYPE);
136 /* Ok, we're loaded, now to convert from indexed -> RGB with alpha if necessary */
139 pixels = malloc (h * w * 4);
141 pixels = malloc (h * w * 3);
147 for (i = 0; i < h; i++)
155 cmap = (gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap);
157 /* Unindex the data, and pack it in RGB(A) order */
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;
166 if (rows[i][j] == t_color)
182 return gdk_pixbuf_new_from_data (pixels, ART_PIX_RGB, is_trans,
183 w, h, is_trans ? (w * 4) : (w * 3),