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
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
37 #include <ETL/stringf>
38 #include "trgt_imagemagick.h"
40 #include <sys/types.h>
61 /* === M A C R O S ========================================================= */
63 using namespace synfig;
67 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
68 #define UNIX_PIPE_TO_PROCESSES
70 #define WIN32_PIPE_TO_PROCESSES
73 /* === G L O B A L S ======================================================= */
75 SYNFIG_TARGET_INIT(imagemagick_trgt);
76 SYNFIG_TARGET_SET_NAME(imagemagick_trgt,"imagemagick");
77 SYNFIG_TARGET_SET_EXT(imagemagick_trgt,"miff");
78 SYNFIG_TARGET_SET_VERSION(imagemagick_trgt,"0.1");
79 SYNFIG_TARGET_SET_CVS_ID(imagemagick_trgt,"$Id$");
81 /* === M E T H O D S ======================================================= */
83 imagemagick_trgt::imagemagick_trgt(const char *Filename,
84 const synfig::TargetParam& /* params */)
94 imagemagick_trgt::~imagemagick_trgt()
97 #if defined(WIN32_PIPE_TO_PROCESSES)
99 #elif defined(UNIX_PIPE_TO_PROCESSES)
102 waitpid(pid,&status,0);
107 delete [] color_buffer;
111 imagemagick_trgt::set_rend_desc(RendDesc *given_desc)
113 if(filename_extension(filename) == ".xpm")
123 imagemagick_trgt::init()
125 imagecount=desc.get_frame_start();
126 if(desc.get_frame_end()-desc.get_frame_start()>0)
130 buffer=new unsigned char[channels(pf)*desc.get_w()];
131 delete [] color_buffer;
132 color_buffer=new Color[desc.get_w()];
137 imagemagick_trgt::end_frame()
143 #if defined(WIN32_PIPE_TO_PROCESSES)
145 #elif defined(UNIX_PIPE_TO_PROCESSES)
148 waitpid(pid,&status,0);
156 imagemagick_trgt::start_frame(synfig::ProgressCallback *cb)
158 const char *msg=_("Unable to open pipe to imagemagick's convert utility");
163 newfilename = (filename_sans_extension(filename) +
164 etl::strprintf(".%04d",imagecount) +
165 filename_extension(filename));
167 newfilename = filename;
169 #if defined(WIN32_PIPE_TO_PROCESSES)
173 command=strprintf("convert -depth 8 -size %dx%d rgb%s:-[0] -density %dx%d \"%s\"\n",
174 desc.get_w(), desc.get_h(), // size
175 ((channels(pf) == 4) ? "a" : ""), // rgba or rgb?
176 round_to_int(desc.get_x_res()/39.3700787402), // density
177 round_to_int(desc.get_y_res()/39.3700787402),
178 newfilename.c_str());
180 file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
182 #elif defined(UNIX_PIPE_TO_PROCESSES)
187 if(cb) cb->error(N_(msg));
188 else synfig::error(N_(msg));
195 if(cb) cb->error(N_(msg));
196 else synfig::error(N_(msg));
202 // Close pipeout, not needed
204 // Dup pipeout to stdin
205 if( dup2( p[0], STDIN_FILENO ) == -1 ){
206 if(cb) cb->error(N_(msg));
207 else synfig::error(N_(msg));
210 // Close the unneeded pipeout
212 execlp("convert", "convert",
214 "-size", strprintf("%dx%d", desc.get_w(), desc.get_h()).c_str(),
215 ((channels(pf) == 4) ? "rgba:-[0]" : "rgb:-[0]"),
216 "-density", strprintf("%dx%d", round_to_int(desc.get_x_res()/39.3700787402), round_to_int(desc.get_y_res()/39.3700787402)).c_str(),
219 // We should never reach here unless the exec failed
220 if(cb) cb->error(N_(msg));
221 else synfig::error(N_(msg));
225 // Close pipein, not needed
227 // Save pipeout to file handle, will write to it later
228 file = fdopen(p[1], "wb");
232 #error There are no known APIs for creating child processes
237 if(cb)cb->error(N_(msg));
238 else synfig::error(N_(msg));
248 imagemagick_trgt::start_scanline(int /*scanline*/)
254 imagemagick_trgt::end_scanline(void)
259 convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
261 if(!fwrite(buffer,channels(pf),desc.get_w(),file))