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 "ten.h"
25 #include "privateTen.h"
26 
27 #define INFO "Converts masked non-redundant tensor images to redundant"
28 static const char *_tend_expandInfoL =
29   (INFO
30    ". For images of 3D tensors, this converts from a 7-value tensor "
31    "starting with the confidence/mask value "
32    "(conf, Dxx, Dxy, Dxz, Dyy, Dyz, Dzz) to "
33    "a 9-value tensor with the full matrix "
34    "(Dxx, Dxy, Dxz, Dxy, Dyy, Dyz, Dxz, Dyz, Dzz). "
35    "This is set to all zeros when the confidence is below the given "
36    "threshold. For images of 2D tensors, the conversion is from "
37    "(conf, Dxx, Dxy, Dyy) to (Dxx, Dxy, Dxy, Dyy). " );
38 
39 int
tend_expandMain(int argc,const char ** argv,const char * me,hestParm * hparm)40 tend_expandMain(int argc, const char **argv, const char *me,
41                 hestParm *hparm) {
42   int pret;
43   hestOpt *hopt = NULL;
44   char *perr, *err;
45   airArray *mop;
46 
47   Nrrd *nin, *nout;
48   char *outS;
49   int orientRed, orientRedWithOrigin, mfRed;
50   float scale, thresh;
51 
52   hestOptAdd(&hopt, "t", "thresh", airTypeFloat, 1, 1, &thresh, "0.5",
53              "confidence level to threshold output tensors at.  Should "
54              "be between 0.0 and 1.0.");
55   hestOptAdd(&hopt, "s", "scale", airTypeFloat, 1, 1, &scale, "1.0",
56              "how to scale values before saving as 9-value tensor.  Useful "
57              "for visualization tools which assume certain characteristic "
58              "ranges of eigenvalues");
59   hestOptAdd(&hopt, "unmf", NULL, airTypeInt, 0, 0, &mfRed, NULL,
60              "apply and remove the measurement frame, if it exists");
61   hestOptAdd(&hopt, "ro", NULL, airTypeInt, 0, 0, &orientRed, NULL,
62              "reduce general image orientation to axis-aligned spacings");
63   hestOptAdd(&hopt, "roo", NULL, airTypeInt, 0, 0,
64              &orientRedWithOrigin, NULL,
65              "reduce general image orientation to axis-aligned spacings, "
66              "while also making some effort to set axis mins from "
67              "space origin");
68   hestOptAdd(&hopt, "i", "nin", airTypeOther, 1, 1, &nin, "-",
69              "input diffusion tensor volume, with 7 values per sample",
70              NULL, NULL, nrrdHestNrrd);
71   hestOptAdd(&hopt, "o", "nout", airTypeString, 1, 1, &outS, NULL,
72              "output tensor volume, with the 9 matrix components per sample");
73 
74   mop = airMopNew();
75   airMopAdd(mop, hopt, (airMopper)hestOptFree, airMopAlways);
76   USAGE(_tend_expandInfoL);
77   PARSE();
78   airMopAdd(mop, hopt, (airMopper)hestParseFree, airMopAlways);
79 
80   nout = nrrdNew();
81   airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
82   if (mfRed
83       && 3 == nin->spaceDim
84       && AIR_EXISTS(nin->measurementFrame[0][0])) {
85     if (tenMeasurementFrameReduce(nin, nin)) {
86       airMopAdd(mop, err=biffGetDone(TEN), airFree, airMopAlways);
87       fprintf(stderr, "%s: trouble with measurement frame:\n%s\n", me, err);
88       airMopError(mop); return 1;
89     }
90   }
91   if (4 == nin->axis[0].size
92       ? tenExpand2D(nout, nin, scale, thresh)
93       : tenExpand(nout, nin, scale, thresh)) {
94     airMopAdd(mop, err=biffGetDone(TEN), airFree, airMopAlways);
95     fprintf(stderr, "%s: trouble expanding tensors:\n%s\n", me, err);
96     airMopError(mop); return 1;
97   }
98   if (orientRedWithOrigin || orientRed) {
99     if (nrrdOrientationReduce(nout, nout,
100                               orientRedWithOrigin
101                               ? AIR_TRUE
102                               : AIR_FALSE)) {
103       airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
104       fprintf(stderr, "%s: trouble unorienting:\n%s\n", me, err);
105       airMopError(mop); return 1;
106     }
107   }
108 
109   if (nrrdSave(outS, nout, NULL)) {
110     airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
111     fprintf(stderr, "%s: trouble writing:\n%s\n", me, err);
112     airMopError(mop); return 1;
113   }
114 
115   airMopOkay(mop);
116   return 0;
117 }
118 TEND_CMD(expand, INFO);
119