File: im_system_image.c

package info (click to toggle)
vips 7.28.5-1%2Bdeb7u1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 15,372 kB
  • sloc: ansic: 95,587; cpp: 50,751; sh: 11,548; python: 1,290; makefile: 916; perl: 40
file content (170 lines) | stat: -rw-r--r-- 4,639 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* im_system_image(): run a command on an image, get an image result
 *
 * 8/1/09
 * 	- from im_system()
 * 2/2/10
 * 	- gtkdoc
 */

/*

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 IM_MAX_STRSIZE (4096)

static int
system_image( IMAGE *im, 
	IMAGE *in_image, char *out_name, const char *cmd_format, 
	char **log ) 
{
	const char *in_name = in_image->filename;
	FILE *fp;
	char line[IM_MAX_STRSIZE];
	char txt[IM_MAX_STRSIZE];
	VipsBuf buf = VIPS_BUF_STATIC( txt );
	int result;

	if( im_copy( im, in_image ) ||
		!(fp = im_popenf( cmd_format, "r", in_name, out_name )) ) 
		return( -1 );

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

	if( (result = pclose( fp )) ) 
		im_error( "im_system_image", 
			_( "command failed: \"%s\"" ), cmd_format );

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

	return( result );
}

/**
 * im_system_image:
 * @im: image to run command on
 * @in_format: write input file like this
 * @out_format: write output filename like this
 * @cmd_format: command to run
 * @log: stdout of command is returned here
 *
 * im_system_image() runs a command, passing an image in and getting an image
 * back. The command's stdout is returned in @log. 
 *
 * First, @im 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. If @in_format is
 * something like "%%s.png", the file will be written in PNG format.
 *
 * 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.
 *
 * Next an output filename is created in the same way using @out_format. The
 * command string to run is made by substituting the first %%s in @cmd_format
 * for the name of the input file and the second %%s for the output filename.
 *
 * The command is executed with popen() and the output captured in @log. If
 * the command fails, the temporary files are deleted and im_system_image()
 * returns NULL. @log is still set.
 *
 * If the command succeeds, the input file is deleted, the output file opened,
 * and returned. Closing the output image will automatically delete the file.
 *
 * In all cases, @log must be freed with im_free().
 *
 * 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.
 *
 * |[
 * im_system_image( in, out, 
 *   "%s.jpg", "%s.jpg", "convert %s -swirl 45 %s", 
 *   &log )
 * ]|
 *
 * See also: im_system().
 *
 * Returns: (transfer full): an image on success, NULL on error
 */
IMAGE *
im_system_image( IMAGE *im, 
	const char *in_format, const char *out_format, const char *cmd_format, 
	char **log )
{
	IMAGE *in_image;
	char *out_name;
	IMAGE *out;

	if( log )
		*log = NULL;

	if( !(in_image = im__open_temp( in_format )) )
		return( NULL );
	if( !(out_name = im__temp_name( out_format )) ) {
		im_close( in_image );
		return( NULL );
	}

	if( system_image( im, in_image, out_name, cmd_format, log ) ) {
		im_close( in_image );
		g_free( out_name );
		g_unlink( out_name );

		return( NULL );
	}
	im_close( in_image );

	if( !(out = im_open( out_name, "r" )) ) {
		g_free( out_name );
		g_unlink( out_name );

		return( NULL );
	}
	g_free( out_name );

	vips_image_set_delete_on_close( out, TRUE );

	return( out );
}