921557f7a242aa01581b0406bfd865116722d54a
[synfig.git] / synfig-core / trunk / 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 **
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.
14 **
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.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include <ETL/stringf>
35 #include "mptr_imagemagick.h"
36 #include <stdio.h>
37 #include <sys/types.h>
38 #if HAVE_SYS_WAIT_H
39  #include <sys/wait.h>
40 #endif
41 #if HAVE_IO_H
42  #include <io.h>
43 #endif
44 #if HAVE_PROCESS_H
45  #include <process.h>
46 #endif
47 #if HAVE_FCNTL_H
48  #include <fcntl.h>
49 #endif
50 #include <unistd.h>
51 #include <algorithm>
52 #include <functional>
53 #include <ETL/stringf>
54 #include <synfig/general.h>
55
56 #endif
57
58 /* === M A C R O S ========================================================= */
59
60 using namespace synfig;
61 using namespace std;
62 using namespace etl;
63
64 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
65  #define UNIX_PIPE_TO_PROCESSES
66 #else
67  #define WIN32_PIPE_TO_PROCESSES
68 #endif
69
70 /* === G L O B A L S ======================================================= */
71
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$");
77
78 /* === M E T H O D S ======================================================= */
79
80
81 imagemagick_mptr::imagemagick_mptr(const char *f)
82 {
83
84         filename=f;
85         file=NULL;
86 }
87
88 imagemagick_mptr::~imagemagick_mptr()
89 {
90         if(file)
91                 pclose(file);
92 }
93
94 bool
95 imagemagick_mptr::get_frame(synfig::Surface &surface,Time /*time*/, synfig::ProgressCallback *cb)
96 {
97 //#define HAS_LIBPNG 1
98
99 #if 1
100         if(filename.empty())
101         {
102                 if(cb)cb->error(_("No file to load"));
103                 else synfig::error(_("No file to load"));
104                 return false;
105         }
106         string temp_file="/tmp/deleteme.png";
107
108 #if defined(WIN32_PIPE_TO_PROCESSES)
109
110         if(file)
111                 pclose(file);
112
113         string command;
114         
115         if(filename.find("psd")!=String::npos)
116                 command=strprintf("convert \"%s\" -flatten \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
117         else
118                 command=strprintf("convert \"%s\" \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
119
120         if(system(command.c_str())!=0)
121                 return false;
122
123 #elif defined(UNIX_PIPE_TO_PROCESSES)
124
125         string output="png32:"+temp_file;
126
127         pid_t pid = fork();
128   
129         if (pid == -1) {
130                 return false;
131         }
132   
133         if (pid == 0){
134                 // Child process
135                 if(filename.find("psd")!=String::npos)
136                         execlp("convert", "convert", filename.c_str(), "-flatten", output.c_str(), (const char *)NULL);
137                 else
138                         execlp("convert", "convert", filename.c_str(), output.c_str(), (const char *)NULL);
139                 // We should never reach here unless the exec failed
140                 return false;
141         }
142
143         int status;
144         waitpid(pid, &status, 0);
145         if( (WIFEXITED(status) && WEXITSTATUS(status) != 0) || !WIFEXITED(status) )
146                 return false;
147
148 #else
149         #error There are no known APIs for creating child processes
150 #endif
151
152         Importer::Handle importer(Importer::open(temp_file));
153
154         if(!importer)
155         {
156                 if(cb)cb->error(_("Unable to open ")+temp_file);
157                 else synfig::error(_("Unable to open ")+temp_file);
158                 return false;
159         }
160
161         if(!importer->get_frame(surface,0,cb))
162         {
163                 if(cb)cb->error(_("Unable to get frame from ")+temp_file);
164                 else synfig::error(_("Unable to get frame from ")+temp_file);
165                 return false;
166         }
167
168         if(!surface)
169         {
170                 if(cb)cb->error(_("Bad surface from ")+temp_file);
171                 else synfig::error(_("Bad surface from ")+temp_file);
172                 return false;
173         }
174
175         if(1)
176         {
177                 // remove odd premultiplication
178                 for(int i=0;i<surface.get_w()*surface.get_h();i++)
179                 {
180                         Color c(surface[0][i]);
181
182                         if(c.get_a())
183                         {
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());
187                         }
188                         else
189                         {
190                                 surface[0][i].set_r(0);
191                                 surface[0][i].set_g(0);
192                                 surface[0][i].set_b(0);
193                         }
194                         surface[0][i].set_a(c.get_a());
195                 }
196         }
197
198         Surface bleh(surface);
199         surface=bleh;
200
201         //remove(temp_file.c_str());
202         return true;
203
204 #else
205         
206 #error This code contains tempfile and arbitrary shell command execution vulnerabilities
207         
208         if(file)
209                 pclose(file);
210
211         string command;
212
213         if(filename.empty())
214         {
215                 if(cb)cb->error(_("No file to load"));
216                 else synfig::error(_("No file to load"));
217                 return false;
218         }
219
220         command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
221
222         file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
223
224         if(!file)
225         {
226                 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
227                 else synfig::error(_("Unable to open pipe to imagemagick"));
228                 return false;
229         }
230         int w,h;
231         float divisor;
232         char cookie[2];
233
234         while((cookie[0]=fgetc(file))!='P' && !feof(file));
235
236         if(feof(file))
237         {
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"));
240                 return false;
241         }
242
243         cookie[1]=fgetc(file);
244
245         if(cookie[0]!='P' || cookie[1]!='6')
246         {
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]+'"');
249                 return false;
250         }
251
252         fgetc(file);
253         fscanf(file,"%d %d\n",&w,&h);
254         fscanf(file,"%f",&divisor);
255         fgetc(file);
256
257         if(feof(file))
258         {
259                 if(cb)cb->error(_("Premature end of file (after header)"));
260                 else synfig::error(_("Premature end of file (after header)"));
261                 return false;
262         }
263
264         int x;
265         int y;
266         frame.set_wh(w,h);
267         for(y=0;y<frame.get_h();y++)
268                 for(x=0;x<frame.get_w();x++)
269                 {
270                         if(feof(file))
271                         {
272                                 if(cb)cb->error(_("Premature end of file"));
273                                 else synfig::error(_("Premature end of file"));
274                                 return false;
275                         }
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));
279 /*
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;
283 */
284                         frame[y][x]=Color(
285                                 b,
286                                 g,
287                                 r,
288                                 1.0
289                         );
290                 }
291
292         surface=frame;
293
294         return true;
295 #endif
296
297
298 }