Leptonica 1.85.0
Image processing and image analysis suite
Loading...
Searching...
No Matches
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 *----------------------------------------------------------------------*/
76l_int32
77l_getDataBit(const void *line,
78 l_int32 n)
79{
80 return (*((const l_uint32 *)line + (n >> 5)) >> (31 - (n & 31))) & 1;
81}
82
83
93void
94l_setDataBit(void *line,
95 l_int32 n)
96{
97 *((l_uint32 *)line + (n >> 5)) |= (0x80000000 >> (n & 31));
98}
99
100
110void
111l_clearDataBit(void *line,
112 l_int32 n)
113{
114 *((l_uint32 *)line + (n >> 5)) &= ~(0x80000000 >> (n & 31));
115}
116
117
136void
138 l_int32 n,
139 l_int32 val)
140{
141l_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
156l_int32
157l_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
172void
173l_setDataDibit(void *line,
174 l_int32 n,
175 l_int32 val)
176{
177l_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
194void
196 l_int32 n)
197{
198 *((l_uint32 *)line + (n >> 4)) &= ~(0xc0000000 >> (2 * (n & 15)));
199}
200
201
209l_int32
210l_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
225void
226l_setDataQbit(void *line,
227 l_int32 n,
228 l_int32 val)
229{
230l_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
247void
249 l_int32 n)
250{
251 *((l_uint32 *)line + (n >> 3)) &= ~(0xf0000000 >> (4 * (n & 7)));
252}
253
254
262l_int32
263l_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
282void
283l_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
302l_int32
303l_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
322void
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
342l_int32
343l_getDataFourBytes(const void *line,
344 l_int32 n)
345{
346 return *((const l_uint32 *)line + n);
347}
348
349
358void
360 l_int32 n,
361 l_int32 val)
362{
363 *((l_uint32 *)line + n) = val;
364}
void l_clearDataBit(void *line, l_int32 n)
l_clearDataBit()
void l_setDataBitVal(void *line, l_int32 n, l_int32 val)
l_setDataBitVal()
void l_setDataDibit(void *line, l_int32 n, l_int32 val)
l_setDataDibit()
void l_setDataFourBytes(void *line, l_int32 n, l_int32 val)
l_setDataFourBytes()
void l_setDataTwoBytes(void *line, l_int32 n, l_int32 val)
l_setDataTwoBytes()
void l_clearDataDibit(void *line, l_int32 n)
l_clearDataDibit()
void l_clearDataQbit(void *line, l_int32 n)
l_clearDataQbit()
void l_setDataByte(void *line, l_int32 n, l_int32 val)
l_setDataByte()
void l_setDataBit(void *line, l_int32 n)
l_setDataBit()
Definition arrayaccess.c:94
l_int32 l_getDataTwoBytes(const void *line, l_int32 n)
l_getDataTwoBytes()
l_int32 l_getDataFourBytes(const void *line, l_int32 n)
l_getDataFourBytes()
void l_setDataQbit(void *line, l_int32 n, l_int32 val)
l_setDataQbit()
l_int32 l_getDataBit(const void *line, l_int32 n)
l_getDataBit()
Definition arrayaccess.c:77
l_int32 l_getDataQbit(const void *line, l_int32 n)
l_getDataQbit()
l_int32 l_getDataByte(const void *line, l_int32 n)
l_getDataByte()
l_int32 l_getDataDibit(const void *line, l_int32 n)
l_getDataDibit()