Merge branch 'genete_static_values'
[synfig.git] / synfig-core / src / modules / mod_filter / halftone2.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file halftone2.cpp
3 **      \brief Implementation of the "Halftone 2" 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 "halftone2.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(Halftone2);
56 SYNFIG_LAYER_SET_NAME(Halftone2,"halftone2");
57 SYNFIG_LAYER_SET_LOCAL_NAME(Halftone2,N_("Halftone 2"));
58 SYNFIG_LAYER_SET_CATEGORY(Halftone2,N_("Filters"));
59 SYNFIG_LAYER_SET_VERSION(Halftone2,"0.0");
60 SYNFIG_LAYER_SET_CVS_ID(Halftone2,"$Id$");
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
66 Halftone2::Halftone2():
67         color_dark(Color::black()),
68         color_light(Color::white())
69 {
70         halftone.origin=(synfig::Point(0,0));
71         halftone.size=(synfig::Vector(0.25,0.25));
72         halftone.angle=(Angle::zero());
73         halftone.type=TYPE_SYMMETRIC;
74
75         set_blend_method(Color::BLEND_STRAIGHT);
76
77         Layer::Vocab voc(get_param_vocab());
78         Layer::fill_static(voc);
79 }
80
81 inline Color
82 Halftone2::color_func(const Point &point, float supersample,const Color& color)const
83 {
84         const float amount(halftone(point,color.get_y(),supersample));
85         Color halfcolor;
86
87         if(amount<=0.0f)
88                 halfcolor=color_dark;
89         else if(amount>=1.0f)
90                 halfcolor=color_light;
91         else
92                 halfcolor=Color::blend(color_light,color_dark,amount,Color::BLEND_STRAIGHT);
93
94         halfcolor.set_a(color.get_a());
95
96         return halfcolor;
97 }
98
99 inline float
100 Halftone2::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
101 {
102         return abs(pw/(halftone.size).mag());
103 }
104
105 synfig::Layer::Handle
106 Halftone2::hit_check(synfig::Context /*context*/, const synfig::Point &/*point*/)const
107 {
108         return const_cast<Halftone2*>(this);
109 }
110
111 bool
112 Halftone2::set_param(const String & param, const ValueBase &value)
113 {
114         IMPORT(color_dark);
115         IMPORT(color_light);
116
117         IMPORT_AS(halftone.size,"size");
118         IMPORT_AS(halftone.type,"type");
119         IMPORT_AS(halftone.angle,"angle");
120         IMPORT_AS(halftone.origin,"origin");
121
122         IMPORT_AS(halftone.origin,"offset");
123
124         return Layer_Composite::set_param(param,value);
125 }
126
127 ValueBase
128 Halftone2::get_param(const String & param)const
129 {
130         EXPORT_AS(halftone.size,"size");
131         EXPORT_AS(halftone.type,"type");
132         EXPORT_AS(halftone.angle,"angle");
133         EXPORT_AS(halftone.origin,"origin");
134
135         EXPORT(color_dark);
136         EXPORT(color_light);
137
138         EXPORT_NAME();
139         EXPORT_VERSION();
140
141         return Layer_Composite::get_param(param);
142 }
143
144 Layer::Vocab
145 Halftone2::get_param_vocab()const
146 {
147         Layer::Vocab ret(Layer_Composite::get_param_vocab());
148
149         ret.push_back(ParamDesc("origin")
150                 .set_local_name(_("Mask Origin"))
151                 .set_is_distance()
152         );
153         ret.push_back(ParamDesc("angle")
154                 .set_local_name(_("Mask Angle"))
155                 .set_origin("origin")
156         );
157         ret.push_back(ParamDesc("size")
158                 .set_local_name(_("Mask Size"))
159                 .set_is_distance()
160                 .set_origin("origin")
161         );
162         ret.push_back(ParamDesc("color_light")
163                 .set_local_name(_("Light Color"))
164         );
165         ret.push_back(ParamDesc("color_dark")
166                 .set_local_name(_("Dark Color"))
167         );
168         ret.push_back(ParamDesc("type")
169                 .set_local_name(_("Type"))
170                 .set_hint("enum")
171                 .add_enum_value(TYPE_SYMMETRIC,"symmetric",_("Symmetric"))
172                 .add_enum_value(TYPE_LIGHTONDARK,"lightondark",_("Light On Dark"))
173                 //.add_enum_value(TYPE_DARKONLIGHT,"darkonlight",_("Dark on Light"))
174                 .add_enum_value(TYPE_DIAMOND,"diamond",_("Diamond"))
175                 .add_enum_value(TYPE_STRIPE,"stripe",_("Stripe"))
176         );
177
178         return ret;
179 }
180
181 Color
182 Halftone2::get_color(Context context, const Point &point)const
183 {
184         const Color undercolor(context.get_color(point));
185         const Color color(color_func(point,0,undercolor));
186
187         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
188                 return color;
189         else
190                 return Color::blend(color,undercolor,get_amount(),get_blend_method());
191 }
192
193 bool
194 Halftone2::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
195 {
196         SuperCallback supercb(cb,0,9500,10000);
197
198         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
199                 return false;
200         if(get_amount()==0)
201                 return true;
202
203         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
204         const Point tl(renddesc.get_tl());
205         const int w(surface->get_w());
206         const int h(surface->get_h());
207         const float supersample_size(abs(pw/(halftone.size).mag()));
208
209         Surface::pen pen(surface->begin());
210         Point pos;
211         int x,y;
212
213         if(is_solid_color())
214         {
215                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
216                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
217                                 pen.put_value(
218                                         color_func(
219                                                 pos,
220                                                 supersample_size,
221                                                 pen.get_value()
222                                         )
223                                 );
224         }
225         else
226         {
227                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
228                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
229                                 pen.put_value(
230                                         Color::blend(
231                                                 color_func(
232                                                         pos,
233                                                         supersample_size,
234                                                         pen.get_value()
235                                                 ),
236                                                 pen.get_value(),
237                                                 get_amount(),
238                                                 get_blend_method()
239                                         )
240                                 );
241         }
242
243         // Mark our progress as finished
244         if(cb && !cb->amount_complete(10000,10000))
245                 return false;
246
247         return true;
248 }