/*
 * 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 "gstcirclegeometrictransform.h"

GST_DEBUG_CATEGORY_STATIC (gst_circle_geometric_transform_debug);
#define GST_CAT_DEFAULT gst_circle_geometric_transform_debug

GstGeometricTransformClass *parent_class;

enum
{
  PROP_0,
  PROP_X_CENTER,
  PROP_Y_CENTER,
  PROP_RADIUS
};

#define DEFAULT_X_CENTER 0.5
#define DEFAULT_Y_CENTER 0.5
#define DEFAULT_RADIUS 0.35

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 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;
  }
}

/* Clean up */
static void
gst_circle_geometric_transform_finalize (GObject * obj)
{
  G_OBJECT_CLASS (parent_class)->finalize (obj);
}

/* GObject vmethod implementations */

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 void
gst_circle_geometric_transform_class_init (GstCircleGeometricTransformClass *
    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_circle_geometric_transform_finalize);
  gobject_class->set_property =
      GST_DEBUG_FUNCPTR (gst_circle_geometric_transform_set_property);
  gobject_class->get_property =
      GST_DEBUG_FUNCPTR (gst_circle_geometric_transform_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, but
   * I can't see a way to have nice default values without % */
  g_object_class_install_property (gobject_class, PROP_X_CENTER,
      g_param_spec_double ("x-center", "x center",
          "X axis center of the circle_geometric_transform effect",
          0.0, 1.0, DEFAULT_X_CENTER,
          GST_PARAM_CONTROLLABLE | 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 circle_geometric_transform effect",
          0.0, 1.0, DEFAULT_Y_CENTER,
          GST_PARAM_CONTROLLABLE | 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 circle_geometric_transform effect", 0.0, 1.0,
          DEFAULT_RADIUS,
          GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  gstgt_class->prepare_func = circle_geometric_transform_precalc;
}

static void
gst_circle_geometric_transform_init (GstCircleGeometricTransform * filter,
    GstCircleGeometricTransformClass * gclass)
{
  filter->x_center = DEFAULT_X_CENTER;
  filter->y_center = DEFAULT_Y_CENTER;
  filter->radius = DEFAULT_RADIUS;
}

GType
gst_circle_geometric_transform_get_type (void)
{
  static GType circle_geometric_transform_type = 0;

  if (!circle_geometric_transform_type) {
    static const GTypeInfo circle_geometric_transform_info = {
      sizeof (GstCircleGeometricTransformClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_circle_geometric_transform_class_init,
      NULL,
      NULL,
      sizeof (GstCircleGeometricTransform),
      0,
      (GInstanceInitFunc) gst_circle_geometric_transform_init,
    };

    circle_geometric_transform_type =
        g_type_register_static (GST_TYPE_GEOMETRIC_TRANSFORM,
        "GstCircleGeometricTransform", &circle_geometric_transform_info,
        G_TYPE_FLAG_ABSTRACT);

    GST_DEBUG_CATEGORY_INIT (gst_circle_geometric_transform_debug,
        "circlegeometrictransform", 0,
        "Base class for geometric transform elements that operate "
        "on a circular area");
  }
  return circle_geometric_transform_type;
}
