GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/affine.c Lines: 21 63 33.3 %
Date: 2017-05-26 Branches: 1 42 2.4 %

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 "Affine (lerp) mapping on 5 nrrds or constants"
28
static const char *_unrrdu_affineInfoL =
29
(INFO
30
 ". All the 5 arguments can be either nrrds or single "
31
 "floating-point values.  When all args are single values, this "
32
 "is subsuming the functionality of the previous stand-alone "
33
 "\"affine\" program. "
34
 "Use \"-\" for an operand to signify "
35
 "a nrrd to be read from stdin (a pipe).  Note, however, "
36
 "that \"-\" can probably only be used once (reliably).\n "
37
 "* Uses nrrdArithAffine or nrrdArithIterAffine");
38
39
int
40
unrrdu_affineMain(int argc, const char **argv, const char *me,
41
                  hestParm *hparm) {
42
2
  hestOpt *opt = NULL;
43
1
  char *out, *err;
44
1
  NrrdIter *minIn, *in, *maxIn, *minOut, *maxOut, *args[5];
45
  Nrrd *nout, *ntmp=NULL;
46
1
  int type, E, pret, clamp;
47
  airArray *mop;
48
  unsigned int ai, nn;
49
50
1
  hestOptAdd(&opt, NULL, "minIn", airTypeOther, 1, 1, &minIn, NULL,
51
             "Lower end of input value range.",
52
1
             NULL, NULL, nrrdHestIter);
53
1
  hestOptAdd(&opt, NULL, "in", airTypeOther, 1, 1, &in, NULL,
54
             "Input value.",
55
1
             NULL, NULL, nrrdHestIter);
56
1
  hestOptAdd(&opt, NULL, "maxIn", airTypeOther, 1, 1, &maxIn, NULL,
57
             "Upper end of input value range.",
58
1
             NULL, NULL, nrrdHestIter);
59
1
  hestOptAdd(&opt, NULL, "minOut", airTypeOther, 1, 1, &minOut, NULL,
60
             "Lower end of output value range.",
61
1
             NULL, NULL, nrrdHestIter);
62
1
  hestOptAdd(&opt, NULL, "maxOut", airTypeOther, 1, 1, &maxOut, NULL,
63
             "Upper end of output value range.",
64
1
             NULL, NULL, nrrdHestIter);
65
1
  hestOptAdd(&opt, "t,type", "type", airTypeOther, 1, 1, &type, "default",
66
             "type to convert all nrrd inputs to, prior to "
67
             "doing operation.  This also determines output type. "
68
             "By default (not using this option), the types of the input "
69
             "nrrds are left unchanged.",
70
             NULL, NULL, &unrrduHestMaybeTypeCB);
71
1
  hestOptAdd(&opt, "clamp", "bool", airTypeBool, 1, 1, &clamp, "false",
72
             "clamp output values to specified output range");
73
1
  OPT_ADD_NOUT(out, "output nrrd");
74
75
1
  mop = airMopNew();
76
1
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
77
78
2
  USAGE(_unrrdu_affineInfoL);
79
  PARSE();
80
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
81
82
  nout = nrrdNew();
83
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
84
85
  args[0] = minIn;
86
  args[1] = in;
87
  args[2] = maxIn;
88
  args[3] = minOut;
89
  args[4] = maxOut;
90
  nn = 0;
91
  for (ai=0; ai<5; ai++) {
92
    nn += !!args[ai]->ownNrrd;
93
  }
94
  if (nrrdTypeDefault != type) {
95
    /* they wanted to convert nrrds to some other type first */
96
    E = 0;
97
    for (ai=0; ai<5; ai++) {
98
      if (args[ai]->ownNrrd) {
99
        if (!E) E |= nrrdConvert(ntmp=nrrdNew(), args[ai]->ownNrrd, type);
100
        if (!E) nrrdIterSetOwnNrrd(args[ai], ntmp);
101
      }
102
    }
103
    if (E) {
104
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
105
      fprintf(stderr, "%s: error converting input nrrd(s):\n%s", me, err);
106
      airMopError(mop);
107
      return 1;
108
    }
109
  }
110
111
  if (0 == nn) {
112
    /* actually, there are no nrrds; we represent the functionality
113
       of the previous stand-alone binary "affine" */
114
    double valOut;
115
    valOut = AIR_AFFINE(minIn->val, in->val, maxIn->val,
116
                        minOut->val, maxOut->val);
117
    if (clamp) {
118
      double mmin = AIR_MIN(minOut->val, maxOut->val);
119
      double mmax = AIR_MAX(minOut->val, maxOut->val);
120
      valOut = AIR_CLAMP(mmin, valOut, mmax);
121
    }
122
    printf("%g\n", valOut);
123
  } else {
124
    /* we have a nrrd output */
125
    if (1 == nn && in->ownNrrd) {
126
      E = nrrdArithAffine(nout, minIn->val, in->ownNrrd, maxIn->val,
127
                          minOut->val, maxOut->val, clamp);
128
    } else {
129
      E = nrrdArithIterAffine(nout, minIn, in, maxIn, minOut, maxOut, clamp);
130
    }
131
    if (E) {
132
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
133
      fprintf(stderr, "%s: error doing ternary operation:\n%s", me, err);
134
      airMopError(mop);
135
      return 1;
136
    }
137
    SAVE(out, nout, NULL);
138
  }
139
140
  airMopOkay(mop);
141
  return 0;
142
1
}
143
144
UNRRDU_CMD(affine, INFO);
145