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