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 "Quantize values to 8, 16, or 32 bits" |
28 |
|
|
static const char *_unrrdu_quantizeInfoL = |
29 |
|
|
(INFO ". Input values can be fixed point (e.g. quantizing ushorts down to " |
30 |
|
|
"uchars) or floating point. Values are clamped to the min and max before " |
31 |
|
|
"they are quantized, so there is no risk of getting 255 where you expect 0 " |
32 |
|
|
"(with unsigned char output, for example). The min and max can be specified " |
33 |
|
|
"explicitly (as a regular number), or in terms of percentiles (a number " |
34 |
|
|
"suffixed with \"" NRRD_MINMAX_PERC_SUFF "\", no space in between). " |
35 |
|
|
"This does only linear quantization. " |
36 |
|
|
"See also \"unu convert\", \"unu 2op x\", " |
37 |
|
|
"and \"unu 3op clamp\".\n " |
38 |
|
|
"* Uses nrrdQuantize"); |
39 |
|
|
|
40 |
|
|
int |
41 |
|
|
unrrdu_quantizeMain(int argc, const char **argv, const char *me, |
42 |
|
|
hestParm *hparm) { |
43 |
|
2 |
hestOpt *opt = NULL; |
44 |
|
1 |
char *out, *err; |
45 |
|
1 |
Nrrd *nin, *nout; |
46 |
|
1 |
char *minStr, *maxStr; |
47 |
|
1 |
int pret, blind8BitRange; |
48 |
|
1 |
unsigned int bits, hbins; |
49 |
|
1 |
double gamma; |
50 |
|
|
NrrdRange *range; |
51 |
|
|
airArray *mop; |
52 |
|
|
|
53 |
|
1 |
hestOptAdd(&opt, "b,bits", "bits", airTypeOther, 1, 1, &bits, NULL, |
54 |
|
|
"Number of bits to quantize down to; determines the type " |
55 |
|
|
"of the output nrrd:\n " |
56 |
|
|
"\b\bo \"8\": unsigned char\n " |
57 |
|
|
"\b\bo \"16\": unsigned short\n " |
58 |
|
|
"\b\bo \"32\": unsigned int", |
59 |
|
|
NULL, NULL, &unrrduHestBitsCB); |
60 |
|
1 |
hestOptAdd(&opt, "min,minimum", "value", airTypeString, 1, 1, |
61 |
|
|
&minStr, "nan", |
62 |
|
|
"The value to map to zero, given explicitly as a regular number, " |
63 |
|
|
"*or*, if the number is given with a \"" NRRD_MINMAX_PERC_SUFF |
64 |
|
|
"\" suffix, this " |
65 |
|
|
"minimum is specified in terms of the percentage of samples in " |
66 |
|
|
"input that are lower. " |
67 |
|
|
"\"0" NRRD_MINMAX_PERC_SUFF "\" means the " |
68 |
|
|
"lowest input value is used, " |
69 |
|
|
"\"1" NRRD_MINMAX_PERC_SUFF "\" means that the " |
70 |
|
|
"1% of the lowest values are all mapped to zero. " |
71 |
|
|
"By default (not using this option), the lowest input value is " |
72 |
|
|
"used."); |
73 |
|
1 |
hestOptAdd(&opt, "max,maximum", "value", airTypeString, 1, 1, |
74 |
|
|
&maxStr, "nan", |
75 |
|
|
"The value to map to the highest unsigned integral value, given " |
76 |
|
|
"explicitly as a regular number, " |
77 |
|
|
"*or*, if the number is given with " |
78 |
|
|
"a \"" NRRD_MINMAX_PERC_SUFF "\" suffix, " |
79 |
|
|
"this maximum is specified " |
80 |
|
|
"in terms of the percentage of samples in input that are higher. " |
81 |
|
|
"\"0" NRRD_MINMAX_PERC_SUFF "\" means the highest input value is " |
82 |
|
|
"used, which is also the default " |
83 |
|
|
"behavior (same as not using this option)."); |
84 |
|
1 |
hestOptAdd(&opt, "g,gamma", "gamma", airTypeDouble, 1, 1, &gamma, "1.0", |
85 |
|
|
"gamma > 1.0 brightens; gamma < 1.0 darkens. " |
86 |
|
|
"Negative gammas invert values. "); |
87 |
|
1 |
hestOptAdd(&opt, "hb,bins", "bins", airTypeUInt, 1, 1, &hbins, "5000", |
88 |
|
|
"number of bins in histogram of values, for determining min " |
89 |
|
|
"or max by percentiles. This has to be large enough so that " |
90 |
|
|
"any errant very high or very low values do not compress the " |
91 |
|
|
"interesting part of the histogram to an inscrutably small " |
92 |
|
|
"number of bins."); |
93 |
|
1 |
hestOptAdd(&opt, "blind8", "bool", airTypeBool, 1, 1, &blind8BitRange, |
94 |
|
1 |
nrrdStateBlind8BitRange ? "true" : "false", |
95 |
|
|
"if not using \"-min\" or \"-max\", whether to know " |
96 |
|
|
"the range of 8-bit data blindly (uchar is always [0,255], " |
97 |
|
|
"signed char is [-128,127])"); |
98 |
|
1 |
OPT_ADD_NIN(nin, "input nrrd"); |
99 |
|
1 |
OPT_ADD_NOUT(out, "output nrrd"); |
100 |
|
|
|
101 |
|
1 |
mop = airMopNew(); |
102 |
|
1 |
airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways); |
103 |
|
|
|
104 |
✓✗ |
2 |
USAGE(_unrrdu_quantizeInfoL); |
105 |
|
|
PARSE(); |
106 |
|
|
airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways); |
107 |
|
|
|
108 |
|
|
range = nrrdRangeNew(AIR_NAN, AIR_NAN); |
109 |
|
|
airMopAdd(mop, range, (airMopper)nrrdRangeNix, airMopAlways); |
110 |
|
|
nout = nrrdNew(); |
111 |
|
|
airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways); |
112 |
|
|
if (nrrdRangePercentileFromStringSet(range, nin, minStr, maxStr, |
113 |
|
|
hbins, blind8BitRange) |
114 |
|
|
|| (1 == gamma ? 0 |
115 |
|
|
: nrrdArithGamma(nin, nin, range, gamma)) |
116 |
|
|
|| nrrdQuantize(nout, nin, range, bits)) { |
117 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
118 |
|
|
fprintf(stderr, "%s: error with range%s quantizing:\n%s", me, |
119 |
|
|
(1 == gamma ? " or" : ", gamma, or"), err); |
120 |
|
|
airMopError(mop); |
121 |
|
|
return 1; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
SAVE(out, nout, NULL); |
125 |
|
|
|
126 |
|
|
airMopOkay(mop); |
127 |
|
|
return 0; |
128 |
|
1 |
} |
129 |
|
|
|
130 |
|
|
UNRRDU_CMD(quantize, INFO); |