WRITEPCX.CPP 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /***************************************************************************
  15. ** 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 **
  16. ***************************************************************************
  17. * *
  18. * Project Name : iff *
  19. * *
  20. * File Name : WRITEPCX.CPP *
  21. * *
  22. * Programmer : Julio R. Jerez *
  23. * *
  24. * Start Date : May 2, 1995 *
  25. * *
  26. * Last Update : May 2, 1995 [JRJ] *
  27. * *
  28. *-------------------------------------------------------------------------*
  29. * Functions: *
  30. * int Save_PCX_File (char* name, GraphicViewPortClass& pic, char* palette)*
  31. *= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  32. #include <wwlib32.h>
  33. #include "filepcx.h"
  34. #include <wwmem.h>
  35. static void Write_Pcx_ScanLine ( int file_handle , int scansize , unsigned char * ptr );
  36. /***************************************************************************
  37. * WRITE_PCX_FILE -- Write the data in ViewPort to a pcx file *
  38. * *
  39. * *
  40. * *
  41. * INPUT: name is a NULL terminated string of the fromat [xxxx.pcx] *
  42. * pic is a pointer to a GraphicViewPortClass or to a *
  43. * GraphicBufferClass holding the picture. *
  44. * palette is a pointer the the memry block holding the color * *
  45. * palette of the picture. *
  46. * *
  47. * OUTPUT: FALSE if the function fails zero otherwise *
  48. * *
  49. * WARNINGS: *
  50. * *
  51. * HISTORY: *
  52. * 05/04/1995 JRJ : Created. *
  53. * 08/01/1995 SKB : Copy the palette so it is not modified. *
  54. *=========================================================================*/
  55. int Write_PCX_File (char* name, GraphicViewPortClass& pic, unsigned char* palette )
  56. {
  57. unsigned char palcopy[256 * 3];
  58. unsigned i ;
  59. //unsigned width ;
  60. int file_handle ;
  61. int VP_Scan_Line ;
  62. char * ptr ;
  63. RGB * pal ;
  64. GraphicBufferClass * Graphic_Buffer ;
  65. PCX_HEADER header = { 10 , 5 , 1 , 8 , 0 , 0 , 319 , 199 ,
  66. 320 , 200 , { 0 } , 0 , 1 , 320 , 1 , {0} } ;
  67. // Open file name
  68. file_handle = Open_File ( name , WRITE ) ;
  69. if ( file_handle == WW_ERROR ) return FALSE ;
  70. header.width = pic.Get_Width() - 1 ;
  71. header.height = pic.Get_Height() - 1 ;
  72. header.byte_per_line = pic.Get_Width() ;
  73. Write_File ( file_handle, & header , sizeof (PCX_HEADER)) ;
  74. VP_Scan_Line = pic.Get_Width() + pic.Get_XAdd();
  75. Graphic_Buffer = pic.Get_Graphic_Buffer() ;
  76. ptr = ( char * ) Graphic_Buffer->Get_Buffer() ;
  77. ptr += ( (pic.Get_YPos() * VP_Scan_Line) + pic.Get_XPos() );
  78. for ( i = 0 ; i < (unsigned)header.height + 1 ; i ++ )
  79. Write_Pcx_ScanLine ( file_handle , header.byte_per_line, (unsigned char*)ptr + i * VP_Scan_Line ) ;
  80. Mem_Copy(palette, palcopy, 256 * 3);
  81. pal = ( RGB * ) palcopy ;
  82. for ( i = 0 ; i < 256 ; i ++ ) {
  83. pal -> red <<= 2 ;
  84. pal -> green <<= 2 ;
  85. pal -> blue <<= 2 ;
  86. pal ++ ;
  87. }
  88. i = 0x0c ;
  89. Write_File ( file_handle, & i , 1 ) ;
  90. Write_File ( file_handle, palcopy , 256 * sizeof (RGB) ) ;
  91. Close_File (file_handle) ;
  92. return 0 ;
  93. }
  94. /***************************************************************************
  95. * WRITE_PCX_SCANLINE -- function to write a single pcx scanline to a file *
  96. * *
  97. * *
  98. * INPUT: *
  99. * *
  100. * OUTPUT: *
  101. * *
  102. * WARNINGS: *
  103. * *
  104. * HISTORY: *
  105. * 05/04/1995 JRJ : Created. *
  106. *=========================================================================*/
  107. #define POOL_SIZE 2048
  108. #define WRITE_CHAR(x) { \
  109. * file_ptr ++ = x ; \
  110. if ( file_ptr >= & pool [ POOL_SIZE ] ) { \
  111. Write_File ( file_handle, pool , POOL_SIZE ) ; \
  112. file_ptr = pool ; \
  113. } }
  114. void Write_Pcx_ScanLine ( int file_handle , int scansize , unsigned char * ptr )
  115. {
  116. unsigned i ;
  117. unsigned rle ;
  118. unsigned color ;
  119. unsigned last ;
  120. unsigned char * file_ptr ;
  121. unsigned char pool [ POOL_SIZE ] ;
  122. file_ptr = pool ;
  123. last = * ptr ;
  124. rle = 1 ;
  125. for ( i = 1 ; i < (unsigned)scansize ; i ++ ) {
  126. color = 0xff & * ++ ptr ;
  127. if ( color == last ) {
  128. rle ++ ;
  129. if ( rle == 63 ) {
  130. WRITE_CHAR ( 255 ) ;
  131. WRITE_CHAR ( color ) ;
  132. rle = 0 ;
  133. }
  134. } else {
  135. if ( rle ) {
  136. if ( rle == 1 && ( 192 != ( 192 & last ))) {
  137. WRITE_CHAR ( last ) ;
  138. } else {
  139. WRITE_CHAR ( rle | 192 ) ;
  140. WRITE_CHAR ( last ) ;
  141. }
  142. }
  143. last = color ;
  144. rle = 1 ;
  145. }
  146. }
  147. if ( rle ) {
  148. if ( rle == 1 && ( 192 != ( 192 & last ))) {
  149. WRITE_CHAR ( last ) ;
  150. } else {
  151. WRITE_CHAR ( rle | 192 ) ;
  152. WRITE_CHAR ( last) ;
  153. }
  154. }
  155. Write_File ( file_handle, pool , ( int ) file_ptr - ( int ) pool ) ;
  156. }