libvips/libsrc/format/format.c

296 lines
6.4 KiB
C
Raw Normal View History

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*/
2008-11-28 17:26:33 +01:00
/* To iterate over supported formats, we build a temp list of subclasses of
* VipsFormat, sort by priority, iterate, and free.
2008-08-15 23:45:18 +02:00
*/
2008-11-28 17:26:33 +01:00
static void *
format_add_class( VipsFormatClass *format, GSList **formats )
2008-08-15 23:45:18 +02:00
{
2008-11-28 17:26:33 +01:00
/* Append so we don't reverse the list of formats.
*/
*formats = g_slist_append( *formats, format );
return( NULL );
2008-08-15 23:45:18 +02:00
}
2008-11-28 17:26:33 +01:00
static gint
format_compare( VipsFormatClass *a, VipsFormatClass *b )
2008-08-15 23:45:18 +02:00
{
2008-11-28 17:26:33 +01:00
return( b->priority - a->priority );
2008-08-15 23:45:18 +02:00
}
2008-11-28 17:26:33 +01:00
void *
vips_format_map( VSListMap2Fn fn, void *a, void *b )
2008-08-15 23:45:18 +02:00
{
2008-11-28 17:26:33 +01:00
GSList *formats;
void *result;
2008-08-15 23:45:18 +02:00
2008-11-28 17:26:33 +01:00
formats = NULL;
(void) vips_class_map_concrete_all( g_type_from_name( "VipsFormat" ),
(VipsClassMap) format_add_class, (void *) &formats );
2008-08-15 23:45:18 +02:00
2008-11-28 17:26:33 +01:00
formats = g_slist_sort( formats, (GCompareFunc) format_compare );
result = im_slist_map2( formats, fn, a, b );
g_slist_free( formats );
return( result );
2008-08-15 23:45:18 +02:00
}
2008-11-28 17:26:33 +01:00
/* Abstract base class for image formats.
*/
2008-08-15 23:45:18 +02:00
2008-11-28 17:26:33 +01:00
static void
vips_format_class_init( VipsFormatClass *class )
{
2008-08-15 23:45:18 +02:00
}
2008-11-28 17:26:33 +01:00
static void
vips_format_init( VipsFormat *object )
2008-08-15 23:45:18 +02:00
{
}
2008-11-28 17:26:33 +01:00
G_DEFINE_ABSTRACT_TYPE( VipsFormat, vips_format, VIPS_TYPE_OBJECT );
/* VIPS format class.
*/
2008-10-11 23:29:16 +02:00
static const char *vips_suffs[] = { ".v", NULL };
static int
file2vips( const char *filename, IMAGE *out )
{
IMAGE *im;
if( !(im = im_open_local( out, filename, "r" )) ||
im_copy( im, out ) )
return( -1 );
return( 0 );
}
static int
vips2file( IMAGE *im, const char *filename )
{
IMAGE *out;
if( !(out = im_open_local( im, filename, "w" )) ||
im_copy( im, out ) )
return( -1 );
return( 0 );
}
2008-11-28 17:26:33 +01:00
static VipsFormatFlags
2008-10-11 23:29:16 +02:00
vips_flags( const char *filename )
{
2008-11-28 17:26:33 +01:00
return( VIPS_FORMAT_FLAG_PARTIAL );
}
/* Vips format adds no new members.
*/
typedef VipsFormat VipsFormatVips;
typedef VipsFormatClass VipsFormatVipsClass;
static void
vips_format_vips_class_init( VipsFormatVipsClass *class )
{
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsFormatClass *format_class = (VipsFormatClass *) class;
object_class->nickname = "vips";
object_class->description = _( "VIPS" );
format_class->is_a = im_isvips;
format_class->header = file2vips;
format_class->load = file2vips;
format_class->save = vips2file;
format_class->get_flags = vips_flags;
format_class->suffs = vips_suffs;
2008-10-11 23:29:16 +02:00
}
2008-11-28 17:26:33 +01:00
static void
vips_format_vips_init( VipsFormatVips *object )
{
}
G_DEFINE_TYPE( VipsFormatVips, vips_format_vips, VIPS_TYPE_FORMAT );
2008-08-15 23:45:18 +02:00
/* Called on startup: register the base vips formats.
*/
void
im__format_init( void )
{
2008-11-28 17:26:33 +01:00
extern GType vips_format_csv_get_type();
extern GType vips_format_ppm_get_type();
extern GType vips_format_analyze_get_type();
2008-10-11 23:29:16 +02:00
2008-11-28 17:26:33 +01:00
vips_format_vips_get_type();
2008-08-16 19:03:45 +02:00
#ifdef HAVE_JPEG
2008-11-28 17:26:33 +01:00
extern GType vips_format_jpeg_get_type();
vips_format_jpeg_get_type();
2008-08-16 19:03:45 +02:00
#endif /*HAVE_JPEG*/
2008-08-25 18:48:15 +02:00
#ifdef HAVE_PNG
2008-11-28 17:26:33 +01:00
extern GType vips_format_png_get_type();
vips_format_png_get_type();
2008-08-16 19:03:45 +02:00
#endif /*HAVE_PNG*/
2008-11-28 17:26:33 +01:00
vips_format_csv_get_type();
vips_format_ppm_get_type();
vips_format_analyze_get_type();
2008-08-16 19:03:45 +02:00
#ifdef HAVE_OPENEXR
2008-11-28 17:26:33 +01:00
extern GType vips_format_exr_get_type();
vips_format_exr_get_type();
2008-08-16 19:03:45 +02:00
#endif /*HAVE_OPENEXR*/
#ifdef HAVE_MAGICK
2008-11-28 17:26:33 +01:00
extern GType vips_format_magick_get_type();
vips_format_magick_get_type();
2008-08-16 19:03:45 +02:00
#endif /*HAVE_MAGICK*/
#ifdef HAVE_TIFF
2008-11-28 17:26:33 +01:00
extern GType vips_format_tiff_get_type();
vips_format_tiff_get_type();
2008-08-16 19:03:45 +02:00
#endif /*HAVE_TIFF*/
2008-08-15 23:45:18 +02:00
}
/* Can this format open this file?
*/
static void *
2008-11-28 17:26:33 +01:00
format_for_file_sub( VipsFormatClass *format,
2008-10-11 23:29:16 +02:00
const char *name, const char *filename )
2008-08-15 23:45:18 +02:00
{
if( format->is_a ) {
2008-10-11 23:29:16 +02:00
if( format->is_a( filename ) )
2008-08-15 23:45:18 +02:00
return( format );
}
2008-10-11 23:29:16 +02:00
else if( im_filename_suffix_match( filename, format->suffs ) )
2008-08-15 23:45:18 +02:00
return( format );
return( NULL );
}
2008-11-28 17:26:33 +01:00
VipsFormatClass *
vips_format_for_file( const char *name )
2008-08-15 23:45:18 +02:00
{
2008-10-11 23:29:16 +02:00
char filename[FILENAME_MAX];
2008-08-15 23:45:18 +02:00
char options[FILENAME_MAX];
2008-11-28 17:26:33 +01:00
VipsFormatClass *format;
2008-08-15 23:45:18 +02:00
/* Break any options off the name ... eg. "fred.tif:jpeg,tile"
* etc.
*/
2008-10-11 23:29:16 +02:00
im_filename_split( name, filename, options );
2008-08-15 23:45:18 +02:00
2008-10-11 23:29:16 +02:00
if( !im_existsf( "%s", filename ) ) {
2008-11-28 17:26:33 +01:00
im_error( "vips_format_for_file",
2008-10-11 23:29:16 +02:00
_( "\"%s\" is not readable" ), filename );
2008-08-15 23:45:18 +02:00
return( NULL );
}
2008-11-28 17:26:33 +01:00
if( !(format = (VipsFormatClass *) vips_format_map(
2008-08-15 23:45:18 +02:00
(VSListMap2Fn) format_for_file_sub,
2008-10-11 23:29:16 +02:00
(void *) name, (void *) filename )) ) {
2008-11-28 17:26:33 +01:00
im_error( "vips_format_for_file",
2008-10-11 23:29:16 +02:00
_( "\"%s\" is not in a supported format" ), filename );
2008-08-15 23:45:18 +02:00
return( NULL );
}
return( format );
}
/* Can we write this filename with this format? Ignore formats without a save
* method.
*/
static void *
2008-11-28 17:26:33 +01:00
format_for_name_sub( VipsFormatClass *format, const char *name )
2008-08-15 23:45:18 +02:00
{
if( format->save &&
im_filename_suffix_match( name, format->suffs ) )
return( format );
return( NULL );
}
2008-11-28 17:26:33 +01:00
VipsFormatClass *
vips_format_for_name( const char *name )
2008-08-15 23:45:18 +02:00
{
2008-11-28 17:26:33 +01:00
VipsFormatClass *format;
2008-08-15 23:45:18 +02:00
2008-11-28 17:26:33 +01:00
if( !(format = (VipsFormatClass *) vips_format_map(
2008-10-11 23:29:16 +02:00
(VSListMap2Fn) format_for_name_sub, (void *) name, NULL )) ) {
char suffix[FILENAME_MAX];
im_filename_suffix( name, suffix );
2008-11-28 17:26:33 +01:00
im_error( "vips_format_for_name",
2008-10-11 23:29:16 +02:00
_( "\"%s\" is not a supported image format." ),
suffix );
return( NULL );
}
return( format );
}
2008-08-15 23:45:18 +02:00
2008-10-11 23:29:16 +02:00
int
2008-11-28 17:26:33 +01:00
vips_format_read( const char *name, IMAGE *out )
2008-10-11 23:29:16 +02:00
{
2008-11-28 17:26:33 +01:00
VipsFormatClass *format;
2008-10-11 23:29:16 +02:00
2008-11-28 17:26:33 +01:00
if( !(format = vips_format_for_file( name )) ||
2008-10-11 23:29:16 +02:00
format->load( name, out ) )
return( -1 );
return( 0 );
2008-08-15 23:45:18 +02:00
}
2008-10-11 23:29:16 +02:00
int
2008-11-28 17:26:33 +01:00
vips_format_write( IMAGE *im, const char *name )
2008-10-11 23:29:16 +02:00
{
2008-11-28 17:26:33 +01:00
VipsFormatClass *format;
2008-10-11 23:29:16 +02:00
2008-11-28 17:26:33 +01:00
if( !(format = vips_format_for_name( name )) ||
2008-10-11 23:29:16 +02:00
format->save( im, name ) )
return( -1 );
return( 0 );
}