File: readpcx.c

package info (click to toggle)
fig2dev 1:3.2.7a-5+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,556 kB
  • sloc: ansic: 35,809; sh: 5,401; makefile: 177; perl: 13; csh: 12
file content (310 lines) | stat: -rw-r--r-- 7,290 bytes parent folder | download | duplicates (2)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Fig2dev: Translate Fig code to various Devices
 * Parts Copyright (c) 1992 by Brian Boyter
 * Parts Copyright (c) 1992-2007 by Brian V. Smith
 * Parts Copyright (c) 2015-2017 by Thomas Loimer
 *
 * Any party obtaining a copy of these files is granted, free of charge, a
 * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
 * nonexclusive right and license to deal in this software and documentation
 * files (the "Software"), including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense and/or sell copies
 * of the Software, and to permit persons who receive copies from any such
 * party to do so, with the only requirement being that the above copyright
 * and this permission notice remain intact.
 *
 */

/*
 * readpcx.c: import pcx into PostScript
 * Copyright (c) 1992 by Brian Boyter
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "fig2dev.h"
#include "object.h"	/* does #include <X11/xpm.h> */
#include "colors.h"	/* rgb2luminance() */

extern int _read_pcx(FILE *pcxfile, F_pic *pic);

/* return codes:  1 : success
		  0 : invalid file
*/

int
read_pcx(FILE *file, int filetype, F_pic *pic, int *llx, int *lly)
{
	(void)	filetype;

	*llx = *lly = 0;
	pic->transp = -1;
	return _read_pcx(file, pic);
}

/* pcx2ppm 1.2 - convert pcx to ppm
 * based on zgv's readpcx.c
 * public domain by RJM
 *
 * this is incredibly messy
 *
 * 1999-03-16	updated to support 24-bit.
 */

typedef unsigned char byte;

struct pcxhed
  {
  byte manuf,ver,encod,bpp;		/* 0 - 3 gen format */
  byte x1lo,x1hi,y1lo,y1hi;		/* 4 - 11 size */
  byte x2lo,x2hi,y2lo,y2hi;
  byte unused1[4];			/* 12 - 15 scrn size */
  byte pal16[48];			/* 16 - 63 4-bit palette */
  byte reserved;			/* 64 reserved */
  byte nplanes;				/* 65 num of bitplanes */
  byte bytelinelo,bytelinehi;		/* 66 - 67 bytes per line */
  byte unused2[60];			/* 68 - 127 unused */
  };  /* palette info is after image data */


/* prototypes */
void dispbyte(unsigned char *ptr,int *xp,int *yp,int c,int w,int h,
              int real_bpp,int byteline,int *planep,int *pmaskp);


/* _read_pcx() is called from read_pcx(), read_gif(), read_ppm(),
   read_tif() and read_epsf().
 */

int
_read_pcx(FILE *pcxfile, F_pic *pic)
{
	int		 w,h,bytepp,x,y,yy,byteline,plane,pmask;
	struct pcxhed	 header;
	int		 count;
	/* long		 bytemax,bytesdone; obviously not used */
	byte		 inbyte,inbyte2;
	int		 real_bpp;		/* how many bpp file really is */

	fprintf(tfp, "%% Begin Imported PCX File: %s\n\n", pic->file);
	pic->subtype = P_PCX;

	fread(&header,1,sizeof(struct pcxhed),pcxfile);
	if (header.manuf!=10 || header.encod!=1)
	    return 0;

	/* header.bpp=1, header.nplanes=1 = 1-bit.
	 * header.bpp=1, header.nplanes=2 = 2-bit.   - added B.V.Smith 6/00
	 * header.bpp=1, header.nplanes=3 = 3-bit.   - added B.V.Smith 6/00
	 * header.bpp=1, header.nplanes=4 = 4-bit.
	 * header.bpp=8, header.nplanes=1 = 8-bit.
	 * header.bpp=8, header.nplanes=3 = 24-bit.
	 * anything else gives an `unsupported' error.
	 */
	real_bpp=0;
	bytepp = 1;
	switch(header.bpp) {
	  case 1:
	    switch(header.nplanes) {
	      case 1: real_bpp=1; break;
	      case 2: real_bpp=2; break;
	      case 3: real_bpp=3; break;
	      case 4: real_bpp=4; break;
	    }
	    break;

	  case 8:
	    switch(header.nplanes) {
	      case 1: real_bpp=8; break;
	      case 3: real_bpp=24; bytepp = 3; break;
	    }
	    break;
	  }

	if (!real_bpp)
	    return 0;

	w=(header.x2lo+256*header.x2hi)-(header.x1lo+256*header.x1hi)+1;
	h=(header.y2lo+256*header.y2hi)-(header.y1lo+256*header.y1hi)+1;
	byteline=header.bytelinelo+256*header.bytelinehi;

	if (w==0 || h==0)
	    return 0;

	x=0; y=0;
	/* bytemax=w*h;
	if (real_bpp==1 || real_bpp==4)
	    bytemax=(1<<30); */	/* we use a 'y<h' test instead for these files */

	if ((pic->bitmap=malloc(w*(h+2)*bytepp))==NULL)
	    return 0;

	/* need this if more than one bitplane */
	memset(pic->bitmap,0,w*h*bytepp);

	/* bytesdone=0;	*/

	/* start reading image */
	for (yy=0; yy<h; yy++) {
	  plane=0;
	  pmask=1;

	  y=yy;
	  x=0;
	  while (y==yy) {
	    inbyte=fgetc(pcxfile);
	    if (inbyte<192) {
	      dispbyte(pic->bitmap,&x,&y,inbyte,w,h,real_bpp,
		byteline,&plane,&pmask);
	      /* bytesdone++;	*/
	    } else {
	      inbyte2=fgetc(pcxfile);
	      inbyte&=63;
	      for (count=0; count<inbyte; count++)
		dispbyte(pic->bitmap,&x,&y,inbyte2,w,h,real_bpp,
			byteline,&plane,&pmask);
	      /* bytesdone+=inbyte; */
	    }
	  }
	}

	/* read palette */
	switch(real_bpp) {
	    case 1:
		pic->cmap[RED][0] = pic->cmap[GREEN][0] = pic->cmap[BLUE][0] = 0;
		pic->cmap[RED][1] = pic->cmap[GREEN][1] = pic->cmap[BLUE][1] = 255;
		pic->numcols = 2;
		break;

	    case 2:
	    case 3:
	    case 4:
		/* 2-,3-, and 4-bit, palette is embedded in header */
		pic->numcols = (1<<real_bpp);
		for (x=0; x < pic->numcols; x++) {
		    pic->cmap[RED][x] = header.pal16[x*3  ];
		    pic->cmap[GREEN][x] = header.pal16[x*3+1];
		    pic->cmap[BLUE][x] = header.pal16[x*3+2];
		    /* if user wants grayscale (-N) then map to gray */
		    if (grayonly)
			pic->cmap[RED][x] = pic->cmap[GREEN][x] = pic->cmap[BLUE][x] =
			    (int) (rgb2luminance(pic->cmap[RED][x]/255.0,
						pic->cmap[GREEN][x]/255.0,
						pic->cmap[BLUE][x]/255.0)*255.0);
		}
		break;

	    case 8:
		/* 8-bit */
		(void) fgetc(pcxfile);		/* ditch splitter byte */
		for (x=0; x<256; x++) {
		    pic->cmap[RED][x] = fgetc(pcxfile);
		    pic->cmap[GREEN][x] = fgetc(pcxfile);
		    pic->cmap[BLUE][x] = fgetc(pcxfile);
		    /* if user wants grayscale (-N) then map to gray */
		    if (grayonly)
			pic->cmap[RED][x] = pic->cmap[GREEN][x] = pic->cmap[BLUE][x] =
			    (int) (rgb2luminance(pic->cmap[RED][x]/255.0,
						pic->cmap[GREEN][x]/255.0,
						pic->cmap[BLUE][x]/255.0)*255.0);
		}
		pic->numcols = 256;
		break;

	    case 24:
		/* no palette, set flag in numcols to write out rgb values */
		pic->numcols = 2<<24;
		break;
	}

	pic->bit_size.x = w;
	pic->bit_size.y = h;

	return 1;
}


void
dispbyte(unsigned char *ptr,int *xp,int *yp,int c,int w,int h,
              int real_bpp,int byteline,int *planep,int *pmaskp)
{
	int f;
	unsigned char *dstptr;

	switch(real_bpp) {
	  case 1:
	  case 2:
	  case 3:
	  case 4:
		/* mono or 4-bit */

		if ((*yp)>=h)
		    return;

		dstptr=ptr+(*yp)*w+*xp;
		w=byteline*8;

		for (f=0; f<8; f++) {
		   *dstptr++|=(c&(0x80>>(f&7)))?(*pmaskp):0;
		   (*xp)++;
		   if (*xp>=w) {
			if (real_bpp==1) {
			    (*xp)=0,(*yp)++;
			    return;
			}
			(*xp)=0;
			(*planep)++;
			(*pmaskp)<<=1;
			if (real_bpp==2) {
			    if (*planep==2) {
				(*yp)++;
				return;
			    }
			} else if (real_bpp==3) {
			    if (*planep==3) {
				(*yp)++;
				return;
			    }
			} else {
			    /* otherwise, it's 4 bpp */
			    if (*planep==4) {
				(*yp)++;
				return;
			    }
			}
		    }
		    if ((*yp)>=h)
			return;
		}
		break;

	  case 8:
		*(ptr+(*yp)*w+*xp)=c;
		(*xp)++;
		if (*xp>=w) {
		    (*xp)=0;
		    (*yp)++;
		}
		break;

	  case 24:
		*(ptr+((*yp)*w+*xp)*3+(2-(*planep)))=c;
		(*xp)++;
		if (*xp>=w) {
		    (*xp)=0;
		    (*planep)++; /* no need to change pmask */
		    if (*planep==3) {
			(*yp)++;
			return;
		    }
		}
		break;
	  }
}