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 "nrrd.h" |
25 |
|
|
#include "privateNrrd.h" |
26 |
|
|
|
27 |
|
|
NrrdIter * |
28 |
|
|
nrrdIterNew() { |
29 |
|
|
NrrdIter *iter; |
30 |
|
|
|
31 |
✓✗ |
64 |
if ( (iter = (NrrdIter *)calloc(1, sizeof(NrrdIter))) ) { |
32 |
|
32 |
iter->nrrd = NULL; |
33 |
|
32 |
iter->ownNrrd = NULL; |
34 |
|
32 |
iter->val = AIR_NAN; |
35 |
|
32 |
iter->size = 0; |
36 |
|
32 |
iter->data = NULL; |
37 |
|
32 |
iter->left = 0; |
38 |
|
32 |
iter->load = NULL; |
39 |
|
32 |
} |
40 |
|
32 |
return iter; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
void |
44 |
|
|
nrrdIterSetValue(NrrdIter *iter, double val) { |
45 |
|
|
|
46 |
✓✗ |
32 |
if (iter) { |
47 |
|
16 |
iter->nrrd = NULL; |
48 |
✗✓ |
32 |
iter->ownNrrd = iter->ownNrrd ? nrrdNuke(iter->ownNrrd) : NULL; |
49 |
|
16 |
iter->val = val; |
50 |
|
16 |
iter->size = nrrdTypeSize[nrrdTypeDouble]; |
51 |
|
16 |
iter->data = (char*)&(iter->val); |
52 |
|
16 |
iter->left = 0; |
53 |
|
16 |
iter->load = nrrdDLoad[nrrdTypeDouble]; |
54 |
|
16 |
} |
55 |
|
16 |
return; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
void |
59 |
|
|
nrrdIterSetNrrd(NrrdIter *iter, const Nrrd *nrrd) { |
60 |
|
|
|
61 |
✓✗✓✗
|
48 |
if (iter && nrrd && nrrd->data) { |
62 |
✗✓ |
16 |
if (nrrdTypeBlock == nrrd->type) { |
63 |
|
|
/* we can't deal */ |
64 |
|
|
nrrdIterSetValue(iter, AIR_NAN); |
65 |
|
|
return; |
66 |
|
|
} |
67 |
|
16 |
iter->nrrd = nrrd; |
68 |
✗✓ |
32 |
iter->ownNrrd = iter->ownNrrd ? nrrdNuke(iter->ownNrrd) : NULL; |
69 |
|
16 |
iter->val = AIR_NAN; |
70 |
|
16 |
iter->size = nrrdTypeSize[nrrd->type]; |
71 |
|
16 |
iter->data = (char *)nrrd->data; |
72 |
|
16 |
iter->left = nrrdElementNumber(nrrd)-1; |
73 |
|
16 |
iter->load = nrrdDLoad[nrrd->type]; |
74 |
|
16 |
} |
75 |
|
|
return; |
76 |
|
16 |
} |
77 |
|
|
|
78 |
|
|
/* |
79 |
|
|
** formerly known as nrrdIterSetNrrd |
80 |
|
|
*/ |
81 |
|
|
void |
82 |
|
|
nrrdIterSetOwnNrrd(NrrdIter *iter, Nrrd *nrrd) { |
83 |
|
|
|
84 |
|
|
if (iter && nrrd && nrrd->data) { |
85 |
|
|
if (nrrdTypeBlock == nrrd->type) { |
86 |
|
|
/* we can't deal */ |
87 |
|
|
nrrdIterSetValue(iter, AIR_NAN); |
88 |
|
|
return; |
89 |
|
|
} |
90 |
|
|
iter->nrrd = NULL; |
91 |
|
|
iter->ownNrrd = iter->ownNrrd ? nrrdNuke(iter->ownNrrd) : NULL; |
92 |
|
|
iter->ownNrrd = nrrd; |
93 |
|
|
iter->val = AIR_NAN; |
94 |
|
|
iter->size = nrrdTypeSize[nrrd->type]; |
95 |
|
|
iter->data = (char *)nrrd->data; |
96 |
|
|
iter->left = nrrdElementNumber(nrrd)-1; |
97 |
|
|
iter->load = nrrdDLoad[nrrd->type]; |
98 |
|
|
} |
99 |
|
|
return; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
double |
103 |
|
|
nrrdIterValue(NrrdIter *iter) { |
104 |
|
|
double ret = 0.0; |
105 |
|
|
|
106 |
✓✗ |
24906240 |
if (iter) { |
107 |
|
12453120 |
ret = iter->load(iter->data); |
108 |
✓✓✗✓
|
18679680 |
if (iter->nrrd || iter->ownNrrd) { |
109 |
|
6226560 |
iter->data += iter->size; |
110 |
✓✓ |
6226560 |
if (iter->left) { |
111 |
|
6226544 |
iter->left -= 1; |
112 |
|
6226544 |
} else { |
113 |
✓✗ |
48 |
iter->data = (char *)(_NRRD_ITER_NRRD(iter)->data); |
114 |
✓✗ |
48 |
iter->left = nrrdElementNumber(_NRRD_ITER_NRRD(iter))-1; |
115 |
|
|
} |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
12453120 |
return ret; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
/* |
122 |
|
|
******** nrrdIterContent() |
123 |
|
|
** |
124 |
|
|
** ALLOCATES a string that is either the nrrd's content (or |
125 |
|
|
** nrrdStateUnknownContent) or a string version of the value; useful |
126 |
|
|
** for when you's use the "content" of a nrrd |
127 |
|
|
*/ |
128 |
|
|
char * |
129 |
|
|
nrrdIterContent(NrrdIter *iter) { |
130 |
|
64 |
char *ret, buff[AIR_STRLEN_SMALL]; |
131 |
|
|
|
132 |
|
|
ret = NULL; |
133 |
✓✗ |
32 |
if (iter) { |
134 |
✓✓✓✗ ✗✓ |
64 |
if (_NRRD_ITER_NRRD(iter)) { |
135 |
✓✗ |
48 |
ret = _nrrdContentGet(_NRRD_ITER_NRRD(iter)); |
136 |
|
16 |
} else { |
137 |
|
16 |
airSinglePrintf(NULL, buff, "%g", iter->val); |
138 |
|
16 |
ret = airStrdup(buff); |
139 |
|
|
} |
140 |
|
|
} |
141 |
|
32 |
return ret; |
142 |
|
32 |
} |
143 |
|
|
|
144 |
|
|
NrrdIter * |
145 |
|
|
nrrdIterNix(NrrdIter *iter) { |
146 |
|
|
|
147 |
✓✗ |
64 |
if (iter) { |
148 |
✗✓ |
32 |
if (iter->ownNrrd) { |
149 |
|
|
iter->ownNrrd = nrrdNuke(iter->ownNrrd); |
150 |
|
|
} |
151 |
|
32 |
free(iter); |
152 |
|
32 |
} |
153 |
|
32 |
return NULL; |
154 |
|
|
} |
155 |
|
|
|