Blame gdk-pixbuf/io-xpm.c

Packit a4058c
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
Packit a4058c
/* GdkPixbuf library - XPM image loader
Packit a4058c
 *
Packit a4058c
 * Copyright (C) 1999 Mark Crichton
Packit a4058c
 * Copyright (C) 1999 The Free Software Foundation
Packit a4058c
 *
Packit a4058c
 * Authors: Mark Crichton <crichton@gimp.org>
Packit a4058c
 *          Federico Mena-Quintero <federico@gimp.org>
Packit a4058c
 *
Packit a4058c
 * This library is free software; you can redistribute it and/or
Packit a4058c
 * modify it under the terms of the GNU Lesser General Public
Packit a4058c
 * License as published by the Free Software Foundation; either
Packit a4058c
 * version 2 of the License, or (at your option) any later version.
Packit a4058c
 *
Packit a4058c
 * This library is distributed in the hope that it will be useful,
Packit a4058c
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4058c
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a4058c
 * Lesser General Public License for more details.
Packit a4058c
 *
Packit a4058c
 * You should have received a copy of the GNU Lesser General Public
Packit a4058c
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit a4058c
 */
Packit a4058c
Packit a4058c
#include "config.h"
Packit a4058c
#include <stdio.h>
Packit a4058c
#include <stdlib.h>
Packit a4058c
#include <string.h>
Packit a4058c
#include <glib.h>
Packit a4058c
#ifdef HAVE_UNISTD_H
Packit a4058c
#include <unistd.h> /* for unlink */
Packit a4058c
#endif
Packit a4058c
#include <errno.h>
Packit a4058c
#include "gdk-pixbuf-private.h"
Packit a4058c
#include <glib/gstdio.h>
Packit a4058c
Packit a4058c
Packit a4058c

Packit a4058c
Packit a4058c
/* I have must have done something to deserve this.
Packit a4058c
 * XPM is such a crappy format to handle.
Packit a4058c
 * This code is an ugly hybrid from gdkpixmap.c
Packit a4058c
 * modified to respect transparent colors.
Packit a4058c
 * It's still a mess, though.
Packit a4058c
 */
Packit a4058c
Packit a4058c
enum buf_op {
Packit a4058c
	op_header,
Packit a4058c
	op_cmap,
Packit a4058c
	op_body
Packit a4058c
};
Packit a4058c
Packit a4058c
typedef struct {
Packit a4058c
	gchar *color_string;
Packit a4058c
	guint16 red;
Packit a4058c
	guint16 green;
Packit a4058c
	guint16 blue;
Packit a4058c
	gint transparent;
Packit a4058c
} XPMColor;
Packit a4058c
Packit a4058c
struct file_handle {
Packit a4058c
	FILE *infile;
Packit a4058c
	gchar *buffer;
Packit a4058c
	guint buffer_size;
Packit a4058c
};
Packit a4058c
Packit a4058c
struct mem_handle {
Packit a4058c
	const gchar **data;
Packit a4058c
	int offset;
Packit a4058c
};
Packit a4058c
Packit a4058c
/* The following 2 routines (parse_color, find_color) come from Tk, via the Win32
Packit a4058c
 * port of GDK. The licensing terms on these (longer than the functions) is:
Packit a4058c
 *
Packit a4058c
 * This software is copyrighted by the Regents of the University of
Packit a4058c
 * California, Sun Microsystems, Inc., and other parties.  The following
Packit a4058c
 * terms apply to all files associated with the software unless explicitly
Packit a4058c
 * disclaimed in individual files.
Packit a4058c
 * 
Packit a4058c
 * The authors hereby grant permission to use, copy, modify, distribute,
Packit a4058c
 * and license this software and its documentation for any purpose, provided
Packit a4058c
 * that existing copyright notices are retained in all copies and that this
Packit a4058c
 * notice is included verbatim in any distributions. No written agreement,
Packit a4058c
 * license, or royalty fee is required for any of the authorized uses.
Packit a4058c
 * Modifications to this software may be copyrighted by their authors
Packit a4058c
 * and need not follow the licensing terms described here, provided that
Packit a4058c
 * the new terms are clearly indicated on the first page of each file where
Packit a4058c
 * they apply.
Packit a4058c
 * 
Packit a4058c
 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
Packit a4058c
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
Packit a4058c
 * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
Packit a4058c
 * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
Packit a4058c
 * POSSIBILITY OF SUCH DAMAGE.
Packit a4058c
 * 
Packit a4058c
 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
Packit a4058c
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
Packit a4058c
 * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
Packit a4058c
 * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
Packit a4058c
 * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
Packit a4058c
 * MODIFICATIONS.
Packit a4058c
 * 
Packit a4058c
 * GOVERNMENT USE: If you are acquiring this software on behalf of the
Packit a4058c
 * U.S. government, the Government shall have only "Restricted Rights"
Packit a4058c
 * in the software and related documentation as defined in the Federal 
Packit a4058c
 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
Packit a4058c
 * are acquiring the software on behalf of the Department of Defense, the
Packit a4058c
 * software shall be classified as "Commercial Computer Software" and the
Packit a4058c
 * Government shall have only "Restricted Rights" as defined in Clause
Packit a4058c
 * 252.227-7013 (c) (1) of DFARs.  Notwithstanding the foregoing, the
Packit a4058c
 * authors grant the U.S. Government and others acting in its behalf
Packit a4058c
 * permission to use and distribute the software in accordance with the
Packit a4058c
 * terms specified in this license.
Packit a4058c
 */
Packit a4058c
Packit a4058c
#include "xpm-color-table.h"
Packit a4058c
 
Packit a4058c
/*
Packit a4058c
 *----------------------------------------------------------------------
Packit a4058c
 *
Packit a4058c
 * find_color --
Packit a4058c
 *
Packit a4058c
 *	This routine finds the color entry that corresponds to the
Packit a4058c
 *	specified color.
Packit a4058c
 *
Packit a4058c
 * Results:
Packit a4058c
 *	Returns non-zero on success.  The RGB values of the XColor
Packit a4058c
 *	will be initialized to the proper values on success.
Packit a4058c
 *
Packit a4058c
 * Side effects:
Packit a4058c
 *	None.
Packit a4058c
 *
Packit a4058c
 *----------------------------------------------------------------------
Packit a4058c
 */
Packit a4058c
Packit a4058c
static int
Packit a4058c
compare_xcolor_entries (const void *a, const void *b)
Packit a4058c
{
Packit a4058c
  return g_ascii_strcasecmp ((const char *) a, 
Packit a4058c
			     color_names + ((const XPMColorEntry *)b)->name_offset);
Packit a4058c
}
Packit a4058c
Packit a4058c
static gboolean
Packit a4058c
find_color(const char *name,
Packit a4058c
	   XPMColor   *colorPtr)
Packit a4058c
{
Packit a4058c
	XPMColorEntry *found;
Packit a4058c
Packit a4058c
	found = bsearch (name, xColors, G_N_ELEMENTS (xColors), sizeof (XPMColorEntry),
Packit a4058c
			 compare_xcolor_entries);
Packit a4058c
	if (found == NULL)
Packit a4058c
	  return FALSE;
Packit a4058c
	
Packit a4058c
	colorPtr->red = (found->red * 65535) / 255;
Packit a4058c
	colorPtr->green = (found->green * 65535) / 255;
Packit a4058c
	colorPtr->blue = (found->blue * 65535) / 255;
Packit a4058c
	
Packit a4058c
	return TRUE;
Packit a4058c
}
Packit a4058c
Packit a4058c
/*
Packit a4058c
 *----------------------------------------------------------------------
Packit a4058c
 *
Packit a4058c
 * parse_color --
Packit a4058c
 *
Packit a4058c
 *	Partial implementation of X color name parsing interface.
Packit a4058c
 *
Packit a4058c
 * Results:
Packit a4058c
 *	Returns TRUE on success.
Packit a4058c
 *
Packit a4058c
 * Side effects:
Packit a4058c
 *	None.
Packit a4058c
 *
Packit a4058c
 *----------------------------------------------------------------------
Packit a4058c
 */
Packit a4058c
Packit a4058c
static gboolean
Packit a4058c
parse_color (const char *spec,
Packit a4058c
	     XPMColor   *colorPtr)
Packit a4058c
{
Packit a4058c
	if (spec[0] == '#') {
Packit a4058c
		int i, red, green, blue;
Packit a4058c
Packit a4058c
		if ((i = strlen (spec + 1)) % 3) {
Packit a4058c
			return FALSE;
Packit a4058c
		}
Packit a4058c
		i /= 3;
Packit a4058c
Packit a4058c
		if (i == 4) {
Packit a4058c
			if (sscanf (spec + 1, "%4x%4x%4x", &red, &green, &blue) != 3)
Packit a4058c
				return FALSE;
Packit a4058c
			colorPtr->red = red;
Packit a4058c
			colorPtr->green = green;
Packit a4058c
			colorPtr->blue = blue;
Packit a4058c
		} else if (i == 1) {
Packit a4058c
			if (sscanf (spec + 1, "%1x%1x%1x", &red, &green, &blue) != 3)
Packit a4058c
				return FALSE;
Packit a4058c
			colorPtr->red = (red * 65535) / 15;
Packit a4058c
			colorPtr->green = (green * 65535) / 15;
Packit a4058c
			colorPtr->blue = (blue * 65535) / 15;
Packit a4058c
		} else if (i == 2) {
Packit a4058c
			if (sscanf (spec + 1, "%2x%2x%2x", &red, &green, &blue) != 3)
Packit a4058c
				return FALSE;
Packit a4058c
			colorPtr->red = (red * 65535) / 255;
Packit a4058c
			colorPtr->green = (green * 65535) / 255;
Packit a4058c
			colorPtr->blue = (blue * 65535) / 255;
Packit a4058c
		} else /* if (i == 3) */ {
Packit a4058c
			if (sscanf (spec + 1, "%3x%3x%3x", &red, &green, &blue) != 3)
Packit a4058c
				return FALSE;
Packit a4058c
			colorPtr->red = (red * 65535) / 4095;
Packit a4058c
			colorPtr->green = (green * 65535) / 4095;
Packit a4058c
			colorPtr->blue = (blue * 65535) / 4095;
Packit a4058c
		}
Packit a4058c
	} else {
Packit a4058c
		if (!find_color(spec, colorPtr))
Packit a4058c
			return FALSE;
Packit a4058c
	}
Packit a4058c
	return TRUE;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gint
Packit a4058c
xpm_seek_string (FILE *infile, const gchar *str)
Packit a4058c
{
Packit a4058c
	char instr[1024];
Packit a4058c
Packit a4058c
	while (!feof (infile)) {
Packit a4058c
		if (fscanf (infile, "%1023s", instr) < 0)
Packit a4058c
                        return FALSE;
Packit a4058c
		if (strcmp (instr, str) == 0)
Packit a4058c
			return TRUE;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	return FALSE;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gint
Packit a4058c
xpm_seek_char (FILE *infile, gchar c)
Packit a4058c
{
Packit a4058c
	gint b, oldb;
Packit a4058c
Packit a4058c
	while ((b = getc (infile)) != EOF) {
Packit a4058c
		if (c != b && b == '/') {
Packit a4058c
			b = getc (infile);
Packit a4058c
			if (b == EOF)
Packit a4058c
				return FALSE;
Packit a4058c
Packit a4058c
			else if (b == '*') {	/* we have a comment */
Packit a4058c
				b = -1;
Packit a4058c
				do {
Packit a4058c
					oldb = b;
Packit a4058c
					b = getc (infile);
Packit a4058c
					if (b == EOF)
Packit a4058c
						return FALSE;
Packit a4058c
				} while (!(oldb == '*' && b == '/'));
Packit a4058c
			}
Packit a4058c
		} else if (c == b)
Packit a4058c
			return TRUE;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	return FALSE;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gint
Packit a4058c
xpm_read_string (FILE *infile, gchar **buffer, guint *buffer_size)
Packit a4058c
{
Packit a4058c
	gint c;
Packit a4058c
	guint cnt = 0, bufsiz, ret = FALSE;
Packit a4058c
	gchar *buf;
Packit a4058c
Packit a4058c
	buf = *buffer;
Packit a4058c
	bufsiz = *buffer_size;
Packit a4058c
	if (buf == NULL) {
Packit a4058c
		bufsiz = 10 * sizeof (gchar);
Packit a4058c
		buf = g_new (gchar, bufsiz);
Packit a4058c
	}
Packit a4058c
Packit a4058c
	do {
Packit a4058c
		c = getc (infile);
Packit a4058c
	} while (c != EOF && c != '"');
Packit a4058c
Packit a4058c
	if (c != '"')
Packit a4058c
		goto out;
Packit a4058c
Packit a4058c
	while ((c = getc (infile)) != EOF) {
Packit a4058c
		if (cnt == bufsiz) {
Packit a4058c
			guint new_size = bufsiz * 2;
Packit a4058c
Packit a4058c
			if (new_size > bufsiz)
Packit a4058c
				bufsiz = new_size;
Packit a4058c
			else
Packit a4058c
				goto out;
Packit a4058c
Packit a4058c
			buf = g_realloc (buf, bufsiz);
Packit a4058c
			buf[bufsiz - 1] = '\0';
Packit a4058c
		}
Packit a4058c
Packit a4058c
		if (c != '"')
Packit a4058c
			buf[cnt++] = c;
Packit a4058c
		else {
Packit a4058c
			buf[cnt] = 0;
Packit a4058c
			ret = TRUE;
Packit a4058c
			break;
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
Packit a4058c
 out:
Packit a4058c
	buf[bufsiz - 1] = '\0';	/* ensure null termination for errors */
Packit a4058c
	*buffer = buf;
Packit a4058c
	*buffer_size = bufsiz;
Packit a4058c
	return ret;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gchar *
Packit a4058c
xpm_extract_color (const gchar *buffer)
Packit a4058c
{
Packit a4058c
	const gchar *p = &buffer[0];
Packit a4058c
	gint new_key = 0;
Packit a4058c
	gint key = 0;
Packit a4058c
	gint current_key = 1;
Packit a4058c
	gint space = 128;
Packit a4058c
	gchar word[129], color[129], current_color[129];
Packit a4058c
	gchar *r; 
Packit a4058c
	
Packit a4058c
	word[0] = '\0';
Packit a4058c
	color[0] = '\0';
Packit a4058c
	current_color[0] = '\0';
Packit a4058c
        while (1) {
Packit a4058c
		/* skip whitespace */
Packit a4058c
		for (; *p != '\0' && g_ascii_isspace (*p); p++) {
Packit a4058c
		} 
Packit a4058c
		/* copy word */
Packit a4058c
		for (r = word; *p != '\0' && !g_ascii_isspace (*p) && r - word < sizeof (word) - 1; p++, r++) {
Packit a4058c
			*r = *p;
Packit a4058c
		}
Packit a4058c
		*r = '\0';
Packit a4058c
		if (*word == '\0') {
Packit a4058c
			if (color[0] == '\0')  /* incomplete colormap entry */
Packit a4058c
				return NULL; 				
Packit a4058c
			else  /* end of entry, still store the last color */
Packit a4058c
				new_key = 1;
Packit a4058c
		} 
Packit a4058c
		else if (key > 0 && color[0] == '\0')  /* next word must be a color name part */
Packit a4058c
			new_key = 0;
Packit a4058c
		else {
Packit a4058c
			if (strcmp (word, "c") == 0)
Packit a4058c
				new_key = 5;
Packit a4058c
			else if (strcmp (word, "g") == 0)
Packit a4058c
				new_key = 4;
Packit a4058c
			else if (strcmp (word, "g4") == 0)
Packit a4058c
				new_key = 3;
Packit a4058c
			else if (strcmp (word, "m") == 0)
Packit a4058c
				new_key = 2;
Packit a4058c
			else if (strcmp (word, "s") == 0)
Packit a4058c
				new_key = 1;
Packit a4058c
			else 
Packit a4058c
				new_key = 0;
Packit a4058c
		}
Packit a4058c
		if (new_key == 0) {  /* word is a color name part */
Packit a4058c
			if (key == 0)  /* key expected */
Packit a4058c
				return NULL;
Packit a4058c
			/* accumulate color name */
Packit a4058c
			if (color[0] != '\0') {
Packit a4058c
				strncat (color, " ", space);
Packit a4058c
				space -= MIN (space, 1);
Packit a4058c
			}
Packit a4058c
			strncat (color, word, space);
Packit a4058c
			space -= MIN (space, strlen (word));
Packit a4058c
		}
Packit a4058c
		else {  /* word is a key */
Packit a4058c
			if (key > current_key) {
Packit a4058c
				current_key = key;
Packit a4058c
				strcpy (current_color, color);
Packit a4058c
			}
Packit a4058c
			space = 128;
Packit a4058c
			color[0] = '\0';
Packit a4058c
			key = new_key;
Packit a4058c
			if (*p == '\0') break;
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
	}
Packit a4058c
	if (current_key > 1)
Packit a4058c
		return g_strdup (current_color);
Packit a4058c
	else
Packit a4058c
		return NULL; 
Packit a4058c
}
Packit a4058c
Packit a4058c
/* (almost) direct copy from gdkpixmap.c... loads an XPM from a file */
Packit a4058c
Packit a4058c
static const gchar *
Packit a4058c
file_buffer (enum buf_op op, gpointer handle)
Packit a4058c
{
Packit a4058c
	struct file_handle *h = handle;
Packit a4058c
Packit a4058c
	switch (op) {
Packit a4058c
	case op_header:
Packit a4058c
		if (xpm_seek_string (h->infile, "XPM") != TRUE)
Packit a4058c
			break;
Packit a4058c
Packit a4058c
		if (xpm_seek_char (h->infile, '{') != TRUE)
Packit a4058c
			break;
Packit a4058c
		/* Fall through to the next xpm_seek_char. */
Packit a4058c
Packit a4058c
	case op_cmap:
Packit a4058c
		xpm_seek_char (h->infile, '"');
Packit a4058c
		if (fseek (h->infile, -1, SEEK_CUR) != 0)
Packit a4058c
			return NULL;
Packit a4058c
		/* Fall through to the xpm_read_string. */
Packit a4058c
Packit a4058c
	case op_body:
Packit a4058c
		if(!xpm_read_string (h->infile, &h->buffer, &h->buffer_size))
Packit a4058c
			return NULL;
Packit a4058c
		return h->buffer;
Packit a4058c
Packit a4058c
	default:
Packit a4058c
		g_assert_not_reached ();
Packit a4058c
	}
Packit a4058c
Packit a4058c
	return NULL;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* This reads from memory */
Packit a4058c
static const gchar *
Packit a4058c
mem_buffer (enum buf_op op, gpointer handle)
Packit a4058c
{
Packit a4058c
	struct mem_handle *h = handle;
Packit a4058c
	switch (op) {
Packit a4058c
	case op_header:
Packit a4058c
	case op_cmap:
Packit a4058c
	case op_body:
Packit a4058c
                if (h->data[h->offset]) {
Packit a4058c
                        const gchar* retval;
Packit a4058c
Packit a4058c
                        retval = h->data[h->offset];
Packit a4058c
                        h->offset += 1;
Packit a4058c
                        return retval;
Packit a4058c
                }
Packit a4058c
                break;
Packit a4058c
Packit a4058c
	default:
Packit a4058c
		g_assert_not_reached ();
Packit a4058c
                break;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	return NULL;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* This function does all the work. */
Packit a4058c
static GdkPixbuf *
Packit a4058c
pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handle), gpointer handle,
Packit a4058c
                        GError **error)
Packit a4058c
{
Packit a4058c
	gint w, h, n_col, cpp, x_hot, y_hot, items;
Packit a4058c
	gint cnt, xcnt, ycnt, wbytes, n;
Packit a4058c
	gint is_trans = FALSE;
Packit a4058c
	const gchar *buffer;
Packit a4058c
        gchar *name_buf;
Packit a4058c
	gchar pixel_str[32];
Packit a4058c
	GHashTable *color_hash;
Packit a4058c
	XPMColor *colors, *color, *fallbackcolor;
Packit a4058c
	guchar *pixtmp;
Packit a4058c
	GdkPixbuf *pixbuf;
Packit a4058c
Packit a4058c
	fallbackcolor = NULL;
Packit a4058c
Packit a4058c
	buffer = (*get_buf) (op_header, handle);
Packit a4058c
	if (!buffer) {
Packit a4058c
                g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("No XPM header found"));
Packit a4058c
		return NULL;
Packit a4058c
	}
Packit a4058c
	items = sscanf (buffer, "%d %d %d %d %d %d", &w, &h, &n_col, &cpp, &x_hot, &y_hot);
Packit a4058c
Packit a4058c
	if (items != 4 && items != 6) {
Packit a4058c
		g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("Invalid XPM header"));
Packit a4058c
		return NULL;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	if (w <= 0) {
Packit a4058c
                g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("XPM file has image width <= 0"));
Packit a4058c
		return NULL;
Packit a4058c
Packit a4058c
	}
Packit a4058c
	if (h <= 0) {
Packit a4058c
                g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("XPM file has image height <= 0"));
Packit a4058c
		return NULL;
Packit a4058c
Packit a4058c
	}
Packit a4058c
	if (cpp <= 0 || cpp >= 32) {
Packit a4058c
                g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("XPM has invalid number of chars per pixel"));
Packit a4058c
		return NULL;
Packit a4058c
	}
Packit a4058c
	if (n_col <= 0 || 
Packit a4058c
	    n_col >= G_MAXINT / (cpp + 1) || 
Packit a4058c
	    n_col >= G_MAXINT / sizeof (XPMColor)) {
Packit a4058c
                g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("XPM file has invalid number of colors"));
Packit a4058c
		return NULL;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	/* The hash is used for fast lookups of color from chars */
Packit a4058c
	color_hash = g_hash_table_new (g_str_hash, g_str_equal);
Packit a4058c
Packit a4058c
	name_buf = g_try_malloc (n_col * (cpp + 1));
Packit a4058c
	if (!name_buf) {
Packit a4058c
		g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Packit a4058c
                                     _("Cannot allocate memory for loading XPM image"));
Packit a4058c
		g_hash_table_destroy (color_hash);
Packit a4058c
		return NULL;
Packit a4058c
	}
Packit a4058c
	colors = (XPMColor *) g_try_malloc (sizeof (XPMColor) * n_col);
Packit a4058c
	if (!colors) {
Packit a4058c
		g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Packit a4058c
                                     _("Cannot allocate memory for loading XPM image"));
Packit a4058c
		g_hash_table_destroy (color_hash);
Packit a4058c
		g_free (name_buf);
Packit a4058c
		return NULL;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	for (cnt = 0; cnt < n_col; cnt++) {
Packit a4058c
		gchar *color_name;
Packit a4058c
Packit a4058c
		buffer = (*get_buf) (op_cmap, handle);
Packit a4058c
		if (!buffer) {
Packit a4058c
                        g_set_error_literal (error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("Cannot read XPM colormap"));
Packit a4058c
			g_hash_table_destroy (color_hash);
Packit a4058c
			g_free (name_buf);
Packit a4058c
			g_free (colors);
Packit a4058c
			return NULL;
Packit a4058c
		}
Packit a4058c
Packit a4058c
		color = &colors[cnt];
Packit a4058c
		color->color_string = &name_buf[cnt * (cpp + 1)];
Packit a4058c
		strncpy (color->color_string, buffer, cpp);
Packit a4058c
		color->color_string[cpp] = 0;
Packit a4058c
		buffer += strlen (color->color_string);
Packit a4058c
		color->transparent = FALSE;
Packit a4058c
Packit a4058c
		color_name = xpm_extract_color (buffer);
Packit a4058c
Packit a4058c
		if ((color_name == NULL) || (g_ascii_strcasecmp (color_name, "None") == 0)
Packit a4058c
		    || (parse_color (color_name, color) == FALSE)) {
Packit a4058c
			color->transparent = TRUE;
Packit a4058c
			color->red = 0;
Packit a4058c
			color->green = 0;
Packit a4058c
			color->blue = 0;
Packit a4058c
			is_trans = TRUE;
Packit a4058c
		}
Packit a4058c
Packit a4058c
		g_free (color_name);
Packit a4058c
		g_hash_table_insert (color_hash, color->color_string, color);
Packit a4058c
Packit a4058c
		if (cnt == 0)
Packit a4058c
			fallbackcolor = color;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, is_trans, 8, w, h);
Packit a4058c
Packit a4058c
	if (!pixbuf) {
Packit a4058c
                g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Packit a4058c
                                     _("Cannot allocate memory for loading XPM image"));
Packit a4058c
		g_hash_table_destroy (color_hash);
Packit a4058c
		g_free (colors);
Packit a4058c
		g_free (name_buf);
Packit a4058c
		return NULL;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	wbytes = w * cpp;
Packit a4058c
Packit a4058c
	for (ycnt = 0; ycnt < h; ycnt++) {
Packit a4058c
		pixtmp = pixbuf->pixels + ycnt * pixbuf->rowstride;
Packit a4058c
Packit a4058c
		buffer = (*get_buf) (op_body, handle);
Packit a4058c
		if ((!buffer) || (strlen (buffer) < wbytes))
Packit a4058c
			continue;
Packit a4058c
Packit a4058c
		for (n = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++) {
Packit a4058c
			strncpy (pixel_str, &buffer[n], cpp);
Packit a4058c
			pixel_str[cpp] = 0;
Packit a4058c
Packit a4058c
			color = g_hash_table_lookup (color_hash, pixel_str);
Packit a4058c
Packit a4058c
			/* Bad XPM...punt */
Packit a4058c
			if (!color)
Packit a4058c
				color = fallbackcolor;
Packit a4058c
Packit a4058c
			*pixtmp++ = color->red >> 8;
Packit a4058c
			*pixtmp++ = color->green >> 8;
Packit a4058c
			*pixtmp++ = color->blue >> 8;
Packit a4058c
Packit a4058c
			if (is_trans && color->transparent)
Packit a4058c
				*pixtmp++ = 0;
Packit a4058c
			else if (is_trans)
Packit a4058c
				*pixtmp++ = 0xFF;
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
Packit a4058c
	g_hash_table_destroy (color_hash);
Packit a4058c
	g_free (colors);
Packit a4058c
	g_free (name_buf);
Packit a4058c
Packit a4058c
	if (items == 6) {
Packit a4058c
		gchar hot[10];
Packit a4058c
		g_snprintf (hot, 10, "%d", x_hot);
Packit a4058c
		gdk_pixbuf_set_option (pixbuf, "x_hot", hot);
Packit a4058c
		g_snprintf (hot, 10, "%d", y_hot);
Packit a4058c
		gdk_pixbuf_set_option (pixbuf, "y_hot", hot);
Packit a4058c
Packit a4058c
	}
Packit a4058c
Packit a4058c
	return pixbuf;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Shared library entry point for file loading */
Packit a4058c
static GdkPixbuf *
Packit a4058c
gdk_pixbuf__xpm_image_load (FILE *f,
Packit a4058c
                            GError **error)
Packit a4058c
{
Packit a4058c
	GdkPixbuf *pixbuf;
Packit a4058c
	struct file_handle h;
Packit a4058c
Packit a4058c
	memset (&h, 0, sizeof (h));
Packit a4058c
	h.infile = f;
Packit a4058c
	pixbuf = pixbuf_create_from_xpm (file_buffer, &h, error);
Packit a4058c
	g_free (h.buffer);
Packit a4058c
Packit a4058c
	return pixbuf;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Shared library entry point for memory loading */
Packit a4058c
static GdkPixbuf *
Packit a4058c
gdk_pixbuf__xpm_image_load_xpm_data (const gchar **data)
Packit a4058c
{
Packit a4058c
        GdkPixbuf *pixbuf;
Packit a4058c
        struct mem_handle h;
Packit a4058c
        GError *error = NULL;
Packit a4058c
        
Packit a4058c
        h.data = data;
Packit a4058c
        h.offset = 0;
Packit a4058c
        
Packit a4058c
	pixbuf = pixbuf_create_from_xpm (mem_buffer, &h, &error);
Packit a4058c
Packit a4058c
        if (error) {
Packit a4058c
                g_warning ("Inline XPM data is broken: %s", error->message);
Packit a4058c
                g_error_free (error);
Packit a4058c
                error = NULL;
Packit a4058c
        }
Packit a4058c
        
Packit a4058c
	return pixbuf;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Progressive loader */
Packit a4058c
typedef struct _XPMContext XPMContext;
Packit a4058c
struct _XPMContext
Packit a4058c
{
Packit a4058c
       GdkPixbufModulePreparedFunc prepare_func;
Packit a4058c
       GdkPixbufModuleUpdatedFunc update_func;
Packit a4058c
       gpointer user_data;
Packit a4058c
Packit a4058c
       gchar *tempname;
Packit a4058c
       FILE *file;
Packit a4058c
       gboolean all_okay;
Packit a4058c
};
Packit a4058c
Packit a4058c
/*
Packit a4058c
 * FIXME xpm loading progressively is not properly implemented.
Packit a4058c
 * Instead we will buffer to a file then load that file when done.
Packit a4058c
 * This is very broken but it should be relatively simple to fix
Packit a4058c
 * in the future.
Packit a4058c
 */
Packit a4058c
static gpointer
Packit a4058c
gdk_pixbuf__xpm_image_begin_load (GdkPixbufModuleSizeFunc size_func,
Packit a4058c
                                  GdkPixbufModulePreparedFunc prepare_func,
Packit a4058c
                                  GdkPixbufModuleUpdatedFunc update_func,
Packit a4058c
                                  gpointer user_data,
Packit a4058c
                                  GError **error)
Packit a4058c
{
Packit a4058c
       XPMContext *context;
Packit a4058c
       gint fd;
Packit a4058c
Packit a4058c
       context = g_new (XPMContext, 1);
Packit a4058c
       context->prepare_func = prepare_func;
Packit a4058c
       context->update_func = update_func;
Packit a4058c
       context->user_data = user_data;
Packit a4058c
       context->all_okay = TRUE;
Packit a4058c
       fd = g_file_open_tmp ("gdkpixbuf-xpm-tmp.XXXXXX", &context->tempname,
Packit a4058c
			     NULL);
Packit a4058c
       if (fd < 0) {
Packit a4058c
               g_free (context);
Packit a4058c
               return NULL;
Packit a4058c
       }
Packit a4058c
Packit a4058c
       context->file = fdopen (fd, "w+");
Packit a4058c
       if (context->file == NULL) {
Packit a4058c
               g_free (context->tempname);
Packit a4058c
               g_free (context);
Packit a4058c
               return NULL;
Packit a4058c
       }
Packit a4058c
Packit a4058c
       return context;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gboolean
Packit a4058c
gdk_pixbuf__xpm_image_stop_load (gpointer data,
Packit a4058c
                                 GError **error)
Packit a4058c
{
Packit a4058c
       XPMContext *context = (XPMContext*) data;
Packit a4058c
       GdkPixbuf *pixbuf;
Packit a4058c
       gboolean retval = FALSE;
Packit a4058c
       
Packit a4058c
       g_return_val_if_fail (data != NULL, FALSE);
Packit a4058c
Packit a4058c
       fflush (context->file);
Packit a4058c
       rewind (context->file);
Packit a4058c
       if (context->all_okay) {
Packit a4058c
               pixbuf = gdk_pixbuf__xpm_image_load (context->file, error);
Packit a4058c
Packit a4058c
               if (pixbuf != NULL) {
Packit a4058c
		       if (context->prepare_func)
Packit a4058c
			       (* context->prepare_func) (pixbuf,
Packit a4058c
							  NULL,
Packit a4058c
							  context->user_data);
Packit a4058c
		       if (context->update_func)
Packit a4058c
			       (* context->update_func) (pixbuf, 0, 0, pixbuf->width, pixbuf->height, context->user_data);
Packit a4058c
                       g_object_unref (pixbuf);
Packit a4058c
Packit a4058c
                       retval = TRUE;
Packit a4058c
               }
Packit a4058c
       }
Packit a4058c
Packit a4058c
       fclose (context->file);
Packit a4058c
       g_unlink (context->tempname);
Packit a4058c
       g_free (context->tempname);
Packit a4058c
       g_free ((XPMContext *) context);
Packit a4058c
Packit a4058c
       return retval;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gboolean
Packit a4058c
gdk_pixbuf__xpm_image_load_increment (gpointer data,
Packit a4058c
                                      const guchar *buf,
Packit a4058c
                                      guint    size,
Packit a4058c
                                      GError **error)
Packit a4058c
{
Packit a4058c
       XPMContext *context = (XPMContext *) data;
Packit a4058c
Packit a4058c
       g_return_val_if_fail (data != NULL, FALSE);
Packit a4058c
Packit a4058c
       if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
Packit a4058c
	       gint save_errno = errno;
Packit a4058c
               context->all_okay = FALSE;
Packit a4058c
               g_set_error_literal (error,
Packit a4058c
                                    G_FILE_ERROR,
Packit a4058c
                                    g_file_error_from_errno (save_errno),
Packit a4058c
                                    _("Failed to write to temporary file when loading XPM image"));
Packit a4058c
               return FALSE;
Packit a4058c
       }
Packit a4058c
Packit a4058c
       return TRUE;
Packit a4058c
}
Packit a4058c
Packit a4058c
#ifndef INCLUDE_xpm
Packit a4058c
#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
Packit a4058c
#else
Packit a4058c
#define MODULE_ENTRY(function) void _gdk_pixbuf__xpm_ ## function
Packit a4058c
#endif
Packit a4058c
Packit a4058c
MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
Packit a4058c
{
Packit a4058c
	module->load = gdk_pixbuf__xpm_image_load;
Packit a4058c
	module->load_xpm_data = gdk_pixbuf__xpm_image_load_xpm_data;
Packit a4058c
	module->begin_load = gdk_pixbuf__xpm_image_begin_load;
Packit a4058c
	module->stop_load = gdk_pixbuf__xpm_image_stop_load;
Packit a4058c
	module->load_increment = gdk_pixbuf__xpm_image_load_increment;
Packit a4058c
}
Packit a4058c
Packit a4058c
MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
Packit a4058c
{
Packit a4058c
	static const GdkPixbufModulePattern signature[] = {
Packit a4058c
		{ "/* XPM */", NULL, 100 },
Packit a4058c
		{ NULL, NULL, 0 }
Packit a4058c
	};
Packit a4058c
	static const gchar *mime_types[] = {
Packit a4058c
		"image/x-xpixmap",
Packit a4058c
		NULL
Packit a4058c
	};
Packit a4058c
	static const gchar *extensions[] = {
Packit a4058c
		"xpm",
Packit a4058c
		NULL
Packit a4058c
	};
Packit a4058c
Packit a4058c
	info->name = "xpm";
Packit a4058c
	info->signature = (GdkPixbufModulePattern *) signature;
Packit a4058c
	info->description = NC_("image format", "XPM");
Packit a4058c
	info->mime_types = (gchar **) mime_types;
Packit a4058c
	info->extensions = (gchar **) extensions;
Packit a4058c
	info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
Packit a4058c
	info->license = "LGPL";
Packit a4058c
}