Source code of Windows XP (NT5)
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.

415 lines
12 KiB

  1. //
  2. // BCD.H
  3. // Bitmap Compression & Decompression
  4. //
  5. // Copyright (c) Microsoft 1997-
  6. //
  7. #ifndef _H_BCD
  8. #define _H_BCD
  9. #define BCD_MATCHCOUNT 8192
  10. #define BCD_NORMALSIZE 65000
  11. #define BCD_XORSIZE 65000
  12. //
  13. // VERSION 2 RLE codes
  14. //
  15. //
  16. // The following codes fill a full single byte address space. The approach
  17. // is to use the high order bits to identify the code type and the low
  18. // order bits to encode the length of the associated run. There are two
  19. // forms of order
  20. // - regular orders which have a 5 bit length field (31 bytes of data)
  21. // - "lite" orders with a 4 bit length
  22. //
  23. // A value of 0 in the length field indicates an extended length, where
  24. // the following byte contains the length of the data. There is also a
  25. // "mega mega" form which has a two byte length field. (See end of
  26. // codespace of the codes that define the megamega form).
  27. //
  28. // A set of codes at the high end of the address space is used to encode
  29. // commonly occuring short sequences, in particular
  30. // - certain single byte FGBG codings
  31. // - single bytes of BLACK and WHITE
  32. //
  33. //
  34. // SUMMARY
  35. // *******
  36. // 7 6 5 4 3 2 1 0 76543210 76543210 76543210
  37. //
  38. // MEGA_BG_RUN 0 0 0 0 0 0 0 0 <length>
  39. //
  40. // BG_RUN 0 0 0 <length->
  41. //
  42. // MEGA_FG_RUN 0 0 1 0 0 0 0 0 <length>
  43. //
  44. // FG_RUN 0 0 1 <length->
  45. //
  46. // MEGA_FG_BG_IMAGE 0 1 0 0 0 0 0 0 <length> <-data-> ...
  47. //
  48. // FG_BG_IMAGE 0 1 0 <length-> <-data-> ...
  49. //
  50. // MEGA_COLOR_RUN 0 1 1 0 0 0 0 0 <length> <-color>
  51. //
  52. // COLOR_RUN 0 1 1 <length-> <color->
  53. //
  54. // MEGA_COLOR_IMAGE 1 0 0 0 0 0 0 0 <length> <-data-> ...
  55. //
  56. // COLOR_IMAGE 1 0 0 <length-> <-data-> ...
  57. //
  58. // MEGA_PACKED_CLR_IMG 1 0 1 0 0 0 0 0 <length> <-data-> ...
  59. //
  60. // PACKED COLOR IMAGE 1 0 1 <length-> <-data-> ...
  61. //
  62. // SET_FG_MEGA_FG_RUN 1 1 0 0 0 0 0 0 <length> <-color>
  63. //
  64. // SET_FG_FG_RUN 1 1 0 0 <-len-> <color->
  65. //
  66. // SET_FG_MEGA_FG_BG 1 1 0 1 0 0 0 0 <length> <-color> <-data-> ...
  67. //
  68. // SET_FG_FG_BG 1 1 0 1 <-len-> <color-> <-data-> ...
  69. //
  70. // MEGA_DITHERED_RUN 1 1 1 0 0 0 0 0 <length> <-data-> <-data->
  71. //
  72. // DITHERED_RUN 1 1 1 0 <-len-> <-data-> <-data->
  73. //
  74. // MEGA_MEGA_BG_RUN 1 1 1 1 0 0 0 0
  75. //
  76. // MEGA_MEGA_FG_RUN 1 1 1 1 0 0 0 1
  77. //
  78. // MEGA_MEGA_FGBG 1 1 1 1 0 0 1 0
  79. //
  80. // MEGA_MEGA_COLOR_RUN 1 1 1 1 0 0 1 1
  81. //
  82. // MEGA_MEGA_CLR_IMG 1 1 1 1 0 1 0 0
  83. //
  84. // MEGA_MEGA_PACKED_CLR 1 1 1 1 0 1 0 1
  85. //
  86. // MEGA_MEGA_SET_FG_RUN 1 1 1 1 0 1 1 0
  87. //
  88. // MEGA_MEGA_SET_FGBG 1 1 1 1 0 1 1 1
  89. //
  90. // MEGA_MEGA_DITHER 1 1 1 1 1 0 0 0
  91. //
  92. // Special FGBG code 1 1 1 1 1 1 0 0 1 FGBG code 0x03 = 11000000
  93. // (Note that 0x01 will generally handled by the single pel insertion code)
  94. //
  95. // Special FBBG code 2 1 1 1 1 1 0 1 0 FGBG code 0x05 = 10100000
  96. //
  97. // Special FBBG code 3 1 1 1 1 1 0 1 1 FGBG code 0x07 = 11100000
  98. //
  99. // Special FBBG code 4 1 1 1 1 1 1 0 0 FGBG code 0x0F = 11110000
  100. //
  101. // BLACK 1 1 1 1 1 1 0 1
  102. //
  103. // WHITE 1 1 1 1 1 1 1 0
  104. //
  105. // START_LOSSY 1 1 1 1 1 1 1 1
  106. //
  107. //
  108. //
  109. // GENERAL NOTES
  110. //
  111. //
  112. // - For MEGA runs the length encoded is the length of the run minus the
  113. // maximum length of the non-mega form.
  114. // In the mega-mega form we encode the plain 16 bit length, to keep
  115. // encoding/deconding simple.
  116. //
  117. // - The sequence BG_RUN,BG_RUN is not exactly what it appears. We
  118. // use the fact that this is not generated in normal encoding to
  119. // encode <n background><1 foreground><n background>. The same pel
  120. // insertion convention applies to any combination of MEGA_BG run and
  121. // BG_RUN
  122. //
  123. // - A packed image is encoded when we find that all the color fields in a
  124. // run have 0 in the high order nibble. We do not currently use this code
  125. // for 8 bit compression, but it is supported by the V2 decoder.
  126. //
  127. // - The set fg color code (Used to exist in V1) has been retired in favor
  128. // of separate commands for those codes that may embed a color. Generally
  129. // This saves one byte for every foreground color transition for 8bpp.
  130. //
  131. // - The color run code is new for V2. It indicates a color run where the
  132. // XOR is not performed. This applies to, for example, the line of bits
  133. // immediately below a text line. (There is no special case for runs of
  134. // the bg color - these are treated as any other color run.)
  135. //
  136. // - Observation shows a high occurrence of BG runs split by single FGBG
  137. // codes. In decreasing probability these are 3,5,7,9,f,11,1f,3f (1 is
  138. // handled by the implicit BG run break). Save 1 byte by encoding as
  139. // single codes
  140. //
  141. // - There is a relatively high occurrence of single pel color codes ff and
  142. // 00. Save 1 byte by encoding as special characters
  143. //
  144. // - The length in a FGBG run is slightly strange. Because they generally
  145. // occur in multiples of 8 bytes we get a big saving if we encode the
  146. // length of a short run as length/8. However, for those special
  147. // cases where the length is not a multiple of 8 we encode a long run.
  148. // Therefore the long form can only cover the range 1-256 bytes.
  149. // beyond that we use the mega-mega form.
  150. //
  151. //
  152. // DETAILS OF COMPRESSION CODES
  153. //
  154. //
  155. // BG_RUN
  156. //
  157. // Represents a background run (black:0) of the specified length.
  158. //
  159. //
  160. //
  161. // FG_BG_IMAGE/SET_FG_FG_BG_IMAGE
  162. //
  163. // Represents a binary image containing only the current foreground(1) and
  164. // background(0) colors.
  165. //
  166. //
  167. //
  168. // FG_RUN/SET_FG_FG_RUN
  169. //
  170. // Represents a continuous foreground run of the specified length.
  171. // The foreground color is white by default, and is changed by the
  172. // SET_FG_FG_RUN version of this code.
  173. //
  174. //
  175. //
  176. // DITHERED_RUN
  177. //
  178. // Represents a run of alternating colors of the specified length.
  179. //
  180. //
  181. //
  182. // COLOR_IMAGE
  183. //
  184. // Represents a color image of the specified length. No XOR is performed.
  185. // This data is uncompressed, so we hope that we won't see
  186. // many of these codes!
  187. //
  188. //
  189. //
  190. // COLOR_RUN
  191. //
  192. // Represents a color run of the specified length. No XOR is performed.
  193. // Since the color is not XORed, it is unlikely to match the running
  194. // foreground color information. Therefore this code always carries a
  195. // color byte and there is no SET_FG_COLOR_RUN form of the code.
  196. //
  197. //
  198. //
  199. // PACKED_COLOR_IMAGE
  200. //
  201. // Represents a color image of the specified length, with pairs of colors
  202. // packed into a single byte. (This can only be done when the color info
  203. // is zero in the high order nibble.)
  204. //
  205. //
  206. //
  207. // START_LOSSY
  208. //
  209. // Informs the decoder that lossy mode has been established and any of the
  210. // following color runs will need pixel doubling performing.
  211. // RLE decoding will remain in this mode until the end of this block
  212. //
  213. //
  214. #define CODE_MASK 0xE0
  215. #define CODE_MASK_LITE 0xF0
  216. #define CODE_BG_RUN 0x00 // 20
  217. #define CODE_FG_RUN 0x20 // 20
  218. #define CODE_FG_BG_IMAGE 0x40 // 20
  219. #define CODE_COLOR_RUN 0x60 // 20
  220. #define CODE_COLOR_IMAGE 0x80 // 20
  221. #define CODE_PACKED_COLOR_IMAGE 0xA0 // 20
  222. #define CODE_SET_FG_FG_RUN 0xC0 // 10
  223. #define CODE_SET_FG_FG_BG 0xD0 // 10
  224. #define CODE_DITHERED_RUN 0xE0 // 10
  225. #define CODE_MEGA_MEGA_BG_RUN 0xF0
  226. #define CODE_MEGA_MEGA_FG_RUN 0xF1
  227. #define CODE_MEGA_MEGA_FGBG 0xF2
  228. #define CODE_MEGA_MEGA_COLOR_RUN 0xF3
  229. #define CODE_MEGA_MEGA_CLR_IMG 0xF4
  230. #define CODE_MEGA_MEGA_PACKED_CLR 0xF5
  231. #define CODE_MEGA_MEGA_SET_FG_RUN 0xF6
  232. #define CODE_MEGA_MEGA_SET_FGBG 0xF7
  233. #define CODE_MEGA_MEGA_DITHER 0xF8
  234. #define CODE_SPECIAL_FGBG_1 0xF9
  235. #define CODE_SPECIAL_FGBG_2 0xFA
  236. #define CODE_SPECIAL_FGBG_3 0xFB
  237. #define CODE_SPECIAL_FGBG_4 0xFC
  238. #define CODE_WHITE 0xFD
  239. #define CODE_BLACK 0xFE
  240. #define CODE_START_LOSSY 0xFF
  241. #define MAX_LENGTH_ORDER 31
  242. #define MAX_LENGTH_LONG_ORDER 287
  243. #define MAX_LENGTH_ORDER_LITE 15
  244. #define MAX_LENGTH_LONG_ORDER_LITE 271
  245. #define MAX_LENGTH_FGBG_ORDER (31*8)
  246. #define MAX_LENGTH_FGBG_ORDER_LITE (15*8)
  247. #define MAX_LENGTH_LONG_FGBG_ORDER 255
  248. //
  249. // The special FGBG codes that correspond to codes F0-F7
  250. //
  251. #define SPECIAL_FGBG_CODE_1 0x03
  252. #define SPECIAL_FGBG_CODE_2 0x05
  253. #define SPECIAL_FGBG_CODE_3 0x07
  254. #define SPECIAL_FGBG_CODE_4 0x0F
  255. //
  256. // Run types as stored in the run index array
  257. //
  258. #define RUN_BG 1
  259. #define RUN_BG_PEL 2
  260. #define RUN_FG 3
  261. #define RUN_COLOR 4
  262. #define RUN_DITHER 5
  263. #define IMAGE_FGBG 6
  264. #define IMAGE_COLOR 7
  265. #define IMAGE_LOSSY_ODD 8
  266. //
  267. // The following structure contains the results of our intermediate scan of
  268. // the buffer. The offset field contains the expected offset into the
  269. // target buffer following decompression of the associated order and is
  270. // used for self-diagnosis.
  271. //
  272. typedef struct
  273. {
  274. WORD length;
  275. BYTE type;
  276. BYTE fgChar;
  277. }
  278. MATCH;
  279. //
  280. // Function Prototypes
  281. //
  282. // Note that the function compresses the whole bitmap in one call. However
  283. // it performs two cycles internally, once for the first line and once for
  284. // all subsequent lines. pelsPerLine (the number of pels on a row) MUST be
  285. // supplied.
  286. //
  287. // The paremeters should be obvious, save
  288. //
  289. // pLossy = pointer to flag indicating we maydiscard part of incompressible
  290. // data
  291. //
  292. // When lossy compression is enabled any color run carries only half the
  293. // pels and they must be doubled up. (Also the encoder replaces color on
  294. // alternate lines with BG_RUN thus giving a fourfold reduction in the
  295. // data, but the decoder does not need any special code to handle this).
  296. //
  297. // The encoder may decide not to honor the lossy request because the data
  298. // is highly compressible anyway. If it determines this then the lossy
  299. // flag is reset so that the caller may determine whether a subsequent
  300. // non-lossy transmission is required or not.
  301. //
  302. //
  303. //
  304. // Unpack4bpp
  305. //
  306. // Convert a 4bpp bitmap into an 8bpp one
  307. //
  308. void Unpack4bpp(LPBYTE destbuf,
  309. LPBYTE srcbuf,
  310. UINT srclen);
  311. //
  312. // Pack4bpp
  313. //
  314. // Convert an 8bpp bitmap back to 4bpp
  315. //
  316. void Pack4bpp(LPBYTE destbuf,
  317. LPBYTE srcbuf,
  318. UINT srclen);
  319. //
  320. // XORBuffer
  321. //
  322. // Create an XOR image of the input bitmap
  323. //
  324. void XORBuffer(LPBYTE destbuf, LPBYTE srcbuf, UINT srclen, int rowDelta);
  325. //
  326. // CompressV2Int
  327. //
  328. // Internal compresssion function
  329. //
  330. // The work buffer addresses are moved onto the stack, thus eliminating any
  331. // need to use DS to address the default data segment. This allows the
  332. // compiler to perform more general optimizations.
  333. //
  334. UINT CompressV2Int(LPBYTE pSrc,
  335. LPBYTE pDst,
  336. UINT numPels,
  337. UINT bpp,
  338. UINT rowDelta,
  339. UINT dstBufferSize,
  340. LPBOOL pLossy,
  341. LPBYTE nrmbuf,
  342. LPBYTE xorbuf,
  343. MATCH FAR *match);
  344. UINT DecompressV2Int(LPBYTE pSrc,
  345. LPBYTE pDst,
  346. UINT bytes,
  347. UINT bpp,
  348. UINT rowDelta,
  349. LPBYTE nrmbuf);
  350. //
  351. // The Compressed Data header structure.
  352. //
  353. // Rather than add a field to indicate V1 vs V2 compression we use the
  354. // fact that V2 compression treats all the bitmap as main body and sets
  355. // the first row size to zero to distinguish them. I hesitate to do this
  356. // but any bandwidth saving is important.
  357. //
  358. typedef struct _CD_HEADER
  359. {
  360. TSHR_UINT16 cbCompFirstRowSize;
  361. TSHR_UINT16 cbCompMainBodySize;
  362. TSHR_UINT16 cbScanWidth;
  363. TSHR_UINT16 cbUncompressedSize;
  364. } CD_HEADER;
  365. typedef CD_HEADER *PCD_HEADER;
  366. #define IsV2CompressedDataHeader(p) ((p)->cbCompFirstRowSize == 0)
  367. #define SetV2CompressedDataHeader(p) ((p)->cbCompFirstRowSize = 0)
  368. //
  369. // Types of bitmap compression.
  370. //
  371. #ifdef _DEBUG // for assertion
  372. #define RLE_V1 1
  373. #endif
  374. #define RLE_V2 2
  375. //
  376. //
  377. // PROTOTYPES
  378. //
  379. //
  380. #endif // _H_BCD