static gboolean twirl_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x, gdouble * in_y) { GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt); GstTwirl *twirl = GST_TWIRL_CAST (gt); gdouble distance; gdouble dx, dy; dx = x - cgt->precalc_x_center; dy = y - cgt->precalc_y_center; distance = dx * dx + dy * dy; if (distance > cgt->precalc_radius2) { *in_x = x; *in_y = y; } else { gdouble d = sqrt (distance); gdouble a = atan2 (dy, dx) + twirl->angle * (cgt->precalc_radius - d) / cgt->precalc_radius; *in_x = cgt->precalc_x_center + d * cos (a); *in_y = cgt->precalc_y_center + d * sin (a); } GST_DEBUG_OBJECT (twirl, "Inversely mapped %d %d into %lf %lf", x, y, *in_x, *in_y); return TRUE; }
static gboolean water_ripple_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x, gdouble * in_y) { GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt); GstWaterRipple *water = GST_WATER_RIPPLE_CAST (gt); gdouble distance; gdouble dx, dy; dx = x - cgt->precalc_x_center; dy = y - cgt->precalc_y_center; distance = dx * dx + dy * dy; if (distance > cgt->precalc_radius2) { *in_x = x; *in_y = y; } else { gdouble d = sqrt (distance); gdouble amount = water->amplitude * sin (d / water->wavelength * G_PI * 2 - water->phase); amount *= (cgt->precalc_radius - d) / cgt->precalc_radius; if (d != 0) amount *= water->wavelength / d; *in_x = x + dx * amount; *in_y = y + dy * amount; } GST_DEBUG_OBJECT (water, "Inversely mapped %d %d into %lf %lf", x, y, *in_x, *in_y); return TRUE; }
static gboolean circle_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x, gdouble * in_y) { GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt); GstCircle *circle = GST_CIRCLE_CAST (gt); gdouble distance; gdouble dx, dy; gdouble theta; dx = x - cgt->precalc_x_center; dy = y - cgt->precalc_y_center; distance = sqrt (dx * dx + dy * dy); theta = atan2 (-dy, -dx) + circle->angle; theta = mod_float (theta, 2 * G_PI); *in_x = gt->width * theta / (circle->spread_angle + 0.0001); *in_y = gt->height * (1 - (distance - cgt->precalc_radius) / (circle->height + 0.0001)); GST_DEBUG_OBJECT (circle, "Inversely mapped %d %d into %lf %lf", x, y, *in_x, *in_y); return TRUE; }
static gboolean kaleidoscope_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x, gdouble * in_y) { GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt); GstKaleidoscope *kaleidoscope = GST_KALEIDOSCOPE_CAST (gt); gdouble dx, dy; gdouble distance; gdouble theta; dx = x - cgt->precalc_x_center; dy = y - cgt->precalc_y_center; distance = sqrt (dx * dx + dy * dy); theta = atan2 (dy, dx) - kaleidoscope->angle - kaleidoscope->angle2; theta = geometric_math_triangle (theta / G_PI * kaleidoscope->sides * 0.5); if (cgt->precalc_radius != 0) { gdouble radiusc = cgt->precalc_radius / cos (theta); distance = radiusc * geometric_math_triangle (distance / radiusc); } theta += kaleidoscope->angle; *in_x = cgt->precalc_x_center + distance * cos (theta); *in_y = cgt->precalc_y_center + distance * sin (theta); GST_DEBUG_OBJECT (kaleidoscope, "Inversely mapped %d %d into %lf %lf", x, y, *in_x, *in_y); return TRUE; }
/* TODO we could have horizontal and vertical 'radius' */ static gboolean sphere_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x, gdouble * in_y) { GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt); GstSphere *sphere = GST_SPHERE_CAST (gt); gdouble dx, dy; gdouble dx2, dy2; dx = x - cgt->precalc_x_center; dy = y - cgt->precalc_y_center; dx2 = dx * dx; dy2 = dy * dy; if (dy2 >= (cgt->precalc_radius2 - (cgt->precalc_radius2 * dx2) / cgt->precalc_radius2)) { *in_x = x; *in_y = y; } else { gdouble r_refraction = 1.0 / sphere->refraction; gdouble z; gdouble z2; gdouble angle; gdouble angle1; gdouble angle2; z = sqrt ((1.0 - dx2 / cgt->precalc_radius2 - dy2 / cgt->precalc_radius2) * (cgt->precalc_radius2)); z2 = z * z; /* x */ angle = acos (dx / sqrt (dx2 + z2)); angle1 = G_PI / 2 - angle; angle2 = asin (sin (angle1) * r_refraction); angle2 = G_PI / 2 - angle - angle2; *in_x = x - tan (angle2) * z; /* y */ angle = acos (dy / sqrt (dy2 + z2)); angle1 = G_PI / 2 - angle; angle2 = asin (sin (angle1) * r_refraction); angle2 = G_PI / 2 - angle - angle2; *in_y = y - tan (angle2) * z; } GST_DEBUG_OBJECT (sphere, "Inversely mapped %d %d into %lf %lf", x, y, *in_x, *in_y); return TRUE; }
static gboolean circle_geometric_transform_precalc (GstGeometricTransform * gt) { GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt); cgt->precalc_x_center = cgt->x_center * gt->width; cgt->precalc_y_center = cgt->y_center * gt->height; cgt->precalc_radius = cgt->radius * 0.5 * sqrt (gt->width * gt->width + gt->height * gt->height); cgt->precalc_radius2 = cgt->precalc_radius * cgt->precalc_radius; return TRUE; }
static gboolean stretch_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x, gdouble * in_y) { GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt); GstStretch *stretch = GST_STRETCH_CAST (gt); gdouble norm_x, norm_y; gdouble r; gdouble width = gt->width; gdouble height = gt->height; gdouble a, b; /* normalize in ((-1.0, -1.0), (1.0, 1.0) and traslate the center */ norm_x = 2.0 * (x / width - cgt->x_center); norm_y = 2.0 * (y / height - cgt->y_center); /* calculate radius, normalize to 1 for future convenience */ r = sqrt (0.5 * (norm_x * norm_x + norm_y * norm_y)); /* actually "stretch" name is a bit misleading, what the transform * really does is shrink the center and gradually return to normal * size while r increases. The shrink thing drags pixels giving * stretching the image around the center */ /* a is the current maximum shrink amount, it goes from 1.0 to * MAX_SHRINK_AMOUNT * intensity */ /* smoothstep goes from 0.0 when r == 0 to b when r == radius */ /* total shrink factor is MAX_SHRINK_AMOUNT at center and gradually * decreases while r goes to radius */ a = 1.0 + (MAX_SHRINK_AMOUNT - 1.0) * stretch->intensity; b = a - 1.0; norm_x *= a - b * smoothstep (0.0, cgt->radius, r); norm_y *= a - b * smoothstep (0.0, cgt->radius, r); /* unnormalize */ *in_x = (0.5 * norm_x + cgt->x_center) * width; *in_y = (0.5 * norm_y + cgt->y_center) * height; GST_DEBUG_OBJECT (stretch, "Inversely mapped %d %d into %lf %lf", x, y, *in_x, *in_y); return TRUE; }
static gboolean bulge_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x, gdouble * in_y) { GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt); GstBulge *bulge = GST_BULGE_CAST (gt); gdouble norm_x, norm_y; gdouble r; gdouble scale; gdouble width = gt->width; gdouble height = gt->height; /* normalize in ((-1.0, -1.0), (1.0, 1.0) and traslate the center */ norm_x = 2.0 * (x / width - cgt->x_center); norm_y = 2.0 * (y / height - cgt->y_center); /* calculate radius, normalize to 1 for future convenience */ r = sqrt (0.5 * (norm_x * norm_x + norm_y * norm_y)); /* zoom in the center region and smoothly get back to no zoom while * r increases */ /* the scale factor goes from bulge->zoom when r == 0 to 1.0 * when r == cgt->radius using Hermite interpolation */ /* scale is inverted because we're doing an inverse transform so * zoom is achieved dividing */ scale = 1.0 / (bulge->zoom + ((1.0 - bulge->zoom) * smoothstep (0, cgt->radius, r))); norm_x *= scale; norm_y *= scale; /* unnormalize */ *in_x = (0.5 * norm_x + cgt->x_center) * width; *in_y = (0.5 * norm_y + cgt->y_center) * height; GST_DEBUG_OBJECT (bulge, "Inversely mapped %d %d into %lf %lf", x, y, *in_x, *in_y); return TRUE; }
static void gst_circle_geometric_transform_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstCircleGeometricTransform *cgt; GstGeometricTransform *gt; gdouble v; gt = GST_GEOMETRIC_TRANSFORM_CAST (object); cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (object); GST_OBJECT_LOCK (cgt); switch (prop_id) { case PROP_X_CENTER: v = g_value_get_double (value); if (v != cgt->x_center) { cgt->x_center = v; gst_geometric_transform_set_need_remap (gt); } break; case PROP_Y_CENTER: v = g_value_get_double (value); if (v != cgt->y_center) { cgt->y_center = v; gst_geometric_transform_set_need_remap (gt); } break; case PROP_RADIUS: v = g_value_get_double (value); if (v != cgt->radius) { cgt->radius = v; gst_geometric_transform_set_need_remap (gt); } break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } GST_OBJECT_UNLOCK (cgt); }
static gboolean pinch_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x, gdouble * in_y) { GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt); GstPinch *pinch = GST_PINCH_CAST (gt); gdouble distance; gdouble dx, dy; dx = x - cgt->precalc_x_center; dy = y - cgt->precalc_y_center; distance = dx * dx + dy * dy; GST_LOG_OBJECT (pinch, "Center %0.5lf (%0.2lf) %0.5lf (%0.2lf)", cgt->precalc_x_center, cgt->x_center, cgt->precalc_y_center, cgt->y_center); GST_LOG_OBJECT (pinch, "Input %d %d, distance=%lf, radius2=%lf, dx=%lf" ", dy=%lf", x, y, distance, cgt->precalc_radius2, dx, dy); if (distance > cgt->precalc_radius2 || distance == 0) { *in_x = x; *in_y = y; } else { gdouble d = sqrt (distance / cgt->precalc_radius2); 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); *in_x = cgt->precalc_x_center + dx; *in_y = cgt->precalc_y_center + dy; } GST_DEBUG_OBJECT (pinch, "Inversely mapped %d %d into %lf %lf", x, y, *in_x, *in_y); return TRUE; }
static gboolean tunnel_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x, gdouble * in_y) { GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt); #ifndef GST_DISABLE_GST_DEBUG GstTunnel *tunnel = GST_TUNNEL_CAST (gt); #endif gdouble norm_x, norm_y; gdouble width = gt->width; gdouble height = gt->height; gdouble r; /* normalize in ((-1.0, -1.0), (1.0, 1.0) and traslate the center */ /* plus a little trick to obtain a perfect circle, normalize in a * square with sides equal to MAX(width, height) */ norm_x = 2.0 * (x - cgt->x_center * width) / MAX (width, height); norm_y = 2.0 * (y - cgt->y_center * height) / MAX (width, height); /* calculate radius, normalize to 1 for future convenience */ r = sqrt (0.5 * (norm_x * norm_x + norm_y * norm_y)); /* do nothing if r < radius */ /* zoom otherwise */ norm_x *= CLAMP (r, 0.0, cgt->radius) / r; norm_y *= CLAMP (r, 0.0, cgt->radius) / r; /* unnormalize */ *in_x = 0.5 * (norm_x) * MAX (width, height) + cgt->x_center * width; *in_y = 0.5 * (norm_y) * MAX (width, height) + cgt->y_center * height; GST_DEBUG_OBJECT (tunnel, "Inversely mapped %d %d into %lf %lf", x, y, *in_x, *in_y); return TRUE; }
static void gst_circle_geometric_transform_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstCircleGeometricTransform *cgt; cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (object); switch (prop_id) { case PROP_X_CENTER: g_value_set_double (value, cgt->x_center); break; case PROP_Y_CENTER: g_value_set_double (value, cgt->y_center); break; case PROP_RADIUS: g_value_set_double (value, cgt->radius); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }