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 "Save all slices along one axis into separate files" |
28 |
|
|
static const char *_unrrdu_diceInfoL = |
29 |
|
|
(INFO |
30 |
|
|
". Calls \"unu slice\" for each position " |
31 |
|
|
"along the indicated axis, and saves out a different " |
32 |
|
|
"file for each sample along that axis.\n " |
33 |
|
|
"* Uses repeated calls to nrrdSlice and nrrdSave"); |
34 |
|
|
|
35 |
|
|
int |
36 |
|
|
unrrdu_diceMain(int argc, const char **argv, const char *me, |
37 |
|
|
hestParm *hparm) { |
38 |
|
2 |
hestOpt *opt = NULL; |
39 |
|
1 |
char *base, *err, fnout[AIR_STRLEN_MED], /* file name out */ |
40 |
|
|
fffname[AIR_STRLEN_MED], /* format for filename */ |
41 |
|
|
*ftmpl; /* format template */ |
42 |
|
1 |
Nrrd *nin, *nout; |
43 |
|
|
int pret, fit; |
44 |
|
1 |
unsigned int axis, start, pos, top, size, sanity; |
45 |
|
|
airArray *mop; |
46 |
|
|
|
47 |
|
1 |
OPT_ADD_AXIS(axis, "axis to slice along"); |
48 |
|
1 |
OPT_ADD_NIN(nin, "input nrrd"); |
49 |
|
1 |
hestOptAdd(&opt, "s,start", "start", airTypeUInt, 1, 1, &start, "0", |
50 |
|
|
"integer value to start numbering with"); |
51 |
|
1 |
hestOptAdd(&opt, "ff,format", "form", airTypeString, 1, 1, &ftmpl, "", |
52 |
|
|
"a printf-style format to use for generating all " |
53 |
|
|
"filenames. Use this to override the number of characters " |
54 |
|
|
"used to represent the slice position, or the file format " |
55 |
|
|
"of the output, e.g. \"-ff %03d.ppm\" for 000.ppm, " |
56 |
|
|
"001.ppm, etc. By default (not using this option), slices " |
57 |
|
|
"are saved in NRRD format (or PNM or PNG where possible) " |
58 |
|
|
"with shortest possible filenames."); |
59 |
|
|
/* the fact that we're using unsigned int instead of size_t is |
60 |
|
|
its own kind of sanity check */ |
61 |
|
1 |
hestOptAdd(&opt, "l,limit", "max#", airTypeUInt, 1, 1, &sanity, "9999", |
62 |
|
|
"a sanity check on how many slice files should be saved " |
63 |
|
|
"out, to prevent accidentally dicing the wrong axis " |
64 |
|
|
"or the wrong array. Can raise this value if needed."); |
65 |
|
1 |
hestOptAdd(&opt, "o,output", "prefix", airTypeString, 1, 1, &base, NULL, |
66 |
|
|
"output filename prefix (excluding info set via \"-ff\"), " |
67 |
|
|
"basically to set path of output files (so be sure to end " |
68 |
|
|
"with \"/\"."); |
69 |
|
|
|
70 |
|
1 |
mop = airMopNew(); |
71 |
|
1 |
airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways); |
72 |
|
|
|
73 |
✓✗ |
2 |
USAGE(_unrrdu_diceInfoL); |
74 |
|
|
PARSE(); |
75 |
|
|
airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways); |
76 |
|
|
|
77 |
|
|
if (!( axis < nin->dim )) { |
78 |
|
|
fprintf(stderr, "%s: given axis (%u) outside range [0,%u]\n", |
79 |
|
|
me, axis, nin->dim-1); |
80 |
|
|
airMopError(mop); |
81 |
|
|
return 1; |
82 |
|
|
} |
83 |
|
|
if (nin->axis[axis].size > sanity) { |
84 |
|
|
char stmp[AIR_STRLEN_SMALL]; |
85 |
|
|
fprintf(stderr, "%s: axis %u size %s > sanity limit %u; " |
86 |
|
|
"increase via \"-l\"\n", me, |
87 |
|
|
axis, airSprintSize_t(stmp, nin->axis[axis].size), sanity); |
88 |
|
|
airMopError(mop); |
89 |
|
|
return 1; |
90 |
|
|
} |
91 |
|
|
size = AIR_UINT(nin->axis[axis].size); |
92 |
|
|
|
93 |
|
|
/* HEY: this should use nrrdSaveMulti(), and if there's additional |
94 |
|
|
smarts here, they should be moved into nrrdSaveMulti() */ |
95 |
|
|
if (airStrlen(ftmpl)) { |
96 |
|
|
if (!( _nrrdContainsPercentThisAndMore(ftmpl, 'd') |
97 |
|
|
|| _nrrdContainsPercentThisAndMore(ftmpl, 'u') )) { |
98 |
|
|
fprintf(stderr, "%s: given filename format \"%s\" doesn't seem to " |
99 |
|
|
"have the converstion specification to print an integer\n", |
100 |
|
|
me, ftmpl); |
101 |
|
|
airMopError(mop); |
102 |
|
|
return 1; |
103 |
|
|
} |
104 |
|
|
sprintf(fffname, "%%s%s", ftmpl); |
105 |
|
|
} else { |
106 |
|
|
unsigned int dignum=0, tmps; |
107 |
|
|
tmps = top = start + size - 1; |
108 |
|
|
do { |
109 |
|
|
dignum++; |
110 |
|
|
tmps /= 10; |
111 |
|
|
} while (tmps); |
112 |
|
|
/* sprintf the number of digits into the string that will be used |
113 |
|
|
to sprintf the slice number into the filename */ |
114 |
|
|
sprintf(fffname, "%%s%%0%uu.nrrd", dignum); |
115 |
|
|
} |
116 |
|
|
nout = nrrdNew(); |
117 |
|
|
airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways); |
118 |
|
|
|
119 |
|
|
for (pos=0; pos<size; pos++) { |
120 |
|
|
if (nrrdSlice(nout, nin, axis, pos)) { |
121 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
122 |
|
|
fprintf(stderr, "%s: error slicing nrrd:%s\n", me, err); |
123 |
|
|
airMopError(mop); |
124 |
|
|
return 1; |
125 |
|
|
} |
126 |
|
|
if (0 == pos && !airStrlen(ftmpl)) { |
127 |
|
|
/* See if these slices would be better saved as PNG or PNM images. |
128 |
|
|
Altering the file name will tell nrrdSave() to use a different |
129 |
|
|
file format. We wait till now to check this so that we can |
130 |
|
|
work from the actual slice */ |
131 |
|
|
if (nrrdFormatPNG->fitsInto(nout, nrrdEncodingRaw, AIR_FALSE)) { |
132 |
|
|
strcpy(fffname + strlen(fffname) - 4, "png"); |
133 |
|
|
} else { |
134 |
|
|
fit = nrrdFormatPNM->fitsInto(nout, nrrdEncodingRaw, AIR_FALSE); |
135 |
|
|
if (2 == fit) { |
136 |
|
|
strcpy(fffname + strlen(fffname) - 4, "pgm"); |
137 |
|
|
} else if (3 == fit) { |
138 |
|
|
strcpy(fffname + strlen(fffname) - 4, "ppm"); |
139 |
|
|
} |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
|
sprintf(fnout, fffname, base, pos+start); |
143 |
|
|
if (nrrdStateVerboseIO > 0) { |
144 |
|
|
fprintf(stderr, "%s: %s ...\n", me, fnout); |
145 |
|
|
} |
146 |
|
|
if (nrrdSave(fnout, nout, NULL)) { |
147 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
148 |
|
|
fprintf(stderr, "%s: error writing nrrd to \"%s\":%s\n", |
149 |
|
|
me, fnout, err); |
150 |
|
|
airMopError(mop); |
151 |
|
|
return 1; |
152 |
|
|
} |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
airMopOkay(mop); |
156 |
|
|
return 0; |
157 |
|
1 |
} |
158 |
|
|
|
159 |
|
|
UNRRDU_CMD(dice, INFO); |