moreupdates
[synfig.git] / synfig-core / trunk / src / modules / mod_filter / halftone3.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file halftone3.cpp
3 **      \brief blehh
4 **
5 **      $Id: halftone3.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "halftone3.h"
32 #include "halftone.h"
33
34 #include <synfig/string.h>
35 #include <synfig/time.h>
36 #include <synfig/context.h>
37 #include <synfig/paramdesc.h>
38 #include <synfig/renddesc.h>
39 #include <synfig/surface.h>
40 #include <synfig/value.h>
41 #include <synfig/valuenode.h>
42
43 #endif
44
45 /* === M A C R O S ========================================================= */
46
47 using namespace synfig;
48 using namespace std;
49 using namespace etl;
50
51 /* === G L O B A L S ======================================================= */
52
53 SYNFIG_LAYER_INIT(Halftone3);
54 SYNFIG_LAYER_SET_NAME(Halftone3,"halftone3");
55 SYNFIG_LAYER_SET_LOCAL_NAME(Halftone3,_("Halftone3"));
56 SYNFIG_LAYER_SET_CATEGORY(Halftone3,_("Filters"));
57 SYNFIG_LAYER_SET_VERSION(Halftone3,"0.0");
58 SYNFIG_LAYER_SET_CVS_ID(Halftone3,"$Id: halftone3.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
59
60 /* === P R O C E D U R E S ================================================= */
61
62 #define HALFSQRT2       (0.7)
63 #define SQRT2   (1.414213562f)
64
65 /* === M E T H O D S ======================================================= */
66
67 Halftone3::Halftone3()
68 {
69         size=(synfig::Vector(0.25,0.25));
70         type=TYPE_SYMMETRIC;
71
72         for(int i=0;i<3;i++)
73         {
74                 tone[i].size=size;
75                 tone[i].type=type;
76                 tone[i].offset=(synfig::Point(0,0));
77                 tone[i].angle=Angle::deg(30.0)*(float)i;
78         }
79
80         subtractive=true;
81
82         if(subtractive)
83         {
84                 color[0]=Color::cyan();
85                 color[1]=Color::magenta();
86                 color[2]=Color::yellow();
87         }
88         else
89         {
90                 color[0]=Color::red();
91                 color[1]=Color::green();
92                 color[2]=Color::blue();
93         }
94                 
95         set_blend_method(Color::BLEND_STRAIGHT);
96
97         for(int i=0;i<3;i++)
98                 for(int j=0;j<3;j++)
99                         inverse_matrix[i][j]=(j==i)?1.0f:0.0f;
100
101         sync();
102 }
103
104 void
105 Halftone3::sync()
106 {
107         for(int i=0;i<3;i++)
108         {
109                 tone[i].size=size;
110                 tone[i].type=type;
111         }
112         
113 #define matrix inverse_matrix
114         //float matrix[3][3];
115         
116         if(subtractive)
117         {
118                 for(int i=0;i<3;i++)
119                 {
120                         matrix[i][0]=1.0f-(color[i].get_r());
121                         matrix[i][1]=1.0f-(color[i].get_g());
122                         matrix[i][2]=1.0f-(color[i].get_b());
123                         float mult=sqrt(matrix[i][0]*matrix[i][0]+matrix[i][1]*matrix[i][1]+matrix[i][2]*matrix[i][2]);
124                         if(mult)
125                         {
126                                 matrix[i][0]/=mult;
127                                 matrix[i][1]/=mult;
128                                 matrix[i][2]/=mult;
129                                 matrix[i][0]/=mult;
130                                 matrix[i][1]/=mult;
131                                 matrix[i][2]/=mult;
132                         }
133                 }
134         }
135         else
136         {
137                 for(int i=0;i<3;i++)
138                 {
139                         matrix[i][0]=color[i].get_r();
140                         matrix[i][1]=color[i].get_g();
141                         matrix[i][2]=color[i].get_b();
142                         float mult=sqrt(matrix[i][0]*matrix[i][0]+matrix[i][1]*matrix[i][1]+matrix[i][2]*matrix[i][2]);
143                         if(mult)
144                         {
145                                 matrix[i][0]/=mult;
146                                 matrix[i][1]/=mult;
147                                 matrix[i][2]/=mult;
148                                 matrix[i][0]/=mult;
149                                 matrix[i][1]/=mult;
150                                 matrix[i][2]/=mult;
151                         }
152                 }
153         }
154 #undef matrix
155
156         
157         
158 #if 0
159         // Insert guass-jordan elimination code here
160         int k=0,i=0,j=0,z_size=3;
161 #define A inverse_matrix
162         
163         for (k=0;k<z_size;k++)   
164   // the pivot element 
165     { A[k][k]= -1/A[k][k];   
166  
167   //the pivot column
168      for (i=0;i<z_size;i++)             
169          if (i!=k) A[i][k]*=A[k][k];
170
171  //elements not in a pivot row or column
172      for (i=0;i<z_size;i++)
173         if (i!=k) 
174             for (j=0;j<z_size;j++)
175                       if (j!=k)
176                           A[i][j]+=A[i][k]*A[k][j];
177
178  //elements in a pivot row
179     for (i=0;i<z_size;i++)
180        if (i!=k) 
181             A[k][i]*=A[k][k];
182    }
183
184  //change sign
185    for (i=0;i<z_size;i++)        /*reverse sign*/
186      for (j=0;j<z_size;j++)
187         A[i][j]=-A[i][j];       
188 #undef A
189 #endif
190 }
191
192 inline Color
193 Halftone3::color_func(const Point &point, float supersample,const Color& in_color)const
194 {
195         Color halfcolor;
196
197         float chan[3];
198         
199         
200         if(subtractive)
201         {
202                 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());
203                 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());
204                 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());
205
206                 halfcolor=Color::white();
207                 halfcolor-=(~color[0])*tone[0](point,chan[0],supersample);
208                 halfcolor-=(~color[1])*tone[1](point,chan[1],supersample);
209                 halfcolor-=(~color[2])*tone[2](point,chan[2],supersample);
210         
211                 halfcolor.set_a(in_color.get_a());
212         }
213         else
214         {
215                 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();
216                 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();
217                 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();
218
219                 halfcolor=Color::black();
220                 halfcolor+=color[0]*tone[0](point,chan[0],supersample);
221                 halfcolor+=color[1]*tone[1](point,chan[1],supersample);
222                 halfcolor+=color[2]*tone[2](point,chan[2],supersample);
223         
224                 halfcolor.set_a(in_color.get_a());
225         }
226
227         return halfcolor;
228 }
229
230 inline float
231 Halftone3::calc_supersample(const synfig::Point &x, float pw,float ph)const
232 {
233         return abs(pw/(tone[0].size).mag());
234 }
235
236 synfig::Layer::Handle
237 Halftone3::hit_check(synfig::Context context, const synfig::Point &point)const
238 {
239         return const_cast<Halftone3*>(this);
240 }
241
242 bool
243 Halftone3::set_param(const String & param, const ValueBase &value)
244 {
245         IMPORT_PLUS(size, {tone[0].size=size; tone[1].size=size; tone[2].size=size;});
246         IMPORT_PLUS(type, {tone[0].type=type; tone[1].type=type; tone[2].type=type;});
247
248         IMPORT_PLUS(color[0],sync());
249         IMPORT_PLUS(color[1],sync());
250         IMPORT_PLUS(color[2],sync());
251         
252         IMPORT_PLUS(subtractive,sync());
253         
254         IMPORT(tone[0].angle);
255         IMPORT(tone[0].offset);
256
257         IMPORT(tone[1].angle);
258         IMPORT(tone[1].offset);
259
260         IMPORT(tone[2].angle);
261         IMPORT(tone[2].offset);
262         
263         return Layer_Composite::set_param(param,value); 
264 }
265
266 ValueBase
267 Halftone3::get_param(const String & param)const
268 {
269         EXPORT(size);
270         EXPORT(type);
271
272         EXPORT(color[0]);
273         EXPORT(color[1]);
274         EXPORT(color[2]);
275
276         EXPORT(subtractive);
277         
278         EXPORT(tone[0].angle);
279         EXPORT(tone[0].offset);
280
281         EXPORT(tone[1].angle);
282         EXPORT(tone[1].offset);
283
284         EXPORT(tone[2].angle);
285         EXPORT(tone[2].offset);
286
287         EXPORT_NAME();
288         EXPORT_VERSION();
289                 
290         return Layer_Composite::get_param(param);       
291 }
292
293 Layer::Vocab
294 Halftone3::get_param_vocab()const
295 {
296         Layer::Vocab ret(Layer_Composite::get_param_vocab());
297
298         ret.push_back(ParamDesc("size")
299                 .set_local_name(_("Mask Size"))
300         );
301         ret.push_back(ParamDesc("type")
302                 .set_local_name(_(" Type"))
303                 .set_hint("enum")
304                 .add_enum_value(TYPE_SYMMETRIC,"symmetric",_("Symmetric"))
305                 .add_enum_value(TYPE_LIGHTONDARK,"lightondark",_("Light On Dark"))
306                 //.add_enum_value(TYPE_DARKONLIGHT,"darkonlight",_("Dark on Light"))
307                 .add_enum_value(TYPE_DIAMOND,"diamond",_("Diamond"))
308                 .add_enum_value(TYPE_STRIPE,"stripe",_("Stripe"))
309         );
310         ret.push_back(ParamDesc("subtractive")
311                 .set_local_name(_("Subtractive Flag"))
312         );
313         
314         for(int i=0;i<3;i++)
315         {
316                 String chan_name(strprintf("Chan%d",i));
317                 
318                 ret.push_back(ParamDesc(strprintf("color[%d]",i))
319                         .set_local_name(chan_name+_(" Color"))
320                 );
321
322                 ret.push_back(ParamDesc(strprintf("tone[%d].offset",i))
323                         .set_local_name(chan_name+_(" Mask Offset"))
324                         .set_is_distance()
325                 );
326                 ret.push_back(ParamDesc(strprintf("tone[%d].angle",i))
327                         .set_local_name(chan_name+_(" Mask Angle"))
328                         .set_origin(strprintf("tone[%d].offset",i))
329                 );
330         }
331         
332         return ret;
333 }
334
335 Color
336 Halftone3::get_color(Context context, const Point &point)const
337 {
338         const Color undercolor(context.get_color(point));
339         const Color color(color_func(point,0,undercolor));
340
341         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
342                 return color;
343         else
344                 return Color::blend(color,undercolor,get_amount(),get_blend_method());
345 }
346
347 bool
348 Halftone3::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
349 {
350         SuperCallback supercb(cb,0,9500,10000);
351
352         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
353                 return false;
354         if(get_amount()==0)
355                 return true;
356                 
357         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
358         const Point tl(renddesc.get_tl());
359         const int w(surface->get_w());
360         const int h(surface->get_h());
361         const float supersample_size(abs(pw/(tone[0].size).mag()));
362
363         Surface::pen pen(surface->begin());
364         Point pos;
365         int x,y;
366         
367         if(is_solid_color())
368         {
369                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
370                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
371                                 pen.put_value(
372                                         color_func(
373                                                 pos,
374                                                 supersample_size,
375                                                 pen.get_value()
376                                         )
377                                 );
378         }
379         else
380         {
381                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
382                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
383                                 pen.put_value(
384                                         Color::blend(
385                                                 color_func(
386                                                         pos,
387                                                         supersample_size,
388                                                         pen.get_value()
389                                                 ),
390                                                 pen.get_value(),
391                                                 get_amount(),
392                                                 get_blend_method()
393                                         )
394                                 );
395         }
396         
397         // Mark our progress as finished
398         if(cb && !cb->amount_complete(10000,10000))
399                 return false;
400
401         return true;
402 }