From: <kin...@us...> - 2008-06-25 23:36:41
|
Revision: 3863 http://teem.svn.sourceforge.net/teem/?rev=3863&view=rev Author: kindlmann Date: 2008-06-25 16:36:45 -0700 (Wed, 25 Jun 2008) Log Message: ----------- Some API additions, an API change (for a function nobody should have been using), and tweaks to string-based kernel identification API NEW: added nrrdSaveMulti(), which is like nrrdSave(), but saves an array of Nrrds to some set of files, based on a sprintf-style formatted string. API CHANGE: changed: _nrrdContainsPercentDAndMore(const char *str) to the slightly more general purpose: _nrrdContainsPercentThisAndMore(const char *str, char this) The only reason this was a public function was so that unrrdu/dice could use it, but unrrdu/dice should be changed to use the new nrrdSaveMulti anyway. API NEW: added int nrrdKernelSpecSprint(char str[AIR_STRLEN_LARGE], NrrdKernelSpec *ksp) which is like the opposite of nrrdKernelSpecParse(), it prints the kernel specification to a string. New code added to test/tkernel.c to demo this (as well as adding airMop use). To make the sprinted result parse'able, it was discovered that sometimes the kernel's self-identification wasn't actually recognized as identifying that kernel by nrrdKernelParse(). This has been fixed. As part of that clean up, some single-letter kernel identifications were removed from _nrrdKernelStrToKern, because they were too clever, and too apt to be confused: - if (!strcmp("z", str)) return nrrdKernelZero; - if (!strcmp("b", str)) return nrrdKernelBox; - if (!strcmp("t", str)) return nrrdKernelTent; - if (!strcmp("c", str)) return nrrdKernelBCCubic; - if (!strcmp("cd", str)) return nrrdKernelBCCubicD; - if (!strcmp("cdd", str)) return nrrdKernelBCCubicDD; - if (!strcmp("q", str)) return nrrdKernelAQuartic; - if (!strcmp("qd", str)) return nrrdKernelAQuarticD; - if (!strcmp("qdd", str)) return nrrdKernelAQuarticDD; - if (!strcmp("g", str)) return nrrdKernelGaussian; Modified Paths: -------------- teem/trunk/src/nrrd/kernel.c teem/trunk/src/nrrd/nrrd.h teem/trunk/src/nrrd/parseNrrd.c teem/trunk/src/nrrd/test/tkernel.c teem/trunk/src/nrrd/write.c Modified: teem/trunk/src/nrrd/kernel.c =================================================================== --- teem/trunk/src/nrrd/kernel.c 2008-06-25 23:36:14 UTC (rev 3862) +++ teem/trunk/src/nrrd/kernel.c 2008-06-25 23:36:45 UTC (rev 3863) @@ -394,7 +394,7 @@ NrrdKernel _nrrdKernelHermiteFlag = { - "hermite flag", + "hermiteFlag", 0, _nrrdHermiteSup,_nrrdHermiteInt, _nrrdHermite1_f, _nrrdHermiteN_f, _nrrdHermite1_d, _nrrdHermiteN_d }; @@ -1277,7 +1277,7 @@ static NrrdKernel _DDc3quint = { - "C3Quintic", + "C3QuinticDD", 1, _DDc3quintSup, _DDc3quintInt, _DDc3quint1_f, _DDc3quintN_f, _DDc3quint1_d, _DDc3quintN_d }; @@ -1530,7 +1530,7 @@ static NrrdKernel _DDc4hex = { - "C4hexic", + "C4hexicDD", 1, _DDc4hexSup, _DDc4hexInt, _DDc4hex1_f, _DDc4hexN_f, _DDc4hex1_d, _DDc4hexN_d }; @@ -1884,36 +1884,34 @@ _nrrdKernelStrToKern(char *str) { if (!strcmp("zero", str)) return nrrdKernelZero; - if (!strcmp("z", str)) return nrrdKernelZero; if (!strcmp("box", str)) return nrrdKernelBox; - if (!strcmp("b", str)) return nrrdKernelBox; if (!strcmp("cheap", str)) return nrrdKernelCheap; + if (!strcmp("hermiteFlag", str)) return nrrdKernelHermiteFlag; if (!strcmp("hermite", str)) return nrrdKernelHermiteFlag; if (!strcmp("herm", str)) return nrrdKernelHermiteFlag; if (!strcmp("tent", str)) return nrrdKernelTent; - if (!strcmp("t", str)) return nrrdKernelTent; if (!strcmp("forwdiff", str)) return nrrdKernelForwDiff; if (!strcmp("fordif", str)) return nrrdKernelForwDiff; if (!strcmp("centdiff", str)) return nrrdKernelCentDiff; if (!strcmp("cendif", str)) return nrrdKernelCentDiff; + if (!strcmp("bccubic", str)) return nrrdKernelBCCubic; if (!strcmp("cubic", str)) return nrrdKernelBCCubic; - if (!strcmp("c", str)) return nrrdKernelBCCubic; + if (!strcmp("bccubicd", str)) return nrrdKernelBCCubicD; if (!strcmp("cubicd", str)) return nrrdKernelBCCubicD; - if (!strcmp("cd", str)) return nrrdKernelBCCubicD; + if (!strcmp("bccubicdd", str)) return nrrdKernelBCCubicDD; if (!strcmp("cubicdd", str)) return nrrdKernelBCCubicDD; - if (!strcmp("cdd", str)) return nrrdKernelBCCubicDD; + if (!strcmp("aquartic", str)) return nrrdKernelAQuartic; if (!strcmp("quartic", str)) return nrrdKernelAQuartic; - if (!strcmp("q", str)) return nrrdKernelAQuartic; + if (!strcmp("aquarticd", str)) return nrrdKernelAQuarticD; if (!strcmp("quarticd", str)) return nrrdKernelAQuarticD; - if (!strcmp("qd", str)) return nrrdKernelAQuarticD; + if (!strcmp("aquarticdd", str)) return nrrdKernelAQuarticDD; if (!strcmp("quarticdd", str)) return nrrdKernelAQuarticDD; - if (!strcmp("qdd", str)) return nrrdKernelAQuarticDD; if (!strcmp("c3quintic", str)) return nrrdKernelC3Quintic; if (!strcmp("c3q", str)) return nrrdKernelC3Quintic; if (!strcmp("c3quinticd", str)) return nrrdKernelC3QuinticD; if (!strcmp("c3qd", str)) return nrrdKernelC3QuinticD; if (!strcmp("c3quinticdd", str)) return nrrdKernelC3QuinticDD; - if (!strcmp("c3qdd", str)) return nrrdKernelC3QuinticDD; + if (!strcmp("c3qdd", str)) return nrrdKernelC3QuinticDD; if (!strcmp("c4hexic", str)) return nrrdKernelC4Hexic; if (!strcmp("c4h", str)) return nrrdKernelC4Hexic; if (!strcmp("c4hexicd", str)) return nrrdKernelC4HexicD; @@ -1922,7 +1920,6 @@ if (!strcmp("c4hdd", str)) return nrrdKernelC4HexicDD; if (!strcmp("gaussian", str)) return nrrdKernelGaussian; if (!strcmp("gauss", str)) return nrrdKernelGaussian; - if (!strcmp("g", str)) return nrrdKernelGaussian; if (!strcmp("gaussiand", str)) return nrrdKernelGaussianD; if (!strcmp("gaussd", str)) return nrrdKernelGaussianD; if (!strcmp("gd", str)) return nrrdKernelGaussianD; @@ -1934,6 +1931,7 @@ if (!strcmp("dg", str)) return nrrdKernelDiscreteGaussian; if (!strcmp("dgauss", str)) return nrrdKernelDiscreteGaussian; if (!strcmp("dgaussian", str)) return nrrdKernelDiscreteGaussian; + if (!strcmp("discretegauss", str)) return nrrdKernelDiscreteGaussian; if (!strcmp("hann", str)) return nrrdKernelHann; if (!strcmp("hannd", str)) return nrrdKernelHannD; if (!strcmp("hanndd", str)) return nrrdKernelHannDD; @@ -2137,3 +2135,39 @@ nrrdKernelSpecSet(ksp, kern, kparm); return 0; } + +/* +** note that the given string has to be allocated for a certain size +** which is plenty big +*/ +int +nrrdKernelSpecSprint(char str[AIR_STRLEN_LARGE], NrrdKernelSpec *ksp) { + char me[]="nrrdKernelSpecSprint", err[BIFF_STRLEN]; + unsigned int warnLen = AIR_STRLEN_LARGE/2; + + if (!( str && ksp )) { + sprintf(err, "%s: got NULL pointer", me); + biffAdd(NRRD, err); return 1; + } + + if (strlen(ksp->kernel->name) > warnLen) { + sprintf(err, "%s: kernel name (len " _AIR_SIZE_T_CNV + ") might lead to overflow", me, + strlen(ksp->kernel->name)); + biffAdd(NRRD, err); return 1; + } + strcpy(str, ksp->kernel->name); + if (ksp->kernel->numParm) { + unsigned int ki; + char sp[AIR_STRLEN_LARGE]; + for (ki=0; ki<ksp->kernel->numParm; ki++) { + sprintf(sp, "%c%g", (!ki ? ':' : ','), ksp->parm[ki]); + if (strlen(str) + strlen(sp) > warnLen) { + sprintf(err, "%s: kernel parm %u might lead to overflow", me, ki); + biffAdd(NRRD, err); return 1; + } + strcat(str, sp); + } + } + return 0; +} Modified: teem/trunk/src/nrrd/nrrd.h =================================================================== --- teem/trunk/src/nrrd/nrrd.h 2008-06-25 23:36:14 UTC (rev 3862) +++ teem/trunk/src/nrrd/nrrd.h 2008-06-25 23:36:45 UTC (rev 3863) @@ -905,7 +905,7 @@ NrrdIoState *nio, int useBiff); NRRD_EXPORT unsigned int _nrrdDataFNNumber(NrrdIoState *nio); -NRRD_EXPORT int _nrrdContainsPercentDAndMore(char *str); +NRRD_EXPORT int _nrrdContainsPercentThisAndMore(const char *str, char this); NRRD_EXPORT int _nrrdDataFNCheck(NrrdIoState *nio, Nrrd *nrrd, int useBiff); /* read.c */ @@ -928,6 +928,9 @@ NRRD_EXPORT const NrrdFormat *nrrdIoStateFormatGet(NrrdIoState *nio); NRRD_EXPORT int nrrdSave(const char *filename, const Nrrd *nrrd, NrrdIoState *nio); +NRRD_EXPORT int nrrdSaveMulti(const char *fnameFormat, + const Nrrd *const *nin, unsigned int ninLen, + unsigned int numStart, NrrdIoState *nio); NRRD_EXPORT int nrrdWrite(FILE *file, const Nrrd *nrrd, NrrdIoState *nio); NRRD_EXPORT int nrrdStringWrite(char **stringP, const Nrrd *nrrd, @@ -1305,8 +1308,9 @@ double *parm, const char *str); NRRD_EXPORT int nrrdKernelSpecParse(NrrdKernelSpec *ksp, const char *str); +NRRD_EXPORT int nrrdKernelSpecSprint(char str[AIR_STRLEN_LARGE], + NrrdKernelSpec *ksp); - /* ---- END non-NrrdIO */ #ifdef __cplusplus Modified: teem/trunk/src/nrrd/parseNrrd.c =================================================================== --- teem/trunk/src/nrrd/parseNrrd.c 2008-06-25 23:36:14 UTC (rev 3862) +++ teem/trunk/src/nrrd/parseNrrd.c 2008-06-25 23:36:45 UTC (rev 3863) @@ -1099,8 +1099,8 @@ } int -_nrrdContainsPercentDAndMore(char *str) { - char *hh, *tmp; +_nrrdContainsPercentThisAndMore(const char *str, char this) { + const char *hh, *tmp; tmp = str; do { @@ -1117,7 +1117,7 @@ } while (tmp[0]); hh++; hh += strspn(hh, "0123456789"); - if (!( hh[0] == 'd' )) { + if (!( hh[0] == this )) { return 0; } hh += strcspn(hh, _nrrdFieldSep); @@ -1224,7 +1224,10 @@ } airMopAdd(mop, info, airFree, airMopAlways); - if (_nrrdContainsPercentDAndMore(info)) { + /* HEY: this change should be made someday + if (_nrrdContainsPercentThisAndMore(info, 'd') + || _nrrdContainsPercentThisAndMore(info, 'u')) { */ + if (_nrrdContainsPercentThisAndMore(info, 'd')) { /* ---------------------------------------------------------- */ /* --------- format.%d <min> <max> <step> [<dim>] ----------- */ /* ---------------------------------------------------------- */ Modified: teem/trunk/src/nrrd/test/tkernel.c =================================================================== --- teem/trunk/src/nrrd/test/tkernel.c 2008-06-25 23:36:14 UTC (rev 3862) +++ teem/trunk/src/nrrd/test/tkernel.c 2008-06-25 23:36:45 UTC (rev 3863) @@ -43,13 +43,15 @@ int main(int argc, char *argv[]) { - char *me, *kS, *minS, *stepS, *maxS, *outS; + char *me, *kS, *minS, *stepS, *maxS, *outS, *err, kstr[AIR_STRLEN_LARGE]; const NrrdKernel *k; + NrrdKernelSpec *ksp; double parm[NRRD_KERNEL_PARMS_NUM], min, step, max, integral, *dom_d, *ran_d; float *dom_f, *ran_f, v, r_f, r_d; FILE *fout; int i, len; + airArray *mop; me = argv[0]; if (6 != argc) { @@ -61,10 +63,23 @@ maxS = argv[4]; outS = argv[5]; + mop = airMopNew(); if (nrrdKernelParse(&k, parm, kS)) { - fprintf(stderr, "%s: trouble:\n%s\n", me, biffGet(NRRD)); + airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways); + fprintf(stderr, "%s: trouble:\n%s\n", me, err); + airMopError(mop); exit(1); } + ksp = nrrdKernelSpecNew(); + airMopAdd(mop, ksp, (airMopper)nrrdKernelSpecNix, airMopAlways); + nrrdKernelSpecSet(ksp, k, parm); + if (nrrdKernelSpecSprint(kstr, ksp)) { + airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways); + fprintf(stderr, "%s: trouble:\n%s\n", me, err); + airMopError(mop); + exit(1); + } + printf("%s: printed kernel as \"%s\"\n", me, kstr); if (3 != (sscanf(minS, "%lf", &min) + sscanf(stepS, "%lf", &step) + sscanf(maxS, "%lf", &max))) { @@ -90,6 +105,10 @@ fprintf(stderr, "%s: PANIC: couldn't allocate buffers\n", me); exit(1); } + airMopAdd(mop, dom_d, airFree, airMopAlways); + airMopAdd(mop, ran_d, airFree, airMopAlways); + airMopAdd(mop, dom_f, airFree, airMopAlways); + airMopAdd(mop, ran_f, airFree, airMopAlways); /* set values in both domains */ i=0; for (v=min; v<=max; v+=step) { @@ -142,12 +161,9 @@ fprintf(fout, "%g %g\n", dom_f[i], ran_f[i]); } fclose(fout); - - free(dom_d); - free(ran_d); - free(dom_f); - free(ran_f); + fprintf(stderr, "(for matlab:)\n"); fprintf(stderr, "x = dlmread(\'%s\', \' \'); plot(x(:,1), x(:,2));\n", outS); + airMopOkay(mop); exit(0); } Modified: teem/trunk/src/nrrd/write.c =================================================================== --- teem/trunk/src/nrrd/write.c 2008-06-25 23:36:14 UTC (rev 3862) +++ teem/trunk/src/nrrd/write.c 2008-06-25 23:36:45 UTC (rev 3863) @@ -1028,3 +1028,50 @@ airMopOkay(mop); return 0; } + +int +nrrdSaveMulti(const char *fnameFormat, const Nrrd *const *nin, + unsigned int ninLen, unsigned int numStart, NrrdIoState *nio) { + char me[]="nrrdSaveMulti", err[BIFF_STRLEN], *fname; + airArray *mop; + unsigned int nii; + + if (!( fnameFormat && nin )) { + sprintf(err, "%s: got NULL pointer", me); + biffAdd(NRRD, err); return 1; + } + if (!( _nrrdContainsPercentThisAndMore(fnameFormat, 'u') )) { + sprintf(err, "%s: given format \"%s\" doesn't seem to " + "have the \"%%u\" conversion specification to print " + "an unsigned int\n", me, fnameFormat); + biffAdd(NRRD, err); return 1; + } + + mop = airMopNew(); + /* should be big enough for the number replacing the format sequence */ + fname = AIR_CAST(char *, malloc(strlen(fnameFormat) + 128)); + if (!(fname)) { + sprintf(err, "%s: couldn't allocated local fname buffer", me); + biffAdd(NRRD, err); airMopError(mop); return 1; + } + airMopAdd(mop, fname, airFree, airMopAlways); + + for (nii=0; nii<ninLen; nii++) { + unsigned int num; + num = numStart + nii; + sprintf(fname, fnameFormat, num); + if (nrrdSave(fname, nin[nii], nio)) { + sprintf(err, "%s: trouble saving nin[%u] to %s", me, nii, fname); + biffAdd(NRRD, err); airMopError(mop); return 1; + } + /* HEY: GLK hopes that the nio doesn't have any state that needs + resetting, but we can't call nrrdIoStateInit() because that + would negate the purpose of sending in the nio for all but the + first saved nrrd */ + } + + airMopOkay(mop); + return 0; +} + + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |