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)
93 imagemagick_trgt::~imagemagick_trgt()
96 #if defined(WIN32_PIPE_TO_PROCESSES)
98 #elif defined(UNIX_PIPE_TO_PROCESSES)
101 waitpid(pid,&status,0);
106 delete [] color_buffer;
110 imagemagick_trgt::set_rend_desc(RendDesc *given_desc)
112 if(filename_extension(filename) == ".xpm")
122 imagemagick_trgt::init()
124 imagecount=desc.get_frame_start();
125 if(desc.get_frame_end()-desc.get_frame_start()>0)
129 buffer=new unsigned char[channels(pf)*desc.get_w()];
130 delete [] color_buffer;
131 color_buffer=new Color[desc.get_w()];
136 imagemagick_trgt::end_frame()
142 #if defined(WIN32_PIPE_TO_PROCESSES)
144 #elif defined(UNIX_PIPE_TO_PROCESSES)
147 waitpid(pid,&status,0);
155 imagemagick_trgt::start_frame(synfig::ProgressCallback *cb)
157 const char *msg=_("Unable to open pipe to imagemagick's convert utility");
162 newfilename = (filename_sans_extension(filename) +
163 etl::strprintf(".%04d",imagecount) +
164 filename_extension(filename));
166 newfilename = filename;
168 #if defined(WIN32_PIPE_TO_PROCESSES)
172 command=strprintf("convert -depth 8 -size %dx%d rgb%s:-[0] -density %dx%d \"%s\"\n",
173 desc.get_w(), desc.get_h(), // size
174 ((channels(pf) == 4) ? "a" : ""), // rgba or rgb?
175 round_to_int(desc.get_x_res()/39.3700787402), // density
176 round_to_int(desc.get_y_res()/39.3700787402),
177 newfilename.c_str());
179 file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
181 #elif defined(UNIX_PIPE_TO_PROCESSES)
186 if(cb) cb->error(N_(msg));
187 else synfig::error(N_(msg));
194 if(cb) cb->error(N_(msg));
195 else synfig::error(N_(msg));
201 // Close pipeout, not needed
203 // Dup pipeout to stdin
204 if( dup2( p[0], STDIN_FILENO ) == -1 ){
205 if(cb) cb->error(N_(msg));
206 else synfig::error(N_(msg));
209 // Close the unneeded pipeout
211 execlp("convert", "convert",
213 "-size", strprintf("%dx%d", desc.get_w(), desc.get_h()).c_str(),
214 ((channels(pf) == 4) ? "rgba:-[0]" : "rgb:-[0]"),
215 "-density", strprintf("%dx%d", round_to_int(desc.get_x_res()/39.3700787402), round_to_int(desc.get_y_res()/39.3700787402)).c_str(),
218 // We should never reach here unless the exec failed
219 if(cb) cb->error(N_(msg));
220 else synfig::error(N_(msg));
224 // Close pipein, not needed
226 // Save pipeout to file handle, will write to it later
227 file = fdopen(p[1], "wb");
231 #error There are no known APIs for creating child processes
236 if(cb)cb->error(N_(msg));
237 else synfig::error(N_(msg));
247 imagemagick_trgt::start_scanline(int /*scanline*/)
253 imagemagick_trgt::end_scanline(void)
258 convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
260 if(!fwrite(buffer,channels(pf),desc.get_w(),file))