GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/1op.c Lines: 15 43 34.9 %
Date: 2017-05-26 Branches: 1 22 4.5 %

Line Branch Exec Source
1
/*
2
  Teem: Tools to process and visualize scientific data and images             .
3
  Copyright (C) 2013, 2012, 2011, 2010, 2009  University of Chicago
4
  Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
5
  Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
6
7
  This library is free software; you can redistribute it and/or
8
  modify it under the terms of the GNU Lesser General Public License
9
  (LGPL) as published by the Free Software Foundation; either
10
  version 2.1 of the License, or (at your option) any later version.
11
  The terms of redistributing and/or modifying this software also
12
  include exceptions to the LGPL that facilitate static linking.
13
14
  This library is distributed in the hope that it will be useful,
15
  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
  Lesser General Public License for more details.
18
19
  You should have received a copy of the GNU Lesser General Public License
20
  along with this library; if not, write to Free Software Foundation, Inc.,
21
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
*/
23
24
#include "unrrdu.h"
25
#include "privateUnrrdu.h"
26
27
#define INFO "Unary operation on a nrrd"
28
static const char *_unrrdu_1opInfoL =
29
  (INFO
30
   ".\n "
31
   "* Uses nrrdArithUnaryOp");
32
33
int
34
unrrdu_1opMain(int argc, const char **argv, const char *me,
35
               hestParm *hparm) {
36
2
  hestOpt *opt = NULL;
37
1
  char *out, *err, *seedS;
38
1
  Nrrd *nin, *nout, *ntmp=NULL;
39
1
  int op, pret, type;
40
  airArray *mop;
41
1
  unsigned int seed;
42
43
1
  hestOptAdd(&opt, NULL, "operator", airTypeEnum, 1, 1, &op, NULL,
44
             "Unary operator. Possibilities include:\n "
45
             "\b\bo \"-\": negative (multiply by -1.0)\n "
46
             "\b\bo \"r\": reciprocal (1.0/value)\n "
47
             "\b\bo \"sin\", \"cos\", \"tan\", \"asin\", \"acos\", \"atan\": "
48
             "same as in C\n "
49
             "\b\bo \"exp\", \"log\", \"log10\": same as in C\n "
50
             "\b\bo \"log1p\", \"expm1\": accurate log(x+1) and exp(x)-1\n "
51
             "\b\bo \"log2\": log base 2\n "
52
             "\b\bo \"sqrt\", \"cbrt\", \"ceil\", \"floor\": same as in C\n "
53
             "\b\bo \"erf\": error function (integral of Gaussian)\n "
54
             "\b\bo \"rup\", \"rdn\": round up or down to integral value\n "
55
             "\b\bo \"abs\": absolute value\n "
56
             "\b\bo \"sgn\": -1, 0, 1 if value is <0, ==0, or >0\n "
57
             "\b\bo \"exists\": 1 iff not NaN or +/-Inf, 0 otherwise\n "
58
             "\b\bo \"rand\": random value in [0.0,1.0), "
59
             "no relation to input\n "
60
             "\b\bo \"nrand\": random sample from normal distribution with "
61
             "mean 0.0 and stdv 1.0, no relation to input\n "
62
             "\b\bo \"if\": if input is non-zero, 1, else 0\n "
63
             "\b\bo \"0\": output always 0\n "
64
             "\b\bo \"1\": output always 1",
65
1
             NULL, nrrdUnaryOp);
66
1
  hestOptAdd(&opt, "s,seed", "seed", airTypeString, 1, 1, &seedS, "",
67
             "seed value for RNG for rand and nrand, so that you "
68
             "can get repeatable results between runs, or, "
69
             "by not using this option, the RNG seeding will be "
70
             "based on the current time");
71
1
  hestOptAdd(&opt, "t,type", "type", airTypeOther, 1, 1, &type, "default",
72
             "convert input nrrd to this type prior to "
73
             "doing operation.  Useful when desired output is float "
74
             "(e.g., with log1p), but input is integral. By default "
75
             "(not using this option), the types of "
76
             "the input nrrds are left unchanged.",
77
             NULL, NULL, &unrrduHestMaybeTypeCB);
78
1
  OPT_ADD_NIN(nin, "input nrrd");
79
1
  OPT_ADD_NOUT(out, "output nrrd");
80
81
1
  mop = airMopNew();
82
1
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
83
84
2
  USAGE(_unrrdu_1opInfoL);
85
  PARSE();
86
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
87
88
  nout = nrrdNew();
89
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
90
91
  if (nrrdTypeDefault != type) {
92
    /* they requested conversion to another type prior to the 1op */
93
    airMopAdd(mop, ntmp=nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
94
    if (nrrdConvert(ntmp, nin, type)) {
95
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
96
      fprintf(stderr, "%s: error converting input nrrd:\n%s", me, err);
97
      airMopError(mop);
98
      return 1;
99
    }
100
  } else {
101
    ntmp = nin;
102
  }
103
  /* see note in 2op.c about the hazards of trying to be clever
104
  ** about minimizing the seeding of the RNG
105
  ** if (nrrdUnaryOpRand == op
106
  **     || nrrdUnaryOpNormalRand == op) {
107
  */
108
  if (airStrlen(seedS)) {
109
    if (1 != sscanf(seedS, "%u", &seed)) {
110
      fprintf(stderr, "%s: couldn't parse seed \"%s\" as uint\n", me, seedS);
111
      airMopError(mop);
112
      return 1;
113
    } else {
114
      airSrandMT(seed);
115
    }
116
  } else {
117
    /* got no request for specific seed */
118
    airSrandMT(AIR_CAST(unsigned int, airTime()));
119
  }
120
  if (nrrdArithUnaryOp(nout, op, ntmp)) {
121
    airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
122
    fprintf(stderr, "%s: error doing unary operation:\n%s", me, err);
123
    airMopError(mop);
124
    return 1;
125
  }
126
  /* if we had to create ntmp with nrrdConvert, it will be mopped,
127
     otherwise ntmp is an alias for nin, which will also be mopped */
128
129
  SAVE(out, nout, NULL);
130
131
  airMopOkay(mop);
132
  return 0;
133
1
}
134
135
UNRRDU_CMD(1op, INFO);