2008-08-15 23:45:18 +02:00
|
|
|
/* VIPS function dispatch tables for image format load/save.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This file is part of VIPS.
|
|
|
|
|
|
|
|
VIPS is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
#include <vips/intl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <vips/vips.h>
|
|
|
|
#include <vips/internal.h>
|
|
|
|
|
|
|
|
#ifdef WITH_DMALLOC
|
|
|
|
#include <dmalloc.h>
|
|
|
|
#endif /*WITH_DMALLOC*/
|
|
|
|
|
|
|
|
/* List of loaded formats.
|
|
|
|
*/
|
|
|
|
static GSList *format_list = NULL;
|
|
|
|
|
|
|
|
static gint
|
2008-08-16 19:03:45 +02:00
|
|
|
format_compare( im_format_t *a, im_format_t *b )
|
2008-08-15 23:45:18 +02:00
|
|
|
{
|
|
|
|
return( b->priority - a->priority );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sort the format list after a change.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
format_sort( void )
|
|
|
|
{
|
|
|
|
format_list = g_slist_sort( format_list,
|
|
|
|
(GCompareFunc) format_compare );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register/unregister formats.
|
|
|
|
*/
|
2008-08-17 17:03:42 +02:00
|
|
|
im_format_t *
|
|
|
|
im_format_register(
|
2008-08-15 23:45:18 +02:00
|
|
|
const char *name, const char *name_user, const char **suffs,
|
|
|
|
im_format_is_a_fn is_a, im_format_header_fn header,
|
|
|
|
im_format_load_fn load, im_format_save_fn save,
|
|
|
|
im_format_flags_fn flags )
|
|
|
|
{
|
2008-08-16 19:03:45 +02:00
|
|
|
im_format_t *format;
|
2008-08-15 23:45:18 +02:00
|
|
|
|
2008-08-16 19:03:45 +02:00
|
|
|
if( !(format = IM_NEW( NULL, im_format_t )) )
|
2008-08-15 23:45:18 +02:00
|
|
|
return( NULL );
|
|
|
|
format->name = name;
|
|
|
|
format->name_user = name_user;
|
|
|
|
format->priority = 0;
|
|
|
|
format->suffs = suffs;
|
|
|
|
format->is_a = is_a;
|
|
|
|
format->header = header;
|
|
|
|
format->load = load;
|
|
|
|
format->save = save;
|
|
|
|
format->flags = flags;
|
|
|
|
|
|
|
|
/* Append, so we keep the ordering where possible.
|
|
|
|
*/
|
|
|
|
format_list = g_slist_append( format_list, format );
|
|
|
|
format_sort();
|
|
|
|
|
|
|
|
return( format );
|
|
|
|
}
|
|
|
|
|
2008-08-17 17:03:42 +02:00
|
|
|
void
|
|
|
|
im_format_set_priority( im_format_t *format, int priority )
|
2008-08-15 23:45:18 +02:00
|
|
|
{
|
|
|
|
g_assert( format );
|
|
|
|
|
|
|
|
format->priority = priority;
|
|
|
|
format_sort();
|
|
|
|
}
|
|
|
|
|
2008-08-17 17:03:42 +02:00
|
|
|
void
|
|
|
|
im_format_unregister( im_format_t *format )
|
2008-08-15 23:45:18 +02:00
|
|
|
{
|
|
|
|
format_list = g_slist_remove( format_list, format );
|
2008-08-17 17:03:42 +02:00
|
|
|
IM_FREE( format );
|
2008-08-15 23:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Called on startup: register the base vips formats.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
im__format_init( void )
|
|
|
|
{
|
2008-08-16 19:03:45 +02:00
|
|
|
#ifdef HAVE_JPEG
|
2008-08-15 23:45:18 +02:00
|
|
|
im__jpeg_register();
|
2008-08-16 19:03:45 +02:00
|
|
|
#endif /*HAVE_JPEG*/
|
2008-08-25 18:48:15 +02:00
|
|
|
#ifdef HAVE_PNG
|
2008-08-15 23:45:18 +02:00
|
|
|
im__png_register();
|
2008-08-16 19:03:45 +02:00
|
|
|
#endif /*HAVE_PNG*/
|
2008-08-15 23:45:18 +02:00
|
|
|
im__csv_register();
|
|
|
|
im__ppm_register();
|
|
|
|
im__analyze_register();
|
2008-08-16 19:03:45 +02:00
|
|
|
#ifdef HAVE_OPENEXR
|
2008-08-15 23:45:18 +02:00
|
|
|
im__exr_register();
|
2008-08-16 19:03:45 +02:00
|
|
|
#endif /*HAVE_OPENEXR*/
|
|
|
|
#ifdef HAVE_MAGICK
|
2008-08-15 23:45:18 +02:00
|
|
|
im__magick_register();
|
2008-08-16 19:03:45 +02:00
|
|
|
#endif /*HAVE_MAGICK*/
|
|
|
|
#ifdef HAVE_TIFF
|
2008-08-15 23:45:18 +02:00
|
|
|
im__tiff_register();
|
2008-08-16 19:03:45 +02:00
|
|
|
#endif /*HAVE_TIFF*/
|
2008-08-15 23:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Map a function over all formats.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
im_format_map( VSListMap2Fn fn, void *a, void *b )
|
|
|
|
{
|
|
|
|
return( im_slist_map2( format_list, fn, a, b ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Can this format open this file?
|
|
|
|
*/
|
|
|
|
static void *
|
2008-08-16 19:03:45 +02:00
|
|
|
format_for_file_sub( im_format_t *format,
|
2008-08-15 23:45:18 +02:00
|
|
|
const char *filename, const char *name )
|
|
|
|
{
|
|
|
|
if( format->is_a ) {
|
|
|
|
if( format->is_a( name ) )
|
|
|
|
return( format );
|
|
|
|
}
|
|
|
|
else if( im_filename_suffix_match( name, format->suffs ) )
|
|
|
|
return( format );
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2008-08-16 19:03:45 +02:00
|
|
|
im_format_t *
|
2008-08-15 23:45:18 +02:00
|
|
|
im_format_for_file( const char *filename )
|
|
|
|
{
|
|
|
|
char name[FILENAME_MAX];
|
|
|
|
char options[FILENAME_MAX];
|
2008-08-16 19:03:45 +02:00
|
|
|
im_format_t *format;
|
2008-08-15 23:45:18 +02:00
|
|
|
|
|
|
|
/* Break any options off the name ... eg. "fred.tif:jpeg,tile"
|
|
|
|
* etc.
|
|
|
|
*/
|
|
|
|
im_filename_split( filename, name, options );
|
|
|
|
|
|
|
|
if( !im_existsf( "%s", name ) ) {
|
|
|
|
im_error( "im_format_for_file",
|
|
|
|
_( "\"%s\" is not readable" ), name );
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2008-08-16 19:03:45 +02:00
|
|
|
format = (im_format_t *) im_format_map(
|
2008-08-15 23:45:18 +02:00
|
|
|
(VSListMap2Fn) format_for_file_sub,
|
|
|
|
(void *) filename, (void *) name );
|
|
|
|
|
|
|
|
if( !format ) {
|
|
|
|
im_error( "im_format_for_file",
|
|
|
|
_( "\"%s\" is not in a supported format" ), name );
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( format );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Can we write this filename with this format? Ignore formats without a save
|
|
|
|
* method.
|
|
|
|
*/
|
|
|
|
static void *
|
2008-08-16 19:03:45 +02:00
|
|
|
format_for_name_sub( im_format_t *format,
|
2008-08-15 23:45:18 +02:00
|
|
|
const char *filename, const char *name )
|
|
|
|
{
|
|
|
|
if( format->save &&
|
|
|
|
im_filename_suffix_match( name, format->suffs ) )
|
|
|
|
return( format );
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2008-08-16 19:03:45 +02:00
|
|
|
im_format_t *
|
2008-08-15 23:45:18 +02:00
|
|
|
im_format_for_name( const char *filename )
|
|
|
|
{
|
|
|
|
char name[FILENAME_MAX];
|
|
|
|
char options[FILENAME_MAX];
|
|
|
|
|
|
|
|
/* Break any options off the name ... eg. "fred.tif:jpeg,tile"
|
|
|
|
* etc.
|
|
|
|
*/
|
|
|
|
im_filename_split( filename, name, options );
|
|
|
|
|
2008-08-16 19:03:45 +02:00
|
|
|
return( (im_format_t *) im_format_map(
|
2008-08-15 23:45:18 +02:00
|
|
|
(VSListMap2Fn) format_for_name_sub,
|
|
|
|
(void *) filename, (void *) name ) );
|
|
|
|
}
|
|
|
|
|