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
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 ======================================================= */
34 #include <ETL/stringf>
35 #include "mptr_imagemagick.h"
37 #include <sys/types.h>
53 #include <ETL/stringf>
54 #include <synfig/general.h>
58 /* === M A C R O S ========================================================= */
60 using namespace synfig;
64 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
65 #define UNIX_PIPE_TO_PROCESSES
67 #define WIN32_PIPE_TO_PROCESSES
70 /* === G L O B A L S ======================================================= */
72 SYNFIG_IMPORTER_INIT(imagemagick_mptr);
73 SYNFIG_IMPORTER_SET_NAME(imagemagick_mptr,"imagemagick");
74 SYNFIG_IMPORTER_SET_EXT(imagemagick_mptr,"miff");
75 SYNFIG_IMPORTER_SET_VERSION(imagemagick_mptr,"0.1");
76 SYNFIG_IMPORTER_SET_CVS_ID(imagemagick_mptr,"$Id$");
78 /* === M E T H O D S ======================================================= */
81 imagemagick_mptr::imagemagick_mptr(const char *f)
88 imagemagick_mptr::~imagemagick_mptr()
95 imagemagick_mptr::get_frame(synfig::Surface &surface,Time /*time*/, synfig::ProgressCallback *cb)
97 //#define HAS_LIBPNG 1
102 if(cb)cb->error(_("No file to load"));
103 else synfig::error(_("No file to load"));
106 string temp_file="/tmp/deleteme.png";
108 #if defined(WIN32_PIPE_TO_PROCESSES)
115 if(filename.find("psd")!=String::npos)
116 command=strprintf("convert \"%s\" -flatten \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
118 command=strprintf("convert \"%s\" \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
120 if(system(command.c_str())!=0)
123 #elif defined(UNIX_PIPE_TO_PROCESSES)
125 string output="png32:"+temp_file;
135 if(filename.find("psd")!=String::npos)
136 execlp("convert", "convert", filename.c_str(), "-flatten", output.c_str(), (const char *)NULL);
138 execlp("convert", "convert", filename.c_str(), output.c_str(), (const char *)NULL);
139 // We should never reach here unless the exec failed
144 waitpid(pid, &status, 0);
145 if( (WIFEXITED(status) && WEXITSTATUS(status) != 0) || !WIFEXITED(status) )
149 #error There are no known APIs for creating child processes
152 Importer::Handle importer(Importer::open(temp_file));
156 if(cb)cb->error(_("Unable to open ")+temp_file);
157 else synfig::error(_("Unable to open ")+temp_file);
161 if(!importer->get_frame(surface,0,cb))
163 if(cb)cb->error(_("Unable to get frame from ")+temp_file);
164 else synfig::error(_("Unable to get frame from ")+temp_file);
170 if(cb)cb->error(_("Bad surface from ")+temp_file);
171 else synfig::error(_("Bad surface from ")+temp_file);
177 // remove odd premultiplication
178 for(int i=0;i<surface.get_w()*surface.get_h();i++)
180 Color c(surface[0][i]);
184 surface[0][i].set_r(c.get_r()/c.get_a()/c.get_a());
185 surface[0][i].set_g(c.get_g()/c.get_a()/c.get_a());
186 surface[0][i].set_b(c.get_b()/c.get_a()/c.get_a());
190 surface[0][i].set_r(0);
191 surface[0][i].set_g(0);
192 surface[0][i].set_b(0);
194 surface[0][i].set_a(c.get_a());
198 Surface bleh(surface);
201 //remove(temp_file.c_str());
206 #error This code contains tempfile and arbitrary shell command execution vulnerabilities
215 if(cb)cb->error(_("No file to load"));
216 else synfig::error(_("No file to load"));
220 command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
222 file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
226 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
227 else synfig::error(_("Unable to open pipe to imagemagick"));
234 while((cookie[0]=fgetc(file))!='P' && !feof(file));
238 if(cb)cb->error(_("Reached end of stream without finding PPM header"));
239 else synfig::error(_("Reached end of stream without finding PPM header"));
243 cookie[1]=fgetc(file);
245 if(cookie[0]!='P' || cookie[1]!='6')
247 if(cb)cb->error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
248 else synfig::error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
253 fscanf(file,"%d %d\n",&w,&h);
254 fscanf(file,"%f",&divisor);
259 if(cb)cb->error(_("Premature end of file (after header)"));
260 else synfig::error(_("Premature end of file (after header)"));
267 for(y=0;y<frame.get_h();y++)
268 for(x=0;x<frame.get_w();x++)
272 if(cb)cb->error(_("Premature end of file"));
273 else synfig::error(_("Premature end of file"));
276 float b=gamma().r_U8_to_F32((unsigned char)fgetc(file));
277 float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
278 float r=gamma().b_U8_to_F32((unsigned char)fgetc(file));
280 float b=(float)(unsigned char)fgetc(file)/divisor;
281 float g=(float)(unsigned char)fgetc(file)/divisor;
282 float r=(float)(unsigned char)fgetc(file)/divisor;