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