Remove spaces and tabs at end of lines.
[synfig.git] / synfig-core / trunk / 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 {
85         pid=-1;
86         file=NULL;
87         filename=Filename;
88         multi_image=false;
89         buffer=NULL;
90         color_buffer=0;
91 }
92
93 imagemagick_trgt::~imagemagick_trgt()
94 {
95         if(file){
96 #if defined(WIN32_PIPE_TO_PROCESSES)
97                 pclose(file);
98 #elif defined(UNIX_PIPE_TO_PROCESSES)
99                 fclose(file);
100                 int status;
101                 waitpid(pid,&status,0);
102 #endif
103         }
104         file=NULL;
105         delete [] buffer;
106         delete [] color_buffer;
107 }
108
109 bool
110 imagemagick_trgt::set_rend_desc(RendDesc *given_desc)
111 {
112         if(filename_extension(filename) == ".xpm")
113                 pf=PF_RGB;
114         else
115                 pf=PF_RGB|PF_A;
116
117         desc=*given_desc;
118         return true;
119 }
120
121 bool
122 imagemagick_trgt::init()
123 {
124         imagecount=desc.get_frame_start();
125         if(desc.get_frame_end()-desc.get_frame_start()>0)
126                 multi_image=true;
127
128         delete [] buffer;
129         buffer=new unsigned char[channels(pf)*desc.get_w()];
130         delete [] color_buffer;
131         color_buffer=new Color[desc.get_w()];
132         return true;
133 }
134
135 void
136 imagemagick_trgt::end_frame()
137 {
138         if(file)
139         {
140                 fputc(0,file);
141                 fflush(file);
142 #if defined(WIN32_PIPE_TO_PROCESSES)
143                 pclose(file);
144 #elif defined(UNIX_PIPE_TO_PROCESSES)
145                 fclose(file);
146                 int status;
147                 waitpid(pid,&status,0);
148 #endif
149         }
150         file=NULL;
151         imagecount++;
152 }
153
154 bool
155 imagemagick_trgt::start_frame(synfig::ProgressCallback *cb)
156 {
157         const char *msg=_("Unable to open pipe to imagemagick's convert utility");
158
159         string newfilename;
160
161         if (multi_image)
162                 newfilename = (filename_sans_extension(filename) +
163                                            etl::strprintf(".%04d",imagecount) +
164                                            filename_extension(filename));
165         else
166                 newfilename = filename;
167
168 #if defined(WIN32_PIPE_TO_PROCESSES)
169
170         string command;
171
172         command=strprintf("convert -depth 8 -size %dx%d rgb%s:-[0] -density %dx%d \"%s\"\n",
173                           desc.get_w(), desc.get_h(),                                   // size
174                           ((channels(pf) == 4) ? "a" : ""),                             // rgba or rgb?
175                           round_to_int(desc.get_x_res()/39.3700787402), // density
176                           round_to_int(desc.get_y_res()/39.3700787402),
177                           newfilename.c_str());
178
179         file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
180
181 #elif defined(UNIX_PIPE_TO_PROCESSES)
182
183         int p[2];
184
185         if (pipe(p)) {
186                 if(cb) cb->error(N_(msg));
187                 else synfig::error(N_(msg));
188                 return false;
189         };
190
191         pid = fork();
192
193         if (pid == -1) {
194                 if(cb) cb->error(N_(msg));
195                 else synfig::error(N_(msg));
196                 return false;
197         }
198
199         if (pid == 0){
200                 // Child process
201                 // Close pipeout, not needed
202                 close(p[1]);
203                 // Dup pipeout to stdin
204                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
205                         if(cb) cb->error(N_(msg));
206                         else synfig::error(N_(msg));
207                         return false;
208                 }
209                 // Close the unneeded pipeout
210                 close(p[0]);
211                 execlp("convert", "convert",
212                         "-depth", "8",
213                         "-size", strprintf("%dx%d", desc.get_w(), desc.get_h()).c_str(),
214                         ((channels(pf) == 4) ? "rgba:-[0]" : "rgb:-[0]"),
215                         "-density", strprintf("%dx%d", round_to_int(desc.get_x_res()/39.3700787402), round_to_int(desc.get_y_res()/39.3700787402)).c_str(),
216                         newfilename.c_str(),
217                         (const char *)NULL);
218                 // We should never reach here unless the exec failed
219                 if(cb) cb->error(N_(msg));
220                 else synfig::error(N_(msg));
221                 return false;
222         } else {
223                 // Parent process
224                 // Close pipein, not needed
225                 close(p[0]);
226                 // Save pipeout to file handle, will write to it later
227                 file = fdopen(p[1], "wb");
228         }
229
230 #else
231         #error There are no known APIs for creating child processes
232 #endif
233
234         if(!file)
235         {
236                 if(cb)cb->error(N_(msg));
237                 else synfig::error(N_(msg));
238                 return false;
239         }
240
241         //etl::yield();
242
243         return true;
244 }
245
246 Color *
247 imagemagick_trgt::start_scanline(int /*scanline*/)
248 {
249         return color_buffer;
250 }
251
252 bool
253 imagemagick_trgt::end_scanline(void)
254 {
255         if(!file)
256                 return false;
257
258         convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
259
260         if(!fwrite(buffer,channels(pf),desc.get_w(),file))
261                 return false;
262
263         return true;
264 }