Ejemplo n.º 1
0
/**
 * im_copy_file:
 * @in: input image
 * @out: output image
 *
 * Copy an image to a disc file, then copy again to output. If the image is
 * already a disc file, just copy straight through.
 *
 * The disc file is allocated in the same way as im_system_image(). 
 * The file is automatically deleted when @out is closed.
 *
 * See also: im_copy(), im_system_image().
 *
 * Returns: 0 on success, -1 on error
 */
int
im_copy_file( IMAGE *in, IMAGE *out )
{
	if( !im_isfile( in ) ) {
		IMAGE *disc;

		if( !(disc = im__open_temp( "%s.v" )) )
			return( -1 );
		if( im_add_close_callback( out, 
			(im_callback_fn) im_close, disc, NULL ) ) {
			im_close( disc );
			return( -1 );
		}

		if( im_copy( in, disc ) ||
			im_copy( disc, out ) )
			return( -1 );
	}
	else {
		if( im_copy( in, out ) )
			return( -1 );
	}

	return( 0 );
}
Ejemplo n.º 2
0
/**
 * im_system:
 * @im: image to run command on
 * @cmd: command to run
 * @out: stdout of command is returned here
 *
 * im_system() runs a command on an image, returning the command's output as a
 * string. The command is executed with popen(), the first '%%s' in the 
 * command being substituted for a filename.
 *
 * If the IMAGE is a file on disc, then the filename will be the name of the 
 * real file. If the image is in memory, or the result of a computation, 
 * then a new file is created in the temporary area called something like 
 * "vips_XXXXXX.v", and that filename given to the command. The file is 
 * deleted when the command finishes.
 *
 * The environment variable TMPDIR can be used to set the temporary 
 * directory. If it is not set, it defaults to "/tmp".
 *
 * In all cases, @log must be freed with im_free().
 *
 * See also: im_system_image().
 *
 * Returns: 0 on success, -1 on error
 */
int
im_system( IMAGE *im, const char *cmd, char **out )
{
	FILE *fp;

	if( !im_isfile( im ) ) {
		IMAGE *disc;

		if( !(disc = im__open_temp( "%s.v" )) )
			return( -1 );
		if( im_copy( im, disc ) ||
			im_system( disc, cmd, out ) ) {
			im_close( disc );
			return( -1 );
		}
		im_close( disc );
	}
	else if( (fp = im_popenf( cmd, "r", im->filename )) ) {
		char line[IM_MAX_STRSIZE];
		char txt[IM_MAX_STRSIZE];
		VipsBuf buf = VIPS_BUF_STATIC( txt );

		while( fgets( line, IM_MAX_STRSIZE, fp ) ) 
			if( !vips_buf_appends( &buf, line ) )
				break; 
		pclose( fp );

		if( out )
			*out = im_strdup( NULL, vips_buf_all( &buf ) );
	}

	return( 0 );
}