177f6a0d0ddfd90d78f8c21e7ad9b0fdf252ec34
[synfig.git] /
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
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.origin,"origin");
118
119         IMPORT_AS(halftone.origin,"offset");
120
121         return Layer_Composite::set_param(param,value);
122 }
123
124 ValueBase
125 Halftone2::get_param(const String & param)const
126 {
127         EXPORT_AS(halftone.size,"size");
128         EXPORT_AS(halftone.type,"type");
129         EXPORT_AS(halftone.angle,"angle");
130         EXPORT_AS(halftone.origin,"origin");
131
132         EXPORT(color_dark);
133         EXPORT(color_light);
134
135         EXPORT_NAME();
136         EXPORT_VERSION();
137
138         return Layer_Composite::get_param(param);
139 }
140
141 Layer::Vocab
142 Halftone2::get_param_vocab()const
143 {
144         Layer::Vocab ret(Layer_Composite::get_param_vocab());
145
146         ret.push_back(ParamDesc("origin")
147                 .set_local_name(_("Mask Origin"))
148                 .set_is_distance()
149         );
150         ret.push_back(ParamDesc("angle")
151                 .set_local_name(_("Mask Angle"))
152                 .set_origin("origin")
153         );
154         ret.push_back(ParamDesc("size")
155                 .set_local_name(_("Mask Size"))
156                 .set_is_distance()
157                 .set_origin("origin")
158         );
159         ret.push_back(ParamDesc("color_light")
160                 .set_local_name(_("Light Color"))
161         );
162         ret.push_back(ParamDesc("color_dark")
163                 .set_local_name(_("Dark Color"))
164         );
165         ret.push_back(ParamDesc("type")
166                 .set_local_name(_("Type"))
167                 .set_hint("enum")
168                 .add_enum_value(TYPE_SYMMETRIC,"symmetric",_("Symmetric"))
169                 .add_enum_value(TYPE_LIGHTONDARK,"lightondark",_("Light On Dark"))
170                 //.add_enum_value(TYPE_DARKONLIGHT,"darkonlight",_("Dark on Light"))
171                 .add_enum_value(TYPE_DIAMOND,"diamond",_("Diamond"))
172                 .add_enum_value(TYPE_STRIPE,"stripe",_("Stripe"))
173         );
174
175         return ret;
176 }
177
178 Color
179 Halftone2::get_color(Context context, const Point &point)const
180 {
181         const Color undercolor(context.get_color(point));
182         const Color color(color_func(point,0,undercolor));
183
184         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
185                 return color;
186         else
187                 return Color::blend(color,undercolor,get_amount(),get_blend_method());
188 }
189
190 bool
191 Halftone2::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
192 {
193         SuperCallback supercb(cb,0,9500,10000);
194
195         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
196                 return false;
197         if(get_amount()==0)
198                 return true;
199
200         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
201         const Point tl(renddesc.get_tl());
202         const int w(surface->get_w());
203         const int h(surface->get_h());
204         const float supersample_size(abs(pw/(halftone.size).mag()));
205
206         Surface::pen pen(surface->begin());
207         Point pos;
208         int x,y;
209
210         if(is_solid_color())
211         {
212                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
213                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
214                                 pen.put_value(
215                                         color_func(
216                                                 pos,
217                                                 supersample_size,
218                                                 pen.get_value()
219                                         )
220                                 );
221         }
222         else
223         {
224                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
225                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
226                                 pen.put_value(
227                                         Color::blend(
228                                                 color_func(
229                                                         pos,
230                                                         supersample_size,
231                                                         pen.get_value()
232                                                 ),
233                                                 pen.get_value(),
234                                                 get_amount(),
235                                                 get_blend_method()
236                                         )
237                                 );
238         }
239
240         // Mark our progress as finished
241         if(cb && !cb->amount_complete(10000,10000))
242                 return false;
243
244         return true;
245 }