Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_06 / src / modules / mod_filter / halftone2.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file halftone2.cpp
3 **      \brief blehh
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "halftone2.h"
33 #include "halftone.h"
34
35 #include <synfig/string.h>
36 #include <synfig/time.h>
37 #include <synfig/context.h>
38 #include <synfig/paramdesc.h>
39 #include <synfig/renddesc.h>
40 #include <synfig/surface.h>
41 #include <synfig/value.h>
42 #include <synfig/valuenode.h>
43
44 #endif
45
46 /* === M A C R O S ========================================================= */
47
48 using namespace synfig;
49 using namespace std;
50 using namespace etl;
51
52 /* === G L O B A L S ======================================================= */
53
54 SYNFIG_LAYER_INIT(Halftone2);
55 SYNFIG_LAYER_SET_NAME(Halftone2,"halftone2");
56 SYNFIG_LAYER_SET_LOCAL_NAME(Halftone2,_("Halftone2"));
57 SYNFIG_LAYER_SET_CATEGORY(Halftone2,_("Filters"));
58 SYNFIG_LAYER_SET_VERSION(Halftone2,"0.0");
59 SYNFIG_LAYER_SET_CVS_ID(Halftone2,"$Id$");
60
61 /* === P R O C E D U R E S ================================================= */
62
63 /* === M E T H O D S ======================================================= */
64
65 Halftone2::Halftone2():
66         color_dark(Color::black()),
67         color_light(Color::white())
68 {
69         halftone.offset=(synfig::Point(0,0));
70         halftone.size=(synfig::Vector(0.25,0.25));
71         halftone.angle=(Angle::zero());
72         halftone.type=TYPE_SYMMETRIC;
73
74         set_blend_method(Color::BLEND_STRAIGHT);
75 }
76
77 inline Color
78 Halftone2::color_func(const Point &point, float supersample,const Color& color)const
79 {
80         const float amount(halftone(point,color.get_y(),supersample));
81         Color halfcolor;
82
83         if(amount<=0.0f)
84                 halfcolor=color_dark;
85         else if(amount>=1.0f)
86                 halfcolor=color_light;
87         else
88                 halfcolor=Color::blend(color_light,color_dark,amount,Color::BLEND_STRAIGHT);
89
90         halfcolor.set_a(color.get_a());
91
92         return halfcolor;
93 }
94
95 inline float
96 Halftone2::calc_supersample(const synfig::Point &x, float pw,float ph)const
97 {
98         return abs(pw/(halftone.size).mag());
99 }
100
101 synfig::Layer::Handle
102 Halftone2::hit_check(synfig::Context context, const synfig::Point &point)const
103 {
104         return const_cast<Halftone2*>(this);
105 }
106
107 bool
108 Halftone2::set_param(const String & param, const ValueBase &value)
109 {
110         IMPORT(color_dark);
111         IMPORT(color_light);
112
113         IMPORT_AS(halftone.size,"size");
114         IMPORT_AS(halftone.type,"type");
115         IMPORT_AS(halftone.angle,"angle");
116         IMPORT_AS(halftone.offset,"offset");
117
118         return Layer_Composite::set_param(param,value);
119 }
120
121 ValueBase
122 Halftone2::get_param(const String & param)const
123 {
124         EXPORT_AS(halftone.size,"size");
125         EXPORT_AS(halftone.type,"type");
126         EXPORT_AS(halftone.angle,"angle");
127         EXPORT_AS(halftone.offset,"offset");
128
129         EXPORT(color_dark);
130         EXPORT(color_light);
131
132         EXPORT_NAME();
133         EXPORT_VERSION();
134
135         return Layer_Composite::get_param(param);
136 }
137
138 Layer::Vocab
139 Halftone2::get_param_vocab()const
140 {
141         Layer::Vocab ret(Layer_Composite::get_param_vocab());
142
143         ret.push_back(ParamDesc("offset")
144                 .set_local_name(_("Mask Offset"))
145                 .set_is_distance()
146         );
147         ret.push_back(ParamDesc("angle")
148                 .set_local_name(_("Mask Angle"))
149                 .set_origin("offset")
150         );
151         ret.push_back(ParamDesc("size")
152                 .set_local_name(_("Mask Size"))
153                 .set_is_distance()
154                 .set_origin("offset")
155         );
156         ret.push_back(ParamDesc("color_light")
157                 .set_local_name(_("Light Color"))
158         );
159         ret.push_back(ParamDesc("color_dark")
160                 .set_local_name(_("Dark Color"))
161         );
162         ret.push_back(ParamDesc("type")
163                 .set_local_name(_("Type"))
164                 .set_hint("enum")
165                 .add_enum_value(TYPE_SYMMETRIC,"symmetric",_("Symmetric"))
166                 .add_enum_value(TYPE_LIGHTONDARK,"lightondark",_("Light On Dark"))
167                 //.add_enum_value(TYPE_DARKONLIGHT,"darkonlight",_("Dark on Light"))
168                 .add_enum_value(TYPE_DIAMOND,"diamond",_("Diamond"))
169                 .add_enum_value(TYPE_STRIPE,"stripe",_("Stripe"))
170         );
171
172         return ret;
173 }
174
175 Color
176 Halftone2::get_color(Context context, const Point &point)const
177 {
178         const Color undercolor(context.get_color(point));
179         const Color color(color_func(point,0,undercolor));
180
181         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
182                 return color;
183         else
184                 return Color::blend(color,undercolor,get_amount(),get_blend_method());
185 }
186
187 bool
188 Halftone2::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
189 {
190         SuperCallback supercb(cb,0,9500,10000);
191
192         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
193                 return false;
194         if(get_amount()==0)
195                 return true;
196
197         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
198         const Point tl(renddesc.get_tl());
199         const int w(surface->get_w());
200         const int h(surface->get_h());
201         const float supersample_size(abs(pw/(halftone.size).mag()));
202
203         Surface::pen pen(surface->begin());
204         Point pos;
205         int x,y;
206
207         if(is_solid_color())
208         {
209                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
210                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
211                                 pen.put_value(
212                                         color_func(
213                                                 pos,
214                                                 supersample_size,
215                                                 pen.get_value()
216                                         )
217                                 );
218         }
219         else
220         {
221                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
222                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
223                                 pen.put_value(
224                                         Color::blend(
225                                                 color_func(
226                                                         pos,
227                                                         supersample_size,
228                                                         pen.get_value()
229                                                 ),
230                                                 pen.get_value(),
231                                                 get_amount(),
232                                                 get_blend_method()
233                                         )
234                                 );
235         }
236
237         // Mark our progress as finished
238         if(cb && !cb->amount_complete(10000,10000))
239                 return false;
240
241         return true;
242 }