WRITEPCX.CPP 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/WRITEPCX.CPP 1 3/03/97 10:26a Joe_bostic $ */
  19. /***************************************************************************
  20. ** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  21. ***************************************************************************
  22. * *
  23. * Project Name : iff *
  24. * *
  25. * File Name : WRITEPCX.CPP *
  26. * *
  27. * Programmer : Julio R. Jerez *
  28. * *
  29. * Start Date : May 2, 1995 *
  30. * *
  31. * Last Update : May 2, 1995 [JRJ] *
  32. * *
  33. *-------------------------------------------------------------------------*
  34. * Functions: *
  35. * int Save_PCX_File (char* name, GraphicViewPortClass& pic, char* palette)*
  36. *= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  37. #include "function.h"
  38. #include "pcx.h"
  39. static void Write_Pcx_ScanLine(FileClass & file, int scansize, char * ptr);
  40. /***************************************************************************
  41. * WRITE_PCX_FILE -- Write the data in ViewPort to a pcx file *
  42. * *
  43. * *
  44. * *
  45. * INPUT: name is a NULL terminated string of the format [xxxx.pcx] *
  46. * pic is a pointer to a GraphicViewPortClass or to a *
  47. * GraphicBufferClass holding the picture. *
  48. * palette is a pointer the the memory block holding the color *
  49. * palette of the picture. *
  50. * *
  51. * OUTPUT: FALSE if the function fails zero otherwise *
  52. * *
  53. * WARNINGS: *
  54. * *
  55. * HISTORY: *
  56. * 05/04/1995 JRJ : Created. *
  57. * 08/01/1995 SKB : Copy the palette so it is not modified. *
  58. * 06/03/1996 JLB : Converted to C++ and file class I/O. *
  59. *=========================================================================*/
  60. static const unsigned char rle_code = 0xC0; // Run code.
  61. static const unsigned char rle_max_run = 0x2F; // Maximum run allowed.
  62. static const unsigned char rle_full_run = (rle_max_run|rle_code); // Full character run.
  63. /***********************************************************************************************
  64. * Write_PCX_File -- Write a PCX file from specified buffer. *
  65. * *
  66. * This routine will take the specified buffer and write out the data as a PCX file to the *
  67. * the file object specified. *
  68. * *
  69. * INPUT: file -- Reference to the file object to write the buffer as a PCX file. *
  70. * *
  71. * pic -- Reference to a graphic buffer that contains the data to be written. *
  72. * *
  73. * palette -- Reference to the palette to be attached to the PCX file as well. *
  74. * *
  75. * OUTPUT: bool; Was there an error? *
  76. * *
  77. * WARNINGS: none *
  78. * *
  79. * HISTORY: *
  80. * 06/03/1996 JLB : Created. *
  81. *=============================================================================================*/
  82. int Write_PCX_File(FileClass & file, GraphicBufferClass & pic, PaletteClass * palette)
  83. {
  84. unsigned char palcopy[256 * sizeof(RGB)];
  85. int VP_Scan_Line;
  86. char * ptr;
  87. RGB * pal;
  88. PCX_HEADER header = {
  89. 10,
  90. 5,
  91. 1,
  92. 8,
  93. 0,
  94. 0,
  95. (short)(pic.Get_Width()-1),
  96. (short)(pic.Get_Height()-1),
  97. (short)(pic.Get_Width()),
  98. (short)(pic.Get_Height()),
  99. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  100. 0,
  101. 1,
  102. (short)pic.Get_Width(),
  103. 1,
  104. {0}
  105. };
  106. /*
  107. ** Open the output file and write out the header information. If the file
  108. ** is already open, then just presume that it is positioned correctly and is
  109. ** open for write.
  110. */
  111. bool open = false;
  112. if (!file.Is_Open()) {
  113. file.Open(WRITE);
  114. open = true;
  115. }
  116. file.Write(&header, sizeof(header));
  117. /*
  118. ** Write out the picture, line by line.
  119. */
  120. VP_Scan_Line = pic.Get_Width() + pic.Get_XAdd();
  121. ptr = (char *)pic.Get_Buffer() ;
  122. ptr += ((pic.Get_YPos() * VP_Scan_Line) + pic.Get_XPos());
  123. for (int line = 0; line < header.height + 1; line++) {
  124. Write_Pcx_ScanLine(file, header.byte_per_line, ptr + line * VP_Scan_Line ) ;
  125. }
  126. /*
  127. ** Special marker for end of RLE data.
  128. */
  129. unsigned char ender = 0x0C;
  130. file.Write(&ender, sizeof(ender));
  131. /*
  132. ** Convert the palette from 6 bit to 8 bit format.
  133. */
  134. memmove(palcopy, palette, sizeof(PaletteClass));
  135. pal = (RGB *)palcopy ;
  136. for (int palindex = 0; palindex < 256; palindex++) {
  137. pal->red = (unsigned char)((pal->red<<2)); // | (pal->red>>6));
  138. pal->green = (unsigned char)((pal->green<<2)); // | (pal->green>>6));
  139. pal->blue = (unsigned char)((pal->blue<<2)); // | (pal->blue>>6));
  140. pal++;
  141. }
  142. /*
  143. ** Write the palette out.
  144. */
  145. file.Write(palcopy, sizeof(palcopy));
  146. /*
  147. ** Close the file (if necessary) and exit with no error flag.
  148. */
  149. if (open) {
  150. file.Close();
  151. }
  152. return(false);
  153. }
  154. /***********************************************************************************************
  155. * Write_Pcx_ScanLine -- Writes a PCX scanline. *
  156. * *
  157. * Writes out a PCX scanline using RLE compression. *
  158. * *
  159. * INPUT: file -- Reference to the file to write the scan line to. *
  160. * *
  161. * scansize -- The number of bytes to compress (write). *
  162. * *
  163. * ptr -- Pointer to the data to compress (write). *
  164. * *
  165. * OUTPUT: none *
  166. * *
  167. * WARNINGS: none *
  168. * *
  169. * HISTORY: *
  170. * 05/04/1995 JRJ : Created. *
  171. * 06/03/1996 JLB : Converted to C++ and file class I/O. *
  172. *=============================================================================================*/
  173. static void Write_Pcx_ScanLine(FileClass & file, int scansize, char * ptr)
  174. {
  175. unsigned char last = *ptr;
  176. unsigned char rle=1;
  177. unsigned char c;
  178. for (int i = 1; i < scansize; i++) {
  179. unsigned char color = (unsigned char)(0xff & * ++ptr);
  180. if (color == last) {
  181. rle++;
  182. if (rle == rle_max_run) {
  183. file.Write(&rle_full_run, sizeof(rle_full_run));
  184. file.Write(&color, sizeof(color));
  185. rle = 0 ;
  186. }
  187. } else {
  188. if (rle) {
  189. if (rle == 1 && (rle_code != (rle_code & last))) {
  190. file.Write(&last, sizeof(last));
  191. } else {
  192. c = (unsigned char)(rle | rle_code);
  193. file.Write(&c, sizeof(c));
  194. file.Write(&last, sizeof(last));
  195. }
  196. }
  197. last = color ;
  198. rle = 1 ;
  199. }
  200. }
  201. if (rle) {
  202. if (rle == 1 && ( rle_code != (rle_code & last))) {
  203. file.Write(&last, sizeof(last));
  204. } else {
  205. c = (unsigned char)(rle | rle_code);
  206. file.Write(&c, sizeof(c));
  207. file.Write(&last, sizeof(last));
  208. }
  209. }
  210. }