1 /* === S Y N F I G ========================================================= */
2 /*! \file halftone3.cpp
3 ** \brief Implementation of the "Halftone 3" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007-2008 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 ======================================================= */
33 #include "halftone3.h"
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
47 /* === M A C R O S ========================================================= */
49 using namespace synfig;
53 /* === G L O B A L S ======================================================= */
55 SYNFIG_LAYER_INIT(Halftone3);
56 SYNFIG_LAYER_SET_NAME(Halftone3,"halftone3");
57 SYNFIG_LAYER_SET_LOCAL_NAME(Halftone3,N_("Halftone 3"));
58 SYNFIG_LAYER_SET_CATEGORY(Halftone3,N_("Filters"));
59 SYNFIG_LAYER_SET_VERSION(Halftone3,"0.0");
60 SYNFIG_LAYER_SET_CVS_ID(Halftone3,"$Id$");
62 /* === P R O C E D U R E S ================================================= */
64 #define HALFSQRT2 (0.7)
65 #define SQRT2 (1.414213562f)
67 /* === M E T H O D S ======================================================= */
69 Halftone3::Halftone3()
71 size=(synfig::Vector(0.25,0.25));
78 tone[i].origin=(synfig::Point(0,0));
79 tone[i].angle=Angle::deg(30.0)*(float)i;
86 color[0]=Color::cyan();
87 color[1]=Color::magenta();
88 color[2]=Color::yellow();
92 color[0]=Color::red();
93 color[1]=Color::green();
94 color[2]=Color::blue();
97 set_blend_method(Color::BLEND_STRAIGHT);
101 inverse_matrix[i][j]=(j==i)?1.0f:0.0f;
105 Layer::Vocab voc(get_param_vocab());
106 Layer::fill_static(voc);
118 #define matrix inverse_matrix
119 //float matrix[3][3];
125 matrix[i][0]=1.0f-(color[i].get_r());
126 matrix[i][1]=1.0f-(color[i].get_g());
127 matrix[i][2]=1.0f-(color[i].get_b());
128 float mult=sqrt(matrix[i][0]*matrix[i][0]+matrix[i][1]*matrix[i][1]+matrix[i][2]*matrix[i][2]);
144 matrix[i][0]=color[i].get_r();
145 matrix[i][1]=color[i].get_g();
146 matrix[i][2]=color[i].get_b();
147 float mult=sqrt(matrix[i][0]*matrix[i][0]+matrix[i][1]*matrix[i][1]+matrix[i][2]*matrix[i][2]);
164 // Insert guass-jordan elimination code here
165 int k=0,i=0,j=0,z_size=3;
166 #define A inverse_matrix
168 for (k=0;k<z_size;k++)
170 { A[k][k]= -1/A[k][k];
173 for (i=0;i<z_size;i++)
174 if (i!=k) A[i][k]*=A[k][k];
176 //elements not in a pivot row or column
177 for (i=0;i<z_size;i++)
179 for (j=0;j<z_size;j++)
181 A[i][j]+=A[i][k]*A[k][j];
183 //elements in a pivot row
184 for (i=0;i<z_size;i++)
190 for (i=0;i<z_size;i++) /*reverse sign*/
191 for (j=0;j<z_size;j++)
198 Halftone3::color_func(const Point &point, float supersample,const Color& in_color)const
207 chan[0]=inverse_matrix[0][0]*(1.0f-in_color.get_r())+inverse_matrix[0][1]*(1.0f-in_color.get_g())+inverse_matrix[0][2]*(1.0f-in_color.get_b());
208 chan[1]=inverse_matrix[1][0]*(1.0f-in_color.get_r())+inverse_matrix[1][1]*(1.0f-in_color.get_g())+inverse_matrix[1][2]*(1.0f-in_color.get_b());
209 chan[2]=inverse_matrix[2][0]*(1.0f-in_color.get_r())+inverse_matrix[2][1]*(1.0f-in_color.get_g())+inverse_matrix[2][2]*(1.0f-in_color.get_b());
211 halfcolor=Color::white();
212 halfcolor-=(~color[0])*tone[0](point,chan[0],supersample);
213 halfcolor-=(~color[1])*tone[1](point,chan[1],supersample);
214 halfcolor-=(~color[2])*tone[2](point,chan[2],supersample);
216 halfcolor.set_a(in_color.get_a());
220 chan[0]=inverse_matrix[0][0]*in_color.get_r()+inverse_matrix[0][1]*in_color.get_g()+inverse_matrix[0][2]*in_color.get_b();
221 chan[1]=inverse_matrix[1][0]*in_color.get_r()+inverse_matrix[1][1]*in_color.get_g()+inverse_matrix[1][2]*in_color.get_b();
222 chan[2]=inverse_matrix[2][0]*in_color.get_r()+inverse_matrix[2][1]*in_color.get_g()+inverse_matrix[2][2]*in_color.get_b();
224 halfcolor=Color::black();
225 halfcolor+=color[0]*tone[0](point,chan[0],supersample);
226 halfcolor+=color[1]*tone[1](point,chan[1],supersample);
227 halfcolor+=color[2]*tone[2](point,chan[2],supersample);
229 halfcolor.set_a(in_color.get_a());
236 Halftone3::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
238 return abs(pw/(tone[0].size).mag());
241 synfig::Layer::Handle
242 Halftone3::hit_check(synfig::Context /*context*/, const synfig::Point &/*point*/)const
244 return const_cast<Halftone3*>(this);
248 Halftone3::set_param(const String & param, const ValueBase &value)
250 IMPORT_PLUS(size, {tone[0].size=size; tone[1].size=size; tone[2].size=size;});
251 IMPORT_PLUS(type, {tone[0].type=type; tone[1].type=type; tone[2].type=type;});
253 IMPORT_PLUS(color[0],sync());
254 IMPORT_PLUS(color[1],sync());
255 IMPORT_PLUS(color[2],sync());
257 IMPORT_PLUS(subtractive,sync());
259 IMPORT(tone[0].angle);
260 IMPORT(tone[0].origin);
262 IMPORT(tone[1].angle);
263 IMPORT(tone[1].origin);
265 IMPORT(tone[2].angle);
266 IMPORT(tone[2].origin);
268 IMPORT_AS(tone[0].origin,"tone[0].offset");
269 IMPORT_AS(tone[1].origin,"tone[1].offset");
270 IMPORT_AS(tone[2].origin,"tone[2].offset");
272 return Layer_Composite::set_param(param,value);
276 Halftone3::get_param(const String & param)const
287 EXPORT(tone[0].angle);
288 EXPORT(tone[0].origin);
290 EXPORT(tone[1].angle);
291 EXPORT(tone[1].origin);
293 EXPORT(tone[2].angle);
294 EXPORT(tone[2].origin);
299 return Layer_Composite::get_param(param);
303 Halftone3::get_param_vocab()const
305 Layer::Vocab ret(Layer_Composite::get_param_vocab());
307 ret.push_back(ParamDesc("size")
308 .set_local_name(_("Mask Size"))
310 ret.push_back(ParamDesc("type")
311 .set_local_name(_(" Type"))
313 .add_enum_value(TYPE_SYMMETRIC,"symmetric",_("Symmetric"))
314 .add_enum_value(TYPE_LIGHTONDARK,"lightondark",_("Light On Dark"))
315 //.add_enum_value(TYPE_DARKONLIGHT,"darkonlight",_("Dark on Light"))
316 .add_enum_value(TYPE_DIAMOND,"diamond",_("Diamond"))
317 .add_enum_value(TYPE_STRIPE,"stripe",_("Stripe"))
319 ret.push_back(ParamDesc("subtractive")
320 .set_local_name(_("Subtractive Flag"))
325 String chan_name(strprintf("Chan%d",i));
327 ret.push_back(ParamDesc(strprintf("color[%d]",i))
328 .set_local_name(chan_name+_(" Color"))
331 ret.push_back(ParamDesc(strprintf("tone[%d].origin",i))
332 .set_local_name(chan_name+_(" Mask Origin"))
335 ret.push_back(ParamDesc(strprintf("tone[%d].angle",i))
336 .set_local_name(chan_name+_(" Mask Angle"))
337 .set_origin(strprintf("tone[%d].origin",i))
345 Halftone3::get_color(Context context, const Point &point)const
347 const Color undercolor(context.get_color(point));
348 const Color color(color_func(point,0,undercolor));
350 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
353 return Color::blend(color,undercolor,get_amount(),get_blend_method());
357 Halftone3::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
359 SuperCallback supercb(cb,0,9500,10000);
361 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
366 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
367 const Point tl(renddesc.get_tl());
368 const int w(surface->get_w());
369 const int h(surface->get_h());
370 const float supersample_size(abs(pw/(tone[0].size).mag()));
372 Surface::pen pen(surface->begin());
378 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
379 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
390 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
391 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
406 // Mark our progress as finished
407 if(cb && !cb->amount_complete(10000,10000))