File: invert_cpp.cc

package info (click to toggle)
gwyddion 2.67-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 54,152 kB
  • sloc: ansic: 412,023; python: 7,885; sh: 5,492; makefile: 4,957; xml: 3,954; cpp: 2,107; pascal: 418; perl: 154; ruby: 130
file content (84 lines) | stat: -rwxr-xr-x 2,419 bytes parent folder | download | duplicates (3)
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
/**
 * $Id: invert_cpp.cc 22454 2019-08-31 19:53:40Z yeti-dn $
 * A very simple Gwyddion plug-in example in C++.
 * Written by Yeti <yeti@gwyddion.net>.  Public domain.
 **/
#include <dump.hh>
#include <plugin-helper.hh>
#include <cfloat>
#include <iostream>

using namespace std;

/* Plug-in helper data.
 * Each action (register, run) is represented by a PluginAction object with
 * one function actually performing the action. */
static bool action_register(char *args[]);
static bool action_run     (char *args[]);

static PluginAction plugin_actions[] = {
    /* name               arguments action */
    { string("register"), 0,        &action_register },
    { string("run"),      2,        &action_run      },
};

#define NACTIONS sizeof(plugin_actions)/sizeof(plugin_actions[0])

int
main(int argc, char *argv[])
{
    /* Just let plug-in helper decide what to do */
    return !run_action(NACTIONS, plugin_actions, argc, argv);
}

/* "register" action: print registration information to standard output */
static bool
action_register(char *args[])
{
    cout << "invert_cpp" << endl;
    cout << "/_Test/Value Invert (C++)" << endl;
    cout << "noninteractive with_defaults" << endl;
    return true;
}

/* "run" action: actually do something;
 * the first argument is run mode, the second one a dump file name to read
 * and overwrite with result */
static bool
action_run(char *args[])
{
    /* Run mode sanity check */
    if (string("noninteractive") != args[0]
        && string("with_defaults") != args[0])
        return false;

    /* Read the dump file */
    Dump dump;
    dump.read(args[1]);

    /* Get "/0/data" data field, i.e., the main data. */
    map<string,DataField>::iterator iter = dump.data.find(string("/0/data"));
    /* Find minimum and maximum to keep data range during value inversion */
    double min = DBL_MAX;
    double max = -DBL_MAX;
    unsigned long int n = iter->second.xres * iter->second.yres;
    double *a = iter->second.data;
    unsigned long int i;
    for (i = 0; i < n; i++) {
        if (a[i] < min)
            min = a[i];
        if (a[i] > max)
            max = a[i];
    }
    /* Invert */
    double mirror = min + max;
    for (i = 0; i < n; i++)
        a[i] = mirror - a[i];

    /* Write back the dump file */
    dump.write(args[1]);

    return true;
}

/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */