/ocr/ocrservice/jni/hydrogen/include/leptonica/arrayaccess.h
C++ Header | 190 lines | 89 code | 37 blank | 64 comment | 1 complexity | 94f22a88fd23f9f0761961d006b56f57 MD5 | raw file
1/*====================================================================* 2 - Copyright (C) 2001 Leptonica. All rights reserved. 3 - This software is distributed in the hope that it will be 4 - useful, but with NO WARRANTY OF ANY KIND. 5 - No author or distributor accepts responsibility to anyone for the 6 - consequences of using this software, or for whether it serves any 7 - particular purpose or works at all, unless he or she says so in 8 - writing. Everyone is granted permission to copy, modify and 9 - redistribute this source code, for commercial or non-commercial 10 - purposes, with the following restrictions: (1) the origin of this 11 - source code must not be misrepresented; (2) modified versions must 12 - be plainly marked as such; and (3) this notice may not be removed 13 - or altered from any source or modified source distribution. 14 *====================================================================*/ 15 16#ifndef LEPTONICA_ARRAY_ACCESS_H 17#define LEPTONICA_ARRAY_ACCESS_H 18 19/* 20 * arrayaccess.h 21 * 22 * 1, 2, 4, 8, 16 and 32 bit data access within an array of 32-bit words 23 * 24 * This is used primarily to access 1, 2, 4, 8, 16 and 32 bit pixels 25 * in a line of image data, represented as an array of 32-bit words. 26 * 27 * pdata: pointer to first 32-bit word in the array 28 * n: index of the pixel in the array 29 * 30 * Function calls for these accessors are defined in arrayaccess.c. 31 * 32 * However, for efficiency we use the inline macros for all accesses. 33 * Even though the 2 and 4 bit set* accessors are more complicated, 34 * they are about 10% faster than the function calls. 35 * 36 * The 32 bit access is just a cast and ptr arithmetic. We include 37 * it so that the input ptr can be void*. 38 * 39 * At the end of this file is code for invoking the function calls 40 * instead of inlining. 41 * 42 * The macro SET_DATA_BIT_VAL(pdata, n, val) is a bit slower than 43 * if (val == 0) 44 * CLEAR_DATA_BIT(pdata, n); 45 * else 46 * SET_DATA_BIT(pdata, n); 47 */ 48 49 50#if 1 /* Inline Accessors */ 51#ifndef _MSC_VER 52 53 /*--------------------------------------------------* 54 * 1 bit access * 55 *--------------------------------------------------*/ 56#define GET_DATA_BIT(pdata, n) \ 57 ((*((l_uint32 *)(pdata) + ((n) >> 5)) >> (31 - ((n) & 31))) & 1) 58 59#define SET_DATA_BIT(pdata, n) \ 60 (*((l_uint32 *)(pdata) + ((n) >> 5)) |= (0x80000000 >> ((n) & 31))) 61 62#define CLEAR_DATA_BIT(pdata, n) \ 63 (*((l_uint32 *)(pdata) + ((n) >> 5)) &= ~(0x80000000 >> ((n) & 31))) 64 65#define SET_DATA_BIT_VAL(pdata, n, val) \ 66 ({l_uint32 *_TEMP_WORD_PTR_; \ 67 _TEMP_WORD_PTR_ = (l_uint32 *)(pdata) + ((n) >> 5); \ 68 *_TEMP_WORD_PTR_ &= ~(0x80000000 >> ((n) & 31)); \ 69 *_TEMP_WORD_PTR_ |= ((val) << (31 - ((n) & 31))); \ 70 }) 71 72 73 /*--------------------------------------------------* 74 * 2 bit access * 75 *--------------------------------------------------*/ 76#define GET_DATA_DIBIT(pdata, n) \ 77 ((*((l_uint32 *)(pdata) + ((n) >> 4)) >> (2 * (15 - ((n) & 15)))) & 3) 78 79#define SET_DATA_DIBIT(pdata, n, val) \ 80 ({l_uint32 *_TEMP_WORD_PTR_; \ 81 _TEMP_WORD_PTR_ = (l_uint32 *)(pdata) + ((n) >> 4); \ 82 *_TEMP_WORD_PTR_ &= ~(0xc0000000 >> (2 * ((n) & 15))); \ 83 *_TEMP_WORD_PTR_ |= (((val) & 3) << (30 - 2 * ((n) & 15))); \ 84 }) 85 86#define CLEAR_DATA_DIBIT(pdata, n) \ 87 (*((l_uint32 *)(pdata) + ((n) >> 4)) &= ~(0xc0000000 >> (2 * ((n) & 15)))) 88 89 90 /*--------------------------------------------------* 91 * 4 bit access * 92 *--------------------------------------------------*/ 93#define GET_DATA_QBIT(pdata, n) \ 94 ((*((l_uint32 *)(pdata) + ((n) >> 3)) >> (4 * (7 - ((n) & 7)))) & 0xf) 95 96#define SET_DATA_QBIT(pdata, n, val) \ 97 ({l_uint32 *_TEMP_WORD_PTR_; \ 98 _TEMP_WORD_PTR_ = (l_uint32 *)(pdata) + ((n) >> 3); \ 99 *_TEMP_WORD_PTR_ &= ~(0xf0000000 >> (4 * ((n) & 7))); \ 100 *_TEMP_WORD_PTR_ |= (((val) & 15) << (28 - 4 * ((n) & 7))); \ 101 }) 102 103#define CLEAR_DATA_QBIT(pdata, n) \ 104 (*((l_uint32 *)(pdata) + ((n) >> 3)) &= ~(0xf0000000 >> (4 * ((n) & 7)))) 105 106 107 /*--------------------------------------------------* 108 * 8 bit access * 109 *--------------------------------------------------*/ 110#ifdef L_BIG_ENDIAN 111#define GET_DATA_BYTE(pdata, n) \ 112 (*((l_uint8 *)(pdata) + (n))) 113#else /* L_LITTLE_ENDIAN */ 114#define GET_DATA_BYTE(pdata, n) \ 115 (*(l_uint8 *)((l_uintptr_t)((l_uint8 *)(pdata) + (n)) ^ 3)) 116#endif /* L_BIG_ENDIAN */ 117 118#ifdef L_BIG_ENDIAN 119#define SET_DATA_BYTE(pdata, n, val) \ 120 (*((l_uint8 *)(pdata) + (n)) = (val)) 121#else /* L_LITTLE_ENDIAN */ 122#define SET_DATA_BYTE(pdata, n, val) \ 123 (*(l_uint8 *)((l_uintptr_t)((l_uint8 *)(pdata) + (n)) ^ 3) = (val)) 124#endif /* L_BIG_ENDIAN */ 125 126 127 /*--------------------------------------------------* 128 * 16 bit access * 129 *--------------------------------------------------*/ 130#ifdef L_BIG_ENDIAN 131#define GET_DATA_TWO_BYTES(pdata, n) \ 132 (*((l_uint16 *)(pdata) + (n))) 133#else /* L_LITTLE_ENDIAN */ 134#define GET_DATA_TWO_BYTES(pdata, n) \ 135 (*(l_uint16 *)((l_uintptr_t)((l_uint16 *)(pdata) + (n)) ^ 2)) 136#endif /* L_BIG_ENDIAN */ 137 138#ifdef L_BIG_ENDIAN 139#define SET_DATA_TWO_BYTES(pdata, n, val) \ 140 (*((l_uint16 *)(pdata) + (n)) = (val)) 141#else /* L_LITTLE_ENDIAN */ 142#define SET_DATA_TWO_BYTES(pdata, n, val) \ 143 (*(l_uint16 *)((l_uintptr_t)((l_uint16 *)(pdata) + (n)) ^ 2) = (val)) 144#endif /* L_BIG_ENDIAN */ 145 146 147 /*--------------------------------------------------* 148 * 32 bit access * 149 *--------------------------------------------------*/ 150#define GET_DATA_FOUR_BYTES(pdata, n) \ 151 (*((l_uint32 *)(pdata) + (n))) 152 153#define SET_DATA_FOUR_BYTES(pdata, n, val) \ 154 (*((l_uint32 *)(pdata) + (n)) = (val)) 155 156 157#endif /* ! _MSC_VER */ 158#endif /* Inline Accessors */ 159 160 161 162 /*--------------------------------------------------* 163 * Slower, using function calls for all accessors * 164 *--------------------------------------------------*/ 165#if 0 || defined(_MSC_VER) 166#define GET_DATA_BIT(pdata, n) l_getDataBit(pdata, n) 167#define SET_DATA_BIT(pdata, n) l_setDataBit(pdata, n) 168#define CLEAR_DATA_BIT(pdata, n) l_clearDataBit(pdata, n) 169#define SET_DATA_BIT_VAL(pdata, n, val) l_setDataBitVal(pdata, n, val) 170 171#define GET_DATA_DIBIT(pdata, n) l_getDataDibit(pdata, n) 172#define SET_DATA_DIBIT(pdata, n, val) l_setDataDibit(pdata, n, val) 173#define CLEAR_DATA_DIBIT(pdata, n) l_clearDataDibit(pdata, n) 174 175#define GET_DATA_QBIT(pdata, n) l_getDataQbit(pdata, n) 176#define SET_DATA_QBIT(pdata, n, val) l_setDataQbit(pdata, n, val) 177#define CLEAR_DATA_QBIT(pdata, n) l_clearDataQbit(pdata, n) 178 179#define GET_DATA_BYTE(pdata, n) l_getDataByte(pdata, n) 180#define SET_DATA_BYTE(pdata, n, val) l_setDataByte(pdata, n, val) 181 182#define GET_DATA_TWO_BYTES(pdata, n) l_getDataTwoBytes(pdata, n) 183#define SET_DATA_TWO_BYTES(pdata, n, val) l_setDataTwoBytes(pdata, n, val) 184 185#define GET_DATA_FOUR_BYTES(pdata, n) l_getDataFourBytes(pdata, n) 186#define SET_DATA_FOUR_BYTES(pdata, n, val) l_setDataFourBytes(pdata, n, val) 187#endif 188 189 190#endif /* LEPTONICA_ARRAY_ACCESS_H */