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.

389 lines
16 KiB

  1. #include <windows.h>
  2. #include "compress.h"
  3. /*********************************************************/
  4. /* RL_ECmd : main function */
  5. /* ARGS : LPBYTE - pointer to image */
  6. /* LPBYTE - pointer to BRL code */
  7. /* WORD - size of image */
  8. /* RET : WORD - size of BRL Code */
  9. /* 0 - COMPRESSION FAILED */
  10. /*********************************************************/
  11. WORD FAR PASCAL RL_ECmd(LPBYTE iptr, LPBYTE cptr, WORD isize)
  12. {
  13. if (RL_Init(iptr, cptr, isize)==VALID)
  14. RL_Enc();
  15. if (BUF_OVERFLOW)
  16. return 0;
  17. else
  18. return RL_CodeSize;
  19. }
  20. /*********************************************************/
  21. /* RL_Init : Initializer */
  22. /* ARGS : BYTE * - pointer to image */
  23. /* BYTE * - pointer to BRL code */
  24. /* WORD - size of image */
  25. /* RET : BYTE - VALID or INVALID */
  26. /*********************************************************/
  27. BYTE FAR PASCAL RL_Init(LPBYTE iptr, LPBYTE cptr, WORD isize)
  28. {
  29. RL_ImagePtr = iptr;
  30. RL_CodePtr = cptr;
  31. RL_ImageSize = isize;
  32. BUF_OVERFLOW = 0;
  33. RL_BufEnd = cptr + CODBUFSZ;
  34. return VALID;
  35. }
  36. /*********************************************************/
  37. /* RL_Enc : Encoder */
  38. /* ARGS : void */
  39. /* RET : char COMP_SUCC or COMP_FAIL */
  40. /*********************************************************/
  41. char FAR PASCAL RL_Enc(void)
  42. {
  43. int repcnt;
  44. BYTE refbyt;
  45. WORD i;
  46. i = 0;
  47. repcnt = 0;
  48. RL_CodeSize = 0;
  49. refbyt = RL_ImagePtr[0];
  50. //@CC 12.22.94 for (i=1;i<=RL_ImageSize; i++)
  51. //@TO 12/01/1995 for (i=1;i<RL_ImageSize;i++)
  52. for (i=1;i<=RL_ImageSize;i++)
  53. {
  54. if ((RL_ImagePtr[i] == refbyt)&&(repcnt<255)&&(i!=RL_ImageSize-1))
  55. repcnt++;
  56. else
  57. {
  58. //-> @CC 12.22.94
  59. if ((RL_ImagePtr[i] == refbyt)&&(repcnt<255))
  60. repcnt++;
  61. //<- @CC 12.22.94
  62. if (RL_CodePtr > RL_BufEnd)
  63. {BUF_OVERFLOW = 1; return COMP_FAIL;}
  64. RL_CodePtr[0] = repcnt;
  65. RL_CodePtr[1] = refbyt;
  66. RL_CodePtr += 2;
  67. RL_CodeSize += 2;
  68. refbyt = RL_ImagePtr[i];
  69. repcnt = 0;
  70. }
  71. }
  72. return COMP_SUCC;
  73. }
  74. /***********************************************************************/
  75. /* APTi-Philippines, Inc. RL4 Compression Routine */
  76. /* �@���́@: RL4_ECmd */
  77. /* �@�\�@: main entry point */
  78. /* �����@: code size = RL4_ECmd(iptr, cptr, isz, iwd, iht) */
  79. /* ���́@: LPBYTE - pointer to image */
  80. /* LPBYTE - pointer to code */
  81. /* WORD - size of image IN BYTES !!! */
  82. /* WORD - image width */
  83. /* WORD - height of image */
  84. /* �o�́@: WORD - size of RL4 code */
  85. /* 0 - COMPRESSION FAILED */
  86. /* ���L�@: */
  87. /* �����@: 1993. 1.27 initial */
  88. /***********************************************************************/
  89. WORD FAR PASCAL RL4_ECmd(LPBYTE iptr, LPBYTE cptr, WORD sz, WORD wd, WORD ht)
  90. {
  91. char status;
  92. if (RL4_ChkParms( iptr, cptr, sz, wd, ht ) == VALID)
  93. if ((status=RL4_Enc())==COMP_FAIL)
  94. return 0;
  95. else
  96. return RL4_CodeSize;
  97. }
  98. /***********************************************************************/
  99. /* APTi-Philippines, Inc. RL4 Compression Routine */
  100. /* �@���́@: RL4_ChkParms */
  101. /* �@�\�@: checks input parameters if valid and sets internal */
  102. /* variables if they are */
  103. /* �����@: ret val = RL4_ChkParms(iptr, cptr, isz, iwd, iht) */
  104. /* ���́@: LPBYTE - pointer to image */
  105. /* LPBYTE - pointer to code */
  106. /* WORD - size of image */
  107. /* WORD - image width IN BYTES !!! */
  108. /* WORD - height of image */
  109. /* �o�́@: BYTE - VALID or INVALID */
  110. /* ���L�@: */
  111. /* �����@: 1994. 1.25 initial */
  112. /* 1994. 2.12 clean up */
  113. /***********************************************************************/
  114. BYTE FAR PASCAL RL4_ChkParms(LPBYTE iptr, LPBYTE cptr, WORD isz, WORD iwd, WORD iht)
  115. {
  116. if ((isz > RL4_MAXISIZE)||
  117. ((iht != isz/iwd+1)&&(iht != isz/iwd))||
  118. (iht <= 0)||
  119. (iht > RL4_MAXHEIGHT)||
  120. (iwd <= 0)||
  121. (iwd > RL4_MAXWIDTH))
  122. return INVALID;
  123. else
  124. {
  125. RL4_ImagePtr = iptr;
  126. RL4_CodePtr = cptr;
  127. RL4_IHeight = iht;
  128. RL4_IWidth = iwd;
  129. RL4_ISize = isz;
  130. RL4_BufEnd = cptr + CODBUFSZ; /* please define CODBUFSZ in header */
  131. /* file COMPRESS.H */
  132. BUF_OVERFLOW = 0;
  133. return VALID;
  134. }
  135. }
  136. /***********************************************************************/
  137. /* APTi-Philippines, Inc. RL4 Compression Routine */
  138. /* �@���́@: RL4_Enc */
  139. /* �@�\�@: encodes image to RL4 code */
  140. /* �����@: RL4_Enc() */
  141. /* ���́@: void */
  142. /* �o�́@: void */
  143. /* ���L�@: */
  144. /* �����@: 1993. 1.25 initial */
  145. /* 1994. 2.12 added RL4_ConvLast */
  146. /***********************************************************************/
  147. char FAR PASCAL RL4_Enc(void) {
  148. LPBYTE rowptr, codeptr;
  149. WORD rownum, lrlen;
  150. long isize, diff;
  151. RL4_Nibble = RL4_FIRST;
  152. rowptr = RL4_ImagePtr;
  153. RL4_CodeSize = 0;
  154. isize = 0;
  155. lrlen = RL4_ISize%RL4_IWidth;
  156. if (lrlen == 0)
  157. lrlen = RL4_IWidth;
  158. diff = RL4_ISize-RL4_IWidth;
  159. for ( rownum = 0; rownum < RL4_IHeight; rownum++ )
  160. {
  161. codeptr = RL4_CodePtr;
  162. if (isize < diff)
  163. {
  164. RL4_ConvRow(rowptr);
  165. if (BUF_OVERFLOW) return COMP_FAIL;
  166. }
  167. else
  168. {
  169. RL4_ConvLast(rowptr,lrlen);
  170. if (BUF_OVERFLOW) return COMP_FAIL;
  171. }
  172. if (RL4_RowAttrib == RL4_DIRTY) /* Encode only rows with '1' bits */
  173. RL4_EncRow(codeptr, rownum);
  174. else
  175. RL4_CodePtr -= 4;
  176. rowptr += RL4_IWidth;
  177. isize += RL4_IWidth;
  178. }
  179. return COMP_SUCC;
  180. }
  181. /***********************************************************************/
  182. /* APTi-Philippines, Inc. RL4 Compression Routine */
  183. /* �@���́@: RL4_ConvRow */
  184. /* �@�\�@: translates runs in a row to RL4 code */
  185. /* �����@: RL4_ConvRow(rowptr) */
  186. /* ���́@: LPBYTE - pointer to a row of image */
  187. /* �o�́@: void */
  188. /* ���L�@: excluding last row */
  189. /* �����@: 1993. 1.25 initial */
  190. /***********************************************************************/
  191. char FAR PASCAL RL4_ConvRow(LPBYTE rowptr){
  192. WORD bytenum;
  193. RL4_CurrRL = 0;
  194. RL4_CurrColor = RL4_WHITE;
  195. RL4_Status = RL4_BYTE;
  196. RL4_CodePtr += 4;
  197. RL4_NblCnt = 0;
  198. for( bytenum = 0; bytenum < RL4_IWidth; bytenum++ )
  199. {
  200. RL4_ByteProc(rowptr[bytenum]);
  201. if (BUF_OVERFLOW) return COMP_FAIL;
  202. }
  203. if ((RL4_CurrColor == RL4_WHITE)&&(RL4_CurrRL==RL4_IWidth*8)) /* all '0' bits */
  204. RL4_RowAttrib = RL4_CLEAN;
  205. else
  206. RL4_RowAttrib = RL4_DIRTY;
  207. return COMP_SUCC;
  208. }
  209. /***********************************************************************/
  210. /* APTi-Philippines, Inc. RL4 Compression Routine */
  211. /* �@���́@: RL4_ConvLast */
  212. /* �@�\�@: translates runs in a row to RL4 code */
  213. /* �����@: RL4_ConvLast(rowptr, lrlen) */
  214. /* ���́@: LPBYTE - pointer to a row of image */
  215. /* WORD - length of last row */
  216. /* �o�́@: void */
  217. /* ���L�@: for image data whose last row is not exactly */
  218. /* RL4_Width bytes wide */
  219. /* �����@: 1993. 2.12 added to eliminate extra bytes in code */
  220. /* 1994. 6.08 made attrib always dirty so that last row */
  221. /* is always encoded */
  222. /***********************************************************************/
  223. char FAR PASCAL RL4_ConvLast(LPBYTE rowptr, WORD lrlen){
  224. WORD bytenum;
  225. RL4_CurrRL = 0;
  226. RL4_CurrColor = RL4_WHITE;
  227. RL4_Status = RL4_BYTE;
  228. RL4_CodePtr += 4;
  229. RL4_NblCnt = 0;
  230. RL4_RowAttrib = RL4_DIRTY;
  231. for( bytenum = 0; bytenum < lrlen; bytenum++ )
  232. {
  233. RL4_ByteProc(rowptr[bytenum]);
  234. if (BUF_OVERFLOW) return COMP_FAIL;
  235. }
  236. return COMP_SUCC;
  237. }
  238. /***********************************************************************/
  239. /* APTi-Philippines, Inc. RL4 Compression Routine */
  240. /* �@���́@: RL4_ByteProc */
  241. /* �@�\�@: processes one byte of image data */
  242. /* �����@: RL4_ByteProc(ibyte) */
  243. /* ���́@: BYTE - one byte of image data */
  244. /* �o�́@: void */
  245. /* ���L�@: */
  246. /* �����@: 1993. 1.25 initial */
  247. /***********************************************************************/
  248. char FAR PASCAL RL4_ByteProc (BYTE ibyte)
  249. {
  250. BYTE mask; /* mask bits from '1000 0000' to '0000 0001' */
  251. int i;
  252. mask = 0x80;
  253. for ( i = 0; i < 8; i++ )
  254. { if (ibyte & mask) /* Next bit is black */
  255. { if (RL4_CurrColor==RL4_WHITE)
  256. { RL4_CurrColor = RL4_BLACK;
  257. RL4_NblCnt += RL4_TransRun(RL4_CurrRL);
  258. if (BUF_OVERFLOW) return COMP_FAIL;
  259. if (RL4_NblCnt & 1) /* Check if even or odd # of nibbles */
  260. RL4_Status = RL4_NONBYTE;
  261. else
  262. RL4_Status = RL4_BYTE;
  263. RL4_CurrRL = 1;
  264. }
  265. else if (RL4_CurrColor==RL4_BLACK)
  266. RL4_CurrRL++;
  267. }
  268. else /* Next bit is white */
  269. { if (RL4_CurrColor==RL4_BLACK)
  270. { RL4_CurrColor = RL4_WHITE;
  271. RL4_NblCnt += RL4_TransRun (RL4_CurrRL);
  272. if (BUF_OVERFLOW) return COMP_FAIL;
  273. if (RL4_NblCnt & 1) /* Check if even or odd # of nibbles */
  274. RL4_Status = RL4_NONBYTE;
  275. else
  276. RL4_Status = RL4_BYTE;
  277. RL4_CurrRL = 1;
  278. }
  279. else if (RL4_CurrColor==RL4_WHITE)
  280. RL4_CurrRL++;
  281. }
  282. mask >>= 1;
  283. }
  284. return COMP_SUCC;
  285. }
  286. /***********************************************************************/
  287. /* APTi-Philippines, Inc. RL4 Compression Routine */
  288. /* �@���́@: RL4_EncRow */
  289. /* �@�\�@: encodes row number, code length, and End-of-Row code */
  290. /* �����@: RL4_EncRow(codeptr, rownum) */
  291. /* ���́@: LPBYTE - pointer to RL4 code */
  292. /* WORD - no. of row being encoded */
  293. /* �o�́@: void */
  294. /* ���L�@: */
  295. /* �����@: 1993. 1.25 initial */
  296. /***********************************************************************/
  297. char FAR PASCAL RL4_EncRow(LPBYTE codeptr, WORD rownum){
  298. short codelen;
  299. codelen = 0;
  300. *codeptr = (BYTE) (rownum >>8);
  301. codeptr[1] = (BYTE) rownum;
  302. if(RL4_Status==RL4_BYTE){
  303. RL4_NblCnt += 2;
  304. RL4_PutNbl(0xFFl,2); /* Append 0xFF to code */
  305. if (BUF_OVERFLOW) return COMP_FAIL;
  306. } else {
  307. RL4_NblCnt += 3;
  308. RL4_PutNbl(0xFFFl,3); /* Append 0xFFF to code */
  309. if (BUF_OVERFLOW) return COMP_FAIL;
  310. }
  311. codelen = RL4_NblCnt/2;
  312. codeptr[2] = (BYTE) (codelen >>8);
  313. codeptr[3] = (BYTE) codelen;
  314. RL4_CodeSize += codelen + 4;
  315. return COMP_SUCC;
  316. }
  317. /***********************************************************************/
  318. /* APTi-Philippines, Inc. RL4 Compression Routine */
  319. /* �@���́@: RL4_TransRun */
  320. /* �@�\�@: translates run length value to RL4 code */
  321. /* �����@: nibble count = RL4_TransRun(rlval) */
  322. /* ���́@: short - run length value to be encoded */
  323. /* �o�́@: short - no. of nibbles appended */
  324. /* ���L�@: */
  325. /* �����@: 1993. 1.25 initial */
  326. /***********************************************************************/
  327. WORD FAR PASCAL RL4_TransRun(WORD rlval){
  328. WORD nblcnt;
  329. long lval;
  330. if (rlval == 0) { lval = 0xFEl;
  331. nblcnt = 2; }
  332. else if (rlval<=8) { lval = (long) (rlval)-1l;
  333. nblcnt = 1; }
  334. else if (rlval<=72) {lval = (long) (rlval)+119l;
  335. nblcnt = 2; }
  336. else if (rlval<=584) { lval = (long) (rlval)+2999l;
  337. nblcnt = 3; }
  338. else if (rlval<=4680) { lval = (long) (rlval)+56759l;
  339. nblcnt = 4; }
  340. else if (rlval<=32767) { lval = (long) (rlval)+978359l;
  341. nblcnt = 5; }
  342. /* PUTNBL(lval, nblcnt)*/
  343. RL4_PutNbl(lval, nblcnt);
  344. if (BUF_OVERFLOW) return COMP_FAIL;
  345. return nblcnt;
  346. }
  347. char RL4_PutNbl(long lval2, short nblcnt2) {
  348. {
  349. short i;
  350. for (i=nblcnt2 ; i>0; i--)
  351. { if (RL4_Nibble==RL4_FIRST)
  352. { RL4_Nibble = RL4_SECOND;
  353. *RL4_CodePtr = (BYTE) (lval2 >> ((i-1)*4)) << 4 ;
  354. } else
  355. { RL4_Nibble = RL4_FIRST;
  356. *RL4_CodePtr |= (BYTE) (lval2 >> ((i-1)*4)) ;
  357. RL4_CodePtr++;
  358. if (RL4_CodePtr>RL4_BufEnd)
  359. {BUF_OVERFLOW = 1; return COMP_FAIL;}
  360. }
  361. }
  362. return COMP_SUCC;
  363. }
  364. }