Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc1 / src / modules / mod_filter / halftone3.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file halftone3.cpp
3 **      \brief blehh
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 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,_("Halftone 3"));
58 SYNFIG_LAYER_SET_CATEGORY(Halftone3,_("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].offset=(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
106 void
107 Halftone3::sync()
108 {
109         for(int i=0;i<3;i++)
110         {
111                 tone[i].size=size;
112                 tone[i].type=type;
113         }
114
115 #define matrix inverse_matrix
116         //float matrix[3][3];
117
118         if(subtractive)
119         {
120                 for(int i=0;i<3;i++)
121                 {
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]);
126                         if(mult)
127                         {
128                                 matrix[i][0]/=mult;
129                                 matrix[i][1]/=mult;
130                                 matrix[i][2]/=mult;
131                                 matrix[i][0]/=mult;
132                                 matrix[i][1]/=mult;
133                                 matrix[i][2]/=mult;
134                         }
135                 }
136         }
137         else
138         {
139                 for(int i=0;i<3;i++)
140                 {
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]);
145                         if(mult)
146                         {
147                                 matrix[i][0]/=mult;
148                                 matrix[i][1]/=mult;
149                                 matrix[i][2]/=mult;
150                                 matrix[i][0]/=mult;
151                                 matrix[i][1]/=mult;
152                                 matrix[i][2]/=mult;
153                         }
154                 }
155         }
156 #undef matrix
157
158
159
160 #if 0
161         // Insert guass-jordan elimination code here
162         int k=0,i=0,j=0,z_size=3;
163 #define A inverse_matrix
164
165         for (k=0;k<z_size;k++)
166   // the pivot element
167     { A[k][k]= -1/A[k][k];
168
169   //the pivot column
170      for (i=0;i<z_size;i++)
171          if (i!=k) A[i][k]*=A[k][k];
172
173  //elements not in a pivot row or column
174      for (i=0;i<z_size;i++)
175         if (i!=k)
176             for (j=0;j<z_size;j++)
177                       if (j!=k)
178                           A[i][j]+=A[i][k]*A[k][j];
179
180  //elements in a pivot row
181     for (i=0;i<z_size;i++)
182        if (i!=k)
183             A[k][i]*=A[k][k];
184    }
185
186  //change sign
187    for (i=0;i<z_size;i++)        /*reverse sign*/
188      for (j=0;j<z_size;j++)
189         A[i][j]=-A[i][j];
190 #undef A
191 #endif
192 }
193
194 inline Color
195 Halftone3::color_func(const Point &point, float supersample,const Color& in_color)const
196 {
197         Color halfcolor;
198
199         float chan[3];
200
201
202         if(subtractive)
203         {
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());
207
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);
212
213                 halfcolor.set_a(in_color.get_a());
214         }
215         else
216         {
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();
220
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);
225
226                 halfcolor.set_a(in_color.get_a());
227         }
228
229         return halfcolor;
230 }
231
232 inline float
233 Halftone3::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
234 {
235         return abs(pw/(tone[0].size).mag());
236 }
237
238 synfig::Layer::Handle
239 Halftone3::hit_check(synfig::Context /*context*/, const synfig::Point &/*point*/)const
240 {
241         return const_cast<Halftone3*>(this);
242 }
243
244 bool
245 Halftone3::set_param(const String & param, const ValueBase &value)
246 {
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;});
249
250         IMPORT_PLUS(color[0],sync());
251         IMPORT_PLUS(color[1],sync());
252         IMPORT_PLUS(color[2],sync());
253
254         IMPORT_PLUS(subtractive,sync());
255
256         IMPORT(tone[0].angle);
257         IMPORT(tone[0].offset);
258
259         IMPORT(tone[1].angle);
260         IMPORT(tone[1].offset);
261
262         IMPORT(tone[2].angle);
263         IMPORT(tone[2].offset);
264
265         return Layer_Composite::set_param(param,value);
266 }
267
268 ValueBase
269 Halftone3::get_param(const String & param)const
270 {
271         EXPORT(size);
272         EXPORT(type);
273
274         EXPORT(color[0]);
275         EXPORT(color[1]);
276         EXPORT(color[2]);
277
278         EXPORT(subtractive);
279
280         EXPORT(tone[0].angle);
281         EXPORT(tone[0].offset);
282
283         EXPORT(tone[1].angle);
284         EXPORT(tone[1].offset);
285
286         EXPORT(tone[2].angle);
287         EXPORT(tone[2].offset);
288
289         EXPORT_NAME();
290         EXPORT_VERSION();
291
292         return Layer_Composite::get_param(param);
293 }
294
295 Layer::Vocab
296 Halftone3::get_param_vocab()const
297 {
298         Layer::Vocab ret(Layer_Composite::get_param_vocab());
299
300         ret.push_back(ParamDesc("size")
301                 .set_local_name(_("Mask Size"))
302         );
303         ret.push_back(ParamDesc("type")
304                 .set_local_name(_(" Type"))
305                 .set_hint("enum")
306                 .add_enum_value(TYPE_SYMMETRIC,"symmetric",_("Symmetric"))
307                 .add_enum_value(TYPE_LIGHTONDARK,"lightondark",_("Light On Dark"))
308                 //.add_enum_value(TYPE_DARKONLIGHT,"darkonlight",_("Dark on Light"))
309                 .add_enum_value(TYPE_DIAMOND,"diamond",_("Diamond"))
310                 .add_enum_value(TYPE_STRIPE,"stripe",_("Stripe"))
311         );
312         ret.push_back(ParamDesc("subtractive")
313                 .set_local_name(_("Subtractive Flag"))
314         );
315
316         for(int i=0;i<3;i++)
317         {
318                 String chan_name(strprintf("Chan%d",i));
319
320                 ret.push_back(ParamDesc(strprintf("color[%d]",i))
321                         .set_local_name(chan_name+_(" Color"))
322                 );
323
324                 ret.push_back(ParamDesc(strprintf("tone[%d].offset",i))
325                         .set_local_name(chan_name+_(" Mask Offset"))
326                         .set_is_distance()
327                 );
328                 ret.push_back(ParamDesc(strprintf("tone[%d].angle",i))
329                         .set_local_name(chan_name+_(" Mask Angle"))
330                         .set_origin(strprintf("tone[%d].offset",i))
331                 );
332         }
333
334         return ret;
335 }
336
337 Color
338 Halftone3::get_color(Context context, const Point &point)const
339 {
340         const Color undercolor(context.get_color(point));
341         const Color color(color_func(point,0,undercolor));
342
343         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
344                 return color;
345         else
346                 return Color::blend(color,undercolor,get_amount(),get_blend_method());
347 }
348
349 bool
350 Halftone3::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
351 {
352         SuperCallback supercb(cb,0,9500,10000);
353
354         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
355                 return false;
356         if(get_amount()==0)
357                 return true;
358
359         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
360         const Point tl(renddesc.get_tl());
361         const int w(surface->get_w());
362         const int h(surface->get_h());
363         const float supersample_size(abs(pw/(tone[0].size).mag()));
364
365         Surface::pen pen(surface->begin());
366         Point pos;
367         int x,y;
368
369         if(is_solid_color())
370         {
371                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
372                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
373                                 pen.put_value(
374                                         color_func(
375                                                 pos,
376                                                 supersample_size,
377                                                 pen.get_value()
378                                         )
379                                 );
380         }
381         else
382         {
383                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
384                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
385                                 pen.put_value(
386                                         Color::blend(
387                                                 color_func(
388                                                         pos,
389                                                         supersample_size,
390                                                         pen.get_value()
391                                                 ),
392                                                 pen.get_value(),
393                                                 get_amount(),
394                                                 get_blend_method()
395                                         )
396                                 );
397         }
398
399         // Mark our progress as finished
400         if(cb && !cb->amount_complete(10000,10000))
401                 return false;
402
403         return true;
404 }