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

http://eyes-free.googlecode.com/ · C Header · 190 lines · 89 code · 37 blank · 64 comment · 1 complexity · 94f22a88fd23f9f0761961d006b56f57 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. #ifndef LEPTONICA_ARRAY_ACCESS_H
  16. #define LEPTONICA_ARRAY_ACCESS_H
  17. /*
  18. * arrayaccess.h
  19. *
  20. * 1, 2, 4, 8, 16 and 32 bit data access within an array of 32-bit words
  21. *
  22. * This is used primarily to access 1, 2, 4, 8, 16 and 32 bit pixels
  23. * in a line of image data, represented as an array of 32-bit words.
  24. *
  25. * pdata: pointer to first 32-bit word in the array
  26. * n: index of the pixel in the array
  27. *
  28. * Function calls for these accessors are defined in arrayaccess.c.
  29. *
  30. * However, for efficiency we use the inline macros for all accesses.
  31. * Even though the 2 and 4 bit set* accessors are more complicated,
  32. * they are about 10% faster than the function calls.
  33. *
  34. * The 32 bit access is just a cast and ptr arithmetic. We include
  35. * it so that the input ptr can be void*.
  36. *
  37. * At the end of this file is code for invoking the function calls
  38. * instead of inlining.
  39. *
  40. * The macro SET_DATA_BIT_VAL(pdata, n, val) is a bit slower than
  41. * if (val == 0)
  42. * CLEAR_DATA_BIT(pdata, n);
  43. * else
  44. * SET_DATA_BIT(pdata, n);
  45. */
  46. #if 1 /* Inline Accessors */
  47. #ifndef _MSC_VER
  48. /*--------------------------------------------------*
  49. * 1 bit access *
  50. *--------------------------------------------------*/
  51. #define GET_DATA_BIT(pdata, n) \
  52. ((*((l_uint32 *)(pdata) + ((n) >> 5)) >> (31 - ((n) & 31))) & 1)
  53. #define SET_DATA_BIT(pdata, n) \
  54. (*((l_uint32 *)(pdata) + ((n) >> 5)) |= (0x80000000 >> ((n) & 31)))
  55. #define CLEAR_DATA_BIT(pdata, n) \
  56. (*((l_uint32 *)(pdata) + ((n) >> 5)) &= ~(0x80000000 >> ((n) & 31)))
  57. #define SET_DATA_BIT_VAL(pdata, n, val) \
  58. ({l_uint32 *_TEMP_WORD_PTR_; \
  59. _TEMP_WORD_PTR_ = (l_uint32 *)(pdata) + ((n) >> 5); \
  60. *_TEMP_WORD_PTR_ &= ~(0x80000000 >> ((n) & 31)); \
  61. *_TEMP_WORD_PTR_ |= ((val) << (31 - ((n) & 31))); \
  62. })
  63. /*--------------------------------------------------*
  64. * 2 bit access *
  65. *--------------------------------------------------*/
  66. #define GET_DATA_DIBIT(pdata, n) \
  67. ((*((l_uint32 *)(pdata) + ((n) >> 4)) >> (2 * (15 - ((n) & 15)))) & 3)
  68. #define SET_DATA_DIBIT(pdata, n, val) \
  69. ({l_uint32 *_TEMP_WORD_PTR_; \
  70. _TEMP_WORD_PTR_ = (l_uint32 *)(pdata) + ((n) >> 4); \
  71. *_TEMP_WORD_PTR_ &= ~(0xc0000000 >> (2 * ((n) & 15))); \
  72. *_TEMP_WORD_PTR_ |= (((val) & 3) << (30 - 2 * ((n) & 15))); \
  73. })
  74. #define CLEAR_DATA_DIBIT(pdata, n) \
  75. (*((l_uint32 *)(pdata) + ((n) >> 4)) &= ~(0xc0000000 >> (2 * ((n) & 15))))
  76. /*--------------------------------------------------*
  77. * 4 bit access *
  78. *--------------------------------------------------*/
  79. #define GET_DATA_QBIT(pdata, n) \
  80. ((*((l_uint32 *)(pdata) + ((n) >> 3)) >> (4 * (7 - ((n) & 7)))) & 0xf)
  81. #define SET_DATA_QBIT(pdata, n, val) \
  82. ({l_uint32 *_TEMP_WORD_PTR_; \
  83. _TEMP_WORD_PTR_ = (l_uint32 *)(pdata) + ((n) >> 3); \
  84. *_TEMP_WORD_PTR_ &= ~(0xf0000000 >> (4 * ((n) & 7))); \
  85. *_TEMP_WORD_PTR_ |= (((val) & 15) << (28 - 4 * ((n) & 7))); \
  86. })
  87. #define CLEAR_DATA_QBIT(pdata, n) \
  88. (*((l_uint32 *)(pdata) + ((n) >> 3)) &= ~(0xf0000000 >> (4 * ((n) & 7))))
  89. /*--------------------------------------------------*
  90. * 8 bit access *
  91. *--------------------------------------------------*/
  92. #ifdef L_BIG_ENDIAN
  93. #define GET_DATA_BYTE(pdata, n) \
  94. (*((l_uint8 *)(pdata) + (n)))
  95. #else /* L_LITTLE_ENDIAN */
  96. #define GET_DATA_BYTE(pdata, n) \
  97. (*(l_uint8 *)((l_uintptr_t)((l_uint8 *)(pdata) + (n)) ^ 3))
  98. #endif /* L_BIG_ENDIAN */
  99. #ifdef L_BIG_ENDIAN
  100. #define SET_DATA_BYTE(pdata, n, val) \
  101. (*((l_uint8 *)(pdata) + (n)) = (val))
  102. #else /* L_LITTLE_ENDIAN */
  103. #define SET_DATA_BYTE(pdata, n, val) \
  104. (*(l_uint8 *)((l_uintptr_t)((l_uint8 *)(pdata) + (n)) ^ 3) = (val))
  105. #endif /* L_BIG_ENDIAN */
  106. /*--------------------------------------------------*
  107. * 16 bit access *
  108. *--------------------------------------------------*/
  109. #ifdef L_BIG_ENDIAN
  110. #define GET_DATA_TWO_BYTES(pdata, n) \
  111. (*((l_uint16 *)(pdata) + (n)))
  112. #else /* L_LITTLE_ENDIAN */
  113. #define GET_DATA_TWO_BYTES(pdata, n) \
  114. (*(l_uint16 *)((l_uintptr_t)((l_uint16 *)(pdata) + (n)) ^ 2))
  115. #endif /* L_BIG_ENDIAN */
  116. #ifdef L_BIG_ENDIAN
  117. #define SET_DATA_TWO_BYTES(pdata, n, val) \
  118. (*((l_uint16 *)(pdata) + (n)) = (val))
  119. #else /* L_LITTLE_ENDIAN */
  120. #define SET_DATA_TWO_BYTES(pdata, n, val) \
  121. (*(l_uint16 *)((l_uintptr_t)((l_uint16 *)(pdata) + (n)) ^ 2) = (val))
  122. #endif /* L_BIG_ENDIAN */
  123. /*--------------------------------------------------*
  124. * 32 bit access *
  125. *--------------------------------------------------*/
  126. #define GET_DATA_FOUR_BYTES(pdata, n) \
  127. (*((l_uint32 *)(pdata) + (n)))
  128. #define SET_DATA_FOUR_BYTES(pdata, n, val) \
  129. (*((l_uint32 *)(pdata) + (n)) = (val))
  130. #endif /* ! _MSC_VER */
  131. #endif /* Inline Accessors */
  132. /*--------------------------------------------------*
  133. * Slower, using function calls for all accessors *
  134. *--------------------------------------------------*/
  135. #if 0 || defined(_MSC_VER)
  136. #define GET_DATA_BIT(pdata, n) l_getDataBit(pdata, n)
  137. #define SET_DATA_BIT(pdata, n) l_setDataBit(pdata, n)
  138. #define CLEAR_DATA_BIT(pdata, n) l_clearDataBit(pdata, n)
  139. #define SET_DATA_BIT_VAL(pdata, n, val) l_setDataBitVal(pdata, n, val)
  140. #define GET_DATA_DIBIT(pdata, n) l_getDataDibit(pdata, n)
  141. #define SET_DATA_DIBIT(pdata, n, val) l_setDataDibit(pdata, n, val)
  142. #define CLEAR_DATA_DIBIT(pdata, n) l_clearDataDibit(pdata, n)
  143. #define GET_DATA_QBIT(pdata, n) l_getDataQbit(pdata, n)
  144. #define SET_DATA_QBIT(pdata, n, val) l_setDataQbit(pdata, n, val)
  145. #define CLEAR_DATA_QBIT(pdata, n) l_clearDataQbit(pdata, n)
  146. #define GET_DATA_BYTE(pdata, n) l_getDataByte(pdata, n)
  147. #define SET_DATA_BYTE(pdata, n, val) l_setDataByte(pdata, n, val)
  148. #define GET_DATA_TWO_BYTES(pdata, n) l_getDataTwoBytes(pdata, n)
  149. #define SET_DATA_TWO_BYTES(pdata, n, val) l_setDataTwoBytes(pdata, n, val)
  150. #define GET_DATA_FOUR_BYTES(pdata, n) l_getDataFourBytes(pdata, n)
  151. #define SET_DATA_FOUR_BYTES(pdata, n, val) l_setDataFourBytes(pdata, n, val)
  152. #endif
  153. #endif /* LEPTONICA_ARRAY_ACCESS_H */