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