small tidies to vips_gmic.cpp

This commit is contained in:
John Cupitt 2014-10-07 09:31:20 +01:00
parent 4c095769f9
commit b8c47eea9f

View File

@ -62,48 +62,47 @@ typedef VipsOperationClass VipsGMicClass;
#define VIPS_TYPE_GMIC (vips_gmic_get_type()) #define VIPS_TYPE_GMIC (vips_gmic_get_type())
#define VIPS_GMIC( obj ) \ #define VIPS_GMIC( obj ) \
(G_TYPE_CHECK_INSTANCE_CAST( (obj), \ (G_TYPE_CHECK_INSTANCE_CAST( (obj), VIPS_TYPE_GMIC, VipsGMic ))
VIPS_TYPE_GMIC, VipsGMic )) #define VIPS_GMIC_CLASS( klass ) \
#define VIPS_LAYER_CLASS( klass ) \ (G_TYPE_CHECK_CLASS_CAST( (klass), VIPS_TYPE_GMIC, VipsGMicClass))
(G_TYPE_CHECK_CLASS_CAST( (klass), \ #define VIPS_IS_GMIC( obj ) \
VIPS_TYPE_GMIC, VipsGMicClass))
#define VIPS_IS_LAYER( obj ) \
(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_GMIC )) (G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_GMIC ))
#define VIPS_IS_LAYER_CLASS( klass ) \ #define VIPS_IS_GMIC_CLASS( klass ) \
(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_GMIC )) (G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_GMIC ))
#define VIPS_LAYER_GET_CLASS( obj ) \ #define VIPS_GMIC_GET_CLASS( obj ) \
(G_TYPE_INSTANCE_GET_CLASS( (obj), \ (G_TYPE_INSTANCE_GET_CLASS( (obj), VIPS_TYPE_GMIC, VipsGMicClass ))
VIPS_TYPE_GMIC, VipsGMicClass ))
extern "C" { extern "C" {
G_DEFINE_TYPE( VipsGMic, vips_gmic, VIPS_TYPE_OPERATION ); G_DEFINE_TYPE( VipsGMic, vips_gmic, VIPS_TYPE_OPERATION );
} }
static int
static int gmic_get_tile_border( VipsGMic* vipsgmic ) gmic_get_tile_border( VipsGMic *vipsgmic )
{ {
return vipsgmic->padding; return( vipsgmic->padding );
} }
#define INDEX( IMG, X, Y, Z ) \
((*(IMG))( (guint) (X), (guint) (Y), (guint) (Z), 0 ))
// copy part of a vips region into a cimg // copy part of a vips region into a cimg
static void static void
vips_to_gmic( VipsRegion *in, VipsRect *area, CImg<float>* img ) vips_to_gmic( VipsRegion *in, VipsRect *area, CImg<float> *img )
{ {
VipsImage *im = in->im; VipsImage *im = in->im;
for( int y = 0; y < area->height; y++ ) { for( int y = 0; y < area->height; y++ ) {
float *p = (float *) VIPS_REGION_ADDR( in, area->left, area->top + y ); float *p = (float *)
VIPS_REGION_ADDR( in, area->left, area->top + y );
for( int x = 0; x < area->width; x++ ) { for( int x = 0; x < area->width; x++ ) {
for( int z = 0; z < im->Bands; z++ ) { for( int z = 0; z < im->Bands; z++ )
//const unsigned long off = (unsigned long)img->offset(x,y,z,c); INDEX( img, x, y, z ) = p[z];
(*img)( (unsigned int)x, (unsigned int)y, (unsigned int)z, 0 ) = p[z];
} p += im->Bands;
p += im->Bands; }
} }
}
} }
// write a CImg to a vips region // write a CImg to a vips region
@ -111,162 +110,144 @@ vips_to_gmic( VipsRegion *in, VipsRect *area, CImg<float>* img )
static void static void
gmic_to_vips( gmic_image<float> *img, VipsRect *img_rect, VipsRegion *out ) gmic_to_vips( gmic_image<float> *img, VipsRect *img_rect, VipsRegion *out )
{ {
VipsImage *im = out->im; VipsImage *im = out->im;
VipsRect *valid = &out->valid; VipsRect *valid = &out->valid;
g_assert( vips_rect_includesrect( img_rect, valid ) ); g_assert( vips_rect_includesrect( img_rect, valid ) );
int x_off = valid->left - img_rect->left; int x_off = valid->left - img_rect->left;
int y_off = valid->top - img_rect->top; int y_off = valid->top - img_rect->top;
for( int y = 0; y < valid->height; y++ ) { for( int y = 0; y < valid->height; y++ ) {
float *p = (float *) VIPS_REGION_ADDR( out, valid->left, valid->top + y ); float *p = (float *) \
VIPS_REGION_ADDR( out, valid->left, valid->top + y );
for( int x = 0; x < valid->width; x++ ) { for( int x = 0; x < valid->width; x++ ) {
for( int z = 0; z < im->Bands; z++ ) for( int z = 0; z < im->Bands; z++ )
p[z] = static_cast<float>( (*img)( p[z] = (float)
(unsigned int)(x + x_off), (unsigned int)(y + y_off), (unsigned int)z, 0 ) ); INDEX( img, x + x_off, y + y_off, z );
p += im->Bands; p += im->Bands;
} }
} }
} }
static int static int
gmic_gen( VipsRegion *oreg, void *seq, void *a, void *b, gboolean *stop ) gmic_gen( VipsRegion *oreg, void *seq, void *a, void *b, gboolean *stop )
{ {
VipsRegion **ir = (VipsRegion **) seq; VipsRegion **ir = (VipsRegion **) seq;
VipsGMic *vipsgmic = (VipsGMic *) b; VipsGMic *vipsgmic = (VipsGMic *) b;
int ninput = VIPS_AREA( vipsgmic->in )->n; int ninput = VIPS_AREA( vipsgmic->in )->n;
const int tile_border = gmic_get_tile_border( vipsgmic );
const VipsRect *r = &oreg->valid;
const int tile_border = gmic_get_tile_border( vipsgmic ); VipsRect need;
VipsRect image;
const VipsRect *r = &oreg->valid; need = *r;
VipsRect* need = vips_rect_dup( r ); vips_rect_marginadjust( &need, tile_border );
std::cout<<"gmic_gen(): need before adjust="<<need->left<<","<<need->top<<"+"<<need->width<<"+"<<need->height<<std::endl; image.left = 0;
std::cout<<"gmic_gen(): tile_border="<<tile_border<<std::endl; image.top = 0;
vips_rect_marginadjust( need, tile_border ); image.width = ir[0]->im->Xsize;
std::cout<<"gmic_gen(): need after adjust="<<need->left<<","<<need->top<<"+"<<need->width<<"+"<<need->height<<std::endl; image.height = ir[0]->im->Ysize;
VipsRect image; vips_rect_intersectrect( &need, &image, &need );
if( !ir ) return -1; for( int i = 0; ir[i]; i++ )
if( vips_region_prepare( ir[i], &need ) )
return( -1 );
image.left = 0; gmic gmic_instance;
image.top = 0; gmic_list<float> images;
image.width = ir[0]->im->Xsize; gmic_list<char> images_names;
image.height = ir[0]->im->Ysize;
vips_rect_intersectrect( need, &image, need );
std::cout<<"gmic_gen(): need="<<need->left<<","<<need->top<<"+"<<need->width<<"+"<<need->height<<std::endl;
for( int i = 0; ir[i]; i++ ) {
if( vips_region_prepare( ir[i], need ) ) {
vips_free( need );
return( -1 );
}
}
gmic gmic_instance; // Construct first an empty 'gmic' instance. try {
gmic_list<float> images; // List of images, will contain all images pixel data. images.assign( (guint) ninput );
gmic_list<char> images_names; // List of images names. Can be left empty if no names are associated to images.
try {
images.assign( (unsigned int)ninput );
for( int i = 0; ir[i]; i++ ) {
gmic_image<float>& img = images._data[i];
img.assign(need->width,need->height,1,ir[i]->im->Bands);
vips_to_gmic( ir[0], need, &img );
}
printf("G'MIC command: %s\n",vipsgmic->command); for( int i = 0; ir[i]; i++ ) {
std::cout<<" ninput="<<ninput gmic_image<float> &img = images._data[i];
<<std::endl; img.assign( need.width, need.height,
std::cout<<" padding="<<vipsgmic->padding 1, ir[i]->im->Bands );
<<" x scale="<<vipsgmic->x_scale<<std::endl; vips_to_gmic( ir[0], &need, &img );
}
gmic_instance.run(vipsgmic->command,images,images_names); gmic_instance.run( vipsgmic->command, images, images_names );
gmic_to_vips( &images._data[0], need, oreg ); gmic_to_vips( &images._data[0], &need, oreg );
} }
catch( gmic_exception e ) { catch( gmic_exception e ) {
images.assign((unsigned int)0); images.assign( (guint) 0 );
vips_error( "VipsGMic", "%s", e.what() ); vips_error( "VipsGMic", "%s", e.what() );
return( -1 ); return( -1 );
} }
images.assign((unsigned int)0); images.assign( (guint) 0 );
/**/
return( 0 ); return( 0 );
} }
static int
static int _gmic_build( VipsObject *object ) _gmic_build( VipsObject *object )
{ {
VipsObjectClass *klass = VIPS_OBJECT_GET_CLASS( object ); VipsObjectClass *klass = VIPS_OBJECT_GET_CLASS( object );
//VipsOperation *operation = VIPS_OPERATION( object ); VipsGMic *vipsgmic = (VipsGMic *) object;
VipsGMic *vipsgmic = (VipsGMic *) object;
VipsImage **in;
VipsImage **t;
int ninput;
int i;
if( VIPS_OBJECT_CLASS( vips_gmic_parent_class )->build( object ) ) VipsImage **in;
return( -1 ); VipsImage **t;
int ninput;
int i;
in = vips_array_image_get( vipsgmic->in, &ninput ); if( VIPS_OBJECT_CLASS( vips_gmic_parent_class )->build( object ) )
return( -1 );
for( i = 0; i < ninput; i++ ) { in = vips_array_image_get( vipsgmic->in, &ninput );
if( vips_image_pio_input( in[i] ) ||
vips_check_coding_known( klass->nickname, in[i] ) )
return( -1 );
}
t = (VipsImage **) vips_object_local_array( object, ninput ); for( i = 0; i < ninput; i++ )
for( i = 0; i < ninput; i++ ) if( vips_image_pio_input( in[i] ) ||
if( vips_cast( in[i], &t[i], VIPS_FORMAT_FLOAT, NULL ) ) vips_check_coding_known( klass->nickname, in[i] ) )
return( -1 ); return( -1 );
in = t;
/* Get ready to write to @out. @out must be set via g_object_set() so t = (VipsImage **) vips_object_local_array( object, ninput );
* that vips can see the assignment. It'll complain that @out hasn't for( i = 0; i < ninput; i++ )
* been set otherwise. if( vips_cast( in[i], &t[i], VIPS_FORMAT_FLOAT, NULL ) )
*/ return( -1 );
g_object_set( vipsgmic, "out", vips_image_new(), NULL ); in = t;
/* Set demand hints. g_object_set( vipsgmic, "out", vips_image_new(), NULL );
*/
if( vips_image_pipeline_array( vipsgmic->out,
VIPS_DEMAND_STYLE_ANY,
in ) )
return( -1 );
if(ninput > 0) { if( vips_image_pipeline_array( vipsgmic->out,
if( vips_image_generate( vipsgmic->out, VIPS_DEMAND_STYLE_ANY, in ) )
vips_start_many, gmic_gen, vips_stop_many, return( -1 );
in, vipsgmic ) )
return( -1 );
}
else {
if( vips_image_generate( vipsgmic->out,
NULL, gmic_gen, NULL, NULL, vipsgmic ) )
return( -1 );
}
return( 0 ); if( ninput > 0 ) {
if( vips_image_generate( vipsgmic->out,
vips_start_many, gmic_gen, vips_stop_many,
in, vipsgmic ) )
return( -1 );
}
else {
if( vips_image_generate( vipsgmic->out,
NULL, gmic_gen, NULL, NULL, vipsgmic ) )
return( -1 );
}
return( 0 );
} }
static void static void
vips_gmic_class_init( VipsGMicClass *klass ) vips_gmic_class_init( VipsGMicClass *klass )
{ {
GObjectClass *gobject_class = G_OBJECT_CLASS( klass ); GObjectClass *gobject_class = G_OBJECT_CLASS( klass );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( klass ); VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( klass );
VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( klass ); VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( klass );
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;
vobject_class->nickname = "gmic";
vobject_class->description = _( "Vips G'MIC" ); vobject_class->nickname = "gmic";
vobject_class->build = _gmic_build; vobject_class->description = _( "Vips G'MIC" );
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED; vobject_class->build = _gmic_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
VIPS_ARG_BOXED( klass, "in", 0, VIPS_ARG_BOXED( klass, "in", 0,
_( "Input" ), _( "Input" ),
@ -275,39 +256,39 @@ vips_gmic_class_init( VipsGMicClass *klass )
G_STRUCT_OFFSET( VipsGMic, in ), G_STRUCT_OFFSET( VipsGMic, in ),
VIPS_TYPE_ARRAY_IMAGE ); VIPS_TYPE_ARRAY_IMAGE );
VIPS_ARG_IMAGE( klass, "out", 1, VIPS_ARG_IMAGE( klass, "out", 1,
_( "Output" ), _( "Output" ),
_( "Output image" ), _( "Output image" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT, VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsGMic, out ) ); G_STRUCT_OFFSET( VipsGMic, out ) );
VIPS_ARG_INT( klass, "padding", 3, VIPS_ARG_INT( klass, "padding", 3,
_( "padding" ), _( "padding" ),
_( "Tile overlap" ), _( "Tile overlap" ),
VIPS_ARGUMENT_REQUIRED_INPUT, VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsGMic, padding ), G_STRUCT_OFFSET( VipsGMic, padding ),
0, INT_MAX, 0); 0, INT_MAX, 0);
VIPS_ARG_DOUBLE( klass, "x_scale", 4, VIPS_ARG_DOUBLE( klass, "x_scale", 4,
_( "x_scale" ), _( "x_scale" ),
_( "X Scale" ), _( "X Scale" ),
VIPS_ARGUMENT_REQUIRED_INPUT, VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsGMic, x_scale ), G_STRUCT_OFFSET( VipsGMic, x_scale ),
0, 100000000, 1); 0, 100000000, 1);
VIPS_ARG_DOUBLE( klass, "y_scale", 5, VIPS_ARG_DOUBLE( klass, "y_scale", 5,
_( "y_scale" ), _( "y_scale" ),
_( "Y Scale" ), _( "Y Scale" ),
VIPS_ARGUMENT_REQUIRED_INPUT, VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsGMic, y_scale ), G_STRUCT_OFFSET( VipsGMic, y_scale ),
0, 100000000, 1); 0, 100000000, 1);
VIPS_ARG_STRING( klass, "command", 10, VIPS_ARG_STRING( klass, "command", 10,
_( "command" ), _( "command" ),
_( "G'MIC command string" ), _( "G'MIC command string" ),
VIPS_ARGUMENT_REQUIRED_INPUT, VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsGMic, command ), G_STRUCT_OFFSET( VipsGMic, command ),
NULL ); NULL );
} }
static void static void