process all gmic images as float

This commit is contained in:
John Cupitt 2014-10-06 22:02:41 +01:00
parent 3788c85e54
commit 4c095769f9

View File

@ -88,13 +88,13 @@ static int gmic_get_tile_border( VipsGMic* vipsgmic )
}
// copy part of a vips region into a cimg
template<typename T> static void
vips_to_gmic( VipsRegion *in, VipsRect *area, CImg<T>* img )
static void
vips_to_gmic( VipsRegion *in, VipsRect *area, CImg<float>* img )
{
VipsImage *im = in->im;
for( int y = 0; y < area->height; y++ ) {
T *p = (T *) 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 z = 0; z < im->Bands; z++ ) {
@ -108,8 +108,8 @@ vips_to_gmic( VipsRegion *in, VipsRect *area, CImg<T>* img )
// write a CImg to a vips region
// fill out->valid, img has pixels in img_rect
template<typename T> static void
gmic_to_vips( gmic_image<T> *img, VipsRect *img_rect, VipsRegion *out )
static void
gmic_to_vips( gmic_image<float> *img, VipsRect *img_rect, VipsRegion *out )
{
VipsImage *im = out->im;
VipsRect *valid = &out->valid;
@ -120,11 +120,11 @@ gmic_to_vips( gmic_image<T> *img, VipsRect *img_rect, VipsRegion *out )
int y_off = valid->top - img_rect->top;
for( int y = 0; y < valid->height; y++ ) {
T *p = (T *) 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 z = 0; z < im->Bands; z++ )
p[z] = static_cast<T>( (*img)(
p[z] = static_cast<float>( (*img)(
(unsigned int)(x + x_off), (unsigned int)(y + y_off), (unsigned int)z, 0 ) );
p += im->Bands;
@ -132,8 +132,8 @@ gmic_to_vips( gmic_image<T> *img, VipsRect *img_rect, VipsRegion *out )
}
}
template<typename T> static int
_gmic_gen( VipsRegion *oreg, void *seq, void *a, void *b, gboolean *stop )
static int
gmic_gen( VipsRegion *oreg, void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion **ir = (VipsRegion **) seq;
VipsGMic *vipsgmic = (VipsGMic *) b;
@ -143,10 +143,10 @@ _gmic_gen( VipsRegion *oreg, void *seq, void *a, void *b, gboolean *stop )
const VipsRect *r = &oreg->valid;
VipsRect* need = vips_rect_dup( r );
std::cout<<"_gmic_gen(): need before adjust="<<need->left<<","<<need->top<<"+"<<need->width<<"+"<<need->height<<std::endl;
std::cout<<"_gmic_gen(): tile_border="<<tile_border<<std::endl;
std::cout<<"gmic_gen(): need before adjust="<<need->left<<","<<need->top<<"+"<<need->width<<"+"<<need->height<<std::endl;
std::cout<<"gmic_gen(): tile_border="<<tile_border<<std::endl;
vips_rect_marginadjust( need, tile_border );
std::cout<<"_gmic_gen(): need after adjust="<<need->left<<","<<need->top<<"+"<<need->width<<"+"<<need->height<<std::endl;
std::cout<<"gmic_gen(): need after adjust="<<need->left<<","<<need->top<<"+"<<need->width<<"+"<<need->height<<std::endl;
VipsRect image;
if( !ir ) return -1;
@ -156,7 +156,7 @@ _gmic_gen( VipsRegion *oreg, void *seq, void *a, void *b, gboolean *stop )
image.width = ir[0]->im->Xsize;
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;
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 );
@ -164,18 +164,15 @@ _gmic_gen( VipsRegion *oreg, void *seq, void *a, void *b, gboolean *stop )
}
}
//CImg<T> img;
//vips_to_gmic<T>( ir[0], need, &img );
/**/
gmic gmic_instance; // Construct first an empty 'gmic' instance.
gmic_list<T> images; // List of images, will contain all images pixel data.
gmic_list<float> images; // List of images, will contain all images pixel data.
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<T>& img = images._data[i];
gmic_image<float>& img = images._data[i];
img.assign(need->width,need->height,1,ir[i]->im->Bands);
vips_to_gmic<T>( ir[0], need, &img );
vips_to_gmic( ir[0], need, &img );
}
printf("G'MIC command: %s\n",vipsgmic->command);
@ -185,7 +182,7 @@ _gmic_gen( VipsRegion *oreg, void *seq, void *a, void *b, gboolean *stop )
<<" x scale="<<vipsgmic->x_scale<<std::endl;
gmic_instance.run(vipsgmic->command,images,images_names);
gmic_to_vips<T>( &images._data[0], need, oreg );
gmic_to_vips( &images._data[0], need, oreg );
}
catch( gmic_exception e ) {
images.assign((unsigned int)0);
@ -201,57 +198,13 @@ _gmic_gen( VipsRegion *oreg, void *seq, void *a, void *b, gboolean *stop )
}
static int
gmic_gen( VipsRegion *oreg, void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion **ir = (VipsRegion **) seq;
if( !ir ) return -1;
std::cout<<"ir[0]->im->BandFmt: "<<ir[0]->im->BandFmt<<std::endl;
switch( ir[0]->im->BandFmt ) {
case VIPS_FORMAT_UCHAR:
//return _gmic_gen<unsigned char>(oreg, seq, a, b, stop);
break;
case VIPS_FORMAT_CHAR:
break;
case VIPS_FORMAT_USHORT:
//return _gmic_gen<unsigned short int>(oreg, seq, a, b, stop);
break;
case VIPS_FORMAT_SHORT:
break;
case VIPS_FORMAT_UINT:
break;
case VIPS_FORMAT_INT:
break;
case VIPS_FORMAT_FLOAT:
return _gmic_gen<float>(oreg, seq, a, b, stop);
break;
case VIPS_FORMAT_DOUBLE:
break;
default:
g_assert( 0 );
break;
}
return 0;
}
static int _gmic_build( VipsObject *object )
{
VipsObjectClass *klass = VIPS_OBJECT_GET_CLASS( object );
//VipsOperation *operation = VIPS_OPERATION( object );
VipsGMic *vipsgmic = (VipsGMic *) object;
VipsImage **in;
VipsImage **t;
int ninput;
int i;
@ -266,6 +219,12 @@ static int _gmic_build( VipsObject *object )
return( -1 );
}
t = (VipsImage **) vips_object_local_array( object, ninput );
for( i = 0; i < ninput; i++ )
if( vips_cast( in[i], &t[i], VIPS_FORMAT_FLOAT, NULL ) )
return( -1 );
in = t;
/* Get ready to write to @out. @out must be set via g_object_set() so
* that vips can see the assignment. It'll complain that @out hasn't
* been set otherwise.