Small tweak to the default Glow effect values, so there's a
[blender-staging.git] / source / blender / src / sequence.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <math.h>
35 #include <stdlib.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #ifdef WIN32
42 #include "BLI_winstuff.h"
43 #endif
44
45 #include "MEM_guardedalloc.h"
46 #include "PIL_dynlib.h"
47
48 #include "BLI_blenlib.h"
49 #include "BLI_arithb.h"
50
51 #include "IMB_imbuf_types.h"
52 #include "IMB_imbuf.h"
53
54 #include "DNA_ipo_types.h"
55 #include "DNA_sequence_types.h"
56 #include "DNA_view3d_types.h"
57
58 #include "BKE_utildefines.h"
59 #include "BKE_plugin_types.h"
60 #include "BKE_global.h"
61 #include "BKE_texture.h"
62 #include "BKE_image.h"
63 #include "BKE_main.h"
64 #include "BKE_scene.h"
65 #include "BKE_ipo.h"
66
67 #include "BIF_screen.h"
68 #include "BIF_interface.h"
69 #include "BIF_toolbox.h"
70 #include "BSE_filesel.h"
71 #include "BSE_headerbuttons.h"
72 #include "BSE_sequence.h"
73
74 #include "blendef.h"
75 #include "render.h"
76
77 Sequence *seq_arr[MAXSEQ+1];
78 int seqrectx, seqrecty;
79
80 /* support for plugin sequences: */
81
82 void open_plugin_seq(PluginSeq *pis, char *seqname)
83 {
84         int (*version)();
85         char *cp;
86
87         /* to be sure: (is tested for) */
88         pis->doit= 0;
89         pis->pname= 0;
90         pis->varstr= 0;
91         pis->cfra= 0;
92         pis->version= 0;
93
94         /* clear the error list */
95         PIL_dynlib_get_error_as_string(NULL);
96
97         /* if(pis->handle) PIL_dynlib_close(pis->handle); */
98         /* pis->handle= 0; */
99
100         /* open the needed object */
101         pis->handle= PIL_dynlib_open(pis->name);
102         if(test_dlerr(pis->name, pis->name)) return;
103
104         if (pis->handle != 0) {
105                 /* find the address of the version function */
106                 version= (int (*)())PIL_dynlib_find_symbol(pis->handle, "plugin_seq_getversion");
107                 if (test_dlerr(pis->name, "plugin_seq_getversion")) return;
108
109                 if (version != 0) {
110                         pis->version= version();
111                         if (pis->version==2 || pis->version==3) {
112                                 int (*info_func)(PluginInfo *);
113                                 PluginInfo *info= (PluginInfo*) MEM_mallocN(sizeof(PluginInfo), "plugin_info");;
114
115                                 info_func= (int (*)(PluginInfo *))PIL_dynlib_find_symbol(pis->handle, "plugin_getinfo");
116
117                                 if(info_func == NULL) error("No info func");
118                                 else {
119                                         info_func(info);
120
121                                         pis->pname= info->name;
122                                         pis->vars= info->nvars;
123                                         pis->cfra= info->cfra;
124
125                                         pis->varstr= info->varstr;
126
127                                         pis->doit= (void(*)(void))info->seq_doit;
128                                         if (info->init)
129                                                 info->init();
130                                 }
131                                 MEM_freeN(info);
132
133                                 cp= PIL_dynlib_find_symbol(pis->handle, "seqname");
134                                 if(cp) strcpy(cp, seqname);
135                         } else {
136                                 printf ("Plugin returned unrecognized version number\n");
137                                 return;
138                         }
139                 }
140         }
141 }
142
143 PluginSeq *add_plugin_seq(char *str, char *seqname)
144 {
145         PluginSeq *pis;
146         VarStruct *varstr;
147         int a;
148
149         pis= MEM_callocN(sizeof(PluginSeq), "PluginSeq");
150
151         strcpy(pis->name, str);
152         open_plugin_seq(pis, seqname);
153
154         if(pis->doit==0) {
155                 if(pis->handle==0) error("no plugin: %s", str);
156                 else error("in plugin: %s", str);
157                 MEM_freeN(pis);
158                 return 0;
159         }
160
161         /* default values */
162         varstr= pis->varstr;
163         for(a=0; a<pis->vars; a++, varstr++) {
164                 if( (varstr->type & FLO)==FLO)
165                         pis->data[a]= varstr->def;
166                 else if( (varstr->type & INT)==INT)
167                         *((int *)(pis->data+a))= (int) varstr->def;
168         }
169
170         return pis;
171 }
172
173 void free_plugin_seq(PluginSeq *pis)
174 {
175
176         if(pis==0) return;
177
178         /* no PIL_dynlib_close: same plugin can be opened multiple times with 1 handle */
179         MEM_freeN(pis);
180 }
181
182 /* ***************** END PLUGIN ************************ */
183
184 void free_stripdata(int len, StripElem *se)
185 {
186         StripElem *seo;
187         int a;
188
189         seo= se;
190
191         for(a=0; a<len; a++, se++) {
192                 if(se->ibuf && se->ok!=2) IMB_freeImBuf(se->ibuf);
193         }
194
195         MEM_freeN(seo);
196
197 }
198
199 void free_strip(Strip *strip)
200 {
201         strip->us--;
202         if(strip->us>0) return;
203         if(strip->us<0) {
204                 printf("error: negative users in strip\n");
205                 return;
206         }
207
208         if(strip->stripdata) {
209                 free_stripdata(strip->len, strip->stripdata);
210         }
211         MEM_freeN(strip);
212 }
213
214 void new_stripdata(Sequence *seq)
215 {
216
217         if(seq->strip) {
218                 if(seq->strip->stripdata) free_stripdata(seq->strip->len, seq->strip->stripdata);
219                 seq->strip->stripdata= 0;
220                 seq->strip->len= seq->len;
221                 if(seq->len>0) seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelems");
222         }
223 }
224
225 void free_sequence(Sequence *seq)
226 {
227         extern Sequence *last_seq;
228
229         if(seq->strip) free_strip(seq->strip);
230         if(seq->effectdata) MEM_freeN(seq->effectdata);
231
232         if(seq->anim) IMB_free_anim(seq->anim);
233
234         free_plugin_seq(seq->plugin);
235
236         if(seq==last_seq) last_seq= 0;
237
238         MEM_freeN(seq);
239 }
240
241 void do_seq_count(ListBase *seqbase, int *totseq)
242 {
243         Sequence *seq;
244
245         seq= seqbase->first;
246         while(seq) {
247                 (*totseq)++;
248                 if(seq->seqbase.first) do_seq_count(&seq->seqbase, totseq);
249                 seq= seq->next;
250         }
251 }
252
253 void do_build_seqar(ListBase *seqbase, Sequence ***seqar, int depth)
254 {
255         Sequence *seq;
256
257         seq= seqbase->first;
258         while(seq) {
259                 seq->depth= depth;
260                 if(seq->seqbase.first) do_build_seqar(&seq->seqbase, seqar, depth+1);
261                 **seqar= seq;
262                 (*seqar)++;
263                 seq= seq->next;
264         }
265 }
266
267 void build_seqar(ListBase *seqbase, Sequence  ***seqar, int *totseq)
268 {
269         Sequence **tseqar;
270
271         *totseq= 0;
272         do_seq_count(seqbase, totseq);
273
274         if(*totseq==0) {
275                 *seqar= 0;
276                 return;
277         }
278         *seqar= MEM_mallocN(sizeof(void *)* *totseq, "seqar");
279         tseqar= *seqar;
280
281         do_build_seqar(seqbase, seqar, 0);
282         *seqar= tseqar;
283 }
284
285 void free_editing(Editing *ed)
286 {
287         MetaStack *ms;
288         Sequence *seq;
289
290         if(ed==0) return;
291
292         WHILE_SEQ(&ed->seqbase) {
293                 free_sequence(seq);
294         }
295         END_SEQ
296
297         while( (ms= ed->metastack.first) ) {
298                 BLI_remlink(&ed->metastack, ms);
299                 MEM_freeN(ms);
300         }
301
302         MEM_freeN(ed);
303 }
304
305 void calc_sequence(Sequence *seq)
306 {
307         Sequence *seqm;
308         int min, max;
309
310         /* check all metas recursively */
311         seqm= seq->seqbase.first;
312         while(seqm) {
313                 if(seqm->seqbase.first) calc_sequence(seqm);
314                 seqm= seqm->next;
315         }
316
317         /* effects and meta: automatic start and end */
318
319         if(seq->type & SEQ_EFFECT) {
320                 /* pointers */
321                 if(seq->seq2==0) seq->seq2= seq->seq1;
322                 if(seq->seq3==0) seq->seq3= seq->seq1;
323
324                 /* effecten go from seq1 -> seq2: test */
325
326                 /* we take the largest start and smallest end */
327
328                 // seq->start= seq->startdisp= MAX2(seq->seq1->startdisp, seq->seq2->startdisp);
329                 // seq->enddisp= MIN2(seq->seq1->enddisp, seq->seq2->enddisp);
330
331                 seq->start= seq->startdisp= MAX3(seq->seq1->startdisp, seq->seq2->startdisp, seq->seq3->startdisp);
332                 seq->enddisp= MIN3(seq->seq1->enddisp, seq->seq2->enddisp, seq->seq3->enddisp);
333                 seq->len= seq->enddisp - seq->startdisp;
334
335                 if(seq->strip && seq->len!=seq->strip->len) {
336                         new_stripdata(seq);
337                 }
338
339         }
340         else {
341                 if(seq->type==SEQ_META) {
342                         seqm= seq->seqbase.first;
343                         if(seqm) {
344                                 min= 1000000;
345                                 max= -1000000;
346                                 while(seqm) {
347                                         if(seqm->startdisp < min) min= seqm->startdisp;
348                                         if(seqm->enddisp > max) max= seqm->enddisp;
349                                         seqm= seqm->next;
350                                 }
351                                 seq->start= min;
352                                 seq->len= max-min;
353
354                                 if(seq->strip && seq->len!=seq->strip->len) {
355                                         new_stripdata(seq);
356                                 }
357                         }
358                 }
359
360
361                 if(seq->startofs && seq->startstill) seq->startstill= 0;
362                 if(seq->endofs && seq->endstill) seq->endstill= 0;
363
364                 seq->startdisp= seq->start + seq->startofs - seq->startstill;
365                 seq->enddisp= seq->start+seq->len - seq->endofs + seq->endstill;
366
367                 seq->handsize= 10.0;    /* 10 frames */
368                 if( seq->enddisp-seq->startdisp < 20 ) {
369                         seq->handsize= (float)(0.5*(seq->enddisp-seq->startdisp));
370                 }
371                 else if(seq->enddisp-seq->startdisp > 250) {
372                         seq->handsize= (float)((seq->enddisp-seq->startdisp)/25);
373                 }
374         }
375 }
376
377 void sort_seq()
378 {
379         /* all strips together per kind, and in order of y location ("machine") */
380         ListBase seqbase, effbase;
381         Editing *ed;
382         Sequence *seq, *seqt;
383
384         ed= G.scene->ed;
385         if(ed==0) return;
386
387         seqbase.first= seqbase.last= 0;
388         effbase.first= effbase.last= 0;
389
390         while( (seq= ed->seqbasep->first) ) {
391                 BLI_remlink(ed->seqbasep, seq);
392
393                 if(seq->type & SEQ_EFFECT) {
394                         seqt= effbase.first;
395                         while(seqt) {
396                                 if(seqt->machine>=seq->machine) {
397                                         BLI_insertlinkbefore(&effbase, seqt, seq);
398                                         break;
399                                 }
400                                 seqt= seqt->next;
401                         }
402                         if(seqt==0) BLI_addtail(&effbase, seq);
403                 }
404                 else {
405                         seqt= seqbase.first;
406                         while(seqt) {
407                                 if(seqt->machine>=seq->machine) {
408                                         BLI_insertlinkbefore(&seqbase, seqt, seq);
409                                         break;
410                                 }
411                                 seqt= seqt->next;
412                         }
413                         if(seqt==0) BLI_addtail(&seqbase, seq);
414                 }
415         }
416
417         addlisttolist(&seqbase, &effbase);
418         *(ed->seqbasep)= seqbase;
419 }
420
421
422 void clear_scene_in_allseqs(Scene *sce)
423 {
424         Scene *sce1;
425         Editing *ed;
426         Sequence *seq;
427
428         /* when a scene is deleted: test all seqs */
429
430         sce1= G.main->scene.first;
431         while(sce1) {
432                 if(sce1!=sce && sce1->ed) {
433                         ed= sce1->ed;
434
435                         WHILE_SEQ(&ed->seqbase) {
436
437                                 if(seq->scene==sce) seq->scene= 0;
438
439                         }
440                         END_SEQ
441                 }
442
443                 sce1= sce1->id.next;
444         }
445 }
446
447 /* ***************** DO THE SEQUENCE ***************** */
448
449 void do_alphaover_effect(float facf0, float facf1, int x, int y, unsigned int *rect1, unsigned int *rect2, unsigned int *out)
450 {
451         int fac2, mfac, fac, fac4;
452         int xo, tempc;
453         char *rt1, *rt2, *rt;
454
455         xo= x;
456         rt1= (char *)rect1;
457         rt2= (char *)rect2;
458         rt= (char *)out;
459
460         fac2= (int)(256.0*facf0);
461         fac4= (int)(256.0*facf1);
462
463         while(y--) {
464
465                 x= xo;
466                 while(x--) {
467
468                         /* rt = rt1 over rt2  (alpha from rt1) */
469
470                         fac= fac2;
471                         mfac= 256 - ( (fac2*rt1[3])>>8 );
472
473                         if(fac==0) *( (unsigned int *)rt) = *( (unsigned int *)rt2);
474                         else if(mfac==0) *( (unsigned int *)rt) = *( (unsigned int *)rt1);
475                         else {
476                                 tempc= ( fac*rt1[0] + mfac*rt2[0])>>8;
477                                 if(tempc>255) rt[0]= 255; else rt[0]= tempc;
478                                 tempc= ( fac*rt1[1] + mfac*rt2[1])>>8;
479                                 if(tempc>255) rt[1]= 255; else rt[1]= tempc;
480                                 tempc= ( fac*rt1[2] + mfac*rt2[2])>>8;
481                                 if(tempc>255) rt[2]= 255; else rt[2]= tempc;
482                                 tempc= ( fac*rt1[3] + mfac*rt2[3])>>8;
483                                 if(tempc>255) rt[3]= 255; else rt[3]= tempc;
484                         }
485                         rt1+= 4; rt2+= 4; rt+= 4;
486                 }
487
488                 if(y==0) break;
489                 y--;
490
491                 x= xo;
492                 while(x--) {
493
494                         fac= fac4;
495                         mfac= 256 - ( (fac4*rt1[3])>>8 );
496
497                         if(fac==0) *( (unsigned int *)rt) = *( (unsigned int *)rt2);
498                         else if(mfac==0) *( (unsigned int *)rt) = *( (unsigned int *)rt1);
499                         else {
500                                 tempc= ( fac*rt1[0] + mfac*rt2[0])>>8;
501                                 if(tempc>255) rt[0]= 255; else rt[0]= tempc;
502                                 tempc= ( fac*rt1[1] + mfac*rt2[1])>>8;
503                                 if(tempc>255) rt[1]= 255; else rt[1]= tempc;
504                                 tempc= ( fac*rt1[2] + mfac*rt2[2])>>8;
505                                 if(tempc>255) rt[2]= 255; else rt[2]= tempc;
506                                 tempc= ( fac*rt1[3] + mfac*rt2[3])>>8;
507                                 if(tempc>255) rt[3]= 255; else rt[3]= tempc;
508                         }
509                         rt1+= 4; rt2+= 4; rt+= 4;
510                 }
511         }
512 }
513
514 void do_alphaunder_effect(float facf0, float facf1, int x, int y, unsigned int *rect1, unsigned int *rect2, unsigned int *out)
515 {
516         int fac2, mfac, fac, fac4;
517         int xo;
518         char *rt1, *rt2, *rt;
519
520         xo= x;
521         rt1= (char *)rect1;
522         rt2= (char *)rect2;
523         rt= (char *)out;
524
525         fac2= (int)(256.0*facf0);
526         fac4= (int)(256.0*facf1);
527
528         while(y--) {
529
530                 x= xo;
531                 while(x--) {
532
533                         /* rt = rt1 under rt2  (alpha from rt2) */
534
535                         /* this complex optimalisation is because the
536                          * 'skybuf' can be crossed in
537                          */
538                         if(rt2[3]==0 && fac2==256) *( (unsigned int *)rt) = *( (unsigned int *)rt1);
539                         else if(rt2[3]==255) *( (unsigned int *)rt) = *( (unsigned int *)rt2);
540                         else {
541                                 mfac= rt2[3];
542                                 fac= (fac2*(256-mfac))>>8;
543
544                                 if(fac==0) *( (unsigned int *)rt) = *( (unsigned int *)rt2);
545                                 else {
546                                         rt[0]= ( fac*rt1[0] + mfac*rt2[0])>>8;
547                                         rt[1]= ( fac*rt1[1] + mfac*rt2[1])>>8;
548                                         rt[2]= ( fac*rt1[2] + mfac*rt2[2])>>8;
549                                         rt[3]= ( fac*rt1[3] + mfac*rt2[3])>>8;
550                                 }
551                         }
552                         rt1+= 4; rt2+= 4; rt+= 4;
553                 }
554
555                 if(y==0) break;
556                 y--;
557
558                 x= xo;
559                 while(x--) {
560
561                         if(rt2[3]==0 && fac4==256) *( (unsigned int *)rt) = *( (unsigned int *)rt1);
562                         else if(rt2[3]==255) *( (unsigned int *)rt) = *( (unsigned int *)rt2);
563                         else {
564                                 mfac= rt2[3];
565                                 fac= (fac4*(256-mfac))>>8;
566
567                                 if(fac==0) *( (unsigned int *)rt) = *( (unsigned int *)rt2);
568                                 else {
569                                         rt[0]= ( fac*rt1[0] + mfac*rt2[0])>>8;
570                                         rt[1]= ( fac*rt1[1] + mfac*rt2[1])>>8;
571                                         rt[2]= ( fac*rt1[2] + mfac*rt2[2])>>8;
572                                         rt[3]= ( fac*rt1[3] + mfac*rt2[3])>>8;
573                                 }
574                         }
575                         rt1+= 4; rt2+= 4; rt+= 4;
576                 }
577         }
578 }
579
580
581 void do_cross_effect(float facf0, float facf1, int x, int y, unsigned int *rect1, unsigned int *rect2, unsigned int *out)
582 {
583         int fac1, fac2, fac3, fac4;
584         int xo;
585         char *rt1, *rt2, *rt;
586
587         xo= x;
588         rt1= (char *)rect1;
589         rt2= (char *)rect2;
590         rt= (char *)out;
591
592         fac2= (int)(256.0*facf0);
593         fac1= 256-fac2;
594         fac4= (int)(256.0*facf1);
595         fac3= 256-fac4;
596
597         while(y--) {
598
599                 x= xo;
600                 while(x--) {
601
602                         rt[0]= (fac1*rt1[0] + fac2*rt2[0])>>8;
603                         rt[1]= (fac1*rt1[1] + fac2*rt2[1])>>8;
604                         rt[2]= (fac1*rt1[2] + fac2*rt2[2])>>8;
605                         rt[3]= (fac1*rt1[3] + fac2*rt2[3])>>8;
606
607                         rt1+= 4; rt2+= 4; rt+= 4;
608                 }
609
610                 if(y==0) break;
611                 y--;
612
613                 x= xo;
614                 while(x--) {
615
616                         rt[0]= (fac3*rt1[0] + fac4*rt2[0])>>8;
617                         rt[1]= (fac3*rt1[1] + fac4*rt2[1])>>8;
618                         rt[2]= (fac3*rt1[2] + fac4*rt2[2])>>8;
619                         rt[3]= (fac3*rt1[3] + fac4*rt2[3])>>8;
620
621                         rt1+= 4; rt2+= 4; rt+= 4;
622                 }
623
624         }
625 }
626
627 void do_gammacross_effect(float facf0, float facf1, int x, int y, unsigned int *rect1, unsigned int *rect2, unsigned int *out)
628 {
629 /*      extern unsigned short *igamtab1, *gamtab; render.h */
630         int fac1, fac2, col;
631         int xo;
632         char *rt1, *rt2, *rt;
633
634         xo= x;
635         rt1= (char *)rect1;
636         rt2= (char *)rect2;
637         rt= (char *)out;
638
639         fac2= (int)(256.0*facf0);
640         fac1= 256-fac2;
641
642         while(y--) {
643
644                 x= xo;
645                 while(x--) {
646
647                         col= (fac1*igamtab1[rt1[0]] + fac2*igamtab1[rt2[0]])>>8;
648                         if(col>65535) rt[0]= 255; else rt[0]= ( (char *)(gamtab+col))[MOST_SIG_BYTE];
649                         col=(fac1*igamtab1[rt1[1]] + fac2*igamtab1[rt2[1]])>>8;
650                         if(col>65535) rt[1]= 255; else rt[1]= ( (char *)(gamtab+col))[MOST_SIG_BYTE];
651                         col= (fac1*igamtab1[rt1[2]] + fac2*igamtab1[rt2[2]])>>8;
652                         if(col>65535) rt[2]= 255; else rt[2]= ( (char *)(gamtab+col))[MOST_SIG_BYTE];
653                         col= (fac1*igamtab1[rt1[3]] + fac2*igamtab1[rt2[3]])>>8;
654                         if(col>65535) rt[3]= 255; else rt[3]= ( (char *)(gamtab+col))[MOST_SIG_BYTE];
655
656                         rt1+= 4; rt2+= 4; rt+= 4;
657                 }
658
659                 if(y==0) break;
660                 y--;
661
662                 x= xo;
663                 while(x--) {
664
665                         col= (fac1*igamtab1[rt1[0]] + fac2*igamtab1[rt2[0]])>>8;
666                         if(col>65535) rt[0]= 255; else rt[0]= ( (char *)(gamtab+col))[MOST_SIG_BYTE];
667                         col= (fac1*igamtab1[rt1[1]] + fac2*igamtab1[rt2[1]])>>8;
668                         if(col>65535) rt[1]= 255; else rt[1]= ( (char *)(gamtab+col))[MOST_SIG_BYTE];
669                         col= (fac1*igamtab1[rt1[2]] + fac2*igamtab1[rt2[2]])>>8;
670                         if(col>65535) rt[2]= 255; else rt[2]= ( (char *)(gamtab+col))[MOST_SIG_BYTE];
671                         col= (fac1*igamtab1[rt1[3]] + fac2*igamtab1[rt2[3]])>>8;
672                         if(col>65535) rt[3]= 255; else rt[3]= ( (char *)(gamtab+col))[MOST_SIG_BYTE];
673
674                         rt1+= 4; rt2+= 4; rt+= 4;
675                 }
676
677         }
678 }
679
680 void do_add_effect(float facf0, float facf1, int x, int y, unsigned int *rect1, unsigned int *rect2, unsigned int *out)
681 {
682         int col, xo, fac1, fac3;
683         char *rt1, *rt2, *rt;
684
685         xo= x;
686         rt1= (char *)rect1;
687         rt2= (char *)rect2;
688         rt= (char *)out;
689
690         fac1= (int)(256.0*facf0);
691         fac3= (int)(256.0*facf1);
692
693         while(y--) {
694
695                 x= xo;
696                 while(x--) {
697
698                         col= rt1[0]+ ((fac1*rt2[0])>>8);
699                         if(col>255) rt[0]= 255; else rt[0]= col;
700                         col= rt1[1]+ ((fac1*rt2[1])>>8);
701                         if(col>255) rt[1]= 255; else rt[1]= col;
702                         col= rt1[2]+ ((fac1*rt2[2])>>8);
703                         if(col>255) rt[2]= 255; else rt[2]= col;
704                         col= rt1[3]+ ((fac1*rt2[3])>>8);
705                         if(col>255) rt[3]= 255; else rt[3]= col;
706
707                         rt1+= 4; rt2+= 4; rt+= 4;
708                 }
709
710                 if(y==0) break;
711                 y--;
712
713                 x= xo;
714                 while(x--) {
715
716                         col= rt1[0]+ ((fac3*rt2[0])>>8);
717                         if(col>255) rt[0]= 255; else rt[0]= col;
718                         col= rt1[1]+ ((fac3*rt2[1])>>8);
719                         if(col>255) rt[1]= 255; else rt[1]= col;
720                         col= rt1[2]+ ((fac3*rt2[2])>>8);
721                         if(col>255) rt[2]= 255; else rt[2]= col;
722                         col= rt1[3]+ ((fac3*rt2[3])>>8);
723                         if(col>255) rt[3]= 255; else rt[3]= col;
724
725                         rt1+= 4; rt2+= 4; rt+= 4;
726                 }
727         }
728 }
729
730 void do_sub_effect(float facf0, float facf1, int x, int y, unsigned int *rect1, unsigned int *rect2, unsigned int *out)
731 {
732         int col, xo, fac1, fac3;
733         char *rt1, *rt2, *rt;
734
735         xo= x;
736         rt1= (char *)rect1;
737         rt2= (char *)rect2;
738         rt= (char *)out;
739
740         fac1= (int)(256.0*facf0);
741         fac3= (int)(256.0*facf1);
742
743         while(y--) {
744
745                 x= xo;
746                 while(x--) {
747
748                         col= rt1[0]- ((fac1*rt2[0])>>8);
749                         if(col<0) rt[0]= 0; else rt[0]= col;
750                         col= rt1[1]- ((fac1*rt2[1])>>8);
751                         if(col<0) rt[1]= 0; else rt[1]= col;
752                         col= rt1[2]- ((fac1*rt2[2])>>8);
753                         if(col<0) rt[2]= 0; else rt[2]= col;
754                         col= rt1[3]- ((fac1*rt2[3])>>8);
755                         if(col<0) rt[3]= 0; else rt[3]= col;
756
757                         rt1+= 4; rt2+= 4; rt+= 4;
758                 }
759
760                 if(y==0) break;
761                 y--;
762
763                 x= xo;
764                 while(x--) {
765
766                         col= rt1[0]- ((fac3*rt2[0])>>8);
767                         if(col<0) rt[0]= 0; else rt[0]= col;
768                         col= rt1[1]- ((fac3*rt2[1])>>8);
769                         if(col<0) rt[1]= 0; else rt[1]= col;
770                         col= rt1[2]- ((fac3*rt2[2])>>8);
771                         if(col<0) rt[2]= 0; else rt[2]= col;
772                         col= rt1[3]- ((fac3*rt2[3])>>8);
773                         if(col<0) rt[3]= 0; else rt[3]= col;
774
775                         rt1+= 4; rt2+= 4; rt+= 4;
776                 }
777         }
778 }
779
780 /* Must be > 0 or add precopy, etc to the function */
781 #define XOFF    8
782 #define YOFF    8
783
784 void do_drop_effect(float facf0, float facf1, int x, int y, unsigned int *rect2i, unsigned int *rect1i, unsigned int *outi)
785 {
786         int height, width, temp, fac, fac1, fac2;
787         char *rt1, *rt2, *out;
788         int field= 1;
789
790         width= x;
791         height= y;
792
793         fac1= (int)(70.0*facf0);
794         fac2= (int)(70.0*facf1);
795
796         rt2= (char*) (rect2i + YOFF*width);
797         rt1= (char*) rect1i;
798         out= (char*) outi;
799         for (y=0; y<height-YOFF; y++) {
800                 if(field) fac= fac1;
801                 else fac= fac2;
802                 field= !field;
803
804                 memcpy(out, rt1, sizeof(int)*XOFF);
805                 rt1+= XOFF*4;
806                 out+= XOFF*4;
807
808                 for (x=XOFF; x<width; x++) {
809                         temp= ((fac*rt2[3])>>8);
810
811                         *(out++)= MAX2(0, *rt1 - temp); rt1++;
812                         *(out++)= MAX2(0, *rt1 - temp); rt1++;
813                         *(out++)= MAX2(0, *rt1 - temp); rt1++;
814                         *(out++)= MAX2(0, *rt1 - temp); rt1++;
815                         rt2+=4;
816                 }
817                 rt2+=XOFF*4;
818         }
819         memcpy(out, rt1, sizeof(int)*YOFF*width);
820 }
821
822                                                 /* WATCH:  rect2 and rect1 reversed */
823 void do_drop_effect2(float facf0, float facf1, int x, int y, unsigned int *rect2, unsigned int *rect1, unsigned int *out)
824 {
825         int col, xo, yo, temp, fac1, fac3;
826         int xofs= -8, yofs= 8;
827         char *rt1, *rt2, *rt;
828
829         xo= x;
830         yo= y;
831
832         rt2= (char *)(rect2 + yofs*x + xofs);
833
834         rt1= (char *)rect1;
835         rt= (char *)out;
836
837         fac1= (int)(70.0*facf0);
838         fac3= (int)(70.0*facf1);
839
840         while(y-- > 0) {
841
842                 temp= y-yofs;
843                 if(temp > 0 && temp < yo) {
844
845                         x= xo;
846                         while(x--) {
847
848                                 temp= x+xofs;
849                                 if(temp > 0 && temp < xo) {
850
851                                         temp= ((fac1*rt2[3])>>8);
852
853                                         col= rt1[0]- temp;
854                                         if(col<0) rt[0]= 0; else rt[0]= col;
855                                         col= rt1[1]- temp;
856                                         if(col<0) rt[1]= 0; else rt[1]= col;
857                                         col= rt1[2]- temp;
858                                         if(col<0) rt[2]= 0; else rt[2]= col;
859                                         col= rt1[3]- temp;
860                                         if(col<0) rt[3]= 0; else rt[3]= col;
861                                 }
862                                 else *( (unsigned int *)rt) = *( (unsigned int *)rt1);
863
864                                 rt1+= 4; rt2+= 4; rt+= 4;
865                         }
866                 }
867                 else {
868                         x= xo;
869                         while(x--) {
870                                 *( (unsigned int *)rt) = *( (unsigned int *)rt1);
871                                 rt1+= 4; rt2+= 4; rt+= 4;
872                         }
873                 }
874
875                 if(y==0) break;
876                 y--;
877
878                 temp= y-yofs;
879                 if(temp > 0 && temp < yo) {
880
881                         x= xo;
882                         while(x--) {
883
884                                 temp= x+xofs;
885                                 if(temp > 0 && temp < xo) {
886
887                                         temp= ((fac3*rt2[3])>>8);
888
889                                         col= rt1[0]- temp;
890                                         if(col<0) rt[0]= 0; else rt[0]= col;
891                                         col= rt1[1]- temp;
892                                         if(col<0) rt[1]= 0; else rt[1]= col;
893                                         col= rt1[2]- temp;
894                                         if(col<0) rt[2]= 0; else rt[2]= col;
895                                         col= rt1[3]- temp;
896                                         if(col<0) rt[3]= 0; else rt[3]= col;
897                                 }
898                                 else *( (unsigned int *)rt) = *( (unsigned int *)rt1);
899
900                                 rt1+= 4; rt2+= 4; rt+= 4;
901                         }
902                 }
903                 else {
904                         x= xo;
905                         while(x--) {
906                                 *( (unsigned int *)rt) = *( (unsigned int *)rt1);
907                                 rt1+= 4; rt2+= 4; rt+= 4;
908                         }
909                 }
910         }
911 }
912
913
914 void do_mul_effect(float facf0, float facf1, int x, int y, unsigned int *rect1, unsigned int *rect2, unsigned int *out)
915 {
916         int  xo, fac1, fac3;
917         char *rt1, *rt2, *rt;
918
919         xo= x;
920         rt1= (char *)rect1;
921         rt2= (char *)rect2;
922         rt= (char *)out;
923
924         fac1= (int)(256.0*facf0);
925         fac3= (int)(256.0*facf1);
926
927         /* formula:
928          *              fac*(a*b) + (1-fac)*a  => fac*a*(b-1)+a
929          */
930
931         while(y--) {
932
933                 x= xo;
934                 while(x--) {
935
936                         rt[0]= rt1[0] + ((fac1*rt1[0]*(rt2[0]-256))>>16);
937                         rt[1]= rt1[1] + ((fac1*rt1[1]*(rt2[1]-256))>>16);
938                         rt[2]= rt1[2] + ((fac1*rt1[2]*(rt2[2]-256))>>16);
939                         rt[3]= rt1[3] + ((fac1*rt1[3]*(rt2[3]-256))>>16);
940
941                         rt1+= 4; rt2+= 4; rt+= 4;
942                 }
943
944                 if(y==0) break;
945                 y--;
946
947                 x= xo;
948                 while(x--) {
949
950                         rt[0]= rt1[0] + ((fac3*rt1[0]*(rt2[0]-256))>>16);
951                         rt[1]= rt1[1] + ((fac3*rt1[1]*(rt2[1]-256))>>16);
952                         rt[2]= rt1[2] + ((fac3*rt1[2]*(rt2[2]-256))>>16);
953                         rt[3]= rt1[3] + ((fac3*rt1[3]*(rt2[3]-256))>>16);
954
955                         rt1+= 4; rt2+= 4; rt+= 4;
956                 }
957         }
958 }
959
960 // This function calculates the blur band for the wipe effects
961 float in_band(float width,float dist, float perc,int side,int dir){
962         
963         float t1,t2,alpha,percwidth;
964         if(width == 0)
965                 return (float)side;
966         if(side == 1)
967                 percwidth = width * perc;
968         else
969                 percwidth = width * (1 - perc);
970         
971         if(width < dist)
972                 return side;
973         
974         t1 = dist / width;  //percentange of width that is
975         t2 = 1 / width;  //amount of alpha per % point
976         
977         if(side == 1)
978                 alpha = (t1*t2*100) + (1-perc); // add point's alpha contrib to current position in wipe
979         else
980                 alpha = (1-perc) - (t1*t2*100);
981         
982         if(dir == 0)
983                 alpha = 1-alpha;
984         return alpha;
985 }
986
987 float check_zone(int x, int y, int xo, int yo, Sequence *seq, float facf0) {
988
989    float posx, posy,hyp,hyp2,angle,hwidth,b1,b2,b3,pointdist;
990    /*some future stuff
991    float hyp3,hyp4,b4,b5           
992    */
993    float temp1,temp2,temp3,temp4; //some placeholder variables
994    float halfx = xo/2;
995    float halfy = yo/2;
996    float widthf,output=0;
997    WipeVars *wipe = (WipeVars *)seq->effectdata;
998    int width;
999
1000         angle = wipe->angle;
1001         if(angle < 0){
1002                 x = xo-x;
1003                 //y = yo-y
1004                 }
1005         angle = pow(fabs(angle)/45,log(xo)/log(2));
1006
1007         posy = facf0 * yo;
1008         if(wipe->forward){
1009                 posx = facf0 * xo;
1010                 posy = facf0 * yo;
1011         } else{
1012                 posx = xo - facf0 * xo;
1013                 posy = yo - facf0 * yo;
1014         }
1015    switch (wipe->wipetype) {
1016        case DO_SINGLE_WIPE:
1017          width = (int)(wipe->edgeWidth*((xo+yo)/2.0));
1018          hwidth = (float)width/2.0;       
1019                 
1020          if (angle == 0.0)angle = 0.000001;
1021          b1 = posy - (-angle)*posx;
1022          b2 = y - (-angle)*x;
1023          hyp  = fabs(angle*x+y+(-posy-angle*posx))/sqrt(angle*angle+1);
1024          if(angle < 0){
1025                  temp1 = b1;
1026                  b1 = b2;
1027                  b2 = temp1;
1028          }
1029          if(wipe->forward){      
1030                      if(b1 < b2)
1031                                 output = in_band(width,hyp,facf0,1,1);
1032                  else
1033                                 output = in_band(width,hyp,facf0,0,1);
1034                  }
1035                  else{   
1036                  if(b1 < b2)
1037                                 output = in_band(width,hyp,facf0,0,1);
1038                  else
1039                                 output = in_band(width,hyp,facf0,1,1);
1040                  }
1041                  break;
1042          
1043          
1044           case DO_DOUBLE_WIPE:
1045                  if(!wipe->forward)facf0 = 1-facf0;   // Go the other direction
1046
1047              width = (int)(wipe->edgeWidth*((xo+yo)/2.0));  // calculate the blur width
1048              hwidth = (float)width/2.0;       
1049              if (angle == 0)angle = 0.000001;
1050              b1 = posy/2 - (-angle)*posx/2;
1051              b3 = (yo-posy/2) - (-angle)*(xo-posx/2);
1052              b2 = y - (-angle)*x;
1053
1054              hyp = abs(angle*x+y+(-posy/2-angle*posx/2))/sqrt(angle*angle+1);
1055              hyp2 = abs(angle*x+y+(-(yo-posy/2)-angle*(xo-posx/2)))/sqrt(angle*angle+1);
1056              
1057              temp1 = xo*(1-facf0/2)-xo*facf0/2;
1058              temp2 = yo*(1-facf0/2)-yo*facf0/2;
1059                  pointdist = sqrt(temp1*temp1 + temp2*temp2);
1060
1061                          if(b2 < b1 && b2 < b3 ){
1062                                 if(hwidth < pointdist)
1063                                         output = in_band(hwidth,hyp,facf0,0,1);
1064                         }
1065                          else if(b2 > b1 && b2 > b3 ){
1066                                 if(hwidth < pointdist)
1067                                         output = in_band(hwidth,hyp2,facf0,0,1);        
1068                         } 
1069                      else{
1070                          if(  hyp < hwidth && hyp2 > hwidth )
1071                                  output = in_band(hwidth,hyp,facf0,1,1);
1072                          else if(  hyp > hwidth && hyp2 < hwidth )
1073                                          output = in_band(hwidth,hyp2,facf0,1,1);
1074                                  else
1075                                          output = in_band(hwidth,hyp2,facf0,1,1) * in_band(hwidth,hyp,facf0,1,1);
1076                      }
1077                      if(!wipe->forward)output = 1-output;
1078          break;     
1079          case DO_CLOCK_WIPE:
1080                         /*
1081                                 temp1: angle of effect center in rads
1082                                 temp2: angle of line through (halfx,halfy) and (x,y) in rads
1083                                 temp3: angle of low side of blur
1084                                 temp4: angle of high side of blur
1085                         */
1086                         output = 1-facf0;
1087                         widthf = wipe->edgeWidth*2*3.14159;
1088                         temp1 = 2 * 3.14159 * facf0;
1089                         
1090                         if(wipe->forward){
1091                                 temp1 = 2*3.14159-temp1;
1092                         }
1093                         
1094                         x = x - halfx;
1095                         y = y - halfy;
1096
1097                         temp2 = asin(abs(y)/sqrt(x*x + y*y));
1098                         if(x <= 0 && y >= 0)
1099                                 temp2 = 3.14159 - temp2;
1100                         else if(x<=0 && y <= 0)
1101                                 temp2 += 3.14159;
1102                         else if(x >= 0 && y <= 0)
1103                                 temp2 = 2*3.14159 - temp2;
1104                         
1105                         if(wipe->forward){
1106                                 temp3 = temp1-(widthf/2)*facf0;
1107                                 temp4 = temp1+(widthf/2)*(1-facf0);
1108                         }
1109                         else{
1110                                 temp3 = temp1-(widthf/2)*(1-facf0);
1111                                 temp4 = temp1+(widthf/2)*facf0;
1112                         }
1113                         if (temp3 < 0)  temp3 = 0;
1114                         if (temp4 > 2*3.14159) temp4 = 2*3.14159;
1115                         
1116                         
1117                         if(temp2 < temp3)
1118                                 output = 0;
1119                         else if (temp2 > temp4)
1120                                 output = 1;
1121                         else
1122                                 output = (temp2-temp3)/(temp4-temp3);
1123                         if(x == 0 && y == 0){
1124                                 output = 1;
1125                         }
1126                         if(output != output)
1127                                 output = 1;
1128                         if(wipe->forward)
1129                                 output = 1 - output;
1130         break;
1131         /* BOX WIPE IS NOT WORKING YET */
1132      /* case DO_CROSS_WIPE: */
1133         /* BOX WIPE IS NOT WORKING YET */
1134      /* case DO_BOX_WIPE: 
1135                  if(invert)facf0 = 1-facf0;
1136
1137              width = (int)(wipe->edgeWidth*((xo+yo)/2.0));
1138              hwidth = (float)width/2.0;       
1139              if (angle == 0)angle = 0.000001;
1140              b1 = posy/2 - (-angle)*posx/2;
1141              b3 = (yo-posy/2) - (-angle)*(xo-posx/2);
1142              b2 = y - (-angle)*x;
1143
1144              hyp = abs(angle*x+y+(-posy/2-angle*posx/2))/sqrt(angle*angle+1);
1145              hyp2 = abs(angle*x+y+(-(yo-posy/2)-angle*(xo-posx/2)))/sqrt(angle*angle+1);
1146              
1147              temp1 = xo*(1-facf0/2)-xo*facf0/2;
1148              temp2 = yo*(1-facf0/2)-yo*facf0/2;
1149                  pointdist = sqrt(temp1*temp1 + temp2*temp2);
1150
1151                          if(b2 < b1 && b2 < b3 ){
1152                                 if(hwidth < pointdist)
1153                                         output = in_band(hwidth,hyp,facf0,0,1);
1154                         }
1155                          else if(b2 > b1 && b2 > b3 ){
1156                                 if(hwidth < pointdist)
1157                                         output = in_band(hwidth,hyp2,facf0,0,1);        
1158                         } 
1159                      else{
1160                          if(  hyp < hwidth && hyp2 > hwidth )
1161                                  output = in_band(hwidth,hyp,facf0,1,1);
1162                          else if(  hyp > hwidth && hyp2 < hwidth )
1163                                          output = in_band(hwidth,hyp2,facf0,1,1);
1164                                  else
1165                                          output = in_band(hwidth,hyp2,facf0,1,1) * in_band(hwidth,hyp,facf0,1,1);
1166                      }
1167                  if(invert)facf0 = 1-facf0;
1168              angle = -1/angle;
1169              b1 = posy/2 - (-angle)*posx/2;
1170              b3 = (yo-posy/2) - (-angle)*(xo-posx/2);
1171              b2 = y - (-angle)*x;
1172
1173              hyp = abs(angle*x+y+(-posy/2-angle*posx/2))/sqrt(angle*angle+1);
1174              hyp2 = abs(angle*x+y+(-(yo-posy/2)-angle*(xo-posx/2)))/sqrt(angle*angle+1);
1175            
1176                  if(b2 < b1 && b2 < b3 ){
1177                                 if(hwidth < pointdist)
1178                                         output *= in_band(hwidth,hyp,facf0,0,1);
1179                         }
1180                          else if(b2 > b1 && b2 > b3 ){
1181                                 if(hwidth < pointdist)
1182                                         output *= in_band(hwidth,hyp2,facf0,0,1);       
1183                         } 
1184                      else{
1185                          if(  hyp < hwidth && hyp2 > hwidth )
1186                                  output *= in_band(hwidth,hyp,facf0,1,1);
1187                          else if(  hyp > hwidth && hyp2 < hwidth )
1188                                          output *= in_band(hwidth,hyp2,facf0,1,1);
1189                                  else
1190                                          output *= in_band(hwidth,hyp2,facf0,1,1) * in_band(hwidth,hyp,facf0,1,1);
1191                      }
1192                      
1193          break;*/
1194       case DO_IRIS_WIPE:
1195          if(xo > yo) yo = xo;
1196          else xo = yo;
1197          
1198                 if(!wipe->forward)
1199                         facf0 = 1-facf0;
1200
1201              width = (int)(wipe->edgeWidth*((xo+yo)/2.0));
1202              hwidth = (float)width/2.0; 
1203              
1204          temp1 = (halfx-(halfx)*facf0);     
1205                  pointdist = sqrt(temp1*temp1 + temp1*temp1);
1206                  
1207                  temp2 = sqrt((halfx-x)*(halfx-x)  +  (halfy-y)*(halfy-y));
1208                  if(temp2 > pointdist)
1209                          output = in_band(hwidth,fabs(temp2-pointdist),facf0,0,1);
1210                  else
1211                          output = in_band(hwidth,fabs(temp2-pointdist),facf0,1,1);
1212                  
1213                 if(!wipe->forward)
1214                         output = 1-output;
1215                         
1216          break;
1217    }
1218    if     (output < 0) output = 0;
1219    else if(output > 1) output = 1;
1220    return output;
1221 }
1222
1223 void init_wipe_effect(Sequence *seq)
1224 {
1225         if(seq->effectdata)MEM_freeN(seq->effectdata);
1226         seq->effectdata = MEM_callocN(sizeof(struct WipeVars), "wipevars");
1227 }
1228
1229 void do_wipe_effect(Sequence *seq, float facf0, float facf1, int x, int y, unsigned int *rect1, unsigned int *rect2, unsigned int *out)
1230 {
1231         int xo, yo;
1232         char *rt1, *rt2, *rt;
1233         rt1 = (char *)rect1;
1234         rt2 = (char *)rect2;
1235         rt = (char *)out;
1236
1237         xo = x;
1238         yo = y;
1239         for(y=0;y<yo;y++) {
1240
1241       for(x=0;x<xo;x++) {
1242                         float check = check_zone(x,y,xo,yo,seq,facf0);
1243                         if (check) {
1244                                 if (rt1) {
1245                                         rt[0] = (int)(rt1[0]*check)+ (int)(rt2[0]*(1-check));
1246                                         rt[1] = (int)(rt1[1]*check)+ (int)(rt2[1]*(1-check));
1247                                         rt[2] = (int)(rt1[2]*check)+ (int)(rt2[2]*(1-check));
1248                                         rt[3] = (int)(rt1[3]*check)+ (int)(rt2[3]*(1-check));
1249                                 } else {
1250                                         rt[0] = 0;
1251                                         rt[1] = 0;
1252                                         rt[2] = 0;
1253                                         rt[3] = 255;
1254                                 }
1255                         } else {
1256                                 if (rt2) {
1257                                         rt[0] = rt2[0];
1258                                         rt[1] = rt2[1];
1259                                         rt[2] = rt2[2];
1260                                         rt[3] = rt2[3];
1261                                 } else {
1262                                         rt[0] = 0;
1263                                         rt[1] = 0;
1264                                         rt[2] = 0;
1265                                         rt[3] = 255;
1266                                 }
1267                         }
1268
1269                         rt+=4;
1270                         if(rt1 !=NULL){
1271                                 rt1+=4;
1272                         }
1273                         if(rt2 !=NULL){
1274                                 rt2+=4;
1275                         }
1276                 }
1277         }
1278 }
1279
1280 /* Glow Functions */
1281
1282 void RVBlurBitmap2 ( unsigned char* map, int width,int height,float blur,
1283    int quality)
1284 /*      MUUUCCH better than the previous blur. */
1285 /*      We do the blurring in two passes which is a whole lot faster. */
1286 /*      I changed the math arount to implement an actual Gaussian */
1287 /*      distribution. */
1288 /* */
1289 /*      Watch out though, it tends to misbehaven with large blur values on */
1290 /*      a small bitmap.  Avoid avoid avoid. */
1291 /*=============================== */
1292 {
1293         unsigned char*  temp=NULL,*swap;
1294         float   *filter=NULL;
1295         int     x,y,i,fx,fy;
1296         int     index, ix, halfWidth;
1297         float   fval, k, curColor[3], curColor2[3], weight=0;
1298
1299         /*      If we're not really blurring, bail out */
1300         if (blur<=0)
1301                 return;
1302
1303         /*      Allocate memory for the tempmap and the blur filter matrix */
1304         temp= MEM_mallocN( (width*height*4), "blurbitmaptemp");
1305         if (!temp)
1306                 return;
1307
1308         /*      Allocate memory for the filter elements */
1309         halfWidth = ((quality+1)*blur);
1310         filter = (float *)MEM_mallocN(sizeof(float)*halfWidth*2, "blurbitmapfilter");
1311         if (!filter){
1312                 MEM_freeN (temp);
1313                 return;
1314         }
1315
1316         /*      Apparently we're calculating a bell curve */
1317         /*      based on the standard deviation (or radius) */
1318         /*      This code is based on an example */
1319         /*      posted to comp.graphics.algorithms by */
1320         /*      Blancmange (bmange@airdmhor.gen.nz) */
1321
1322         k = -1.0/(2.0*3.14159*blur*blur);
1323         fval=0;
1324         for (ix = 0;ix< halfWidth;ix++){
1325                 weight = (float)exp(k*(ix*ix));
1326                 filter[halfWidth - ix] = weight;
1327                 filter[halfWidth + ix] = weight;
1328         }
1329         filter[0] = weight;
1330
1331         /*      Normalize the array */
1332         fval=0;
1333         for (ix = 0;ix< halfWidth*2;ix++)
1334                 fval+=filter[ix];
1335
1336         for (ix = 0;ix< halfWidth*2;ix++)
1337                 filter[ix]/=fval;
1338
1339         /*      Blur the rows */
1340         for (y=0;y<height;y++){
1341                 /*      Do the left & right strips */
1342                 for (x=0;x<halfWidth;x++){
1343                         index=(x+y*width)*4;
1344                         fx=0;
1345                         curColor[0]=curColor[1]=curColor[2]=0;
1346                         curColor2[0]=curColor2[1]=curColor2[2]=0;
1347
1348                         for (i=x-halfWidth;i<x+halfWidth;i++){
1349                            if ((i>=0)&&(i<width)){
1350                                 curColor[0]+=map[(i+y*width)*4+GlowR]*filter[fx];
1351                                 curColor[1]+=map[(i+y*width)*4+GlowG]*filter[fx];
1352                                 curColor[2]+=map[(i+y*width)*4+GlowB]*filter[fx];
1353
1354                                 curColor2[0]+=map[(width-1-i+y*width)*4+GlowR] *
1355                                    filter[fx];
1356                                 curColor2[1]+=map[(width-1-i+y*width)*4+GlowG] *
1357                                    filter[fx];
1358                                 curColor2[2]+=map[(width-1-i+y*width)*4+GlowB] *
1359                                    filter[fx];
1360                                 }
1361                                 fx++;
1362                         }
1363                         temp[index+GlowR]=curColor[0];
1364                         temp[index+GlowG]=curColor[1];
1365                         temp[index+GlowB]=curColor[2];
1366
1367                         temp[((width-1-x+y*width)*4)+GlowR]=curColor2[0];
1368                         temp[((width-1-x+y*width)*4)+GlowG]=curColor2[1];
1369                         temp[((width-1-x+y*width)*4)+GlowB]=curColor2[2];
1370
1371                 }
1372                 /*      Do the main body */
1373                 for (x=halfWidth;x<width-halfWidth;x++){
1374                         index=(x+y*width)*4;
1375                         fx=0;
1376                         curColor[0]=curColor[1]=curColor[2]=0;
1377                         for (i=x-halfWidth;i<x+halfWidth;i++){
1378                                 curColor[0]+=map[(i+y*width)*4+GlowR]*filter[fx];
1379                                 curColor[1]+=map[(i+y*width)*4+GlowG]*filter[fx];
1380                                 curColor[2]+=map[(i+y*width)*4+GlowB]*filter[fx];
1381                                 fx++;
1382                         }
1383                         temp[index+GlowR]=curColor[0];
1384                         temp[index+GlowG]=curColor[1];
1385                         temp[index+GlowB]=curColor[2];
1386                 }
1387         }
1388
1389         /*      Swap buffers */
1390         swap=temp;temp=map;map=swap;
1391
1392
1393         /*      Blur the columns */
1394         for (x=0;x<width;x++){
1395                 /*      Do the top & bottom strips */
1396                 for (y=0;y<halfWidth;y++){
1397                         index=(x+y*width)*4;
1398                         fy=0;
1399                         curColor[0]=curColor[1]=curColor[2]=0;
1400                         curColor2[0]=curColor2[1]=curColor2[2]=0;
1401                         for (i=y-halfWidth;i<y+halfWidth;i++){
1402                                 if ((i>=0)&&(i<height)){
1403                                    /*   Bottom */
1404                                    curColor[0]+=map[(x+i*width)*4+GlowR]*filter[fy];
1405                                    curColor[1]+=map[(x+i*width)*4+GlowG]*filter[fy];
1406                                    curColor[2]+=map[(x+i*width)*4+GlowB]*filter[fy];
1407
1408                                    /*   Top */
1409                                    curColor2[0]+=map[(x+(height-1-i)*width) *
1410                                       4+GlowR]*filter[fy];
1411                                    curColor2[1]+=map[(x+(height-1-i)*width) *
1412                                       4+GlowG]*filter[fy];
1413                                    curColor2[2]+=map[(x+(height-1-i)*width) *
1414                                       4+GlowB]*filter[fy];
1415                                 }
1416                                 fy++;
1417                         }
1418                         temp[index+GlowR]=curColor[0];
1419                         temp[index+GlowG]=curColor[1];
1420                         temp[index+GlowB]=curColor[2];
1421                         temp[((x+(height-1-y)*width)*4)+GlowR]=curColor2[0];
1422                         temp[((x+(height-1-y)*width)*4)+GlowG]=curColor2[1];
1423                         temp[((x+(height-1-y)*width)*4)+GlowB]=curColor2[2];
1424                 }
1425                 /*      Do the main body */
1426                 for (y=halfWidth;y<height-halfWidth;y++){
1427                         index=(x+y*width)*4;
1428                         fy=0;
1429                         curColor[0]=curColor[1]=curColor[2]=0;
1430                         for (i=y-halfWidth;i<y+halfWidth;i++){
1431                                 curColor[0]+=map[(x+i*width)*4+GlowR]*filter[fy];
1432                                 curColor[1]+=map[(x+i*width)*4+GlowG]*filter[fy];
1433                                 curColor[2]+=map[(x+i*width)*4+GlowB]*filter[fy];
1434                                 fy++;
1435                         }
1436                         temp[index+GlowR]=curColor[0];
1437                         temp[index+GlowG]=curColor[1];
1438                         temp[index+GlowB]=curColor[2];
1439                 }
1440         }
1441
1442
1443         /*      Swap buffers */
1444         swap=temp;temp=map;map=swap;
1445
1446         /*      Tidy up  */
1447         MEM_freeN (filter);
1448         MEM_freeN (temp);
1449 }
1450
1451
1452 /*      Adds two bitmaps and puts the results into a third map. */
1453 /*      C must have been previously allocated but it may be A or B. */
1454 /*      We clamp values to 255 to prevent weirdness */
1455 /*=============================== */
1456 void RVAddBitmaps (unsigned char* a, unsigned char* b, unsigned char* c, int width, int height)
1457 {
1458         int     x,y,index;
1459
1460         for (y=0;y<height;y++){
1461                 for (x=0;x<width;x++){
1462                         index=(x+y*width)*4;
1463                         c[index+GlowR]=MIN2(255,a[index+GlowR]+b[index+GlowR]);
1464                         c[index+GlowG]=MIN2(255,a[index+GlowG]+b[index+GlowG]);
1465                         c[index+GlowB]=MIN2(255,a[index+GlowB]+b[index+GlowB]);
1466                         c[index+GlowA]=MIN2(255,a[index+GlowA]+b[index+GlowA]);
1467                 }
1468         }
1469 }
1470
1471 /*      For each pixel whose total luminance exceeds the threshold, */
1472 /*      Multiply it's value by BOOST and add it to the output map */
1473 void RVIsolateHighlights (unsigned char* in, unsigned char* out, int width, int height, int threshold, float boost, float clamp)
1474 {
1475         int x,y,index;
1476         int     intensity;
1477
1478
1479         for(y=0;y< height;y++) {
1480                 for (x=0;x< width;x++) {
1481                    index= (x+y*width)*4;
1482
1483                    /*   Isolate the intensity */
1484                    intensity=(in[index+GlowR]+in[index+GlowG]+in[index+GlowB]-threshold);
1485                    if (intensity>0){
1486                         out[index+GlowR]=MIN2(255*clamp, (in[index+GlowR]*boost*intensity)/255);
1487                         out[index+GlowG]=MIN2(255*clamp, (in[index+GlowG]*boost*intensity)/255);
1488                         out[index+GlowB]=MIN2(255*clamp, (in[index+GlowB]*boost*intensity)/255);
1489                         }
1490                         else{
1491                                 out[index+GlowR]=0;
1492                                 out[index+GlowG]=0;
1493                                 out[index+GlowB]=0;
1494                         }
1495                 }
1496         }
1497 }
1498
1499 void init_glow_effect(Sequence *seq)
1500 {
1501         GlowVars *glow;
1502
1503         if(seq->effectdata)MEM_freeN(seq->effectdata);
1504         seq->effectdata = MEM_callocN(sizeof(struct GlowVars), "glowvars");
1505
1506         glow = (GlowVars *)seq->effectdata;
1507         glow->fMini = 0.25;
1508         glow->fClamp = 1.0;
1509         glow->fBoost = 0.5;
1510         glow->dDist = 3.0;
1511         glow->dQuality = 3;
1512         glow->bNoComp = 0;
1513 }
1514
1515
1516 //void do_glow_effect(Cast *cast, float facf0, float facf1, int xo, int yo, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *outbuf, ImBuf *use)
1517 void do_glow_effect(Sequence *seq, float facf0, float facf1, int x, int y, unsigned int *rect1, unsigned int *rect2, unsigned int *out)
1518 {
1519         unsigned char *outbuf=(unsigned char *)out;
1520         unsigned char *inbuf=(unsigned char *)rect1;
1521         GlowVars *glow = (GlowVars *)seq->effectdata;
1522
1523         RVIsolateHighlights     (inbuf, outbuf , x, y, glow->fMini*765, glow->fBoost, glow->fClamp);
1524         RVBlurBitmap2 (outbuf, x, y, glow->dDist,glow->dQuality);
1525         if (!glow->bNoComp)
1526                 RVAddBitmaps (inbuf , outbuf, outbuf, x, y);
1527 }
1528
1529 void make_black_ibuf(ImBuf *ibuf)
1530 {
1531         unsigned int *rect;
1532         int tot;
1533
1534         if(ibuf==0 || ibuf->rect==0) return;
1535
1536         tot= ibuf->x*ibuf->y;
1537         rect= ibuf->rect;
1538         while(tot--) *(rect++)= 0;
1539
1540 }
1541
1542 void multibuf(ImBuf *ibuf, float fmul)
1543 {
1544         char *rt;
1545         int a, mul, icol;
1546
1547         mul= (int)(256.0*fmul);
1548
1549         a= ibuf->x*ibuf->y;
1550         rt= (char *)ibuf->rect;
1551         while(a--) {
1552
1553                 icol= (mul*rt[0])>>8;
1554                 if(icol>254) rt[0]= 255; else rt[0]= icol;
1555                 icol= (mul*rt[1])>>8;
1556                 if(icol>254) rt[1]= 255; else rt[1]= icol;
1557                 icol= (mul*rt[2])>>8;
1558                 if(icol>254) rt[2]= 255; else rt[2]= icol;
1559                 icol= (mul*rt[3])>>8;
1560                 if(icol>254) rt[3]= 255; else rt[3]= icol;
1561
1562                 rt+= 4;
1563         }
1564 }
1565
1566 void do_effect(int cfra, Sequence *seq, StripElem *se)
1567 {
1568         StripElem *se1, *se2, *se3;
1569         float fac, facf;
1570         int x, y;
1571         char *cp;
1572
1573         if(se->se1==0 || se->se2==0 || se->se3==0) {
1574                 make_black_ibuf(se->ibuf);
1575                 return;
1576         }
1577
1578         /* if metastrip: other se's */
1579         if(se->se1->ok==2) se1= se->se1->se1;
1580         else se1= se->se1;
1581
1582         if(se->se2->ok==2) se2= se->se2->se1;
1583         else se2= se->se2;
1584
1585         if(se->se3->ok==2) se3= se->se3->se1;
1586         else se3= se->se3;
1587
1588         if(se1==0 || se2==0 || se3==0 || se1->ibuf==0 || se2->ibuf==0 || se3->ibuf==0) {
1589                 make_black_ibuf(se->ibuf);
1590                 return;
1591         }
1592
1593         x= se2->ibuf->x;
1594         y= se2->ibuf->y;
1595
1596         if(seq->ipo && seq->ipo->curve.first) {
1597                 do_seq_ipo(seq);
1598                 fac= seq->facf0;
1599                 facf= seq->facf1;
1600         }
1601         else if ( seq->type==SEQ_CROSS || seq->type==SEQ_GAMCROSS || seq->type==SEQ_PLUGIN || seq->type==SEQ_WIPE) {
1602                 fac= (float)(cfra - seq->startdisp);
1603                 facf= (float)(fac+0.5);
1604                 fac /= seq->len;
1605                 facf /= seq->len;
1606         }
1607         else {
1608                 fac= facf= 1.0;
1609         }
1610
1611         if( G.scene->r.mode & R_FIELDS ); else facf= fac;
1612
1613         switch(seq->type) {
1614         case SEQ_CROSS:
1615                 do_cross_effect(fac, facf, x, y, se1->ibuf->rect, se2->ibuf->rect, se->ibuf->rect);
1616                 break;
1617         case SEQ_GAMCROSS:
1618                 do_gammacross_effect(fac, facf, x, y, se1->ibuf->rect, se2->ibuf->rect, se->ibuf->rect);
1619                 break;
1620         case SEQ_ADD:
1621                 do_add_effect(fac, facf, x, y, se1->ibuf->rect, se2->ibuf->rect, se->ibuf->rect);
1622                 break;
1623         case SEQ_SUB:
1624                 do_sub_effect(fac, facf, x, y, se1->ibuf->rect, se2->ibuf->rect, se->ibuf->rect);
1625                 break;
1626         case SEQ_MUL:
1627                 do_mul_effect(fac, facf, x, y, se1->ibuf->rect, se2->ibuf->rect, se->ibuf->rect);
1628                 break;
1629         case SEQ_ALPHAOVER:
1630                 do_alphaover_effect(fac, facf, x, y, se1->ibuf->rect, se2->ibuf->rect, se->ibuf->rect);
1631                 break;
1632         case SEQ_OVERDROP:
1633                 do_drop_effect(fac, facf, x, y, se1->ibuf->rect, se2->ibuf->rect, se->ibuf->rect);
1634                 do_alphaover_effect(fac, facf, x, y, se1->ibuf->rect, se->ibuf->rect, se->ibuf->rect);
1635                 break;
1636         case SEQ_ALPHAUNDER:
1637                 do_alphaunder_effect(fac, facf, x, y, se1->ibuf->rect, se2->ibuf->rect, se->ibuf->rect);
1638                 break;
1639         case SEQ_WIPE:
1640                 do_wipe_effect(seq, fac, facf, x, y, se1->ibuf->rect, se2->ibuf->rect, se->ibuf->rect);
1641                 break;
1642         case SEQ_GLOW:
1643                 do_glow_effect(seq, fac, facf, x, y, se1->ibuf->rect, se2->ibuf->rect, se->ibuf->rect);
1644                 break;
1645         case SEQ_PLUGIN:
1646                 if(seq->plugin && seq->plugin->doit) {
1647
1648                         if((G.f & G_PLAYANIM)==0) waitcursor(1);
1649
1650                         if(seq->plugin->cfra) *(seq->plugin->cfra)= frame_to_float(CFRA);
1651
1652                         cp= PIL_dynlib_find_symbol(seq->plugin->handle, "seqname");
1653                         if(cp) strcpy(cp, seq->name+2);
1654
1655                         if (seq->plugin->version<=2) {
1656                                 if(se1->ibuf) IMB_convert_rgba_to_abgr(se1->ibuf->x*se1->ibuf->y, se1->ibuf->rect);
1657                                 if(se2->ibuf) IMB_convert_rgba_to_abgr(se2->ibuf->x*se2->ibuf->y, se2->ibuf->rect);
1658                                 if(se3->ibuf) IMB_convert_rgba_to_abgr(se3->ibuf->x*se3->ibuf->y, se3->ibuf->rect);
1659                         }
1660
1661                         ((SeqDoit)seq->plugin->doit)(seq->plugin->data, fac, facf, x, y,
1662                                                 se1->ibuf, se2->ibuf, se->ibuf, se3->ibuf);
1663
1664                         if (seq->plugin->version<=2) {
1665                                 if(se1->ibuf) IMB_convert_rgba_to_abgr(se1->ibuf->x*se1->ibuf->y, se1->ibuf->rect);
1666                                 if(se2->ibuf) IMB_convert_rgba_to_abgr(se2->ibuf->x*se2->ibuf->y, se2->ibuf->rect);
1667                                 if(se3->ibuf) IMB_convert_rgba_to_abgr(se3->ibuf->x*se3->ibuf->y, se3->ibuf->rect);
1668                                 IMB_convert_rgba_to_abgr(se->ibuf->x*se->ibuf->y, se->ibuf->rect);
1669                         }
1670
1671                         if((G.f & G_PLAYANIM)==0) waitcursor(0);
1672                 }
1673                 break;
1674         }
1675
1676 }
1677
1678 int evaluate_seq_frame(int cfra)
1679 {
1680         Sequence *seq;
1681         Editing *ed;
1682         int totseq=0;
1683
1684         memset(seq_arr, 0, 4*MAXSEQ);
1685
1686         ed= G.scene->ed;
1687         if(ed==0) return 0;
1688
1689         seq= ed->seqbasep->first;
1690         while(seq) {
1691                 if(seq->startdisp <=cfra && seq->enddisp > cfra) {
1692                         seq_arr[seq->machine]= seq;
1693                         totseq++;
1694                 }
1695                 seq= seq->next;
1696         }
1697
1698         return totseq;
1699 }
1700
1701 StripElem *give_stripelem(Sequence *seq, int cfra)
1702 {
1703         Strip *strip;
1704         StripElem *se;
1705         int nr;
1706
1707         strip= seq->strip;
1708         se= strip->stripdata;
1709
1710         if(se==0) return 0;
1711         if(seq->startdisp >cfra || seq->enddisp <= cfra) return 0;
1712
1713         if(cfra <= seq->start) nr= 0;
1714         else if(cfra >= seq->start+seq->len-1) nr= seq->len-1;
1715         else nr= cfra-seq->start;
1716
1717         se+= nr;
1718         se->nr= nr;
1719
1720         return se;
1721 }
1722
1723 void set_meta_stripdata(Sequence *seqm)
1724 {
1725         Sequence *seq, *seqim, *seqeff;
1726         Editing *ed;
1727         ListBase *tempbase;
1728         StripElem *se;
1729         int a, cfra, b;
1730
1731         /* sets all ->se1 pointers in stripdata, to read the ibuf from it */
1732
1733         ed= G.scene->ed;
1734         if(ed==0) return;
1735
1736         tempbase= ed->seqbasep;
1737         ed->seqbasep= &seqm->seqbase;
1738
1739         se= seqm->strip->stripdata;
1740         for(a=0; a<seqm->len; a++, se++) {
1741                 cfra= a+seqm->start;
1742                 if(evaluate_seq_frame(cfra)) {
1743
1744                         /* we take the upper effect strip or the lowest imagestrip/metastrip */
1745                         seqim= seqeff= 0;
1746
1747                         for(b=1; b<MAXSEQ; b++) {
1748                                 if(seq_arr[b]) {
1749                                         seq= seq_arr[b];
1750                                         if(seq->type & SEQ_EFFECT) {
1751                                                 if(seqeff==0) seqeff= seq;
1752                                                 else if(seqeff->machine < seq->machine) seqeff= seq;
1753                                         }
1754                                         else {
1755                                                 if(seqim==0) seqim= seq;
1756                                                 else if(seqim->machine > seq->machine) seqim= seq;
1757                                         }
1758                                 }
1759                         }
1760                         if(seqeff) seq= seqeff;
1761                         else if(seqim) seq= seqim;
1762                         else seq= 0;
1763
1764                         if(seq) {
1765                                 se->se1= give_stripelem(seq, cfra);
1766                         }
1767                         else se->se1= 0;
1768                 }
1769         }
1770
1771         ed->seqbasep= tempbase;
1772 }
1773
1774
1775
1776 /* HELP FUNCTIONS FOR GIVE_IBUF_SEQ */
1777
1778 void do_seq_count_cfra(ListBase *seqbase, int *totseq, int cfra)
1779 {
1780         Sequence *seq;
1781
1782         seq= seqbase->first;
1783         while(seq) {
1784                 if(seq->startdisp <=cfra && seq->enddisp > cfra) {
1785
1786                         if(seq->seqbase.first) {
1787
1788                                 if(cfra< seq->start) do_seq_count_cfra(&seq->seqbase, totseq, seq->start);
1789                                 else if(cfra> seq->start+seq->len-1) do_seq_count_cfra(&seq->seqbase, totseq, seq->start+seq->len-1);
1790                                 else do_seq_count_cfra(&seq->seqbase, totseq, cfra);
1791                         }
1792
1793                         (*totseq)++;
1794                 }
1795                 seq= seq->next;
1796         }
1797 }
1798
1799 void do_build_seqar_cfra(ListBase *seqbase, Sequence ***seqar, int cfra)
1800 {
1801         Sequence *seq;
1802         StripElem *se;
1803         Scene *oldsce;
1804         unsigned int *rectot;
1805         int oldx, oldy, oldcfra, doseq;
1806         char name[FILE_MAXDIR];
1807
1808         seq= seqbase->first;
1809         while(seq) {
1810
1811                 /* set at zero because free_imbuf_seq... */
1812                 seq->curelem= 0;
1813
1814                 if ((seq->type == SEQ_SOUND) && (seq->ipo)
1815                   &&(seq->startdisp<=cfra+2) && (seq->enddisp>cfra)) do_seq_ipo(seq);
1816
1817                 if(seq->startdisp <=cfra && seq->enddisp > cfra) {
1818
1819                         if(seq->seqbase.first) {
1820                                 if(cfra< seq->start) do_build_seqar_cfra(&seq->seqbase, seqar, seq->start);
1821                                 else if(cfra> seq->start+seq->len-1) do_build_seqar_cfra(&seq->seqbase, seqar, seq->start+seq->len-1);
1822                                 else do_build_seqar_cfra(&seq->seqbase, seqar, cfra);
1823                         }
1824
1825                         **seqar= seq;
1826                         (*seqar)++;
1827
1828                         se=seq->curelem= give_stripelem(seq, cfra);
1829
1830                         if(se) {
1831                                 if(seq->type == SEQ_META) {
1832                                         se->ok= 2;
1833                                         if(se->se1==0) set_meta_stripdata(seq);
1834                                         if(se->se1) {
1835                                                 se->ibuf= se->se1->ibuf;
1836                                         }
1837                                 }
1838                                 else if(seq->type == SEQ_SOUND) {
1839                                         se->ok= 2;
1840                                 }
1841                                 else if(seq->type & SEQ_EFFECT) {
1842
1843                                         /* test if image is too small: reload */
1844                                         if(se->ibuf) {
1845                                                 if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty) {
1846                                                         IMB_freeImBuf(se->ibuf);
1847                                                         se->ibuf= 0;
1848                                                 }
1849                                         }
1850
1851                                         /* does the effect should be recalculated? */
1852
1853                                         if(se->ibuf==0 || (se->se1 != seq->seq1->curelem) || (se->se2 != seq->seq2->curelem) || (se->se3 != seq->seq3->curelem)) {
1854                                                 se->se1= seq->seq1->curelem;
1855                                                 se->se2= seq->seq2->curelem;
1856                                                 se->se3= seq->seq3->curelem;
1857
1858                                                 if(se->ibuf==0) se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1859
1860                                                 do_effect(cfra, seq, se);
1861                                         }
1862
1863                                         /* test size */
1864                                         if(se->ibuf) {
1865                                                 if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty ) {
1866                                                         if(G.scene->r.mode & R_OSA)
1867                                                                 IMB_scaleImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
1868                                                         else
1869                                                                 IMB_scalefastImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
1870                                                 }
1871                                         }
1872                                 }
1873                                 else if(seq->type < SEQ_EFFECT) {
1874
1875                                         if(se->ibuf) {
1876                                                 /* test if image too small: reload */
1877                                                 if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty) {
1878                                                         IMB_freeImBuf(se->ibuf);
1879                                                         se->ibuf= 0;
1880                                                         se->ok= 1;
1881                                                 }
1882                                         }
1883
1884                                         if(seq->type==SEQ_IMAGE) {
1885                                                 if(se->ok && se->ibuf==0) {
1886
1887                                                         /* if playanim or render: no waitcursor */
1888                                                         if((G.f & G_PLAYANIM)==0) waitcursor(1);
1889
1890                                                         strcpy(name, seq->strip->dir);
1891                                                         strcat(name, se->name);
1892                                                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1893                                                         se->ibuf= IMB_loadiffname(name, IB_rect);
1894
1895                                                         if((G.f & G_PLAYANIM)==0) waitcursor(0);
1896
1897                                                         if(se->ibuf==0) se->ok= 0;
1898                                                         else {
1899                                                                 if(se->ibuf->depth==32 && se->ibuf->zbuf==0) converttopremul(se->ibuf);
1900                                                                 seq->strip->orx= se->ibuf->x;
1901                                                                 seq->strip->ory= se->ibuf->y;
1902                                                         }
1903                                                 }
1904                                         }
1905                                         else if(seq->type==SEQ_MOVIE) {
1906                                                 if(se->ok && se->ibuf==0) {
1907
1908                                                         /* if playanim r render: no waitcursor */
1909                                                         if((G.f & G_PLAYANIM)==0) waitcursor(1);
1910
1911                                                         if(seq->anim==0) {
1912                                                                 strcpy(name, seq->strip->dir);
1913                                                                 strcat(name, seq->strip->stripdata->name);
1914                                                                 BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1915
1916                                                                 seq->anim = openanim(name, IB_rect);
1917                                                         }
1918                                                         if(seq->anim) {
1919                                                                 se->ibuf = IMB_anim_absolute(seq->anim, se->nr);
1920                                                         }
1921
1922                                                         if(se->ibuf==0) se->ok= 0;
1923                                                         else {
1924                                                                 if(se->ibuf->depth==32) converttopremul(se->ibuf);
1925                                                                 seq->strip->orx= se->ibuf->x;
1926                                                                 seq->strip->ory= se->ibuf->y;
1927                                                                 if(seq->flag & SEQ_FILTERY) IMB_filtery(se->ibuf);
1928                                                                 if(seq->mul==0.0) seq->mul= 1.0;
1929                                                                 if(seq->mul != 1.0) multibuf(se->ibuf, seq->mul);
1930                                                         }
1931                                                         if((G.f & G_PLAYANIM)==0) waitcursor(0);
1932                                                 }
1933                                         }
1934                                         else if(seq->type==SEQ_SCENE && se->ibuf==0) {
1935                                                 View3D *vd;
1936
1937                                                 oldsce= G.scene;
1938                                                 set_scene_bg(seq->scene);
1939
1940                                                 /* prevent eternal loop */
1941                                                 doseq= G.scene->r.scemode & R_DOSEQ;
1942                                                 G.scene->r.scemode &= ~R_DOSEQ;
1943
1944                                                 /* store stuffies */
1945                                                 oldcfra= CFRA; CFRA= seq->sfra + se->nr;
1946                                                 waitcursor(1);
1947
1948                                                 rectot= R.rectot; R.rectot= 0;
1949                                                 oldx= R.rectx; oldy= R.recty;
1950                                                 /* needed because current 3D window cannot define the layers, like in a background render */
1951                                                 vd= G.vd;
1952                                                 G.vd= 0;
1953
1954                                                 RE_initrender(NULL);
1955                                                 if (!G.background) {
1956                                                         if(R.r.mode & R_FIELDS) update_for_newframe_muted();
1957                                                         R.flag= 0;
1958
1959                                                         free_filesel_spec(G.scene->r.pic);
1960                                                 }
1961
1962                                                 se->ibuf= IMB_allocImBuf(R.rectx, R.recty, 32, IB_rect, 0);
1963                                                 if(R.rectot) memcpy(se->ibuf->rect, R.rectot, 4*R.rectx*R.recty);
1964                                                 if(R.rectz) {
1965                                                         se->ibuf->zbuf= (int *)R.rectz;
1966                                                         /* make sure ibuf frees it */
1967                                                         se->ibuf->mall |= IB_zbuf;
1968                                                         R.rectz= 0;
1969                                                 }
1970
1971                                                 /* and restore */
1972                                                 G.vd= vd;
1973
1974                                                 if((G.f & G_PLAYANIM)==0) waitcursor(0);
1975                                                 CFRA= oldcfra;
1976                                                 if(R.rectot) MEM_freeN(R.rectot);
1977                                                 R.rectot= rectot;
1978                                                 R.rectx=oldx; R.recty=oldy;
1979                                                 G.scene->r.scemode |= doseq;
1980                                                 set_scene_bg(oldsce);
1981
1982                                                 /* restore!! */
1983                                                 R.rectx= seqrectx;
1984                                                 R.recty= seqrecty;
1985
1986                                                 /* added because this flag is checked for
1987                                                  * movie writing when rendering an anim.
1988                                                  * very convoluted. fix. -zr
1989                                                  */
1990                                                 R.r.imtype= G.scene->r.imtype;
1991                                         }
1992
1993                                         /* size test */
1994                                         if(se->ibuf) {
1995                                                 if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty ) {
1996
1997                                                         if (G.scene->r.mode & R_FIELDS) {
1998
1999                                                                 if (seqrecty > 288) IMB_scalefieldImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
2000                                                                 else {
2001                                                                         IMB_de_interlace(se->ibuf);
2002
2003                                                                         if(G.scene->r.mode & R_OSA)
2004                                                                                 IMB_scaleImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
2005                                                                         else
2006                                                                                 IMB_scalefastImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
2007                                                                 }
2008                                                         }
2009                                                         else {
2010                                                                 if(G.scene->r.mode & R_OSA)
2011                                                                         IMB_scaleImBuf(se->ibuf,(short)seqrectx, (short)seqrecty);
2012                                                                 else
2013                                                                         IMB_scalefastImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
2014                                                         }
2015                                                 }
2016                                         }
2017                                 }
2018                         }
2019                 }
2020
2021                 seq= seq->next;
2022         }
2023 }
2024
2025 ImBuf *give_ibuf_seq(int cfra)
2026 {
2027         Sequence **tseqar, **seqar;
2028         Sequence *seq, *seqfirst=0;/*  , *effirst=0; */
2029         Editing *ed;
2030         StripElem *se;
2031         int seqnr, totseq;
2032
2033         /* we make recursively a 'stack' of sequences, these are
2034          * sorted nicely as well.
2035          * this method has been developed especially for stills before or after metas
2036          */
2037
2038         totseq= 0;
2039         ed= G.scene->ed;
2040         if(ed==0) return 0;
2041         do_seq_count_cfra(ed->seqbasep, &totseq, cfra);
2042
2043         if(totseq==0) return 0;
2044
2045         seqrectx= (G.scene->r.size*G.scene->r.xsch)/100;
2046         if(G.scene->r.mode & R_PANORAMA) seqrectx*= G.scene->r.xparts;
2047         seqrecty= (G.scene->r.size*G.scene->r.ysch)/100;
2048
2049
2050         /* tseqar is neede because in do_build_... the pointer changes */
2051         seqar= tseqar= MEM_callocN(sizeof(void *)*totseq, "seqar");
2052
2053         /* this call loads and makes the ibufs */
2054         do_build_seqar_cfra(ed->seqbasep, &seqar, cfra);
2055         seqar= tseqar;
2056
2057         for(seqnr=0; seqnr<totseq; seqnr++) {
2058                 seq= seqar[seqnr];
2059
2060                 se= seq->curelem;
2061                 if((seq->type != SEQ_SOUND) && (se)) {
2062                         if(seq->type==SEQ_META) {
2063
2064                                 /* bottom strip! */
2065                                 if(seqfirst==0) seqfirst= seq;
2066                                 else if(seqfirst->depth > seq->depth) seqfirst= seq;
2067                                 else if(seqfirst->machine > seq->machine) seqfirst= seq;
2068
2069                         }
2070                         else if(seq->type & SEQ_EFFECT) {
2071
2072                                 /* top strip! */
2073                                 if(seqfirst==0) seqfirst= seq;
2074                                 else if(seqfirst->depth > seq->depth) seqfirst= seq;
2075                                 else if(seqfirst->machine < seq->machine) seqfirst= seq;
2076
2077
2078                         }
2079                         else if(seq->type < SEQ_EFFECT) {       /* images */
2080
2081                                 /* bottom strip! a feature that allows you to store junk in locations above */
2082
2083                                 if(seqfirst==0) seqfirst= seq;
2084                                 else if(seqfirst->depth > seq->depth) seqfirst= seq;
2085                                 else if(seqfirst->machine > seq->machine) seqfirst= seq;
2086
2087                         }
2088                 }
2089         }
2090
2091         MEM_freeN(seqar);
2092
2093         if(!seqfirst) return 0;
2094         if(!seqfirst->curelem) return 0;
2095         return seqfirst->curelem->ibuf;
2096
2097 }
2098
2099 void free_imbuf_effect_spec(int cfra)
2100 {
2101         Sequence *seq;
2102         StripElem *se;
2103         Editing *ed;
2104         int a;
2105
2106         ed= G.scene->ed;
2107         if(ed==0) return;
2108
2109         WHILE_SEQ(&ed->seqbase) {
2110
2111                 if(seq->strip) {
2112
2113                         if(seq->type & SEQ_EFFECT) {
2114                                 se= seq->strip->stripdata;
2115                                 for(a=0; a<seq->len; a++, se++) {
2116                                         if(se==seq->curelem && se->ibuf) {
2117                                                 IMB_freeImBuf(se->ibuf);
2118                                                 se->ibuf= 0;
2119                                                 se->ok= 1;
2120                                                 se->se1= se->se2= se->se3= 0;
2121                                         }
2122                                 }
2123                         }
2124                 }
2125         }
2126         END_SEQ
2127 }
2128
2129 void free_imbuf_seq_except(int cfra)
2130 {
2131         Sequence *seq;
2132         StripElem *se;
2133         Editing *ed;
2134         int a;
2135
2136         ed= G.scene->ed;
2137         if(ed==0) return;
2138
2139         WHILE_SEQ(&ed->seqbase) {
2140
2141                 if(seq->strip) {
2142
2143                         if( seq->type==SEQ_META ) {
2144                                 ;
2145                         }
2146                         else {
2147                                 se= seq->strip->stripdata;
2148                                 for(a=0; a<seq->len; a++, se++) {
2149                                         if(se!=seq->curelem && se->ibuf) {
2150                                                 IMB_freeImBuf(se->ibuf);
2151                                                 se->ibuf= 0;
2152                                                 se->ok= 1;
2153                                                 se->se1= se->se2= se->se3= 0;
2154                                         }
2155                                 }
2156                         }
2157
2158                         if(seq->type==SEQ_MOVIE) {
2159                                 if(seq->startdisp > cfra || seq->enddisp < cfra) {
2160                                         if(seq->anim) {
2161                                                 IMB_free_anim(seq->anim);
2162                                                 seq->anim = 0;
2163                                         }
2164                                 }
2165                         }
2166                 }
2167         }
2168         END_SEQ
2169 }
2170
2171 void free_imbuf_seq()
2172 {
2173         Sequence *seq;
2174         StripElem *se;
2175         Editing *ed;
2176         int a;
2177
2178         ed= G.scene->ed;
2179         if(ed==0) return;
2180
2181         WHILE_SEQ(&ed->seqbase) {
2182
2183                 if(seq->strip) {
2184
2185                         if( seq->type==SEQ_META ) {
2186                                 ;
2187                         }
2188                         else {
2189                                 se= seq->strip->stripdata;
2190                                 for(a=0; a<seq->len; a++, se++) {
2191                                         if(se->ibuf) {
2192                                                 IMB_freeImBuf(se->ibuf);
2193                                                 se->ibuf= 0;
2194                                                 se->ok= 1;
2195                                                 se->se1= se->se2= se->se3= 0;
2196                                         }
2197                                 }
2198                         }
2199
2200                         if(seq->type==SEQ_MOVIE) {
2201                                 if(seq->anim) {
2202                                         IMB_free_anim(seq->anim);
2203                                         seq->anim = 0;
2204                                 }
2205                         }
2206                 }
2207         }
2208         END_SEQ
2209 }
2210
2211 void do_render_seq()
2212 {
2213 /*      static ImBuf *lastibuf=0; */
2214         ImBuf *ibuf;
2215
2216         /* copy image into R.rectot */
2217
2218         G.f |= G_PLAYANIM;      /* waitcursor patch */
2219
2220         ibuf= give_ibuf_seq(CFRA);
2221         if(ibuf) {
2222
2223                 memcpy(R.rectot, ibuf->rect, 4*R.rectx*R.recty);
2224
2225                 /* if (ibuf->zbuf) { */
2226                 /*      if (R.rectz) freeN(R.rectz); */
2227                 /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
2228                 /* } */
2229
2230                 free_imbuf_seq_except(CFRA);
2231         }
2232         G.f &= ~G_PLAYANIM;
2233
2234 }