libvips/libsrc/iofuncs/type.c

346 lines
7.5 KiB
C
Raw Normal View History

2008-08-19 00:57:32 +02:00
/* Define built-in VIPS types.
*/
/*
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 <stdlib.h>
#include <string.h>
#include <vips/vips.h>
#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif /*WITH_DMALLOC*/
2008-08-19 16:06:09 +02:00
/* Keep types in a GHashTable, indexed by name + type_param.
2008-08-19 00:57:32 +02:00
*/
static GHashTable *im_type_table = NULL;
2008-08-19 18:54:55 +02:00
/* Keep operations in a GHashTable, indexed by name.
*/
static GHashTable *im_operation_table = NULL;
2008-08-19 16:06:09 +02:00
static unsigned int
im_type_hash( im_type_t *type )
{
return( g_str_hash( type->name ) |
GPOINTER_TO_UINT( type->type_param ) );
}
static gboolean
im_type_equal( im_type_t *type1, im_type_t *type2 )
{
return( type1->type_param == type2->type_param &&
g_str_equal( type1->name, type2->name ) );
}
2008-08-19 00:57:32 +02:00
im_type_t *
2008-08-19 16:06:09 +02:00
im_type_register( const char *name,
2008-08-19 18:54:55 +02:00
im_type_t *type_param, size_t size )
2008-08-19 00:57:32 +02:00
{
im_type_t *type;
2008-08-19 16:06:09 +02:00
if( im_type_lookup( name, type_param ) ) {
2008-08-19 10:01:23 +02:00
im_error( "im_type_register",
_( "type name already registered" ) );
return( NULL );
}
2008-08-19 00:57:32 +02:00
if( !(type = IM_NEW( NULL, im_type_t )) )
return( NULL );
type->name = name;
2008-08-19 16:06:09 +02:00
type->type_param = type_param;
2008-08-19 00:57:32 +02:00
type->size = size;
if( !im_type_table )
2008-08-19 16:06:09 +02:00
im_type_table = g_hash_table_new(
(GHashFunc) im_type_hash,
(GEqualFunc) im_type_equal );
g_hash_table_insert( im_type_table, type, type );
2008-08-19 00:57:32 +02:00
return( type );
}
typedef struct {
void *a;
void *b;
VSListMap2Fn fn;
void *result;
} Pair;
static gboolean
2008-08-19 18:54:55 +02:00
im_hash_table_predicate( const char *key, im_type_t *type, Pair *pair )
2008-08-19 00:57:32 +02:00
{
return( (pair->result == pair->fn( type, pair->a, pair->b )) );
}
void *
2008-08-19 18:54:55 +02:00
im_hash_table_map( GHashTable *hash, VSListMap2Fn fn, void *a, void *b )
2008-08-19 00:57:32 +02:00
{
Pair pair;
pair.a = a;
pair.b = b;
pair.fn = fn;
pair.result = NULL;
2008-08-19 18:54:55 +02:00
g_hash_table_find( hash, (GHRFunc) im_hash_table_predicate, &pair );
2008-08-19 00:57:32 +02:00
return( pair.result );
}
2008-08-19 18:54:55 +02:00
void *
im_type_map( VSListMap2Fn fn, void *a, void *b )
{
return( im_hash_table_map( im_type_table, fn, a, b ) );
}
2008-08-19 00:57:32 +02:00
im_type_t *
2008-08-19 16:06:09 +02:00
im_type_lookup( const char *name, im_type_t *type_param )
2008-08-19 00:57:32 +02:00
{
2008-08-19 16:06:09 +02:00
im_type_t type;
type.name = name;
type.type_param = type_param;
return( (im_type_t *) g_hash_table_lookup( im_type_table, &type ) );
2008-08-19 00:57:32 +02:00
}
2008-08-19 18:54:55 +02:00
/* Register the base VIPS types.
*/
void
im__type_init( void )
{
im_type_register( IM_TYPE_NAME_DOUBLE, NULL, sizeof( double ) );
im_type_register( IM_TYPE_NAME_INT, NULL, sizeof( int ) );
im_type_register( IM_TYPE_NAME_COMPLEX, NULL, 2 * sizeof( double ) );
im_type_register( IM_TYPE_NAME_STRING, NULL, 0 );
im_type_register( IM_TYPE_NAME_IMASK, NULL, sizeof( im_value_mask_t ) );
im_type_register( IM_TYPE_NAME_DMASK, NULL, sizeof( im_value_mask_t ) );
im_type_register( IM_TYPE_NAME_IMAGE, NULL, 0 );
im_type_register( IM_TYPE_NAME_DISPLAY, NULL, 0 );
im_type_register( IM_TYPE_NAME_GVALUE, NULL, sizeof( GValue ) );
im_type_register( IM_TYPE_NAME_ARRAY,
NULL, sizeof( im_value_array_t ) );
}
2008-08-19 16:06:09 +02:00
/* Allocate an im_value_t.
*/
static im_value_t *
2008-08-19 18:54:55 +02:00
im_value_new( im_type_t *type )
2008-08-19 16:06:09 +02:00
{
2008-08-19 18:54:55 +02:00
im_value_t *value;
2008-08-19 16:06:09 +02:00
if( type->size ) {
2008-08-19 18:54:55 +02:00
if( !(value = im_malloc( NULL, type->size )) )
2008-08-19 16:06:09 +02:00
return( NULL );
2008-08-19 18:54:55 +02:00
memset( value, 0, type->size );
2008-08-19 16:06:09 +02:00
}
else
2008-08-19 18:54:55 +02:00
value = NULL;
2008-08-19 16:06:09 +02:00
2008-08-19 18:54:55 +02:00
return( value );
2008-08-19 16:06:09 +02:00
}
/* Free an im_value_t.
2008-08-19 00:57:32 +02:00
*/
static void
2008-08-19 18:54:55 +02:00
im_value_free( im_value_t *value, im_type_t *type )
2008-08-19 00:57:32 +02:00
{
2008-08-19 16:06:09 +02:00
if( type->size )
2008-08-19 18:54:55 +02:00
IM_FREE( value );
2008-08-19 00:57:32 +02:00
}
2008-08-19 18:54:55 +02:00
/* Convenience functions to build and free various values.
2008-08-19 16:06:09 +02:00
*/
2008-08-19 18:54:55 +02:00
void
im_value_imask_free( im_value_mask_t *value )
2008-08-19 00:57:32 +02:00
{
2008-08-19 16:06:09 +02:00
IM_FREE( value->name );
IM_FREEF( im_free_imask, value->mask );
2008-08-19 00:57:32 +02:00
}
2008-08-19 18:54:55 +02:00
void
im_value_dmask_free( im_value_mask_t *value )
2008-08-19 00:57:32 +02:00
{
2008-08-19 16:06:09 +02:00
IM_FREE( value->name );
IM_FREEF( im_free_dmask, value->mask );
}
2008-08-19 00:57:32 +02:00
2008-08-19 18:54:55 +02:00
void
im_value_gvalue_free( GValue *value )
2008-08-19 16:06:09 +02:00
{
2008-08-19 00:57:32 +02:00
g_value_unset( value );
}
2008-08-19 18:54:55 +02:00
void
2008-08-19 16:06:09 +02:00
im_value_array_free( im_value_array_t *value, im_type_t *type )
2008-08-19 00:57:32 +02:00
{
2008-08-19 16:06:09 +02:00
int i;
for( i = 0; i < value->n; i++ )
im_value_free( value->array[i], type->type_param );
2008-08-19 00:57:32 +02:00
}
2008-08-19 18:54:55 +02:00
gboolean
im_value_mask_output_init( im_value_mask_t *value, const char *name )
{
if( !(value->name = im_strdup( NULL, name )) )
return( FALSE );
return( TRUE );
}
gboolean
im_value_imask_input_init( im_value_mask_t *value, const char *name )
{
INTMASK *mask;
if( !(mask = im_read_imask( name )) )
return( FALSE );
value->mask = (void *) mask;
if( !(value->name = im_strdup( NULL, name )) ) {
im_value_imask_free( value );
return( FALSE );
}
return( TRUE );
}
/* Create arguments.
*/
im_argument_t *
im_argument_new( const char *name, im_type_t *type, gboolean input )
{
im_argument_t *argument;
if( !(argument = IM_NEW( NULL, im_argument_t )) )
return( NULL );
argument->name = name;
argument->type = type;
argument->input = input;
return( argument );
}
void
im_argument_free( im_argument_t *argument )
{
im_free( argument );
}
/* Register/iterate/lookup operations.
2008-08-19 10:01:23 +02:00
*/
2008-08-19 16:06:09 +02:00
void
2008-08-19 18:54:55 +02:00
im_operation_unregister( im_operation_t *operation )
{
int i;
g_hash_table_remove( im_operation_table, operation->name );
for( i = 0; i < operation->argc; i++ )
IM_FREEF( im_argument_free, operation->argv[i] );
IM_FREE( operation );
}
im_operation_t *
im_operation_register( const char *name, const char *desc,
im_operation_flags flags, im_operation_dispatch_fn disp, int argc )
{
im_operation_t *operation;
if( im_operation_lookup( name ) ) {
im_error( "im_operation_register",
_( "operation name already registered" ) );
return( NULL );
}
if( !(operation = IM_NEW( NULL, im_operation_t )) )
return( NULL );
operation->name = name;
operation->desc = desc;
operation->flags = flags;
operation->disp = disp;
operation->argc = argc;
operation->argv = NULL;
if( !(operation->argv = IM_ARRAY( NULL, argc, im_argument_t * )) ) {
im_operation_unregister( operation );
return( NULL );
}
memset( operation->argv, 0, argc * sizeof( im_argument_t * ) );
if( !im_operation_table )
im_operation_table = g_hash_table_new(
g_str_hash, g_str_equal );
g_hash_table_insert( im_operation_table, (char *) name, operation );
return( operation );
}
void *
im_operation_map( VSListMap2Fn fn, void *a, void *b )
{
return( im_hash_table_map( im_operation_table, fn, a, b ) );
}
im_operation_t *
im_operation_lookup( const char *name )
{
return( (im_operation_t *)
g_hash_table_lookup( im_operation_table, name ) );
}
static int
add_vec( im_value_t **argv )
{
return( im_add( argv[0], argv[1], argv[2] ) );
}
/* Make a sample operation.
*/
void
im__operation_init( void )
2008-08-19 10:01:23 +02:00
{
2008-08-19 18:54:55 +02:00
im_operation_t *operation;
operation = im_operation_register( "im_add", _( "add two images" ),
IM_FN_PIO | IM_FN_PTOP,
add_vec,
3 );
operation->argv[0] = im_argument_new( "in1",
im_type_lookup( IM_TYPE_NAME_IMAGE, NULL ), TRUE );
operation->argv[1] = im_argument_new( "in2",
im_type_lookup( IM_TYPE_NAME_IMAGE, NULL ), TRUE );
operation->argv[2] = im_argument_new( "out",
im_type_lookup( IM_TYPE_NAME_IMAGE, NULL ), TRUE );
2008-08-19 10:01:23 +02:00
}