Huub Schaek's filterlist patch
[swftools.git] / src / swfc-interpolation.c
1 /* swfc- Compiles swf code (.sc) files into .swf files.
2
3    Part of the swftools package.
4
5    Copyright (c) 2007 Huub Schaeks <huub@h-schaeks.speedlinq.nl>
6    Copyright (c) 2007 Matthias Kramm <kramm@quiss.org>
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #include <stdlib.h>
23 #include <math.h>
24 #include <memory.h>
25 #include "swfc-interpolation.h"
26
27 static inline float poly(float fraction, float start, float delta, int degree)
28 {
29         return delta * pow(fraction, degree) + start;
30 }
31
32 float linear(float fraction, float start, float delta)
33 {
34         return poly(fraction, start, delta, 1);
35 }
36
37 float quadIn(float fraction, float start, float delta)
38 {
39         return poly(fraction, start, delta, 2);
40 }
41
42 float quadOut(float fraction, float start, float delta)
43 {
44         return quadIn(1 - fraction, start + delta, -delta);
45 }
46
47 float quadInOut(float fraction, float start, float delta)
48 {
49         if (fraction < 0.5)
50                 return quadIn(2 * fraction, start, delta / 2);
51         return quadOut(2 * fraction - 1, start + delta / 2, delta / 2);
52 }
53
54 float cubicIn(float fraction, float start, float delta)
55 {
56         return poly(fraction, start, delta, 3);
57 }
58
59 float cubicOut(float fraction, float start, float delta)
60 {
61         return cubicIn(1 - fraction, start + delta, -delta);
62 }
63
64 float cubicInOut(float fraction, float start, float delta)
65 {
66         if (fraction < 0.5)
67                 return cubicIn(2 * fraction, start, delta / 2);
68         return cubicOut(2 * fraction - 1, start + delta / 2, delta / 2);
69 }
70
71 float quartIn(float fraction, float start, float delta)
72 {
73         return poly(fraction, start, delta, 4);
74 }
75
76 float quartOut(float fraction, float start, float delta)
77 {
78         return quartIn(1 - fraction, start + delta, -delta);
79 }
80
81 float quartInOut(float fraction, float start, float delta)
82 {
83         if (fraction < 0.5)
84                 return quartIn(2 * fraction, start, delta / 2);
85         return quartOut(2 * fraction - 1, start + delta / 2, delta / 2);
86 }
87
88 float quintIn(float fraction, float start, float delta)
89 {
90         return poly(fraction, start, delta, 5);
91 }
92
93 float quintOut(float fraction, float start, float delta)
94 {
95         return quintIn(1 - fraction, start + delta, -delta);
96 }
97
98 float quintInOut(float fraction, float start, float delta)
99 {
100         if (fraction < 0.5)
101                 return quintIn(2 * fraction, start, delta / 2);
102         return quintOut(2 * fraction - 1, start + delta / 2, delta / 2);
103 }
104
105 float circleIn(float fraction, float start, float delta)
106 {
107         return delta * (1 - sqrt(1 - fraction * fraction)) + start;
108 }
109
110 float circleOut(float fraction, float start, float delta)
111 {
112         return circleIn(1 - fraction, start + delta, -delta);
113 }
114
115 float circleInOut(float fraction, float start, float delta)
116 {
117         if (fraction < 0.5)
118                 return circleIn(2 * fraction, start, delta / 2);
119         return circleOut(2 * fraction - 1, start + delta / 2, delta / 2);
120 }
121
122 float exponentialIn(float fraction, float start, float delta)
123 {
124         if (fraction == 0)
125                 return start;
126         return delta * pow(2, 10 * (fraction - 1)) + start;
127 }
128
129 float exponentialOut(float fraction, float start, float delta)
130 {
131         return exponentialIn(1 - fraction, start + delta, -delta);
132 }
133
134 float exponentialInOut(float fraction, float start, float delta)
135 {
136         if (fraction < 0.5)
137                 return exponentialIn(2 * fraction, start, delta / 2);
138         return exponentialOut(2 * fraction - 1, start + delta / 2, delta / 2);
139 }
140
141 float sineIn(float fraction, float start, float delta)
142 {
143         return delta * (1 - cos(fraction * M_PI/2)) + start;
144 }
145
146 float sineOut(float fraction, float start, float delta)
147 {
148         return sineIn(1 - fraction, start + delta, -delta);
149 }
150
151 float sineInOut(float fraction, float start, float delta)
152 {
153         if (fraction < 0.5)
154                 return sineIn(2 * fraction, start, delta / 2);
155         return sineOut(2 * fraction - 1, start + delta / 2, delta / 2);
156 }
157
158 float elasticIn(float fraction, float start, float delta, float amplitude, int bounces, float damping)
159 {
160         if (fraction == 0 || delta == 0)
161                 return start;
162         if (fraction == 1)
163                 return start + delta;
164         if (amplitude < fabs(delta))
165                 amplitude = delta;
166         float period = 1 / (bounces + 0.25);
167         return amplitude * pow(2, damping * (fraction - 1)) * sin(fraction * (2 * M_PI) / period) + start;
168 }
169
170 float elasticOut(float fraction, float start, float delta, float amplitude, int bounces, float damping)
171 {
172         return elasticIn(1 - fraction, start + delta, -delta, amplitude, bounces, damping);
173 }
174         
175 float elasticInOut(float fraction, float start, float delta, float amplitude, int bounces, float damping)
176 {
177         if (fraction < 0.5)
178                 return elasticIn(2 * fraction, start, delta / 2, amplitude, bounces, damping);
179         return elasticOut(2 * fraction - 1, start + delta / 2, delta / 2, amplitude, bounces, damping);
180 }
181
182 float backIn(float fraction, float start, float delta, float speed)
183 {
184         return delta * fraction * fraction * ((speed + 1) * fraction - speed) + start;
185 }
186
187 float backOut(float fraction, float start, float delta, float speed)
188 {
189         return backIn(1 - fraction, start + delta, -delta, speed);
190 }
191
192 float backInOut(float fraction, float start, float delta, float speed)
193 {
194         if (fraction < 0.5)
195                 return backIn(2 * fraction, start, delta / 2, speed);
196         return backOut(2 * fraction - 1, start + delta / 2, delta / 2, speed);
197 }
198
199 /* when applied to movement bounceIn the object 'hits the floor' bounces times 
200  * (after leaving the floor first) before gently reaching the final position at the top of the final bounce
201  * Each bounce takes growth times a long as the previous, except for the last one which lasts only half 
202  * that time. The heights of the intermediate bounces are determined by the damping parameter.
203  * Set damping to 0 for an undamped movement.*/
204
205 float bounceIn(float fraction, float start, float delta, int bounces, float growth, float damping)
206 {
207         if (fraction == 0 || delta == 0)
208                 return start;
209         if (fraction == 1)
210                 return start + delta;
211         float w0;
212         if (growth == 1.0)
213                 w0 = 1 / (bounces + 0.5);
214         else
215         {
216                 float gN = pow(growth, bounces);
217                 w0 = 1 / ((gN - 1) / (growth - 1) + gN / 2 );
218         }
219         float bounceStart = 0;
220         int i;
221         float w = w0;
222         for (i = 0; i <= bounces; i++)
223         {
224                 float bounceEnd = bounceStart + w;
225                 if (fraction >= bounceStart && fraction < bounceEnd)
226                 {
227                         float half = (bounceEnd + bounceStart) / 2;
228                         float top = delta / pow(2, damping * ((bounces - i)));
229                         fraction -= half;
230                         fraction /= (w / 2);
231                         return (1 - fraction * fraction) * top + start;
232                 }
233                 bounceStart = bounceEnd;
234                 w = w * growth;
235         }
236 }
237
238 /* bounceOut is a time-reversed bounceIn; therefore each bounce takes 1/growth times as long as
239  * the previous, which I think fits the idea when applied to movement */
240
241 float bounceOut(float fraction, float start, float delta, int bounces, float growth, float damping)
242 {
243         return bounceIn(1 - fraction, start + delta, -delta, bounces, growth, damping);
244 }
245
246 /* since bounceIn and bounceOut are combined, if growth > 1 then the bounce-times will increase in
247  * the first half and decrease in the second half */
248
249 float bounceInOut(float fraction, float start, float delta, int bounces, float growth, float damping)
250 {
251         if (fraction < 0.5)
252                 return bounceIn(2 * fraction, start, delta / 2, bounces, growth, damping);
253         return bounceOut(2 * fraction - 1, start + delta / 2, delta / 2, bounces, growth, damping);
254 }
255 /* fastBounce(In/Out) doesn't end or start in a horizontal slope (= gentle end or start) as
256  * bounce(In/Out) do which means fastBounceInOut doesn't have the 'delay' in the middle */
257 float fastBounceIn(float fraction, float start, float delta, int bounces, float growth, float damping)
258 {
259         if (fraction == 0 || delta == 0)
260                 return start;
261         if (fraction == 1)
262                 return start + delta;
263         float w0;
264         if (growth == 1.0)
265                 w0 = 1 / (bounces + 0.25); /* in general (bounces + 1 / (2 * f)) */
266         else
267         {
268                 float gN = pow(growth, bounces);
269                 w0 = 1 / ((gN - 1) / (growth - 1) + gN / 4 /* in general: gN / (2 * f) */ );
270         }
271         float bounceStart = 0;
272         int i;
273         float w = w0;
274         for (i = 0; i <= bounces; i++)
275         {
276                 float bounceEnd = bounceStart + w;
277                 if (fraction >= bounceStart && fraction < bounceEnd)
278                 {
279                         float half = (bounceEnd + bounceStart) / 2;
280                         float top = delta / 0.75/* in general: (1 - (1 / f) * (1 / f)) */ / pow(2, damping * ((bounces - i)));
281                         fraction -= half;
282                         fraction /= (w / 2);
283                         return (1 - fraction * fraction) * top + start;
284                 }
285                 bounceStart = bounceEnd;
286                 w = w * growth;
287         }
288 }
289
290 float fastBounceOut(float fraction, float start, float delta, int bounces, float growth, float damping)
291 {
292         return fastBounceIn(1 - fraction, start + delta, -delta, bounces, growth, damping);
293 }
294
295 float fastBounceInOut(float fraction, float start, float delta, int bounces, float growth, float damping)
296 {
297         if (fraction < 0.5)
298                 return fastBounceIn(2 * fraction, start, delta / 2, bounces, growth, damping);
299         return fastBounceOut(2 * fraction - 1, start + delta / 2, delta / 2, bounces, growth, damping);
300 }