a2a4283d18669a607939c6f1dffaf05489b6d0e9
[synfig.git] /
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
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].origin);
258
259         IMPORT(tone[1].angle);
260         IMPORT(tone[1].origin);
261
262         IMPORT(tone[2].angle);
263         IMPORT(tone[2].origin);
264
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");
268
269         return Layer_Composite::set_param(param,value);
270 }
271
272 ValueBase
273 Halftone3::get_param(const String & param)const
274 {
275         EXPORT(size);
276         EXPORT(type);
277
278         EXPORT(color[0]);
279         EXPORT(color[1]);
280         EXPORT(color[2]);
281
282         EXPORT(subtractive);
283
284         EXPORT(tone[0].angle);
285         EXPORT(tone[0].origin);
286
287         EXPORT(tone[1].angle);
288         EXPORT(tone[1].origin);
289
290         EXPORT(tone[2].angle);
291         EXPORT(tone[2].origin);
292
293         EXPORT_NAME();
294         EXPORT_VERSION();
295
296         return Layer_Composite::get_param(param);
297 }
298
299 Layer::Vocab
300 Halftone3::get_param_vocab()const
301 {
302         Layer::Vocab ret(Layer_Composite::get_param_vocab());
303
304         ret.push_back(ParamDesc("size")
305                 .set_local_name(_("Mask Size"))
306         );
307         ret.push_back(ParamDesc("type")
308                 .set_local_name(_(" Type"))
309                 .set_hint("enum")
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"))
315         );
316         ret.push_back(ParamDesc("subtractive")
317                 .set_local_name(_("Subtractive Flag"))
318         );
319
320         for(int i=0;i<3;i++)
321         {
322                 String chan_name(strprintf("Chan%d",i));
323
324                 ret.push_back(ParamDesc(strprintf("color[%d]",i))
325                         .set_local_name(chan_name+_(" Color"))
326                 );
327
328                 ret.push_back(ParamDesc(strprintf("tone[%d].origin",i))
329                         .set_local_name(chan_name+_(" Mask Origin"))
330                         .set_is_distance()
331                 );
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))
335                 );
336         }
337
338         return ret;
339 }
340
341 Color
342 Halftone3::get_color(Context context, const Point &point)const
343 {
344         const Color undercolor(context.get_color(point));
345         const Color color(color_func(point,0,undercolor));
346
347         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
348                 return color;
349         else
350                 return Color::blend(color,undercolor,get_amount(),get_blend_method());
351 }
352
353 bool
354 Halftone3::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
355 {
356         SuperCallback supercb(cb,0,9500,10000);
357
358         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
359                 return false;
360         if(get_amount()==0)
361                 return true;
362
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()));
368
369         Surface::pen pen(surface->begin());
370         Point pos;
371         int x,y;
372
373         if(is_solid_color())
374         {
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)
377                                 pen.put_value(
378                                         color_func(
379                                                 pos,
380                                                 supersample_size,
381                                                 pen.get_value()
382                                         )
383                                 );
384         }
385         else
386         {
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)
389                                 pen.put_value(
390                                         Color::blend(
391                                                 color_func(
392                                                         pos,
393                                                         supersample_size,
394                                                         pen.get_value()
395                                                 ),
396                                                 pen.get_value(),
397                                                 get_amount(),
398                                                 get_blend_method()
399                                         )
400                                 );
401         }
402
403         // Mark our progress as finished
404         if(cb && !cb->amount_complete(10000,10000))
405                 return false;
406
407         return true;
408 }