Merge branch 'nikitakit_master'
[synfig.git] / synfig-core / src / modules / mod_imagemagick / trgt_imagemagick.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_imagemagick.cpp
3 **      \brief ppm Target Module
4 **
5 **      \legal
6 ** $Id$
7 **
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 #define SYNFIG_TARGET
29
30 #ifdef USING_PCH
31 #       include "pch.h"
32 #else
33 #ifdef HAVE_CONFIG_H
34 #       include <config.h>
35 #endif
36
37 #include <ETL/stringf>
38 #include "trgt_imagemagick.h"
39 #include <stdio.h>
40 #include <sys/types.h>
41 #if HAVE_SYS_WAIT_H
42  #include <sys/wait.h>
43 #endif
44 #if HAVE_IO_H
45  #include <io.h>
46 #endif
47 #if HAVE_PROCESS_H
48  #include <process.h>
49 #endif
50 #if HAVE_FCNTL_H
51  #include <fcntl.h>
52 #endif
53 #include <unistd.h>
54 #include <algorithm>
55 #include <functional>
56 #include <ETL/clock>
57 #include <ETL/misc>
58
59 #endif
60
61 /* === M A C R O S ========================================================= */
62
63 using namespace synfig;
64 using namespace std;
65 using namespace etl;
66
67 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
68  #define UNIX_PIPE_TO_PROCESSES
69 #else
70  #define WIN32_PIPE_TO_PROCESSES
71 #endif
72
73 /* === G L O B A L S ======================================================= */
74
75 SYNFIG_TARGET_INIT(imagemagick_trgt);
76 SYNFIG_TARGET_SET_NAME(imagemagick_trgt,"imagemagick");
77 SYNFIG_TARGET_SET_EXT(imagemagick_trgt,"miff");
78 SYNFIG_TARGET_SET_VERSION(imagemagick_trgt,"0.1");
79 SYNFIG_TARGET_SET_CVS_ID(imagemagick_trgt,"$Id$");
80
81 /* === M E T H O D S ======================================================= */
82
83 imagemagick_trgt::imagemagick_trgt(const char *Filename,
84                                                                    const synfig::TargetParam& /* params */)
85 {
86         pid=-1;
87         file=NULL;
88         filename=Filename;
89         multi_image=false;
90         buffer=NULL;
91         color_buffer=0;
92 }
93
94 imagemagick_trgt::~imagemagick_trgt()
95 {
96         if(file){
97 #if defined(WIN32_PIPE_TO_PROCESSES)
98                 pclose(file);
99 #elif defined(UNIX_PIPE_TO_PROCESSES)
100                 fclose(file);
101                 int status;
102                 waitpid(pid,&status,0);
103 #endif
104         }
105         file=NULL;
106         delete [] buffer;
107         delete [] color_buffer;
108 }
109
110 bool
111 imagemagick_trgt::set_rend_desc(RendDesc *given_desc)
112 {
113         if(filename_extension(filename) == ".xpm")
114                 pf=PF_RGB;
115         else
116                 pf=PF_RGB|PF_A;
117
118         desc=*given_desc;
119         return true;
120 }
121
122 bool
123 imagemagick_trgt::init()
124 {
125         imagecount=desc.get_frame_start();
126         if(desc.get_frame_end()-desc.get_frame_start()>0)
127                 multi_image=true;
128
129         delete [] buffer;
130         buffer=new unsigned char[channels(pf)*desc.get_w()];
131         delete [] color_buffer;
132         color_buffer=new Color[desc.get_w()];
133         return true;
134 }
135
136 void
137 imagemagick_trgt::end_frame()
138 {
139         if(file)
140         {
141                 fputc(0,file);
142                 fflush(file);
143 #if defined(WIN32_PIPE_TO_PROCESSES)
144                 pclose(file);
145 #elif defined(UNIX_PIPE_TO_PROCESSES)
146                 fclose(file);
147                 int status;
148                 waitpid(pid,&status,0);
149 #endif
150         }
151         file=NULL;
152         imagecount++;
153 }
154
155 bool
156 imagemagick_trgt::start_frame(synfig::ProgressCallback *cb)
157 {
158         const char *msg=_("Unable to open pipe to imagemagick's convert utility");
159
160         string newfilename;
161
162         if (multi_image)
163                 newfilename = (filename_sans_extension(filename) +
164                                            etl::strprintf(".%04d",imagecount) +
165                                            filename_extension(filename));
166         else
167                 newfilename = filename;
168
169 #if defined(WIN32_PIPE_TO_PROCESSES)
170
171         string command;
172
173         command=strprintf("convert -depth 8 -size %dx%d rgb%s:-[0] -density %dx%d \"%s\"\n",
174                           desc.get_w(), desc.get_h(),                                   // size
175                           ((channels(pf) == 4) ? "a" : ""),                             // rgba or rgb?
176                           round_to_int(desc.get_x_res()/39.3700787402), // density
177                           round_to_int(desc.get_y_res()/39.3700787402),
178                           newfilename.c_str());
179
180         file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
181
182 #elif defined(UNIX_PIPE_TO_PROCESSES)
183
184         int p[2];
185
186         if (pipe(p)) {
187                 if(cb) cb->error(N_(msg));
188                 else synfig::error(N_(msg));
189                 return false;
190         };
191
192         pid = fork();
193
194         if (pid == -1) {
195                 if(cb) cb->error(N_(msg));
196                 else synfig::error(N_(msg));
197                 return false;
198         }
199
200         if (pid == 0){
201                 // Child process
202                 // Close pipeout, not needed
203                 close(p[1]);
204                 // Dup pipeout to stdin
205                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
206                         if(cb) cb->error(N_(msg));
207                         else synfig::error(N_(msg));
208                         return false;
209                 }
210                 // Close the unneeded pipeout
211                 close(p[0]);
212                 execlp("convert", "convert",
213                         "-depth", "8",
214                         "-size", strprintf("%dx%d", desc.get_w(), desc.get_h()).c_str(),
215                         ((channels(pf) == 4) ? "rgba:-[0]" : "rgb:-[0]"),
216                         "-density", strprintf("%dx%d", round_to_int(desc.get_x_res()/39.3700787402), round_to_int(desc.get_y_res()/39.3700787402)).c_str(),
217                         newfilename.c_str(),
218                         (const char *)NULL);
219                 // We should never reach here unless the exec failed
220                 if(cb) cb->error(N_(msg));
221                 else synfig::error(N_(msg));
222                 return false;
223         } else {
224                 // Parent process
225                 // Close pipein, not needed
226                 close(p[0]);
227                 // Save pipeout to file handle, will write to it later
228                 file = fdopen(p[1], "wb");
229         }
230
231 #else
232         #error There are no known APIs for creating child processes
233 #endif
234
235         if(!file)
236         {
237                 if(cb)cb->error(N_(msg));
238                 else synfig::error(N_(msg));
239                 return false;
240         }
241
242         //etl::yield();
243
244         return true;
245 }
246
247 Color *
248 imagemagick_trgt::start_scanline(int /*scanline*/)
249 {
250         return color_buffer;
251 }
252
253 bool
254 imagemagick_trgt::end_scanline(void)
255 {
256         if(!file)
257                 return false;
258
259         convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
260
261         if(!fwrite(buffer,channels(pf),desc.get_w(),file))
262                 return false;
263
264         return true;
265 }