LOADPCX.CPP 7.9 KB

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