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.

301 lines
9.3 KiB

  1. //-----------------------------------------------------------------------------
  2. // FILE NAME : FUMH2.c
  3. // FUNCTION : MH Compress and MH2 Compress
  4. // AUTHER : 1996.08.08 FPL)Y.YUTANI
  5. // NOTE : for Windows NT V4.0
  6. // MODIFY : Reduce data size Oct.31,1996 H.Ishida
  7. // MODIFY : for NT5.0 Minidriver Sep.3,1997 H.Ishida(FPL)
  8. //-----------------------------------------------------------------------------
  9. // COPYRIGHT(C) FUJITSU LIMITED 1996-1997
  10. #include <minidrv.h>
  11. #include "fuxl.h"
  12. #include "fumhdef.h"
  13. // NTRAID#NTBUG9-589500-2002/03/29-v-kkon-: Remove dead codes
  14. //-----------------------------------------------------------------------------
  15. // BOOL SameLineCheck
  16. // PBYTE pSrc Pointer of sources bits image
  17. // DWORD cSrcX Width size of sources image(byte)
  18. // Return code : TRUE Same image line
  19. // FALSE Not same image line
  20. //-----------------------------------------------------------------------------
  21. BOOL SameLineCheck( PBYTE pSrc, DWORD cSrcX )
  22. {
  23. DWORD i;
  24. PBYTE pLine1, pLine2;
  25. pLine1 = pSrc;
  26. pLine2 = pSrc + cSrcX;
  27. for( i = 0; i < cSrcX; i++ ) {
  28. if( *pLine1 != *pLine2 ) return FALSE;
  29. pLine1++;
  30. pLine2++;
  31. }
  32. return TRUE;
  33. }
  34. //-----------------------------------------------------------------------------
  35. // DWORD SamePatternCheck
  36. // BYTE *pTmp Pointer of sources bits image
  37. // DWORD cBitsTmp Bit number from top of sources bits image
  38. // DWORD cBitsMax Maximam bit of sources bits image
  39. // PATNINFO *pInfo Pointer of Same pattern finormation struction
  40. // Rerutn code : Sources bit number
  41. //-----------------------------------------------------------------------------
  42. DWORD SamePatternCheck( BYTE *pTmp, DWORD cBitsTmp, DWORD cBitsMax, PATNINFO *pInfo )
  43. {
  44. DWORD cBits, k;
  45. BYTE ptn1, ptn2;
  46. DWORD dwPtn;
  47. // Initial same pattern number
  48. pInfo->dwPatnNum = 1;
  49. // Nothing remain bits
  50. if( cBitsTmp >= cBitsMax ) return cBitsTmp;
  51. // Caluclation sources bytes and bits
  52. pTmp += (cBitsTmp / 8);
  53. k = cBitsTmp % 8;
  54. // If remain bits bolow 16bits, return function
  55. if( ( cBitsTmp + 16 ) > cBitsMax ) return cBitsTmp;
  56. // Get Top 8bits(bit number is byte baundary?)
  57. // NTRAID#NTBUG9-589500-2002/03/29-v-kkon-: Remove dead codes
  58. if( k != 0 ) {
  59. ptn1 = *pTmp << k;
  60. ptn1 |= *(pTmp+1) >> ( 8 - k );
  61. } else {
  62. ptn1 = *pTmp;
  63. }
  64. // If 8bits image is all white or black, return function
  65. if( ptn1 == ALL_BLACK || ptn1 == ALL_WHITE ) return cBitsTmp;
  66. // Compare top 8bits image and next 8bits image
  67. // (Careful same pattern number maximam)
  68. for (cBits = cBitsTmp + 8;
  69. (cBits + 7 < cBitsMax) && (pInfo->dwPatnNum < SAMEPATN_MAX); cBits += 8 ) {
  70. pTmp++;
  71. // NTRAID#NTBUG9-589500-2002/03/29-v-kkon-: Remove dead codes
  72. if( k != 0 ) {
  73. ptn2 = *pTmp << k;
  74. ptn2 |= *(pTmp+1) >> ( 8 - k );
  75. } else {
  76. ptn2 = *pTmp;
  77. }
  78. // If top image not iqual next image, stop counting same pattern
  79. if( ptn1 != ptn2 ) break;
  80. // Same pattern number addition
  81. pInfo->dwPatnNum++;
  82. }
  83. // Nothing same pattern
  84. if( pInfo->dwPatnNum == 1 ) return cBitsTmp;
  85. // Set pattern
  86. pInfo->dwPatn = (DWORD)ptn1;
  87. // If bits remain, check joint bit's color and set
  88. if( cBits < cBitsMax ) {
  89. if ( (*pTmp & (1 << (7 - k)) ) == 0 ) {
  90. pInfo->dwNextColor = NEXT_COLOR_WHITE;
  91. } else {
  92. pInfo->dwNextColor = NEXT_COLOR_BLACK;
  93. }
  94. } else {
  95. pInfo->dwNextColor = NEXT_COLOR_WHITE;
  96. }
  97. return cBits;
  98. }
  99. //-----------------------------------------------------------------------------
  100. // DWORD Mh2Compress
  101. // BYTE *pDest Pointer of destinaition area
  102. // DWORD cDestN Size of destination area(byte)
  103. // BYTE *pSrc Pointer of sources area
  104. // DWORD cSrcN Size of sources area(byte)
  105. // DWORD cSrcX Sources image x width
  106. // DWORD cSrcY Sources image y height
  107. // Return code : Writing size to destination area
  108. //-----------------------------------------------------------------------------
  109. DWORD Mh2Compress( BYTE *pDest, DWORD cDestN, BYTE *pSrc, DWORD cSrcN, DWORD cSrcX, DWORD cSrcY )
  110. {
  111. DWORD cBitsSrc, cBitsSrcMax;
  112. DWORD cBitsDest, cBitsDestMax, cBitsDestMark;
  113. DWORD cBitsRun;
  114. DWORD dwCode, cBits;
  115. DWORD i;
  116. DWORD dwSameLine;
  117. PBYTE pSrcLine;
  118. PATNINFO ptnInfo;
  119. cBitsDest = 0;
  120. cBitsSrc = 0;
  121. dwSameLine = 1;
  122. cBitsDestMax = cDestN * 8;
  123. for (i = 0; i < cSrcY; i++) {
  124. // Set initial color
  125. ptnInfo.dwNextColor = NEXT_COLOR_WHITE;
  126. // Top pointer of now line
  127. pSrcLine = pSrc + ( i * cSrcX );
  128. // Now line equal next line image?(No check last line)
  129. if( i != ( cSrcY - 1 ) ) {
  130. if( SameLineCheck( pSrcLine, cSrcX ) ) {
  131. dwSameLine++;
  132. cBitsSrc += ( cSrcX * 8 );
  133. if( dwSameLine < SAMELINE_MAX ) continue;
  134. }
  135. }
  136. // Top EOL
  137. if (cBitsDest + CBITS_EOL_CODE > cBitsDestMax)
  138. return 0;
  139. FjBitsCopy(pDest, cBitsDest, EOL_CODE, CBITS_EOL_CODE);
  140. cBitsDest += CBITS_EOL_CODE;
  141. // There are same lines
  142. if( dwSameLine > 1 ) {
  143. if (cBitsDest + CBITS_SAMELINE > cBitsDestMax)
  144. return 0;
  145. // Set same line code
  146. FjBitsCopy( pDest, cBitsDest, SAMELINE_CODE, CBITS_SAMELINE_CODE );
  147. cBitsDest += CBITS_SAMELINE_CODE;
  148. // Set same line number
  149. FjBitsCopy( pDest, cBitsDest, dwSameLine << 8, CBITS_SAMELINE_NUM );
  150. cBitsDest += CBITS_SAMELINE_NUM;
  151. // Initial same line number
  152. dwSameLine = 1;
  153. }
  154. // vvv Oct.31,1996 H.Ishida
  155. cBitsDestMark = cBitsDest;
  156. // ^^^ Oct.31,1996 H.Ishida
  157. // Encode
  158. cBitsSrcMax = cBitsSrc + (cSrcX * 8);
  159. // Compress one line image
  160. while ( cBitsSrc < cBitsSrcMax ) {
  161. // Check same pattern
  162. cBitsSrc = SamePatternCheck( pSrc, cBitsSrc, cBitsSrcMax, &ptnInfo );
  163. // there are same patterns
  164. if( ptnInfo.dwPatnNum > 1 ) {
  165. if ( ( cBitsDest + CBITS_SAMEPATN ) > cBitsDestMax)
  166. return 0;
  167. // Set same pattern code
  168. FjBitsCopy(pDest, cBitsDest, SAMEPATN_CODE, CBITS_SAMEPATN_CODE );
  169. cBitsDest += CBITS_SAMEPATN_CODE;
  170. // Set same pattern image
  171. FjBitsCopy(pDest, cBitsDest, ptnInfo.dwPatn << 8, CBITS_SAMEPATN_BYTE );
  172. cBitsDest += CBITS_SAMEPATN_BYTE;
  173. ptnInfo.dwPatnNum <<= 5;
  174. ptnInfo.dwPatnNum |= ptnInfo.dwNextColor;
  175. // Set same pattern number & next run color
  176. FjBitsCopy(pDest, cBitsDest, ptnInfo.dwPatnNum, CBITS_SAMEPATN_NUM );
  177. cBitsDest += CBITS_SAMEPATN_NUM;
  178. // Unknown same pattern on after here
  179. continue;
  180. }
  181. // Next run is white
  182. if( ptnInfo.dwNextColor == NEXT_COLOR_WHITE ) {
  183. // Count white bits
  184. cBitsRun = FjCountBits(pSrc, cBitsSrc, (cBitsSrcMax - cBitsSrc), TRUE);
  185. cBitsSrc += cBitsRun;
  186. // vvv Oct.31,1996 H.Ishida
  187. // reduce data size
  188. if(cBitsSrc >= cBitsSrcMax){
  189. if(cBitsDest > cBitsDestMark)
  190. break;
  191. cBitsRun = 2; // Whole white line is convert to white 2 dots:Minimun MH data.
  192. }
  193. // ^^^ Oct.31,1996 H.Ishida
  194. // Careful, white run length over maximam
  195. while( cBitsRun > RUNLENGTH_MAX ) {
  196. dwCode = WhiteMakeUpTable[MAKEUP_TABLE_MAX - 1].wCode;
  197. cBits = WhiteMakeUpTable[MAKEUP_TABLE_MAX - 1].cBits;
  198. if (cBitsDest + cBits > cBitsDestMax)
  199. return 0;
  200. FjBitsCopy(pDest, cBitsDest, dwCode, cBits);
  201. cBitsDest += cBits;
  202. cBitsRun -= RUNLENGTH_MAX;
  203. }
  204. if (cBitsRun >= 64) {
  205. dwCode = WhiteMakeUpTable[(cBitsRun / TERMINATE_MAX) - 1].wCode;
  206. cBits = WhiteMakeUpTable[(cBitsRun / TERMINATE_MAX) - 1].cBits;
  207. if (cBitsDest + cBits > cBitsDestMax)
  208. return 0;
  209. FjBitsCopy(pDest, cBitsDest, dwCode, cBits);
  210. cBitsDest += cBits;
  211. }
  212. dwCode = WhiteTerminateTable[cBitsRun % TERMINATE_MAX].wCode;
  213. cBits = WhiteTerminateTable[cBitsRun % TERMINATE_MAX].cBits;
  214. if (cBitsDest + cBits > cBitsDestMax)
  215. return 0;
  216. FjBitsCopy(pDest, cBitsDest, dwCode, cBits);
  217. cBitsDest += cBits;
  218. ptnInfo.dwNextColor = NEXT_COLOR_BLACK;
  219. } else {
  220. // Black bits
  221. cBitsRun = FjCountBits(pSrc, cBitsSrc, (cBitsSrcMax - cBitsSrc), FALSE);
  222. cBitsSrc += cBitsRun;
  223. // Careful, black run length over maximam
  224. while( cBitsRun > RUNLENGTH_MAX ) {
  225. dwCode = BlackMakeUpTable[MAKEUP_TABLE_MAX - 1].wCode;
  226. cBits = BlackMakeUpTable[MAKEUP_TABLE_MAX - 1].cBits;
  227. if (cBitsDest + cBits > cBitsDestMax)
  228. return 0;
  229. FjBitsCopy(pDest, cBitsDest, dwCode, cBits);
  230. cBitsDest += cBits;
  231. cBitsRun -= RUNLENGTH_MAX;
  232. }
  233. if (cBitsRun >= 64) {
  234. dwCode = BlackMakeUpTable[(cBitsRun / TERMINATE_MAX) - 1].wCode;
  235. cBits = BlackMakeUpTable[(cBitsRun / TERMINATE_MAX) - 1].cBits;
  236. if (cBitsDest + cBits > cBitsDestMax)
  237. return 0;
  238. FjBitsCopy(pDest, cBitsDest, dwCode, cBits);
  239. cBitsDest += cBits;
  240. }
  241. dwCode = BlackTerminateTable[cBitsRun % TERMINATE_MAX].wCode;
  242. cBits = BlackTerminateTable[cBitsRun % TERMINATE_MAX].cBits;
  243. if (cBitsDest + cBits > cBitsDestMax)
  244. return 0;
  245. FjBitsCopy(pDest, cBitsDest, dwCode, cBits);
  246. cBitsDest += cBits;
  247. ptnInfo.dwNextColor = NEXT_COLOR_WHITE;
  248. }
  249. }
  250. // End of one raster
  251. }
  252. // Last EOL.
  253. if (cBitsDest + CBITS_EOL_CODE > cBitsDestMax)
  254. return 0;
  255. FjBitsCopy(pDest, cBitsDest, EOL_CODE, CBITS_EOL_CODE);
  256. cBitsDest += CBITS_EOL_CODE;
  257. // Pad with 0 until byte boundary
  258. if ((cBits = (8 - (cBitsDest % 8)) % 8) != 0) {
  259. if (cBitsDest + cBits > cBitsDestMax)
  260. return 0;
  261. FjBitsCopy(pDest, cBitsDest, FILL_CODE, cBits);
  262. cBitsDest += cBits;
  263. }
  264. return cBitsDest / 8;
  265. }
  266. // end of FUMH2.c