Remove spaces and tabs at end of lines.
[synfig.git] / synfig-core / trunk / src / modules / mod_dv / trgt_dv.cpp
blob13fed5c844d4a9a79c5de67d745c4f753c0b9717
1 /* === S Y N F I G ========================================================= */
2 /*! \file trgt_dv.cpp
3 ** \brief ppm Target Module
4 **
5 ** $Id$
6 **
7 ** \legal
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
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.
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
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
28 #define SYNFIG_TARGET
30 #ifdef USING_PCH
31 # include "pch.h"
32 #else
33 #ifdef HAVE_CONFIG_H
34 # include <config.h>
35 #endif
37 #include <ETL/stringf>
38 #include "trgt_dv.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>
58 #endif
60 /* === M A C R O S ========================================================= */
62 using namespace synfig;
63 using namespace std;
64 using namespace etl;
66 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
67 #define UNIX_PIPE_TO_PROCESSES
68 #else
69 #define WIN32_PIPE_TO_PROCESSES
70 #endif
72 /* === G L O B A L S ======================================================= */
74 SYNFIG_TARGET_INIT(dv_trgt);
75 SYNFIG_TARGET_SET_NAME(dv_trgt,"dv");
76 SYNFIG_TARGET_SET_EXT(dv_trgt,"dv");
77 SYNFIG_TARGET_SET_VERSION(dv_trgt,"0.1");
78 SYNFIG_TARGET_SET_CVS_ID(dv_trgt,"$Id$");
80 /* === M E T H O D S ======================================================= */
83 dv_trgt::dv_trgt(const char *Filename)
85 pid=-1;
86 file=NULL;
87 filename=Filename;
88 buffer=NULL;
89 wide_aspect=false;
90 color_buffer=0;
91 set_remove_alpha();
95 dv_trgt::~dv_trgt()
97 if(file){
98 #if defined(WIN32_PIPE_TO_PROCESSES)
99 pclose(file);
100 #elif defined(UNIX_PIPE_TO_PROCESSES)
101 fclose(file);
102 int status;
103 waitpid(pid,&status,0);
104 #endif
106 file=NULL;
107 delete [] buffer;
108 delete [] color_buffer;
111 bool
112 dv_trgt::set_rend_desc(RendDesc *given_desc)
114 // Set the aspect ratio
115 if(wide_aspect)
117 // 16:9 Aspect
118 given_desc->set_wh(160,90);
120 // Widescreen should be progressive scan
121 given_desc->set_interlaced(false);
123 else
125 // 4:3 Aspect
126 given_desc->set_wh(400,300);
128 // We should be interlaced
129 given_desc->set_interlaced(true);
132 // but the pixel res should be 720x480
133 given_desc->clear_flags(),given_desc->set_wh(720,480);
135 // NTSC Frame rate is 29.97
136 given_desc->set_frame_rate(29.97);
138 // The pipe to encodedv is PPM, which needs RGB data
139 //given_desc->set_pixel_format(PF_RGB);
141 // Set the description
142 desc=*given_desc;
144 return true;
147 bool
148 dv_trgt::init()
150 imagecount=desc.get_frame_start();
152 #if defined(WIN32_PIPE_TO_PROCESSES)
154 string command;
156 if(wide_aspect)
157 command=strprintf("encodedv -w 1 - > \"%s\"\n",filename.c_str());
158 else
159 command=strprintf("encodedv - > \"%s\"\n",filename.c_str());
161 // Open the pipe to encodedv
162 file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
164 if(!file)
166 synfig::error(_("Unable to open pipe to encodedv"));
167 return false;
170 #elif defined(UNIX_PIPE_TO_PROCESSES)
172 int p[2];
174 if (pipe(p)) {
175 synfig::error(_("Unable to open pipe to encodedv"));
176 return false;
179 pid_t pid = fork();
181 if (pid == -1) {
182 synfig::error(_("Unable to open pipe to encodedv"));
183 return false;
186 if (pid == 0){
187 // Child process
188 // Close pipeout, not needed
189 close(p[1]);
190 // Dup pipeout to stdin
191 if( dup2( p[0], STDIN_FILENO ) == -1 ){
192 synfig::error(_("Unable to open pipe to encodedv"));
193 return false;
195 // Close the unneeded pipeout
196 close(p[0]);
197 // Open filename to stdout
198 FILE* outfile = fopen(filename.c_str(),"wb");
199 if( outfile == NULL ){
200 synfig::error(_("Unable to open pipe to encodedv"));
201 return false;
203 int outfilefd = fileno(outfile);
204 if( outfilefd == -1 ){
205 synfig::error(_("Unable to open pipe to encodedv"));
206 return false;
208 if( dup2( outfilefd, STDOUT_FILENO ) == -1 ){
209 synfig::error(_("Unable to open pipe to encodedv"));
210 return false;
213 if(wide_aspect)
214 execlp("encodedv", "encodedv", "-w", "1", "-", (const char *)NULL);
215 else
216 execlp("encodedv", "encodedv", "-", (const char *)NULL);
217 // We should never reach here unless the exec failed
218 synfig::error(_("Unable to open pipe to encodedv"));
219 return false;
220 } else {
221 // Parent process
222 // Close pipein, not needed
223 close(p[0]);
224 // Save pipeout to file handle, will write to it later
225 file = fdopen(p[1], "wb");
226 if (file == NULL) {
227 synfig::error(_("Unable to open pipe to encodedv"));
228 return false;
232 #else
233 #error There are no known APIs for creating child processes
234 #endif
237 // Sleep for a moment to let the pipe catch up
238 etl::clock().sleep(0.25f);
240 return true;
243 void
244 dv_trgt::end_frame()
246 fprintf(file, " ");
247 fflush(file);
248 imagecount++;
251 bool
252 dv_trgt::start_frame(synfig::ProgressCallback */*callback*/)
254 int w=desc.get_w(),h=desc.get_h();
256 if(!file)
257 return false;
259 fprintf(file, "P6\n");
260 fprintf(file, "%d %d\n", w, h);
261 fprintf(file, "%d\n", 255);
263 delete [] buffer;
264 buffer=new unsigned char[3*w];
266 delete [] color_buffer;
267 color_buffer=new Color[w];
269 return true;
272 Color *
273 dv_trgt::start_scanline(int /*scanline*/)
275 return color_buffer;
278 bool
279 dv_trgt::end_scanline()
281 if(!file)
282 return false;
284 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
286 if(!fwrite(buffer,1,desc.get_w()*3,file))
287 return false;
289 return true;