Enable $Id$ expansion.
[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 **      \legal
6 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
7 **
8 **      This package is free software; you can redistribute it and/or
9 **      modify it under the terms of the GNU General Public License as
10 **      published by the Free Software Foundation; either version 2 of
11 **      the License, or (at your option) any later version.
12 **
13 **      This package is distributed in the hope that it will be useful,
14 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **      General Public License for more details.
17 **      \endlegal
18 **
19 ** === N O T E S ===========================================================
20 **
21 ** ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #define SYNFIG_TARGET
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include <ETL/stringf>
35 #include "trgt_dv.h"
36 #include <stdio.h>
37 #include <algorithm>
38 #include <functional>
39 #include <ETL/clock>
40
41 #endif
42
43 /* === M A C R O S ========================================================= */
44
45 using namespace synfig;
46 using namespace std;
47 using namespace etl;
48
49 /* === G L O B A L S ======================================================= */
50
51 SYNFIG_TARGET_INIT(dv_trgt);
52 SYNFIG_TARGET_SET_NAME(dv_trgt,"dv");
53 SYNFIG_TARGET_SET_EXT(dv_trgt,"dv");
54 SYNFIG_TARGET_SET_VERSION(dv_trgt,"0.1");
55 SYNFIG_TARGET_SET_CVS_ID(dv_trgt,"$Id$");
56
57 /* === M E T H O D S ======================================================= */
58
59
60 dv_trgt::dv_trgt(const char *Filename)
61 {
62         file=NULL;
63         filename=Filename;
64         buffer=NULL;
65         wide_aspect=false;
66         color_buffer=0;
67                 set_remove_alpha();
68
69 }
70
71 dv_trgt::~dv_trgt()
72 {
73         if(file)
74                 pclose(file);
75         file=NULL;
76         delete [] buffer;
77         delete [] color_buffer;
78 }
79
80 bool
81 dv_trgt::set_rend_desc(RendDesc *given_desc)
82 {
83         // Set the aspect ratio
84         if(wide_aspect)
85         {
86                 // 16:9 Aspect
87                 given_desc->set_wh(160,90);
88
89                 // Widescreen should be progressive scan
90                 given_desc->set_interlaced(false);
91         }
92         else
93         {
94                 // 4:3 Aspect
95                 given_desc->set_wh(400,300);
96
97                 // We should be interlaced
98                 given_desc->set_interlaced(true);
99         }
100
101         // but the pixel res should be 720x480
102         given_desc->clear_flags(),given_desc->set_wh(720,480);
103
104         // NTSC Frame rate is 29.97
105         given_desc->set_frame_rate(29.97);
106
107         // The pipe to encodedv is PPM, which needs RGB data
108         //given_desc->set_pixel_format(PF_RGB);
109
110         // Set the description
111         desc=*given_desc;
112
113         return true;
114 }
115
116 bool
117 dv_trgt::init()
118 {
119         imagecount=desc.get_frame_start();
120
121         string command;
122
123         if(wide_aspect)
124                 command=strprintf("encodedv -w 1 - > \"%s\"\n",filename.c_str());
125         else
126                 command=strprintf("encodedv - > \"%s\"\n",filename.c_str());
127
128         // Open the pipe to encodedv
129         file=popen(command.c_str(),"w");
130
131         if(!file)
132         {
133                 synfig::error(_("Unable to open pipe to encodedv"));
134                 return false;
135         }
136
137         // Sleep for a moment to let the pipe catch up
138         etl::clock().sleep(0.25f);
139
140         return true;
141 }
142
143 void
144 dv_trgt::end_frame()
145 {
146         fprintf(file, " ");
147         fflush(file);
148         imagecount++;
149 }
150
151 bool
152 dv_trgt::start_frame(synfig::ProgressCallback *callback)
153 {
154         int w=desc.get_w(),h=desc.get_h();
155
156         if(!file)
157                 return false;
158
159         fprintf(file, "P6\n");
160         fprintf(file, "%d %d\n", w, h);
161         fprintf(file, "%d\n", 255);
162
163         delete [] buffer;
164         buffer=new unsigned char[3*w];
165
166         delete [] color_buffer;
167         color_buffer=new Color[w];
168
169         return true;
170 }
171
172 Color *
173 dv_trgt::start_scanline(int scanline)
174 {
175         return color_buffer;
176 }
177
178 bool
179 dv_trgt::end_scanline()
180 {
181         if(!file)
182                 return false;
183
184         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
185
186         if(!fwrite(buffer,1,desc.get_w()*3,file))
187                 return false;
188
189         return true;
190 }