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

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