Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
6.0 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. util.c
  5. Abstract:
  6. This file contains utilitarian functions for
  7. the FAX TIFF library.
  8. Environment:
  9. WIN32 User Mode
  10. Author:
  11. Wesley Witt (wesw) 17-Feb-1996
  12. --*/
  13. #include "tifflibp.h"
  14. #pragma hdrstop
  15. INT
  16. FindWhiteRun(
  17. PBYTE pbuf,
  18. INT startBit,
  19. INT stopBit
  20. )
  21. /*++
  22. Routine Description:
  23. Find the next span of white pixels on the specified line
  24. Arguments:
  25. pbuf - Points to uncompressed pixel data for the current line
  26. startBit - Starting bit index
  27. stopBit - Last bit index
  28. Return Value:
  29. Length of the next run of white pixels
  30. --*/
  31. {
  32. static const BYTE WhiteRuns[256] = {
  33. 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  34. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  35. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  36. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  37. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  38. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  39. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  40. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  45. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  46. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  47. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  49. };
  50. INT run, bits, n;
  51. pbuf += (startBit >> 3);
  52. if ((bits = stopBit-startBit) <= 0)
  53. return 0;
  54. //
  55. // Take care of the case where starting bit index is not a multiple of 8
  56. //
  57. if (n = (startBit & 7)) {
  58. run = WhiteRuns[(*pbuf << n) & 0xff];
  59. if (run > BYTEBITS-n)
  60. run = BYTEBITS-n;
  61. if (n+run < BYTEBITS)
  62. return run;
  63. bits -= run;
  64. pbuf++;
  65. } else
  66. run = 0;
  67. //
  68. // Look for consecutive DWORD value = 0
  69. //
  70. if (bits >= DWORDBITS * 2) {
  71. PDWORD pdw;
  72. //
  73. // Align to a DWORD boundary first
  74. //
  75. while ((DWORD_PTR) pbuf & 3) {
  76. if (*pbuf != 0)
  77. return run + WhiteRuns[*pbuf];
  78. run += BYTEBITS;
  79. bits -= BYTEBITS;
  80. pbuf++;
  81. }
  82. pdw = (PDWORD) pbuf;
  83. while (bits >= DWORDBITS && *pdw == 0) {
  84. pdw++;
  85. run += DWORDBITS;
  86. bits -= DWORDBITS;
  87. }
  88. pbuf = (PBYTE) pdw;
  89. }
  90. //
  91. // Look for consecutive BYTE value = 0
  92. //
  93. while (bits >= BYTEBITS) {
  94. if (*pbuf != 0)
  95. return run + WhiteRuns[*pbuf];
  96. pbuf++;
  97. run += BYTEBITS;
  98. bits -= BYTEBITS;
  99. }
  100. //
  101. // Count the number of white pixels in the last byte
  102. //
  103. if (bits > 0)
  104. run += WhiteRuns[*pbuf];
  105. return run;
  106. }
  107. INT
  108. FindBlackRun(
  109. PBYTE pbuf,
  110. INT startBit,
  111. INT stopBit
  112. )
  113. /*++
  114. Routine Description:
  115. Find the next span of black pixels on the specified line
  116. Arguments:
  117. pbuf - Points to uncompressed pixel data for the current line
  118. startBit - Starting bit index
  119. stopBit - Last bit index
  120. Return Value:
  121. Length of the next run of black pixels
  122. --*/
  123. {
  124. static const BYTE BlackRuns[256] = {
  125. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  126. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  127. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  128. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  129. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  130. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  131. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  132. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  133. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  134. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  135. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  136. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  137. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  138. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  139. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  140. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8
  141. };
  142. INT run, bits, n;
  143. pbuf += (startBit >> 3);
  144. if ((bits = stopBit-startBit) <= 0)
  145. return 0;
  146. //
  147. // Take care of the case where starting bit index is not a multiple of 8
  148. //
  149. if (n = (startBit & 7)) {
  150. run = BlackRuns[(*pbuf << n) & 0xff];
  151. if (run > BYTEBITS-n)
  152. run = BYTEBITS-n;
  153. if (n+run < BYTEBITS)
  154. return run;
  155. bits -= run;
  156. pbuf++;
  157. } else
  158. run = 0;
  159. //
  160. // Look for consecutive DWORD value = 0xffffffff
  161. //
  162. if (bits >= DWORDBITS * 2) {
  163. PDWORD pdw;
  164. //
  165. // Align to a DWORD boundary first
  166. //
  167. while ((DWORD_PTR) pbuf & 3) {
  168. if (*pbuf != 0xff)
  169. return run + BlackRuns[*pbuf];
  170. run += BYTEBITS;
  171. bits -= BYTEBITS;
  172. pbuf++;
  173. }
  174. pdw = (PDWORD) pbuf;
  175. while (bits >= DWORDBITS && *pdw == 0xffffffff) {
  176. pdw++;
  177. run += DWORDBITS;
  178. bits -= DWORDBITS;
  179. }
  180. pbuf = (PBYTE) pdw;
  181. }
  182. //
  183. // Look for consecutive BYTE value = 0xff
  184. //
  185. while (bits >= BYTEBITS) {
  186. if (*pbuf != 0xff)
  187. return run + BlackRuns[*pbuf];
  188. pbuf++;
  189. run += BYTEBITS;
  190. bits -= BYTEBITS;
  191. }
  192. //
  193. // Count the number of white pixels in the last byte
  194. //
  195. if (bits > 0)
  196. run += BlackRuns[*pbuf];
  197. return run;
  198. }