libvips/libvips/iofuncs/system.c

321 lines
7.8 KiB
C

/* vips_system(): run a command on an image
*
* 7/3/00 JC
* - hacked it in
* 21/10/02 JC
* - use mktemp() if mkstemp() is not available
* 10/3/03 JC
* - out can be NULL
* 23/12/04
* - use g_mkstemp()
* 8/9/09
* - add .v suffix (thanks Roland)
* - use vipsbuf
* - rewrite to make it simpler
* 2/2/10
* - gtkdoc
* 4/6/13
* - redo as a class
* - input and output images are now optional
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /*HAVE_UNISTD_H*/
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <vips/vips.h>
#define MAX_STRSIZE (4096)
typedef struct _VipsSystem {
VipsOperation parent_instance;
VipsImage *in;
VipsImage *out;
char *cmd_format;
char *in_format;
char *out_format;
char *log;
/* Set to delete in_name on close.
*/
gboolean delete_on_close;
char *in_name;
char *out_name;
} VipsSystem;
typedef VipsOperationClass VipsSystemClass;
G_DEFINE_TYPE( VipsSystem, vips_system, VIPS_TYPE_OPERATION );
static void
vips_system_dispose( GObject *gobject )
{
VipsSystem *system = (VipsSystem *) gobject;
if( system->delete_on_close &&
system->in_name )
g_unlink( system->in_name );
VIPS_FREE( system->in_name );
system->delete_on_close = FALSE;
VIPS_FREE( system->out_name );
G_OBJECT_CLASS( vips_system_parent_class )->dispose( gobject );
}
static int
vips_system_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsSystem *system = (VipsSystem *) object;
FILE *fp;
char line[MAX_STRSIZE];
char txt[MAX_STRSIZE];
VipsBuf buf = VIPS_BUF_STATIC( txt );
int result;
if( VIPS_OBJECT_CLASS( vips_system_parent_class )->build( object ) )
return( -1 );
/* Write the input image to a file. We must always make a copy of the
* file, even if this is a disc file already, in case the command
* needs a different format.
*/
if( system->in ) {
char *in_format = system->in_format ?
system->in_format : "%s.tif";
if( !(system->in_name = vips__temp_name( in_format )) )
return( -1 );
if( vips_image_write_to_file( system->in, system->in_name ) )
return( -1 );
system->delete_on_close = TRUE;
}
/* Make the output filename.
*/
if( system->out_format &&
!(system->out_name = vips__temp_name( system->out_format )) )
return( -1 );
if( system->in_name ) {
if( !(fp = vips_popenf( system->cmd_format, "r",
system->in_name, system->out_name )) )
return( -1 );
}
else {
if( !(fp = vips_popenf( system->cmd_format, "r",
system->out_name )) )
return( -1 );
}
while( fgets( line, MAX_STRSIZE, fp ) )
if( !vips_buf_appends( &buf, line ) )
break;
g_object_set( system, "log", vips_buf_all( &buf ), NULL );
if( (result = pclose( fp )) ) {
vips_error( class->nickname,
_( "command failed: \"%s\"" ), system->cmd_format );
return( -1 );
}
if( system->out_format ) {
VipsImage *out;
if( !(out = vips_image_new_from_file( system->out_name )) )
return( -1 );
vips_image_set_delete_on_close( out, TRUE );
g_object_set( system, "out", out, NULL );
}
return( 0 );
}
static void
vips_system_class_init( VipsSystemClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
gobject_class->dispose = vips_system_dispose;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "system";
vobject_class->description = _( "run an external command" );
vobject_class->build = vips_system_build;
VIPS_ARG_IMAGE( class, "in", 0,
_( "Input" ),
_( "Input image" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsSystem, in ) );
VIPS_ARG_IMAGE( class, "out", 1,
_( "Output" ),
_( "Output image" ),
VIPS_ARGUMENT_OPTIONAL_OUTPUT,
G_STRUCT_OFFSET( VipsSystem, out ) );
VIPS_ARG_STRING( class, "cmd_format", 2,
_( "Command" ),
_( "Command to run" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsSystem, cmd_format ),
NULL );
VIPS_ARG_STRING( class, "in_format", 2,
_( "Input format" ),
_( "Format for input filename" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsSystem, in_format ),
NULL );
VIPS_ARG_STRING( class, "out_format", 2,
_( "Output format" ),
_( "Format for output filename" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsSystem, out_format ),
NULL );
VIPS_ARG_STRING( class, "log", 2,
_( "Log" ),
_( "Command log" ),
VIPS_ARGUMENT_OPTIONAL_OUTPUT,
G_STRUCT_OFFSET( VipsSystem, log ),
NULL );
}
static void
vips_system_init( VipsSystem *system )
{
}
/**
* vips_system:
* @cmd_format: command to run
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @in: input image
* @out: output image
* @in_format: write input file like this
* @out_format: write output filename like this
* @log: stdout of command is returned here
*
* vips_system() runs a command, optionally passing an image in and
* optionally getting an image
* back. The command's stdout is returned in @log.
*
* First, if @in is set, it is written to a file. The filename is formed by
* substituting something like "vips-49857-1" for the first %%s in @in_format,
* then
* prepending "/tmp". If the environment variable TMPDIR is defined, it
* can be used to set a different temporary directory.
*
* On Windows, if the environment variable TMPDIR is not defined, VIPS calls
* GetTempPath() to get the user's preferred temporary area. If that fails, it
* defaults to C:\temp.
*
* If @in_format is
* something like "%%s.png", the file will be written in PNG format. By
* default, @in_format is "%%s.tif".
*
* If @out_format is set, an output filename is formed in the same way.
*
* The
* command string to run is made by substituting the first %%s in @cmd_format
* for the name of the input file, if @in is set, and the second %%s for the
* output filename, if set.
*
* The command is executed with popen() and the output captured in @log.
*
* After the command finishes, if @out_format is set, the output image is
* opened and returned in @out.
* Closing the output image will automatically delete the output file.
*
* Finally the input images are deleted.
*
* For example, this call will run the ImageMagick convert program on an
* image, using JPEG files to pass images into and out of the convert command.
*
* |[
* VipsImage *in;
* VipsImage *out;
* char *log;
*
* if (vips_system ("convert %s -swirl 45 %s",
* "in", in,
* "out", &out,
* "in_format", "%s.jpg",
* "out_format", "%s.jpg",
* "log", &log,
* NULL))
* error ...
* ]|
*
* Returns: 0 on success, -1 on failure.
*/
int
vips_system( const char *cmd_format, ... )
{
va_list ap;
int result;
va_start( ap, cmd_format );
result = vips_call_split( "system", ap, cmd_format );
va_end( ap );
return( result );
}