WRITEPCX.CPP 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /***************************************************************************
  19. ** 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 **
  20. ***************************************************************************
  21. * *
  22. * Project Name : iff *
  23. * *
  24. * File Name : WRITEPCX.CPP *
  25. * *
  26. * Programmer : Julio R. Jerez *
  27. * *
  28. * Start Date : May 2, 1995 *
  29. * *
  30. * Last Update : May 2, 1995 [JRJ] *
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * int Save_PCX_File (char* name, GraphicViewPortClass& pic, char* palette)*
  35. *= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  36. #include <wwlib32.h>
  37. #include "filepcx.h"
  38. #include <wwmem.h>
  39. static void Write_Pcx_ScanLine ( int file_handle , 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 fromat [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 memry 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. *=========================================================================*/
  59. int Write_PCX_File (char* name, GraphicViewPortClass& pic, unsigned char* palette )
  60. {
  61. unsigned char palcopy[256 * 3];
  62. unsigned i ;
  63. unsigned width ;
  64. int file_handle ;
  65. int VP_Scan_Line ;
  66. char * ptr ;
  67. RGB * pal ;
  68. GraphicBufferClass * Graphic_Buffer ;
  69. PCX_HEADER header = { 10 , 5 , 1 , 8 , 0 , 0 , 319 , 199 ,
  70. 320 , 200 , { 0 } , 0 , 1 , 320 , 1 , {0} } ;
  71. // Open file name
  72. file_handle = Open_File ( name , WRITE ) ;
  73. if ( file_handle == ERROR ) return FALSE ;
  74. header.width = pic.Get_Width() - 1 ;
  75. header.height = pic.Get_Height() - 1 ;
  76. header.byte_per_line = pic.Get_Width() ;
  77. Write_File ( file_handle, & header , sizeof (PCX_HEADER)) ;
  78. VP_Scan_Line = pic.Get_Width() + pic.Get_XAdd();
  79. Graphic_Buffer = pic.Get_Graphic_Buffer() ;
  80. ptr = ( char * ) Graphic_Buffer->Get_Buffer() ;
  81. ptr += ( (pic.Get_YPos() * VP_Scan_Line) + pic.Get_XPos() );
  82. for ( i = 0 ; i < header.height + 1 ; i ++ )
  83. Write_Pcx_ScanLine ( file_handle , header.byte_per_line, ptr + i * VP_Scan_Line ) ;
  84. Mem_Copy(palette, palcopy, 256 * 3);
  85. pal = ( RGB * ) palcopy ;
  86. for ( i = 0 ; i < 256 ; i ++ ) {
  87. pal -> red <<= 2 ;
  88. pal -> green <<= 2 ;
  89. pal -> blue <<= 2 ;
  90. pal ++ ;
  91. }
  92. i = 0x0c ;
  93. Write_File ( file_handle, & i , 1 ) ;
  94. Write_File ( file_handle, palcopy , 256 * sizeof (RGB) ) ;
  95. Close_File (file_handle) ;
  96. return 0 ;
  97. }
  98. /***************************************************************************
  99. * WRITE_PCX_SCANLINE -- function to write a single pcx scanline to a file *
  100. * *
  101. * *
  102. * INPUT: *
  103. * *
  104. * OUTPUT: *
  105. * *
  106. * WARNINGS: *
  107. * *
  108. * HISTORY: *
  109. * 05/04/1995 JRJ : Created. *
  110. *=========================================================================*/
  111. #define POOL_SIZE 2048
  112. #define WRITE_CHAR(x) { \
  113. * file_ptr ++ = x ; \
  114. if ( file_ptr >= & pool [ POOL_SIZE ] ) { \
  115. Write_File ( file_handle, pool , POOL_SIZE ) ; \
  116. file_ptr = pool ; \
  117. } }
  118. void Write_Pcx_ScanLine ( int file_handle , int scansize , char * ptr )
  119. {
  120. unsigned i ;
  121. unsigned rle ;
  122. unsigned color ;
  123. unsigned last ;
  124. char * file_ptr ;
  125. char pool [ POOL_SIZE ] ;
  126. file_ptr = pool ;
  127. last = * ptr ;
  128. rle = 1 ;
  129. for ( i = 1 ; i < scansize ; i ++ ) {
  130. color = 0xff & * ++ ptr ;
  131. if ( color == last ) {
  132. rle ++ ;
  133. if ( rle == 63 ) {
  134. WRITE_CHAR ( 255 ) ;
  135. WRITE_CHAR ( color ) ;
  136. rle = 0 ;
  137. }
  138. } else {
  139. if ( rle ) {
  140. if ( rle == 1 && ( 192 != ( 192 & last ))) {
  141. WRITE_CHAR ( last ) ;
  142. } else {
  143. WRITE_CHAR ( rle | 192 ) ;
  144. WRITE_CHAR ( last ) ;
  145. }
  146. }
  147. last = color ;
  148. rle = 1 ;
  149. }
  150. }
  151. if ( rle ) {
  152. if ( rle == 1 && ( 192 != ( 192 & last ))) {
  153. WRITE_CHAR ( last ) ;
  154. } else {
  155. WRITE_CHAR ( rle | 192 ) ;
  156. WRITE_CHAR ( last) ;
  157. }
  158. }
  159. Write_File ( file_handle, pool , ( int ) file_ptr - ( int ) pool ) ;
  160. }