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

Generated on Thu Apr 26 04:04:16 2007 for TNTsdk by  doxygen 1.5.2