smallconvol.C

Go to the documentation of this file.
00001 /* Gnome gxsm - Gnome X Scanning Microscopy
00002  * universal STM/AFM/SARLS/SPALEED/... controlling and
00003  * data analysis software
00004  * 
00005  * Gxsm Plugin Name: smallconvol.C
00006  * ========================================
00007  * 
00008  * Copyright (C) 1999 The Free Software Foundation
00009  *
00010  * Authors: Percy Zahl <zahl@fkp.uni-hannover.de>
00011  * additional features: Andreas Klust <klust@fkp.uni-hannover.de>
00012  *
00013  * This program is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 2 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * This program is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this program; if not, write to the Free Software
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00026  */
00027 
00028 /* Please do not change the Begin/End lines of this comment section!
00029  * this is a LaTeX style section used for auto generation of the PlugIn Manual 
00030  * Chapter. Add a complete PlugIn documentation inbetween the Begin/End marks!
00031  * --------------------------------------------------------------------------------
00032 % BeginPlugInDocuSection
00033 % PlugInDocuCaption: Convolution with 3x3 kernel
00034 % PlugInName: smallconvol
00035 % PlugInAuthor: Percy Zahl
00036 % PlugInAuthorEmail: zahl@users.sf.net
00037 % PlugInMenuPath: Math/Filter 2D/Small Convol
00038 
00039 % PlugInDescription
00040 \label{plugin:smallconvol}
00041 This plug-in convolutes the active scan with a 3x3 matrix (kernel).  The
00042 kernel can be changed using \GxsmMenu{Tools/Plugin Details} by calling the
00043 plug-ins configure function (c.f.\ \ref{plugin:listplugins}).
00044 
00045 % PlugInUsage
00046 The \GxsmEmph{smallconvol} plug-in can be found in \GxsmMenu{Math/Filter
00047 2D/Small Convol}.  It acts on the active channel and the output is put in
00048 the math channel.
00049 
00050 %% OptPlugInKnownBugs
00051 %No known.
00052 
00053 % EndPlugInDocuSection
00054  * -------------------------------------------------------------------------------- 
00055  */
00056 
00057 
00058 #include <gtk/gtk.h>
00059 #include "config.h"
00060 #include "gxsm/plugin.h"
00061 
00062 
00063 static void smallconvol_init( void );
00064 static void smallconvol_about( void );
00065 static void smallconvol_configure( void );
00066 static void smallconvol_cleanup( void );
00067 static gboolean smallconvol_run( Scan *Src, Scan *Dest );
00068 
00069 GxsmPlugin smallconvol_pi = {
00070   NULL,
00071   NULL,
00072   0,
00073   NULL,
00074   "Smallconvol-M1S-F1D",
00075   NULL,
00076   NULL,
00077   "Percy Zahl",
00078   "_Math/Filter _2D/",
00079   N_("Small Convol"),
00080   N_("Sorry, no help for smallconvol filter!"),
00081   "no more info",
00082   NULL,
00083   NULL,
00084   smallconvol_init,
00085   NULL,
00086   smallconvol_about,
00087   smallconvol_configure,
00088   NULL,
00089   smallconvol_cleanup
00090 };
00091 
00092 GxsmMathOneSrcPlugin smallconvol_m1s_pi = {
00093   smallconvol_run
00094 };
00095 
00096 static const char *about_text = N_("Gxsm Smallconvol Plugin\n\n"
00097                                    "Matrix Convolution PlugIn.");
00098 
00099 GxsmPlugin *get_gxsm_plugin_info ( void ){ 
00100   smallconvol_pi.description = g_strdup_printf(N_("Gxsm MathOneArg smallconvol plugin %s"), VERSION);
00101   return &smallconvol_pi; 
00102 }
00103 
00104 GxsmMathOneSrcPlugin *get_gxsm_math_one_src_plugin_info( void ) { 
00105   return &smallconvol_m1s_pi; 
00106 }
00107 
00108 // Filter Code...
00109 
00110 #define SET_SMC_PROGRESS(P) { smallconvol_pi.app->SetProgress((gfloat)(P)); while (gtk_events_pending()) gtk_main_iteration(); }
00111 
00112 double kernel[3][3];
00113 
00114 static void smallconvol_init(void)
00115 {
00116   PI_DEBUG (DBG_L2, "Smallconvol Plugin Init");
00117   // init kernel:
00118   kernel[0][0] = 1.0;
00119   kernel[0][1] = 1.0;
00120   kernel[0][2] = 1.0;
00121   kernel[1][0] = 1.0;
00122   kernel[1][1] = 1.0;
00123   kernel[1][2] = 1.0;
00124   kernel[2][0] = 1.0;
00125   kernel[2][1] = 1.0;
00126   kernel[2][2] = 1.0;
00127 }
00128 
00129 static void smallconvol_about(void)
00130 {
00131   const gchar *authors[] = { smallconvol_pi.authors, NULL};
00132   gtk_widget_show(gnome_about_new ( smallconvol_pi.name,
00133                                     VERSION,
00134                                     N_("(C) 2000 the Free Software Foundation"),
00135                                     about_text,
00136                                     authors,
00137                                     NULL, NULL, NULL
00138                                     ));
00139 }
00140 
00141 static void smallconvol_configure(void)
00142 {
00143   GtkWidget *dialog;
00144   GtkWidget *vbox;
00145   GtkWidget *hbox;
00146   GtkWidget *info;
00147   GtkWidget *input;
00148 
00149   dialog = gnome_dialog_new(_("Small Convol Kernel Setup"),
00150                             GNOME_STOCK_BUTTON_OK,
00151                             NULL); 
00152         
00153   gnome_dialog_set_close(GNOME_DIALOG(dialog), FALSE);
00154   gnome_dialog_close_hides(GNOME_DIALOG(dialog), FALSE);
00155   gnome_dialog_set_default(GNOME_DIALOG(dialog), 0);
00156 
00157   vbox = gtk_vbox_new (FALSE, 0);
00158   gtk_widget_show (vbox);
00159 
00160   gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dialog)->vbox),
00161                      vbox, TRUE, TRUE, GNOME_PAD);
00162 
00163   info = gtk_label_new ("3x3 Kernel");
00164   gtk_widget_show (info);
00165   gtk_box_pack_start(GTK_BOX(vbox), info, TRUE, TRUE, GNOME_PAD);
00166 
00167   input = smallconvol_pi.app->mygtk_create_input("1", vbox, hbox);
00168   Gtk_EntryControl ECvar11(smallconvol_pi.app->xsm->Unity, "Value out of range !", &kernel[0][0], 
00169                          -999, 999, ".0f", input);
00170   input = smallconvol_pi.app->mygtk_add_input(hbox);
00171   Gtk_EntryControl ECvar12(smallconvol_pi.app->xsm->Unity, "Value out of range !", &kernel[0][1], 
00172                          -999, 999, ".0f", input);
00173   input = smallconvol_pi.app->mygtk_add_input(hbox);
00174   Gtk_EntryControl ECvar13(smallconvol_pi.app->xsm->Unity, "Value out of range !", &kernel[0][2], 
00175                          -999, 999, ".0f", input);
00176 
00177   input = smallconvol_pi.app->mygtk_create_input("2", vbox, hbox);
00178   Gtk_EntryControl ECvar21(smallconvol_pi.app->xsm->Unity, "Value out of range !", &kernel[1][0], 
00179                          -999, 999, ".0f", input);
00180   input = smallconvol_pi.app->mygtk_add_input(hbox);
00181   Gtk_EntryControl ECvar22(smallconvol_pi.app->xsm->Unity, "Value out of range !", &kernel[1][1], 
00182                          -999, 999, ".0f", input);
00183   input = smallconvol_pi.app->mygtk_add_input(hbox);
00184   Gtk_EntryControl ECvar23(smallconvol_pi.app->xsm->Unity, "Value out of range !", &kernel[1][2], 
00185                          -999, 999, ".0f", input);
00186 
00187   input = smallconvol_pi.app->mygtk_create_input("3", vbox, hbox);
00188   Gtk_EntryControl ECvar31(smallconvol_pi.app->xsm->Unity, "Value out of range !", &kernel[2][0], 
00189                          -999, 999, ".0f", input);
00190   input = smallconvol_pi.app->mygtk_add_input(hbox);
00191   Gtk_EntryControl ECvar32(smallconvol_pi.app->xsm->Unity, "Value out of range !", &kernel[2][1], 
00192                          -999, 999, ".0f", input);
00193   input = smallconvol_pi.app->mygtk_add_input(hbox);
00194   Gtk_EntryControl ECvar33(smallconvol_pi.app->xsm->Unity, "Value out of range !", &kernel[2][2], 
00195                          -999, 999, ".0f", input);
00196 
00197   gtk_widget_show(dialog);
00198 
00199   gnome_dialog_run_and_close(GNOME_DIALOG(dialog));
00200 }
00201 
00202 static void smallconvol_cleanup(void)
00203 {
00204   PI_DEBUG (DBG_L2, "Smallconvol Plugin Cleanup");
00205 }
00206 
00207 // Do 2D Convolution with 3x3 kernel --------------------------------------
00208 static gboolean smallconvol_run(Scan *Src, Scan *Dest)
00209 {
00210   int line, col;
00211   ZData *src;
00212 
00213   // new dest size:
00214   // (the scan size is cropped by one line at each side of the scan
00215   //  to get rid of border Problems during the convolution)
00216   Dest->data.s.nx -= 2;
00217   Dest->data.s.ny -= 2;
00218   Dest->mem2d->Resize(Dest->data.s.nx, Dest->data.s.ny);
00219 
00220   // compute normalization factors:
00221   double norm   = 1. / (kernel[0][0] + kernel[0][1] + kernel[0][2] +
00222                         kernel[1][0] + kernel[1][1] + kernel[1][2] +
00223                         kernel[2][0] + kernel[2][1] + kernel[2][2]);
00224 
00225   PI_DEBUG (DBG_L2, "Norm of Kernel is " << norm);
00226   
00227   // Get help object... for fast access access to surrounding data
00228   src  = Src->mem2d->data;
00229   for (line = 0; line < Dest->mem2d->GetNy(); ++line) {
00230     if(!(line%32)) SET_SMC_PROGRESS( line / Dest->mem2d->GetNy() );
00231     src->SetPtrTB(1, line+1); // set to x=1 of second line
00232     for (col = 0; col < Dest->mem2d->GetNx(); ++col) {
00233       Dest->mem2d->PutDataPkt
00234         ( norm * 
00235           ( kernel[0][0] * src->GetThisLT() + // access This-Left-Top
00236             kernel[1][0] * src->GetThisT() + 
00237             kernel[2][0] * src->GetThisRT() + 
00238             
00239             kernel[0][1] * src->GetThisL() + 
00240             kernel[1][1] * src->GetThis() + 
00241             kernel[2][1] * src->GetThisR() + 
00242 
00243             kernel[0][2] * src->GetThisLB() + 
00244             kernel[1][2] * src->GetThisB() + 
00245             kernel[2][2] * src->GetThisRB() 
00246             ), col, line );
00247       src->IncPtrTB(); // go to next x ...
00248     }
00249   }
00250   SET_SMC_PROGRESS( 0. );
00251   return MATH_OK;
00252 }

Generated on Sat Apr 1 09:04:21 2006 for GXSM by  doxygen 1.4.6