1 /* === S Y N F I G ========================================================= */
2 /*! \file trgt_imagemagick.cpp
3 ** \brief ppm Target Module
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === H E A D E R S ======================================================= */
36 #include <ETL/stringf>
37 #include "trgt_imagemagick.h"
39 #include <sys/types.h>
60 /* === M A C R O S ========================================================= */
62 using namespace synfig;
66 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
67 #define UNIX_PIPE_TO_PROCESSES
68 #elif defined(HAVE__SPAWNLP) && defined(HAVE__PIPE) && defined(HAVE_CWAIT)
69 #define WIN32_PIPE_TO_PROCESSES
72 /* === G L O B A L S ======================================================= */
74 SYNFIG_TARGET_INIT(imagemagick_trgt);
75 SYNFIG_TARGET_SET_NAME(imagemagick_trgt,"imagemagick");
76 SYNFIG_TARGET_SET_EXT(imagemagick_trgt,"miff");
77 SYNFIG_TARGET_SET_VERSION(imagemagick_trgt,"0.1");
78 SYNFIG_TARGET_SET_CVS_ID(imagemagick_trgt,"$Id$");
80 /* === M E T H O D S ======================================================= */
82 imagemagick_trgt::imagemagick_trgt(const char *Filename)
92 imagemagick_trgt::~imagemagick_trgt()
97 #if defined(WIN32_PIPE_TO_PROCESSES)
99 #elif defined(UNIX_PIPE_TO_PROCESSES)
100 waitpid(pid,&status,0);
105 delete [] color_buffer;
109 imagemagick_trgt::set_rend_desc(RendDesc *given_desc)
111 if(filename_extension(filename) == ".xpm")
121 imagemagick_trgt::init()
123 imagecount=desc.get_frame_start();
124 if(desc.get_frame_end()-desc.get_frame_start()>0)
128 buffer=new unsigned char[channels(pf)*desc.get_w()];
129 delete [] color_buffer;
130 color_buffer=new Color[desc.get_w()];
135 imagemagick_trgt::end_frame()
143 #if defined(WIN32_PIPE_TO_PROCESSES)
144 cwait(&status,pid,0);
145 #elif defined(UNIX_PIPE_TO_PROCESSES)
146 waitpid(pid,&status,0);
154 imagemagick_trgt::start_frame(synfig::ProgressCallback *cb)
156 const char *msg=_("Unable to open pipe to imagemagick's convert utility");
161 newfilename = (filename_sans_extension(filename) +
162 etl::strprintf(".%04d",imagecount) +
163 filename_extension(filename));
165 newfilename = filename;
167 #if defined(WIN32_PIPE_TO_PROCESSES)
170 int stdin_fileno, stdout_fileno;
172 if(_pipe(p, 512, O_BINARY | O_NOINHERIT) < 0) {
173 if(cb) cb->error(N_(msg));
174 else synfig::error(N_(msg));
178 // Save stdin/stdout so we can restore them later
179 stdin_fileno = _dup(_fileno(stdin));
180 stdout_fileno = _dup(_fileno(stdout));
182 // convert should read from the pipe
183 if(_dup2(p[0], _fileno(stdin)) != 0) {
184 if(cb) cb->error(N_(msg));
185 else synfig::error(N_(msg));
190 convert accepts the output filename on the command-line
191 if(_dup2(_fileno(output), _fileno(stdout)) != 0) {
192 if(cb) cb->error(N_(msg));
193 else synfig::error(N_(msg));
198 pid = _spawnlp(_P_NOWAIT, "convert", "convert",
200 "-size", strprintf("%dx%d", desc.get_w(), desc.get_h()).c_str(),
201 ((channels(pf) == 4) ? "rgba:-[0]" : "rgb:-[0]"),
202 "-density", strprintf("%dx%d", round_to_int(desc.get_x_res()/39.3700787402), round_to_int(desc.get_y_res()/39.3700787402)).c_str(),
207 if(cb) cb->error(N_(msg));
208 else synfig::error(N_(msg));
212 // Restore stdin/stdout
213 if(_dup2(stdin_fileno, _fileno(stdin)) != 0) {
214 if(cb) cb->error(N_(msg));
215 else synfig::error(N_(msg));
218 if(_dup2(stdout_fileno, _fileno(stdout)) != 0) {
219 if(cb) cb->error(N_(msg));
220 else synfig::error(N_(msg));
224 close(stdout_fileno);
226 // Close the pipe read end - convert uses it
229 // We write data to the write end of the pipe
230 file = fdopen(p[1], "wb");
232 #elif defined(UNIX_PIPE_TO_PROCESSES)
237 if(cb) cb->error(N_(msg));
238 else synfig::error(N_(msg));
245 if(cb) cb->error(N_(msg));
246 else synfig::error(N_(msg));
252 // Close pipeout, not needed
254 // Dup pipeout to stdin
255 if( dup2( p[0], STDIN_FILENO ) == -1 ){
256 if(cb) cb->error(N_(msg));
257 else synfig::error(N_(msg));
260 // Close the unneeded pipeout
262 execlp("convert", "convert",
264 "-size", strprintf("%dx%d", desc.get_w(), desc.get_h()).c_str(),
265 ((channels(pf) == 4) ? "rgba:-[0]" : "rgb:-[0]"),
266 "-density", strprintf("%dx%d", round_to_int(desc.get_x_res()/39.3700787402), round_to_int(desc.get_y_res()/39.3700787402)).c_str(),
269 // We should never reach here unless the exec failed
270 if(cb) cb->error(N_(msg));
271 else synfig::error(N_(msg));
275 // Close pipein, not needed
277 // Save pipeout to file handle, will write to it later
278 file = fdopen(p[1], "wb");
282 #error There are no known APIs for creating child processes
287 if(cb)cb->error(N_(msg));
288 else synfig::error(N_(msg));
298 imagemagick_trgt::start_scanline(int /*scanline*/)
304 imagemagick_trgt::end_scanline(void)
309 convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
311 if(!fwrite(buffer,channels(pf),desc.get_w(),file))