diff options
author | Arturo Espinosa <unammx@src.gnome.org> | 1999-01-05 04:31:03 +0000 |
---|---|---|
committer | Arturo Espinosa <unammx@src.gnome.org> | 1999-01-05 04:31:03 +0000 |
commit | f876ca2681065a625a9e314e5f71732c6f7c4f5b (patch) | |
tree | ac06361733730b09f25cdf1664ea22cfc8480e07 /gdk-pixbuf/gdk-pixbuf-io.c | |
parent | f0723b00a98c0294f1a7fbe369e63cb2fc0146e9 (diff) | |
download | gtk+-f876ca2681065a625a9e314e5f71732c6f7c4f5b.tar.gz |
Loading framework done, start of PNG loader
Diffstat (limited to 'gdk-pixbuf/gdk-pixbuf-io.c')
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf-io.c | 170 |
1 files changed, 150 insertions, 20 deletions
diff --git a/gdk-pixbuf/gdk-pixbuf-io.c b/gdk-pixbuf/gdk-pixbuf-io.c index 61786ded23..03422587ba 100644 --- a/gdk-pixbuf/gdk-pixbuf-io.c +++ b/gdk-pixbuf/gdk-pixbuf-io.c @@ -5,22 +5,124 @@ * Miguel de Icaza (miguel@gnu.org) */ #include <config.h> +#include <stdio.h> #include "gdk-pixbuf.h" +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; + + return TRUE; +} + +static gboolean +pixbuf_check_tiff (unsigned char *buffer, int size) +{ + if (size < 10) + return FALSE; + + if (buffer [0] == 'M' && buffer [1] == 'M' && buffer [2] == 0 && buffer [3] == 0x2a) + return TRUE; + + if (buffer [0] == 'I' && buffer [1] == 'I' && buffer [2] == 0x2a && buffer [3] == 0) + return TRUE; + + return FALSE; +} + +static gboolean +pixbuf_check_gif (unsigned char *buffer, int size) +{ + if (size < 20) + return FALSE; + + if (strncmp (buffer, "GIF8", 4) == 0) + return TRUE; + + return FALSE; +} + +static gboolean +pixbuf_check_xpm (unsigned char *buffer, int size) +{ + if (size < 20) + return FALSE; + + if (strncmp (buffer, "/* XPM */", 9) == 0) + return TRUE; + + return FALSE; +} + +static gboolean +pixbuf_check_bmp (unsigned char *buffer, int size) +{ + if (size < 20) + return FALSE; + + if (buffer [0] != 'B' || buffer [1] != 'M') + return FALSE; + + return TRUE; +} + +static gboolean +pixbuf_check_ppm (unsigned char *buffer, int size) +{ + if (size < 20) + return FALSE; + + if (buffer [0] == 'P'){ + if (buffer [1] == '1' || + buffer [1] == '2' || + buffer [1] == '3' || + buffer [1] == '4' || + buffer [1] == '5' || + buffer [1] == '6') + return TRUE; + } + return FALSE; +} + static struct { char *module_name; - gboolean (*format_check)(char *buffer, int size); - GdkPixBuf *(*load)(char *filename); + gboolean (*format_check)(unsigned char *buffer, int size); + GModule *module; + GdkPixBuf *(*load)(FILE *f) int (*save)(char *filename, ...); -} loaders [] = { - { "png", pixbuf_check_png, NULL, NULL }, - { "jpeg", pixbuf_check_jpeg, NULL, NULL }, - { "tiff", pixbuf_check_tiff, NULL, NULL }, - { "gif", pixbuf_check_gif, NULL, NULL }, - { "xpm", pixbuf_check_xpm, pixbuf_xpm_load, pixbuf_xpm_save }, - { "bmp", pixbuf_check_bmp, NULL, NULL }, - { "ppm", pixbuf_check_ppm, NULL, NULL }, - { NULL, NULL, NULL, NULL }, +} 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 }, + { "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 }, }; static int @@ -33,8 +135,27 @@ image_file_format (const char *file) } static void -image_loader_load (int idx) +image_handler_load (int idx) { + char *module_name = g_strconcat ("pixbuf-", file_formats [idx].module_name, NULL); + char *path; + GModule *module; + void *load_sym, *save_sym; + + path = g_module_build_path (PIXBUF_LIBDIR, module_name); + g_free (module_name); + + module = g_module_open (path, G_MODULE_BIND_LAZY); + if (!module) + return; + + 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; } GdkPixBuf * @@ -49,21 +170,30 @@ gdk_pixbuf_load_image (const char *file) if (!f) return NULL; n = fread (&buffer, 1, sizeof (buffer), f); - fclose (f); - if (n == 0) + + if (n == 0){ + fclose (f); return NULL; + } - for (i = 0; loaders [i].module_name; i++){ - if ((*loaders [i].format_check)(buffer, n)){ - if (!loaders [i].load) - image_loader_load (i); + for (i = 0; file_formats [i].module_name; i++){ + if ((*file_formats [i].format_check)(buffer, n)){ + if (!file_formats [i].load) + image_handler_load (i); - if (!loaders [i].load) + if (!file_formats [i].load){ + fclose (f); return NULL; + } - return (*loaders [i].load)(file); + rewind (f); + pixbuf = (*file_formats [i].load)(f); + fclose (f); + return pixbuf; } } + fclose (f); return NULL; } + |