redo binary relational as a class

This commit is contained in:
John Cupitt 2011-11-11 13:45:12 +00:00
parent c0399f8e84
commit 26ae049d91
12 changed files with 387 additions and 186 deletions

View File

@ -9,7 +9,8 @@
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_measure(), im_recomb(), im_floor(), im_ceil(),
im_rint()
im_rint(), im_equal(), im_notequal(), im_less(), im_lesseq(), im_more(),
im_moreeq()
redone as classes
- added argument priorites to help control arg ordering
- generate has a 'stop' param to signal successful early termination

2
TODO
View File

@ -1,5 +1,7 @@
- test _O_TEMPORARY thing on Windows
* See also: #VipsBoolean, #VipsRelationalConst.
- avg/dev etc. should uncode images? eg. labq2lab etc.

View File

@ -30,6 +30,7 @@ libarithmetic_la_SOURCES = \
binary.h \
unary.c \
unary.h \
relational.c \
add.c \
linear.c \
invert.c \

View File

@ -518,6 +518,7 @@ vips_arithmetic_operation_init( 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 );
vips_add_get_type();
vips_subtract_get_type();
@ -536,4 +537,5 @@ vips_arithmetic_operation_init( void )
vips_measure_get_type();
vips_recomb_get_type();
vips_round_get_type();
vips_relational_get_type();
}

View File

@ -0,0 +1,265 @@
/* relational.c --- various relational operations
*
* Modified:
* 26/7/93 JC
* - >,<,>=,<= tests now as (double) to prevent compiler warnings. Should
* split into int/float cases really for speed.
* 25/1/95 JC
* - partialized
* - updated
* 7/2/95 JC
* - oops! bug with doubles fixed
* 3/7/98 JC
* - vector versions added ... im_equal_vec(), im_lesseq_vec() etc
* - small tidies
* - should be a bit faster, lots of *q++ changed to q[x]
* 10/3/03 JC
* - reworked to remove nested #defines: a bit slower, but much smaller
* - all except _vec forms now work on complex
* 31/7/03 JC
* - oops, relational_format was broken for some combinations
* 23/9/09
* - gtkdoc
* - use new im__arith_binary*() functions
* - more meta-programming
* 23/6/10
* - oops, moreconst and moreeqconst were the same
* 4/11/11
* - redone 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 <vips/vips.h>
#include "arithmetic.h"
#include "binary.h"
/**
* VipsRelational:
* @in: input #VipsImage
* @out: output #VipsImage
* @relational: relational operation to perform
*
* Perform various relational operations on pairs of images.
*
* The output type is always uchar, with 0 for FALSE and 255 for TRUE.
*
* Less-than and greater-than for complex images compare the modulus.
*
* If the images differ in size, the smaller image is enlarged to match the
* larger by adding zero pixels along the bottom and right.
*
* If the number of bands differs, one of the images
* must have one band. In this case, an n-band image is formed from the
* one-band image by joining n copies of the one-band image together, and then
* the two n-band images are operated upon.
*
* The two input images are cast up to the smallest common type (see table
* Smallest common format in
* <link linkend="VIPS-arithmetic">arithmetic</link>).
*
* See also: #VipsBoolean, #VipsRelationalConst.
*/
typedef struct _VipsRelational {
VipsBinary parent_instance;
VipsOperationRelational relational;
} VipsRelational;
typedef VipsBinaryClass VipsRelationalClass;
G_DEFINE_TYPE( VipsRelational, vips_relational, VIPS_TYPE_BINARY );
static int
vips_relational_build( VipsObject *object )
{
VipsRelational *relational = (VipsRelational *) object;
VipsArithmetic *arithmetic = (VipsArithmetic *) object;
if( relational->relational == VIPS_OPERATION_RELATIONAL_MORE ) {
relational->relational = VIPS_OPERATION_RELATIONAL_LESS;
VIPS_SWAP( VipsImage *,
arithmetic->ready[0], arithmetic->ready[1] );
}
if( relational->relational == VIPS_OPERATION_RELATIONAL_MOREEQ ) {
relational->relational = VIPS_OPERATION_RELATIONAL_LESSEQ;
VIPS_SWAP( VipsImage *,
arithmetic->ready[0], arithmetic->ready[1] );
}
if( VIPS_OBJECT_CLASS( vips_relational_parent_class )->build( object ) )
return( -1 );
return( 0 );
}
#define RLOOP( TYPE, ROP ) { \
TYPE *left = (TYPE *) in[0]; \
TYPE *right = (TYPE *) in[1]; \
PEL *q = (PEL *) out; \
\
for( x = 0; x < sz; x++ ) \
q[x] = (left[x] ROP right[x]) ? 255 : 0; \
}
#define CLOOP( TYPE, COP ) { \
TYPE *left = (TYPE *) in[0]; \
TYPE *right = (TYPE *) in[1]; \
PEL *q = (PEL *) out; \
\
for( x = 0; x < sz; x++ ) { \
q[x] = COP( left[0], left[1], right[0], right[1]) ? 255 : 0; \
\
left += 2; \
right += 2; \
} \
}
#define SWITCH( ROP, COP ) \
switch( vips_image_get_format( im ) ) { \
case VIPS_FORMAT_UCHAR: RLOOP( unsigned char, ROP ); break; \
case VIPS_FORMAT_CHAR: RLOOP( signed char, ROP ); break; \
case VIPS_FORMAT_USHORT: RLOOP( unsigned short, ROP ); break; \
case VIPS_FORMAT_SHORT: RLOOP( signed short, ROP ); break; \
case VIPS_FORMAT_UINT: RLOOP( unsigned int, ROP ); break; \
case VIPS_FORMAT_INT: RLOOP( signed int, ROP ); break; \
case VIPS_FORMAT_FLOAT: RLOOP( float, ROP ); break; \
case VIPS_FORMAT_DOUBLE: RLOOP( double, ROP ); break;\
case VIPS_FORMAT_COMPLEX: CLOOP( float, COP ); break; \
case VIPS_FORMAT_DPCOMPLEX: CLOOP( double, COP ); break;\
\
default: \
g_assert( 0 ); \
}
#define CEQUAL( x1, y1, x2, y2 ) (x1 == y1 && x2 == y2)
#define CNOTEQUAL( x1, y1, x2, y2 ) (x1 != y1 || x2 != y2)
#define CLESS( x1, y1, x2, y2 ) (x1 * x1 + y1 * y1 < x2 * x2 + y2 * y2)
#define CLESSEQ( x1, y1, x2, y2 ) (x1 * x1 + y1 * y1 <= x2 * x2 + y2 * y2)
static void
vips_relational_buffer( VipsArithmetic *arithmetic,
PEL *out, PEL **in, int width )
{
VipsRelational *relational = (VipsRelational *) arithmetic;
VipsImage *im = arithmetic->ready[0];
const int sz = width * vips_image_get_bands( im );
int x;
switch( relational->relational ) {
case VIPS_OPERATION_RELATIONAL_EQUAL: SWITCH( ==, CEQUAL ); break;
case VIPS_OPERATION_RELATIONAL_NOTEQUAL:SWITCH( !=, CNOTEQUAL ); break;
case VIPS_OPERATION_RELATIONAL_LESS: SWITCH( <, CLESS ); break;
case VIPS_OPERATION_RELATIONAL_LESSEQ: SWITCH( <=, CLESSEQ ); break;
default:
g_assert( 0 );
}
}
/* Save a bit of typing.
*/
#define UC VIPS_FORMAT_UCHAR
#define C VIPS_FORMAT_CHAR
#define US VIPS_FORMAT_USHORT
#define S VIPS_FORMAT_SHORT
#define UI VIPS_FORMAT_UINT
#define I VIPS_FORMAT_INT
#define F VIPS_FORMAT_FLOAT
#define X VIPS_FORMAT_COMPLEX
#define D VIPS_FORMAT_DOUBLE
#define DX VIPS_FORMAT_DPCOMPLEX
static const VipsBandFormat vips_bandfmt_relational[10] = {
/* UC C US S UI I F X D DX */
UC, UC, UC, UC, UC, UC, UC, UC, UC, UC
};
static void
vips_relational_class_init( VipsRelationalClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsArithmeticClass *aclass = VIPS_ARITHMETIC_CLASS( class );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "relational";
object_class->description =
_( "a relational operation on a pair of images" );
object_class->build = vips_relational_build;
vips_arithmetic_set_format_table( aclass, vips_bandfmt_relational );
aclass->process_line = vips_relational_buffer;
VIPS_ARG_ENUM( class, "relational", 200,
_( "Operation" ),
_( "relational to perform" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsRelational, relational ),
VIPS_TYPE_OPERATION_RELATIONAL,
VIPS_OPERATION_RELATIONAL_EQUAL );
}
static void
vips_relational_init( VipsRelational *relational )
{
}
int
vips_relational( VipsImage *left, VipsImage *right, VipsImage **out,
VipsOperationRelational relational, ... )
{
va_list ap;
int result;
va_start( ap, relational );
result = vips_call_split( "relational", ap, left, right, out,
relational );
va_end( ap );
return( result );
}

View File

@ -1680,3 +1680,63 @@ im_ceil( IMAGE *in, IMAGE *out )
{
return( vips__round( in, out, VIPS_OPERATION_ROUND_CEIL ) );
}
static int
vips__relational( IMAGE *in1, IMAGE *in2, IMAGE *out,
VipsOperationRelational relational )
{
VipsImage *t;
if( vips_relational( in1, in2, &t, relational,
NULL ) )
return( -1 );
if( vips_image_write( t, out ) ) {
g_object_unref( t );
return( -1 );
}
g_object_unref( t );
return( 0 );
}
int
im_equal( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( vips__relational( in1, in2, out,
VIPS_OPERATION_RELATIONAL_EQUAL ) );
}
int
im_notequal( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( vips__relational( in1, in2, out,
VIPS_OPERATION_RELATIONAL_NOTEQUAL ) );
}
int
im_less( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( vips__relational( in1, in2, out,
VIPS_OPERATION_RELATIONAL_LESS ) );
}
int
im_lesseq( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( vips__relational( in1, in2, out,
VIPS_OPERATION_RELATIONAL_LESSEQ ) );
}
int
im_more( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( vips__relational( in1, in2, out,
VIPS_OPERATION_RELATIONAL_MORE ) );
}
int
im_moreeq( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( vips__relational( in1, in2, out,
VIPS_OPERATION_RELATIONAL_MOREEQ ) );
}

View File

@ -81,6 +81,27 @@ typedef enum {
VIPS_OPERATION_ROUND_LAST
} VipsOperationRound;
/**
* VipsOperationRelational:
* @VIPS_OPERATION_RELATIONAL_EQUAL: ==
* @VIPS_OPERATION_RELATIONAL_NOTEQUAL: !=
* @VIPS_OPERATION_RELATIONAL_LESS: <
* @VIPS_OPERATION_RELATIONAL_LESSEQ: <=
* @VIPS_OPERATION_RELATIONAL_MORE: >
* @VIPS_OPERATION_RELATIONAL_MOREEQ: >=
*
* See also: vips_relational().
*/
typedef enum {
VIPS_OPERATION_RELATIONAL_EQUAL,
VIPS_OPERATION_RELATIONAL_NOTEQUAL,
VIPS_OPERATION_RELATIONAL_LESS,
VIPS_OPERATION_RELATIONAL_LESSEQ,
VIPS_OPERATION_RELATIONAL_MORE,
VIPS_OPERATION_RELATIONAL_MOREEQ,
VIPS_OPERATION_RELATIONAL_LAST
} VipsOperationRelational;
int vips_add( VipsImage *left, VipsImage *right, VipsImage **out, ... )
__attribute__((sentinel));
int vips_subtract( VipsImage *in1, VipsImage *in2, VipsImage **out, ... )
@ -119,6 +140,9 @@ int vips_recomb( VipsImage *in, VipsImage **out, VipsImage *m, ... )
__attribute__((sentinel));
int vips_round( VipsImage *in, VipsImage **out, VipsOperationRound round, ... )
__attribute__((sentinel));
int vips_relational( VipsImage *left, VipsImage *right, VipsImage **out,
VipsOperationRelational relational, ... )
__attribute__((sentinel));

View File

@ -11,6 +11,8 @@ GType vips_operation_math_get_type (void) G_GNUC_CONST;
#define VIPS_TYPE_OPERATION_MATH (vips_operation_math_get_type())
GType vips_operation_round_get_type (void) G_GNUC_CONST;
#define VIPS_TYPE_OPERATION_ROUND (vips_operation_round_get_type())
GType vips_operation_relational_get_type (void) G_GNUC_CONST;
#define VIPS_TYPE_OPERATION_RELATIONAL (vips_operation_relational_get_type())
/* enumerations from "../../../libvips/include/vips/conversion.h" */
GType vips_extend_get_type (void) G_GNUC_CONST;
#define VIPS_TYPE_EXTEND (vips_extend_get_type())

View File

@ -37,12 +37,6 @@
extern "C" {
#endif /*__cplusplus*/
int im_equal( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_notequal( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_less( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_lesseq( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_more( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_moreeq( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_equal_vec( VipsImage *in, VipsImage *out, int n, double *c );
int im_notequal_vec( VipsImage *in, VipsImage *out, int n, double *c );
int im_less_vec( VipsImage *in, VipsImage *out, int n, double *c );

View File

@ -557,6 +557,13 @@ int im_floor( VipsImage *in, VipsImage *out );
int im_rint( VipsImage *in, VipsImage *out );
int im_ceil( VipsImage *in, VipsImage *out );
int im_equal( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_notequal( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_less( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_lesseq( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_more( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_moreeq( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_copy( VipsImage *in, VipsImage *out );
int im_copy_set( VipsImage *in, VipsImage *out,
VipsInterpretation interpretation,

View File

@ -129,6 +129,28 @@ vips_operation_round_get_type( void )
return( etype );
}
GType
vips_operation_relational_get_type( void )
{
static GType etype = 0;
if( etype == 0 ) {
static const GEnumValue values[] = {
{VIPS_OPERATION_RELATIONAL_EQUAL, "VIPS_OPERATION_RELATIONAL_EQUAL", "equal"},
{VIPS_OPERATION_RELATIONAL_NOTEQUAL, "VIPS_OPERATION_RELATIONAL_NOTEQUAL", "notequal"},
{VIPS_OPERATION_RELATIONAL_LESS, "VIPS_OPERATION_RELATIONAL_LESS", "less"},
{VIPS_OPERATION_RELATIONAL_LESSEQ, "VIPS_OPERATION_RELATIONAL_LESSEQ", "lesseq"},
{VIPS_OPERATION_RELATIONAL_MORE, "VIPS_OPERATION_RELATIONAL_MORE", "more"},
{VIPS_OPERATION_RELATIONAL_MOREEQ, "VIPS_OPERATION_RELATIONAL_MOREEQ", "moreeq"},
{VIPS_OPERATION_RELATIONAL_LAST, "VIPS_OPERATION_RELATIONAL_LAST", "last"},
{0, NULL, NULL}
};
etype = g_enum_register_static( "VipsOperationRelational", values );
}
return( etype );
}
/* enumerations from "../../libvips/include/vips/util.h" */
GType
vips_token_get_type( void )

View File

@ -72,51 +72,6 @@ static int bandfmt_relational[10] = {
UC, UC, UC, UC, UC, UC, UC, UC, UC, UC,
};
#define RBINARY( IN, FUN ) { \
IN *tp1 = (IN *) p[0]; \
IN *tp2 = (IN *) p[1]; \
\
for( i = 0; i < ne; i++ ) \
FUN( q[i], tp1[i], tp2[i] ); \
}
#define CBINARY( IN, FUN ) { \
IN *tp1 = (IN *) p[0]; \
IN *tp2 = (IN *) p[1]; \
\
for( i = 0; i < ne; i++ ) { \
FUN( q[i], tp1, tp2 ); \
\
tp1 += 2; \
tp2 += 2; \
} \
}
#define BINARY_BUFFER( NAME, RFUN, CFUN ) \
static void \
NAME ## _buffer( PEL **p, PEL *q, int n, IMAGE *im ) \
{ \
const int ne = n * im->Bands; \
\
int i; \
\
switch( im->BandFmt ) { \
case IM_BANDFMT_CHAR: RBINARY( signed char, RFUN ); break; \
case IM_BANDFMT_UCHAR: RBINARY( unsigned char, RFUN ); break; \
case IM_BANDFMT_SHORT: RBINARY( signed short, RFUN ); break; \
case IM_BANDFMT_USHORT: RBINARY( unsigned short, RFUN ); break; \
case IM_BANDFMT_INT: RBINARY( signed int, RFUN ); break; \
case IM_BANDFMT_UINT: RBINARY( unsigned int, RFUN ); break; \
case IM_BANDFMT_FLOAT: RBINARY( float, RFUN ); break; \
case IM_BANDFMT_COMPLEX: CBINARY( float, CFUN ); break; \
case IM_BANDFMT_DOUBLE: RBINARY( double, RFUN ); break; \
case IM_BANDFMT_DPCOMPLEX: CBINARY( double, CFUN ); break; \
\
default: \
g_assert( 0 ); \
} \
}
#define EQUAL_REAL( Q, A, B ) { \
if( (A) == (B) ) \
Q = 255; \
@ -131,30 +86,6 @@ NAME ## _buffer( PEL **p, PEL *q, int n, IMAGE *im ) \
Q = 0; \
}
BINARY_BUFFER( EQUAL, EQUAL_REAL, EQUAL_COMPLEX )
/**
* im_equal:
* @in1: input #IMAGE 1
* @in2: input #IMAGE 2
* @out: output #IMAGE
*
* This operation calculates @in1 == @in2 (image element equals image element)
* and writes the result to @out.
*
* See also: im_notequal().
*
* Returns: 0 on success, -1 on error
*/
int
im_equal( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( im__arith_binary( "im_equal",
in1, in2, out,
bandfmt_relational,
(im_wrapmany_fn) EQUAL_buffer, NULL ) );
}
#define NOTEQUAL_REAL( Q, A, B ) { \
if( (A) != (B) ) \
Q = 255; \
@ -169,30 +100,6 @@ im_equal( IMAGE *in1, IMAGE *in2, IMAGE *out )
Q = 0; \
}
BINARY_BUFFER( NOTEQUAL, NOTEQUAL_REAL, NOTEQUAL_COMPLEX )
/**
* im_notequal:
* @in1: input #IMAGE 1
* @in2: input #IMAGE 2
* @out: output #IMAGE
*
* This operation calculates @in1 != @in2 (image element does not equal image
* element) and writes the result to @out.
*
* See also: im_notequal().
*
* Returns: 0 on success, -1 on error
*/
int
im_notequal( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( im__arith_binary( "im_notequal",
in1, in2, out,
bandfmt_relational,
(im_wrapmany_fn) NOTEQUAL_buffer, NULL ) );
}
#define LESS_REAL( Q, A, B ) { \
if( (A) < (B) ) \
Q = 255; \
@ -210,30 +117,6 @@ im_notequal( IMAGE *in1, IMAGE *in2, IMAGE *out )
Q = 0; \
}
BINARY_BUFFER( LESS, LESS_REAL, LESS_COMPLEX )
/**
* im_less:
* @in1: input #IMAGE 1
* @in2: input #IMAGE 2
* @out: output #IMAGE
*
* This operation calculates @in1 < @in2 (image element is less than image
* element) and writes the result to @out.
*
* See also: im_more().
*
* Returns: 0 on success, -1 on error
*/
int
im_less( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( im__arith_binary( "im_less",
in1, in2, out,
bandfmt_relational,
(im_wrapmany_fn) LESS_buffer, NULL ) );
}
#define LESSEQ_REAL( Q, A, B ) { \
if( (A) <= (B) ) \
Q = 255; \
@ -251,68 +134,6 @@ im_less( IMAGE *in1, IMAGE *in2, IMAGE *out )
Q = 0; \
}
BINARY_BUFFER( LESSEQ, LESSEQ_REAL, LESSEQ_COMPLEX )
/**
* im_lesseq:
* @in1: input #IMAGE 1
* @in2: input #IMAGE 2
* @out: output #IMAGE
*
* This operation calculates @in1 <= @in2 (image element is less than or equal
* to image elemment) and writes the result to @out.
*
* See also: im_more().
*
* Returns: 0 on success, -1 on error
*/
int
im_lesseq( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( im__arith_binary( "im_lesseq",
in1, in2, out,
bandfmt_relational,
(im_wrapmany_fn) LESSEQ_buffer, NULL ) );
}
/**
* im_more:
* @in1: input #IMAGE 1
* @in2: input #IMAGE 2
* @out: output #IMAGE
*
* This operation calculates @in1 > @in2 (image element is greater than
* image elemment) and writes the result to @out.
*
* See also: im_less().
*
* Returns: 0 on success, -1 on error
*/
int
im_more( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( im_less( in2, in1, out ) );
}
/**
* im_moreeq:
* @in1: input #IMAGE 1
* @in2: input #IMAGE 2
* @out: output #IMAGE
*
* This operation calculates @in1 >= @in2 (image element is greater than or
* equal to image element) and writes the result to @out.
*
* See also: im_more().
*
* Returns: 0 on success, -1 on error
*/
int
im_moreeq( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
return( im_lesseq( in2, in1, out ) );
}
#define RCONST1( IN, FUN ) { \
IN *tp = (IN *) p; \
IN tc = *((IN *) vector); \