1 /*
2 Teem: Tools to process and visualize scientific data and images .
3 Copyright (C) 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
unrrdu_affineMain(int argc,const char ** argv,const char * me,hestParm * hparm)40 unrrdu_affineMain(int argc, const char **argv, const char *me,
41 hestParm *hparm) {
42 hestOpt *opt = NULL;
43 char *out, *err;
44 NrrdIter *minIn, *in, *maxIn, *minOut, *maxOut, *args[5];
45 Nrrd *nout, *ntmp=NULL;
46 int type, E, pret, clamp;
47 airArray *mop;
48 unsigned int ai, nn;
49
50 hestOptAdd(&opt, NULL, "minIn", airTypeOther, 1, 1, &minIn, NULL,
51 "Lower end of input value range.",
52 NULL, NULL, nrrdHestIter);
53 hestOptAdd(&opt, NULL, "in", airTypeOther, 1, 1, &in, NULL,
54 "Input value.",
55 NULL, NULL, nrrdHestIter);
56 hestOptAdd(&opt, NULL, "maxIn", airTypeOther, 1, 1, &maxIn, NULL,
57 "Upper end of input value range.",
58 NULL, NULL, nrrdHestIter);
59 hestOptAdd(&opt, NULL, "minOut", airTypeOther, 1, 1, &minOut, NULL,
60 "Lower end of output value range.",
61 NULL, NULL, nrrdHestIter);
62 hestOptAdd(&opt, NULL, "maxOut", airTypeOther, 1, 1, &maxOut, NULL,
63 "Upper end of output value range.",
64 NULL, NULL, nrrdHestIter);
65 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 hestOptAdd(&opt, "clamp", "bool", airTypeBool, 1, 1, &clamp, "false",
72 "clamp output values to specified output range");
73 OPT_ADD_NOUT(out, "output nrrd");
74
75 mop = airMopNew();
76 airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
77
78 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 valOut = AIR_CLAMP(minOut->val, valOut, maxOut->val);
119 }
120 printf("%g\n", valOut);
121 } else {
122 /* we have a nrrd output */
123 if (1 == nn && in->ownNrrd) {
124 E = nrrdArithAffine(nout, minIn->val, in->ownNrrd, maxIn->val,
125 minOut->val, maxOut->val, clamp);
126 } else {
127 E = nrrdArithIterAffine(nout, minIn, in, maxIn, minOut, maxOut, clamp);
128 }
129 if (E) {
130 airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
131 fprintf(stderr, "%s: error doing ternary operation:\n%s", me, err);
132 airMopError(mop);
133 return 1;
134 }
135 SAVE(out, nout, NULL);
136 }
137
138 airMopOkay(mop);
139 return 0;
140 }
141
142 UNRRDU_CMD(affine, INFO);
143
144