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