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