fixed bug in jpeg2000 decoding
[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, float slope, int degree)
28 {
29     return delta * ((1 - slope) * pow(fraction, degree) + slope * fraction) + start;
30 }
31
32 float linear(float fraction, float start, float delta)
33 {
34     return poly(fraction, start, delta, 0, 1);
35 }
36
37 float quadIn(float fraction, float start, float delta, float slope)
38 {
39     return poly(fraction, start, delta, slope, 2);
40 }
41
42 float quadOut(float fraction, float start, float delta, float slope)
43 {
44     return quadIn(1 - fraction, start + delta, -delta, slope);
45 }
46
47 float quadInOut(float fraction, float start, float delta, float slope)
48 {
49     if (fraction < 0.5)
50         return quadIn(2 * fraction, start, delta / 2, slope);
51     return quadOut(2 * fraction - 1, start + delta / 2, delta / 2, slope);
52 }
53
54 float cubicIn(float fraction, float start, float delta, float slope)
55 {
56     return poly(fraction, start, delta, slope, 3);
57 }
58
59 float cubicOut(float fraction, float start, float delta, float slope)
60 {
61     return cubicIn(1 - fraction, start + delta, -delta, slope);
62 }
63
64 float cubicInOut(float fraction, float start, float delta, float slope)
65 {
66     if (fraction < 0.5)
67         return cubicIn(2 * fraction, start, delta / 2, slope);
68     return cubicOut(2 * fraction - 1, start + delta / 2, delta / 2, slope);
69 }
70
71 float quartIn(float fraction, float start, float delta, float slope)
72 {
73     return poly(fraction, start, delta, slope, 4);
74 }
75
76 float quartOut(float fraction, float start, float delta, float slope)
77 {
78     return quartIn(1 - fraction, start + delta, -delta, slope);
79 }
80
81 float quartInOut(float fraction, float start, float delta, float slope)
82 {
83     if (fraction < 0.5)
84         return quartIn(2 * fraction, start, delta / 2, slope);
85     return quartOut(2 * fraction - 1, start + delta / 2, delta / 2, slope);
86 }
87
88 float quintIn(float fraction, float start, float delta, float slope)
89 {
90     return poly(fraction, start, delta, slope, 5);
91 }
92
93 float quintOut(float fraction, float start, float delta, float slope)
94 {
95     return quintIn(1 - fraction, start + delta, -delta, slope);
96 }
97
98 float quintInOut(float fraction, float start, float delta, float slope)
99 {
100     if (fraction < 0.5)
101         return quintIn(2 * fraction, start, delta / 2, slope);
102     return quintOut(2 * fraction - 1, start + delta / 2, delta / 2, slope);
103 }
104
105 float circleIn(float fraction, float start, float delta, float slope)
106 {
107     return delta * (1 - sqrt(1 - (1 - 2 * slope) * fraction * fraction - 2 * slope * fraction)) + start;
108 }
109
110 float circleOut(float fraction, float start, float delta, float slope)
111 {
112     return circleIn(1 - fraction, start + delta, -delta, slope);
113 }
114
115 float circleInOut(float fraction, float start, float delta, float slope)
116 {
117     if (fraction < 0.5)
118         return circleIn(2 * fraction, start, delta / 2, slope);
119     return circleOut(2 * fraction - 1, start + delta / 2, delta / 2, slope);
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     return w;
237 }
238
239 /* bounceOut is a time-reversed bounceIn; therefore each bounce takes 1/growth times as long as
240  * the previous, which I think fits the idea when applied to movement */
241
242 float bounceOut(float fraction, float start, float delta, int bounces, float growth, float damping)
243 {
244     return bounceIn(1 - fraction, start + delta, -delta, bounces, growth, damping);
245 }
246
247 /* since bounceIn and bounceOut are combined, if growth > 1 then the bounce-times will increase in
248  * the first half and decrease in the second half */
249
250 float bounceInOut(float fraction, float start, float delta, int bounces, float growth, float damping)
251 {
252     if (fraction < 0.5)
253         return bounceIn(2 * fraction, start, delta / 2, bounces, growth, damping);
254     return bounceOut(2 * fraction - 1, start + delta / 2, delta / 2, bounces, growth, damping);
255 }
256 /* fastBounce(In/Out) doesn't end or start in a horizontal slope (= gentle end or start) as
257  * bounce(In/Out) do which means fastBounceInOut doesn't have the 'delay' in the middle */
258 float fastBounceIn(float fraction, float start, float delta, int bounces, float growth, float damping)
259 {
260     if (fraction == 0 || delta == 0)
261         return start;
262     if (fraction == 1)
263         return start + delta;
264     float w0;
265     if (growth == 1.0)
266         w0 = 1 / (bounces + 0.25); /* in general (bounces + 1 / (2 * f)) */
267     else
268     {
269         float gN = pow(growth, bounces);
270         w0 = 1 / ((gN - 1) / (growth - 1) + gN / 4 /* in general: gN / (2 * f) */ );
271     }
272     float bounceStart = 0;
273     int i;
274     float w = w0;
275     for (i = 0; i <= bounces; i++)
276     {
277         float bounceEnd = bounceStart + w;
278         if (fraction >= bounceStart && fraction < bounceEnd)
279         {
280             float half = (bounceEnd + bounceStart) / 2;
281             float top = delta / 0.75/* in general: (1 - (1 / f) * (1 / f)) */ / pow(2, damping * ((bounces - i)));
282             fraction -= half;
283             fraction /= (w / 2);
284             return (1 - fraction * fraction) * top + start;
285         }
286         bounceStart = bounceEnd;
287         w = w * growth;
288     }
289     return 0;
290 }
291
292 float fastBounceOut(float fraction, float start, float delta, int bounces, float growth, float damping)
293 {
294     return fastBounceIn(1 - fraction, start + delta, -delta, bounces, growth, damping);
295 }
296
297 float fastBounceInOut(float fraction, float start, float delta, int bounces, float growth, float damping)
298 {
299     if (fraction < 0.5)
300         return fastBounceIn(2 * fraction, start, delta / 2, bounces, growth, damping);
301     return fastBounceOut(2 * fraction - 1, start + delta / 2, delta / 2, bounces, growth, damping);
302 }