Leptonica  1.82.0
Image processing and image analysis suite
arrayaccess.c
Go to the documentation of this file.
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. All rights reserved.
3  -
4  - Redistribution and use in source and binary forms, with or without
5  - modification, are permitted provided that the following conditions
6  - are met:
7  - 1. Redistributions of source code must retain the above copyright
8  - notice, this list of conditions and the following disclaimer.
9  - 2. Redistributions in binary form must reproduce the above
10  - copyright notice, this list of conditions and the following
11  - disclaimer in the documentation and/or other materials
12  - provided with the distribution.
13  -
14  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18  - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
60 #ifdef HAVE_CONFIG_H
61 #include <config_auto.h>
62 #endif /* HAVE_CONFIG_H */
63 
64 #include "allheaders.h"
65 
66 /*----------------------------------------------------------------------*
67  * Access within an array of 32-bit words *
68  *----------------------------------------------------------------------*/
76 l_int32
77 l_getDataBit(const void *line,
78  l_int32 n)
79 {
80  return (*((const l_uint32 *)line + (n >> 5)) >> (31 - (n & 31))) & 1;
81 }
82 
83 
93 void
94 l_setDataBit(void *line,
95  l_int32 n)
96 {
97  *((l_uint32 *)line + (n >> 5)) |= (0x80000000 >> (n & 31));
98 }
99 
100 
110 void
111 l_clearDataBit(void *line,
112  l_int32 n)
113 {
114  *((l_uint32 *)line + (n >> 5)) &= ~(0x80000000 >> (n & 31));
115 }
116 
117 
136 void
137 l_setDataBitVal(void *line,
138  l_int32 n,
139  l_int32 val)
140 {
141 l_uint32 *pword;
142 
143  pword = (l_uint32 *)line + (n >> 5);
144  *pword &= ~(0x80000000 >> (n & 31)); /* clear */
145  *pword |= (l_uint32)val << (31 - (n & 31)); /* set */
146 }
147 
148 
156 l_int32
157 l_getDataDibit(const void *line,
158  l_int32 n)
159 {
160  return (*((const l_uint32 *)line + (n >> 4)) >> (2 * (15 - (n & 15)))) & 3;
161 }
162 
163 
172 void
173 l_setDataDibit(void *line,
174  l_int32 n,
175  l_int32 val)
176 {
177 l_uint32 *pword;
178 
179  pword = (l_uint32 *)line + (n >> 4);
180  *pword &= ~(0xc0000000 >> (2 * (n & 15))); /* clear */
181  *pword |= (l_uint32)(val & 3) << (30 - 2 * (n & 15)); /* set */
182 }
183 
184 
194 void
195 l_clearDataDibit(void *line,
196  l_int32 n)
197 {
198  *((l_uint32 *)line + (n >> 4)) &= ~(0xc0000000 >> (2 * (n & 15)));
199 }
200 
201 
209 l_int32
210 l_getDataQbit(const void *line,
211  l_int32 n)
212 {
213  return (*((const l_uint32 *)line + (n >> 3)) >> (4 * (7 - (n & 7)))) & 0xf;
214 }
215 
216 
225 void
226 l_setDataQbit(void *line,
227  l_int32 n,
228  l_int32 val)
229 {
230 l_uint32 *pword;
231 
232  pword = (l_uint32 *)line + (n >> 3);
233  *pword &= ~(0xf0000000 >> (4 * (n & 7))); /* clear */
234  *pword |= (l_uint32)(val & 15) << (28 - 4 * (n & 7)); /* set */
235 }
236 
237 
247 void
248 l_clearDataQbit(void *line,
249  l_int32 n)
250 {
251  *((l_uint32 *)line + (n >> 3)) &= ~(0xf0000000 >> (4 * (n & 7)));
252 }
253 
254 
262 l_int32
263 l_getDataByte(const void *line,
264  l_int32 n)
265 {
266 #ifdef L_BIG_ENDIAN
267  return *((const l_uint8 *)line + n);
268 #else /* L_LITTLE_ENDIAN */
269  return *(l_uint8 *)((l_uintptr_t)((const l_uint8 *)line + n) ^ 3);
270 #endif /* L_BIG_ENDIAN */
271 }
272 
273 
282 void
283 l_setDataByte(void *line,
284  l_int32 n,
285  l_int32 val)
286 {
287 #ifdef L_BIG_ENDIAN
288  *((l_uint8 *)line + n) = val;
289 #else /* L_LITTLE_ENDIAN */
290  *(l_uint8 *)((l_uintptr_t)((l_uint8 *)line + n) ^ 3) = val;
291 #endif /* L_BIG_ENDIAN */
292 }
293 
294 
302 l_int32
303 l_getDataTwoBytes(const void *line,
304  l_int32 n)
305 {
306 #ifdef L_BIG_ENDIAN
307  return *((const l_uint16 *)line + n);
308 #else /* L_LITTLE_ENDIAN */
309  return *(l_uint16 *)((l_uintptr_t)((const l_uint16 *)line + n) ^ 2);
310 #endif /* L_BIG_ENDIAN */
311 }
312 
313 
322 void
323 l_setDataTwoBytes(void *line,
324  l_int32 n,
325  l_int32 val)
326 {
327 #ifdef L_BIG_ENDIAN
328  *((l_uint16 *)line + n) = val;
329 #else /* L_LITTLE_ENDIAN */
330  *(l_uint16 *)((l_uintptr_t)((l_uint16 *)line + n) ^ 2) = val;
331 #endif /* L_BIG_ENDIAN */
332 }
333 
334 
342 l_int32
343 l_getDataFourBytes(const void *line,
344  l_int32 n)
345 {
346  return *((const l_uint32 *)line + n);
347 }
348 
349 
358 void
360  l_int32 n,
361  l_int32 val)
362 {
363  *((l_uint32 *)line + n) = val;
364 }
void l_clearDataQbit(void *line, l_int32 n)
l_clearDataQbit()
Definition: arrayaccess.c:248
void l_clearDataDibit(void *line, l_int32 n)
l_clearDataDibit()
Definition: arrayaccess.c:195
void l_clearDataBit(void *line, l_int32 n)
l_clearDataBit()
Definition: arrayaccess.c:111
void l_setDataDibit(void *line, l_int32 n, l_int32 val)
l_setDataDibit()
Definition: arrayaccess.c:173
void l_setDataByte(void *line, l_int32 n, l_int32 val)
l_setDataByte()
Definition: arrayaccess.c:283
l_int32 l_getDataByte(const void *line, l_int32 n)
l_getDataByte()
Definition: arrayaccess.c:263
l_int32 l_getDataDibit(const void *line, l_int32 n)
l_getDataDibit()
Definition: arrayaccess.c:157
l_int32 l_getDataTwoBytes(const void *line, l_int32 n)
l_getDataTwoBytes()
Definition: arrayaccess.c:303
void l_setDataBitVal(void *line, l_int32 n, l_int32 val)
l_setDataBitVal()
Definition: arrayaccess.c:137
void l_setDataQbit(void *line, l_int32 n, l_int32 val)
l_setDataQbit()
Definition: arrayaccess.c:226
l_int32 l_getDataQbit(const void *line, l_int32 n)
l_getDataQbit()
Definition: arrayaccess.c:210
l_int32 l_getDataFourBytes(const void *line, l_int32 n)
l_getDataFourBytes()
Definition: arrayaccess.c:343
void l_setDataTwoBytes(void *line, l_int32 n, l_int32 val)
l_setDataTwoBytes()
Definition: arrayaccess.c:323
void l_setDataFourBytes(void *line, l_int32 n, l_int32 val)
l_setDataFourBytes()
Definition: arrayaccess.c:359
l_int32 l_getDataBit(const void *line, l_int32 n)
l_getDataBit()
Definition: arrayaccess.c:77
void l_setDataBit(void *line, l_int32 n)
l_setDataBit()
Definition: arrayaccess.c:94