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>
42 #include <ETL/stringf>
43 #include <synfig/general.h>
47 /* === M A C R O S ========================================================= */
49 using namespace synfig;
53 /* === G L O B A L S ======================================================= */
55 SYNFIG_IMPORTER_INIT(imagemagick_mptr);
56 SYNFIG_IMPORTER_SET_NAME(imagemagick_mptr,"imagemagick");
57 SYNFIG_IMPORTER_SET_EXT(imagemagick_mptr,"miff");
58 SYNFIG_IMPORTER_SET_VERSION(imagemagick_mptr,"0.1");
59 SYNFIG_IMPORTER_SET_CVS_ID(imagemagick_mptr,"$Id$");
61 /* === M E T H O D S ======================================================= */
64 imagemagick_mptr::imagemagick_mptr(const char *f)
71 imagemagick_mptr::~imagemagick_mptr()
78 imagemagick_mptr::get_frame(synfig::Surface &surface,Time /*time*/, synfig::ProgressCallback *cb)
80 //#define HAS_LIBPNG 1
85 if(cb)cb->error(_("No file to load"));
86 else synfig::error(_("No file to load"));
89 string temp_file="/tmp/deleteme.png";
90 string output="png32:"+temp_file;
100 if(filename.find("psd")!=String::npos)
101 execlp("convert", "convert", filename.c_str(), "-flatten", output.c_str(), (const char *)NULL);
103 execlp("convert", "convert", filename.c_str(), output.c_str(), (const char *)NULL);
104 // We should never reach here unless the exec failed
109 waitpid(pid, &status, 0);
110 if( (WIFEXITED(status) && WEXITSTATUS(status) != 0) || !WIFEXITED(status) )
113 Importer::Handle importer(Importer::open(temp_file));
119 if(cb)cb->error(_("Unable to open ")+temp_file);
120 else synfig::error(_("Unable to open ")+temp_file);
126 if(!importer->get_frame(surface,0,cb))
128 if(cb)cb->error(_("Unable to get frame from ")+temp_file);
129 else synfig::error(_("Unable to get frame from ")+temp_file);
135 if(cb)cb->error(_("Bad surface from ")+temp_file);
136 else synfig::error(_("Bad surface from ")+temp_file);
142 // remove odd premultiplication
143 for(int i=0;i<surface.get_w()*surface.get_h();i++)
145 Color c(surface[0][i]);
149 surface[0][i].set_r(c.get_r()/c.get_a()/c.get_a());
150 surface[0][i].set_g(c.get_g()/c.get_a()/c.get_a());
151 surface[0][i].set_b(c.get_b()/c.get_a()/c.get_a());
155 surface[0][i].set_r(0);
156 surface[0][i].set_g(0);
157 surface[0][i].set_b(0);
159 surface[0][i].set_a(c.get_a());
163 Surface bleh(surface);
167 //remove(temp_file.c_str());
173 #error This code contains tempfile and arbitrary shell command execution vulnerabilities
182 if(cb)cb->error(_("No file to load"));
183 else synfig::error(_("No file to load"));
187 command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
189 file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
193 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
194 else synfig::error(_("Unable to open pipe to imagemagick"));
201 while((cookie[0]=fgetc(file))!='P' && !feof(file));
205 if(cb)cb->error(_("Reached end of stream without finding PPM header"));
206 else synfig::error(_("Reached end of stream without finding PPM header"));
210 cookie[1]=fgetc(file);
212 if(cookie[0]!='P' || cookie[1]!='6')
214 if(cb)cb->error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
215 else synfig::error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
220 fscanf(file,"%d %d\n",&w,&h);
221 fscanf(file,"%f",&divisor);
226 if(cb)cb->error(_("Premature end of file (after header)"));
227 else synfig::error(_("Premature end of file (after header)"));
234 for(y=0;y<frame.get_h();y++)
235 for(x=0;x<frame.get_w();x++)
239 if(cb)cb->error(_("Premature end of file"));
240 else synfig::error(_("Premature end of file"));
243 float b=gamma().r_U8_to_F32((unsigned char)fgetc(file));
244 float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
245 float r=gamma().b_U8_to_F32((unsigned char)fgetc(file));
247 float b=(float)(unsigned char)fgetc(file)/divisor;
248 float g=(float)(unsigned char)fgetc(file)/divisor;
249 float r=(float)(unsigned char)fgetc(file)/divisor;