示例#1
0
/*
******** 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;
}
示例#2
0
文件: keyvalue.c 项目: 151706061/ITK
/*
******** nrrdKeyValueCopy()
**
** copies key/value pairs from one nrrd to another
** Existing key/value pairs in nout are blown away
*/
int
nrrdKeyValueCopy(Nrrd *nout, const Nrrd *nin) {
  char *key, *value;
  unsigned int ki;

  if (!(nout && nin)) {
    /* got NULL pointer */
    return 1;
  }
  if (nout == nin) {
    /* can't satisfy semantics of copying with nout==nin */
    return 2;
  }

  nrrdKeyValueClear(nout);
  for (ki=0; ki<nin->kvpArr->len; ki++) {
    key = nin->kvp[0 + 2*ki];
    value = nin->kvp[1 + 2*ki];
    if (nrrdKeyValueAdd(nout, key, value)) {
      return 3;
    }
  }

  return 0;
}
示例#3
0
void
demoIO(char *filename)
{
  char me[]="demoIO", newname[]="foo.nrrd", *err, *key, *val;
  int kvn, kvi;
  Nrrd *nin;

  /* create a nrrd; at this point this is just an empty container */
  nin = nrrdNew();

  /* read in the nrrd from file */
  if (nrrdLoad(nin, filename, NULL))
  {
    err = biffGetDone(NRRD);
    fprintf(stderr, "%s: trouble reading \"%s\":\n%s", me, filename, err);
    free(err);
    return;
  }

  /* say something about the array */
  printf("%s: \"%s\" is a %d-dimensional nrrd of type %d (%s)\n",
         me, filename, nin->dim, nin->type,
         airEnumStr(nrrdType, nin->type));
  printf("%s: the array contains %d elements, each %d bytes in size\n",
         me, (int)nrrdElementNumber(nin), (int)nrrdElementSize(nin));

  /* print out the key/value pairs present */
  kvn = nrrdKeyValueSize(nin);
  if (kvn)
  {
    for (kvi=0; kvi<kvn; kvi++)
    {
      nrrdKeyValueIndex(nin, &key, &val, kvi);
      printf("%s: key:value %d = %s:%s\n", me, kvi, key, val);
      free(key);
      free(val);
      key = val = NULL;
    }
  }

  /* modify key/value pairs, and write out the nrrd to a different file */
  nrrdKeyValueClear(nin);
  nrrdKeyValueAdd(nin, "new key", "precious value");
  if (nrrdSave(newname, nin, NULL))
  {
    err = biffGetDone(NRRD);
    fprintf(stderr, "%s: trouble writing \"%s\":\n%s", me, newname, err);
    free(err);
    return;
  }

  /* blow away both the Nrrd struct *and* the memory at nin->data
     (nrrdNix() frees the struct but not the data,
     nrrdEmpty() frees the data but not the struct) */
  nrrdNuke(nin);

  return;
}
示例#4
0
/*
******** 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;
}
示例#5
0
文件: dnorm.c 项目: BRAINSia/teem
int
unrrdu_dnormMain(int argc, const char **argv, const char *me,
                 hestParm *hparm) {
    char *outS;
    int pret;

    Nrrd *nin, *nout;
    NrrdIoState *nio;
    int kindIn, kindOut, headerOnly, haveMM, trivialOrient, recenter, gotmf;
    unsigned int kindAxis, axi, si, sj;
    double sscl;

    hestOpt *opt = NULL;
    char *err;
    airArray *mop;

    hestOptAdd(&opt, "h,header", NULL, airTypeInt, 0, 0, &headerOnly, NULL,
               "output header of nrrd file only, not the data itself");
    hestOptAdd(&opt, "to", NULL, airTypeInt, 0, 0, &trivialOrient, NULL,
               "(*t*rivial *o*rientation) "
               "even if the input nrrd comes with full orientation or "
               "per-axis min-max info, ignore it and instead assert the "
               "identity mapping between index and world space");
    hestOptAdd(&opt, "c,center", NULL, airTypeInt, 0, 0, &recenter, NULL,
               "re-locate output spaceOrigin so that field is centered "
               "around origin of space coordinates");
    hestOptAdd(&opt, "s,scaling", "scl", airTypeDouble, 1, 1, &sscl, "1.0",
               "when having to contrive orientation information and there's "
               "no per-axis min/max to inform what the sample spacing is, "
               "this is the sample spacing to assert");
    OPT_ADD_NIN(nin, "input image");
    OPT_ADD_NOUT(outS, "output filename");

    mop = airMopNew();
    airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
    USAGE(_unrrdu_dnormInfoL);
    PARSE();
    airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);

    /* can't deal with block type */
    if (nrrdTypeBlock == nin->type) {
        fprintf(stderr, "%s: can only have scalar kinds (not %s)\n", me,
                airEnumStr(nrrdType, nrrdTypeBlock));
        airMopError(mop);
        exit(1);
    }

    /* make sure all kinds are set to something */
    /* see if there's a range kind, verify that there's only one */
    /* set haveMM */
    haveMM = AIR_TRUE;
    kindIn = nrrdKindUnknown;
    kindAxis = 0;
    for (axi=0; axi<nin->dim; axi++) {
        if (nrrdKindUnknown == nin->axis[axi].kind
                || nrrdKindIsDomain(nin->axis[axi].kind)) {
            haveMM &= AIR_EXISTS(nin->axis[axi].min);
            haveMM &= AIR_EXISTS(nin->axis[axi].max);
        } else {
            if (nrrdKindUnknown != kindIn) {
                fprintf(stderr, "%s: got non-domain kind %s on axis %u, but already "
                        "have %s from axis %u\n", me,
                        airEnumStr(nrrdKind, nin->axis[axi].kind), axi,
                        airEnumStr(nrrdKind, kindIn), kindAxis);
                airMopError(mop);
                exit(1);
            }
            kindIn = nin->axis[axi].kind;
            kindAxis = axi;
        }
    }
    /* see if the non-domain kind is something we can interpret as a tensor */
    if (nrrdKindUnknown != kindIn) {
        switch (kindIn) {
        /* ======= THESE are the kinds that we can possibly output ======= */
        case nrrdKind2Vector:
        case nrrdKind3Vector:
        case nrrdKind4Vector:
        case nrrdKind2DSymMatrix:
        case nrrdKind2DMatrix:
        case nrrdKind3DSymMatrix:
        case nrrdKind3DMatrix:
            /* =============================================================== */
            kindOut = kindIn;
            break;
        /* Some other kinds are mapped to those above */
        case nrrdKind3Color:
        case nrrdKindRGBColor:
            kindOut = nrrdKind3Vector;
            break;
        case nrrdKind4Color:
        case nrrdKindRGBAColor:
            kindOut = nrrdKind4Vector;
            break;
        default:
            fprintf(stderr, "%s: got non-conforming kind %s on axis %u\n", me,
                    airEnumStr(nrrdKind, kindIn), kindAxis);
            airMopError(mop);
            exit(1);
            break;
        }
    } else {
        /* kindIn is nrrdKindUnknown, so its a simple scalar image,
           and that's what the output will be too; kindOut == nrrdKindUnknown
           is used in the code below to say "its a scalar image" */
        kindOut = nrrdKindUnknown;
    }

    /* initialize output by copying */
    nout = nrrdNew();
    airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
    if (nrrdCopy(nout, nin)) {
        airMopAdd(mop, err = biffGet(NRRD), airFree, airMopAlways);
        fprintf(stderr, "%s: trouble copying:\n%s", me, err);
        airMopError(mop);
        exit(1);
    }

    /* no comments, either advertising the format URL or anything else */
    nio = nrrdIoStateNew();
    airMopAdd(mop, nio, (airMopper)nrrdIoStateNix, airMopAlways);
    nio->skipFormatURL = AIR_TRUE;
    if (headerOnly) {
        nio->skipData = AIR_TRUE;
    }
    nrrdCommentClear(nout);

    /* no measurement frame */
    gotmf = AIR_FALSE;
    for (si=0; si<NRRD_SPACE_DIM_MAX; si++) {
        for (sj=0; sj<NRRD_SPACE_DIM_MAX; sj++) {
            gotmf |= AIR_EXISTS(nout->measurementFrame[si][sj]);
        }
    }
    if (gotmf) {
        fprintf(stderr, "%s: WARNING: incoming array measurement frame; "
                "it will be erased on output.\n", me);
        /* airMopError(mop); exit(1); */
    }
    for (si=0; si<NRRD_SPACE_DIM_MAX; si++) {
        for (sj=0; sj<NRRD_SPACE_DIM_MAX; sj++) {
            nout->measurementFrame[si][sj] = AIR_NAN;
        }
    }

    /* no key/value pairs */
    nrrdKeyValueClear(nout);

    /* no content field */
    nout->content = airFree(nout->content);

    /* normalize domain kinds to "space" */
    /* turn off centers (Diderot assumes cell-centered, but that could
       probably stand to be tested and enforced more) */
    /* turn off thickness */
    /* turn off labels and units */
    for (axi=0; axi<nout->dim; axi++) {
        if (nrrdKindUnknown == kindOut) {
            nout->axis[axi].kind = nrrdKindSpace;
        } else {
            nout->axis[axi].kind = (kindAxis == axi
                                    ? kindOut
                                    : nrrdKindSpace);
        }
        nout->axis[axi].center = nrrdCenterUnknown;
        nout->axis[axi].thickness = AIR_NAN;
        nout->axis[axi].label = airFree(nout->axis[axi].label);
        nout->axis[axi].units = airFree(nout->axis[axi].units);
        nout->axis[axi].min = AIR_NAN;
        nout->axis[axi].max = AIR_NAN;
        nout->axis[axi].spacing = AIR_NAN;
    }

    /* logic of orientation definition:
       If space dimension is known:
          set origin to zero if not already set
          set space direction to unit vector if not already set
       Else if have per-axis min and max:
          set spae origin and directions to communicate same intent
          as original per-axis min and max and original centering
       Else
          set origin to zero and all space directions to units.
       It might be nice to use gage's logic for mapping from world to index,
       but we have to accept a greater variety of kinds and dimensions
       than gage ever has to process.
    */
    if (nout->spaceDim && !trivialOrient) {
        int saxi = 0;
        /* we use only the space dimension, not any named space */
        nout->space = nrrdSpaceUnknown;
        if (!nrrdSpaceVecExists(nout->spaceDim, nout->spaceOrigin)) {
            nrrdSpaceVecSetZero(nout->spaceOrigin);
        }
        for (axi=0; axi<nout->dim; axi++) {
            if (nrrdKindUnknown == kindOut || kindAxis != axi) {
                /* its a domain axis of output */
                if (!nrrdSpaceVecExists(nout->spaceDim,
                                        nout->axis[axi].spaceDirection)) {
                    nrrdSpaceVecSetZero(nout->axis[axi].spaceDirection);
                    nout->axis[axi].spaceDirection[saxi] = sscl;
                }
                /* else we leave existing space vector as is */
                saxi++;
            } else {
                /* else its a range axis */
                nrrdSpaceVecSetNaN(nout->axis[axi].spaceDirection);
            }
        }
    } else if (haveMM && !trivialOrient) {
        int saxi = 0;
        size_t N;
        double rng;
        for (axi=0; axi<nout->dim; axi++) {
            if (nrrdKindUnknown == kindOut || kindAxis != axi) {
                /* its a domain axis of output */
                nrrdSpaceVecSetZero(nout->axis[axi].spaceDirection);
                rng = nin->axis[axi].max - nin->axis[axi].min;
                if (nrrdCenterNode == nin->axis[axi].center) {
                    nout->spaceOrigin[saxi] = nin->axis[axi].min;
                    N = nin->axis[axi].size;
                    nout->axis[axi].spaceDirection[saxi] = rng/(N-1);
                } else {
                    /* unknown centering treated as cell */
                    N = nin->axis[axi].size;
                    nout->spaceOrigin[saxi] = nin->axis[axi].min + (rng/N)/2;
                    nout->axis[axi].spaceDirection[saxi] = rng/N;
                }
                saxi++;
            } else {
                /* else its a range axis */
                nrrdSpaceVecSetNaN(nout->axis[axi].spaceDirection);
            }
        }
        nout->spaceDim = saxi;
    } else {
        /* either trivialOrient, or, not spaceDim and not haveMM */
        int saxi = 0;
        nout->space = nrrdSpaceUnknown;
        nrrdSpaceVecSetZero(nout->spaceOrigin);
        for (axi=0; axi<nout->dim; axi++) {
            if (nrrdKindUnknown == kindOut || kindAxis != axi) {
                /* its a domain axis of output */
                nrrdSpaceVecSetZero(nout->axis[axi].spaceDirection);
                nout->axis[axi].spaceDirection[saxi]
                    = (AIR_EXISTS(nin->axis[axi].spacing)
                       ? nin->axis[axi].spacing
                       : sscl);
                saxi++;
            } else {
                /* else its a range axis */
                nrrdSpaceVecSetNaN(nout->axis[axi].spaceDirection);
            }
        }
        nout->spaceDim = saxi;
    }

    /* space dimension has to match the number of domain axes */
    if (nout->dim != nout->spaceDim + !!kindOut) {
        fprintf(stderr, "%s: output dim %d != spaceDim %d + %d %s%s%s\n",
                me, nout->dim, nout->spaceDim, !!kindOut,
                kindOut ? "for non-scalar (" : "(scalar data)",
                kindOut ? airEnumStr(nrrdKind, kindOut) : "",
                kindOut ? ") data" : "");
        airMopError(mop);
        exit(1);
    }

    if (recenter) {
        /* sets field's origin so field is centered on the origin. capiche? */
        /* this code was tacked on later than the stuff above, so its
           logic could probably be moved up there, but it seems cleaner to
           have it as a separate post-process */
        double mean[NRRD_SPACE_DIM_MAX];
        nrrdSpaceVecSetZero(mean);
        for (axi=0; axi<nout->dim; axi++) {
            if (nrrdKindUnknown == kindOut || kindAxis != axi) {
                nrrdSpaceVecScaleAdd2(mean, 1.0, mean,
                                      0.5*(nout->axis[axi].size - 1),
                                      nout->axis[axi].spaceDirection);
            }
        }
        nrrdSpaceVecScaleAdd2(mean, 1.0, mean,
                              1.0, nout->spaceOrigin);
        /* now mean is the center of the field */
        nrrdSpaceVecScaleAdd2(nout->spaceOrigin,
                              1.0, nout->spaceOrigin,
                              -1.0, mean);
    }

    if (nrrdSave(outS, nout, nio)) {
        airMopAdd(mop, err = biffGet(NRRD), airFree, airMopAlways);
        fprintf(stderr, "%s: trouble saving \"%s\":\n%s",
                me, outS, err);
        airMopError(mop);
        exit(1);
    }

    airMopOkay(mop);
    return 0;
}