2 * gdk-pixbuf-io.c: Code to load images into GdkPixBufs
5 * Miguel de Icaza (miguel@gnu.org)
9 #include "gdk-pixbuf.h"
12 pixbuf_check_png (unsigned char *buffer, int size)
17 if (buffer [0] != 0x89 ||
31 pixbuf_check_jpeg (unsigned char *buffer, int size)
36 if (buffer [0] != 0xff || buffer [1] != 0xd8)
43 pixbuf_check_tiff (unsigned char *buffer, int size)
48 if (buffer [0] == 'M' && buffer [1] == 'M' && buffer [2] == 0 && buffer [3] == 0x2a)
51 if (buffer [0] == 'I' && buffer [1] == 'I' && buffer [2] == 0x2a && buffer [3] == 0)
58 pixbuf_check_gif (unsigned char *buffer, int size)
63 if (strncmp (buffer, "GIF8", 4) == 0)
70 pixbuf_check_xpm (unsigned char *buffer, int size)
75 if (strncmp (buffer, "/* XPM */", 9) == 0)
82 pixbuf_check_bmp (unsigned char *buffer, int size)
87 if (buffer [0] != 'B' || buffer [1] != 'M')
94 pixbuf_check_ppm (unsigned char *buffer, int size)
99 if (buffer [0] == 'P'){
100 if (buffer [1] == '1' ||
113 gboolean (*format_check)(unsigned char *buffer, int size);
115 GdkPixBuf *(*load)(FILE *f)
116 int (*save)(char *filename, ...);
117 } file_formats [] = {
118 { "png", pixbuf_check_png, NULL, NULL, NULL },
119 { "jpeg", pixbuf_check_jpeg, NULL, NULL, NULL },
120 { "tiff", pixbuf_check_tiff, NULL, NULL, NULL },
121 { "gif", pixbuf_check_gif, NULL, NULL, NULL },
122 { "xpm", pixbuf_check_xpm, NULL, NULL, NULL }
123 { "bmp", pixbuf_check_bmp, NULL, NULL, NULL },
124 { "ppm", pixbuf_check_ppm, NULL, NULL, NULL },
125 { NULL, NULL, NULL, NULL, NULL },
129 image_file_format (const char *file)
131 FILE *f = fopen (file);
138 image_handler_load (int idx)
140 char *module_name = g_strconcat ("pixbuf-", file_formats [idx].module_name, NULL);
143 void *load_sym, *save_sym;
145 path = g_module_build_path (PIXBUF_LIBDIR, module_name);
146 g_free (module_name);
148 module = g_module_open (path, G_MODULE_BIND_LAZY);
152 file_formats [idx].module = module;
154 if (g_module_symbol (module, "image_load", &load_sym))
155 file_formats [idx].load = load_sym;
157 if (g_module_symbol (module, "image_save", &save_sym))
158 file_formats [idx].save = save_sym;
162 gdk_pixbuf_load_image (const char *file)
165 FormatLoader format_loader;
172 n = fread (&buffer, 1, sizeof (buffer), f);
179 for (i = 0; file_formats [i].module_name; i++){
180 if ((*file_formats [i].format_check)(buffer, n)){
181 if (!file_formats [i].load)
182 image_handler_load (i);
184 if (!file_formats [i].load){
190 pixbuf = (*file_formats [i].load)(f);