/StormLib/stormlib/huffman/huff.h

http://ghostcb.googlecode.com/ · C Header · 143 lines · 83 code · 26 blank · 34 comment · 0 complexity · a29d763e8af1cb9568c6bc79c9b1073f MD5 · raw file

  1. /*****************************************************************************/
  2. /* huffman.h Copyright (c) Ladislav Zezula 2003 */
  3. /*---------------------------------------------------------------------------*/
  4. /* Description : */
  5. /*---------------------------------------------------------------------------*/
  6. /* Date Ver Who Comment */
  7. /* -------- ---- --- ------- */
  8. /* xx.xx.xx 1.00 Lad The first version of huffman.h */
  9. /* 03.05.03 2.00 Lad Added compression */
  10. /* 08.12.03 2.01 Dan High-memory handling (> 0x80000000) */
  11. /*****************************************************************************/
  12. #ifndef __HUFFMAN_H__
  13. #define __HUFFMAN_H__
  14. #include "../StormPort.h"
  15. //-----------------------------------------------------------------------------
  16. // Defines
  17. #define INSERT_ITEM 1
  18. #define SWITCH_ITEMS 2 // Switch the item1 and item2
  19. #define PTR_NOT(ptr) (THTreeItem *)(~(DWORD_PTR)(ptr))
  20. #define PTR_PTR(ptr) ((THTreeItem *)(ptr))
  21. #define PTR_INT(ptr) (INT_PTR)(ptr)
  22. #ifndef NULL
  23. #define NULL 0
  24. #endif
  25. //-----------------------------------------------------------------------------
  26. // Structures and classes
  27. // Input stream for Huffmann decompression
  28. class TInputStream
  29. {
  30. public:
  31. unsigned long GetBit();
  32. unsigned long Get7Bits();
  33. unsigned long Get8Bits();
  34. void SkipBits(unsigned int BitCount);
  35. unsigned char * pbInBuffer; // 00 - Input data
  36. unsigned long BitBuffer; // 04 - Input bit buffer
  37. unsigned int BitCount; // 08 - Number of bits remaining in 'dwBitBuff'
  38. };
  39. // Output stream for Huffmann compression
  40. class TOutputStream
  41. {
  42. public:
  43. void PutBits(unsigned long dwBuff, unsigned int nPutBits);
  44. unsigned char * pbOutBuffer; // 00 : Output buffer
  45. unsigned long dwOutSize; // 04 : Size of output buffer
  46. unsigned char * pbOutPos; // 08 : Current output position
  47. unsigned long dwBitBuff; // 0C : Bit buffer
  48. unsigned long nBits; // 10 : Number of bits in the bit buffer
  49. };
  50. // Huffmann tree item (?)
  51. struct THTreeItem
  52. {
  53. public:
  54. THTreeItem * Call1501DB70(THTreeItem * pLast);
  55. THTreeItem * GetPrevItem(LONG_PTR value);
  56. void ClearItemLinks();
  57. void RemoveItem();
  58. THTreeItem * next; // 00 - Pointer to next THTreeItem
  59. THTreeItem * prev; // 04 - Pointer to prev THTreeItem (< 0 if none)
  60. unsigned long dcmpByte; // 08 - Index of this item in item pointer array, decompressed byte value
  61. unsigned long byteValue; // 0C - Some byte value
  62. THTreeItem * parent; // 10 - Pointer to parent THTreeItem (NULL if none)
  63. THTreeItem * child; // 14 - Pointer to child THTreeItem
  64. int addressMultiplier; // -1 if object on negative address (>0x80000000), +1 if positive
  65. };
  66. // Structure used for quick decompress. The 'bitCount' contains number of bits
  67. // and byte value contains result decompressed byte value.
  68. // After each walk through Huffman tree are filled all entries which are
  69. // multiplies of number of bits loaded from input stream. These entries
  70. // contain number of bits and result value. At the next 7 bits is tested this
  71. // structure first. If corresponding entry found, decompression routine will
  72. // not walk through Huffman tree and directly stores output byte to output stream.
  73. struct TQDecompress
  74. {
  75. unsigned long offs00; // 00 - 1 if resolved
  76. unsigned long nBits; // 04 - Bit count
  77. union
  78. {
  79. unsigned long dcmpByte; // 08 - Byte value for decompress (if bitCount <= 7)
  80. THTreeItem * pItem; // 08 - THTreeItem (if number of bits is greater than 7
  81. };
  82. };
  83. // Structure for Huffman tree (Size 0x3674 bytes). Because I'm not expert
  84. // for the decompression, I do not know actually if the class is really a Hufmann
  85. // tree. If someone knows the decompression details, please let me know
  86. class THuffmannTree
  87. {
  88. public:
  89. THuffmannTree();
  90. void InitTree(bool bCompression);
  91. void BuildTree(unsigned int nCmpType);
  92. // void ModifyTree(unsigned long dwIndex);
  93. // void UninitTree();
  94. // void Call15007010(Bit32 dwInLength, THTreeItem * item);
  95. THTreeItem * Call1500E740(unsigned int nValue);
  96. void Call1500E820(THTreeItem * pItem);
  97. unsigned int DoCompression(TOutputStream * os, unsigned char * pbInBuffer, int nInLength, int nCmpType);
  98. unsigned int DoDecompression(unsigned char * pbOutBuffer, unsigned int dwOutLength, TInputStream * is);
  99. unsigned long bIsCmp0; // 0000 - 1 if compression type 0
  100. unsigned long offs0004; // 0004 - Some flag
  101. THTreeItem items0008[0x203]; // 0008 - HTree items
  102. //- Sometimes used as HTree item -----------
  103. THTreeItem * pItem3050; // 3050 - Always NULL (?)
  104. THTreeItem * pItem3054; // 3054 - Pointer to Huffman tree item
  105. THTreeItem * pItem3058; // 3058 - Pointer to Huffman tree item (< 0 if invalid)
  106. //- Sometimes used as HTree item -----------
  107. THTreeItem * pItem305C; // 305C - Usually NULL
  108. THTreeItem * pFirst; // 3060 - Pointer to top (first) Huffman tree item
  109. THTreeItem * pLast; // 3064 - Pointer to bottom (last) Huffman tree item (< 0 if invalid)
  110. unsigned long nItems; // 3068 - Number of used HTree items
  111. //-------------------------------------------
  112. THTreeItem * items306C[0x102]; // 306C - THTreeItem pointer array
  113. TQDecompress qd3474[0x80]; // 3474 - Array for quick decompression
  114. int addressMultiplier; // -1 if object on negative address (>0x80000000), +1 if positive
  115. static unsigned char Table1502A630[];// Some table
  116. };
  117. #endif // __HUFFMAN_H__