From 735749a4a2044d6d3168dc0de77178ecf2576d5b Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Mon, 31 Oct 2011 14:25:27 +0000 Subject: [PATCH] add VipsLinear remove im_lintra*(), redone as a class --- ChangeLog | 5 +- TODO | 16 +- libvips/arithmetic/Makefile.am | 2 +- libvips/arithmetic/arithmetic.c | 23 +- libvips/arithmetic/arithmetic.h | 5 + libvips/arithmetic/im_lintra.c | 355 ----------------------------- libvips/arithmetic/linear.c | 259 +++++++++++++-------- libvips/arithmetic/unary.c | 3 +- libvips/deprecated/vips7compat.c | 50 ++++ libvips/include/vips/arithmetic.h | 10 +- libvips/include/vips/internal.h | 2 +- libvips/include/vips/vips7compat.h | 12 +- 12 files changed, 263 insertions(+), 479 deletions(-) delete mode 100644 libvips/arithmetic/im_lintra.c diff --git a/ChangeLog b/ChangeLog index ea67e33c..e5fac917 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,7 +3,8 @@ - im_subtract(), im_avg(), im_min(), im_minpos(), im_copy(), im_embed(), im_flophor(), im_flipver(), im_insert(), im_insert_noexpand(), im_lrjoin(), im_tbjoin(), im_extract_area(), im_extract_bands(), im_extract_areabands(), - im_replicate(), im_clip2fmt(), im_gbandjoin(), im_bandjoin(), im_invert() + im_replicate(), im_clip2fmt(), im_gbandjoin(), im_bandjoin(), im_invert(), + im_lintra(), im_lintra_vec() redone as classes - added argument priorites to help control arg ordering - generate has a 'stop' param to signal successful early termination @@ -27,7 +28,7 @@ - added vips_region_paint_pel() - added VipsArea as a public struct - added array members and arguments -- Unary +- added nary 12/10/11 started 7.26.6 - NOCACHE was not being set correctly on OS X causing performance diff --git a/TODO b/TODO index 6938e290..d6622cd6 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,14 @@ -- lintra_vec next + +- do clip etc. in new style, good to get everything that VipsAdd uses in + the new stack + + insert* needs clip, lintra_vec + + can we move everything to new-style ops now? + +- what's the performance hit for removing the 1ary lintra path? + @@ -39,11 +48,6 @@ vips__vector_to_ink: ink = 0x177d660 (0 0 0) -- do clip etc. in new style, good to get everything that VipsAdd uses in - the new stack - - insert* needs clip, lintra_vec - diff --git a/libvips/arithmetic/Makefile.am b/libvips/arithmetic/Makefile.am index 55b6f299..0a8778e8 100644 --- a/libvips/arithmetic/Makefile.am +++ b/libvips/arithmetic/Makefile.am @@ -9,7 +9,6 @@ libarithmetic_la_SOURCES = \ im_divide.c \ im_recomb.c \ im_linreg.c \ - im_lintra.c \ im_maxpos_avg.c \ im_maxpos.c \ im_maxpos_vec.c \ @@ -32,6 +31,7 @@ libarithmetic_la_SOURCES = \ unary.c \ unary.h \ add.c \ + linear.c \ invert.c \ power.c \ round.c diff --git a/libvips/arithmetic/arithmetic.c b/libvips/arithmetic/arithmetic.c index 9dd09716..1a241744 100644 --- a/libvips/arithmetic/arithmetic.c +++ b/libvips/arithmetic/arithmetic.c @@ -175,17 +175,23 @@ vips__bandup( const char *domain, VipsImage *in, VipsImage **out, int n ) return( vips_bandjoin( bands, out, n, NULL ) ); } +/* base_bands is the default minimum. + * + * Handy for example, if you have VipsLinear with + * a 3-element vector of constants and a 1-band input image, you need to cast + * the image up to three bands. + */ int vips__bandalike_vec( const char *domain, - VipsImage **in, VipsImage **out, int n ) + VipsImage **in, VipsImage **out, int n, int base_bands ) { int i; int max_bands; g_assert( n >= 1 ); - max_bands = in[0]->Bands; - for( i = 1; i < n; i++ ) + max_bands = base_bands; + for( i = 0; i < n; i++ ) max_bands = VIPS_MAX( max_bands, in[i]->Bands ); for( i = 0; i < n; i++ ) if( vips__bandup( domain, in[i], &out[i], max_bands ) ) @@ -242,7 +248,7 @@ vips__bandalike( const char *domain, in[0] = in1; in[1] = in2; - if( vips__bandalike_vec( domain, in, out, 2 ) ) + if( vips__bandalike_vec( domain, in, out, 2, 1 ) ) return( -1 ); *out1 = out[0]; @@ -310,6 +316,8 @@ vips_arithmetic_build( VipsObject *object ) if( VIPS_OBJECT_CLASS( vips_arithmetic_parent_class )->build( object ) ) return( -1 ); + /* No need to check input bands, bandalike will do this for us. + */ if( arithmetic->n > MAX_INPUT_IMAGES ) { vips_error( "VipsArithmetic", "%s", _( "too many input images" ) ); @@ -317,8 +325,6 @@ vips_arithmetic_build( VipsObject *object ) } for( i = 0; i < arithmetic->n; i++ ) if( vips_image_pio_input( arithmetic->in[i] ) || - vips_check_bands_1orn( "VipsArithmetic", - arithmetic->in[0], arithmetic->in[i] ) || vips_check_uncoded( "VipsArithmetic", arithmetic->in[i] ) ) return( -1 ); @@ -336,7 +342,7 @@ vips_arithmetic_build( VipsObject *object ) */ if( vips__formatalike_vec( arithmetic->in, format, arithmetic->n ) || vips__bandalike_vec( "VipsArithmetic", - format, band, arithmetic->n ) || + format, band, arithmetic->n, arithmetic->base_bands ) || vips__sizealike_vec( band, size, arithmetic->n ) ) return( -1 ); @@ -406,6 +412,7 @@ vips_arithmetic_class_init( VipsArithmeticClass *class ) static void vips_arithmetic_init( VipsArithmetic *arithmetic ) { + arithmetic->base_bands = 1; } void @@ -499,11 +506,13 @@ vips_arithmetic_operation_init( void ) extern GType vips_subtract_get_type( void ); extern GType vips_avg_get_type( void ); extern GType vips_min_get_type( void ); + extern GType vips_linear_get_type( void ); vips_add_get_type(); vips_invert_get_type(); vips_subtract_get_type(); vips_avg_get_type(); vips_min_get_type(); + vips_linear_get_type(); } diff --git a/libvips/arithmetic/arithmetic.h b/libvips/arithmetic/arithmetic.h index e6be41b2..e1de9894 100644 --- a/libvips/arithmetic/arithmetic.h +++ b/libvips/arithmetic/arithmetic.h @@ -76,6 +76,11 @@ typedef struct _VipsArithmetic { VipsImage **in; int n; + /* The minimum number of output bands. For example, VipsLinear with a + * three element constant must make at least a three-band output. + */ + int base_bands; + /* The input images, ready for the operation. */ VipsImage **ready; diff --git a/libvips/arithmetic/im_lintra.c b/libvips/arithmetic/im_lintra.c deleted file mode 100644 index a20d8495..00000000 --- a/libvips/arithmetic/im_lintra.c +++ /dev/null @@ -1,355 +0,0 @@ -/* im_lintra.c -- linear transform - * - * Copyright: 1990, N. Dessipris, based on im_powtra() - * Author: Nicos Dessipris - * Written on: 02/05/1990 - * Modified on: - * 23/4/93 JC - * - adapted to work with partial images - * 1/7/93 JC - * - adapted for partial v2 - * 7/10/94 JC - * - new IM_NEW() - * - more typedefs - * 9/2/95 JC - * - adapted for im_wrap... - * - operations on complex images now just transform the real channel - * 29/9/95 JC - * - complex was broken - * 15/4/97 JC - * - return(0) missing from generate, arrgh! - * 1/7/98 JC - * - im_lintra_vec added - * 3/8/02 JC - * - fall back to im_copy() for a == 1, b == 0 - * 10/10/02 JC - * - auug, failing to multiply imag for complex! (thanks matt) - * 10/12/02 JC - * - removed im_copy() fallback ... meant that output format could change - * with value :-( very confusing - * 30/6/04 - * - added 1 band image * n band vector case - * 8/12/06 - * - add liboil support - * 9/9/09 - * - gtkdoc comment, minor reformat - * 31/7/10 - * - remove liboil - */ - -/* - - 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 -#endif /*HAVE_CONFIG_H*/ -#include - -#include -#include -#include - -#include -#include - -/* Struct we need for im_generate(). - */ -typedef struct { - int n; /* Number of bands of constants */ - double *a, *b; -} LintraInfo; - -/* 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] = a * (OUT) p[x] + b; \ -} - -/* Complex input, complex output. - */ -#define LOOPCMPLX( IN, OUT ) { \ - IN *p = (IN *) in; \ - OUT *q = (OUT *) out; \ - \ - for( x = 0; x < sz; x++ ) { \ - q[0] = a * p[0] + b; \ - q[1] = a * p[1]; \ - q += 2; \ - p += 2; \ - } \ -} - -/* Lintra a buffer, 1 set of scale/offset. - */ -static int -lintra1_gen( PEL *in, PEL *out, int width, IMAGE *im, LintraInfo *inf ) -{ - double a = inf->a[0]; - double b = inf->b[0]; - int sz = width * im->Bands; - int x; - - /* Lintra all input types. - */ - switch( im->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; - case IM_BANDFMT_COMPLEX: LOOPCMPLX( float, float ); break; - case IM_BANDFMT_DPCOMPLEX: LOOPCMPLX( double, double ); break; - - default: - g_assert( 0 ); - } - - return( 0 ); -} - -/* Define what we do for each band element type. Non-complex input, any - * output. - */ -#define LOOPN( IN, OUT ) { \ - IN *p = (IN *) in; \ - OUT *q = (OUT *) out; \ - \ - for( i = 0, x = 0; x < width; x++ ) \ - for( k = 0; k < nb; k++, i++ ) \ - q[i] = a[k] * (OUT) p[i] + b[k]; \ -} - -/* Complex input, complex output. - */ -#define LOOPCMPLXN( IN, OUT ) { \ - IN *p = (IN *) in; \ - OUT *q = (OUT *) out; \ - \ - for( x = 0; x < width; x++ ) \ - for( k = 0; k < nb; k++ ) { \ - q[0] = a[k] * p[0] + b[k]; \ - q[1] = a[k] * p[1]; \ - q += 2; \ - p += 2; \ - } \ -} - -/* Lintra a buffer, n set of scale/offset. - */ -static int -lintran_gen( PEL *in, PEL *out, int width, IMAGE *im, LintraInfo *inf ) -{ - double *a = inf->a; - double *b = inf->b; - int nb = im->Bands; - int i, x, k; - - /* Lintra all input types. - */ - switch( im->BandFmt ) { - case IM_BANDFMT_UCHAR: LOOPN( unsigned char, float ); break; - case IM_BANDFMT_CHAR: LOOPN( signed char, float ); break; - case IM_BANDFMT_USHORT: LOOPN( unsigned short, float ); break; - case IM_BANDFMT_SHORT: LOOPN( signed short, float ); break; - case IM_BANDFMT_UINT: LOOPN( unsigned int, float ); break; - case IM_BANDFMT_INT: LOOPN( signed int, float ); break; - case IM_BANDFMT_FLOAT: LOOPN( float, float ); break; - case IM_BANDFMT_DOUBLE: LOOPN( double, double ); break; - case IM_BANDFMT_COMPLEX: LOOPCMPLXN( float, float ); break; - case IM_BANDFMT_DPCOMPLEX: LOOPCMPLXN( double, double ); break; - - default: - g_assert( 0 ); - } - - return( 0 ); -} - -/* 1 band image, n band vector. - */ -#define LOOPNV( IN, OUT ) { \ - IN *p = (IN *) in; \ - OUT *q = (OUT *) out; \ - \ - for( i = 0, x = 0; x < width; x++ ) { \ - OUT v = p[x]; \ - \ - for( k = 0; k < nb; k++, i++ ) \ - q[i] = a[k] * v + b[k]; \ - } \ -} - -#define LOOPCMPLXNV( IN, OUT ) { \ - IN *p = (IN *) in; \ - OUT *q = (OUT *) out; \ - \ - for( x = 0; x < width; x++ ) { \ - OUT p0 = p[0]; \ - OUT p1 = p[1]; \ - \ - for( k = 0; k < nb; k++ ) { \ - q[0] = a[k] * p0 + b[k]; \ - q[1] = a[k] * p1; \ - q += 2; \ - } \ - \ - p += 2; \ - } \ -} - -static int -lintranv_gen( PEL *in, PEL *out, int width, IMAGE *im, LintraInfo *inf ) -{ - double *a = inf->a; - double *b = inf->b; - int nb = inf->n; - int i, x, k; - - /* Lintra all input types. - */ - switch( im->BandFmt ) { - case IM_BANDFMT_UCHAR: LOOPNV( unsigned char, float ); break; - case IM_BANDFMT_CHAR: LOOPNV( signed char, float ); break; - case IM_BANDFMT_USHORT: LOOPNV( unsigned short, float ); break; - case IM_BANDFMT_SHORT: LOOPNV( signed short, float ); break; - case IM_BANDFMT_UINT: LOOPNV( unsigned int, float ); break; - case IM_BANDFMT_INT: LOOPNV( signed int, float ); break; - case IM_BANDFMT_FLOAT: LOOPNV( float, float ); break; - case IM_BANDFMT_DOUBLE: LOOPNV( double, double ); break; - case IM_BANDFMT_COMPLEX: LOOPCMPLXNV( float, float ); break; - case IM_BANDFMT_DPCOMPLEX: LOOPCMPLXNV( double, double ); break; - - default: - g_assert( 0 ); - } - - return( 0 ); -} - -/** - * im_lintra_vec: - * @n: array size - * @a: array of constants for multiplication - * @in: image to transform - * @b: array of constants for addition - * @out: output image - * - * Pass an image through a linear transform - ie. @out = @in * @a + @b. Output - * is always float for integer input, double for double input, complex for - * complex input and double complex for double complex input. - * - * If the arrays of constants have just one element, that constant are used for - * all image bands. If the arrays have 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: im_add(), im_lintra(). - * - * Returns: 0 on success, -1 on error - */ -int -im_lintra_vec( int n, double *a, IMAGE *in, double *b, IMAGE *out ) -{ - LintraInfo *inf; - int i; - - if( im_piocheck( in, out ) || - im_check_vector( "im_lintra_vec", n, in ) || - im_check_uncoded( "lintra_vec", in ) ) - return( -1 ); - - /* Prepare output header. - */ - if( im_cp_desc( out, in ) ) - return( -1 ); - if( vips_bandfmt_isint( in->BandFmt ) ) - out->BandFmt = IM_BANDFMT_FLOAT; - if( in->Bands == 1 ) - out->Bands = n; - - /* Make space for a little buffer. - */ - if( !(inf = IM_NEW( out, LintraInfo )) || - !(inf->a = IM_ARRAY( out, n, double )) || - !(inf->b = IM_ARRAY( out, n, double )) ) - return( -1 ); - inf->n = n; - for( i = 0; i < n; i++ ) { - inf->a[i] = a[i]; - inf->b[i] = b[i]; - } - - /* Generate! - */ - if( n == 1 ) { - if( im_wrapone( in, out, - (im_wrapone_fn) lintra1_gen, in, inf ) ) - return( -1 ); - } - else if( in->Bands == 1 ) { - if( im_wrapone( in, out, - (im_wrapone_fn) lintranv_gen, in, inf ) ) - return( -1 ); - } - else { - if( im_wrapone( in, out, - (im_wrapone_fn) lintran_gen, in, inf ) ) - return( -1 ); - } - - return( 0 ); -} - -/** - * im_lintra: - * @a: constant for multiplication - * @in: image to transform - * @b: constant for addition - * @out: output image - * - * Pass an image through a linear transform - ie. @out = @in * @a + @b. Output - * is always float for integer input, double for double input, complex for - * complex input and double complex for double complex input. - * - * See also: im_add(), im_lintra_vec(). - * - * Returns: 0 on success, -1 on error - */ -int -im_lintra( double a, IMAGE *in, double b, IMAGE *out ) -{ - return( im_lintra_vec( 1, &a, in, &b, out ) ); -} diff --git a/libvips/arithmetic/linear.c b/libvips/arithmetic/linear.c index 9a8fbf02..95889286 100644 --- a/libvips/arithmetic/linear.c +++ b/libvips/arithmetic/linear.c @@ -85,14 +85,23 @@ /** * VipsLinear: - * @in: input #VipsImage - * @out: output #VipsImage + * @in: image to transform + * @out: output image + * @a: array of constants for multiplication + * @b: array of constants for addition * - * For unsigned formats, this operation calculates (max - @in), eg. (255 - - * @in) for uchar. For signed and float formats, this operation calculates (-1 - * * @in). + * Pass an image through a linear transform, ie. (@out = @in * @a + @b). Output + * is always float for integer input, double for double input, complex for + * complex input and double complex for double complex input. * - * See also: im_lintra(). + * If the arrays of constants have just one element, that constant is used for + * all image bands. If the arrays have 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: #VipsAdd. * * Returns: 0 on success, -1 on error */ @@ -105,6 +114,12 @@ typedef struct _VipsLinear { VipsArea *a; VipsArea *b; + /* Our constants expanded to match arith->ready in size. + */ + int n; + double *a_ready; + double *b_ready; + } VipsLinear; typedef VipsUnaryClass VipsLinearClass; @@ -115,65 +130,111 @@ static int vips_linear_build( VipsObject *object ) { VipsArithmetic *arithmetic = VIPS_ARITHMETIC( object ); + VipsUnary *unary = (VipsUnary *) object; VipsLinear *linear = (VipsLinear *) object; + int i; - if( VIPS_OBJECT_CLASS( vips_insert_parent_class )->build( object ) ) + /* If we have a three-element vector, we need to bandup the image to + * match. + */ + linear->n = 1; + if( linear->a ) + linear->n = VIPS_MAX( linear->n, linear->b->n ); + if( linear->b ) + linear->n = VIPS_MAX( linear->n, linear->b->n ); + if( unary->in ) + linear->n = VIPS_MAX( linear->n, unary->in->Bands ); + arithmetic->base_bands = linear->n; + + if( unary->in ) { + if( vips_check_vector( "VipsLinear", + linear->a->n, unary->in ) || + vips_check_vector( "VipsLinear", + linear->b->n, unary->in ) ) return( -1 ); + } - if( vips_check_vector( "VipsLinear", - linear->a->n, arithmetic->in[0] ) || - vips_check_vector( "VipsLinear", - linear->b->n, arithmetic->in[0] ) ) + /* Make up-banded versions of our constants. + */ + linear->a_ready = g_new( double, linear->n ); + linear->b_ready = g_new( double, linear->n ); + + for( i = 0; i < linear->n; i++ ) { + if( linear->a ) { + double *ary = (double *) linear->a->data; + int j = VIPS_MIN( i, linear->a->n - 1 ); + + linear->a_ready[i] = ary[j]; + } + + if( linear->b ) { + double *ary = (double *) linear->b->data; + int j = VIPS_MIN( i, linear->b->n - 1 ); + + linear->b_ready[i] = ary[j]; + } + } + + if( VIPS_OBJECT_CLASS( vips_linear_parent_class )->build( object ) ) return( -1 ); - how do we do this?? unary or arithmetic needs a bit of chopping about - - if( in->Bands == 1 ) - out->Bands = n; - - bandalike a and b - return( 0 ); } +/* Non-complex input, any output. + */ +#define LOOPN( IN, OUT ) { \ + IN *p = (IN *) in[0]; \ + OUT *q = (OUT *) out; \ + \ + for( i = 0, x = 0; x < width; x++ ) \ + for( k = 0; k < nb; k++, i++ ) \ + q[i] = a[k] * (OUT) p[i] + b[k]; \ +} + +/* Complex input, complex output. + */ +#define LOOPCMPLXN( IN, OUT ) { \ + IN *p = (IN *) in[0]; \ + OUT *q = (OUT *) out; \ + \ + for( x = 0; x < width; x++ ) \ + for( k = 0; k < nb; k++ ) { \ + q[0] = a[k] * p[0] + b[k]; \ + q[1] = a[k] * p[1]; \ + q += 2; \ + p += 2; \ + } \ +} + +/* Lintra a buffer, n set of scale/offset. + */ static void vips_linear_buffer( VipsArithmetic *arithmetic, PEL *out, PEL **in, int width ) { VipsImage *im = arithmetic->ready[0]; + VipsLinear *linear = (VipsLinear *) arithmetic; + double *a = linear->a_ready; + double *b = linear->b_ready; + int nb = im->Bands; - /* Complex just doubles the size. - */ - const int sz = width * vips_image_get_bands( im ) * - (vips_band_format_iscomplex( vips_image_get_format( im ) ) ? - 2 : 1); - - int x; + int i, x, k; switch( vips_image_get_format( im ) ) { - case VIPS_FORMAT_UCHAR: - LOOP( unsigned char, UCHAR_MAX ); break; - case VIPS_FORMAT_CHAR: - LOOPN( signed char ); break; - case VIPS_FORMAT_USHORT: - LOOP( unsigned short, USHRT_MAX ); break; - case VIPS_FORMAT_SHORT: - LOOPN( signed short ); break; - case VIPS_FORMAT_UINT: - LOOP( unsigned int, UINT_MAX ); break; - case VIPS_FORMAT_INT: - LOOPN( signed int ); break; + case VIPS_FORMAT_UCHAR: LOOPN( unsigned char, float ); break; + case VIPS_FORMAT_CHAR: LOOPN( signed char, float ); break; + case VIPS_FORMAT_USHORT: LOOPN( unsigned short, float ); break; + case VIPS_FORMAT_SHORT: LOOPN( signed short, float ); break; + case VIPS_FORMAT_UINT: LOOPN( unsigned int, float ); break; + case VIPS_FORMAT_INT: LOOPN( signed int, float ); break; + case VIPS_FORMAT_FLOAT: LOOPN( float, float ); break; + case VIPS_FORMAT_DOUBLE: LOOPN( double, double ); break; + case VIPS_FORMAT_COMPLEX: LOOPCMPLXN( float, float ); break; + case VIPS_FORMAT_DPCOMPLEX: LOOPCMPLXN( double, double ); break; - case VIPS_FORMAT_FLOAT: - case VIPS_FORMAT_COMPLEX: - LOOPN( float ); break; - - case VIPS_FORMAT_DOUBLE: - case VIPS_FORMAT_DPCOMPLEX: - LOOPN( double ); break; - - default: + default: g_assert( 0 ); - } + } } /* Save a bit of typing. @@ -193,55 +254,19 @@ vips_linear_buffer( VipsArithmetic *arithmetic, PEL *out, PEL **in, int width ) */ static const VipsBandFormat vips_bandfmt_linear[10] = { /* UC C US S UI I F X D DX */ - F, F F, F, F, F, F, X, D, DX + F, F, F, F, F, F, F, X, D, DX }; -static void -vips_invert_buffer( VipsArithmetic *arithmetic, PEL *out, PEL **in, int width ) -{ - VipsImage *im = arithmetic->ready[0]; - - /* Complex just doubles the size. - */ - const int sz = width * vips_image_get_bands( im ) * - (vips_band_format_iscomplex( vips_image_get_format( im ) ) ? - 2 : 1); - - int x; - - switch( vips_image_get_format( im ) ) { - case VIPS_FORMAT_UCHAR: - LOOP( unsigned char, UCHAR_MAX ); break; - case VIPS_FORMAT_CHAR: - LOOPN( signed char ); break; - case VIPS_FORMAT_USHORT: - LOOP( unsigned short, USHRT_MAX ); break; - case VIPS_FORMAT_SHORT: - LOOPN( signed short ); break; - case VIPS_FORMAT_UINT: - LOOP( unsigned int, UINT_MAX ); break; - case VIPS_FORMAT_INT: - LOOPN( signed int ); break; - - case VIPS_FORMAT_FLOAT: - case VIPS_FORMAT_COMPLEX: - LOOPN( float ); break; - - case VIPS_FORMAT_DOUBLE: - case VIPS_FORMAT_DPCOMPLEX: - LOOPN( double ); break; - - default: - g_assert( 0 ); - } -} - static void vips_linear_class_init( VipsLinearClass *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 = "linear"; object_class->description = _( "calculate (a * in + b)" ); object_class->build = vips_linear_build; @@ -250,17 +275,17 @@ vips_linear_class_init( VipsLinearClass *class ) aclass->process_line = vips_linear_buffer; - VIPS_ARG_BOXED( class, "a", 4, + VIPS_ARG_BOXED( class, "a", 110, _( "a" ), _( "Multiply by this" ), - VIPS_ARGUMENT_OPTIONAL_INPUT, + VIPS_ARGUMENT_REQUIRED_INPUT, G_STRUCT_OFFSET( VipsLinear, a ), VIPS_TYPE_ARRAY_DOUBLE ); - VIPS_ARG_BOXED( class, "b", 5, + VIPS_ARG_BOXED( class, "b", 111, _( "b" ), _( "Add this" ), - VIPS_ARGUMENT_OPTIONAL_INPUT, + VIPS_ARGUMENT_REQUIRED_INPUT, G_STRUCT_OFFSET( VipsLinear, b ), VIPS_TYPE_ARRAY_DOUBLE ); @@ -272,14 +297,58 @@ vips_linear_init( VipsLinear *linear ) } int -vips_linear( VipsImage *in, VipsImage **out, ... ) +vips_linear( VipsImage *in, VipsImage **out, double *a, double *b, int n, ... ) { va_list ap; + VipsArea *area_a; + VipsArea *area_b; + double *array; int result; + int i; - va_start( ap, out ); - result = vips_call_split( "linear", ap, in, out ); + area_a = vips_area_new_array_object( n ); + array = (double *) area_a->data; + for( i = 0; i < n; i++ ) + array[i] = a[i]; + + area_b = vips_area_new_array_object( n ); + array = (double *) area_b->data; + for( i = 0; i < n; i++ ) + array[i] = b[i]; + + va_start( ap, n ); + result = vips_call_split( "linear", ap, in, out, area_a, area_b ); va_end( ap ); + vips_area_unref( area_a ); + vips_area_unref( area_b ); + + return( result ); +} + +int +vips_linear1( VipsImage *in, VipsImage **out, double a, double b, ... ) +{ + va_list ap; + VipsArea *area_a; + VipsArea *area_b; + double *array; + int result; + + area_a = vips_area_new_array_object( 1 ); + array = (double *) area_a->data; + array[0] = a; + + area_b = vips_area_new_array_object( 1 ); + array = (double *) area_b->data; + array[0] = b; + + va_start( ap, b ); + result = vips_call_split( "linear", ap, in, out, area_a, area_b ); + va_end( ap ); + + vips_area_unref( area_a ); + vips_area_unref( area_b ); + return( result ); } diff --git a/libvips/arithmetic/unary.c b/libvips/arithmetic/unary.c index 7567991c..4ca68428 100644 --- a/libvips/arithmetic/unary.c +++ b/libvips/arithmetic/unary.c @@ -59,7 +59,8 @@ vips_unary_build( VipsObject *object ) arithmetic->n = 1; arithmetic->in = (VipsImage **) vips_object_local_array( object, 1 ); arithmetic->in[0] = unary->in; - g_object_ref( arithmetic->in[0] ); + if( arithmetic->in[0] ) + g_object_ref( arithmetic->in[0] ); if( VIPS_OBJECT_CLASS( vips_unary_parent_class )->build( object ) ) return( -1 ); diff --git a/libvips/deprecated/vips7compat.c b/libvips/deprecated/vips7compat.c index 27041523..47cc4488 100644 --- a/libvips/deprecated/vips7compat.c +++ b/libvips/deprecated/vips7compat.c @@ -1327,3 +1327,53 @@ im_gbandjoin( VipsImage **in, VipsImage *out, int n ) return( 0 ); } +int +im_invert( IMAGE *in, IMAGE *out ) +{ + VipsImage *t; + + if( vips_invert( 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 ) +{ + VipsImage *t; + + if( vips_linear1( in, &t, a, b, + NULL ) ) + return( -1 ); + if( vips_image_write( t, out ) ) { + g_object_unref( t ); + return( -1 ); + } + g_object_unref( t ); + + return( 0 ); +} + +int +im_lintra_vec( int n, double *a, IMAGE *in, double *b, IMAGE *out ) +{ + VipsImage *t; + + if( vips_linear( in, &t, a, b, n, + NULL ) ) + return( -1 ); + if( vips_image_write( t, out ) ) { + g_object_unref( t ); + return( -1 ); + } + g_object_unref( t ); + + return( 0 ); +} diff --git a/libvips/include/vips/arithmetic.h b/libvips/include/vips/arithmetic.h index 4cbd786d..a2dbddf4 100644 --- a/libvips/include/vips/arithmetic.h +++ b/libvips/include/vips/arithmetic.h @@ -45,6 +45,13 @@ int vips_avg( VipsImage *in, double *out, ... ) __attribute__((sentinel)); int vips_min( VipsImage *in, double *out, ... ) __attribute__((sentinel)); +int vips_invert( VipsImage *in, VipsImage **out, ... ) + __attribute__((sentinel)); +int vips_linear( VipsImage *in, VipsImage **out, + double *a, double *b, int n, ... ) + __attribute__((sentinel)); +int vips_linear1( VipsImage *in, VipsImage **out, double a, double b, ... ) + __attribute__((sentinel)); @@ -62,9 +69,6 @@ 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_invert( VipsImage *in, VipsImage *out ); -int im_lintra( double a, VipsImage *in, double b, VipsImage *out ); -int im_lintra_vec( int n, double *a, VipsImage *in, double *b, VipsImage *out ); int im_multiply( VipsImage *in1, VipsImage *in2, VipsImage *out ); int im_divide( VipsImage *in1, VipsImage *in2, VipsImage *out ); int im_remainder( VipsImage *in1, VipsImage *in2, VipsImage *out ); diff --git a/libvips/include/vips/internal.h b/libvips/include/vips/internal.h index d50221ad..5d9905cc 100644 --- a/libvips/include/vips/internal.h +++ b/libvips/include/vips/internal.h @@ -120,7 +120,7 @@ int vips__formatalike_vec( VipsImage **in, VipsImage **out, int n ); int vips__sizealike_vec( VipsImage **in, VipsImage **out, int n ); int vips__bandup( const char *domain, VipsImage *in, VipsImage **out, int n ); int vips__bandalike_vec( const char *domain, - VipsImage **in, VipsImage **out, int n ); + VipsImage **in, VipsImage **out, int n, int base_bands ); int vips__formatalike( VipsImage *in1, VipsImage *in2, VipsImage **out1, VipsImage **out2 ); diff --git a/libvips/include/vips/vips7compat.h b/libvips/include/vips/vips7compat.h index cfc5a8eb..2aa7dc8a 100644 --- a/libvips/include/vips/vips7compat.h +++ b/libvips/include/vips/vips7compat.h @@ -525,6 +525,9 @@ int im_subtract( VipsImage *in1, VipsImage *in2, VipsImage *out ); int im_min( VipsImage *in, double *out ); int im_minpos( VipsImage *in, int *xpos, int *ypos, double *out ); int im_avg( VipsImage *in, double *out ); +int im_invert( VipsImage *in, VipsImage *out ); +int im_lintra( double a, VipsImage *in, double b, VipsImage *out ); +int im_lintra_vec( int n, double *a, VipsImage *in, double *b, VipsImage *out ); int im_copy( VipsImage *in, VipsImage *out ); int im_copy_set( VipsImage *in, VipsImage *out, @@ -536,34 +539,27 @@ int im_copy_morph( VipsImage *in, VipsImage *out, int bands, VipsBandFormat format, VipsCoding coding ); int im_copy_swap( VipsImage *in, VipsImage *out ); int im_copy_native( VipsImage *in, VipsImage *out, gboolean is_msb_first ); - int im_embed( VipsImage *in, VipsImage *out, int type, int x, int y, int width, int height ); - int im_fliphor( VipsImage *in, VipsImage *out ); int im_flipver( VipsImage *in, VipsImage *out ); - int im_insert( VipsImage *main, VipsImage *sub, VipsImage *out, int x, int y ); int im_insert_noexpand( VipsImage *main, VipsImage *sub, VipsImage *out, int x, int y ); - int im_lrjoin( VipsImage *left, VipsImage *right, VipsImage *out ); int im_tbjoin( VipsImage *top, VipsImage *bottom, VipsImage *out ); - int im_extract_area( VipsImage *in, VipsImage *out, int left, int top, int width, int height ); int im_extract_band( VipsImage *in, VipsImage *out, int band ); int im_extract_bands( VipsImage *in, VipsImage *out, int band, int nbands ); int im_extract_areabands( VipsImage *in, VipsImage *out, int left, int top, int width, int height, int band, int nbands ); - int im_replicate( VipsImage *in, VipsImage *out, int across, int down ); - int im_clip2fmt( VipsImage *in, VipsImage *out, VipsBandFormat fmt ); - int im_bandjoin( VipsImage *in1, VipsImage *in2, VipsImage *out ); int im_gbandjoin( VipsImage **in, VipsImage *out, int n ); + /* ruby-vips uses this */ #define vips_class_map_concrete_all vips_class_map_all