bandmean as a class

also moved recomb over to conversion, other small fixes
This commit is contained in:
John Cupitt 2011-11-20 09:56:02 +00:00
parent b41b9ac19c
commit f944742c2a
17 changed files with 317 additions and 198 deletions

View File

@ -11,7 +11,8 @@
im_multiply(), im_stats(), im_measure(), im_recomb(), im_floor(), im_ceil(),
im_rint(), im_equal*(), im_notequal*(), im_less*(), im_lesseq*(), im_more*(),
im_moreeq*(), im_remainder*(), im_and*(), im_or*(), im_eor*(), im_shift*(),
im_pow*(), im_exp*(), im_ifthenelse(), im_blend(), im_c2amph(), im_c2rect()
im_pow*(), im_exp*(), im_ifthenelse(), im_blend(), im_c2amph(), im_c2rect(),
im_bandmean()
redone as classes
- added argument priorites to help control arg ordering
- generate has a 'stop' param to signal successful early termination

22
TODO
View File

@ -1,21 +1,15 @@
- can we common-up bits of recomb and bandmean? any other ops? bandjoin must
be rather similar?
they should certainly be able to share a y pixel loop
- test docs
- look at the help message from the vips-7.27 script
- try
$ vips join
... usage ...
join: too few arguments
VipsObject: parameter in1 to VipsJoin not set
VipsObject: parameter in2 to VipsJoin not set
VipsObject: parameter direction to VipsJoin not set
why do we have four errors? shouldn't we stop after the first one?
- add vips_extract_real(), vips_extract_imag(), vips_complexjoin()

View File

@ -4,11 +4,9 @@ libarithmetic_la_SOURCES = \
arith_dispatch.c \
abs.c \
complex.c \
im_bandmean.c \
im_cross_phase.c \
deviate.c \
divide.c \
recomb.c \
im_linreg.c \
im_maxpos_avg.c \
im_maxpos_vec.c \

View File

@ -493,7 +493,6 @@ 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 );
extern GType vips_round_get_type( void );
extern GType vips_relational_get_type( void );
extern GType vips_relational_const_get_type( void );
@ -520,7 +519,6 @@ vips_arithmetic_operation_init( void )
vips_sign_get_type();
vips_stats_get_type();
vips_measure_get_type();
vips_recomb_get_type();
vips_round_get_type();
vips_relational_get_type();
vips_relational_const_get_type();

View File

@ -1,148 +0,0 @@
/* im_bandmean.c
*
* Author: Simon Goodall
* Written on: 17/7/07
* 17/7/07 JC
* - hacked about a bit
* 18/8/09
* - gtkdoc
* - get rid of the complex case, just double the width
*/
/*
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 <assert.h>
#include <vips/vips.h>
#include <vips/internal.h>
/* Int types. Round, keep sum in a larger variable.
*/
#define ILOOP( TYPE, STYPE ) { \
TYPE *p1 = (TYPE *) p; \
TYPE *q1 = (TYPE *) q; \
\
for( i = 0; i < sz; i++ ) { \
STYPE sum; \
\
sum = 0; \
for( j = 0; j < b; j++ ) \
sum += p1[j]; \
q1[i] = sum > 0 ? (sum + b / 2) / b : (sum - b / 2) / b; \
p1 += b; \
} \
}
/* Float loop. No rounding, sum in same container.
*/
#define FLOOP( TYPE ) { \
TYPE *p1 = (TYPE *) p; \
TYPE *q1 = (TYPE *) q; \
\
for( i = 0; i < sz; i++ ) { \
TYPE sum; \
\
sum = 0; \
for( j = 0; j < b; j++ ) \
sum += p1[j]; \
q1[i] = sum / b; \
p1 += b; \
} \
}
static void
bandmean_buffer( PEL *p, PEL *q, int n, IMAGE *in )
{
/* Complex just doubles the size.
*/
const int sz = n * (vips_bandfmt_iscomplex( in->BandFmt ) ? 2 : 1);
const int b = in->Bands;
int i, j;
switch( in->BandFmt ) {
case IM_BANDFMT_CHAR: ILOOP( signed char, int ); break;
case IM_BANDFMT_UCHAR: ILOOP( unsigned char, unsigned int ); break;
case IM_BANDFMT_SHORT: ILOOP( signed short, int ); break;
case IM_BANDFMT_USHORT: ILOOP( unsigned short, unsigned int ); break;
case IM_BANDFMT_INT: ILOOP( signed int, int ); break;
case IM_BANDFMT_UINT: ILOOP( unsigned int, unsigned int ); break;
case IM_BANDFMT_FLOAT: FLOOP( float ); break;
case IM_BANDFMT_DOUBLE: FLOOP( double ); break;
case IM_BANDFMT_COMPLEX:FLOOP( float ); break;
case IM_BANDFMT_DPCOMPLEX:FLOOP( double ); break;
default:
assert( 0 );
}
}
/**
* im_bandmean:
* @in: input #IMAGE
* @out: output #IMAGE
*
* im_bandmean() writes a one-band image where each pixel is the average of
* the bands for that pixel in the input image. The output band format is
* the same as the input band format. Integer types use round-to-nearest
* averaging.
*
* See also: im_add(), im_avg(), im_recomb()
*
* Returns: 0 on success, -1 on error
*/
int
im_bandmean( IMAGE *in, IMAGE *out )
{
/* Check input params
*/
if( in->Bands == 1 )
return( im_copy( in, out ) );
if( im_check_uncoded( "im_bandmean", in ) )
return( -1 );
/* Prepare output image.
*/
if( im_cp_desc( out, in ) )
return( -1 );
out->Bands = 1;
out->Type = IM_TYPE_B_W;
/* And process!
*/
if( im_wrapone( in, out,
(im_wrapone_fn) bandmean_buffer, in, NULL ) )
return( -1 );
return( 0 );
}

View File

@ -13,6 +13,8 @@ libconversion_la_SOURCES = \
cast.c \
bandjoin.c \
black.c \
recomb.c \
bandmean.c \
rot.c \
ifthenelse.c \
conver_dispatch.c \

View File

@ -0,0 +1,250 @@
/* im_bandmean.c
*
* Author: Simon Goodall
* Written on: 17/7/07
* 17/7/07 JC
* - hacked about a bit
* 18/8/09
* - gtkdoc
* - get rid of the complex case, just double the width
* 19/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 "conversion.h"
typedef struct _VipsBandmean {
VipsConversion parent_instance;
VipsImage *in;
} VipsBandmean;
typedef VipsConversionClass VipsBandmeanClass;
G_DEFINE_TYPE( VipsBandmean, vips_bandmean, VIPS_TYPE_CONVERSION );
/* Unsigned int types. Round, keep sum in a larger variable.
*/
#define UILOOP( TYPE, STYPE ) { \
TYPE *p = (TYPE *) in; \
TYPE *q = (TYPE *) out; \
\
for( i = 0; i < sz; i++ ) { \
STYPE sum; \
\
sum = 0; \
for( j = 0; j < bands; j++ ) \
sum += p[j]; \
q[i] = (sum + bands / 2) / bands; \
p += bands; \
} \
}
/* Signed int types. Round, keep sum in a larger variable.
*/
#define SILOOP( TYPE, STYPE ) { \
TYPE *p = (TYPE *) in; \
TYPE *q = (TYPE *) out; \
\
for( i = 0; i < sz; i++ ) { \
STYPE sum; \
\
sum = 0; \
for( j = 0; j < bands; j++ ) \
sum += p[j]; \
q[i] = sum > 0 ? \
(sum + bands / 2) / bands : \
(sum - bands / 2) / bands; \
p += bands; \
} \
}
/* Float loop. No rounding, sum in same container.
*/
#define FLOOP( TYPE ) { \
TYPE *p = (TYPE *) in; \
TYPE *q = (TYPE *) out; \
\
for( i = 0; i < sz; i++ ) { \
TYPE sum; \
\
sum = 0; \
for( j = 0; j < bands; j++ ) \
sum += p[j]; \
q[i] = sum / bands; \
p += bands; \
} \
}
static int
vips_bandmean_gen( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion *ir = (VipsRegion *) seq;
VipsBandmean *bandmean = (VipsBandmean *) b;
VipsImage *im = bandmean->in;
VipsRect *r = &or->valid;
const int bands = im->Bands;
const int sz = r->width *
(vips_bandfmt_iscomplex( im->BandFmt ) ? 2 : 1);
int y, i, j;
if( vips_region_prepare( ir, r ) )
return( -1 );
for( y = 0; y < r->height; y++ ) {
PEL *in = (PEL *) VIPS_REGION_ADDR( ir, r->left, r->top + y );
PEL *out = (PEL *) VIPS_REGION_ADDR( or, r->left, r->top + y );
switch( vips_image_get_format( im ) ) {
case VIPS_FORMAT_CHAR:
SILOOP( signed char, int ); break;
case VIPS_FORMAT_UCHAR:
UILOOP( unsigned char, unsigned int ); break;
case VIPS_FORMAT_SHORT:
SILOOP( signed short, int ); break;
case VIPS_FORMAT_USHORT:
UILOOP( unsigned short, unsigned int ); break;
case VIPS_FORMAT_INT:
SILOOP( signed int, int ); break;
case VIPS_FORMAT_UINT:
UILOOP( unsigned int, unsigned int ); break;
case VIPS_FORMAT_FLOAT:
FLOOP( float ); break;
case VIPS_FORMAT_DOUBLE:
FLOOP( double ); break;
case VIPS_FORMAT_COMPLEX:
FLOOP( float ); break;
case VIPS_FORMAT_DPCOMPLEX:
FLOOP( double ); break;
default:
g_assert( 0 );
}
}
return( 0 );
}
static int
vips_bandmean_build( VipsObject *object )
{
VipsConversion *conversion = (VipsConversion *) object;
VipsBandmean *bandmean = (VipsBandmean *) object;
if( VIPS_OBJECT_CLASS( vips_bandmean_parent_class )->build( object ) )
return( -1 );
if( vips_image_pio_input( bandmean->in ) ||
vips_check_uncoded( "VipsBandmean", bandmean->in ) )
return( -1 );
if( vips_image_copy_fields( conversion->out, bandmean->in ) )
return( -1 );
vips_demand_hint( conversion->out,
VIPS_DEMAND_STYLE_THINSTRIP, bandmean->in, NULL );
conversion->out->Bands = 1;
if( vips_image_generate( conversion->out,
vips_start_one, vips_bandmean_gen, vips_stop_one,
bandmean->in, bandmean ) )
return( -1 );
return( 0 );
}
static void
vips_bandmean_class_init( VipsBandmeanClass *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 = "bandmean";
object_class->description = _( "band-wise average" );
object_class->build = vips_bandmean_build;
VIPS_ARG_IMAGE( class, "in", 0,
_( "Input" ),
_( "Input image argument" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsBandmean, in ) );
}
static void
vips_bandmean_init( VipsBandmean *bandmean )
{
}
/**
* vips_bandmean:
* @in: input #IMAGE
* @out: output #IMAGE
* @...: %NULL-terminated list of optional named arguments
*
* This operation writes a one-band image where each pixel is the average of
* the bands for that pixel in the input image. The output band format is
* the same as the input band format. Integer types use round-to-nearest
* averaging.
*
* See also: vips_add(), vips_avg(), vips_recomb()
*
* Returns: 0 on success, -1 on error
*/
int
vips_bandmean( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "bandmean", ap, in, out );
va_end( ap );
return( result );
}

View File

@ -116,6 +116,8 @@ vips_conversion_operation_init( void )
extern GType vips_black_get_type( void );
extern GType vips_rot_get_type( void );
extern GType vips_ifthenelse_get_type( void );
extern GType vips_recomb_get_type( void );
extern GType vips_bandmean_get_type( void );
vips_copy_get_type();
vips_embed_get_type();
@ -130,6 +132,8 @@ vips_conversion_operation_init( void )
vips_black_get_type();
vips_rot_get_type();
vips_ifthenelse_get_type();
vips_recomb_get_type();
vips_bandmean_get_type();
}

View File

@ -51,13 +51,12 @@
#include <vips/vips.h>
#include "unary.h"
#include "conversion.h"
typedef struct _VipsRecomb {
VipsOperation parent_instance;
VipsConversion parent_instance;
VipsImage *in;
VipsImage *out;
VipsImage *m;
/* m converted to a one-band double.
@ -66,8 +65,9 @@ typedef struct _VipsRecomb {
} VipsRecomb;
typedef VipsOperationClass VipsRecombClass;
G_DEFINE_TYPE( VipsRecomb, vips_recomb, VIPS_TYPE_OPERATION );
typedef VipsConversionClass VipsRecombClass;
G_DEFINE_TYPE( VipsRecomb, vips_recomb, VIPS_TYPE_CONVERSION );
/* Inner loop.
*/
@ -139,6 +139,7 @@ vips_recomb_gen( VipsRegion *or,
static int
vips_recomb_build( VipsObject *object )
{
VipsConversion *conversion = (VipsConversion *) object;
VipsRecomb *recomb = (VipsRecomb *) object;
VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 );
@ -165,18 +166,16 @@ vips_recomb_build( VipsObject *object )
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 ) )
if( vips_image_copy_fields( conversion->out, recomb->in ) )
return( -1 );
vips_demand_hint( recomb->out,
vips_demand_hint( conversion->out,
VIPS_DEMAND_STYLE_THINSTRIP, recomb->in, NULL );
recomb->out->Bands = recomb->m->Ysize;
conversion->out->Bands = recomb->m->Ysize;
if( vips_bandfmt_isint( recomb->in->BandFmt ) )
recomb->out->BandFmt = VIPS_FORMAT_FLOAT;
conversion->out->BandFmt = VIPS_FORMAT_FLOAT;
if( vips_image_generate( recomb->out,
if( vips_image_generate( conversion->out,
vips_start_one, vips_recomb_gen, vips_stop_one,
recomb->in, recomb ) )
return( -1 );
@ -197,25 +196,17 @@ vips_recomb_class_init( VipsRecombClass *class )
object_class->description = _( "linear recombination with matrix" );
object_class->build = vips_recomb_build;
VIPS_ARG_IMAGE( class, "in", 1,
VIPS_ARG_IMAGE( class, "in", 0,
_( "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
@ -228,6 +219,7 @@ vips_recomb_init( VipsRecomb *recomb )
* @in: input image
* @out: output image
* @m: recombination matrix
* @...: %NULL-terminated list of optional named arguments
*
* 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

View File

@ -1476,6 +1476,23 @@ im_abs( IMAGE *in, IMAGE *out )
return( 0 );
}
int
im_bandmean( IMAGE *in, IMAGE *out )
{
VipsImage *t;
if( vips_bandmean( in, &t,
NULL ) )
return( -1 );
if( vips_image_write( t, out ) ) {
g_object_unref( t );
return( -1 );
}
g_object_unref( t );
return( 0 );
}
int
im_lintra( double a, IMAGE *in, double b, IMAGE *out )
{
@ -2105,3 +2122,5 @@ im_c2rect( IMAGE *in, IMAGE *out )
{
return( vips__complex( in, out, VIPS_OPERATION_COMPLEX_RECT ) );
}

View File

@ -323,9 +323,6 @@ int vips_pow_const1( VipsImage *in, VipsImage **out, double c, ... )
int vips_wop_const1( VipsImage *in, VipsImage **out, double c, ... )
__attribute__((sentinel));
int vips_recomb( VipsImage *in, VipsImage **out, VipsImage *m, ... )
__attribute__((sentinel));
int vips_avg( VipsImage *in, double *out, ... )
__attribute__((sentinel));
int vips_deviate( VipsImage *in, double *out, ... )
@ -345,7 +342,6 @@ int vips_measure( VipsImage *in, VipsImage **out, int h, int v, ... )
int im_maxpos_avg( VipsImage *im, double *xpos, double *ypos, double *out );
int im_maxpos_vec( VipsImage *im, int *xpos, int *ypos, double *maxima, int n );
int im_minpos_vec( VipsImage *im, int *xpos, int *ypos, double *minima, int n );
int im_bandmean( VipsImage *in, VipsImage *out );
int im_linreg( VipsImage **ins, VipsImage *out, double *xs );
int im_point( VipsImage *im, VipsInterpolate *interpolate,

View File

@ -183,6 +183,10 @@ int vips_bandjoin( VipsImage **in, VipsImage **out, int n, ... )
__attribute__((sentinel));
int vips_bandjoin2( VipsImage *in1, VipsImage *in2, VipsImage **out, ... )
__attribute__((sentinel));
int vips_bandmean( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_recomb( VipsImage *in, VipsImage **out, VipsImage *m, ... )
__attribute__((sentinel));
int vips_black( VipsImage **out, int width, int height, ... )
__attribute__((sentinel));
int vips_rot( VipsImage *in, VipsImage **out, VipsAngle angle, ... )

View File

@ -540,7 +540,6 @@ 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 );
@ -644,6 +643,8 @@ int im_blend( VipsImage *c, VipsImage *a, VipsImage *b, VipsImage *out );
DOUBLEMASK *im_vips2mask( VipsImage *in, const char *filename );
int im_mask2vips( DOUBLEMASK *in, VipsImage *out );
int im_bandmean( VipsImage *in, VipsImage *out );
int im_recomb( VipsImage *in, VipsImage *out, DOUBLEMASK *recomb );
/* ruby-vips uses this

View File

@ -261,7 +261,7 @@ vips_image_dispose( GObject *gobject )
if( image->baseaddr ) {
/* MMAP file.
*/
VIPS_DEBUG_MSG( "vips_image_finalize: unmapping file\n" );
VIPS_DEBUG_MSG( "vips_image_dispose: unmapping file\n" );
vips__munmap( image->baseaddr, image->length );
image->baseaddr = NULL;
@ -276,10 +276,11 @@ vips_image_dispose( GObject *gobject )
/* Is there a file descriptor?
*/
if( image->fd != -1 ) {
VIPS_DEBUG_MSG( "vips_image_finalize: closing output file\n" );
VIPS_DEBUG_MSG( "vips_image_dispose: closing output file\n" );
if( image->dtype == VIPS_IMAGE_OPENOUT )
(void) vips__writehist( image );
if( vips_tracked_close( image->fd ) == -1 )
vips_error( "VipsImage",
"%s", _( "unable to close fd" ) );

View File

@ -318,6 +318,10 @@ vips_leak( void )
void
vips_shutdown( void )
{
#ifdef DEBUG
printf( "vips_shutdown:\n" );
#endif /*DEBUG*/
vips_cache_drop_all();
im_close_plugins();

View File

@ -380,6 +380,8 @@ vips_tracked_open( const char *pathname, int flags, ... )
int
vips_tracked_close( int fd )
{
int result;
g_mutex_lock( vips_tracked_mutex );
g_assert( vips_tracked_files > 0 );
@ -391,7 +393,9 @@ vips_tracked_close( int fd )
g_mutex_unlock( vips_tracked_mutex );
return( close( fd ) );
result = close( fd );
return( result );
}
/**

View File

@ -13,9 +13,8 @@ bname=`basename $0`
if [[ $# < 1 ]]; then
echo "usage: $bname [command ...]"
echo "examples:"
echo " $bname nip"
echo " $bname man im_invert"
echo " $bname im_invert /pics/tmp/fred.jpg /pics/tmp/fred2.tif"
echo " $bname vips im_invert /pics/tmp/fred.jpg /pics/tmp/fred2.tif"
exit 1
fi