rle.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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 : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/Library/RLE.cpp $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 9/24/98 10:05a $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * RLEEngine::Compress -- Compresses a sequence of bytes. *
  35. * RLEEngine::Decompress -- Decompress a sequence of RLE compressed bytes. *
  36. * RLEEngine::Line_Compress -- Compress a line of data. *
  37. * RLEEngine::Line_Decompress -- Decompresses a line-compressed RLE data sequence. *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #include "always.h"
  40. #include "rle.h"
  41. #include <assert.h>
  42. #include <stdlib.h>
  43. /***********************************************************************************************
  44. * RLEEngine::Compress -- Compresses a sequence of bytes. *
  45. * *
  46. * This routine will compress the sequence of bytes specified by using RLE compression. *
  47. * *
  48. * INPUT: source -- Pointer to the data to be compressed. *
  49. * *
  50. * dest -- Pointer to the buffer that will hold the compressed data. *
  51. * *
  52. * length -- The length of the data to compress. *
  53. * *
  54. * OUTPUT: Returns with the number of bytes long for the compressed data. *
  55. * *
  56. * WARNINGS: The compressed data may, in rare instances, be larger than the data it was *
  57. * compressing. The worst case is 33% larger. Keep that in mind when supplying *
  58. * the destination buffer to this routine. *
  59. * *
  60. * HISTORY: *
  61. * 04/17/1997 JLB : Created. *
  62. *=============================================================================================*/
  63. int RLEEngine::Compress(void const * source, void * dest, int length) const
  64. {
  65. assert(source != NULL);
  66. assert(length > 0);
  67. int outlen = 0;
  68. unsigned char const * sptr = (unsigned char const *) source;
  69. unsigned char * dptr = (unsigned char *)dest;
  70. while (length > 0) {
  71. /*
  72. ** Examine each source byte. If the byte is zero (transparent), then
  73. ** it is recorded as a run. Otherwise it is output without translation.
  74. */
  75. if (*sptr == '\0') {
  76. /*
  77. ** Count the number of transparent pixels in this run.
  78. */
  79. int runcount = 0;
  80. while (sptr[runcount] == '\0' && runcount <= length) {
  81. runcount++;
  82. }
  83. /*
  84. ** Limit the run to 255 characters maximum.
  85. */
  86. runcount = MIN(runcount, 255);
  87. if (dptr != NULL) {
  88. *dptr++ = '\0';
  89. *dptr++ = (unsigned char)runcount;
  90. }
  91. outlen += 2;
  92. sptr += runcount;
  93. length -= runcount;
  94. } else {
  95. /*
  96. ** Store the raw byte without any translation.
  97. */
  98. if (dptr != NULL) {
  99. *dptr++ = *sptr++;
  100. } else {
  101. sptr++;
  102. }
  103. outlen++;
  104. length--;
  105. }
  106. }
  107. /*
  108. ** Return with the number of compressed output bytes.
  109. */
  110. return(outlen);
  111. }
  112. /***********************************************************************************************
  113. * RLEEngine::Line_Compress -- Compress a line of data. *
  114. * *
  115. * This routine will compress a sequence of bytes and store the length of the compressed *
  116. * data at the beginning of the output buffer. By encoding the compressed size in this *
  117. * fashion, it is possible to build a sequence of compressed bytes (such as with a sprite) *
  118. * so that each sequence can be quickly traversed. *
  119. * *
  120. * INPUT: source -- Pointer to the source data to be compressed. *
  121. * *
  122. * dest -- Pointer to the destination buffer that will hold the length value and *
  123. * the compressed data. *
  124. * *
  125. * length -- The number of source bytes to compress. *
  126. * *
  127. * OUTPUT: Returns with the number of bytes stored into the output buffer. *
  128. * *
  129. * WARNINGS: The output data could be larger than the source data by as much as 33% + 2 *
  130. * *
  131. * HISTORY: *
  132. * 04/17/1997 JLB : Created. *
  133. *=============================================================================================*/
  134. int RLEEngine::Line_Compress(void const * source, void * dest, int length) const
  135. {
  136. assert(source != NULL);
  137. assert(length > 0);
  138. /*
  139. ** If an output buffer was specified, then the data is actually compressed
  140. ** into the buffer.
  141. */
  142. if (dest != NULL) {
  143. unsigned short * sizeptr = (unsigned short *)dest;
  144. int complen = Compress(source, sizeptr+1, length) + sizeof(short);
  145. *sizeptr = (unsigned short)complen;
  146. return(complen);
  147. }
  148. /*
  149. ** Since no output buffer was specifed, this call merely determins how
  150. ** many bytes would be consumed in the output buffer.
  151. */
  152. return(Compress(source, NULL, length) + sizeof(short));
  153. }
  154. /***********************************************************************************************
  155. * RLEEngine::Decompress -- Decompress a sequence of RLE compressed bytes. *
  156. * *
  157. * This will decompress a sequence of RLE compressed bytes. *
  158. * *
  159. * INPUT: source -- Pointer to the RLE compressed data. *
  160. * *
  161. * dest -- Pointer to the buffer that will hold the uncompressed data. *
  162. * *
  163. * length -- The length of the source RLE data to process. *
  164. * *
  165. * OUTPUT: Returns with the actual number of bytes stored into the output buffer. *
  166. * *
  167. * WARNINGS: The output buffer must be large enough to hold the decompressed data. This *
  168. * could be as much as 128 times larger than the source RLE data. *
  169. * *
  170. * HISTORY: *
  171. * 04/17/1997 JLB : Created. *
  172. *=============================================================================================*/
  173. int RLEEngine::Decompress(void const * source, void * dest, int length) const
  174. {
  175. assert(source != NULL);
  176. assert(dest != NULL);
  177. assert(length > 0);
  178. unsigned char * dptr = (unsigned char *)dest;
  179. unsigned char const * sptr = (unsigned char const *)source;
  180. /*
  181. ** Process the RLE data normally.
  182. */
  183. while (length > 0) {
  184. /*
  185. ** Detect if a zero-run code is present. If so, then dump the desired
  186. ** number of bytes. Otherwise output the pixel in untranslated form.
  187. */
  188. unsigned char value = *sptr++;
  189. length--;
  190. if (value == '\0') {
  191. int outlen = *sptr++;
  192. length--;
  193. while (outlen > 0) {
  194. *dptr++ = '\0';
  195. outlen--;
  196. }
  197. } else {
  198. *dptr++ = value;
  199. }
  200. }
  201. /*
  202. ** Return with the number of bytes stored into the output buffer.
  203. */
  204. return(dptr - (unsigned char const *)dest);
  205. }
  206. /***********************************************************************************************
  207. * RLEEngine::Line_Decompress -- Decompresses a line-compressed RLE data sequence. *
  208. * *
  209. * This routine is the counterpart to Line_Compress. It will take a compressed line and *
  210. * fully decompress it into the destination buffer. *
  211. * *
  212. * INPUT: source -- The pointer to the line compressed RLE data. *
  213. * *
  214. * dest -- Pointer to the destination buffer that will hold the decompressed *
  215. * data. *
  216. * *
  217. * OUTPUT: Returns with the number of bytes stored into the destination buffer. *
  218. * *
  219. * WARNINGS: none *
  220. * *
  221. * HISTORY: *
  222. * 04/17/1997 JLB : Created. *
  223. *=============================================================================================*/
  224. int RLEEngine::Line_Decompress(void const * source, void * dest) const
  225. {
  226. assert(source != NULL);
  227. assert(dest != NULL);
  228. unsigned short const * sptr = (unsigned short const *)source;
  229. int datalen = *sptr++;
  230. /*
  231. ** Process the RLE data normally.
  232. */
  233. return(Decompress(sptr, dest, datalen - sizeof(short)));
  234. }