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(),"wb") ),
70 // YUV420P doesn't have an alpha channel
79 yuv::set_rend_desc(RendDesc *given_desc)
81 given_desc->clear_flags();
83 // Make sure our width is divisible by two
84 given_desc->set_w(given_desc->get_w()*2/2);
85 given_desc->set_h(given_desc->get_h()*2/2);
90 surface.set_wh(desc.get_w(),desc.get_h());
96 yuv::start_frame(synfig::ProgressCallback *callback)
98 return static_cast<bool>(file);
102 yuv::start_scanline(int x)
110 return static_cast<bool>(file);
116 const int w=desc.get_w(),h=desc.get_h();
121 // Output Y' channel, adjusting
122 // the gamma as we go
126 Color& c(surface[y][x]);
128 c.set_r(gamma().r_F32_to_F32(c.get_r()));
129 c.set_g(gamma().g_F32_to_F32(c.get_g()));
130 c.set_b(gamma().b_F32_to_F32(c.get_b()));
132 int i(max(min(round_to_int(c.get_y()*Y_RANGE),Y_RANGE),0)+Y_FLOOR);
136 const float er(f-((float)i-Y_FLOOR)/Y_RANGE);
137 const Color error(er,er,er);
139 if(surface.get_h()>y+1)
141 surface[y+1][x-1]+=error * ((float)3/(float)16);
142 surface[y+1][x]+=error * ((float)5/(float)16);
143 if(surface.get_w()>x+1)
144 surface[y+1][x+1]+=error * ((float)1/(float)16);
146 if(surface.get_w()>x+1)
147 surface[y][x+1]+=error * ((float)7/(float)16);
154 // Create new super-sampled surface
155 Surface sm_surface(w/2,h/2);
159 Color c(Color::alpha());
163 c+=surface[y+1][x+1];
165 sm_surface[y/2][x/2]=c;
169 for(y=0;y<sm_surface.get_h();y++)
170 for(x=0;x<sm_surface.get_w();x++)
172 const Color& c(sm_surface[y][x]);
173 const float f(c.get_u());
174 const int i(max(min(round_to_int((f+0.5f)*UV_RANGE),UV_RANGE),0)+UV_FLOOR);
178 const float er(f-((((float)i-UV_FLOOR)/UV_RANGE)-0.5f));
179 const Color error(Color::YUV(0,er,0));
181 if(sm_surface.get_h()>y+1)
183 sm_surface[y+1][x-1]+=error * ((float)3/(float)16);
184 sm_surface[y+1][x]+=error * ((float)5/(float)16);
185 if(sm_surface.get_w()>x+1)
186 sm_surface[y+1][x+1]+=error * ((float)1/(float)16);
188 if(sm_surface.get_w()>x+1)
189 sm_surface[y][x+1]+=error * ((float)7/(float)16);
195 for(y=0;y<sm_surface.get_h();y++)
196 for(x=0;x<sm_surface.get_w();x++)
198 const Color& c(sm_surface[y][x]);
199 const float f(c.get_v());
200 const int i(max(min(round_to_int((f+0.5f)*UV_RANGE),UV_RANGE),0)+UV_FLOOR);
204 const float er(f-((((float)i-UV_FLOOR)/UV_RANGE)-0.5f));
205 const Color error(Color::YUV(0,0,er));
207 if(sm_surface.get_h()>y+1)
209 sm_surface[y+1][x-1]+=error * ((float)3/(float)16);
210 sm_surface[y+1][x]+=error * ((float)5/(float)16);
211 if(sm_surface.get_w()>x+1)
212 sm_surface[y+1][x+1]+=error * ((float)1/(float)16);
214 if(sm_surface.get_w()>x+1)
215 sm_surface[y][x+1]+=error * ((float)7/(float)16);
220 // Flush out the frame