rewrite im_recomb() as a class

This commit is contained in:
John Cupitt 2011-11-09 14:48:38 +00:00
parent 6631fab25c
commit f79bb525f5
10 changed files with 287 additions and 157 deletions

View File

@ -8,7 +8,7 @@
im_sintra(), im_costra(), im_tantra(), im_asintra(), im_acostra(),
im_atantra(), im_exptra(), im_exp10tra(), im_logtra(), im_log10tra(),
im_abs(), im_sign(), im_max(), im_maxpos(), im_deviate(), im_divide(),
im_multiply(), im_stats()
im_multiply(), im_stats(), im_measure(), im_recomb()
redone as classes
- added argument priorites to help control arg ordering
- generate has a 'stop' param to signal successful early termination

4
TODO
View File

@ -1,13 +1,13 @@
- avg/dev etc. should uncode images? eg. labq2lab etc.
test new measure
- can move the whole of mask, plus headers, plus conversion/im_vips2mask and
mask2vips to deprecated
make a new thing to load a mask file from csv2vips?

View File

@ -7,7 +7,7 @@ libarithmetic_la_SOURCES = \
im_cross_phase.c \
deviate.c \
divide.c \
im_recomb.c \
recomb.c \
im_linreg.c \
im_maxpos_avg.c \
im_maxpos_vec.c \

View File

@ -516,6 +516,7 @@ vips_arithmetic_operation_init( void )
extern GType vips_sign_get_type( void );
extern GType vips_stats_get_type( void );
extern GType vips_measure_get_type( void );
extern GType vips_recomb_get_type( void );
vips_add_get_type();
vips_subtract_get_type();
@ -532,5 +533,5 @@ vips_arithmetic_operation_init( void )
vips_sign_get_type();
vips_stats_get_type();
vips_measure_get_type();
vips_recomb_get_type();
}

View File

@ -1,151 +0,0 @@
/* im_recomb.c
*
* 21/6/95 JC
* - mildly modernised
* 14/3/96 JC
* - better error checks, partial
* 4/11/09
* - gtkdoc
*/
/*
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 <vips/vips.h>
/* Inner loop.
*/
#define LOOP( IN, OUT ) { \
IN *p = (IN *) bin; \
OUT *q = (OUT *) bout; \
\
for( i = 0; i < width; i++ ) { \
double *m = mat->coeff; \
\
for( v = 0; v < mat->ysize; v++ ) { \
double t = 0.0; \
\
for( u = 0; u < mat->xsize; u++ ) \
t += *m++ * p[u]; \
\
*q++ = (OUT) t; \
} \
\
p += mat->xsize; \
} \
}
/* Process a buffer of PELs.
*/
static int
recomb_buf( void *bin, void *bout, int width, IMAGE *in, DOUBLEMASK *mat )
{
int i;
int u, v;
/* Do the processing.
*/
switch( in->BandFmt ) {
case IM_BANDFMT_UCHAR: LOOP( unsigned char, float ); break;
case IM_BANDFMT_CHAR: LOOP( signed char, float ); break;
case IM_BANDFMT_USHORT: LOOP( unsigned short, float ); break;
case IM_BANDFMT_SHORT: LOOP( signed short, float ); break;
case IM_BANDFMT_UINT: LOOP( unsigned int, float ); break;
case IM_BANDFMT_INT: LOOP( signed int, float ); break;
case IM_BANDFMT_FLOAT: LOOP( float, float ); break;
case IM_BANDFMT_DOUBLE: LOOP( double, double ); break;
default:
g_assert( 0 );
}
return( 0 );
}
/**
* im_recomb:
* @in: input image
* @out: output image
* @recomb: recombination matrix
*
* This operation recombines an image's bands. Each pixel in @in is treated as
* an n-element vector, where n is the number of bands in @in, and multipled by
* the n x m matrix @recomb to produce the m-band image @out.
*
* @out is always float, unless @in is double, in which case @out is double
* too. No complex images allowed.
*
* It's useful for various sorts of colour space conversions.
*
* Returns: 0 on success, -1 on error.
*/
int
im_recomb( IMAGE *in, IMAGE *out, DOUBLEMASK *recomb )
{
DOUBLEMASK *mcpy;
/* Check input image.
*/
if( im_piocheck( in, out ) ||
im_check_uncoded( "im_recomb", in ) ||
im_check_noncomplex( "im_recomb", in ) )
return( -1 );
if( in->Bands != recomb->xsize ) {
im_error( "im_recomb", "%s",
_( "bands in must equal matrix width" ) );
return( -1 );
}
/* Prepare the output image
*/
if( im_cp_desc( out, in ) )
return( -1 );
out->Bands = recomb->ysize;
if( vips_bandfmt_isint( in->BandFmt ) )
out->BandFmt = IM_BANDFMT_FLOAT;
/* Take a copy of the matrix.
*/
if( !(mcpy = im_dup_dmask( recomb, "conv_mask" )) )
return( -1 );
if( im_add_close_callback( out,
(im_callback_fn) im_free_dmask, mcpy, NULL ) ) {
im_free_dmask( mcpy );
return( -1 );
}
/* And process!
*/
if( im_wrapone( in, out, (im_wrapone_fn) recomb_buf, in, mcpy ) )
return( -1 );
return( 0 );
}

254
libvips/arithmetic/recomb.c Normal file
View File

@ -0,0 +1,254 @@
/* recomb.c ... pass an image though a matrix
*
* 21/6/95 JC
* - mildly modernised
* 14/3/96 JC
* - better error checks, partial
* 4/11/09
* - gtkdoc
* 9/11/11
* - redo as a class
*/
/*
Copyright (C) 1991-2005 The National Gallery
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU 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
*/
/*
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
#include "arithmetic.h"
#include "unary.h"
/**
* VipsRecomb:
* @in: input image
* @out: output image
* @m: recombination matrix
*
* This operation recombines an image's bands. Each pixel in @in is treated as
* an n-element vector, where n is the number of bands in @in, and multipled by
* the n x m matrix @recomb to produce the m-band image @out.
*
* @out is always float, unless @in is double, in which case @out is double
* too. No complex images allowed.
*
* It's useful for various sorts of colour space conversions.
*/
typedef struct _VipsRecomb {
VipsOperation parent_instance;
VipsImage *in;
VipsImage *out;
VipsImage *m;
/* m converted to a one-band double.
*/
double *coeff;
} VipsRecomb;
typedef VipsOperationClass VipsRecombClass;
G_DEFINE_TYPE( VipsRecomb, vips_recomb, VIPS_TYPE_OPERATION );
/* Inner loop.
*/
#define LOOP( IN, OUT ) { \
IN *p = (IN *) in; \
OUT *q = (OUT *) out; \
\
for( x = 0; x < or->valid.width; x++ ) { \
double *m; \
\
m = recomb->coeff; \
\
for( v = 0; v < mheight; v++ ) { \
double t; \
\
t = 0.0; \
\
for( u = 0; u < mwidth; u++ ) \
t += m[u] * p[u]; \
\
q[v] = (OUT) t; \
m += mwidth; \
} \
\
p += mwidth; \
q += mheight; \
} \
}
static int
vips_recomb_gen( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion *ir = (VipsRegion *) seq;
VipsRecomb *recomb = (VipsRecomb *) b;
VipsImage *im = recomb->in;
int mwidth = recomb->m->Xsize;
int mheight = recomb->m->Ysize;
int y, x, u, v;
if( vips_region_prepare( ir, &or->valid ) )
return( -1 );
for( y = 0; y < or->valid.height; y++ ) {
PEL *in = (PEL *) VIPS_REGION_ADDR( ir,
or->valid.left, or->valid.top + y );
PEL *out = (PEL *) VIPS_REGION_ADDR( or,
or->valid.left, or->valid.top + y );
switch( vips_image_get_format( im ) ) {
case VIPS_FORMAT_UCHAR: LOOP( unsigned char, float ); break;
case VIPS_FORMAT_CHAR: LOOP( signed char, float ); break;
case VIPS_FORMAT_USHORT:LOOP( unsigned short, float ); break;
case VIPS_FORMAT_SHORT: LOOP( signed short, float ); break;
case VIPS_FORMAT_UINT: LOOP( unsigned int, float ); break;
case VIPS_FORMAT_INT: LOOP( signed int, float ); break;
case VIPS_FORMAT_FLOAT: LOOP( float, float ); break;
case VIPS_FORMAT_DOUBLE:LOOP( double, double ); break;
default:
g_assert( 0 );
}
}
return( 0 );
}
static int
vips_recomb_build( VipsObject *object )
{
VipsRecomb *recomb = (VipsRecomb *) object;
VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 );
if( VIPS_OBJECT_CLASS( vips_recomb_parent_class )->build( object ) )
return( -1 );
if( vips_image_pio_input( recomb->in ) ||
vips_check_uncoded( "VipsRecomb", recomb->in ) ||
vips_check_noncomplex( "VipsRecomb", recomb->in ) )
return( -1 );
if( vips_image_pio_input( recomb->m ) ||
vips_check_uncoded( "VipsRecomb", recomb->m ) ||
vips_check_noncomplex( "VipsRecomb", recomb->m ) ||
vips_check_mono( "VipsRecomb", recomb->m ) )
return( -1 );
if( recomb->in->Bands != recomb->m->Xsize ) {
vips_error( "VipsRecomb",
"%s", _( "bands in must equal matrix width" ) );
return( -1 );
}
if( vips_cast( recomb->m, &t[0], VIPS_FORMAT_DOUBLE, NULL ) ||
vips_image_wio_input( t[0] ) )
return( -1 );
recomb->coeff = (double *) VIPS_IMAGE_ADDR( t[0], 0, 0 );
g_object_set( recomb, "out", vips_image_new(), NULL );
if( vips_image_copy_fields( recomb->out, recomb->in ) )
return( -1 );
vips_demand_hint( recomb->out,
VIPS_DEMAND_STYLE_THINSTRIP, recomb->in, NULL );
recomb->out->Bands = recomb->m->Ysize;
if( vips_bandfmt_isint( recomb->in->BandFmt ) )
recomb->out->BandFmt = VIPS_FORMAT_FLOAT;
if( vips_image_generate( recomb->out,
vips_start_one, vips_recomb_gen, vips_stop_one,
recomb->in, recomb ) )
return( -1 );
return( 0 );
}
static void
vips_recomb_class_init( VipsRecombClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "recomb";
object_class->description = _( "linear recombination with matrix" );
object_class->build = vips_recomb_build;
VIPS_ARG_IMAGE( class, "in", 1,
_( "Input" ),
_( "Input image argument" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsRecomb, in ) );
VIPS_ARG_IMAGE( class, "out", 100,
_( "Output" ),
_( "Output image" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsRecomb, out ) );
VIPS_ARG_IMAGE( class, "m", 102,
_( "M" ),
_( "matrix of coefficients" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsRecomb, m ) );
}
static void
vips_recomb_init( VipsRecomb *recomb )
{
}
int
vips_recomb( VipsImage *in, VipsImage **out, VipsImage *m, ... )
{
va_list ap;
int result;
va_start( ap, m );
result = vips_call_split( "recomb", ap, in, out, m );
va_end( ap );
return( result );
}

View File

@ -89,6 +89,7 @@ vips_unary_class_init( VipsUnaryClass *class )
_( "Input image argument" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsUnary, in ) );
}
static void

View File

@ -1622,3 +1622,26 @@ im_stats( VipsImage *in )
return( msk );
}
int
im_recomb( IMAGE *in, IMAGE *out, DOUBLEMASK *recomb )
{
VipsImage *t1, *t2;
if( !(t1 = vips_image_new()) ||
im_mask2vips( recomb, t1 ) )
return( -1 );
if( vips_recomb( in, &t2, t1,
NULL ) ) {
g_object_unref( t1 );
return( -1 );
}
g_object_unref( t1 );
if( vips_image_write( t2, out ) ) {
g_object_unref( t2 );
return( -1 );
}
g_object_unref( t2 );
return( 0 );
}

View File

@ -100,6 +100,8 @@ int vips_stats( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_measure( VipsImage *in, VipsImage **out, int h, int v, ... )
__attribute__((sentinel));
int vips_recomb( VipsImage *in, VipsImage **out, VipsImage *m, ... )
__attribute__((sentinel));
@ -112,7 +114,6 @@ int im_bandmean( VipsImage *in, VipsImage *out );
int im_remainder( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_remainder_vec( VipsImage *in, VipsImage *out, int n, double *c );
int im_remainderconst( VipsImage *in, VipsImage *out, double c );
int im_recomb( VipsImage *in, VipsImage *out, DOUBLEMASK *recomb );
int im_floor( VipsImage *in, VipsImage *out );
int im_rint( VipsImage *in, VipsImage *out );

View File

@ -540,6 +540,7 @@ DOUBLEMASK *im_measure_area( VipsImage *im,
int left, int top, int width, int height,
int h, int v,
int *sel, int nsel, const char *name );
int im_recomb( VipsImage *in, VipsImage *out, DOUBLEMASK *recomb );
int im_sintra( VipsImage *in, VipsImage *out );
int im_costra( VipsImage *in, VipsImage *out );