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