[r3531]: branches / Gxsm-2x3-transition-sig / plug-ins / math / background / removelineshifts.C Maximize Restore History

Download this file

removelineshifts.C    448 lines (376 with data), 14.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* Gnome gxsm - Gnome X Scanning Microscopy
* universal STM/AFM/SARLS/SPALEED/... controlling and
* data analysis software
*
* plugin_helper reports your answers as
author =percy
email =zahl@users.sourceforge.net
pluginname =removelineshifts
pluginmenuentry =Rm Line Shifts
menupath =_Math/Background/
entryplace =Background
shortentryplace =BG
abouttext =remove line shifts
smallhelp =removes line shifts by determining jumps in average height inbetween lines using a threshold
longhelp =removes line shifts by determining jumps in average height inbetween lines using a threshold
*
* Gxsm Plugin Name: removelineshifts.C
* ========================================
*
* Copyright (C) 1999 The Free Software Foundation
*
* Authors: Percy Zahl <zahl@fkp.uni-hannover.de>
* additional features: Andreas Klust <klust@fkp.uni-hannover.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* Please do not change the Begin/End lines of this comment section!
* this is a LaTeX style section used for auto generation of the PlugIn Manual
* Chapter. Add a complete PlugIn documentation inbetween the Begin/End marks!
* All "% PlugInXXX" commentary tags are mandatory
* All "% OptPlugInXXX" tags are optional and can be removed or commented in
* --------------------------------------------------------------------------------
% BeginPlugInDocuSection
% PlugInDocuCaption: Remove Line Shifts
% PlugInName: removelineshifts
% PlugInAuthor: percy
% PlugInAuthorEmail: zahl@users.sourceforge.net
% PlugInMenuPath: _Math/Background/Rm Line Shifts
% PlugInDescription
This filter removes $Z$ line shifts from the image background -- most
commonly due to tip changes or any other sudden Z changes from one to
the next line. It works by comparing the 2nd order change of average
scan line $Z$ value to an given threashold value, i.e. it determines a
jump in the $\partial_Y Z$ (Y-slope), if so, this jump is evaluated and $Z$ of
all following lines is ajusted. The the filter asks for the treashold value.
% PlugInUsage
Activate source scan and call it filter from \GxsmMenu{Math/Background/Rm Line Shifts} menu.
Input the desired threashold value.
%% OptPlugInSection: replace this by the section caption
%all following lines until next tag are going into this section
%...
%% OptPlugInSubSection: replace this line by the subsection caption
%all following lines until next tag are going into this subsection
%...
%% you can repeat OptPlugIn(Sub)Sections multiple times!
%% OptPlugInSources
%The active channel is used as data source.
%% OptPlugInObjects
%A optional rectangle is used for data extraction...
%% OptPlugInDest
%The computation result is placed into an existing math channel, else into a new created math channel.
%% OptPlugInConfig
%describe the configuration options of your plug in here!
%% OptPlugInFiles
%Does it uses, needs, creates any files? Put info here!
%% OptPlugInRefs
%Any references?
%% OptPlugInKnownBugs
%Are there known bugs? List! How to work around if not fixed?
%% OptPlugInNotes
%If you have any additional notes
%% OptPlugInHints
%Any tips and tricks?
% EndPlugInDocuSection
* --------------------------------------------------------------------------------
*/
#include <gtk/gtk.h>
#include "config.h"
#include "gxsm/plugin.h"
// Plugin Prototypes
static void removelineshifts_init( void );
static void removelineshifts_about( void );
static void removelineshifts_configure( void );
static void removelineshifts_cleanup( void );
// Define Type of math plugin here, only one line should be commented in!!
#define GXSM_ONE_SRC_PLUGIN__DEF
// #define GXSM_TWO_SRC_PLUGIN__DEF
// Math-Run-Function, use only one of (automatically done :=)
#ifdef GXSM_ONE_SRC_PLUGIN__DEF
// "OneSrc" Prototype
static gboolean removelineshifts_run( Scan *Src, Scan *Dest );
#else
// "TwoSrc" Prototype
static gboolean removelineshifts_run( Scan *Src1, Scan *Src2, Scan *Dest );
#endif
// Fill in the GxsmPlugin Description here
GxsmPlugin removelineshifts_pi = {
NULL, // filled in and used by Gxsm, don't touch !
NULL, // filled in and used by Gxsm, don't touch !
0, // filled in and used by Gxsm, don't touch !
NULL, // The Gxsm-App Class Ref.pointer (called "gapp" in Gxsm) is
// filled in here by Gxsm on Plugin load,
// just after init() is called !!!
// ----------------------------------------------------------------------
// Plugins Name, CodeStly is like: Name-M1S|M2S-BG|F1D|F2D|ST|TR|Misc
"removelineshifts-"
#ifdef GXSM_ONE_SRC_PLUGIN__DEF
"M1S"
#else
"M2S"
#endif
"-BG",
// Plugin's Category - used to autodecide on Pluginloading or ignoring
// NULL: load, else
// example: "+noHARD +STM +AFM"
// load only, if "+noHARD: no hardware" and Instrument is STM or AFM
// +/-xxxHARD und (+/-INST or ...)
NULL,
// Description, is shown by PluginViewer (Plugin: listplugin, Tools->Plugin Details)
g_strdup("removes line shifts by determining jumps in average height inbetween lines using a threshold"),
// Author(s)
"percy",
// Menupath to position where it is appendet to
N_("_Math/_Background/"),
// Menuentry
N_("Rm Line Shifts"),
// help text shown on menu
N_("removes line shifts by determining jumps in average height inbetween lines using a threshold"),
// more info...
"removes line shifts by determining jumps in average height inbetween lines using a threshold",
NULL, // error msg, plugin may put error status msg here later
NULL, // Plugin Status, managed by Gxsm, plugin may manipulate it too
// init-function pointer, can be "NULL",
// called if present at plugin load
removelineshifts_init,
// query-function pointer, can be "NULL",
// called if present after plugin init to let plugin manage it install itself
NULL, // query should be "NULL" for Gxsm-Math-Plugin !!!
// about-function, can be "NULL"
// can be called by "Plugin Details"
removelineshifts_about,
// configure-function, can be "NULL"
// can be called by "Plugin Details"
removelineshifts_configure,
// run-function, can be "NULL", if non-Zero and no query defined,
// it is called on menupath->"plugin"
NULL, // run should be "NULL" for Gxsm-Math-Plugin !!!
// cleanup-function, can be "NULL"
// called if present at plugin removeal
removelineshifts_cleanup
};
// special math Plugin-Strucure, use
// GxsmMathOneSrcPlugin removelineshifts_m1s_pi -> "OneSrcMath"
// GxsmMathTwoSrcPlugin removelineshifts_m2s_pi -> "TwoSrcMath"
#ifdef GXSM_ONE_SRC_PLUGIN__DEF
GxsmMathOneSrcPlugin removelineshifts_m1s_pi
#else
GxsmMathTwoSrcPlugin removelineshifts_m2s_pi
#endif
= {
// math-function to run, see prototype(s) above!!
removelineshifts_run
};
// Text used in Aboutbox, please update!!
static const char *about_text = N_("Gxsm removelineshifts Plugin\n\n"
"remove line shifts");
// Symbol "get_gxsm_plugin_info" is resolved by dlsym from Gxsm, used to get Plugin's info!!
// Essential Plugin Function!!
GxsmPlugin *get_gxsm_plugin_info ( void ){
removelineshifts_pi.description = g_strdup_printf(N_("Gxsm MathOneArg removelineshifts plugin %s"), VERSION);
return &removelineshifts_pi;
}
// Symbol "get_gxsm_math_one|two_src_plugin_info" is resolved by dlsym from Gxsm,
// used to find out which Math Type the Plugin is!!
// Essential Plugin Function!!
#ifdef GXSM_ONE_SRC_PLUGIN__DEF
GxsmMathOneSrcPlugin *get_gxsm_math_one_src_plugin_info( void ) {
return &removelineshifts_m1s_pi;
}
#else
GxsmMathTwoSrcPlugin *get_gxsm_math_two_src_plugin_info( void ) {
return &removelineshifts_m2s_pi;
}
#endif
/* Here we go... */
// init-Function
static void removelineshifts_init(void)
{
PI_DEBUG (DBG_L2, "Plugin Init" );
}
// about-Function
static void removelineshifts_about(void)
{
const gchar *authors[] = { removelineshifts_pi.authors, NULL};
gtk_widget_show(gnome_about_new ( removelineshifts_pi.name,
VERSION,
N_("(C) 2000 the Free Software Foundation"),
about_text,
authors,
NULL, NULL, NULL
));
}
// configure-Function
static void removelineshifts_configure(void)
{
if(removelineshifts_pi.app)
removelineshifts_pi.app->message("removelineshifts Plugin Configuration");
}
// cleanup-Function
static void removelineshifts_cleanup(void)
{
PI_DEBUG (DBG_L2, "Plugin Cleanup");
}
void setup_multidimensional_data_copy (const gchar *title, Scan *src, int &ti, int &tf, int &vi, int &vf, int *crop_window_xy=NULL, gboolean crop=FALSE){
GtkWidget *input, *label, *dialog, *vbox, *hbox;
Gtk_EntryControl *ec[8];
int k=0;
for (int i=0; i<8; ++i)
ec[i] = NULL;
dialog = gtk_dialog_new_with_buttons (_(title),
NULL,
(GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
NULL);
vbox = gtk_vbox_new (FALSE, 0);
gtk_widget_show (vbox);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
vbox, TRUE, TRUE, GNOME_PAD);
UnitObj *Pixel = new UnitObj("Pix","Pix");
UnitObj *Unity = new UnitObj(" "," ");
label = gtk_label_new ("New Time and Layer bounds:");
gtk_container_add (GTK_CONTAINER (vbox), label);
gint t_max = src->number_of_time_elements ()-1;
if (t_max < 0) t_max=0;
input = gapp->mygtk_create_input("t-initial", vbox, hbox);
ec[k++] = new Gtk_EntryControl (Unity, "Value out of range !", &ti, 0, t_max, ".0f", input);
input = gapp->mygtk_create_input("t-final", vbox, hbox);
ec[k++] = new Gtk_EntryControl (Unity, "Value out of range !", &tf, 0, t_max, ".0f", input);
input = gapp->mygtk_create_input("lv-initial", vbox, hbox);
ec[k++] = new Gtk_EntryControl (Unity, "Value out of range !", &vi, 0, src->mem2d->GetNv (), ".0f", input);
input = gapp->mygtk_create_input("lv-final", vbox, hbox);
ec[k++] = new Gtk_EntryControl (Unity, "Value out of range !", &vf, 0, src->mem2d->GetNv (), ".0f", input);
if (crop && crop_window_xy){
label = gtk_label_new ("Crop window bounds:");
gtk_container_add (GTK_CONTAINER (vbox), label);
input = gapp->mygtk_create_input("X-left", vbox, hbox);
ec[k++] = new Gtk_EntryControl (Pixel, "Value out of range !", &crop_window_xy[0], 0, src->mem2d->GetNx ()-1, ".0f", input);
input = gapp->mygtk_create_input("Y-top", vbox, hbox);
ec[k++] = new Gtk_EntryControl (Pixel, "Value out of range !", &crop_window_xy[1], 0, src->mem2d->GetNy ()-1, ".0f", input);
input = gapp->mygtk_create_input("X-right", vbox, hbox);
ec[k++] = new Gtk_EntryControl (Pixel, "Value out of range !", &crop_window_xy[2], 0, src->mem2d->GetNx ()-1, ".0f", input);
input = gapp->mygtk_create_input("Y-bottom", vbox, hbox);
ec[k++] = new Gtk_EntryControl (Pixel, "Value out of range !", &crop_window_xy[3], 0, src->mem2d->GetNy ()-1, ".0f", input);
}
gtk_widget_show_all (dialog);
gtk_dialog_run (GTK_DIALOG(dialog));
gtk_widget_destroy (dialog);
for (int i=0; i<k; ++i)
if (ec[i])
delete ec[i];
delete Pixel;
delete Unity;
}
// run-Function
static gboolean removelineshifts_run(Scan *Src, Scan *Dest)
{
double avg, avg_prev1, avg_prev2, avg_delta1, avg_delta2, Z_adjust;
int nx,ny;
int x1,x2,y1,y2;
double threashold = 10.;
ZData *SrcZ, *DestZ;
nx=Dest->mem2d->GetNx();
ny=Dest->mem2d->GetNy();
x1=0; x2=nx; y1=y2=0;
int ti=0;
int tf=0;
int vi=0;
int vf=0;
gboolean multidim = FALSE;
if (Src->data.s.ntimes != 1 || Src->mem2d->GetNv () != 1){
multidim = TRUE;
do {
ti=vi=0;
tf=Src->number_of_time_elements ()-1;
if (tf < 0) tf = 0;
vf=Src->mem2d->GetNv ()-1;
setup_multidimensional_data_copy ("Multidimensional Rm Line Shifts", Src, ti, tf, vi, vf);
} while (ti > tf || vi > vf);
gapp->progress_info_new ("Multidimenssional Rm Line Shifts", 2);
gapp->progress_info_set_bar_fraction (0., 1);
gapp->progress_info_set_bar_fraction (0., 2);
gapp->progress_info_set_bar_text ("Time", 1);
gapp->progress_info_set_bar_text ("Value", 2);
}
gapp->ValueRequest("Z shift threashold", "Level", "Set threashold [Ang]",
gapp->xsm->Unity, -1e4, 1e4, ".2f", &threashold);
if (Src->number_of_object () > 0){
XSM_DEBUG(DBG_L2, "found object(s)" );
if( Src->get_object_data (0) -> get_num_points () == 2){
double x=0.;
double y=0.;
Src->get_object_data (0) -> get_xy (0, x, y);
Src->World2Pixel (x,y, x1,y1);
Src->get_object_data (0) -> get_xy (1, x, y);
Src->World2Pixel (x,y, x2,y2);
XSM_DEBUG(DBG_L2, "got x range " << x1 << " .. " << x2 );
}
if (x1 > x2){
int tmp = x1;
x1 = x2;
x2 = tmp;
}
if (y1 > y2){
int tmp = y1;
y1 = y2;
y2 = tmp;
}
}
int ntimes_tmp = tf-ti+1;
for (int time_index=ti; time_index <= tf; ++time_index){
Mem2d *m = Src->mem2d_time_element (time_index);
if (multidim)
gapp->progress_info_set_bar_fraction ((gdouble)(time_index-ti)/(gdouble)ntimes_tmp, 1);
Dest->mem2d->Resize (m->GetNx (), m->GetNy (), vf-vi+1, m->GetTyp());
for (int v_index = vi; v_index <= vf; ++v_index){
m->SetLayer (v_index);
Dest->mem2d->SetLayer (v_index-vi);
Dest->mem2d->CopyFrom(m, 0,0, 0,0, nx,ny);
SrcZ = m->data;
DestZ = Dest->mem2d->data;
Z_adjust = 0.;
avg_prev1 = 0.;
avg_prev2 = 0.;
for (int line = y1; line < ny; line++) {
avg = 0.;
SrcZ->SetPtr(0,line);
if (line < y2){
for (int col = x1; col < x2; col++)
avg += SrcZ->GetNext();
avg /= (x2-x1);
avg_delta1 = avg - avg_prev1;
avg_delta2 = avg_prev1 - avg_prev2;
if ( fabs (avg_delta2 - avg_delta1) > threashold) // 2nd order check
Z_adjust += avg_delta1;
avg_prev2 = avg_prev1;
avg_prev1 = avg;
}
SrcZ->SetPtr(0,line);
DestZ->SetPtr(0,line);
for (int col = 0; col < nx; col++)
DestZ->SetNext(SrcZ->GetNext() - Z_adjust);
}
}
Dest->append_current_to_time_elements (time_index-ti, m->get_frame_time ());
}
Dest->data.s.ntimes = ntimes_tmp;
Dest->data.s.nvalues=Dest->mem2d->GetNv ();
if (multidim){
gapp->progress_info_close ();
Dest->retrieve_time_element (0);
Dest->mem2d->SetLayer(0);
}
return MATH_OK;
}