1 /* === S Y N F I G ========================================================= */
2 /*! \file halftone3.cpp
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 ======================================================= */
32 #include "halftone3.h"
35 #include <synfig/string.h>
36 #include <synfig/time.h>
37 #include <synfig/context.h>
38 #include <synfig/paramdesc.h>
39 #include <synfig/renddesc.h>
40 #include <synfig/surface.h>
41 #include <synfig/value.h>
42 #include <synfig/valuenode.h>
46 /* === M A C R O S ========================================================= */
48 using namespace synfig;
52 /* === G L O B A L S ======================================================= */
54 SYNFIG_LAYER_INIT(Halftone3);
55 SYNFIG_LAYER_SET_NAME(Halftone3,"halftone3");
56 SYNFIG_LAYER_SET_LOCAL_NAME(Halftone3,_("Halftone 3"));
57 SYNFIG_LAYER_SET_CATEGORY(Halftone3,_("Filters"));
58 SYNFIG_LAYER_SET_VERSION(Halftone3,"0.0");
59 SYNFIG_LAYER_SET_CVS_ID(Halftone3,"$Id$");
61 /* === P R O C E D U R E S ================================================= */
63 #define HALFSQRT2 (0.7)
64 #define SQRT2 (1.414213562f)
66 /* === M E T H O D S ======================================================= */
68 Halftone3::Halftone3()
70 size=(synfig::Vector(0.25,0.25));
77 tone[i].offset=(synfig::Point(0,0));
78 tone[i].angle=Angle::deg(30.0)*(float)i;
85 color[0]=Color::cyan();
86 color[1]=Color::magenta();
87 color[2]=Color::yellow();
91 color[0]=Color::red();
92 color[1]=Color::green();
93 color[2]=Color::blue();
96 set_blend_method(Color::BLEND_STRAIGHT);
100 inverse_matrix[i][j]=(j==i)?1.0f:0.0f;
114 #define matrix inverse_matrix
115 //float matrix[3][3];
121 matrix[i][0]=1.0f-(color[i].get_r());
122 matrix[i][1]=1.0f-(color[i].get_g());
123 matrix[i][2]=1.0f-(color[i].get_b());
124 float mult=sqrt(matrix[i][0]*matrix[i][0]+matrix[i][1]*matrix[i][1]+matrix[i][2]*matrix[i][2]);
140 matrix[i][0]=color[i].get_r();
141 matrix[i][1]=color[i].get_g();
142 matrix[i][2]=color[i].get_b();
143 float mult=sqrt(matrix[i][0]*matrix[i][0]+matrix[i][1]*matrix[i][1]+matrix[i][2]*matrix[i][2]);
160 // Insert guass-jordan elimination code here
161 int k=0,i=0,j=0,z_size=3;
162 #define A inverse_matrix
164 for (k=0;k<z_size;k++)
166 { A[k][k]= -1/A[k][k];
169 for (i=0;i<z_size;i++)
170 if (i!=k) A[i][k]*=A[k][k];
172 //elements not in a pivot row or column
173 for (i=0;i<z_size;i++)
175 for (j=0;j<z_size;j++)
177 A[i][j]+=A[i][k]*A[k][j];
179 //elements in a pivot row
180 for (i=0;i<z_size;i++)
186 for (i=0;i<z_size;i++) /*reverse sign*/
187 for (j=0;j<z_size;j++)
194 Halftone3::color_func(const Point &point, float supersample,const Color& in_color)const
203 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());
204 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());
205 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());
207 halfcolor=Color::white();
208 halfcolor-=(~color[0])*tone[0](point,chan[0],supersample);
209 halfcolor-=(~color[1])*tone[1](point,chan[1],supersample);
210 halfcolor-=(~color[2])*tone[2](point,chan[2],supersample);
212 halfcolor.set_a(in_color.get_a());
216 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();
217 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();
218 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();
220 halfcolor=Color::black();
221 halfcolor+=color[0]*tone[0](point,chan[0],supersample);
222 halfcolor+=color[1]*tone[1](point,chan[1],supersample);
223 halfcolor+=color[2]*tone[2](point,chan[2],supersample);
225 halfcolor.set_a(in_color.get_a());
232 Halftone3::calc_supersample(const synfig::Point &x, float pw,float ph)const
234 return abs(pw/(tone[0].size).mag());
237 synfig::Layer::Handle
238 Halftone3::hit_check(synfig::Context context, const synfig::Point &point)const
240 return const_cast<Halftone3*>(this);
244 Halftone3::set_param(const String & param, const ValueBase &value)
246 IMPORT_PLUS(size, {tone[0].size=size; tone[1].size=size; tone[2].size=size;});
247 IMPORT_PLUS(type, {tone[0].type=type; tone[1].type=type; tone[2].type=type;});
249 IMPORT_PLUS(color[0],sync());
250 IMPORT_PLUS(color[1],sync());
251 IMPORT_PLUS(color[2],sync());
253 IMPORT_PLUS(subtractive,sync());
255 IMPORT(tone[0].angle);
256 IMPORT(tone[0].offset);
258 IMPORT(tone[1].angle);
259 IMPORT(tone[1].offset);
261 IMPORT(tone[2].angle);
262 IMPORT(tone[2].offset);
264 return Layer_Composite::set_param(param,value);
268 Halftone3::get_param(const String & param)const
279 EXPORT(tone[0].angle);
280 EXPORT(tone[0].offset);
282 EXPORT(tone[1].angle);
283 EXPORT(tone[1].offset);
285 EXPORT(tone[2].angle);
286 EXPORT(tone[2].offset);
291 return Layer_Composite::get_param(param);
295 Halftone3::get_param_vocab()const
297 Layer::Vocab ret(Layer_Composite::get_param_vocab());
299 ret.push_back(ParamDesc("size")
300 .set_local_name(_("Mask Size"))
302 ret.push_back(ParamDesc("type")
303 .set_local_name(_(" Type"))
305 .add_enum_value(TYPE_SYMMETRIC,"symmetric",_("Symmetric"))
306 .add_enum_value(TYPE_LIGHTONDARK,"lightondark",_("Light On Dark"))
307 //.add_enum_value(TYPE_DARKONLIGHT,"darkonlight",_("Dark on Light"))
308 .add_enum_value(TYPE_DIAMOND,"diamond",_("Diamond"))
309 .add_enum_value(TYPE_STRIPE,"stripe",_("Stripe"))
311 ret.push_back(ParamDesc("subtractive")
312 .set_local_name(_("Subtractive Flag"))
317 String chan_name(strprintf("Chan%d",i));
319 ret.push_back(ParamDesc(strprintf("color[%d]",i))
320 .set_local_name(chan_name+_(" Color"))
323 ret.push_back(ParamDesc(strprintf("tone[%d].offset",i))
324 .set_local_name(chan_name+_(" Mask Offset"))
327 ret.push_back(ParamDesc(strprintf("tone[%d].angle",i))
328 .set_local_name(chan_name+_(" Mask Angle"))
329 .set_origin(strprintf("tone[%d].offset",i))
337 Halftone3::get_color(Context context, const Point &point)const
339 const Color undercolor(context.get_color(point));
340 const Color color(color_func(point,0,undercolor));
342 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
345 return Color::blend(color,undercolor,get_amount(),get_blend_method());
349 Halftone3::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
351 SuperCallback supercb(cb,0,9500,10000);
353 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
358 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
359 const Point tl(renddesc.get_tl());
360 const int w(surface->get_w());
361 const int h(surface->get_h());
362 const float supersample_size(abs(pw/(tone[0].size).mag()));
364 Surface::pen pen(surface->begin());
370 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
371 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
382 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
383 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
398 // Mark our progress as finished
399 if(cb && !cb->amount_complete(10000,10000))