1999-10-20 21:20:49 +00:00
|
|
|
|
/* GdkPixbuf library - JPEG image loader
|
|
|
|
|
*
|
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-30 15:28:43 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
#include <gdk/gdk.h>
|
|
|
|
|
#include "gdk-pixbuf.h"
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
|
|
|
|
/* I have must have done something to deserve this.
|
|
|
|
|
* XPM is such a crappy format to handle.
|
|
|
|
|
* This code is an ugly hybred from gdkpixmap.c
|
|
|
|
|
* modified to respect transparent colors.
|
|
|
|
|
* It's still a mess, though.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
enum buf_op {
|
1999-10-18 19:29:45 +00:00
|
|
|
|
op_header,
|
|
|
|
|
op_cmap,
|
|
|
|
|
op_body
|
1999-06-30 15:28:43 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gchar *color_string;
|
|
|
|
|
GdkColor color;
|
|
|
|
|
gint transparent;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
} _XPMColor;
|
|
|
|
|
|
|
|
|
|
struct file_handle {
|
1999-10-18 19:29:45 +00:00
|
|
|
|
FILE *infile;
|
|
|
|
|
gchar *buffer;
|
|
|
|
|
guint buffer_size;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
};
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
|
|
|
|
struct mem_handle {
|
1999-10-22 21:05:16 +00:00
|
|
|
|
const gchar **data;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
int offset;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
};
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
|
|
|
|
static gint
|
1999-10-20 21:20:49 +00:00
|
|
|
|
xpm_seek_string (FILE *infile, const gchar *str, gint skip_comments)
|
1999-06-30 15:28:43 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
char instr[1024];
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
while (!feof (infile)) {
|
|
|
|
|
fscanf (infile, "%1023s", instr);
|
|
|
|
|
if (skip_comments == TRUE && strcmp (instr, "/*") == 0) {
|
|
|
|
|
fscanf (infile, "%1023s", instr);
|
|
|
|
|
while (!feof (infile) && strcmp (instr, "*/") != 0)
|
|
|
|
|
fscanf (infile, "%1023s", instr);
|
|
|
|
|
fscanf (infile, "%1023s", instr);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
if (strcmp (instr, str) == 0)
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return TRUE;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return FALSE;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gint
|
1999-10-20 21:20:49 +00:00
|
|
|
|
xpm_seek_char (FILE *infile, gchar c)
|
1999-06-30 15:28:43 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gint b, oldb;
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
while ((b = getc (infile)) != EOF) {
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (c != b && b == '/') {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
b = getc (infile);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (b == EOF)
|
|
|
|
|
return FALSE;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
else if (b == '*') { /* we have a comment */
|
|
|
|
|
b = -1;
|
|
|
|
|
do {
|
|
|
|
|
oldb = b;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
b = getc (infile);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (b == EOF)
|
|
|
|
|
return FALSE;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
} while (!(oldb == '*' && b == '/'));
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
|
|
|
|
} else if (c == b)
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return FALSE;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gint
|
1999-10-20 21:20:49 +00:00
|
|
|
|
xpm_read_string (FILE *infile, gchar **buffer, guint *buffer_size)
|
1999-06-30 15:28:43 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gint c;
|
|
|
|
|
guint cnt = 0, bufsiz, ret = FALSE;
|
|
|
|
|
gchar *buf;
|
|
|
|
|
|
|
|
|
|
buf = *buffer;
|
|
|
|
|
bufsiz = *buffer_size;
|
|
|
|
|
if (buf == NULL) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
bufsiz = 10 * sizeof (gchar);
|
|
|
|
|
buf = g_new (gchar, bufsiz);
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
do {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
c = getc (infile);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
} while (c != EOF && c != '"');
|
|
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
|
if (c != '"')
|
1999-10-18 19:29:45 +00:00
|
|
|
|
goto out;
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
while ((c = getc (infile)) != EOF) {
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (cnt == bufsiz) {
|
|
|
|
|
guint new_size = bufsiz * 2;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (new_size > bufsiz)
|
|
|
|
|
bufsiz = new_size;
|
|
|
|
|
else
|
|
|
|
|
goto out;
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
buf = g_realloc (buf, bufsiz);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
buf[bufsiz - 1] = '\0';
|
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (c != '"')
|
|
|
|
|
buf[cnt++] = c;
|
|
|
|
|
else {
|
|
|
|
|
buf[cnt] = 0;
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
out:
|
|
|
|
|
buf[bufsiz - 1] = '\0'; /* ensure null termination for errors */
|
|
|
|
|
*buffer = buf;
|
|
|
|
|
*buffer_size = bufsiz;
|
|
|
|
|
return ret;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-22 23:26:22 +00:00
|
|
|
|
static const gchar *
|
|
|
|
|
xpm_skip_whitespaces (const gchar *buffer)
|
1999-06-30 15:28:43 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gint32 index = 0;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
|
|
|
|
|
index++;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return &buffer[index];
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-22 23:26:22 +00:00
|
|
|
|
static const gchar *
|
|
|
|
|
xpm_skip_string (const gchar *buffer)
|
1999-06-30 15:28:43 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gint32 index = 0;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
|
|
|
|
|
index++;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return &buffer[index];
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Xlib crashed once at a color name lengths around 125 */
|
|
|
|
|
#define MAX_COLOR_LEN 120
|
|
|
|
|
|
|
|
|
|
static gchar *
|
1999-10-22 23:26:22 +00:00
|
|
|
|
xpm_extract_color (const gchar *buffer)
|
1999-06-30 15:28:43 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gint counter, numnames;
|
1999-10-22 23:26:22 +00:00
|
|
|
|
const gchar *ptr = NULL;
|
|
|
|
|
gchar ch, temp[128];
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gchar color[MAX_COLOR_LEN], *retcol;
|
|
|
|
|
gint space;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
counter = 0;
|
|
|
|
|
while (ptr == NULL) {
|
|
|
|
|
if (buffer[counter] == 'c') {
|
|
|
|
|
ch = buffer[counter + 1];
|
|
|
|
|
if (ch == 0x20 || ch == 0x09)
|
|
|
|
|
ptr = &buffer[counter + 1];
|
|
|
|
|
} else if (buffer[counter] == 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
counter++;
|
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
ptr = xpm_skip_whitespaces (ptr);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
if (ptr[0] == 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
else if (ptr[0] == '#') {
|
|
|
|
|
counter = 1;
|
|
|
|
|
while (ptr[counter] != 0 &&
|
|
|
|
|
((ptr[counter] >= '0' && ptr[counter] <= '9') ||
|
|
|
|
|
(ptr[counter] >= 'a' && ptr[counter] <= 'f') ||
|
|
|
|
|
(ptr[counter] >= 'A' && ptr[counter] <= 'F')))
|
|
|
|
|
counter++;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
retcol = g_new (gchar, counter + 1);
|
|
|
|
|
strncpy (retcol, ptr, counter);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
retcol[counter] = 0;
|
|
|
|
|
|
|
|
|
|
return retcol;
|
|
|
|
|
}
|
|
|
|
|
color[0] = 0;
|
|
|
|
|
numnames = 0;
|
|
|
|
|
|
|
|
|
|
space = MAX_COLOR_LEN - 1;
|
|
|
|
|
while (space > 0) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
sscanf (ptr, "%127s", temp);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
if (((gint) ptr[0] == 0) ||
|
1999-10-20 21:20:49 +00:00
|
|
|
|
(strcmp ("s", temp) == 0) || (strcmp ("m", temp) == 0) ||
|
|
|
|
|
(strcmp ("g", temp) == 0) || (strcmp ("g4", temp) == 0))
|
1999-10-18 19:29:45 +00:00
|
|
|
|
break;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
else {
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (numnames > 0) {
|
|
|
|
|
space -= 1;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
strcat (color, " ");
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
strncat (color, temp, space);
|
|
|
|
|
space -= MIN (space, strlen (temp));
|
|
|
|
|
ptr = xpm_skip_string (ptr);
|
|
|
|
|
ptr = xpm_skip_whitespaces (ptr);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
numnames++;
|
|
|
|
|
}
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
retcol = g_strdup (color);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return retcol;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* (almost) direct copy from gdkpixmap.c... loads an XPM from a file */
|
|
|
|
|
|
1999-10-22 23:26:22 +00:00
|
|
|
|
static const gchar *
|
1999-10-20 21:20:49 +00:00
|
|
|
|
file_buffer (enum buf_op op, gpointer handle)
|
1999-06-30 15:28:43 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
struct file_handle *h = handle;
|
|
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
|
case op_header:
|
1999-10-20 21:20:49 +00:00
|
|
|
|
if (xpm_seek_string (h->infile, "XPM", FALSE) != TRUE)
|
1999-10-18 19:29:45 +00:00
|
|
|
|
break;
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
if (xpm_seek_char (h->infile, '{') != TRUE)
|
1999-10-18 19:29:45 +00:00
|
|
|
|
break;
|
|
|
|
|
/* Fall through to the next xpm_seek_char. */
|
|
|
|
|
|
|
|
|
|
case op_cmap:
|
1999-10-20 21:20:49 +00:00
|
|
|
|
xpm_seek_char (h->infile, '"');
|
|
|
|
|
fseek (h->infile, -1, SEEK_CUR);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
/* Fall through to the xpm_read_string. */
|
|
|
|
|
|
|
|
|
|
case op_body:
|
1999-10-20 21:20:49 +00:00
|
|
|
|
xpm_read_string (h->infile, &h->buffer, &h->buffer_size);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return h->buffer;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
g_assert_not_reached ();
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
return NULL;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This reads from memory */
|
1999-10-22 23:26:22 +00:00
|
|
|
|
static const gchar *
|
1999-10-20 21:20:49 +00:00
|
|
|
|
mem_buffer (enum buf_op op, gpointer handle)
|
1999-06-30 15:28:43 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
struct mem_handle *h = handle;
|
|
|
|
|
switch (op) {
|
|
|
|
|
case op_header:
|
|
|
|
|
case op_cmap:
|
|
|
|
|
case op_body:
|
1999-10-22 23:26:22 +00:00
|
|
|
|
if (h->data[h->offset]) {
|
|
|
|
|
const gchar* retval;
|
|
|
|
|
|
|
|
|
|
retval = h->data[h->offset];
|
|
|
|
|
h->offset += 1;
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
break;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
g_assert_not_reached ();
|
1999-10-22 23:26:22 +00:00
|
|
|
|
break;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return NULL;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
/* Destroy notification function for the libart pixbuf */
|
|
|
|
|
static void
|
|
|
|
|
free_buffer (gpointer user_data, gpointer data)
|
|
|
|
|
{
|
|
|
|
|
free (data);
|
|
|
|
|
}
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
/* This function does all the work. */
|
1999-10-18 19:29:45 +00:00
|
|
|
|
static GdkPixbuf *
|
1999-10-22 23:26:22 +00:00
|
|
|
|
pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handle), gpointer handle)
|
1999-06-30 15:28:43 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gint w, h, n_col, cpp;
|
|
|
|
|
gint cnt, xcnt, ycnt, wbytes, n, ns;
|
|
|
|
|
gint is_trans = FALSE;
|
1999-10-22 23:26:22 +00:00
|
|
|
|
const gchar *buffer;
|
|
|
|
|
gchar *name_buf;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gchar pixel_str[32];
|
|
|
|
|
GHashTable *color_hash;
|
|
|
|
|
_XPMColor *colors, *color, *fallbackcolor;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
guchar *pixels, *pixtmp;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
buffer = (*get_buf) (op_header, handle);
|
1999-06-30 15:28:43 +00:00
|
|
|
|
if (!buffer) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
g_warning ("No XPM header found");
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return NULL;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
sscanf (buffer, "%d %d %d %d", &w, &h, &n_col, &cpp);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (cpp >= 32) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
g_warning ("XPM has more than 31 chars per pixel.");
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return NULL;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
/* The hash is used for fast lookups of color from chars */
|
1999-10-20 21:20:49 +00:00
|
|
|
|
color_hash = g_hash_table_new (g_str_hash, g_str_equal);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
name_buf = g_new (gchar, n_col * (cpp + 1));
|
|
|
|
|
colors = g_new (_XPMColor, n_col);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
for (cnt = 0; cnt < n_col; cnt++) {
|
|
|
|
|
gchar *color_name;
|
|
|
|
|
|
|
|
|
|
buffer = (*get_buf) (op_cmap, handle);
|
|
|
|
|
if (!buffer) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
g_warning ("Can't load XPM colormap");
|
|
|
|
|
g_hash_table_destroy (color_hash);
|
|
|
|
|
g_free (name_buf);
|
|
|
|
|
g_free (colors);
|
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
|
|
|
|
color = &colors[cnt];
|
|
|
|
|
color->color_string = &name_buf[cnt * (cpp + 1)];
|
1999-10-20 21:20:49 +00:00
|
|
|
|
strncpy (color->color_string, buffer, cpp);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
color->color_string[cpp] = 0;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
buffer += strlen (color->color_string);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
color->transparent = FALSE;
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
color_name = xpm_extract_color (buffer);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
if ((color_name == NULL) || (g_strcasecmp (color_name, "None") == 0)
|
|
|
|
|
|| (gdk_color_parse (color_name, &color->color) == FALSE)) {
|
1999-10-18 19:29:45 +00:00
|
|
|
|
color->transparent = TRUE;
|
|
|
|
|
is_trans = TRUE;
|
|
|
|
|
}
|
1999-07-13 01:01:14 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
g_free (color_name);
|
|
|
|
|
g_hash_table_insert (color_hash, color->color_string, color);
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (cnt == 0)
|
|
|
|
|
fallbackcolor = color;
|
|
|
|
|
}
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (is_trans)
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixels = malloc (w * h * 4);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
else
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixels = malloc (w * h * 3);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
if (!pixels) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
g_hash_table_destroy (color_hash);
|
|
|
|
|
g_free (colors);
|
|
|
|
|
g_free (name_buf);
|
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
|
|
|
|
wbytes = w * cpp;
|
|
|
|
|
pixtmp = pixels;
|
|
|
|
|
|
|
|
|
|
for (ycnt = 0; ycnt < h; ycnt++) {
|
|
|
|
|
buffer = (*get_buf) (op_body, handle);
|
1999-10-20 21:20:49 +00:00
|
|
|
|
if ((!buffer) || (strlen (buffer) < wbytes))
|
1999-10-18 19:29:45 +00:00
|
|
|
|
continue;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++) {
|
1999-10-20 21:20:49 +00:00
|
|
|
|
strncpy (pixel_str, &buffer[n], cpp);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
pixel_str[cpp] = 0;
|
|
|
|
|
ns = 0;
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
color = g_hash_table_lookup (color_hash, pixel_str);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
/* Bad XPM...punt */
|
|
|
|
|
if (!color)
|
|
|
|
|
color = fallbackcolor;
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
*pixtmp++ = color->color.red >> 8;
|
|
|
|
|
*pixtmp++ = color->color.green >> 8;
|
|
|
|
|
*pixtmp++ = color->color.blue >> 8;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
if (is_trans && color->transparent)
|
1999-10-18 19:29:45 +00:00
|
|
|
|
*pixtmp++ = 0;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
else if (is_trans)
|
1999-10-18 19:29:45 +00:00
|
|
|
|
*pixtmp++ = 0xFF;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-07-13 01:01:14 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
g_hash_table_destroy (color_hash);
|
|
|
|
|
g_free (colors);
|
|
|
|
|
g_free (name_buf);
|
1999-06-30 15:28:43 +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-30 15:28:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Shared library entry point for file loading */
|
1999-10-18 19:29:45 +00:00
|
|
|
|
GdkPixbuf *
|
|
|
|
|
image_load (FILE *f)
|
1999-06-30 15:28:43 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
|
struct file_handle h;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
memset (&h, 0, sizeof (h));
|
1999-10-18 19:29:45 +00:00
|
|
|
|
h.infile = f;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixbuf = pixbuf_create_from_xpm (file_buffer, &h);
|
|
|
|
|
g_free (h.buffer);
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
return pixbuf;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
1999-10-22 21:05:16 +00:00
|
|
|
|
|
|
|
|
|
/* Shared library entry point for memory loading */
|
|
|
|
|
GdkPixbuf *
|
|
|
|
|
image_load_xpm_data (const gchar **data)
|
|
|
|
|
{
|
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
|
struct mem_handle h;
|
|
|
|
|
|
|
|
|
|
h.data = data;
|
|
|
|
|
h.offset = 0;
|
|
|
|
|
|
|
|
|
|
pixbuf = pixbuf_create_from_xpm (mem_buffer, &h);
|
|
|
|
|
|
|
|
|
|
return pixbuf;
|
|
|
|
|
}
|