gegl r2504 - in branches/branch2_zhangjb: . operations/frequency operations/frequency/tools



Author: zhangjb
Date: Fri Jun 27 20:53:47 2008
New Revision: 2504
URL: http://svn.gnome.org/viewvc/gegl?rev=2504&view=rev

Log:
* operations/frequency/preview-frequency.c: moved to the new format.
* operations/frequency/tools/display.c: added.


Added:
   branches/branch2_zhangjb/operations/frequency/preview-frequency.c
   branches/branch2_zhangjb/operations/frequency/tools/display.c
Modified:
   branches/branch2_zhangjb/ChangeLog

Added: branches/branch2_zhangjb/operations/frequency/preview-frequency.c
==============================================================================
--- (empty file)
+++ branches/branch2_zhangjb/operations/frequency/preview-frequency.c	Fri Jun 27 20:53:47 2008
@@ -0,0 +1,132 @@
+/* This file is a part of 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 2008 Zhang Junbo  <zhangjb svn gnome org>
+ */
+
+#ifdef GEGL_CHANT_PROPERTIES
+
+/* no properties */
+
+#else
+
+#define GEGL_CHANT_TYPE_FILTER
+#define GEGL_CHANT_C_FILE       "preview-frequency.c"
+
+#include "gegl-chant.h"
+#include "tools/display.c"
+#include "tools/component.c"
+#include <fftw3.h>
+
+static GeglRectangle
+get_bounding_box (GeglOperation *operation)
+{
+  GeglRectangle *in_rect = gegl_operation_source_get_bounding_box (operation,
+                                                                   "input");
+  GeglRectangle  result  = *in_rect;
+
+  result.width = 2*(result.width-1);
+  return result;
+}
+
+static GeglRectangle
+get_cached_region(GeglOperation *operation,
+                  const GeglRectangle *roi)
+{
+  return get_bounding_box(operation);
+}
+
+static GeglRectangle
+get_required_for_output(GeglOperation *operation,
+                        const gchar *input_pad,
+                        const GeglRectangle *roi)
+{
+  GeglRectangle result = *gegl_operation_source_get_bounding_box(operation,
+                                                                 "input");
+  return result;
+}
+
+static void
+prepare(GeglOperation *operation)
+{
+  gegl_operation_set_format(operation, "input",
+                            babl_format("frequency double"));
+  gegl_operation_set_format(operation, "output", babl_format("RGBA double"));
+}
+
+static gboolean
+process(GeglOperation *operation,
+        GeglBuffer *input,
+        GeglBuffer *output,
+        const GeglRectangle *result)
+{
+  gint width = 2*(gegl_buffer_get_width(input)-1); /* width always refers to the
+                                                      image's (not buffer's)
+                                                      width. */
+  gint height = gegl_buffer_get_height(input);
+  gdouble *src_buf;
+  gdouble *dst_buf;
+  gdouble *tmp_src_buf;
+  gdouble *tmp_dst_buf;
+  glong i;
+  
+  src_buf = g_new0(gdouble, 4*2*height*FFT_HALF(width));
+  dst_buf = g_new0(gdouble, 4*width*height);
+  tmp_src_buf = g_new0(gdouble, 2*height*FFT_HALF(width));
+  tmp_dst_buf = g_new0(gdouble, width*height);
+
+  gegl_buffer_get(input, 1.0, NULL, babl_format("frequency double"),
+                  (gdouble *)src_buf, GEGL_AUTO_ROWSTRIDE);
+  for (i=0; i<3; i++)
+    {
+      get_rgba_component(src_buf, tmp_src_buf, i, 2*height*FFT_HALF(width));
+      fre2img((fftw_complex *)tmp_src_buf, tmp_dst_buf, width, height);
+      set_rgba_component(tmp_dst_buf, dst_buf, i, width*height);
+    }
+  for (i=0; i<height*width; i++)
+    {
+      dst_buf[i*4+3] = 1;
+    }
+  
+  gegl_buffer_set(output, 
+                  NULL, babl_format ("RGBA double"), (gdouble *)dst_buf,
+                  GEGL_AUTO_ROWSTRIDE);   
+  g_free(src_buf);
+  g_free(dst_buf);
+  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_bounding_box = get_bounding_box;
+  operation_class->get_required_for_output= get_required_for_output;  
+  operation_class->get_cached_region = get_cached_region;
+
+  operation_class->name = "preview-frequency";
+  operation_class->categories = "frequency";
+  operation_class->description
+    = "Convert the frequency buffer to a displayable RGBA image.";
+}
+
+#endif

Added: branches/branch2_zhangjb/operations/frequency/tools/display.c
==============================================================================
--- (empty file)
+++ branches/branch2_zhangjb/operations/frequency/tools/display.c	Fri Jun 27 20:53:47 2008
@@ -0,0 +1,158 @@
+/* This file is a part of 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 2008 Zhang Junbo  <zhangjb svn gnome org>
+ */
+
+#ifndef FFT_HALF
+#define FFT_HALF(n) (gint)((n)/2+1)
+#define ELEM_ID_MATRIX(x, y, c) ((y)*(c)+(x)) 
+#define ELEM_ID_HALF_MATRIX(x, y, c) ((y)*(FFT_HALF(c))+(x))
+#endif
+
+#include "gegl.h"
+#include <fftw3.h>
+#include <math.h>
+
+static gint fft_complex_get_half_id(gint x, gint y, gint width, gint height);
+gboolean shift_dft(gdouble *buf, gint width, gint height);
+void get_min_max(gdouble *buf, gdouble *min, gdouble *max, glong samples);
+gboolean zoomshow(gdouble *buf, glong samples);
+gboolean fre2img(fftw_complex *src_buf, gdouble *des_buf, gint width, gint height);
+
+static gint
+fft_complex_get_half_id(gint x, gint y, gint width, gint height)
+{
+  if (x >= FFT_HALF(x))
+    {
+      if (y == 0)
+        return ELEM_ID_HALF_MATRIX(width-x, y, width);
+      else
+        return ELEM_ID_HALF_MATRIX(width-x, height-y, width);
+    }
+  else
+    return 0;
+}
+
+gboolean
+shift_dft(gdouble *buf, gint width, gint height)
+{
+  gint cx, cy;
+  gint add_x, add_y;
+  gint x, y;
+  gdouble tmp_buf[width*height];
+
+  cx = width/2;
+  cy = height/2;
+
+  for (x=0; x<width; x++)
+    {
+      for (y=0; y<height; y++)
+        {
+          add_x = (x<(cx+width%2)) ? cx : (0-cx-width%2);
+          add_y = (y<(cy+height%2)) ? cy : (0-cy-height%2);
+          tmp_buf[ELEM_ID_MATRIX(x+add_x, y+add_y, width)] = buf[ELEM_ID_MATRIX(x, y, width)];
+        }
+    }
+  for (x=0; x<width*height; x++)
+    {
+      buf[x] = tmp_buf[x];
+    }
+
+  return TRUE;
+}
+
+void
+get_min_max(gdouble *buf, gdouble *min, gdouble *max, glong samples)
+{
+  gfloat tmin = 9000000.0;
+  gfloat tmax =-9000000.0;
+
+  gint i;
+  for (i=0; i<samples; i++)
+    {
+      gfloat val = buf[i];
+      if (val<tmin)
+        tmin=val;
+      if (val>tmax)
+        tmax=val;
+    }
+  if (min)
+    *min = tmin;
+  if (max)
+    *max = tmax;
+}
+
+gboolean
+zoomshow(gdouble *buf, glong samples)
+{
+  glong i;
+  gdouble min, max;
+
+  for (i=0; i<samples; i++)
+    {
+      if (*(buf+i)<1)
+        *(buf+i) = 0;
+      else
+        *(buf+i) = log(*(buf+i));
+    }
+  get_min_max(buf, &min, &max, samples);
+  for (i=0; i<samples; i++)
+    {
+      *(buf+i) = (*(buf+i))/max;
+    }
+  return TRUE;
+}
+
+gboolean
+fre2img(fftw_complex *src_buf, gdouble *dst_buf, gint width, gint height)
+{
+  gint x, y;
+  glong samples = width*height;
+  gdouble *dst_real_buf;
+  gdouble *dst_imag_buf;
+
+  dst_real_buf = g_new0(gdouble, width*height);
+  dst_imag_buf = g_new0(gdouble, width*height);
+
+  for (y=0; y<height; y++)
+    {
+      for (x=0; x<width; x++)
+        {
+          if (x<FFT_HALF(width))
+            {
+              dst_real_buf[ELEM_ID_MATRIX(x, y, width)] = src_buf[ELEM_ID_HALF_MATRIX(x, y, width)][0];
+              dst_imag_buf[ELEM_ID_MATRIX(x, y, width)] = src_buf[ELEM_ID_HALF_MATRIX(x, y, width)][1];
+            }
+          else
+            {
+              dst_real_buf[ELEM_ID_MATRIX(x, y, width)] = src_buf[fft_complex_get_half_id(x, y, width, height)][0];
+              dst_imag_buf[ELEM_ID_MATRIX(x, y, width)] = 0-src_buf[fft_complex_get_half_id(x, y, width, height)][1];
+            }
+        }
+    }
+  for (x=0; x<width*height; x++)
+    {
+      dst_buf[x] = sqrt(dst_real_buf[x]*dst_real_buf[x]+dst_imag_buf[x]*dst_imag_buf[x]);
+    }
+  
+  zoomshow(dst_buf, samples);
+  shift_dft(dst_buf, width, height);
+
+  g_free(dst_real_buf);
+  g_free(dst_imag_buf);
+  return TRUE;
+
+}



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