Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc1 / 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 <algorithm>
40 #include <functional>
41 #include <ETL/clock>
42
43 #endif
44
45 /* === M A C R O S ========================================================= */
46
47 using namespace synfig;
48 using namespace std;
49 using namespace etl;
50
51 /* === G L O B A L S ======================================================= */
52
53 SYNFIG_TARGET_INIT(dv_trgt);
54 SYNFIG_TARGET_SET_NAME(dv_trgt,"dv");
55 SYNFIG_TARGET_SET_EXT(dv_trgt,"dv");
56 SYNFIG_TARGET_SET_VERSION(dv_trgt,"0.1");
57 SYNFIG_TARGET_SET_CVS_ID(dv_trgt,"$Id$");
58
59 /* === M E T H O D S ======================================================= */
60
61
62 dv_trgt::dv_trgt(const char *Filename)
63 {
64         file=NULL;
65         filename=Filename;
66         buffer=NULL;
67         wide_aspect=false;
68         color_buffer=0;
69                 set_remove_alpha();
70
71 }
72
73 dv_trgt::~dv_trgt()
74 {
75         if(file)
76                 pclose(file);
77         file=NULL;
78         delete [] buffer;
79         delete [] color_buffer;
80 }
81
82 bool
83 dv_trgt::set_rend_desc(RendDesc *given_desc)
84 {
85         // Set the aspect ratio
86         if(wide_aspect)
87         {
88                 // 16:9 Aspect
89                 given_desc->set_wh(160,90);
90
91                 // Widescreen should be progressive scan
92                 given_desc->set_interlaced(false);
93         }
94         else
95         {
96                 // 4:3 Aspect
97                 given_desc->set_wh(400,300);
98
99                 // We should be interlaced
100                 given_desc->set_interlaced(true);
101         }
102
103         // but the pixel res should be 720x480
104         given_desc->clear_flags(),given_desc->set_wh(720,480);
105
106         // NTSC Frame rate is 29.97
107         given_desc->set_frame_rate(29.97);
108
109         // The pipe to encodedv is PPM, which needs RGB data
110         //given_desc->set_pixel_format(PF_RGB);
111
112         // Set the description
113         desc=*given_desc;
114
115         return true;
116 }
117
118 bool
119 dv_trgt::init()
120 {
121         imagecount=desc.get_frame_start();
122
123         string command;
124
125         if(wide_aspect)
126                 command=strprintf("encodedv -w 1 - > \"%s\"\n",filename.c_str());
127         else
128                 command=strprintf("encodedv - > \"%s\"\n",filename.c_str());
129
130         // Open the pipe to encodedv
131         file=popen(command.c_str(),"w");
132
133         if(!file)
134         {
135                 synfig::error(_("Unable to open pipe to encodedv"));
136                 return false;
137         }
138
139         // Sleep for a moment to let the pipe catch up
140         etl::clock().sleep(0.25f);
141
142         return true;
143 }
144
145 void
146 dv_trgt::end_frame()
147 {
148         fprintf(file, " ");
149         fflush(file);
150         imagecount++;
151 }
152
153 bool
154 dv_trgt::start_frame(synfig::ProgressCallback */*callback*/)
155 {
156         int w=desc.get_w(),h=desc.get_h();
157
158         if(!file)
159                 return false;
160
161         fprintf(file, "P6\n");
162         fprintf(file, "%d %d\n", w, h);
163         fprintf(file, "%d\n", 255);
164
165         delete [] buffer;
166         buffer=new unsigned char[3*w];
167
168         delete [] color_buffer;
169         color_buffer=new Color[w];
170
171         return true;
172 }
173
174 Color *
175 dv_trgt::start_scanline(int /*scanline*/)
176 {
177         return color_buffer;
178 }
179
180 bool
181 dv_trgt::end_scanline()
182 {
183         if(!file)
184                 return false;
185
186         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
187
188         if(!fwrite(buffer,1,desc.get_w()*3,file))
189                 return false;
190
191         return true;
192 }