1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
35 #include <ETL/stringf>
41 using namespace synfig;
45 /* === M A C R O S ========================================================= */
49 #define Y_RANGE (Y_CEIL-Y_FLOOR)
53 #define UV_RANGE (UV_CEIL-UV_FLOOR)
55 /* === G L O B A L S ======================================================= */
57 SYNFIG_TARGET_INIT(yuv);
58 SYNFIG_TARGET_SET_NAME(yuv,"yuv420p");
59 SYNFIG_TARGET_SET_EXT(yuv,"yuv");
60 SYNFIG_TARGET_SET_VERSION(yuv,"0.1");
61 SYNFIG_TARGET_SET_CVS_ID(yuv,"$Id$");
63 /* === M E T H O D S ======================================================= */
65 yuv::yuv(const char *FILENAME):
67 file( (filename=="-")?stdout:fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE) ),
70 // YUV420P doesn't have an alpha channel
84 fprintf(file.get(), "YUV4MPEG2 W%d H%d F%d:1 Ip\n",
85 desc.get_w(), desc.get_h(),
86 round_to_int(desc.get_frame_rate()));
91 yuv::set_rend_desc(RendDesc *given_desc)
93 given_desc->clear_flags();
95 // Make sure our width is divisible by two
96 given_desc->set_w(given_desc->get_w()*2/2);
97 given_desc->set_h(given_desc->get_h()*2/2);
101 // Set up our surface
102 surface.set_wh(desc.get_w(),desc.get_h());
108 yuv::start_frame(synfig::ProgressCallback */*callback*/)
110 fprintf(file.get(), "FRAME\n");
111 return static_cast<bool>(file);
115 yuv::start_scanline(int x)
123 return static_cast<bool>(file);
129 const int w=desc.get_w(),h=desc.get_h();
134 // Output Y' channel, adjusting
135 // the gamma as we go
139 Color& c(surface[y][x]);
141 c.set_r(gamma().r_F32_to_F32(c.get_r()));
142 c.set_g(gamma().g_F32_to_F32(c.get_g()));
143 c.set_b(gamma().b_F32_to_F32(c.get_b()));
145 int i(max(min(round_to_int(c.get_y()*Y_RANGE),Y_RANGE),0)+Y_FLOOR);
149 const float er(f-((float)i-Y_FLOOR)/Y_RANGE);
150 const Color error(er,er,er);
152 if(surface.get_h()>y+1)
154 surface[y+1][x-1]+=error * ((float)3/(float)16);
155 surface[y+1][x]+=error * ((float)5/(float)16);
156 if(surface.get_w()>x+1)
157 surface[y+1][x+1]+=error * ((float)1/(float)16);
159 if(surface.get_w()>x+1)
160 surface[y][x+1]+=error * ((float)7/(float)16);
167 // Create new super-sampled surface
168 Surface sm_surface(w/2,h/2);
172 Color c(Color::alpha());
176 c+=surface[y+1][x+1];
178 sm_surface[y/2][x/2]=c;
182 for(y=0;y<sm_surface.get_h();y++)
183 for(x=0;x<sm_surface.get_w();x++)
185 const Color& c(sm_surface[y][x]);
186 const float f(c.get_u());
187 const int i(max(min(round_to_int((f+0.5f)*UV_RANGE),UV_RANGE),0)+UV_FLOOR);
191 const float er(f-((((float)i-UV_FLOOR)/UV_RANGE)-0.5f));
192 const Color error(Color::YUV(0,er,0));
194 if(sm_surface.get_h()>y+1)
196 sm_surface[y+1][x-1]+=error * ((float)3/(float)16);
197 sm_surface[y+1][x]+=error * ((float)5/(float)16);
198 if(sm_surface.get_w()>x+1)
199 sm_surface[y+1][x+1]+=error * ((float)1/(float)16);
201 if(sm_surface.get_w()>x+1)
202 sm_surface[y][x+1]+=error * ((float)7/(float)16);
208 for(y=0;y<sm_surface.get_h();y++)
209 for(x=0;x<sm_surface.get_w();x++)
211 const Color& c(sm_surface[y][x]);
212 const float f(c.get_v());
213 const int i(max(min(round_to_int((f+0.5f)*UV_RANGE),UV_RANGE),0)+UV_FLOOR);
217 const float er(f-((((float)i-UV_FLOOR)/UV_RANGE)-0.5f));
218 const Color error(Color::YUV(0,0,er));
220 if(sm_surface.get_h()>y+1)
222 sm_surface[y+1][x-1]+=error * ((float)3/(float)16);
223 sm_surface[y+1][x]+=error * ((float)5/(float)16);
224 if(sm_surface.get_w()>x+1)
225 sm_surface[y+1][x+1]+=error * ((float)1/(float)16);
227 if(sm_surface.get_w()>x+1)
228 sm_surface[y][x+1]+=error * ((float)7/(float)16);
233 // Flush out the frame