[gegl] stereographic-projection: little planet rendering for panoramas



commit eaeed4ab80b57aa21e20a198774089d00dfb64d6
Author: Øyvind Kolås <pippin gimp org>
Date:   Fri May 2 02:07:42 2014 +0200

    stereographic-projection: little planet rendering for panoramas
    
    Operation performing little-planet style rendering of equirectangular
    panoramas.

 operations/common/Makefile.am                |    1 +
 operations/common/gnomonic-projection.c      |   12 +-
 operations/common/stereographic-projection.c |  229 ++++++++++++++++++++++++++
 po/POTFILES.in                               |    1 +
 4 files changed, 237 insertions(+), 6 deletions(-)
---
diff --git a/operations/common/Makefile.am b/operations/common/Makefile.am
index b7948d2..d4bc15d 100644
--- a/operations/common/Makefile.am
+++ b/operations/common/Makefile.am
@@ -100,6 +100,7 @@ op_LTLIBRARIES = \
        shift.la \
        snn-mean.la \
        softglow.la \
+       stereographic-projection.la \
        stress.la \
        stretch-contrast-hsv.la \
        stretch-contrast.la \
diff --git a/operations/common/gnomonic-projection.c b/operations/common/gnomonic-projection.c
index 647531b..02a5bdc 100644
--- a/operations/common/gnomonic-projection.c
+++ b/operations/common/gnomonic-projection.c
@@ -82,14 +82,14 @@ calc_long_lat (float x, float  y,
   sin_c = sinf(c);
   cos_c = cosf(c);
 
-  longtitude = asinf (cos_c * sin_tilt + ( y * sin_c * cos_tilt) / p);
-  latitude = pan + atan2f ( x * sin_c, p * cos_tilt * cos_c - y * sin_tilt * sin_c);
+  latitude = asinf (cos_c * sin_tilt + ( y * sin_c * cos_tilt) / p);
+  longtitude = pan + atan2f ( x * sin_c, p * cos_tilt * cos_c - y * sin_tilt * sin_c);
 
-  if (latitude < 0)
-    latitude += M_PI * 2;
+  if (longtitude < 0)
+    longtitude += M_PI * 2;
 
-  *in_long = (latitude / (M_PI * 2));
-  *in_lat = ((longtitude + M_PI/2) / M_PI);
+  *in_long = (longtitude / (M_PI * 2));
+  *in_lat = ((latitude + M_PI/2) / M_PI);
 }
 
 static gboolean
diff --git a/operations/common/stereographic-projection.c b/operations/common/stereographic-projection.c
new file mode 100644
index 0000000..82361b4
--- /dev/null
+++ b/operations/common/stereographic-projection.c
@@ -0,0 +1,229 @@
+/* This file is an image processing operation for GEGL
+ *
+ * GEGL is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * GEGL 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2014 <pippin gimp org>
+ *
+ */
+
+#include <math.h>
+
+#ifdef GEGL_CHANT_PROPERTIES
+
+gegl_chant_double (pan, _("pan"), -180.0, 360, 0.0,
+       _("pan degree of ground for planet"))
+gegl_chant_double (tilt, _("tilt"), -180.0, 180.0, 90.0,
+       _("tilt degree of ground for planet"))
+gegl_chant_double (spin, _("spin"), -360.0, 360.0, 0.0,
+       _("spin of camera / rendering"))
+gegl_chant_double (zoom, _("zoom"), 0.01, 1000.0, 50.0, 
+       _("scale factor"))
+gegl_chant_double (radius, _("local radius"), 0.01, 100.0, 5.0, 
+       _("factor determining dimension of planet"))
+
+gegl_chant_enum (sampler_type, _("Sampler"), GeglSamplerType, gegl_sampler_type,
+                 GEGL_SAMPLER_CUBIC, _("Image resampling method to use"))
+
+#else
+
+#define GEGL_CHANT_TYPE_FILTER
+#define GEGL_CHANT_C_FILE       "stereographic-projection.c"
+
+#include "config.h"
+#include <glib/gi18n-lib.h>
+#include "gegl-chant.h"
+
+
+static void
+prepare (GeglOperation *operation)
+{
+  const Babl *format = babl_format ("RaGaBaA float");
+
+  gegl_operation_set_format (operation, "input", format);
+  gegl_operation_set_format (operation, "output", format);
+}
+
+static GeglRectangle
+get_required_for_output (GeglOperation       *operation,
+                         const gchar         *input_pad,
+                         const GeglRectangle *region)
+{
+  GeglRectangle result = *gegl_operation_source_get_bounding_box (operation, "input");
+
+  return result;
+}
+
+/* formulas from:
+ * http://mathworld.wolfram.com/StereographicProjection.html
+ */
+
+static void inline
+calc_long_lat (float x, float  y,
+               float tilt, float pan, float spin, float radius,
+               float sin_tilt, float cos_tilt,
+               float *in_long, float *in_lat)
+{
+  float p, c;
+  float longtitude, latitude;
+  float sin_c, cos_c;
+  float R = radius;
+
+  p = sqrtf (x*x+y*y);
+  c = 2 * atanf (p / 2 * R);
+
+  sin_c = sinf(c);
+  cos_c = cosf(c);
+
+  latitude = asinf (cos_c * sin_tilt + ( y * sin_c * cos_tilt) / p);
+  longtitude = pan + atan2f ( x * sin_c, p * cos_tilt * cos_c - y * sin_tilt * sin_c);
+
+  if (longtitude < 0)
+    longtitude += M_PI * 2;
+
+  *in_long = (longtitude / (M_PI * 2));
+  *in_lat = ((latitude + M_PI/2) / M_PI);
+}
+
+static gboolean
+process (GeglOperation       *operation,
+         GeglBuffer          *input,
+         GeglBuffer          *output,
+         const GeglRectangle *result,
+         gint                 level)
+{
+  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
+  float pan = o->pan / 360 * M_PI * 2;
+  float spin = o->spin / 360 * M_PI * 2;
+  float zoom = o->zoom / 100.0;
+  float tilt = o->tilt / 360 * M_PI * 2;
+  float width;
+  float height;
+  const Babl           *format_io;
+  GeglSampler          *sampler;
+  GeglBufferIterator   *it;
+  float sin_tilt, cos_tilt;
+  float cos_spin, sin_spin;
+  gint                  index_in, index_out;
+  float xoffset = 0.5;
+  GeglRectangle in_rect = *gegl_operation_source_get_bounding_box (operation, "input");
+  GeglMatrix2  scale_matrix;
+  GeglMatrix2 *scale = NULL;
+  float radius = o->radius;
+
+  format_io = babl_format ("RaGaBaA float");
+  sampler = gegl_buffer_sampler_new (input, format_io, o->sampler_type);
+
+  while (pan > M_PI)
+    pan -= M_PI;
+
+  sin_tilt = sinf (tilt);
+  cos_tilt = cosf (tilt);
+  sin_spin = sinf (spin);
+  cos_spin = cosf (spin);
+
+  width = in_rect.height;
+  height = width;
+  xoffset = ((in_rect.width - height)/height) / 2 + 0.5;
+
+  if (o->sampler_type == GEGL_SAMPLER_NOHALO ||
+      o->sampler_type == GEGL_SAMPLER_LOHALO)
+    scale = &scale_matrix;
+
+    {
+      it = gegl_buffer_iterator_new (output, result, level, format_io, GEGL_BUFFER_WRITE, GEGL_ABYSS_NONE);
+      index_out = 0;
+
+      while (gegl_buffer_iterator_next (it))
+        {
+          gint    i;
+          gint    n_pixels = it->length;
+          gint    x = it->roi->x; /* initial x                   */
+          gint    y = it->roi->y; /*           and y coordinates */
+          float *in = it->data[index_in];
+          float *out = it->data[index_out];
+
+          for (i=0; i<n_pixels; i++)
+            {
+              float cx, cy;
+              float u, v;
+
+              u = ((x/width) - xoffset) / zoom; 
+              v = ((y/height) - 0.5) / zoom; 
+              
+              if (scale)
+              {
+#define gegl_unmap(xx,yy,ud,vd) { \
+  float rx, ry;\
+                  calc_long_lat (\
+                      xx * cos_spin - yy * sin_spin,\
+                      yy * cos_spin + xx * sin_spin,\
+                      tilt, pan, spin, radius,\
+                      sin_tilt, cos_tilt,\
+                      &rx, &ry);\
+  ud = rx;vd = ry;}
+              gegl_sampler_compute_scale (scale_matrix, u, v);
+              gegl_unmap(u,v, cx, cy);
+#undef gegl_unmap
+              }
+              else
+              {
+                  calc_long_lat (
+                      u * cos_spin - v * sin_spin,
+                      v * cos_spin + u * sin_spin,
+                      tilt, pan, spin, radius,
+                      sin_tilt, cos_tilt,
+                      &cx, &cy);
+              }
+
+              gegl_sampler_get (sampler, cx * in_rect.width, cy * in_rect.height,
+                                scale, out, GEGL_ABYSS_NONE);
+
+              in  += 4;
+              out += 4;
+
+              /* update x and y coordinates */
+              x++;
+              if (x >= (it->roi->x + it->roi->width))
+                {
+                  x = it->roi->x;
+                  y++;
+                }
+            }
+        }
+    }
+
+  g_object_unref (sampler);
+
+  return TRUE;
+}
+
+static void
+gegl_chant_class_init (GeglChantClass *klass)
+{
+  GeglOperationClass         *operation_class;
+  GeglOperationFilterClass *filter_class;
+  operation_class = GEGL_OPERATION_CLASS (klass);
+  filter_class  = GEGL_OPERATION_FILTER_CLASS (klass);
+
+  filter_class->process = process;
+  operation_class->prepare = prepare;
+  operation_class->get_required_for_output = get_required_for_output;
+
+  gegl_operation_class_set_keys (operation_class,
+    "name"       , "gegl:stereographic-projection",
+    "categories" , "misc",
+    "description", _("Perform a stereographics / little planet projection of a equirectangular input 
image."),
+    NULL);
+}
+#endif
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 61f3e05..f5d8943 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -93,6 +93,7 @@ operations/common/save.c
 operations/common/shift.c
 operations/common/snn-mean.c
 operations/common/softglow.c
+operations/common/stereographic-projection.c
 operations/common/stress.c
 operations/common/stretch-contrast.c
 operations/common/stretch-contrast-hsv.c


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]