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