Remove the svn:keywords property.
[blender.git] / release / plugins / sequence / scatter.c
1 /**
2  * $Id: scatter.c 36271 2011-04-21 13:11:51Z campbellbarton $
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 "plugin.h"
31
32 /* ******************** GLOBAL VARIABLES ***************** */
33
34
35 char name[24]= "scatter";
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         NUM|INT,        "seed: ",               1.0,    0.0, 10.0, "Offset in random table", 
44         NUMSLI|FLO,     "swing: ",              1.0,    0.0, 3.0, "The amplitude, width of the effect", 
45         TOG|INT,        "wrap",                 0.0,    0.0, 1.0, "Cyclic wrap around the left/right edges", 
46         NUM|INT,        "type: ",               1.0,    0.0, 1.0, "Type 1 is random for each frame", 
47 };
48
49 /* The cast struct is for input in the main doit function
50    Varstr and Cast must have the same variables in the same order */ 
51
52 typedef struct Cast {
53         int dummy;                      /* because of the 'label' button */
54         int seed;
55         float swing;
56         int wrap;
57         int type;
58 } Cast;
59
60 /* cfra: the current frame */
61
62 float cfra;
63
64 void plugin_seq_doit(Cast *, float, float, int, int, ImBuf *, ImBuf *, ImBuf *, ImBuf *);
65
66
67 /* ******************** Fixed functions ***************** */
68
69 int plugin_seq_getversion(void) 
70 {
71         return B_PLUGIN_VERSION;
72 }
73
74 void plugin_but_changed(int but) 
75 {
76 }
77
78 void plugin_init()
79 {
80 }
81
82 void plugin_getinfo(PluginInfo *info)
83 {
84         info->name= name;
85         info->nvars= sizeof(varstr)/sizeof(VarStruct);
86         info->cfra= &cfra;
87
88         info->varstr= varstr;
89
90         info->init= plugin_init;
91         info->seq_doit= (SeqDoit) plugin_seq_doit;
92         info->callback= plugin_but_changed;
93 }
94
95
96 /* ************************************************************
97         Scatter
98         
99    ************************************************************ */
100
101 static void rectcpy(ImBuf *dbuf, ImBuf *sbuf,   
102                                 int destx, int desty, 
103                                 int srcx, int srcy, int width, int height)
104 {
105         uint *drect,*srect;
106         float *dfrect, *sfrect;
107         int tmp;
108
109         if (dbuf == 0) return;
110
111         if (destx < 0){
112                 srcx -= destx ;
113                 width += destx ;
114                 destx = 0;
115         }
116         if (srcx < 0){
117                 destx -= srcx ;
118                 width += destx ;
119                 srcx = 0;
120         }
121         if (desty < 0){
122                 srcy -= desty ;
123                 height += desty ;
124                 desty = 0;
125         }
126         if (srcy < 0){
127                 desty -= srcy ;
128                 height += desty ;
129                 srcy = 0;
130         }
131
132         if (width > dbuf->x - destx) width = dbuf->x - destx;
133         if (height > dbuf->y - desty) height = dbuf->y - desty;
134         if (sbuf){
135                 if (width > sbuf->x - srcx) width = sbuf->x - srcx;
136                 if (height > sbuf->y - srcy) height = sbuf->y - srcy;
137                 srect = sbuf->rect;
138                 sfrect = sbuf->rect_float;
139         }
140
141         if (width <= 0) return;
142         if (height <= 0) return;
143
144         drect = dbuf->rect;
145         dfrect = dbuf->rect_float;
146
147         tmp = (desty * dbuf->x + destx);
148
149         if (dbuf->rect_float) dfrect += 4 * tmp;
150         else drect += tmp;
151
152         destx = dbuf->x;
153
154         if (sbuf) {
155                 tmp = (srcy * sbuf->x + srcx );
156                 if (dbuf->rect_float) sfrect += 4 * tmp; 
157                 else srect += tmp;
158                 srcx = sbuf->x;
159         } else{
160                 if (dbuf->rect_float) sfrect = dfrect;
161                 else srect = drect;
162                 srcx = destx;
163         }
164
165         for (;height > 0; height--){
166                 if (dbuf->rect_float) {
167                         memcpy(dfrect,sfrect, 4 * width * sizeof(float));
168                         dfrect += destx;
169                         sfrect += srcx;
170                 } else {
171                         memcpy(drect,srect, width * sizeof(int));
172                         drect += destx;
173                         srect += srcx;
174                 }
175         }
176 }
177
178 static void fill_out(ImBuf *out, float r, float g, float b, float a)
179 {
180         int tot,x;
181         float *rectf = out->rect_float;
182         unsigned char *rect = (unsigned char *)out->rect;
183
184         tot = out->x * out->y;
185         if (out->rect_float) {
186                 for (x = 0;x < tot; x++) {
187                         rectf[0] = r;
188                         rectf[1] = g;
189                         rectf[2] = b;
190                         rectf[3] = a;
191                         rectf += 4;
192                 }
193         } else {
194                 for (x=0;x < tot;x++) {
195                         rect[0] = (int)(r * 255);
196                         rect[1] = (int)(g * 255);
197                         rect[2] = (int)(b * 255);
198                         rect[3] = (int)(a * 255);
199                         rect += 4;
200                 }
201         }
202 }
203
204
205 void plugin_seq_doit(Cast *cast, float facf0, float facf1, int sx, int sy, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use)
206 {
207         float f1, f2, t1, t2, t3;
208         int x, y, lr;
209         
210         /* fill imbuf 'out' with black */
211         fill_out(out, 0,0,0,0);
212
213
214         switch (cast->type) {
215                 case 0:
216                         srand48(cast->seed);
217                         break;
218                 case 1:
219                         srand48(cast->seed + facf0 * 1000);
220                         break;
221         }
222
223         for (y = 0; y < sy; y++) {
224                 switch (cast->type) {
225                         case 0:
226                                 if ((y & 1) == 0) {
227                                         f1 = drand48() - 0.5;
228                                         f2 = drand48() - 0.5;
229                                         f1 = cast->swing * f1;
230                                         f2 = cast->swing * f2;
231                                         if (cast->wrap) f2 += 1.0;
232                                         lr = drand48()>0.5;
233                                         t1 = facf0;
234                                 } else t1 = facf1;
235                                 
236                                 t2 = 1.0 - t1;
237                                 t3 = 3.0 * (f1 * t1 * t1 * t2 + f2 * t1 * t2 * t2);
238                                 if (cast->wrap) t3 += t2 * t2 * t2;
239                                 x = sx * t3;
240                                 if (lr) x = -x;
241                                 break;
242                         case 1:
243                                 f1 = drand48() - 0.5;
244                                 f1 = f1 * cast->swing;
245                                 if ((y & 1) == 0) f1 *= facf0;
246                                 else f1 *= facf1;
247                                 x = f1 * sx;
248                                 break;
249                 }
250                 
251                 rectcpy(out, ibuf1, 0, y, x, y, 32767, 1);
252                 if (cast->wrap) {
253                         rectcpy(out, ibuf1, 0, y, x + sx, y, 32767, 1);
254                         rectcpy(out, ibuf1, 0, y, x + sx + sx, y, 32767, 1);
255                         rectcpy(out, ibuf1, 0, y, x - sx, y, 32767, 1);
256                         rectcpy(out, ibuf1, 0, y, x - sx - sx, y, 32767, 1);
257                 }
258         }
259 }
260