Forgot to include the right headers in the security fixes
[synfig.git] / synfig-core / trunk / src / modules / mod_dv / trgt_dv.cpp
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 **
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 #define SYNFIG_TARGET
28
29 #ifdef USING_PCH
30 #       include "pch.h"
31 #else
32 #ifdef HAVE_CONFIG_H
33 #       include <config.h>
34 #endif
35
36 #include <ETL/stringf>
37 #include "trgt_dv.h"
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41 #include <algorithm>
42 #include <functional>
43 #include <ETL/clock>
44
45 #endif
46
47 /* === M A C R O S ========================================================= */
48
49 using namespace synfig;
50 using namespace std;
51 using namespace etl;
52
53 /* === G L O B A L S ======================================================= */
54
55 SYNFIG_TARGET_INIT(dv_trgt);
56 SYNFIG_TARGET_SET_NAME(dv_trgt,"dv");
57 SYNFIG_TARGET_SET_EXT(dv_trgt,"dv");
58 SYNFIG_TARGET_SET_VERSION(dv_trgt,"0.1");
59 SYNFIG_TARGET_SET_CVS_ID(dv_trgt,"$Id$");
60
61 /* === M E T H O D S ======================================================= */
62
63
64 dv_trgt::dv_trgt(const char *Filename)
65 {
66         file=NULL;
67         filename=Filename;
68         buffer=NULL;
69         wide_aspect=false;
70         color_buffer=0;
71                 set_remove_alpha();
72
73 }
74
75 dv_trgt::~dv_trgt()
76 {
77         if(file)
78                 pclose(file);
79         file=NULL;
80         delete [] buffer;
81         delete [] color_buffer;
82 }
83
84 bool
85 dv_trgt::set_rend_desc(RendDesc *given_desc)
86 {
87         // Set the aspect ratio
88         if(wide_aspect)
89         {
90                 // 16:9 Aspect
91                 given_desc->set_wh(160,90);
92
93                 // Widescreen should be progressive scan
94                 given_desc->set_interlaced(false);
95         }
96         else
97         {
98                 // 4:3 Aspect
99                 given_desc->set_wh(400,300);
100
101                 // We should be interlaced
102                 given_desc->set_interlaced(true);
103         }
104
105         // but the pixel res should be 720x480
106         given_desc->clear_flags(),given_desc->set_wh(720,480);
107
108         // NTSC Frame rate is 29.97
109         given_desc->set_frame_rate(29.97);
110
111         // The pipe to encodedv is PPM, which needs RGB data
112         //given_desc->set_pixel_format(PF_RGB);
113
114         // Set the description
115         desc=*given_desc;
116
117         return true;
118 }
119
120 bool
121 dv_trgt::init()
122 {
123         imagecount=desc.get_frame_start();
124
125         string command;
126
127         int p[2];
128   
129         if (pipe(p)) {
130                 synfig::error(_("Unable to open pipe to encodedv"));
131                 return false;
132         };
133   
134         pid_t pid = fork();
135   
136         if (pid == -1) {
137                 synfig::error(_("Unable to open pipe to encodedv"));
138                 return false;
139         }
140   
141         if (pid == 0){
142                 // Child process
143                 // Dup pipeout to stdin
144                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
145                         synfig::error(_("Unable to open pipe to encodedv"));
146                         return false;
147                 }
148                 
149                 // Open filename to stdout
150                 FILE* outfile = fopen(filename.c_str(),"wb");
151                 if( outfile == NULL ){
152                         synfig::error(_("Unable to open pipe to encodedv"));
153                         return false;
154                 }
155                 int outfilefd = fileno(outfile);
156                 if( outfilefd == -1 ){
157                         synfig::error(_("Unable to open pipe to encodedv"));
158                         return false;
159                 }
160                 if( dup2( outfilefd, STDOUT_FILENO ) == -1 ){
161                         synfig::error(_("Unable to open pipe to encodedv"));
162                         return false;
163                 }
164                 
165                 if(wide_aspect)
166                         execlp("encodedv", "encodedv", "-w", "1", "-", (const char *)NULL);
167                 else
168                         execlp("encodedv", "encodedv", "-", (const char *)NULL);
169                 // We should never reach here unless the exec failed
170                 synfig::error(_("Unable to open pipe to encodedv"));
171                 return false;
172         } else {
173                 // Parent process
174                 // Close pipein, not needed
175                 close(p[0]);
176                 // Save pipeout to file handle, will write to it later
177                 file = fdopen(p[1], "wb");
178                 if (file == NULL) {
179                         synfig::error(_("Unable to open pipe to encodedv"));
180                         return false;
181                 }
182         }
183
184         // Sleep for a moment to let the pipe catch up
185         etl::clock().sleep(0.25f);
186
187         return true;
188 }
189
190 void
191 dv_trgt::end_frame()
192 {
193         fprintf(file, " ");
194         fflush(file);
195         imagecount++;
196 }
197
198 bool
199 dv_trgt::start_frame(synfig::ProgressCallback */*callback*/)
200 {
201         int w=desc.get_w(),h=desc.get_h();
202
203         if(!file)
204                 return false;
205
206         fprintf(file, "P6\n");
207         fprintf(file, "%d %d\n", w, h);
208         fprintf(file, "%d\n", 255);
209
210         delete [] buffer;
211         buffer=new unsigned char[3*w];
212
213         delete [] color_buffer;
214         color_buffer=new Color[w];
215
216         return true;
217 }
218
219 Color *
220 dv_trgt::start_scanline(int /*scanline*/)
221 {
222         return color_buffer;
223 }
224
225 bool
226 dv_trgt::end_scanline()
227 {
228         if(!file)
229                 return false;
230
231         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
232
233         if(!fwrite(buffer,1,desc.get_w()*3,file))
234                 return false;
235
236         return true;
237 }