diff --git a/ChangeLog b/ChangeLog index de1b1aa3..684dfc03 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,7 +10,7 @@ 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_equal*(), im_notequal*(), im_less*(), im_lesseq*(), im_more*(), - im_moreeq*(), im_remainder*() + im_moreeq*(), im_remainder*(), im_and*(), im_or*(), im_eor*() redone as classes - added argument priorites to help control arg ordering - generate has a 'stop' param to signal successful early termination diff --git a/libvips/arithmetic/Makefile.am b/libvips/arithmetic/Makefile.am index 244dce9b..781d1409 100644 --- a/libvips/arithmetic/Makefile.am +++ b/libvips/arithmetic/Makefile.am @@ -33,6 +33,7 @@ libarithmetic_la_SOURCES = \ unaryconst.c \ unaryconst.h \ relational.c \ + boolean.c \ add.c \ linear.c \ invert.c \ diff --git a/libvips/arithmetic/arithmetic.c b/libvips/arithmetic/arithmetic.c index cdcd79d1..ab7758ab 100644 --- a/libvips/arithmetic/arithmetic.c +++ b/libvips/arithmetic/arithmetic.c @@ -522,6 +522,8 @@ vips_arithmetic_operation_init( void ) extern GType vips_relational_const_get_type( void ); extern GType vips_remainder_get_type( void ); extern GType vips_remainder_const_get_type( void ); + extern GType vips_boolean_get_type( void ); + extern GType vips_boolean_const_get_type( void ); vips_add_get_type(); vips_subtract_get_type(); @@ -544,4 +546,6 @@ vips_arithmetic_operation_init( void ) vips_relational_const_get_type(); vips_remainder_get_type(); vips_remainder_const_get_type(); + vips_boolean_get_type(); + vips_boolean_const_get_type(); } diff --git a/libvips/arithmetic/boolean.c b/libvips/arithmetic/boolean.c new file mode 100644 index 00000000..182ad539 --- /dev/null +++ b/libvips/arithmetic/boolean.c @@ -0,0 +1,447 @@ +/* boolean.c --- various bit operations + * + * Modified: + * 15/12/94 JC + * - ANSIfied + * - adapted to partials with im_wrap... + * 25/1/95 JC + * - added check1ary(), check2ary() + * 8/2/95 JC + * - new im_wrapmany + * 19/7/95 JC + * - added im_shiftleft() and im_shiftright() + * 6/7/98 JC + * - added _vec forms + * - removed *p++ stuff + * 10/9/99 JC + * - and/or/eor now do all int types + * 10/10/02 JC + * - renamed im_and() etc. as im_andimage() to remove breakage in the C++ + * layer if operator names are turned on + * 30/6/04 + * - now cast float/complex args to int + * 11/9/09 + * - use new im__cast_and__call() + * - therefore now supports 1-band $op n-band + * 17/9/09 + * - moved to im__arith_binary*() + * - renamed im_eor_vec() as im_eorimage_vec() for C++ sanity + * 21/11/10 + * - oop, constants are always (int) now, so (^-1) works for unsigned + * types + * 12/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 +#endif /*HAVE_CONFIG_H*/ +#include + +#include +#include + +#include + +#include "binary.h" +#include "unaryconst.h" + +/** + * VipsBoolean: + * @left: left-hand input #VipsImage + * @right: right-hand input #VipsImage + * @out: output #VipsImage + * @boolean: boolean operation to perform + * + * Perform various boolean operations on pairs of images. + * + * The output image is the same format as the upcast input images for integer + * types. Float types are cast to int before processing. Complex types are not + * supported. + * + * 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 + * arithmetic). + * + * See also: #VipsBoolean, #VipsBooleanConst. + */ + +typedef struct _VipsBoolean { + VipsBinary parent_instance; + + VipsOperationBoolean boolean; + +} VipsBoolean; + +typedef VipsBinaryClass VipsBooleanClass; + +G_DEFINE_TYPE( VipsBoolean, vips_boolean, VIPS_TYPE_BINARY ); + +static int +vips_boolean_build( VipsObject *object ) +{ + VipsBinary *binary = (VipsBinary *) object; + + if( binary->left && + vips_check_noncomplex( "VipsBoolean", binary->left ) ) + return( -1 ); + if( binary->right && + vips_check_noncomplex( "VipsBoolean", binary->right ) ) + return( -1 ); + + if( VIPS_OBJECT_CLASS( vips_boolean_parent_class )->build( object ) ) + return( -1 ); + + return( 0 ); +} + +#define LOOP( TYPE, OP ) { \ + TYPE *left = (TYPE *) in[0]; \ + TYPE *right = (TYPE *) in[1]; \ + TYPE *q = (TYPE *) out; \ + \ + for( x = 0; x < sz; x++ ) \ + q[x] = left[x] OP right[x]; \ +} + +#define FLOOP( TYPE, OP ) { \ + TYPE *left = (TYPE *) in[0]; \ + TYPE *right = (TYPE *) in[1]; \ + int *q = (int *) out; \ + \ + for( x = 0; x < sz; x++ ) \ + q[x] = ((int) left[x]) OP ((int) right[x]); \ +} + +#define SWITCH( I, F, OP ) \ + switch( vips_image_get_format( im ) ) { \ + case VIPS_FORMAT_UCHAR: I( unsigned char, OP ); break; \ + case VIPS_FORMAT_CHAR: I( signed char, OP ); break; \ + case VIPS_FORMAT_USHORT: I( unsigned short, OP ); break; \ + case VIPS_FORMAT_SHORT: I( signed short, OP ); break; \ + case VIPS_FORMAT_UINT: I( unsigned int, OP ); break; \ + case VIPS_FORMAT_INT: I( signed int, OP ); break; \ + case VIPS_FORMAT_FLOAT: F( float, OP ); break; \ + case VIPS_FORMAT_DOUBLE: F( double, OP ); break;\ + \ + default: \ + g_assert( 0 ); \ + } + +static void +vips_boolean_buffer( VipsArithmetic *arithmetic, + PEL *out, PEL **in, int width ) +{ + VipsBoolean *boolean = (VipsBoolean *) arithmetic; + VipsImage *im = arithmetic->ready[0]; + const int sz = width * vips_image_get_bands( im ); + + int x; + + switch( boolean->boolean ) { + case VIPS_OPERATION_BOOLEAN_AND: + SWITCH( LOOP, FLOOP, & ); + break; + + case VIPS_OPERATION_BOOLEAN_OR: + SWITCH( LOOP, FLOOP, | ); + break; + + case VIPS_OPERATION_BOOLEAN_EOR: + SWITCH( LOOP, FLOOP, ^ ); + 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 + +/* Type conversions for boolean. + */ +static const VipsBandFormat vips_bandfmt_boolean[10] = { +/* UC C US S UI I F X D DX */ + UC, C, US, S, UI, I, I, I, I, I, +}; + +static void +vips_boolean_class_init( VipsBooleanClass *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 = "boolean"; + object_class->description = + _( "a boolean operation on a pair of images" ); + object_class->build = vips_boolean_build; + + vips_arithmetic_set_format_table( aclass, vips_bandfmt_boolean ); + + aclass->process_line = vips_boolean_buffer; + + VIPS_ARG_ENUM( class, "boolean", 200, + _( "Operation" ), + _( "boolean to perform" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsBoolean, boolean ), + VIPS_TYPE_OPERATION_BOOLEAN, + VIPS_OPERATION_BOOLEAN_AND ); +} + +static void +vips_boolean_init( VipsBoolean *boolean ) +{ +} + +int +vips_boolean( VipsImage *left, VipsImage *right, VipsImage **out, + VipsOperationBoolean boolean, ... ) +{ + va_list ap; + int result; + + va_start( ap, boolean ); + result = vips_call_split( "boolean", ap, left, right, out, + boolean ); + va_end( ap ); + + return( result ); +} + +/** + * VipsBooleanConst: + * @in: input image + * @out: output image + * @a: array of constants + * @boolean: boolean operation to perform + * + * Perform various boolean operations on an image against a constant. + * + * The output type is always uchar, with 0 for FALSE and 255 for TRUE. + * + * If the array of constants has just one element, that constant is used for + * all image bands. If the array has more than one element and they have + * the same number of elements as there are bands in the image, then + * one array element is used for each band. If the arrays have more than one + * element and the image only has a single band, the result is a many-band + * image where each band corresponds to one array element. + * + * See also: #VipsBoolean, #VipsBoolean. + */ + +typedef struct _VipsBooleanConst { + VipsUnaryConst parent_instance; + + VipsOperationBoolean boolean; +} VipsBooleanConst; + +typedef VipsUnaryConstClass VipsBooleanConstClass; + +G_DEFINE_TYPE( VipsBooleanConst, + vips_boolean_const, VIPS_TYPE_UNARY_CONST ); + +static int +vips_boolean_const_build( VipsObject *object ) +{ + VipsUnary *unary = (VipsUnary *) object; + VipsUnaryConst *uconst = (VipsUnaryConst *) object; + + if( unary->in && + vips_check_noncomplex( "VipsBoolean", unary->in ) ) + return( -1 ); + + uconst->const_format = VIPS_FORMAT_INT; + + if( VIPS_OBJECT_CLASS( vips_boolean_const_parent_class )-> + build( object ) ) + return( -1 ); + + return( 0 ); +} + +#define LOOPC( TYPE, OP ) { \ + TYPE *p = (TYPE *) in[0]; \ + TYPE *q = (TYPE *) out; \ + int *c = (int *) uconst->c_ready; \ + \ + for( i = 0, x = 0; x < width; x++ ) \ + for( b = 0; b < bands; b++, i++ ) \ + q[i] = p[i] OP c[b]; \ +} + +#define FLOOPC( TYPE, OP ) { \ + TYPE *p = (TYPE *) in[0]; \ + int *q = (int *) out; \ + int *c = (int *) uconst->c_ready; \ + \ + for( i = 0, x = 0; x < width; x++ ) \ + for( b = 0; b < bands; b++, i++ ) \ + q[i] = ((int) p[i]) OP ((int) c[b]); \ +} + +static void +vips_boolean_const_buffer( VipsArithmetic *arithmetic, + PEL *out, PEL **in, int width ) +{ + VipsUnaryConst *uconst = (VipsUnaryConst *) arithmetic; + VipsBooleanConst *bconst = (VipsBooleanConst *) arithmetic; + VipsImage *im = arithmetic->ready[0]; + int bands = im->Bands; + + int i, x, b; + + switch( bconst->boolean ) { + case VIPS_OPERATION_BOOLEAN_AND: + SWITCH( LOOPC, FLOOPC, & ); + break; + + case VIPS_OPERATION_BOOLEAN_OR: + SWITCH( LOOPC, FLOOPC, | ); + break; + + case VIPS_OPERATION_BOOLEAN_EOR: + SWITCH( LOOPC, FLOOPC, ^ ); + break; + + default: + g_assert( 0 ); + } +} + +static void +vips_boolean_const_class_init( VipsBooleanConstClass *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 = "boolean_const"; + object_class->description = + _( "boolean operations against a constant" ); + object_class->build = vips_boolean_const_build; + + vips_arithmetic_set_format_table( aclass, vips_bandfmt_boolean ); + + aclass->process_line = vips_boolean_const_buffer; + + VIPS_ARG_ENUM( class, "boolean", 200, + _( "Operation" ), + _( "boolean to perform" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsBooleanConst, boolean ), + VIPS_TYPE_OPERATION_BOOLEAN, + VIPS_OPERATION_BOOLEAN_AND ); +} + +static void +vips_boolean_const_init( VipsBooleanConst *boolean_const ) +{ +} + +static int +vips_boolean_constv( VipsImage *in, VipsImage **out, + VipsOperationBoolean boolean, double *c, int n, va_list ap ) +{ + VipsArea *area_c; + double *array; + int result; + int i; + + area_c = vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), n ); + array = (double *) area_c->data; + for( i = 0; i < n; i++ ) + array[i] = c[i]; + + result = vips_call_split( "boolean_const", ap, + in, out, boolean, area_c ); + + vips_area_unref( area_c ); + + return( result ); +} + +int +vips_boolean_const( VipsImage *in, VipsImage **out, + VipsOperationBoolean boolean, double *c, int n, ... ) +{ + va_list ap; + int result; + + va_start( ap, n ); + result = vips_boolean_constv( in, out, boolean, c, n, ap ); + va_end( ap ); + + return( result ); +} + +int +vips_boolean_const1( VipsImage *in, VipsImage **out, + VipsOperationBoolean boolean, double c, ... ) +{ + va_list ap; + int result; + + va_start( ap, c ); + result = vips_boolean_constv( in, out, boolean, &c, 1, ap ); + va_end( ap ); + + return( result ); +} diff --git a/libvips/boolean/boolean.c b/libvips/boolean/boolean.c index 76cdadd2..e2ecf90e 100644 --- a/libvips/boolean/boolean.c +++ b/libvips/boolean/boolean.c @@ -84,128 +84,6 @@ static int bandfmt_bool[10] = { UC, C, US, S, UI, I, I, I, I, I, }; -#define BINARY( IN, OUT, OP ) { \ - OUT *tq = (OUT *) q; \ - IN *tp1 = (IN *) p[0]; \ - IN *tp2 = (IN *) p[1]; \ - \ - for( i = 0; i < ne; i++ ) \ - tq[i] = (OUT) tp1[i] OP (OUT) tp2[i]; \ -} - -#define BINARY_BUFFER( NAME, OP ) \ -static void \ -NAME ## _buffer( PEL **p, PEL *q, int n, IMAGE *im ) \ -{ \ - /* Complex just doubles the size. \ - */ \ - const int ne = n * im->Bands * \ - (vips_bandfmt_iscomplex( im->BandFmt ) ? 2 : 1); \ - \ - int i; \ - \ - switch( im->BandFmt ) { \ - case IM_BANDFMT_CHAR: \ - BINARY( signed char, signed char, OP ); break; \ - case IM_BANDFMT_UCHAR: \ - BINARY( unsigned char, unsigned char, OP ); break; \ - case IM_BANDFMT_SHORT: \ - BINARY( signed short, signed short, OP ); break; \ - case IM_BANDFMT_USHORT: \ - BINARY( unsigned short, unsigned short, OP ); break; \ - case IM_BANDFMT_INT: \ - BINARY( signed int, signed int, OP ); break; \ - case IM_BANDFMT_UINT: \ - BINARY( unsigned int, unsigned int, OP ); break; \ - case IM_BANDFMT_FLOAT: \ - BINARY( float, signed int, OP ); break; \ - case IM_BANDFMT_COMPLEX: \ - BINARY( float, signed int, OP ); break; \ - case IM_BANDFMT_DOUBLE: \ - BINARY( double, signed int, OP ); break; \ - case IM_BANDFMT_DPCOMPLEX: \ - BINARY( double, signed int, OP ); break; \ - \ - default: \ - g_assert( 0 ); \ - } \ -} - -BINARY_BUFFER( AND, & ) - -/** - * im_andimage: - * @in1: input #IMAGE 1 - * @in2: input #IMAGE 2 - * @out: output #IMAGE - * - * This operation calculates @in1 & @in2 and writes the result to @out. - * The images may have any format. They may differ - * in their number of bands, see above. They may differ in size, see above. - * - * See also: im_orimage(). - * - * Returns: 0 on success, -1 on error - */ -int -im_andimage( IMAGE *in1, IMAGE *in2, IMAGE *out ) -{ - return( im__arith_binary( "im_andimage", - in1, in2, out, - bandfmt_bool, - (im_wrapmany_fn) AND_buffer, NULL ) ); -} - -BINARY_BUFFER( OR, | ) - -/** - * im_orimage: - * @in1: input #IMAGE 1 - * @in2: input #IMAGE 2 - * @out: output #IMAGE - * - * This operation calculates @in1 | @in2 and writes the result to @out. - * The images may have any format. They may differ - * in their number of bands, see above. They may differ in size, see above. - * - * See also: im_eorimage(). - * - * Returns: 0 on success, -1 on error - */ -int -im_orimage( IMAGE *in1, IMAGE *in2, IMAGE *out ) -{ - return( im__arith_binary( "im_orimage", - in1, in2, out, - bandfmt_bool, - (im_wrapmany_fn) OR_buffer, NULL ) ); -} - -BINARY_BUFFER( EOR, ^ ) - -/** - * im_eorimage: - * @in1: input #IMAGE 1 - * @in2: input #IMAGE 2 - * @out: output #IMAGE - * - * This operation calculates @in1 ^ @in2 and writes the result to @out. - * The images may have any format. They may differ - * in their number of bands, see above. They may differ in size, see above. - * - * See also: im_eorimage_vec(), im_andimage(). - * - * Returns: 0 on success, -1 on error - */ -int -im_eorimage( IMAGE *in1, IMAGE *in2, IMAGE *out ) -{ - return( im__arith_binary( "im_eorimage", - in1, in2, out, - bandfmt_bool, - (im_wrapmany_fn) EOR_buffer, NULL ) ); -} - #define CONST1( IN, OUT, OP ) { \ OUT *tq = (OUT *) q; \ IN *tp = (IN *) p; \ @@ -298,152 +176,6 @@ NAME ## n_buffer( PEL *p, PEL *q, int n, PEL *vector, IMAGE *im ) \ } \ } -CONST1_BUFFER( AND, & ) - -CONSTN_BUFFER( AND, & ) - -/** - * im_andimage_vec: - * @in: input #IMAGE 1 - * @out: output #IMAGE - * @n: array length - * @c: array of constants - * - * This operation calculates @in & @c (bitwise and of image pixels with array - * @c) and writes the result to @out. - * - * See also: im_andimage(), im_orimage_vec(). - * - * Returns: 0 on success, -1 on error - */ -int -im_andimage_vec( IMAGE *in, IMAGE *out, int n, double *c ) -{ - return( im__arith_binary_const( "im_andimage", - in, out, n, c, IM_BANDFMT_INT, - bandfmt_bool, - (im_wrapone_fn) AND1_buffer, - (im_wrapone_fn) ANDn_buffer ) ); -} - -/** - * im_andimageconst: - * @in: input #IMAGE 1 - * @out: output #IMAGE - * @c: constant - * - * This operation calculates @in & @c (bitwise and of image pixels with - * constant - * @c) and writes the result to @out. - * - * See also: im_andimage(), im_orimage_vec(). - * - * Returns: 0 on success, -1 on error - */ -int -im_andimageconst( IMAGE *in, IMAGE *out, double c ) -{ - return( im_andimage_vec( in, out, 1, &c ) ); -} - -CONST1_BUFFER( OR, | ) - -CONSTN_BUFFER( OR, | ) - -/** - * im_orimage_vec: - * @in: input #IMAGE 1 - * @out: output #IMAGE - * @n: array length - * @c: array of constants - * - * This operation calculates @in | @c (bitwise or of image pixels with array - * @c) and writes the result to @out. - * - * See also: im_andimage(), im_orimageconst(). - * - * Returns: 0 on success, -1 on error - */ -int -im_orimage_vec( IMAGE *in, IMAGE *out, int n, double *c ) -{ - return( im__arith_binary_const( "im_orimage", - in, out, n, c, IM_BANDFMT_INT, - bandfmt_bool, - (im_wrapone_fn) OR1_buffer, - (im_wrapone_fn) ORn_buffer ) ); -} - -/** - * im_orimageconst: - * @in: input #IMAGE 1 - * @out: output #IMAGE - * @c: constant - * - * This operation calculates @in | @c (bitwise or of image pixels with - * constant - * @c) and writes the result to @out. - * - * See also: im_andimage(), im_orimage_vec(). - * - * Returns: 0 on success, -1 on error - */ -int -im_orimageconst( IMAGE *in, IMAGE *out, double c ) -{ - return( im_orimage_vec( in, out, 1, &c ) ); -} - -CONST1_BUFFER( EOR, ^ ) - -CONSTN_BUFFER( EOR, ^ ) - -/** - * im_eorimage_vec: - * @in: input #IMAGE 1 - * @out: output #IMAGE - * @n: array length - * @c: array of constants - * - * This operation calculates @in ^ @c (bitwise exclusive-or of image pixels - * with array - * @c) and writes the result to @out. - * - * See also: im_andimage(), im_orimageconst(). - * - * Returns: 0 on success, -1 on error - */ -int -im_eorimage_vec( IMAGE *in, IMAGE *out, int n, double *c ) -{ - return( im__arith_binary_const( "im_eorimage", - in, out, n, c, IM_BANDFMT_INT, - bandfmt_bool, - (im_wrapone_fn) EOR1_buffer, - (im_wrapone_fn) EORn_buffer ) ); -} - -/** - * im_eorimageconst: - * @in: input #IMAGE 1 - * @out: output #IMAGE - * @c: constant - * - * This operation calculates @in ^ @c (bitwise exclusive-or of image pixels - * with - * constant - * @c) and writes the result to @out. - * - * See also: im_andimage(), im_orimage_vec(). - * - * Returns: 0 on success, -1 on error - */ -int -im_eorimageconst( IMAGE *in, IMAGE *out, double c ) -{ - return( im_eorimage_vec( in, out, 1, &c ) ); -} - CONST1_BUFFER( SHIFTL, << ) CONSTN_BUFFER( SHIFTL, << ) diff --git a/libvips/deprecated/vips7compat.c b/libvips/deprecated/vips7compat.c index c0f2ae64..da38c930 100644 --- a/libvips/deprecated/vips7compat.c +++ b/libvips/deprecated/vips7compat.c @@ -1876,3 +1876,97 @@ im_remainderconst( IMAGE *in, IMAGE *out, double c ) { return( im_remainder_vec( in, out, 1, &c ) ); } + +static int +vips__boolean( IMAGE *in1, IMAGE *in2, IMAGE *out, + VipsOperationBoolean boolean ) +{ + VipsImage *t; + + if( vips_relational( in1, in2, &t, boolean, + NULL ) ) + return( -1 ); + if( vips_image_write( t, out ) ) { + g_object_unref( t ); + return( -1 ); + } + g_object_unref( t ); + + return( 0 ); +} + +int +im_andimage( VipsImage *in1, VipsImage *in2, VipsImage *out ) +{ + return( vips__boolean( in1, in2, out, VIPS_OPERATION_BOOLEAN_AND ) ); +} + +int +im_orimage( VipsImage *in1, VipsImage *in2, VipsImage *out ) +{ + return( vips__boolean( in1, in2, out, VIPS_OPERATION_BOOLEAN_OR ) ); +} + +int +im_eorimage( VipsImage *in1, VipsImage *in2, VipsImage *out ) +{ + return( vips__boolean( in1, in2, out, VIPS_OPERATION_BOOLEAN_EOR ) ); +} + +static int +vips__boolean_vec( IMAGE *in, IMAGE *out, + VipsOperationBoolean boolean, double *c, int n ) +{ + VipsImage *t; + + if( vips_boolean_const( in, &t, boolean, c, n, + NULL ) ) + return( -1 ); + if( vips_image_write( t, out ) ) { + g_object_unref( t ); + return( -1 ); + } + g_object_unref( t ); + + return( 0 ); +} + +int +im_andimage_vec( VipsImage *in, VipsImage *out, int n, double *c ) +{ + return( vips__boolean_vec( in, out, + VIPS_OPERATION_BOOLEAN_AND, c, n ) ); +} + +int +im_orimage_vec( VipsImage *in, VipsImage *out, int n, double *c ) +{ + return( vips__boolean_vec( in, out, + VIPS_OPERATION_BOOLEAN_OR, c, n ) ); +} + +int +im_eorimage_vec( VipsImage *in, VipsImage *out, int n, double *c ) +{ + return( vips__boolean_vec( in, out, + VIPS_OPERATION_BOOLEAN_EOR, c, n ) ); +} + +int +im_andimageconst( IMAGE *in, IMAGE *out, double c ) +{ + return( im_andimage_vec( in, out, 1, &c ) ); +} + +int +im_orimageconst( IMAGE *in, IMAGE *out, double c ) +{ + return( im_orimage_vec( in, out, 1, &c ) ); +} + +int +im_eorimageconst( IMAGE *in, IMAGE *out, double c ) +{ + return( im_eorimage_vec( in, out, 1, &c ) ); +} + diff --git a/libvips/include/vips/arithmetic.h b/libvips/include/vips/arithmetic.h index 35744d04..3e4cfa9e 100644 --- a/libvips/include/vips/arithmetic.h +++ b/libvips/include/vips/arithmetic.h @@ -102,6 +102,21 @@ typedef enum { VIPS_OPERATION_RELATIONAL_LAST } VipsOperationRelational; +/** + * VipsOperationBoolean: + * @VIPS_OPERATION_BOOLEAN_AND: & + * @VIPS_OPERATION_BOOLEAN_OR: | + * @VIPS_OPERATION_BOOLEAN_EOR: ^ + * + * See also: vips_boolean(). + */ +typedef enum { + VIPS_OPERATION_BOOLEAN_AND, + VIPS_OPERATION_BOOLEAN_OR, + VIPS_OPERATION_BOOLEAN_EOR, + VIPS_OPERATION_BOOLEAN_LAST +} VipsOperationBoolean; + int vips_add( VipsImage *left, VipsImage *right, VipsImage **out, ... ) __attribute__((sentinel)); int vips_subtract( VipsImage *in1, VipsImage *in2, VipsImage **out, ... ) @@ -157,6 +172,12 @@ int vips_remainder_const( VipsImage *in, VipsImage **out, int vips_remainder_const1( VipsImage *in, VipsImage **out, double c, ... ) __attribute__((sentinel)); +int vips_boolean( VipsImage *left, VipsImage *right, VipsImage **out, + VipsOperationBoolean boolean, ... ) + __attribute__((sentinel)); +int vips_boolean_const( VipsImage *in, VipsImage **out, + VipsOperationBoolean boolean, double *c, int n, ... ) + __attribute__((sentinel)); diff --git a/libvips/include/vips/boolean.h b/libvips/include/vips/boolean.h index f6ccd990..a8337a17 100644 --- a/libvips/include/vips/boolean.h +++ b/libvips/include/vips/boolean.h @@ -37,19 +37,6 @@ extern "C" { #endif /*__cplusplus*/ -int im_andimage( VipsImage *in1, VipsImage *in2, VipsImage *out ); -int im_orimage( VipsImage *in1, VipsImage *in2, VipsImage *out ); -int im_eorimage( VipsImage *in1, VipsImage *in2, VipsImage *out ); - -int im_andimage_vec( VipsImage *in, VipsImage *out, int n, double *c ); -int im_andimageconst( VipsImage *in, VipsImage *out, double c ); - -int im_orimage_vec( VipsImage *in, VipsImage *out, int n, double *c ); -int im_orimageconst( VipsImage *in, VipsImage *out, double c ); - -int im_eorimage_vec( VipsImage *in, VipsImage *out, int n, double *c ); -int im_eorimageconst( VipsImage *in, VipsImage *out, double c ); - int im_shiftleft_vec( VipsImage *in, VipsImage *out, int n, double *c ); int im_shiftleft( VipsImage *in, VipsImage *out, int n ); int im_shiftright_vec( VipsImage *in, VipsImage *out, int n, double *c ); diff --git a/libvips/include/vips/enumtypes.h b/libvips/include/vips/enumtypes.h index a97b4405..136bdcaf 100644 --- a/libvips/include/vips/enumtypes.h +++ b/libvips/include/vips/enumtypes.h @@ -13,6 +13,8 @@ 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()) +GType vips_operation_boolean_get_type (void) G_GNUC_CONST; +#define VIPS_TYPE_OPERATION_BOOLEAN (vips_operation_boolean_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()) diff --git a/libvips/include/vips/vips7compat.h b/libvips/include/vips/vips7compat.h index 8b103d23..7942702c 100644 --- a/libvips/include/vips/vips7compat.h +++ b/libvips/include/vips/vips7compat.h @@ -564,6 +564,16 @@ 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_andimage( VipsImage *in1, VipsImage *in2, VipsImage *out ); +int im_orimage( VipsImage *in1, VipsImage *in2, VipsImage *out ); +int im_eorimage( VipsImage *in1, VipsImage *in2, VipsImage *out ); +int im_andimage_vec( VipsImage *in, VipsImage *out, int n, double *c ); +int im_orimage_vec( VipsImage *in, VipsImage *out, int n, double *c ); +int im_eorimage_vec( VipsImage *in, VipsImage *out, int n, double *c ); +int im_andimageconst( VipsImage *in, VipsImage *out, double c ); +int im_orimageconst( VipsImage *in, VipsImage *out, double c ); +int im_eorimageconst( VipsImage *in, VipsImage *out, double c ); + 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 ); diff --git a/libvips/iofuncs/enumtypes.c b/libvips/iofuncs/enumtypes.c index 0ba4e36f..89665c95 100644 --- a/libvips/iofuncs/enumtypes.c +++ b/libvips/iofuncs/enumtypes.c @@ -151,6 +151,25 @@ vips_operation_relational_get_type( void ) return( etype ); } +GType +vips_operation_boolean_get_type( void ) +{ + static GType etype = 0; + + if( etype == 0 ) { + static const GEnumValue values[] = { + {VIPS_OPERATION_BOOLEAN_AND, "VIPS_OPERATION_BOOLEAN_AND", "and"}, + {VIPS_OPERATION_BOOLEAN_OR, "VIPS_OPERATION_BOOLEAN_OR", "or"}, + {VIPS_OPERATION_BOOLEAN_EOR, "VIPS_OPERATION_BOOLEAN_EOR", "eor"}, + {VIPS_OPERATION_BOOLEAN_LAST, "VIPS_OPERATION_BOOLEAN_LAST", "last"}, + {0, NULL, NULL} + }; + + etype = g_enum_register_static( "VipsOperationBoolean", values ); + } + + return( etype ); +} /* enumerations from "../../libvips/include/vips/util.h" */ GType vips_token_get_type( void )