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