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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
|
/*
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"
/*
Wed Sep 14 05:55:40 EDT 2005: these are no longer used
void
nrrdPeripheralInit(Nrrd *nrrd) {
nrrdBasicInfoInit(nrrd,
NRRD_BASIC_INFO_DATA_BIT
| NRRD_BASIC_INFO_TYPE_BIT
| NRRD_BASIC_INFO_BLOCKSIZE_BIT
| NRRD_BASIC_INFO_DIMENSION_BIT
| NRRD_BASIC_INFO_CONTENT_BIT
| NRRD_BASIC_INFO_COMMENTS_BIT
| NRRD_BASIC_INFO_KEYVALUEPAIRS_BIT);
return;
}
int
nrrdPeripheralCopy(Nrrd *nout, const Nrrd *nin) {
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_CONTENT_BIT
| NRRD_BASIC_INFO_COMMENTS_BIT
| NRRD_BASIC_INFO_KEYVALUEPAIRS_BIT);
return 0;
}
*/
/* ------------------------------------------------------------ */
void
nrrdIoStateInit(NrrdIoState *nio) {
if (nio) {
nio->path = (char *)airFree(nio->path);
nio->base = (char *)airFree(nio->base);
nio->line = (char *)airFree(nio->line);
nio->dataFNFormat = (char *)airFree(nio->dataFNFormat);
/* the way IO to/from strings works, I don't think this should be freed */
nio->headerStringRead = NULL;
nio->headerStringWrite = NULL;
airArrayLenSet(nio->dataFNArr, 0);
/* closing this is always someone else's responsibility */
nio->headerFile = NULL;
nio->dataFile = NULL;
nio->dataFileDim = 0;
nio->dataFNMin = 0;
nio->dataFNMax = 0;
nio->dataFNStep = 0;
nio->dataFNIndex = -1;
nio->lineLen = 0;
nio->pos = 0;
nio->endian = airEndianUnknown;
nio->lineSkip = 0;
nio->headerStrlen = 0;
nio->headerStrpos = 0;
nio->byteSkip = 0;
memset(nio->seen, 0, (NRRD_FIELD_MAX+1)*sizeof(int));
nio->detachedHeader = AIR_FALSE;
nio->bareText = nrrdDefaultWriteBareText;
nio->charsPerLine = nrrdDefaultWriteCharsPerLine;
nio->valsPerLine = nrrdDefaultWriteValsPerLine;
nio->skipData = AIR_FALSE;
nio->keepNrrdDataFileOpen = AIR_FALSE;
nio->zlibLevel = -1;
nio->zlibStrategy = nrrdZlibStrategyDefault;
nio->bzip2BlockSize = -1;
nio->learningHeaderStrlen = AIR_FALSE;
nio->oldData = NULL;
nio->oldDataSize = 0;
nio->format = nrrdFormatUnknown;
nio->encoding = nrrdEncodingUnknown;
}
return;
}
NrrdIoState *
nrrdIoStateNew(void) {
NrrdIoState *nio;
nio = (NrrdIoState *)calloc(1, sizeof(NrrdIoState));
if (nio) {
nio->path = NULL;
nio->base = NULL;
nio->line = NULL;
nio->dataFNFormat = NULL;
nio->dataFN = NULL;
nio->headerStringRead = NULL;
nio->headerStringWrite = NULL;
nio->dataFNArr = airArrayNew((void**)(&(nio->dataFN)), NULL,
sizeof(char *), NRRD_FILENAME_INCR);
airArrayPointerCB(nio->dataFNArr, airNull, airFree);
nio->format = nrrdFormatUnknown;
nio->encoding = nrrdEncodingUnknown;
nrrdIoStateInit(nio);
}
return nio;
}
NrrdIoState *
nrrdIoStateNix(NrrdIoState *nio) {
nio->path = (char *)airFree(nio->path);
nio->base = (char *)airFree(nio->base);
nio->line = (char *)airFree(nio->line);
nio->dataFNFormat = (char *)airFree(nio->dataFNFormat);
nio->dataFNArr = airArrayNuke(nio->dataFNArr);
/* the NrrdIoState never owned nio->oldData; we don't free it */
airFree(nio); /* no NULL assignment, else compile warnings */
return NULL;
}
/* ------------------------------------------------------------ */
/* see axis.c for axis-specific "methods" */
/* ------------------------------------------------------------ */
/*
******** nrrdBasicInfoInit
**
** resets "basic" (per-array) information
** formerly nrrdPeripheralInit
**
** the bitflag communicates which fields should *not* be initialized
*/
void
nrrdBasicInfoInit(Nrrd *nrrd, int bitflag) {
int dd, ee;
if (!nrrd) {
return;
}
if (!(NRRD_BASIC_INFO_DATA_BIT & bitflag)) {
nrrd->data = airFree(nrrd->data);
}
if (!(NRRD_BASIC_INFO_TYPE_BIT & bitflag)) {
nrrd->type = nrrdTypeUnknown;
}
if (!(NRRD_BASIC_INFO_BLOCKSIZE_BIT & bitflag)) {
nrrd->blockSize = 0;
}
if (!(NRRD_BASIC_INFO_DIMENSION_BIT & bitflag)) {
nrrd->dim = 0;
}
if (!(NRRD_BASIC_INFO_CONTENT_BIT & bitflag)) {
nrrd->content = (char *)airFree(nrrd->content);
}
if (!(NRRD_BASIC_INFO_SAMPLEUNITS_BIT & bitflag)) {
nrrd->sampleUnits = (char *)airFree(nrrd->sampleUnits);
}
if (!(NRRD_BASIC_INFO_SPACE_BIT & bitflag)) {
nrrd->space = nrrdSpaceUnknown;
nrrd->spaceDim = 0;
}
if (!(NRRD_BASIC_INFO_SPACEDIMENSION_BIT & bitflag)) {
nrrd->space = nrrdSpaceUnknown;
nrrd->spaceDim = 0;
}
if (!(NRRD_BASIC_INFO_SPACEUNITS_BIT & bitflag)) {
for (dd=0; dd<NRRD_SPACE_DIM_MAX; dd++) {
nrrd->spaceUnits[dd] = (char *)airFree(nrrd->spaceUnits[dd]);
}
}
if (!(NRRD_BASIC_INFO_SPACEORIGIN_BIT & bitflag)) {
for (dd=0; dd<NRRD_SPACE_DIM_MAX; dd++) {
nrrd->spaceOrigin[dd] = AIR_NAN;
}
}
if (!(NRRD_BASIC_INFO_MEASUREMENTFRAME_BIT & bitflag)) {
for (dd=0; dd<NRRD_SPACE_DIM_MAX; dd++) {
for (ee=0; ee<NRRD_SPACE_DIM_MAX; ee++) {
nrrd->measurementFrame[dd][ee] = AIR_NAN;
}
}
}
if (!(NRRD_BASIC_INFO_OLDMIN_BIT & bitflag)) {
nrrd->oldMin = AIR_NAN;
}
if (!(NRRD_BASIC_INFO_OLDMAX_BIT & bitflag)) {
nrrd->oldMax = AIR_NAN;
}
if (!(NRRD_BASIC_INFO_COMMENTS_BIT & bitflag)) {
nrrdCommentClear(nrrd);
}
if (!(NRRD_BASIC_INFO_KEYVALUEPAIRS_BIT & bitflag)) {
nrrdKeyValueClear(nrrd);
}
return;
}
/*
******** nrrdBasicInfoCopy
**
** copies "basic" (per-array) information
** formerly known as nrrdPeripheralCopy, which was not used consistently
**
** the bitflag communicates which fields should *not* be copied
*/
int
nrrdBasicInfoCopy(Nrrd *dest, const Nrrd *src, int bitflag) {
static const char me[]="nrrdBasicInfoCopy";
unsigned int dd, ee;
if (!( dest && src ))
return 0;
if (dest == src) {
/* nothing to do */
return 0;
}
if (!(NRRD_BASIC_INFO_DATA_BIT & bitflag)) {
dest->data = src->data;
}
if (!(NRRD_BASIC_INFO_TYPE_BIT & bitflag)) {
dest->type = src->type;
}
if (!(NRRD_BASIC_INFO_BLOCKSIZE_BIT & bitflag)) {
dest->blockSize = src->blockSize;
}
if (!(NRRD_BASIC_INFO_DIMENSION_BIT & bitflag)) {
dest->dim = src->dim;
}
if (!(NRRD_BASIC_INFO_CONTENT_BIT & bitflag)) {
dest->content = (char *)airFree(dest->content);
dest->content = airStrdup(src->content);
if (src->content && !dest->content) {
biffAddf(NRRD, "%s: couldn't copy content", me);
return 1;
}
}
if (!(NRRD_BASIC_INFO_SAMPLEUNITS_BIT & bitflag)) {
dest->sampleUnits = (char *)airFree(dest->sampleUnits);
dest->sampleUnits = airStrdup(src->sampleUnits);
if (src->sampleUnits && !dest->sampleUnits) {
biffAddf(NRRD, "%s: couldn't copy sampleUnits", me);
return 1;
}
}
if (!(NRRD_BASIC_INFO_SPACE_BIT & bitflag)) {
dest->space = src->space;
}
if (!(NRRD_BASIC_INFO_SPACEDIMENSION_BIT & bitflag)) {
dest->spaceDim = src->spaceDim;
}
if (!(NRRD_BASIC_INFO_SPACEUNITS_BIT & bitflag)) {
for (dd=0; dd<src->spaceDim; dd++) {
dest->spaceUnits[dd] = (char *)airFree(dest->spaceUnits[dd]);
dest->spaceUnits[dd] = airStrdup(src->spaceUnits[dd]);
if (src->spaceUnits[dd] && !dest->spaceUnits[dd]) {
biffAddf(NRRD, "%s: couldn't copy spaceUnits[%d]", me, dd);
return 1;
}
}
for (dd=src->spaceDim; dd<NRRD_SPACE_DIM_MAX; dd++) {
dest->spaceUnits[dd] = (char *)airFree(dest->spaceUnits[dd]);
}
}
if (!(NRRD_BASIC_INFO_SPACEORIGIN_BIT & bitflag)) {
for (dd=0; dd<NRRD_SPACE_DIM_MAX; dd++) {
if (dd <= src->spaceDim-1) {
dest->spaceOrigin[dd] = src->spaceOrigin[dd];
} else {
dest->spaceOrigin[dd] = AIR_NAN;
}
}
}
if (!(NRRD_BASIC_INFO_MEASUREMENTFRAME_BIT & bitflag)) {
for (dd=0; dd<NRRD_SPACE_DIM_MAX; dd++) {
for (ee=0; ee<NRRD_SPACE_DIM_MAX; ee++) {
if (dd <= src->spaceDim-1 && ee <= src->spaceDim-1) {
dest->measurementFrame[dd][ee] = src->measurementFrame[dd][ee];
} else {
dest->measurementFrame[dd][ee] = AIR_NAN;
}
}
}
for (dd=src->spaceDim; dd<NRRD_SPACE_DIM_MAX; dd++) {
dest->spaceOrigin[dd] = AIR_NAN;
}
}
if (!(NRRD_BASIC_INFO_OLDMIN_BIT & bitflag)) {
dest->oldMin = src->oldMin;
}
if (!(NRRD_BASIC_INFO_OLDMAX_BIT & bitflag)) {
dest->oldMax = src->oldMax;
}
if (!(NRRD_BASIC_INFO_COMMENTS_BIT & bitflag)) {
if (nrrdCommentCopy(dest, src)) {
biffAddf(NRRD, "%s: trouble copying comments", me);
return 1;
}
}
if (!(NRRD_BASIC_INFO_KEYVALUEPAIRS_BIT & bitflag)) {
if (nrrdKeyValueCopy(dest, src)) {
biffAddf(NRRD, "%s: trouble copying key/value pairs", me);
return 1;
}
}
return 0;
}
/*
******* nrrdInit
**
** initializes a nrrd to default state. All nrrd functions in the
** business of initializing a nrrd struct use this function. Mostly
** just sets values to 0, NaN, "", NULL, or Unknown
*/
void
nrrdInit(Nrrd *nrrd) {
int ii;
if (nrrd) {
nrrdBasicInfoInit(nrrd, NRRD_BASIC_INFO_NONE);
for (ii=0; ii<NRRD_DIM_MAX; ii++) {
_nrrdAxisInfoInit(nrrd->axis + ii);
}
}
return;
}
/*
******** nrrdNew()
**
** creates and initializes a Nrrd
**
** this does NOT use biff
*/
Nrrd *
nrrdNew(void) {
int ii;
Nrrd *nrrd;
nrrd = (Nrrd*)(calloc(1, sizeof(Nrrd)));
if (!nrrd) {
return NULL;
}
/* explicitly set pointers to NULL, since calloc isn't officially
guaranteed to do that. */
nrrd->data = NULL;
for (ii=0; ii<NRRD_DIM_MAX; ii++) {
_nrrdAxisInfoNewInit(nrrd->axis + ii);
}
for (ii=0; ii<NRRD_SPACE_DIM_MAX; ii++) {
nrrd->spaceUnits[ii] = NULL;
}
nrrd->content = NULL;
nrrd->sampleUnits = NULL;
/* create comment airArray (even though it starts empty) */
nrrd->cmt = NULL;
nrrd->cmtArr = airArrayNew((void**)(&(nrrd->cmt)), NULL,
sizeof(char *), NRRD_COMMENT_INCR);
if (!nrrd->cmtArr) {
return NULL;
}
airArrayPointerCB(nrrd->cmtArr, airNull, airFree);
/* create key/value airArray (even thought it starts empty) */
nrrd->kvp = NULL;
nrrd->kvpArr = airArrayNew((void**)(&(nrrd->kvp)), NULL,
2*sizeof(char *), NRRD_KEYVALUE_INCR);
if (!nrrd->kvpArr) {
return NULL;
}
/* key/value airArray uses no callbacks for now */
/* finish initializations */
nrrdInit(nrrd);
return nrrd;
}
/*
******** nrrdNix()
**
** does nothing with the array data inside, just does whatever is needed
** to free the nrrd itself
**
** returns NULL
**
** this does NOT use biff
*/
Nrrd *
nrrdNix(Nrrd *nrrd) {
int ii;
if (nrrd) {
for (ii=0; ii<NRRD_DIM_MAX; ii++) {
_nrrdAxisInfoInit(&(nrrd->axis[ii]));
}
for (ii=0; ii<NRRD_SPACE_DIM_MAX; ii++) {
nrrd->spaceUnits[ii] = (char *)airFree(nrrd->spaceUnits[ii]);
}
nrrd->content = (char *)airFree(nrrd->content);
nrrd->sampleUnits = (char *)airFree(nrrd->sampleUnits);
nrrdCommentClear(nrrd);
nrrd->cmtArr = airArrayNix(nrrd->cmtArr);
nrrdKeyValueClear(nrrd);
nrrd->kvpArr = airArrayNix(nrrd->kvpArr);
airFree(nrrd);
}
return NULL;
}
/*
******** nrrdEmpty()
**
** frees data inside nrrd AND resets all its state, so its the
** same as what comes from nrrdNew(). This includes free()ing
** any comments.
*/
Nrrd *
nrrdEmpty(Nrrd *nrrd) {
if (nrrd) {
nrrd->data = airFree(nrrd->data);
nrrdInit(nrrd);
}
return nrrd;
}
/*
******** nrrdNuke()
**
** blows away the nrrd and everything inside
**
** always returns NULL
*/
Nrrd *
nrrdNuke(Nrrd *nrrd) {
if (nrrd) {
nrrdEmpty(nrrd);
nrrdNix(nrrd);
}
return NULL;
}
/* ------------------------------------------------------------ */
int
_nrrdSizeCheck(const size_t *size, unsigned int dim, int useBiff) {
static const char me[]="_nrrdSizeCheck";
size_t num, pre;
unsigned int ai;
pre = num = 1;
for (ai=0; ai<dim; ai++) {
if (!size[ai]) {
biffMaybeAddf(useBiff, NRRD, "%s: axis %u size is zero!", me, ai);
return 1;
}
num *= size[ai];
if (num/size[ai] != pre) {
biffMaybeAddf(useBiff, NRRD,
"%s: total # of elements too large to be represented in "
"type size_t, so too large for current architecture", me);
return 1;
}
pre *= size[ai];
}
return 0;
}
/*
******** nrrdWrap_nva()
**
** wraps a given Nrrd around a given array
**
** we don't touch any of the peripheral information (content, comments,
** blocksize, min/max) because it is entirely reasonable to be setting
** this before or after this call. "type" could be passed as
** nrrdTypeBlock, in which case it is the user's responsibility to
** set nrrd->blockSize at some other time.
*/
int
nrrdWrap_nva(Nrrd *nrrd, void *data, int type,
unsigned int dim, const size_t *size) {
static const char me[]="nrrdWrap_nva";
if (!(nrrd && size)) {
biffAddf(NRRD, "%s: got NULL pointer", me);
return 1;
}
nrrd->data = data;
nrrd->type = type;
nrrd->dim = dim;
if (_nrrdSizeCheck(size, dim, AIR_TRUE)) {
biffAddf(NRRD, "%s:", me);
return 1;
}
nrrdAxisInfoSet_nva(nrrd, nrrdAxisInfoSize, size);
return 0;
}
/*
******** nrrdWrap_va()
**
** Minimal var args wrapper around nrrdWrap_nva, with the advantage of
** taking all the axes sizes as the var args.
**
** This is THE BEST WAY to wrap a nrrd around existing raster data,
** assuming that the dimension is known at compile time.
**
** If successful, returns 0, otherwise, 1.
** This does use biff.
*/
int
nrrdWrap_va(Nrrd *nrrd, void *data, int type, unsigned int dim, ...) {
static const char me[]="nrrdWrap_va";
va_list ap;
size_t size[NRRD_DIM_MAX];
unsigned int ai;
if (!(nrrd && data)) {
biffAddf(NRRD, "%s: got NULL pointer", me);
return 1;
}
va_start(ap, dim);
for (ai=0; ai<dim; ai++) {
size[ai] = va_arg(ap, size_t);
}
va_end(ap);
return nrrdWrap_nva(nrrd, data, type, dim, size);
}
/*
void
_nrrdTraverse(Nrrd *nrrd) {
char *test, tval;
size_t I, N;
int S;
N = nrrdElementNumber(nrrd);
S = nrrdElementSize(nrrd);
tval = 0;
test = nrrd->data;
for (I=0; I<N*S; I++) {
tval += test[I];
}
}
*/
int
_nrrdCopy(Nrrd *nout, const Nrrd *nin, int bitflag) {
static const char me[]="_nrrdCopy";
size_t size[NRRD_DIM_MAX];
if (!(nin && nout)) {
biffAddf(NRRD, "%s: got NULL pointer", me);
return 1;
}
if (nout == nin) {
/* its not the case that we have nothing to do- the semantics of
copying cannot be achieved if the input and output nrrd are
the same; this is an error */
biffAddf(NRRD, "%s: nout==nin disallowed", me);
return 1;
}
if (!nrrdElementSize(nin)) {
biffAddf(NRRD, "%s: input nrrd reports zero element size!", me);
return 1;
}
nrrdAxisInfoGet_nva(nin, nrrdAxisInfoSize, size);
if (nin->data) {
if (nrrdMaybeAlloc_nva(nout, nin->type, nin->dim, size)) {
biffAddf(NRRD, "%s: couldn't allocate data", me);
return 1;
}
memcpy(nout->data, nin->data,
nrrdElementNumber(nin)*nrrdElementSize(nin));
} else {
/* someone is trying to copy structs without data, fine fine fine */
if (nrrdWrap_nva(nout, NULL, nin->type, nin->dim, size)) {
biffAddf(NRRD, "%s: couldn't allocate data", me);
return 1;
}
}
nrrdAxisInfoCopy(nout, nin, NULL, NRRD_AXIS_INFO_SIZE_BIT);
/* if nin->data non-NULL (second branch above), this will
harmlessly unset and set type and dim */
nrrdBasicInfoInit(nout, NRRD_BASIC_INFO_DATA_BIT | bitflag);
if (nrrdBasicInfoCopy(nout, nin, NRRD_BASIC_INFO_DATA_BIT | bitflag)) {
biffAddf(NRRD, "%s: trouble copying basic info", me);
return 1;
}
return 0;
}
/*
******** nrrdCopy
**
** copy method for nrrds. nout will end up as an "exact" copy of nin.
** New space for data is allocated here, and output nrrd points to it.
** Comments from old are added to comments for new, so these are also
** newly allocated. nout->ptr is not set, nin->ptr is not read.
*/
int
nrrdCopy(Nrrd *nout, const Nrrd *nin) {
static const char me[]="nrrdCopy";
if (_nrrdCopy(nout, nin, NRRD_BASIC_INFO_NONE)) {
biffAddf(NRRD, "%s:", me);
return 1;
}
return 0;
}
/*
******** nrrdAlloc_nva()
**
** allocates data array and sets information. If this is a block type
** nrrd, it is necessary to set nrrd->blockSize PRIOR to calling
** this function.
**
** This function will always allocate more memory (via calloc), but
** it will free() nrrd->data if it is non-NULL when passed in.
**
** This function takes the same "don't mess with peripheral information"
** attitude as nrrdWrap().
**
** Note to Gordon: don't get clever and change ANY axis-specific
** information here. It may be very convenient to set that before
** nrrdAlloc or nrrdMaybeAlloc
**
** Note: This function DOES use biff
*/
int
nrrdAlloc_nva(Nrrd *nrrd, int type, unsigned int dim, const size_t *size) {
static const char me[]="nrrdAlloc_nva";
size_t num, esize;
if (!(nrrd && size)) {
biffAddf(NRRD, "%s: got NULL pointer", me);
return 1;
}
if (airEnumValCheck(nrrdType, type)) {
biffAddf(NRRD, "%s: type (%d) is invalid", me, type);
return 1;
}
if (nrrdTypeBlock == type) {
if (!(0 < nrrd->blockSize)) {
biffAddf(NRRD, "%s: given nrrd->blockSize " _AIR_SIZE_T_CNV " invalid",
me, nrrd->blockSize);
return 1;
}
}
if (!AIR_IN_CL(1, dim, NRRD_DIM_MAX)) {
biffAddf(NRRD, "%s: dim (%d) not in valid range [1,%d]",
me, dim, NRRD_DIM_MAX);
return 1;
}
nrrd->data = airFree(nrrd->data);
if (nrrdWrap_nva(nrrd, NULL, type, dim, size)) {
biffAddf(NRRD, "%s:", me);
return 1 ;
}
num = nrrdElementNumber(nrrd);
esize = nrrdElementSize(nrrd);
nrrd->data = calloc(num, esize);
if (!(nrrd->data)) {
biffAddf(NRRD, "%s: calloc(" _AIR_SIZE_T_CNV ","
_AIR_SIZE_T_CNV ") failed",
me, num, esize);
return 1 ;
}
return 0;
}
/*
******** nrrdAlloc_va()
**
** Handy wrapper around nrrdAlloc_nva, which takes, as its vararg list,
** all the axes sizes.
*/
int
nrrdAlloc_va(Nrrd *nrrd, int type, unsigned int dim, ...) {
static const char me[]="nrrdAlloc_va";
size_t size[NRRD_DIM_MAX];
unsigned int ai;
va_list ap;
if (!nrrd) {
biffAddf(NRRD, "%s: got NULL pointer", me);
return 1;
}
va_start(ap, dim);
for (ai=0; ai<dim; ai++) {
size[ai] = va_arg(ap, size_t);
}
va_end(ap);
if (nrrdAlloc_nva(nrrd, type, dim, size)) {
biffAddf(NRRD, "%s:", me);
return 1;
}
return 0;
}
/*
******** nrrdMaybeAlloc_nva
**
** calls nrrdAlloc_nva if the requested space is different than
** what is currently held
**
** also subscribes to the "don't mess with peripheral information" philosophy
*/
int
nrrdMaybeAlloc_nva(Nrrd *nrrd, int type,
unsigned int dim, const size_t *size) {
static const char me[]="nrrdMaybeAlloc_nva";
size_t sizeWant, sizeHave, numWant, elementSizeWant;
int need;
unsigned int ai;
if (!nrrd) {
biffAddf(NRRD, "%s: got NULL pointer", me);
return 1;
}
if (airEnumValCheck(nrrdType, type)) {
biffAddf(NRRD, "%s: type (%d) is invalid", me, type);
return 1;
}
if (nrrdTypeBlock == type) {
if (nrrdTypeBlock == nrrd->type) {
biffAddf(NRRD, "%s: can't change from one block nrrd to another", me);
return 1;
}
if (!(0 < nrrd->blockSize)) {
biffAddf(NRRD, "%s: given nrrd->blockSize "
_AIR_SIZE_T_CNV " invalid", me, nrrd->blockSize);
return 1;
}
elementSizeWant = nrrd->blockSize;
} else {
elementSizeWant = nrrdTypeSize[type];
}
if (_nrrdSizeCheck(size, dim, AIR_TRUE)) {
biffAddf(NRRD, "%s:", me);
return 1;
}
if (!(nrrd->data)) {
need = 1;
} else {
numWant = 1;
for (ai=0; ai<dim; ai++) {
numWant *= size[ai];
}
if (!nrrdElementSize(nrrd)) {
biffAddf(NRRD, "%s: nrrd reports zero element size!", me);
return 1;
}
sizeHave = nrrdElementNumber(nrrd) * nrrdElementSize(nrrd);
/* fprintf(stderr, "##%s: sizeHave = %d * %d = %d\n", me,
(int)(nrrdElementNumber(nrrd)),
(int)(nrrdElementSize(nrrd)), (int)sizeHave); */
sizeWant = numWant * elementSizeWant;
/* fprintf(stderr, "##%s: sizeWant = %d * %d = %d\n", me,
(int)(numWant),
(int)(elementSizeWant), (int)sizeWant); */
need = sizeHave != sizeWant;
/* fprintf(stderr, "##%s: need = %d\n", me, need); */
}
if (need) {
if (nrrdAlloc_nva(nrrd, type, dim, size)) {
biffAddf(NRRD, "%s:", me);
return 1;
}
} else {
if (nrrdWrap_nva(nrrd, nrrd->data, type, dim, size)) {
biffAddf(NRRD, "%s:", me);
return 1;
}
/* but we do have to initialize memory! */
memset(nrrd->data, 0, nrrdElementNumber(nrrd)*nrrdElementSize(nrrd));
}
return 0;
}
/*
******** nrrdMaybeAlloc_va()
**
** Handy wrapper around nrrdAlloc, which takes, as its vararg list
** all the axes sizes, thereby calculating the total number.
*/
int
nrrdMaybeAlloc_va(Nrrd *nrrd, int type, unsigned int dim, ...) {
static const char me[]="nrrdMaybeAlloc_va";
size_t size[NRRD_DIM_MAX];
unsigned int ai;
va_list ap;
if (!nrrd) {
biffAddf(NRRD, "%s: got NULL pointer", me);
return 1;
}
va_start(ap, dim);
for (ai=0; ai<dim; ai++) {
size[ai] = va_arg(ap, size_t);
}
va_end(ap);
if (nrrdMaybeAlloc_nva(nrrd, type, dim, size)) {
biffAddf(NRRD, "%s:", me);
return 1;
}
return 0;
}
/*
******** nrrdPPM()
**
** for making a nrrd suitable for holding PPM data
**
** "don't mess with peripheral information"
*/
int
nrrdPPM(Nrrd *ppm, size_t sx, size_t sy) {
static const char me[]="nrrdPPM";
if (nrrdMaybeAlloc_va(ppm, nrrdTypeUChar, 3,
AIR_CAST(size_t, 3), sx, sy)) {
biffAddf(NRRD, "%s: couldn't allocate " _AIR_SIZE_T_CNV
" x " _AIR_SIZE_T_CNV " 24-bit image", me, sx, sy);
return 1;
}
return 0;
}
/*
******** nrrdPGM()
**
** for making a nrrd suitable for holding PGM data
**
** "don't mess with peripheral information"
*/
int
nrrdPGM(Nrrd *pgm, size_t sx, size_t sy) {
static const char me[]="nrrdPGM";
if (nrrdMaybeAlloc_va(pgm, nrrdTypeUChar, 2,
sx, sy)) {
biffAddf(NRRD, "%s: couldn't allocate " _AIR_SIZE_T_CNV
" x " _AIR_SIZE_T_CNV " 8-bit image", me, sx, sy);
return 1;
}
return 0;
}
|