c945ee538f9809483995ba999477f11e6a7760ca
[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         DEBUGPOINT();
146
147         if(!importer)
148         {
149                 if(cb)cb->error(_("Unable to open ")+temp_file);
150                 else synfig::error(_("Unable to open ")+temp_file);
151                 return false;
152         }
153
154         DEBUGPOINT();
155
156         if(!importer->get_frame(surface,0,cb))
157         {
158                 if(cb)cb->error(_("Unable to get frame from ")+temp_file);
159                 else synfig::error(_("Unable to get frame from ")+temp_file);
160                 return false;
161         }
162
163         if(!surface)
164         {
165                 if(cb)cb->error(_("Bad surface from ")+temp_file);
166                 else synfig::error(_("Bad surface from ")+temp_file);
167                 return false;
168         }
169
170         if(1)
171         {
172                 // remove odd premultiplication
173                 for(int i=0;i<surface.get_w()*surface.get_h();i++)
174                 {
175                         Color c(surface[0][i]);
176
177                         if(c.get_a())
178                         {
179                                 surface[0][i].set_r(c.get_r()/c.get_a()/c.get_a());
180                                 surface[0][i].set_g(c.get_g()/c.get_a()/c.get_a());
181                                 surface[0][i].set_b(c.get_b()/c.get_a()/c.get_a());
182                         }
183                         else
184                         {
185                                 surface[0][i].set_r(0);
186                                 surface[0][i].set_g(0);
187                                 surface[0][i].set_b(0);
188                         }
189                         surface[0][i].set_a(c.get_a());
190                 }
191         }
192
193         Surface bleh(surface);
194         surface=bleh;
195
196
197         //remove(temp_file.c_str());
198         DEBUGPOINT();
199         return true;
200
201 #else
202         
203 #error This code contains tempfile and arbitrary shell command execution vulnerabilities
204         
205         if(file)
206                 pclose(file);
207
208         string command;
209
210         if(filename.empty())
211         {
212                 if(cb)cb->error(_("No file to load"));
213                 else synfig::error(_("No file to load"));
214                 return false;
215         }
216
217         command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
218
219         file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
220
221         if(!file)
222         {
223                 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
224                 else synfig::error(_("Unable to open pipe to imagemagick"));
225                 return false;
226         }
227         int w,h;
228         float divisor;
229         char cookie[2];
230
231         while((cookie[0]=fgetc(file))!='P' && !feof(file));
232
233         if(feof(file))
234         {
235                 if(cb)cb->error(_("Reached end of stream without finding PPM header"));
236                 else synfig::error(_("Reached end of stream without finding PPM header"));
237                 return false;
238         }
239
240         cookie[1]=fgetc(file);
241
242         if(cookie[0]!='P' || cookie[1]!='6')
243         {
244                 if(cb)cb->error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
245                 else synfig::error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
246                 return false;
247         }
248
249         fgetc(file);
250         fscanf(file,"%d %d\n",&w,&h);
251         fscanf(file,"%f",&divisor);
252         fgetc(file);
253
254         if(feof(file))
255         {
256                 if(cb)cb->error(_("Premature end of file (after header)"));
257                 else synfig::error(_("Premature end of file (after header)"));
258                 return false;
259         }
260
261         int x;
262         int y;
263         frame.set_wh(w,h);
264         for(y=0;y<frame.get_h();y++)
265                 for(x=0;x<frame.get_w();x++)
266                 {
267                         if(feof(file))
268                         {
269                                 if(cb)cb->error(_("Premature end of file"));
270                                 else synfig::error(_("Premature end of file"));
271                                 return false;
272                         }
273                         float b=gamma().r_U8_to_F32((unsigned char)fgetc(file));
274                         float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
275                         float r=gamma().b_U8_to_F32((unsigned char)fgetc(file));
276 /*
277                         float b=(float)(unsigned char)fgetc(file)/divisor;
278                         float g=(float)(unsigned char)fgetc(file)/divisor;
279                         float r=(float)(unsigned char)fgetc(file)/divisor;
280 */
281                         frame[y][x]=Color(
282                                 b,
283                                 g,
284                                 r,
285                                 1.0
286                         );
287                 }
288
289         surface=frame;
290
291         return true;
292 #endif
293
294
295 }