Commit b7660b12 authored by Thiago Santos's avatar Thiago Santos

geometrictransform: New plugin for geometric transforms

Adds a new plugin that has elements that perform geometric
transformations to images. By geometric transformations I mean
that the operations are functions that given the output pixel
position, get the pixel position in the input image. This pixel
is then copied from input to output.

The gstgeometrictransform baseclass makes it easy to write
such elements. It boils down to write the mapping function
and exposing properties

Already added the first of the elements, 'pinch'. It's a common
effect in image editors, like gimp (distort -> pinch)
parent fbbb708e
......@@ -305,6 +305,7 @@ AG_GST_CHECK_PLUGIN(dvdspu)
AG_GST_CHECK_PLUGIN(festival)
AG_GST_CHECK_PLUGIN(freeze)
AG_GST_CHECK_PLUGIN(frei0r)
AG_GST_CHECK_PLUGIN(geometrictransform)
AG_GST_CHECK_PLUGIN(h264parse)
AG_GST_CHECK_PLUGIN(hdvparse)
AG_GST_CHECK_PLUGIN(id3tag)
......@@ -1681,6 +1682,7 @@ gst/dvdspu/Makefile
gst/festival/Makefile
gst/freeze/Makefile
gst/frei0r/Makefile
gst/geometrictransform/Makefile
gst/h264parse/Makefile
gst/hdvparse/Makefile
gst/id3tag/Makefile
......
plugin_LTLIBRARIES = libgstgeometrictransform.la
libgstgeometrictransform_la_SOURCES = plugin.c \
gstgeometrictransform.c \
gstpinch.c
libgstgeometrictransform_la_CFLAGS = $(GST_CFLAGS) $(GST_BASE_CFLAGS) \
$(GST_PLUGINS_BASE_CFLAGS)
libgstgeometrictransform_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) -lgstvideo-@GST_MAJORMINOR@ $(GST_BASE_LIBS) $(GST_LIBS)
libgstgeometrictransform_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
libgstgeometrictransform_la_LIBTOOLFLAGS = --tag=disable-static
noinst_HEADERS = gstgeometrictransform.h gstpinch.h
/* GStreamer
* Copyright (C) <2010> Thiago Santos <thiago.sousa.santos@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstgeometrictransform.h"
#include <string.h>
GST_DEBUG_CATEGORY_STATIC (geometric_transform_debug);
#define GST_CAT_DEFAULT geometric_transform_debug
static GstStaticPadTemplate gst_geometric_transform_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
);
static GstStaticPadTemplate gst_geometric_transform_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
);
static GstVideoFilterClass *parent_class = NULL;
static gboolean
gst_geometric_transform_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
GstCaps * outcaps)
{
GstGeometricTransform *vf;
vf = GST_GEOMETRIC_TRANSFORM (btrans);
return gst_video_format_parse_caps (incaps, &vf->format, &vf->width,
&vf->height);
}
static void
gst_geometric_transform_do_map (GstGeometricTransform * gt, GstBuffer * inbuf,
GstBuffer * outbuf, gint x, gint y, gdouble out_x, gdouble out_y)
{
gint trunc_x = (gint) out_x;
gint trunc_y = (gint) out_y;
gint in_offset;
gint out_offset;
/* NOP */
out_offset = (y * gt->width + x) * 3;
in_offset = (trunc_y * gt->width + trunc_x) * 3;
memcpy (GST_BUFFER_DATA (outbuf) + out_offset,
GST_BUFFER_DATA (inbuf) + in_offset, 3);
}
static GstFlowReturn
gst_geometric_transform_transform (GstBaseTransform * trans, GstBuffer * buf,
GstBuffer * outbuf)
{
GstGeometricTransform *gt;
GstGeometricTransformClass *klass;
gint x, y;
gdouble out_x, out_y;
guint8 *data;
GstFlowReturn ret = GST_FLOW_OK;
gt = GST_GEOMETRIC_TRANSFORM (trans);
klass = GST_GEOMETRIC_TRANSFORM_GET_CLASS (gt);
/* subclass must have defined the map_func */
g_return_val_if_fail (klass->map_func, GST_FLOW_ERROR);
data = GST_BUFFER_DATA (buf);
for (x = 0; x < gt->width; x++) {
for (y = 0; y < gt->height; y++) {
if (!klass->map_func (gt, x, y, &out_x, &out_y)) {
/* child should have warned */
ret = GST_FLOW_ERROR;
goto end;
}
/* do the mapping */
gst_geometric_transform_do_map (gt, buf, outbuf, x, y, out_x, out_y);
}
}
end:
return ret;
}
static void
gst_geometric_transform_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_geometric_transform_sink_template));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_geometric_transform_src_template));
}
static void
gst_geometric_transform_class_init (gpointer klass, gpointer class_data)
{
GstBaseTransformClass *trans_class;
trans_class = (GstBaseTransformClass *) klass;
parent_class = g_type_class_peek_parent (klass);
trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_geometric_transform_set_caps);
trans_class->transform =
GST_DEBUG_FUNCPTR (gst_geometric_transform_transform);
}
static void
gst_geometric_transform_init (GTypeInstance * instance, gpointer g_class)
{
}
GType
gst_geometric_transform_get_type (void)
{
static GType geometric_transform_type = 0;
if (!geometric_transform_type) {
static const GTypeInfo geometric_transform_info = {
sizeof (GstGeometricTransformClass),
gst_geometric_transform_base_init,
NULL,
gst_geometric_transform_class_init,
NULL,
NULL,
sizeof (GstGeometricTransform),
0,
gst_geometric_transform_init,
};
geometric_transform_type = g_type_register_static (GST_TYPE_VIDEO_FILTER,
"GstGeometricTransform", &geometric_transform_info,
G_TYPE_FLAG_ABSTRACT);
GST_DEBUG_CATEGORY_INIT (geometric_transform_debug, "geometrictransform", 0,
"Base class for geometric transform elements");
}
return geometric_transform_type;
}
/* GStreamer
* Copyright (C) <2010> Thiago Santos <thiago.sousa.santos@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GST_GEOMETRIC_TRANSFORM_H__
#define __GST_GEOMETRIC_TRANSFORM_H__
#include <gst/video/gstvideofilter.h>
#include <gst/video/video.h>
G_BEGIN_DECLS
#define GST_TYPE_GEOMETRIC_TRANSFORM \
(gst_geometric_transform_get_type())
#define GST_GEOMETRIC_TRANSFORM(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_GEOMETRIC_TRANSFORM,GstGeometricTransform))
#define GST_GEOMETRIC_TRANSFORM_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_GEOMETRIC_TRANSFORM,GstGeometricTransformClass))
#define GST_IS_GEOMETRIC_TRANSFORM(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_GEOMETRIC_TRANSFORM))
#define GST_IS_GEOMETRIC_TRANSFORM_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_GEOMETRIC_TRANSFORM))
#define GST_GEOMETRIC_TRANSFORM_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_GEOMETRIC_TRANSFORM,GstGeometricTransformClass))
typedef struct _GstGeometricTransform GstGeometricTransform;
typedef struct _GstGeometricTransformClass GstGeometricTransformClass;
typedef gboolean (*GstGeometricTransformMapFunc) (GstGeometricTransform * gt,
gint x, gint y, gdouble * _out_x, gdouble *_out_y);
/**
* GstGeometricTransform:
*
* Opaque datastructure.
*/
struct _GstGeometricTransform {
GstVideoFilter videofilter;
gint width, height;
GstVideoFormat format;
};
struct _GstGeometricTransformClass {
GstVideoFilterClass parent_class;
GstGeometricTransformMapFunc map_func;
};
GType gst_geometric_transform_get_type (void);
G_END_DECLS
#endif /* __GST_GEOMETRIC_TRANSFORM_H__ */
/*
* GStreamer
* Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Alternatively, the contents of this file may be used under the
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
* which case the following provisions apply instead of the ones
* mentioned above:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Thanks to Jerry Huxtable <http://www.jhlabs.com> work on its java
* image editor and filters. The algorithms here were extracted from
* his code.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gst/gst.h>
#include <math.h>
#include "gstpinch.h"
GST_DEBUG_CATEGORY_STATIC (gst_pinch_debug);
#define GST_CAT_DEFAULT gst_pinch_debug
enum
{
PROP_0,
PROP_X_CENTER,
PROP_Y_CENTER,
PROP_RADIUS,
PROP_INTENSITY
};
#define DEFAULT_X_CENTER 0.5
#define DEFAULT_Y_CENTER 0.5
#define DEFAULT_RADIUS 100
#define DEFAULT_INTENSITY 0.5
GST_BOILERPLATE (GstPinch, gst_pinch, GstGeometricTransform,
GST_TYPE_GEOMETRIC_TRANSFORM);
static void
gst_pinch_set_property (GObject * object, guint prop_id, const GValue * value,
GParamSpec * pspec)
{
GstPinch *pinch;
pinch = GST_PINCH (object);
switch (prop_id) {
case PROP_X_CENTER:
pinch->x_center = g_value_get_double (value);
break;
case PROP_Y_CENTER:
pinch->y_center = g_value_get_double (value);
break;
case PROP_RADIUS:
pinch->radius = g_value_get_double (value);
break;
case PROP_INTENSITY:
pinch->intensity = g_value_get_double (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_pinch_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstPinch *pinch;
pinch = GST_PINCH (object);
switch (prop_id) {
case PROP_X_CENTER:
g_value_set_double (value, pinch->x_center);
break;
case PROP_Y_CENTER:
g_value_set_double (value, pinch->y_center);
break;
case PROP_RADIUS:
g_value_set_double (value, pinch->radius);
break;
case PROP_INTENSITY:
g_value_set_double (value, pinch->intensity);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* Clean up */
static void
gst_pinch_finalize (GObject * obj)
{
G_OBJECT_CLASS (parent_class)->finalize (obj);
}
/* GObject vmethod implementations */
static void
gst_pinch_base_init (gpointer gclass)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
gst_element_class_set_details_simple (element_class,
"pinch",
"Transform/Effect/Video",
"Applies 'pinch' geometric transform to the image",
"Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
}
/* FIXME optimize a little using cast macro and pre calculating some
* values so we don't need them every mapping */
static gboolean
dummy_map (GstGeometricTransform * gt, gint x, gint y, gdouble * ox,
gdouble * oy)
{
GstPinch *pinch = GST_PINCH (gt);
gdouble r2;
gdouble distance;
gdouble x_center;
gdouble y_center;
gdouble dx, dy;
/* get the center in pixels instead of % */
x_center = pinch->x_center * gt->width;
y_center = pinch->y_center * gt->height;
dx = x - x_center;
dy = y - y_center;
distance = dx * dx + dy * dy;
r2 = pinch->radius * pinch->radius;
GST_LOG_OBJECT (pinch, "Center %0.5lf (%0.2lf) %0.5lf (%0.2lf)",
x_center, pinch->x_center, y_center, pinch->y_center);
GST_LOG_OBJECT (pinch, "Input %d %d, distance=%lf, radius2=%lf, dx=%lf"
", dy=%lf", x, y, distance, r2, dx, dy);
if (distance > r2 || distance == 0) {
*ox = x;
*oy = y;
} else {
gdouble d = sqrt (distance / r2);
gdouble t = pow (sin (G_PI * 0.5 * d), -pinch->intensity);
dx *= t;
dy *= t;
GST_LOG_OBJECT (pinch, "D=%lf, t=%lf, dx=%lf" ", dy=%lf", d, t, dx, dy);
*ox = x_center + dx;
*oy = y_center + dy;
*ox = CLAMP (*ox, 0, gt->width - 1);
*oy = CLAMP (*oy, 0, gt->height - 1);
}
GST_DEBUG_OBJECT (pinch, "Mapped %d %d into %lf %lf", x, y, *ox, *oy);
return TRUE;
}
static void
gst_pinch_class_init (GstPinchClass * klass)
{
GObjectClass *gobject_class;
GstGeometricTransformClass *gstgt_class;
gobject_class = (GObjectClass *) klass;
gstgt_class = (GstGeometricTransformClass *) klass;
parent_class = g_type_class_peek_parent (klass);
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_pinch_finalize);
gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pinch_set_property);
gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pinch_get_property);
/* FIXME I don't like the idea of x-center and y-center being in % and
* radius and intensity in absolute values, I think no one likes it. */
g_object_class_install_property (gobject_class, PROP_X_CENTER,
g_param_spec_double ("x-center", "x center",
"X axis center of the pinch effect",
0.0, 1.0, DEFAULT_X_CENTER,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_Y_CENTER,
g_param_spec_double ("y-center", "y center",
"Y axis center of the pinch effect",
0.0, 1.0, DEFAULT_Y_CENTER,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_RADIUS,
g_param_spec_double ("radius", "radius", "radius of the pinch effect",
0.0, G_MAXDOUBLE, DEFAULT_RADIUS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_INTENSITY,
g_param_spec_double ("intensity", "intensity",
"intensity of the pinch effect",
0.0, G_MAXDOUBLE, DEFAULT_INTENSITY,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gstgt_class->map_func = dummy_map;
}
static void
gst_pinch_init (GstPinch * filter, GstPinchClass * gclass)
{
filter->x_center = DEFAULT_X_CENTER;
filter->y_center = DEFAULT_Y_CENTER;
filter->intensity = DEFAULT_INTENSITY;
filter->radius = DEFAULT_RADIUS;
}
gboolean
gst_pinch_plugin_init (GstPlugin * plugin)
{
GST_DEBUG_CATEGORY_INIT (gst_pinch_debug, "pinch", 0, "pinch");
return gst_element_register (plugin, "pinch", GST_RANK_NONE, GST_TYPE_PINCH);
}
/*
* GStreamer
* Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Alternatively, the contents of this file may be used under the
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
* which case the following provisions apply instead of the ones
* mentioned above:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GST_PINCH_H__
#define __GST_PINCH_H__
#include <gst/gst.h>
#include "gstgeometrictransform.h"
G_BEGIN_DECLS
/* #defines don't like whitespacey bits */
#define GST_TYPE_PINCH \
(gst_pinch_get_type())
#define GST_PINCH(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PINCH,GstPinch))
#define GST_PINCH_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PINCH,GstPinchClass))
#define GST_IS_PINCH(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PINCH))
#define GST_IS_PINCH_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PINCH))
typedef struct _GstPinch GstPinch;
typedef struct _GstPinchClass GstPinchClass;
struct _GstPinch
{
GstGeometricTransform element;
gdouble x_center;
gdouble y_center;
gdouble radius;
gdouble intensity;
};
struct _GstPinchClass
{
GstGeometricTransformClass parent_class;
};
GType gst_pinch_get_type (void);
gboolean gst_pinch_plugin_init (GstPlugin * plugin);
G_END_DECLS
#endif /* __GST_PINCH_H__ */
/* GStreamer
* Copyright (C) <2010> Thiago Santos <thiago.sousa.santos@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstpinch.h"
static gboolean
plugin_init (GstPlugin * plugin)
{
if (!gst_pinch_plugin_init (plugin))
return FALSE;
return TRUE;
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
"geometrictransform",
"Various geometric image transform elements",
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment