2 * gdk-pixbuf-io.c: Code to load images into GdkPixBufs
5 * Miguel de Icaza (miguel@gnu.org)
11 #include "gdk-pixbuf.h"
13 #define PIXBUF_LIBDIR "."
16 pixbuf_check_png (unsigned char *buffer, int size)
21 if (buffer [0] != 0x89 ||
35 pixbuf_check_jpeg (unsigned char *buffer, int size)
40 if (buffer [0] != 0xff || buffer [1] != 0xd8)
47 pixbuf_check_tiff (unsigned char *buffer, int size)
52 if (buffer [0] == 'M' && buffer [1] == 'M' && buffer [2] == 0 && buffer [3] == 0x2a)
55 if (buffer [0] == 'I' && buffer [1] == 'I' && buffer [2] == 0x2a && buffer [3] == 0)
62 pixbuf_check_gif (unsigned char *buffer, int size)
67 if (strncmp (buffer, "GIF8", 4) == 0)
74 pixbuf_check_xpm (unsigned char *buffer, int size)
79 if (strncmp (buffer, "/* XPM */", 9) == 0)
86 pixbuf_check_bmp (unsigned char *buffer, int size)
91 if (buffer [0] != 'B' || buffer [1] != 'M')
98 pixbuf_check_ppm (unsigned char *buffer, int size)
103 if (buffer [0] == 'P'){
104 if (buffer [1] == '1' ||
117 gboolean (*format_check)(unsigned char *buffer, int size);
119 GdkPixBuf *(*load)(FILE *f);
120 int (*save)(char *filename, ...);
121 } file_formats [] = {
122 { "png", pixbuf_check_png, NULL, NULL, NULL },
123 { "jpeg", pixbuf_check_jpeg, NULL, NULL, NULL },
124 { "tiff", pixbuf_check_tiff, NULL, NULL, NULL },
125 { "gif", pixbuf_check_gif, NULL, NULL, NULL },
126 { "xpm", pixbuf_check_xpm, NULL, NULL, NULL },
127 /* { "bmp", pixbuf_check_bmp, NULL, NULL, NULL },
128 { "ppm", pixbuf_check_ppm, NULL, NULL, NULL },*/
129 { NULL, NULL, NULL, NULL, NULL }
133 image_file_format (const char *file)
135 FILE *f = fopen (file, "r");
142 image_handler_load (int idx)
144 char *module_name = g_strconcat ("pixbuf-", file_formats [idx].module_name, NULL);
147 void *load_sym, *save_sym;
149 path = g_module_build_path (PIXBUF_LIBDIR, module_name);
150 g_free (module_name);
152 module = g_module_open (path, G_MODULE_BIND_LAZY);
156 file_formats [idx].module = module;
158 if (g_module_symbol (module, "image_load", &load_sym))
159 file_formats [idx].load = load_sym;
161 if (g_module_symbol (module, "image_save", &save_sym))
162 file_formats [idx].save = save_sym;
166 gdk_pixbuf_load_image (const char *file)
173 f = fopen (file, "r");
176 n = fread (&buffer, 1, sizeof (buffer), f);
183 for (i = 0; file_formats [i].module_name; i++){
184 if ((*file_formats [i].format_check)(buffer, n)){
185 if (!file_formats [i].load)
186 image_handler_load (i);
188 if (!file_formats [i].load){
193 fseek(f, 0, SEEK_SET);
194 pixbuf = (*file_formats [i].load)(f);