1 /* === S Y N F I G ========================================================= */
3 ** \brief ppm Target Module
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === H E A D E R S ======================================================= */
36 #include <ETL/stringf>
39 #include <sys/types.h>
59 /* === M A C R O S ========================================================= */
61 using namespace synfig;
65 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
66 #define UNIX_PIPE_TO_PROCESSES
67 #elif defined(HAVE__SPAWNLP) && defined(HAVE__PIPE) && defined(HAVE_CWAIT)
68 #define WIN32_PIPE_TO_PROCESSES
71 /* === G L O B A L S ======================================================= */
73 SYNFIG_TARGET_INIT(dv_trgt);
74 SYNFIG_TARGET_SET_NAME(dv_trgt,"dv");
75 SYNFIG_TARGET_SET_EXT(dv_trgt,"dv");
76 SYNFIG_TARGET_SET_VERSION(dv_trgt,"0.1");
77 SYNFIG_TARGET_SET_CVS_ID(dv_trgt,"$Id$");
79 /* === M E T H O D S ======================================================= */
82 dv_trgt::dv_trgt(const char *Filename)
99 #if defined(WIN32_PIPE_TO_PROCESSES)
100 cwait(&status,pid,0);
101 #elif defined(UNIX_PIPE_TO_PROCESSES)
102 waitpid(pid,&status,0);
107 delete [] color_buffer;
111 dv_trgt::set_rend_desc(RendDesc *given_desc)
113 // Set the aspect ratio
117 given_desc->set_wh(160,90);
119 // Widescreen should be progressive scan
120 given_desc->set_interlaced(false);
125 given_desc->set_wh(400,300);
127 // We should be interlaced
128 given_desc->set_interlaced(true);
131 // but the pixel res should be 720x480
132 given_desc->clear_flags(),given_desc->set_wh(720,480);
134 // NTSC Frame rate is 29.97
135 given_desc->set_frame_rate(29.97);
137 // The pipe to encodedv is PPM, which needs RGB data
138 //given_desc->set_pixel_format(PF_RGB);
140 // Set the description
149 imagecount=desc.get_frame_start();
151 #if defined(WIN32_PIPE_TO_PROCESSES)
154 int stdin_fileno, stdout_fileno;
156 if(_pipe(p, 512, O_BINARY | O_NOINHERIT) < 0) {
157 synfig::error(_("Unable to open pipe to encodedv"));
161 // Save stdin/stdout so we can restore them later
162 stdin_fileno = _dup(_fileno(stdin));
163 stdout_fileno = _dup(_fileno(stdout));
165 // encodedv should read from the pipe
166 if(_dup2(p[0], _fileno(stdin)) != 0) {
167 synfig::error(_("Unable to open pipe to encodedv"));
171 FILE* outfile = fopen(filename.c_str(),"wb");
172 if( outfile == NULL ){
173 synfig::error(_("Unable to open pipe to encodedv"));
176 if(_dup2(_fileno(outfile), _fileno(stdout)) != 0) {
177 synfig::error(_("Unable to open pipe to encodedv"));
182 pid = _spawnlp(_P_NOWAIT, "encodedv", "encodedv", "-w", "1", "-", (const char *)NULL);
184 pid = _spawnlp(_P_NOWAIT, "encodedv", "encodedv", "-", (const char *)NULL);
187 synfig::error(_("Unable to open pipe to encodedv"));
191 // Restore stdin/stdout
192 if(_dup2(stdin_fileno, _fileno(stdin)) != 0) {
193 synfig::error(_("Unable to open pipe to encodedv"));
196 if(_dup2(stdout_fileno, _fileno(stdout)) != 0) {
197 synfig::error(_("Unable to open pipe to encodedv"));
201 close(stdout_fileno);
203 // Close the pipe read end - encodedv uses it
206 // We write data to the write end of the pipe
207 file = fdopen(p[1], "wb");
209 #elif defined(UNIX_PIPE_TO_PROCESSES)
214 synfig::error(_("Unable to open pipe to encodedv"));
221 synfig::error(_("Unable to open pipe to encodedv"));
227 // Close pipeout, not needed
229 // Dup pipeout to stdin
230 if( dup2( p[0], STDIN_FILENO ) == -1 ){
231 synfig::error(_("Unable to open pipe to encodedv"));
234 // Close the unneeded pipeout
236 // Open filename to stdout
237 FILE* outfile = fopen(filename.c_str(),"wb");
238 if( outfile == NULL ){
239 synfig::error(_("Unable to open pipe to encodedv"));
242 int outfilefd = fileno(outfile);
243 if( outfilefd == -1 ){
244 synfig::error(_("Unable to open pipe to encodedv"));
247 if( dup2( outfilefd, STDOUT_FILENO ) == -1 ){
248 synfig::error(_("Unable to open pipe to encodedv"));
253 execlp("encodedv", "encodedv", "-w", "1", "-", (const char *)NULL);
255 execlp("encodedv", "encodedv", "-", (const char *)NULL);
256 // We should never reach here unless the exec failed
257 synfig::error(_("Unable to open pipe to encodedv"));
261 // Close pipein, not needed
263 // Save pipeout to file handle, will write to it later
264 file = fdopen(p[1], "wb");
266 synfig::error(_("Unable to open pipe to encodedv"));
272 #error There are no known APIs for creating child processes
276 // Sleep for a moment to let the pipe catch up
277 etl::clock().sleep(0.25f);
291 dv_trgt::start_frame(synfig::ProgressCallback */*callback*/)
293 int w=desc.get_w(),h=desc.get_h();
298 fprintf(file, "P6\n");
299 fprintf(file, "%d %d\n", w, h);
300 fprintf(file, "%d\n", 255);
303 buffer=new unsigned char[3*w];
305 delete [] color_buffer;
306 color_buffer=new Color[w];
312 dv_trgt::start_scanline(int /*scanline*/)
318 dv_trgt::end_scanline()
323 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
325 if(!fwrite(buffer,1,desc.get_w()*3,file))