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