LOADPCX.CPP 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 : LOADPCX.CPP *
  25. * *
  26. * Programmer : Julio R. Jerez *
  27. * *
  28. * Start Date : May 2, 1995 *
  29. * *
  30. * Last Update : May 3, 1995 [JRJ] *
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * GraphicBufferClass* Read_PCX_File (char* name, void *Buff, long size ); *
  35. * int Get_PCX_Palette (char * name, void& palette ) *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include <wwlib32.h>
  38. #include "filepcx.h"
  39. /***************************************************************************
  40. * READ_PCX_FILE -- read a pcx file into a Graphic Buffer *
  41. * *
  42. * GraphicBufferClass* Read_PCX_File (char* name, char* palette ,void *Buff, long size ); *
  43. * *
  44. * *
  45. * INPUT: name is a NULL terminated string of the fromat [xxxx.pcx] *
  46. * palette is optional, if palette != NULL the the color palette of *
  47. * the pcx file will be place in the memory block pointed *
  48. * by palette. *
  49. * Buff is optinal, if Buff == NULL a new memory Buffer *
  50. * will be allocated, otherwise the file will be placed *
  51. * at location pointd by Buffer; *
  52. * Size is the size in bytes of the memory block pointed by Buff *
  53. * is also optional;
  54. * *
  55. * OUTPUT: on succes a pointer to a GraphicBufferClass cointaining the *
  56. * pcx file, NULL othewise. *
  57. * *
  58. * WARNINGS: *
  59. * *
  60. * HISTORY: *
  61. * 05/03/1995 JRJ : Created. *
  62. *=========================================================================*/
  63. #define POOL_SIZE 2048
  64. #define READ_CHAR() *file_ptr++ ; \
  65. if ( file_ptr >= & pool [ POOL_SIZE ] ) { \
  66. Read_File ( file_handle, pool , POOL_SIZE ) ; \
  67. file_ptr = pool ; \
  68. }
  69. GraphicBufferClass* Read_PCX_File(char* name, char* Palette, void *Buff, long Size)
  70. {
  71. unsigned i , j ;
  72. unsigned rle ;
  73. unsigned color ;
  74. unsigned scan_pos ;
  75. char * file_ptr ;
  76. int width ;
  77. int height ;
  78. int file_handle ;
  79. char * buffer ;
  80. PCX_HEADER header ;
  81. RGB * pal ;
  82. char pool [ POOL_SIZE ] ;
  83. GraphicBufferClass * pic ;
  84. // Open file name
  85. file_handle = Open_File ( name , READ ) ;
  86. if ( file_handle == WW_ERROR ) return NULL ;
  87. Read_File ( file_handle, & header , sizeof (PCX_HEADER)) ;
  88. if ( header.id != 10 && header.version != 5 &&
  89. header.pixelsize != 8 ) return NULL ;
  90. width = header.width - header.x + 1 ;
  91. height = header.height - header.y + 1 ;
  92. if ( Buff ) {
  93. buffer = ( char * ) Buff;
  94. i = Size / width;
  95. height = MIN ( i - 1, height);
  96. pic = new GraphicBufferClass( width, height, buffer ,Size);
  97. if ( !(pic && pic->Get_Buffer()))return NULL ;
  98. } else {
  99. pic = new GraphicBufferClass( width, height, NULL, width*(height+4));
  100. if ( !(pic && pic->Get_Buffer()))return NULL ;
  101. }
  102. buffer = (char *) pic->Get_Buffer() ;
  103. file_ptr = pool ;
  104. Read_File ( file_handle, pool , POOL_SIZE ) ;
  105. if ( header.byte_per_line != width )
  106. for ( scan_pos = j = 0 ; j < height ; j ++, scan_pos += width ) {
  107. for ( i = 0 ; i < width ; ) {
  108. rle = READ_CHAR ();
  109. if ( rle > 192 ) {
  110. rle -= 192 ;
  111. color = READ_CHAR (); ;
  112. memset ( buffer + scan_pos + i , color , rle ) ;
  113. i += rle ;
  114. } else * ( buffer + scan_pos + i ++ ) = (char)rle ;
  115. }
  116. if ( i == width )
  117. rle = READ_CHAR () ;
  118. // if ( rle > 192 ) rle = READ_CHAR ();
  119. }
  120. else for ( i = 0 ; i < width * height ; ) {
  121. rle = READ_CHAR ();
  122. rle &= 0xff;
  123. if ( rle > 192 ) {
  124. rle -= 192 ;
  125. color = READ_CHAR ();
  126. memset ( buffer + i , color , rle ) ;
  127. i += rle ;
  128. } else * ( buffer + i ++ ) = (char)rle ;
  129. }
  130. if ( Palette ) {
  131. //Seek_File ( file_handle , - 256 * sizeof ( RGB ) , SEEK_END ) ;
  132. Seek_File ( file_handle , 256 * sizeof ( RGB ) , SEEK_END ) ;
  133. Read_File ( file_handle, Palette , 256L * sizeof ( RGB )) ;
  134. pal = ( RGB * ) Palette ;
  135. for ( i = 0 ; i < 256 ; i ++ ) {
  136. pal -> red >>= 2 ;
  137. pal -> green >>= 2 ;
  138. pal -> blue >>= 2 ;
  139. pal ++ ;
  140. }
  141. }
  142. Close_File (file_handle) ;
  143. return pic ;
  144. }
  145. /***************************************************************************
  146. * READ_PCX_FILE -- read a pcx file into a Graphic Buffer *
  147. * *
  148. * GraphicBufferClass* Read_PCX_File (char* name, BufferClass& Buff, *
  149. * char* palette) * *
  150. * *
  151. * *
  152. * INPUT: name is a NULL terminated string of the fromat [xxxx.pcx] *
  153. * Buff is a pointer to a BufferClass the will hold the pcx file *
  154. * at location pointd by Buffer; *
  155. * palette is optional, if palette != NULL the the color palette of *
  156. * the pcx file will be place in the memory block pointed *
  157. * by palette. *
  158. * *
  159. * OUTPUT: on succes a pointer to a GraphicBufferClass cointaining the *
  160. * pcx file, NULL othewise. *
  161. * *
  162. * WARNINGS: *
  163. * *
  164. * HISTORY: *
  165. * 05/03/1995 JRJ : Created. *
  166. *=========================================================================*/
  167. GraphicBufferClass* Read_PCX_File (char* name, BufferClass& Buff,char* palette)
  168. {
  169. return Read_PCX_File(name, palette, (void*)Buff.Get_Buffer(), Buff.Get_Size());
  170. }