Style cleanup: white space
[blender.git] / source / blender / blenkernel / intern / seqeffects.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): 
22  * - Blender Foundation, 2003-2009
23  * - Peter Schlaile <peter [at] schlaile [dot] de> 2005/2006
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/seqeffects.c
29  *  \ingroup bke
30  */
31
32
33 #include <string.h>
34 #include <math.h>
35 #include <stdlib.h>
36
37 #include "MEM_guardedalloc.h"
38 #include "BLI_dynlib.h"
39
40 #include "BLI_math.h" /* windows needs for M_PI */
41 #include "BLI_utildefines.h"
42 #include "BLI_string.h"
43
44 #include "DNA_scene_types.h"
45 #include "DNA_sequence_types.h"
46 #include "DNA_anim_types.h"
47
48 #include "BKE_fcurve.h"
49 #include "BKE_main.h"
50 #include "BKE_sequencer.h"
51 #include "BKE_texture.h"
52 #include "BKE_utildefines.h"
53
54 #include "IMB_imbuf_types.h"
55 #include "IMB_imbuf.h"
56
57 #include "RNA_access.h"
58
59 /* **** XXX **** */
60
61 /*********************** Glow effect *************************/
62
63 enum {
64         GlowR = 0,
65         GlowG = 1,
66         GlowB = 2,
67         GlowA = 3
68 };
69
70 static ImBuf *prepare_effect_imbufs(SeqRenderData context, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
71 {
72         ImBuf *out;
73         int x = context.rectx;
74         int y = context.recty;
75
76         if (!ibuf1 && !ibuf2 && !ibuf3) {
77                 /* hmmm, global float option ? */
78                 out = IMB_allocImBuf(x, y, 32, IB_rect);
79         }
80         else if ((ibuf1 && ibuf1->rect_float) ||
81                  (ibuf2 && ibuf2->rect_float) ||
82                  (ibuf3 && ibuf3->rect_float))
83         {
84                 /* if any inputs are rectfloat, output is float too */
85
86                 out = IMB_allocImBuf(x, y, 32, IB_rectfloat);
87         }
88         else {
89                 out = IMB_allocImBuf(x, y, 32, IB_rect);
90         }
91         
92         if (ibuf1 && !ibuf1->rect_float && out->rect_float) {
93                 IMB_float_from_rect_simple(ibuf1);
94         }
95         if (ibuf2 && !ibuf2->rect_float && out->rect_float) {
96                 IMB_float_from_rect_simple(ibuf2);
97         }
98         if (ibuf3 && !ibuf3->rect_float && out->rect_float) {
99                 IMB_float_from_rect_simple(ibuf3);
100         }
101         
102         if (ibuf1 && !ibuf1->rect && !out->rect_float) {
103                 IMB_rect_from_float(ibuf1);
104         }
105         if (ibuf2 && !ibuf2->rect && !out->rect_float) {
106                 IMB_rect_from_float(ibuf2);
107         }
108         if (ibuf3 && !ibuf3->rect && !out->rect_float) {
109                 IMB_rect_from_float(ibuf3);
110         }
111                         
112         return out;
113 }
114
115 /*********************** Alpha Over *************************/
116
117 static void init_alpha_over_or_under(Sequence *seq)
118 {
119         Sequence *seq1 = seq->seq1;
120         Sequence *seq2 = seq->seq2;
121
122         seq->seq2 = seq1;
123         seq->seq1 = seq2;
124 }
125
126 static void do_alphaover_effect_byte(float facf0, float facf1, int x, int y,  char *rect1, char *rect2, char *out)
127 {
128         int fac2, mfac, fac, fac4;
129         int xo, tempc;
130         char *rt1, *rt2, *rt;
131
132         xo = x;
133         rt1 = (char *) rect1;
134         rt2 = (char *) rect2;
135         rt = (char *) out;
136
137         fac2 = (int) (256.0f * facf0);
138         fac4 = (int) (256.0f * facf1);
139
140         while (y--) {
141                 x = xo;
142                 while (x--) {
143
144                         /* rt = rt1 over rt2  (alpha from rt1) */
145
146                         fac = fac2;
147                         mfac = 256 - ( (fac2 * rt1[3]) >> 8);
148
149                         if (fac == 0) *( (unsigned int *) rt) = *( (unsigned int *) rt2);
150                         else if (mfac == 0) *( (unsigned int *) rt) = *( (unsigned int *) rt1);
151                         else {
152                                 tempc = (fac * rt1[0] + mfac * rt2[0]) >> 8;
153                                 if (tempc > 255) rt[0] = 255; else rt[0] = tempc;
154                                 tempc = (fac * rt1[1] + mfac * rt2[1]) >> 8;
155                                 if (tempc > 255) rt[1] = 255; else rt[1] = tempc;
156                                 tempc = (fac * rt1[2] + mfac * rt2[2]) >> 8;
157                                 if (tempc > 255) rt[2] = 255; else rt[2] = tempc;
158                                 tempc = (fac * rt1[3] + mfac * rt2[3]) >> 8;
159                                 if (tempc > 255) rt[3] = 255; else rt[3] = tempc;
160                         }
161                         rt1 += 4; rt2 += 4; rt += 4;
162                 }
163
164                 if (y == 0) break;
165                 y--;
166
167                 x = xo;
168                 while (x--) {
169                         fac = fac4;
170                         mfac = 256 - ( (fac4 * rt1[3]) >> 8);
171
172                         if (fac == 0) *( (unsigned int *) rt) = *( (unsigned int *) rt2);
173                         else if (mfac == 0) *( (unsigned int *) rt) = *( (unsigned int *) rt1);
174                         else {
175                                 tempc = (fac * rt1[0] + mfac * rt2[0]) >> 8;
176                                 if (tempc > 255) rt[0] = 255; else rt[0] = tempc;
177                                 tempc = (fac * rt1[1] + mfac * rt2[1]) >> 8;
178                                 if (tempc > 255) rt[1] = 255; else rt[1] = tempc;
179                                 tempc = (fac * rt1[2] + mfac * rt2[2]) >> 8;
180                                 if (tempc > 255) rt[2] = 255; else rt[2] = tempc;
181                                 tempc = (fac * rt1[3] + mfac * rt2[3]) >> 8;
182                                 if (tempc > 255) rt[3] = 255; else rt[3] = tempc;
183                         }
184                         rt1 += 4; rt2 += 4; rt += 4;
185                 }
186         }
187 }
188
189 static void do_alphaover_effect_float(float facf0, float facf1, int x, int y,  float *rect1, float *rect2, float *out)
190 {
191         float fac2, mfac, fac, fac4;
192         int xo;
193         float *rt1, *rt2, *rt;
194
195         xo = x;
196         rt1 = rect1;
197         rt2 = rect2;
198         rt = out;
199
200         fac2 = facf0;
201         fac4 = facf1;
202
203         while (y--) {
204                 x = xo;
205                 while (x--) {
206                         /* rt = rt1 over rt2  (alpha from rt1) */
207
208                         fac = fac2;
209                         mfac = 1.0f - (fac2 * rt1[3]);
210
211                         if (fac <= 0.0f) {
212                                 memcpy(rt, rt2, 4 * sizeof(float));
213                         }
214                         else if (mfac <= 0) {
215                                 memcpy(rt, rt1, 4 * sizeof(float));
216                         }
217                         else {
218                                 rt[0] = fac * rt1[0] + mfac * rt2[0];
219                                 rt[1] = fac * rt1[1] + mfac * rt2[1];
220                                 rt[2] = fac * rt1[2] + mfac * rt2[2];
221                                 rt[3] = fac * rt1[3] + mfac * rt2[3];
222                         }
223                         rt1 += 4; rt2 += 4; rt += 4;
224                 }
225
226                 if (y == 0)
227                         break;
228                 y--;
229
230                 x = xo;
231                 while (x--) {
232                         fac = fac4;
233                         mfac = 1.0f - (fac4 * rt1[3]);
234
235                         if (fac <= 0.0f) {
236                                 memcpy(rt, rt2, 4 * sizeof(float));
237                         }
238                         else if (mfac <= 0.0f) {
239                                 memcpy(rt, rt1, 4 * sizeof(float));
240                         }
241                         else {
242                                 rt[0] = fac * rt1[0] + mfac * rt2[0];
243                                 rt[1] = fac * rt1[1] + mfac * rt2[1];
244                                 rt[2] = fac * rt1[2] + mfac * rt2[2];
245                                 rt[3] = fac * rt1[3] + mfac * rt2[3];
246                         }
247                         rt1 += 4; rt2 += 4; rt += 4;
248                 }
249         }
250 }
251
252 static ImBuf *do_alphaover_effect(SeqRenderData context, Sequence *UNUSED(seq), float UNUSED(cfra), float facf0,
253                                                                   float facf1, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
254 {
255         struct ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
256
257         if (out->rect_float) {
258                 do_alphaover_effect_float(
259                         facf0, facf1, context.rectx, context.recty,
260                         ibuf1->rect_float, ibuf2->rect_float,
261                         out->rect_float);
262         }
263         else {
264                 do_alphaover_effect_byte(
265                         facf0, facf1, context.rectx, context.recty,
266                         (char *) ibuf1->rect, (char *) ibuf2->rect,
267                         (char *) out->rect);
268         }
269
270         return out;
271 }
272
273 /*********************** Alpha Under *************************/
274
275 static void do_alphaunder_effect_byte(float facf0, float facf1, int x, int y, char *rect1, char *rect2, char *out)
276 {
277         int fac2, mfac, fac, fac4;
278         int xo;
279         char *rt1, *rt2, *rt;
280
281         xo = x;
282         rt1 = rect1;
283         rt2 = rect2;
284         rt = out;
285
286         fac2 = (int)(256.0f * facf0);
287         fac4 = (int)(256.0f * facf1);
288
289         while (y--) {
290                 x = xo;
291                 while (x--) {
292                         /* rt = rt1 under rt2  (alpha from rt2) */
293
294                         /* this complex optimization is because the
295                          * 'skybuf' can be crossed in
296                          */
297                         if (rt2[3] == 0 && fac2 == 256) *( (unsigned int *) rt) = *( (unsigned int *) rt1);
298                         else if (rt2[3] == 255) *( (unsigned int *) rt) = *( (unsigned int *) rt2);
299                         else {
300                                 mfac = rt2[3];
301                                 fac = (fac2 * (256 - mfac)) >> 8;
302
303                                 if (fac == 0) *( (unsigned int *) rt) = *( (unsigned int *) rt2);
304                                 else {
305                                         rt[0] = (fac * rt1[0] + mfac * rt2[0]) >> 8;
306                                         rt[1] = (fac * rt1[1] + mfac * rt2[1]) >> 8;
307                                         rt[2] = (fac * rt1[2] + mfac * rt2[2]) >> 8;
308                                         rt[3] = (fac * rt1[3] + mfac * rt2[3]) >> 8;
309                                 }
310                         }
311                         rt1 += 4; rt2 += 4; rt += 4;
312                 }
313
314                 if (y == 0)
315                         break;
316                 y--;
317
318                 x = xo;
319                 while (x--) {
320                         if (rt2[3] == 0 && fac4 == 256) *( (unsigned int *) rt) = *( (unsigned int *) rt1);
321                         else if (rt2[3] == 255) *( (unsigned int *) rt) = *( (unsigned int *) rt2);
322                         else {
323                                 mfac = rt2[3];
324                                 fac = (fac4 * (256 - mfac)) >> 8;
325
326                                 if (fac == 0) *( (unsigned int *)rt) = *( (unsigned int *)rt2);
327                                 else {
328                                         rt[0] = (fac * rt1[0] + mfac * rt2[0]) >> 8;
329                                         rt[1] = (fac * rt1[1] + mfac * rt2[1]) >> 8;
330                                         rt[2] = (fac * rt1[2] + mfac * rt2[2]) >> 8;
331                                         rt[3] = (fac * rt1[3] + mfac * rt2[3]) >> 8;
332                                 }
333                         }
334                         rt1 += 4; rt2 += 4; rt += 4;
335                 }
336         }
337 }
338
339 static void do_alphaunder_effect_float(float facf0, float facf1, int x, int y,  float *rect1, float *rect2, float *out)
340 {
341         float fac2, mfac, fac, fac4;
342         int xo;
343         float *rt1, *rt2, *rt;
344
345         xo = x;
346         rt1 = rect1;
347         rt2 = rect2;
348         rt = out;
349
350         fac2 = facf0;
351         fac4 = facf1;
352
353         while (y--) {
354                 x = xo;
355                 while (x--) {
356                         /* rt = rt1 under rt2  (alpha from rt2) */
357
358                         /* this complex optimization is because the
359                          * 'skybuf' can be crossed in
360                          */
361                         if (rt2[3] <= 0 && fac2 >= 1.0f) {
362                                 memcpy(rt, rt1, 4 * sizeof(float));
363                         }
364                         else if (rt2[3] >= 1.0f) {
365                                 memcpy(rt, rt2, 4 * sizeof(float));
366                         }
367                         else {
368                                 mfac = rt2[3];
369                                 fac = fac2 * (1.0f - mfac);
370
371                                 if (fac == 0) {
372                                         memcpy(rt, rt2, 4 * sizeof(float));
373                                 }
374                                 else {
375                                         rt[0] = fac * rt1[0] + mfac * rt2[0];
376                                         rt[1] = fac * rt1[1] + mfac * rt2[1];
377                                         rt[2] = fac * rt1[2] + mfac * rt2[2];
378                                         rt[3] = fac * rt1[3] + mfac * rt2[3];
379                                 }
380                         }
381                         rt1 += 4; rt2 += 4; rt += 4;
382                 }
383
384                 if (y == 0)
385                         break;
386                 y--;
387
388                 x = xo;
389                 while (x--) {
390                         if (rt2[3] <= 0 && fac4 >= 1.0f) {
391                                 memcpy(rt, rt1, 4 * sizeof(float));
392                         }
393                         else if (rt2[3] >= 1.0f) {
394                                 memcpy(rt, rt2, 4 * sizeof(float));
395                         }
396                         else {
397                                 mfac = rt2[3];
398                                 fac = fac4 * (1.0f - mfac);
399
400                                 if (fac == 0) {
401                                         memcpy(rt, rt2, 4 * sizeof(float));
402                                 }
403                                 else {
404                                         rt[0] = fac * rt1[0] + mfac * rt2[0];
405                                         rt[1] = fac * rt1[1] + mfac * rt2[1];
406                                         rt[2] = fac * rt1[2] + mfac * rt2[2];
407                                         rt[3] = fac * rt1[3] + mfac * rt2[3];
408                                 }
409                         }
410                         rt1 += 4; rt2 += 4; rt += 4;
411                 }
412         }
413 }
414
415 static ImBuf *do_alphaunder_effect(SeqRenderData context, Sequence *UNUSED(seq), float UNUSED(cfra),
416                                    float facf0, float facf1, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
417 {
418         struct ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
419
420         if (out->rect_float) {
421                 do_alphaunder_effect_float(facf0, facf1, context.rectx, context.recty, ibuf1->rect_float,
422                                            ibuf2->rect_float, out->rect_float);
423         }
424         else {
425                 do_alphaunder_effect_byte(facf0, facf1, context.rectx, context.recty,
426                                           (char *) ibuf1->rect, (char *) ibuf2->rect, (char *) out->rect);
427         }
428         return out;
429 }
430
431 /*********************** Cross *************************/
432
433 static void do_cross_effect_byte(float facf0, float facf1, int x, int y, char *rect1, char *rect2, char *out)
434 {
435         int fac1, fac2, fac3, fac4;
436         int xo;
437         char *rt1, *rt2, *rt;
438
439         xo = x;
440         rt1 = rect1;
441         rt2 = rect2;
442         rt = out;
443
444         fac2 = (int) (256.0f * facf0);
445         fac1 = 256 - fac2;
446         fac4 = (int) (256.0f * facf1);
447         fac3 = 256 - fac4;
448
449         while (y--) {
450                 x = xo;
451                 while (x--) {
452                         rt[0] = (fac1 * rt1[0] + fac2 * rt2[0]) >> 8;
453                         rt[1] = (fac1 * rt1[1] + fac2 * rt2[1]) >> 8;
454                         rt[2] = (fac1 * rt1[2] + fac2 * rt2[2]) >> 8;
455                         rt[3] = (fac1 * rt1[3] + fac2 * rt2[3]) >> 8;
456
457                         rt1 += 4; rt2 += 4; rt += 4;
458                 }
459
460                 if (y == 0)
461                         break;
462                 y--;
463
464                 x = xo;
465                 while (x--) {
466                         rt[0] = (fac3 * rt1[0] + fac4 * rt2[0]) >> 8;
467                         rt[1] = (fac3 * rt1[1] + fac4 * rt2[1]) >> 8;
468                         rt[2] = (fac3 * rt1[2] + fac4 * rt2[2]) >> 8;
469                         rt[3] = (fac3 * rt1[3] + fac4 * rt2[3]) >> 8;
470
471                         rt1 += 4; rt2 += 4; rt += 4;
472                 }
473
474         }
475 }
476
477 static void do_cross_effect_float(float facf0, float facf1, int x, int y, float *rect1, float *rect2, float *out)
478 {
479         float fac1, fac2, fac3, fac4;
480         int xo;
481         float *rt1, *rt2, *rt;
482
483         xo = x;
484         rt1 = rect1;
485         rt2 = rect2;
486         rt = out;
487
488         fac2 = facf0;
489         fac1 = 1.0f - fac2;
490         fac4 = facf1;
491         fac3 = 1.0f - fac4;
492
493         while (y--) {
494                 x = xo;
495                 while (x--) {
496                         rt[0] = fac1 * rt1[0] + fac2 * rt2[0];
497                         rt[1] = fac1 * rt1[1] + fac2 * rt2[1];
498                         rt[2] = fac1 * rt1[2] + fac2 * rt2[2];
499                         rt[3] = fac1 * rt1[3] + fac2 * rt2[3];
500
501                         rt1 += 4; rt2 += 4; rt += 4;
502                 }
503
504                 if (y == 0)
505                         break;
506                 y--;
507
508                 x = xo;
509                 while (x--) {
510                         rt[0] = fac3 * rt1[0] + fac4 * rt2[0];
511                         rt[1] = fac3 * rt1[1] + fac4 * rt2[1];
512                         rt[2] = fac3 * rt1[2] + fac4 * rt2[2];
513                         rt[3] = fac3 * rt1[3] + fac4 * rt2[3];
514
515                         rt1 += 4; rt2 += 4; rt += 4;
516                 }
517
518         }
519 }
520
521 /* careful: also used by speed effect! */
522
523 static ImBuf *do_cross_effect(SeqRenderData context, Sequence *UNUSED(seq), float UNUSED(cfra),
524                               float facf0, float facf1, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
525 {
526         ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
527
528         if (out->rect_float) {
529                 do_cross_effect_float(facf0, facf1, context.rectx, context.recty,
530                                       ibuf1->rect_float, ibuf2->rect_float, out->rect_float);
531         }
532         else {
533                 do_cross_effect_byte(facf0, facf1, context.rectx, context.recty,
534                                      (char *) ibuf1->rect, (char *) ibuf2->rect, (char *) out->rect);
535         }
536         return out;
537 }
538
539 /*********************** Gamma Cross *************************/
540
541 /* copied code from initrender.c */
542 static unsigned short gamtab[65536];
543 static unsigned short igamtab1[256];
544 static int gamma_tabs_init = FALSE;
545
546 #define RE_GAMMA_TABLE_SIZE 400
547
548 static float gamma_range_table[RE_GAMMA_TABLE_SIZE + 1];
549 static float gamfactor_table[RE_GAMMA_TABLE_SIZE];
550 static float inv_gamma_range_table[RE_GAMMA_TABLE_SIZE + 1];
551 static float inv_gamfactor_table[RE_GAMMA_TABLE_SIZE];
552 static float color_domain_table[RE_GAMMA_TABLE_SIZE + 1]; 
553 static float color_step;
554 static float inv_color_step;
555 static float valid_gamma;
556 static float valid_inv_gamma;
557
558 static void makeGammaTables(float gamma)
559 {
560         /* we need two tables: one forward, one backward */
561         int i;
562
563         valid_gamma       = gamma;
564         valid_inv_gamma   = 1.0f / gamma;
565         color_step        = 1.0f / RE_GAMMA_TABLE_SIZE;
566         inv_color_step    = (float) RE_GAMMA_TABLE_SIZE; 
567
568         /* We could squeeze out the two range tables to gain some memory */     
569         for (i = 0; i < RE_GAMMA_TABLE_SIZE; i++) {
570                 color_domain_table[i]    = i * color_step;
571                 gamma_range_table[i]     = pow(color_domain_table[i], valid_gamma);
572                 inv_gamma_range_table[i] = pow(color_domain_table[i], valid_inv_gamma);
573         }
574
575         /* The end of the table should match 1.0 carefully. In order to avoid
576          * rounding errors, we just set this explicitly. The last segment may
577         * have a different length than the other segments, but our
578         * interpolation is insensitive to that
579         */
580         color_domain_table[RE_GAMMA_TABLE_SIZE]    = 1.0;
581         gamma_range_table[RE_GAMMA_TABLE_SIZE]     = 1.0;
582         inv_gamma_range_table[RE_GAMMA_TABLE_SIZE] = 1.0;
583
584         /* To speed up calculations, we make these calc factor tables. They are
585          * multiplication factors used in scaling the interpolation
586          */
587         for (i = 0; i < RE_GAMMA_TABLE_SIZE; i++) {
588                 gamfactor_table[i] = inv_color_step * (gamma_range_table[i + 1] - gamma_range_table[i]);
589                 inv_gamfactor_table[i] = inv_color_step * (inv_gamma_range_table[i + 1] - inv_gamma_range_table[i]);
590         }
591
592 }
593
594 static float gammaCorrect(float c)
595 {
596         int i;
597         float res = 0.0;
598         
599         i = floor(c * inv_color_step);
600         /* Clip to range [0, 1]: outside, just do the complete calculation.
601          * We may have some performance problems here. Stretching up the LUT
602          * may help solve that, by exchanging LUT size for the interpolation.
603          * Negative colors are explicitly handled.
604          */
605         if (i < 0) res = -pow(abs(c), valid_gamma);
606         else if (i >= RE_GAMMA_TABLE_SIZE) res = pow(c, valid_gamma);
607         else res = gamma_range_table[i] + ( (c - color_domain_table[i]) * gamfactor_table[i]);
608         
609         return res;
610 }
611
612 /* ------------------------------------------------------------------------- */
613
614 static float invGammaCorrect(float col)
615 {
616         int i;
617         float res = 0.0;
618
619         i = floor(col * inv_color_step);
620         /* Negative colors are explicitly handled */
621         if (i < 0) res = -pow(abs(col), valid_inv_gamma);
622         else if (i >= RE_GAMMA_TABLE_SIZE) res = pow(col, valid_inv_gamma);
623         else res = inv_gamma_range_table[i] +  ( (col - color_domain_table[i]) * inv_gamfactor_table[i]);
624  
625         return res;
626 }
627
628 static void gamtabs(float gamma)
629 {
630         float val, igamma = 1.0f / gamma;
631         int a;
632         
633         /* gamtab: in short, out short */
634         for (a = 0; a < 65536; a++) {
635                 val = a;
636                 val /= 65535.0f;
637                 
638                 if (gamma == 2.0f) val = sqrt(val);
639                 else if (gamma != 1.0f) val = pow(val, igamma);
640                 
641                 gamtab[a] = (65535.99f * val);
642         }
643         /* inverse gamtab1 : in byte, out short */
644         for (a = 1; a <= 256; a++) {
645                 if (gamma == 2.0f) igamtab1[a - 1] = a * a - 1;
646                 else if (gamma == 1.0f) igamtab1[a - 1] = 256 * a - 1;
647                 else {
648                         val = a / 256.0f;
649                         igamtab1[a - 1] = (65535.0 * pow(val, gamma)) - 1;
650                 }
651         }
652 }
653
654 static void build_gammatabs(void)
655 {
656         if (gamma_tabs_init == FALSE) {
657                 gamtabs(2.0f);
658                 makeGammaTables(2.0f);
659                 gamma_tabs_init = TRUE;
660         }
661 }
662
663 static void init_gammacross(Sequence *UNUSED(seq))
664 {
665 }
666
667 static void load_gammacross(Sequence *UNUSED(seq))
668 {
669 }
670
671 static void free_gammacross(Sequence *UNUSED(seq))
672 {
673 }
674
675 static void do_gammacross_effect_byte(float facf0, float UNUSED(facf1),  int x, int y, unsigned char *rect1,
676                                       unsigned char *rect2, unsigned char *out)
677 {
678         int fac1, fac2, col;
679         int xo;
680         unsigned char *rt1, *rt2, *rt;
681         
682         xo = x;
683         rt1 = (unsigned char *) rect1;
684         rt2 = (unsigned char *) rect2;
685         rt = (unsigned char *) out;
686
687         fac2 = (int)(256.0f * facf0);
688         fac1 = 256 - fac2;
689
690         while (y--) {
691                 x = xo;
692                 while (x--) {
693                         col = (fac1 * igamtab1[rt1[0]] + fac2 * igamtab1[rt2[0]]) >> 8;
694                         if (col > 65535) rt[0] = 255; else rt[0] = ( (char *)(gamtab + col))[MOST_SIG_BYTE];
695                         col = (fac1 * igamtab1[rt1[1]] + fac2 * igamtab1[rt2[1]]) >> 8;
696                         if (col > 65535) rt[1] = 255; else rt[1] = ( (char *)(gamtab + col))[MOST_SIG_BYTE];
697                         col = (fac1 * igamtab1[rt1[2]] + fac2 * igamtab1[rt2[2]]) >> 8;
698                         if (col > 65535) rt[2] = 255; else rt[2] = ( (char *)(gamtab + col))[MOST_SIG_BYTE];
699                         col = (fac1 * igamtab1[rt1[3]] + fac2 * igamtab1[rt2[3]]) >> 8;
700                         if (col > 65535) rt[3] = 255; else rt[3] = ( (char *)(gamtab + col))[MOST_SIG_BYTE];
701
702                         rt1 += 4; rt2 += 4; rt += 4;
703                 }
704
705                 if (y == 0)
706                         break;
707                 y--;
708
709                 x = xo;
710                 while (x--) {
711                         col = (fac1 * igamtab1[rt1[0]] + fac2 * igamtab1[rt2[0]]) >> 8;
712                         if (col > 65535) rt[0] = 255; else rt[0] = ( (char *)(gamtab + col))[MOST_SIG_BYTE];
713                         col = (fac1 * igamtab1[rt1[1]] + fac2 * igamtab1[rt2[1]]) >> 8;
714                         if (col > 65535) rt[1] = 255; else rt[1] = ( (char *)(gamtab + col))[MOST_SIG_BYTE];
715                         col = (fac1 * igamtab1[rt1[2]] + fac2 * igamtab1[rt2[2]]) >> 8;
716                         if (col > 65535) rt[2] = 255; else rt[2] = ( (char *)(gamtab + col))[MOST_SIG_BYTE];
717                         col = (fac1 * igamtab1[rt1[3]] + fac2 * igamtab1[rt2[3]]) >> 8;
718                         if (col > 65535) rt[3] = 255; else rt[3] = ( (char *)(gamtab + col))[MOST_SIG_BYTE];
719
720                         rt1 += 4; rt2 += 4; rt += 4;
721                 }
722         }
723 }
724
725 static void do_gammacross_effect_float(float facf0, float UNUSED(facf1), int x, int y, float *rect1,
726                                        float *rect2, float *out)
727 {
728         float fac1, fac2;
729         int xo;
730         float *rt1, *rt2, *rt;
731
732         xo = x;
733         rt1 = rect1;
734         rt2 = rect2;
735         rt = out;
736
737         fac2 = facf0;
738         fac1 = 1.0f - fac2;
739
740         while (y--) {
741                 x = xo * 4;
742                 while (x--) {
743                         *rt = gammaCorrect(fac1 * invGammaCorrect(*rt1) + fac2 * invGammaCorrect(*rt2));
744                         rt1++; rt2++; rt++;
745                 }
746
747                 if (y == 0)
748                         break;
749                 y--;
750
751                 x = xo * 4;
752                 while (x--) {
753                         *rt = gammaCorrect(fac1 * invGammaCorrect(*rt1) + fac2 * invGammaCorrect(*rt2));
754
755                         rt1++; rt2++; rt++;
756                 }
757         }
758 }
759
760 static ImBuf *do_gammacross_effect(SeqRenderData context, Sequence *UNUSED(seq), float UNUSED(cfra),
761                                    float facf0, float facf1, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
762 {
763         struct ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
764
765         build_gammatabs();
766
767         if (out->rect_float) {
768                 do_gammacross_effect_float(facf0, facf1, context.rectx, context.recty, ibuf1->rect_float,
769                                            ibuf2->rect_float, out->rect_float);
770         }
771         else {
772                 do_gammacross_effect_byte(facf0, facf1, context.rectx, context.recty,
773                                           (unsigned char *) ibuf1->rect, (unsigned char *) ibuf2->rect,
774                                           (unsigned char *) out->rect);
775         }
776
777         return out;
778 }
779
780 /*********************** Add *************************/
781
782 static void do_add_effect_byte(float facf0, float facf1, int x, int y, unsigned char *rect1, unsigned char *rect2,
783                                unsigned char *out)
784 {
785         int col, xo, fac1, fac3;
786         char *rt1, *rt2, *rt;
787
788         xo = x;
789         rt1 = (char *)rect1;
790         rt2 = (char *)rect2;
791         rt = (char *)out;
792
793         fac1 = (int)(256.0f * facf0);
794         fac3 = (int)(256.0f * facf1);
795
796         while (y--) {
797                 x = xo;
798
799                 while (x--) {
800                         col = rt1[0] + ((fac1 * rt2[0]) >> 8);
801                         if (col > 255) rt[0] = 255; else rt[0] = col;
802                         col = rt1[1] + ((fac1 * rt2[1]) >> 8);
803                         if (col > 255) rt[1] = 255; else rt[1] = col;
804                         col = rt1[2] + ((fac1 * rt2[2]) >> 8);
805                         if (col > 255) rt[2] = 255; else rt[2] = col;
806                         col = rt1[3] + ((fac1 * rt2[3]) >> 8);
807                         if (col > 255) rt[3] = 255; else rt[3] = col;
808
809                         rt1 += 4; rt2 += 4; rt += 4;
810                 }
811
812                 if (y == 0)
813                         break;
814                 y--;
815
816                 x = xo;
817                 while (x--) {
818                         col = rt1[0] + ((fac3 * rt2[0]) >> 8);
819                         if (col > 255) rt[0] = 255; else rt[0] = col;
820                         col = rt1[1] + ((fac3 * rt2[1]) >> 8);
821                         if (col > 255) rt[1] = 255; else rt[1] = col;
822                         col = rt1[2] + ((fac3 * rt2[2]) >> 8);
823                         if (col > 255) rt[2] = 255; else rt[2] = col;
824                         col = rt1[3] + ((fac3 * rt2[3]) >> 8);
825                         if (col > 255) rt[3] = 255; else rt[3] = col;
826
827                         rt1 += 4; rt2 += 4; rt += 4;
828                 }
829         }
830 }
831
832 static void do_add_effect_float(float facf0, float facf1, int x, int y, float *rect1, float *rect2, float *out)
833 {
834         int xo;
835         float fac1, fac3;
836         float *rt1, *rt2, *rt;
837
838         xo = x;
839         rt1 = rect1;
840         rt2 = rect2;
841         rt = out;
842
843         fac1 = facf0;
844         fac3 = facf1;
845
846         while (y--) {
847                 x = xo * 4;
848                 while (x--) {
849                         *rt = *rt1 + fac1 * (*rt2);
850
851                         rt1++; rt2++; rt++;
852                 }
853
854                 if (y == 0)
855                         break;
856                 y--;
857
858                 x = xo * 4;
859                 while (x--) {
860                         *rt = *rt1 + fac3 * (*rt2);
861
862                         rt1++; rt2++; rt++;
863                 }
864         }
865 }
866
867 static ImBuf *do_add_effect(SeqRenderData context, Sequence *UNUSED(seq), float UNUSED(cfra), float facf0, float facf1,
868                             ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
869 {
870         struct ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
871
872         if (out->rect_float) {
873                 do_add_effect_float(facf0, facf1, context.rectx, context.recty, ibuf1->rect_float,
874                                     ibuf2->rect_float, out->rect_float);
875         }
876         else {
877                 do_add_effect_byte(facf0, facf1, context.rectx, context.recty, (unsigned char *) ibuf1->rect,
878                                    (unsigned char *) ibuf2->rect, (unsigned char *) out->rect);
879         }
880
881         return out;
882 }
883
884 /*********************** Sub *************************/
885
886 static void do_sub_effect_byte(float facf0, float facf1, int x, int y, char *rect1, char *rect2, char *out)
887 {
888         int col, xo, fac1, fac3;
889         char *rt1, *rt2, *rt;
890
891         xo = x;
892         rt1 = (char *) rect1;
893         rt2 = (char *) rect2;
894         rt = (char *) out;
895
896         fac1 = (int) (256.0f * facf0);
897         fac3 = (int) (256.0f * facf1);
898
899         while (y--) {
900                 x = xo;
901                 while (x--) {
902                         col = rt1[0] - ((fac1 * rt2[0]) >> 8);
903                         if (col < 0) rt[0] = 0; else rt[0] = col;
904                         col = rt1[1] - ((fac1 * rt2[1]) >> 8);
905                         if (col < 0) rt[1] = 0; else rt[1] = col;
906                         col = rt1[2] - ((fac1 * rt2[2]) >> 8);
907                         if (col < 0) rt[2] = 0; else rt[2] = col;
908                         col = rt1[3] - ((fac1 * rt2[3]) >> 8);
909                         if (col < 0) rt[3] = 0; else rt[3] = col;
910
911                         rt1 += 4; rt2 += 4; rt += 4;
912                 }
913
914                 if (y == 0)
915                         break;
916                 y--;
917
918                 x = xo;
919                 while (x--) {
920                         col = rt1[0] - ((fac3 * rt2[0]) >> 8);
921                         if (col < 0) rt[0] = 0; else rt[0] = col;
922                         col = rt1[1] - ((fac3 * rt2[1]) >> 8);
923                         if (col < 0) rt[1] = 0; else rt[1] = col;
924                         col = rt1[2] - ((fac3 * rt2[2]) >> 8);
925                         if (col < 0) rt[2] = 0; else rt[2] = col;
926                         col = rt1[3] - ((fac3 * rt2[3]) >> 8);
927                         if (col < 0) rt[3] = 0; else rt[3] = col;
928
929                         rt1 += 4; rt2 += 4; rt += 4;
930                 }
931         }
932 }
933
934 static void do_sub_effect_float(float facf0, float facf1, int x, int y, float *rect1, float *rect2, float *out)
935 {
936         int xo;
937         float fac1, fac3;
938         float *rt1, *rt2, *rt;
939
940         xo = x;
941         rt1 = rect1;
942         rt2 = rect2;
943         rt = out;
944
945         fac1 = facf0;
946         fac3 = facf1;
947
948         while (y--) {
949                 x = xo * 4;
950                 while (x--) {
951                         *rt = *rt1 - fac1 * (*rt2);
952
953                         rt1++; rt2++; rt++;
954                 }
955
956                 if (y == 0)
957                         break;
958                 y--;
959
960                 x = xo * 4;
961                 while (x--) {
962                         *rt = *rt1 - fac3 * (*rt2);
963
964                         rt1++; rt2++; rt++;
965                 }
966         }
967 }
968
969 static ImBuf *do_sub_effect(SeqRenderData context, Sequence *UNUSED(seq), float UNUSED(cfra), float facf0, float facf1,
970                             ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
971 {
972         struct ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
973
974         if (out->rect_float) {
975                 do_sub_effect_float(facf0, facf1, context.rectx, context.recty, ibuf1->rect_float,
976                                     ibuf2->rect_float, out->rect_float);
977         }
978         else {
979                 do_sub_effect_byte(facf0, facf1, context.rectx, context.recty, (char *) ibuf1->rect,
980                                    (char *) ibuf2->rect, (char *) out->rect);
981         }
982
983         return out;
984 }
985
986 /*********************** Drop *************************/
987
988 /* Must be > 0 or add precopy, etc to the function */
989 #define XOFF    8
990 #define YOFF    8
991
992 static void do_drop_effect_byte(float facf0, float facf1, int x, int y, char *rect2i, char *rect1i, char *outi)
993 {
994         int height, width, temp, fac, fac1, fac2;
995         char *rt1, *rt2, *out;
996         int field = 1;
997
998         width = x;
999         height = y;
1000
1001         fac1 = (int) (70.0f * facf0);
1002         fac2 = (int) (70.0f * facf1);
1003
1004         rt2 = (char *) (rect2i + YOFF * width);
1005         rt1 = (char *) rect1i;
1006         out = (char *) outi;
1007         for (y = 0; y < height - YOFF; y++) {
1008                 if (field) fac = fac1;
1009                 else fac = fac2;
1010                 field = !field;
1011
1012                 memcpy(out, rt1, sizeof(int) * XOFF);
1013                 rt1 += XOFF * 4;
1014                 out += XOFF * 4;
1015
1016                 for (x = XOFF; x < width; x++) {
1017                         temp = ((fac * rt2[3]) >> 8);
1018
1019                         *(out++) = MAX2(0, *rt1 - temp); rt1++;
1020                         *(out++) = MAX2(0, *rt1 - temp); rt1++;
1021                         *(out++) = MAX2(0, *rt1 - temp); rt1++;
1022                         *(out++) = MAX2(0, *rt1 - temp); rt1++;
1023                         rt2 += 4;
1024                 }
1025                 rt2 += XOFF * 4;
1026         }
1027         memcpy(out, rt1, sizeof(int) * YOFF * width);
1028 }
1029
1030 static void do_drop_effect_float(float facf0, float facf1, int x, int y, float *rect2i, float *rect1i, float *outi)
1031 {
1032         int height, width;
1033         float temp, fac, fac1, fac2;
1034         float *rt1, *rt2, *out;
1035         int field = 1;
1036
1037         width = x;
1038         height = y;
1039
1040         fac1 = 70.0f * facf0;
1041         fac2 = 70.0f * facf1;
1042
1043         rt2 =  (rect2i + YOFF * width);
1044         rt1 =  rect1i;
1045         out =  outi;
1046         for (y = 0; y < height - YOFF; y++) {
1047                 if (field) fac = fac1;
1048                 else fac = fac2;
1049                 field = !field;
1050
1051                 memcpy(out, rt1, 4 * sizeof(float) * XOFF);
1052                 rt1 += XOFF * 4;
1053                 out += XOFF * 4;
1054
1055                 for (x = XOFF; x < width; x++) {
1056                         temp = fac * rt2[3];
1057
1058                         *(out++) = MAX2(0.0f, *rt1 - temp); rt1++;
1059                         *(out++) = MAX2(0.0f, *rt1 - temp); rt1++;
1060                         *(out++) = MAX2(0.0f, *rt1 - temp); rt1++;
1061                         *(out++) = MAX2(0.0f, *rt1 - temp); rt1++;
1062                         rt2 += 4;
1063                 }
1064                 rt2 += XOFF * 4;
1065         }
1066         memcpy(out, rt1, 4 * sizeof(float) * YOFF * width);
1067 }
1068
1069 /*********************** Mul *************************/
1070
1071 static void do_mul_effect_byte(float facf0, float facf1, int x, int y, unsigned char *rect1, unsigned char *rect2,
1072                                unsigned char *out)
1073 {
1074         int xo, fac1, fac3;
1075         char *rt1, *rt2, *rt;
1076
1077         xo = x;
1078         rt1 = (char *)rect1;
1079         rt2 = (char *)rect2;
1080         rt = (char *)out;
1081
1082         fac1 = (int)(256.0f * facf0);
1083         fac3 = (int)(256.0f * facf1);
1084
1085         /* formula:
1086          *              fac * (a * b) + (1-fac)*a  => fac * a * (b - 1) + axaux = c * px + py * s; //+centx
1087          *              yaux = -s * px + c * py; //+centy
1088          */
1089
1090         while (y--) {
1091
1092                 x = xo;
1093                 while (x--) {
1094
1095                         rt[0] = rt1[0] + ((fac1 * rt1[0] * (rt2[0] - 256)) >> 16);
1096                         rt[1] = rt1[1] + ((fac1 * rt1[1] * (rt2[1] - 256)) >> 16);
1097                         rt[2] = rt1[2] + ((fac1 * rt1[2] * (rt2[2] - 256)) >> 16);
1098                         rt[3] = rt1[3] + ((fac1 * rt1[3] * (rt2[3] - 256)) >> 16);
1099
1100                         rt1 += 4; rt2 += 4; rt += 4;
1101                 }
1102
1103                 if (y == 0) break;
1104                 y--;
1105
1106                 x = xo;
1107                 while (x--) {
1108
1109                         rt[0] = rt1[0] + ((fac3 * rt1[0] * (rt2[0] - 256)) >> 16);
1110                         rt[1] = rt1[1] + ((fac3 * rt1[1] * (rt2[1] - 256)) >> 16);
1111                         rt[2] = rt1[2] + ((fac3 * rt1[2] * (rt2[2] - 256)) >> 16);
1112                         rt[3] = rt1[3] + ((fac3 * rt1[3] * (rt2[3] - 256)) >> 16);
1113
1114                         rt1 += 4; rt2 += 4; rt += 4;
1115                 }
1116         }
1117 }
1118
1119 static void do_mul_effect_float(float facf0, float facf1, int x, int y, float *rect1, float *rect2, float *out)
1120 {
1121         int xo;
1122         float fac1, fac3;
1123         float *rt1, *rt2, *rt;
1124
1125         xo = x;
1126         rt1 = rect1;
1127         rt2 = rect2;
1128         rt = out;
1129
1130         fac1 = facf0;
1131         fac3 = facf1;
1132
1133         /* formula:
1134          *              fac*(a*b) + (1-fac)*a  => fac*a*(b-1)+a
1135          */
1136
1137         while (y--) {
1138                 x = xo;
1139                 while (x--) {
1140                         rt[0] = rt1[0] + fac1 * rt1[0] * (rt2[0] - 1.0f);
1141                         rt[1] = rt1[1] + fac1 * rt1[1] * (rt2[1] - 1.0f);
1142                         rt[2] = rt1[2] + fac1 * rt1[2] * (rt2[2] - 1.0f);
1143                         rt[3] = rt1[3] + fac1 * rt1[3] * (rt2[3] - 1.0f);
1144
1145                         rt1 += 4; rt2 += 4; rt += 4;
1146                 }
1147
1148                 if (y == 0)
1149                         break;
1150                 y--;
1151
1152                 x = xo;
1153                 while (x--) {
1154                         rt[0] = rt1[0] + fac3 * rt1[0] * (rt2[0] - 1.0f);
1155                         rt[1] = rt1[1] + fac3 * rt1[1] * (rt2[1] - 1.0f);
1156                         rt[2] = rt1[2] + fac3 * rt1[2] * (rt2[2] - 1.0f);
1157                         rt[3] = rt1[3] + fac3 * rt1[3] * (rt2[3] - 1.0f);
1158
1159                         rt1 += 4; rt2 += 4; rt += 4;
1160                 }
1161         }
1162 }
1163
1164 static ImBuf *do_mul_effect(SeqRenderData context, Sequence *UNUSED(seq), float UNUSED(cfra), float facf0, float facf1,
1165                             ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
1166 {
1167         struct ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
1168
1169         if (out->rect_float) {
1170                 do_mul_effect_float(facf0, facf1, context.rectx, context.recty, ibuf1->rect_float,
1171                                     ibuf2->rect_float, out->rect_float);
1172         }
1173         else {
1174                 do_mul_effect_byte(facf0, facf1, context.rectx, context.recty, (unsigned char *) ibuf1->rect,
1175                                    (unsigned char *) ibuf2->rect, (unsigned char *) out->rect);
1176         }
1177
1178         return out;
1179 }
1180
1181 /*********************** Wipe *************************/
1182
1183 typedef struct WipeZone {
1184         float angle;
1185         int flip;
1186         int xo, yo;
1187         int width;
1188         float pythangle;
1189 } WipeZone;
1190
1191 static void precalc_wipe_zone(WipeZone *wipezone, WipeVars *wipe, int xo, int yo)
1192 {
1193         wipezone->flip = (wipe->angle < 0);
1194         wipezone->angle = tanf(DEG2RADF(fabsf(wipe->angle)));
1195         wipezone->xo = xo;
1196         wipezone->yo = yo;
1197         wipezone->width = (int)(wipe->edgeWidth * ((xo + yo) / 2.0f));
1198         wipezone->pythangle = 1.0f / sqrtf(wipezone->angle * wipezone->angle + 1.0f);
1199 }
1200
1201 // This function calculates the blur band for the wipe effects
1202 static float in_band(float width, float dist, int side, int dir)
1203 {
1204         float alpha;
1205
1206         if (width == 0)
1207                 return (float)side;
1208
1209         if (width < dist)
1210                 return (float)side;
1211
1212         if (side == 1)
1213                 alpha = (dist + 0.5f * width) / (width);
1214         else
1215                 alpha = (0.5f * width - dist) / (width);
1216
1217         if (dir == 0)
1218                 alpha = 1 - alpha;
1219
1220         return alpha;
1221 }
1222
1223 static float check_zone(WipeZone *wipezone, int x, int y, Sequence *seq, float facf0)
1224 {
1225         float posx, posy, hyp, hyp2, angle, hwidth, b1, b2, b3, pointdist;
1226         /* some future stuff */
1227         /* float hyp3, hyp4, b4, b5 */
1228         float temp1, temp2, temp3, temp4; /* some placeholder variables */
1229         int xo = wipezone->xo;
1230         int yo = wipezone->yo;
1231         float halfx = xo * 0.5f;
1232         float halfy = yo * 0.5f;
1233         float widthf, output = 0;
1234         WipeVars *wipe = (WipeVars *)seq->effectdata;
1235         int width;
1236
1237         if (wipezone->flip) x = xo - x;
1238         angle = wipezone->angle;
1239
1240         if (wipe->forward) {
1241                 posx = facf0 * xo;
1242                 posy = facf0 * yo;
1243         }
1244         else {
1245                 posx = xo - facf0 * xo;
1246                 posy = yo - facf0 * yo;
1247         }
1248
1249         switch (wipe->wipetype) {
1250                 case DO_SINGLE_WIPE:
1251                         width = wipezone->width;
1252
1253                         if (angle == 0.0f) {
1254                                 b1 = posy;
1255                                 b2 = y;
1256                                 hyp = fabs(y - posy);
1257                         }
1258                         else {
1259                                 b1 = posy - (-angle) * posx;
1260                                 b2 = y - (-angle) * x;
1261                                 hyp = fabsf(angle * x + y + (-posy - angle * posx)) * wipezone->pythangle;
1262                         }
1263
1264                         if (angle < 0) {
1265                                 temp1 = b1;
1266                                 b1 = b2;
1267                                 b2 = temp1;
1268                         }
1269
1270                         if (wipe->forward) {
1271                                 if (b1 < b2)
1272                                         output = in_band(width, hyp, 1, 1);
1273                                 else
1274                                         output = in_band(width, hyp, 0, 1);
1275                         }
1276                         else {
1277                                 if (b1 < b2)
1278                                         output = in_band(width, hyp, 0, 1);
1279                                 else
1280                                         output = in_band(width, hyp, 1, 1);
1281                         }
1282                         break;
1283
1284                 case DO_DOUBLE_WIPE:
1285                         if (!wipe->forward)
1286                                 facf0 = 1.0f - facf0;  /* Go the other direction */
1287
1288                         width = wipezone->width;  /* calculate the blur width */
1289                         hwidth = width * 0.5f;
1290                         if (angle == 0) {
1291                                 b1 = posy * 0.5f;
1292                                 b3 = yo - posy * 0.5f;
1293                                 b2 = y;
1294
1295                                 hyp = abs(y - posy * 0.5f);
1296                                 hyp2 = abs(y - (yo - posy * 0.5f));
1297                         }
1298                         else {
1299                                 b1 = posy * 0.5f - (-angle) * posx * 0.5f;
1300                                 b3 = (yo - posy * 0.5f) - (-angle) * (xo - posx * 0.5f);
1301                                 b2 = y - (-angle) * x;
1302
1303                                 hyp = fabsf(angle * x + y + (-posy * 0.5f - angle * posx * 0.5f)) * wipezone->pythangle;
1304                                 hyp2 = fabsf(angle * x + y + (-(yo - posy * 0.5f) - angle * (xo - posx * 0.5f))) * wipezone->pythangle;
1305                         }
1306
1307                         hwidth = minf(hwidth, fabsf(b3 - b1) / 2.0f);
1308
1309                         if (b2 < b1 && b2 < b3) {
1310                                 output = in_band(hwidth, hyp, 0, 1);
1311                         }
1312                         else if (b2 > b1 && b2 > b3) {
1313                                 output = in_band(hwidth, hyp2, 0, 1);
1314                         }
1315                         else {
1316                                 if (hyp < hwidth && hyp2 > hwidth)
1317                                         output = in_band(hwidth, hyp, 1, 1);
1318                                 else if (hyp > hwidth && hyp2 < hwidth)
1319                                         output = in_band(hwidth, hyp2, 1, 1);
1320                                 else
1321                                         output = in_band(hwidth, hyp2, 1, 1) * in_band(hwidth, hyp, 1, 1);
1322                         }
1323                         if (!wipe->forward) output = 1 - output;
1324                         break;
1325                 case DO_CLOCK_WIPE:
1326                         /*
1327                          *  temp1: angle of effect center in rads
1328                          *  temp2: angle of line through (halfx, halfy) and (x, y) in rads
1329                          *  temp3: angle of low side of blur
1330                          *  temp4: angle of high side of blur
1331                          */
1332                         output = 1.0f - facf0;
1333                         widthf = wipe->edgeWidth * 2.0f * (float)M_PI;
1334                         temp1 = 2.0f * (float)M_PI * facf0;
1335
1336                         if (wipe->forward) {
1337                                 temp1 = 2.0f * (float)M_PI - temp1;
1338                         }
1339
1340                         x = x - halfx;
1341                         y = y - halfy;
1342
1343                         temp2 = asin(abs(y) / sqrt(x * x + y * y));
1344                         if (x <= 0 && y >= 0) temp2 = (float)M_PI - temp2;
1345                         else if (x <= 0 && y <= 0) temp2 += (float)M_PI;
1346                         else if (x >= 0 && y <= 0) temp2 = 2.0f * (float)M_PI - temp2;
1347
1348                         if (wipe->forward) {
1349                                 temp3 = temp1 - (widthf * 0.5f) * facf0;
1350                                 temp4 = temp1 + (widthf * 0.5f) * (1 - facf0);
1351                         }
1352                         else {
1353                                 temp3 = temp1 - (widthf * 0.5f) * (1 - facf0);
1354                                 temp4 = temp1 + (widthf * 0.5f) * facf0;
1355                         }
1356                         if (temp3 < 0) temp3 = 0;
1357                         if (temp4 > 2.0f * (float)M_PI) temp4 = 2.0f * (float)M_PI;
1358
1359
1360                         if (temp2 < temp3) output = 0;
1361                         else if (temp2 > temp4) output = 1;
1362                         else output = (temp2 - temp3) / (temp4 - temp3);
1363                         if (x == 0 && y == 0) output = 1;
1364                         if (output != output) output = 1;
1365                         if (wipe->forward) output = 1 - output;
1366                         break;
1367                         /* BOX WIPE IS NOT WORKING YET */
1368                         /* case DO_CROSS_WIPE: */
1369                         /* BOX WIPE IS NOT WORKING YET */
1370 #if 0
1371                 case DO_BOX_WIPE: 
1372                         if (invert) facf0 = 1 - facf0;
1373
1374                         width = (int)(wipe->edgeWidth * ((xo + yo) / 2.0));
1375                         hwidth = (float)width / 2.0;
1376                         if (angle == 0) angle = 0.000001;
1377                         b1 = posy / 2 - (-angle) * posx / 2;
1378                         b3 = (yo - posy / 2) - (-angle) * (xo - posx / 2);
1379                         b2 = y - (-angle) * x;
1380
1381                         hyp = abs(angle * x + y + (-posy / 2 - angle * posx / 2)) * wipezone->pythangle;
1382                         hyp2 = abs(angle * x + y + (-(yo - posy / 2) - angle * (xo - posx / 2))) * wipezone->pythangle;
1383
1384                         temp1 = xo * (1 - facf0 / 2) - xo * facf0 / 2;
1385                         temp2 = yo * (1 - facf0 / 2) - yo * facf0 / 2;
1386                         pointdist = sqrt(temp1 * temp1 + temp2 * temp2);
1387
1388                         if (b2 < b1 && b2 < b3) {
1389                                 if (hwidth < pointdist)
1390                                         output = in_band(wipezone, hwidth, hyp, facf0, 0, 1);
1391                         }
1392                         else if (b2 > b1 && b2 > b3) {
1393                                 if (hwidth < pointdist)
1394                                         output = in_band(wipezone, hwidth, hyp2, facf0, 0, 1);
1395                         }
1396                         else {
1397                                 if (hyp < hwidth && hyp2 > hwidth)
1398                                         output = in_band(wipezone, hwidth, hyp, facf0, 1, 1);
1399                                 else if (hyp > hwidth && hyp2 < hwidth)
1400                                         output = in_band(wipezone, hwidth, hyp2, facf0, 1, 1);
1401                                 else
1402                                         output = in_band(wipezone, hwidth, hyp2, facf0, 1, 1) * in_band(wipezone, hwidth, hyp, facf0, 1, 1);
1403                         }
1404
1405                         if (invert) facf0 = 1 - facf0;
1406                         angle = -1 / angle;
1407                         b1 = posy / 2 - (-angle) * posx / 2;
1408                         b3 = (yo - posy / 2) - (-angle) * (xo - posx / 2);
1409                         b2 = y - (-angle) * x;
1410
1411                         hyp = abs(angle * x + y + (-posy / 2 - angle * posx / 2)) * wipezone->pythangle;
1412                         hyp2 = abs(angle * x + y + (-(yo - posy / 2) - angle * (xo - posx / 2))) * wipezone->pythangle;
1413
1414                         if (b2 < b1 && b2 < b3) {
1415                                 if (hwidth < pointdist)
1416                                         output *= in_band(wipezone, hwidth, hyp, facf0, 0, 1);
1417                         }
1418                         else if (b2 > b1 && b2 > b3) {
1419                                 if (hwidth < pointdist)
1420                                         output *= in_band(wipezone, hwidth, hyp2, facf0, 0, 1);
1421                         }
1422                         else {
1423                                 if (hyp < hwidth && hyp2 > hwidth)
1424                                         output *= in_band(wipezone, hwidth, hyp, facf0, 1, 1);
1425                                 else if (hyp > hwidth && hyp2 < hwidth)
1426                                         output *= in_band(wipezone, hwidth, hyp2, facf0, 1, 1);
1427                                 else
1428                                         output *= in_band(wipezone, hwidth, hyp2, facf0, 1, 1) * in_band(wipezone, hwidth, hyp, facf0, 1, 1);
1429                         }
1430
1431                         break;
1432 #endif
1433                 case DO_IRIS_WIPE:
1434                         if (xo > yo) yo = xo;
1435                         else xo = yo;
1436
1437                         if (!wipe->forward) facf0 = 1 - facf0;
1438
1439                         width = wipezone->width;
1440                         hwidth = width * 0.5f;
1441
1442                         temp1 = (halfx - (halfx) * facf0);
1443                         pointdist = sqrt(temp1 * temp1 + temp1 * temp1);
1444
1445                         temp2 = sqrt((halfx - x) * (halfx - x) + (halfy - y) * (halfy - y));
1446                         if (temp2 > pointdist) output = in_band(hwidth, fabs(temp2 - pointdist), 0, 1);
1447                         else output = in_band(hwidth, fabs(temp2 - pointdist), 1, 1);
1448
1449                         if (!wipe->forward) output = 1 - output;
1450                         
1451                         break;
1452         }
1453         if (output < 0) output = 0;
1454         else if (output > 1) output = 1;
1455         return output;
1456 }
1457
1458 static void init_wipe_effect(Sequence *seq)
1459 {
1460         if (seq->effectdata)
1461                 MEM_freeN(seq->effectdata);
1462
1463         seq->effectdata = MEM_callocN(sizeof(struct WipeVars), "wipevars");
1464 }
1465
1466 static int num_inputs_wipe(void)
1467 {
1468         return 1;
1469 }
1470
1471 static void free_wipe_effect(Sequence *seq)
1472 {
1473         if (seq->effectdata)
1474                 MEM_freeN(seq->effectdata);
1475
1476         seq->effectdata = NULL;
1477 }
1478
1479 static void copy_wipe_effect(Sequence *dst, Sequence *src)
1480 {
1481         dst->effectdata = MEM_dupallocN(src->effectdata);
1482 }
1483
1484 static void do_wipe_effect_byte(Sequence *seq, float facf0, float UNUSED(facf1),  int x, int y, unsigned char *rect1,
1485                                 unsigned char *rect2, unsigned char *out)
1486 {
1487         WipeZone wipezone;
1488         WipeVars *wipe = (WipeVars *)seq->effectdata;
1489         int xo, yo;
1490         char *rt1, *rt2, *rt;
1491
1492         precalc_wipe_zone(&wipezone, wipe, x, y);
1493
1494         rt1 = (char *)rect1;
1495         rt2 = (char *)rect2;
1496         rt = (char *)out;
1497
1498         xo = x;
1499         yo = y;
1500         for (y = 0; y < yo; y++) {
1501                 for (x = 0; x < xo; x++) {
1502                         float check = check_zone(&wipezone, x, y, seq, facf0);
1503                         if (check) {
1504                                 if (rt1) {
1505                                         rt[0] = (int)(rt1[0] * check) + (int)(rt2[0] * (1 - check));
1506                                         rt[1] = (int)(rt1[1] * check) + (int)(rt2[1] * (1 - check));
1507                                         rt[2] = (int)(rt1[2] * check) + (int)(rt2[2] * (1 - check));
1508                                         rt[3] = (int)(rt1[3] * check) + (int)(rt2[3] * (1 - check));
1509                                 }
1510                                 else {
1511                                         rt[0] = 0;
1512                                         rt[1] = 0;
1513                                         rt[2] = 0;
1514                                         rt[3] = 255;
1515                                 }
1516                         }
1517                         else {
1518                                 if (rt2) {
1519                                         rt[0] = rt2[0];
1520                                         rt[1] = rt2[1];
1521                                         rt[2] = rt2[2];
1522                                         rt[3] = rt2[3];
1523                                 }
1524                                 else {
1525                                         rt[0] = 0;
1526                                         rt[1] = 0;
1527                                         rt[2] = 0;
1528                                         rt[3] = 255;
1529                                 }
1530                         }
1531
1532                         rt += 4;
1533                         if (rt1 != NULL) {
1534                                 rt1 += 4;
1535                         }
1536                         if (rt2 != NULL) {
1537                                 rt2 += 4;
1538                         }
1539                 }
1540         }
1541 }
1542
1543 static void do_wipe_effect_float(Sequence *seq, float facf0, float UNUSED(facf1), int x, int y, float *rect1,
1544                                  float *rect2, float *out)
1545 {
1546         WipeZone wipezone;
1547         WipeVars *wipe = (WipeVars *)seq->effectdata;
1548         int xo, yo;
1549         float *rt1, *rt2, *rt;
1550
1551         precalc_wipe_zone(&wipezone, wipe, x, y);
1552
1553         rt1 = rect1;
1554         rt2 = rect2;
1555         rt = out;
1556
1557         xo = x;
1558         yo = y;
1559         for (y = 0; y < yo; y++) {
1560                 for (x = 0; x < xo; x++) {
1561                         float check = check_zone(&wipezone, x, y, seq, facf0);
1562                         if (check) {
1563                                 if (rt1) {
1564                                         rt[0] = rt1[0] * check + rt2[0] * (1 - check);
1565                                         rt[1] = rt1[1] * check + rt2[1] * (1 - check);
1566                                         rt[2] = rt1[2] * check + rt2[2] * (1 - check);
1567                                         rt[3] = rt1[3] * check + rt2[3] * (1 - check);
1568                                 }
1569                                 else {
1570                                         rt[0] = 0;
1571                                         rt[1] = 0;
1572                                         rt[2] = 0;
1573                                         rt[3] = 1.0;
1574                                 }
1575                         }
1576                         else {
1577                                 if (rt2) {
1578                                         rt[0] = rt2[0];
1579                                         rt[1] = rt2[1];
1580                                         rt[2] = rt2[2];
1581                                         rt[3] = rt2[3];
1582                                 }
1583                                 else {
1584                                         rt[0] = 0;
1585                                         rt[1] = 0;
1586                                         rt[2] = 0;
1587                                         rt[3] = 1.0;
1588                                 }
1589                         }
1590
1591                         rt += 4;
1592                         if (rt1 != NULL) {
1593                                 rt1 += 4;
1594                         }
1595                         if (rt2 != NULL) {
1596                                 rt2 += 4;
1597                         }
1598                 }
1599         }
1600 }
1601
1602 static ImBuf *do_wipe_effect(SeqRenderData context, Sequence *seq, float UNUSED(cfra), float facf0, float facf1,
1603                              ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
1604 {
1605         struct ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
1606
1607         if (out->rect_float) {
1608                 do_wipe_effect_float(seq, facf0, facf1, context.rectx, context.recty, ibuf1->rect_float,
1609                                      ibuf2->rect_float, out->rect_float);
1610         }
1611         else {
1612                 do_wipe_effect_byte(seq, facf0, facf1, context.rectx, context.recty, (unsigned char *) ibuf1->rect,
1613                                     (unsigned char *) ibuf2->rect, (unsigned char *) out->rect);
1614         }
1615
1616         return out;
1617 }
1618
1619 /*********************** Transform *************************/
1620
1621 static void init_transform_effect(Sequence *seq)
1622 {
1623         TransformVars *transform;
1624
1625         if (seq->effectdata)
1626                 MEM_freeN(seq->effectdata);
1627
1628         seq->effectdata = MEM_callocN(sizeof(struct TransformVars), "transformvars");
1629
1630         transform = (TransformVars *) seq->effectdata;
1631
1632         transform->ScalexIni = 1.0f;
1633         transform->ScaleyIni = 1.0f;
1634
1635         transform->xIni = 0.0f;
1636         transform->yIni = 0.0f;
1637
1638         transform->rotIni = 0.0f;
1639         
1640         transform->interpolation = 1;
1641         transform->percent = 1;
1642         transform->uniform_scale = 0;
1643 }
1644
1645 static int num_inputs_transform(void)
1646 {
1647         return 1;
1648 }
1649
1650 static void free_transform_effect(Sequence *seq)
1651 {
1652         if (seq->effectdata) MEM_freeN(seq->effectdata);
1653         seq->effectdata = NULL;
1654 }
1655
1656 static void copy_transform_effect(Sequence *dst, Sequence *src)
1657 {
1658         dst->effectdata = MEM_dupallocN(src->effectdata);
1659 }
1660
1661 static void transform_image(int x, int y, ImBuf *ibuf1, ImBuf *out,  float scale_x, float scale_y,
1662                             float translate_x, float translate_y, float rotate, int interpolation)
1663 {
1664         int xo, yo, xi, yi;
1665         float xt, yt, xr, yr;
1666         float s, c;
1667
1668         xo = x;
1669         yo = y;
1670         
1671         /* Rotate */
1672         s = sin(rotate);
1673         c = cos(rotate);
1674
1675         for (yi = 0; yi < yo; yi++) {
1676                 for (xi = 0; xi < xo; xi++) {
1677                         /* translate point */
1678                         xt = xi - translate_x;
1679                         yt = yi - translate_y;
1680
1681                         /* rotate point with center ref */
1682                         xr =  c * xt + s * yt;
1683                         yr = -s * xt + c * yt;
1684
1685                         /* scale point with center ref */
1686                         xt = xr / scale_x;
1687                         yt = yr / scale_y;
1688
1689                         /* undo reference center point  */
1690                         xt += (xo / 2.0f);
1691                         yt += (yo / 2.0f);
1692
1693                         /* interpolate */
1694                         switch (interpolation) {
1695                                 case 0:
1696                                         neareast_interpolation(ibuf1, out, xt, yt, xi, yi);
1697                                         break;
1698                                 case 1:
1699                                         bilinear_interpolation(ibuf1, out, xt, yt, xi, yi);
1700                                         break;
1701                                 case 2:
1702                                         bicubic_interpolation(ibuf1, out, xt, yt, xi, yi);
1703                                         break;
1704                         }
1705                 }
1706         }
1707 }
1708
1709 static void do_transform(Scene *scene, Sequence *seq, float UNUSED(facf0), int x, int y,  ImBuf *ibuf1, ImBuf *out)
1710 {
1711         TransformVars *transform = (TransformVars *) seq->effectdata;
1712         float scale_x, scale_y, translate_x, translate_y, rotate_radians;
1713         
1714         /* Scale */
1715         if (transform->uniform_scale) {
1716                 scale_x = scale_y = transform->ScalexIni;
1717         }
1718         else {
1719                 scale_x = transform->ScalexIni;
1720                 scale_y = transform->ScaleyIni;
1721         }
1722
1723         /* Translate */
1724         if (!transform->percent) {
1725                 float rd_s = (scene->r.size / 100.0f);
1726
1727                 translate_x = transform->xIni * rd_s + (x / 2.0f);
1728                 translate_y = transform->yIni * rd_s + (y / 2.0f);
1729         }
1730         else {
1731                 translate_x = x * (transform->xIni / 100.0f) + (x / 2.0f);
1732                 translate_y = y * (transform->yIni / 100.0f) + (y / 2.0f);
1733         }
1734
1735         /* Rotate */
1736         rotate_radians = DEG2RADF(transform->rotIni);
1737
1738         transform_image(x, y, ibuf1, out, scale_x, scale_y, translate_x, translate_y, rotate_radians, transform->interpolation);
1739 }
1740
1741
1742 static ImBuf *do_transform_effect(SeqRenderData context, Sequence *seq, float UNUSED(cfra), float facf0,
1743                                   float UNUSED(facf1), ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
1744 {
1745         struct ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
1746
1747         do_transform(context.scene, seq, facf0, context.rectx, context.recty, ibuf1, out);
1748
1749         return out;
1750 }
1751
1752 /*********************** Glow *************************/
1753
1754 static void RVBlurBitmap2_byte(unsigned char *map, int width, int height, float blur, int quality)
1755 /*      MUUUCCH better than the previous blur. */
1756 /*      We do the blurring in two passes which is a whole lot faster. */
1757 /*      I changed the math arount to implement an actual Gaussian */
1758 /*      distribution. */
1759 /* */
1760 /*      Watch out though, it tends to misbehaven with large blur values on */
1761 /*      a small bitmap.  Avoid avoid avoid. */
1762 /*=============================== */
1763 {
1764         unsigned char *temp = NULL, *swap;
1765         float   *filter = NULL;
1766         int x, y, i, fx, fy;
1767         int index, ix, halfWidth;
1768         float fval, k, curColor[3], curColor2[3], weight = 0;
1769
1770         /* If we're not really blurring, bail out */
1771         if (blur <= 0)
1772                 return;
1773
1774         /*Allocate memory for the tempmap and the blur filter matrix */
1775         temp = MEM_mallocN((width * height * 4), "blurbitmaptemp");
1776         if (!temp)
1777                 return;
1778
1779         /*Allocate memory for the filter elements */
1780         halfWidth = ((quality + 1) * blur);
1781         filter = (float *)MEM_mallocN(sizeof(float) * halfWidth * 2, "blurbitmapfilter");
1782         if (!filter) {
1783                 MEM_freeN(temp);
1784                 return;
1785         }
1786
1787         /* Apparently we're calculating a bell curve based on the standard deviation (or radius)
1788          * This code is based on an example posted to comp.graphics.algorithms by
1789          * Blancmange (bmange@airdmhor.gen.nz)
1790          */
1791
1792         k = -1.0f / (2.0f * (float)M_PI * blur * blur);
1793         for (ix = 0; ix < halfWidth; ix++) {
1794                 weight = (float)exp(k * (ix * ix));
1795                 filter[halfWidth - ix] = weight;
1796                 filter[halfWidth + ix] = weight;
1797         }
1798         filter[0] = weight;
1799
1800         /* Normalize the array */
1801         fval = 0;
1802         for (ix = 0; ix < halfWidth * 2; ix++)
1803                 fval += filter[ix];
1804
1805         for (ix = 0; ix < halfWidth * 2; ix++)
1806                 filter[ix] /= fval;
1807
1808         /* Blur the rows */
1809         for (y = 0; y < height; y++) {
1810                 /* Do the left & right strips */
1811                 for (x = 0; x < halfWidth; x++) {
1812                         index = (x + y * width) * 4;
1813                         fx = 0;
1814                         zero_v3(curColor);
1815                         zero_v3(curColor2);
1816
1817                         for (i = x - halfWidth; i < x + halfWidth; i++) {
1818                                 if ((i >= 0) && (i < width)) {
1819                                         curColor[0] += map[(i + y * width) * 4 + GlowR] * filter[fx];
1820                                         curColor[1] += map[(i + y * width) * 4 + GlowG] * filter[fx];
1821                                         curColor[2] += map[(i + y * width) * 4 + GlowB] * filter[fx];
1822
1823                                         curColor2[0] += map[(width - 1 - i + y * width) * 4 + GlowR] * filter[fx];
1824                                         curColor2[1] += map[(width - 1 - i + y * width) * 4 + GlowG] * filter[fx];
1825                                         curColor2[2] += map[(width - 1 - i + y * width) * 4 + GlowB] * filter[fx];
1826                                 }
1827                                 fx++;
1828                         }
1829                         temp[index + GlowR] = curColor[0];
1830                         temp[index + GlowG] = curColor[1];
1831                         temp[index + GlowB] = curColor[2];
1832
1833                         temp[((width - 1 - x + y * width) * 4) + GlowR] = curColor2[0];
1834                         temp[((width - 1 - x + y * width) * 4) + GlowG] = curColor2[1];
1835                         temp[((width - 1 - x + y * width) * 4) + GlowB] = curColor2[2];
1836
1837                 }
1838
1839                 /* Do the main body */
1840                 for (x = halfWidth; x < width - halfWidth; x++) {
1841                         index = (x + y * width) * 4;
1842                         fx = 0;
1843                         zero_v3(curColor);
1844                         for (i = x - halfWidth; i < x + halfWidth; i++) {
1845                                 curColor[0] += map[(i + y * width) * 4 + GlowR] * filter[fx];
1846                                 curColor[1] += map[(i + y * width) * 4 + GlowG] * filter[fx];
1847                                 curColor[2] += map[(i + y * width) * 4 + GlowB] * filter[fx];
1848                                 fx++;
1849                         }
1850                         temp[index + GlowR] = curColor[0];
1851                         temp[index + GlowG] = curColor[1];
1852                         temp[index + GlowB] = curColor[2];
1853                 }
1854         }
1855
1856         /* Swap buffers */
1857         swap = temp; temp = map; map = swap;
1858
1859         /* Blur the columns */
1860         for (x = 0; x < width; x++) {
1861                 /* Do the top & bottom strips */
1862                 for (y = 0; y < halfWidth; y++) {
1863                         index = (x + y * width) * 4;
1864                         fy = 0;
1865                         zero_v3(curColor);
1866                         zero_v3(curColor2);
1867                         for (i = y - halfWidth; i < y + halfWidth; i++) {
1868                                 if ((i >= 0) && (i < height)) {
1869                                         /* Bottom */
1870                                         curColor[0] += map[(x + i * width) * 4 + GlowR] * filter[fy];
1871                                         curColor[1] += map[(x + i * width) * 4 + GlowG] * filter[fy];
1872                                         curColor[2] += map[(x + i * width) * 4 + GlowB] * filter[fy];
1873
1874                                         /* Top */
1875                                         curColor2[0] += map[(x + (height - 1 - i) * width) * 4 + GlowR] * filter[fy];
1876                                         curColor2[1] += map[(x + (height - 1 - i) * width) * 4 + GlowG] * filter[fy];
1877                                         curColor2[2] += map[(x + (height - 1 - i) * width) * 4 + GlowB] * filter[fy];
1878                                 }
1879                                 fy++;
1880                         }
1881                         temp[index + GlowR] = curColor[0];
1882                         temp[index + GlowG] = curColor[1];
1883                         temp[index + GlowB] = curColor[2];
1884                         temp[((x + (height - 1 - y) * width) * 4) + GlowR] = curColor2[0];
1885                         temp[((x + (height - 1 - y) * width) * 4) + GlowG] = curColor2[1];
1886                         temp[((x + (height - 1 - y) * width) * 4) + GlowB] = curColor2[2];
1887                 }
1888
1889                 /* Do the main body */
1890                 for (y = halfWidth; y < height - halfWidth; y++) {
1891                         index = (x + y * width) * 4;
1892                         fy = 0;
1893                         zero_v3(curColor);
1894                         for (i = y - halfWidth; i < y + halfWidth; i++) {
1895                                 curColor[0] += map[(x + i * width) * 4 + GlowR] * filter[fy];
1896                                 curColor[1] += map[(x + i * width) * 4 + GlowG] * filter[fy];
1897                                 curColor[2] += map[(x + i * width) * 4 + GlowB] * filter[fy];
1898                                 fy++;
1899                         }
1900                         temp[index + GlowR] = curColor[0];
1901                         temp[index + GlowG] = curColor[1];
1902                         temp[index + GlowB] = curColor[2];
1903                 }
1904         }
1905
1906         /* Swap buffers */
1907         swap = temp; temp = map; /* map = swap; */ /* UNUSED */
1908
1909         /* Tidy up */
1910         MEM_freeN(filter);
1911         MEM_freeN(temp);
1912 }
1913
1914 static void RVBlurBitmap2_float(float *map, int width, int height, float blur, int quality)
1915 /*      MUUUCCH better than the previous blur. */
1916 /*      We do the blurring in two passes which is a whole lot faster. */
1917 /*      I changed the math arount to implement an actual Gaussian */
1918 /*      distribution. */
1919 /* */
1920 /*      Watch out though, it tends to misbehaven with large blur values on */
1921 /*      a small bitmap.  Avoid avoid avoid. */
1922 /*=============================== */
1923 {
1924         float *temp = NULL, *swap;
1925         float *filter = NULL;
1926         int x, y, i, fx, fy;
1927         int index, ix, halfWidth;
1928         float fval, k, curColor[3], curColor2[3], weight = 0;
1929
1930         /* If we're not really blurring, bail out */
1931         if (blur <= 0)
1932                 return;
1933
1934         /* Allocate memory for the tempmap and the blur filter matrix */
1935         temp = MEM_mallocN((width * height * 4 * sizeof(float)), "blurbitmaptemp");
1936         if (!temp)
1937                 return;
1938
1939         /* Allocate memory for the filter elements */
1940         halfWidth = ((quality + 1) * blur);
1941         filter = (float *)MEM_mallocN(sizeof(float) * halfWidth * 2, "blurbitmapfilter");
1942         if (!filter) {
1943                 MEM_freeN(temp);
1944                 return;
1945         }
1946
1947         /* Apparently we're calculating a bell curve based on the standard deviation (or radius)
1948          * This code is based on an example posted to comp.graphics.algorithms by
1949          * Blancmange (bmange@airdmhor.gen.nz)
1950          */
1951
1952         k = -1.0f / (2.0f * (float)M_PI * blur * blur);
1953
1954         for (ix = 0; ix < halfWidth; ix++) {
1955                 weight = (float)exp(k * (ix * ix));
1956                 filter[halfWidth - ix] = weight;
1957                 filter[halfWidth + ix] = weight;
1958         }
1959         filter[0] = weight;
1960
1961         /* Normalize the array */
1962         fval = 0;
1963         for (ix = 0; ix < halfWidth * 2; ix++)
1964                 fval += filter[ix];
1965
1966         for (ix = 0; ix < halfWidth * 2; ix++)
1967                 filter[ix] /= fval;
1968
1969         /* Blur the rows */
1970         for (y = 0; y < height; y++) {
1971                 /* Do the left & right strips */
1972                 for (x = 0; x < halfWidth; x++) {
1973                         index = (x + y * width) * 4;
1974                         fx = 0;
1975                         curColor[0] = curColor[1] = curColor[2] = 0.0f;
1976                         curColor2[0] = curColor2[1] = curColor2[2] = 0.0f;
1977
1978                         for (i = x - halfWidth; i < x + halfWidth; i++) {
1979                                 if ((i >= 0) && (i < width)) {
1980                                         curColor[0] += map[(i + y * width) * 4 + GlowR] * filter[fx];
1981                                         curColor[1] += map[(i + y * width) * 4 + GlowG] * filter[fx];
1982                                         curColor[2] += map[(i + y * width) * 4 + GlowB] * filter[fx];
1983
1984                                         curColor2[0] += map[(width - 1 - i + y * width) * 4 + GlowR] * filter[fx];
1985                                         curColor2[1] += map[(width - 1 - i + y * width) * 4 + GlowG] * filter[fx];
1986                                         curColor2[2] += map[(width - 1 - i + y * width) * 4 + GlowB] * filter[fx];
1987                                 }
1988                                 fx++;
1989                         }
1990                         temp[index + GlowR] = curColor[0];
1991                         temp[index + GlowG] = curColor[1];
1992                         temp[index + GlowB] = curColor[2];
1993
1994                         temp[((width - 1 - x + y * width) * 4) + GlowR] = curColor2[0];
1995                         temp[((width - 1 - x + y * width) * 4) + GlowG] = curColor2[1];
1996                         temp[((width - 1 - x + y * width) * 4) + GlowB] = curColor2[2];
1997
1998                 }
1999
2000                 /* Do the main body */
2001                 for (x = halfWidth; x < width - halfWidth; x++) {
2002                         index = (x + y * width) * 4;
2003                         fx = 0;
2004                         zero_v3(curColor);
2005                         for (i = x - halfWidth; i < x + halfWidth; i++) {
2006                                 curColor[0] += map[(i + y * width) * 4 + GlowR] * filter[fx];
2007                                 curColor[1] += map[(i + y * width) * 4 + GlowG] * filter[fx];
2008                                 curColor[2] += map[(i + y * width) * 4 + GlowB] * filter[fx];
2009                                 fx++;
2010                         }
2011                         temp[index + GlowR] = curColor[0];
2012                         temp[index + GlowG] = curColor[1];
2013                         temp[index + GlowB] = curColor[2];
2014                 }
2015         }
2016
2017         /* Swap buffers */
2018         swap = temp; temp = map; map = swap;
2019
2020         /* Blur the columns */
2021         for (x = 0; x < width; x++) {
2022                 /* Do the top & bottom strips */
2023                 for (y = 0; y < halfWidth; y++) {
2024                         index = (x + y * width) * 4;
2025                         fy = 0;
2026                         zero_v3(curColor);
2027                         zero_v3(curColor2);
2028                         for (i = y - halfWidth; i < y + halfWidth; i++) {
2029                                 if ((i >= 0) && (i < height)) {
2030                                         /* Bottom */
2031                                         curColor[0] += map[(x + i * width) * 4 + GlowR] * filter[fy];
2032                                         curColor[1] += map[(x + i * width) * 4 + GlowG] * filter[fy];
2033                                         curColor[2] += map[(x + i * width) * 4 + GlowB] * filter[fy];
2034
2035                                         /* Top */
2036                                         curColor2[0] += map[(x + (height - 1 - i) * width) * 4 + GlowR] * filter[fy];
2037                                         curColor2[1] += map[(x + (height - 1 - i) * width) * 4 + GlowG] * filter[fy];
2038                                         curColor2[2] += map[(x + (height - 1 - i) * width) * 4 + GlowB] * filter[fy];
2039                                 }
2040                                 fy++;
2041                         }
2042                         temp[index + GlowR] = curColor[0];
2043                         temp[index + GlowG] = curColor[1];
2044                         temp[index + GlowB] = curColor[2];
2045                         temp[((x + (height - 1 - y) * width) * 4) + GlowR] = curColor2[0];
2046                         temp[((x + (height - 1 - y) * width) * 4) + GlowG] = curColor2[1];
2047                         temp[((x + (height - 1 - y) * width) * 4) + GlowB] = curColor2[2];
2048                 }
2049         
2050                 /* Do the main body */
2051                 for (y = halfWidth; y < height - halfWidth; y++) {
2052                         index = (x + y * width) * 4;
2053                         fy = 0;
2054                         zero_v3(curColor);
2055                         for (i = y - halfWidth; i < y + halfWidth; i++) {
2056                                 curColor[0] += map[(x + i * width) * 4 + GlowR] * filter[fy];
2057                                 curColor[1] += map[(x + i * width) * 4 + GlowG] * filter[fy];
2058                                 curColor[2] += map[(x + i * width) * 4 + GlowB] * filter[fy];
2059                                 fy++;
2060                         }
2061                         temp[index + GlowR] = curColor[0];
2062                         temp[index + GlowG] = curColor[1];
2063                         temp[index + GlowB] = curColor[2];
2064                 }
2065         }
2066
2067         /* Swap buffers */
2068         swap = temp; temp = map; /* map = swap; */ /* UNUSED */
2069
2070         /* Tidy up       */
2071         MEM_freeN(filter);
2072         MEM_freeN(temp);
2073 }
2074
2075
2076 /*      Adds two bitmaps and puts the results into a third map. */
2077 /*      C must have been previously allocated but it may be A or B. */
2078 /*      We clamp values to 255 to prevent weirdness */
2079 /*=============================== */
2080 static void RVAddBitmaps_byte(unsigned char *a, unsigned char *b, unsigned char *c, int width, int height)
2081 {
2082         int x, y, index;
2083
2084         for (y = 0; y < height; y++) {
2085                 for (x = 0; x < width; x++) {
2086                         index = (x + y * width) * 4;
2087                         c[index + GlowR] = MIN2(255, a[index + GlowR] + b[index + GlowR]);
2088                         c[index + GlowG] = MIN2(255, a[index + GlowG] + b[index + GlowG]);
2089                         c[index + GlowB] = MIN2(255, a[index + GlowB] + b[index + GlowB]);
2090                         c[index + GlowA] = MIN2(255, a[index + GlowA] + b[index + GlowA]);
2091                 }
2092         }
2093 }
2094
2095 static void RVAddBitmaps_float(float *a, float *b, float *c, int width, int height)
2096 {
2097         int x, y, index;
2098
2099         for (y = 0; y < height; y++) {
2100                 for (x = 0; x < width; x++) {
2101                         index = (x + y * width) * 4;
2102                         c[index + GlowR] = MIN2(1.0f, a[index + GlowR] + b[index + GlowR]);
2103                         c[index + GlowG] = MIN2(1.0f, a[index + GlowG] + b[index + GlowG]);
2104                         c[index + GlowB] = MIN2(1.0f, a[index + GlowB] + b[index + GlowB]);
2105                         c[index + GlowA] = MIN2(1.0f, a[index + GlowA] + b[index + GlowA]);
2106                 }
2107         }
2108 }
2109
2110 /* For each pixel whose total luminance exceeds the threshold,
2111  * Multiply it's value by BOOST and add it to the output map
2112  */
2113 static void RVIsolateHighlights_byte(unsigned char *in, unsigned char *out, int width, int height, int threshold,
2114                                      float boost, float clamp)
2115 {
2116         int x, y, index;
2117         int intensity;
2118
2119         for (y = 0; y < height; y++) {
2120                 for (x = 0; x < width; x++) {
2121                         index = (x + y * width) * 4;
2122
2123                         /* Isolate the intensity */
2124                         intensity = (in[index + GlowR] + in[index + GlowG] + in[index + GlowB] - threshold);
2125                         if (intensity > 0) {
2126                                 out[index + GlowR] = MIN2(255 * clamp, (in[index + GlowR] * boost * intensity) / 255);
2127                                 out[index + GlowG] = MIN2(255 * clamp, (in[index + GlowG] * boost * intensity) / 255);
2128                                 out[index + GlowB] = MIN2(255 * clamp, (in[index + GlowB] * boost * intensity) / 255);
2129                                 out[index + GlowA] = MIN2(255 * clamp, (in[index + GlowA] * boost * intensity) / 255);
2130                         }
2131                         else {
2132                                 out[index + GlowR] = 0;
2133                                 out[index + GlowG] = 0;
2134                                 out[index + GlowB] = 0;
2135                                 out[index + GlowA] = 0;
2136                         }
2137                 }
2138         }
2139 }
2140
2141 static void RVIsolateHighlights_float(float *in, float *out, int width, int height, float threshold, float boost, float clamp)
2142 {
2143         int x, y, index;
2144         float intensity;
2145
2146         for (y = 0; y < height; y++) {
2147                 for (x = 0; x < width; x++) {
2148                         index = (x + y * width) * 4;
2149
2150                         /* Isolate the intensity */
2151                         intensity = (in[index + GlowR] + in[index + GlowG] + in[index + GlowB] - threshold);
2152                         if (intensity > 0) {
2153                                 out[index + GlowR] = MIN2(clamp, (in[index + GlowR] * boost * intensity));
2154                                 out[index + GlowG] = MIN2(clamp, (in[index + GlowG] * boost * intensity));
2155                                 out[index + GlowB] = MIN2(clamp, (in[index + GlowB] * boost * intensity));
2156                                 out[index + GlowA] = MIN2(clamp, (in[index + GlowA] * boost * intensity));
2157                         }
2158                         else {
2159                                 out[index + GlowR] = 0;
2160                                 out[index + GlowG] = 0;
2161                                 out[index + GlowB] = 0;
2162                                 out[index + GlowA] = 0;
2163                         }
2164                 }
2165         }
2166 }
2167
2168 static void init_glow_effect(Sequence *seq)
2169 {
2170         GlowVars *glow;
2171
2172         if (seq->effectdata)
2173                 MEM_freeN(seq->effectdata);
2174
2175         seq->effectdata = MEM_callocN(sizeof(struct GlowVars), "glowvars");
2176
2177         glow = (GlowVars *)seq->effectdata;
2178         glow->fMini = 0.25;
2179         glow->fClamp = 1.0;
2180         glow->fBoost = 0.5;
2181         glow->dDist = 3.0;
2182         glow->dQuality = 3;
2183         glow->bNoComp = 0;
2184 }
2185
2186 static int num_inputs_glow(void)
2187 {
2188         return 1;
2189 }
2190
2191 static void free_glow_effect(Sequence *seq)
2192 {
2193         if (seq->effectdata)
2194                 MEM_freeN(seq->effectdata);
2195
2196         seq->effectdata = NULL;
2197 }
2198
2199 static void copy_glow_effect(Sequence *dst, Sequence *src)
2200 {
2201         dst->effectdata = MEM_dupallocN(src->effectdata);
2202 }
2203
2204 static void do_glow_effect_byte(Sequence *seq, int render_size, float facf0, float UNUSED(facf1),  int x, int y,
2205                                 char *rect1, char *UNUSED(rect2), char *out)
2206 {
2207         unsigned char *outbuf = (unsigned char *)out;
2208         unsigned char *inbuf = (unsigned char *)rect1;
2209         GlowVars *glow = (GlowVars *)seq->effectdata;
2210         
2211         RVIsolateHighlights_byte(inbuf, outbuf, x, y, glow->fMini * 765, glow->fBoost * facf0, glow->fClamp);
2212         RVBlurBitmap2_byte(outbuf, x, y, glow->dDist * (render_size / 100.0f), glow->dQuality);
2213         if (!glow->bNoComp)
2214                 RVAddBitmaps_byte(inbuf, outbuf, outbuf, x, y);
2215 }
2216
2217 static void do_glow_effect_float(Sequence *seq, int render_size, float facf0, float UNUSED(facf1),  int x, int y,
2218                                  float *rect1, float *UNUSED(rect2), float *out)
2219 {
2220         float *outbuf = out;
2221         float *inbuf = rect1;
2222         GlowVars *glow = (GlowVars *)seq->effectdata;
2223
2224         RVIsolateHighlights_float(inbuf, outbuf, x, y, glow->fMini * 3.0f, glow->fBoost * facf0, glow->fClamp);
2225         RVBlurBitmap2_float(outbuf, x, y, glow->dDist * (render_size / 100.0f), glow->dQuality);
2226         if (!glow->bNoComp)
2227                 RVAddBitmaps_float(inbuf, outbuf, outbuf, x, y);
2228 }
2229
2230 static ImBuf *do_glow_effect(SeqRenderData context, Sequence *seq, float UNUSED(cfra), float facf0, float facf1,
2231                              ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
2232 {
2233         struct ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
2234
2235         int render_size = 100 * context.rectx / context.scene->r.xsch;
2236
2237         if (out->rect_float) {
2238                 do_glow_effect_float(seq, render_size, facf0, facf1, context.rectx, context.recty,
2239                                      ibuf1->rect_float, ibuf2->rect_float, out->rect_float);
2240         }
2241         else {
2242                 do_glow_effect_byte(seq, render_size, facf0, facf1, context.rectx, context.recty,
2243                                     (char *) ibuf1->rect, (char *) ibuf2->rect, (char *) out->rect);
2244         }
2245
2246         return out;
2247 }
2248
2249 /*********************** Solid color *************************/
2250
2251 static void init_solid_color(Sequence *seq)
2252 {
2253         SolidColorVars *cv;
2254         
2255         if (seq->effectdata)
2256                 MEM_freeN(seq->effectdata);
2257
2258         seq->effectdata = MEM_callocN(sizeof(struct SolidColorVars), "solidcolor");
2259         
2260         cv = (SolidColorVars *)seq->effectdata;
2261         cv->col[0] = cv->col[1] = cv->col[2] = 0.5;
2262 }
2263
2264 static int num_inputs_color(void)
2265 {
2266         return 0;
2267 }
2268
2269 static void free_solid_color(Sequence *seq)
2270 {
2271         if (seq->effectdata)
2272                 MEM_freeN(seq->effectdata);
2273
2274         seq->effectdata = NULL;
2275 }
2276
2277 static void copy_solid_color(Sequence *dst, Sequence *src)
2278 {
2279         dst->effectdata = MEM_dupallocN(src->effectdata);
2280 }
2281
2282 static int early_out_color(Sequence *UNUSED(seq), float UNUSED(facf0), float UNUSED(facf1))
2283 {
2284         return -1;
2285 }
2286
2287 static ImBuf *do_solid_color(SeqRenderData context, Sequence *seq, float UNUSED(cfra), float facf0, float facf1,
2288                              ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
2289 {
2290         ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
2291
2292         SolidColorVars *cv = (SolidColorVars *)seq->effectdata;
2293
2294         unsigned char *rect;
2295         float *rect_float;
2296         int x; /*= context.rectx;*/ /*UNUSED*/
2297         int y; /*= context.recty;*/ /*UNUSED*/
2298
2299         if (out->rect) {
2300                 unsigned char col0[3];
2301                 unsigned char col1[3];
2302
2303                 col0[0] = facf0 * cv->col[0] * 255;
2304                 col0[1] = facf0 * cv->col[1] * 255;
2305                 col0[2] = facf0 * cv->col[2] * 255;
2306
2307                 col1[0] = facf1 * cv->col[0] * 255;
2308                 col1[1] = facf1 * cv->col[1] * 255;
2309                 col1[2] = facf1 * cv->col[2] * 255;
2310
2311                 rect = (unsigned char *)out->rect;
2312                 
2313                 for (y = 0; y < out->y; y++) {
2314                         for (x = 0; x < out->x; x++, rect += 4) {
2315                                 rect[0] = col0[0];
2316                                 rect[1] = col0[1];
2317                                 rect[2] = col0[2];
2318                                 rect[3] = 255;
2319                         }
2320                         y++;
2321                         if (y < out->y) {
2322                                 for (x = 0; x < out->x; x++, rect += 4) {
2323                                         rect[0] = col1[0];
2324                                         rect[1] = col1[1];
2325                                         rect[2] = col1[2];
2326                                         rect[3] = 255;
2327                                 }       
2328                         }
2329                 }
2330
2331         }
2332         else if (out->rect_float) {
2333                 float col0[3];
2334                 float col1[3];
2335
2336                 col0[0] = facf0 * cv->col[0];
2337                 col0[1] = facf0 * cv->col[1];
2338                 col0[2] = facf0 * cv->col[2];
2339
2340                 col1[0] = facf1 * cv->col[0];
2341                 col1[1] = facf1 * cv->col[1];
2342                 col1[2] = facf1 * cv->col[2];
2343
2344                 rect_float = out->rect_float;
2345                 
2346                 for (y = 0; y < out->y; y++) {
2347                         for (x = 0; x < out->x; x++, rect_float += 4) {
2348                                 rect_float[0] = col0[0];
2349                                 rect_float[1] = col0[1];
2350                                 rect_float[2] = col0[2];
2351                                 rect_float[3] = 1.0;
2352                         }
2353                         y++;
2354                         if (y < out->y) {
2355                                 for (x = 0; x < out->x; x++, rect_float += 4) {
2356                                         rect_float[0] = col1[0];
2357                                         rect_float[1] = col1[1];
2358                                         rect_float[2] = col1[2];
2359                                         rect_float[3] = 1.0;
2360                                 }
2361                         }
2362                 }
2363         }
2364         return out;
2365 }
2366
2367 /*********************** Mulitcam *************************/
2368
2369 /* no effect inputs for multicam, we use give_ibuf_seq */
2370 static int num_inputs_multicam(void)
2371 {
2372         return 0;
2373 }
2374
2375 static int early_out_multicam(struct Sequence *UNUSED(seq), float UNUSED(facf0), float UNUSED(facf1))
2376 {
2377         return -1;
2378 }
2379
2380 static ImBuf *do_multicam(SeqRenderData context, Sequence *seq, float cfra, float UNUSED(facf0), float UNUSED(facf1),
2381                           ImBuf *UNUSED(ibuf1), ImBuf *UNUSED(ibuf2), ImBuf *UNUSED(ibuf3))
2382 {
2383         ImBuf *i;
2384         ImBuf *out;
2385         Editing *ed;
2386         ListBase *seqbasep;
2387
2388         if (seq->multicam_source == 0 || seq->multicam_source >= seq->machine) {
2389                 return NULL;
2390         }
2391
2392         ed = context.scene->ed;
2393         if (!ed) {
2394                 return NULL;
2395         }
2396         seqbasep = seq_seqbase(&ed->seqbase, seq);
2397         if (!seqbasep) {
2398                 return NULL;
2399         }
2400
2401         i = give_ibuf_seqbase(context, cfra, seq->multicam_source, seqbasep);
2402         if (!i) {
2403                 return NULL;
2404         }
2405
2406         if (input_have_to_preprocess(context, seq, cfra)) {
2407                 out = IMB_dupImBuf(i);
2408                 IMB_freeImBuf(i);
2409         }
2410         else {
2411                 out = i;
2412         }
2413         
2414         return out;
2415 }
2416
2417 /*********************** Adjustment *************************/
2418
2419 /* no effect inputs for adjustment, we use give_ibuf_seq */
2420 static int num_inputs_adjustment(void)
2421 {
2422         return 0;
2423 }
2424
2425 static int early_out_adjustment(Sequence *UNUSED(seq), float UNUSED(facf0), float UNUSED(facf1))
2426 {
2427         return -1;
2428 }
2429
2430 static ImBuf *do_adjustment_impl(SeqRenderData context, Sequence *seq, float cfra)
2431 {
2432         Editing *ed;
2433         ListBase *seqbasep;
2434         ImBuf *i = NULL;
2435
2436         ed = context.scene->ed;
2437
2438         seqbasep = seq_seqbase(&ed->seqbase, seq);
2439
2440         if (seq->machine > 0) {
2441                 i = give_ibuf_seqbase(context, cfra, seq->machine - 1, seqbasep);
2442         }
2443
2444         /* found nothing? so let's work the way up the metastrip stack, so
2445          * that it is possible to group a bunch of adjustment strips into
2446          * a metastrip and have that work on everything below the metastrip
2447          */
2448
2449         if (!i) {
2450                 Sequence *meta;
2451
2452                 meta = seq_metastrip(&ed->seqbase, NULL, seq);
2453
2454                 if (meta) {
2455                         i = do_adjustment_impl(context, meta, cfra);
2456                 }
2457         }
2458
2459         return i;
2460 }
2461
2462 static ImBuf *do_adjustment(SeqRenderData context, Sequence *seq, float cfra, float UNUSED(facf0), float UNUSED(facf1),
2463                             ImBuf *UNUSED(ibuf1), ImBuf *UNUSED(ibuf2), ImBuf *UNUSED(ibuf3))
2464 {
2465         ImBuf *i = NULL;
2466         ImBuf *out;
2467         Editing *ed;
2468
2469         ed = context.scene->ed;
2470
2471         if (!ed) {
2472                 return NULL;
2473         }
2474
2475         i = do_adjustment_impl(context, seq, cfra);
2476
2477         if (input_have_to_preprocess(context, seq, cfra)) {
2478                 out = IMB_dupImBuf(i);
2479                 IMB_freeImBuf(i);
2480         }
2481         else {
2482                 out = i;
2483         }
2484         
2485         return out;
2486 }
2487
2488 /*********************** Speed *************************/
2489
2490 static void init_speed_effect(Sequence *seq)
2491 {
2492         SpeedControlVars *v;
2493
2494         if (seq->effectdata)
2495                 MEM_freeN(seq->effectdata);
2496
2497         seq->effectdata = MEM_callocN(sizeof(struct SpeedControlVars), "speedcontrolvars");
2498
2499         v = (SpeedControlVars *)seq->effectdata;
2500         v->globalSpeed = 1.0;
2501         v->frameMap = NULL;
2502         v->flags |= SEQ_SPEED_INTEGRATE; /* should be default behavior */
2503         v->length = 0;
2504 }
2505
2506 static void load_speed_effect(Sequence *seq)
2507 {
2508         SpeedControlVars *v = (SpeedControlVars *)seq->effectdata;
2509
2510         v->frameMap = NULL;
2511         v->length = 0;
2512 }
2513
2514 static int num_inputs_speed(void)
2515 {
2516         return 1;
2517 }
2518
2519 static void free_speed_effect(Sequence *seq)
2520 {
2521         SpeedControlVars *v = (SpeedControlVars *)seq->effectdata;
2522         if (v->frameMap)
2523                 MEM_freeN(v->frameMap);
2524         if (seq->effectdata)
2525                 MEM_freeN(seq->effectdata);
2526         seq->effectdata = NULL;
2527 }
2528
2529 static void copy_speed_effect(Sequence *dst, Sequence *src)
2530 {
2531         SpeedControlVars *v;
2532         dst->effectdata = MEM_dupallocN(src->effectdata);
2533         v = (SpeedControlVars *)dst->effectdata;
2534         v->frameMap = NULL;
2535         v->length = 0;
2536 }
2537
2538 static int early_out_speed(Sequence *UNUSED(seq), float UNUSED(facf0), float UNUSED(facf1))
2539 {
2540         return 1;
2541 }
2542
2543 static void store_icu_yrange_speed(Sequence *seq, short UNUSED(adrcode), float *ymin, float *ymax)
2544 {
2545         SpeedControlVars *v = (SpeedControlVars *)seq->effectdata;
2546
2547         /* if not already done, load / initialize data */
2548         get_sequence_effect(seq);
2549
2550         if ((v->flags & SEQ_SPEED_INTEGRATE) != 0) {
2551                 *ymin = -100.0;
2552                 *ymax = 100.0;
2553         }
2554         else {
2555                 if (v->flags & SEQ_SPEED_COMPRESS_IPO_Y) {
2556                         *ymin = 0.0;
2557                         *ymax = 1.0;
2558                 }
2559                 else {
2560                         *ymin = 0.0;
2561                         *ymax = seq->len;
2562                 }
2563         }       
2564 }
2565
2566 void sequence_effect_speed_rebuild_map(Scene *scene, Sequence *seq, int force)
2567 {
2568         int cfra;
2569         float fallback_fac = 1.0f;
2570         SpeedControlVars *v = (SpeedControlVars *) seq->effectdata;
2571         FCurve *fcu = NULL;
2572         int flags = v->flags;
2573
2574         /* if not already done, load / initialize data */
2575         get_sequence_effect(seq);
2576
2577         if ((force == FALSE) &&
2578             (seq->len == v->length) &&
2579             (v->frameMap != NULL))
2580         {
2581                 return;
2582         }
2583         if ((seq->seq1 == NULL) || (seq->len < 1)) {
2584                 /* make coverity happy and check for (CID 598) input strip ... */
2585                 return;
2586         }
2587
2588         /* XXX - new in 2.5x. should we use the animation system this way?
2589          * The fcurve is needed because many frames need evaluating at once - campbell */
2590         fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "speed_factor", 0, NULL);
2591
2592
2593         if (!v->frameMap || v->length != seq->len) {
2594                 if (v->frameMap) MEM_freeN(v->frameMap);
2595
2596                 v->length = seq->len;
2597
2598                 v->frameMap = MEM_callocN(sizeof(float) * v->length, "speedcontrol frameMap");
2599         }
2600
2601         fallback_fac = 1.0;
2602
2603         if (seq->flag & SEQ_USE_EFFECT_DEFAULT_FADE) {
2604                 if ((seq->seq1->enddisp != seq->seq1->start) &&
2605                     (seq->seq1->len != 0))
2606                 {
2607                         fallback_fac = (float) seq->seq1->len / (float) (seq->seq1->enddisp - seq->seq1->start);
2608                         flags = SEQ_SPEED_INTEGRATE;
2609                         fcu = NULL;
2610                 }
2611         }
2612         else {
2613                 /* if there is no fcurve, use value as simple multiplier */
2614                 if (!fcu) {
2615                         fallback_fac = seq->speed_fader; /* same as speed_factor in rna*/
2616                 }
2617         }
2618
2619         if (flags & SEQ_SPEED_INTEGRATE) {
2620                 float cursor = 0;
2621                 float facf;
2622
2623                 v->frameMap[0] = 0;
2624                 v->lastValidFrame = 0;
2625
2626                 for (cfra = 1; cfra < v->length; cfra++) {
2627                         if (fcu) {
2628                                 facf = evaluate_fcurve(fcu, seq->startdisp + cfra);
2629                         }
2630                         else {
2631                                 facf = fallback_fac;
2632                         }
2633                         facf *= v->globalSpeed;
2634
2635                         cursor += facf;
2636
2637                         if (cursor >= seq->seq1->len) {
2638                                 v->frameMap[cfra] = seq->seq1->len - 1;
2639                         }
2640                         else {
2641                                 v->frameMap[cfra] = cursor;
2642                                 v->lastValidFrame = cfra;
2643                         }
2644                 }
2645         }
2646         else {
2647                 float facf;
2648
2649                 v->lastValidFrame = 0;
2650                 for (cfra = 0; cfra < v->length; cfra++) {
2651
2652                         if (fcu) {
2653                                 facf = evaluate_fcurve(fcu, seq->startdisp + cfra);
2654                         }
2655                         else {
2656                                 facf = fallback_fac;
2657                         }
2658
2659                         if (flags & SEQ_SPEED_COMPRESS_IPO_Y) {
2660                                 facf *= seq->seq1->len;
2661                         }
2662                         facf *= v->globalSpeed;
2663                         
2664                         if (facf >= seq->seq1->len) {
2665                                 facf = seq->seq1->len - 1;
2666                         }
2667                         else {
2668                                 v->lastValidFrame = cfra;
2669                         }
2670                         v->frameMap[cfra] = facf;
2671                 }
2672         }
2673 }
2674
2675 /*********************** sequence effect factory *************************/
2676
2677 static void init_noop(Sequence *UNUSED(seq))
2678 {
2679
2680 }
2681
2682 static void load_noop(Sequence *UNUSED(seq))
2683 {
2684
2685 }
2686
2687 static void free_noop(Sequence *UNUSED(seq))
2688 {
2689
2690 }
2691
2692 static int num_inputs_default(void)
2693 {
2694         return 2;
2695 }
2696
2697 static int early_out_noop(Sequence *UNUSED(seq), float UNUSED(facf0), float UNUSED(facf1))
2698 {
2699         return 0;
2700 }
2701
2702 static int early_out_fade(Sequence *UNUSED(seq), float facf0, float facf1)
2703 {
2704         if (facf0 == 0.0f && facf1 == 0.0f) {
2705                 return 1;
2706         }
2707         else if (facf0 == 1.0f && facf1 == 1.0f) {
2708                 return 2;
2709         }
2710         return 0;
2711 }
2712
2713 static int early_out_mul_input2(Sequence *UNUSED(seq), float facf0, float facf1)
2714 {
2715         if (facf0 == 0.0f && facf1 == 0.0f) {
2716                 return 1;
2717         }
2718         return 0;
2719 }
2720
2721 static void store_icu_yrange_noop(Sequence *UNUSED(seq), short UNUSED(adrcode), float *UNUSED(ymin), float *UNUSED(ymax))
2722 {
2723         /* defaults are fine */
2724 }
2725
2726 static void get_default_fac_noop(Sequence *UNUSED(seq), float UNUSED(cfra), float *facf0, float *facf1)
2727 {
2728         *facf0 = *facf1 = 1.0;
2729 }
2730
2731 static void get_default_fac_fade(Sequence *seq, float cfra, float *facf0, float *facf1)
2732 {
2733         *facf0 = (float)(cfra - seq->startdisp);
2734         *facf1 = (float)(*facf0 + 0.5f);
2735         *facf0 /= seq->len;
2736         *facf1 /= seq->len;
2737 }
2738
2739 static ImBuf *do_overdrop_effect(SeqRenderData context, Sequence *UNUSED(seq), float UNUSED(cfra), float facf0, float facf1,
2740                                  ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
2741 {
2742         ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
2743         int x = context.rectx;
2744         int y = context.recty;
2745
2746         if (out->rect_float) {
2747                 do_drop_effect_float(facf0, facf1, x, y, ibuf1->rect_float, ibuf2->rect_float, out->rect_float);
2748                 do_alphaover_effect_float(facf0, facf1, x, y, ibuf1->rect_float, ibuf2->rect_float, out->rect_float);
2749         }
2750         else {
2751                 do_drop_effect_byte(facf0, facf1, x, y, (char *) ibuf1->rect, (char *) ibuf2->rect, (char *) out->rect);
2752                 do_alphaover_effect_byte(facf0, facf1, x, y, (char *) ibuf1->rect, (char *) ibuf2->rect, (char *) out->rect);
2753         }
2754
2755         return out;
2756 }
2757
2758 static struct SeqEffectHandle get_sequence_effect_impl(int seq_type)
2759 {
2760         struct SeqEffectHandle rval;
2761         int sequence_type = seq_type;
2762
2763         rval.init = init_noop;
2764         rval.num_inputs = num_inputs_default;
2765         rval.load = load_noop;
2766         rval.free = free_noop;
2767         rval.early_out = early_out_noop;
2768         rval.get_default_fac = get_default_fac_noop;
2769         rval.store_icu_yrange = store_icu_yrange_noop;
2770         rval.execute = NULL;
2771         rval.copy = NULL;
2772
2773         switch (sequence_type) {
2774                 case SEQ_TYPE_CROSS:
2775                         rval.execute = do_cross_effect;
2776                         rval.early_out = early_out_fade;
2777                         rval.get_default_fac = get_default_fac_fade;
2778                         break;
2779                 case SEQ_TYPE_GAMCROSS:
2780                         rval.init = init_gammacross;
2781                         rval.load = load_gammacross;
2782                         rval.free = free_gammacross;
2783                         rval.early_out = early_out_fade;
2784                         rval.get_default_fac = get_default_fac_fade;
2785                         rval.execute = do_gammacross_effect;
2786                         break;
2787                 case SEQ_TYPE_ADD:
2788                         rval.execute = do_add_effect;
2789                         rval.early_out = early_out_mul_input2;
2790                         break;
2791                 case SEQ_TYPE_SUB:
2792                         rval.execute = do_sub_effect;
2793                         rval.early_out = early_out_mul_input2;
2794                         break;
2795                 case SEQ_TYPE_MUL:
2796                         rval.execute = do_mul_effect;
2797                         rval.early_out = early_out_mul_input2;
2798                         break;
2799                 case SEQ_TYPE_ALPHAOVER:
2800                         rval.init = init_alpha_over_or_under;
2801                         rval.execute = do_alphaover_effect;
2802                         break;
2803                 case SEQ_TYPE_OVERDROP:
2804                         rval.execute = do_overdrop_effect;
2805                         break;
2806                 case SEQ_TYPE_ALPHAUNDER:
2807                         rval.init = init_alpha_over_or_under;
2808                         rval.execute = do_alphaunder_effect;
2809                         break;
2810                 case SEQ_TYPE_WIPE:
2811                         rval.init = init_wipe_effect;
2812                         rval.num_inputs = num_inputs_wipe;
2813                         rval.free = free_wipe_effect;
2814                         rval.copy = copy_wipe_effect;
2815                         rval.early_out = early_out_fade;
2816                         rval.get_default_fac = get_default_fac_fade;
2817                         rval.execute = do_wipe_effect;
2818                         break;
2819                 case SEQ_TYPE_GLOW:
2820                         rval.init = init_glow_effect;
2821                         rval.num_inputs = num_inputs_glow;
2822                         rval.free = free_glow_effect;
2823                         rval.copy = copy_glow_effect;
2824                         rval.execute = do_glow_effect;
2825                         break;
2826                 case SEQ_TYPE_TRANSFORM:
2827                         rval.init = init_transform_effect;
2828                         rval.num_inputs = num_inputs_transform;
2829                         rval.free = free_transform_effect;
2830                         rval.copy = copy_transform_effect;
2831                         rval.execute = do_transform_effect;
2832                         break;
2833                 case SEQ_TYPE_SPEED:
2834                         rval.init = init_speed_effect;
2835                         rval.num_inputs = num_inputs_speed;
2836                         rval.load = load_speed_effect;
2837                         rval.free = free_speed_effect;
2838                         rval.copy = copy_speed_effect;
2839                         rval.execute = do_cross_effect;
2840                         rval.early_out = early_out_speed;
2841                         rval.store_icu_yrange = store_icu_yrange_speed;
2842                         break;
2843                 case SEQ_TYPE_COLOR:
2844                         rval.init = init_solid_color;
2845                         rval.num_inputs = num_inputs_color;
2846                         rval.early_out = early_out_color;
2847                         rval.free = free_solid_color;
2848                         rval.copy = copy_solid_color;
2849                         rval.execute = do_solid_color;
2850                         break;
2851                 case SEQ_TYPE_MULTICAM:
2852                         rval.num_inputs = num_inputs_multicam;
2853                         rval.early_out = early_out_multicam;
2854                         rval.execute = do_multicam;
2855                         break;
2856                 case SEQ_TYPE_ADJUSTMENT:
2857                         rval.num_inputs = num_inputs_adjustment;
2858                         rval.early_out = early_out_adjustment;
2859                         rval.execute = do_adjustment;
2860                         break;
2861         }
2862
2863         return rval;
2864 }
2865
2866
2867 struct SeqEffectHandle get_sequence_effect(Sequence *seq)
2868 {
2869         struct SeqEffectHandle rval = {NULL};
2870
2871         if (seq->type & SEQ_TYPE_EFFECT) {
2872                 rval = get_sequence_effect_impl(seq->type);
2873                 if ((seq->flag & SEQ_EFFECT_NOT_LOADED) != 0) {
2874                         rval.load(seq);
2875                         seq->flag &= ~SEQ_EFFECT_NOT_LOADED;
2876                 }
2877         }
2878
2879         return rval;
2880 }
2881
2882 struct SeqEffectHandle get_sequence_blend(Sequence *seq)
2883 {
2884         struct SeqEffectHandle rval = {NULL};
2885
2886         if (seq->blend_mode != 0) {
2887                 rval = get_sequence_effect_impl(seq->blend_mode);
2888                 if ((seq->flag & SEQ_EFFECT_NOT_LOADED) != 0) {
2889                         rval.load(seq);
2890                         seq->flag &= ~SEQ_EFFECT_NOT_LOADED;
2891                 }
2892         }
2893
2894         return rval;
2895 }
2896
2897 int get_sequence_effect_num_inputs(int seq_type)
2898 {
2899         struct SeqEffectHandle rval = get_sequence_effect_impl(seq_type);
2900
2901         int cnt = rval.num_inputs();
2902         if (rval.execute) {
2903                 return cnt;
2904         }
2905         return 0;
2906 }