1507
|
1 /* Paranormal - A highly customizable audio visualization library
|
|
2 * Copyright (C) 2001 Jamie Gennis <jgennis@mindspring.com>
|
|
3 *
|
|
4 * This library is free software; you can redistribute it and/or
|
|
5 * modify it under the terms of the GNU Library General Public
|
|
6 * License as published by the Free Software Foundation; either
|
|
7 * version 2 of the License, or (at your option) any later version.
|
|
8 *
|
|
9 * This library is distributed in the hope that it will be useful,
|
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12 * Library General Public License for more details.
|
|
13 *
|
|
14 * You should have received a copy of the GNU Library General Public
|
|
15 * License along with this library; if not, write to the Free
|
|
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17 */
|
|
18
|
|
19 #include <stdio.h>
|
|
20 #include <math.h>
|
|
21
|
|
22 #include <glib.h>
|
|
23 #include "pnrotozoom.h"
|
|
24 #include "pnstringoption.h"
|
|
25 #include "pnscript.h"
|
|
26 #include "pnsymboltable.h"
|
|
27 #include "pncpu.h"
|
|
28
|
|
29 static void pn_roto_zoom_class_init (PnRotoZoomClass *class);
|
|
30 static void pn_roto_zoom_init (PnRotoZoom *roto_zoom,
|
|
31 PnRotoZoomClass *class);
|
|
32
|
|
33 /* PnObject signals */
|
|
34 static void pn_roto_zoom_destroy (PnObject *object);
|
|
35
|
|
36 /* PnActuator methods */
|
|
37 static void pn_roto_zoom_prepare (PnRotoZoom *roto_zoom,
|
|
38 PnImage *image);
|
|
39 static void pn_roto_zoom_execute (PnRotoZoom *roto_zoom,
|
|
40 PnImage *image,
|
|
41 PnAudioData *audio_data);
|
|
42
|
|
43 static PnActuatorClass *parent_class = NULL;
|
|
44
|
|
45 GType
|
|
46 pn_roto_zoom_get_type (void)
|
|
47 {
|
|
48 static GType roto_zoom_type = 0;
|
|
49
|
|
50 if (! roto_zoom_type)
|
|
51 {
|
|
52 static const GTypeInfo roto_zoom_info =
|
|
53 {
|
|
54 sizeof (PnRotoZoomClass),
|
|
55 NULL, /* base_init */
|
|
56 NULL, /* base_finalize */
|
|
57 (GClassInitFunc) pn_roto_zoom_class_init,
|
|
58 NULL, /* class_finalize */
|
|
59 NULL, /* class_data */
|
|
60 sizeof (PnRotoZoom),
|
|
61 0, /* n_preallocs */
|
|
62 (GInstanceInitFunc) pn_roto_zoom_init
|
|
63 };
|
|
64
|
|
65 /* FIXME: should this be dynamic? */
|
|
66 roto_zoom_type = g_type_register_static (PN_TYPE_ACTUATOR,
|
|
67 "PnRotoZoom",
|
|
68 &roto_zoom_info,
|
|
69 0);
|
|
70 }
|
|
71 return roto_zoom_type;
|
|
72 }
|
|
73
|
|
74 static void
|
|
75 pn_roto_zoom_class_init (PnRotoZoomClass *class)
|
|
76 {
|
|
77 PnObjectClass *object_class;
|
|
78 PnUserObjectClass *user_object_class;
|
|
79 PnActuatorClass *actuator_class;
|
|
80
|
|
81 parent_class = g_type_class_peek_parent (class);
|
|
82
|
|
83 object_class = (PnObjectClass *) class;
|
|
84 user_object_class = (PnUserObjectClass *) class;
|
|
85 actuator_class = (PnActuatorClass *) class;
|
|
86
|
|
87 /* PnObject signals */
|
|
88 object_class->destroy = pn_roto_zoom_destroy;
|
|
89
|
|
90 /* PnActuator methods */
|
|
91 actuator_class->prepare = (PnActuatorPrepFunc) pn_roto_zoom_prepare;
|
|
92 actuator_class->execute = (PnActuatorExecFunc) pn_roto_zoom_execute;
|
|
93 }
|
|
94
|
|
95 static void
|
|
96 pn_roto_zoom_init (PnRotoZoom *roto_zoom, PnRotoZoomClass *class)
|
|
97 {
|
|
98 PnStringOption *init_script_opt, *frame_script_opt;
|
|
99
|
|
100 /* Set up the name and description */
|
|
101 pn_user_object_set_name (PN_USER_OBJECT (roto_zoom), "Transform.Roto_Zoom");
|
|
102 pn_user_object_set_description (PN_USER_OBJECT (roto_zoom),
|
|
103 "Rotate and zoom the image");
|
|
104
|
|
105 /* Set up the options */
|
|
106 init_script_opt = pn_string_option_new ("init_script", "The initialization script");
|
|
107 frame_script_opt = pn_string_option_new ("frame_script", "The per-frame script");
|
|
108
|
|
109 pn_actuator_add_option (PN_ACTUATOR (roto_zoom), PN_OPTION (init_script_opt));
|
|
110 pn_actuator_add_option (PN_ACTUATOR (roto_zoom), PN_OPTION (frame_script_opt));
|
|
111
|
|
112 /* Create the script objects and symbol table */
|
|
113 roto_zoom->init_script = pn_script_new ();
|
|
114 pn_object_ref (PN_OBJECT (roto_zoom->init_script));
|
|
115 pn_object_sink (PN_OBJECT (roto_zoom->init_script));
|
|
116 roto_zoom->frame_script = pn_script_new ();
|
|
117 pn_object_ref (PN_OBJECT (roto_zoom->frame_script));
|
|
118 pn_object_sink (PN_OBJECT (roto_zoom->frame_script));
|
|
119 roto_zoom->symbol_table = pn_symbol_table_new ();
|
|
120 pn_object_ref (PN_OBJECT (roto_zoom->symbol_table));
|
|
121 pn_object_sink (PN_OBJECT (roto_zoom->symbol_table));
|
|
122
|
|
123 /* Get the variables from the symbol table */
|
|
124 roto_zoom->zoom_var = pn_symbol_table_ref_variable_by_name (roto_zoom->symbol_table, "zoom");
|
|
125 roto_zoom->theta_var = pn_symbol_table_ref_variable_by_name (roto_zoom->symbol_table, "theta");
|
|
126 roto_zoom->volume_var = pn_symbol_table_ref_variable_by_name (roto_zoom->symbol_table, "volume");
|
|
127 }
|
|
128
|
|
129 static void
|
|
130 pn_roto_zoom_destroy (PnObject *object)
|
|
131 {
|
|
132 PnRotoZoom *roto_zoom;
|
|
133
|
|
134 roto_zoom = (PnRotoZoom *) object;
|
|
135
|
|
136 pn_object_unref (PN_OBJECT (roto_zoom->init_script));
|
|
137 pn_object_unref (PN_OBJECT (roto_zoom->frame_script));
|
|
138 pn_object_unref (PN_OBJECT (roto_zoom->symbol_table));
|
|
139 }
|
|
140
|
|
141 static void
|
|
142 pn_roto_zoom_prepare (PnRotoZoom *roto_zoom, PnImage *image)
|
|
143 {
|
|
144 PnStringOption *init_script_opt, *frame_script_opt;
|
|
145
|
|
146 g_return_if_fail (roto_zoom != NULL);
|
|
147 g_return_if_fail (PN_IS_ROTO_ZOOM (roto_zoom));
|
|
148 g_return_if_fail (image != NULL);
|
|
149 g_return_if_fail (PN_IS_IMAGE (image));
|
|
150
|
|
151 /* Parse the script strings */
|
|
152 init_script_opt =
|
|
153 (PnStringOption *) pn_actuator_get_option_by_index (PN_ACTUATOR (roto_zoom),
|
|
154 PN_ROTO_ZOOM_OPT_INIT_SCRIPT);
|
|
155 frame_script_opt =
|
|
156 (PnStringOption *) pn_actuator_get_option_by_index (PN_ACTUATOR (roto_zoom),
|
|
157 PN_ROTO_ZOOM_OPT_FRAME_SCRIPT);
|
|
158
|
|
159 pn_script_parse_string (roto_zoom->init_script, roto_zoom->symbol_table,
|
|
160 pn_string_option_get_value (init_script_opt));
|
|
161 pn_script_parse_string (roto_zoom->frame_script, roto_zoom->symbol_table,
|
|
162 pn_string_option_get_value (frame_script_opt));
|
|
163
|
|
164 /* Set up in-script vars for no rotozooming */
|
|
165 PN_VARIABLE_VALUE (roto_zoom->zoom_var) = 1.0;
|
|
166 PN_VARIABLE_VALUE (roto_zoom->theta_var) = 0.0;
|
|
167
|
|
168 /* Run the init script */
|
|
169 pn_script_execute (roto_zoom->init_script);
|
|
170
|
|
171 if (PN_ACTUATOR_CLASS (parent_class)->prepare)
|
|
172 PN_ACTUATOR_CLASS (parent_class)->prepare (PN_ACTUATOR (roto_zoom), image);
|
|
173 }
|
|
174
|
|
175 static void
|
|
176 pn_roto_zoom_execute (PnRotoZoom *roto_zoom, PnImage *image, PnAudioData *audio_data)
|
|
177 {
|
|
178 gint width, height, stride;
|
|
179 gdouble ax, ay, bx, by, cx, cy;
|
|
180 gint xdx, xdy, ydx, ydy;
|
|
181 gint xx, xy, yx, yy;
|
|
182 gint i, j;
|
|
183 gdouble zoom, theta, r;
|
|
184
|
|
185 PnColor *src, *dest;
|
|
186
|
|
187 g_return_if_fail (roto_zoom != NULL);
|
|
188 g_return_if_fail (PN_IS_ROTO_ZOOM (roto_zoom));
|
|
189 g_return_if_fail (image != NULL);
|
|
190 g_return_if_fail (PN_IS_IMAGE (image));
|
|
191 g_return_if_fail (audio_data != NULL);
|
|
192 g_return_if_fail (PN_IS_AUDIO_DATA (audio_data));
|
|
193
|
|
194 width = pn_image_get_width (image);
|
|
195 height = pn_image_get_height (image);
|
|
196 stride = pn_image_get_pitch (image) >> 2;
|
|
197 src = pn_image_get_image_buffer (image);
|
|
198 dest = pn_image_get_transform_buffer (image);
|
|
199
|
|
200 /* set up the volume in-script variable */
|
|
201 PN_VARIABLE_VALUE (roto_zoom->volume_var) = pn_audio_data_get_volume (audio_data);
|
|
202
|
|
203 /* run the frame scipt */
|
|
204 pn_script_execute (roto_zoom->frame_script);
|
|
205
|
|
206 /* get the in-script variable values */
|
|
207 zoom = PN_VARIABLE_VALUE (roto_zoom->zoom_var);
|
|
208 theta = PN_VARIABLE_VALUE (roto_zoom->theta_var);
|
|
209
|
|
210 if (zoom == 0.0)
|
|
211 return;
|
|
212
|
|
213 r = G_SQRT2 / zoom;
|
|
214
|
|
215 ax = r * cos ((G_PI * .75) + theta);
|
|
216 ay = r * sin ((G_PI * .75) + theta);
|
|
217 bx = r * cos ((G_PI * .25) + theta);
|
|
218 by = r * sin ((G_PI * .25) + theta);
|
|
219 cx = r * cos ((G_PI * 1.25) + theta);
|
|
220 cy = r * sin ((G_PI * 1.25) + theta);
|
|
221
|
|
222 /* fprintf (stderr, "ax: %f, ay: %f\n", ax, ay); */
|
|
223 /* fprintf (stderr, "bx: %f, by: %f\n", bx, by); */
|
|
224 /* fprintf (stderr, "cx: %f, cy: %f\n", cx, cy); */
|
|
225
|
|
226 ax = ((width-1)<<8) * ((ax + 1) * .5);
|
|
227 ay = ((height-1)<<8) * ((-ay + 1) * .5);
|
|
228 bx = ((width-1)<<8) * ((bx + 1) * .5);
|
|
229 by = ((height-1)<<8) * ((-by + 1) * .5);
|
|
230 cx = ((width-1)<<8) * ((cx + 1) * .5);
|
|
231 cy = ((height-1)<<8) * ((-cy + 1) * .5);
|
|
232
|
|
233 /* fprintf (stderr, "ax': %f, ay': %f\n", ax, ay); */
|
|
234 /* fprintf (stderr, "bx': %f, by': %f\n", bx, by); */
|
|
235 /* fprintf (stderr, "cx': %f, cy': %f\n", cx, cy); */
|
|
236
|
|
237 xdx = (bx - ax) / (width-1);
|
|
238 xdy = (by - ay) / (width-1);
|
|
239 ydx = (cx - ax) / (height-1);
|
|
240 ydy = (cy - ay) / (height-1);
|
|
241
|
|
242 /* fprintf (stderr, "xdx: %i, xdy: %i\n", xdx, xdy); */
|
|
243 /* fprintf (stderr, "ydx: %i, ydy: %i\n", ydx, ydy); */
|
|
244
|
|
245 yx = ax;
|
|
246 yy = ay;
|
|
247
|
|
248 for (j=0; j<height; j++)
|
|
249 {
|
|
250 while (yx < 0) yx += width<<8;
|
|
251 while (yy < 0) yy += height<<8;
|
|
252 while (yx >= width<<8) yx -= width<<8;
|
|
253 while (yy >= height<<8) yy -= height<<8;
|
|
254
|
|
255 xx = yx;
|
|
256 xy = yy;
|
|
257
|
|
258 for (i=0; i<width; i++)
|
|
259 {
|
|
260 guint r, g, b, offset;
|
|
261 guint xfrac, yfrac;
|
|
262
|
|
263 while (xx < 0) xx += width<<8;
|
|
264 while (xy < 0) xy += height<<8;
|
|
265 while (xx >= width<<8) xx -= width<<8;
|
|
266 while (xy >= height<<8) xy -= height<<8;
|
|
267
|
|
268 offset = (xy >> 8) * stride + (xx >> 8);
|
|
269 xfrac = xx & 0xff;
|
|
270 yfrac = xy & 0xff;
|
|
271
|
|
272 r = src[offset].red * (256 - xfrac) * (256 - yfrac);
|
|
273 g = src[offset].green * (256 - xfrac) * (256 - yfrac);
|
|
274 b = src[offset].blue * (256 - xfrac) * (256 - yfrac);
|
|
275
|
|
276 if (xx >> 8 < width-1)
|
|
277 {
|
|
278 r += src[offset+1].red * xfrac * (256 - yfrac);
|
|
279 g += src[offset+1].green * xfrac * (256 - yfrac);
|
|
280 b += src[offset+1].blue * xfrac * (256 - yfrac);
|
|
281 }
|
|
282 else
|
|
283 {
|
|
284 r += src[(xy>>8) * stride].red * xfrac * (256 - yfrac);
|
|
285 g += src[(xy>>8) * stride].green * xfrac * (256 - yfrac);
|
|
286 b += src[(xy>>8) * stride].blue * xfrac * (256 - yfrac);
|
|
287 }
|
|
288
|
|
289 if (xy >> 8 < height-1)
|
|
290 {
|
|
291 r += src[offset+stride].red * (256 - xfrac) * yfrac;
|
|
292 g += src[offset+stride].green * (256 - xfrac) * yfrac;
|
|
293 b += src[offset+stride].blue * (256 - xfrac) * yfrac;
|
|
294 }
|
|
295 else
|
|
296 {
|
|
297 r += src[xx>>8].red * (256 - xfrac) * yfrac;
|
|
298 g += src[xx>>8].green * (256 - xfrac) * yfrac;
|
|
299 b += src[xx>>8].blue * (256 - xfrac) * yfrac;
|
|
300 }
|
|
301
|
|
302 if (xx >> 8 < width-1 && xy >> 8 < height-1)
|
|
303 {
|
|
304 r += src[offset+stride+1].red * xfrac * yfrac;
|
|
305 g += src[offset+stride+1].green * xfrac * yfrac;
|
|
306 b += src[offset+stride+1].blue * xfrac * yfrac;
|
|
307 }
|
|
308 else
|
|
309 {
|
|
310 r += src[0].red * xfrac * yfrac;
|
|
311 g += src[0].green * xfrac * yfrac;
|
|
312 b += src[0].blue * xfrac * yfrac;
|
|
313 }
|
|
314
|
|
315 dest[j * stride + i].red = r >> 16;
|
|
316 dest[j * stride + i].green = g >> 16;
|
|
317 dest[j * stride + i].blue = b >> 16;
|
|
318
|
|
319 xx += xdx;
|
|
320 xy += xdy;
|
|
321 }
|
|
322
|
|
323 yx += ydx;
|
|
324 yy += ydy;
|
|
325 }
|
|
326
|
|
327 /* fprintf (stderr, "xx: %i, xy: %i\n", xx, xy); */
|
|
328
|
|
329 pn_image_apply_transform (image);
|
|
330
|
|
331 if (PN_ACTUATOR_CLASS (parent_class)->execute != NULL)
|
|
332 PN_ACTUATOR_CLASS (parent_class)->execute (PN_ACTUATOR (roto_zoom), image, audio_data);
|
|
333 }
|
|
334
|
|
335 PnRotoZoom*
|
|
336 pn_roto_zoom_new (void)
|
|
337 {
|
|
338 return (PnRotoZoom *) g_object_new (PN_TYPE_ROTO_ZOOM, NULL);
|
|
339 }
|