SCM Repository
Annotation of /trunk/src/dnorm/dnorm.c
Parent Directory
|
Revision Log
Revision 147 - (view) (download) (as text)
1 : | jhr | 114 | /* |
2 : | Teem: Tools to process and visualize scientific data and images | ||
3 : | Copyright (C) 2008, 2007, 2006, 2005 Gordon Kindlmann | ||
4 : | Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998 University of Utah | ||
5 : | |||
6 : | This library is free software; you can redistribute it and/or | ||
7 : | modify it under the terms of the GNU Lesser General Public License | ||
8 : | (LGPL) as published by the Free Software Foundation; either | ||
9 : | version 2.1 of the License, or (at your option) any later version. | ||
10 : | The terms of redistributing and/or modifying this software also | ||
11 : | include exceptions to the LGPL that facilitate static linking. | ||
12 : | |||
13 : | This library is distributed in the hope that it will be useful, | ||
14 : | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 : | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 : | Lesser General Public License for more details. | ||
17 : | |||
18 : | You should have received a copy of the GNU Lesser General Public License | ||
19 : | along with this library; if not, write to Free Software Foundation, Inc., | ||
20 : | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 : | */ | ||
22 : | |||
23 : | #ifdef DIDEROT | ||
24 : | # include <teem/air.h> | ||
25 : | # include <teem/biff.h> | ||
26 : | # include <teem/nrrd.h> | ||
27 : | #else | ||
28 : | # include "../nrrd.h" | ||
29 : | #endif | ||
30 : | |||
31 : | char *dnormInfo = ("Normalizes nrrd representation for Diderot. " | ||
32 : | "Forces information about kind and orientation into " | ||
33 : | "a consistent form, and nixes various other fields. "); | ||
34 : | |||
35 : | int | ||
36 : | main(int argc, char **argv) { | ||
37 : | char *me, *outS; | ||
38 : | hestOpt *hopt; | ||
39 : | hestParm *hparm; | ||
40 : | airArray *mop; | ||
41 : | |||
42 : | char *err; | ||
43 : | Nrrd *nin, *nout; | ||
44 : | NrrdIoState *nio; | ||
45 : | glk | 125 | int kindIn, kindOut, headerOnly; |
46 : | jhr | 114 | unsigned int kindAxis, axi, si, sj; |
47 : | |||
48 : | me = argv[0]; | ||
49 : | mop = airMopNew(); | ||
50 : | hparm = hestParmNew(); | ||
51 : | hopt = NULL; | ||
52 : | airMopAdd(mop, hparm, (airMopper)hestParmFree, airMopAlways); | ||
53 : | glk | 125 | hestOptAdd(&hopt, "h,header", NULL, airTypeInt, 0, 0, &headerOnly, NULL, |
54 : | "output header of nrrd file only, not the data itself"); | ||
55 : | jhr | 114 | hestOptAdd(&hopt, "i", "nin", airTypeOther, 1, 1, &nin, NULL, |
56 : | "input image", NULL, NULL, nrrdHestNrrd); | ||
57 : | hestOptAdd(&hopt, "o", "nout", airTypeString, 1, 1, &outS, "-", | ||
58 : | "output filename", NULL); | ||
59 : | |||
60 : | hestParseOrDie(hopt, argc-1, argv+1, hparm, | ||
61 : | me, dnormInfo, AIR_TRUE, AIR_TRUE, AIR_TRUE); | ||
62 : | airMopAdd(mop, hopt, (airMopper)hestOptFree, airMopAlways); | ||
63 : | airMopAdd(mop, hopt, (airMopper)hestParseFree, airMopAlways); | ||
64 : | |||
65 : | /* can't deal with block type */ | ||
66 : | if (nrrdTypeBlock == nin->type) { | ||
67 : | fprintf(stderr, "%s: can only have scalar kinds (not %s)\n", me, | ||
68 : | airEnumStr(nrrdType, nrrdTypeBlock)); | ||
69 : | airMopError(mop); exit(1); | ||
70 : | } | ||
71 : | |||
72 : | /* make sure all kinds are set to something */ | ||
73 : | /* see if there's a range kind, verify that there's only one */ | ||
74 : | kindIn = nrrdKindUnknown; | ||
75 : | kindAxis = 0; | ||
76 : | for (axi=0; axi<nin->dim; axi++) { | ||
77 : | if (nrrdKindUnknown == nin->axis[axi].kind | ||
78 : | || nrrdKindIsDomain(nin->axis[axi].kind)) { | ||
79 : | continue; | ||
80 : | } | ||
81 : | if (nrrdKindUnknown != kindIn) { | ||
82 : | fprintf(stderr, "%s: got non-domain kind %s on axis %u, but already " | ||
83 : | "have %s from axis %u\n", me, | ||
84 : | airEnumStr(nrrdKind, nin->axis[axi].kind), axi, | ||
85 : | airEnumStr(nrrdKind, kindIn), kindAxis); | ||
86 : | airMopError(mop); exit(1); | ||
87 : | } | ||
88 : | kindIn = nin->axis[axi].kind; | ||
89 : | kindAxis = axi; | ||
90 : | } | ||
91 : | /* see if the non-domain kind is something we can interpret as a tensor */ | ||
92 : | if (nrrdKindUnknown != kindIn) { | ||
93 : | switch (kindIn) { | ||
94 : | /* ======= THESE are the kinds that we can possibly output ======= */ | ||
95 : | case nrrdKind2Vector: | ||
96 : | case nrrdKind3Vector: | ||
97 : | case nrrdKind4Vector: | ||
98 : | case nrrdKind2DSymMatrix: | ||
99 : | case nrrdKind2DMatrix: | ||
100 : | case nrrdKind3DSymMatrix: | ||
101 : | case nrrdKind3DMatrix: | ||
102 : | /* =============================================================== */ | ||
103 : | kindOut = kindIn; | ||
104 : | break; | ||
105 : | case nrrdKind3Color: | ||
106 : | case nrrdKindRGBColor: | ||
107 : | kindOut = nrrdKind3Vector; | ||
108 : | break; | ||
109 : | case nrrdKind4Color: | ||
110 : | case nrrdKindRGBAColor: | ||
111 : | kindOut = nrrdKind4Vector; | ||
112 : | break; | ||
113 : | default: | ||
114 : | fprintf(stderr, "%s: got non-conforming kind %s on axis %u\n", me, | ||
115 : | airEnumStr(nrrdKind, kindIn), kindAxis); | ||
116 : | airMopError(mop); exit(1); | ||
117 : | break; | ||
118 : | } | ||
119 : | } else { | ||
120 : | kindOut = nrrdKindUnknown; | ||
121 : | } | ||
122 : | |||
123 : | glk | 147 | /* initialize output by copying */ |
124 : | jhr | 114 | nout = nrrdNew(); |
125 : | airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways); | ||
126 : | if (nrrdCopy(nout, nin)) { | ||
127 : | airMopAdd(mop, err = biffGet(NRRD), airFree, airMopAlways); | ||
128 : | fprintf(stderr, "%s: trouble copying:\n%s", me, err); | ||
129 : | airMopError(mop); exit(1); | ||
130 : | } | ||
131 : | |||
132 : | /* no comments, either advertising the format URL or anything else */ | ||
133 : | nio = nrrdIoStateNew(); | ||
134 : | airMopAdd(mop, nio, (airMopper)nrrdIoStateNix, airMopAlways); | ||
135 : | nio->skipFormatURL = AIR_TRUE; | ||
136 : | glk | 125 | if (headerOnly) { |
137 : | nio->skipData = AIR_TRUE; | ||
138 : | } | ||
139 : | jhr | 114 | nrrdCommentClear(nout); |
140 : | |||
141 : | /* no measurement frame */ | ||
142 : | for (si=0; si<NRRD_SPACE_DIM_MAX; si++) { | ||
143 : | for (sj=0; sj<NRRD_SPACE_DIM_MAX; sj++) { | ||
144 : | nout->measurementFrame[si][sj] = AIR_NAN; | ||
145 : | } | ||
146 : | } | ||
147 : | |||
148 : | /* no key/value pairs */ | ||
149 : | nrrdKeyValueClear(nout); | ||
150 : | |||
151 : | /* no content field */ | ||
152 : | nout->content = airFree(nout->content); | ||
153 : | |||
154 : | /* normalize domain kinds to "space" */ | ||
155 : | /* turn off centers (perhaps Diderot should assume cell-centered) */ | ||
156 : | /* turn off thickness */ | ||
157 : | /* turn off labels and units */ | ||
158 : | for (axi=0; axi<nout->dim; axi++) { | ||
159 : | if (nrrdKindUnknown == kindOut) { | ||
160 : | nout->axis[axi].kind = nrrdKindSpace; | ||
161 : | } else { | ||
162 : | nout->axis[axi].kind = (kindAxis == axi | ||
163 : | ? kindOut | ||
164 : | : nrrdKindSpace); | ||
165 : | } | ||
166 : | nout->axis[axi].center = nrrdCenterUnknown; | ||
167 : | nout->axis[axi].thickness = AIR_NAN; | ||
168 : | nout->axis[axi].label = airFree(nout->axis[axi].label); | ||
169 : | nout->axis[axi].units = airFree(nout->axis[axi].units); | ||
170 : | nout->axis[axi].min = AIR_NAN; | ||
171 : | nout->axis[axi].max = AIR_NAN; | ||
172 : | nout->axis[axi].spacing = AIR_NAN; | ||
173 : | } | ||
174 : | |||
175 : | /* logic of orientation definition: | ||
176 : | if space dimension is known: | ||
177 : | set origin to zero if not already set | ||
178 : | set space direction to unit vector if not already set | ||
179 : | else: | ||
180 : | set origin to zero and all space directions to units | ||
181 : | might be nice to use gage's logic for mapping from world to index, | ||
182 : | but we have to accept a greater variety of kinds and dimensions | ||
183 : | than gage ever has to process. | ||
184 : | */ | ||
185 : | if (nout->spaceDim) { | ||
186 : | int saxi = 0; | ||
187 : | glk | 147 | /* we use only the space dimension, not any named space */ |
188 : | nout->space = nrrdSpaceUnknown; | ||
189 : | jhr | 114 | if (!nrrdSpaceVecExists(nout->spaceDim, nout->spaceOrigin)) { |
190 : | nrrdSpaceVecSetZero(nout->spaceOrigin); | ||
191 : | } | ||
192 : | for (axi=0; axi<nout->dim; axi++) { | ||
193 : | if (nrrdKindUnknown == kindOut || kindAxis != axi) { | ||
194 : | if (!nrrdSpaceVecExists(nout->spaceDim, | ||
195 : | nout->axis[axi].spaceDirection)) { | ||
196 : | nrrdSpaceVecSetZero(nout->axis[axi].spaceDirection); | ||
197 : | nout->axis[axi].spaceDirection[saxi] = 1.0; | ||
198 : | } | ||
199 : | saxi++; | ||
200 : | } else { | ||
201 : | nrrdSpaceVecSetNaN(nout->axis[axi].spaceDirection); | ||
202 : | } | ||
203 : | } | ||
204 : | } else { | ||
205 : | int saxi = 0; | ||
206 : | nrrdSpaceVecSetZero(nout->spaceOrigin); | ||
207 : | for (axi=0; axi<nout->dim; axi++) { | ||
208 : | if (nrrdKindUnknown == kindOut || kindAxis != axi) { | ||
209 : | nrrdSpaceVecSetZero(nout->axis[axi].spaceDirection); | ||
210 : | glk | 125 | nout->axis[axi].spaceDirection[saxi] |
211 : | = (AIR_EXISTS(nin->axis[axi].spacing) | ||
212 : | ? nin->axis[axi].spacing | ||
213 : | : 1.0); | ||
214 : | jhr | 114 | saxi++; |
215 : | } else { | ||
216 : | nrrdSpaceVecSetNaN(nout->axis[axi].spaceDirection); | ||
217 : | } | ||
218 : | } | ||
219 : | nout->spaceDim = saxi; | ||
220 : | } | ||
221 : | /* probably should be asserted earlier */ | ||
222 : | if (nout->dim != nout->spaceDim + !!kindOut) { | ||
223 : | fprintf(stderr, "%s: output dim %d != spaceDim %d + %d %s%s%s\n", | ||
224 : | me, nout->dim, nout->spaceDim, !!kindOut, | ||
225 : | kindOut ? "for non-scalar (" : "(scalar data)", | ||
226 : | kindOut ? airEnumStr(nrrdKind, kindOut) : "", | ||
227 : | kindOut ? ") data" : ""); | ||
228 : | airMopError(mop); exit(1); | ||
229 : | } | ||
230 : | |||
231 : | if (nrrdSave(outS, nout, nio)) { | ||
232 : | airMopAdd(mop, err = biffGet(NRRD), airFree, airMopAlways); | ||
233 : | fprintf(stderr, "%s: trouble saving \"%s\":\n%s", | ||
234 : | me, outS, err); | ||
235 : | airMopError(mop); exit(1); | ||
236 : | } | ||
237 : | |||
238 : | airMopOkay(mop); | ||
239 : | exit(0); | ||
240 : | } |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |