Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.08 / src / modules / mod_imagemagick / mptr_imagemagick.cpp
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
10 **
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.
15 **
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
21 **
22 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
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>
56
57 #endif
58
59 /* === M A C R O S ========================================================= */
60
61 using namespace synfig;
62 using namespace std;
63 using namespace etl;
64
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
70
71 /* === G L O B A L S ======================================================= */
72
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$");
78
79 /* === M E T H O D S ======================================================= */
80
81
82 imagemagick_mptr::imagemagick_mptr(const char *f)
83 {
84
85         filename=f;
86         file=NULL;
87 }
88
89 imagemagick_mptr::~imagemagick_mptr()
90 {
91         if(file)
92                 pclose(file);
93 }
94
95 bool
96 imagemagick_mptr::get_frame(synfig::Surface &surface,Time /*time*/, synfig::ProgressCallback *cb)
97 {
98 //#define HAS_LIBPNG 1
99
100 #if 1
101         if(filename.empty())
102         {
103                 if(cb)cb->error(_("No file to load"));
104                 else synfig::error(_("No file to load"));
105                 return false;
106         }
107         string temp_file="/tmp/deleteme.png";
108
109 #if defined(WIN32_PIPE_TO_PROCESSES)
110
111         if(file)
112                 pclose(file);
113
114         string command;
115         
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());
120
121         if(system(command.c_str())!=0)
122                 return false;
123
124 #elif defined(UNIX_PIPE_TO_PROCESSES)
125
126         string output="png32:"+temp_file;
127
128         pid_t pid = fork();
129   
130         if (pid == -1) {
131                 return false;
132         }
133   
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;
142         }
143
144         int status;
145         waitpid(pid, &status, 0);
146         if( (WIFEXITED(status) && WEXITSTATUS(status) != 0) || !WIFEXITED(status) )
147                 return false;
148
149 #else
150         #error There are no known APIs for creating child processes
151 #endif
152
153         Importer::Handle importer(Importer::open(temp_file));
154
155         if(!importer)
156         {
157                 if(cb)cb->error(_("Unable to open ")+temp_file);
158                 else synfig::error(_("Unable to open ")+temp_file);
159                 return false;
160         }
161
162         if(!importer->get_frame(surface,0,cb))
163         {
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;
167         }
168
169         if(!surface)
170         {
171                 if(cb)cb->error(_("Bad surface from ")+temp_file);
172                 else synfig::error(_("Bad surface from ")+temp_file);
173                 return false;
174         }
175
176         if(1)
177         {
178                 // remove odd premultiplication
179                 for(int i=0;i<surface.get_w()*surface.get_h();i++)
180                 {
181                         Color c(surface[0][i]);
182
183                         if(c.get_a())
184                         {
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());
188                         }
189                         else
190                         {
191                                 surface[0][i].set_r(0);
192                                 surface[0][i].set_g(0);
193                                 surface[0][i].set_b(0);
194                         }
195                         surface[0][i].set_a(c.get_a());
196                 }
197         }
198
199         Surface bleh(surface);
200         surface=bleh;
201
202         //remove(temp_file.c_str());
203         return true;
204
205 #else
206         
207 #error This code contains tempfile and arbitrary shell command execution vulnerabilities
208         
209         if(file)
210                 pclose(file);
211
212         string command;
213
214         if(filename.empty())
215         {
216                 if(cb)cb->error(_("No file to load"));
217                 else synfig::error(_("No file to load"));
218                 return false;
219         }
220
221         command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
222
223         file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
224
225         if(!file)
226         {
227                 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
228                 else synfig::error(_("Unable to open pipe to imagemagick"));
229                 return false;
230         }
231         int w,h;
232         float divisor;
233         char cookie[2];
234
235         while((cookie[0]=fgetc(file))!='P' && !feof(file));
236
237         if(feof(file))
238         {
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;
242         }
243
244         cookie[1]=fgetc(file);
245
246         if(cookie[0]!='P' || cookie[1]!='6')
247         {
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;
251         }
252
253         fgetc(file);
254         fscanf(file,"%d %d\n",&w,&h);
255         fscanf(file,"%f",&divisor);
256         fgetc(file);
257
258         if(feof(file))
259         {
260                 if(cb)cb->error(_("Premature end of file (after header)"));
261                 else synfig::error(_("Premature end of file (after header)"));
262                 return false;
263         }
264
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++)
270                 {
271                         if(feof(file))
272                         {
273                                 if(cb)cb->error(_("Premature end of file"));
274                                 else synfig::error(_("Premature end of file"));
275                                 return false;
276                         }
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));
280 /*
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;
284 */
285                         frame[y][x]=Color(
286                                 b,
287                                 g,
288                                 r,
289                                 1.0
290                         );
291                 }
292
293         surface=frame;
294
295         return true;
296 #endif
297
298
299 }