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