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;
115 #define matrix inverse_matrix
116 //float matrix[3][3];
122 matrix[i][0]=1.0f-(color[i].get_r());
123 matrix[i][1]=1.0f-(color[i].get_g());
124 matrix[i][2]=1.0f-(color[i].get_b());
125 float mult=sqrt(matrix[i][0]*matrix[i][0]+matrix[i][1]*matrix[i][1]+matrix[i][2]*matrix[i][2]);
141 matrix[i][0]=color[i].get_r();
142 matrix[i][1]=color[i].get_g();
143 matrix[i][2]=color[i].get_b();
144 float mult=sqrt(matrix[i][0]*matrix[i][0]+matrix[i][1]*matrix[i][1]+matrix[i][2]*matrix[i][2]);
161 // Insert guass-jordan elimination code here
162 int k=0,i=0,j=0,z_size=3;
163 #define A inverse_matrix
165 for (k=0;k<z_size;k++)
167 { A[k][k]= -1/A[k][k];
170 for (i=0;i<z_size;i++)
171 if (i!=k) A[i][k]*=A[k][k];
173 //elements not in a pivot row or column
174 for (i=0;i<z_size;i++)
176 for (j=0;j<z_size;j++)
178 A[i][j]+=A[i][k]*A[k][j];
180 //elements in a pivot row
181 for (i=0;i<z_size;i++)
187 for (i=0;i<z_size;i++) /*reverse sign*/
188 for (j=0;j<z_size;j++)
195 Halftone3::color_func(const Point &point, float supersample,const Color& in_color)const
204 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());
205 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());
206 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());
208 halfcolor=Color::white();
209 halfcolor-=(~color[0])*tone[0](point,chan[0],supersample);
210 halfcolor-=(~color[1])*tone[1](point,chan[1],supersample);
211 halfcolor-=(~color[2])*tone[2](point,chan[2],supersample);
213 halfcolor.set_a(in_color.get_a());
217 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();
218 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();
219 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();
221 halfcolor=Color::black();
222 halfcolor+=color[0]*tone[0](point,chan[0],supersample);
223 halfcolor+=color[1]*tone[1](point,chan[1],supersample);
224 halfcolor+=color[2]*tone[2](point,chan[2],supersample);
226 halfcolor.set_a(in_color.get_a());
233 Halftone3::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
235 return abs(pw/(tone[0].size).mag());
238 synfig::Layer::Handle
239 Halftone3::hit_check(synfig::Context /*context*/, const synfig::Point &/*point*/)const
241 return const_cast<Halftone3*>(this);
245 Halftone3::set_param(const String & param, const ValueBase &value)
247 IMPORT_PLUS(size, {tone[0].size=size; tone[1].size=size; tone[2].size=size;});
248 IMPORT_PLUS(type, {tone[0].type=type; tone[1].type=type; tone[2].type=type;});
250 IMPORT_PLUS(color[0],sync());
251 IMPORT_PLUS(color[1],sync());
252 IMPORT_PLUS(color[2],sync());
254 IMPORT_PLUS(subtractive,sync());
256 IMPORT(tone[0].angle);
257 IMPORT(tone[0].origin);
259 IMPORT(tone[1].angle);
260 IMPORT(tone[1].origin);
262 IMPORT(tone[2].angle);
263 IMPORT(tone[2].origin);
265 IMPORT_AS(tone[0].origin,"tone[0].offset");
266 IMPORT_AS(tone[1].origin,"tone[1].offset");
267 IMPORT_AS(tone[2].origin,"tone[2].offset");
269 return Layer_Composite::set_param(param,value);
273 Halftone3::get_param(const String & param)const
284 EXPORT(tone[0].angle);
285 EXPORT(tone[0].origin);
287 EXPORT(tone[1].angle);
288 EXPORT(tone[1].origin);
290 EXPORT(tone[2].angle);
291 EXPORT(tone[2].origin);
296 return Layer_Composite::get_param(param);
300 Halftone3::get_param_vocab()const
302 Layer::Vocab ret(Layer_Composite::get_param_vocab());
304 ret.push_back(ParamDesc("size")
305 .set_local_name(_("Mask Size"))
307 ret.push_back(ParamDesc("type")
308 .set_local_name(_(" Type"))
310 .add_enum_value(TYPE_SYMMETRIC,"symmetric",_("Symmetric"))
311 .add_enum_value(TYPE_LIGHTONDARK,"lightondark",_("Light On Dark"))
312 //.add_enum_value(TYPE_DARKONLIGHT,"darkonlight",_("Dark on Light"))
313 .add_enum_value(TYPE_DIAMOND,"diamond",_("Diamond"))
314 .add_enum_value(TYPE_STRIPE,"stripe",_("Stripe"))
316 ret.push_back(ParamDesc("subtractive")
317 .set_local_name(_("Subtractive Flag"))
322 String chan_name(strprintf("Chan%d",i));
324 ret.push_back(ParamDesc(strprintf("color[%d]",i))
325 .set_local_name(chan_name+_(" Color"))
328 ret.push_back(ParamDesc(strprintf("tone[%d].origin",i))
329 .set_local_name(chan_name+_(" Mask Origin"))
332 ret.push_back(ParamDesc(strprintf("tone[%d].angle",i))
333 .set_local_name(chan_name+_(" Mask Angle"))
334 .set_origin(strprintf("tone[%d].origin",i))
342 Halftone3::get_color(Context context, const Point &point)const
344 const Color undercolor(context.get_color(point));
345 const Color color(color_func(point,0,undercolor));
347 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
350 return Color::blend(color,undercolor,get_amount(),get_blend_method());
354 Halftone3::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
356 SuperCallback supercb(cb,0,9500,10000);
358 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
363 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
364 const Point tl(renddesc.get_tl());
365 const int w(surface->get_w());
366 const int h(surface->get_h());
367 const float supersample_size(abs(pw/(tone[0].size).mag()));
369 Surface::pen pen(surface->begin());
375 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
376 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
387 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
388 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
403 // Mark our progress as finished
404 if(cb && !cb->amount_complete(10000,10000))