1999-10-20 21:20:49 +00:00
|
|
|
|
/* GdkPixbuf library - GIF image loader
|
1999-06-29 06:07:14 +00:00
|
|
|
|
*
|
1999-06-30 15:28:43 +00:00
|
|
|
|
* Copyright (C) 1999 Mark Crichton
|
1999-10-20 21:20:49 +00:00
|
|
|
|
* Copyright (C) 1999 The Free Software Foundation
|
|
|
|
|
*
|
|
|
|
|
* Authors: Mark Crichton <crichton@gimp.org>
|
|
|
|
|
* Federico Mena-Quintero <federico@gimp.org>
|
1999-06-30 15:28:43 +00:00
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
1999-10-20 21:20:49 +00:00
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
|
* Boston, MA 02111-1307, USA.
|
1999-06-29 06:07:14 +00:00
|
|
|
|
*/
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-06-29 06:07:14 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
#include <gif_lib.h>
|
1999-10-20 21:20:49 +00:00
|
|
|
|
#include "gdk-pixbuf.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Destroy notification function for the libart pixbuf */
|
|
|
|
|
static void
|
|
|
|
|
free_buffer (gpointer user_data, gpointer data)
|
|
|
|
|
{
|
|
|
|
|
free (data);
|
|
|
|
|
}
|
1999-06-29 06:07:14 +00:00
|
|
|
|
|
|
|
|
|
/* Shared library entry point */
|
1999-10-18 19:29:45 +00:00
|
|
|
|
GdkPixbuf *
|
1999-10-20 21:20:49 +00:00
|
|
|
|
image_load (FILE *f)
|
1999-06-29 06:07:14 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gint fn, is_trans = FALSE;
|
|
|
|
|
gint done = 0;
|
|
|
|
|
gint t_color = -1;
|
|
|
|
|
gint w, h, i, j;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
guchar *pixels, *tmpptr;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
GifFileType *gif;
|
|
|
|
|
GifRowType *rows;
|
|
|
|
|
GifRecordType rec;
|
|
|
|
|
ColorMapObject *cmap;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
int intoffset[] = { 0, 4, 2, 1 };
|
|
|
|
|
int intjump[] = { 8, 8, 4, 2 };
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
fn = fileno (f);
|
|
|
|
|
gif = DGifOpenFileHandle (fn);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
if (!gif) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
g_warning ("DGifOpenFilehandle FAILED");
|
|
|
|
|
PrintGifError ();
|
1999-06-29 06:07:14 +00:00
|
|
|
|
return NULL;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
/* Now we do the ungodly mess of loading a GIF image I used to remember
|
|
|
|
|
* when I liked this file format... of course, I still coded in
|
|
|
|
|
* assembler then. This comes from gdk_imlib, with some cleanups.
|
1999-10-18 19:29:45 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
do {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
if (DGifGetRecordType (gif, &rec) == GIF_ERROR) {
|
|
|
|
|
PrintGifError ();
|
|
|
|
|
DGifCloseFile (gif);
|
|
|
|
|
return NULL;
|
1999-06-29 06:07:14 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
if (rec == IMAGE_DESC_RECORD_TYPE && !done) {
|
|
|
|
|
if (DGifGetImageDesc (gif) == GIF_ERROR) {
|
|
|
|
|
PrintGifError ();
|
|
|
|
|
DGifCloseFile (gif);
|
|
|
|
|
return NULL;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
/* Note the careful use of malloc/calloc vs. g_malloc;
|
|
|
|
|
* we want to fail gracefully if we run out of memory.
|
|
|
|
|
*/
|
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
w = gif->Image.Width;
|
|
|
|
|
h = gif->Image.Height;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
rows = calloc (sizeof (GifRowType *), h);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (!rows) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
DGifCloseFile (gif);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
for (i = 0; i < h; i++) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
rows[i] = calloc (sizeof (GifPixelType), w);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (!rows[i]) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
DGifCloseFile (gif);
|
|
|
|
|
for (j = 0; j < h; j++)
|
|
|
|
|
if (rows[j])
|
|
|
|
|
free (rows[j]);
|
|
|
|
|
|
|
|
|
|
free (rows);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
if (gif->Image.Interlace)
|
1999-10-18 19:29:45 +00:00
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
|
for (j = intoffset[i]; j < h; j += intjump[i])
|
1999-10-20 21:20:49 +00:00
|
|
|
|
DGifGetLine (gif, rows[j], w);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
else
|
1999-10-18 19:29:45 +00:00
|
|
|
|
for (i = 0; i < h; i++)
|
1999-10-20 21:20:49 +00:00
|
|
|
|
DGifGetLine (gif, rows[i], w);
|
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
done = 1;
|
|
|
|
|
} else if (rec == EXTENSION_RECORD_TYPE) {
|
|
|
|
|
gint ext_code;
|
|
|
|
|
GifByteType *ext;
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
DGifGetExtension (gif, &ext_code, &ext);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
while (ext) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
if ((ext_code == 0xf9) && (ext[1] & 1) && (t_color < 0)) {
|
1999-10-18 19:29:45 +00:00
|
|
|
|
is_trans = TRUE;
|
|
|
|
|
t_color = (gint) ext[4];
|
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
ext = NULL;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
DGifGetExtensionNext (gif, &ext);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-06-29 06:07:14 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
} while (rec != TERMINATE_RECORD_TYPE);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
/* Ok, we're loaded, now to convert from indexed -> RGB with alpha if necessary */
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
if (is_trans)
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixels = malloc (h * w * 4);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
else
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixels = malloc (h * w * 3);
|
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
tmpptr = pixels;
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
if (!pixels) {
|
|
|
|
|
DGifCloseFile (gif);
|
|
|
|
|
for (i = 0; i < h; i++)
|
|
|
|
|
if (rows[i])
|
|
|
|
|
free (rows[i]);
|
|
|
|
|
|
|
|
|
|
free (rows);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return NULL;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
}
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
cmap = (gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap);
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
/* Unindex the data, and pack it in RGB(A) order */
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < h; i++) {
|
|
|
|
|
for (j = 0; j < w; j++) {
|
|
|
|
|
tmpptr[0] = cmap->Colors[rows[i][j]].Red;
|
|
|
|
|
tmpptr[1] = cmap->Colors[rows[i][j]].Green;
|
|
|
|
|
tmpptr[2] = cmap->Colors[rows[i][j]].Blue;
|
1999-06-29 06:07:14 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
if (is_trans) {
|
|
|
|
|
if (rows[i][j] == t_color)
|
|
|
|
|
tmpptr[3] = 0;
|
|
|
|
|
else
|
|
|
|
|
tmpptr[3] = 0xFF;
|
1999-09-22 22:30:51 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
tmpptr += 4;
|
|
|
|
|
} else
|
|
|
|
|
tmpptr += 3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free (rows[i]);
|
|
|
|
|
}
|
1999-06-29 06:07:14 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
free (rows);
|
|
|
|
|
DGifCloseFile (gif);
|
1999-06-29 06:07:14 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
return gdk_pixbuf_new_from_data (pixels, ART_PIX_RGB, is_trans,
|
|
|
|
|
w, h, is_trans ? (w * 4) : (w * 3),
|
|
|
|
|
free_buffer, NULL);
|
1999-06-29 06:07:14 +00:00
|
|
|
|
}
|