Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
pcx.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(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 S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : Command & Conquer *
24 * *
25 * $Archive:: /Commando/Code/Library/PCX.cpp $*
26 * *
27 * $Author:: Greg_h $*
28 * *
29 * $Modtime:: 9/28/98 12:06p $*
30 * *
31 * $Revision:: 2 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37#include "always.h"
38#include "pcx.h"
39#include <stdlib.h>
40
41
42/***************************************************************************
43 * READ_PCX_FILE -- read a pcx file into a Graphic Buffer *
44 * *
45 * GraphicBufferClass* Read_PCX_File (char* name, char* palette,void *Buff, long size ); *
46 * *
47 * *
48 * INPUT: name is a NULL terminated string of the format [xxxx.pcx] *
49 * palette is optional, if palette != NULL the the color palette of *
50 * the pcx file will be place in the memory block pointed *
51 * by palette. *
52 * Buff is optional, if Buff == NULL a new memory Buffer *
53 * will be allocated, otherwise the file will be placed *
54 * at location pointed by Buffer; *
55 * Size is the size in bytes of the memory block pointed by Buff *
56 * is also optional; * *
57 * OUTPUT: on success a pointer to a GraphicBufferClass containing the *
58 * pcx file, NULL otherwise. *
59 * *
60 * WARNINGS: *
61 * Appears to be a comment-free zone *
62 * *
63 * HISTORY: *
64 * 05/03/1995 JRJ : Created. *
65 * 04/30/1996 ST : Tidied up and modified to use CCFileClass *
66 *=========================================================================*/
67#define POOL_SIZE 2048
68#define READ_CHAR() *file_ptr++ ; \
69 if ( file_ptr >= & pool [ POOL_SIZE ] ) { \
70 file_handle.Read (pool, POOL_SIZE ); \
71 file_ptr = pool ; \
72 }
73#define READ_CHARx() *file_ptr++ ; \
74 if ( file_ptr >= & pool [ POOL_SIZE ] ) { \
75 file_handle.Read (pool, POOL_SIZE ); \
76 }
77
78
79Surface * Read_PCX_File(FileClass & file_handle, PaletteClass * palette, void * Buff, long Size)
80{
81 unsigned i, j;
82 unsigned rle;
83 unsigned color;
84 unsigned scan_pos;
85 char *file_ptr;
86 unsigned width;
87 unsigned height;
88 char *buffer;
89 PCX_HEADER header;
90 char pool [POOL_SIZE];
91 BSurface * pic;
92
93 if (!file_handle.Is_Available()) return (NULL);
94
95 file_handle.Open(FileClass::READ);
96
97 file_handle.Read (&header, sizeof (PCX_HEADER));
98
99 if (header.id != 10 && header.version != 5 && header.pixelsize != 8 ) return NULL ;
100
101 width = header.width - header.x + 1;
102 height = header.height - header.y + 1;
103
104 if (Buff != NULL) {
105 i = Size / width;
106 height = MIN ((int)(i - 1), (int)height);
107 Buffer b(Buff, Size);
108 pic = W3DNEW BSurface(width, height, 1, &b);
109 if (pic == NULL) return NULL ;
110 } else {
111 pic = W3DNEW BSurface(width, height, 1);
112 if (pic == NULL) return NULL ;
113 }
114
115 buffer = (char *)pic->Lock();
116 if (buffer != NULL) {
117 file_ptr = pool ;
118 file_handle.Read (pool, POOL_SIZE);
119
120 if ( header.byte_per_line != width ) {
121
122 i = 0;
123 rle = 0;
124 for ( scan_pos = j = 0 ; j < height ; j ++, scan_pos += width ) {
125 for ( i = 0 ; i < width ; ) {
126 rle = READ_CHAR ();
127 if ( rle > 192 ) {
128 rle -= 192 ;
129 color = READ_CHAR (); ;
130 memset ( buffer + scan_pos + i, color, rle );
131 i += rle;
132 } else {
133 *(buffer+scan_pos + i++ ) = (char)rle;
134 }
135 }
136 }
137
138 if ( i == width ) rle = READ_CHAR ();
139 if ( rle > 192 ) READ_CHARx();
140
141 } else {
142
143 for ( i = 0 ; i < width * height ; ) {
144 rle = READ_CHAR ();
145 rle &= 0xff;
146 if ( rle > 192 ) {
147 rle -= 192 ;
148 color = READ_CHAR ();
149 memset ( buffer + i, color, rle );
150 i += rle ;
151 } else {
152 *(buffer + i++) = (char)rle;
153 }
154 }
155 }
156 pic->Unlock();
157 }
158
159 if ( palette ) {
160 file_handle.Seek (- (256 * (int)sizeof(RGB)), SEEK_END );
161 file_handle.Read (palette, 256L * sizeof ( RGB ));
162 }
163
164 file_handle.Close();
165 return pic;
166}
167
168
#define NULL
Definition BaseType.h:92
#define SEEK_END
Definition WWFILE.H:57
#define MIN(a, b)
Definition always.h:189
#define W3DNEW
Definition always.h:109
virtual void * Lock(Point2D point=Point2D(0, 0)) const
Definition bsurface.h:62
Definition BUFF.H:57
virtual int Seek(int pos, int dir=SEEK_CUR)=0
virtual bool Is_Available(int forced=false)=0
virtual int Read(void *buffer, int size)=0
virtual void Close(void)=0
virtual int Open(char const *filename, int rights=READ)=0
virtual bool Unlock(void) const
Definition xsurface.h:93
Surface * Read_PCX_File(FileClass &file_handle, PaletteClass *palette, void *Buff, long Size)
Definition pcx.cpp:79
#define READ_CHARx()
Definition pcx.cpp:73
#define POOL_SIZE
Definition pcx.cpp:67
#define READ_CHAR()
Definition pcx.cpp:68
char pixelsize
Definition pcx.H:57
unsigned short byte_per_line
Definition pcx.H:67
char version
Definition pcx.H:55
short y
Definition pcx.H:59
short width
Definition pcx.H:60
short height
Definition pcx.H:61
short x
Definition pcx.H:58
char id
Definition pcx.H:54
Definition pcx.H:46