start adding select

This commit is contained in:
John Cupitt 2019-08-13 03:04:33 +01:00
parent d5634a918b
commit 12371e68c2
5 changed files with 128 additions and 52 deletions

View File

@ -1,6 +1,7 @@
noinst_LTLIBRARIES = libconversion.la noinst_LTLIBRARIES = libconversion.la
libconversion_la_SOURCES = \ libconversion_la_SOURCES = \
select.c \
transpose3d.c \ transpose3d.c \
composite.cpp \ composite.cpp \
smartcrop.c \ smartcrop.c \

View File

@ -1,4 +1,4 @@
/* use pixel values to case between an array of images /* use pixel values to select between an array of images
* *
* 28/7/19 * 28/7/19
* - from maplut.c * - from maplut.c
@ -43,28 +43,99 @@
#include <vips/vips.h> #include <vips/vips.h>
#include <vips/internal.h> #include <vips/internal.h>
typedef struct _VipsCase { typedef struct _VipsSelect {
VipsOperation parent_instance; VipsOperation parent_instance;
VipsImage *in; VipsArrayImage *tests;
VipsArrayImage *cases;
VipsImage *out; VipsImage *out;
VipsArrayImage *lut;
int n; int n;
} VipsCase; } VipsSelect;
typedef VipsOperationClass VipsCaseClass; typedef VipsOperationClass VipsSelectClass;
G_DEFINE_TYPE( VipsCase, vips_case, VIPS_TYPE_OPERATION ); G_DEFINE_TYPE( VipsSelect, vips_select, VIPS_TYPE_OPERATION );
/* Our sequence value.
*/
typedef struct _VipsSelectSeq {
VipsSelect *select;
/* Set of input regions.
*/
VipsRegion **tests;
VipsRegion **cases;
} VipsSelectSeq;
static void *
vips_select_start( VipsImage *out, void *a, void *b )
{
VipsImage **in = (VipsImage **) a;
VipsSelect *select = (VipsSelect *) b;
VipsSelectSeq *seq;
int i, n;
if( !(seq = VIPS_NEW( NULL, VipsSelectSeq )) )
return( NULL );
seq->select = select;
seq->tests = NULL;
seq->cases = NULL;
/* How many images?
*/
for( n = 0; in[n]; n++ )
;
/* Alocate space for region array.
*/
if( !(seq->ir = VIPS_ARRAY( NULL, n + 1, VipsRegion * )) ) {
vips_bandary_stop( seq, NULL, NULL );
return( NULL );
}
/* Create a set of regions.
*/
for( i = 0; i < n; i++ )
if( !(seq->ir[i] = vips_region_new( in[i] )) ) {
vips_bandary_stop( seq, NULL, NULL );
return( NULL );
}
seq->ir[n] = NULL;
/* Input pointers.
*/
if( !(seq->p = VIPS_ARRAY( NULL, n + 1, VipsPel * )) ) {
vips_bandary_stop( seq, NULL, NULL );
return( NULL );
}
/* Pixel buffer. This is used as working space by some subclasses.
*/
if( !(seq->pixels = VIPS_ARRAY( NULL,
n * VIPS_IMAGE_SIZEOF_PEL( bandary->ready[0] ), VipsPel )) ) {
vips_bandary_stop( seq, NULL, NULL );
return( NULL );
}
return( seq );
}
need start / stop and a seq typedef for our two arrays of regions
/* Do a map. /* Do a map.
*/ */
static int static int
vips_case_gen( VipsRegion *or, void *seq, void *a, void *b, vips_select_gen( VipsRegion *or, void *seq, void *a, void *b,
gboolean *stop ) gboolean *stop )
{ {
VipsRegion **ar = (VipsRegion **) seq; VipsRegion **ar = (VipsRegion **) seq;
VipsCase *swit = (VipsCase *) b; VipsSelect *swit = (VipsSelect *) b;
VipsRect *r = &or->valid; VipsRect *r = &or->valid;
VipsRegion *index = ar[swit->n]; VipsRegion *index = ar[swit->n];
@ -131,10 +202,10 @@ vips_case_gen( VipsRegion *or, void *seq, void *a, void *b,
} }
static int static int
vips_case_build( VipsObject *object ) vips_select_build( VipsObject *object )
{ {
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object ); VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsCase *swit = (VipsCase *) object; VipsSelect *swit = (VipsSelect *) object;
VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 ); VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 );
VipsImage *in; VipsImage *in;
@ -147,7 +218,7 @@ vips_case_build( VipsObject *object )
g_object_set( object, "out", vips_image_new(), NULL ); g_object_set( object, "out", vips_image_new(), NULL );
if( VIPS_OBJECT_CLASS( vips_case_parent_class )->build( object ) ) if( VIPS_OBJECT_CLASS( vips_select_parent_class )->build( object ) )
return( -1 ); return( -1 );
in = swit->in; in = swit->in;
@ -206,7 +277,7 @@ vips_case_build( VipsObject *object )
return( -1 ); return( -1 );
if( vips_image_generate( swit->out, if( vips_image_generate( swit->out,
vips_start_many, vips_case_gen, vips_stop_many, vips_start_many, vips_select_gen, vips_stop_many,
lut, swit ) ) lut, swit ) )
return( -1 ); return( -1 );
@ -214,7 +285,7 @@ vips_case_build( VipsObject *object )
} }
static void static void
vips_case_class_init( VipsCaseClass *class ) vips_select_class_init( VipsSelectClass *class )
{ {
GObjectClass *gobject_class = G_OBJECT_CLASS( class ); GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = VIPS_OBJECT_CLASS( class ); VipsObjectClass *object_class = VIPS_OBJECT_CLASS( class );
@ -223,84 +294,88 @@ vips_case_class_init( VipsCaseClass *class )
gobject_class->set_property = vips_object_set_property; gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property; gobject_class->get_property = vips_object_get_property;
object_class->nickname = "case"; object_class->nickname = "select";
object_class->description = object_class->description =
_( "use pixel values to case between a set of images" ); _( "test images pick pixels from a set of case images" );
object_class->build = vips_case_build; object_class->build = vips_select_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL; operation_class->flags = VIPS_OPERATION_SEQUENTIAL;
VIPS_ARG_IMAGE( class, "in", 1, VIPS_ARG_BOXED( class, "tests", 1,
_( "Input" ), _( "Tests" ),
_( "Input image" ), _( "Table of images to test" ),
VIPS_ARGUMENT_REQUIRED_INPUT, VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsCase, in ) ); G_STRUCT_OFFSET( VipsSelect, tests ),
VIPS_TYPE_ARRAY_IMAGE );
VIPS_ARG_IMAGE( class, "out", 2, VIPS_ARG_BOXED( class, "cases", 2,
_( "Cases" ),
_( "Table of image cases" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsSelect, cases ),
VIPS_TYPE_ARRAY_IMAGE );
VIPS_ARG_IMAGE( class, "out", 3,
_( "Output" ), _( "Output" ),
_( "Output image" ), _( "Output image" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT, VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsCase, out ) ); G_STRUCT_OFFSET( VipsSelect, out ) );
VIPS_ARG_BOXED( class, "lut", 3,
_( "LUT" ),
_( "Look-up table of images" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsCase, lut ),
VIPS_TYPE_ARRAY_IMAGE );
} }
static void static void
vips_case_init( VipsCase *swit ) vips_select_init( VipsSelect *swit )
{ {
} }
static int static int
vips_casev( VipsImage *in, VipsImage **out, VipsImage **lut, int n, vips_selectv( VipsImage **tests, VipsImage **cases, VipsImage **out, int n,
va_list ap ) va_list ap )
{ {
VipsArrayImage *array; VipsArrayImage *tests_array;
VipsArrayImage *cases_array;
int result; int result;
array = vips_array_image_new( lut, n ); tests_array = vips_array_image_new( tests n );
result = vips_call_split( "case", ap, in, out, array ); cases_array = vips_array_image_new( selects, n );
vips_area_unref( VIPS_AREA( array ) ); result = vips_call_split( "select", ap,
tests_array, cases_array, out );
vips_area_unref( VIPS_AREA( tests_array ) );
vips_area_unref( VIPS_AREA( cases_array ) );
return( result ); return( result );
} }
/** /**
* vips_case: (method) * vips_select: (method)
* @in: input image * @tests: (array length=n): test these images
* @cases: (array length=n): to pick between these images
* @out: (out): output image * @out: (out): output image
* @lut: (array length=n): LUT of input images
* @n: number of input images * @n: number of input images
* @...: %NULL-terminated list of optional named arguments * @...: %NULL-terminated list of optional named arguments
* *
* Use pixel values to case between an array of images. * The @tests images are evaluated and the index of the first non-zero value
* in @tests is used to pick a pixel from @cases. If all @tests are false, the
* pixel from the final image is @cases is used.
* *
* Each value in @in is used to select an image from @lut, and the * Images in @tests images must have one uchar band. @cases and @tests can
* corresponding pixel is copied to the output. * have up to 256 elements. The images in @tests and @cases
*
* @in must have one band. @lut can have up to 256 elements. Values in @in
* greater than or equal to @n use the final image in @lut. The images in @lut
* must have either one band or the same number of bands. The output image is * must have either one band or the same number of bands. The output image is
* the same size as @in. Images in @lut are expanded to the smallest common * the same size as @tests. Images in @cases are expanded to the smallest
* format and number of bands. * common format and number of bands.
* *
* See also: vips_maplut(). * See also: vips_maplut().
* *
* Returns: 0 on success, -1 on error * Returns: 0 on success, -1 on error
*/ */
int int
vips_case( VipsImage *in, VipsImage **out, VipsImage **lut, int n, ... ) vips_select( VipsImage **tests, VipsImage **cases, VipsImage **out, int n, ... )
{ {
va_list ap; va_list ap;
int result; int result;
va_start( ap, n ); va_start( ap, n );
result = vips_casev( in, out, lut, n, ap ); result = vips_selectv( tests, cases, out, n, ap );
va_end( ap ); va_end( ap );
return( result ); return( result );

View File

@ -5,7 +5,6 @@ libhistogram_la_SOURCES = \
phistogram.h \ phistogram.h \
maplut.c \ maplut.c \
switch.c \ switch.c \
case.c \
hist_unary.c \ hist_unary.c \
hist_unary.h \ hist_unary.h \
hist_cum.c \ hist_cum.c \

View File

@ -262,6 +262,9 @@ int vips_recomb( VipsImage *in, VipsImage **out, VipsImage *m, ... )
int vips_ifthenelse( VipsImage *cond, VipsImage *in1, VipsImage *in2, int vips_ifthenelse( VipsImage *cond, VipsImage *in1, VipsImage *in2,
VipsImage **out, ... ) VipsImage **out, ... )
__attribute__((sentinel)); __attribute__((sentinel));
int vips_select( VipsImage **tests, VipsImage **cases,
VipsImage **out, int n, ... )
__attribute__((sentinel));
int vips_flatten( VipsImage *in, VipsImage **out, ... ) int vips_flatten( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel)); __attribute__((sentinel));

View File

@ -66,8 +66,6 @@ int vips_hist_entropy( VipsImage *in, double *out, ... )
int vips_switch( VipsImage *in, VipsImage **out, VipsImage **lut, int n, ... ) int vips_switch( VipsImage *in, VipsImage **out, VipsImage **lut, int n, ... )
__attribute__((sentinel)); __attribute__((sentinel));
int vips_case( VipsImage *in, VipsImage **out, VipsImage **lut, int n, ... )
__attribute__((sentinel));
#ifdef __cplusplus #ifdef __cplusplus
} }