Merge branch 'genete_static_values'
[synfig.git] / synfig-core / src / modules / mod_filter / halftone3.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file halftone3.cpp
3 **      \brief Implementation of the "Halftone 3" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007-2008 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "halftone3.h"
34 #include "halftone.h"
35
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>
44
45 #endif
46
47 /* === M A C R O S ========================================================= */
48
49 using namespace synfig;
50 using namespace std;
51 using namespace etl;
52
53 /* === G L O B A L S ======================================================= */
54
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$");
61
62 /* === P R O C E D U R E S ================================================= */
63
64 #define HALFSQRT2       (0.7)
65 #define SQRT2   (1.414213562f)
66
67 /* === M E T H O D S ======================================================= */
68
69 Halftone3::Halftone3()
70 {
71         size=(synfig::Vector(0.25,0.25));
72         type=TYPE_SYMMETRIC;
73
74         for(int i=0;i<3;i++)
75         {
76                 tone[i].size=size;
77                 tone[i].type=type;
78                 tone[i].origin=(synfig::Point(0,0));
79                 tone[i].angle=Angle::deg(30.0)*(float)i;
80         }
81
82         subtractive=true;
83
84         if(subtractive)
85         {
86                 color[0]=Color::cyan();
87                 color[1]=Color::magenta();
88                 color[2]=Color::yellow();
89         }
90         else
91         {
92                 color[0]=Color::red();
93                 color[1]=Color::green();
94                 color[2]=Color::blue();
95         }
96
97         set_blend_method(Color::BLEND_STRAIGHT);
98
99         for(int i=0;i<3;i++)
100                 for(int j=0;j<3;j++)
101                         inverse_matrix[i][j]=(j==i)?1.0f:0.0f;
102
103         sync();
104
105         Layer::Vocab voc(get_param_vocab());
106         Layer::fill_static(voc);
107 }
108
109 void
110 Halftone3::sync()
111 {
112         for(int i=0;i<3;i++)
113         {
114                 tone[i].size=size;
115                 tone[i].type=type;
116         }
117
118 #define matrix inverse_matrix
119         //float matrix[3][3];
120
121         if(subtractive)
122         {
123                 for(int i=0;i<3;i++)
124                 {
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]);
129                         if(mult)
130                         {
131                                 matrix[i][0]/=mult;
132                                 matrix[i][1]/=mult;
133                                 matrix[i][2]/=mult;
134                                 matrix[i][0]/=mult;
135                                 matrix[i][1]/=mult;
136                                 matrix[i][2]/=mult;
137                         }
138                 }
139         }
140         else
141         {
142                 for(int i=0;i<3;i++)
143                 {
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]);
148                         if(mult)
149                         {
150                                 matrix[i][0]/=mult;
151                                 matrix[i][1]/=mult;
152                                 matrix[i][2]/=mult;
153                                 matrix[i][0]/=mult;
154                                 matrix[i][1]/=mult;
155                                 matrix[i][2]/=mult;
156                         }
157                 }
158         }
159 #undef matrix
160
161
162
163 #if 0
164         // Insert guass-jordan elimination code here
165         int k=0,i=0,j=0,z_size=3;
166 #define A inverse_matrix
167
168         for (k=0;k<z_size;k++)
169   // the pivot element
170     { A[k][k]= -1/A[k][k];
171
172   //the pivot column
173      for (i=0;i<z_size;i++)
174          if (i!=k) A[i][k]*=A[k][k];
175
176  //elements not in a pivot row or column
177      for (i=0;i<z_size;i++)
178         if (i!=k)
179             for (j=0;j<z_size;j++)
180                       if (j!=k)
181                           A[i][j]+=A[i][k]*A[k][j];
182
183  //elements in a pivot row
184     for (i=0;i<z_size;i++)
185        if (i!=k)
186             A[k][i]*=A[k][k];
187    }
188
189  //change sign
190    for (i=0;i<z_size;i++)        /*reverse sign*/
191      for (j=0;j<z_size;j++)
192         A[i][j]=-A[i][j];
193 #undef A
194 #endif
195 }
196
197 inline Color
198 Halftone3::color_func(const Point &point, float supersample,const Color& in_color)const
199 {
200         Color halfcolor;
201
202         float chan[3];
203
204
205         if(subtractive)
206         {
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());
210
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);
215
216                 halfcolor.set_a(in_color.get_a());
217         }
218         else
219         {
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();
223
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);
228
229                 halfcolor.set_a(in_color.get_a());
230         }
231
232         return halfcolor;
233 }
234
235 inline float
236 Halftone3::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
237 {
238         return abs(pw/(tone[0].size).mag());
239 }
240
241 synfig::Layer::Handle
242 Halftone3::hit_check(synfig::Context /*context*/, const synfig::Point &/*point*/)const
243 {
244         return const_cast<Halftone3*>(this);
245 }
246
247 bool
248 Halftone3::set_param(const String & param, const ValueBase &value)
249 {
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;});
252
253         IMPORT_PLUS(color[0],sync());
254         IMPORT_PLUS(color[1],sync());
255         IMPORT_PLUS(color[2],sync());
256
257         IMPORT_PLUS(subtractive,sync());
258
259         IMPORT(tone[0].angle);
260         IMPORT(tone[0].origin);
261
262         IMPORT(tone[1].angle);
263         IMPORT(tone[1].origin);
264
265         IMPORT(tone[2].angle);
266         IMPORT(tone[2].origin);
267
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");
271
272         return Layer_Composite::set_param(param,value);
273 }
274
275 ValueBase
276 Halftone3::get_param(const String & param)const
277 {
278         EXPORT(size);
279         EXPORT(type);
280
281         EXPORT(color[0]);
282         EXPORT(color[1]);
283         EXPORT(color[2]);
284
285         EXPORT(subtractive);
286
287         EXPORT(tone[0].angle);
288         EXPORT(tone[0].origin);
289
290         EXPORT(tone[1].angle);
291         EXPORT(tone[1].origin);
292
293         EXPORT(tone[2].angle);
294         EXPORT(tone[2].origin);
295
296         EXPORT_NAME();
297         EXPORT_VERSION();
298
299         return Layer_Composite::get_param(param);
300 }
301
302 Layer::Vocab
303 Halftone3::get_param_vocab()const
304 {
305         Layer::Vocab ret(Layer_Composite::get_param_vocab());
306
307         ret.push_back(ParamDesc("size")
308                 .set_local_name(_("Mask Size"))
309         );
310         ret.push_back(ParamDesc("type")
311                 .set_local_name(_(" Type"))
312                 .set_hint("enum")
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"))
318         );
319         ret.push_back(ParamDesc("subtractive")
320                 .set_local_name(_("Subtractive Flag"))
321         );
322
323         for(int i=0;i<3;i++)
324         {
325                 String chan_name(strprintf("Chan%d",i));
326
327                 ret.push_back(ParamDesc(strprintf("color[%d]",i))
328                         .set_local_name(chan_name+_(" Color"))
329                 );
330
331                 ret.push_back(ParamDesc(strprintf("tone[%d].origin",i))
332                         .set_local_name(chan_name+_(" Mask Origin"))
333                         .set_is_distance()
334                 );
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))
338                 );
339         }
340
341         return ret;
342 }
343
344 Color
345 Halftone3::get_color(Context context, const Point &point)const
346 {
347         const Color undercolor(context.get_color(point));
348         const Color color(color_func(point,0,undercolor));
349
350         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
351                 return color;
352         else
353                 return Color::blend(color,undercolor,get_amount(),get_blend_method());
354 }
355
356 bool
357 Halftone3::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
358 {
359         SuperCallback supercb(cb,0,9500,10000);
360
361         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
362                 return false;
363         if(get_amount()==0)
364                 return true;
365
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()));
371
372         Surface::pen pen(surface->begin());
373         Point pos;
374         int x,y;
375
376         if(is_solid_color())
377         {
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)
380                                 pen.put_value(
381                                         color_func(
382                                                 pos,
383                                                 supersample_size,
384                                                 pen.get_value()
385                                         )
386                                 );
387         }
388         else
389         {
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)
392                                 pen.put_value(
393                                         Color::blend(
394                                                 color_func(
395                                                         pos,
396                                                         supersample_size,
397                                                         pen.get_value()
398                                                 ),
399                                                 pen.get_value(),
400                                                 get_amount(),
401                                                 get_blend_method()
402                                         )
403                                 );
404         }
405
406         // Mark our progress as finished
407         if(cb && !cb->amount_complete(10000,10000))
408                 return false;
409
410         return true;
411 }