Remove spaces and tabs at end of lines.
[synfig.git] / synfig-core / trunk / src / modules / mod_imagemagick / mptr_imagemagick.cpp
blob2860c1f97434413f6e4689fce916294cff52aa4f
1 /* === S Y N F I G ========================================================= */
2 /*! \file mptr_imagemagick.cpp
3 ** \brief ppm Target Module
4 **
5 ** $Id$
6 **
7 ** \legal
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.
20 ** \endlegal
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
28 #ifdef USING_PCH
29 # include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 # include <config.h>
33 #endif
35 #include <ETL/stringf>
36 #include "mptr_imagemagick.h"
37 #include <stdio.h>
38 #include <sys/types.h>
39 #if HAVE_SYS_WAIT_H
40 #include <sys/wait.h>
41 #endif
42 #if HAVE_IO_H
43 #include <io.h>
44 #endif
45 #if HAVE_PROCESS_H
46 #include <process.h>
47 #endif
48 #if HAVE_FCNTL_H
49 #include <fcntl.h>
50 #endif
51 #include <unistd.h>
52 #include <algorithm>
53 #include <functional>
54 #include <ETL/stringf>
55 #include <synfig/general.h>
57 #endif
59 /* === M A C R O S ========================================================= */
61 using namespace synfig;
62 using namespace std;
63 using namespace etl;
65 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
66 #define UNIX_PIPE_TO_PROCESSES
67 #else
68 #define WIN32_PIPE_TO_PROCESSES
69 #endif
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)
85 filename=f;
86 file=NULL;
89 imagemagick_mptr::~imagemagick_mptr()
91 if(file)
92 pclose(file);
95 bool
96 imagemagick_mptr::get_frame(synfig::Surface &surface,Time /*time*/, synfig::ProgressCallback *cb)
98 //#define HAS_LIBPNG 1
100 #if 1
101 if(filename.empty())
103 if(cb)cb->error(_("No file to load"));
104 else synfig::error(_("No file to load"));
105 return false;
107 string temp_file="/tmp/deleteme.png";
109 #if defined(WIN32_PIPE_TO_PROCESSES)
111 if(file)
112 pclose(file);
114 string command;
116 if(filename.find("psd")!=String::npos)
117 command=strprintf("convert \"%s\" -flatten \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
118 else
119 command=strprintf("convert \"%s\" \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
121 if(system(command.c_str())!=0)
122 return false;
124 #elif defined(UNIX_PIPE_TO_PROCESSES)
126 string output="png32:"+temp_file;
128 pid_t pid = fork();
130 if (pid == -1) {
131 return false;
134 if (pid == 0){
135 // Child process
136 if(filename.find("psd")!=String::npos)
137 execlp("convert", "convert", filename.c_str(), "-flatten", output.c_str(), (const char *)NULL);
138 else
139 execlp("convert", "convert", filename.c_str(), output.c_str(), (const char *)NULL);
140 // We should never reach here unless the exec failed
141 return false;
144 int status;
145 waitpid(pid, &status, 0);
146 if( (WIFEXITED(status) && WEXITSTATUS(status) != 0) || !WIFEXITED(status) )
147 return false;
149 #else
150 #error There are no known APIs for creating child processes
151 #endif
153 Importer::Handle importer(Importer::open(temp_file));
155 if(!importer)
157 if(cb)cb->error(_("Unable to open ")+temp_file);
158 else synfig::error(_("Unable to open ")+temp_file);
159 return false;
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);
166 return false;
169 if(!surface)
171 if(cb)cb->error(_("Bad surface from ")+temp_file);
172 else synfig::error(_("Bad surface from ")+temp_file);
173 return false;
176 if(1)
178 // remove odd premultiplication
179 for(int i=0;i<surface.get_w()*surface.get_h();i++)
181 Color c(surface[0][i]);
183 if(c.get_a())
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());
189 else
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);
200 surface=bleh;
202 //remove(temp_file.c_str());
203 return true;
205 #else
207 #error This code contains tempfile and arbitrary shell command execution vulnerabilities
209 if(file)
210 pclose(file);
212 string command;
214 if(filename.empty())
216 if(cb)cb->error(_("No file to load"));
217 else synfig::error(_("No file to load"));
218 return false;
221 command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
223 file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
225 if(!file)
227 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
228 else synfig::error(_("Unable to open pipe to imagemagick"));
229 return false;
231 int w,h;
232 float divisor;
233 char cookie[2];
235 while((cookie[0]=fgetc(file))!='P' && !feof(file));
237 if(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"));
241 return false;
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]+'"');
250 return false;
253 fgetc(file);
254 fscanf(file,"%d %d\n",&w,&h);
255 fscanf(file,"%f",&divisor);
256 fgetc(file);
258 if(feof(file))
260 if(cb)cb->error(_("Premature end of file (after header)"));
261 else synfig::error(_("Premature end of file (after header)"));
262 return false;
265 int x;
266 int y;
267 frame.set_wh(w,h);
268 for(y=0;y<frame.get_h();y++)
269 for(x=0;x<frame.get_w();x++)
271 if(feof(file))
273 if(cb)cb->error(_("Premature end of file"));
274 else synfig::error(_("Premature end of file"));
275 return false;
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;
285 frame[y][x]=Color(
293 surface=frame;
295 return true;
296 #endif