1 /* === S Y N F I G ========================================================= */
2 /*! \file mptr_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 "mptr_imagemagick.h"
38 #include <sys/types.h>
54 #include <ETL/stringf>
55 #include <synfig/general.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_IMPORTER_INIT(imagemagick_mptr);
74 SYNFIG_IMPORTER_SET_NAME(imagemagick_mptr,"imagemagick");
75 SYNFIG_IMPORTER_SET_EXT(imagemagick_mptr,"miff");
76 SYNFIG_IMPORTER_SET_VERSION(imagemagick_mptr,"0.1");
77 SYNFIG_IMPORTER_SET_CVS_ID(imagemagick_mptr,"$Id$");
79 /* === M E T H O D S ======================================================= */
82 imagemagick_mptr::imagemagick_mptr(const char *f)
89 imagemagick_mptr::~imagemagick_mptr()
96 imagemagick_mptr::get_frame(synfig::Surface &surface,Time /*time*/, synfig::ProgressCallback *cb)
98 //#define HAS_LIBPNG 1
103 if(cb)cb->error(_("No file to load"));
104 else synfig::error(_("No file to load"));
107 string temp_file="/tmp/deleteme.png";
109 #if defined(WIN32_PIPE_TO_PROCESSES)
116 if(filename.find("psd")!=String::npos)
117 command=strprintf("convert \"%s\" -flatten \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
119 command=strprintf("convert \"%s\" \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
121 if(system(command.c_str())!=0)
124 #elif defined(UNIX_PIPE_TO_PROCESSES)
126 string output="png32:"+temp_file;
136 if(filename.find("psd")!=String::npos)
137 execlp("convert", "convert", filename.c_str(), "-flatten", output.c_str(), (const char *)NULL);
139 execlp("convert", "convert", filename.c_str(), output.c_str(), (const char *)NULL);
140 // We should never reach here unless the exec failed
145 waitpid(pid, &status, 0);
146 if( (WIFEXITED(status) && WEXITSTATUS(status) != 0) || !WIFEXITED(status) )
150 #error There are no known APIs for creating child processes
153 Importer::Handle importer(Importer::open(temp_file));
157 if(cb)cb->error(_("Unable to open ")+temp_file);
158 else synfig::error(_("Unable to open ")+temp_file);
162 if(!importer->get_frame(surface,0,cb))
164 if(cb)cb->error(_("Unable to get frame from ")+temp_file);
165 else synfig::error(_("Unable to get frame from ")+temp_file);
171 if(cb)cb->error(_("Bad surface from ")+temp_file);
172 else synfig::error(_("Bad surface from ")+temp_file);
178 // remove odd premultiplication
179 for(int i=0;i<surface.get_w()*surface.get_h();i++)
181 Color c(surface[0][i]);
185 surface[0][i].set_r(c.get_r()/c.get_a()/c.get_a());
186 surface[0][i].set_g(c.get_g()/c.get_a()/c.get_a());
187 surface[0][i].set_b(c.get_b()/c.get_a()/c.get_a());
191 surface[0][i].set_r(0);
192 surface[0][i].set_g(0);
193 surface[0][i].set_b(0);
195 surface[0][i].set_a(c.get_a());
199 Surface bleh(surface);
202 //remove(temp_file.c_str());
207 #error This code contains tempfile and arbitrary shell command execution vulnerabilities
216 if(cb)cb->error(_("No file to load"));
217 else synfig::error(_("No file to load"));
221 command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
223 file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
227 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
228 else synfig::error(_("Unable to open pipe to imagemagick"));
235 while((cookie[0]=fgetc(file))!='P' && !feof(file));
239 if(cb)cb->error(_("Reached end of stream without finding PPM header"));
240 else synfig::error(_("Reached end of stream without finding PPM header"));
244 cookie[1]=fgetc(file);
246 if(cookie[0]!='P' || cookie[1]!='6')
248 if(cb)cb->error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
249 else synfig::error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
254 fscanf(file,"%d %d\n",&w,&h);
255 fscanf(file,"%f",&divisor);
260 if(cb)cb->error(_("Premature end of file (after header)"));
261 else synfig::error(_("Premature end of file (after header)"));
268 for(y=0;y<frame.get_h();y++)
269 for(x=0;x<frame.get_w();x++)
273 if(cb)cb->error(_("Premature end of file"));
274 else synfig::error(_("Premature end of file"));
277 float b=gamma().r_U8_to_F32((unsigned char)fgetc(file));
278 float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
279 float r=gamma().b_U8_to_F32((unsigned char)fgetc(file));
281 float b=(float)(unsigned char)fgetc(file)/divisor;
282 float g=(float)(unsigned char)fgetc(file)/divisor;
283 float r=(float)(unsigned char)fgetc(file)/divisor;