static int vips_scale_build( VipsObject *object ) { VipsConversion *conversion = VIPS_CONVERSION( object ); VipsScale *scale = (VipsScale *) object; VipsImage **t = (VipsImage **) vips_object_local_array( object, 7 ); double mx; double mn; if( VIPS_OBJECT_CLASS( vips_scale_parent_class )->build( object ) ) return( -1 ); if( vips_stats( scale->in, &t[0], NULL ) ) return( -1 ); mn = *VIPS_MATRIX( t[0], 0, 0 ); mx = *VIPS_MATRIX( t[0], 1, 0 ); if( mn == mx ) { /* Range of zero: just return black. */ if( vips_black( &t[1], scale->in->Xsize, scale->in->Ysize, "bands", scale->in->Bands, NULL ) || vips_image_write( t[1], conversion->out ) ) return( -1 ); } else if( scale->log ) { double f = 255.0 / log10( 1.0 + pow( mx, scale->exp ) ); if( vips_pow_const1( scale->in, &t[2], scale->exp, NULL ) || vips_linear1( t[2], &t[3], 1.0, 1.0, NULL ) || vips_log10( t[3], &t[4], NULL ) || vips_linear1( t[4], &t[5], f, 0.0, "uchar", TRUE, NULL ) || vips_image_write( t[5], conversion->out ) ) return( -1 ); } else { double f = 255.0 / (mx - mn); /* Add .5 to get round-to-nearest. */ double a = -(mn * f) + 0.5; if( vips_linear1( scale->in, &t[2], f, a, "uchar", TRUE, NULL ) || vips_image_write( t[2], conversion->out ) ) return( -1 ); } return( 0 ); }
static int vips_labelregions_build( VipsObject *object ) { VipsMorphology *morphology = VIPS_MORPHOLOGY( object ); VipsImage *in = morphology->in; VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 ); VipsImage *mask; int segments; int *m; int x, y; if( VIPS_OBJECT_CLASS( vips_labelregions_parent_class )-> build( object ) ) return( -1 ); /* Create the zero mask image in memory. */ mask = vips_image_new_memory(); g_object_set( object, "mask", mask, NULL ); if( vips_black( &t[0], in->Xsize, in->Ysize, NULL ) || vips_cast( t[0], &t[1], VIPS_FORMAT_INT, NULL ) || vips_image_write( t[1], mask ) ) return( -1 ); segments = 1; m = (int *) mask->data; for( y = 0; y < mask->Ysize; y++ ) { for( x = 0; x < mask->Xsize; x++ ) { if( !m[x] ) { /* Use a direct path for speed. */ if( vips__draw_flood_direct( mask, in, segments, x, y ) ) return( -1 ); segments += 1; } } m += mask->Xsize; } g_object_set( object, "segments", segments, NULL ); return( 0 ); }
static int write_blank( VipsForeignSaveDz *dz ) { VipsImage *x, *t; int n; VipsArea *ones; double *d; int i; void *buf; size_t len; GsfOutput *out; if( vips_black( &x, dz->tile_size, dz->tile_size, NULL ) ) return( -1 ); vips_area_get_data( (VipsArea *) dz->background, NULL, &n, NULL, NULL ); ones = vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), n ); d = (double *) vips_area_get_data( ones, NULL, NULL, NULL, NULL ); for( i = 0; i < n; i++ ) d[i] = 1.0; if( vips_linear( x, &t, d, (double *) vips_area_get_data( (VipsArea *) dz->background, NULL, NULL, NULL, NULL ), n, NULL ) ) { vips_area_unref( ones ); g_object_unref( x ); return( -1 ); } vips_area_unref( ones ); g_object_unref( x ); x = t; if( vips_pngsave_buffer( x, &buf, &len, NULL ) ) { g_object_unref( x ); return( -1 ); } g_object_unref( x ); out = vips_gsf_path( dz->tree, "blank.png", NULL ); gsf_output_write( out, len, buf ); gsf_output_close( out ); g_object_unref( out ); g_free( buf ); return( 0 ); }
/* Calculate a pixel for an image from a vec of double. Valid while im is * valid. imag can be NULL, meaning all zero for the imaginary component. */ VipsPel * vips__vector_to_ink( const char *domain, VipsImage *im, double *real, double *imag, int n ) { /* Run our pipeline relative to this. */ VipsImage *context = vips_image_new(); VipsImage **t = (VipsImage **) vips_object_local_array( VIPS_OBJECT( context ), 6 ); VipsBandFormat format; int bands; double *ones; VipsPel *result; int i; #ifdef VIPS_DEBUG printf( "vips__vector_to_ink: starting\n" ); #endif /*VIPS_DEBUG*/ vips_image_decode_predict( im, &bands, &format ); ones = VIPS_ARRAY( im, n, double ); for( i = 0; i < n; i++ ) ones[i] = 1.0; /* Cast vec to match the decoded image. */ if( vips_black( &t[1], 1, 1, "bands", bands, NULL ) || vips_linear( t[1], &t[2], ones, real, n, NULL ) || vips_cast( t[2], &t[3], format, NULL ) ) { g_object_unref( context ); return( NULL ); } /* And now recode the vec to match the original im. */ if( vips_image_encode( t[3], &t[4], im->Coding ) || !(t[5] = vips_image_new_memory()) || vips_image_write( t[4], t[5] ) ) { g_object_unref( context ); return( NULL ); } if( !(result = VIPS_ARRAY( im, VIPS_IMAGE_SIZEOF_PEL( t[5] ), VipsPel )) ) { g_object_unref( context ); return( NULL ); } g_assert( VIPS_IMAGE_SIZEOF_PEL( t[5] ) == VIPS_IMAGE_SIZEOF_PEL( im ) ); memcpy( result, t[5]->data, VIPS_IMAGE_SIZEOF_PEL( im ) ); g_object_unref( context ); #ifdef VIPS_DEBUG { int i; printf( "vips__vector_to_ink:\n" ); printf( "\t(real, imag) = " ); for( i = 0; i < n; i++ ) printf( "(%g, %g) ", real[i], imag ? imag[i] : 0 ); printf( "\n" ); printf( "\tink = " ); for( i = 0; i < VIPS_IMAGE_SIZEOF_PEL( im ); i++ ) printf( "%d ", result[i] ); printf( "\n" ); } #endif /*VIPS_DEBUG*/ return( result ); }
/* Make a pair of vector constants into a set of formatted pixels. bands can * be 3 while n is 1, meaning expand the constant to the number of bands. * imag can be NULL, meaning all zero for the imaginary component. */ VipsPel * vips__vector_to_pels( const char *domain, int bands, VipsBandFormat format, VipsCoding coding, double *real, double *imag, int n ) { /* Run our pipeline relative to this. */ VipsImage *context = vips_image_new(); VipsImage **t = (VipsImage **) vips_object_local_array( VIPS_OBJECT( context ), 8 ); VipsImage *in; double *ones; VipsPel *result; int i; #ifdef VIPS_DEBUG printf( "vips__vector_to_pels: starting\n" ); #endif /*VIPS_DEBUG*/ ones = VIPS_ARRAY( context, n, double ); for( i = 0; i < n; i++ ) ones[i] = 1.0; /* Make the real and imaginary parts. */ if( vips_black( &t[0], 1, 1, "bands", bands, NULL ) || vips_linear( t[0], &t[1], ones, real, n, NULL ) ) { g_object_unref( context ); return( NULL ); } in = t[1]; if( imag ) { if( vips_black( &t[2], 1, 1, "bands", bands, NULL ) || vips_linear( t[2], &t[3], ones, imag, n, NULL ) || vips_complexform( in, t[3], &t[4], NULL ) ) { g_object_unref( context ); return( NULL ); } in = t[4]; } /* Cast to the output type and coding. */ if( vips_cast( in, &t[5], format, NULL ) || vips_image_encode( t[5], &t[6], coding ) ) { g_object_unref( context ); return( NULL ); } in = t[6]; /* Write to memory, copy to output buffer. */ if( !(t[7] = vips_image_new_memory()) || vips_image_write( in, t[7] ) ) { g_object_unref( context ); return( NULL ); } in = t[7]; if( !(result = VIPS_ARRAY( NULL, VIPS_IMAGE_SIZEOF_PEL( in ), VipsPel )) ) { g_object_unref( context ); return( NULL ); } memcpy( result, in->data, VIPS_IMAGE_SIZEOF_PEL( in ) ); #ifdef VIPS_DEBUG { int i; printf( "vips__vector_to_ink:\n" ); printf( "\t(real, imag) = " ); for( i = 0; i < n; i++ ) printf( "(%g, %g) ", real[i], imag ? imag[i] : 0 ); printf( "\n" ); printf( "\tink = " ); for( i = 0; i < VIPS_IMAGE_SIZEOF_PEL( in ); i++ ) printf( "%d ", result[i] ); printf( "\n" ); } #endif /*VIPS_DEBUG*/ g_object_unref( context ); return( result ); }