This is taking me longer than I wanted.
[blender.git] / release / plugins / sequence / blur.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 "plugin.h"
34
35 /* ******************** GLOBAL VARIABLES ***************** */
36
37
38 char name[24]= "Blur";  
39
40 /* structure for buttons, 
41  *  butcode      name           default  min  max  0
42  */
43
44 VarStruct varstr[]= {
45         LABEL,                  "Input: 1 strip", 0.0, 0.0, 0.0, "", 
46         NUMSLI|FLO,             "Blur",         0.5,    0.0,    10.0, "Maximum filtersize", 
47         NUMSLI|FLO,             "Gamma",        1.0,    0.4,    2.0, "Gamma correction", 
48         TOG|INT,                "Animated",     0.0,    0.0,    1.0, "For (Ipo) animated blur", 
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 } Cast;
60
61
62 /* cfra: the current frame */
63
64 float cfra;
65
66 void plugin_seq_doit(Cast *, float, float, int, int, ImBuf *, ImBuf *, ImBuf *, ImBuf *);
67
68 /* ******************** Fixed functions ***************** */
69
70 int plugin_seq_getversion(void) 
71 {
72         return B_PLUGIN_VERSION;
73 }
74
75 void plugin_but_changed(int but) 
76 {
77 }
78
79 void plugin_init()
80 {
81 }
82
83 void plugin_getinfo(PluginInfo *info)
84 {
85         info->name= name;
86         info->nvars= sizeof(varstr)/sizeof(VarStruct);
87         info->cfra= &cfra;
88
89         info->varstr= varstr;
90
91         info->init= plugin_init;
92         info->seq_doit= (SeqDoit) plugin_seq_doit;
93         info->callback= plugin_but_changed;
94 }
95
96
97 void blurbuf(struct ImBuf *ibuf, int nr, Cast *cast)
98 {
99         /* nr = number of blurs */
100         /* the ibuf->rect is replaced */
101         struct ImBuf *tbuf, *ttbuf;
102         int i, x4;
103         
104         tbuf= dupImBuf(ibuf);
105         x4= ibuf->x/4;
106         
107         if(cast->gamma != 1.0) gamwarp(tbuf, cast->gamma);
108
109         /* reduce */
110         for(i=0; i<nr; i++) {
111                 ttbuf = onehalf(tbuf);
112                 if (ttbuf) {
113                         freeImBuf(tbuf);
114                         tbuf = ttbuf;
115                 }
116                 if(tbuf->x<4 || tbuf->y<4) break;
117         }
118
119         /* enlarge */
120         for(i=0; i<nr; i++) {
121                 ttbuf = double_x(tbuf);
122                 if (ttbuf) {
123                         freeImBuf(tbuf);
124                         tbuf = ttbuf;
125                 }
126                 ttbuf = double_y(tbuf);
127                 if (ttbuf) {
128                         freeImBuf(tbuf);
129                         tbuf = ttbuf;
130                 }
131                 
132                 if(tbuf->x > x4) {
133                         scaleImBuf(tbuf, ibuf->x, ibuf->y);
134                         break;
135                 }
136         }
137         
138         if(cast->gamma != 1.0) gamwarp(tbuf, 1.0 / cast->gamma);
139
140         /* Very bad code warning! This fails badly with float-buffers!!! */
141         if (ibuf->rect_float) {
142                 freeN(ibuf->rect_float);
143                 ibuf->rect_float= tbuf->rect_float;
144         } else {
145                 freeN(ibuf->rect);
146                 ibuf->rect= tbuf->rect;
147         }
148         freeN(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;
159         int n, b1, b2;
160         char *irect, *prect, *mrect;
161         float *irectf, *prectf, *mrectf;
162         
163         /* wich buffers ? */
164                                 
165         if(fac>7.0) fac= 7.0;
166         if(fac<=1.0) return;
167         
168         pfac= 2.0;
169
170         pbuf= dupImBuf(mbuf);
171         n= 1;
172         while(pfac < fac) {
173                 blurbuf(pbuf, n, cast);
174                 blurbuf(pbuf, n, cast);
175                 
176                 n++;
177                 pfac+= 1.0;
178         }
179
180         ifac= pfac;
181         pfac-= 1.0;
182
183         ibuf= dupImBuf(pbuf);
184         blurbuf(ibuf, n, cast);
185         blurbuf(ibuf, n, cast);
186         
187         fac = (fac-pfac)/(ifac-pfac);
188         b1= 255.0*fac;
189         if(b1>255) b1= 255;
190         b2= 255-b1;
191         
192         if(b1==255) {
193                 if (mbuf->rect_float) 
194                         memcpy(mbuf->rect_float, ibuf->rect_float, 4*ibuf->x*ibuf->y * sizeof(float));
195                 else
196                         memcpy(mbuf->rect_float, ibuf->rect_float, 4*ibuf->x*ibuf->y);
197         }
198         else if(b1==0) {
199                 if (mbuf->rect_float)
200                         memcpy(mbuf->rect_float, pbuf->rect_float, 4*pbuf->x*pbuf->y * sizeof(float));
201                 else
202                         memcpy(mbuf->rect, pbuf->rect, 4*pbuf->x*pbuf->y);
203         }
204         else {  /* interpolate */
205                 n= ibuf->x*ibuf->y;
206                 if (mbuf->rect_float) {
207                         irectf= ibuf->rect_float;
208                         prectf= pbuf->rect_float;
209                         mrectf= mbuf->rect_float;
210                         while(n--) {
211                                 mrectf[0]= (irectf[0]*fac+ prectf[0]*(1-fac))/2;
212                                 mrectf[1]= (irectf[1]*fac+ prectf[1]*(1-fac))/2;
213                                 mrectf[2]= (irectf[2]*fac+ prectf[2]*(1-fac))/2;
214                                 mrectf[3]= (irectf[3]*fac+ prectf[3]*(1-fac))/2;
215                                 mrectf[0]= 1;
216                                 mrectf[1]= 1;
217                                 mrectf[2]= 1;
218                                 mrectf[3]= 1;
219                                 mrectf+= 4;
220                                 irectf+= 4;
221                                 prectf+= 4;
222                         }
223                 } else {
224                         irect= (char *)ibuf->rect;
225                         prect= (char *)pbuf->rect;
226                         mrect= (char *)mbuf->rect;
227                         while(n--) {
228                                 mrect[0]= (irect[0]*b1+ prect[0]*b2)>>8;
229                                 mrect[1]= (irect[1]*b1+ prect[1]*b2)>>8;
230                                 mrect[2]= (irect[2]*b1+ prect[2]*b2)>>8;
231                                 mrect[3]= (irect[3]*b1+ prect[3]*b2)>>8;
232                                 mrect+= 4;
233                                 irect+= 4;
234                                 prect+= 4;
235                         }
236                 }
237         }
238         freeImBuf(ibuf);
239         freeImBuf(pbuf);
240 }
241
242
243 void plugin_seq_doit(Cast *cast, float facf0, float facf1, int x, int y, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use)
244 {
245         float  bfacf0, bfacf1;
246         
247         if(cast->use_ipo==0) {
248                 bfacf0= bfacf1= cast->blur+1.0;
249         }
250         else {
251                 bfacf0 = (facf0 * 6.0) + 1.0;
252                 bfacf1 = (facf1 * 6.0) + 1.0;
253         }
254
255         if (out->rect_float) memcpy(out->rect_float, ibuf1->rect_float, 4*out->x*out->y * sizeof(float));
256         else memcpy(out->rect, ibuf1->rect, 4*out->x*out->y);
257
258         /* it blurs interlaced, only tested with even fields */
259         de_interlace(out);
260
261         /* otherwise scaling goes wrong */
262         out->flags &= ~IB_fields;
263         
264         doblur(out, bfacf0, cast); /*fieldA*/
265
266         if (out->rect_float) out->rect_float += out->x * out->y;
267         else out->rect += out->x * out->y;
268
269         doblur(out, bfacf1, cast); /*fieldB*/
270
271         if (out->rect_float) out->rect_float -= out->x * out->y;
272         else out->rect -= out->x * out->y;
273
274         out->flags |= IB_fields;
275
276         interlace(out);
277         
278 }
279