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>
|
2008-08-21 10:32:24 +02:00
|
|
|
#include <stdarg.h>
|
2008-08-19 00:57:32 +02:00
|
|
|
|
|
|
|
#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 );
|
|
|
|
}
|
|
|
|
|
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-21 10:32:24 +02:00
|
|
|
im_type_t tmp;
|
|
|
|
im_type_t *type;
|
2008-08-19 16:06:09 +02:00
|
|
|
|
2008-08-21 10:32:24 +02:00
|
|
|
/* Look for this exact type.
|
|
|
|
*/
|
|
|
|
tmp.name = name;
|
|
|
|
tmp.type_param = type_param;
|
|
|
|
if( !(type = (im_type_t *)
|
|
|
|
g_hash_table_lookup( im_type_table, &tmp )) ) {
|
|
|
|
/* Not found. Look for just the name type, eg. "array".
|
|
|
|
*/
|
|
|
|
tmp.type_param = NULL;
|
|
|
|
if( (type = (im_type_t *)
|
|
|
|
g_hash_table_lookup( im_type_table, &tmp )) )
|
|
|
|
/* Found it. Register a new compound type with this
|
|
|
|
* param.
|
|
|
|
*/
|
|
|
|
type = im_type_register( name, type_param, type->size );
|
|
|
|
}
|
2008-08-19 16:06:09 +02:00
|
|
|
|
2008-08-21 10:32:24 +02:00
|
|
|
return( 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 *
|
2008-08-21 10:32:24 +02:00
|
|
|
im_argument_new( const char *name, im_type_t *type, im_argument_flags flags )
|
2008-08-19 18:54:55 +02:00
|
|
|
{
|
|
|
|
im_argument_t *argument;
|
|
|
|
|
|
|
|
if( !(argument = IM_NEW( NULL, im_argument_t )) )
|
|
|
|
return( NULL );
|
|
|
|
argument->name = name;
|
|
|
|
argument->type = type;
|
2008-08-21 10:32:24 +02:00
|
|
|
argument->flags = flags;
|
2008-08-19 18:54:55 +02:00
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2008-08-21 10:32:24 +02:00
|
|
|
im_operation_t *
|
|
|
|
im_operation_registerv( const char *name, const char *desc,
|
|
|
|
im_operation_flags flags, im_operation_dispatch_fn disp, ... )
|
|
|
|
{
|
|
|
|
im_operation_t *operation;
|
|
|
|
im_argument_t *argument;
|
|
|
|
va_list ap;
|
|
|
|
int argc;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
va_start( ap, disp );
|
|
|
|
for( argc = 0; va_arg( ap, im_argument_t * ); argc++ )
|
|
|
|
;
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
operation = im_operation_register( name, desc, flags, disp, argc );
|
|
|
|
va_start( ap, disp );
|
|
|
|
for( i = 0; (argument = va_arg( ap, im_argument_t * )); i++ )
|
|
|
|
operation->argv[i] = argument;
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
return( operation );
|
|
|
|
}
|
|
|
|
|
2008-08-19 18:54:55 +02:00
|
|
|
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-21 10:32:24 +02:00
|
|
|
im_operation_registerv( "im_add", _( "add two images" ),
|
2008-08-19 18:54:55 +02:00
|
|
|
IM_FN_PIO | IM_FN_PTOP,
|
|
|
|
add_vec,
|
2008-08-21 10:32:24 +02:00
|
|
|
im_argument_new( "in1", IM_TYPE_IM, 0 ),
|
|
|
|
im_argument_new( "in2", IM_TYPE_IM, 0 ),
|
|
|
|
im_argument_new( "out", IM_TYPE_IM, IM_ARGUMENT_OUTPUT ),
|
|
|
|
NULL );
|
2008-08-19 10:01:23 +02:00
|
|
|
}
|