Simplified the ntreeUpdate function by getting rid of the dependency list and limitin...
[blender-staging.git] / release / plugins / sequence / blur.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdio.h>
30 #include "plugin.h"
31
32 /* ******************** GLOBAL VARIABLES ***************** */
33
34
35 char name[24]= "Blur";  
36
37 /* structure for buttons, 
38  *  butcode      name           default  min  max  0
39  */
40
41 VarStruct varstr[]= {
42         LABEL,                  "Input: 1 strip", 0.0, 0.0, 0.0, "", 
43         NUMSLI|FLO,             "Blur",         0.5,    0.0,    10.0, "Maximum filtersize",
44         NUMSLI|FLO,             "Gamma",        1.0,    0.4,    2.0, "Gamma correction", 
45         TOG|INT,                "Animated",     0.0,    0.0,    1.0, "For (Ipo) animated blur", 
46         NUM|INT,                "debug",        0.0,    0.0,    2.0,
47                 "0:off 1: show primary blur buffer 2: show 2nd blur buffer", 
48
49 };
50
51 /* The cast struct is for input in the main doit function
52    Varstr and Cast must have the same variables in the same order */ 
53
54 typedef struct Cast {
55         int dummy;                      /* because of the 'label' button */
56         float blur;
57         float gamma;
58         float use_ipo;
59         int show;
60 } Cast;
61
62
63 /* cfra: the current frame */
64
65 float cfra;
66
67 void plugin_seq_doit(Cast *, float, float, int, int, ImBuf *, ImBuf *, ImBuf *, ImBuf *);
68
69 /* ******************** Fixed functions ***************** */
70
71 int plugin_seq_getversion(void) 
72 {
73         return B_PLUGIN_VERSION;
74 }
75
76 void plugin_but_changed(int but) 
77 {
78 }
79
80 void plugin_init(void)
81 {
82 }
83
84 void plugin_getinfo(PluginInfo *info)
85 {
86         info->name= name;
87         info->nvars= sizeof(varstr)/sizeof(VarStruct);
88         info->cfra= &cfra;
89
90         info->varstr= varstr;
91
92         info->init= plugin_init;
93         info->seq_doit= (SeqDoit) plugin_seq_doit;
94         info->callback= plugin_but_changed;
95 }
96
97
98 void blurbuf(struct ImBuf *ibuf, int nr, Cast *cast)
99 {
100         /* nr = number of blurs */
101         /* the ibuf->rect is replaced */
102         struct ImBuf *tbuf, *ttbuf;
103         int i, x4;
104         
105         tbuf= dupImBuf(ibuf);
106         x4= ibuf->x/4;
107         
108         /* This doesn't seem to work... paprmh */
109         if(cast->gamma != 1.0) gamwarp(tbuf, cast->gamma);
110
111         /* reduce */
112         for(i=0; i<nr; i++) {
113                 ttbuf = onehalf(tbuf);
114                 if (ttbuf) {
115                         freeImBuf(tbuf);
116                         tbuf = ttbuf;
117                 }
118                 if(tbuf->x<4 || tbuf->y<4) break;
119         }
120
121         /* enlarge */
122         for(i=0; i<nr; i++) {
123                 ttbuf = double_x(tbuf);
124                 if (ttbuf) {
125                         freeImBuf(tbuf);
126                         tbuf = ttbuf;
127                 }
128                 ttbuf = double_y(tbuf);
129                 if (ttbuf) {
130                         freeImBuf(tbuf);
131                         tbuf = ttbuf;
132                 }
133                 if(tbuf->x > x4) {
134                         scaleImBuf(tbuf, ibuf->x, ibuf->y);
135                         break;
136                 }
137         }
138         
139         /* this doesn't seem to work...paprmh*/
140         if(cast->gamma != 1.0) gamwarp(tbuf, 1.0 / cast->gamma);
141
142         if(ibuf->rect)memcpy(ibuf->rect, tbuf->rect, 4*ibuf->x*ibuf->y);
143
144         if(ibuf->rect_float) 
145                 memcpy(ibuf->rect_float, tbuf->rect_float, 4*ibuf->x*ibuf->y*sizeof(float));
146
147         freeImBuf(tbuf);
148         
149 }
150
151 void doblur(struct ImBuf *mbuf, float fac, Cast *cast)
152 {
153         /* make two filtered images, like a mipmap structure
154          * fac is filtersize in pixels
155          */
156         struct ImBuf *ibuf, *pbuf;
157         float ifac, pfac, infac;
158         int n, b1, b2;
159         char *irect, *prect, *mrect;
160         float *irectf, *prectf, *mrectf;
161         
162         /* which buffers ? */
163                                 
164         if(fac>7.0) fac= 7.0;
165         if(fac<=1.0) return;
166         
167         pfac= 2.0;
168         pbuf= dupImBuf(mbuf);
169         n= 1;
170         while(pfac < fac) {
171                 blurbuf(pbuf, n, cast);
172                 blurbuf(pbuf, n, cast);
173                 
174                 n++;
175                 pfac+= 1.0;
176         }
177
178         ifac= pfac;
179         pfac-= 1.0;
180
181         ibuf= dupImBuf(pbuf);
182         blurbuf(ibuf, n, cast);
183         blurbuf(ibuf, n, cast);
184         
185         fac= (fac-pfac)/(ifac-pfac);
186         n= mbuf->x*mbuf->y;
187         
188         if(cast->show) fac=cast->show-1;
189         
190         if(mbuf->rect_float){
191                 if(fac>=1) {
192                         memcpy(mbuf->rect_float, ibuf->rect_float, 4*n*sizeof(float));
193                 }
194                 else if(fac<=0) {
195                         memcpy(mbuf->rect_float, pbuf->rect_float, 4*n*sizeof(float));
196                 }
197                 else {  /* interpolate */
198                         infac= 1-fac;
199                         
200                         irectf= (float *)ibuf->rect_float;
201                         prectf= (float *)pbuf->rect_float;
202                         mrectf= (float *)mbuf->rect_float;
203                         while(n--) {
204                                 mrectf[0]= irectf[0]*fac+ prectf[0]*infac;
205                                 mrectf[1]= irectf[1]*fac+ prectf[1]*infac;
206                                 mrectf[2]= irectf[2]*fac+ prectf[2]*infac;
207                                 mrectf[3]= irectf[3]*fac+ prectf[3]*infac;
208                                 mrectf+= 4;
209                                 irectf+= 4;
210                                 prectf+= 4;
211                         }
212                 }
213         }       
214         else if(mbuf->rect){
215                 b1= (int)fac*255.0;
216                 if(b1>255) b1= 255;
217                 b2= 255-b1;
218         
219                 if(b1==255) {
220                         memcpy(mbuf->rect, ibuf->rect, 4*n);
221                 }
222                 else if(b1==0) {
223                         memcpy(mbuf->rect, pbuf->rect, 4*n);
224                 }
225                 else {  /* interpolate */
226                         irect= (char *)ibuf->rect;
227                         prect= (char *)pbuf->rect;
228                         mrect= (char *)mbuf->rect;
229                         while(n--) {
230                                 mrect[0]= (irect[0]*b1+ prect[0]*b2)>>8;
231                                 mrect[1]= (irect[1]*b1+ prect[1]*b2)>>8;
232                                 mrect[2]= (irect[2]*b1+ prect[2]*b2)>>8;
233                                 mrect[3]= (irect[3]*b1+ prect[3]*b2)>>8;
234                                 mrect+= 4;
235                                 irect+= 4;
236                                 prect+= 4;
237                         }
238                 }
239         }
240
241         freeImBuf(ibuf);
242         freeImBuf(pbuf);
243 }
244
245
246 void plugin_seq_doit(Cast *cast, float facf0, float facf1, int x, int y, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use)
247 {
248         float  bfacf0, bfacf1;
249         
250         if(cast->use_ipo==0) {
251                 bfacf0= bfacf1= cast->blur+1.0;
252         }
253         else {
254                 bfacf0 = (facf0 * 6.0) + 1.0;
255                 bfacf1 = (facf1 * 6.0) + 1.0;
256         }
257
258         if(out->rect) memcpy(out->rect, ibuf1->rect, 4*out->x*out->y);
259         if(out->rect_float) memcpy(out->rect_float, ibuf1->rect_float, 4*out->x*out->y*sizeof(float));
260         
261 /****************I can't get this field code to work... works ok without...paprmh****************/
262
263
264         /* it blurs interlaced, only tested with even fields */
265
266 /*      de_interlace(out);*/
267         /* otherwise scaling goes wrong */
268 /*      out->flags &= ~IB_fields;*/
269         
270         doblur(out, bfacf0, cast); /*fieldA*/
271
272 /*      if(out->rect)out->rect += out->x * out->y;
273         if(out->rect_float)out->rect_float += out->x * out->y;
274                 
275         doblur(out, bfacf1, cast);*/ /*fieldB*/
276
277 /*      if(out->rect)out->rect -= out->x * out->y;
278         if(out->rect_float)out->rect_float -= out->x * out->y;
279         out->flags |= IB_fields;
280
281         interlace(out);*/
282         
283 }
284