Leptonica  1.77.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 #include "allheaders.h"
61 
62 
63 /*----------------------------------------------------------------------*
64  * Access within an array of 32-bit words *
65  *----------------------------------------------------------------------*/
73 l_int32
74 l_getDataBit(void *line,
75  l_int32 n)
76 {
77  return (*((l_uint32 *)line + (n >> 5)) >> (31 - (n & 31))) & 1;
78 }
79 
80 
90 void
91 l_setDataBit(void *line,
92  l_int32 n)
93 {
94  *((l_uint32 *)line + (n >> 5)) |= (0x80000000 >> (n & 31));
95 }
96 
97 
107 void
108 l_clearDataBit(void *line,
109  l_int32 n)
110 {
111  *((l_uint32 *)line + (n >> 5)) &= ~(0x80000000 >> (n & 31));
112 }
113 
114 
133 void
134 l_setDataBitVal(void *line,
135  l_int32 n,
136  l_int32 val)
137 {
138 l_uint32 *pword;
139 
140  pword = (l_uint32 *)line + (n >> 5);
141  *pword &= ~(0x80000000 >> (n & 31)); /* clear */
142  *pword |= val << (31 - (n & 31)); /* set */
143  return;
144 }
145 
146 
154 l_int32
155 l_getDataDibit(void *line,
156  l_int32 n)
157 {
158  return (*((l_uint32 *)line + (n >> 4)) >> (2 * (15 - (n & 15)))) & 3;
159 }
160 
161 
170 void
171 l_setDataDibit(void *line,
172  l_int32 n,
173  l_int32 val)
174 {
175 l_uint32 *pword;
176 
177  pword = (l_uint32 *)line + (n >> 4);
178  *pword &= ~(0xc0000000 >> (2 * (n & 15))); /* clear */
179  *pword |= (val & 3) << (30 - 2 * (n & 15)); /* set */
180  return;
181 }
182 
183 
193 void
194 l_clearDataDibit(void *line,
195  l_int32 n)
196 {
197  *((l_uint32 *)line + (n >> 4)) &= ~(0xc0000000 >> (2 * (n & 15)));
198 }
199 
200 
208 l_int32
209 l_getDataQbit(void *line,
210  l_int32 n)
211 {
212  return (*((l_uint32 *)line + (n >> 3)) >> (4 * (7 - (n & 7)))) & 0xf;
213 }
214 
215 
224 void
225 l_setDataQbit(void *line,
226  l_int32 n,
227  l_int32 val)
228 {
229 l_uint32 *pword;
230 
231  pword = (l_uint32 *)line + (n >> 3);
232  *pword &= ~(0xf0000000 >> (4 * (n & 7))); /* clear */
233  *pword |= (val & 15) << (28 - 4 * (n & 7)); /* set */
234  return;
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(void *line,
264  l_int32 n)
265 {
266 #ifdef L_BIG_ENDIAN
267  return *((l_uint8 *)line + n);
268 #else /* L_LITTLE_ENDIAN */
269  return *(l_uint8 *)((l_uintptr_t)((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(void *line,
304  l_int32 n)
305 {
306 #ifdef L_BIG_ENDIAN
307  return *((l_uint16 *)line + n);
308 #else /* L_LITTLE_ENDIAN */
309  return *(l_uint16 *)((l_uintptr_t)((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
344  l_int32 n)
345 {
346  return *((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:194
void l_clearDataBit(void *line, l_int32 n)
l_clearDataBit()
Definition: arrayaccess.c:108
l_int32 l_getDataTwoBytes(void *line, l_int32 n)
l_getDataTwoBytes()
Definition: arrayaccess.c:303
void l_setDataDibit(void *line, l_int32 n, l_int32 val)
l_setDataDibit()
Definition: arrayaccess.c:171
void l_setDataByte(void *line, l_int32 n, l_int32 val)
l_setDataByte()
Definition: arrayaccess.c:283
void l_setDataBitVal(void *line, l_int32 n, l_int32 val)
l_setDataBitVal()
Definition: arrayaccess.c:134
void l_setDataQbit(void *line, l_int32 n, l_int32 val)
l_setDataQbit()
Definition: arrayaccess.c:225
l_int32 l_getDataDibit(void *line, l_int32 n)
l_getDataDibit()
Definition: arrayaccess.c:155
l_int32 l_getDataFourBytes(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(void *line, l_int32 n)
l_getDataBit()
Definition: arrayaccess.c:74
l_int32 l_getDataByte(void *line, l_int32 n)
l_getDataByte()
Definition: arrayaccess.c:263
l_int32 l_getDataQbit(void *line, l_int32 n)
l_getDataQbit()
Definition: arrayaccess.c:209
void l_setDataBit(void *line, l_int32 n)
l_setDataBit()
Definition: arrayaccess.c:91