File: pcx.c

package info (click to toggle)
xli 1.17.0+20061110-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,540 kB
  • sloc: ansic: 25,840; makefile: 11
file content (254 lines) | stat: -rw-r--r-- 6,004 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
** pcx.c - load a ZSoft PC Paintbrush (PCX) file for use inside xli
**
**	Tim Northrup <tim@BRS.Com>
**	Adapted from code by Jef Poskanzer (see Copyright below).
**
**	Version 0.1 --  4/25/91 -- Initial cut
**	Version 0.2 -- 2001-12-15 -- Support 8bit color images with palette.
**				     (Alexandre Duret-Lutz <duret_g@epita.fr>)
**
**  Copyright (c) 1991 Tim Northrup
**	(see file "tgncpyrght.h" for complete copyright information)
*/

/*
** Copyright (C) 1988 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
**
** This program (pcxtopbm) is based on the pcx2rf program by:
**   Mike Macgirvin
**   Stanford Relativity Gyro Program GP-B
**   Stanford, Calif. 94503
**   ARPA: mike@relgyro.stanford.edu
*/

#include "tgncpyrght.h"
#include "xli.h"
#include "imagetypes.h"

#define PCX_MAGIC 0x0a			/* first byte in a PCX image file */

/* Routine to load a PCX file */
static boolean PCX_LoadImage(ZFILE *zf, int in_bpr, int img_bpr,
			     Image *image, int rows, boolean readpal);


/*
**  pcxIdent
**
**	Identify passed file as a PC Paintbrush image or not
**
**	Returns 1 if file is a PCX file, 0 otherwise
*/

int pcxIdent(char *fullname, char *name)
{
	ZFILE *zf;
	int  ret;
	int xmin;
	int xmax;
	int ymin;
	int ymax;
	unsigned char pcxhd[128];

	ret = 0;

	if (! (zf = zopen(fullname))) {
		perror("pcxIdent");
		return(0);
	}

	/* Read .pcx header. */
	if (zread(zf,pcxhd,128) == 128) {
		if (pcxhd[0] == PCX_MAGIC) {
			/* Calculate raster header and swap bytes. */
			xmin = pcxhd[4] + ( 256 * pcxhd[5] );
			ymin = pcxhd[6] + ( 256 * pcxhd[7] );
			xmax = pcxhd[8] + ( 256 * pcxhd[9] );
			ymax = pcxhd[10] + ( 256 * pcxhd[11] );
			xmax = xmax - xmin + 1;
			ymax = ymax - ymin + 1;

			printf("%s is a %dx%d PC Paintbrush image\n",
				name,xmax,ymax);
			ret = 1;
			}
		}

	zclose(zf);
	return(ret);
}


/*
**  pcxLoad
**
**	Load PCX Paintbrush file into an Image structure.
**
**	Returns pointer to allocated struct if successful, NULL otherwise
*/

Image *pcxLoad (char *fullname, ImageOptions *image_ops, boolean verbose)
{
	ZFILE *zf;
	char *name = image_ops->name;
	unsigned char pcxhd[128];
	int xmin;
	int xmax;
	int ymin;
	int ymax;
	int in_bpr;		/* in input format bytes per row */
	int img_bpr;	/* xli image format bytes per row */
	register Image *image;

	/* Open input file. */
	if ( ! (zf = zopen(fullname))) {
		perror("pcxLoad");
		return((Image *)NULL);
	}

	/* Read .pcx header. */
	if (zread(zf,pcxhd,128) != 128) {
		zclose(zf);
		return((Image *)NULL);
		}

	if ((pcxhd[0] != PCX_MAGIC) || (pcxhd[1] > 5)) {
		zclose(zf);
		return((Image *)NULL);
		}

	/* Calculate raster header and swap bytes. */
	xmin = pcxhd[4] + ( 256 * pcxhd[5] );
	ymin = pcxhd[6] + ( 256 * pcxhd[7] );
	xmax = pcxhd[8] + ( 256 * pcxhd[9] );
	ymax = pcxhd[10] + ( 256 * pcxhd[11] );
	xmax = xmax - xmin + 1;
	ymax = ymax - ymin + 1;
	in_bpr = pcxhd[66] + ( 256 * pcxhd[67] );
	img_bpr = (xmax * pcxhd[3] + 7)/8; /* Recompute the number of bytes
					      per lines.  */

	/* double check image */
	if (xmax < 0 || ymax < 0 || in_bpr < img_bpr) {
		zclose(zf);
		return((Image *)NULL);
	}

	if (verbose)
		printf("%s is a %dx%d PC Paintbrush image\n",name,xmax,ymax);

	/* Only monochorme or 8bits paletized images are supported.  */
	if (pcxhd[65] > 1 || !(pcxhd[3] == 1 || pcxhd[3] == 8)) {
		fprintf(stderr,
			"pcxLoad: %s - Unsupported PCX type\n",
			name);
		zclose(zf);
		return((Image *)NULL);
	}

	znocache(zf);

	/* Allocate image. */
	if (pcxhd[3] == 1)
	  image = newBitImage(xmax, ymax);
	else
	  image = newRGBImage(xmax, ymax, pcxhd[3]);
	image->title = dupString(name);

	/* Read compressed bitmap. */
	if (!PCX_LoadImage(zf, in_bpr, img_bpr, image, ymax, pcxhd[3] == 8)) {
		fprintf(stderr,"pcxLoad: %s - Short read of PCX file\n",name);
		zclose(zf);
		return(image);
	}

	read_trail_opt(image_ops,zf,image,verbose);
	zclose(zf);
	return(image);
}


/*
**  PCX_LoadImage
**
**	Load PC Paintbrush file into the passed Image structure.
**
**	Returns FALSE if there was a short read.
*/

static boolean PCX_LoadImage (ZFILE *zf, int in_bpr, int img_bpr,
			      Image *image, int rows, boolean readpal)
{
	/* Goes like this: Read a byte.  If the two high bits are set,
	** then the low 6 bits contain a repeat count, and the byte to
	** repeat is the next byte in the file.  If the two high bits are
	** not set, then this is the byte to write.
	*/

	register unsigned char *ptr;
	int row = 0;
	int bytes_this_row = 0;
	int b, i, cnt;
	/* For binary image we need to reverse all bits.  */
	int xor_mask = (image->type == IBITMAP) ? 0xff : 0;

	ptr = &(image->data[0]);

	while (row < rows && (b = zgetc(zf)) != EOF) {

		if ((b & 0xC0) == 0xC0) {
			/* have a repetition count -- mask flag bits */
			cnt = b & 0x3F;
			b = zgetc(zf);
			if (b == EOF)
				return FALSE;
			}
		else {
			cnt = 1;		/* no repeating this one */
			}

		b ^= xor_mask;

		for ( i = 0; i < cnt; i++ ) {
			if ( row >= rows )
				break;
			if (bytes_this_row < img_bpr)
				*ptr++ = (unsigned char) b;
			if (++bytes_this_row == in_bpr) {
				/* start of a new line */
				row++;
				bytes_this_row = 0;
				}
			}
		}
	/* Read a palette if needed.  */
	if (readpal) {
		/* The palette is separated from the pixels data by dummy
		   byte equal to 12.  */
		if ((b = zgetc(zf)) == EOF || b != 12)
			return FALSE;

		for (cnt = 0; cnt < 256; ++cnt) {
			int r, g, b;
			if ((r = zgetc(zf)) == EOF
			    || (g = zgetc(zf)) == EOF
			    || (b = zgetc(zf)) == EOF)
				return FALSE;
			image->rgb.red[cnt] = r << 8;
			image->rgb.green[cnt] = g << 8;
			image->rgb.blue[cnt] = b << 8;
		}
		image->rgb.used = 256;
	}


	return TRUE;
}