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 ======================================================= */
35 #include <ETL/stringf>
36 #include "trgt_imagemagick.h"
38 #include <sys/types.h>
59 /* === M A C R O S ========================================================= */
61 using namespace synfig;
65 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
66 #define UNIX_PIPE_TO_PROCESSES
68 #define WIN32_PIPE_TO_PROCESSES
71 /* === G L O B A L S ======================================================= */
73 SYNFIG_TARGET_INIT(imagemagick_trgt);
74 SYNFIG_TARGET_SET_NAME(imagemagick_trgt,"imagemagick");
75 SYNFIG_TARGET_SET_EXT(imagemagick_trgt,"miff");
76 SYNFIG_TARGET_SET_VERSION(imagemagick_trgt,"0.1");
77 SYNFIG_TARGET_SET_CVS_ID(imagemagick_trgt,"$Id$");
79 /* === M E T H O D S ======================================================= */
81 imagemagick_trgt::imagemagick_trgt(const char *Filename,
82 const synfig::TargetParam& /* params */)
92 imagemagick_trgt::~imagemagick_trgt()
95 #if defined(WIN32_PIPE_TO_PROCESSES)
97 #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()
141 #if defined(WIN32_PIPE_TO_PROCESSES)
143 #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)
171 command=strprintf("convert -depth 8 -size %dx%d rgb%s:-[0] -density %dx%d \"%s\"\n",
172 desc.get_w(), desc.get_h(), // size
173 ((channels(pf) == 4) ? "a" : ""), // rgba or rgb?
174 round_to_int(desc.get_x_res()/39.3700787402), // density
175 round_to_int(desc.get_y_res()/39.3700787402),
176 newfilename.c_str());
178 file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
180 #elif defined(UNIX_PIPE_TO_PROCESSES)
185 if(cb) cb->error(N_(msg));
186 else synfig::error(N_(msg));
193 if(cb) cb->error(N_(msg));
194 else synfig::error(N_(msg));
200 // Close pipeout, not needed
202 // Dup pipeout to stdin
203 if( dup2( p[0], STDIN_FILENO ) == -1 ){
204 if(cb) cb->error(N_(msg));
205 else synfig::error(N_(msg));
208 // Close the unneeded pipeout
210 execlp("convert", "convert",
212 "-size", strprintf("%dx%d", desc.get_w(), desc.get_h()).c_str(),
213 ((channels(pf) == 4) ? "rgba:-[0]" : "rgb:-[0]"),
214 "-density", strprintf("%dx%d", round_to_int(desc.get_x_res()/39.3700787402), round_to_int(desc.get_y_res()/39.3700787402)).c_str(),
217 // We should never reach here unless the exec failed
218 if(cb) cb->error(N_(msg));
219 else synfig::error(N_(msg));
223 // Close pipein, not needed
225 // Save pipeout to file handle, will write to it later
226 file = fdopen(p[1], "wb");
230 #error There are no known APIs for creating child processes
235 if(cb)cb->error(N_(msg));
236 else synfig::error(N_(msg));
246 imagemagick_trgt::start_scanline(int /*scanline*/)
252 imagemagick_trgt::end_scanline(void)
257 convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
259 if(!fwrite(buffer,channels(pf),desc.get_w(),file))