Leptonica 1.68
C Image Processing Library
|
00001 /*====================================================================* 00002 - Copyright (C) 2001 Leptonica. All rights reserved. 00003 - This software is distributed in the hope that it will be 00004 - useful, but with NO WARRANTY OF ANY KIND. 00005 - No author or distributor accepts responsibility to anyone for the 00006 - consequences of using this software, or for whether it serves any 00007 - particular purpose or works at all, unless he or she says so in 00008 - writing. Everyone is granted permission to copy, modify and 00009 - redistribute this source code, for commercial or non-commercial 00010 - purposes, with the following restrictions: (1) the origin of this 00011 - source code must not be misrepresented; (2) modified versions must 00012 - be plainly marked as such; and (3) this notice may not be removed 00013 - or altered from any source or modified source distribution. 00014 *====================================================================*/ 00015 00016 /* 00017 * arrayaccess.c 00018 * 00019 * Access within an array of 32-bit words 00020 * 00021 * l_int32 l_getDataBit() 00022 * void l_setDataBit() 00023 * void l_clearDataBit() 00024 * void l_setDataBitVal() 00025 * l_int32 l_getDataDibit() 00026 * void l_setDataDibit() 00027 * void l_clearDataDibit() 00028 * l_int32 l_getDataQbit() 00029 * void l_setDataQbit() 00030 * void l_clearDataQbit() 00031 * l_int32 l_getDataByte() 00032 * void l_setDataByte() 00033 * l_int32 l_getDataTwoBytes() 00034 * void l_setDataTwoBytes() 00035 * l_int32 l_getDataFourBytes() 00036 * void l_setDataFourBytes() 00037 * 00038 * Note that these all require 32-bit alignment, and hence an input 00039 * ptr to l_uint32. However, this is not enforced by the compiler. 00040 * Instead, we allow the use of a void* ptr, because the line ptrs 00041 * are an efficient way to get random access (see pixGetLinePtrs()). 00042 * It is then necessary to cast internally within each function 00043 * because ptr arithmetic requires knowing the size of the units 00044 * being referenced. 00045 */ 00046 00047 #include <stdio.h> 00048 #include "allheaders.h" 00049 00050 00051 /*----------------------------------------------------------------------* 00052 * Access within an array of 32-bit words * 00053 *----------------------------------------------------------------------*/ 00054 /*! 00055 * l_getDataBit() 00056 * 00057 * Input: line (ptr to beginning of data line) 00058 * n (pixel index) 00059 * Return: val of the nth (1-bit) pixel. 00060 */ 00061 l_int32 00062 l_getDataBit(void *line, 00063 l_int32 n) 00064 { 00065 return (*((l_uint32 *)line + (n >> 5)) >> (31 - (n & 31))) & 1; 00066 } 00067 00068 00069 /*! 00070 * l_setDataBit() 00071 * 00072 * Input: line (ptr to beginning of data line) 00073 * n (pixel index) 00074 * Return: void 00075 * 00076 * Action: sets the pixel to 1 00077 */ 00078 void 00079 l_setDataBit(void *line, 00080 l_int32 n) 00081 { 00082 *((l_uint32 *)line + (n >> 5)) |= (0x80000000 >> (n & 31)); 00083 } 00084 00085 00086 /*! 00087 * l_clearDataBit() 00088 * 00089 * Input: line (ptr to beginning of data line) 00090 * n (pixel index) 00091 * Return: void 00092 * 00093 * Action: sets the (1-bit) pixel to 0 00094 */ 00095 void 00096 l_clearDataBit(void *line, 00097 l_int32 n) 00098 { 00099 *((l_uint32 *)line + (n >> 5)) &= ~(0x80000000 >> (n & 31)); 00100 } 00101 00102 00103 /*! 00104 * l_setDataBitVal() 00105 * 00106 * Input: line (ptr to beginning of data line) 00107 * n (pixel index) 00108 * val (val to be inserted: 0 or 1) 00109 * Return: void 00110 * 00111 * Notes: 00112 * (1) This is an accessor for a 1 bpp pix. 00113 * (2) It is actually a little slower than using: 00114 * if (val == 0) 00115 * l_ClearDataBit(line, n); 00116 * else 00117 * l_SetDataBit(line, n); 00118 */ 00119 void 00120 l_setDataBitVal(void *line, 00121 l_int32 n, 00122 l_int32 val) 00123 { 00124 l_uint32 *pword; 00125 00126 pword = (l_uint32 *)line + (n >> 5); 00127 *pword &= ~(0x80000000 >> (n & 31)); /* clear */ 00128 *pword |= val << (31 - (n & 31)); /* set */ 00129 return; 00130 } 00131 00132 00133 /*! 00134 * l_getDataDibit() 00135 * 00136 * Input: line (ptr to beginning of data line) 00137 * n (pixel index) 00138 * Return: val of the nth (2-bit) pixel. 00139 */ 00140 l_int32 00141 l_getDataDibit(void *line, 00142 l_int32 n) 00143 { 00144 return (*((l_uint32 *)line + (n >> 4)) >> (2 * (15 - (n & 15)))) & 3; 00145 } 00146 00147 00148 /*! 00149 * l_setDataDibit() 00150 * 00151 * Input: line (ptr to beginning of data line) 00152 * n (pixel index) 00153 * val (val to be inserted: 0 - 3) 00154 * Return: void 00155 */ 00156 void 00157 l_setDataDibit(void *line, 00158 l_int32 n, 00159 l_int32 val) 00160 { 00161 l_uint32 *pword; 00162 00163 pword = (l_uint32 *)line + (n >> 4); 00164 *pword &= ~(0xc0000000 >> (2 * (n & 15))); /* clear */ 00165 *pword |= (val & 3) << (30 - 2 * (n & 15)); /* set */ 00166 return; 00167 } 00168 00169 00170 /*! 00171 * l_clearDataDibit() 00172 * 00173 * Input: line (ptr to beginning of data line) 00174 * n (pixel index) 00175 * Return: void 00176 * 00177 * Action: sets the (2-bit) pixel to 0 00178 */ 00179 void 00180 l_clearDataDibit(void *line, 00181 l_int32 n) 00182 { 00183 *((l_uint32 *)line + (n >> 4)) &= ~(0xc0000000 >> (2 * (n & 15))); 00184 } 00185 00186 00187 /*! 00188 * l_getDataQbit() 00189 * 00190 * Input: line (ptr to beginning of data line) 00191 * n (pixel index) 00192 * Return: val of the nth (4-bit) pixel. 00193 */ 00194 l_int32 00195 l_getDataQbit(void *line, 00196 l_int32 n) 00197 { 00198 return (*((l_uint32 *)line + (n >> 3)) >> (4 * (7 - (n & 7)))) & 0xf; 00199 } 00200 00201 00202 /*! 00203 * l_setDataQbit() 00204 * 00205 * Input: line (ptr to beginning of data line) 00206 * n (pixel index) 00207 * val (val to be inserted: 0 - 0xf) 00208 * Return: void 00209 */ 00210 void 00211 l_setDataQbit(void *line, 00212 l_int32 n, 00213 l_int32 val) 00214 { 00215 l_uint32 *pword; 00216 00217 pword = (l_uint32 *)line + (n >> 3); 00218 *pword &= ~(0xf0000000 >> (4 * (n & 7))); /* clear */ 00219 *pword |= (val & 15) << (28 - 4 * (n & 7)); /* set */ 00220 return; 00221 } 00222 00223 00224 /*! 00225 * l_clearDataQbit() 00226 * 00227 * Input: line (ptr to beginning of data line) 00228 * n (pixel index) 00229 * Return: void 00230 * 00231 * Action: sets the (4-bit) pixel to 0 00232 */ 00233 void 00234 l_clearDataQbit(void *line, 00235 l_int32 n) 00236 { 00237 *((l_uint32 *)line + (n >> 3)) &= ~(0xf0000000 >> (4 * (n & 7))); 00238 } 00239 00240 00241 /*! 00242 * l_getDataByte() 00243 * 00244 * Input: line (ptr to beginning of data line) 00245 * n (pixel index) 00246 * Return: value of the n-th (byte) pixel 00247 */ 00248 l_int32 00249 l_getDataByte(void *line, 00250 l_int32 n) 00251 { 00252 #ifdef L_BIG_ENDIAN 00253 return *((l_uint8 *)line + n); 00254 #else /* L_LITTLE_ENDIAN */ 00255 return *(l_uint8 *)((l_uintptr_t)((l_uint8 *)line + n) ^ 3); 00256 #endif /* L_BIG_ENDIAN */ 00257 } 00258 00259 00260 /*! 00261 * l_setDataByte() 00262 * 00263 * Input: line (ptr to beginning of data line) 00264 * n (pixel index) 00265 * val (val to be inserted: 0 - 0xff) 00266 * Return: void 00267 */ 00268 void 00269 l_setDataByte(void *line, 00270 l_int32 n, 00271 l_int32 val) 00272 { 00273 #ifdef L_BIG_ENDIAN 00274 *((l_uint8 *)line + n) = val; 00275 #else /* L_LITTLE_ENDIAN */ 00276 *(l_uint8 *)((l_uintptr_t)((l_uint8 *)line + n) ^ 3) = val; 00277 #endif /* L_BIG_ENDIAN */ 00278 } 00279 00280 00281 /*! 00282 * l_getDataTwoBytes() 00283 * 00284 * Input: line (ptr to beginning of data line) 00285 * n (pixel index) 00286 * Return: value of the n-th (2-byte) pixel 00287 */ 00288 l_int32 00289 l_getDataTwoBytes(void *line, 00290 l_int32 n) 00291 { 00292 #ifdef L_BIG_ENDIAN 00293 return *((l_uint16 *)line + n); 00294 #else /* L_LITTLE_ENDIAN */ 00295 return *(l_uint16 *)((l_uintptr_t)((l_uint16 *)line + n) ^ 2); 00296 #endif /* L_BIG_ENDIAN */ 00297 } 00298 00299 00300 /*! 00301 * l_setDataTwoBytes() 00302 * 00303 * Input: line (ptr to beginning of data line) 00304 * n (pixel index) 00305 * val (val to be inserted: 0 - 0xffff) 00306 * Return: void 00307 */ 00308 void 00309 l_setDataTwoBytes(void *line, 00310 l_int32 n, 00311 l_int32 val) 00312 { 00313 #ifdef L_BIG_ENDIAN 00314 *((l_uint16 *)line + n) = val; 00315 #else /* L_LITTLE_ENDIAN */ 00316 *(l_uint16 *)((l_uintptr_t)((l_uint16 *)line + n) ^ 2) = val; 00317 #endif /* L_BIG_ENDIAN */ 00318 } 00319 00320 00321 /*! 00322 * l_getDataFourBytes() 00323 * 00324 * Input: line (ptr to beginning of data line) 00325 * n (pixel index) 00326 * Return: value of the n-th (4-byte) pixel 00327 */ 00328 l_int32 00329 l_getDataFourBytes(void *line, 00330 l_int32 n) 00331 { 00332 return *((l_uint32 *)line + n); 00333 } 00334 00335 00336 /*! 00337 * l_setDataFourBytes() 00338 * 00339 * Input: line (ptr to beginning of data line) 00340 * n (pixel index) 00341 * val (val to be inserted: 0 - 0xffffffff) 00342 * Return: void 00343 */ 00344 void 00345 l_setDataFourBytes(void *line, 00346 l_int32 n, 00347 l_int32 val) 00348 { 00349 *((l_uint32 *)line + n) = val; 00350 } 00351 00352 00353