2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
5 * Copyright (C) 2001 FUKUCHI Kentarou
7 * Inspired by Adrian Likin's script for the GIMP.
8 * EffecTV is free software. This library is free software;
9 * you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library 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 GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
29 #include <gst/video/gstvideofilter.h>
34 #include <gst/video/video.h>
37 #define M_PI 3.14159265358979323846
40 #define GST_TYPE_SHAGADELICTV \
41 (gst_shagadelictv_get_type())
42 #define GST_SHAGADELICTV(obj) \
43 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SHAGADELICTV,GstShagadelicTV))
44 #define GST_SHAGADELICTV_CLASS(klass) \
45 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_SHAGADELICTV,GstShagadelicTVClass))
46 #define GST_IS_SHAGADELICTV(obj) \
47 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SHAGADELICTV))
48 #define GST_IS_SHAGADELICTV_CLASS(klass) \
49 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_SHAGADELICTV))
51 typedef struct _GstShagadelicTV GstShagadelicTV;
52 typedef struct _GstShagadelicTVClass GstShagadelicTVClass;
54 struct _GstShagadelicTV
56 GstVideoFilter videofilter;
69 struct _GstShagadelicTVClass
71 GstVideoFilterClass parent_class;
74 GType gst_shagadelictv_get_type (void);
76 static void gst_shagadelic_initialize (GstShagadelicTV * filter);
78 static const GstElementDetails shagadelictv_details =
79 GST_ELEMENT_DETAILS ("ShagadelicTV",
80 "Filter/Effect/Video",
81 "Oh behave, ShagedelicTV makes images shagadelic!",
82 "Wim Taymans <wim.taymans@chello.be>");
84 static GstStaticPadTemplate gst_shagadelictv_src_template =
85 GST_STATIC_PAD_TEMPLATE ("src",
88 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
91 static GstStaticPadTemplate gst_shagadelictv_sink_template =
92 GST_STATIC_PAD_TEMPLATE ("sink",
95 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
98 static GstVideoFilterClass *parent_class = NULL;
101 gst_shagadelictv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
104 GstShagadelicTV *filter = GST_SHAGADELICTV (btrans);
105 GstStructure *structure;
106 gboolean ret = FALSE;
108 structure = gst_caps_get_structure (incaps, 0);
110 if (gst_structure_get_int (structure, "width", &filter->width) &&
111 gst_structure_get_int (structure, "height", &filter->height)) {
112 gint area = filter->width * filter->height;
114 g_free (filter->ripple);
115 g_free (filter->spiral);
117 filter->ripple = (gchar *) g_malloc (area * 4);
118 filter->spiral = (gchar *) g_malloc (area);
120 gst_shagadelic_initialize (filter);
128 gst_shagadelictv_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
131 GstShagadelicTV *filter;
132 GstStructure *structure;
133 gboolean ret = FALSE;
136 filter = GST_SHAGADELICTV (btrans);
138 structure = gst_caps_get_structure (caps, 0);
140 if (gst_structure_get_int (structure, "width", &width) &&
141 gst_structure_get_int (structure, "height", &height)) {
142 *size = width * height * 32 / 8;
144 GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
154 static unsigned int fastrand_val;
156 return (fastrand_val = fastrand_val * 1103515245 + 12345);
160 gst_shagadelic_initialize (GstShagadelicTV * filter)
171 for (y = 0; y < filter->height * 2; y++) {
172 yy = y - filter->height;
175 for (x = 0; x < filter->width * 2; x++) {
176 xx = x - filter->width;
178 filter->ripple[i++] = ((unsigned int) (sqrtf (xx * xx + yy) * 8)) & 255;
180 filter->ripple[i++] = ((unsigned int) (sqrt (xx * xx + yy) * 8)) & 255;
186 for (y = 0; y < filter->height; y++) {
187 yy = y - filter->height / 2;
189 for (x = 0; x < filter->width; x++) {
190 xx = x - filter->width / 2;
192 filter->spiral[i++] = ((unsigned int)
194 yy) / ((float) M_PI) * 256 * 9) + (sqrtf (xx * xx +
195 yy * yy) * 5))) & 255;
197 filter->spiral[i++] = ((unsigned int)
198 ((atan2 (xx, yy) / M_PI * 256 * 9) + (sqrt (xx * xx +
199 yy * yy) * 5))) & 255;
201 /* Here is another Swinger!
202 * ((atan2(xx, yy)/M_PI*256) + (sqrt(xx*xx+yy*yy)*10))&255;
206 filter->rx = fastrand () % filter->width;
207 filter->ry = fastrand () % filter->height;
208 filter->bx = fastrand () % filter->width;
209 filter->by = fastrand () % filter->height;
218 gst_shagadelictv_transform (GstBaseTransform * trans, GstBuffer * in,
221 GstShagadelicTV *filter;
227 GstFlowReturn ret = GST_FLOW_OK;
229 filter = GST_SHAGADELICTV (trans);
231 gst_buffer_copy_metadata (out, in, GST_BUFFER_COPY_TIMESTAMPS);
233 src = (guint32 *) GST_BUFFER_DATA (in);
234 dest = (guint32 *) GST_BUFFER_DATA (out);
236 width = filter->width;
237 height = filter->height;
239 for (y = 0; y < height; y++) {
240 for (x = 0; x < width; x++) {
241 v = *src++ | 0x1010100;
242 v = (v - 0x707060) & 0x1010100;
246 * *dest++ = v & ((r<<16)|(g<<8)|b);
248 r = (gchar) (filter->ripple[(filter->ry + y) * width * 2 + filter->rx +
249 x] + filter->phase * 2) >> 7;
250 g = (gchar) (filter->spiral[y * width + x] + filter->phase * 3) >> 7;
251 b = (gchar) (filter->ripple[(filter->by + y) * width * 2 + filter->bx +
252 x] - filter->phase) >> 7;
253 *dest++ = v & ((r << 16) | (g << 8) | b);
258 if ((filter->rx + filter->rvx) < 0 || (filter->rx + filter->rvx) >= width)
259 filter->rvx = -filter->rvx;
260 if ((filter->ry + filter->rvy) < 0 || (filter->ry + filter->rvy) >= height)
261 filter->rvy = -filter->rvy;
262 if ((filter->bx + filter->bvx) < 0 || (filter->bx + filter->bvx) >= width)
263 filter->bvx = -filter->bvx;
264 if ((filter->by + filter->bvy) < 0 || (filter->by + filter->bvy) >= height)
265 filter->bvy = -filter->bvy;
266 filter->rx += filter->rvx;
267 filter->ry += filter->rvy;
268 filter->bx += filter->bvx;
269 filter->by += filter->bvy;
275 gst_shagadelictv_base_init (gpointer g_class)
277 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
279 gst_element_class_set_details (element_class, &shagadelictv_details);
281 gst_element_class_add_pad_template (element_class,
282 gst_static_pad_template_get (&gst_shagadelictv_sink_template));
283 gst_element_class_add_pad_template (element_class,
284 gst_static_pad_template_get (&gst_shagadelictv_src_template));
288 gst_shagadelictv_class_init (gpointer klass, gpointer class_data)
290 GstBaseTransformClass *trans_class;
292 trans_class = (GstBaseTransformClass *) klass;
294 parent_class = g_type_class_peek_parent (klass);
296 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_shagadelictv_set_caps);
297 trans_class->get_unit_size =
298 GST_DEBUG_FUNCPTR (gst_shagadelictv_get_unit_size);
299 trans_class->transform = GST_DEBUG_FUNCPTR (gst_shagadelictv_transform);
303 gst_shagadelictv_init (GTypeInstance * instance, gpointer g_class)
305 GstShagadelicTV *filter = GST_SHAGADELICTV (instance);
307 filter->ripple = NULL;
308 filter->spiral = NULL;
312 gst_shagadelictv_get_type (void)
314 static GType shagadelictv_type = 0;
316 if (!shagadelictv_type) {
317 static const GTypeInfo shagadelictv_info = {
318 sizeof (GstShagadelicTVClass),
319 gst_shagadelictv_base_init,
321 (GClassInitFunc) gst_shagadelictv_class_init,
324 sizeof (GstShagadelicTV),
326 (GInstanceInitFunc) gst_shagadelictv_init,
330 g_type_register_static (GST_TYPE_VIDEO_FILTER, "GstShagadelicTV",
331 &shagadelictv_info, 0);
333 return shagadelictv_type;