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.

2622 lines
131 KiB

  1. /****************************************************************************/
  2. /* abcapi.cpp */
  3. /* */
  4. /* Bitmap Compressor API functions. */
  5. /* */
  6. /* Copyright(c) Microsoft, PictureTel 1992-1997 */
  7. /* Copyright(c) Microsoft 1997-1999 */
  8. /****************************************************************************/
  9. #ifdef DLL_DISP
  10. #include <adcg.h>
  11. #include <adcs.h>
  12. #include <abcapi.h>
  13. #include <abcdata.c>
  14. #define _pShm pddShm
  15. #else
  16. #include <as_conf.hpp>
  17. #define _pShm m_pShm
  18. #endif
  19. #ifdef COMP_STATS
  20. /****************************************************************************/
  21. /* Define some globals for storing useful stats data. */
  22. /****************************************************************************/
  23. UINT32 ulPreCompData = 0;
  24. UINT32 ulTotalCompTime = 0;
  25. UINT32 ulCompRate = 0;
  26. #endif
  27. #ifdef DC_DEBUG
  28. // compression testing
  29. #include <abdapi.h>
  30. #endif
  31. #ifdef Unused
  32. // Restore this instead of macro if data added to abcdata.c
  33. /****************************************************************************/
  34. /* API FUNCTION: BC_Init */
  35. /* */
  36. /* Initializes the Bitmap Compressor. */
  37. /****************************************************************************/
  38. void RDPCALL SHCLASS BC_Init(void)
  39. {
  40. DC_BEGIN_FN("BC_Init");
  41. #define DC_INIT_DATA
  42. #include <abcdata.c>
  43. #undef DC_INIT_DATA
  44. DC_END_FN();
  45. }
  46. #endif
  47. /****************************************************************************/
  48. /* API FUNCTION: BC_CompressBitmap */
  49. /* */
  50. /* Compresses the supplied bitmap into the supplied memory buffer. */
  51. /* */
  52. /* PARAMETERS: */
  53. /* */
  54. /* pSrcBitmap - a pointer to the source bitmap data bits. */
  55. /* */
  56. /* pDstBuffer - a pointer to the destination memory buffer (where the */
  57. /* compressed data will be written). */
  58. /* */
  59. /* dstBufferSize - the size in bytes of the destination buffer */
  60. /* */
  61. /* pCompressedDataSize - pointer to variable that receives the compressed */
  62. /* data size */
  63. /* */
  64. /* bitmapWidth - width of the src bitmap in pels, should be divisible by 4. */
  65. /* */
  66. /* bitmapHeight - the height of the source bitmap in pels. */
  67. /* */
  68. /* RETURNS: */
  69. /* */
  70. /* TRUE - the bitmap data was successfully compressed. */
  71. /* *pCompressedDataSize is updated */
  72. /* */
  73. /* FALSE - the bitmap data could not be compressed. */
  74. /****************************************************************************/
  75. #ifdef DC_HICOLOR
  76. BOOL RDPCALL SHCLASS BC_CompressBitmap(
  77. PBYTE pSrcBitmap,
  78. PBYTE pDstBuffer,
  79. PBYTE pBCWorkingBuffer,
  80. unsigned dstBufferSize,
  81. unsigned *pCompressedDataSize,
  82. unsigned bitmapWidth,
  83. unsigned bitmapHeight,
  84. unsigned bpp)
  85. #else
  86. BOOL RDPCALL SHCLASS BC_CompressBitmap(
  87. PBYTE pSrcBitmap,
  88. PBYTE pDstBuffer,
  89. unsigned dstBufferSize,
  90. unsigned *pCompressedDataSize,
  91. unsigned bitmapWidth,
  92. unsigned bitmapHeight)
  93. #endif
  94. {
  95. BOOL rc;
  96. PTS_CD_HEADER_UA pCompDataHeader;
  97. unsigned cbUncompressedDataSize;
  98. unsigned cbCompMainBodySize;
  99. #ifdef COMP_STATS
  100. UINT32 ulStartCompTime;
  101. UINT32 ulEndCompTime;
  102. #endif
  103. DC_BEGIN_FN("BC_CompressBitmap");
  104. #ifdef COMP_STATS
  105. /************************************************************************/
  106. /* Record the start time. */
  107. /************************************************************************/
  108. COM_GETTICKCOUNT(ulStartCompTime);
  109. #endif
  110. TRC_ASSERT(((bitmapWidth & 3) == 0),(TB,"Width not divisible by 4"));
  111. TRC_ASSERT((dstBufferSize > 0),(TB,"No destination space!"));
  112. // Trace the important parameters.
  113. TRC_DBG((TB, "pSrc(%p) pDst(%p) dstBufferSize(%#x)",
  114. pSrcBitmap, pDstBuffer, dstBufferSize));
  115. TRC_DBG((TB, "width(%u) height(%u)", bitmapWidth, bitmapHeight));
  116. // Calculate the size of the uncompressed src data. Make sure it
  117. // is within our allowed size.
  118. #ifdef DC_HICOLOR
  119. cbUncompressedDataSize = bitmapWidth * bitmapHeight * ((bpp + 7) / 8);
  120. #else
  121. cbUncompressedDataSize = bitmapWidth * bitmapHeight;
  122. #endif
  123. TRC_ASSERT((cbUncompressedDataSize <= MAX_UNCOMPRESSED_DATA_SIZE || pBCWorkingBuffer),
  124. (TB,"Bitmap size > max: size=%u, max=%u",
  125. cbUncompressedDataSize, MAX_UNCOMPRESSED_DATA_SIZE));
  126. // Do we send the bitmap compression header?
  127. if (_pShm->bc.noBitmapCompressionHdr)
  128. {
  129. #ifdef DC_HICOLOR
  130. switch (bpp)
  131. {
  132. case 32:
  133. {
  134. TRC_DBG((TB, "Compress 32 bpp"));
  135. cbCompMainBodySize = CompressV2Int32(pSrcBitmap,
  136. pDstBuffer,
  137. cbUncompressedDataSize,
  138. bitmapWidth * 4,
  139. dstBufferSize,
  140. pBCWorkingBuffer ? pBCWorkingBuffer :
  141. _pShm->bc.xor_buffer,
  142. _pShm->bc.match);
  143. }
  144. break;
  145. case 24:
  146. {
  147. TRC_DBG((TB, "Compress 24 bpp"));
  148. cbCompMainBodySize = CompressV2Int24(pSrcBitmap,
  149. pDstBuffer,
  150. cbUncompressedDataSize,
  151. bitmapWidth * 3,
  152. dstBufferSize,
  153. pBCWorkingBuffer ? pBCWorkingBuffer :
  154. _pShm->bc.xor_buffer,
  155. _pShm->bc.match);
  156. }
  157. break;
  158. case 16:
  159. {
  160. TRC_DBG((TB, "Compress 16bpp"));
  161. cbCompMainBodySize = CompressV2Int16(pSrcBitmap,
  162. pDstBuffer,
  163. cbUncompressedDataSize,
  164. bitmapWidth * 2,
  165. dstBufferSize,
  166. pBCWorkingBuffer ? pBCWorkingBuffer :
  167. _pShm->bc.xor_buffer,
  168. _pShm->bc.match);
  169. }
  170. break;
  171. case 15:
  172. {
  173. TRC_DBG((TB, "Compress 15bpp"));
  174. cbCompMainBodySize = CompressV2Int15(pSrcBitmap,
  175. pDstBuffer,
  176. cbUncompressedDataSize,
  177. bitmapWidth * 2,
  178. dstBufferSize,
  179. pBCWorkingBuffer ? pBCWorkingBuffer :
  180. _pShm->bc.xor_buffer,
  181. _pShm->bc.match);
  182. }
  183. break;
  184. case 8:
  185. default:
  186. {
  187. TRC_DBG((TB, "Compress 8bpp"));
  188. cbCompMainBodySize = CompressV2Int(pSrcBitmap,
  189. pDstBuffer,
  190. cbUncompressedDataSize,
  191. bitmapWidth,
  192. dstBufferSize,
  193. pBCWorkingBuffer ? pBCWorkingBuffer :
  194. _pShm->bc.xor_buffer);
  195. }
  196. break;
  197. }
  198. #else
  199. cbCompMainBodySize = CompressV2Int(pSrcBitmap,
  200. pDstBuffer,
  201. cbUncompressedDataSize,
  202. bitmapWidth,
  203. dstBufferSize,
  204. _pShm->bc.xor_buffer);
  205. #endif
  206. if (cbCompMainBodySize != 0) {
  207. // Write back the new (compressed) packet size.
  208. *pCompressedDataSize = cbCompMainBodySize;
  209. TRC_DBG((TB, "*pCompressedDataSize(%u)",
  210. *pCompressedDataSize));
  211. rc = TRUE;
  212. }
  213. else {
  214. TRC_NRM((TB, "Failed to compress main body"));
  215. rc = FALSE;
  216. }
  217. }
  218. else {
  219. if (dstBufferSize > sizeof(TS_CD_HEADER)) {
  220. // Compress the bitmap data.
  221. #ifdef DC_HICOLOR
  222. switch (bpp)
  223. {
  224. case 32:
  225. {
  226. TRC_DBG((TB, "Compress 32 bpp"));
  227. cbCompMainBodySize = CompressV2Int32(pSrcBitmap,
  228. pDstBuffer + sizeof(TS_CD_HEADER),
  229. cbUncompressedDataSize,
  230. bitmapWidth * 4,
  231. dstBufferSize - sizeof(TS_CD_HEADER),
  232. pBCWorkingBuffer ? pBCWorkingBuffer :
  233. _pShm->bc.xor_buffer,
  234. _pShm->bc.match);
  235. }
  236. break;
  237. case 24:
  238. {
  239. TRC_DBG((TB, "Compress 24 bpp"));
  240. cbCompMainBodySize = CompressV2Int24(pSrcBitmap,
  241. pDstBuffer + sizeof(TS_CD_HEADER),
  242. cbUncompressedDataSize,
  243. bitmapWidth * 3,
  244. dstBufferSize - sizeof(TS_CD_HEADER),
  245. pBCWorkingBuffer ? pBCWorkingBuffer :
  246. _pShm->bc.xor_buffer,
  247. _pShm->bc.match);
  248. }
  249. break;
  250. case 16:
  251. {
  252. TRC_DBG((TB, "Compress 16bpp"));
  253. cbCompMainBodySize = CompressV2Int16(pSrcBitmap,
  254. pDstBuffer + sizeof(TS_CD_HEADER),
  255. cbUncompressedDataSize,
  256. bitmapWidth * 2,
  257. dstBufferSize - sizeof(TS_CD_HEADER),
  258. pBCWorkingBuffer ? pBCWorkingBuffer :
  259. _pShm->bc.xor_buffer,
  260. _pShm->bc.match);
  261. }
  262. break;
  263. case 15:
  264. {
  265. TRC_DBG((TB, "Compress 15bpp"));
  266. cbCompMainBodySize = CompressV2Int15(pSrcBitmap,
  267. pDstBuffer + sizeof(TS_CD_HEADER),
  268. cbUncompressedDataSize,
  269. bitmapWidth * 2,
  270. dstBufferSize - sizeof(TS_CD_HEADER),
  271. pBCWorkingBuffer ? pBCWorkingBuffer :
  272. _pShm->bc.xor_buffer,
  273. _pShm->bc.match);
  274. }
  275. break;
  276. case 8:
  277. default:
  278. {
  279. TRC_DBG((TB, "Compress 8bpp"));
  280. cbCompMainBodySize = CompressV2Int(pSrcBitmap,
  281. pDstBuffer + sizeof(TS_CD_HEADER),
  282. cbUncompressedDataSize,
  283. bitmapWidth,
  284. dstBufferSize - sizeof(TS_CD_HEADER),
  285. pBCWorkingBuffer ? pBCWorkingBuffer :
  286. _pShm->bc.xor_buffer);
  287. }
  288. break;
  289. }
  290. #else
  291. cbCompMainBodySize = CompressV2Int(pSrcBitmap,
  292. pDstBuffer + sizeof(TS_CD_HEADER),
  293. cbUncompressedDataSize,
  294. bitmapWidth,
  295. dstBufferSize - sizeof(TS_CD_HEADER),
  296. _pShm->bc.xor_buffer);
  297. #endif
  298. if (cbCompMainBodySize != 0) {
  299. // Fill in the compressed data header.
  300. // FirstRowSize is 0 by historical convention.
  301. pCompDataHeader = (PTS_CD_HEADER_UA)pDstBuffer;
  302. pCompDataHeader->cbCompFirstRowSize = 0;
  303. pCompDataHeader->cbCompMainBodySize =
  304. (UINT16)cbCompMainBodySize;
  305. if (bpp > 8) {
  306. pCompDataHeader->cbScanWidth = TS_BYTES_IN_SCANLINE(bitmapWidth, bpp);
  307. }
  308. else {
  309. pCompDataHeader->cbScanWidth = (UINT16)bitmapWidth;
  310. }
  311. pCompDataHeader->cbUncompressedSize =
  312. (UINT16)cbUncompressedDataSize;
  313. // Write back the new (compressed) packet size.
  314. *pCompressedDataSize = sizeof(TS_CD_HEADER) +
  315. cbCompMainBodySize;
  316. TRC_DBG((TB, "*pCompressedDataSize(%u)",
  317. *pCompressedDataSize));
  318. rc = TRUE;
  319. }
  320. else {
  321. TRC_NRM((TB, "Failed to compress main body"));
  322. rc = FALSE;
  323. }
  324. }
  325. else {
  326. TRC_NRM((TB, "Not enough buffer space for header: %u",
  327. dstBufferSize));
  328. rc = FALSE;
  329. }
  330. }
  331. #if 0
  332. /************************************************************************/
  333. /* Check that the compressed output decompresses to the same thing */
  334. /************************************************************************/
  335. if (cbCompMainBodySize)
  336. {
  337. HRESULT hr;
  338. hr = BD_DecompressBitmap(
  339. #ifndef DLL_DISP
  340. m_pTSWd,
  341. #endif
  342. pDstBuffer + (_pShm->bc.noBitmapCompressionHdr ? 0 : 8),
  343. _pShm->bc.decompBuffer,
  344. cbCompMainBodySize,
  345. TRUE,
  346. (BYTE)bpp,
  347. (UINT16)bitmapWidth,
  348. (UINT16)bitmapHeight);
  349. if (FAILED(hr) || memcmp(pSrcBitmap, _pShm->bc.decompBuffer,cbUncompressedDataSize))
  350. {
  351. // TRC_ASSERT(FALSE, (TB, "Decompression failure"));
  352. }
  353. }
  354. #endif
  355. #ifdef COMP_STATS
  356. /************************************************************************/
  357. /* Work out how long the compression took, in ms. */
  358. /************************************************************************/
  359. COM_GETTICKCOUNT(ulEndCompTime);
  360. ulTotalCompTime += (ulEndCompTime - ulStartCompTime) / 10000;
  361. if (ulTotalCompTime != 0)
  362. ulCompRate = ulPreCompData / ulTotalCompTime;
  363. #endif
  364. DC_END_FN();
  365. return rc;
  366. }
  367. /****************************************************************************/
  368. /* Bitmap Compression core code. */
  369. /* */
  370. /* A cunning multidimensional RLE compression scheme, particularly suitable */
  371. /* for compressing bitmaps containing captured images of Windows */
  372. /* applications. For images which use lots of different colors intermixed */
  373. /* (full-color pictures, etc.) this compression sceme will be inefficient. */
  374. /* */
  375. /* These functions and macros encode a bitmap according to the codes */
  376. /* defined in abcapi.h. Although there are some complexities in the */
  377. /* encoding, the encodings should be self-explanatory. abcapi.h describes */
  378. /* some nuances of the encoding scheme. */
  379. /****************************************************************************/
  380. /****************************************************************************/
  381. /* Utility macros for encoding orders */
  382. /****************************************************************************/
  383. /****************************************************************************/
  384. /* Encode an order for a standard run */
  385. /****************************************************************************/
  386. #define ENCODE_ORDER_MEGA(buffer, \
  387. order_code, \
  388. length, \
  389. mega_order_code, \
  390. DEF_LENGTH_ORDER, \
  391. DEF_LENGTH_LONG_ORDER) \
  392. if (length <= DEF_LENGTH_ORDER) { \
  393. *buffer++ = (BYTE)((BYTE)order_code | (BYTE)length); \
  394. } \
  395. else if (length <= DEF_LENGTH_LONG_ORDER) { \
  396. *buffer++ = (BYTE)order_code; \
  397. *buffer++ = (BYTE)(length - DEF_LENGTH_ORDER - 1); \
  398. } \
  399. else { \
  400. *buffer++ = (BYTE)mega_order_code; \
  401. *(PUINT16_UA)(buffer) = (UINT16)length; \
  402. buffer += 2; \
  403. }
  404. /****************************************************************************/
  405. /* Encode a special FGBG image */
  406. /****************************************************************************/
  407. #define ENCODE_ORDER_MEGA_FGBG(buffer, \
  408. order_code, \
  409. length, \
  410. mega_order_code, \
  411. DEF_LENGTH_ORDER, \
  412. DEF_LENGTH_LONG_ORDER) \
  413. if ((length & 0x0007) == 0 && length <= DEF_LENGTH_ORDER) { \
  414. *buffer++ = (BYTE)((BYTE)order_code | (BYTE)(length / 8)); \
  415. } \
  416. else if (length <= DEF_LENGTH_LONG_ORDER) { \
  417. *buffer++ = (BYTE)order_code; \
  418. *buffer++ = (BYTE)(length-1); \
  419. } \
  420. else { \
  421. *buffer++ = (BYTE)mega_order_code; \
  422. *(PUINT16_UA)(buffer) = (UINT16)length; \
  423. buffer += 2; \
  424. }
  425. /****************************************************************************/
  426. /* RunSingle */
  427. /* */
  428. /* Determine the length of the current run */
  429. /* */
  430. /* RunSingle may only be called if the buffer has at least four */
  431. /* consecutive identical bytes from the start position */
  432. /****************************************************************************/
  433. #define RUNSINGLE(buffer, length, result) \
  434. { \
  435. BYTE *buf = buffer + 4; \
  436. BYTE *endbuf = buffer + length - 4; \
  437. while (buf < endbuf && \
  438. (*(PUINT32_UA)(buf) == *(PUINT32_UA)(buf - 4))) \
  439. buf += 4; \
  440. endbuf += 4; \
  441. while (buf < endbuf && *buf == *(buf - 1)) \
  442. buf++; \
  443. result = (unsigned)(buf - (buffer)); \
  444. }
  445. /****************************************************************************/
  446. // RunDouble
  447. //
  448. // Determine the length of the current run of dithered bytes. Assumes that
  449. // the dither pattern resides in the first 2 bytes of buffer.
  450. /****************************************************************************/
  451. #define RunDouble(buffer, length, result) \
  452. { \
  453. int len = ((int)length) - 2; \
  454. BYTE *buf = (buffer) + 2; \
  455. UINT16 Pattern = *(PUINT16_UA)(buffer); \
  456. result = 2; \
  457. while (len > 1) { \
  458. if (*(PUINT16_UA)buf != Pattern) \
  459. break; \
  460. buf += 2; \
  461. result += 2; \
  462. len -= 2; \
  463. } \
  464. }
  465. /****************************************************************************/
  466. // RUNFGBG
  467. //
  468. // Determine the length of the run of bytes that consist only of black (0x00)
  469. // or a single FG color. We exit the loop when
  470. // - the next character is not a fg or bg color
  471. // - we hit a run of 24 of the FG or BG color
  472. // Example compression calculations:
  473. // Lookahead KBytes* Comp CPU ("hits")
  474. // 24 54846 148497
  475. // 20 54885 151827
  476. // 16 54967 156809
  477. // * = KBytes server->client WinBench98 Graphics WinMark minus CorelDRAW,
  478. // measured in NetMon on Ethernet.
  479. /****************************************************************************/
  480. #define RUNFGBG(buffer, length, result, work) \
  481. { \
  482. BYTE *buf = buffer; \
  483. BYTE *endbuf = buffer + length; \
  484. result = 0; \
  485. work = *buf; \
  486. while (TRUE) { \
  487. buf++; \
  488. result++; \
  489. if (buf < endbuf) { \
  490. if (*buf != work && *buf != 0) \
  491. break; \
  492. \
  493. if ((result & 0x0007) == 0) { \
  494. if ((*buf == *(buf + 1)) && \
  495. (*(PUINT16_UA)(buf) == *(PUINT16_UA)(buf + 2)) && \
  496. (*(PUINT32_UA)(buf) == *(PUINT32_UA)(buf + 4)) && \
  497. (*(PUINT32_UA)(buf) == *(PUINT32_UA)(buf + 8)) && \
  498. (*(PUINT32_UA)(buf) == *(PUINT32_UA)(buf + 12)) && \
  499. (*(PUINT32_UA)(buf) == *(PUINT32_UA)(buf + 16)) && \
  500. (*(PUINT32_UA)(buf) == *(PUINT32_UA)(buf + 20))) \
  501. { \
  502. break; \
  503. } \
  504. } \
  505. } \
  506. else { \
  507. break; \
  508. } \
  509. } \
  510. }
  511. /****************************************************************************/
  512. // Determine whether a run is better than any previous run.
  513. // For efficiency we take the run if over a threshold. Threshold comparisons:
  514. // Threshold KBytes* Comp CPU ("hits")
  515. // 32 54846 148497
  516. // 28 54817 145085
  517. // 24 54825 144366
  518. // 20 54852 143662
  519. // 16 54858 146343
  520. // * = KBytes server->client WinBench98 Graphics WinMark minus CorelDRAW,
  521. // measured in NetMon on Ethernet.
  522. /****************************************************************************/
  523. #define CHECK_BEST_RUN(run_type, run_length, bestrun_length, bestrun_type) \
  524. if (run_length > bestrun_length) { \
  525. bestrun_length = run_length; \
  526. bestrun_type = run_type; \
  527. if (bestrun_length >= 20) \
  528. break; \
  529. }
  530. /****************************************************************************/
  531. /* SETFGCHAR */
  532. /* */
  533. /* Set up a new value in fgChar and recalculate the shift */
  534. /****************************************************************************/
  535. #define SETFGCHAR(newchar, curchar, curshift) \
  536. curchar = newchar; \
  537. { \
  538. BYTE workchar = curchar; \
  539. curshift = 0; \
  540. while ((workchar & 0x01) == 0) { \
  541. curshift++; \
  542. workchar = (BYTE)(workchar >> 1); \
  543. } \
  544. }
  545. /****************************************************************************/
  546. /* ENCODEFGBG */
  547. /* */
  548. /* Encode 8 bytes of FG and black into a one byte bitmap representation */
  549. /* */
  550. /* The FgChar will always be non-zero, and therefore must have at least one */
  551. /* bit set. */
  552. /* */
  553. /* We arrange that all bytes have this bit in their lowest position */
  554. /* The zero pels will still have a 0 in the lowest bit. */
  555. /* */
  556. /* Getting the result is a 4 stage process */
  557. /* */
  558. /* 1) Get the wanted bits into bit 0 of each byte */
  559. /* */
  560. /* <***************work1*****************> */
  561. /* 31 0 */
  562. /* 0000 000d 0000 000c 0000 000b 0000 000a */
  563. /* ^ ^ ^ ^ */
  564. /* <***************work2*****************> */
  565. /* 31 0 */
  566. /* 0000 000h 0000 000g 0000 000f 0000 000e */
  567. /* ^ ^ ^ ^ */
  568. /* */
  569. /* a..h = bits that we want to output */
  570. /* */
  571. /* We just need to collect the indicated bits and squash them into a single */
  572. /* byte. */
  573. /* */
  574. /* 2) Compress down to 32 bits */
  575. /* */
  576. /* <***************work1*****************> */
  577. /* 31 0 */
  578. /* 000h 000d 000g 000c 000f 000b 000e 000a */
  579. /* ^ ^ ^ ^ ^ ^ ^ ^ */
  580. /* */
  581. /* 3) Compress down to 16 bits */
  582. /* */
  583. /* <******work*******> */
  584. /* 15 0 */
  585. /* 0h0f 0d0b 0g0e 0c0a */
  586. /* ^ ^ ^ ^ ^ ^ ^ ^ */
  587. /* */
  588. /* 4) Compress down to 8 bits */
  589. /* */
  590. /* hgfedcba */
  591. /****************************************************************************/
  592. #define ENCODEFGBG(result) \
  593. { \
  594. UINT32 work1; \
  595. UINT32 work2; \
  596. unsigned work; \
  597. \
  598. work1 = ((*(PUINT32_UA)(xorbuf + EncodeSrcOffset)) >> fgShift) & \
  599. 0x01010101; \
  600. work2 = ((*(PUINT32_UA)(xorbuf + EncodeSrcOffset + 4)) >> fgShift) & \
  601. 0x01010101; \
  602. work1 = (work2 << 4) | work1; \
  603. work = work1 | (work1 >> 14); \
  604. result = ((BYTE)(((BYTE)(work >> 7)) | ((BYTE)work))); \
  605. }
  606. #ifndef DC_HICOLOR
  607. /****************************************************************************/
  608. // The following structure contains the results of our intermediate scan of
  609. // the buffer.
  610. /****************************************************************************/
  611. typedef struct {
  612. unsigned length;
  613. BYTE type;
  614. BYTE fgChar;
  615. } MATCH;
  616. #endif
  617. /****************************************************************************/
  618. // Critical minimum limit on a run size -- magic number that determines
  619. // color run search characteristics. Minimum is 4 for hard-coded DWORD-size
  620. // checks below. Comparisons of values:
  621. // MinRunSize KBytes* Comp CPU ("hits")
  622. // 4 52487 115842
  623. // 5 52697 115116
  624. // 6 52980 120565
  625. // 7 53306 123680
  626. // * = KBytes server->client WinBench98 Graphics WinMark minus CorelDRAW,
  627. // measured in NetMon on Ethernet.
  628. /****************************************************************************/
  629. #define MinRunSize 5
  630. /****************************************************************************/
  631. // CompressV2Int
  632. //
  633. // Compresses a bitmap in one call, returning the size of the space used in
  634. // the destination buffer or zero if the buffer was not large enough.
  635. //
  636. // Implementation notes: We use a length-2 array of MATCH elements as a
  637. // running lookbehind buffer, allowing us to combine current run analysis
  638. // results with previous entries before encoding into the destination buffer.
  639. /****************************************************************************/
  640. #ifdef DC_HICOLOR
  641. unsigned RDPCALL SHCLASS CompressV2Int(
  642. PBYTE pSrc,
  643. PBYTE pDst,
  644. unsigned numPels,
  645. unsigned rowDelta,
  646. unsigned dstBufferSize,
  647. BYTE *xorbuf)
  648. {
  649. unsigned srcOffset;
  650. unsigned EncodeSrcOffset;
  651. unsigned bestRunLength;
  652. unsigned nextRunLength;
  653. unsigned runLength;
  654. unsigned bestFGRunLength;
  655. unsigned scanCount;
  656. unsigned saveNumPels;
  657. BOOLEAN inColorRun = FALSE;
  658. BOOLEAN bEncodeAllMatches;
  659. BYTE bestRunType = 0;
  660. BYTE fgPel = 0xFF;
  661. BYTE fgPelWork = 0xFF;
  662. BYTE fgShift = 0;
  663. BYTE EncodeFGPel;
  664. PBYTE destbuf = pDst;
  665. unsigned compressedLength = 0;
  666. MATCH match[2];
  667. DC_BEGIN_FN("CompressV2Int");
  668. /************************************************************************/
  669. // Validate params.
  670. /************************************************************************/
  671. TRC_ASSERT((numPels >= rowDelta),(TB,"numPels < rowDelta"));
  672. TRC_ASSERT((!(rowDelta & 0x3)),(TB,"rowDelta not multiple of 4"));
  673. TRC_ASSERT((!(numPels & 0x3)),(TB,"numPels not multiple of 4"));
  674. TRC_ASSERT((!((UINT_PTR)pSrc & 0x3)),
  675. (TB, "Possible unaligned access, pSrc = %p", pSrc));
  676. /************************************************************************/
  677. // Create XOR buffer - first row is copied from src, succeeding rows
  678. // are the corresponding src row XOR'd with the next src row.
  679. /************************************************************************/
  680. memcpy(xorbuf, pSrc, rowDelta);
  681. {
  682. BYTE *srcbuf = pSrc + rowDelta;
  683. unsigned srclen = numPels - rowDelta;
  684. UINT32 *dwdest = (UINT32 *)(xorbuf + rowDelta);
  685. while (srclen >= 8) {
  686. *dwdest++ = *((PUINT32)srcbuf) ^ *((PUINT32)(srcbuf -
  687. (int)rowDelta));
  688. srcbuf += 4;
  689. *dwdest++ = *((PUINT32)srcbuf) ^ *((PUINT32)(srcbuf -
  690. (int)rowDelta));
  691. srcbuf += 4;
  692. srclen -= 8;
  693. }
  694. if (srclen) {
  695. // Since we're 4-byte aligned we can only have a single DWORD
  696. // remaining.
  697. *dwdest = *((PUINT32)srcbuf) ^ *((PUINT32)(srcbuf -
  698. (int)rowDelta));
  699. }
  700. }
  701. /************************************************************************/
  702. // Set up encoding state variables.
  703. /************************************************************************/
  704. srcOffset = 0; // Offset in src buf where we are analyzing.
  705. EncodeSrcOffset = 0; // Offset in src buf from where we are encoding.
  706. EncodeFGPel = 0xFF; // Foreground color for encoding.
  707. bEncodeAllMatches = FALSE; // Used to force encoding of all matches.
  708. match[0].type = 0; // Initially no match types.
  709. match[1].type = 0;
  710. saveNumPels = numPels;
  711. numPels = rowDelta;
  712. /************************************************************************/
  713. // Loop processing the input.
  714. // We perform the loop twice, the first time for the non-XOR first line
  715. // of the buffer and the second for the XOR portion, adjusting numPels
  716. // to the needed value for each pass.
  717. /************************************************************************/
  718. for (scanCount = 0; ; scanCount++) {
  719. while (srcOffset < numPels) {
  720. /****************************************************************/
  721. /* Start a while loop to allow a more structured break when we */
  722. /* hit the first run type we want to encode (We can't afford */
  723. /* the overheads of a function call to provide the scope here.) */
  724. /****************************************************************/
  725. while (TRUE) {
  726. bestRunLength = 0;
  727. bestFGRunLength = 0;
  728. /************************************************************/
  729. // If we are hitting the end of the buffer then just take
  730. // color characters now. We will only hit this condition if
  731. // we break out of a run just before the end of the buffer,
  732. // so this should not be too common a situation, which is
  733. // good given that we are encoding the final MinRunSize bytes
  734. // uncompressed.
  735. /************************************************************/
  736. if ((srcOffset + MinRunSize) < numPels) {
  737. goto ContinueScan;
  738. }
  739. else {
  740. bestRunType = IMAGE_COLOR;
  741. bestRunLength = numPels - srcOffset;
  742. break;
  743. }
  744. ContinueScan:
  745. /************************************************************/
  746. // First do the scans on the XOR buffer. Look for a
  747. // character run or a BG run.
  748. // We must do the test independent of how long the run
  749. // might be because even for a 1 pel BG run our later logic
  750. // requires that we detect it seperately. This code is
  751. // absolute main path so fastpath as much as possible. In
  752. // particular detect short bg runs early and allow
  753. // RunSingle to presuppose at least 4 matching bytes.
  754. /************************************************************/
  755. if (xorbuf[srcOffset] == 0x00) {
  756. if ((srcOffset + 1) >= numPels ||
  757. xorbuf[srcOffset + 1] != 0x00) {
  758. bestRunType = RUN_BG;
  759. bestRunLength = 1;
  760. if (!inColorRun)
  761. break;
  762. }
  763. else {
  764. if ((srcOffset + 2) >= numPels ||
  765. xorbuf[srcOffset + 2] != 0x00) {
  766. bestRunType = RUN_BG;
  767. bestRunLength = 2;
  768. if (!inColorRun)
  769. break;
  770. }
  771. else {
  772. if ((srcOffset + 3) >= numPels ||
  773. xorbuf[srcOffset + 3] != 0x00) {
  774. bestRunType = RUN_BG;
  775. bestRunLength = 3;
  776. if (!inColorRun)
  777. break;
  778. }
  779. else {
  780. RUNSINGLE(xorbuf + srcOffset,
  781. numPels - srcOffset,
  782. bestFGRunLength);
  783. CHECK_BEST_RUN(RUN_BG,
  784. bestFGRunLength,
  785. bestRunLength,
  786. bestRunType);
  787. if (!inColorRun)
  788. break;
  789. }
  790. }
  791. }
  792. }
  793. else {
  794. /********************************************************/
  795. // No point in starting if FG run less than 4 bytes so
  796. // check the first dword as quickly as possible.
  797. /********************************************************/
  798. if (xorbuf[srcOffset] == xorbuf[srcOffset + 1] &&
  799. *(PUINT16_UA)(xorbuf + srcOffset) ==
  800. *(PUINT16_UA)(xorbuf + srcOffset + 2))
  801. {
  802. RUNSINGLE(xorbuf+srcOffset,
  803. numPels-srcOffset,
  804. bestFGRunLength);
  805. /****************************************************/
  806. // Don't permit a short FG run to prevent a FGBG
  807. // image from starting up.
  808. /****************************************************/
  809. if (bestFGRunLength >= MinRunSize) {
  810. CHECK_BEST_RUN(RUN_FG,
  811. bestFGRunLength,
  812. bestRunLength,
  813. bestRunType);
  814. }
  815. }
  816. }
  817. /************************************************************/
  818. // Look for solid or dithered sequences in the normal
  819. // (non-XOR) buffer.
  820. /************************************************************/
  821. if ( (pSrc[srcOffset] == pSrc[srcOffset + 2]) &&
  822. (pSrc[srcOffset + 1] == pSrc[srcOffset + 3])) {
  823. /********************************************************/
  824. // Now do the scan on the normal buffer for a character
  825. // run. Don't bother if first line because we will have
  826. // found it already in the XOR buffer, since we just
  827. // copy pSrc to xorbuf for the first line. We insist on
  828. // a run of at least MinRunSize pixels.
  829. /********************************************************/
  830. if (*(pSrc + srcOffset) == *(pSrc + srcOffset + 1)) {
  831. if (srcOffset >= rowDelta) {
  832. RUNSINGLE(pSrc + srcOffset,
  833. numPels - srcOffset,
  834. nextRunLength);
  835. if (nextRunLength >= MinRunSize) {
  836. CHECK_BEST_RUN(RUN_COLOR,
  837. nextRunLength,
  838. bestRunLength,
  839. bestRunType);
  840. }
  841. }
  842. }
  843. else {
  844. /****************************************************/
  845. // Look for a dither on the nrm buffer. Dithers are
  846. // not very efficient for short runs so only take
  847. // if 8 or longer. Note that our check against
  848. // numPels above for MinRunSize will be overrun here
  849. // so we need to make sure we don't go over the
  850. // end of the buffer.
  851. /****************************************************/
  852. if (((numPels - srcOffset) > 8) &&
  853. (*(PUINT32_UA)(pSrc + srcOffset) ==
  854. *(PUINT32_UA)(pSrc + srcOffset + 4))) {
  855. RunDouble(pSrc + srcOffset + 6,
  856. numPels - srcOffset - 6,
  857. nextRunLength);
  858. nextRunLength += 6;
  859. CHECK_BEST_RUN(RUN_DITHER,
  860. nextRunLength,
  861. bestRunLength,
  862. bestRunType);
  863. }
  864. }
  865. }
  866. /************************************************************/
  867. // If nothing so far then look for a FGBG run.
  868. /************************************************************/
  869. if (bestRunLength < MinRunSize) {
  870. // Check this is not a single FG bit breaking up a BG
  871. // run. If so then encode a BG_PEL run. Careful of the
  872. // enforced BG run break across the first line
  873. // non-XOR/XOR boundary.
  874. if (*(PUINT32_UA)(xorbuf + srcOffset + 1) != 0 ||
  875. *(xorbuf + srcOffset) != fgPel ||
  876. match[1].type != RUN_BG ||
  877. srcOffset == rowDelta) {
  878. // If we have not found a run then look for a FG/BG
  879. // image. Bandwidth/CPU comparisons:
  880. // chkFGBGLen* KBytes** Comp CPU ("hits")
  881. // 48/16/8 54856 140178
  882. // 32/16/8 53177 129343
  883. // 24/8/8 53020 130583
  884. // 16/8/8 52874 126454
  885. // 8/8/0 52980 120565
  886. // no check 59753 101091
  887. // * = minimum run length for checking best:
  888. // start val / subtract for workchar==fgPel /
  889. // subtract for nextRunLen divisible by 8
  890. // ** = KBytes server->client WinBench98 Graphics
  891. // WinMark minus CorelDRAW, measured in NetMon
  892. // on Ethernet.
  893. RUNFGBG(xorbuf + srcOffset, numPels - srcOffset,
  894. nextRunLength, fgPelWork);
  895. if (fgPelWork == fgPel || nextRunLength >= 8) {
  896. CHECK_BEST_RUN(IMAGE_FGBG,
  897. nextRunLength,
  898. bestRunLength,
  899. bestRunType);
  900. }
  901. }
  902. else {
  903. RUNSINGLE(xorbuf + srcOffset + 1,
  904. numPels - srcOffset - 1,
  905. nextRunLength);
  906. nextRunLength++;
  907. CHECK_BEST_RUN(RUN_BG_PEL,
  908. nextRunLength,
  909. bestRunLength,
  910. bestRunType);
  911. }
  912. }
  913. /************************************************************/
  914. /* If nothing useful so far then allow a short run. */
  915. /* Don't do this if we are accumulating a color run because */
  916. /* it will really mess up GDC compression if we allow lots */
  917. /* of little runs. Also require that it is a regular short */
  918. /* run, rather than one that disturbs the fgPel */
  919. /************************************************************/
  920. if (!inColorRun) {
  921. if (bestRunLength < MinRunSize) {
  922. if (bestFGRunLength >= MinRunSize &&
  923. xorbuf[srcOffset] == fgPel) {
  924. /************************************************/
  925. /* We mustn't merge with the previous code */
  926. /* if we have just crossed the non-XOR/XOR */
  927. /* boundary. */
  928. /************************************************/
  929. if (match[1].type == RUN_FG &&
  930. srcOffset != rowDelta) {
  931. match[1].length += bestFGRunLength;
  932. srcOffset += bestFGRunLength;
  933. continue;
  934. }
  935. else {
  936. bestRunLength = bestFGRunLength;
  937. bestRunType = RUN_FG;
  938. }
  939. }
  940. else {
  941. /************************************************/
  942. /* If we decided to take a run earlier then */
  943. /* allow it now. (May be a short BG run, for */
  944. /* example) If nothing so far then take color */
  945. /* image) */
  946. /************************************************/
  947. if (bestRunLength == 0) {
  948. bestRunType = IMAGE_COLOR;
  949. bestRunLength = 1;
  950. }
  951. }
  952. }
  953. }
  954. else {
  955. // We're in a color run. Keep small runs of other types
  956. // from breaking up the color run and increasing the
  957. // encoded size.
  958. if (bestRunLength < (unsigned)(bestRunType == RUN_BG ?
  959. MinRunSize : (MinRunSize + 2))) {
  960. bestRunType = IMAGE_COLOR;
  961. bestRunLength = 1;
  962. }
  963. }
  964. // Get out of the loop after all checks are completed.
  965. break;
  966. }
  967. /****************************************************************/
  968. /* When we get here we have found the best run. Now check for */
  969. /* various amalgamation conditions with the previous run type. */
  970. /* Note that we may already have done amalgamation of short */
  971. /* runs, but we had to do multiple samples for the longer runs */
  972. /* so we repeat the checks here */
  973. /****************************************************************/
  974. /****************************************************************/
  975. // If we are encoding a color run then combine it with an
  976. // existing run if possible.
  977. /****************************************************************/
  978. if (bestRunType != IMAGE_COLOR) {
  979. /************************************************************/
  980. /* We are no longer encoding a COLOR_IMAGE of any kind */
  981. /************************************************************/
  982. inColorRun = FALSE;
  983. // If we can amalgamate the entry then do so without creating
  984. // a new array entry. Our search for FGBG runs is dependent
  985. // upon that type of run being amalgamated because we break
  986. // every 64 characters so that our mode switch detection
  987. // works OK.
  988. //
  989. // Take care not to merge across the non-xor/xor boundary.
  990. if (srcOffset != rowDelta) {
  991. // Bump srcOffset and try a merge.
  992. srcOffset += bestRunLength;
  993. switch (bestRunType) {
  994. case RUN_BG:
  995. // BG runs merge with BG and BG_PEL runs.
  996. if (match[1].type == RUN_BG ||
  997. match[1].type == RUN_BG_PEL) {
  998. match[1].length += bestRunLength;
  999. TRC_DBG((TB, "Merged BG with preceding, "
  1000. "giving %u", match[1].length));
  1001. continue;
  1002. }
  1003. // Deliberate fallthrough to BG_PEL.
  1004. case RUN_BG_PEL:
  1005. // If it is a BG run following a FGBG run then
  1006. // merge in the pels to make the FGBG length a
  1007. // multiple of 8. If the remaining BG run is <= 8
  1008. // (which would translate to one extra byte in
  1009. // the previous FGBG as well as one byte of BG),
  1010. // merge it in also, otherwise just write the
  1011. // shortened BG run. Note that for RUN_BG_PEL,
  1012. // FG color will be the same as for the
  1013. // FGBG, no need to check.
  1014. if (match[1].type == IMAGE_FGBG &&
  1015. match[1].length & 0x0007) {
  1016. unsigned mergelen = 8 - (match[1].length &
  1017. 0x0007);
  1018. if (mergelen > bestRunLength)
  1019. mergelen = bestRunLength;
  1020. match[1].length += mergelen;
  1021. bestRunLength -= mergelen;
  1022. TRC_DBG((TB,"Add %u pels to FGBG giving %u "
  1023. "leaving %u",
  1024. mergelen, match[1].length,
  1025. bestRunLength));
  1026. if (bestRunLength <= 8) {
  1027. match[1].length += bestRunLength;
  1028. TRC_DBG((TB,"Merge BG with prev FGBG "
  1029. "gives %u", match[1].length));
  1030. continue;
  1031. }
  1032. }
  1033. break;
  1034. case RUN_FG:
  1035. // Keep track of the FG color. Remember to
  1036. // subtract bestRunLength since we incremented
  1037. // it before the switch statement.
  1038. fgPel = xorbuf[srcOffset - bestRunLength];
  1039. // FG run merges with previous FG if FG color is same.
  1040. if (match[1].type == RUN_FG &&
  1041. match[1].fgPel == fgPel) {
  1042. match[1].length += bestRunLength;
  1043. TRC_DBG((TB, "Merged FG with preceding, giving %u",
  1044. match[1].length));
  1045. continue;
  1046. }
  1047. break;
  1048. case IMAGE_FGBG:
  1049. // FGBG leaves the foreground character in
  1050. // fgPelWork.
  1051. fgPel = fgPelWork;
  1052. // FGBG merges with previous if the FG colors are
  1053. // the same.
  1054. if (match[1].type == IMAGE_FGBG &&
  1055. match[1].fgPel == fgPel) {
  1056. match[1].length += bestRunLength;
  1057. TRC_DBG((TB, "Merged FGBG with preceding "
  1058. "FGBG, giving %u", match[1].length));
  1059. continue;
  1060. }
  1061. // FGBG merges with with small BG runs.
  1062. if (match[1].type == RUN_BG &&
  1063. match[1].length < 8) {
  1064. match[1].type = IMAGE_FGBG;
  1065. match[1].length += bestRunLength;
  1066. match[1].fgPel = fgPel;
  1067. TRC_DBG((TB, "Merged FGBG with preceding "
  1068. "BG run -> %u", match[1].length));
  1069. continue;
  1070. }
  1071. break;
  1072. }
  1073. }
  1074. else {
  1075. // Keep track of the FG color. The macro that searches for
  1076. // FGBG runs leaves the character in fgPelWork.
  1077. // Note this code is inlined into the merging code
  1078. // before.
  1079. if (bestRunType == RUN_FG)
  1080. fgPel = xorbuf[srcOffset];
  1081. else if (bestRunType == IMAGE_FGBG)
  1082. fgPel = fgPelWork;
  1083. // We're at the end of the first line. Just bump the
  1084. // source offset.
  1085. srcOffset += bestRunLength;
  1086. }
  1087. }
  1088. else {
  1089. /************************************************************/
  1090. /* Flag that we are within a color run */
  1091. /************************************************************/
  1092. inColorRun = TRUE;
  1093. srcOffset += bestRunLength;
  1094. /************************************************************/
  1095. // Merge the color run immediately, if possible. Note color
  1096. // runs are not restricted by the non-XOR/XOR boundary.
  1097. /************************************************************/
  1098. if (match[1].type == IMAGE_COLOR) {
  1099. match[1].length += bestRunLength;
  1100. continue;
  1101. }
  1102. if (match[0].type == IMAGE_COLOR && match[1].length == 1) {
  1103. // If it is a color run spanning any kind of single pel
  1104. // entity then merge all three runs into one.
  1105. // We have to create a special match queue condition
  1106. // here -- the single merged entry needs to be placed
  1107. // in the match[1] position and a null entry into [0]
  1108. // to allow the rest of the code to continue to
  1109. // be hardcoded to merge with [1].
  1110. match[1].length = match[0].length +
  1111. bestRunLength + 1;
  1112. match[1].type = IMAGE_COLOR;
  1113. match[0].type = 0;
  1114. TRC_DBG((TB, "Merged color with preceding color gives %u",
  1115. match[1].length));
  1116. continue;
  1117. }
  1118. }
  1119. /****************************************************************/
  1120. // The current run could not be merged with a previous match
  1121. // queue entry, We have to encode the [0] slot then add the
  1122. // current run the the queue.
  1123. /****************************************************************/
  1124. TRC_DBG((TB, "Best run of type %u has length %u", bestRunType,
  1125. bestRunLength));
  1126. DoEncoding:
  1127. // First check for our approaching the end of the destination
  1128. // buffer and get out if this is the case. We allow for the
  1129. // largest general run order (a mega-mega set run = 4 bytes).
  1130. // Orders which may be larger are checked within the case arm
  1131. if ((unsigned)(destbuf - pDst + 4) <= dstBufferSize)
  1132. goto ContinueEncoding;
  1133. else
  1134. DC_QUIT;
  1135. ContinueEncoding:
  1136. switch (match[0].type) {
  1137. case 0:
  1138. // Unused entry.
  1139. break;
  1140. case RUN_BG:
  1141. case RUN_BG_PEL:
  1142. // Note that for BG_PEL we utilize the code sequence
  1143. // BG,BG which would not otherwise appear as a special
  1144. // case meaning insert one current FG char between
  1145. // the two runs.
  1146. ENCODE_ORDER_MEGA(destbuf,
  1147. CODE_BG_RUN,
  1148. match[0].length,
  1149. CODE_MEGA_MEGA_BG_RUN,
  1150. MAX_LENGTH_ORDER,
  1151. MAX_LENGTH_LONG_ORDER);
  1152. TRC_DBG((TB, "BG RUN %u",match[0].length));
  1153. EncodeSrcOffset += match[0].length;
  1154. break;
  1155. case RUN_FG:
  1156. // If the fg value is different from the current
  1157. // then encode a set+run code.
  1158. if (EncodeFGPel != match[0].fgPel) {
  1159. SETFGCHAR((BYTE)match[0].fgPel, EncodeFGPel, fgShift);
  1160. ENCODE_ORDER_MEGA(destbuf,
  1161. CODE_SET_FG_FG_RUN,
  1162. match[0].length,
  1163. CODE_MEGA_MEGA_SET_FG_RUN,
  1164. MAX_LENGTH_ORDER_LITE,
  1165. MAX_LENGTH_LONG_ORDER_LITE);
  1166. *destbuf++ = EncodeFGPel;
  1167. TRC_DBG((TB, "SET_FG_FG_RUN %u", match[0].length));
  1168. }
  1169. else {
  1170. ENCODE_ORDER_MEGA(destbuf,
  1171. CODE_FG_RUN,
  1172. match[0].length,
  1173. CODE_MEGA_MEGA_FG_RUN,
  1174. MAX_LENGTH_ORDER,
  1175. MAX_LENGTH_LONG_ORDER);
  1176. TRC_DBG((TB, "FG_RUN %u", match[0].length));
  1177. }
  1178. EncodeSrcOffset += match[0].length;
  1179. break;
  1180. case IMAGE_FGBG:
  1181. runLength = match[0].length;
  1182. // Check for our approaching the end of the destination
  1183. // buffer and get out if this is the case.
  1184. if ((destbuf - pDst + (runLength + 7)/8 + 4) <=
  1185. dstBufferSize)
  1186. goto ContinueFGBG;
  1187. else
  1188. DC_QUIT;
  1189. ContinueFGBG:
  1190. // We need to convert FGBG runs into the pixel form.
  1191. if (EncodeFGPel != match[0].fgPel) {
  1192. SETFGCHAR((BYTE)match[0].fgPel, EncodeFGPel, fgShift);
  1193. ENCODE_ORDER_MEGA_FGBG(destbuf,
  1194. CODE_SET_FG_FG_BG,
  1195. runLength,
  1196. CODE_MEGA_MEGA_SET_FGBG,
  1197. MAX_LENGTH_FGBG_ORDER_LITE,
  1198. MAX_LENGTH_LONG_FGBG_ORDER);
  1199. *destbuf++ = EncodeFGPel;
  1200. TRC_DBG((TB, "SET_FG_FG_BG %u", match[0].length));
  1201. while (runLength >= 8) {
  1202. ENCODEFGBG(*destbuf);
  1203. destbuf++;
  1204. EncodeSrcOffset += 8;
  1205. runLength -= 8;
  1206. }
  1207. if (runLength) {
  1208. ENCODEFGBG(*destbuf);
  1209. // Keep the final partial byte clean to help GDC
  1210. // packing.
  1211. *destbuf &= ((0x01 << runLength) - 1);
  1212. destbuf++;
  1213. EncodeSrcOffset += runLength;
  1214. }
  1215. }
  1216. else {
  1217. if (runLength == 8) {
  1218. BYTE fgbgChar;
  1219. // See if it is one of the high probability bytes.
  1220. ENCODEFGBG(fgbgChar);
  1221. // Check for single byte encoding of FGBG images.
  1222. switch (fgbgChar) {
  1223. case SPECIAL_FGBG_CODE_1:
  1224. *destbuf++ = CODE_SPECIAL_FGBG_1;
  1225. break;
  1226. case SPECIAL_FGBG_CODE_2:
  1227. *destbuf++ = CODE_SPECIAL_FGBG_2;
  1228. break;
  1229. default:
  1230. ENCODE_ORDER_MEGA_FGBG(destbuf,
  1231. CODE_FG_BG_IMAGE,
  1232. runLength,
  1233. CODE_MEGA_MEGA_FGBG,
  1234. MAX_LENGTH_FGBG_ORDER,
  1235. MAX_LENGTH_LONG_FGBG_ORDER);
  1236. *destbuf++ = fgbgChar;
  1237. break;
  1238. }
  1239. EncodeSrcOffset += 8;
  1240. }
  1241. else {
  1242. // Encode as standard FGBG.
  1243. ENCODE_ORDER_MEGA_FGBG(destbuf,
  1244. CODE_FG_BG_IMAGE,
  1245. runLength,
  1246. CODE_MEGA_MEGA_FGBG,
  1247. MAX_LENGTH_FGBG_ORDER,
  1248. MAX_LENGTH_LONG_FGBG_ORDER);
  1249. TRC_DBG((TB, "FG_BG %u", match[0].length));
  1250. while (runLength >= 8) {
  1251. ENCODEFGBG(*destbuf);
  1252. destbuf++;
  1253. EncodeSrcOffset += 8;
  1254. runLength -= 8;
  1255. }
  1256. if (runLength) {
  1257. ENCODEFGBG(*destbuf);
  1258. *destbuf &= ((0x01 << runLength) - 1);
  1259. destbuf++;
  1260. EncodeSrcOffset += runLength;
  1261. }
  1262. }
  1263. }
  1264. break;
  1265. case RUN_COLOR:
  1266. ENCODE_ORDER_MEGA(destbuf,
  1267. CODE_COLOR_RUN,
  1268. match[0].length,
  1269. CODE_MEGA_MEGA_COLOR_RUN,
  1270. MAX_LENGTH_ORDER,
  1271. MAX_LENGTH_LONG_ORDER);
  1272. TRC_DBG((TB, "COLOR_RUN %u", match[0].length));
  1273. *destbuf++ = pSrc[EncodeSrcOffset];
  1274. EncodeSrcOffset += match[0].length;
  1275. break;
  1276. case RUN_DITHER:
  1277. {
  1278. unsigned ditherlen = match[0].length / 2;
  1279. ENCODE_ORDER_MEGA(destbuf,
  1280. CODE_DITHERED_RUN,
  1281. ditherlen,
  1282. CODE_MEGA_MEGA_DITHER,
  1283. MAX_LENGTH_ORDER_LITE,
  1284. MAX_LENGTH_LONG_ORDER_LITE);
  1285. TRC_DBG((TB, "DITHERED_RUN %u", match[0].length));
  1286. // First check for our approaching the end of the
  1287. // destination buffer and get out if this is the case.
  1288. if ((unsigned)(destbuf - pDst + 2) <= dstBufferSize) {
  1289. *destbuf++ = pSrc[EncodeSrcOffset];
  1290. *destbuf++ = pSrc[EncodeSrcOffset + 1];
  1291. EncodeSrcOffset += match[0].length;
  1292. }
  1293. else {
  1294. DC_QUIT;
  1295. }
  1296. }
  1297. break;
  1298. case IMAGE_COLOR:
  1299. // Length 1 can possibly be encoded as a single BLACK/WHITE.
  1300. if (match[0].length == 1) {
  1301. if (pSrc[EncodeSrcOffset] == 0x00) {
  1302. *destbuf++ = CODE_BLACK;
  1303. EncodeSrcOffset++;
  1304. break;
  1305. }
  1306. if (pSrc[EncodeSrcOffset] == 0xFF) {
  1307. *destbuf++ = CODE_WHITE;
  1308. EncodeSrcOffset++;
  1309. break;
  1310. }
  1311. }
  1312. // Store the data in non-compressed form.
  1313. ENCODE_ORDER_MEGA(destbuf,
  1314. CODE_COLOR_IMAGE,
  1315. match[0].length,
  1316. CODE_MEGA_MEGA_CLR_IMG,
  1317. MAX_LENGTH_ORDER,
  1318. MAX_LENGTH_LONG_ORDER);
  1319. TRC_DBG((TB, "COLOR_IMAGE %u", match[0].length));
  1320. // First check for our approaching the end of the
  1321. // destination buffer and get out if this is the case.
  1322. if ((destbuf - pDst + (UINT_PTR)match[0].length) <=
  1323. dstBufferSize) {
  1324. // Now just copy the data over.
  1325. memcpy(destbuf, pSrc+EncodeSrcOffset, match[0].length);
  1326. destbuf += match[0].length;
  1327. EncodeSrcOffset += match[0].length;
  1328. }
  1329. else {
  1330. DC_QUIT;
  1331. }
  1332. break;
  1333. #ifdef DC_DEBUG
  1334. default:
  1335. TRC_ERR((TB, "Invalid run type %u",match[0].type));
  1336. break;
  1337. #endif
  1338. }
  1339. /****************************************************************/
  1340. // Done encoding, what we do next is determined by whether we're
  1341. // flushing the match queue after everything is scanned.
  1342. /****************************************************************/
  1343. match[0] = match[1];
  1344. if (!bEncodeAllMatches) {
  1345. // Push the current run into the top of the queue.
  1346. match[1].type = bestRunType;
  1347. match[1].length = bestRunLength;
  1348. match[1].fgPel = fgPel;
  1349. }
  1350. else {
  1351. // We need to check to see if we're really finished. Since
  1352. // our maximum queue depth is 2, if we're done then the only
  1353. // remaining entry has an encoding type of 0.
  1354. if (match[0].type == 0) {
  1355. goto PostScan;
  1356. }
  1357. else {
  1358. match[1].type = 0;
  1359. goto DoEncoding;
  1360. }
  1361. }
  1362. }
  1363. if (scanCount == 0) {
  1364. // If we have just done our scan of the first line then now do the
  1365. // rest of the buffer. Reset our saved pel count.
  1366. numPels = saveNumPels;
  1367. }
  1368. else {
  1369. // When we are done with the second pass (we've reached the end of
  1370. // the buffer) we have to force the remaining items in the match
  1371. // queue to be encoded. Yes this is similar to old BASIC
  1372. // code in using gotos, but we cannot place the encoding code into
  1373. // a function because of the number of params required, and
  1374. // we cannot duplicate it because it is too big. This code is
  1375. // some of the most used in the system so the cost is worth it.
  1376. bEncodeAllMatches = TRUE;
  1377. goto DoEncoding;
  1378. }
  1379. }
  1380. PostScan:
  1381. // Success, calculate the amount of space we used.
  1382. compressedLength = (unsigned)(destbuf - pDst);
  1383. DC_EXIT_POINT:
  1384. DC_END_FN();
  1385. return compressedLength;
  1386. }
  1387. #else
  1388. unsigned RDPCALL SHCLASS CompressV2Int(
  1389. PBYTE pSrc,
  1390. PBYTE pDst,
  1391. unsigned numPels,
  1392. unsigned rowDelta,
  1393. unsigned dstBufferSize,
  1394. BYTE *xorbuf)
  1395. {
  1396. unsigned srcOffset;
  1397. unsigned EncodeSrcOffset;
  1398. unsigned bestRunLength;
  1399. unsigned nextRunLength;
  1400. unsigned runLength;
  1401. unsigned bestFGRunLength;
  1402. unsigned scanCount;
  1403. unsigned saveNumPels;
  1404. BOOLEAN inColorRun = FALSE;
  1405. BOOLEAN bEncodeAllMatches;
  1406. BYTE bestRunType = 0;
  1407. BYTE fgChar = 0xFF;
  1408. BYTE fgCharWork = 0xFF;
  1409. BYTE fgShift = 0;
  1410. BYTE EncodeFGChar;
  1411. PBYTE destbuf = pDst;
  1412. unsigned compressedLength = 0;
  1413. MATCH match[2];
  1414. DC_BEGIN_FN("CompressV2Int");
  1415. /************************************************************************/
  1416. // Validate params.
  1417. /************************************************************************/
  1418. TRC_ASSERT((numPels >= rowDelta),(TB,"numPels < rowDelta"));
  1419. TRC_ASSERT((!(rowDelta & 0x3)),(TB,"rowDelta not multiple of 4"));
  1420. TRC_ASSERT((!(numPels & 0x3)),(TB,"numPels not multiple of 4"));
  1421. TRC_ASSERT((!((UINT_PTR)pSrc & 0x3)),
  1422. (TB, "Possible unaligned access, pSrc = %p", pSrc));
  1423. /************************************************************************/
  1424. // Create XOR buffer - first row is copied from src, succeeding rows
  1425. // are the corresponding src row XOR'd with the next src row.
  1426. /************************************************************************/
  1427. memcpy(xorbuf, pSrc, rowDelta);
  1428. {
  1429. BYTE *srcbuf = pSrc + rowDelta;
  1430. unsigned srclen = numPels - rowDelta;
  1431. UINT32 *dwdest = (UINT32 *)(xorbuf + rowDelta);
  1432. while (srclen >= 8) {
  1433. *dwdest++ = *((PUINT32)srcbuf) ^ *((PUINT32)(srcbuf -
  1434. (int)rowDelta));
  1435. srcbuf += 4;
  1436. *dwdest++ = *((PUINT32)srcbuf) ^ *((PUINT32)(srcbuf -
  1437. (int)rowDelta));
  1438. srcbuf += 4;
  1439. srclen -= 8;
  1440. }
  1441. if (srclen) {
  1442. // Since we're 4-byte aligned we can only have a single DWORD
  1443. // remaining.
  1444. *dwdest = *((PUINT32)srcbuf) ^ *((PUINT32)(srcbuf -
  1445. (int)rowDelta));
  1446. }
  1447. }
  1448. /************************************************************************/
  1449. // Set up encoding state variables.
  1450. /************************************************************************/
  1451. srcOffset = 0; // Offset in src buf where we are analyzing.
  1452. EncodeSrcOffset = 0; // Offset in src buf from where we are encoding.
  1453. EncodeFGChar = 0xFF; // Foreground color for encoding.
  1454. bEncodeAllMatches = FALSE; // Used to force encoding of all matches.
  1455. match[0].type = 0; // Initially no match types.
  1456. match[1].type = 0;
  1457. saveNumPels = numPels;
  1458. numPels = rowDelta;
  1459. /************************************************************************/
  1460. // Loop processing the input.
  1461. // We perform the loop twice, the first time for the non-XOR first line
  1462. // of the buffer and the second for the XOR portion, adjusting numPels
  1463. // to the needed value for each pass.
  1464. /************************************************************************/
  1465. for (scanCount = 0; ; scanCount++) {
  1466. while (srcOffset < numPels) {
  1467. /****************************************************************/
  1468. /* Start a while loop to allow a more structured break when we */
  1469. /* hit the first run type we want to encode (We can't afford */
  1470. /* the overheads of a function call to provide the scope here.) */
  1471. /****************************************************************/
  1472. while (TRUE) {
  1473. bestRunLength = 0;
  1474. bestFGRunLength = 0;
  1475. /************************************************************/
  1476. // If we are hitting the end of the buffer then just take
  1477. // color characters now. We will only hit this condition if
  1478. // we break out of a run just before the end of the buffer,
  1479. // so this should not be too common a situation, which is
  1480. // good given that we are encoding the final MinRunSize bytes
  1481. // uncompressed.
  1482. /************************************************************/
  1483. if ((srcOffset + MinRunSize) < numPels) {
  1484. goto ContinueScan;
  1485. }
  1486. else {
  1487. bestRunType = IMAGE_COLOR;
  1488. bestRunLength = numPels - srcOffset;
  1489. break;
  1490. }
  1491. ContinueScan:
  1492. /************************************************************/
  1493. // First do the scans on the XOR buffer. Look for a
  1494. // character run or a BG run.
  1495. // We must do the test independent of how long the run
  1496. // might be because even for a 1 pel BG run our later logic
  1497. // requires that we detect it seperately. This code is
  1498. // absolute main path so fastpath as much as possible. In
  1499. // particular detect short bg runs early and allow
  1500. // RunSingle to presuppose at least 4 matching bytes.
  1501. /************************************************************/
  1502. if (xorbuf[srcOffset] == 0x00) {
  1503. if ((srcOffset + 1) >= numPels ||
  1504. xorbuf[srcOffset + 1] != 0x00) {
  1505. bestRunType = RUN_BG;
  1506. bestRunLength = 1;
  1507. if (!inColorRun)
  1508. break;
  1509. }
  1510. else {
  1511. if ((srcOffset + 2) >= numPels ||
  1512. xorbuf[srcOffset + 2] != 0x00) {
  1513. bestRunType = RUN_BG;
  1514. bestRunLength = 2;
  1515. if (!inColorRun)
  1516. break;
  1517. }
  1518. else {
  1519. if ((srcOffset + 3) >= numPels ||
  1520. xorbuf[srcOffset + 3] != 0x00) {
  1521. bestRunType = RUN_BG;
  1522. bestRunLength = 3;
  1523. if (!inColorRun)
  1524. break;
  1525. }
  1526. else {
  1527. RUNSINGLE(xorbuf + srcOffset,
  1528. numPels - srcOffset,
  1529. bestFGRunLength);
  1530. CHECK_BEST_RUN(RUN_BG,
  1531. bestFGRunLength,
  1532. bestRunLength,
  1533. bestRunType);
  1534. if (!inColorRun)
  1535. break;
  1536. }
  1537. }
  1538. }
  1539. }
  1540. else {
  1541. /********************************************************/
  1542. // No point in starting if FG run less than 4 bytes so
  1543. // check the first dword as quickly as possible.
  1544. /********************************************************/
  1545. if (xorbuf[srcOffset] == xorbuf[srcOffset + 1] &&
  1546. *(PUINT16_UA)(xorbuf + srcOffset) ==
  1547. *(PUINT16_UA)(xorbuf + srcOffset + 2))
  1548. {
  1549. RUNSINGLE(xorbuf + srcOffset,
  1550. numPels - srcOffset,
  1551. bestFGRunLength);
  1552. /****************************************************/
  1553. // Don't permit a short FG run to prevent a FGBG
  1554. // image from starting up.
  1555. /****************************************************/
  1556. if (bestFGRunLength >= MinRunSize) {
  1557. CHECK_BEST_RUN(RUN_FG,
  1558. bestFGRunLength,
  1559. bestRunLength,
  1560. bestRunType);
  1561. }
  1562. }
  1563. }
  1564. /************************************************************/
  1565. // Look for solid or dithered sequences in the normal
  1566. // (non-XOR) buffer.
  1567. /************************************************************/
  1568. if ( (pSrc[srcOffset] == pSrc[srcOffset + 2]) &&
  1569. (pSrc[srcOffset + 1] == pSrc[srcOffset + 3])) {
  1570. /********************************************************/
  1571. // Now do the scan on the normal buffer for a character
  1572. // run. Don't bother if first line because we will have
  1573. // found it already in the XOR buffer, since we just
  1574. // copy pSrc to xorbuf for the first line. We insist on
  1575. // a run of at least MinRunSize pixels.
  1576. /********************************************************/
  1577. if (*(pSrc + srcOffset) == *(pSrc + srcOffset + 1)) {
  1578. if (srcOffset >= rowDelta) {
  1579. RUNSINGLE(pSrc + srcOffset,
  1580. numPels - srcOffset,
  1581. nextRunLength);
  1582. if (nextRunLength >= MinRunSize) {
  1583. CHECK_BEST_RUN(RUN_COLOR,
  1584. nextRunLength,
  1585. bestRunLength,
  1586. bestRunType);
  1587. }
  1588. }
  1589. }
  1590. else {
  1591. /****************************************************/
  1592. /* Look for a dither on the nrm buffer Dithers are */
  1593. /* not very efficient for short runs so only take */
  1594. /* if 8 or longer */
  1595. /****************************************************/
  1596. if (*(PUINT32_UA)(pSrc + srcOffset) ==
  1597. *(PUINT32_UA)(pSrc + srcOffset + 4)) {
  1598. RunDouble(pSrc + srcOffset + 6,
  1599. numPels - srcOffset - 6,
  1600. nextRunLength);
  1601. nextRunLength += 6;
  1602. CHECK_BEST_RUN(RUN_DITHER,
  1603. nextRunLength,
  1604. bestRunLength,
  1605. bestRunType);
  1606. }
  1607. }
  1608. }
  1609. /************************************************************/
  1610. // If nothing so far then look for a FGBG run.
  1611. /************************************************************/
  1612. if (bestRunLength < MinRunSize) {
  1613. // Check this is not a single FG bit breaking up a BG
  1614. // run. If so then encode a BG_PEL run. Careful of the
  1615. // enforced BG run break across the first line
  1616. // non-XOR/XOR boundary.
  1617. if (*(PUINT32_UA)(xorbuf + srcOffset + 1) != 0 ||
  1618. *(xorbuf + srcOffset) != fgChar ||
  1619. match[1].type != RUN_BG ||
  1620. srcOffset == rowDelta) {
  1621. // If we have not found a run then look for a FG/BG
  1622. // image. Bandwidth/CPU comparisons:
  1623. // chkFGBGLen* KBytes** Comp CPU ("hits")
  1624. // 48/16/8 54856 140178
  1625. // 32/16/8 53177 129343
  1626. // 24/8/8 53020 130583
  1627. // 16/8/8 52874 126454
  1628. // 8/8/0 52980 120565
  1629. // no check 59753 101091
  1630. // * = minimum run length for checking best:
  1631. // start val / subtract for workchar==fgChar /
  1632. // subtract for nextRunLen divisible by 8
  1633. // ** = KBytes server->client WinBench98 Graphics
  1634. // WinMark minus CorelDRAW, measured in NetMon
  1635. // on Ethernet.
  1636. RUNFGBG(xorbuf + srcOffset, numPels - srcOffset,
  1637. nextRunLength, fgCharWork);
  1638. if (fgCharWork == fgChar || nextRunLength >= 8) {
  1639. CHECK_BEST_RUN(IMAGE_FGBG,
  1640. nextRunLength,
  1641. bestRunLength,
  1642. bestRunType);
  1643. }
  1644. }
  1645. else {
  1646. RUNSINGLE(xorbuf + srcOffset + 1,
  1647. numPels - srcOffset - 1,
  1648. nextRunLength);
  1649. nextRunLength++;
  1650. CHECK_BEST_RUN(RUN_BG_PEL,
  1651. nextRunLength,
  1652. bestRunLength,
  1653. bestRunType);
  1654. }
  1655. }
  1656. /************************************************************/
  1657. /* If nothing useful so far then allow a short run. */
  1658. /* Don't do this if we are accumulating a color run because */
  1659. /* it will really mess up GDC compression if we allow lots */
  1660. /* of little runs. Also require that it is a regular short */
  1661. /* run, rather than one that disturbs the fgChar */
  1662. /************************************************************/
  1663. if (!inColorRun) {
  1664. if (bestRunLength < MinRunSize) {
  1665. if (bestFGRunLength >= MinRunSize &&
  1666. xorbuf[srcOffset] == fgChar) {
  1667. /************************************************/
  1668. /* We mustn't merge with the previous code */
  1669. /* if we have just crossed the non-XOR/XOR */
  1670. /* boundary. */
  1671. /************************************************/
  1672. if (match[1].type == RUN_FG &&
  1673. srcOffset != rowDelta) {
  1674. match[1].length += bestFGRunLength;
  1675. srcOffset += bestFGRunLength;
  1676. continue;
  1677. }
  1678. else {
  1679. bestRunLength = bestFGRunLength;
  1680. bestRunType = RUN_FG;
  1681. }
  1682. }
  1683. else {
  1684. /************************************************/
  1685. /* If we decided to take a run earlier then */
  1686. /* allow it now. (May be a short BG run, for */
  1687. /* example) If nothing so far then take color */
  1688. /* image) */
  1689. /************************************************/
  1690. if (bestRunLength == 0) {
  1691. bestRunType = IMAGE_COLOR;
  1692. bestRunLength = 1;
  1693. }
  1694. }
  1695. }
  1696. }
  1697. else {
  1698. // We're in a color run. Keep small runs of other types
  1699. // from breaking up the color run and increasing the
  1700. // encoded size.
  1701. if (bestRunLength < (unsigned)(bestRunType == RUN_BG ?
  1702. MinRunSize : (MinRunSize + 2))) {
  1703. bestRunType = IMAGE_COLOR;
  1704. bestRunLength = 1;
  1705. }
  1706. }
  1707. // Get out of the loop after all checks are completed.
  1708. break;
  1709. }
  1710. /****************************************************************/
  1711. /* When we get here we have found the best run. Now check for */
  1712. /* various amalgamation conditions with the previous run type. */
  1713. /* Note that we may already have done amalgamation of short */
  1714. /* runs, but we had to do multiple samples for the longer runs */
  1715. /* so we repeat the checks here */
  1716. /****************************************************************/
  1717. /****************************************************************/
  1718. // If we are encoding a color run then combine it with an
  1719. // existing run if possible.
  1720. /****************************************************************/
  1721. if (bestRunType != IMAGE_COLOR) {
  1722. /************************************************************/
  1723. /* We are no longer encoding a COLOR_IMAGE of any kind */
  1724. /************************************************************/
  1725. inColorRun = FALSE;
  1726. // If we can amalgamate the entry then do so without creating
  1727. // a new array entry. Our search for FGBG runs is dependent
  1728. // upon that type of run being amalgamated because we break
  1729. // every 64 characters so that our mode switch detection
  1730. // works OK.
  1731. //
  1732. // Take care not to merge across the non-xor/xor boundary.
  1733. if (srcOffset != rowDelta) {
  1734. // Bump srcOffset and try a merge.
  1735. srcOffset += bestRunLength;
  1736. switch (bestRunType) {
  1737. case RUN_BG:
  1738. // BG runs merge with BG and BG_PEL runs.
  1739. if (match[1].type == RUN_BG ||
  1740. match[1].type == RUN_BG_PEL) {
  1741. match[1].length += bestRunLength;
  1742. TRC_DBG((TB, "Merged BG with preceding, "
  1743. "giving %u", match[1].length));
  1744. continue;
  1745. }
  1746. // Deliberate fallthrough to BG_PEL.
  1747. case RUN_BG_PEL:
  1748. // If it is a BG run following a FGBG run then
  1749. // merge in the pels to make the FGBG length a
  1750. // multiple of 8. If the remaining BG run is <= 8
  1751. // (which would translate to one extra byte in
  1752. // the previous FGBG as well as one byte of BG),
  1753. // merge it in also, otherwise just write the
  1754. // shortened BG run. Note that for RUN_BG_PEL,
  1755. // FG color will be the same as for the
  1756. // FGBG, no need to check.
  1757. if (match[1].type == IMAGE_FGBG &&
  1758. match[1].length & 0x0007) {
  1759. unsigned mergelen = 8 - (match[1].length &
  1760. 0x0007);
  1761. if (mergelen > bestRunLength)
  1762. mergelen = bestRunLength;
  1763. match[1].length += mergelen;
  1764. bestRunLength -= mergelen;
  1765. TRC_DBG((TB,"Add %u pels to FGBG giving %u "
  1766. "leaving %u",
  1767. mergelen, match[1].length,
  1768. bestRunLength));
  1769. if (bestRunLength <= 8) {
  1770. match[1].length += bestRunLength;
  1771. TRC_DBG((TB,"Merge BG with prev FGBG "
  1772. "gives %u", match[1].length));
  1773. continue;
  1774. }
  1775. }
  1776. break;
  1777. case RUN_FG:
  1778. // Keep track of the FG color. Remember to
  1779. // subtract bestRunLength since we incremented
  1780. // it before the switch statement.
  1781. fgChar = xorbuf[srcOffset - bestRunLength];
  1782. // FG run merges with previous FG if FG color is same.
  1783. if (match[1].type == RUN_FG &&
  1784. match[1].fgChar == fgChar) {
  1785. match[1].length += bestRunLength;
  1786. TRC_DBG((TB, "Merged FG with preceding, giving %u",
  1787. match[1].length));
  1788. continue;
  1789. }
  1790. break;
  1791. case IMAGE_FGBG:
  1792. // FGBG leaves the foreground character in
  1793. // fgCharWork.
  1794. fgChar = fgCharWork;
  1795. // FGBG merges with previous if the FG colors are
  1796. // the same.
  1797. if (match[1].type == IMAGE_FGBG &&
  1798. match[1].fgChar == fgChar) {
  1799. match[1].length += bestRunLength;
  1800. TRC_DBG((TB, "Merged FGBG with preceding "
  1801. "FGBG, giving %u", match[1].length));
  1802. continue;
  1803. }
  1804. // FGBG merges with with small BG runs.
  1805. if (match[1].type == RUN_BG &&
  1806. match[1].length < 8) {
  1807. match[1].type = IMAGE_FGBG;
  1808. match[1].length += bestRunLength;
  1809. match[1].fgChar = fgChar;
  1810. TRC_DBG((TB, "Merged FGBG with preceding "
  1811. "BG run -> %u", match[1].length));
  1812. continue;
  1813. }
  1814. break;
  1815. }
  1816. }
  1817. else {
  1818. // Keep track of the FG color. The macro that searches for
  1819. // FGBG runs leaves the character in fgCharWork.
  1820. // Note this code is inlined into the merging code
  1821. // before.
  1822. if (bestRunType == RUN_FG)
  1823. fgChar = xorbuf[srcOffset];
  1824. else if (bestRunType == IMAGE_FGBG)
  1825. fgChar = fgCharWork;
  1826. // We're at the end of the first line. Just bump the
  1827. // source offset.
  1828. srcOffset += bestRunLength;
  1829. }
  1830. }
  1831. else {
  1832. /************************************************************/
  1833. /* Flag that we are within a color run */
  1834. /************************************************************/
  1835. inColorRun = TRUE;
  1836. srcOffset += bestRunLength;
  1837. /************************************************************/
  1838. // Merge the color run immediately, if possible. Note color
  1839. // runs are not restricted by the non-XOR/XOR boundary.
  1840. /************************************************************/
  1841. if (match[1].type == IMAGE_COLOR) {
  1842. match[1].length += bestRunLength;
  1843. continue;
  1844. }
  1845. if (match[0].type == IMAGE_COLOR && match[1].length == 1) {
  1846. // If it is a color run spanning any kind of single pel
  1847. // entity then merge all three runs into one.
  1848. // We have to create a special match queue condition
  1849. // here -- the single merged entry needs to be placed
  1850. // in the match[1] position and a null entry into [0]
  1851. // to allow the rest of the code to continue to
  1852. // be hardcoded to merge with [1].
  1853. match[1].length = match[0].length +
  1854. bestRunLength + 1;
  1855. match[1].type = IMAGE_COLOR;
  1856. match[0].type = 0;
  1857. TRC_DBG((TB, "Merged color with preceding color gives %u",
  1858. match[1].length));
  1859. continue;
  1860. }
  1861. }
  1862. /****************************************************************/
  1863. // The current run could not be merged with a previous match
  1864. // queue entry, We have to encode the [0] slot then add the
  1865. // current run the the queue.
  1866. /****************************************************************/
  1867. TRC_DBG((TB, "Best run of type %u has length %u", bestRunType,
  1868. bestRunLength));
  1869. DoEncoding:
  1870. // First check for our approaching the end of the destination
  1871. // buffer and get out if this is the case. We allow for the
  1872. // largest general run order (a mega-mega set run = 4 bytes).
  1873. // Orders which may be larger are checked within the case arm
  1874. if ((unsigned)(destbuf - pDst + 4) <= dstBufferSize)
  1875. goto ContinueEncoding;
  1876. else
  1877. DC_QUIT;
  1878. ContinueEncoding:
  1879. switch (match[0].type) {
  1880. case 0:
  1881. // Unused entry.
  1882. break;
  1883. case RUN_BG:
  1884. case RUN_BG_PEL:
  1885. // Note that for BG_PEL we utilize the code sequence
  1886. // BG,BG which would not otherwise appear as a special
  1887. // case meaning insert one current FG char between
  1888. // the two runs.
  1889. ENCODE_ORDER_MEGA(destbuf,
  1890. CODE_BG_RUN,
  1891. match[0].length,
  1892. CODE_MEGA_MEGA_BG_RUN,
  1893. MAX_LENGTH_ORDER,
  1894. MAX_LENGTH_LONG_ORDER);
  1895. TRC_DBG((TB, "BG RUN %u",match[0].length));
  1896. EncodeSrcOffset += match[0].length;
  1897. break;
  1898. case RUN_FG:
  1899. // If the fg value is different from the current
  1900. // then encode a set+run code.
  1901. if (EncodeFGChar != match[0].fgChar) {
  1902. SETFGCHAR(match[0].fgChar, EncodeFGChar, fgShift);
  1903. ENCODE_ORDER_MEGA(destbuf,
  1904. CODE_SET_FG_FG_RUN,
  1905. match[0].length,
  1906. CODE_MEGA_MEGA_SET_FG_RUN,
  1907. MAX_LENGTH_ORDER_LITE,
  1908. MAX_LENGTH_LONG_ORDER_LITE);
  1909. *destbuf++ = EncodeFGChar;
  1910. TRC_DBG((TB, "SET_FG_FG_RUN %u", match[0].length));
  1911. }
  1912. else {
  1913. ENCODE_ORDER_MEGA(destbuf,
  1914. CODE_FG_RUN,
  1915. match[0].length,
  1916. CODE_MEGA_MEGA_FG_RUN,
  1917. MAX_LENGTH_ORDER,
  1918. MAX_LENGTH_LONG_ORDER);
  1919. TRC_DBG((TB, "FG_RUN %u", match[0].length));
  1920. }
  1921. EncodeSrcOffset += match[0].length;
  1922. break;
  1923. case IMAGE_FGBG:
  1924. runLength = match[0].length;
  1925. // Check for our approaching the end of the destination
  1926. // buffer and get out if this is the case.
  1927. if ((destbuf - pDst + (runLength + 7)/8 + 4) <=
  1928. dstBufferSize)
  1929. goto ContinueFGBG;
  1930. else
  1931. DC_QUIT;
  1932. ContinueFGBG:
  1933. // We need to convert FGBG runs into the pixel form.
  1934. if (EncodeFGChar != match[0].fgChar) {
  1935. SETFGCHAR(match[0].fgChar, EncodeFGChar, fgShift);
  1936. ENCODE_ORDER_MEGA_FGBG(destbuf,
  1937. CODE_SET_FG_FG_BG,
  1938. runLength,
  1939. CODE_MEGA_MEGA_SET_FGBG,
  1940. MAX_LENGTH_FGBG_ORDER_LITE,
  1941. MAX_LENGTH_LONG_FGBG_ORDER);
  1942. *destbuf++ = EncodeFGChar;
  1943. TRC_DBG((TB, "SET_FG_FG_BG %u", match[0].length));
  1944. while (runLength >= 8) {
  1945. ENCODEFGBG(*destbuf);
  1946. destbuf++;
  1947. EncodeSrcOffset += 8;
  1948. runLength -= 8;
  1949. }
  1950. if (runLength) {
  1951. ENCODEFGBG(*destbuf);
  1952. // Keep the final partial byte clean to help GDC
  1953. // packing.
  1954. *destbuf &= ((0x01 << runLength) - 1);
  1955. destbuf++;
  1956. EncodeSrcOffset += runLength;
  1957. }
  1958. }
  1959. else {
  1960. if (runLength == 8) {
  1961. BYTE fgbgChar;
  1962. // See if it is one of the high probability bytes.
  1963. ENCODEFGBG(fgbgChar);
  1964. // Check for single byte encoding of FGBG images.
  1965. switch (fgbgChar) {
  1966. case SPECIAL_FGBG_CODE_1:
  1967. *destbuf++ = CODE_SPECIAL_FGBG_1;
  1968. break;
  1969. case SPECIAL_FGBG_CODE_2:
  1970. *destbuf++ = CODE_SPECIAL_FGBG_2;
  1971. break;
  1972. default:
  1973. ENCODE_ORDER_MEGA_FGBG(destbuf,
  1974. CODE_FG_BG_IMAGE,
  1975. runLength,
  1976. CODE_MEGA_MEGA_FGBG,
  1977. MAX_LENGTH_FGBG_ORDER,
  1978. MAX_LENGTH_LONG_FGBG_ORDER);
  1979. *destbuf++ = fgbgChar;
  1980. break;
  1981. }
  1982. EncodeSrcOffset += 8;
  1983. }
  1984. else {
  1985. // Encode as standard FGBG.
  1986. ENCODE_ORDER_MEGA_FGBG(destbuf,
  1987. CODE_FG_BG_IMAGE,
  1988. runLength,
  1989. CODE_MEGA_MEGA_FGBG,
  1990. MAX_LENGTH_FGBG_ORDER,
  1991. MAX_LENGTH_LONG_FGBG_ORDER);
  1992. TRC_DBG((TB, "FG_BG %u", match[0].length));
  1993. while (runLength >= 8) {
  1994. ENCODEFGBG(*destbuf);
  1995. destbuf++;
  1996. EncodeSrcOffset += 8;
  1997. runLength -= 8;
  1998. }
  1999. if (runLength) {
  2000. ENCODEFGBG(*destbuf);
  2001. *destbuf &= ((0x01 << runLength) - 1);
  2002. destbuf++;
  2003. EncodeSrcOffset += runLength;
  2004. }
  2005. }
  2006. }
  2007. break;
  2008. case RUN_COLOR:
  2009. ENCODE_ORDER_MEGA(destbuf,
  2010. CODE_COLOR_RUN,
  2011. match[0].length,
  2012. CODE_MEGA_MEGA_COLOR_RUN,
  2013. MAX_LENGTH_ORDER,
  2014. MAX_LENGTH_LONG_ORDER);
  2015. TRC_DBG((TB, "COLOR_RUN %u", match[0].length));
  2016. *destbuf++ = pSrc[EncodeSrcOffset];
  2017. EncodeSrcOffset += match[0].length;
  2018. break;
  2019. case RUN_DITHER:
  2020. {
  2021. unsigned ditherlen = match[0].length / 2;
  2022. ENCODE_ORDER_MEGA(destbuf,
  2023. CODE_DITHERED_RUN,
  2024. ditherlen,
  2025. CODE_MEGA_MEGA_DITHER,
  2026. MAX_LENGTH_ORDER_LITE,
  2027. MAX_LENGTH_LONG_ORDER_LITE);
  2028. TRC_DBG((TB, "DITHERED_RUN %u", match[0].length));
  2029. // First check for our approaching the end of the
  2030. // destination buffer and get out if this is the case.
  2031. if ((unsigned)(destbuf - pDst + 2) <= dstBufferSize) {
  2032. *destbuf++ = pSrc[EncodeSrcOffset];
  2033. *destbuf++ = pSrc[EncodeSrcOffset + 1];
  2034. EncodeSrcOffset += match[0].length;
  2035. }
  2036. else {
  2037. DC_QUIT;
  2038. }
  2039. }
  2040. break;
  2041. case IMAGE_COLOR:
  2042. // Length 1 can possibly be encoded as a single BLACK/WHITE.
  2043. if (match[0].length == 1) {
  2044. if (pSrc[EncodeSrcOffset] == 0x00) {
  2045. *destbuf++ = CODE_BLACK;
  2046. EncodeSrcOffset++;
  2047. break;
  2048. }
  2049. if (pSrc[EncodeSrcOffset] == 0xFF) {
  2050. *destbuf++ = CODE_WHITE;
  2051. EncodeSrcOffset++;
  2052. break;
  2053. }
  2054. }
  2055. // Store the data in non-compressed form.
  2056. ENCODE_ORDER_MEGA(destbuf,
  2057. CODE_COLOR_IMAGE,
  2058. match[0].length,
  2059. CODE_MEGA_MEGA_CLR_IMG,
  2060. MAX_LENGTH_ORDER,
  2061. MAX_LENGTH_LONG_ORDER);
  2062. TRC_DBG((TB, "COLOR_IMAGE %u", match[0].length));
  2063. // First check for our approaching the end of the
  2064. // destination buffer and get out if this is the case.
  2065. if ((destbuf - pDst + (UINT_PTR)match[0].length) <=
  2066. dstBufferSize) {
  2067. // Now just copy the data over.
  2068. memcpy(destbuf, pSrc+EncodeSrcOffset, match[0].length);
  2069. destbuf += match[0].length;
  2070. EncodeSrcOffset += match[0].length;
  2071. }
  2072. else {
  2073. DC_QUIT;
  2074. }
  2075. break;
  2076. #ifdef DC_DEBUG
  2077. default:
  2078. TRC_ERR((TB, "Invalid run type %u",match[0].type));
  2079. break;
  2080. #endif
  2081. }
  2082. /****************************************************************/
  2083. // Done encoding, what we do next is determined by whether we're
  2084. // flushing the match queue after everything is scanned.
  2085. /****************************************************************/
  2086. match[0] = match[1];
  2087. if (!bEncodeAllMatches) {
  2088. // Push the current run into the top of the queue.
  2089. match[1].type = bestRunType;
  2090. match[1].length = bestRunLength;
  2091. match[1].fgChar = fgChar;
  2092. }
  2093. else {
  2094. // We need to check to see if we're really finished. Since
  2095. // our maximum queue depth is 2, if we're done then the only
  2096. // remaining entry has an encoding type of 0.
  2097. if (match[0].type == 0) {
  2098. goto PostScan;
  2099. }
  2100. else {
  2101. match[1].type = 0;
  2102. goto DoEncoding;
  2103. }
  2104. }
  2105. }
  2106. if (scanCount == 0) {
  2107. // If we have just done our scan of the first line then now do the
  2108. // rest of the buffer. Reset our saved pel count.
  2109. numPels = saveNumPels;
  2110. }
  2111. else {
  2112. // When we are done with the second pass (we've reached the end of
  2113. // the buffer) we have to force the remaining items in the match
  2114. // queue to be encoded. Yes this is similar to old BASIC
  2115. // code in using gotos, but we cannot place the encoding code into
  2116. // a function because of the number of params required, and
  2117. // we cannot duplicate it because it is too big. This code is
  2118. // some of the most used in the system so the cost is worth it.
  2119. bEncodeAllMatches = TRUE;
  2120. goto DoEncoding;
  2121. }
  2122. }
  2123. PostScan:
  2124. // Success, calculate the amount of space we used.
  2125. compressedLength = (unsigned)(destbuf - pDst);
  2126. DC_EXIT_POINT:
  2127. DC_END_FN();
  2128. return compressedLength;
  2129. }
  2130. #endif
  2131. #ifdef DC_HICOLOR
  2132. /****************************************************************************/
  2133. /* Hi res color compression functions */
  2134. /****************************************************************************/
  2135. /****************************************************************************/
  2136. /* 15bpp version of CompressV2Int */
  2137. /****************************************************************************/
  2138. unsigned RDPCALL SHCLASS CompressV2Int15(PBYTE pSrc,
  2139. PBYTE pDst,
  2140. unsigned numBytes,
  2141. unsigned rowDelta,
  2142. unsigned dstBufferSize,
  2143. BYTE * xorbuf,
  2144. MATCH * match)
  2145. {
  2146. /****************************************************************************/
  2147. /* Function name */
  2148. /****************************************************************************/
  2149. #define BC_FN_NAME "CompressV2Int15"
  2150. /****************************************************************************/
  2151. /* Data type of a pixel */
  2152. /****************************************************************************/
  2153. #define BC_PIXEL TSUINT16
  2154. /****************************************************************************/
  2155. /* Length in bytes of a pixel */
  2156. /****************************************************************************/
  2157. #define BC_PIXEL_LEN 2
  2158. /****************************************************************************/
  2159. /* Default fgPel */
  2160. /****************************************************************************/
  2161. #define BC_DEFAULT_FGPEL 0x0000FF7F
  2162. /****************************************************************************/
  2163. /* Macro to move to the next pixel in the buffer (modifies pPos) */
  2164. /****************************************************************************/
  2165. #define BC_TO_NEXT_PIXEL(pPos) pPos += 2
  2166. /****************************************************************************/
  2167. /* Macro to returns the value of the pixel at pPos (doesn't modify pPos) */
  2168. /****************************************************************************/
  2169. #define BC_GET_PIXEL(pPos) ((TSUINT16) ((((PTSUINT8)(pPos))[1]) & 0x7f) | \
  2170. (TSUINT16) (((((PTSUINT8)(pPos))[0])) << 8) )
  2171. /****************************************************************************/
  2172. /* Macro to insert a pixel value pel at position pPos (doesn't modify pPos) */
  2173. /* */
  2174. /* pel may well be an expression (e.g. a BC_GET_PIXEL macro) so evaluate */
  2175. /* it once into a local variable. */
  2176. /****************************************************************************/
  2177. #define BC_SET_PIXEL(pPos, pel) \
  2178. { \
  2179. BC_PIXEL val = pel; \
  2180. (((PTSUINT8)(pPos))[1]) = (TSUINT8)( (val) & 0x007F); \
  2181. (((PTSUINT8)(pPos))[0]) = (TSUINT8)(((val) >> 8) & 0x00FF); \
  2182. }
  2183. /****************************************************************************/
  2184. /* Include the function body */
  2185. /****************************************************************************/
  2186. #include <abccom.c>
  2187. /****************************************************************************/
  2188. /* Undefine everything */
  2189. /****************************************************************************/
  2190. #undef BC_FN_NAME
  2191. #undef BC_PIXEL
  2192. #undef BC_PIXEL_LEN
  2193. #undef BC_TO_NEXT_PIXEL
  2194. #undef BC_GET_PIXEL
  2195. #undef BC_SET_PIXEL
  2196. #undef BC_DEFAULT_FGPEL
  2197. }
  2198. /****************************************************************************/
  2199. /* 16bpp version of CompressV2Int */
  2200. /****************************************************************************/
  2201. unsigned RDPCALL SHCLASS CompressV2Int16(PBYTE pSrc,
  2202. PBYTE pDst,
  2203. unsigned numBytes,
  2204. unsigned rowDelta,
  2205. unsigned dstBufferSize,
  2206. BYTE * xorbuf,
  2207. MATCH * match)
  2208. {
  2209. /****************************************************************************/
  2210. /* Function name */
  2211. /****************************************************************************/
  2212. #define BC_FN_NAME "CompressV2Int16"
  2213. /****************************************************************************/
  2214. /* Data type of a pixel */
  2215. /****************************************************************************/
  2216. #define BC_PIXEL TSUINT16
  2217. /****************************************************************************/
  2218. /* Length in bytes of a pixel */
  2219. /****************************************************************************/
  2220. #define BC_PIXEL_LEN 2
  2221. /****************************************************************************/
  2222. /* Default fgPel */
  2223. /****************************************************************************/
  2224. #define BC_DEFAULT_FGPEL 0x0000FFFF
  2225. /****************************************************************************/
  2226. /* Macro to move to the next pixel in the buffer (modifies pPos) */
  2227. /****************************************************************************/
  2228. #define BC_TO_NEXT_PIXEL(pPos) pPos += 2
  2229. /****************************************************************************/
  2230. /* Macro to returns the value of the pixel at pPos (doesn't modify pPos) */
  2231. /****************************************************************************/
  2232. #define BC_GET_PIXEL(pPos) ((TSUINT16) (((PTSUINT8)(pPos))[1]) | \
  2233. (TSUINT16) ((((PTSUINT8)(pPos))[0]) << 8) )
  2234. /****************************************************************************/
  2235. /* Macro to insert a pixel value pel at position pPos (doesn't modify pPos) */
  2236. /* */
  2237. /* pel may well be an expression (e.g. a BC_GET_PIXEL macro) so evaluate */
  2238. /* it once into a local variable. */
  2239. /****************************************************************************/
  2240. #define BC_SET_PIXEL(pPos, pel) \
  2241. { \
  2242. BC_PIXEL val = pel; \
  2243. (((PTSUINT8)(pPos))[1]) = (TSUINT8)( (val) & 0x00FF); \
  2244. (((PTSUINT8)(pPos))[0]) = (TSUINT8)(((val)>>8) & 0x00FF); \
  2245. }
  2246. /****************************************************************************/
  2247. /* Include the function body */
  2248. /****************************************************************************/
  2249. #include <abccom.c>
  2250. /****************************************************************************/
  2251. /* Undefine everything */
  2252. /****************************************************************************/
  2253. #undef BC_FN_NAME
  2254. #undef BC_PIXEL
  2255. #undef BC_PIXEL_LEN
  2256. #undef BC_TO_NEXT_PIXEL
  2257. #undef BC_GET_PIXEL
  2258. #undef BC_SET_PIXEL
  2259. #undef BC_DEFAULT_FGPEL
  2260. }
  2261. /****************************************************************************/
  2262. /* 24bpp version of CompressV2Int */
  2263. /****************************************************************************/
  2264. unsigned RDPCALL SHCLASS CompressV2Int24(PBYTE pSrc,
  2265. PBYTE pDst,
  2266. unsigned numBytes,
  2267. unsigned rowDelta,
  2268. unsigned dstBufferSize,
  2269. BYTE * xorbuf,
  2270. MATCH * match)
  2271. {
  2272. /****************************************************************************/
  2273. /* Function name */
  2274. /****************************************************************************/
  2275. #define BC_FN_NAME "CompressV2Int24"
  2276. /****************************************************************************/
  2277. /* Data type of a pixel */
  2278. /****************************************************************************/
  2279. #define BC_PIXEL TSUINT32
  2280. /****************************************************************************/
  2281. /* Length in bytes of a pixel */
  2282. /****************************************************************************/
  2283. #define BC_PIXEL_LEN 3
  2284. /****************************************************************************/
  2285. /* Default fgPel */
  2286. /****************************************************************************/
  2287. #define BC_DEFAULT_FGPEL 0x00FFFFFF
  2288. /****************************************************************************/
  2289. /* Macro to move to the next pixel in the buffer (modifies pPos) */
  2290. /****************************************************************************/
  2291. #define BC_TO_NEXT_PIXEL(pPos) pPos += 3
  2292. /****************************************************************************/
  2293. /* Macro to returns the value of the pixel at pPos (doesn't modify pPos) */
  2294. /****************************************************************************/
  2295. #define BC_GET_PIXEL(pPos) ((TSUINT32) (((PTSUINT8)(pPos))[2]) | \
  2296. (TSUINT32) ((((PTSUINT8)(pPos))[1]) << 8) | \
  2297. (TSUINT32) ((((PTSUINT8)(pPos))[0]) << 16) )
  2298. /****************************************************************************/
  2299. /* Macro to insert a pixel value pel at position pPos (doesn't modify pPos) */
  2300. /* */
  2301. /* pel may well be an expression (e.g. a BC_GET_PIXEL macro) so evaluate */
  2302. /* it once into a local variable. */
  2303. /****************************************************************************/
  2304. #define BC_SET_PIXEL(pPos, pel) \
  2305. { \
  2306. BC_PIXEL val = pel; \
  2307. (((PTSUINT8)(pPos))[2]) = (TSUINT8)((val) & 0x000000FF); \
  2308. (((PTSUINT8)(pPos))[1]) = (TSUINT8)(((val)>>8) & 0x000000FF); \
  2309. (((PTSUINT8)(pPos))[0]) = (TSUINT8)(((val)>>16) & 0x000000FF); \
  2310. }
  2311. /****************************************************************************/
  2312. /* Include the function body */
  2313. /****************************************************************************/
  2314. #include <abccom.c>
  2315. /****************************************************************************/
  2316. /* Undefine everything */
  2317. /****************************************************************************/
  2318. #undef BC_FN_NAME
  2319. #undef BC_PIXEL
  2320. #undef BC_PIXEL_LEN
  2321. #undef BC_TO_NEXT_PIXEL
  2322. #undef BC_GET_PIXEL
  2323. #undef BC_SET_PIXEL
  2324. #undef BC_DEFAULT_FGPEL
  2325. }
  2326. /****************************************************************************/
  2327. /* 32bpp version of CompressV2Int */
  2328. /****************************************************************************/
  2329. unsigned RDPCALL SHCLASS CompressV2Int32(PBYTE pSrc,
  2330. PBYTE pDst,
  2331. unsigned numBytes,
  2332. unsigned rowDelta,
  2333. unsigned dstBufferSize,
  2334. BYTE * xorbuf,
  2335. MATCH * match)
  2336. {
  2337. /****************************************************************************/
  2338. /* Function name */
  2339. /****************************************************************************/
  2340. #define BC_FN_NAME "CompressV2Int32"
  2341. /****************************************************************************/
  2342. /* Data type of a pixel */
  2343. /****************************************************************************/
  2344. #define BC_PIXEL TSUINT32
  2345. /****************************************************************************/
  2346. /* Length in bytes of a pixel */
  2347. /****************************************************************************/
  2348. #define BC_PIXEL_LEN 4
  2349. /****************************************************************************/
  2350. /* Default fgPel */
  2351. /****************************************************************************/
  2352. #define BC_DEFAULT_FGPEL 0xFFFFFFFF
  2353. /****************************************************************************/
  2354. /* Macro to move to the next pixel in the buffer (modifies pPos) */
  2355. /****************************************************************************/
  2356. #define BC_TO_NEXT_PIXEL(pPos) pPos += 4
  2357. /****************************************************************************/
  2358. /* Macro to returns the value of the pixel at pPos (doesn't modify pPos) */
  2359. /****************************************************************************/
  2360. #define BC_GET_PIXEL(pPos) ( \
  2361. (TSUINT32) ( (TSUINT16)(((PTSUINT8)(pPos))[3]) ) | \
  2362. (TSUINT32) (((TSUINT16)(((PTSUINT8)(pPos))[2])) << 8) | \
  2363. (TSUINT32) (((TSUINT32)(((PTSUINT8)(pPos))[1])) << 16) | \
  2364. (TSUINT32) (((TSUINT32)(((PTSUINT8)(pPos))[0])) << 24))
  2365. /****************************************************************************/
  2366. /* Macro to insert a pixel value pel at position pPos (doesn't modify pPos) */
  2367. /* */
  2368. /* pel may well be an expression (e.g. a BC_GET_PIXEL macro) so evaluate */
  2369. /* it once into a local variable. */
  2370. /****************************************************************************/
  2371. #define BC_SET_PIXEL(pPos, pel) \
  2372. { \
  2373. BC_PIXEL val = pel; \
  2374. (((PTSUINT8)(pPos))[3]) = (TSUINT8)((val) & 0x000000FF); \
  2375. (((PTSUINT8)(pPos))[2]) = (TSUINT8)(((val)>>8) & 0x000000FF); \
  2376. (((PTSUINT8)(pPos))[1]) = (TSUINT8)(((val)>>16) & 0x000000FF); \
  2377. (((PTSUINT8)(pPos))[0]) = (TSUINT8)(((val)>>24) & 0x000000FF); \
  2378. }
  2379. /****************************************************************************/
  2380. /* Include the function body */
  2381. /****************************************************************************/
  2382. #include <abccom.c>
  2383. /****************************************************************************/
  2384. /* Undefine everything */
  2385. /****************************************************************************/
  2386. #undef BC_FN_NAME
  2387. #undef BC_PIXEL
  2388. #undef BC_PIXEL_LEN
  2389. #undef BC_TO_NEXT_PIXEL
  2390. #undef BC_GET_PIXEL
  2391. #undef BC_SET_PIXEL
  2392. #undef BC_DEFAULT_FGPEL
  2393. }
  2394. #endif /* DC_HICOLOR */
  2395. #ifdef DC_DEBUG
  2396. // compression testing
  2397. #include <abdapi.c>
  2398. #endif