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