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
|
/*
* FIG : Facility for Interactive Generation of figures
* Copyright (c) 1992 by Brian Boyter
* Parts Copyright (c) 1991 by Paul King
* Parts Copyright (c) 1996 by Brian V. Smith
*
* The X Consortium, and any party obtaining a copy of these files from
* the X Consortium, directly or indirectly, 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 subject to the restriction stated
* below, and to permit persons who receive copies from any such party to
* do so, with the only requirement being that this copyright notice remain
* intact.
* This license includes without limitation a license to do the foregoing
* actions under any patents of the party supplying this software to the
* X Consortium.
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "fig2dev.h"
#include "object.h"
#include "pcx.h"
extern FILE *open_picfile();
extern void close_picfile();
int _read_pcx();
void readpcxhead();
int
read_pcx(pic)
F_pic *pic;
{
int status;
status = _read_pcx(pic);
if (status != 1)
return 0;
pic->hw_ratio = (float) pic->bit_size.y / pic->bit_size.x;
pic->subtype = P_PCX;
return 1;
}
/* _read_pcx() is called from read_pcx() and read_epsf().
The latter is because the output of ghostscript is to a PCX
file (actually a pipe).
*/
/* the filetype should always be 0 (file, not pipe) because we need to seek() */
/* since this is from the output of ghostscript, it is not a problem */
void pcx_decode();
_read_pcx(pic)
F_pic *pic;
{
FILE *pcxfile;
int filtype;
pcxheadr pcxhead; /* PCX header */
unsigned short wid; /* Width of image */
unsigned short ht; /* Height of image */
unsigned char *buffer; /* current input line */
int i, bufsize;
/* Read the PCX image file header information */
if ((pcxfile = open_picfile(pic->file, &filtype)) == NULL) {
close_picfile(pcxfile,filtype);
return 0;
}
/* read the header */
(void) readpcxhead(&pcxhead, pcxfile);
/* Check for FILE stream error */
if (ferror(pcxfile)) {
close_picfile(pcxfile,filtype);
return 0;
}
/* Check the identification byte value */
if (pcxhead.id != 0x0A) {
return 0;
}
/* copy the EGA palette now in case there is no VGA palette later in the file */
for (i=0; i<16; i++) {
pic->cmap[0][i] = (unsigned short) pcxhead.egapal[i*3];
pic->cmap[1][i] = (unsigned short) pcxhead.egapal[i*3+1];
pic->cmap[2][i] = (unsigned short) pcxhead.egapal[i*3+2];
}
pic->numcols = 16;
/* Calculate size of image in pixels and scan lines */
wid = pcxhead.xmax - pcxhead.xmin + 1;
ht = pcxhead.ymax - pcxhead.ymin + 1;
/* put in the width/height now in case there is some other failure later */
pic->bit_size.x = wid;
pic->bit_size.y = ht;
/* allocate space for the image */
bufsize = pcxhead.bppl * (wid+sizeof(unsigned char)-1) *
ht / sizeof(unsigned char);
if ((pic->bitmap = (unsigned char*) malloc(bufsize)) == NULL) {
close_picfile(pcxfile,filtype);
return 0; /* couldn't alloc space for image */
}
buffer = pic->bitmap;
switch (pcxhead.bppl) {
case 1:
case 8:
pcx_decode(pcxfile, buffer, bufsize, pcxhead.bppl, &pcxhead, wid, ht);
break;
default:
fprintf(stderr,"Unsupported PCX format in %s",pic->file);
free(pic->bitmap);
close_picfile(pcxfile,filtype);
return 0;
}
/* See if there is a VGA palette; read it into the pic->cmap */
if (pcxhead.vers == 5) {
fseek(pcxfile, -769L, SEEK_END); /* backwards from end of file */
if (getc(pcxfile) == 0x0C) { /* VGA Palette ID value */
for (i = 0; i < 256; i++) {
pic->cmap[0][i] = getc(pcxfile);
pic->cmap[1][i] = getc(pcxfile);
pic->cmap[2][i] = getc(pcxfile);
}
pic->numcols = 256; /* for a VGA colormap */
}
}
close_picfile(pcxfile,filtype);
return 1;
}
unsigned short
getwrd(file)
FILE *file;
{
unsigned char c1;
c1 = getc(file);
return (unsigned short) (c1 + (unsigned char) getc(file)*256);
}
void
readpcxhead(head, pcxfile)
pcxheadr *head;
FILE *pcxfile;
{
register unsigned short i;
head->id = getc(pcxfile);
head->vers = getc(pcxfile);
head->format = getc(pcxfile);
head->bppl = getc(pcxfile);
head->xmin = getwrd(pcxfile);
head->ymin = getwrd(pcxfile);
head->xmax = getwrd(pcxfile);
head->ymax = getwrd(pcxfile);
head->hdpi = getwrd(pcxfile);
head->vdpi = getwrd(pcxfile);
/* Read the EGA Palette */
for (i = 0; i < sizeof(head->egapal); i++)
head->egapal[i] = getc(pcxfile);
head->reserv = getc(pcxfile);
head->nplanes = getc(pcxfile);
head->blp = getwrd(pcxfile);
head->palinfo = getwrd(pcxfile);
head->hscrnsiz = getwrd(pcxfile);
head->vscrnsiz = getwrd(pcxfile);
/* Read the reserved area at the end of the header */
for (i = 0; i < sizeof(head->fill); i++)
head->fill[i] = getc(pcxfile);
}
void
pcx_decode(file, image, bufsize, planes, header, w, h)
FILE *file;
unsigned char *image;
int bufsize, planes;
pcxheadr *header;
int w,h;
{
int row, bcnt, bpl, pd;
int i, j, b, cnt;
unsigned char mask, plane, pmsk;
unsigned char *oimage;
/* clear area first */
bzero((char*)image,bufsize);
bpl = header->blp;
if (planes == 1)
pd = (bpl * 8) - w;
else
pd = bpl - w;
row = bcnt = 0;
plane = 0;
pmsk = 1;
oimage = image;
while ( (b=getc(file)) != EOF) {
if ((b & 0xC0) == 0xC0) { /* this is a repitition count */
cnt = b & 0x3F;
b = getc(file);
if (b == EOF) {
getc(file);
return;
}
} else
cnt = 1;
for (i=0; i<cnt; i++) {
if (planes == 1) {
for (j=0, mask=0x80; j<8; j++) {
*image++ |= (unsigned char) (((b & mask) ? pmsk : 0));
mask = mask >> 1;
}
} else {
*image++ = (unsigned char) b;
}
bcnt++;
if (bcnt == bpl) { /* end of a scan line */
bcnt = 0;
plane++;
if (plane >= (int) header->nplanes) { /* go to the next row */
plane = 0;
image -= pd;
oimage = image;
row++;
if (row >= h) {
return; /* done */
}
} else { /* new plane, same row */
image = oimage;
}
pmsk = 1 << plane;
}
}
}
}
|