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

SCM Repository

[diderot] Annotation of /branches/lamont/src/dnorm/dnorm.c
ViewVC logotype

Annotation of /branches/lamont/src/dnorm/dnorm.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1685 - (view) (download) (as text)
Original Path: branches/vis12/src/dnorm/dnorm.c

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

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