Leptonica 1.68
C Image Processing Library

arrayaccess.h

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 #ifndef  LEPTONICA_ARRAY_ACCESS_H
00017 #define  LEPTONICA_ARRAY_ACCESS_H
00018 
00019 /*
00020  *  arrayaccess.h
00021  *
00022  *  1, 2, 4, 8, 16 and 32 bit data access within an array of 32-bit words
00023  *
00024  *  This is used primarily to access 1, 2, 4, 8, 16 and 32 bit pixels
00025  *  in a line of image data, represented as an array of 32-bit words.
00026  *
00027  *     pdata:  pointer to first 32-bit word in the array
00028  *     n:      index of the pixel in the array
00029  *
00030  *  Function calls for these accessors are defined in arrayaccess.c.
00031  *
00032  *  However, for efficiency we use the inline macros for all accesses.
00033  *  Even though the 2 and 4 bit set* accessors are more complicated,
00034  *  they are about 10% faster than the function calls.
00035  *
00036  *  The 32 bit access is just a cast and ptr arithmetic.  We include
00037  *  it so that the input ptr can be void*.
00038  *
00039  *  At the end of this file is code for invoking the function calls
00040  *  instead of inlining.
00041  *
00042  *  The macro SET_DATA_BIT_VAL(pdata, n, val) is a bit slower than
00043  *      if (val == 0)
00044  *          CLEAR_DATA_BIT(pdata, n);
00045  *      else
00046  *          SET_DATA_BIT(pdata, n);
00047  */
00048 
00049 
00050     /* Use the inline accessors (except with _MSC_VER), because they
00051      * are faster.  */
00052 #define  USE_INLINE_ACCESSORS    1
00053 
00054 #if USE_INLINE_ACCESSORS
00055 #ifndef _MSC_VER
00056 
00057     /*--------------------------------------------------*
00058      *                     1 bit access                 *
00059      *--------------------------------------------------*/
00060 #define  GET_DATA_BIT(pdata, n) \
00061     ((*((l_uint32 *)(pdata) + ((n) >> 5)) >> (31 - ((n) & 31))) & 1)
00062 
00063 #define  SET_DATA_BIT(pdata, n) \
00064     (*((l_uint32 *)(pdata) + ((n) >> 5)) |= (0x80000000 >> ((n) & 31)))
00065 
00066 #define  CLEAR_DATA_BIT(pdata, n) \
00067     (*((l_uint32 *)(pdata) + ((n) >> 5)) &= ~(0x80000000 >> ((n) & 31)))
00068 
00069 #define  SET_DATA_BIT_VAL(pdata, n, val) \
00070     ({l_uint32 *_TEMP_WORD_PTR_; \
00071      _TEMP_WORD_PTR_ = (l_uint32 *)(pdata) + ((n) >> 5); \
00072      *_TEMP_WORD_PTR_ &= ~(0x80000000 >> ((n) & 31)); \
00073      *_TEMP_WORD_PTR_ |= ((val) << (31 - ((n) & 31))); \
00074     })
00075 
00076 
00077     /*--------------------------------------------------*
00078      *                     2 bit access                 *
00079      *--------------------------------------------------*/
00080 #define  GET_DATA_DIBIT(pdata, n) \
00081     ((*((l_uint32 *)(pdata) + ((n) >> 4)) >> (2 * (15 - ((n) & 15)))) & 3)
00082 
00083 #define  SET_DATA_DIBIT(pdata, n, val) \
00084     ({l_uint32 *_TEMP_WORD_PTR_; \
00085      _TEMP_WORD_PTR_ = (l_uint32 *)(pdata) + ((n) >> 4); \
00086      *_TEMP_WORD_PTR_ &= ~(0xc0000000 >> (2 * ((n) & 15))); \
00087      *_TEMP_WORD_PTR_ |= (((val) & 3) << (30 - 2 * ((n) & 15))); \
00088     })
00089 
00090 #define  CLEAR_DATA_DIBIT(pdata, n) \
00091     (*((l_uint32 *)(pdata) + ((n) >> 4)) &= ~(0xc0000000 >> (2 * ((n) & 15))))
00092 
00093 
00094     /*--------------------------------------------------*
00095      *                     4 bit access                 *
00096      *--------------------------------------------------*/
00097 #define  GET_DATA_QBIT(pdata, n) \
00098      ((*((l_uint32 *)(pdata) + ((n) >> 3)) >> (4 * (7 - ((n) & 7)))) & 0xf)
00099 
00100 #define  SET_DATA_QBIT(pdata, n, val) \
00101     ({l_uint32 *_TEMP_WORD_PTR_; \
00102      _TEMP_WORD_PTR_ = (l_uint32 *)(pdata) + ((n) >> 3); \
00103      *_TEMP_WORD_PTR_ &= ~(0xf0000000 >> (4 * ((n) & 7))); \
00104      *_TEMP_WORD_PTR_ |= (((val) & 15) << (28 - 4 * ((n) & 7))); \
00105     })
00106 
00107 #define  CLEAR_DATA_QBIT(pdata, n) \
00108     (*((l_uint32 *)(pdata) + ((n) >> 3)) &= ~(0xf0000000 >> (4 * ((n) & 7))))
00109 
00110 
00111     /*--------------------------------------------------*
00112      *                     8 bit access                 *
00113      *--------------------------------------------------*/
00114 #ifdef  L_BIG_ENDIAN
00115 #define  GET_DATA_BYTE(pdata, n) \
00116              (*((l_uint8 *)(pdata) + (n)))
00117 #else  /* L_LITTLE_ENDIAN */
00118 #define  GET_DATA_BYTE(pdata, n) \
00119              (*(l_uint8 *)((l_uintptr_t)((l_uint8 *)(pdata) + (n)) ^ 3))
00120 #endif  /* L_BIG_ENDIAN */
00121 
00122 #ifdef  L_BIG_ENDIAN
00123 #define  SET_DATA_BYTE(pdata, n, val) \
00124              (*((l_uint8 *)(pdata) + (n)) = (val))
00125 #else  /* L_LITTLE_ENDIAN */
00126 #define  SET_DATA_BYTE(pdata, n, val) \
00127              (*(l_uint8 *)((l_uintptr_t)((l_uint8 *)(pdata) + (n)) ^ 3) = (val))
00128 #endif  /* L_BIG_ENDIAN */
00129 
00130 
00131     /*--------------------------------------------------*
00132      *                    16 bit access                 *
00133      *--------------------------------------------------*/
00134 #ifdef  L_BIG_ENDIAN
00135 #define  GET_DATA_TWO_BYTES(pdata, n) \
00136              (*((l_uint16 *)(pdata) + (n)))
00137 #else  /* L_LITTLE_ENDIAN */
00138 #define  GET_DATA_TWO_BYTES(pdata, n) \
00139              (*(l_uint16 *)((l_uintptr_t)((l_uint16 *)(pdata) + (n)) ^ 2))
00140 #endif  /* L_BIG_ENDIAN */
00141 
00142 #ifdef  L_BIG_ENDIAN
00143 #define  SET_DATA_TWO_BYTES(pdata, n, val) \
00144              (*((l_uint16 *)(pdata) + (n)) = (val))
00145 #else  /* L_LITTLE_ENDIAN */
00146 #define  SET_DATA_TWO_BYTES(pdata, n, val) \
00147              (*(l_uint16 *)((l_uintptr_t)((l_uint16 *)(pdata) + (n)) ^ 2) = (val))
00148 #endif  /* L_BIG_ENDIAN */
00149 
00150 
00151     /*--------------------------------------------------*
00152      *                    32 bit access                 *
00153      *--------------------------------------------------*/
00154 #define  GET_DATA_FOUR_BYTES(pdata, n) \
00155              (*((l_uint32 *)(pdata) + (n)))
00156 
00157 #define  SET_DATA_FOUR_BYTES(pdata, n, val) \
00158              (*((l_uint32 *)(pdata) + (n)) = (val))
00159 
00160 
00161 #endif  /* ! _MSC_VER */
00162 #endif  /* USE_INLINE_ACCESSORS */
00163 
00164 
00165 
00166     /*--------------------------------------------------*
00167      *  Slower, using function calls for all accessors  *
00168      *--------------------------------------------------*/
00169 #if !USE_INLINE_ACCESSORS || defined(_MSC_VER)
00170 #define  GET_DATA_BIT(pdata, n)               l_getDataBit(pdata, n)
00171 #define  SET_DATA_BIT(pdata, n)               l_setDataBit(pdata, n)
00172 #define  CLEAR_DATA_BIT(pdata, n)             l_clearDataBit(pdata, n)
00173 #define  SET_DATA_BIT_VAL(pdata, n, val)      l_setDataBitVal(pdata, n, val)
00174 
00175 #define  GET_DATA_DIBIT(pdata, n)             l_getDataDibit(pdata, n)
00176 #define  SET_DATA_DIBIT(pdata, n, val)        l_setDataDibit(pdata, n, val)
00177 #define  CLEAR_DATA_DIBIT(pdata, n)           l_clearDataDibit(pdata, n)
00178 
00179 #define  GET_DATA_QBIT(pdata, n)              l_getDataQbit(pdata, n)
00180 #define  SET_DATA_QBIT(pdata, n, val)         l_setDataQbit(pdata, n, val)
00181 #define  CLEAR_DATA_QBIT(pdata, n)            l_clearDataQbit(pdata, n)
00182 
00183 #define  GET_DATA_BYTE(pdata, n)              l_getDataByte(pdata, n)
00184 #define  SET_DATA_BYTE(pdata, n, val)         l_setDataByte(pdata, n, val)
00185 
00186 #define  GET_DATA_TWO_BYTES(pdata, n)         l_getDataTwoBytes(pdata, n)
00187 #define  SET_DATA_TWO_BYTES(pdata, n, val)    l_setDataTwoBytes(pdata, n, val)
00188 
00189 #define  GET_DATA_FOUR_BYTES(pdata, n)         l_getDataFourBytes(pdata, n)
00190 #define  SET_DATA_FOUR_BYTES(pdata, n, val)    l_setDataFourBytes(pdata, n, val)
00191 #endif  /* !USE_INLINE_ACCESSORS || _MSC_VER */
00192 
00193 
00194 #endif /* LEPTONICA_ARRAY_ACCESS_H */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines