Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.08 / 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 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.offset=(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
78 inline Color
79 Halftone2::color_func(const Point &point, float supersample,const Color& color)const
80 {
81         const float amount(halftone(point,color.get_y(),supersample));
82         Color halfcolor;
83
84         if(amount<=0.0f)
85                 halfcolor=color_dark;
86         else if(amount>=1.0f)
87                 halfcolor=color_light;
88         else
89                 halfcolor=Color::blend(color_light,color_dark,amount,Color::BLEND_STRAIGHT);
90
91         halfcolor.set_a(color.get_a());
92
93         return halfcolor;
94 }
95
96 inline float
97 Halftone2::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
98 {
99         return abs(pw/(halftone.size).mag());
100 }
101
102 synfig::Layer::Handle
103 Halftone2::hit_check(synfig::Context /*context*/, const synfig::Point &/*point*/)const
104 {
105         return const_cast<Halftone2*>(this);
106 }
107
108 bool
109 Halftone2::set_param(const String & param, const ValueBase &value)
110 {
111         IMPORT(color_dark);
112         IMPORT(color_light);
113
114         IMPORT_AS(halftone.size,"size");
115         IMPORT_AS(halftone.type,"type");
116         IMPORT_AS(halftone.angle,"angle");
117         IMPORT_AS(halftone.offset,"offset");
118
119         return Layer_Composite::set_param(param,value);
120 }
121
122 ValueBase
123 Halftone2::get_param(const String & param)const
124 {
125         EXPORT_AS(halftone.size,"size");
126         EXPORT_AS(halftone.type,"type");
127         EXPORT_AS(halftone.angle,"angle");
128         EXPORT_AS(halftone.offset,"offset");
129
130         EXPORT(color_dark);
131         EXPORT(color_light);
132
133         EXPORT_NAME();
134         EXPORT_VERSION();
135
136         return Layer_Composite::get_param(param);
137 }
138
139 Layer::Vocab
140 Halftone2::get_param_vocab()const
141 {
142         Layer::Vocab ret(Layer_Composite::get_param_vocab());
143
144         ret.push_back(ParamDesc("offset")
145                 .set_local_name(_("Mask Offset"))
146                 .set_is_distance()
147         );
148         ret.push_back(ParamDesc("angle")
149                 .set_local_name(_("Mask Angle"))
150                 .set_origin("offset")
151         );
152         ret.push_back(ParamDesc("size")
153                 .set_local_name(_("Mask Size"))
154                 .set_is_distance()
155                 .set_origin("offset")
156         );
157         ret.push_back(ParamDesc("color_light")
158                 .set_local_name(_("Light Color"))
159         );
160         ret.push_back(ParamDesc("color_dark")
161                 .set_local_name(_("Dark Color"))
162         );
163         ret.push_back(ParamDesc("type")
164                 .set_local_name(_("Type"))
165                 .set_hint("enum")
166                 .add_enum_value(TYPE_SYMMETRIC,"symmetric",_("Symmetric"))
167                 .add_enum_value(TYPE_LIGHTONDARK,"lightondark",_("Light On Dark"))
168                 //.add_enum_value(TYPE_DARKONLIGHT,"darkonlight",_("Dark on Light"))
169                 .add_enum_value(TYPE_DIAMOND,"diamond",_("Diamond"))
170                 .add_enum_value(TYPE_STRIPE,"stripe",_("Stripe"))
171         );
172
173         return ret;
174 }
175
176 Color
177 Halftone2::get_color(Context context, const Point &point)const
178 {
179         const Color undercolor(context.get_color(point));
180         const Color color(color_func(point,0,undercolor));
181
182         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
183                 return color;
184         else
185                 return Color::blend(color,undercolor,get_amount(),get_blend_method());
186 }
187
188 bool
189 Halftone2::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
190 {
191         SuperCallback supercb(cb,0,9500,10000);
192
193         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
194                 return false;
195         if(get_amount()==0)
196                 return true;
197
198         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
199         const Point tl(renddesc.get_tl());
200         const int w(surface->get_w());
201         const int h(surface->get_h());
202         const float supersample_size(abs(pw/(halftone.size).mag()));
203
204         Surface::pen pen(surface->begin());
205         Point pos;
206         int x,y;
207
208         if(is_solid_color())
209         {
210                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
211                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
212                                 pen.put_value(
213                                         color_func(
214                                                 pos,
215                                                 supersample_size,
216                                                 pen.get_value()
217                                         )
218                                 );
219         }
220         else
221         {
222                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
223                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
224                                 pen.put_value(
225                                         Color::blend(
226                                                 color_func(
227                                                         pos,
228                                                         supersample_size,
229                                                         pen.get_value()
230                                                 ),
231                                                 pen.get_value(),
232                                                 get_amount(),
233                                                 get_blend_method()
234                                         )
235                                 );
236         }
237
238         // Mark our progress as finished
239         if(cb && !cb->amount_complete(10000,10000))
240                 return false;
241
242         return true;
243 }