Category : Printer Utilities
Archive   : CIP.ZIP
Filename : PCX.C

 
Output of file : PCX.C contained in archive : CIP.ZIP
/*
Filename : cip1.c
Author : Richard Simpson
Date : 8/31/1990
Project : Convert pcx file to VDS-2000 Printer format

NOTE : Compressed image will be used without further checking
*/

/* Default header files */
#include
#include
#include
#include

/* PCX image header file */
#include "pcx.h"

/* Default constants */
#define PCX_IMGWIDTH 640 /* Image width for default pcx image */
#define PCX_IMGHEIGHT 480 /* Image height for default pcx image */
#define PCX_REC_SIZE 128 /* Image record size for pcx image */
#define PALETTE_SIZE 768 /* Palette size for default pcx image */
#define PALETTE_ID_CHAR 0x0c /* Palette ID character */

#define REPEAT_PATTERN 0xc0 /* Repeat data pattern */

#define NULL 0
/* Set string for the printer. */
static char *INIT[] =
{
"\x1b\x54\x34\x9a\x01\xc0\x03\x7d\x00\x00\x05"
};
/* Form feed for color plane separation */
static char *ff[] =
{
"\x0c"
};


/* Global variables */
int Pwidth; /* Image width variable */
int Pheight; /* Image length variable */
int Ppsize; /* Palette size variable */
int rec_size; /* Image record sizevariable */
long pal_offset; /* Palette offset of end of file variable */
long image_offset; /* Image offset of start of file variable */
int file_handle; /* Handle to the input file */
int rfile_handle; /* Handle to the red image file */
int gfile_handle; /* Handle to the green image file */
int bfile_handle; /* Handle to the blue image file */
int width_bytes; /* image width in bytes */
int line_count;
long byte_size; /* Number of bytes request */



/* Filename buffer */
char ifile[80]; /* input file name */
char ifile_full[80]; /* input file name with extension */
char rfile[80]; /* red image file name */
char gfile[80]; /* green image file name */
char bfile[80]; /* blue image file name */

/* Palette bufer */
char palette[PALETTE_SIZE]; /* Palette */
char pal_red[PALETTE_SIZE/3]; /* Red palette buffer */
char pal_green[PALETTE_SIZE/3]; /* Green palette buffer */
char pal_blue[PALETTE_SIZE/3]; /* Blue palette buffer */

/* Temp red, green and blue buffer */
char red_buf[PCX_IMGWIDTH*2];
char green_buf[PCX_IMGWIDTH*2];
char blue_buf[PCX_IMGWIDTH*2];

/*
... Main program starts here
*/
int main(argc,argv)
int argc;
char *argv[];
{
int file_handle; /* file descriptor */
int i,j;
unsigned repeat_count;
unsigned line_bytes, bytes_cnt;
unsigned char idx;
int height;
char id_char;
char ch;

if(valid_parm(argc,argv)) {
printf("usage: %s infile.pcx\n",argv[0]);
exit(1);
}

if((file_handle = open_pcx_file(ifile_full)) == -1) {
printf("Cannot Open '%s' for reading\n",ifile);
printf("Job Aborted\n");
exit(2);
}

/*
... Load image information: use default
*/
Ppsize = PALETTE_SIZE ;
pal_offset = (long) (-(PALETTE_SIZE + 1));
rec_size = PCX_REC_SIZE;
image_offset = (long) rec_size;

/*
... Read image header
*/
read(file_handle, (char *)&header_rec, rec_size);
Pwidth = header_rec.xmax - header_rec.xmin + 1;
Pheight = header_rec.ymax - header_rec.ymin + 1;
line_bytes = header_rec.bytes_per_line;
/*
DEBUG
printf("%d %d %d\n", Pwidth, Pheight, line_bytes);
*/
/*
... Search image palette
*/
if (lseek(file_handle, pal_offset, SEEK_END) == -1)
{
printf("Search palette error on reading\n",ifile_full);
}

/* Read palette ID character */
read(file_handle, &id_char, 1);

/* Check ID character */
if (id_char != PALETTE_ID_CHAR)
{
printf("Palette ID character error on reading\n",ifile_full);
}

/*
... Read image palette
*/
if (read(file_handle,palette, PALETTE_SIZE) == -1)
{
printf("Palette error on reading\n",ifile_full);
}

/*
DEBUG
for (i = 0; i <24; i++)
printf("%c %d", palette[i], (unsigned char) palette[i]);
*/
/* Split palette into red, green and blue */
for (i = 0,j = 0; i < PALETTE_SIZE/3; i++, j = j+3)
{
pal_red[i] = palette[j];
pal_green[i] = palette[j+1];
pal_blue[i] = palette[j+2];
}
/*
DEBUG
for (i = 0; i < 8; i++)
printf("%d %d %d\n", (unsigned char) pal_red[i],
(unsigned char) pal_green[i], (unsigned char) pal_blue[i]);
*/

/*
... Search image data
*/
if (lseek(file_handle, image_offset, SEEK_SET) == -1)
{
printf("Image data error on reading\n",ifile_full);
}

/*
... Create "girl" type image data files
... Red file extension .R
... Green file extension .G
... Blue file extension .B

strcpy(rfile,ifile);
strcat(rfile,".R");
if((rfile_handle = create_girl_file(rfile)) == -1) {
printf("Cannot Create '%s' for writing\n",rfile);
printf("Job Aborted\n");
exit(2);
}
strcpy(gfile,ifile);
strcat(gfile,".G");
if((gfile_handle = create_girl_file(gfile)) == -1) {
printf("Cannot Create '%s' for writing\n",gfile);
printf("Job Aborted\n");
exit(2);
}
strcpy(bfile,ifile);
strcat(bfile,".B");
if((bfile_handle = create_girl_file(bfile)) == -1) {
printf("Cannot Create '%s' for writing\n",bfile);
printf("Job Aborted\n");
exit(2);
}
*/
write(4,*INIT,11);

/*
... Read image data and convert to printer type data
... One line a time for the BLUE COLOR PLANE....
*/
for (height = 0; height < Pheight; height++)
{
bytes_cnt = 0;
while(bytes_cnt < line_bytes*2)
{
read(file_handle, &ch, 1);
if ((ch & REPEAT_PATTERN) == REPEAT_PATTERN)
{
/* Repeate image data bytes */
repeat_count = ((unsigned char) ch) &
(unsigned) (~(unsigned char) REPEAT_PATTERN);

/* Get repeat data */
read(file_handle, &ch, 1);
idx = (unsigned char) ch;

/* Write to red, green and blue buffer */
setmem(&blue_buf[bytes_cnt], repeat_count*2, ~pal_blue[idx]);
bytes_cnt += repeat_count*2;
}

else
{
/* Normal image data */
idx = (unsigned char) ch;
/* Write to blue buffer */
blue_buf[bytes_cnt] = ~pal_blue[idx];
blue_buf[bytes_cnt+1] = ~pal_blue[idx];
bytes_cnt += 2;
}
} /* End of WHITE statement */
/*
DEBUG
printf("height = %d, bytes_cnt = %d\n", height, bytes_cnt);
*/
/*
... Fill zero's on the end of line to make default resolution
*/
setmem(&blue_buf[bytes_cnt], PCX_IMGWIDTH*2 - bytes_cnt, ~pal_blue[0]);
/*
... Write blue buffer to Yelow plane of printer
*/
write(4,blue_buf, PCX_IMGWIDTH*2);
write(4,blue_buf, PCX_IMGWIDTH*2);
} /* End of FOR statement */

/*
... Fill zero's on the end of vertical line to make default resolution
*/
for (i = Pheight; i < PCX_IMGHEIGHT; i++)
{
setmem(&blue_buf, PCX_IMGWIDTH*2, ~pal_blue[0]);
write(4,blue_buf, PCX_IMGWIDTH*2);
write(4,blue_buf, PCX_IMGWIDTH*2);
/*
DEBUG
printf("height = %d\n", i);
*/

}
write(4,*ff,1);
if (lseek(file_handle, image_offset, SEEK_SET) == -1)
{
printf("Image data error on reading\n",ifile_full);
}

/*
... Read image data and convert to printer type data
... One line a time for GREEN COLOR DATA
*/
for (height = 0; height < Pheight; height++)
{
bytes_cnt = 0;
while(bytes_cnt < line_bytes*2)
{
read(file_handle, &ch, 1);
if ((ch & REPEAT_PATTERN) == REPEAT_PATTERN)
{
/* Repeate image data bytes */
repeat_count = ((unsigned char) ch) &
(unsigned) (~(unsigned char) REPEAT_PATTERN);

/* Get repeat data */
read(file_handle, &ch, 1);
idx = (unsigned char) ch;

/* Write to green buffer */
setmem(&green_buf[bytes_cnt], repeat_count*2, ~pal_green[idx]);
bytes_cnt += repeat_count*2;
}

else
{
/* Normal image data */
idx = (unsigned char) ch;
/* Write to red, green and green buffer */
green_buf[bytes_cnt] = ~pal_green[idx];
green_buf[bytes_cnt+1] = ~pal_green[idx];
bytes_cnt += 2;
}
} /* End of WHITE statement */
/*
DEBUG
printf("height = %d, bytes_cnt = %d\n", height, bytes_cnt);
*/
/*
... Fill zero's on the end of line to make default resolution
*/
setmem(&green_buf[bytes_cnt], PCX_IMGWIDTH*2 - bytes_cnt, ~pal_green[0]);
/*
... Write green buffer to magenta plane on the printer
*/
write(4,green_buf, PCX_IMGWIDTH*2);
write(4,green_buf, PCX_IMGWIDTH*2);
} /* End of FOR statement */

/*
... Fill zero's on the end of vertical line to make default resolution
*/
for (i = Pheight; i < PCX_IMGHEIGHT; i++)
{
setmem(&green_buf, PCX_IMGWIDTH*2, ~pal_green[0]);
write(4,green_buf, PCX_IMGWIDTH*2);
write(4,green_buf, PCX_IMGWIDTH*2);
/*
DEBUG
printf("height = %d\n", i);
*/

}
write(4,*ff,1);
if (lseek(file_handle, image_offset, SEEK_SET) == -1)
{
printf("Image data error on reading\n",ifile_full);
}

/*
... Read image data and convert to printer type data
... One line at a time for the RED color
*/
for (height = 0; height < Pheight; height++)
{
bytes_cnt = 0;
while(bytes_cnt < line_bytes*2)
{
read(file_handle, &ch, 1);
if ((ch & REPEAT_PATTERN) == REPEAT_PATTERN)
{
/* Repeate image data bytes */
repeat_count = ((unsigned char) ch) &
(unsigned) (~(unsigned char) REPEAT_PATTERN);

/* Get repeat data */
read(file_handle, &ch, 1);
idx = (unsigned char) ch;

/* Write to red buffer */
setmem(&red_buf[bytes_cnt], repeat_count*2, ~pal_red[idx]);
bytes_cnt += repeat_count*2;
}

else
{
/* Normal image data */
idx = (unsigned char) ch;
/* Write to red, green and red buffer */
red_buf[bytes_cnt] = ~pal_red[idx];
red_buf[bytes_cnt+1] = ~pal_red[idx];
bytes_cnt += 2;
}
} /* End of WHITE statement */
/*
DEBUG
printf("height = %d, bytes_cnt = %d\n", height, bytes_cnt);
*/
/*
... Fill zero's on the end of line to make default resolution
*/
setmem(&red_buf[bytes_cnt], PCX_IMGWIDTH*2 - bytes_cnt, ~pal_red[0]);
/*
... Write red buffer to the cyan plane
*/
write(4,red_buf, PCX_IMGWIDTH*2);
write(4,red_buf, PCX_IMGWIDTH*2);
} /* End of FOR statement */

/*
... Fill zero's on the end of vertical line to make default resolution
*/
for (i = Pheight; i < PCX_IMGHEIGHT; i++)
{
setmem(&red_buf, PCX_IMGWIDTH*2, ~pal_red[0]);
write(4,red_buf, PCX_IMGWIDTH*2);
write(4,red_buf, PCX_IMGWIDTH*2);
/*
DEBUG
printf("height = %d\n", i);
*/

}
write(4,*ff,1);

/*
... Close files

close(file_handle);
close(rfile_handle);
close(gfile_handle);
close(bfile_handle);
*/
}

/* valid_parm - parses the passed parameters. loads ifile */
/* no file extension is needed */

valid_parm(argc,argv)
int argc;
char *argv[];
{

if(argc < 1 || argc >2) {
return(1);
}

strcpy(ifile,argv[1]);
strcpy(ifile_full,argv[1]);
strcat(ifile_full,".pcx");

return(0);
}


/* open pcx file - nothing fancy here, just open the pcx file with the
* standard ms-dos file opens
*/

int open_pcx_file(infilename)
char *infilename;
{

return(open(infilename,O_RDONLY|O_BINARY));
}

/* create girl file - nothing fancy here, just create the girl file with the
* standard ms-dos file opens


int create_girl_file(outfilename)
char *outfilename;
{

return(open(outfilename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY));
}
*/


  3 Responses to “Category : Printer Utilities
Archive   : CIP.ZIP
Filename : PCX.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/