1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
34 #include <ETL/stringf>
40 using namespace synfig;
44 /* === M A C R O S ========================================================= */
48 #define Y_RANGE (Y_CEIL-Y_FLOOR)
52 #define UV_RANGE (UV_CEIL-UV_FLOOR)
54 /* === G L O B A L S ======================================================= */
56 SYNFIG_TARGET_INIT(yuv);
57 SYNFIG_TARGET_SET_NAME(yuv,"yuv420p");
58 SYNFIG_TARGET_SET_EXT(yuv,"yuv");
59 SYNFIG_TARGET_SET_VERSION(yuv,"0.1");
60 SYNFIG_TARGET_SET_CVS_ID(yuv,"$Id$");
62 /* === M E T H O D S ======================================================= */
64 yuv::yuv(const char *FILENAME, const synfig::TargetParam& /* params */):
66 file( (filename=="-")?stdout:fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE) ),
69 // YUV420P doesn't have an alpha channel
83 fprintf(file.get(), "YUV4MPEG2 W%d H%d F%d:1 Ip\n",
84 desc.get_w(), desc.get_h(),
85 round_to_int(desc.get_frame_rate()));
90 yuv::set_rend_desc(RendDesc *given_desc)
92 given_desc->clear_flags();
94 // Make sure our width is divisible by two
95 given_desc->set_w(given_desc->get_w()*2/2);
96 given_desc->set_h(given_desc->get_h()*2/2);
100 // Set up our surface
101 surface.set_wh(desc.get_w(),desc.get_h());
107 yuv::start_frame(synfig::ProgressCallback */*callback*/)
109 fprintf(file.get(), "FRAME\n");
110 return static_cast<bool>(file);
114 yuv::start_scanline(int x)
122 return static_cast<bool>(file);
128 const int w=desc.get_w(),h=desc.get_h();
133 // Output Y' channel, adjusting
134 // the gamma as we go
138 Color& c(surface[y][x]);
140 c.set_r(gamma().r_F32_to_F32(c.get_r()));
141 c.set_g(gamma().g_F32_to_F32(c.get_g()));
142 c.set_b(gamma().b_F32_to_F32(c.get_b()));
144 int i(max(min(round_to_int(c.get_y()*Y_RANGE),Y_RANGE),0)+Y_FLOOR);
148 const float er(f-((float)i-Y_FLOOR)/Y_RANGE);
149 const Color error(er,er,er);
151 if(surface.get_h()>y+1)
153 surface[y+1][x-1]+=error * ((float)3/(float)16);
154 surface[y+1][x]+=error * ((float)5/(float)16);
155 if(surface.get_w()>x+1)
156 surface[y+1][x+1]+=error * ((float)1/(float)16);
158 if(surface.get_w()>x+1)
159 surface[y][x+1]+=error * ((float)7/(float)16);
166 // Create new super-sampled surface
167 Surface sm_surface(w/2,h/2);
171 Color c(Color::alpha());
175 c+=surface[y+1][x+1];
177 sm_surface[y/2][x/2]=c;
181 for(y=0;y<sm_surface.get_h();y++)
182 for(x=0;x<sm_surface.get_w();x++)
184 const Color& c(sm_surface[y][x]);
185 const float f(c.get_u());
186 const int i(max(min(round_to_int((f+0.5f)*UV_RANGE),UV_RANGE),0)+UV_FLOOR);
190 const float er(f-((((float)i-UV_FLOOR)/UV_RANGE)-0.5f));
191 const Color error(Color::YUV(0,er,0));
193 if(sm_surface.get_h()>y+1)
195 sm_surface[y+1][x-1]+=error * ((float)3/(float)16);
196 sm_surface[y+1][x]+=error * ((float)5/(float)16);
197 if(sm_surface.get_w()>x+1)
198 sm_surface[y+1][x+1]+=error * ((float)1/(float)16);
200 if(sm_surface.get_w()>x+1)
201 sm_surface[y][x+1]+=error * ((float)7/(float)16);
207 for(y=0;y<sm_surface.get_h();y++)
208 for(x=0;x<sm_surface.get_w();x++)
210 const Color& c(sm_surface[y][x]);
211 const float f(c.get_v());
212 const int i(max(min(round_to_int((f+0.5f)*UV_RANGE),UV_RANGE),0)+UV_FLOOR);
216 const float er(f-((((float)i-UV_FLOOR)/UV_RANGE)-0.5f));
217 const Color error(Color::YUV(0,0,er));
219 if(sm_surface.get_h()>y+1)
221 sm_surface[y+1][x-1]+=error * ((float)3/(float)16);
222 sm_surface[y+1][x]+=error * ((float)5/(float)16);
223 if(sm_surface.get_w()>x+1)
224 sm_surface[y+1][x+1]+=error * ((float)1/(float)16);
226 if(sm_surface.get_w()>x+1)
227 sm_surface[y][x+1]+=error * ((float)7/(float)16);
232 // Flush out the frame