1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/*
NrrdIO: stand-alone code for basic nrrd functionality
Copyright (C) 2008, 2007, 2006, 2005 Gordon Kindlmann
Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998 University of Utah
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "NrrdIO.h"
#include "privateNrrd.h"
/*
******** nrrdSlice()
**
** slices a nrrd along a given axis, at a given position.
**
** This is a newer version of the procedure, which is simpler, faster,
** and requires less memory overhead than the first one. It is based
** on the observation that any slice is a periodic square-wave pattern
** in the original data (viewed as a one- dimensional array). The
** characteristics of that periodic pattern are how far from the
** beginning it starts (offset), the length of the "on" part (length),
** the period (period), and the number of periods (numper).
*/
int
nrrdSlice(Nrrd *nout, const Nrrd *nin, unsigned int saxi, size_t pos) {
static const char me[]="nrrdSlice", func[]="slice";
size_t
I,
rowLen, /* length of segment */
colStep, /* distance between start of each segment */
colLen, /* number of periods */
szOut[NRRD_DIM_MAX];
unsigned int ai, outdim;
int map[NRRD_DIM_MAX];
char *src, *dest;
if (!(nin && nout)) {
biffAddf(NRRD, "%s: got NULL pointer", me);
return 1;
}
if (nout == nin) {
biffAddf(NRRD, "%s: nout==nin disallowed", me);
return 1;
}
if (1 == nin->dim) {
biffAddf(NRRD, "%s: can't slice a 1-D nrrd; use nrrd{I,F,D}Lookup[]",
me);
return 1;
}
if (!( saxi < nin->dim )) {
biffAddf(NRRD, "%s: slice axis %d out of bounds (0 to %d)",
me, saxi, nin->dim-1);
return 1;
}
if (!( pos < nin->axis[saxi].size )) {
biffAddf(NRRD, "%s: position " _AIR_SIZE_T_CNV
" out of bounds (0 to " _AIR_SIZE_T_CNV ")",
me, pos, nin->axis[saxi].size-1);
return 1;
}
/* this shouldn't actually be necessary .. */
if (!nrrdElementSize(nin)) {
biffAddf(NRRD, "%s: nrrd reports zero element size!", me);
return 1;
}
/* set up control variables */
rowLen = colLen = 1;
for (ai=0; ai<nin->dim; ai++) {
if (ai < saxi) {
rowLen *= nin->axis[ai].size;
} else if (ai > saxi) {
colLen *= nin->axis[ai].size;
}
}
rowLen *= nrrdElementSize(nin);
colStep = rowLen*nin->axis[saxi].size;
outdim = nin->dim-1;
for (ai=0; ai<outdim; ai++) {
map[ai] = ai + (ai >= saxi);
szOut[ai] = nin->axis[map[ai]].size;
}
nout->blockSize = nin->blockSize;
if (nrrdMaybeAlloc_nva(nout, nin->type, outdim, szOut)) {
biffAddf(NRRD, "%s: failed to create slice", me);
return 1;
}
/* the skinny */
src = (char *)nin->data;
dest = (char *)nout->data;
src += rowLen*pos;
for (I=0; I<colLen; I++) {
/* HEY: replace with AIR_MEMCPY() or similar, when applicable */
memcpy(dest, src, rowLen);
src += colStep;
dest += rowLen;
}
/* copy the peripheral information */
if (nrrdAxisInfoCopy(nout, nin, map, NRRD_AXIS_INFO_NONE)) {
biffAddf(NRRD, "%s:", me);
return 1;
}
if (nrrdContentSet_va(nout, func, nin, "%d,%d", saxi, pos)) {
biffAddf(NRRD, "%s:", me);
return 1;
}
if (nrrdBasicInfoCopy(nout, nin,
NRRD_BASIC_INFO_DATA_BIT
| NRRD_BASIC_INFO_TYPE_BIT
| NRRD_BASIC_INFO_BLOCKSIZE_BIT
| NRRD_BASIC_INFO_DIMENSION_BIT
| NRRD_BASIC_INFO_SPACEORIGIN_BIT
| NRRD_BASIC_INFO_CONTENT_BIT
| NRRD_BASIC_INFO_COMMENTS_BIT
| (nrrdStateKeyValuePairsPropagate
? 0
: NRRD_BASIC_INFO_KEYVALUEPAIRS_BIT))) {
biffAddf(NRRD, "%s:", me);
return 1;
}
/* translate origin if this was a spatial axis, otherwise copy */
/* note that if there is no spatial info at all, this is all harmless */
if (AIR_EXISTS(nin->axis[saxi].spaceDirection[0])) {
nrrdSpaceVecScaleAdd2(nout->spaceOrigin,
1.0, nin->spaceOrigin,
AIR_CAST(double, pos),
nin->axis[saxi].spaceDirection);
} else {
nrrdSpaceVecCopy(nout->spaceOrigin, nin->spaceOrigin);
}
return 0;
}
/*
******** nrrdCrop()
**
** select some sub-volume inside a given nrrd, producing an output
** nrrd with the same dimensions, but with equal or smaller sizes
** along each axis.
*/
int
nrrdCrop(Nrrd *nout, const Nrrd *nin, size_t *min, size_t *max) {
static const char me[]="nrrdCrop", func[] = "crop";
char buff1[NRRD_DIM_MAX*30], buff2[AIR_STRLEN_SMALL];
unsigned int ai;
size_t I,
lineSize, /* #bytes in one scanline to be copied */
typeSize, /* size of data type */
cIn[NRRD_DIM_MAX], /* coords for line start, in input */
cOut[NRRD_DIM_MAX], /* coords for line start, in output */
szIn[NRRD_DIM_MAX],
szOut[NRRD_DIM_MAX],
idxIn, idxOut, /* linear indices for input and output */
numLines; /* number of scanlines in output nrrd */
char *dataIn, *dataOut;
/* errors */
if (!(nout && nin && min && max)) {
biffAddf(NRRD, "%s: got NULL pointer", me);
return 1;
}
if (nout == nin) {
biffAddf(NRRD, "%s: nout==nin disallowed", me);
return 1;
}
for (ai=0; ai<nin->dim; ai++) {
if (!(min[ai] <= max[ai])) {
biffAddf(NRRD, "%s: axis %d min (" _AIR_SIZE_T_CNV
") not <= max (" _AIR_SIZE_T_CNV ")",
me, ai, min[ai], max[ai]);
return 1;
}
if (!( min[ai] < nin->axis[ai].size && max[ai] < nin->axis[ai].size )) {
biffAddf(NRRD, "%s: axis %d min (" _AIR_SIZE_T_CNV
") or max (" _AIR_SIZE_T_CNV ") out of bounds [0,"
_AIR_SIZE_T_CNV "]",
me, ai, min[ai], max[ai], nin->axis[ai].size-1);
return 1;
}
}
/* this shouldn't actually be necessary .. */
if (!nrrdElementSize(nin)) {
biffAddf(NRRD, "%s: nrrd reports zero element size!", me);
return 1;
}
/* allocate */
nrrdAxisInfoGet_nva(nin, nrrdAxisInfoSize, szIn);
numLines = 1;
for (ai=0; ai<nin->dim; ai++) {
szOut[ai] = max[ai] - min[ai] + 1;
if (ai) {
numLines *= szOut[ai];
}
}
nout->blockSize = nin->blockSize;
if (nrrdMaybeAlloc_nva(nout, nin->type, nin->dim, szOut)) {
biffAddf(NRRD, "%s:", me);
return 1;
}
lineSize = szOut[0]*nrrdElementSize(nin);
/* the skinny */
typeSize = nrrdElementSize(nin);
dataIn = (char *)nin->data;
dataOut = (char *)nout->data;
memset(cOut, 0, NRRD_DIM_MAX*sizeof(unsigned int));
/*
printf("!%s: nin->dim = %d\n", me, nin->dim);
printf("!%s: min = %d %d %d\n", me, min[0], min[1], min[2]);
printf("!%s: szIn = %d %d %d\n", me, szIn[0], szIn[1], szIn[2]);
printf("!%s: szOut = %d %d %d\n", me, szOut[0], szOut[1], szOut[2]);
printf("!%s: lineSize = %d\n", me, lineSize);
printf("!%s: typeSize = %d\n", me, typeSize);
printf("!%s: numLines = %d\n", me, (int)numLines);
*/
for (I=0; I<numLines; I++) {
for (ai=0; ai<nin->dim; ai++) {
cIn[ai] = cOut[ai] + min[ai];
}
NRRD_INDEX_GEN(idxOut, cOut, szOut, nin->dim);
NRRD_INDEX_GEN(idxIn, cIn, szIn, nin->dim);
/*
printf("!%s: %5d: cOut=(%3d,%3d,%3d) --> idxOut = %5d\n",
me, (int)I, cOut[0], cOut[1], cOut[2], (int)idxOut);
printf("!%s: %5d: cIn=(%3d,%3d,%3d) --> idxIn = %5d\n",
me, (int)I, cIn[0], cIn[1], cIn[2], (int)idxIn);
*/
memcpy(dataOut + idxOut*typeSize, dataIn + idxIn*typeSize, lineSize);
/* the lowest coordinate in cOut[] will stay zero, since we are
copying one (1-D) scanline at a time */
NRRD_COORD_INCR(cOut, szOut, nin->dim, 1);
}
if (nrrdAxisInfoCopy(nout, nin, NULL, (NRRD_AXIS_INFO_SIZE_BIT |
NRRD_AXIS_INFO_MIN_BIT |
NRRD_AXIS_INFO_MAX_BIT ))) {
biffAddf(NRRD, "%s:", me);
return 1;
}
for (ai=0; ai<nin->dim; ai++) {
nrrdAxisInfoPosRange(&(nout->axis[ai].min), &(nout->axis[ai].max),
nin, ai, AIR_CAST(double, min[ai]),
AIR_CAST(double, max[ai]));
/* do the safe thing first */
nout->axis[ai].kind = _nrrdKindAltered(nin->axis[ai].kind, AIR_FALSE);
/* try cleverness */
if (!nrrdStateKindNoop) {
if (nout->axis[ai].size == nin->axis[ai].size) {
/* we can safely copy kind; the samples didn't change */
nout->axis[ai].kind = nin->axis[ai].kind;
} else if (nrrdKind4Color == nin->axis[ai].kind
&& 3 == szOut[ai]) {
nout->axis[ai].kind = nrrdKind3Color;
} else if (nrrdKind4Vector == nin->axis[ai].kind
&& 3 == szOut[ai]) {
nout->axis[ai].kind = nrrdKind3Vector;
} else if ((nrrdKind4Vector == nin->axis[ai].kind
|| nrrdKind3Vector == nin->axis[ai].kind)
&& 2 == szOut[ai]) {
nout->axis[ai].kind = nrrdKind2Vector;
} else if (nrrdKindRGBAColor == nin->axis[ai].kind
&& 0 == min[ai]
&& 2 == max[ai]) {
nout->axis[ai].kind = nrrdKindRGBColor;
} else if (nrrdKind2DMaskedSymMatrix == nin->axis[ai].kind
&& 1 == min[ai]
&& max[ai] == szIn[ai]-1) {
nout->axis[ai].kind = nrrdKind2DSymMatrix;
} else if (nrrdKind2DMaskedMatrix == nin->axis[ai].kind
&& 1 == min[ai]
&& max[ai] == szIn[ai]-1) {
nout->axis[ai].kind = nrrdKind2DMatrix;
} else if (nrrdKind3DMaskedSymMatrix == nin->axis[ai].kind
&& 1 == min[ai]
&& max[ai] == szIn[ai]-1) {
nout->axis[ai].kind = nrrdKind3DSymMatrix;
} else if (nrrdKind3DMaskedMatrix == nin->axis[ai].kind
&& 1 == min[ai]
&& max[ai] == szIn[ai]-1) {
nout->axis[ai].kind = nrrdKind3DMatrix;
}
}
}
strcpy(buff1, "");
for (ai=0; ai<nin->dim; ai++) {
sprintf(buff2, "%s[" _AIR_SIZE_T_CNV "," _AIR_SIZE_T_CNV "]",
(ai ? "x" : ""), min[ai], max[ai]);
strcat(buff1, buff2);
}
if (nrrdContentSet_va(nout, func, nin, "%s", buff1)) {
biffAddf(NRRD, "%s:", me);
return 1;
}
if (nrrdBasicInfoCopy(nout, nin,
NRRD_BASIC_INFO_DATA_BIT
| NRRD_BASIC_INFO_TYPE_BIT
| NRRD_BASIC_INFO_BLOCKSIZE_BIT
| NRRD_BASIC_INFO_DIMENSION_BIT
| NRRD_BASIC_INFO_SPACEORIGIN_BIT
| NRRD_BASIC_INFO_CONTENT_BIT
| NRRD_BASIC_INFO_COMMENTS_BIT
| (nrrdStateKeyValuePairsPropagate
? 0
: NRRD_BASIC_INFO_KEYVALUEPAIRS_BIT))) {
biffAddf(NRRD, "%s:", me);
return 1;
}
/* copy origin, then shift it along the spatial axes */
nrrdSpaceVecCopy(nout->spaceOrigin, nin->spaceOrigin);
for (ai=0; ai<nin->dim; ai++) {
if (AIR_EXISTS(nin->axis[ai].spaceDirection[0])) {
nrrdSpaceVecScaleAdd2(nout->spaceOrigin,
1.0, nout->spaceOrigin,
AIR_CAST(double, min[ai]),
nin->axis[ai].spaceDirection);
}
}
return 0;
}
|