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