IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 12381


Ignore:
Timestamp:
Mar 9, 2007, 10:16:40 AM (19 years ago)
Author:
Paul Price
Message:

Robert Lupton reported that using var_arg for psImageInit gives it no
type safety. Changed this to use a double (we're deprecating complex,
otherwise I would use double complex). Checks to see if the value
will fit (for truncation to integer). Also added optimisation for
char-size integer types. Since psVectorInit used the same system,
overhauled that too in the same way.

Location:
trunk/psLib/src/mathtypes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/mathtypes/psImage.c

    r11699 r12381  
    99 *  @author Ross Harman, MHPCC
    1010 *
    11  *  @version $Revision: 1.123 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2007-02-08 03:10:33 $
     11 *  @version $Revision: 1.124 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2007-03-09 20:16:40 $
    1313 *
    1414 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    121121}
    122122
    123 bool psImageInit (psImage *image,...)
    124 {
    125     PS_ASSERT_PTR(image, false);
    126     PS_ASSERT_IMAGE_NON_NULL(image, false);
    127 
    128     va_list argp;
    129     va_start (argp, image);
    130 
    131     switch (image->type.type) {
    132         #define IMAGEINIT_INTCASE(TYPE,NATIVETYPE) \
     123
     124
     125// Image initialisation for integer types
     126#define IMAGEINIT_INTCASE(TYPE) \
    133127    case PS_TYPE_##TYPE: { \
    134             NATIVETYPE temp = va_arg(argp, NATIVETYPE); \
    135             if ( temp < PS_MIN_##TYPE || temp > PS_MAX_##TYPE ) { \
    136                 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error: Value %d out of Range.\n", temp); \
    137                 return false; \
    138             } \
    139             ps##TYPE value = temp; \
    140             if (value == 0) { \
     128            if (value == 0.0) { \
    141129                size_t numBytes = image->numCols * sizeof(ps##TYPE); \
    142130                for (int y = 0; y < image->numRows; y++) { \
     
    144132                } \
    145133            } else { \
     134                if (value < (double)PS_MIN_##TYPE || value > (double)PS_MAX_##TYPE) { \
     135                    psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error: Value %f out of range for type %s.\n", \
     136                            value, #TYPE); \
     137                    return false; \
     138                } \
     139                ps##TYPE castValue = (ps##TYPE)value; \
    146140                for (int iy = 0; iy < image->numRows; iy++) { \
    147141                    ps##TYPE *row = image->data.TYPE[iy]; \
    148142                    for (int ix = 0; ix < image->numCols; ix++) { \
    149                         row[ix] = value; \
     143                        row[ix] = castValue; \
    150144                    } \
    151145                } \
     
    154148        }
    155149
    156         IMAGEINIT_INTCASE(U8,unsigned int)
    157         IMAGEINIT_INTCASE(U16,unsigned int)
    158         IMAGEINIT_INTCASE(U32,unsigned int)
    159         IMAGEINIT_INTCASE(S8,int)
    160         IMAGEINIT_INTCASE(S16,int)
    161         IMAGEINIT_INTCASE(S32,int)
    162 
    163         #define IMAGEINIT_LONGCASE(TYPE,NATIVETYPE) \
     150// Image initialisation for char-size integer types
     151#define IMAGEINIT_CHARCASE(TYPE) \
    164152    case PS_TYPE_##TYPE: { \
    165             ps##TYPE value = va_arg(argp, NATIVETYPE); \
    166             if (value == 0) { \
     153            if (value < (double)PS_MIN_##TYPE || value > (double)PS_MAX_##TYPE) { \
     154                psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error: Value %f out of range for type %s.\n", \
     155                        value, #TYPE); \
     156                return false; \
     157            } \
     158            size_t numBytes = image->numCols * sizeof(ps##TYPE); \
     159            ps##TYPE castValue = (ps##TYPE)value; \
     160            for (int y = 0; y < image->numRows; y++) { \
     161                memset(image->data.TYPE[y], castValue, numBytes); \
     162            } \
     163            return true; \
     164        }
     165// Image initialisation for floating point types
     166#define IMAGEINIT_FLOATCASE(TYPE) \
     167    case PS_TYPE_##TYPE: { \
     168            if (value == 0.0) { \
    167169                size_t numBytes = image->numCols * sizeof(ps##TYPE); \
    168170                for (int y = 0; y < image->numRows; y++) { \
     
    170172                } \
    171173            } else { \
     174                ps##TYPE castValue = (ps##TYPE)value; \
    172175                for (int iy = 0; iy < image->numRows; iy++) { \
    173176                    ps##TYPE *row = image->data.TYPE[iy]; \
    174177                    for (int ix = 0; ix < image->numCols; ix++) { \
    175                         row[ix] = value; \
     178                        row[ix] = castValue; \
    176179                    } \
    177180                } \
     
    180183        }
    181184
    182         IMAGEINIT_LONGCASE(U64,unsigned long)
    183         IMAGEINIT_LONGCASE(S64,long)
    184 
    185         #define IMAGEINIT_FLOATCASE(TYPE) \
    186     case PS_TYPE_##TYPE: { \
    187             ps##TYPE value = va_arg(argp, psF64); \
    188             if (value == 0) { \
    189                 size_t numBytes = image->numCols * sizeof(ps##TYPE); \
    190                 for (int y = 0; y < image->numRows; y++) { \
    191                     memset(image->data.TYPE[y], 0, numBytes); \
    192                 } \
    193             } else { \
    194                 for (int iy = 0; iy < image->numRows; iy++) { \
    195                     ps##TYPE *row = image->data.TYPE[iy]; \
    196                     for (int ix = 0; ix < image->numCols; ix++) { \
    197                         row[ix] = value; \
    198                     } \
    199                 } \
    200             } \
    201             return true; \
    202         }
    203 
     185
     186bool psImageInit (psImage *image, double value)
     187{
     188    PS_ASSERT_PTR(image, false);
     189    PS_ASSERT_IMAGE_NON_NULL(image, false);
     190
     191    switch (image->type.type) {
     192        IMAGEINIT_CHARCASE(U8)
     193        IMAGEINIT_INTCASE(U16)
     194        IMAGEINIT_INTCASE(U32)
     195        IMAGEINIT_CHARCASE(S8)
     196        IMAGEINIT_INTCASE(S16)
     197        IMAGEINIT_INTCASE(S32)
     198        IMAGEINIT_INTCASE(U64)
     199        IMAGEINIT_INTCASE(S64)
    204200        IMAGEINIT_FLOATCASE(F32)
    205201        IMAGEINIT_FLOATCASE(F64)
    206 
    207202    default:
    208         psError(PS_ERR_BAD_PARAMETER_TYPE, true, "datatype not defined in psImageInit\n");
     203        psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Type %x not supported", image->type.type);
    209204    }
    210205    return (false);
  • trunk/psLib/src/mathtypes/psImage.h

    r11700 r12381  
    99 * @author Joshua Hoblitt, University of Hawaii
    1010 *
    11  * @version $Revision: 1.88 $ $Name: not supported by cvs2svn $
    12  * @date $Date: 2007-02-08 03:26:15 $
     11 * @version $Revision: 1.89 $ $Name: not supported by cvs2svn $
     12 * @date $Date: 2007-03-09 20:16:40 $
    1313 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
    1414 */
     
    116116/** Checks the type of a particular pointer.
    117117 *
    118  *  Uses the appropriate deallocation function in psMemBlock to check the ptr * datatype. 
     118 *  Uses the appropriate deallocation function in psMemBlock to check the ptr * datatype.
    119119 *  @return bool:       True if the pointer matches a psImage structure, false otherwise.
    120120 */
     
    131131bool psImageInit(
    132132    psImage *image,                    ///< the image to be initialized
    133     ...                                ///< Variable argument list for initialization
     133    double value                        ///< Value to which to initialise
    134134);
    135135
  • trunk/psLib/src/mathtypes/psVector.c

    r11698 r12381  
    1010*  @author Joshua Hoblitt, University of Hawaii
    1111*
    12 *  @version $Revision: 1.94 $ $Name: not supported by cvs2svn $
    13 *  @date $Date: 2007-02-08 02:59:36 $
     12*  @version $Revision: 1.95 $ $Name: not supported by cvs2svn $
     13*  @date $Date: 2007-03-09 20:16:40 $
    1414*
    1515*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    678678}
    679679
    680 
    681 bool psVectorInit(psVector *vector,
    682                   ...)
    683 {
    684     va_list argp;
    685     psU8  vU8;
    686     psU16 vU16;
    687     psU32 vU32;
    688     psU64 vU64;
    689     psS8 vS8;
    690     psS16 vS16;
    691     psS32 vS32;
    692     psS64 vS64;
    693     psF32 vF32;
    694     psF64 vF64;
    695     psC32 vC32;
    696     psC64 vC64;
    697 
    698     unsigned int temp;
    699     int temp2;
    700 
    701     if (vector == NULL)
    702         return (false);
    703 
    704     va_start (argp, vector);
     680// Image initialisation for integer types
     681#define VECTORINIT_INTCASE(TYPE) \
     682    case PS_TYPE_##TYPE: { \
     683            if (value == 0.0) { \
     684                memset(vector->data.TYPE, 0, vector->n * sizeof(ps##TYPE)); \
     685            } else { \
     686                if (value < (double)PS_MIN_##TYPE || value > (double)PS_MAX_##TYPE) { \
     687                    psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error: Value %f out of range for type %s.\n", \
     688                            value, #TYPE); \
     689                    return false; \
     690                } \
     691                ps##TYPE castValue = (ps##TYPE)value; \
     692                ps##TYPE *vectorData = vector->data.TYPE; \
     693                for (int i = 0; i < vector->n; i++) { \
     694                    vectorData[i] = castValue; \
     695                } \
     696            } \
     697            return true; \
     698        }
     699
     700// Image initialisation for char-size integer types
     701#define VECTORINIT_CHARCASE(TYPE) \
     702    case PS_TYPE_##TYPE: { \
     703            if (value < (double)PS_MIN_##TYPE || value > (double)PS_MAX_##TYPE) { \
     704                psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error: Value %f out of range for type %s.\n", \
     705                        value, #TYPE); \
     706                return false; \
     707            } \
     708            memset(vector->data.TYPE, (ps##TYPE)value, vector->n * sizeof(ps##TYPE)); \
     709            return true; \
     710        }
     711
     712// Image initialisation for floating point types
     713#define VECTORINIT_FLOATCASE(TYPE) \
     714    case PS_TYPE_##TYPE: { \
     715            if (value == 0.0) { \
     716                memset(vector->data.TYPE, 0, vector->n * sizeof(ps##TYPE)); \
     717            } else { \
     718                ps##TYPE castValue = (ps##TYPE)value; \
     719                ps##TYPE *vectorData = vector->data.TYPE; \
     720                for (int i = 0; i < vector->n; i++) { \
     721                    vectorData[i] = castValue; \
     722                } \
     723            } \
     724            return true; \
     725        }
     726
     727
     728
     729bool psVectorInit(psVector *vector, double value)
     730{
     731    PS_ASSERT_VECTOR_NON_NULL(vector, false);
    705732
    706733    switch (vector->type.type) {
    707     case PS_TYPE_BOOL: {
    708             // This is a little ugly, because psVector doesn't have a boolean type yet.
    709             int temp = va_arg(argp, int);
    710             bool value = temp;
    711             if (!value) {
    712                 memset(vector->data.U8, 0, vector->n * sizeof(bool));
    713             } else {
    714                 for (long i = 0; i < vector->n; i++) {
    715                     vector->data.U8[i] = value;
    716                 }
    717             }
    718             return true;
    719         }
    720     case PS_TYPE_U8:
    721         temp = va_arg (argp, psU32);
    722         if ( temp >= PS_MIN_U8 && temp <= PS_MAX_U8 ) {
    723             vU8 = temp;
    724         } else {
    725             psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error:  U8 Value out of Range.\n");
    726             return false;
    727         }
    728         // Can use memset for char-size values
    729         memset(vector->data.U8, vU8, vector->n * sizeof(psU8));
    730         return (true);
    731     case PS_TYPE_F32:
    732         vF32 = (psF32)va_arg (argp, psF64);
    733         if (vF32 == 0.0) {
    734             memset(vector->data.F32, 0, vector->n * sizeof(psF32));
    735         } else {
    736             for (long iy = 0; iy < vector->n; iy++) {
    737                 vector->data.F32[iy] = vF32;
    738             }
    739         }
    740         return (true);
    741     case PS_TYPE_F64:
    742         vF64 = va_arg (argp, psF64);
    743         if (vF64 == 0.0) {
    744             memset(vector->data.F64, 0, vector->n * sizeof(psF64));
    745         } else {
    746             for (long iy = 0; iy < vector->n; iy++) {
    747                 vector->data.F64[iy] = vF64;
    748             }
    749         }
    750         return (true);
    751     case PS_TYPE_U16:
    752         temp = va_arg (argp, psU32);
    753         if ( temp >= PS_MIN_U16 && temp <= PS_MAX_U16 )
    754             vU16 = temp;
    755         else {
    756             psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error:  U16 Value out of Range.\n");
    757             return false;
    758         }
    759         if (vU16 == 0) {
    760             memset(vector->data.U16, 0, vector->n * sizeof(psU16));
    761         } else {
    762             for (long iy = 0; iy < vector->n; iy++) {
    763                 vector->data.U16[iy] = vU16;
    764             }
    765         }
    766         return (true);
    767     case PS_TYPE_U32:
    768         vU32 = va_arg (argp, psU32);
    769         if (vU32 == 0) {
    770             memset(vector->data.U32, 0, vector->n * sizeof(psU32));
    771         } else {
    772             for (long iy = 0; iy < vector->n; iy++) {
    773                 vector->data.U32[iy] = vU32;
    774             }
    775         }
    776         return (true);
    777     case PS_TYPE_U64:
    778         vU64 = va_arg (argp, psU64);
    779         if (vU64 == 0) {
    780             memset(vector->data.U64, 0, vector->n * sizeof(psU64));
    781         } else {
    782             for (long iy = 0; iy < vector->n; iy++) {
    783                 vector->data.U64[iy] = vU64;
    784             }
    785         }
    786         return (true);
    787     case PS_TYPE_S8:
    788         temp2 = va_arg (argp, psS32);
    789         if ( temp2 >= PS_MIN_S8 && temp2 <= PS_MAX_S8 )
    790             vS8 = temp2;
    791         else {
    792             psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error:  S8 Value out of Range.\n");
    793             return false;
    794         }
    795         // Can use memset for char-size values
    796         memset(vector->data.S8, vS8, vector->n * sizeof(psS8));
    797         return (true);
    798     case PS_TYPE_S16:
    799         temp2 = va_arg (argp, psS32);
    800         if ( temp2 >= PS_MIN_S16 && temp2 <= PS_MAX_S16 )
    801             vS16 = temp2;
    802         else {
    803             psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error:  S16 Value out of Range.\n");
    804             return false;
    805         }
    806         if (vS16 == 0) {
    807             memset(vector->data.S16, 0, vector->n * sizeof(psS16));
    808         } else {
    809             for (long iy = 0; iy < vector->n; iy++) {
    810                 vector->data.S16[iy] = vS16;
    811             }
    812         }
    813         return (true);
    814     case PS_TYPE_S32:
    815         vS32 = va_arg (argp, psS32);
    816         if (vS32 == 0) {
    817             memset(vector->data.S32, 0, vector->n * sizeof(psS32));
    818         } else {
    819             for (long iy = 0; iy < vector->n; iy++) {
    820                 vector->data.S32[iy] = vS32;
    821             }
    822         }
    823         return (true);
    824     case PS_TYPE_S64:
    825         vS64 = va_arg (argp, psS64);
    826         if (vS64 == 0) {
    827             memset(vector->data.S64, 0, vector->n * sizeof(psS64));
    828         } else {
    829             for (long iy = 0; iy < vector->n; iy++) {
    830                 vector->data.S64[iy] = vS64;
    831             }
    832         }
    833         return (true);
    834     case PS_TYPE_C32:
    835         vC32 = va_arg (argp, psC32);
    836         if (vC32 == 0) {
    837             memset(vector->data.C32, 0, vector->n * sizeof(psC32));
    838         } else {
    839             for (long iy = 0; iy < vector->n; iy++) {
    840                 vector->data.C32[iy] = vC32;
    841             }
    842         }
    843         return (true);
    844     case PS_TYPE_C64:
    845         vC64 = va_arg (argp, psC64);
    846         if (vC64 == 0) {
    847             memset(vector->data.C64, 0, vector->n * sizeof(psC64));
    848         } else {
    849             for (long iy = 0; iy < vector->n; iy++) {
    850                 vector->data.C64[iy] = vC64;
    851             }
    852         }
    853         return (true);
     734        VECTORINIT_CHARCASE(U8)
     735        VECTORINIT_INTCASE(U16)
     736        VECTORINIT_INTCASE(U32)
     737        VECTORINIT_CHARCASE(S8)
     738        VECTORINIT_INTCASE(S16)
     739        VECTORINIT_INTCASE(S32)
     740        VECTORINIT_INTCASE(U64)
     741        VECTORINIT_INTCASE(S64)
     742        VECTORINIT_FLOATCASE(F32)
     743        VECTORINIT_FLOATCASE(F64)
    854744    default:
    855         psError (PS_ERR_BAD_PARAMETER_TYPE, true, "datatype not defined in psImageInit\n");
    856         return (false);
     745        psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Type %x not supported", vector->type.type);
    857746    }
    858747    return (false);
  • trunk/psLib/src/mathtypes/psVector.h

    r11698 r12381  
    1010 * @author Joshua Hoblitt, University of Hawaii
    1111 *
    12  * @version $Revision: 1.65 $ $Name: not supported by cvs2svn $
    13  * @date $Date: 2007-02-08 02:59:36 $
     12 * @version $Revision: 1.66 $ $Name: not supported by cvs2svn $
     13 * @date $Date: 2007-03-09 20:16:40 $
    1414 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
    1515 */
     
    8989    const char *file,                   ///< File of caller
    9090    unsigned int lineno,                ///< Line number of caller
    91     const char *func,                   ///< Function name of caller 
     91    const char *func,                   ///< Function name of caller
    9292    long nalloc,                        ///< Total number of elements to make available.
    9393    psElemType type                    ///< Type of data to be held by vector.
     
    9898//#endif // ifdef __GNUC__
    9999#define psVectorAlloc(nalloc, type) \
    100       p_psVectorAlloc(__FILE__, __LINE__, __func__, nalloc, type) 
     100      p_psVectorAlloc(__FILE__, __LINE__, __func__, nalloc, type)
    101101#endif // ifdef DOXYGEN
    102102
     
    118118    const char *file,                   ///< File of caller
    119119    unsigned int lineno,                ///< Line number of caller
    120     const char *func,                   ///< Function name of caller 
     120    const char *func,                   ///< Function name of caller
    121121    long nalloc,                       ///< Total number of elements to make available.
    122122    psElemType type                    ///< Type of data to be held by vector.
     
    127127//#endif // ifdef __GNUC__
    128128#define psVectorAllocEmpty(nalloc, type) \
    129       p_psVectorAllocEmpty(__FILE__, __LINE__, __func__, nalloc, type) 
     129      p_psVectorAllocEmpty(__FILE__, __LINE__, __func__, nalloc, type)
    130130#endif // ifdef DOXYGEN
    131131
     
    184184    const char *file,                   ///< File of caller
    185185    unsigned int lineno,                ///< Line number of caller
    186     const char *func,                   ///< Function name of caller 
     186    const char *func,                   ///< Function name of caller
    187187    psVector* vector,
    188188    ///< Vector to recycle.  If NULL, a new vector is created.  No effort
     
    215215    const char *file,                   ///< File of caller
    216216    unsigned int lineno,                ///< Line number of caller
    217     const char *func,                   ///< Function name of caller 
     217    const char *func,                   ///< Function name of caller
    218218    psVector* output,                  ///< if non-NULL, a psVector to recycle
    219219    const psVector* input,             ///< the vector to copy.
     
    221221);
    222222#define psVectorCopy(output, input, type) \
    223       p_psVectorCopy(__FILE__, __LINE__, __func__, output, input, type) 
     223      p_psVectorCopy(__FILE__, __LINE__, __func__, output, input, type)
    224224#endif // ifdef DOXYGEN
    225225
     
    291291bool psVectorInit(
    292292    psVector *vector,                  ///< the vector to be initialized
    293     ...                                ///< Variable argument list for initialization
     293    double value                        ///< Value to which to initialise
    294294);
    295295
     
    315315    const char *file,                   ///< File of caller
    316316    unsigned int lineno,                ///< Line number of caller
    317     const char *func,                   ///< Function name of caller 
     317    const char *func,                   ///< Function name of caller
    318318    psVector *input,                   ///< Input vector
    319319    double lower,                      ///< lower bound
Note: See TracChangeset for help on using the changeset viewer.