From: Gordon K. <kin...@us...> - 2006-03-23 11:39:59
|
Update of /cvsroot/teem/teem/src/nrrd In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5640 Modified Files: accessors.c convertNrrd.c map.c nrrd.h privateNrrd.h Log Message: slightly cleaning up macro craziness, moving nrrdFClamp[] and nrrdDClamp[] function arrays from accessors.c to convert.c, to enable creation of new _nrrdClampConv[] function arrays, supporting new function nrrdClampConvert(), which was very long overdue Index: privateNrrd.h =================================================================== RCS file: /cvsroot/teem/teem/src/nrrd/privateNrrd.h,v retrieving revision 1.63 retrieving revision 1.64 diff -C2 -d -r1.63 -r1.64 *** privateNrrd.h 17 Mar 2006 09:29:28 -0000 1.63 --- privateNrrd.h 23 Mar 2006 11:39:54 -0000 1.64 *************** *** 149,152 **** --- 149,153 ---- /* convert.c */ extern void (*_nrrdConv[][NRRD_TYPE_MAX+1])(void *, const void *, size_t); + extern void (*_nrrdClampConv[][NRRD_TYPE_MAX+1])(void *, const void *, size_t); /* read.c */ Index: accessors.c =================================================================== RCS file: /cvsroot/teem/teem/src/nrrd/accessors.c,v retrieving revision 1.37 retrieving revision 1.38 diff -C2 -d -r1.37 -r1.38 *** accessors.c 17 Mar 2006 09:29:28 -0000 1.37 --- accessors.c 23 Mar 2006 11:39:52 -0000 1.38 *************** *** 284,352 **** NULL}; - /* - ******** nrrdFClamp - ** - ** for integral types, clamps a given float to the range representable - ** by by that type; for floating point types we just return - ** the given number, since every float must fit in a double. - */ - static float _nrrdFClampCH(FL v) { return AIR_CLAMP(SCHAR_MIN, v, SCHAR_MAX);} - static float _nrrdFClampUC(FL v) { return AIR_CLAMP(0, v, UCHAR_MAX);} - static float _nrrdFClampSH(FL v) { return AIR_CLAMP(SHRT_MIN, v, SHRT_MAX);} - static float _nrrdFClampUS(FL v) { return AIR_CLAMP(0, v, USHRT_MAX);} - static float _nrrdFClampIN(FL v) { return AIR_CLAMP(INT_MIN, v, INT_MAX);} - static float _nrrdFClampUI(FL v) { return AIR_CLAMP(0, v, UINT_MAX);} - static float _nrrdFClampLL(FL v) { return AIR_CLAMP(NRRD_LLONG_MIN, v, - NRRD_LLONG_MAX);} - static float _nrrdFClampUL(FL v) { return AIR_CLAMP(0, v, NRRD_ULLONG_MAX);} - static float _nrrdFClampFL(FL v) { return v; } - static float _nrrdFClampDB(FL v) { return v; } - float (* - nrrdFClamp[NRRD_TYPE_MAX+1])(FL) = { - NULL, - _nrrdFClampCH, - _nrrdFClampUC, - _nrrdFClampSH, - _nrrdFClampUS, - _nrrdFClampIN, - _nrrdFClampUI, - _nrrdFClampLL, - _nrrdFClampUL, - _nrrdFClampFL, - _nrrdFClampDB, - NULL}; - - /* - ******** nrrdDClamp - ** - ** same as nrrdDClamp, but for doubles. One change: in the case of - ** floats, doubles are clamped to the range -FLT_MAX to FLT_MAX. - */ - static double _nrrdDClampCH(DB v) { return AIR_CLAMP(SCHAR_MIN, v, SCHAR_MAX);} - static double _nrrdDClampUC(DB v) { return AIR_CLAMP(0, v, UCHAR_MAX);} - static double _nrrdDClampSH(DB v) { return AIR_CLAMP(SHRT_MIN, v, SHRT_MAX);} - static double _nrrdDClampUS(DB v) { return AIR_CLAMP(0, v, USHRT_MAX);} - static double _nrrdDClampIN(DB v) { return AIR_CLAMP(INT_MIN, v, INT_MAX);} - static double _nrrdDClampUI(DB v) { return AIR_CLAMP(0, v, UINT_MAX);} - static double _nrrdDClampLL(DB v) { return AIR_CLAMP(NRRD_LLONG_MIN, v, - NRRD_LLONG_MAX);} - static double _nrrdDClampUL(DB v) { return AIR_CLAMP(0, v, NRRD_ULLONG_MAX);} - static double _nrrdDClampFL(DB v) { return AIR_CLAMP(-FLT_MAX, v, FLT_MAX); } - static double _nrrdDClampDB(DB v) { return v; } - double (* - nrrdDClamp[NRRD_TYPE_MAX+1])(DB) = { - NULL, - _nrrdDClampCH, - _nrrdDClampUC, - _nrrdDClampSH, - _nrrdDClampUS, - _nrrdDClampIN, - _nrrdDClampUI, - _nrrdDClampLL, - _nrrdDClampUL, - _nrrdDClampFL, - _nrrdDClampDB, - NULL}; - /* about here is where Gordon admits he might have some use for C++ */ --- 284,287 ---- Index: map.c =================================================================== RCS file: /cvsroot/teem/teem/src/nrrd/map.c,v retrieving revision 1.58 retrieving revision 1.59 diff -C2 -d -r1.58 -r1.59 *** map.c 14 Jan 2006 20:20:16 -0000 1.58 --- map.c 23 Mar 2006 11:39:52 -0000 1.59 *************** *** 133,151 **** */ ! /* ! ******** nrrdConvert() ! ** ! ** copies values from one type of nrrd to another, without any ! ** transformation, except what you get with a cast. The point is to ! ** make available on Nrrds the exact same behavior as you have in C ! ** with casts and assignments. ! */ ! int ! nrrdConvert(Nrrd *nout, const Nrrd *nin, int type) { ! char me[]="nrrdConvert", typeS[AIR_STRLEN_SMALL], err[BIFF_STRLEN]; size_t num, size[NRRD_DIM_MAX]; if (!( nin && nout ! && !airEnumValCheck(nrrdType, nin->type) && !airEnumValCheck(nrrdType, type) )) { sprintf(err, "%s: invalid args", me); --- 133,143 ---- */ ! static int ! clampConvert(Nrrd *nout, const Nrrd *nin, int type, int doClamp) { ! char me[]="clampConvert", typeS[AIR_STRLEN_SMALL], err[BIFF_STRLEN]; size_t num, size[NRRD_DIM_MAX]; if (!( nin && nout ! && !nrrdCheck(nin) && !airEnumValCheck(nrrdType, type) )) { sprintf(err, "%s: invalid args", me); *************** *** 198,202 **** /* call the appropriate converter */ num = nrrdElementNumber(nin); ! _nrrdConv[nout->type][nin->type](nout->data, nin->data, num); nout->blockSize = 0; --- 190,198 ---- /* call the appropriate converter */ num = nrrdElementNumber(nin); ! if (doClamp) { ! _nrrdClampConv[nout->type][nin->type](nout->data, nin->data, num); ! } else { ! _nrrdConv[nout->type][nin->type](nout->data, nin->data, num); ! } nout->blockSize = 0; *************** *** 228,231 **** --- 224,263 ---- /* + ******** nrrdConvert() + ** + ** copies values from one type of nrrd to another, without any + ** transformation, except what you get with a cast. The point is to + ** make available on Nrrds the exact same behavior as you have in C + ** with casts and assignments. + */ + int + nrrdConvert(Nrrd *nout, const Nrrd *nin, int type) { + char me[]="nrrdConvert", err[BIFF_STRLEN]; + + if (clampConvert(nout, nin, type, AIR_FALSE)) { + sprintf(err, "%s: trouble", me); + biffAdd(NRRD, err); return 1; + } + return 0; + } + + /* + ******** nrrdClampConvert() + ** + ** same as nrrdConvert, but with clamping to output value representation range + ** so as to avoid wrap-around artifacts + */ + int + nrrdClampConvert(Nrrd *nout, const Nrrd *nin, int type) { + char me[]="nrrdClampConvert", err[BIFF_STRLEN]; + + if (clampConvert(nout, nin, type, AIR_TRUE)) { + sprintf(err, "%s: trouble", me); + biffAdd(NRRD, err); return 1; + } + return 0; + } + + /* ******** nrrdQuantize() ** Index: nrrd.h =================================================================== RCS file: /cvsroot/teem/teem/src/nrrd/nrrd.h,v retrieving revision 1.198 retrieving revision 1.199 diff -C2 -d -r1.198 -r1.199 *** nrrd.h 20 Mar 2006 11:06:36 -0000 1.198 --- nrrd.h 23 Mar 2006 11:39:53 -0000 1.199 *************** *** 947,952 **** /* ---- BEGIN non-NrrdIO */ NRRD_EXPORT int (*nrrdFprint[NRRD_TYPE_MAX+1])(FILE *, const void *); - NRRD_EXPORT float (*nrrdFClamp[NRRD_TYPE_MAX+1])(float); - NRRD_EXPORT double (*nrrdDClamp[NRRD_TYPE_MAX+1])(double); NRRD_EXPORT void (*nrrdMinMaxExactFind[NRRD_TYPE_MAX+1])(void *minP, void *maxP, --- 947,950 ---- *************** *** 1024,1028 **** --- 1022,1029 ---- /******** some of the point-wise value remapping, conversion, and such */ /* map.c */ + NRRD_EXPORT float (*nrrdFClamp[NRRD_TYPE_MAX+1])(float); + NRRD_EXPORT double (*nrrdDClamp[NRRD_TYPE_MAX+1])(double); NRRD_EXPORT int nrrdConvert(Nrrd *nout, const Nrrd *nin, int type); + NRRD_EXPORT int nrrdClampConvert(Nrrd *nout, const Nrrd *nin, int type); NRRD_EXPORT int nrrdQuantize(Nrrd *nout, const Nrrd *nin, const NrrdRange *range, unsigned int bits); Index: convertNrrd.c =================================================================== RCS file: /cvsroot/teem/teem/src/nrrd/convertNrrd.c,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** convertNrrd.c 14 Jan 2006 20:20:16 -0000 1.14 --- convertNrrd.c 23 Mar 2006 11:39:52 -0000 1.15 *************** *** 95,110 **** */ #define CONV_DEF(TA, TB) \ ! void _nrrdConv##TA##TB(TA *a, const TB *b, IT N) { while (N--) a[N]=(TA)b[N]; } /* ** the individual converter's appearance in the array initialization, ! ** using the cast to the "CF" typedef defined below */ #define CONV_LIST(TA, TB) (CF)_nrrdConv##TA##TB, /* ! ** the brace-delimited list of all converters _to_ type TYPE */ ! #define CONVTO_LIST(_dummy_, TYPE) {NULL, MAP2(CONV_LIST, TYPE) NULL}, --- 95,139 ---- */ #define CONV_DEF(TA, TB) \ ! static void \ ! _nrrdConv##TA##TB(TA *a, const TB *b, IT N) { \ ! size_t ii; \ ! for (ii=0; ii<N; ii++) { \ ! a[ii] = AIR_CAST(TA, b[ii]); \ ! } \ ! } ! ! /* ! ** _nrrdClCv<Ta><Tb>() ! ** ! ** same as _nrrdConv<Ta><Tb>(), but with clamping to the representable ! ** range of values of the output type, using a double as intermediate ! ** storage type. ! */ ! #define CLCV_DEF(TA, TB) \ ! static void \ ! _nrrdClCv##TA##TB(TA *a, const TB *b, IT N) { \ ! size_t ii; \ ! for (ii=0; ii<N; ii++) { \ ! a[ii] = AIR_CAST(TA, _nrrdDClamp##TA(b[ii])); \ ! } \ ! } ! ! /* ! ** This typedef makes the definition of _nrrdConv[][] shorter ! */ ! typedef void (*CF)(void *, const void *, IT); /* ** the individual converter's appearance in the array initialization, ! ** using the cast to the "CF" typedef */ #define CONV_LIST(TA, TB) (CF)_nrrdConv##TA##TB, + #define CLCV_LIST(TA, TB) (CF)_nrrdClCv##TA##TB, /* ! ** the brace-delimited list of all converters _to_ type TA */ ! #define CONVTO_LIST(_dummy_, TA) {NULL, MAP2(CONV_LIST, TA) NULL}, ! #define CLCVTO_LIST(_dummy_, TA) {NULL, MAP2(CLCV_LIST, TA) NULL}, *************** *** 115,128 **** /* ! ** This typedef makes the definition of _nrrdConv[][] shorter */ ! typedef void (*CF)(void *, const void *, IT); /* ! ** Define all 100 of the individual converters. */ MAP1(MAP2, CONV_DEF) /* --- 144,224 ---- + /* + ** the clamping functions where moved here from accessors.c in order + ** to create the combined clamp-and-convert functions + */ /* ! ******** nrrdFClamp ! ** ! ** for integral types, clamps a given float to the range representable ! ** by that type; for floating point types we just return ! ** the given number, since every float must fit in a double. */ ! static float _nrrdFClampCH(FL v) { return AIR_CLAMP(SCHAR_MIN, v, SCHAR_MAX);} ! static float _nrrdFClampUC(FL v) { return AIR_CLAMP(0, v, UCHAR_MAX);} ! static float _nrrdFClampSH(FL v) { return AIR_CLAMP(SHRT_MIN, v, SHRT_MAX);} ! static float _nrrdFClampUS(FL v) { return AIR_CLAMP(0, v, USHRT_MAX);} ! static float _nrrdFClampJN(FL v) { return AIR_CLAMP(INT_MIN, v, INT_MAX);} ! static float _nrrdFClampUI(FL v) { return AIR_CLAMP(0, v, UINT_MAX);} ! static float _nrrdFClampLL(FL v) { return AIR_CLAMP(NRRD_LLONG_MIN, v, ! NRRD_LLONG_MAX);} ! static float _nrrdFClampUL(FL v) { return AIR_CLAMP(0, v, NRRD_ULLONG_MAX);} ! static float _nrrdFClampFL(FL v) { return v; } ! static float _nrrdFClampDB(FL v) { return v; } ! float (* ! nrrdFClamp[NRRD_TYPE_MAX+1])(FL) = { ! NULL, ! _nrrdFClampCH, ! _nrrdFClampUC, ! _nrrdFClampSH, ! _nrrdFClampUS, ! _nrrdFClampJN, ! _nrrdFClampUI, ! _nrrdFClampLL, ! _nrrdFClampUL, ! _nrrdFClampFL, ! _nrrdFClampDB, ! NULL}; ! ! /* ! ******** nrrdDClamp ! ** ! ** same as nrrdDClamp, but for doubles. One change: in the case of ! ** floats, doubles are clamped to the range -FLT_MAX to FLT_MAX. ! */ ! static double _nrrdDClampCH(DB v) { return AIR_CLAMP(SCHAR_MIN, v, SCHAR_MAX);} ! static double _nrrdDClampUC(DB v) { return AIR_CLAMP(0, v, UCHAR_MAX);} ! static double _nrrdDClampSH(DB v) { return AIR_CLAMP(SHRT_MIN, v, SHRT_MAX);} ! static double _nrrdDClampUS(DB v) { return AIR_CLAMP(0, v, USHRT_MAX);} ! static double _nrrdDClampJN(DB v) { return AIR_CLAMP(INT_MIN, v, INT_MAX);} ! static double _nrrdDClampUI(DB v) { return AIR_CLAMP(0, v, UINT_MAX);} ! static double _nrrdDClampLL(DB v) { return AIR_CLAMP(NRRD_LLONG_MIN, v, ! NRRD_LLONG_MAX);} ! static double _nrrdDClampUL(DB v) { return AIR_CLAMP(0, v, NRRD_ULLONG_MAX);} ! static double _nrrdDClampFL(DB v) { return AIR_CLAMP(-FLT_MAX, v, FLT_MAX); } ! static double _nrrdDClampDB(DB v) { return v; } ! double (* ! nrrdDClamp[NRRD_TYPE_MAX+1])(DB) = { ! NULL, ! _nrrdDClampCH, ! _nrrdDClampUC, ! _nrrdDClampSH, ! _nrrdDClampUS, ! _nrrdDClampJN, ! _nrrdDClampUI, ! _nrrdDClampLL, ! _nrrdDClampUL, ! _nrrdDClampFL, ! _nrrdDClampDB, ! NULL}; ! /* ! ** Define all 100 of both converters. */ MAP1(MAP2, CONV_DEF) + MAP1(MAP2, CLCV_DEF) + /* *************** *** 130,138 **** ** ** This generates one incredibly long line of text, which hopefully will not ! ** break a stupid compiler with assumptions about line-length... */ ! CF _nrrdConv[NRRD_TYPE_MAX+1][NRRD_TYPE_MAX+1] = { {NULL}, MAP1(CONVTO_LIST, _dummy_) {NULL} }; --- 226,242 ---- ** ** This generates one incredibly long line of text, which hopefully will not ! ** break a poor compiler with limitations on line-length... */ ! CF ! _nrrdConv[NRRD_TYPE_MAX+1][NRRD_TYPE_MAX+1] = { {NULL}, MAP1(CONVTO_LIST, _dummy_) {NULL} }; + + CF + _nrrdClampConv[NRRD_TYPE_MAX+1][NRRD_TYPE_MAX+1] = { + {NULL}, + MAP1(CLCVTO_LIST, _dummy_) + {NULL} + }; |