2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * dice.c: a 'dicing' effect
5 * copyright (c) 2001 Sam Mertens. This code is subject to the provisions of
6 * the GNU Library Public License.
8 * I suppose this looks similar to PuzzleTV, but it's not. The screen is
9 * divided into small squares, each of which is rotated either 0, 90, 180 or
10 * 270 degrees. The amount of rotation for each square is chosen at random.
18 #include <gstvideofilter.h>
20 #define GST_TYPE_DICETV \
21 (gst_dicetv_get_type())
22 #define GST_DICETV(obj) \
23 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DICETV,GstDiceTV))
24 #define GST_DICETV_CLASS(klass) \
25 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DICETV,GstDiceTVClass))
26 #define GST_IS_DICETV(obj) \
27 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DICETV))
28 #define GST_IS_DICETV_CLASS(obj) \
29 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DICETV))
31 typedef struct _GstDiceTV GstDiceTV;
32 typedef struct _GstDiceTVClass GstDiceTVClass;
34 #define DEFAULT_CUBE_BITS 4
35 #define MAX_CUBE_BITS 5
36 #define MIN_CUBE_BITS 0
38 typedef enum _dice_dir
48 GstVideofilter videofilter;
59 struct _GstDiceTVClass
61 GstVideofilterClass parent_class;
63 void (*reset) (GstElement *element);
66 /* Filter signals and args */
80 static void gst_dicetv_base_init (gpointer g_class);
81 static void gst_dicetv_class_init (gpointer g_class, gpointer class_data);
82 static void gst_dicetv_init (GTypeInstance *instance, gpointer g_class);
84 static void gst_dicetv_reset_handler (GstElement *elem);
85 static void gst_dicetv_create_map (GstDiceTV *filter);
87 static void gst_dicetv_set_property (GObject * object, guint prop_id,
88 const GValue * value, GParamSpec * pspec);
89 static void gst_dicetv_get_property (GObject * object, guint prop_id,
90 GValue * value, GParamSpec * pspec);
91 static void gst_dicetv_setup (GstVideofilter *videofilter);
92 static void gst_dicetv_draw (GstVideofilter *videofilter, void *d, void *s);
94 static guint gst_dicetv_signals[LAST_SIGNAL] = { 0 };
96 GType gst_dicetv_get_type (void)
98 static GType dicetv_type = 0;
101 static const GTypeInfo dicetv_info = {
102 sizeof (GstDiceTVClass),
103 gst_dicetv_base_init,
105 (GClassInitFunc) gst_dicetv_class_init,
110 (GInstanceInitFunc) gst_dicetv_init,
113 dicetv_type = g_type_register_static (GST_TYPE_VIDEOFILTER, "GstDiceTV", &dicetv_info, 0);
118 static GstVideofilterFormat gst_dicetv_formats[] = {
119 { "RGB ", 32, gst_dicetv_draw, 24, G_BIG_ENDIAN, 0x00ff0000, 0x0000ff00, 0x000000ff },
120 { "RGB ", 32, gst_dicetv_draw, 24, G_BIG_ENDIAN, 0xff000000, 0x00ff0000, 0x0000ff00 },
121 { "RGB ", 32, gst_dicetv_draw, 24, G_BIG_ENDIAN, 0x000000ff, 0x0000ff00, 0x00ff0000 },
122 { "RGB ", 32, gst_dicetv_draw, 24, G_BIG_ENDIAN, 0x0000ff00, 0x00ff0000, 0xff000000 },
126 gst_dicetv_base_init (gpointer g_class)
128 /* elementfactory information */
129 static GstElementDetails gst_dicetv_details = GST_ELEMENT_DETAILS (
131 "Filter/Effect/Video",
132 "'Dices' the screen up into many small squares",
133 "Wim Taymans <wim.taymans@chello.be>"
136 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
137 GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
140 gst_element_class_set_details (element_class, &gst_dicetv_details);
142 for(i=0; i < G_N_ELEMENTS(gst_dicetv_formats); i++) {
143 gst_videofilter_class_add_format(videofilter_class,
144 gst_dicetv_formats + i);
147 gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
151 gst_dicetv_class_init (gpointer g_class, gpointer class_data)
153 GObjectClass *gobject_class;
154 GstVideofilterClass *videofilter_class;
155 GstDiceTVClass *dicetv_class;
157 gobject_class = G_OBJECT_CLASS (g_class);
158 videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
159 dicetv_class = GST_DICETV_CLASS (g_class);
161 gst_dicetv_signals[RESET_SIGNAL] =
162 g_signal_new ("reset",
163 G_TYPE_FROM_CLASS (g_class),
165 G_STRUCT_OFFSET (GstDiceTVClass, reset),
167 g_cclosure_marshal_VOID__VOID,
170 dicetv_class->reset = gst_dicetv_reset_handler;
172 g_object_class_install_property (gobject_class, ARG_CUBE_BITS,
173 g_param_spec_int ("square_bits","Square Bits","The size of the Squares",
174 MIN_CUBE_BITS, MAX_CUBE_BITS, DEFAULT_CUBE_BITS, G_PARAM_READWRITE));
176 gobject_class->set_property = gst_dicetv_set_property;
177 gobject_class->get_property = gst_dicetv_get_property;
179 videofilter_class->setup = gst_dicetv_setup;
183 gst_dicetv_setup (GstVideofilter *videofilter)
187 g_return_if_fail (GST_IS_DICETV (videofilter));
188 dicetv = GST_DICETV (videofilter);
190 dicetv->width = gst_videofilter_get_input_width (videofilter);
191 dicetv->height = gst_videofilter_get_input_height (videofilter);
193 g_free (dicetv->dicemap);
194 dicetv->dicemap = (gchar *) g_malloc (dicetv->height * dicetv->width * sizeof(char));
195 gst_dicetv_create_map (dicetv);
199 gst_dicetv_init (GTypeInstance *instance, gpointer g_class)
201 GstDiceTV *filter = GST_DICETV (instance);
203 filter->dicemap = NULL;
204 filter->g_cube_bits = DEFAULT_CUBE_BITS;
205 filter->g_cube_size = 0;
206 filter->g_map_height = 0;
207 filter->g_map_width = 0;
211 gst_dicetv_reset_handler (GstElement *element)
213 GstDiceTV *filter = GST_DICETV (element);
215 gst_dicetv_create_map (filter);
221 static unsigned int fastrand_val;
223 return (fastrand_val = fastrand_val * 1103515245 + 12345);
227 gst_dicetv_draw (GstVideofilter *videofilter, void *d, void *s)
233 gint map_x, map_y, map_i;
236 gint video_width = filter->width;
237 gint g_cube_bits = filter->g_cube_bits;
238 gint g_cube_size = filter->g_cube_size;
240 filter = GST_DICETV (videofilter);
244 video_width = filter->width;
245 g_cube_bits = filter->g_cube_bits;
246 g_cube_size = filter->g_cube_size;
249 for (map_y = 0; map_y < filter->g_map_height; map_y++) {
250 for (map_x = 0; map_x < filter->g_map_width; map_x++) {
251 base = (map_y << g_cube_bits) * video_width + (map_x << g_cube_bits);
253 switch (filter->dicemap[map_i]) {
255 for (dy = 0; dy < g_cube_size; dy++) {
256 i = base + dy * video_width;
257 for (dx = 0; dx < g_cube_size; dx++) {
264 for (dy = 0; dy < g_cube_size; dy++) {
265 i = base + dy * video_width;
267 for (dx = 0; dx < g_cube_size; dx++) {
268 di = base + (dx * video_width) + (g_cube_size - dy - 1);
275 for (dy = 0; dy < g_cube_size; dy++) {
276 di = base + dy * video_width;
277 i = base + (g_cube_size - dy - 1) * video_width + g_cube_size;
278 for (dx = 0; dx < g_cube_size; dx++) {
286 for (dy = 0; dy < g_cube_size; dy++) {
287 i = base + (dy * video_width);
288 for (dx = 0; dx < g_cube_size; dx++) {
289 di = base + dy + (g_cube_size - dx - 1) * video_width;
296 g_assert_not_reached ();
305 gst_dicetv_create_map (GstDiceTV *filter)
309 filter->g_map_height = filter->height >> filter->g_cube_bits;
310 filter->g_map_width = filter->width >> filter->g_cube_bits;
311 filter->g_cube_size = 1 << filter->g_cube_bits;
315 for (y = 0; y < filter->g_map_height; y++) {
316 for(x = 0; x < filter->g_map_width; x++) {
317 // dicemap[i] = ((i + y) & 0x3); /* Up, Down, Left or Right */
318 filter->dicemap[i] = (fastrand() >> 24) & 0x03;
325 gst_dicetv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
329 /* it's not null if we got it, but it might not be ours */
330 g_return_if_fail (GST_IS_DICETV (object));
332 filter = GST_DICETV (object);
336 filter->g_cube_bits = g_value_get_int (value);
337 gst_dicetv_create_map (filter);
344 gst_dicetv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
348 /* it's not null if we got it, but it might not be ours */
349 g_return_if_fail (GST_IS_DICETV (object));
351 filter = GST_DICETV (object);
355 g_value_set_int (value, filter->g_cube_bits);
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);