Пример #1
0
int
tenTripleConvert(Nrrd *nout, int dstType,
                 const Nrrd *nin, int srcType) {
  static const char me[]="tenTripleConvert";
  size_t II, NN;
  double (*ins)(void *, size_t, double), (*lup)(const void *, size_t);

  if (!( nout && nin )) {
    biffAddf(TEN, "%s: got NULL pointer", me);
    return 1;
  }
  if ( airEnumValCheck(tenTripleType, dstType) ||
       airEnumValCheck(tenTripleType, srcType) ) {
    biffAddf(TEN, "%s: got invalid %s dst (%d) or src (%d)", me,
             tenTripleType->name, dstType, srcType);
    return 1;
  }
  if (3 != nin->axis[0].size) {
    char stmp[AIR_STRLEN_SMALL];
    biffAddf(TEN, "%s: need axis[0].size 3, not %s", me,
             airSprintSize_t(stmp, nin->axis[0].size));
    return 1;
  }
  if (nrrdTypeBlock == nin->type) {
    biffAddf(TEN, "%s: input has non-scalar %s type",
             me, airEnumStr(nrrdType, nrrdTypeBlock));
    return 1;
  }

  if (nrrdCopy(nout, nin)) {
    biffMovef(TEN, NRRD, "%s: couldn't initialize output", me);
    return 1;
  }
  lup = nrrdDLookup[nin->type];
  ins = nrrdDInsert[nout->type];
  NN = nrrdElementNumber(nin)/3;
  for (II=0; II<NN; II++) {
    double src[3], dst[3];
    src[0] = lup(nin->data, 0 + 3*II);
    src[1] = lup(nin->data, 1 + 3*II);
    src[2] = lup(nin->data, 2 + 3*II);
    tenTripleConvertSingle_d(dst, dstType, src, srcType);
    ins(nout->data, 0 + 3*II, dst[0]);
    ins(nout->data, 1 + 3*II, dst[1]);
    ins(nout->data, 2 + 3*II, dst[2]);
  }

  return 0;
}
Пример #2
0
int
rvaLattSpecConvert(rvaLattSpec *dst, int latt, const rvaLattSpec *src) {
  static const char me[]="rvaLattSpecConvert";
  int nocando;

  if (!(dst && src)) {
    biffAddf(RVA, "%s: got NULL pointer", me);
    return 1;
  }
  if (airEnumValCheck(rvaLatt, latt)) {
    biffAddf(RVA, "%s: %d not valid %s", me, latt, rvaLatt->name);
    return 1;
  }
  if (latt == src->latt) {
    rvaLattSpecCopy(dst, src);
    return 0;
  }
  /* else have work to do */
  if (lattConv(latt, dst->parm, src->latt, src->parm)) {
    dst->latt = rvaLattUnknown;
    biffAddf(RVA, "%s: %s -> %s conversion not implemented", me,
             airEnumStr(rvaLatt, src->latt), airEnumStr(rvaLatt, latt));
    return 1;
  }
  /* else no error; we're done */
  dst->latt = latt;

  return 0;
}
Пример #3
0
/*
******** limnCameraAspectSet
**
** simply sets the "aspect" field of the cam.  Note that calling this
** does *not* automatically mean that the uRange and vRange in the camera
** will be set according to the "fov"- the "fov" has to actually be set
** (be non-NaN) for that to happen.  This allows dumber functions to
** call this whenever they have the information required to do so, even
** if the "aspect" is not going to be needed for a given camera use
*/
int
limnCameraAspectSet(limnCamera *cam, unsigned int horz, unsigned int vert,
                    int centering) {
  static const char me[] = "limnCameraAspectSet";

  if (!cam) {
    biffAddf(LIMN, "%s: got NULL pointer", me);
    return 1;
  }
  if (!( horz > 0 && vert > 0 )) {
    biffAddf(LIMN, "%s: bad image dimensions %ux%u", me, horz, vert);
    return 1;
  }
  if (airEnumValCheck(nrrdCenter, centering)) {
    biffAddf(LIMN, "%s: centering %d not valid", me, centering);
    return 1;
  }

  if (nrrdCenterCell == centering) {
    cam->aspect = ((double)horz)/vert;
  } else {
    cam->aspect = ((double)(horz-1))/(vert-1);
  }

  return 0;
}
Пример #4
0
void
nrrdRangeSet(NrrdRange *range, const Nrrd *nrrd, int blind8BitRange) {
  NRRD_TYPE_BIGGEST _min, _max;
  int blind;
  
  if (!range) {
    return;
  }
  if (nrrd 
      && !airEnumValCheck(nrrdType, nrrd->type)
      && nrrdTypeBlock != nrrd->type) {
    blind = (nrrdBlind8BitRangeTrue == blind8BitRange
             || (nrrdBlind8BitRangeState == blind8BitRange
                 && nrrdStateBlind8BitRange));
    if (blind && 1 == nrrdTypeSize[nrrd->type]) {
      if (nrrdTypeChar == nrrd->type) {
        range->min = SCHAR_MIN;
        range->max = SCHAR_MAX;
      } else {
        range->min = 0;
        range->max = UCHAR_MAX;
      }
      range->hasNonExist = nrrdHasNonExistFalse;
    } else {
      nrrdMinMaxExactFind[nrrd->type](&_min, &_max, &(range->hasNonExist),
                                      nrrd);
      range->min = nrrdDLoad[nrrd->type](&_min);
      range->max = nrrdDLoad[nrrd->type](&_max);
    }
  } else {
    range->min = range->max = AIR_NAN;
    range->hasNonExist = nrrdHasNonExistUnknown;
  }
  return;
}
Пример #5
0
/*
******** limnCameraAspectSet
**
** simply sets the "aspect" field of the cam.  Note that calling this
** does *not* automatically mean that the uRange and vRange in the camera
** will be set according to the "fov"- the "fov" has to actually be set
** (be non-NaN) for that to happen.  This allows dumber functions to
** call this whenever they have the information required to do so, even
** if the "aspect" is not going to be needed for a given camera use
*/
int
limnCameraAspectSet(limnCamera *cam, int horz, int vert, int centering) {
  char me[] = "limnCameraAspectSet", err[BIFF_STRLEN];

  if (!cam) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(LIMN, err); return 1;
  }
  if (!( horz > 0 && vert > 0 )) {
    sprintf(err, "%s: bad image dimensions %dx%d", me, horz, vert);
    biffAdd(LIMN, err); return 1;
  }
  if (airEnumValCheck(nrrdCenter, centering)) {
    sprintf(err, "%s: centering %d not valid", me, centering);
    biffAdd(LIMN, err); return 1;
  }

  if (nrrdCenterCell == centering) {
    cam->aspect = ((double)horz)/vert;
  } else {
    cam->aspect = ((double)(horz-1))/(vert-1);
  }
  
  return 0;
}
Пример #6
0
/*
******** nrrdSpaceSet
**
** What to use to set space, when a value from nrrdSpace enum is known,
** or, to nullify all space-related information when passed nrrdSpaceUnknown
*/
int
nrrdSpaceSet(Nrrd *nrrd, int space) {
  static const char me[]="nrrdSpaceSet";
  unsigned axi, saxi;

  if (!nrrd) {
    biffAddf(NRRD, "%s: got NULL pointer", me);
    return 1;
  }
  if (nrrdSpaceUnknown == space) {
    nrrd->space = nrrdSpaceUnknown;
    nrrd->spaceDim = 0;
    for (axi=0; axi<NRRD_DIM_MAX; axi++) {
      nrrdSpaceVecSetNaN(nrrd->axis[axi].spaceDirection);
    }
    for (saxi=0; saxi<NRRD_SPACE_DIM_MAX; saxi++) {
      airFree(nrrd->spaceUnits[saxi]);
      nrrd->spaceUnits[saxi] = NULL;
    }
    nrrdSpaceVecSetNaN(nrrd->spaceOrigin);
  } else {
    if (airEnumValCheck(nrrdSpace, space)) {
      biffAddf(NRRD, "%s: given space (%d) not valid", me, space);
      return 1;
    }
    nrrd->space = space;
    nrrd->spaceDim = nrrdSpaceDimension(space);
  }
  return 0;
}
Пример #7
0
static int
_nrrdFieldCheck_kinds(const Nrrd *nrrd, int useBiff) {
  static const char me[]="_nrrdFieldCheck_kinds";
  int val[NRRD_DIM_MAX];
  unsigned int wantLen, ai;

  nrrdAxisInfoGet_nva(nrrd, nrrdAxisInfoKind, val);
  for (ai=0; ai<nrrd->dim; ai++) {
    if (!( nrrdKindUnknown == val[ai]
           || !airEnumValCheck(nrrdKind, val[ai]) )) {
      biffMaybeAddf(useBiff, NRRD,
                    "%s: axis %d kind %d invalid", me, ai, val[ai]);
      return 1;
    }
    wantLen = nrrdKindSize(val[ai]);
    if (wantLen && wantLen != nrrd->axis[ai].size) {
      char stmp[AIR_STRLEN_SMALL];
      biffMaybeAddf(useBiff, NRRD,
                    "%s: axis %d kind %s requires size %u, but have %s", me,
                    ai, airEnumStr(nrrdKind, val[ai]), wantLen,
                    airSprintSize_t(stmp, nrrd->axis[ai].size));
      return 1;
    }
  }
  return 0;
}
Пример #8
0
void
tenFiberStopOff(tenFiberContext *tfx, int stop) {

  if (tfx && !airEnumValCheck(tenFiberStop, stop)) {
    tfx->stop = tfx->stop & ~(1 << stop);
  }
  return;
}
Пример #9
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)
{
  char me[]="nrrdAlloc_nva", err[BIFF_STRLEN];
  size_t num;
  int esize;

  if (!(nrrd && size))
  {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(NRRD, err);
    return 1;
  }
  if (airEnumValCheck(nrrdType, type))
  {
    sprintf(err, "%s: type (%d) is invalid", me, type);
    biffAdd(NRRD, err);
    return 1;
  }
  if (nrrdTypeBlock == type)
  {
    if (!(0 < nrrd->blockSize))
    {
      sprintf(err, "%s: given nrrd->blockSize " _AIR_SIZE_T_CNV " invalid",
              me, nrrd->blockSize);
      biffAdd(NRRD, err);
      return 1;
    }
  }
  if (!AIR_IN_CL(1, dim, NRRD_DIM_MAX))
  {
    sprintf(err, "%s: dim (%d) not in valid range [1,%d]",
            me, dim, NRRD_DIM_MAX);
    biffAdd(NRRD, err);
    return 1;
  }

  nrrd->data = airFree(nrrd->data);
  if (nrrdWrap_nva(nrrd, NULL, type, dim, size))
  {
    sprintf(err, "%s:", me);
    biffAdd(NRRD, err);
    return 1 ;
  }
  num = nrrdElementNumber(nrrd);
  esize = nrrdElementSize(nrrd);
  nrrd->data = calloc(num, esize);
  if (!(nrrd->data))
  {
    sprintf(err, "%s: calloc(" _AIR_SIZE_T_CNV ",%d) failed",
            me, num, esize);
    biffAdd(NRRD, err);
    return 1 ;
  }

  return 0;
}
Пример #10
0
int
_nrrdFieldCheck_type(const Nrrd *nrrd, int useBiff) {
  char me[]="_nrrdFieldCheck_type", err[BIFF_STRLEN];
  
  if (airEnumValCheck(nrrdType, nrrd->type)) {
    sprintf(err, "%s: type (%d) is not valid", me, nrrd->type);
    biffMaybeAdd(NRRD, err, useBiff); return 1;
  }
  return 0;
}
Пример #11
0
unsigned int
pullInfoLen(int info) {
    unsigned int ret;

    if (!airEnumValCheck(pullInfo, info)) {
        ret = _pullInfoLen[info];
    } else {
        ret = 0;
    }
    return ret;
}
Пример #12
0
static int
_nrrdFieldCheck_type(const Nrrd *nrrd, int useBiff) {
  static const char me[]="_nrrdFieldCheck_type";

  if (airEnumValCheck(nrrdType, nrrd->type)) {
    biffMaybeAddf(useBiff, NRRD,
                  "%s: type (%d) is not valid", me, nrrd->type);
    return 1;
  }
  return 0;
}
Пример #13
0
/* non-const version of the above */
double *
_gageAnswerPointer(const gageContext *ctx, gagePerVolume *pvl, int item) {
  double *ret;

  AIR_UNUSED(ctx);
  if (pvl && !airEnumValCheck(pvl->kind->enm, item)) {
    ret = pvl->answer + gageKindAnswerOffset(pvl->kind, item);
  } else {
    ret = NULL;
  }
  return ret;
}
Пример #14
0
unsigned int
gageAnswerLength(const gageContext *ctx, const gagePerVolume *pvl, int item) {
  unsigned int ret;
  
  AIR_UNUSED(ctx);
  if (pvl && !airEnumValCheck(pvl->kind->enm, item)) {
    ret = gageKindAnswerLength(pvl->kind, item);
  } else {
    ret = 0;
  }
  return ret;
}
Пример #15
0
void
tenTripleConvertSingle_d(double dst[3], int dstType,
                         const double src[3], const int srcType) {
  static const char me[]="tenTripleConvertSingle_d";
  int direct;

  if (airEnumValCheck(tenTripleType, dstType)
      || airEnumValCheck(tenTripleType, srcType)) {
    /* got invalid source or destination type */
    ELL_3V_SET(dst, AIR_NAN, AIR_NAN, AIR_NAN);
    return;
  }

  if (_convert[dstType][srcType]) {
    /* we have a direct converter */
    _convert[dstType][srcType](dst, src);
    direct = AIR_TRUE;
  } else {
    double eval[3];
    /* else, for lack of anything clever, we convert via evals */
    _convert[tenTripleTypeEigenvalue][srcType](eval, src);
    _convert[dstType][tenTripleTypeEigenvalue](dst, eval);
    direct = AIR_FALSE;
  }

  /* warn if conversion created non-existent values from
     existent input */
  if (ELL_3V_EXISTS(src) && !ELL_3V_EXISTS(dst)) {
    fprintf(stderr, "%s: problem? (%s) %g %g %g <-%s- (%s) %g %g %g\n", me,
            airEnumStr(tenTripleType, dstType),
            dst[0], dst[1], dst[2],
            direct ? "-" : "...",
            airEnumStr(tenTripleType, srcType),
            src[0], src[1], src[2]);
  }

  return;
}
Пример #16
0
unsigned int
gageKindAnswerLength(const gageKind *kind, int item) {
  char me[]="gageKindAnswerLength", *err;

  if (gageKindCheck(kind)) {
    err = biffGetDone(GAGE); 
    fprintf(stderr, "%s: PANIC:\n %s", me, err);
    free(err); exit(1);
  }

  return (!airEnumValCheck(kind->enm, item)
          ? kind->table[item].answerLength
          : 0);
}
Пример #17
0
unsigned int
nrrdCRC32(const Nrrd *nin, int endian) {
  size_t nn;

  /* NULL nrrd or data */
  if (!nin
      || !(nin->data)
      || !(nn = nrrdElementSize(nin)*nrrdElementNumber(nin))
      || airEnumValCheck(airEndian, endian)) {
    return 0;
  }

  return airCRC32(AIR_CAST(const unsigned char *, nin->data),
                  nn, nrrdElementSize(nin),
                  endian == airMyEndian() ? AIR_FALSE : AIR_TRUE);
}
Пример #18
0
int
_nrrdFieldCheck_centers(const Nrrd *nrrd, int useBiff) {
  char me[]="_nrrdFieldCheck_centers", err[BIFF_STRLEN];
  unsigned int ai;
  int val[NRRD_DIM_MAX];

  nrrdAxisInfoGet_nva(nrrd, nrrdAxisInfoCenter, val);
  for (ai=0; ai<nrrd->dim; ai++) {
    if (!( nrrdCenterUnknown == val[ai]
           || !airEnumValCheck(nrrdCenter, val[ai]) )) {
      sprintf(err, "%s: axis %d center %d invalid", me, ai, val[ai]);
      biffMaybeAdd(NRRD, err, useBiff); return 1;
    }
  }
  return 0;
}
Пример #19
0
static int
_nrrdFieldCheck_centers(const Nrrd *nrrd, int useBiff) {
  static const char me[]="_nrrdFieldCheck_centers";
  unsigned int ai;
  int val[NRRD_DIM_MAX];

  nrrdAxisInfoGet_nva(nrrd, nrrdAxisInfoCenter, val);
  for (ai=0; ai<nrrd->dim; ai++) {
    if (!( nrrdCenterUnknown == val[ai]
           || !airEnumValCheck(nrrdCenter, val[ai]) )) {
      biffMaybeAddf(useBiff, NRRD, "%s: axis %d center %d invalid",
                    me, ai, val[ai]);
      return 1;
    }
  }
  return 0;
}
Пример #20
0
int
nrrdArithUnaryOp(Nrrd *nout, int op, const Nrrd *nin) {
  static const char me[]="nrrdArithUnaryOp";
  size_t N, I;
  int size[NRRD_DIM_MAX];
  double (*insert)(void *v, size_t I, double d),
    (*lookup)(const void *v, size_t I), (*uop)(double), val;

  if (!(nout && nin)) {
    biffAddf(NRRD, "%s: got NULL pointer", me);
    return 1;
  }
  if (nrrdTypeBlock == nin->type) {
    biffAddf(NRRD, "%s: can't operate on type %s", me,
             airEnumStr(nrrdType, nrrdTypeBlock));
    return 1;
  }
  if (airEnumValCheck(nrrdUnaryOp, op)) {
    biffAddf(NRRD, "%s: unary op %d invalid", me, op);
    return 1;
  }
  if (nout != nin) {
    if (nrrdCopy(nout, nin)) {
      biffAddf(NRRD, "%s:", me);
      return 1;
    }
  }
  nrrdAxisInfoGet_nva(nin, nrrdAxisInfoSize, size);
  uop = _nrrdUnaryOp[op];

  N = nrrdElementNumber(nin);
  lookup = nrrdDLookup[nin->type];
  insert = nrrdDInsert[nin->type];
  for (I=0; I<N; I++) {
    val = lookup(nin->data, I);
    insert(nout->data, I, uop(val));
  }
  if (nrrdContentSet_va(nout, airEnumStr(nrrdUnaryOp, op), nin, "")) {
    biffAddf(NRRD, "%s:", me);
    return 1;
  }
  nrrdBasicInfoInit(nout,
                    NRRD_BASIC_INFO_ALL ^ (NRRD_BASIC_INFO_OLDMIN_BIT
                                           | NRRD_BASIC_INFO_OLDMAX_BIT));
  return 0;
}
Пример #21
0
/*
******** nrrdElementSize()
**
** So just how many bytes long is one element in this nrrd?  This is
** needed (over the simple nrrdTypeSize[] array) because some nrrds
** may be of "block" type, and because it does bounds checking on
** nrrd->type.  Returns 0 if given a bogus nrrd->type, or if the block
** size isn't greater than zero (in which case it sets nrrd->blockSize
** to 0, just out of spite).  This function never returns a negative
** value; using (!nrrdElementSize(nrrd)) is a sufficient check for
** invalidity.
**
** Besides learning how many bytes long one element is, this function
** is useful as a way of detecting an invalid blocksize on a block nrrd.
*/
size_t
nrrdElementSize (const Nrrd *nrrd) {

  if (!( nrrd && !airEnumValCheck(nrrdType, nrrd->type) )) {
    return 0;
  }
  if (nrrdTypeBlock != nrrd->type) {
    return nrrdTypeSize[nrrd->type];
  }
  /* else its block type */
  if (nrrd->blockSize > 0) {
    return nrrd->blockSize;
  }
  /* else we got an invalid block size */
  /* nrrd->blockSize = 0; */
  return 0;
}
Пример #22
0
limnSplineTypeSpec *
limnSplineTypeSpecNew(int type, ...) {
  char me[]="limnSplineTypeSpecNew", err[BIFF_STRLEN];
  limnSplineTypeSpec *spec;
  va_list ap;

  if (airEnumValCheck(limnSplineType, type)) {
    sprintf(err, "%s: given type %d not a valid limnSplineType", me, type);
    biffAdd(LIMN, err); return NULL;
  }
  spec = (limnSplineTypeSpec *)calloc(1, sizeof(limnSplineTypeSpec));
  spec->type = type;
  va_start(ap, type);
  if (limnSplineTypeBC == type) {
    spec->B = va_arg(ap, double);
    spec->C = va_arg(ap, double);
  }
Пример #23
0
/*
******** nrrdHasNonExist
**
** returns the nrrdHasNonExist* enum value appropriate for a given nrrd.
** By cleverness, this value can be used as a regular C boolean, so that
** the function will act as you expect.  
**
** (the existence of this function implies that I'll never have an airEnum
** of the same name, which would be the usual thing to do with a C enum,
** but I don't think an airEnum for this would be useful)
*/
int
nrrdHasNonExist(const Nrrd *nrrd) {
  NRRD_TYPE_BIGGEST _min, _max;
  int ret;
  
  if (nrrd
      && !airEnumValCheck(nrrdType, nrrd->type)
      && nrrdTypeBlock != nrrd->type) {
    if (nrrdTypeIsIntegral[nrrd->type]) {
      ret = nrrdHasNonExistFalse;
    } else {
      /* HEY: this could be optimized by being more specialized */
      nrrdMinMaxExactFind[nrrd->type](&_min, &_max, &ret, nrrd);
    }
  } else {
    ret = nrrdHasNonExistUnknown;
  }
  return ret;
}
Пример #24
0
/*
******** airEnumFmtDesc()
**
** Formats a description line for one element "val" of airEnum "enm",
** and puts the result in a NEWLY ALLOCATED string which is the return
** of this function.  The formatting is done via sprintf(), as governed
** by "fmt", which should contain to "%s" conversion sequences, the
** first for the string version "val", and the second for the
** description If "canon", then the canonical string representation
** will be used (the one in enm->str[]), otherwise the shortest string
** representation will be used (which differs from the canonical one
** when there is a strEqv[]/valEqv[] pair defining a shorter string)
*/
char *
airEnumFmtDesc(const airEnum *enm, int val, int canon, const char *fmt) {
  const char *desc;
  char *buff, ident[AIR_STRLEN_SMALL];
  const char *_ident;
  int i;
  size_t len;

  if (!(enm && enm->desc && fmt)) {
    return airStrdup("(airEnumDesc: invalid args)");
  }
  if (airEnumValCheck(enm, val)) {
    val = airEnumUnknown(enm);
  }
  _ident = airEnumStr(enm, val);
  if (!canon && enm->strEqv) {
    len = airStrlen(_ident);
    for (i=0; airStrlen(enm->strEqv[i]); i++) {
      if (val != enm->valEqv[i]) {
        /* this isn't a string representing the value we care about */
        continue;
      }
      if (airStrlen(enm->strEqv[i]) < len) {
        /* this one is shorter */
        len = airStrlen(enm->strEqv[i]);
        _ident = enm->strEqv[i];
      }
    }
  }
  strncpy(ident, _ident, AIR_STRLEN_SMALL);
  ident[AIR_STRLEN_SMALL-1] = '\0';
  if (!enm->sense) {
    airToLower(ident);
  }
  desc = enm->desc[_airEnumIndex(enm, val)];
  buff = AIR_CALLOC(airStrlen(fmt) + airStrlen(ident) +
                    airStrlen(desc) + 1, char);
  if (buff) {
    sprintf(buff, fmt, ident, desc);
  }
  return buff;
}
Пример #25
0
int
pullInfoGet(Nrrd *ninfo, int info, pullContext *pctx) {
    static const char me[]="pullInfoGet";
    size_t size[2];
    unsigned int dim, pointNum, pointIdx, binIdx, outIdx, alen, aidx;
    double *out_d;
    pullBin *bin;
    pullPoint *point;

    if (airEnumValCheck(pullInfo, info)) {
        biffAddf(PULL, "%s: info %d not valid", me, info);
        return 1;
    }
    pointNum = pullPointNumber(pctx);
    alen = pullInfoLen(info);
    aidx = pctx->infoIdx[info];
    if (1 == alen) {
        dim = 1;
        size[0] = pointNum;
    } else {
        dim = 2;
        size[0] = alen;
        size[1] = pointNum;
    }
    if (nrrdMaybeAlloc_nva(ninfo, nrrdTypeDouble, dim, size)) {
        biffMovef(PULL, NRRD, "%s: trouble allocating output", me);
        return 1;
    }
    out_d = AIR_CAST(double *, ninfo->data);

    outIdx = 0;
    for (binIdx=0; binIdx<pctx->binNum; binIdx++) {
        bin = pctx->bin + binIdx;
        for (pointIdx=0; pointIdx<bin->pointNum; pointIdx++) {
            point = bin->point[pointIdx];
            _pullInfoCopy[alen](out_d + outIdx, point->info + aidx);
            outIdx += alen;
        }
    }

    return 0;
}
Пример #26
0
int
gageQueryItemOn(gageContext *ctx, gagePerVolume *pvl, int item) {
  char me[]="gageQueryItemOn", err[BIFF_STRLEN];

  if (!( pvl )) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(GAGE, err); return 1;
  }

  if (airEnumValCheck(pvl->kind->enm, item)) {
    sprintf(err, "%s: %d not a valid %s value", me,
            item, pvl->kind->enm->name);
    biffAdd(GAGE, err); return 1;
  }
  GAGE_QUERY_ITEM_ON(pvl->query, item);
  if (gageQuerySet(ctx, pvl, pvl->query)) {
    sprintf(err, "%s: trouble", me);
    biffAdd(GAGE, err); return 1;
  }

  return 0;
}
Пример #27
0
int
_nrrdFieldCheck_kinds(const Nrrd *nrrd, int useBiff) {
  char me[]="_nrrdFieldCheck_kinds", err[BIFF_STRLEN];
  int val[NRRD_DIM_MAX];
  unsigned int wantLen, ai;

  nrrdAxisInfoGet_nva(nrrd, nrrdAxisInfoKind, val);
  for (ai=0; ai<nrrd->dim; ai++) {
    if (!( nrrdKindUnknown == val[ai]
           || !airEnumValCheck(nrrdKind, val[ai]) )) {
      sprintf(err, "%s: axis %d kind %d invalid", me, ai, val[ai]);
      biffMaybeAdd(NRRD, err, useBiff); return 1;
    }
    wantLen = nrrdKindSize(val[ai]);
    if (wantLen && wantLen != nrrd->axis[ai].size) {
      sprintf(err, "%s: axis %d kind %s requires size %d, but have "
              _AIR_SIZE_T_CNV, me,
              ai, airEnumStr(nrrdKind, val[ai]), wantLen, nrrd->axis[ai].size);
      biffMaybeAdd(NRRD, err, useBiff); return 1;
    }
  }
  return 0;
}
Пример #28
0
/*
** asserts all the properties associated with orientation information
**
** The most important part of this is asserting the per-axis mutual
** exclusion of min/max/spacing/units versus using spaceDirection.
*/
static int
_nrrdFieldCheckSpaceInfo(const Nrrd *nrrd, int useBiff) {
  static const char me[]="_nrrdFieldCheckSpaceInfo";
  unsigned int dd, ii;
  int exists;

  if (!( !nrrd->space || !airEnumValCheck(nrrdSpace, nrrd->space) )) {
    biffMaybeAddf(useBiff, NRRD, "%s: space %d invalid",
                  me, nrrd->space);
    return 1;
  }
  if (!( nrrd->spaceDim <= NRRD_SPACE_DIM_MAX )) {
    biffMaybeAddf(useBiff, NRRD, "%s: space dimension %d is outside "
                  "valid range [0,NRRD_SPACE_DIM_MAX] = [0,%d]",
                  me, nrrd->dim, NRRD_SPACE_DIM_MAX);
    return 1;
  }
  if (nrrd->spaceDim) {
    if (nrrd->space) {
      if (nrrdSpaceDimension(nrrd->space) != nrrd->spaceDim) {
        biffMaybeAddf(useBiff, NRRD,
                      "%s: space %s has dimension %d but spaceDim is %d",
                      me, airEnumStr(nrrdSpace, nrrd->space),
                      nrrdSpaceDimension(nrrd->space), nrrd->spaceDim);
        return 1;
      }
    }
    /* check that all coeffs of spaceOrigin have consistent existance */
    exists = AIR_EXISTS(nrrd->spaceOrigin[0]);
    for (ii=0; ii<nrrd->spaceDim; ii++) {
      if (exists ^ AIR_EXISTS(nrrd->spaceOrigin[ii])) {
        biffMaybeAddf(useBiff, NRRD,
                      "%s: existance of space origin coefficients must "
                      "be consistent (val[0] not like val[%d])", me, ii);
        return 1;
      }
    }
    /* check that all coeffs of measurementFrame have consistent existance */
    exists = AIR_EXISTS(nrrd->measurementFrame[0][0]);
    for (dd=0; dd<nrrd->spaceDim; dd++) {
      for (ii=0; ii<nrrd->spaceDim; ii++) {
        if (exists ^ AIR_EXISTS(nrrd->measurementFrame[dd][ii])) {
          biffMaybeAddf(useBiff, NRRD,
                        "%s: existance of measurement frame coefficients "
                        "must be consistent: [col][row] [%d][%d] not "
                        "like [0][0])", me, dd, ii);
          return 1;
        }
      }
    }
    /* check on space directions */
    for (dd=0; dd<nrrd->dim; dd++) {
      exists = AIR_EXISTS(nrrd->axis[dd].spaceDirection[0]);
      for (ii=1; ii<nrrd->spaceDim; ii++) {
        if (exists ^ AIR_EXISTS(nrrd->axis[dd].spaceDirection[ii])) {
          biffMaybeAddf(useBiff, NRRD,
                        "%s: existance of space direction %d coefficients "
                        "must be consistent (val[0] not like val[%d])", me,
                        dd, ii); return 1;
        }
      }
      if (exists) {
        if (AIR_EXISTS(nrrd->axis[dd].min)
            || AIR_EXISTS(nrrd->axis[dd].max)
            || AIR_EXISTS(nrrd->axis[dd].spacing)
            || !!airStrlen(nrrd->axis[dd].units)) {
          biffMaybeAddf(useBiff, NRRD,
                        "%s: axis[%d] has a direction vector, and so can't "
                        "have min, max, spacing, or units set", me, dd);
          return 1;
        }
      }
    }
  } else {
    /* else there's not supposed to be anything in "space" */
    if (nrrd->space) {
      biffMaybeAddf(useBiff, NRRD,
                    "%s: space %s can't be set with spaceDim %d",
                    me, airEnumStr(nrrdSpace, nrrd->space),
                    nrrd->spaceDim);
      return 1;
    }
    /* -------- */
    exists = AIR_FALSE;
    for (dd=0; dd<NRRD_SPACE_DIM_MAX; dd++) {
      exists |= !!airStrlen(nrrd->spaceUnits[dd]);
    }
    if (exists) {
      biffMaybeAddf(useBiff, NRRD,
                    "%s: spaceDim is 0, but space units is set", me);
      return 1;
    }
    /* -------- */
    exists = AIR_FALSE;
    for (dd=0; dd<NRRD_SPACE_DIM_MAX; dd++) {
      exists |= AIR_EXISTS(nrrd->spaceOrigin[dd]);
    }
    if (exists) {
      biffMaybeAddf(useBiff, NRRD,
                    "%s: spaceDim is 0, but space origin is set", me);
      return 1;
    }
    /* -------- */
    exists = AIR_FALSE;
    for (dd=0; dd<NRRD_SPACE_DIM_MAX; dd++) {
      for (ii=0; ii<NRRD_DIM_MAX; ii++) {
        exists |= AIR_EXISTS(nrrd->axis[ii].spaceDirection[dd]);
      }
    }
    if (exists) {
      biffMaybeAddf(useBiff, NRRD,
                    "%s: spaceDim is 0, but space directions are set", me);
      return 1;
    }
  }
  return 0;
}
Пример #29
0
/*
****** nrrdSanity
**
** makes sure that all the basic assumptions of nrrd hold for
** the architecture/etc which we're currently running on.
**
** returns 1 if all is okay, 0 if there is a problem
*/
int /*Teem: biff if (!ret) */
nrrdSanity(void) {
  static const char me[]="nrrdSanity";
  int aret, type;
  size_t maxsize;
  airLLong tmpLLI;
  airULLong tmpULLI;
  static int _nrrdSanity = 0;

  if (_nrrdSanity) {
    /* we've been through this once before and things looked okay ... */
    /* Is this thread-safe?  I think so.  If we assume that any two
       threads are going to compute the same value, isn't it the case
       that, at worse, both of them will go through all the tests and
       then set _nrrdSanity to the same thing? */
    return 1;
  }

  aret = airSanity();
  if (aret != airInsane_not) {
    biffAddf(NRRD, "%s: airSanity() failed: %s", me,
             airInsaneErr(aret));
    return 0;
  }


  if (airEnumValCheck(nrrdEncodingType, nrrdDefaultWriteEncodingType)) {
    biffAddf(NRRD,
             "%s: nrrdDefaultWriteEncodingType (%d) not in valid "
             "range [%d,%d]", me, nrrdDefaultWriteEncodingType,
             nrrdEncodingTypeUnknown+1, nrrdEncodingTypeLast-1);
    return 0;
  }
  if (airEnumValCheck(nrrdCenter, nrrdDefaultCenter)) {
    biffAddf(NRRD,
             "%s: nrrdDefaultCenter (%d) not in valid range [%d,%d]",
             me, nrrdDefaultCenter,
             nrrdCenterUnknown+1, nrrdCenterLast-1);
    return 0;
  }

  if (!( nrrdTypeSize[nrrdTypeChar] == sizeof(char)
         && nrrdTypeSize[nrrdTypeUChar] == sizeof(unsigned char)
         && nrrdTypeSize[nrrdTypeShort] == sizeof(short)
         && nrrdTypeSize[nrrdTypeUShort] == sizeof(unsigned short)
         && nrrdTypeSize[nrrdTypeInt] == sizeof(int)
         && nrrdTypeSize[nrrdTypeUInt] == sizeof(unsigned int)
         && nrrdTypeSize[nrrdTypeLLong] == sizeof(airLLong)
         && nrrdTypeSize[nrrdTypeULLong] == sizeof(airULLong)
         && nrrdTypeSize[nrrdTypeFloat] == sizeof(float)
         && nrrdTypeSize[nrrdTypeDouble] == sizeof(double) )) {
    biffAddf(NRRD, "%s: sizeof() for nrrd types has problem: "
             "expected (%u,%u,%u,%u,%u,%u,%u,%u,%u,%u) "
             "but got (%u,%u,%u,%u,%u,%u,%u,%u,%u,%u)", me,
             AIR_CAST(unsigned int, nrrdTypeSize[nrrdTypeChar]),
             AIR_CAST(unsigned int, nrrdTypeSize[nrrdTypeUChar]),
             AIR_CAST(unsigned int, nrrdTypeSize[nrrdTypeShort]),
             AIR_CAST(unsigned int, nrrdTypeSize[nrrdTypeUShort]),
             AIR_CAST(unsigned int, nrrdTypeSize[nrrdTypeInt]),
             AIR_CAST(unsigned int, nrrdTypeSize[nrrdTypeUInt]),
             AIR_CAST(unsigned int, nrrdTypeSize[nrrdTypeLLong]),
             AIR_CAST(unsigned int, nrrdTypeSize[nrrdTypeULLong]),
             AIR_CAST(unsigned int, nrrdTypeSize[nrrdTypeFloat]),
             AIR_CAST(unsigned int, nrrdTypeSize[nrrdTypeDouble]),
             AIR_CAST(unsigned int, sizeof(char)),
             AIR_CAST(unsigned int, sizeof(unsigned char)),
             AIR_CAST(unsigned int, sizeof(short)),
             AIR_CAST(unsigned int, sizeof(unsigned short)),
             AIR_CAST(unsigned int, sizeof(int)),
             AIR_CAST(unsigned int, sizeof(unsigned int)),
             AIR_CAST(unsigned int, sizeof(airLLong)),
             AIR_CAST(unsigned int, sizeof(airULLong)),
             AIR_CAST(unsigned int, sizeof(float)),
             AIR_CAST(unsigned int, sizeof(double)));
    return 0;
  }
Пример #30
0
int
gageDeconvolve(Nrrd *_nout, double *lastDiffP,
               const Nrrd *nin, const gageKind *kind,
               const NrrdKernelSpec *ksp, int typeOut,
               unsigned int maxIter, int saveAnyway,
               double step, double epsilon, int verbose) {
  static const char me[]="gageDeconvolve";
  gageContext *ctx[2];
  gagePerVolume *pvl[2];
  double *out[2], *val[2], alpha, (*lup)(const void *, size_t), meandiff=0;
  const double *ans[2];
  Nrrd *nout[2];
  airArray *mop;
  unsigned int sx, sy, sz, xi, yi, zi, anslen, thiz=0, last, inIdx, iter;
  int E, valItem;

  if (!(_nout && lastDiffP && nin && kind && ksp)) {
    biffAddf(GAGE, "%s: got NULL pointer", me);
    return 1;
  }
  if (!(nrrdTypeDefault == typeOut
        || !airEnumValCheck(nrrdType, typeOut))) {
    biffAddf(GAGE, "%s: typeOut %d not valid", me, typeOut);
    return 1;
  }
  if (!( maxIter >= 1 )) {
    biffAddf(GAGE, "%s: need maxIter >= 1 (not %u)", me, maxIter);
    return 1;
  }
  if (!( epsilon >= 0 )) {
    biffAddf(GAGE, "%s: need epsilon >= 0.0 (not %g)", me, epsilon);
    return 1;
  }

  /* this once changed from 0 to 1, but is unlikely to change again */
  valItem = 1;

  mop = airMopNew();
  for (iter=0; iter<2; iter++) {
    nout[iter] = nrrdNew();
    airMopAdd(mop, nout[iter], (airMopper)nrrdNuke, airMopAlways);
    if (nrrdConvert(nout[iter], nin, nrrdTypeDouble)) {
      biffMovef(GAGE, NRRD, "%s: couldn't allocate working buffer %u",
                me, iter);
      airMopError(mop); return 1;
    }
    ctx[iter] = gageContextNew();
    airMopAdd(mop, ctx[iter], (airMopper)gageContextNix, airMopAlways);
    E = 0;
    if (!E) E |= !(pvl[iter] = gagePerVolumeNew(ctx[iter], nout[iter], kind));
    if (!E) E |= gagePerVolumeAttach(ctx[iter], pvl[iter]);
    if (!E) E |= gageKernelSet(ctx[iter], gageKernel00,
                               ksp->kernel, ksp->parm);
    if (!E) E |= gageQueryItemOn(ctx[iter], pvl[iter], valItem);
    if (!E) E |= gageUpdate(ctx[iter]);
    if (E) {
      biffAddf(GAGE, "%s: trouble setting up context %u", me, iter);
      airMopError(mop); return 1;
    }
    out[iter] = AIR_CAST(double*, nout[iter]->data);
    ans[iter] = gageAnswerPointer(ctx[iter], pvl[iter], valItem);
  }

  anslen = kind->table[valItem].answerLength;
  lup = nrrdDLookup[nin->type];

  alpha = ksp->kernel->eval1_d(0.0, ksp->parm);
  sx = ctx[0]->shape->size[0];
  sy = ctx[0]->shape->size[1];
  sz = ctx[0]->shape->size[2];

  for (iter=0; iter<maxIter; iter++) {
    thiz = (iter+1) % 2;
    last = (iter+0) % 2;
    val[thiz] = out[thiz];
    val[last] = out[last];
    inIdx = 0;
    meandiff = 0;
    for (zi=0; zi<sz; zi++) {
      for (yi=0; yi<sy; yi++) {
        for (xi=0; xi<sx; xi++) {
          unsigned int ai;
          double in, aa;
          gageProbe(ctx[last], xi, yi, zi);
          for (ai=0; ai<anslen; ai++) {
            in = lup(nin->data, ai + anslen*inIdx);
            aa = ans[last][ai];
            val[thiz][ai] = val[last][ai] + step*(in - aa)/alpha;
            meandiff += 2*(in - aa)*(in - aa)/(DBL_EPSILON + in*in + aa*aa);
          }
          val[thiz] += anslen;
          val[last] += anslen;
          ++inIdx;
        }
      }
    }
    meandiff /= sx*sy*sz;
    if (verbose) {
      fprintf(stderr, "%s: iter %u meandiff = %g\n", me, iter, meandiff);
    }
    if (meandiff < epsilon) {
      /* we have indeed converged while iter < maxIter */
      break;
    }
  }
  if (iter == maxIter) {
    if (!saveAnyway) {
      biffAddf(GAGE, "%s: failed to converge in %u iterations, meandiff = %g",
               me, maxIter, meandiff);
      airMopError(mop); return 1;
    } else {
      if (verbose) {
        fprintf(stderr, "%s: at maxIter %u; err %g still > thresh %g\n", me,
                iter, meandiff, epsilon);
      }
    }
  }

  if (nrrdClampConvert(_nout, nout[thiz], (nrrdTypeDefault == typeOut
                                           ? nin->type
                                           : typeOut))) {
    biffAddf(GAGE, "%s: couldn't create output", me);
    airMopError(mop); return 1;
  }
  *lastDiffP = meandiff;

  airMopOkay(mop);
  return 0;
}