int tenDWMRIKeyValueFromExperSpecSet(Nrrd *ndwi, const tenExperSpec *espec) { static char me[]="tenDWMRIKeyValueFromExperSpecSet"; char keystr[AIR_STRLEN_MED], valstr[AIR_STRLEN_MED]; double maxb, bb; unsigned int ii; if (!(ndwi && espec)) { biffAddf(TEN, "%s: got NULL pointer", me); return 1; } nrrdKeyValueAdd(ndwi, tenDWMRIModalityKey, tenDWMRIModalityVal); maxb = tenExperSpecMaxBGet(espec); sprintf(valstr, "%.17g", maxb); nrrdKeyValueAdd(ndwi, tenDWMRIBValueKey, valstr); for (ii=0; ii<espec->imgNum; ii++) { double vec[3]; sprintf(keystr, tenDWMRIGradKeyFmt, ii); ELL_3V_COPY(vec, espec->grad + 3*ii); bb = espec->bval[ii]; /* Thu Dec 20 03:25:20 CST 2012 this rescaling is not, btw, what is causing the small discrepency between ngrad before and after saving to KVPs */ ELL_3V_SCALE(vec, sqrt(bb/maxb), vec); sprintf(valstr, "%.17g %.17g %.17g", vec[0], vec[1], vec[2]); nrrdKeyValueAdd(ndwi, keystr, valstr); } /* HEY what if its a full B-matrix? */ return 0; }
/* ******** 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; }
int _nrrdReadNrrdParse_keyvalue (FILE *file, Nrrd *nrrd, NrrdIoState *nio, int useBiff) { char me[]="_nrrdReadNrrdParse_keyvalue", err[BIFF_STRLEN]; char *keysep, *line, *key, *value; AIR_UNUSED(file); /* we know this will find something */ line = airStrdup(nio->line); if (!line) { sprintf(err, "%s: can't allocate parse line", me); biffMaybeAdd(NRRD, err, useBiff); return 1; } keysep = strstr(line, ":="); if (!keysep) { sprintf(err, "%s: didn't see \":=\" key/value delimiter in \"%s\"", me, line); free(line); biffMaybeAdd(NRRD, err, useBiff); return 1; } keysep[0] = 0; keysep[1] = 0; key = line; value = keysep+2; /* convert escape sequences */ airUnescape(key); airUnescape(value); nrrdKeyValueAdd(nrrd, key, value); free(line); return 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; }
/* ** little helper function to do pre-blurring of a given nrrd ** of the sort that might be useful for scale-space gage use ** ** nblur has to already be allocated for "blnum" Nrrd*s, AND, they all ** have to point to valid (possibly empty) Nrrds, so they can hold the ** results of blurring. "scale" is filled with the result of ** scaleCB(d_i), for "dom" evenly-spaced samples d_i between ** scldomMin and scldomMax */ int gageStackBlur(Nrrd *const nblur[], const double *scale, unsigned int blnum, const Nrrd *nin, unsigned int baseDim, const NrrdKernelSpec *_kspec, int boundary, int renormalize, int verbose) { char me[]="gageStackBlur", err[BIFF_STRLEN], val[AIR_STRLEN_LARGE], keyscl[]="scale", keykern[]="kernel"; unsigned int blidx, axi; NrrdResampleContext *rsmc; NrrdKernelSpec *kspec; airArray *mop; int E; if (!(nblur && scale && nin && _kspec)) { sprintf(err, "%s: got NULL pointer", me); biffAdd(GAGE, err); return 1; } if (!( blnum >= 2)) { sprintf(err, "%s: need blnum > 2, not %u", me, blnum); biffAdd(GAGE, err); return 1; } for (blidx=0; blidx<blnum; blidx++) { if (!AIR_EXISTS(scale[blidx])) { fprintf(stderr, "%s: scale[%u] = %g doesn't exist", me, blidx, scale[blidx]); biffAdd(GAGE, err); return 1; } if (blidx) { if (!( scale[blidx-1] < scale[blidx] )) { fprintf(stderr, "%s: scale[%u] = %g not < scale[%u] = %g", me, blidx, scale[blidx-1], blidx+1, scale[blidx]); biffAdd(GAGE, err); return 1; } } } if (3 + baseDim != nin->dim) { sprintf(err, "%s: need nin->dim %u (not %u) with baseDim %u", me, 3 + baseDim, nin->dim, baseDim); biffAdd(GAGE, err); return 1; } if (airEnumValCheck(nrrdBoundary, boundary)) { sprintf(err, "%s: %d not a valid %s value", me, boundary, nrrdBoundary->name); biffAdd(GAGE, err); return 1; } mop = airMopNew(); kspec = nrrdKernelSpecCopy(_kspec); if (!kspec) { sprintf(err, "%s: problem copying kernel spec", me); biffAdd(GAGE, err); airMopError(mop); return 1; } airMopAdd(mop, kspec, (airMopper)nrrdKernelSpecNix, airMopAlways); /* pre-allocate output Nrrds in case not already there */ for (blidx=0; blidx<blnum; blidx++) { if (!nblur[blidx]) { sprintf(err, "%s: got NULL nblur[%u]", me, blidx); biffAdd(GAGE, err); airMopError(mop); return 1; } } rsmc = nrrdResampleContextNew(); airMopAdd(mop, rsmc, (airMopper)nrrdResampleContextNix, airMopAlways); E = 0; if (!E) E |= nrrdResampleDefaultCenterSet(rsmc, nrrdDefaultCenter); if (!E) E |= nrrdResampleNrrdSet(rsmc, nin); if (baseDim) { unsigned int bai; for (bai=0; bai<baseDim; bai++) { if (!E) E |= nrrdResampleKernelSet(rsmc, bai, NULL, NULL); } } for (axi=0; axi<3; axi++) { if (!E) E |= nrrdResampleSamplesSet(rsmc, baseDim + axi, nin->axis[baseDim + axi].size); if (!E) E |= nrrdResampleRangeFullSet(rsmc, baseDim + axi); } if (!E) E |= nrrdResampleBoundarySet(rsmc, boundary); if (!E) E |= nrrdResampleTypeOutSet(rsmc, nrrdTypeDefault); if (!E) E |= nrrdResampleRenormalizeSet(rsmc, renormalize); if (E) { fprintf(stderr, "%s: trouble setting up resampling\n", me); biffAdd(GAGE, err); airMopError(mop); return 1; } for (blidx=0; blidx<blnum; blidx++) { kspec->parm[0] = scale[blidx]; for (axi=0; axi<3; axi++) { if (!E) E |= nrrdResampleKernelSet(rsmc, baseDim + axi, kspec->kernel, kspec->parm); } if (verbose) { fprintf(stderr, "%s: resampling %u of %u (scale %g) ... ", me, blidx, blnum, scale[blidx]); fflush(stderr); } if (!E) E |= nrrdResampleExecute(rsmc, nblur[blidx]); if (!E) nrrdKeyValueAdd(nblur[blidx], me, "true"); sprintf(val, "%g", scale[blidx]); if (!E) nrrdKeyValueAdd(nblur[blidx], keyscl, val); nrrdKernelSpecSprint(val, kspec); if (!E) nrrdKeyValueAdd(nblur[blidx], keykern, val); if (E) { if (verbose) { fprintf(stderr, "problem!\n"); } sprintf(err, "%s: trouble resampling %u of %u (scale %g)", me, blidx, blnum, scale[blidx]); biffAdd(GAGE, err); airMopError(mop); return 1; } if (verbose) { fprintf(stderr, "done.\n"); } } airMopOkay(mop); return 0; }