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>
49 /* === M A C R O S ========================================================= */
51 using namespace synfig;
55 /* === G L O B A L S ======================================================= */
57 SYNFIG_TARGET_INIT(imagemagick_trgt);
58 SYNFIG_TARGET_SET_NAME(imagemagick_trgt,"imagemagick");
59 SYNFIG_TARGET_SET_EXT(imagemagick_trgt,"miff");
60 SYNFIG_TARGET_SET_VERSION(imagemagick_trgt,"0.1");
61 SYNFIG_TARGET_SET_CVS_ID(imagemagick_trgt,"$Id$");
63 /* === M E T H O D S ======================================================= */
65 imagemagick_trgt::imagemagick_trgt(const char *Filename)
75 imagemagick_trgt::~imagemagick_trgt()
80 waitpid(pid,&status,0);
84 delete [] color_buffer;
88 imagemagick_trgt::set_rend_desc(RendDesc *given_desc)
90 if(filename_extension(filename) == ".xpm")
100 imagemagick_trgt::init()
102 imagecount=desc.get_frame_start();
103 if(desc.get_frame_end()-desc.get_frame_start()>0)
107 buffer=new unsigned char[channels(pf)*desc.get_w()];
108 delete [] color_buffer;
109 color_buffer=new Color[desc.get_w()];
114 imagemagick_trgt::end_frame()
122 waitpid(pid,&status,0);
129 imagemagick_trgt::start_frame(synfig::ProgressCallback *cb)
131 const char *msg=_("Unable to open pipe to imagemagick's convert utility");
136 newfilename = (filename_sans_extension(filename) +
137 etl::strprintf(".%04d",imagecount) +
138 filename_extension(filename));
140 newfilename = filename;
145 if(cb) cb->error(N_(msg));
146 else synfig::error(N_(msg));
153 if(cb) cb->error(N_(msg));
154 else synfig::error(N_(msg));
160 // Close pipeout, not needed
162 // Dup pipeout to stdin
163 if( dup2( p[0], STDIN_FILENO ) == -1 ){
164 if(cb) cb->error(N_(msg));
165 else synfig::error(N_(msg));
168 // Close the unneeded pipeout
170 execlp("convert", "convert",
172 "-size", strprintf("%dx%d", desc.get_w(), desc.get_h()).c_str(),
173 ((channels(pf) == 4) ? "rgba:-[0]" : "rgb:-[0]"),
174 "-density", strprintf("%dx%d", round_to_int(desc.get_x_res()/39.3700787402), round_to_int(desc.get_y_res()/39.3700787402)).c_str(),
177 // We should never reach here unless the exec failed
178 if(cb) cb->error(N_(msg));
179 else synfig::error(N_(msg));
183 // Close pipein, not needed
185 // Save pipeout to file handle, will write to it later
186 file = fdopen(p[1], "wb");
191 if(cb)cb->error(N_(msg));
192 else synfig::error(N_(msg));
202 imagemagick_trgt::start_scanline(int /*scanline*/)
208 imagemagick_trgt::end_scanline(void)
213 convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
215 if(!fwrite(buffer,channels(pf),desc.get_w(),file))