Exemplo n.º 1
0
/*
******** nrrdSimpleCrop()
**
*/
int
nrrdSimpleCrop(Nrrd *nout, const Nrrd *nin, unsigned int crop) {
  char me[]="nrrdSimpleCrop", err[BIFF_STRLEN];
  unsigned int ai;
  size_t min[NRRD_DIM_MAX], max[NRRD_DIM_MAX];

  if (!(nout && nin)) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(NRRD, err); return 1;
  }
  for (ai=0; ai<nin->dim; ai++) {
    min[ai] = crop;
    max[ai] = nin->axis[ai].size-1 - crop;
  }
  if (nrrdCrop(nout, nin, min, max)) {
    sprintf(err, "%s:", me);
    biffAdd(NRRD, err); return 1;
  }
  return 0;
}
Exemplo n.º 2
0
int
unrrdu_cropMain(int argc, char **argv, char *me, hestParm *hparm) {
  hestOpt *opt = NULL;
  char *out, *err;
  Nrrd *nin, *nout;
  unsigned int ai;
  int minLen, maxLen, pret;
  long int *minOff, *maxOff;
  size_t min[NRRD_DIM_MAX], max[NRRD_DIM_MAX];
  airArray *mop;

  OPT_ADD_BOUND("min,minimum", minOff,
                "low corner of bounding box.\n "
                "\b\bo <int> gives 0-based index\n "
                "\b\bo M, M+<int>, M-<int> give index relative "
                "to the last sample on the axis (M == #samples-1).",
                minLen);
  OPT_ADD_BOUND("max,maximum", maxOff, "high corner of bounding box.  Besides "
                "the specification styles described above, there's also:\n "
                "\b\bo m+<int> give index relative to minimum.",
                maxLen);
  OPT_ADD_NIN(nin, "input nrrd");
  OPT_ADD_NOUT(out, "output nrrd");

  mop = airMopNew();
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);

  USAGE(_unrrdu_cropInfoL);
  PARSE();
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);

  if (!( minLen == (int)nin->dim && maxLen == (int)nin->dim )) {
    fprintf(stderr,
            "%s: # min coords (%d) or max coords (%d) != nrrd dim (%d)\n",
            me, minLen, maxLen, nin->dim);
    airMopError(mop);
    return 1;
  }
  for (ai=0; ai<nin->dim; ai++) {
    if (-1 == minOff[0 + 2*ai]) {
      fprintf(stderr, "%s: can't use m+<int> specification for axis %d min\n",
              me, ai);
      airMopError(mop);
      return 1;
    }
  }
  for (ai=0; ai<nin->dim; ai++) {
    min[ai] = minOff[0 + 2*ai]*(nin->axis[ai].size-1) + minOff[1 + 2*ai];
    if (-1 == maxOff[0 + 2*ai]) {
      max[ai] = min[ai] + maxOff[1 + 2*ai];
    } else {
      max[ai] = maxOff[0 + 2*ai]*(nin->axis[ai].size-1) + maxOff[1 + 2*ai];
    }
    /*
    fprintf(stderr, "%s: ai %2d: min = %4d, max = %4d\n",
            me, ai, min[ai], max[ai]);
    */
  }

  nout = nrrdNew();
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);

  if (nrrdCrop(nout, nin, min, max)) {
    airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
    fprintf(stderr, "%s: error cropping nrrd:\n%s", me, err);
    airMopError(mop);
    return 1;
  }

  SAVE(out, nout, NULL);

  airMopOkay(mop);
  return 0;
}
Exemplo n.º 3
0
/*
******** nrrd1DIrregMapCheck()
**
** return zero only for the valid forms of 1D irregular map.
** imap must be 2D, both sizes >= 2, non-block-type, no non-existant
** values in range.  If the first point's position is non-existant,
** than the first three points positions must be -inf, NaN, and +inf,
** and none of the other points locations can be non-existant, and
** they must increase monotonically.  There must be at least two
** points with existant positions.
*/
int
nrrd1DIrregMapCheck(const Nrrd *nmap) {
  char me[]="nrrd1DIrregMapCheck", err[BIFF_STRLEN];
  double (*mapLup)(const void *v, size_t I);
  int i, entLen, mapLen, baseI;
  size_t min[2], max[2];
  Nrrd *nrange;

  if (!nmap) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(NRRD, err); return 1;
  }
  if (nrrdCheck(nmap)) {
    sprintf(err, "%s: ", me);
    biffAdd(NRRD, err); return 1;
  }
  if (nrrdTypeBlock == nmap->type) {
    sprintf(err, "%s: map is %s type, need scalar", 
            me, airEnumStr(nrrdType, nrrdTypeBlock));
    biffAdd(NRRD, err); return 1;
  }
  if (2 != nmap->dim) {
    sprintf(err, "%s: map needs to have dimension 2, not %d", me, nmap->dim);
    biffAdd(NRRD, err); return 1;
  }
  entLen = nmap->axis[0].size;
  mapLen = nmap->axis[1].size;
  if (!( entLen >= 2 && mapLen >= 2 )) {
    sprintf(err, "%s: both map's axes sizes should be >= 2 (not %d,%d)",
            me, entLen, mapLen);
    biffAdd(NRRD, err); return 1;
  }
  min[0] = 1; max[0] = nmap->axis[0].size-1;
  min[1] = 0; max[1] = nmap->axis[1].size-1;
  if (nrrdCrop(nrange=nrrdNew(), nmap, min, max)) {
    sprintf(err, "%s: couldn't crop to isolate range of map", me);
    biffAdd(NRRD, err); nrrdNuke(nrange); return 1;
  }
  if (nrrdHasNonExist(nrange)) {
    sprintf(err, "%s: map has non-existent values in its range", me);
    biffAdd(NRRD, err); nrrdNuke(nrange); return 1;
  }
  nrrdNuke(nrange);
  mapLup = nrrdDLookup[nmap->type];
  if (AIR_EXISTS(mapLup(nmap->data, 0))) {
    baseI = 0;
  } else {
    baseI = 3;
    if (!( mapLen >= 5 )) {
      sprintf(err, "%s: length of map w/ non-existant locations must "
              "be >= 5 (not %d)", me, mapLen);
      biffAdd(NRRD, err); return 1;
    }
    if (!( airFP_NEG_INF == airFPClass_d(mapLup(nmap->data, 0*entLen)) &&
           airFP_QNAN    == airFPClass_d(mapLup(nmap->data, 1*entLen)) &&
           airFP_POS_INF == airFPClass_d(mapLup(nmap->data, 2*entLen)) )) {
      sprintf(err, "%s: 1st entry's position non-existant, but position "
              "of 1st three entries not -inf, NaN, and +inf", me);
      biffAdd(NRRD, err); return 1;
    }
  }
  for (i=baseI; i<mapLen; i++) {
    if (!AIR_EXISTS(mapLup(nmap->data, i*entLen))) {
      sprintf(err, "%s: entry %d has non-existant position", me, i);
      biffAdd(NRRD, err); return 1;
    }
  }
  for (i=baseI; i<mapLen-1; i++) {
    if (!( mapLup(nmap->data, i*entLen) < mapLup(nmap->data, (i+1)*entLen) )) {
      sprintf(err, "%s: map entry %d pos (%g) not < entry %d pos (%g)",
              me, i, mapLup(nmap->data, i*entLen),
              i+1, mapLup(nmap->data, (i+1)*entLen));
      biffAdd(NRRD, err); return 1;
    }
  }
  return 0;
}