1 /* -*- mode: C; c-file-style: "linux" -*- */
2 /* GdkPixbuf library - XBM image loader
4 * Copyright (C) 1999 Mark Crichton
5 * Copyright (C) 1999 The Free Software Foundation
6 * Copyright (C) 2001 Eazel, Inc.
8 * Authors: Mark Crichton <crichton@gimp.org>
9 * Federico Mena-Quintero <federico@gimp.org>
10 * Jonathan Blandford <jrb@redhat.com>
11 * John Harper <jsh@eazel.com>
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Library General Public License for more details.
23 * You should have received a copy of the GNU Library General Public
24 * License along with this library; if not, write to the
25 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 * Boston, MA 02111-1307, USA.
29 /* Following code adapted from io-tiff.c, which was ``(almost) blatantly
30 ripped from Imlib'' */
40 #include "gdk-pixbuf-private.h"
41 #include "gdk-pixbuf-io.h"
45 typedef struct _XBMData XBMData;
48 GdkPixbufModulePreparedFunc prepare_func;
49 GdkPixbufModuleUpdatedFunc update_func;
58 /* xbm parser borrowed from xc/lib/X11/RdBitF.c */
62 /* shared data for the image read/parse logic */
63 static short hex_table[256]; /* conversion value */
64 static gboolean initialized = FALSE; /* easier to fill in at run time */
67 /* Table index for the hex values. Initialized once, first time.
68 * Used for translation value or delimiter significance lookup.
74 * We build the table at run time for several reasons:
76 * 1. portable to non-ASCII machines.
77 * 2. still reentrant since we set the init flag after setting table.
78 * 3. easier to extend.
79 * 4. less prone to bugs.
104 /* delimiters of significance are flagged w/ negative value */
108 hex_table['\n'] = -1;
109 hex_table['\t'] = -1;
114 /* Read next hex value in the input stream, return -1 if EOF */
116 next_int (FILE *fstream)
123 /* loop, accumulate hex value until find delimiter
124 skip any initial delimiters found in read stream */
132 /* trim high bits, check type and accumulate */
134 if (g_ascii_isxdigit (ch)) {
135 value = (value << 4) + g_ascii_xdigit_value (ch);
137 } else if ((hex_table[ch]) < 0 && gotone) {
146 read_bitmap_file_data (FILE *fstream,
147 guint *width, guint *height,
149 int *x_hot, int *y_hot)
151 guchar *bits = NULL; /* working variable */
152 char line[MAX_SIZE]; /* input line from file */
153 int size; /* number of bytes of data */
154 char name_and_type[MAX_SIZE]; /* an input line */
155 char *type; /* for parsing */
156 int value; /* from an input line */
157 int version10p; /* boolean, old format */
158 int padding; /* to handle alignment */
159 int bytes_per_line; /* per scanline of data */
160 guint ww = 0; /* width */
161 guint hh = 0; /* height */
162 int hx = -1; /* x hotspot */
163 int hy = -1; /* y hotspot */
165 /* first time initialization */
170 /* error cleanup and return macro */
171 #define RETURN(code) { g_free (bits); return code; }
173 while (fgets (line, MAX_SIZE, fstream)) {
174 if (strlen (line) == MAX_SIZE-1)
176 if (sscanf (line,"#define %s %d",name_and_type,&value) == 2) {
177 if (!(type = strrchr (name_and_type, '_')))
178 type = name_and_type;
183 if (!strcmp ("width", type))
184 ww = (unsigned int) value;
185 if (!strcmp ("height", type))
186 hh = (unsigned int) value;
187 if (!strcmp ("hot", type)) {
188 if (type-- == name_and_type
189 || type-- == name_and_type)
191 if (!strcmp ("x_hot", type))
193 if (!strcmp ("y_hot", type))
199 if (sscanf (line, "static short %s = {", name_and_type) == 1)
201 else if (sscanf (line,"static unsigned char %s = {",name_and_type) == 1)
203 else if (sscanf (line, "static char %s = {", name_and_type) == 1)
208 if (!(type = strrchr (name_and_type, '_')))
209 type = name_and_type;
213 if (strcmp ("bits[]", type))
219 if ((ww % 16) && ((ww % 16) < 9) && version10p)
224 bytes_per_line = (ww+7)/8 + padding;
226 size = bytes_per_line * hh;
227 bits = g_malloc (size);
233 for (bytes = 0, ptr = bits; bytes < size; (bytes += 2)) {
234 if ((value = next_int (fstream)) < 0)
237 if (!padding || ((bytes+2) % bytes_per_line))
238 *(ptr++) = value >> 8;
244 for (bytes = 0, ptr = bits; bytes < size; bytes++, ptr++) {
245 if ((value = next_int (fstream)) < 0)
270 gdk_pixbuf__xbm_image_load_real (FILE *f, XBMData *context, GError **error)
278 int reg = 0; /* Quiet compiler */
283 if (!read_bitmap_file_data (f, &w, &h, &data, &x_hot, &y_hot)) {
286 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
287 _("Invalid XBM file"));
291 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, w, h);
293 if (pixbuf == NULL) {
296 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
297 _("Insufficient memory to load XBM image file"));
301 if (x_hot != -1 && y_hot != -1) {
303 g_snprintf (hot, 10, "%d", x_hot);
304 gdk_pixbuf_set_option (pixbuf, "x_hot", hot);
305 g_snprintf (hot, 10, "%d", y_hot);
306 gdk_pixbuf_set_option (pixbuf, "y_hot", hot);
309 pixels = gdk_pixbuf_get_pixels (pixbuf);
310 row_stride = gdk_pixbuf_get_rowstride (pixbuf);
313 (* context->prepare_func) (pixbuf, NULL, context->user_data);
316 /* Initialize PIXBUF */
319 for (y = 0; y < h; y++) {
321 for (x = 0; x < w; x++) {
328 channel = (reg & 1) ? 0 : 255;
332 pixels[x*3+0] = channel;
333 pixels[x*3+1] = channel;
334 pixels[x*3+2] = channel;
336 pixels += row_stride;
341 (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
342 g_object_unref (pixbuf);
353 gdk_pixbuf__xbm_image_load (FILE *f, GError **error)
355 return gdk_pixbuf__xbm_image_load_real (f, NULL, error);
359 /* Progressive loader */
362 * Proper XBM progressive loading isn't implemented. Instead we write
363 * it to a file, then load the file when it's done. It's not pretty.
367 gdk_pixbuf__xbm_image_begin_load (GdkPixbufModuleSizeFunc size_func,
368 GdkPixbufModulePreparedFunc prepare_func,
369 GdkPixbufModuleUpdatedFunc update_func,
376 context = g_new (XBMData, 1);
377 context->prepare_func = prepare_func;
378 context->update_func = update_func;
379 context->user_data = user_data;
380 context->all_okay = TRUE;
381 fd = g_file_open_tmp ("gdkpixbuf-xbm-tmp.XXXXXX",
389 context->file = fdopen (fd, "w+");
390 if (context->file == NULL) {
391 g_free (context->tempname);
400 gdk_pixbuf__xbm_image_stop_load (gpointer data,
403 XBMData *context = (XBMData*) data;
404 gboolean retval = TRUE;
406 g_return_val_if_fail (data != NULL, TRUE);
408 fflush (context->file);
409 rewind (context->file);
410 if (context->all_okay) {
412 pixbuf = gdk_pixbuf__xbm_image_load_real (context->file, context,
418 fclose (context->file);
419 unlink (context->tempname);
420 g_free (context->tempname);
421 g_free ((XBMData *) context);
427 gdk_pixbuf__xbm_image_load_increment (gpointer data,
432 XBMData *context = (XBMData *) data;
434 g_return_val_if_fail (data != NULL, FALSE);
436 if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
437 context->all_okay = FALSE;
440 g_file_error_from_errno (errno),
441 _("Failed to write to temporary file when loading XBM image"));
449 MODULE_ENTRY (xbm, fill_vtable) (GdkPixbufModule *module)
451 module->load = gdk_pixbuf__xbm_image_load;
452 module->begin_load = gdk_pixbuf__xbm_image_begin_load;
453 module->stop_load = gdk_pixbuf__xbm_image_stop_load;
454 module->load_increment = gdk_pixbuf__xbm_image_load_increment;
458 MODULE_ENTRY (xbm, fill_info) (GdkPixbufFormat *info)
460 static GdkPixbufModulePattern signature[] = {
461 { "#define ", NULL, 100 },
465 static gchar * mime_types[] = {
469 static gchar * extensions[] = {
475 info->signature = signature;
476 info->description = N_("The XBM image format");
477 info->mime_types = mime_types;
478 info->extensions = extensions;
480 info->license = "LGPL";