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 / halftone.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file halftone.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 "halftone.h"
33
34 #endif
35
36 /* === M A C R O S ========================================================= */
37
38 using namespace synfig;
39 using namespace std;
40 using namespace etl;
41
42 /* === G L O B A L S ======================================================= */
43
44
45 /* === P R O C E D U R E S ================================================= */
46
47 #define SQRT2   (1.414213562f)
48
49 /* === M E T H O D S ======================================================= */
50
51 float
52 Halftone::operator()(const Point &point, const float& luma, float supersample)const
53 {
54         float halftone(mask(point));
55
56         if(supersample>=0.5f)
57                 supersample=0.4999999999f;
58
59         halftone=(halftone-0.5f)*(1.0f-supersample*2.0f)+0.5f;
60
61         const float diff(halftone-luma);
62
63         if(supersample)
64         {
65                 const float amount(diff/(supersample*2.0f)+0.5f);
66
67                 if(amount<=0.0f+0.01f)
68                         return 1.0f;
69                 else if(amount>=1.0f-0.01f)
70                         return 0.0f;
71                 else
72                         return 1.0f-amount;
73         }
74         else
75         {
76                 if(diff>=0)
77                         return 0.0f;
78                 else
79                         return 1.0f;
80         }
81
82         return 0.0f;
83 }
84
85 float
86 Halftone::mask(synfig::Point point)const
87 {
88         float radius1;
89         float radius2;
90
91         point-=offset;
92
93         {
94                 const float     a(Angle::sin(-angle).get()),    b(Angle::cos(-angle).get());
95                 const float     u(point[0]),v(point[1]);
96
97                 point[0]=b*u-a*v;
98                 point[1]=a*u+b*v;
99         }
100
101         if(type==TYPE_STRIPE)
102         {
103                 Point pnt(fmod(point[0],size[0]),fmod(point[1],size[1]));
104                 while(pnt[0]<0)pnt[0]+=abs(size[0]);
105                 while(pnt[1]<0)pnt[1]+=abs(size[1]);
106
107                 float x(pnt[1]/size[1]);
108                 if(x>0.5)x=1.0-x;
109                 x*=2;
110                 return x;
111         }
112
113         {
114                 Point pnt(fmod(point[0],size[0]),fmod(point[1],size[1]));
115                 while(pnt[0]<0)pnt[0]+=abs(size[0]);
116                 while(pnt[1]<0)pnt[1]+=abs(size[1]);
117                 pnt-=Vector(size[0]*0.5,size[1]*0.5);
118                 pnt*=2.0;
119                 pnt[0]/=size[0];
120                 pnt[1]/=size[1];
121
122                 radius1=pnt.mag()/SQRT2;
123                 radius1*=radius1;
124         }
125         if(type==TYPE_DARKONLIGHT || type== TYPE_LIGHTONDARK)
126                 return radius1;
127
128         {
129                 Point pnt(fmod(point[0]+size[0]*0.5,size[0]),fmod(point[1]+size[0]*0.5,size[1]));
130                 while(pnt[0]<0)pnt[0]+=abs(size[0]);
131                 while(pnt[1]<0)pnt[1]+=abs(size[1]);
132                 pnt-=Vector(size[0]*0.5,size[1]*0.5);
133                 pnt*=2.0;
134                 pnt[0]/=size[0];
135                 pnt[1]/=size[1];
136
137                 radius2=pnt.mag()/SQRT2;
138                 radius2*=radius2;
139         }
140
141         if(type==TYPE_DIAMOND)
142         {
143                 //return (radius1+(1.0f-radius2))*0.5;
144                 float x((radius1+(1.0f-radius2))*0.5);
145                 //float x(((radius2-radius1)*((radius1+(1.0f-radius2))*0.5)+radius1)*2.0f);
146                 x-=0.5;
147                 x*=2.0;
148                 if(x<0)x=-sqrt(-x);else x=sqrt(x);
149                 x*=1.01f;
150                 x/=2.0;
151                 x+=0.5;
152                 return x;
153         }
154
155         if(type==TYPE_SYMMETRIC)
156         {
157                 float x(((radius2-radius1)*((radius1+(1.0f-radius2))*0.5)+radius1)*2.0f);
158                 x-=0.5;
159                 x*=2.0;
160                 if(x<0)x=-sqrt(-x);else x=sqrt(x);
161                 x*=1.01f;
162                 x/=2.0;
163                 x+=0.5;
164                 return x;
165         }
166         return 0;
167 }