From 5f288314bb28b698d78207ff2c0b9dbe59cd059f Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 22 Nov 2011 12:00:32 +0000 Subject: [PATCH] factor out some stuff into base classes --- TODO | 78 +++++++++++++++++------------------ libvips/arithmetic/abs.c | 13 +----- libvips/arithmetic/complex.c | 16 ++----- libvips/arithmetic/round.c | 15 ++----- libvips/arithmetic/unary.c | 18 ++++++++ libvips/arithmetic/unary.h | 2 + libvips/conversion/bandary.c | 18 ++++++++ libvips/conversion/bandary.h | 2 + libvips/conversion/bandjoin.c | 11 +---- libvips/conversion/bandmean.c | 13 ++---- libvips/conversion/extract.c | 19 +++------ 11 files changed, 97 insertions(+), 108 deletions(-) diff --git a/TODO b/TODO index 5b63d01d..8ca28490 100644 --- a/TODO +++ b/TODO @@ -1,44 +1,3 @@ -- move recomb to bandary? - - - -- transform_g_string_array_image() can't handle quoted strings, so filenames - with spaces will break - - is there an easy fix? can we reuse code from the csv parser? - - - - -- test docs - - gtk-doc can't introspect to generate class docs since it has no way to init - the classes ... investigate - - - - -- try an area operation, like conv, VipsArea? oops no haha what name should we - use? - - - -- test _O_TEMPORARY thing on Windows - - - -- avg/dev etc. should uncode images? eg. labq2lab etc. - - how about ifthenelse? - - - -- see: vips_abs_build(), should that set an arithmetic member? ugly - - same code in round.c - - - - vipsimage should be cached too, eg. VipsImage *a = vips_image_new_from_file( "poop.jpg" ); @@ -89,6 +48,43 @@ +- transform_g_string_array_image() can't handle quoted strings, so filenames + with spaces will break + + is there an easy fix? can we reuse code from the csv parser? + + the csv parser just parses FILE* streams, we'd need to break it out + + + + +- test docs + + gtk-doc can't introspect to generate class docs since it has no way to init + the classes ... investigate + + + + +- try an area operation, like conv, VipsArea? oops no haha what name should we + use? + + + +- test _O_TEMPORARY thing on Windows + + + +- avg/dev etc. should uncode images? eg. labq2lab etc. + + how about ifthenelse? + + + + + + + - bandalike: consider RGB + RGBA ... we should bandup by adding a black band diff --git a/libvips/arithmetic/abs.c b/libvips/arithmetic/abs.c index 79bb41f7..9767bf71 100644 --- a/libvips/arithmetic/abs.c +++ b/libvips/arithmetic/abs.c @@ -77,20 +77,11 @@ G_DEFINE_TYPE( VipsAbs, vips_abs, VIPS_TYPE_UNARY ); static int vips_abs_build( VipsObject *object ) { - VipsArithmetic *arithmetic = VIPS_ARITHMETIC( object ); VipsUnary *unary = (VipsUnary *) object; if( unary->in && - vips_band_format_isuint( unary->in->BandFmt ) ) { - /* This isn't set by arith until build(), so we have to set - * again here. - * - * Should arith set out in _init()? - */ - g_object_set( arithmetic, "out", vips_image_new(), NULL ); - - return( vips_image_write( unary->in, arithmetic->out ) ); - } + vips_band_format_isuint( unary->in->BandFmt ) ) + return( vips_unary_copy( unary ) ); if( VIPS_OBJECT_CLASS( vips_abs_parent_class )->build( object ) ) return( -1 ); diff --git a/libvips/arithmetic/complex.c b/libvips/arithmetic/complex.c index 99602a3c..5abea5ec 100644 --- a/libvips/arithmetic/complex.c +++ b/libvips/arithmetic/complex.c @@ -347,21 +347,13 @@ G_DEFINE_TYPE( VipsComplexget, vips_complexget, VIPS_TYPE_UNARY ); static int vips_complexget_build( VipsObject *object ) { - VipsArithmetic *arithmetic = VIPS_ARITHMETIC( object ); VipsUnary *unary = (VipsUnary *) object; VipsComplexget *complexget = (VipsComplexget *) object; - if( unary->in ) { - if( !vips_band_format_iscomplex( unary->in->BandFmt ) && - complexget->get == VIPS_OPERATION_COMPLEXGET_REAL ) { - g_object_set( arithmetic, - "out", vips_image_new(), - NULL ); - - return( vips_image_write( unary->in, - arithmetic->out ) ); - } - } + if( unary->in && + !vips_band_format_iscomplex( unary->in->BandFmt ) && + complexget->get == VIPS_OPERATION_COMPLEXGET_REAL ) + return( vips_unary_copy( unary ) ); if( VIPS_OBJECT_CLASS( vips_complexget_parent_class )->build( object ) ) return( -1 ); diff --git a/libvips/arithmetic/round.c b/libvips/arithmetic/round.c index a10e5c52..0cd39bb4 100644 --- a/libvips/arithmetic/round.c +++ b/libvips/arithmetic/round.c @@ -68,23 +68,14 @@ G_DEFINE_TYPE( VipsRound, vips_round, VIPS_TYPE_UNARY ); static int vips_round_build( VipsObject *object ) { - VipsArithmetic *arithmetic = VIPS_ARITHMETIC( object ); VipsUnary *unary = (VipsUnary *) object; - /* Is this one of the int types? Degenerate to im_copy() if it + /* Is this one of the int types? Degenerate to vips_copy() if it * is. */ if( unary->in && - vips_bandfmt_isint( unary->in->BandFmt ) ) { - /* This isn't set by arith until build(), so we have to set - * again here. - * - * Should arith set out in _init()? - */ - g_object_set( arithmetic, "out", vips_image_new(), NULL ); - - return( vips_image_write( unary->in, arithmetic->out ) ); - } + vips_bandfmt_isint( unary->in->BandFmt ) ) + return( vips_unary_copy( unary ) ); if( VIPS_OBJECT_CLASS( vips_round_parent_class )->build( object ) ) return( -1 ); diff --git a/libvips/arithmetic/unary.c b/libvips/arithmetic/unary.c index ca14d0dc..8ed2c121 100644 --- a/libvips/arithmetic/unary.c +++ b/libvips/arithmetic/unary.c @@ -97,3 +97,21 @@ vips_unary_init( VipsUnary *unary ) /* Init our instance fields. */ } + +/* Call this before chaining up in _build() to make the operation fall back to + * copy. + */ +int +vips_unary_copy( VipsUnary *unary ) +{ + VipsArithmetic *arithmetic = VIPS_ARITHMETIC( unary ); + + /* This isn't set by arith until build(), so we have to set + * again here. + * + * Should arith set out in _init()? + */ + g_object_set( unary, "out", vips_image_new(), NULL ); + + return( vips_image_write( unary->in, arithmetic->out ) ); +} diff --git a/libvips/arithmetic/unary.h b/libvips/arithmetic/unary.h index 7f9e8db9..d0eed896 100644 --- a/libvips/arithmetic/unary.h +++ b/libvips/arithmetic/unary.h @@ -59,6 +59,8 @@ typedef VipsArithmeticClass VipsUnaryClass; GType vips_unary_get_type( void ); +int vips_unary_copy( VipsUnary *unary ); + #ifdef __cplusplus } #endif /*__cplusplus*/ diff --git a/libvips/conversion/bandary.c b/libvips/conversion/bandary.c index 504352fb..7fe5be94 100644 --- a/libvips/conversion/bandary.c +++ b/libvips/conversion/bandary.c @@ -183,3 +183,21 @@ vips_bandary_init( VipsBandary *bandjoin ) /* Init our instance fields. */ } + +/* Call this before chaining up in _build() to make the operation fall back to + * copy. + */ +int +vips_bandary_copy( VipsBandary *bandary ) +{ + VipsConversion *conversion = VIPS_CONVERSION( bandary ); + + /* This isn't set by arith until build(), so we have to set + * again here. + * + * Should arith set out in _init()? + */ + g_object_set( bandary, "out", vips_image_new(), NULL ); + + return( vips_image_write( bandary->in[0], conversion->out ) ); +} diff --git a/libvips/conversion/bandary.h b/libvips/conversion/bandary.h index a7f126a2..4364617f 100644 --- a/libvips/conversion/bandary.h +++ b/libvips/conversion/bandary.h @@ -85,6 +85,8 @@ typedef struct _VipsBandaryClass { GType vips_bandary_get_type( void ); +int vips_bandary_copy( VipsBandary *bandary ); + #ifdef __cplusplus } #endif /*__cplusplus*/ diff --git a/libvips/conversion/bandjoin.c b/libvips/conversion/bandjoin.c index bc38e155..04be30ed 100644 --- a/libvips/conversion/bandjoin.c +++ b/libvips/conversion/bandjoin.c @@ -123,7 +123,6 @@ vips_bandjoin_buffer( VipsBandary *bandary, PEL *q, PEL **p, int width ) static int vips_bandjoin_build( VipsObject *object ) { - VipsConversion *conversion = VIPS_CONVERSION( object ); VipsBandary *bandary = (VipsBandary *) object; VipsBandjoin *bandjoin = (VipsBandjoin *) object; @@ -131,14 +130,8 @@ vips_bandjoin_build( VipsObject *object ) bandary->in = bandjoin->in->data; bandary->n = bandjoin->in->n; - if( bandary->n == 1 ) { - g_object_set( conversion, - "out", vips_image_new(), - NULL ); - - return( vips_image_write( bandary->in[0], - conversion->out ) ); - } + if( bandary->n == 1 ) + return( vips_bandary_copy( bandary ) ); else { int i; diff --git a/libvips/conversion/bandmean.c b/libvips/conversion/bandmean.c index e8b87336..d0e31306 100644 --- a/libvips/conversion/bandmean.c +++ b/libvips/conversion/bandmean.c @@ -157,24 +157,17 @@ vips_bandmean_buffer( VipsBandary *bandary, PEL *out, PEL **in, int width ) static int vips_bandmean_build( VipsObject *object ) { - VipsConversion *conversion = VIPS_CONVERSION( object ); VipsBandary *bandary = (VipsBandary *) object; VipsBandmean *bandmean = (VipsBandmean *) object; bandary->out_bands = 1; if( bandmean->in ) { - if( bandmean->in->Bands == 1 ) { - g_object_set( conversion, - "out", vips_image_new(), - NULL ); - - return( vips_image_write( bandmean->in, - conversion->out ) ); - } - bandary->n = 1; bandary->in = &bandmean->in; + + if( bandmean->in->Bands == 1 ) + return( vips_bandary_copy( bandary ) ); } if( VIPS_OBJECT_CLASS( vips_bandmean_parent_class )->build( object ) ) diff --git a/libvips/conversion/extract.c b/libvips/conversion/extract.c index 7f1bbfff..4156f1c3 100644 --- a/libvips/conversion/extract.c +++ b/libvips/conversion/extract.c @@ -305,11 +305,14 @@ vips_extract_band_buffer( VipsBandary *bandary, PEL *out, PEL **in, int width ) static int vips_extract_band_build( VipsObject *object ) { - VipsConversion *conversion = VIPS_CONVERSION( object ); VipsBandary *bandary = (VipsBandary *) object; VipsExtractBand *extract = (VipsExtractBand *) object; if( extract->in ) { + bandary->n = 1; + bandary->in = &extract->in; + bandary->out_bands = extract->n; + if( extract->band + extract->n > extract->in->Bands ) { vips_error( "VipsExtractBand", "%s", _( "bad extract band" ) ); @@ -317,18 +320,8 @@ vips_extract_band_build( VipsObject *object ) } if( extract->band == 0 && - extract->n == extract->in->Bands ) { - g_object_set( conversion, - "out", vips_image_new(), - NULL ); - - return( vips_image_write( extract->in, - conversion->out ) ); - } - - bandary->n = 1; - bandary->in = &extract->in; - bandary->out_bands = extract->n; + extract->n == extract->in->Bands ) + return( vips_bandary_copy( bandary ) ); } if( VIPS_OBJECT_CLASS( vips_extract_band_parent_class )->