From 97f1c38befdddfe7f1620833561c32facdd35ad1 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 18 Aug 2009 11:30:47 +0000 Subject: [PATCH] patch im_check_ back --- libvips/arithmetic/im_abs.c | 6 +- libvips/arithmetic/im_add.c | 18 ++---- libvips/arithmetic/im_avg.c | 12 +--- libvips/arithmetic/im_bandmean.c | 4 +- libvips/arithmetic/im_ceil.c | 6 +- libvips/arithmetic/im_costra.c | 60 +++++++----------- libvips/arithmetic/im_cross_phase.c | 27 +++----- libvips/arithmetic/im_deviate.c | 14 ++--- libvips/arithmetic/im_divide.c | 20 +++--- libvips/arithmetic/im_expntra.c | 72 +++++++++------------- libvips/include/vips/proto.h | 9 +++ libvips/iofuncs/predicate.c | 96 +++++++++++++++++++++++++++++ 12 files changed, 187 insertions(+), 157 deletions(-) diff --git a/libvips/arithmetic/im_abs.c b/libvips/arithmetic/im_abs.c index 11c70e37..6963b059 100644 --- a/libvips/arithmetic/im_abs.c +++ b/libvips/arithmetic/im_abs.c @@ -221,12 +221,8 @@ abs_gen( PEL *in, PEL *out, int width, IMAGE *im ) int im_abs( IMAGE *in, IMAGE *out ) { - /* Check args. - */ - if( in->Coding != IM_CODING_NONE ) { - im_error( "im_abs", "%s", _( "not uncoded" ) ); + if( im_check_uncoded( "im_abs", in ) ) return( -1 ); - } /* Is this one of the unsigned types? Degenerate to im_copy() if it * is. diff --git a/libvips/arithmetic/im_add.c b/libvips/arithmetic/im_add.c index a7ae3351..ef1d6a72 100644 --- a/libvips/arithmetic/im_add.c +++ b/libvips/arithmetic/im_add.c @@ -344,19 +344,13 @@ static int bandfmt_add[10] = { int im_add( IMAGE *in1, IMAGE *in2, IMAGE *out ) { - /* Basic checks. - */ - if( im_piocheck( in1, out ) || im_pincheck( in2 ) ) + if( im_piocheck( in1, out ) || + im_pincheck( in2 ) || + im_check_bands_1orn( "im_add", in1, in2 ) || + im_check_uncoded( "im_add", in1 ) || + im_check_uncoded( "im_add", in2 ) ) return( -1 ); - if( in1->Bands != in2->Bands && - (in1->Bands != 1 && in2->Bands != 1) ) { - im_error( "im_add", "%s", _( "not same number of bands" ) ); - return( -1 ); - } - if( in1->Coding != IM_CODING_NONE || in2->Coding != IM_CODING_NONE ) { - im_error( "im_add", "%s", _( "not uncoded" ) ); - return( -1 ); - } + if( im_cp_descv( out, in1, in2, NULL ) ) return( -1 ); diff --git a/libvips/arithmetic/im_avg.c b/libvips/arithmetic/im_avg.c index 21aac595..9ba3f8f8 100644 --- a/libvips/arithmetic/im_avg.c +++ b/libvips/arithmetic/im_avg.c @@ -181,16 +181,10 @@ im_avg( IMAGE *in, double *out ) /* Check our args. */ - if( im_pincheck( in ) ) + if( im_pincheck( in ) || + im_check_noncomplex( "im_avg", in ) || + im_check_uncoded( "im_avg", in ) ) return( -1 ); - if( im_iscomplex( in ) ) { - im_error( "im_avg", "%s", _( "bad input type" ) ); - return( -1 ); - } - if( in->Coding != IM_CODING_NONE ) { - im_error( "im_avg", "%s", _( "not uncoded" ) ); - return( -1 ); - } /* Loop over input, summing pixels. */ diff --git a/libvips/arithmetic/im_bandmean.c b/libvips/arithmetic/im_bandmean.c index dd602afb..dedaae8a 100644 --- a/libvips/arithmetic/im_bandmean.c +++ b/libvips/arithmetic/im_bandmean.c @@ -138,10 +138,8 @@ im_bandmean( IMAGE *in, IMAGE *out ) */ if( in->Bands == 1 ) return( im_copy( in, out ) ); - if( in->Coding != IM_CODING_NONE ) { - im_error( "im_bandmean", "%s", _( "uncoded multiband only" ) ); + if( im_check_uncoded( "im_bandmean", in ) ) return( -1 ); - } /* Prepare output image. */ diff --git a/libvips/arithmetic/im_ceil.c b/libvips/arithmetic/im_ceil.c index eb3eb290..476b747f 100644 --- a/libvips/arithmetic/im_ceil.c +++ b/libvips/arithmetic/im_ceil.c @@ -86,12 +86,8 @@ ceil_gen( PEL *in, PEL *out, int width, IMAGE *im ) int im_ceil( IMAGE *in, IMAGE *out ) { - /* Check args. - */ - if( in->Coding != IM_CODING_NONE ) { - im_error( "im_ceil", "%s", _( "not uncoded" ) ); + if( im_check_uncoded( "im_ceil", in ) ) return( -1 ); - } /* Is this one of the int types? Degenerate to im_copy() if it * is. diff --git a/libvips/arithmetic/im_costra.c b/libvips/arithmetic/im_costra.c index abe022c4..a48498f0 100644 --- a/libvips/arithmetic/im_costra.c +++ b/libvips/arithmetic/im_costra.c @@ -70,14 +70,13 @@ /* Define what we do for each band element type. Non-complex input, any * output. */ -#define loop( IN, OUT )\ - {\ - IN *p = (IN *) in;\ - OUT *q = (OUT *) out;\ - \ - for( x = 0; x < sz; x++ )\ - q[x] = cos( IM_RAD( (double) p[x] ) );\ - } +#define loop( IN, OUT ) { \ + IN *p = (IN *) in; \ + OUT *q = (OUT *) out; \ + \ + for( x = 0; x < sz; x++ ) \ + q[x] = cos( IM_RAD( (double) p[x] ) ); \ +} /* cos a buffer of PELs. */ @@ -109,18 +108,10 @@ costra_gen( PEL *in, PEL *out, int width, IMAGE *im ) int im_costra( IMAGE *in, IMAGE *out ) { - /* Check args. - */ - if( im_piocheck( in, out ) ) + if( im_piocheck( in, out ) || + im_check_uncoded( "im_costra", in ) || + im_check_noncomplex( "im_costra", in ) ) return( -1 ); - if( in->Coding != IM_CODING_NONE ) { - im_error( "im_costra", "%s", _( "not uncoded" ) ); - return( -1 ); - } - if( im_iscomplex( in ) ) { - im_error( "im_costra", "%s", _( "bad input type" ) ); - return( -1 ); - } /* Prepare output header. */ @@ -155,14 +146,13 @@ im_costra( IMAGE *in, IMAGE *out ) /* And acos(). */ -#define aloop( IN, OUT )\ - {\ - IN *p = (IN *) in;\ - OUT *q = (OUT *) out;\ - \ - for( x = 0; x < sz; x++ )\ - q[x] = IM_DEG( acos( (double) p[x] ) );\ - } +#define aloop( IN, OUT ) { \ + IN *p = (IN *) in; \ + OUT *q = (OUT *) out; \ + \ + for( x = 0; x < sz; x++ ) \ + q[x] = IM_DEG( acos( (double) p[x] ) ); \ +} /* acos a buffer of PELs. */ @@ -193,19 +183,11 @@ acostra_gen( PEL *in, PEL *out, int width, IMAGE *im ) */ int im_acostra( IMAGE *in, IMAGE *out ) -{ - /* Check args. - */ - if( im_piocheck( in, out ) ) +{ + if( im_piocheck( in, out ) || + im_check_uncoded( "im_acostra", in ) || + im_check_noncomplex( "im_acostra", in ) ) return( -1 ); - if( in->Coding != IM_CODING_NONE ) { - im_error( "im_acostra", "%s", _( "not uncoded" ) ); - return( -1 ); - } - if( im_iscomplex( in ) ) { - im_error( "im_acostra", "%s", _( "bad input type" ) ); - return( -1 ); - } /* Prepare output header. */ diff --git a/libvips/arithmetic/im_cross_phase.c b/libvips/arithmetic/im_cross_phase.c index 5aba17af..424cd912 100644 --- a/libvips/arithmetic/im_cross_phase.c +++ b/libvips/arithmetic/im_cross_phase.c @@ -133,26 +133,15 @@ int im_cross_phase( IMAGE *a, IMAGE *b, IMAGE *out ){ if( im_pincheck( a ) || im_pincheck( b ) || im_poutcheck( out )) return -1; - if( a-> Xsize != b-> Xsize || a-> Ysize != b-> Ysize ){ - im_error( FUNCTION_NAME, "not same size" ); + if( im_check_size( FUNCTION_NAME, a, b ) || + im_check_bands( FUNCTION_NAME, a, b ) || + im_check_uncoded( FUNCTION_NAME, a ) || + im_check_uncoded( FUNCTION_NAME, b ) || + im_check_format( FUNCTION_NAME, a, b ) || + im_check_complex( FUNCTION_NAME, a ) || + im_check_complex( FUNCTION_NAME, b ) ) return -1; - } - if( a-> Bands != b-> Bands ){ - im_error( FUNCTION_NAME, "numbers of bands differ" ); - return -1; - } - if( a-> Coding || b-> Coding ){ - im_error( FUNCTION_NAME, "not uncoded" ); - return -1; - } - if( a-> BandFmt != b-> BandFmt ){ - im_error( FUNCTION_NAME, "formats differ" ); - return -1; - } - if( IM_BANDFMT_COMPLEX != a-> BandFmt && IM_BANDFMT_DPCOMPLEX != a-> BandFmt ){ - im_error( FUNCTION_NAME, "not complex format" ); - return -1; - } + return im_cp_descv( out, a, b, NULL ) || im_wraptwo( a, b, out, IM_BANDFMT_COMPLEX == a-> BandFmt ? complex_phase_float : complex_phase_double, a, NULL ); } diff --git a/libvips/arithmetic/im_deviate.c b/libvips/arithmetic/im_deviate.c index addb7097..a524b151 100644 --- a/libvips/arithmetic/im_deviate.c +++ b/libvips/arithmetic/im_deviate.c @@ -193,16 +193,10 @@ im_deviate( IMAGE *in, double *out ) /* Check our args. */ - if( im_pincheck( in ) ) + if( im_pincheck( in ) || + im_check_uncoded( "im_deviate", in ) || + im_check_noncomplex( "im_deviate", in ) ) return( -1 ); - if( in->Coding != IM_CODING_NONE ) { - im_error( "im_deviate", "%s", _( "not uncoded" ) ); - return( -1 ); - } - if( im_iscomplex( in ) ) { - im_error( "im_deviate", "%s", _( "bad input type" ) ); - return( -1 ); - } /* Loop over input, summing pixels. */ @@ -211,7 +205,7 @@ im_deviate( IMAGE *in, double *out ) /* - NOTE: NR suggests a two-pass algorithm to minimise roundoff. + NOTE: NR suggests a two-pass algorithm to minimise roundoff. But that's too expensive for us :-( so do it the old one-pass way. diff --git a/libvips/arithmetic/im_divide.c b/libvips/arithmetic/im_divide.c index c17755db..be9fcc23 100644 --- a/libvips/arithmetic/im_divide.c +++ b/libvips/arithmetic/im_divide.c @@ -172,20 +172,14 @@ divide_buffer( PEL **in, PEL *out, int width, IMAGE *im ) int im_divide( IMAGE *in1, IMAGE *in2, IMAGE *out ) -{ - /* Basic checks. - */ - if( im_piocheck( in1, out ) || im_pincheck( in2 ) ) +{ + if( im_piocheck( in1, out ) || + im_pincheck( in2 ) || + im_check_bands_1orn( "im_divide", in1, in2 ) || + im_check_uncoded( "im_divide", in1 ) || + im_check_uncoded( "im_divide", in2 ) ) return( -1 ); - if( in1->Bands != in2->Bands && - (in1->Bands != 1 && in2->Bands != 1) ) { - im_error( "im_divide", "%s", _( "not same number of bands" ) ); - return( -1 ); - } - if( in1->Coding != IM_CODING_NONE || in2->Coding != IM_CODING_NONE ) { - im_error( "im_divide", "%s", _( "not uncoded" ) ); - return( -1 ); - } + if( im_cp_descv( out, in1, in2, NULL ) ) return( -1 ); diff --git a/libvips/arithmetic/im_expntra.c b/libvips/arithmetic/im_expntra.c index 7a927c88..e2283e4b 100644 --- a/libvips/arithmetic/im_expntra.c +++ b/libvips/arithmetic/im_expntra.c @@ -80,22 +80,21 @@ typedef struct { /* Define what we do for each band element type. Single constant. */ -#define loop1(IN, OUT)\ -{\ - IN *p = (IN *) in;\ - OUT *q = (OUT *) out;\ +#define loop1(IN, OUT) { \ + IN *p = (IN *) in; \ + OUT *q = (OUT *) out; \ \ - for( x = 0; x < sz; x++ ) {\ - double f = (double) p[x];\ + for( x = 0; x < sz; x++ ) { \ + double f = (double) p[x]; \ \ - if( e == 0.0 && f < 0.0 ) {\ - /* Division by zero! Difficult to report tho'\ - */\ - q[x] = 0.0;\ - }\ - else\ - q[x] = pow( e, f );\ - }\ + if( e == 0.0 && f < 0.0 ) { \ + /* Division by zero! Difficult to report tho' \ + */ \ + q[x] = 0.0; \ + } \ + else \ + q[x] = pow( e, f ); \ + } \ } /* Expntra a buffer. @@ -128,22 +127,21 @@ expntra1_gen( PEL *in, PEL *out, int width, IMAGE *im, ExpntraInfo *inf ) /* Define what we do for each band element type. One constant per band. */ -#define loopn(IN, OUT)\ -{\ - IN *p = (IN *) in;\ - OUT *q = (OUT *) out;\ +#define loopn(IN, OUT) { \ + IN *p = (IN *) in; \ + OUT *q = (OUT *) out; \ \ - for( i = 0, x = 0; x < width; x++ )\ - for( k = 0; k < im->Bands; k++, i++ ) {\ - double e = inf->e[k];\ - double f = (double) p[i];\ + for( i = 0, x = 0; x < width; x++ ) \ + for( k = 0; k < im->Bands; k++, i++ ) { \ + double e = inf->e[k]; \ + double f = (double) p[i]; \ \ - if( e == 0.0 && f < 0.0 ) {\ - q[i] = 0.0;\ - }\ - else\ - q[i] = pow( e, f );\ - }\ + if( e == 0.0 && f < 0.0 ) { \ + q[i] = 0.0; \ + } \ + else \ + q[i] = pow( e, f ); \ + } \ } /* Expntra a buffer. @@ -178,21 +176,11 @@ im_expntra_vec( IMAGE *in, IMAGE *out, int n, double *e ) ExpntraInfo *inf; int i; - /* Check args. - */ - if( in->Coding != IM_CODING_NONE ) { - im_error( "im_expntra_vec", "%s", _( "not uncoded" ) ); + if( im_piocheck( in, out ) || + im_check_uncoded( "im_expntra", in ) || + im_check_noncomplex( "im_expntra", in ) || + im_check_vector( "im_expntra_vec", n, in ) ) return( -1 ); - } - if( im_iscomplex( in ) ) { - im_error( "im_expntra_vec", "%s", _( "not non-complex" ) ); - return( -1 ); - } - if( n != 1 && n != in->Bands ) { - im_error( "im_expntra_vec", - _( "not 1 or %d elements in vector" ), in->Bands ); - return( -1 ); - } /* Prepare output header. */ diff --git a/libvips/include/vips/proto.h b/libvips/include/vips/proto.h index f9bba8bd..2bde108b 100644 --- a/libvips/include/vips/proto.h +++ b/libvips/include/vips/proto.h @@ -136,6 +136,15 @@ int im_ispartial( IMAGE * ); int im_isMSBfirst( IMAGE * ); int im_amiMSBfirst( void ); +int im_check_uncoded( const char *domain, IMAGE *im ); +int im_check_bands_1orn( const char *domain, IMAGE *im1, IMAGE *im2 ); +int im_check_noncomplex( const char *domain, IMAGE *im ); +int im_check_complex( const char *domain, IMAGE *im ); +int im_check_size( const char *domain, IMAGE *im1, IMAGE *im2 ); +int im_check_bands( const char *domain, IMAGE *im1, IMAGE *im2 ); +int im_check_format( const char *domain, IMAGE *im1, IMAGE *im2 ); +int im_check_vector( const char *domain, int n, IMAGE *im ); + int im_ispoweroftwo( int ); int im_existsf( const char *name, ... ) diff --git a/libvips/iofuncs/predicate.c b/libvips/iofuncs/predicate.c index c8042f83..cd79ca1c 100644 --- a/libvips/iofuncs/predicate.c +++ b/libvips/iofuncs/predicate.c @@ -26,6 +26,8 @@ * - cleanups * 22/5/08 * - image format stuff broken out + * 29/7/09 + * - check funcs added */ /* @@ -356,3 +358,97 @@ im_isvips( const char *filename ) return( 0 ); } + +int +im_check_uncoded( const char *domain, IMAGE *im ) +{ + if( im->Coding != IM_CODING_NONE ) { + im_error( domain, "%s", _( "image must be uncoded" ) ); + return( -1 ); + } + + return( 0 ); +} + +int +im_check_bands_1orn( const char *domain, IMAGE *im1, IMAGE *im2 ) +{ + if( im1->Bands != im2->Bands && + (im1->Bands != 1 && im2->Bands != 1) ) { + im_error( domain, "%s", + _( "images must have the same number of bands, " + "or one muct be single-band" ) ); + return( -1 ); + } + + return( 0 ); +} + +int +im_check_noncomplex( const char *domain, IMAGE *im ) +{ + if( im_iscomplex( im ) ) { + im_error( domain, "%s", _( "image must be non-complex" ) ); + return( -1 ); + } + + return( 0 ); +} + +int +im_check_complex( const char *domain, IMAGE *im ) +{ + if( !im_iscomplex( im ) ) { + im_error( domain, "%s", _( "image must be complex" ) ); + return( -1 ); + } + + return( 0 ); +} + +int +im_check_size( const char *domain, IMAGE *im1, IMAGE *im2 ) +{ + if( im1->Xsize != im2->Xsize || im1->Ysize != im2->Ysize ) { + im_error( domain, "%s", _( "images must match in size" ) ); + return( -1 ); + } + + return( 0 ); +} + +int +im_check_bands( const char *domain, IMAGE *im1, IMAGE *im2 ) +{ + if( im1->Bands != im2->Bands ) { + im_error( domain, "%s", + _( "images must have the same number of bands" ) ); + return( -1 ); + } + + return( 0 ); +} + +int +im_check_format( const char *domain, IMAGE *im1, IMAGE *im2 ) +{ + if( im1->BandFmt != im2->BandFmt ) { + im_error( domain, "%s", + _( "images must have the same band format" ) ); + return( -1 ); + } + + return( 0 ); +} + +int +im_check_vector( const char *domain, int n, IMAGE *im ) +{ + if( n != 1 && n != im->Bands ) { + im_error( domain, + _( "vector must have 1 or %d elements" ), im->Bands ); + return( -1 ); + } + + return( 0 ); +}