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