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 "Map nrrd through whole nrrd of univariate lookup tables" |
28 |
|
|
static const char *_unrrdu_mlutInfoL = |
29 |
|
|
(INFO |
30 |
|
|
", with one lookup table per element of input nrrd. The multiple " |
31 |
|
|
"tables are stored in a nrrd with a dimension which is either 1 or 2 " |
32 |
|
|
"more than the dimension of the input nrrd, resulting in an output " |
33 |
|
|
"which has either the same or one more dimension than the input, " |
34 |
|
|
"resptectively.\n " |
35 |
|
|
"* Uses nrrdApplyMulti1DLut"); |
36 |
|
|
|
37 |
|
|
int |
38 |
|
|
unrrdu_mlutMain(int argc, const char **argv, const char *me, |
39 |
|
|
hestParm *hparm) { |
40 |
|
2 |
hestOpt *opt = NULL; |
41 |
|
1 |
char *out, *err; |
42 |
|
1 |
Nrrd *nin, **_nmlut, *nmlut, *nout; |
43 |
|
|
airArray *mop; |
44 |
|
1 |
int typeOut, rescale, pret, blind8BitRange; |
45 |
|
1 |
unsigned int _nmlutLen, mapAxis; |
46 |
|
1 |
double min, max; |
47 |
|
|
NrrdRange *range=NULL; |
48 |
|
|
|
49 |
|
1 |
hestOptAdd(&opt, "m,map", "mlut", airTypeOther, 1, -1, &_nmlut, NULL, |
50 |
|
|
"one nrrd of lookup tables to map input nrrd through, or, " |
51 |
|
|
"list of nrrds which contain the individual entries of " |
52 |
|
|
"the lookup table at each voxel, which will be joined together.", |
53 |
|
1 |
&_nmlutLen, NULL, nrrdHestNrrd); |
54 |
|
1 |
hestOptAdd(&opt, "r,rescale", NULL, airTypeInt, 0, 0, &rescale, NULL, |
55 |
|
|
"rescale the input values from the input range to the " |
56 |
|
|
"lut domain. The lut domain is either explicitly " |
57 |
|
|
"defined by the axis min,max along axis 0 or 1, or, it " |
58 |
|
|
"is implicitly defined as zero to the length of that axis " |
59 |
|
|
"minus one."); |
60 |
|
1 |
hestOptAdd(&opt, "min,minimum", "value", airTypeDouble, 1, 1, &min, "nan", |
61 |
|
|
"Low end of input range. Defaults to lowest value " |
62 |
|
|
"found in input nrrd. Explicitly setting this is useful " |
63 |
|
|
"only with rescaling (\"-r\")"); |
64 |
|
1 |
hestOptAdd(&opt, "max,maximum", "value", airTypeDouble, 1, 1, &max, "nan", |
65 |
|
|
"High end of input range. Defaults to highest value " |
66 |
|
|
"found in input nrrd. Explicitly setting this is useful " |
67 |
|
|
"only with rescaling (\"-r\")"); |
68 |
|
1 |
hestOptAdd(&opt, "blind8", "bool", airTypeBool, 1, 1, &blind8BitRange, |
69 |
|
1 |
nrrdStateBlind8BitRange ? "true" : "false", |
70 |
|
|
"Whether to know the range of 8-bit data blindly " |
71 |
|
|
"(uchar is always [0,255], signed char is [-128,127]). " |
72 |
|
|
"Explicitly setting this is useful only with rescaling (\"-r\")"); |
73 |
|
1 |
hestOptAdd(&opt, "t,type", "type", airTypeOther, 1, 1, &typeOut, "default", |
74 |
|
|
"specify the type (\"int\", \"float\", etc.) of the " |
75 |
|
|
"output nrrd. " |
76 |
|
|
"By default (not using this option), the output type " |
77 |
|
|
"is the lut's type.", |
78 |
|
|
NULL, NULL, &unrrduHestMaybeTypeCB); |
79 |
|
1 |
OPT_ADD_NIN(nin, "input nrrd"); |
80 |
|
1 |
OPT_ADD_NOUT(out, "output nrrd"); |
81 |
|
|
|
82 |
|
1 |
mop = airMopNew(); |
83 |
|
1 |
airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways); |
84 |
|
|
|
85 |
✓✗ |
2 |
USAGE(_unrrdu_mlutInfoL); |
86 |
|
|
PARSE(); |
87 |
|
|
airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways); |
88 |
|
|
|
89 |
|
|
nout = nrrdNew(); |
90 |
|
|
airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways); |
91 |
|
|
|
92 |
|
|
/* by the end of this block we need to have nmlut and mapAxis */ |
93 |
|
|
if (1 == _nmlutLen) { |
94 |
|
|
/* we got the mlut as a single nrrd */ |
95 |
|
|
nmlut = _nmlut[0]; |
96 |
|
|
mapAxis = nmlut->dim - nin->dim - 1; |
97 |
|
|
/* its not our job to do real error checking ... */ |
98 |
|
|
mapAxis = AIR_MIN(mapAxis, nmlut->dim - 1); |
99 |
|
|
} else { |
100 |
|
|
/* we have to join together multiple nrrds to get the mlut */ |
101 |
|
|
nmlut = nrrdNew(); |
102 |
|
|
airMopAdd(mop, nmlut, (airMopper)nrrdNuke, airMopAlways); |
103 |
|
|
/* assume that mlut component nrrds are all compatible sizes, |
104 |
|
|
nrrdJoin will fail if they aren't */ |
105 |
|
|
mapAxis = _nmlut[0]->dim - nin->dim; |
106 |
|
|
if (nrrdJoin(nmlut, (const Nrrd*const*)_nmlut, _nmlutLen, |
107 |
|
|
mapAxis, AIR_TRUE)) { |
108 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
109 |
|
|
fprintf(stderr, "%s: trouble joining mlut:\n%s", me, err); |
110 |
|
|
airMopError(mop); |
111 |
|
|
return 1; |
112 |
|
|
} |
113 |
|
|
/* set these if they were given, they'll be NaN otherwise */ |
114 |
|
|
nmlut->axis[mapAxis].min = min; |
115 |
|
|
nmlut->axis[mapAxis].max = max; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
if (!( AIR_EXISTS(nmlut->axis[mapAxis].min) && |
119 |
|
|
AIR_EXISTS(nmlut->axis[mapAxis].max) )) { |
120 |
|
|
rescale = AIR_TRUE; |
121 |
|
|
} |
122 |
|
|
if (rescale) { |
123 |
|
|
range = nrrdRangeNew(min, max); |
124 |
|
|
airMopAdd(mop, range, (airMopper)nrrdRangeNix, airMopAlways); |
125 |
|
|
nrrdRangeSafeSet(range, nin, blind8BitRange); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
if (nrrdTypeDefault == typeOut) { |
129 |
|
|
typeOut = nmlut->type; |
130 |
|
|
} |
131 |
|
|
if (nrrdApplyMulti1DLut(nout, nin, range, nmlut, typeOut, rescale)) { |
132 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
133 |
|
|
fprintf(stderr, "%s: trouble applying multi-LUT:\n%s", me, err); |
134 |
|
|
airMopError(mop); |
135 |
|
|
return 1; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
SAVE(out, nout, NULL); |
139 |
|
|
|
140 |
|
|
airMopOkay(mop); |
141 |
|
|
return 0; |
142 |
|
1 |
} |
143 |
|
|
|
144 |
|
|
UNRRDU_CMD(mlut, INFO); |