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
66 #elif defined(HAVE__SPAWNLP) && defined(HAVE__PIPE) && defined(HAVE_CWAIT)
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";
107 string output="png32:"+temp_file;
109 #if defined(WIN32_PIPE_TO_PROCESSES)
111 if(filename.find("psd")!=String::npos)
112 _spawnlp(_P_WAIT, "convert", "convert", filename.c_str(), "-flatten", output.c_str(), (const char *)NULL);
114 _spawnlp(_P_WAIT, "convert", "convert", filename.c_str(), output.c_str(), (const char *)NULL);
116 #elif defined(UNIX_PIPE_TO_PROCESSES)
126 if(filename.find("psd")!=String::npos)
127 execlp("convert", "convert", filename.c_str(), "-flatten", output.c_str(), (const char *)NULL);
129 execlp("convert", "convert", filename.c_str(), output.c_str(), (const char *)NULL);
130 // We should never reach here unless the exec failed
135 waitpid(pid, &status, 0);
136 if( (WIFEXITED(status) && WEXITSTATUS(status) != 0) || !WIFEXITED(status) )
140 #error There are no known APIs for creating child processes
143 Importer::Handle importer(Importer::open(temp_file));
147 if(cb)cb->error(_("Unable to open ")+temp_file);
148 else synfig::error(_("Unable to open ")+temp_file);
152 if(!importer->get_frame(surface,0,cb))
154 if(cb)cb->error(_("Unable to get frame from ")+temp_file);
155 else synfig::error(_("Unable to get frame from ")+temp_file);
161 if(cb)cb->error(_("Bad surface from ")+temp_file);
162 else synfig::error(_("Bad surface from ")+temp_file);
168 // remove odd premultiplication
169 for(int i=0;i<surface.get_w()*surface.get_h();i++)
171 Color c(surface[0][i]);
175 surface[0][i].set_r(c.get_r()/c.get_a()/c.get_a());
176 surface[0][i].set_g(c.get_g()/c.get_a()/c.get_a());
177 surface[0][i].set_b(c.get_b()/c.get_a()/c.get_a());
181 surface[0][i].set_r(0);
182 surface[0][i].set_g(0);
183 surface[0][i].set_b(0);
185 surface[0][i].set_a(c.get_a());
189 Surface bleh(surface);
192 //remove(temp_file.c_str());
197 #error This code contains tempfile and arbitrary shell command execution vulnerabilities
206 if(cb)cb->error(_("No file to load"));
207 else synfig::error(_("No file to load"));
211 command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
213 file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
217 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
218 else synfig::error(_("Unable to open pipe to imagemagick"));
225 while((cookie[0]=fgetc(file))!='P' && !feof(file));
229 if(cb)cb->error(_("Reached end of stream without finding PPM header"));
230 else synfig::error(_("Reached end of stream without finding PPM header"));
234 cookie[1]=fgetc(file);
236 if(cookie[0]!='P' || cookie[1]!='6')
238 if(cb)cb->error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
239 else synfig::error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
244 fscanf(file,"%d %d\n",&w,&h);
245 fscanf(file,"%f",&divisor);
250 if(cb)cb->error(_("Premature end of file (after header)"));
251 else synfig::error(_("Premature end of file (after header)"));
258 for(y=0;y<frame.get_h();y++)
259 for(x=0;x<frame.get_w();x++)
263 if(cb)cb->error(_("Premature end of file"));
264 else synfig::error(_("Premature end of file"));
267 float b=gamma().r_U8_to_F32((unsigned char)fgetc(file));
268 float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
269 float r=gamma().b_U8_to_F32((unsigned char)fgetc(file));
271 float b=(float)(unsigned char)fgetc(file)/divisor;
272 float g=(float)(unsigned char)fgetc(file)/divisor;
273 float r=(float)(unsigned char)fgetc(file)/divisor;