Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/dnorm/dnorm.c
ViewVC logotype

Annotation of /branches/pure-cfg/src/dnorm/dnorm.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1029 - (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 1029 int kindIn, kindOut, headerOnly, haveMM;
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 : glk 1029 /* set haveMM */
75 :     haveMM = AIR_TRUE;
76 : jhr 114 kindIn = nrrdKindUnknown;
77 :     kindAxis = 0;
78 :     for (axi=0; axi<nin->dim; axi++) {
79 :     if (nrrdKindUnknown == nin->axis[axi].kind
80 :     || nrrdKindIsDomain(nin->axis[axi].kind)) {
81 : glk 1029 haveMM &= AIR_EXISTS(nin->axis[axi].min);
82 :     haveMM &= AIR_EXISTS(nin->axis[axi].max);
83 :     } else {
84 :     if (nrrdKindUnknown != kindIn) {
85 :     fprintf(stderr, "%s: got non-domain kind %s on axis %u, but already "
86 :     "have %s from axis %u\n", me,
87 :     airEnumStr(nrrdKind, nin->axis[axi].kind), axi,
88 :     airEnumStr(nrrdKind, kindIn), kindAxis);
89 :     airMopError(mop); exit(1);
90 :     }
91 :     kindIn = nin->axis[axi].kind;
92 :     kindAxis = axi;
93 : jhr 114 }
94 :     }
95 :     /* see if the non-domain kind is something we can interpret as a tensor */
96 :     if (nrrdKindUnknown != kindIn) {
97 :     switch (kindIn) {
98 :     /* ======= THESE are the kinds that we can possibly output ======= */
99 :     case nrrdKind2Vector:
100 :     case nrrdKind3Vector:
101 :     case nrrdKind4Vector:
102 :     case nrrdKind2DSymMatrix:
103 :     case nrrdKind2DMatrix:
104 :     case nrrdKind3DSymMatrix:
105 :     case nrrdKind3DMatrix:
106 :     /* =============================================================== */
107 :     kindOut = kindIn;
108 :     break;
109 :     case nrrdKind3Color:
110 :     case nrrdKindRGBColor:
111 :     kindOut = nrrdKind3Vector;
112 :     break;
113 :     case nrrdKind4Color:
114 :     case nrrdKindRGBAColor:
115 :     kindOut = nrrdKind4Vector;
116 :     break;
117 :     default:
118 :     fprintf(stderr, "%s: got non-conforming kind %s on axis %u\n", me,
119 :     airEnumStr(nrrdKind, kindIn), kindAxis);
120 :     airMopError(mop); exit(1);
121 :     break;
122 :     }
123 :     } else {
124 :     kindOut = nrrdKindUnknown;
125 :     }
126 :    
127 : glk 147 /* initialize output by copying */
128 : jhr 114 nout = nrrdNew();
129 :     airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
130 :     if (nrrdCopy(nout, nin)) {
131 :     airMopAdd(mop, err = biffGet(NRRD), airFree, airMopAlways);
132 :     fprintf(stderr, "%s: trouble copying:\n%s", me, err);
133 :     airMopError(mop); exit(1);
134 :     }
135 :    
136 :     /* no comments, either advertising the format URL or anything else */
137 :     nio = nrrdIoStateNew();
138 :     airMopAdd(mop, nio, (airMopper)nrrdIoStateNix, airMopAlways);
139 :     nio->skipFormatURL = AIR_TRUE;
140 : glk 125 if (headerOnly) {
141 :     nio->skipData = AIR_TRUE;
142 :     }
143 : jhr 114 nrrdCommentClear(nout);
144 :    
145 :     /* no measurement frame */
146 :     for (si=0; si<NRRD_SPACE_DIM_MAX; si++) {
147 :     for (sj=0; sj<NRRD_SPACE_DIM_MAX; sj++) {
148 :     nout->measurementFrame[si][sj] = AIR_NAN;
149 :     }
150 :     }
151 :    
152 :     /* no key/value pairs */
153 :     nrrdKeyValueClear(nout);
154 :    
155 :     /* no content field */
156 :     nout->content = airFree(nout->content);
157 :    
158 :     /* normalize domain kinds to "space" */
159 :     /* turn off centers (perhaps Diderot should assume cell-centered) */
160 :     /* turn off thickness */
161 :     /* turn off labels and units */
162 :     for (axi=0; axi<nout->dim; axi++) {
163 :     if (nrrdKindUnknown == kindOut) {
164 :     nout->axis[axi].kind = nrrdKindSpace;
165 :     } else {
166 :     nout->axis[axi].kind = (kindAxis == axi
167 :     ? kindOut
168 :     : nrrdKindSpace);
169 :     }
170 :     nout->axis[axi].center = nrrdCenterUnknown;
171 :     nout->axis[axi].thickness = AIR_NAN;
172 :     nout->axis[axi].label = airFree(nout->axis[axi].label);
173 :     nout->axis[axi].units = airFree(nout->axis[axi].units);
174 :     nout->axis[axi].min = AIR_NAN;
175 :     nout->axis[axi].max = AIR_NAN;
176 :     nout->axis[axi].spacing = AIR_NAN;
177 :     }
178 :    
179 :     /* logic of orientation definition:
180 :     if space dimension is known:
181 :     set origin to zero if not already set
182 :     set space direction to unit vector if not already set
183 :     else:
184 :     set origin to zero and all space directions to units
185 :     might be nice to use gage's logic for mapping from world to index,
186 :     but we have to accept a greater variety of kinds and dimensions
187 :     than gage ever has to process.
188 :     */
189 :     if (nout->spaceDim) {
190 :     int saxi = 0;
191 : glk 147 /* we use only the space dimension, not any named space */
192 :     nout->space = nrrdSpaceUnknown;
193 : jhr 114 if (!nrrdSpaceVecExists(nout->spaceDim, nout->spaceOrigin)) {
194 :     nrrdSpaceVecSetZero(nout->spaceOrigin);
195 :     }
196 :     for (axi=0; axi<nout->dim; axi++) {
197 :     if (nrrdKindUnknown == kindOut || kindAxis != axi) {
198 :     if (!nrrdSpaceVecExists(nout->spaceDim,
199 :     nout->axis[axi].spaceDirection)) {
200 :     nrrdSpaceVecSetZero(nout->axis[axi].spaceDirection);
201 :     nout->axis[axi].spaceDirection[saxi] = 1.0;
202 :     }
203 :     saxi++;
204 :     } else {
205 :     nrrdSpaceVecSetNaN(nout->axis[axi].spaceDirection);
206 :     }
207 :     }
208 : glk 1029 } else if (haveMM) {
209 :     int saxi = 0;
210 :     for (axi=0; axi<nout->dim; axi++) {
211 :     if (nrrdKindUnknown == kindOut || kindAxis != axi) {
212 :     nrrdSpaceVecSetZero(nout->axis[axi].spaceDirection);
213 :     nout->axis[axi].spaceDirection[saxi]
214 :     = (nin->axis[axi].max - nin->axis[axi].min)/(nin->axis[axi].size-1);
215 :     nout->spaceOrigin[saxi] = nin->axis[axi].min;
216 :     saxi++;
217 :     } else {
218 :     nrrdSpaceVecSetNaN(nout->axis[axi].spaceDirection);
219 :     }
220 :     }
221 :     nout->spaceDim = saxi;
222 : jhr 114 } else {
223 :     int saxi = 0;
224 :     nrrdSpaceVecSetZero(nout->spaceOrigin);
225 :     for (axi=0; axi<nout->dim; axi++) {
226 :     if (nrrdKindUnknown == kindOut || kindAxis != axi) {
227 :     nrrdSpaceVecSetZero(nout->axis[axi].spaceDirection);
228 : glk 125 nout->axis[axi].spaceDirection[saxi]
229 :     = (AIR_EXISTS(nin->axis[axi].spacing)
230 :     ? nin->axis[axi].spacing
231 :     : 1.0);
232 : jhr 114 saxi++;
233 :     } else {
234 :     nrrdSpaceVecSetNaN(nout->axis[axi].spaceDirection);
235 :     }
236 :     }
237 :     nout->spaceDim = saxi;
238 :     }
239 : glk 1029
240 :     /* space dimension has to match the number of domain axes */
241 : jhr 114 if (nout->dim != nout->spaceDim + !!kindOut) {
242 :     fprintf(stderr, "%s: output dim %d != spaceDim %d + %d %s%s%s\n",
243 :     me, nout->dim, nout->spaceDim, !!kindOut,
244 :     kindOut ? "for non-scalar (" : "(scalar data)",
245 :     kindOut ? airEnumStr(nrrdKind, kindOut) : "",
246 :     kindOut ? ") data" : "");
247 :     airMopError(mop); exit(1);
248 :     }
249 :    
250 :     if (nrrdSave(outS, nout, nio)) {
251 :     airMopAdd(mop, err = biffGet(NRRD), airFree, airMopAlways);
252 :     fprintf(stderr, "%s: trouble saving \"%s\":\n%s",
253 :     me, outS, err);
254 :     airMopError(mop); exit(1);
255 :     }
256 :    
257 :     airMopOkay(mop);
258 :     exit(0);
259 :     }

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0