membuf.h

Go to the documentation of this file.
00001 /**
00002  * \file membuf.h <mi32/membuf.h>
00003  * \brief Functions to handle memory buffers
00004  *
00005  * \if NODOC
00006  * $Id: membuf.h_v 1.15 2003/09/15 13:49:56 fileserver!dwilliss Exp $
00007  *
00008  * $Log: membuf.h_v $
00009  * Revision 1.15  2003/09/15 13:49:56  fileserver!dwilliss
00010  * Doxygen
00011  *
00012  * Revision 1.14  2003/09/08 21:05:08  dwilliss
00013  * Added doxygen start/end tags for functions groups
00014  *
00015  * Revision 1.13  2001/11/07 16:44:22  scowan
00016  * Bad paran in putbit.
00017  *
00018  * Revision 1.12  2001/11/06 18:39:01  dwilliss
00019  * Made inline versions of getbit/putbit and memswap
00020  *
00021  * Revision 1.11  2001/03/01 16:37:39  scowan
00022  * Change list name to alist for std namespace.
00023  *
00024  * Revision 1.10  2000/06/22 21:36:06  mju
00025  * Improve docs for SwapBytes2/4/8.
00026  *
00027  * Revision 1.8  2000/06/20 18:00:30  sparsons
00028  * Genitor documentation.
00029  *
00030  * Revision 1.7  2000/06/08 17:14:39  mju
00031  * Fix to compile for non-C++.
00032  *
00033  * Revision 1.6  2000/05/12 15:26:48  mju
00034  * Use BUF instead of _B so doesn't conflict with ctype.h
00035  *
00036  * Revision 1.5  2000/05/11 16:24:57  mju
00037  * Must capitalize 'I' in "End Ignore" comment.
00038  *
00039  * Revision 1.4  2000/05/10 13:35:25  mju
00040  * Apparently sometimes Unix compiler doesn't like having ';' after ending brace in inline fn defn.
00041  * Make bitrev() an inline.
00042  *
00043  * Revision 1.3  2000/05/09 15:42:03  scowan
00044  * Added inline and template spawbytes routines.
00045  *
00046  * Revision 1.2  1999/05/12 16:55:54  mju
00047  * Use const for getbit/n.
00048  *
00049  * Revision 1.1  1999/04/19  17:36:54  mju
00050  * Initial revision
00051  *
00052  * \endif
00053 **/
00054 
00055 #ifndef  INC_MI32_MEMBUF_H
00056 #define  INC_MI32_MEMBUF_H
00057 
00058 #ifndef  INC_MI32_STDDEFNS_H
00059 #include <mi32/stddefns.h>
00060 #endif
00061 
00062 #if defined(__cplusplus)
00063 
00064 //!:Associate with "Memory buffer functions"
00065 //!\addtogroup membuf Memory buffer functions
00066 //!@{
00067 
00068 //! Swap bytes - character.
00069 inline char SwapBytes (
00070    char& value
00071    ) {
00072    return (value);
00073    }
00074 
00075 //! Swap bytes - 8 bit signed.
00076 inline INT8 SwapBytes (
00077    INT8& value
00078    ) {
00079    return (value);
00080    }
00081 
00082 //! Swap bytes - 8 bit unsigned.
00083 inline UINT8 SwapBytes (
00084    UINT8& value
00085    ) {
00086    return (value);
00087    }
00088 
00089 //! Swap bytes - 16 bit signed.
00090 inline INT16 SwapBytes (
00091    INT16& value
00092    ) {
00093    UINT8 *p = reinterpret_cast<UINT8*>(&value);
00094    UINT8 tmp = p[0]; p[0] = p[1]; p[1] = tmp;
00095    return (value);
00096    }
00097 
00098 //! Swap bytes - 16 bit unsigned.
00099 inline UINT16 SwapBytes (
00100    UINT16& value
00101    ) {
00102    UINT8 *p = reinterpret_cast<UINT8*>(&value);
00103    UINT8 tmp = p[0]; p[0] = p[1]; p[1] = tmp;
00104    return (value);
00105    }
00106 
00107 //! Swap bytes - 32 bit signed.
00108 inline INT32 SwapBytes (
00109    INT32& value
00110    ) {
00111    UINT8 *p = reinterpret_cast<UINT8*>(&value);
00112    UINT8 tmp = p[0]; p[0] = p[3]; p[3] = tmp;
00113    tmp = p[1]; p[1] = p[2]; p[2] = tmp;
00114    return (value);
00115    }
00116 
00117 //! Swap bytes - 32 bit unsigned.
00118 inline UINT32 SwapBytes (
00119    UINT32& value
00120    ) {
00121    UINT8 *p = reinterpret_cast<UINT8*>(&value);
00122    UINT8 tmp = p[0]; p[0] = p[3]; p[3] = tmp;
00123    tmp = p[1]; p[1] = p[2]; p[2] = tmp;
00124    return (value);
00125    }
00126 
00127 //! Swap bytes - float.
00128 inline float SwapBytes (
00129    float& value
00130    ) {
00131    UINT8 *p = reinterpret_cast<UINT8*>(&value);
00132    UINT8 tmp = p[0]; p[0] = p[3]; p[3] = tmp;
00133    tmp = p[1]; p[1] = p[2]; p[2] = tmp;
00134    return (value);
00135    }
00136 
00137 //! Swap bytes - 64 bit signed.
00138 inline INT64 SwapBytes (
00139    INT64& value
00140    ) {
00141    UINT8 *p = reinterpret_cast<UINT8*>(&value);
00142    UINT8 tmp = p[0]; p[0] = p[7]; p[7] = tmp;
00143    tmp = p[1]; p[1] = p[6]; p[6] = tmp;
00144    tmp = p[2]; p[2] = p[5]; p[5] = tmp;
00145    tmp = p[3]; p[3] = p[4]; p[4] = tmp;
00146    return (value);
00147    }
00148 
00149 //! Swap bytes - 64 bit unsigned.
00150 inline UINT64 SwapBytes (
00151    UINT64& value
00152    ) {
00153    UINT8 *p = reinterpret_cast<UINT8*>(&value);
00154    UINT8 tmp = p[0]; p[0] = p[7]; p[7] = tmp;
00155    tmp = p[1]; p[1] = p[6]; p[6] = tmp;
00156    tmp = p[2]; p[2] = p[5]; p[5] = tmp;
00157    tmp = p[3]; p[3] = p[4]; p[4] = tmp;
00158    return (value);
00159    }
00160 
00161 //! Swap bytes - double.
00162 inline double SwapBytes (
00163    double& value
00164    ) {
00165    UINT8 *p = reinterpret_cast<UINT8*>(&value);
00166    UINT8 tmp = p[0]; p[0] = p[7]; p[7] = tmp;
00167    tmp = p[1]; p[1] = p[6]; p[6] = tmp;
00168    tmp = p[2]; p[2] = p[5]; p[5] = tmp;
00169    tmp = p[3]; p[3] = p[4]; p[4] = tmp;
00170    return (value);
00171    }
00172 
00173 //! Swap bytes.
00174 template <class _BUF>
00175 inline void SwapBytes (
00176    _BUF *alist,                        //!< Array of items to swap
00177    INT32 num                           //!< Number of items in array
00178    ) {
00179    for (INT32 _xy_ = 0; (_xy_ < num); ++_xy_) SwapBytes(alist[_xy_]);
00180    return;
00181    }
00182 
00183 //! Swap bytes in array of 2-byte values (INT16, UINT16).
00184 inline void SwapBytes2 (
00185    void *data,                         //!< Array of items to swap
00186    int num                             //!< Number of items
00187    ) {
00188    SwapBytes(static_cast<UINT16*>(data), num);
00189    return;
00190    }
00191 
00192 //! Swap bytes in array of 4-byte values (float, INT32, UINT32).
00193 inline void SwapBytes4 (
00194    void *data,                         //!< Array of items to swap
00195    int num                             //!< Number of items
00196    ) {
00197    SwapBytes(static_cast<UINT32*>(data), num);
00198    return;
00199    }
00200 
00201 //! Swap bytes in array of 8-byte values (double, INT64, UINT64).
00202 inline void SwapBytes8 (
00203    void *data,                         //!< Array of items to swap
00204    int num                             //!< Number of items
00205    ) {
00206    SwapBytes(static_cast<double*>(data), num);
00207    return;
00208    }
00209 
00210 #endif   //!< C++
00211 
00212 #if defined(__cplusplus)
00213 extern "C" {
00214 #endif
00215 
00216 #ifndef GENERATING_DOXYGEN_OUTPUT
00217 extern UINT8 _bitrevtable[];
00218 #endif //!< GENERATING_DOXYGEN_OUTPUT
00219 
00220 //! Reverse order of bits in a single byte.
00221 #if defined(__cplusplus)
00222 inline UINT8 bitrev (
00223    UINT8 b
00224    ) {
00225    return (_bitrevtable[b]);
00226    }
00227 #else
00228 #define  bitrev(b)   _bitrevtable[b]
00229 #endif
00230 
00231 //! Reverse bits for array.
00232 void bitrevlin (
00233    UINT8 *buf, 
00234    int numbytes
00235    );
00236 
00237 #if defined(__cplusplus) && !defined(GPBIT_C)
00238 //!
00239 //! Get a value from a bit array.
00240 //! 
00241 //! @return 0 or 1.
00242 inline int getbit (
00243    const UINT8 *array,                 //!< Bit array 
00244    INT32 index                         //!< Index into array
00245    ) {
00246    return ((array[index>>3] >> (index & 7)) & 1);
00247    }
00248 
00249 //!   Get a value from a bit array, returns 0 if array is NULL.
00250 //!
00251 //! @return 0 or 1 (0 if array is NULL).
00252 inline int getbitn (
00253    const UINT8 *array,                 //!< Bit array, can be NULL
00254    INT32 index                         //!< Index into array
00255    ) {
00256    if (array == 0) return (0);
00257    return ((array[index>>3] >> (index & 7)) & 1);
00258    }  
00259 
00260 #else
00261 #ifndef GENERATING_DOXYGEN_OUTPUT
00262 //! Prototypes for if we don't have C++.  Also force the prototypes
00263 //! for gpbit.c, which defines the things
00264 
00265 //!
00266 //! Get a value from a bit array.
00267 //! 
00268 //! @return 0 or 1.
00269 int getbit (
00270    const UINT8 *array,                 //!< Bit array 
00271    INT32 index                         //!< Index into array
00272    );
00273 
00274 //!   Get a value from a bit array, returns 0 if array is NULL.
00275 //!
00276 //! @return 0 or 1 (0 if array is NULL).
00277 int getbitn (
00278    const UINT8 *array,                 //!< Bit array, can be NULL
00279    INT32 index                         //!< Index into array
00280    );
00281 
00282 #endif //!< GENERATING_DOXYGEN_OUTPUT
00283 #endif
00284 
00285 #if defined(__cplusplus) && !defined(MEMSWAP_C)
00286 //! Swap data in two buffers.
00287 inline void memswap (
00288    void *vb1, 
00289    void *vb2, 
00290    int count
00291    ) {
00292    UINT8* b1 = static_cast<UINT8*>(vb1);
00293    UINT8* b2 = static_cast<UINT8*>(vb2);
00294 
00295    for (int i = 0 ; i < count ; ++i, ++b1, ++b2) {
00296       UINT8 t = *b1; *b1 = *b2 ; *b2 = t;
00297       }
00298    }
00299 
00300 #else
00301 #ifndef GENERATING_DOXYGEN_OUTPUT
00302 //!  Force prototype to exist for non-C++ and for the module that implements
00303 //!  the non-inline version
00304 //! Swap data in two buffers.
00305 void memswap (
00306    void *vb1, 
00307    void *vb2, 
00308    int count
00309    );
00310 #endif //!< GENERATING_DOXYGEN_OUTPUT
00311 #endif
00312 
00313 //! Pack TRUE/FALSE values from 8-bit array into bit array with least-significant bit as first bit.
00314 //!
00315 //! Non-zero values in the 8-bit array will be written as '1', zero values will
00316 //! be written as '0'.
00317 void packbits (
00318    UINT8 *dest, 
00319    UINT8 *source, 
00320    int count
00321    );
00322 
00323 //! Pack TRUE / FALSE values from 8-bit array into bit array with MOST-significannt bit as first bit.
00324 //!
00325 //! Non-zero values in the 8-bit array will be written as '1', zero values will
00326 //! be written as '0'.
00327 void packbitsr (
00328    UINT8 *dest, 
00329    UINT8 *source, 
00330    int count
00331    );
00332 
00333 #if defined(__cplusplus) && !defined(GPBIT_C)
00334 
00335 //! Set entry in a bit array to a specified value.
00336 inline void putbit (
00337    UINT8 *array,                       //!< Bit array
00338    INT32 index,                        //!< Index into array
00339    int value                           //!< Value to set to (any non-zero value assumed to be 1)
00340    ) {
00341    if (value)
00342       array[index >> 3] |= 1 << (index & 7);
00343    else
00344       array[index >> 3] &= ~(1 << (index & 7));
00345    }
00346 
00347 #else
00348 #ifndef GENERATING_DOXYGEN_OUTPUT
00349 //! Prototypes for if we don't have C++.  Also force the prototypes
00350 //! for gpbit.c, which defines the things
00351 
00352 //! Set entry in a bit array to a specified value.
00353 void putbit (
00354    UINT8 *array,                       //!< Bit array
00355    INT32 index,                        //!< Index into array
00356    int value                           //!< Value to set to (any non-zero value assumed to be 1)
00357    );
00358 
00359 #endif //!< GENERATING_DOXYGEN_OUTPUT
00360 #endif
00361 
00362 //! Reverse the order of values in UINT8 array.
00363 void revbuffer (
00364    void *vbuf, 
00365    int cols
00366    );
00367 
00368 //! Swap data in a buffer given number of items and size.
00369 void SwapDataInBuffer (
00370    void *data,                         //!< Array of items to swap
00371    INT32 NumItems,                     //!< Number of items in the array
00372    int ItemSize                        //!< Size of item to swap
00373    );
00374 
00375 #if defined(__cplusplus)
00376 }
00377 #endif
00378 
00379 //!@}
00380 
00381 #endif   //!<  INC_MI32_MEMBUF_H 

Generated on Tue Dec 14 13:18:26 2004 for TNTsdk by  doxygen 1.3.8-20040913