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.
34 #include <gst/video/video.h>
35 #include <gst/video/gstvideofilter.h>
38 #define M_PI 3.14159265358979323846
41 #define GST_TYPE_SHAGADELICTV \
42 (gst_shagadelictv_get_type())
43 #define GST_SHAGADELICTV(obj) \
44 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SHAGADELICTV,GstShagadelicTV))
45 #define GST_SHAGADELICTV_CLASS(klass) \
46 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_SHAGADELICTV,GstShagadelicTVClass))
47 #define GST_IS_SHAGADELICTV(obj) \
48 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SHAGADELICTV))
49 #define GST_IS_SHAGADELICTV_CLASS(klass) \
50 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_SHAGADELICTV))
52 typedef struct _GstShagadelicTV GstShagadelicTV;
53 typedef struct _GstShagadelicTVClass GstShagadelicTVClass;
55 struct _GstShagadelicTV
57 GstVideoFilter videofilter;
70 struct _GstShagadelicTVClass
72 GstVideoFilterClass parent_class;
75 GST_BOILERPLATE (GstShagadelicTV, gst_shagadelictv, GstVideoFilter,
76 GST_TYPE_VIDEO_FILTER);
78 static void gst_shagadelic_initialize (GstShagadelicTV * filter);
80 static GstStaticPadTemplate gst_shagadelictv_src_template =
81 GST_STATIC_PAD_TEMPLATE ("src",
84 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
87 static GstStaticPadTemplate gst_shagadelictv_sink_template =
88 GST_STATIC_PAD_TEMPLATE ("sink",
91 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
95 gst_shagadelictv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
98 GstShagadelicTV *filter = GST_SHAGADELICTV (btrans);
99 GstStructure *structure;
100 gboolean ret = FALSE;
102 structure = gst_caps_get_structure (incaps, 0);
104 if (gst_structure_get_int (structure, "width", &filter->width) &&
105 gst_structure_get_int (structure, "height", &filter->height)) {
106 gint area = filter->width * filter->height;
108 g_free (filter->ripple);
109 g_free (filter->spiral);
111 filter->ripple = (guint8 *) g_malloc (area * 4);
112 filter->spiral = (guint8 *) g_malloc (area);
114 gst_shagadelic_initialize (filter);
124 static guint fastrand_val;
126 return (fastrand_val = fastrand_val * 1103515245 + 12345);
130 gst_shagadelic_initialize (GstShagadelicTV * filter)
141 for (y = 0; y < filter->height * 2; y++) {
142 yy = y - filter->height;
145 for (x = 0; x < filter->width * 2; x++) {
146 xx = x - filter->width;
148 filter->ripple[i++] = ((unsigned int) (sqrtf (xx * xx + yy) * 8)) & 255;
150 filter->ripple[i++] = ((unsigned int) (sqrt (xx * xx + yy) * 8)) & 255;
156 for (y = 0; y < filter->height; y++) {
157 yy = y - filter->height / 2;
159 for (x = 0; x < filter->width; x++) {
160 xx = x - filter->width / 2;
162 filter->spiral[i++] = ((unsigned int)
164 yy) / ((float) M_PI) * 256 * 9) + (sqrtf (xx * xx +
165 yy * yy) * 5))) & 255;
167 filter->spiral[i++] = ((unsigned int)
168 ((atan2 (xx, yy) / M_PI * 256 * 9) + (sqrt (xx * xx +
169 yy * yy) * 5))) & 255;
171 /* Here is another Swinger!
172 * ((atan2(xx, yy)/M_PI*256) + (sqrt(xx*xx+yy*yy)*10))&255;
176 filter->rx = fastrand () % filter->width;
177 filter->ry = fastrand () % filter->height;
178 filter->bx = fastrand () % filter->width;
179 filter->by = fastrand () % filter->height;
188 gst_shagadelictv_transform (GstBaseTransform * trans, GstBuffer * in,
191 GstShagadelicTV *filter = GST_SHAGADELICTV (trans);
197 GstFlowReturn ret = GST_FLOW_OK;
199 src = (guint32 *) GST_BUFFER_DATA (in);
200 dest = (guint32 *) GST_BUFFER_DATA (out);
202 width = filter->width;
203 height = filter->height;
205 for (y = 0; y < height; y++) {
206 for (x = 0; x < width; x++) {
207 v = *src++ | 0x1010100;
208 v = (v - 0x707060) & 0x1010100;
212 * *dest++ = v & ((r<<16)|(g<<8)|b);
214 r = ((gint8) (filter->ripple[(filter->ry + y) * width * 2 + filter->rx +
215 x] + filter->phase * 2)) >> 7;
216 g = ((gint8) (filter->spiral[y * width + x] + filter->phase * 3)) >> 7;
217 b = ((gint8) (filter->ripple[(filter->by + y) * width * 2 + filter->bx +
218 x] - filter->phase)) >> 7;
219 *dest++ = v & ((r << 16) | (g << 8) | b);
224 if ((filter->rx + filter->rvx) < 0 || (filter->rx + filter->rvx) >= width)
225 filter->rvx = -filter->rvx;
226 if ((filter->ry + filter->rvy) < 0 || (filter->ry + filter->rvy) >= height)
227 filter->rvy = -filter->rvy;
228 if ((filter->bx + filter->bvx) < 0 || (filter->bx + filter->bvx) >= width)
229 filter->bvx = -filter->bvx;
230 if ((filter->by + filter->bvy) < 0 || (filter->by + filter->bvy) >= height)
231 filter->bvy = -filter->bvy;
232 filter->rx += filter->rvx;
233 filter->ry += filter->rvy;
234 filter->bx += filter->bvx;
235 filter->by += filter->bvy;
241 gst_shagadelictv_finalize (GObject * object)
243 GstShagadelicTV *filter = GST_SHAGADELICTV (object);
246 g_free (filter->ripple);
247 filter->ripple = NULL;
250 g_free (filter->spiral);
251 filter->spiral = NULL;
253 G_OBJECT_CLASS (parent_class)->finalize (object);
257 gst_shagadelictv_base_init (gpointer g_class)
259 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
261 gst_element_class_set_details_simple (element_class, "ShagadelicTV",
262 "Filter/Effect/Video",
263 "Oh behave, ShagedelicTV makes images shagadelic!",
264 "Wim Taymans <wim.taymans@chello.be>");
266 gst_element_class_add_pad_template (element_class,
267 gst_static_pad_template_get (&gst_shagadelictv_sink_template));
268 gst_element_class_add_pad_template (element_class,
269 gst_static_pad_template_get (&gst_shagadelictv_src_template));
273 gst_shagadelictv_class_init (GstShagadelicTVClass * klass)
275 GObjectClass *gobject_class = (GObjectClass *) klass;
276 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
278 gobject_class->finalize = gst_shagadelictv_finalize;
280 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_shagadelictv_set_caps);
281 trans_class->transform = GST_DEBUG_FUNCPTR (gst_shagadelictv_transform);
285 gst_shagadelictv_init (GstShagadelicTV * filter, GstShagadelicTVClass * klass)
287 filter->ripple = NULL;
288 filter->spiral = NULL;