1999-10-20 21:20:49 +00:00
|
|
|
|
/* GdkPixbuf library - Main loading interface.
|
1999-01-04 23:53:12 +00:00
|
|
|
|
*
|
1999-10-20 21:20:49 +00:00
|
|
|
|
* Copyright (C) 1999 The Free Software Foundation
|
|
|
|
|
*
|
|
|
|
|
* Authors: Miguel de Icaza <miguel@gnu.org>
|
|
|
|
|
* Federico Mena-Quintero <federico@gimp.org>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
|
* Boston, MA 02111-1307, USA.
|
1999-01-04 23:53:12 +00:00
|
|
|
|
*/
|
1999-08-09 06:09:24 +00:00
|
|
|
|
|
1999-01-04 23:53:12 +00:00
|
|
|
|
#include <config.h>
|
1999-08-09 06:09:24 +00:00
|
|
|
|
#include <string.h>
|
1999-10-26 20:43:39 +00:00
|
|
|
|
#include "gdk-pixbuf-io.h"
|
1999-01-04 23:53:12 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
|
static gboolean
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixbuf_check_png (guchar *buffer, int size)
|
1999-01-05 04:31:03 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixbuf_check_jpeg (guchar *buffer, int size)
|
1999-01-05 04:31:03 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixbuf_check_tiff (guchar *buffer, int size)
|
1999-01-05 04:31:03 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixbuf_check_gif (guchar *buffer, int size)
|
1999-01-05 04:31:03 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixbuf_check_xpm (guchar *buffer, int size)
|
1999-01-05 04:31:03 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-26 20:43:39 +00:00
|
|
|
|
#if 0
|
1999-01-05 04:31:03 +00:00
|
|
|
|
static gboolean
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixbuf_check_bmp (guchar *buffer, int size)
|
1999-01-05 04:31:03 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixbuf_check_ppm (guchar *buffer, int size)
|
1999-01-05 04:31:03 +00:00
|
|
|
|
{
|
|
|
|
|
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-10-26 20:43:39 +00:00
|
|
|
|
#endif
|
1999-01-05 04:31:03 +00:00
|
|
|
|
|
1999-10-26 20:43:39 +00:00
|
|
|
|
ModuleType file_formats [] = {
|
1999-10-22 21:05:16 +00:00
|
|
|
|
{ "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 },
|
|
|
|
|
#define XPM_FILE_FORMAT_INDEX 4
|
|
|
|
|
{ "xpm", pixbuf_check_xpm, NULL, NULL, NULL },
|
1999-10-20 21:20:49 +00:00
|
|
|
|
#if 0
|
1999-10-22 21:05:16 +00:00
|
|
|
|
{ "bmp", pixbuf_check_bmp, NULL, NULL, NULL },
|
|
|
|
|
{ "ppm", pixbuf_check_ppm, NULL, NULL, NULL },
|
1999-10-20 21:20:49 +00:00
|
|
|
|
#endif
|
|
|
|
|
{ NULL, NULL, NULL, NULL }
|
1999-01-04 23:53:12 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
1999-10-26 20:43:39 +00:00
|
|
|
|
image_handler_load (ModuleType *image_module)
|
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;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
void *load_sym;
|
1999-08-09 06:09:24 +00:00
|
|
|
|
|
1999-10-26 20:43:39 +00:00
|
|
|
|
g_return_if_fail(image_module->module == NULL);
|
|
|
|
|
|
|
|
|
|
module_name = g_strconcat ("pixbuf-", image_module->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-10-21 00:37:21 +00:00
|
|
|
|
g_free (path);
|
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-10-26 20:43:39 +00:00
|
|
|
|
image_module->module = module;
|
1999-01-05 04:31:03 +00:00
|
|
|
|
|
|
|
|
|
if (g_module_symbol (module, "image_load", &load_sym))
|
1999-10-26 20:43:39 +00:00
|
|
|
|
image_module->load = load_sym;
|
1999-10-22 21:05:16 +00:00
|
|
|
|
|
|
|
|
|
if (g_module_symbol (module, "image_load_xpm_data", &load_sym))
|
1999-10-26 20:43:39 +00:00
|
|
|
|
image_module->load_xpm_data = load_sym;
|
1999-01-04 23:53:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
|
|
|
|
|
1999-10-26 20:43:39 +00:00
|
|
|
|
ModuleType *
|
|
|
|
|
gdk_pixbuf_get_module (gchar *buffer, gint size)
|
|
|
|
|
{
|
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; file_formats [i].module_name; i++) {
|
|
|
|
|
if ((* file_formats [i].format_check) (buffer, size))
|
|
|
|
|
return &(file_formats[i]);
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
GdkPixbuf *
|
1999-10-20 21:20:49 +00:00
|
|
|
|
gdk_pixbuf_new_from_file (const char *filename)
|
1999-01-04 23:53:12 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
GdkPixbuf *pixbuf;
|
1999-10-26 20:43:39 +00:00
|
|
|
|
gint size;
|
1999-01-04 23:53:12 +00:00
|
|
|
|
FILE *f;
|
|
|
|
|
char buffer [128];
|
1999-10-26 20:43:39 +00:00
|
|
|
|
ModuleType *image_module;
|
1999-01-04 23:53:12 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
f = fopen (filename, "r");
|
1999-01-04 23:53:12 +00:00
|
|
|
|
if (!f)
|
|
|
|
|
return NULL;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-26 20:43:39 +00:00
|
|
|
|
size = fread (&buffer, 1, sizeof (buffer), f);
|
1999-01-05 04:31:03 +00:00
|
|
|
|
|
1999-10-26 20:43:39 +00:00
|
|
|
|
if (size == 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-26 20:43:39 +00:00
|
|
|
|
image_module = gdk_pixbuf_get_module (buffer, size);
|
|
|
|
|
if (image_module){
|
|
|
|
|
if (!image_module->load)
|
|
|
|
|
image_handler_load (image_module);
|
1999-01-04 23:53:12 +00:00
|
|
|
|
|
1999-10-26 20:43:39 +00:00
|
|
|
|
if (!image_module->load) {
|
1999-01-05 04:31:03 +00:00
|
|
|
|
fclose (f);
|
1999-10-26 20:43:39 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-26 20:43:39 +00:00
|
|
|
|
fseek (f, 0, SEEK_SET);
|
|
|
|
|
pixbuf = (* image_module->load) (f);
|
|
|
|
|
fclose (f);
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-26 20:43:39 +00:00
|
|
|
|
if (pixbuf)
|
|
|
|
|
g_assert (pixbuf->ref_count != 0);
|
|
|
|
|
|
|
|
|
|
return pixbuf;
|
|
|
|
|
} else {
|
|
|
|
|
g_warning ("Unable to find handler for file: %s", filename);
|
1999-01-04 23:53:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
|
fclose (f);
|
1999-01-04 23:53:12 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
1999-10-22 21:05:16 +00:00
|
|
|
|
|
|
|
|
|
GdkPixbuf *
|
|
|
|
|
gdk_pixbuf_new_from_xpm_data (const gchar **data)
|
|
|
|
|
{
|
|
|
|
|
GdkPixbuf *(* load_xpm_data) (const gchar **data);
|
|
|
|
|
GdkPixbuf *pixbuf;
|
1999-10-26 20:43:39 +00:00
|
|
|
|
|
1999-10-22 21:05:16 +00:00
|
|
|
|
if (file_formats[XPM_FILE_FORMAT_INDEX].load_xpm_data == NULL) {
|
1999-10-26 20:43:39 +00:00
|
|
|
|
image_handler_load(&file_formats[XPM_FILE_FORMAT_INDEX]);
|
1999-10-22 21:05:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (file_formats[XPM_FILE_FORMAT_INDEX].load_xpm_data == NULL) {
|
|
|
|
|
g_warning("Can't find gdk-pixbuf module for parsing inline XPM data");
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
load_xpm_data = file_formats[XPM_FILE_FORMAT_INDEX].load_xpm_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pixbuf = load_xpm_data(data);
|
|
|
|
|
|
|
|
|
|
return pixbuf;
|
|
|
|
|
}
|
1999-10-26 20:43:39 +00:00
|
|
|
|
|