/*
******** 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;
}
Example #2
0
int
tkwbReadTemplate(char **tmplSP, char *filename) {
    char me[]="tkwbReadTemplate", err[BIFF_STRLEN];
    FILE *file;
    airArray *mop;

    mop = airMopNew();
    if (!( file = airFopen(filename, stdin, "rb") )) {
        sprintf(err, "%s: couldn't open %s: %s", me, filename, strerror(errno));
        biffAdd(TKWB, err);
        airMopError(mop);
        return 1;
    }
    airMopAdd(mop, file, (airMopper)airFclose, airMopAlways);

    if (tkwbReadFileToString(tmplSP, NULL, file, NULL)) {
        sprintf(err, "%s: couldn't read in template file %s", me, filename);
        biffAdd(TKWB, err);
        airMopError(mop);
        return 1;
    }

    airMopOkay(mop);
    return 0;
}
Example #3
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) {
  char me[]="nrrdSpaceSet", err[BIFF_STRLEN];
  unsigned axi, saxi;
  
  if (!nrrd) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(NRRD, err); 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)) {
      sprintf(err, "%s: given space (%d) not valid", me, space);
      biffAdd(NRRD, err); return 1;
    }
    nrrd->space = space;
    nrrd->spaceDim = nrrdSpaceDimension(space);
  }
  return 0;
}
Example #4
0
int
nrrdIoStateEncodingSet(NrrdIoState *nio, const NrrdEncoding *encoding)
{
    char me[]="nrrdIoStateEncodingSet", err[BIFF_STRLEN];

    if (!( nio && encoding ))
    {
        sprintf(err, "%s: got NULL pointer", me);
        if (nio)
        {
            nio->encoding = nrrdEncodingUnknown;
        }
        biffAdd(NRRD, err);
        return 1;
    }
    if (!encoding->available())
    {
        sprintf(err, "%s: %s encoding isn't actually available", me,
                encoding->name);
        nio->encoding = nrrdEncodingUnknown;
        biffAdd(NRRD, err);
        return 1;
    }
    nio->encoding = encoding;
    return 0;
}
Example #5
0
int
_nrrdContentSet_nva(Nrrd *nout, const char *func,
                    char *content, const char *format, va_list arg) {
  char me[]="_nrrdContentSet_nva", err[BIFF_STRLEN], *buff;

  buff = (char *)malloc(128*AIR_STRLEN_HUGE);
  if (!buff) {
    sprintf(err, "%s: couln't alloc buffer!", me);
    biffAdd(NRRD, err); return 1;
  }
  nout->content = (char *)airFree(nout->content);

  /* we are currently praying that this won't overflow the "buff" array */
  /* HEY: replace with vsnprintf or whatever when its available */
  vsprintf(buff, format, arg);

  nout->content = (char *)calloc(strlen("(,)")
                                 + airStrlen(func)
                                 + 1                      /* '(' */
                                 + airStrlen(content)
                                 + 1                      /* ',' */
                                 + airStrlen(buff)
                                 + 1                      /* ')' */
                                 + 1, sizeof(char));      /* '\0' */
  if (!nout->content) {
    sprintf(err, "%s: couln't alloc output content!", me);
    biffAdd(NRRD, err); airFree(buff); return 1;
  }
  sprintf(nout->content, "%s(%s%s%s)", func, content,
          airStrlen(buff) ? "," : "", buff);
  airFree(buff);  /* no NULL assignment, else compile warnings */
  return 0;
}
Example #6
0
/*
******** nrrdContentSet
**
** Kind of like sprintf, but for the content string of the nrrd.
**
** Whether or not we write a new content for an old nrrd ("nin") with
** NULL content is decided here, according to
** nrrdStateAlwaysSetContent.
**
** Does the string allocation and some attempts at error detection.
** Does allow nout==nin, which requires some care.
*/
int
nrrdContentSet_va(Nrrd *nout, const char *func,
                  const Nrrd *nin, const char *format, ...) {
  char me[]="nrrdContentSet_va", err[BIFF_STRLEN];
  va_list ap;
  char *content;
  
  if (!(nout && func && nin && format)) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(NRRD, err); return 1;
  }
  if (nrrdStateDisableContent) {
    /* we kill content always */
    nout->content = (char *)airFree(nout->content);
    return 0;
  }
  if (!nin->content && !nrrdStateAlwaysSetContent) {
    /* there's no input content, and we're not supposed to invent any
       content, so after freeing nout's content we're done */
    nout->content = (char *)airFree(nout->content);
    return 0;
  }
  /* we copy the input nrrd content first, before blowing away the
     output content, in case nout == nin */
  content = _nrrdContentGet(nin);
  va_start(ap, format);
  if (_nrrdContentSet_nva(nout, func, content, format, ap)) {
    sprintf(err, "%s:", me);
    biffAdd(NRRD, err); va_end(ap); free(content); return 1;
  }
  va_end(ap);
  free(content); 

  return 0;
}
Example #7
0
/*
******** nrrdSample_nva()
**
** given coordinates within a nrrd, copies the 
** single element into given *val
*/
int
nrrdSample_nva(void *val, const Nrrd *nrrd, const size_t *coord) {
  char me[]="nrrdSample_nva", err[BIFF_STRLEN];
  size_t I, size[NRRD_DIM_MAX], typeSize;
  unsigned int ai;
  
  if (!(nrrd && coord && val)) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(NRRD, err); return 1;
  }
  /* this shouldn't actually be necessary .. */
  if (!nrrdElementSize(nrrd)) {
    sprintf(err, "%s: nrrd reports zero element size!", me);
    biffAdd(NRRD, err); return 1;
  }
  
  typeSize = nrrdElementSize(nrrd);
  nrrdAxisInfoGet_nva(nrrd, nrrdAxisInfoSize, size);
  for (ai=0; ai<nrrd->dim; ai++) {
    if (!( coord[ai] < size[ai] )) {
      sprintf(err, "%s: coordinate " _AIR_SIZE_T_CNV 
              " on axis %d out of bounds (0 to " _AIR_SIZE_T_CNV  ")", 
              me, coord[ai], ai, size[ai]-1);
      biffAdd(NRRD, err); return 1;
    }
  }

  NRRD_INDEX_GEN(I, coord, size, nrrd->dim);

  memcpy(val, (char*)(nrrd->data) + I*typeSize, typeSize);
  return 0;
}
Example #8
0
int
limnEnvMapCheck(Nrrd *envMap) {
  char me[]="limnEnvMapCheck", err[BIFF_STRLEN];

  if (nrrdCheck(envMap)) {
    sprintf(err, "%s: basic nrrd validity check failed", me);
    biffMove(LIMN, err, NRRD); return 1;
  }
  if (!(nrrdTypeFloat == envMap->type)) {
    sprintf(err, "%s: type should be %s, not %s", me,
            airEnumStr(nrrdType, nrrdTypeFloat),
            airEnumStr(nrrdType, envMap->type));
    biffAdd(LIMN, err); return 1;
  }
  if (!(3 == envMap->dim)) {
    sprintf(err, "%s: dimension should be 3, not %d", me, envMap->dim);
    biffAdd(LIMN, err); return 1;
  }
  if (!(3 == envMap->axis[0].size
        && 256 == envMap->axis[1].size
        && 256 == envMap->axis[2].size)) {
    sprintf(err, "%s: dimension should be 3x256x256, not " 
            _AIR_SIZE_T_CNV "x" 
            _AIR_SIZE_T_CNV "x" 
            _AIR_SIZE_T_CNV, me,
            envMap->axis[0].size, 
            envMap->axis[1].size, 
            envMap->axis[2].size);
    biffAdd(LIMN, err); return 1;
  }
  return 0;
}
Example #9
0
int
_pullContextCheck(pullContext *pctx) {
  char me[]="_pullContextCheck", err[BIFF_STRLEN];
  unsigned int ii, sclvi;
  int gotIspec, gotConstr;

  if (!pctx) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(PULL, err); return 1;
  }
  if (pctx->npos) {
    if (nrrdCheck(pctx->npos)) {
      sprintf(err, "%s: got a broken npos", me);
      biffMove(PULL, err, NRRD); return 1;
    }
    if (!( 2 == pctx->npos->dim 
           && 4 == pctx->npos->axis[0].size
           && (nrrdTypeDouble == pctx->npos->type 
               || nrrdTypeFloat == pctx->npos->type) )) {
      sprintf(err, "%s: npos not a 2-D 4-by-N array of %s or %s"
              "(got %u-D %u-by-X of %s)", me,
              airEnumStr(nrrdType, nrrdTypeFloat),
              airEnumStr(nrrdType, nrrdTypeDouble),
              pctx->npos->dim,
              AIR_CAST(unsigned int, pctx->npos->axis[0].size),
              airEnumStr(nrrdType, pctx->npos->type));
      biffAdd(PULL, err); return 1;
    }
  } else {
Example #10
0
File: gzio.c Project: rblake/seg3d2
/*
** _nrrdGzDestroy()
**
** Cleans up then free the given _NrrdGzStream. Returns a zlib error code.
** Try freeing in the reverse order of allocations.  FILE* s->file is not
** closed.  Because we didn't allocate it, we shouldn't delete it.
*/
static int
_nrrdGzDestroy(_NrrdGzStream *s) {
  char me[]="_nrrdGzDestroy", err[BIFF_STRLEN];
  int error = Z_OK;

  if (s == NULL) {
    sprintf(err, "%s: invalid stream", me);
    biffAdd(NRRD, err);
    return 1;
  }
  s->msg = (char *)airFree(s->msg);
  if (s->stream.state != NULL) {
    if (s->mode == 'w') {
      error = deflateEnd(&(s->stream));
    } else if (s->mode == 'r') {
      error = inflateEnd(&(s->stream));
    }
  }
  if (error != Z_OK) {
    sprintf(err, "%s: %s", me, _NRRD_GZ_ERR_MSG(error));
    biffAdd(NRRD, err);
  }
  if (s->z_err < 0) error = s->z_err;
  if (error != Z_OK) {
    sprintf(err, "%s: %s", me, _NRRD_GZ_ERR_MSG(error));
    biffAdd(NRRD, err);
  }
  s->inbuf = (Byte *)airFree(s->inbuf);
  s->outbuf = (Byte *)airFree(s->outbuf);
  airFree(s);   /* avoiding unused value warnings, no NULL set */
  return error != Z_OK;
}
Example #11
0
File: gzio.c Project: rblake/seg3d2
/*
** _nrrdGzWrite()
**
** Writes the given number of uncompressed bytes into the compressed file.
** Returns the number of bytes actually written (0 in case of error).
*/
int
_nrrdGzWrite(gzFile file, const voidp buf, unsigned int len,
             unsigned int* written) {
  char me[]="_nrrdGzWrite", err[BIFF_STRLEN];
  _NrrdGzStream *s = (_NrrdGzStream*)file;

  if (s == NULL || s->mode != 'w') {
    sprintf(err, "%s: invalid stream or file mode", me);
    biffAdd(NRRD, err);
    *written = 0;
    return 1;
  }

  s->stream.next_in = (Bytef*)buf;
  s->stream.avail_in = len;

  while (s->stream.avail_in != 0) {
    if (s->stream.avail_out == 0) {
      s->stream.next_out = s->outbuf;
      if (fwrite(s->outbuf, 1, _NRRD_Z_BUFSIZE, s->file) != _NRRD_Z_BUFSIZE) {
        s->z_err = Z_ERRNO;
        sprintf(err, "%s: failed to write to file", me);
        biffAdd(NRRD, err);
        break;
      }
      s->stream.avail_out = _NRRD_Z_BUFSIZE;
    }
    s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
    if (s->z_err != Z_OK) break;
  }
  s->crc = crc32(s->crc, (const Bytef *)buf, len);

  *written = len - s->stream.avail_in;
  return 0;
}
Example #12
0
int
limnPolyDataCopy(limnPolyData *pldB, const limnPolyData *pldA) {
  char me[]="limnPolyDataCopy", err[BIFF_STRLEN];

  if (!( pldB && pldA )) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(LIMN, err); return 1;
  }
  if (limnPolyDataAlloc(pldB, limnPolyDataInfoBitFlag(pldA),
                        pldA->xyzwNum, pldA->indxNum, pldA->primNum)) {
    sprintf(err, "%s: couldn't allocate output", me);
    biffAdd(LIMN, err); return 1;
  }
  memcpy(pldB->xyzw, pldA->xyzw, pldA->xyzwNum*sizeof(float)*4);
  if (pldA->rgba) {
    memcpy(pldB->rgba, pldA->rgba, pldA->rgbaNum*sizeof(unsigned char)*4);
  }
  if (pldA->norm) {
    memcpy(pldB->norm, pldA->norm, pldA->normNum*sizeof(float)*3);
  }
  if (pldA->tex2) {
    memcpy(pldB->tex2, pldA->tex2, pldA->tex2Num*sizeof(float)*2);
  }
  memcpy(pldB->indx, pldA->indx, pldA->indxNum*sizeof(unsigned int));
  memcpy(pldB->type, pldA->type, pldA->primNum*sizeof(signed char));
  memcpy(pldB->icnt, pldA->icnt, pldA->primNum*sizeof(unsigned int));
  return 0;
}
Example #13
0
int
nrrdIoStateFormatSet(NrrdIoState *nio, const NrrdFormat *format)
{
    char me[]="nrrdIoStateFormatSet", err[BIFF_STRLEN];

    if (!( nio && format ))
    {
        sprintf(err, "%s: got NULL pointer", me);
        if (nio)
        {
            nio->format = nrrdFormatUnknown;
        }
        biffAdd(NRRD, err);
        return 1;
    }
    if (!format->available())
    {
        sprintf(err, "%s: %s format isn't actually available", me, format->name);
        nio->format = nrrdFormatUnknown;
        biffAdd(NRRD, err);
        return 1;
    }
    nio->format = format;
    return 0;
}
Example #14
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, ...)
{
  char me[]="nrrdMaybeAlloc_va", err[BIFF_STRLEN];
  size_t size[NRRD_DIM_MAX];
  unsigned int ai;
  va_list ap;

  if (!nrrd)
  {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(NRRD, err);
    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))
  {
    sprintf(err, "%s:", me);
    biffAdd(NRRD, err);
    return 1;
  }
  return 0;
}
/*
******** nrrd1DIrregAclCheck()
**
** returns zero only on valid accelerators for 1D irregular mappings
*/
int
nrrd1DIrregAclCheck(const Nrrd *nacl) {
  char me[]="nrrd1DIrregAclCheck", err[BIFF_STRLEN];

  if (!nacl) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(NRRD, err); return 1;
  }
  if (nrrdCheck(nacl)) {
    sprintf(err, "%s: ", me);
    biffAdd(NRRD, err); return 1;
  }
  if (nrrdTypeUShort != nacl->type) {
    sprintf(err, "%s: type should be %s, not %s", me,
            airEnumStr(nrrdType, nrrdTypeUShort),
            airEnumStr(nrrdType, nacl->type));
    biffAdd(NRRD, err); return 1;
  }
  if (2 != nacl->dim) {
    sprintf(err, "%s: dimension should be 2, not %d", me, nacl->dim);
    biffAdd(NRRD, err); return 1;
  }
  if (!( nacl->axis[0].size == 2 && nacl->axis[1].size >= 2 )) {
    sprintf(err, "%s: sizes (" _AIR_SIZE_T_CNV "," _AIR_SIZE_T_CNV ") "
            "not (2,>=2)", me, nacl->axis[0].size, nacl->axis[1].size);
    biffAdd(NRRD, err); return 1;
  }

  return 0;
}
int
dyeColorParse(dyeColor *col, char *_str) {
  char me[]="dyeColorParse", err[128], *str;
  char *colon, *valS;
  float v0, v1, v2;
  int spc;
  
  if (!(col && _str)) {
    sprintf(err, "%s: got NULL pointer", me); biffAdd(DYE, err); return 1;
  }
  if (!(str = airStrdup(_str))) {
    sprintf(err, "%s: couldn't strdup!", me);
    biffAdd(DYE, err); return 1;
  }
  if (!(colon = strchr(str, ':'))) {
    sprintf(err, "%s: given string \"%s\" didn't contain colon", me, str);
    biffAdd(DYE, err); return 1;
  }
  *colon = '\0';
  valS = colon+1;
  if (3 != sscanf(valS, "%g,%g,%g", &v0, &v1, &v2)) {
    sprintf(err, "%s: couldn't parse three floats from \"%s\"", me, valS);
    biffAdd(DYE, err); return 1;
  }
  spc = dyeStrToSpace(str);
  if (dyeSpaceUnknown == spc) {
    sprintf(err, "%s: couldn't parse colorspace from \"%s\"", me, str);
    biffAdd(DYE, err); return 1;
  }
  str = (char *)airFree(str);

  dyeColorSet(col, spc, v0, v1, v2);
  return 0;
}
Example #17
0
int
_alanCheck(alanContext *actx) {
  char me[]="alanCheck", err[BIFF_STRLEN];

  if (!actx) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(ALAN, err); return 1;
  }
  if (0 == actx->dim) {
    sprintf(err, "%s: dimension of texture not set", me);
    biffAdd(ALAN, err); return 1;
  }
  if (alanTextureTypeUnknown == actx->textureType) {
    sprintf(err, "%s: texture type not set", me);
    biffAdd(ALAN, err); return 1;
  }
  if (!( actx->size[0] > 0 && actx->size[1] > 0
         && (2 == actx->dim || actx->size[2] > 0) )) {
    sprintf(err, "%s: texture sizes invalid", me);
    biffAdd(ALAN, err); return 1;
  }
  if (0 == actx->deltaT) {
    sprintf(err, "%s: deltaT == 0", me);
    biffAdd(ALAN, err); return 1;
  }

  return 0;
}
int
nrrdApplyMulti1DRegMap(Nrrd *nout, const Nrrd *nin,
                       const NrrdRange *_range, const Nrrd *nmmap,
                       int typeOut, int rescale) {
  char me[]="nrrdApplyMulti1DRegMap", err[BIFF_STRLEN];
  NrrdRange *range;
  airArray *mop;

  if (!(nout && nmmap && nin)) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(NRRD, err); return 1;
  }
  mop = airMopNew();
  if (_range) {
    range = nrrdRangeCopy(_range);
    nrrdRangeSafeSet(range, nin, nrrdBlind8BitRangeState);
  } else {
    range = nrrdRangeNewSet(nin, nrrdBlind8BitRangeState);
  }
  airMopAdd(mop, range, (airMopper)nrrdRangeNix, airMopAlways);
  if (_nrrdApply1DSetUp(nout, nin, range, nmmap, kindRmap, typeOut,
                        rescale, AIR_TRUE)
      || _nrrdApply1DLutOrRegMap(nout, nin, range, nmmap, AIR_TRUE,
                                 rescale, AIR_TRUE)) {
    sprintf(err, "%s:", me);
    biffAdd(NRRD, err); airMopError(mop); return 1;
  }
  airMopOkay(mop);
  return 0;
}
Example #19
0
int
baneBcptsCheck (Nrrd *Bcpts) {
  char me[]="baneBcptsCheck", err[BIFF_STRLEN];
  int i, len;
  float *data;

  if (2 != Bcpts->dim) {
    sprintf(err, "%s: need 2-dimensional (not %d)", me, Bcpts->dim);
    biffAdd(BANE, err); return 1;
  }
  if (2 != Bcpts->axis[0].size) {
    sprintf(err, "%s: axis#0 needs size 2 (not " _AIR_SIZE_T_CNV ")",
            me, Bcpts->axis[0].size);
    biffAdd(BANE, err); return 1;
  }
  if (nrrdTypeFloat != Bcpts->type) {
    sprintf(err, "%s: need data of type float", me);
    biffAdd(BANE, err); return 1;
  }
  len = Bcpts->axis[1].size;
  data = (float *)Bcpts->data;
  for (i=0; i<=len-2; i++) {
    if (!(data[0 + 2*i] <= data[0 + 2*(i+1)])) {
      sprintf(err, "%s: value coord %d (%g) not <= coord %d (%g)", me,
              i, data[0 + 2*i], i+1, data[0 + 2*(i+1)]);
      biffAdd(BANE, err); return 1;
    }
  }
  return 0;
}
Example #20
0
int
banePosCheck (Nrrd *pos, int wantDim) {
  char me[]="banePosCheck", err[BIFF_STRLEN];
  int gotDim;

  if (!pos) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(BANE, err); return 1;
  }
  gotDim = pos->dim;
  if (wantDim) {
    if (!(1 == wantDim || 2 == wantDim)) {
      sprintf(err, "%s: wantDim should be 1 or 2, not %d", me, wantDim);
      biffAdd(BANE, err); return 1;
    }
    if (wantDim != gotDim) {
      sprintf(err, "%s: dim is %d, not %d", me, gotDim, wantDim);
      biffAdd(BANE, err); return 1;
    }
  }
  else {
    if (!(1 == gotDim || 2 == gotDim)) {
      sprintf(err, "%s: dim is %d, not 1 or 2", me, gotDim);
      biffAdd(BANE, err); return 1;
    }
  }
  if (nrrdTypeFloat != pos->type) {
    sprintf(err, "%s: need data of type float", me);
    biffAdd(BANE, err); return 1;
  }
  /* HEY? check for values in axisMin[0] and axisMax[0] ? */
  /* HEY? check for values in axisMin[0] and axisMax[0] ? */
  /* HEY? check for values in axisMin[1] and axisMax[1] ? */
  return 0;
}
Example #21
0
int
mrendUserCheck(mrendUser *uu) {
  char me[]="mrendUserCheck", err[BIFF_STRLEN];
  
  if (3 + uu->kind->baseDim != uu->nin->dim) {
    sprintf(err, "%s: input nrrd needs %d dimensions, not %d", 
            me,  + uu->kind->baseDim, uu->nin->dim);
    biffAdd(MREND, err); return 1;
  }
  if (!( uu->nin->axis[0].center == uu->nin->axis[1].center &&
         uu->nin->axis[0].center == uu->nin->axis[2].center )) {
    sprintf(err, "%s: axes 0,1,2 centerings (%s,%s,%s) not equal", me,
            airEnumStr(nrrdCenter, uu->nin->axis[0].center),
            airEnumStr(nrrdCenter, uu->nin->axis[1].center),
            airEnumStr(nrrdCenter, uu->nin->axis[2].center));
    biffAdd(MREND, err); return 1;
  }
  if (1 != uu->kind->table[uu->whatq].answerLength) {
    sprintf(err, "%s: quantity %s (in %s volumes) isn't a scalar; "
            "can't render it",
            me, airEnumStr(uu->kind->enm, uu->whatq), uu->kind->name);
    biffAdd(MREND, err); return 1;
  }
  
  return 0;
}
Example #22
0
int
limnSplineSample(Nrrd *nout, limnSpline *spline,
                 double minT, size_t M, double maxT) {
  char me[]="limnSplineSample", err[BIFF_STRLEN];
  airArray *mop;
  Nrrd *ntt;
  double *tt;
  size_t I;

  if (!(nout && spline)) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(LIMN, err); return 1;
  }
  mop = airMopNew();
  airMopAdd(mop, ntt=nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  if (nrrdMaybeAlloc_va(ntt, nrrdTypeDouble, 1,
                        M)) {
    sprintf(err, "%s: trouble allocating tmp nrrd", me);
    biffMove(LIMN, err, NRRD); airMopError(mop); return 1;
  }
  tt = (double*)(ntt->data);
  for (I=0; I<M; I++) {
    tt[I] = AIR_AFFINE(0, I, M-1, minT, maxT);
  }
  if (limnSplineNrrdEvaluate(nout, spline, ntt)) {
    sprintf(err, "%s: trouble", me);
    biffAdd(LIMN, err); airMopError(mop); return 1;
  }
  airMopOkay(mop);
  return 0;
}
Example #23
0
/*
** _pullIterate
**
** (documentation)
**
** NB: this implements the body of thread 0, the master thread
*/
int
_pullIterate(pullContext *pctx) {
  char me[]="_pullIterate", err[BIFF_STRLEN];
  double time0;
  int myError;
  unsigned int thi;

  if (!pctx) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(PULL, err); return 1;
  }
  
  if (pctx->verbose) {
    fprintf(stderr, "%s: start iter %d w/ %u threads; energy = %g\n",
            me, pctx->iter, pctx->threadNum, _pullEnergyTotal(pctx));
  }

  time0 = airTime();

  /* the _pullWorker checks finished after iterBarrierA */
  pctx->finished = AIR_FALSE;

  /* initialize index of next bin to be doled out to threads */
  pctx->binNextIdx=0;

  if (pctx->threadNum > 1) {
    airThreadBarrierWait(pctx->iterBarrierA);
  }
  myError = AIR_FALSE;
  if (_pullProcess(pctx->task[0])) {
    sprintf(err, "%s: master thread trouble w/ iter %u", me, pctx->iter);
    biffAdd(PULL, err);
    pctx->finished = AIR_TRUE;
    myError = AIR_TRUE;
  }
  if (pctx->threadNum > 1) {
    airThreadBarrierWait(pctx->iterBarrierB);
  }
  if (pctx->finished) {
    if (!myError) {
      /* we didn't set finished- one of the workers must have */
      sprintf(err, "%s: worker error on iter %u", me, pctx->iter);
      biffAdd(PULL, err); 
    }
    return 1;
  }
  pctx->stuckNum = 0;
  for (thi=0; thi<pctx->threadNum; thi++) {
    pctx->stuckNum += pctx->task[thi]->stuckNum;
  }
  _pullPointNixMeRemove(pctx);
  if (pullRebin(pctx)) {
    sprintf(err, "%s: problem with new point locations", me);
    biffAdd(PULL, err); return 1;
  }

  pctx->timeIteration = airTime() - time0;
  return 0;
}
Example #24
0
/*
******** nrrdSave
**
** save a given nrrd to a given filename, with cleverness to guess
** format if not specified by the caller
**
** currently, for NRRD format files, we play the detached header game
** whenever the filename ends in NRRD_EXT_NHDR, and when we play this
** game, the data file is ALWAYS header relative.
*/
int
nrrdSave(const char *filename, const Nrrd *nrrd, NrrdIoState *nio) {
    char me[]="nrrdSave", err[BIFF_STRLEN];
    FILE *file;
    airArray *mop;

    if (!(nrrd && filename)) {
        sprintf(err, "%s: got NULL pointer", me);
        biffAdd(NRRD, err);
        return 1;
    }
    mop = airMopNew();
    if (!nio) {
        nio = nrrdIoStateNew();
        if (!nio) {
            sprintf(err, "%s: couldn't alloc local NrrdIoState", me);
            biffAdd(NRRD, err);
            return 1;
        }
        airMopAdd(mop, nio, (airMopper)nrrdIoStateNix, airMopAlways);
    }
    if (_nrrdEncodingMaybeSet(nio)
            || _nrrdFormatMaybeGuess(nrrd, nio, filename)) {
        sprintf(err, "%s: ", me);
        biffAdd(NRRD, err);
        airMopError(mop);
        return 1;
    }

    if (nrrdFormatNRRD == nio->format
            && airEndsWith(filename, NRRD_EXT_NHDR)) {
        nio->detachedHeader = AIR_TRUE;
        _nrrdSplitName(&(nio->path), &(nio->base), filename);
        /* nix the ".nhdr" suffix */
        nio->base[strlen(nio->base) - strlen(NRRD_EXT_NHDR)] = 0;
        /* nrrdFormatNRRD->write will do the rest */
    } else {
        nio->detachedHeader = AIR_FALSE;
    }

    if (!( file = airFopen(filename, stdout, "wb") )) {
        sprintf(err, "%s: couldn't fopen(\"%s\",\"wb\"): %s",
                me, filename, strerror(errno));
        biffAdd(NRRD, err);
        airMopError(mop);
        return 1;
    }
    airMopAdd(mop, file, (airMopper)airFclose, airMopAlways);

    if (nrrdWrite(file, nrrd, nio)) {
        sprintf(err, "%s:", me);
        biffAdd(NRRD, err);
        airMopError(mop);
        return 1;
    }

    airMopOkay(mop);
    return 0;
}
Example #25
0
File: pvl.c Project: rblake/seg3d2
/*
******** gageQuerySet()
**
** sets a query in a pervolume.  Does recursive expansion of query
** to cover all prerequisite measures.  
**
** Sets: pvl->query
**
** the gageContext is not actually used here, but I'm cautiously
** including it in case its used in the future.
*/
int
gageQuerySet(gageContext *ctx, gagePerVolume *pvl, gageQuery query) {
  char me[]="gageQuerySet", err[BIFF_STRLEN];
  gageQuery lastQuery;
  int pi, ii;
  
  AIR_UNUSED(ctx);
  if (!( pvl )) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(GAGE, err); return 1;
  }
  GAGE_QUERY_COPY(pvl->query, query);
  if (pvl->verbose) {
    fprintf(stderr, "%s: original ", me);
    gageQueryPrint(stderr, pvl->kind, pvl->query);
  }
  /* recursive expansion of prerequisites */
  do {
    GAGE_QUERY_COPY(lastQuery, pvl->query);
    ii = pvl->kind->itemMax+1;
    do {
      ii--;
      if (GAGE_QUERY_ITEM_TEST(pvl->query, ii)) {
        for (pi=0; pi<GAGE_ITEM_PREREQ_MAXNUM; pi++) {
          if (0 != pvl->kind->table[ii].prereq[pi]) {
            GAGE_QUERY_ITEM_ON(pvl->query, pvl->kind->table[ii].prereq[pi]);
          }
        }
      }
    } while (ii);
  } while (!GAGE_QUERY_EQUAL(pvl->query, lastQuery));
  if (pvl->verbose) {
    fprintf(stderr, "%s: expanded ", me);
    gageQueryPrint(stderr, pvl->kind, pvl->query);
  }

  /* doing this kind of error checking here is not really
     the way gage should work-- it should be done at the 
     time of gageUpdate()-- but the novelty of pvl->data
     encourages putting new smarts at superficial levels
     instead of deeper levels */
  if (!pvl->data) {
    for (ii=1; ii<=pvl->kind->itemMax; ii++) {
      if (GAGE_QUERY_ITEM_TEST(pvl->query, ii)
          && pvl->kind->table[ii].needData) {
        sprintf(err, "%s: item %d (%s) needs data, but pvl->data is NULL", 
                me, ii, airEnumStr(pvl->kind->enm, ii));
        biffAdd(GAGE, err); return 1;
      }
    }
  }

  pvl->flag[gagePvlFlagQuery] = AIR_TRUE;

  return 0;
}
Example #26
0
int
limnPolyDataAlloc(limnPolyData *pld,
                  unsigned int infoBitFlag,
                  unsigned int vertNum,
                  unsigned int indxNum,
                  unsigned int primNum) {
  char me[]="limnPolyDataAlloc", err[BIFF_STRLEN];
  
  if (!pld) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(LIMN, err); return 1;
  }
  if (vertNum != pld->xyzwNum) {
    pld->xyzw = (float *)airFree(pld->xyzw);
    if (vertNum) {
      pld->xyzw = (float *)calloc(vertNum, 4*sizeof(float));
      if (!pld->xyzw) {
        sprintf(err, "%s: couldn't allocate %u xyzw", me, vertNum);
        biffAdd(LIMN, err); return 1;
      }
    }
    pld->xyzwNum = vertNum;
  }
  if (_limnPolyDataInfoAlloc(pld, infoBitFlag, vertNum)) {
    sprintf(err, "%s: couldn't allocate info", me);
    biffAdd(LIMN, err); return 1;
  }
  if (indxNum != pld->indxNum) {
    pld->indx = (unsigned int *)airFree(pld->indx);
    if (indxNum) {
      pld->indx = (unsigned int *)calloc(indxNum, sizeof(unsigned int));
      if (!pld->indx) {
        sprintf(err, "%s: couldn't allocate %u indices", me, indxNum);
        biffAdd(LIMN, err); return 1;
      }
    }
    pld->indxNum = indxNum;
  }
  if (primNum != pld->primNum) {
    pld->type = (unsigned char *)airFree(pld->type);
    pld->icnt = (unsigned int *)airFree(pld->icnt);
    if (primNum) {
      pld->type = (unsigned char *)calloc(primNum, sizeof(unsigned char));
      pld->icnt = (unsigned int *)calloc(primNum, sizeof(unsigned int));
      if (!(pld->type && pld->icnt)) {
        sprintf(err, "%s: couldn't allocate %u primitives", me, primNum);
        biffAdd(LIMN, err); return 1;
      }
    }
    pld->primNum = primNum;
  }
  return 0;
}
Example #27
0
int
ninspect_proj(Nrrd *nout, Nrrd *nin, int axis, int smart, float amount) {
  char me[]="ninspect_proj", err[BIFF_STRLEN];
  airArray *mop;
  Nrrd *ntmpA, *ntmpB, *nrgb[3];
  int bins;

  if (!(nout && nin)) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(NINSPECT, err);
    return 1;
  }
  if (!( AIR_IN_CL(0, axis, 2) )) {
    sprintf(err, "%s: given axis %d outside valid range [0,1,2]", me, axis);
    biffAdd(NINSPECT, err);
    return 1;
  }

  /* allocate a bunch of nrrds to use as basically temp variables */
  mop = airMopNew();
  airMopAdd(mop, ntmpA = nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  airMopAdd(mop, ntmpB = nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  airMopAdd(mop, nrgb[0] = nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  airMopAdd(mop, nrgb[1] = nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  airMopAdd(mop, nrgb[2] = nrrdNew(), (airMopper)nrrdNuke, airMopAlways);

  /* these arguments to nrrdHistoEq will control its behavior */
  bins = 3000;  /* equalization will use a histogram with this many bins */

  /* the following idiom is one way of handling the fact that any 
     non-trivial nrrd call can fail, and if it does, then any subsequent
     nrrd calls should be avoided (to be perfectly safe), so that you can
     get the error message from biff.  Because of the left-to-right ordering
     ensured for logical expressions, this will all be called in sequence
     until one of them has a non-zero return.  If he had exception handling,
     we'd put all the nrrd calls in one "try" block.  */
  if (nrrdProject(ntmpA, nin, axis, nrrdMeasureSum, nrrdTypeDefault)
      || nrrdHistoEq(ntmpB, ntmpA, NULL, bins, smart, amount)
      || nrrdQuantize(nrgb[0], ntmpB, NULL, 8)
      || nrrdProject(ntmpA, nin, axis, nrrdMeasureVariance, nrrdTypeDefault)
      || nrrdHistoEq(ntmpB, ntmpA, NULL, bins, smart, amount)
      || nrrdQuantize(nrgb[1], ntmpB, NULL, 8)
      || nrrdProject(ntmpA, nin, axis, nrrdMeasureMax, nrrdTypeDefault)
      || nrrdQuantize(nrgb[2], ntmpA, NULL, 8)
      || nrrdJoin(nout, (const Nrrd**)nrgb, 3, 0, AIR_TRUE)) {
    sprintf(err, "%s: trouble with nrrd operations", me);
    biffMove(NINSPECT, err, NRRD);
    airMopError(mop); return 1;
  }

  airMopOkay(mop);
  return 0;
}
Example #28
0
int
_nrrdEncodingRaw_write(FILE *file, const void *data, size_t elementNum,
                       const Nrrd *nrrd, NrrdIoState *nio) {
  char me[]="_nrrdEncodingRaw_write", err[BIFF_STRLEN];
  int fd, dio;
  size_t ret, bsize;
  
  bsize = nrrdElementSize(nrrd)*elementNum;
  if (nio->format->usesDIO) {
    fd = fileno(file);
    dio = airDioTest(fd, data, bsize);
  } else {
    fd = -1;
    dio = airNoDio_format;
  }
  if (airNoDio_okay == dio) {
    if (2 <= nrrdStateVerboseIO) {
      fprintf(stderr, "with direct I/O ... ");
    }
    ret = airDioWrite(fd, data, bsize);
    if (ret != bsize) {
      sprintf(err, "%s: airDioWrite wrote only "
              _AIR_SIZE_T_CNV " of " _AIR_SIZE_T_CNV " bytes "
              "(%g%% of expected)", me,
              ret, bsize, 100.0*ret/bsize);
      biffAdd(NRRD, err); return 1;
    }
  } else {
    if (2 <= nrrdStateVerboseIO) {
      if (AIR_DIO && nio->format->usesDIO) {
        fprintf(stderr, "with fread(), not DIO: %s ...", airNoDioErr(dio));
      }
    }
    ret = fwrite(data, nrrdElementSize(nrrd), elementNum, file);
    if (ret != elementNum) {
      sprintf(err, "%s: fwrite wrote read only "
              _AIR_SIZE_T_CNV " " _AIR_SIZE_T_CNV "-sized things, not " 
              _AIR_SIZE_T_CNV " (%g%% of expected)", me,
              ret, nrrdElementSize(nrrd), elementNum,
              100.0*ret/elementNum);
      biffAdd(NRRD, err); return 1;
    }
    fflush(file);
    /*
    if (ferror(file)) {
      sprintf(err, "%s: ferror returned non-zero", me);
      biffAdd(NRRD, err); return 1;
    }
    */
  }
  return 0;
}
Example #29
0
miteThread *
miteThreadNew() {
  char me[]="miteThreadNew", err[BIFF_STRLEN];
  miteThread *mtt;
  int ii;
  
  mtt = (miteThread *)calloc(1, sizeof(miteThread));
  if (!mtt) {
    sprintf(err, "%s: couldn't calloc miteThread", me);
    biffAdd(MITE, err); return NULL;
  }

  mtt->rmop = airMopNew();
  if (!mtt->rmop) {
    sprintf(err, "%s: couldn't calloc thread's mop", me);
    biffAdd(MITE, err); airFree(mtt); return NULL;
  }
  mtt->gctx = NULL;
  mtt->ansScl = mtt->ansVec = mtt->ansTen = NULL;
  mtt->_normal = NULL;
  mtt->shadeVec0 = NULL;
  mtt->shadeVec1 = NULL;
  mtt->shadeScl0 = NULL;
  mtt->shadeScl1 = NULL;
  /* were miteVal a full-fledged gageKind, the following would
     be done by gagePerVolumeNew */
  mtt->ansMiteVal = 
    (double *)calloc(gageKindTotalAnswerLength(miteValGageKind), 
                     sizeof(double));
  mtt->directAnsMiteVal = 
    (double **)calloc(miteValGageKind->itemMax+1, sizeof(double*));
  if (!(mtt->ansMiteVal && mtt->directAnsMiteVal)) {
    sprintf(err, "%s: couldn't calloc miteVal answer arrays", me);
    biffAdd(MITE, err); return NULL;
  }
  for (ii=0; ii<=miteValGageKind->itemMax; ii++) {
    mtt->directAnsMiteVal[ii] = mtt->ansMiteVal 
      + gageKindAnswerOffset(miteValGageKind, ii);
  }
  mtt->verbose = 0;
  mtt->skip = 0;
  mtt->thrid = -1;
  mtt->ui = mtt->vi = -1;
  mtt->raySample = 0;
  mtt->samples = 0;
  mtt->stage = NULL;
  /* mtt->range[], rayStep, V, RR, GG, BB, TT  initialized in 
     miteRayBegin or in miteSample */
  
  return mtt;
}
Example #30
0
int
nrrdSimpleResample(Nrrd *nout, Nrrd *nin,
                   const NrrdKernel *kernel, const double *parm,
                   const size_t *samples, const double *scalings) {
  char me[]="nrrdSimpleResample", err[BIFF_STRLEN];
  NrrdResampleInfo *info;
  int p, np, center;
  unsigned ai;

  if (!(nout && nin && kernel && (samples || scalings))) {
    sprintf(err, "%s: not NULL pointer", me);
    biffAdd(NRRD, err); return 1;
  }
  if (!(info = nrrdResampleInfoNew())) {
    sprintf(err, "%s: can't allocate resample info struct", me);
    biffAdd(NRRD, err); return 1;
  }

  np = kernel->numParm;
  for (ai=0; ai<nin->dim; ai++) {
    info->kernel[ai] = kernel;
    if (samples) {
      info->samples[ai] = samples[ai];
    } else {
      center = _nrrdCenter(nin->axis[ai].center);
      if (nrrdCenterCell == center) {
        info->samples[ai] = (size_t)(nin->axis[ai].size*scalings[ai]);
      } else {
        info->samples[ai] = (size_t)((nin->axis[ai].size - 1)
                                     *scalings[ai]) + 1;
      }
    }
    for (p=0; p<np; p++)
      info->parm[ai][p] = parm[p];
    /* set the min/max for this axis if not already set to something */
    if (!( AIR_EXISTS(nin->axis[ai].min) && AIR_EXISTS(nin->axis[ai].max) ))
      nrrdAxisInfoMinMaxSet(nin, ai, nrrdDefaultCenter);
    info->min[ai] = nin->axis[ai].min;
    info->max[ai] = nin->axis[ai].max;
  }
  /* we go with the defaults (enstated by _nrrdResampleInfoInit())
     for all the remaining fields */

  if (nrrdSpatialResample(nout, nin, info)) {
    sprintf(err, "%s:", me);
    biffAdd(NRRD, err); return 1;
  }

  info = nrrdResampleInfoNix(info);
  return 0;
}