PageRenderTime 33ms CodeModel.GetById 24ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/ocr/ocrservice/jni/hydrogen/include/leptonica/arrayaccess.h.vc

http://eyes-free.googlecode.com/
Unknown | 166 lines | 134 code | 32 blank | 0 comment | 0 complexity | 331b797f33af20f0f5d146f666f1be9d 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 ARRAY_ACCESS_H
 17#define ARRAY_ACCESS_H
 18
 19/*
 20 *  arrayaccess.h.vc
 21 *
 22 *  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 23 *  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 24 *  This version is for MS VC++, which doesn't support expression
 25 *  statements.  It can give warnings with gcc-4.1.0, so it is not
 26 *  installed by default.
 27 *  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 28 *  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 29 *
 30 *
 31 *  1, 2, 4, 8 and 16 bit data access within an array of 32-bit words
 32 *
 33 *  This is used primarily to access 1, 2, 4, 8 and 16 bit pixels
 34 *  in a line of image data, represented as an array of 32-bit words.
 35 *
 36 *     pdata:  pointer to first 32-bit word in the array
 37 *     n:      index of the pixel in the array
 38 *
 39 *  Function calls for these accessors are defined in arrayaccess.c.
 40 *
 41 *  However, for efficiency we use macros for all accesses.
 42 *  Even though the 2 and 4 bit set* accessors are more complicated,
 43 *  they are about 10% faster than the function calls.
 44 *
 45 *  At the end we give code for using invoking the function calls
 46 *  with the macros.
 47 */
 48
 49
 50#include "environ.h"
 51
 52
 53    /*--------------------------------------------------*
 54     *                     1 bit access                 *
 55     *--------------------------------------------------*/
 56#define  GET_DATA_BIT(pdata, n) \
 57    ((*((pdata) + ((n) >> 5)) >> (31 - ((n) & 31))) & 1)
 58
 59#define  SET_DATA_BIT(pdata, n) \
 60    (*((pdata) + ((n) >> 5)) |= (0x80000000 >> ((n) & 31)))
 61
 62#define  CLEAR_DATA_BIT(pdata, n) \
 63    (*((pdata) + ((n) >> 5)) &= ~(0x80000000 >> ((n) & 31)))
 64
 65
 66    /*--------------------------------------------------*
 67     *                     2 bit access                 *
 68     *--------------------------------------------------*/
 69#define  GET_DATA_DIBIT(pdata, n) \
 70    ((*((pdata) + ((n) >> 4)) >> (2 * (15 - ((n) & 15)))) & 3)
 71
 72    /* ANSI C does not support statement expressions */
 73static l_uint32 *_TEMP_WORD_PTR_;
 74#define  SET_DATA_DIBIT(pdata, n, val) \
 75    ( _TEMP_WORD_PTR_ = (pdata) + ((n) >> 4), \
 76     *_TEMP_WORD_PTR_ &= ~(0xc0000000 >> (2 * ((n) & 15))), \
 77     *_TEMP_WORD_PTR_ |= ((val) << (30 - 2 * ((n) & 15))) \
 78    )
 79
 80#define  CLEAR_DATA_DIBIT(pdata, n) \
 81    (*((pdata) + ((n) >> 4)) &= ~(0xc0000000 >> (2 * ((n) & 15))))
 82
 83
 84    /*--------------------------------------------------*
 85     *                     4 bit access                 *
 86     *--------------------------------------------------*/
 87#define  GET_DATA_QBIT(pdata, n) \
 88     ((*((pdata) + ((n) >> 3)) >> (4 * (7 - ((n) & 7)))) & 0xf)
 89
 90#define  SET_DATA_QBIT(pdata, n, val) \
 91    ( _TEMP_WORD_PTR_ = (pdata) + ((n) >> 3), \
 92     *_TEMP_WORD_PTR_ &= ~(0xf0000000 >> (4 * ((n) & 7))), \
 93     *_TEMP_WORD_PTR_ |= ((val) << (28 - 4 * ((n) & 7))) \
 94    )
 95
 96#define  CLEAR_DATA_QBIT(pdata, n) \
 97    (*((pdata) + ((n) >> 3)) &= ~(0xf0000000 >> (4 * ((n) & 7))))
 98
 99
100    /*--------------------------------------------------*
101     *                     8 bit access                 *
102     *--------------------------------------------------*/
103#ifdef  L_BIG_ENDIAN
104#define  GET_DATA_BYTE(pdata, n) \
105             (*((l_uint8 *)(pdata) + (n)))
106#else  /* L_LITTLE_ENDIAN */
107#define  GET_DATA_BYTE(pdata, n) \
108             (*(l_uint8 *)((l_uintptr_t)((l_uint8 *)(pdata) + (n)) ^ 3))
109#endif  /* L_BIG_ENDIAN */
110
111#ifdef  L_BIG_ENDIAN
112#define  SET_DATA_BYTE(pdata, n, val) \
113             (*((l_uint8 *)(pdata) + (n)) = (val))
114#else  /* L_LITTLE_ENDIAN */
115#define  SET_DATA_BYTE(pdata, n, val) \
116             (*(l_uint8 *)((l_uintptr_t)((l_uint8 *)(pdata) + (n)) ^ 3) = (val))
117#endif  /* L_BIG_ENDIAN */
118
119
120    /*--------------------------------------------------*
121     *                    16 bit access                 *
122     *--------------------------------------------------*/
123#ifdef  L_BIG_ENDIAN
124#define  GET_DATA_TWO_BYTES(pdata, n) \
125             (*((l_uint16 *)(pdata) + (n)))
126#else  /* L_LITTLE_ENDIAN */
127#define  GET_DATA_TWO_BYTES(pdata, n) \
128             (*(l_uint16 *)((l_uintptr_t)((l_uint16 *)(pdata) + (n)) ^ 2))
129#endif  /* L_BIG_ENDIAN */
130
131#ifdef  L_BIG_ENDIAN
132#define  SET_DATA_TWO_BYTES(pdata, n, val) \
133             (*((l_uint16 *)(pdata) + (n)) = (val))
134#else  /* L_LITTLE_ENDIAN */
135#define  SET_DATA_TWO_BYTES(pdata, n, val) \
136             (*(l_uint16 *)((l_uintptr_t)((l_uint16 *)(pdata) + (n)) ^ 2) = (val))
137#endif  /* L_BIG_ENDIAN */
138
139
140
141    /*--------------------------------------------------*
142     *  Slower, using function calls for all accessors  *
143     *--------------------------------------------------*/
144#if 0
145#define  GET_DATA_BIT(pdata, n)               l_getDataBit(pdata, n)
146#define  SET_DATA_BIT(pdata, n)               l_setDataBit(pdata, n)
147#define  CLEAR_DATA_BIT(pdata, n)             l_clearDataBit(pdata, n)
148
149#define  GET_DATA_DIBIT(pdata, n)             l_getDataDibit(pdata, n)
150#define  SET_DATA_DIBIT(pdata, n, val)        l_setDataDibit(pdata, n, val)
151#define  CLEAR_DATA_DIBIT(pdata, n)           l_clearDataDibit(pdata, n)
152
153#define  GET_DATA_QBIT(pdata, n)              l_getDataQbit(pdata, n)
154#define  SET_DATA_QBIT(pdata, n, val)         l_setDataQbit(pdata, n, val)
155#define  CLEAR_DATA_QBIT(pdata, n)            l_clearDataQbit(pdata, n)
156
157#define  GET_DATA_BYTE(pdata, n)              l_getDataByte(pdata, n)
158#define  SET_DATA_BYTE(pdata, n, val)         l_setDataByte(pdata, n, val)
159
160#define  GET_DATA_TWO_BYTES(pdata, n)         l_getDataTwoBytes(pdata, n)
161#define  SET_DATA_TWO_BYTES(pdata, n, val)    l_setDataTwoBytes(pdata, n, val)
162#endif
163
164
165#endif /* ARRAY_ACCESS_H */
166