|
Packit |
a4058c |
/* -*- mode: C; c-file-style: "linux" -*- */
|
|
Packit |
a4058c |
/* GdkPixbuf library - XBM image loader
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* Copyright (C) 1999 Mark Crichton
|
|
Packit |
a4058c |
* Copyright (C) 1999 The Free Software Foundation
|
|
Packit |
a4058c |
* Copyright (C) 2001 Eazel, Inc.
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* Authors: Mark Crichton <crichton@gimp.org>
|
|
Packit |
a4058c |
* Federico Mena-Quintero <federico@gimp.org>
|
|
Packit |
a4058c |
* Jonathan Blandford <jrb@redhat.com>
|
|
Packit |
a4058c |
* John Harper <jsh@eazel.com>
|
|
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 Library 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 |
* Library General Public License for more details.
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* You should have received a copy of the GNU Library General Public
|
|
Packit |
a4058c |
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
Packit |
a4058c |
*/
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Following code adapted from io-tiff.c, which was ``(almost) blatantly
|
|
Packit |
a4058c |
ripped from Imlib'' */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#include "config.h"
|
|
Packit |
a4058c |
#include <stdlib.h>
|
|
Packit |
a4058c |
#include <string.h>
|
|
Packit |
a4058c |
#ifdef HAVE_UNISTD_H
|
|
Packit |
a4058c |
#include <unistd.h>
|
|
Packit |
a4058c |
#endif
|
|
Packit |
a4058c |
#include <stdio.h>
|
|
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 |
typedef struct _XBMData XBMData;
|
|
Packit |
a4058c |
struct _XBMData
|
|
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 |
/* xbm parser borrowed from xc/lib/X11/RdBitF.c */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#define MAX_SIZE 255
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* shared data for the image read/parse logic */
|
|
Packit |
a4058c |
static short hex_table[256]; /* conversion value */
|
|
Packit |
a4058c |
static gboolean initialized = FALSE; /* easier to fill in at run time */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Table index for the hex values. Initialized once, first time.
|
|
Packit |
a4058c |
* Used for translation value or delimiter significance lookup.
|
|
Packit |
a4058c |
*/
|
|
Packit |
a4058c |
static void
|
|
Packit |
a4058c |
init_hex_table (void)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
/*
|
|
Packit |
a4058c |
* We build the table at run time for several reasons:
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* 1. portable to non-ASCII machines.
|
|
Packit |
a4058c |
* 2. still reentrant since we set the init flag after setting table.
|
|
Packit |
a4058c |
* 3. easier to extend.
|
|
Packit |
a4058c |
* 4. less prone to bugs.
|
|
Packit |
a4058c |
*/
|
|
Packit |
a4058c |
hex_table['0'] = 0;
|
|
Packit |
a4058c |
hex_table['1'] = 1;
|
|
Packit |
a4058c |
hex_table['2'] = 2;
|
|
Packit |
a4058c |
hex_table['3'] = 3;
|
|
Packit |
a4058c |
hex_table['4'] = 4;
|
|
Packit |
a4058c |
hex_table['5'] = 5;
|
|
Packit |
a4058c |
hex_table['6'] = 6;
|
|
Packit |
a4058c |
hex_table['7'] = 7;
|
|
Packit |
a4058c |
hex_table['8'] = 8;
|
|
Packit |
a4058c |
hex_table['9'] = 9;
|
|
Packit |
a4058c |
hex_table['A'] = 10;
|
|
Packit |
a4058c |
hex_table['B'] = 11;
|
|
Packit |
a4058c |
hex_table['C'] = 12;
|
|
Packit |
a4058c |
hex_table['D'] = 13;
|
|
Packit |
a4058c |
hex_table['E'] = 14;
|
|
Packit |
a4058c |
hex_table['F'] = 15;
|
|
Packit |
a4058c |
hex_table['a'] = 10;
|
|
Packit |
a4058c |
hex_table['b'] = 11;
|
|
Packit |
a4058c |
hex_table['c'] = 12;
|
|
Packit |
a4058c |
hex_table['d'] = 13;
|
|
Packit |
a4058c |
hex_table['e'] = 14;
|
|
Packit |
a4058c |
hex_table['f'] = 15;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* delimiters of significance are flagged w/ negative value */
|
|
Packit |
a4058c |
hex_table[' '] = -1;
|
|
Packit |
a4058c |
hex_table[','] = -1;
|
|
Packit |
a4058c |
hex_table['}'] = -1;
|
|
Packit |
a4058c |
hex_table['\n'] = -1;
|
|
Packit |
a4058c |
hex_table['\t'] = -1;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
initialized = TRUE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Read next hex value in the input stream, return -1 if EOF */
|
|
Packit |
a4058c |
static int
|
|
Packit |
a4058c |
next_int (FILE *fstream)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
int ch;
|
|
Packit |
a4058c |
int value = 0;
|
|
Packit |
a4058c |
int gotone = 0;
|
|
Packit |
a4058c |
int done = 0;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* loop, accumulate hex value until find delimiter
|
|
Packit |
a4058c |
skip any initial delimiters found in read stream */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
while (!done) {
|
|
Packit |
a4058c |
ch = getc (fstream);
|
|
Packit |
a4058c |
if (ch == EOF) {
|
|
Packit |
a4058c |
value = -1;
|
|
Packit |
a4058c |
done++;
|
|
Packit |
a4058c |
} else {
|
|
Packit |
a4058c |
/* trim high bits, check type and accumulate */
|
|
Packit |
a4058c |
ch &= 0xff;
|
|
Packit |
a4058c |
if (g_ascii_isxdigit (ch)) {
|
|
Packit |
a4058c |
value = (value << 4) + g_ascii_xdigit_value (ch);
|
|
Packit |
a4058c |
gotone++;
|
|
Packit |
a4058c |
} else if ((hex_table[ch]) < 0 && gotone) {
|
|
Packit |
a4058c |
done++;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
return value;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
read_bitmap_file_data (FILE *fstream,
|
|
Packit |
a4058c |
guint *width,
|
|
Packit |
a4058c |
guint *height,
|
|
Packit |
a4058c |
guchar **data,
|
|
Packit |
a4058c |
int *x_hot,
|
|
Packit |
a4058c |
int *y_hot)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
guchar *bits = NULL; /* working variable */
|
|
Packit |
a4058c |
char line[MAX_SIZE]; /* input line from file */
|
|
Packit |
a4058c |
guint size; /* number of bytes of data */
|
|
Packit |
a4058c |
char name_and_type[MAX_SIZE]; /* an input line */
|
|
Packit |
a4058c |
char *type; /* for parsing */
|
|
Packit |
a4058c |
int value; /* from an input line */
|
|
Packit |
a4058c |
int version10p; /* boolean, old format */
|
|
Packit |
a4058c |
int padding; /* to handle alignment */
|
|
Packit |
a4058c |
int bytes_per_line; /* per scanline of data */
|
|
Packit |
a4058c |
guint ww = 0; /* width */
|
|
Packit |
a4058c |
guint hh = 0; /* height */
|
|
Packit |
a4058c |
int hx = -1; /* x hotspot */
|
|
Packit |
a4058c |
int hy = -1; /* y hotspot */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* first time initialization */
|
|
Packit |
a4058c |
if (!initialized) {
|
|
Packit |
a4058c |
init_hex_table ();
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* error cleanup and return macro */
|
|
Packit |
a4058c |
#define RETURN(code) { g_free (bits); return code; }
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
while (fgets (line, MAX_SIZE, fstream)) {
|
|
Packit |
a4058c |
if (strlen (line) == MAX_SIZE-1)
|
|
Packit |
a4058c |
RETURN (FALSE);
|
|
Packit |
a4058c |
if (sscanf (line,"#define %s %d",name_and_type,&value) == 2) {
|
|
Packit |
a4058c |
if (!(type = strrchr (name_and_type, '_')))
|
|
Packit |
a4058c |
type = name_and_type;
|
|
Packit |
a4058c |
else {
|
|
Packit |
a4058c |
type++;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!strcmp ("width", type)) {
|
|
Packit |
a4058c |
if (value <= 0)
|
|
Packit |
a4058c |
RETURN (FALSE);
|
|
Packit |
a4058c |
ww = (unsigned int) value;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
if (!strcmp ("height", type)) {
|
|
Packit |
a4058c |
if (value <= 0)
|
|
Packit |
a4058c |
RETURN (FALSE);
|
|
Packit |
a4058c |
hh = (unsigned int) value;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
if (!strcmp ("hot", type)) {
|
|
Packit |
a4058c |
if (type-- == name_and_type
|
|
Packit |
a4058c |
|| type-- == name_and_type)
|
|
Packit |
a4058c |
continue;
|
|
Packit |
a4058c |
if (!strcmp ("x_hot", type))
|
|
Packit |
a4058c |
hx = value;
|
|
Packit |
a4058c |
if (!strcmp ("y_hot", type))
|
|
Packit |
a4058c |
hy = value;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
continue;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (sscanf (line, "static short %s = {", name_and_type) == 1)
|
|
Packit |
a4058c |
version10p = 1;
|
|
Packit |
a4058c |
else if (sscanf (line,"static const unsigned char %s = {",name_and_type) == 1)
|
|
Packit |
a4058c |
version10p = 0;
|
|
Packit |
a4058c |
else if (sscanf (line,"static unsigned char %s = {",name_and_type) == 1)
|
|
Packit |
a4058c |
version10p = 0;
|
|
Packit |
a4058c |
else if (sscanf (line, "static const char %s = {", name_and_type) == 1)
|
|
Packit |
a4058c |
version10p = 0;
|
|
Packit |
a4058c |
else if (sscanf (line, "static char %s = {", name_and_type) == 1)
|
|
Packit |
a4058c |
version10p = 0;
|
|
Packit |
a4058c |
else
|
|
Packit |
a4058c |
continue;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!(type = strrchr (name_and_type, '_')))
|
|
Packit |
a4058c |
type = name_and_type;
|
|
Packit |
a4058c |
else
|
|
Packit |
a4058c |
type++;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (strcmp ("bits[]", type))
|
|
Packit |
a4058c |
continue;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!ww || !hh)
|
|
Packit |
a4058c |
RETURN (FALSE);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Choose @padding so @size is even if @version10p is %TRUE.
|
|
Packit |
a4058c |
* If @version10p is %FALSE, @size could be even or odd. */
|
|
Packit |
a4058c |
if ((ww % 16) && ((ww % 16) < 9) && version10p)
|
|
Packit |
a4058c |
padding = 1;
|
|
Packit |
a4058c |
else
|
|
Packit |
a4058c |
padding = 0;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Check for overflow for the bytes_per_line calculation. */
|
|
Packit |
a4058c |
if (ww > G_MAXUINT - 7)
|
|
Packit |
a4058c |
RETURN (FALSE);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
bytes_per_line = (ww+7)/8 + padding;
|
|
Packit |
a4058c |
g_assert (!version10p || (bytes_per_line % 2) == 0);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* size = bytes_per_line * hh */
|
|
Packit |
a4058c |
if (!g_uint_checked_mul (&size, bytes_per_line, hh))
|
|
Packit |
a4058c |
RETURN (FALSE);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
bits = g_malloc (size);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (version10p) {
|
|
Packit |
a4058c |
unsigned char *ptr;
|
|
Packit |
a4058c |
guint bytes;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* @bytes is guaranteed not to overflow (which could
|
|
Packit |
a4058c |
* happen if @size is the odd-valued %G_MAXUINT: @bytes would reach
|
|
Packit |
a4058c |
* %G_MAXUINT-1 in the loop, then be incremented to %G_MAXUINT+1 on the
|
|
Packit |
a4058c |
* next iteration) because @bytes_per_line is guaranteed to be even if
|
|
Packit |
a4058c |
* @version10p is %TRUE (due to the selection of
|
|
Packit |
a4058c |
* @padding in that case), so @size must be even too. */
|
|
Packit |
a4058c |
g_assert ((size % 2) == 0);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
for (bytes = 0, ptr = bits; bytes < size; (bytes += 2)) {
|
|
Packit |
a4058c |
if ((value = next_int (fstream)) < 0)
|
|
Packit |
a4058c |
RETURN (FALSE);
|
|
Packit |
a4058c |
*(ptr++) = value;
|
|
Packit |
a4058c |
if (!padding || ((bytes+2) % bytes_per_line))
|
|
Packit |
a4058c |
*(ptr++) = value >> 8;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
} else {
|
|
Packit |
a4058c |
unsigned char *ptr;
|
|
Packit |
a4058c |
guint bytes;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
for (bytes = 0, ptr = bits; bytes < size; bytes++, ptr++) {
|
|
Packit |
a4058c |
if ((value = next_int (fstream)) < 0)
|
|
Packit |
a4058c |
RETURN (FALSE);
|
|
Packit |
a4058c |
*ptr=value;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!bits)
|
|
Packit |
a4058c |
RETURN (FALSE);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
*data = bits;
|
|
Packit |
a4058c |
*width = ww;
|
|
Packit |
a4058c |
*height = hh;
|
|
Packit |
a4058c |
if (x_hot)
|
|
Packit |
a4058c |
*x_hot = hx;
|
|
Packit |
a4058c |
if (y_hot)
|
|
Packit |
a4058c |
*y_hot = hy;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return TRUE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static GdkPixbuf *
|
|
Packit |
a4058c |
gdk_pixbuf__xbm_image_load_real (FILE *f,
|
|
Packit |
a4058c |
XBMData *context,
|
|
Packit |
a4058c |
GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
guint w, h;
|
|
Packit |
a4058c |
int x_hot, y_hot;
|
|
Packit |
a4058c |
guchar *data, *ptr;
|
|
Packit |
a4058c |
guchar *pixels;
|
|
Packit |
a4058c |
guint row_stride;
|
|
Packit |
a4058c |
int x, y;
|
|
Packit |
a4058c |
int reg = 0; /* Quiet compiler */
|
|
Packit |
a4058c |
int bits;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
GdkPixbuf *pixbuf;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!read_bitmap_file_data (f, &w, &h, &data, &x_hot, &y_hot)) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
Packit |
a4058c |
_("Invalid XBM file"));
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, w, h);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (pixbuf == NULL) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
|
|
Packit |
a4058c |
_("Insufficient memory to load XBM image file"));
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (x_hot != -1 && y_hot != -1) {
|
|
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 |
pixels = gdk_pixbuf_get_pixels (pixbuf);
|
|
Packit |
a4058c |
row_stride = gdk_pixbuf_get_rowstride (pixbuf);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (context && context->prepare_func)
|
|
Packit |
a4058c |
(* context->prepare_func) (pixbuf, NULL, context->user_data);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Initialize PIXBUF */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
ptr = data;
|
|
Packit |
a4058c |
for (y = 0; y < h; y++) {
|
|
Packit |
a4058c |
bits = 0;
|
|
Packit |
a4058c |
for (x = 0; x < w; x++) {
|
|
Packit |
a4058c |
guchar channel;
|
|
Packit |
a4058c |
if (bits == 0) {
|
|
Packit |
a4058c |
reg = *ptr++;
|
|
Packit |
a4058c |
bits = 8;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
channel = (reg & 1) ? 0 : 255;
|
|
Packit |
a4058c |
reg >>= 1;
|
|
Packit |
a4058c |
bits--;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
pixels[x*3+0] = channel;
|
|
Packit |
a4058c |
pixels[x*3+1] = channel;
|
|
Packit |
a4058c |
pixels[x*3+2] = channel;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
pixels += row_stride;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
g_free (data);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (context) {
|
|
Packit |
a4058c |
if (context->update_func)
|
|
Packit |
a4058c |
(* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return pixbuf;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Static loader */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static GdkPixbuf *
|
|
Packit |
a4058c |
gdk_pixbuf__xbm_image_load (FILE *f,
|
|
Packit |
a4058c |
GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
return gdk_pixbuf__xbm_image_load_real (f, NULL, error);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Progressive loader */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/*
|
|
Packit |
a4058c |
* Proper XBM progressive loading isn't implemented. Instead we write
|
|
Packit |
a4058c |
* it to a file, then load the file when it's done. It's not pretty.
|
|
Packit |
a4058c |
*/
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gpointer
|
|
Packit |
a4058c |
gdk_pixbuf__xbm_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 |
XBMData *context;
|
|
Packit |
a4058c |
gint fd;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
context = g_new (XBMData, 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-xbm-tmp.XXXXXX",
|
|
Packit |
a4058c |
&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__xbm_image_stop_load (gpointer data,
|
|
Packit |
a4058c |
GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
XBMData *context = (XBMData*) data;
|
|
Packit |
a4058c |
gboolean retval = TRUE;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
g_return_val_if_fail (data != NULL, TRUE);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
fflush (context->file);
|
|
Packit |
a4058c |
rewind (context->file);
|
|
Packit |
a4058c |
if (context->all_okay) {
|
|
Packit |
a4058c |
GdkPixbuf *pixbuf;
|
|
Packit |
a4058c |
pixbuf = gdk_pixbuf__xbm_image_load_real (context->file,
|
|
Packit |
a4058c |
context,
|
|
Packit |
a4058c |
error);
|
|
Packit |
a4058c |
if (pixbuf == NULL)
|
|
Packit |
a4058c |
retval = FALSE;
|
|
Packit |
a4058c |
else
|
|
Packit |
a4058c |
g_object_unref (pixbuf);
|
|
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 ((XBMData *) context);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return retval;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
gdk_pixbuf__xbm_image_load_increment (gpointer data,
|
|
Packit |
a4058c |
const guchar *buf,
|
|
Packit |
a4058c |
guint size,
|
|
Packit |
a4058c |
GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
XBMData *context = (XBMData *) 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 XBM image"));
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return TRUE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#ifndef INCLUDE_xbm
|
|
Packit |
a4058c |
#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
|
|
Packit |
a4058c |
#else
|
|
Packit |
a4058c |
#define MODULE_ENTRY(function) void _gdk_pixbuf__xbm_ ## function
|
|
Packit |
a4058c |
#endif
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
module->load = gdk_pixbuf__xbm_image_load;
|
|
Packit |
a4058c |
module->begin_load = gdk_pixbuf__xbm_image_begin_load;
|
|
Packit |
a4058c |
module->stop_load = gdk_pixbuf__xbm_image_stop_load;
|
|
Packit |
a4058c |
module->load_increment = gdk_pixbuf__xbm_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 |
{ "#define ", NULL, 100 },
|
|
Packit |
a4058c |
{ "/*", NULL, 50 },
|
|
Packit |
a4058c |
{ NULL, NULL, 0 }
|
|
Packit |
a4058c |
};
|
|
Packit |
a4058c |
static const gchar *mime_types[] = {
|
|
Packit |
a4058c |
"image/x-xbitmap",
|
|
Packit |
a4058c |
NULL
|
|
Packit |
a4058c |
};
|
|
Packit |
a4058c |
static const gchar *extensions[] = {
|
|
Packit |
a4058c |
"xbm",
|
|
Packit |
a4058c |
NULL
|
|
Packit |
a4058c |
};
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
info->name = "xbm";
|
|
Packit |
a4058c |
info->signature = (GdkPixbufModulePattern *) signature;
|
|
Packit |
a4058c |
info->description = NC_("image format", "XBM");
|
|
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 |
}
|