1999-01-04 23:53:12 +00:00
|
|
|
/*
|
1999-10-18 19:29:45 +00:00
|
|
|
* gdk-pixbuf-io.c: Code to load images into GdkPixbufs
|
1999-01-04 23:53:12 +00:00
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Miguel de Icaza (miguel@gnu.org)
|
|
|
|
*/
|
1999-08-09 06:09:24 +00:00
|
|
|
|
1999-01-04 23:53:12 +00:00
|
|
|
#include <config.h>
|
1999-01-05 04:31:03 +00:00
|
|
|
#include <stdio.h>
|
1999-08-09 06:09:24 +00:00
|
|
|
#include <string.h>
|
1999-07-08 16:04:16 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <gmodule.h>
|
1999-01-04 23:53:12 +00:00
|
|
|
#include "gdk-pixbuf.h"
|
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
static gboolean
|
|
|
|
pixbuf_check_png (unsigned char *buffer, int size)
|
|
|
|
{
|
|
|
|
if (size < 28)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (buffer [0] != 0x89 ||
|
|
|
|
buffer [1] != 'P' ||
|
|
|
|
buffer [2] != 'N' ||
|
|
|
|
buffer [3] != 'G' ||
|
|
|
|
buffer [4] != 0x0d ||
|
|
|
|
buffer [5] != 0x0a ||
|
|
|
|
buffer [6] != 0x1a ||
|
|
|
|
buffer [7] != 0x0a)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
pixbuf_check_jpeg (unsigned char *buffer, int size)
|
|
|
|
{
|
|
|
|
if (size < 10)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (buffer [0] != 0xff || buffer [1] != 0xd8)
|
|
|
|
return FALSE;
|
1999-08-09 06:09:24 +00:00
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
pixbuf_check_tiff (unsigned char *buffer, int size)
|
|
|
|
{
|
|
|
|
if (size < 10)
|
|
|
|
return FALSE;
|
|
|
|
|
1999-08-09 06:09:24 +00:00
|
|
|
if (buffer [0] == 'M' &&
|
|
|
|
buffer [1] == 'M' &&
|
|
|
|
buffer [2] == 0 &&
|
1999-07-19 02:13:34 +00:00
|
|
|
buffer [3] == 0x2a)
|
1999-01-05 04:31:03 +00:00
|
|
|
return TRUE;
|
|
|
|
|
1999-08-09 06:09:24 +00:00
|
|
|
if (buffer [0] == 'I' &&
|
|
|
|
buffer [1] == 'I' &&
|
|
|
|
buffer [2] == 0x2a &&
|
1999-07-19 02:13:34 +00:00
|
|
|
buffer [3] == 0)
|
1999-01-05 04:31:03 +00:00
|
|
|
return TRUE;
|
1999-08-09 06:09:24 +00:00
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
pixbuf_check_gif (unsigned char *buffer, int size)
|
|
|
|
{
|
|
|
|
if (size < 20)
|
|
|
|
return FALSE;
|
1999-08-09 06:09:24 +00:00
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
if (strncmp (buffer, "GIF8", 4) == 0)
|
|
|
|
return TRUE;
|
1999-08-09 06:09:24 +00:00
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
pixbuf_check_xpm (unsigned char *buffer, int size)
|
|
|
|
{
|
|
|
|
if (size < 20)
|
|
|
|
return FALSE;
|
1999-08-09 06:09:24 +00:00
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
if (strncmp (buffer, "/* XPM */", 9) == 0)
|
|
|
|
return TRUE;
|
1999-08-09 06:09:24 +00:00
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
pixbuf_check_bmp (unsigned char *buffer, int size)
|
|
|
|
{
|
|
|
|
if (size < 20)
|
|
|
|
return FALSE;
|
1999-08-09 06:09:24 +00:00
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
if (buffer [0] != 'B' || buffer [1] != 'M')
|
|
|
|
return FALSE;
|
1999-08-09 06:09:24 +00:00
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
pixbuf_check_ppm (unsigned char *buffer, int size)
|
|
|
|
{
|
|
|
|
if (size < 20)
|
|
|
|
return FALSE;
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
if (buffer [0] == 'P') {
|
1999-01-05 04:31:03 +00:00
|
|
|
if (buffer [1] == '1' ||
|
|
|
|
buffer [1] == '2' ||
|
|
|
|
buffer [1] == '3' ||
|
|
|
|
buffer [1] == '4' ||
|
|
|
|
buffer [1] == '5' ||
|
|
|
|
buffer [1] == '6')
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
1999-01-04 23:53:12 +00:00
|
|
|
static struct {
|
|
|
|
char *module_name;
|
1999-01-05 04:31:03 +00:00
|
|
|
gboolean (*format_check)(unsigned char *buffer, int size);
|
|
|
|
GModule *module;
|
1999-10-18 19:29:45 +00:00
|
|
|
GdkPixbuf *(*load)(FILE *f);
|
|
|
|
int (*save)(GdkPixbuf *p, FILE *f, ...);
|
1999-01-05 04:31:03 +00:00
|
|
|
} file_formats [] = {
|
|
|
|
{ "png", pixbuf_check_png, NULL, NULL, NULL },
|
|
|
|
{ "jpeg", pixbuf_check_jpeg, NULL, NULL, NULL },
|
|
|
|
{ "tiff", pixbuf_check_tiff, NULL, NULL, NULL },
|
|
|
|
{ "gif", pixbuf_check_gif, NULL, NULL, NULL },
|
1999-07-08 16:04:16 +00:00
|
|
|
{ "xpm", pixbuf_check_xpm, NULL, NULL, NULL },
|
|
|
|
/* { "bmp", pixbuf_check_bmp, NULL, NULL, NULL },
|
|
|
|
{ "ppm", pixbuf_check_ppm, NULL, NULL, NULL },*/
|
|
|
|
{ NULL, NULL, NULL, NULL, NULL }
|
1999-01-04 23:53:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
1999-01-05 04:31:03 +00:00
|
|
|
image_handler_load (int idx)
|
1999-01-04 23:53:12 +00:00
|
|
|
{
|
1999-07-19 02:13:34 +00:00
|
|
|
char *module_name;
|
1999-01-05 04:31:03 +00:00
|
|
|
char *path;
|
|
|
|
GModule *module;
|
|
|
|
void *load_sym, *save_sym;
|
1999-08-09 06:09:24 +00:00
|
|
|
|
|
|
|
module_name = g_strconcat ("pixbuf-",
|
1999-07-19 02:13:34 +00:00
|
|
|
file_formats [idx].module_name, NULL);
|
1999-01-05 04:31:03 +00:00
|
|
|
path = g_module_build_path (PIXBUF_LIBDIR, module_name);
|
|
|
|
g_free (module_name);
|
|
|
|
|
|
|
|
module = g_module_open (path, G_MODULE_BIND_LAZY);
|
1999-07-17 20:03:34 +00:00
|
|
|
if (!module) {
|
|
|
|
g_warning ("Unable to load module: %s", path);
|
1999-01-05 04:31:03 +00:00
|
|
|
return;
|
1999-07-17 20:03:34 +00:00
|
|
|
}
|
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
file_formats [idx].module = module;
|
|
|
|
|
|
|
|
if (g_module_symbol (module, "image_load", &load_sym))
|
|
|
|
file_formats [idx].load = load_sym;
|
|
|
|
|
|
|
|
if (g_module_symbol (module, "image_save", &save_sym))
|
|
|
|
file_formats [idx].save = save_sym;
|
1999-01-04 23:53:12 +00:00
|
|
|
}
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
GdkPixbuf *
|
1999-01-04 23:53:12 +00:00
|
|
|
gdk_pixbuf_load_image (const char *file)
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
GdkPixbuf *pixbuf;
|
1999-07-08 16:04:16 +00:00
|
|
|
gint n, i;
|
1999-01-04 23:53:12 +00:00
|
|
|
FILE *f;
|
|
|
|
char buffer [128];
|
|
|
|
|
1999-07-08 16:04:16 +00:00
|
|
|
f = fopen (file, "r");
|
1999-01-04 23:53:12 +00:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
n = fread (&buffer, 1, sizeof (buffer), f);
|
1999-01-05 04:31:03 +00:00
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
if (n == 0) {
|
1999-08-09 06:09:24 +00:00
|
|
|
fclose (f);
|
1999-01-04 23:53:12 +00:00
|
|
|
return NULL;
|
1999-01-05 04:31:03 +00:00
|
|
|
}
|
1999-01-04 23:53:12 +00:00
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
for (i = 0; file_formats [i].module_name; i++) {
|
|
|
|
if ((*file_formats [i].format_check)(buffer, n)) {
|
1999-01-05 04:31:03 +00:00
|
|
|
if (!file_formats [i].load)
|
|
|
|
image_handler_load (i);
|
1999-01-04 23:53:12 +00:00
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
if (!file_formats [i].load) {
|
1999-01-05 04:31:03 +00:00
|
|
|
fclose (f);
|
1999-01-04 23:53:12 +00:00
|
|
|
return NULL;
|
1999-01-05 04:31:03 +00:00
|
|
|
}
|
1999-01-04 23:53:12 +00:00
|
|
|
|
1999-07-08 16:04:16 +00:00
|
|
|
fseek(f, 0, SEEK_SET);
|
1999-01-05 04:31:03 +00:00
|
|
|
pixbuf = (*file_formats [i].load)(f);
|
|
|
|
fclose (f);
|
1999-09-17 07:57:25 +00:00
|
|
|
g_assert (pixbuf->ref_count != 0);
|
1999-01-05 04:31:03 +00:00
|
|
|
return pixbuf;
|
1999-01-04 23:53:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
fclose (f);
|
1999-07-17 20:03:34 +00:00
|
|
|
g_warning ("Unable to find handler for file: %s", file);
|
1999-01-04 23:53:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|