4bcf1f8695b021f298fad127c58ee5784ce85160
[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 #elif defined(HAVE__SPAWNLP) && defined(HAVE__PIPE) && defined(HAVE_CWAIT)
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         string output="png32:"+temp_file;
108
109 #if defined(WIN32_PIPE_TO_PROCESSES)
110
111         if(filename.find("psd")!=String::npos)
112                 _spawnlp(_P_WAIT, "convert", "convert", filename.c_str(), "-flatten", output.c_str(), (const char *)NULL);
113         else
114                 _spawnlp(_P_WAIT, "convert", "convert", filename.c_str(), output.c_str(), (const char *)NULL);
115
116 #elif defined(UNIX_PIPE_TO_PROCESSES)
117
118         pid_t pid = fork();
119   
120         if (pid == -1) {
121                 return false;
122         }
123   
124         if (pid == 0){
125                 // Child process
126                 if(filename.find("psd")!=String::npos)
127                         execlp("convert", "convert", filename.c_str(), "-flatten", output.c_str(), (const char *)NULL);
128                 else
129                         execlp("convert", "convert", filename.c_str(), output.c_str(), (const char *)NULL);
130                 // We should never reach here unless the exec failed
131                 return false;
132         }
133
134         int status;
135         waitpid(pid, &status, 0);
136         if( (WIFEXITED(status) && WEXITSTATUS(status) != 0) || !WIFEXITED(status) )
137                 return false;
138
139 #else
140         #error There are no known APIs for creating child processes
141 #endif
142
143         Importer::Handle importer(Importer::open(temp_file));
144
145         if(!importer)
146         {
147                 if(cb)cb->error(_("Unable to open ")+temp_file);
148                 else synfig::error(_("Unable to open ")+temp_file);
149                 return false;
150         }
151
152         if(!importer->get_frame(surface,0,cb))
153         {
154                 if(cb)cb->error(_("Unable to get frame from ")+temp_file);
155                 else synfig::error(_("Unable to get frame from ")+temp_file);
156                 return false;
157         }
158
159         if(!surface)
160         {
161                 if(cb)cb->error(_("Bad surface from ")+temp_file);
162                 else synfig::error(_("Bad surface from ")+temp_file);
163                 return false;
164         }
165
166         if(1)
167         {
168                 // remove odd premultiplication
169                 for(int i=0;i<surface.get_w()*surface.get_h();i++)
170                 {
171                         Color c(surface[0][i]);
172
173                         if(c.get_a())
174                         {
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());
178                         }
179                         else
180                         {
181                                 surface[0][i].set_r(0);
182                                 surface[0][i].set_g(0);
183                                 surface[0][i].set_b(0);
184                         }
185                         surface[0][i].set_a(c.get_a());
186                 }
187         }
188
189         Surface bleh(surface);
190         surface=bleh;
191
192         //remove(temp_file.c_str());
193         return true;
194
195 #else
196         
197 #error This code contains tempfile and arbitrary shell command execution vulnerabilities
198         
199         if(file)
200                 pclose(file);
201
202         string command;
203
204         if(filename.empty())
205         {
206                 if(cb)cb->error(_("No file to load"));
207                 else synfig::error(_("No file to load"));
208                 return false;
209         }
210
211         command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
212
213         file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
214
215         if(!file)
216         {
217                 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
218                 else synfig::error(_("Unable to open pipe to imagemagick"));
219                 return false;
220         }
221         int w,h;
222         float divisor;
223         char cookie[2];
224
225         while((cookie[0]=fgetc(file))!='P' && !feof(file));
226
227         if(feof(file))
228         {
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"));
231                 return false;
232         }
233
234         cookie[1]=fgetc(file);
235
236         if(cookie[0]!='P' || cookie[1]!='6')
237         {
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]+'"');
240                 return false;
241         }
242
243         fgetc(file);
244         fscanf(file,"%d %d\n",&w,&h);
245         fscanf(file,"%f",&divisor);
246         fgetc(file);
247
248         if(feof(file))
249         {
250                 if(cb)cb->error(_("Premature end of file (after header)"));
251                 else synfig::error(_("Premature end of file (after header)"));
252                 return false;
253         }
254
255         int x;
256         int y;
257         frame.set_wh(w,h);
258         for(y=0;y<frame.get_h();y++)
259                 for(x=0;x<frame.get_w();x++)
260                 {
261                         if(feof(file))
262                         {
263                                 if(cb)cb->error(_("Premature end of file"));
264                                 else synfig::error(_("Premature end of file"));
265                                 return false;
266                         }
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));
270 /*
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;
274 */
275                         frame[y][x]=Color(
276                                 b,
277                                 g,
278                                 r,
279                                 1.0
280                         );
281                 }
282
283         surface=frame;
284
285         return true;
286 #endif
287
288
289 }