bitstream.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : wwbitpack *
  23. * *
  24. * $Archive:: /Commando/Code/wwbitpack/bitstream.cpp $*
  25. * *
  26. * Original Author:: Tom Spencer-Smith *
  27. * *
  28. * $Author:: Bhayes $*
  29. * *
  30. * $Modtime:: 2/18/02 10:49p $*
  31. * *
  32. * $Revision:: 4 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "bitstream.h"
  38. #include <string.h> // for strlen
  39. #include <math.h> // for ceil
  40. #include "wwdebug.h"
  41. #include "mathutil.h"
  42. #include "widestring.h"
  43. //-----------------------------------------------------------------------------
  44. BitStreamClass::BitStreamClass() :
  45. cBitPacker(),
  46. UncompressedSizeBytes(0)
  47. {
  48. }
  49. //-----------------------------------------------------------------------------
  50. BitStreamClass& BitStreamClass::operator=(const BitStreamClass& rhs)
  51. {
  52. //
  53. // Call operator for base class
  54. //
  55. cBitPacker::operator= (rhs);
  56. UncompressedSizeBytes = rhs.UncompressedSizeBytes;
  57. return * this;
  58. }
  59. //-----------------------------------------------------------------------------
  60. void BitStreamClass::Add(bool value)
  61. {
  62. if (cEncoderList::Is_Compression_Enabled()) {
  63. Add_Bits(value, 1);
  64. } else {
  65. Add_Bits(value, BIT_DEPTH(bool));
  66. }
  67. UncompressedSizeBytes += BYTE_DEPTH(bool);
  68. }
  69. //-----------------------------------------------------------------------------
  70. bool BitStreamClass::Get(bool & value)
  71. {
  72. ULONG u_value;
  73. if (cEncoderList::Is_Compression_Enabled()) {
  74. Get_Bits(u_value, 1);
  75. } else {
  76. Get_Bits(u_value, BIT_DEPTH(bool));
  77. }
  78. value = (u_value == 1);
  79. return value;
  80. }
  81. //-----------------------------------------------------------------------------
  82. void BitStreamClass::Add_Raw_Data(LPCSTR data, USHORT data_size)
  83. {
  84. WWASSERT(data != NULL);
  85. WWASSERT(data_size >= 0);
  86. for (int i = 0; i < data_size; i++) {
  87. Add(data[i]);
  88. }
  89. }
  90. //-----------------------------------------------------------------------------
  91. void BitStreamClass::Get_Raw_Data(char * buffer, USHORT buffer_size, USHORT data_size)
  92. {
  93. WWASSERT(buffer != NULL);
  94. WWASSERT(data_size >= 0);
  95. WWASSERT(buffer_size >= data_size);
  96. for (int i = 0; i < data_size; i++) {
  97. Get(buffer[i]);
  98. }
  99. }
  100. //-----------------------------------------------------------------------------
  101. void BitStreamClass::Add_Terminated_String(LPCSTR string, bool permit_empty)
  102. {
  103. WWASSERT(string != NULL);
  104. //
  105. // The terminating null is not transmitted.
  106. //
  107. USHORT len = (USHORT) strlen(string);
  108. if (!permit_empty) {
  109. WWASSERT(len > 0);
  110. }
  111. Add(len);
  112. for (int i = 0; i < len; i++) {
  113. Add(string[i]);
  114. }
  115. }
  116. //-----------------------------------------------------------------------------
  117. void BitStreamClass::Get_Terminated_String(char * buffer, USHORT buffer_size, bool permit_empty)
  118. {
  119. WWASSERT(buffer != NULL);
  120. WWASSERT(buffer_size > 0);
  121. USHORT len;
  122. Get(len);
  123. WWASSERT(len < buffer_size);
  124. if (!permit_empty) {
  125. WWASSERT(len > 0);
  126. }
  127. char temp = '?';
  128. int i = 0;
  129. for (i = 0; i < len; i++) {
  130. Get(temp);
  131. if (i < buffer_size - 1) {
  132. buffer[i] = temp;
  133. }
  134. }
  135. // Null-terminate it.
  136. if (i < buffer_size) {
  137. buffer[i] = 0;
  138. } else {
  139. buffer[buffer_size - 1] = 0;
  140. }
  141. }
  142. //-----------------------------------------------------------------------------
  143. void BitStreamClass::Add_Wide_Terminated_String(const WCHAR *string, bool permit_empty)
  144. {
  145. WWASSERT(string != NULL);
  146. //
  147. // The terminating null is not transmitted.
  148. //
  149. USHORT len = (USHORT)wcslen (string);
  150. if (!permit_empty) {
  151. WWASSERT(len > 0 && "Empty string not permitted");
  152. }
  153. Add(len);
  154. for (int i = 0; i < len; i++) {
  155. Add(string[i]);
  156. }
  157. }
  158. //-----------------------------------------------------------------------------
  159. void BitStreamClass::Get_Wide_Terminated_String(WCHAR *buffer, USHORT buffer_len, bool permit_empty)
  160. {
  161. WWASSERT(buffer != NULL);
  162. WWASSERT(buffer_len > 0);
  163. USHORT len;
  164. Get(len);
  165. WWASSERT(len < buffer_len && "String length exceeds provided buffer");
  166. if (!permit_empty) {
  167. WWASSERT(len > 0 && "Empty string not permitted");
  168. }
  169. WCHAR temp = L'?';
  170. int i = 0;
  171. for (i = 0; i < len; i++) {
  172. Get(temp);
  173. if (i < buffer_len - 1) {
  174. buffer[i] = temp;
  175. }
  176. }
  177. if (i < buffer_len - 1) {
  178. buffer[i] = 0; // Null-terminate it.
  179. } else {
  180. buffer[buffer_len-1] = 0;
  181. }
  182. }
  183. //-----------------------------------------------------------------------------
  184. UINT BitStreamClass::Get_Compressed_Size_Bytes() const
  185. {
  186. return (UINT) ceil(Get_Bit_Write_Position() / 8.0f);
  187. }
  188. //-----------------------------------------------------------------------------
  189. UINT BitStreamClass::Get_Compression_Pc() const
  190. {
  191. UINT c_size = Get_Compressed_Size_Bytes();
  192. UINT u_size = Get_Uncompressed_Size_Bytes();
  193. if (cEncoderList::Is_Compression_Enabled()) {
  194. WWASSERT(c_size <= u_size);
  195. } else {
  196. WWASSERT(c_size == u_size);
  197. }
  198. WWASSERT(u_size > 0);
  199. UINT compression_pc = (UINT) cMathUtil::Round(100 * c_size / (float) u_size);
  200. WWASSERT(compression_pc >= 0 && compression_pc <= 100);
  201. return compression_pc;
  202. }